/*********************************************************************************************************//**
 * @file    BMS33M332_HT32/src/BMS33M332.h
 * @version V1.0.1
 * @date    2024-08-08
 * @brief   The header file of BMS33M332 driver.
 *************************************************************************************************************
 * @attention
 *
 * Firmware Disclaimer Information
 *
 * 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
 *    code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
 *    proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
 *    other intellectual property laws.
 *
 * 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
 *    code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
 *    other than HOLTEK and the customer.
 *
 * 3. The program technical documentation, including the code, is provided "as is" and for customer reference
 *    only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
 *    the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
 *    the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
 *
 * <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
 ************************************************************************************************************/
/* Define to prevent recursive inclusion -------------------------------------------------------------------*/
#ifndef __BMS33M332_H
#define __BMS33M332_H

#ifdef __cplusplus
 extern "C" {
#endif

/* Includes ------------------------------------------------------------------------------------------------*/
#include "ht32.h"
#include "i2c_master_config.h"
#include "ht32_board_config.h"
#include "bmduino_check.h"

#include "middleware/i2c_master.h"

/* Settings ------------------------------------------------------------------------------------------------*/
/* Exported typedef ----------------------------------------------------------------------------------------*/
typedef enum {
  BMS33M332_SUCCESS,
  BMS33M332_FAILURE
} BMS33M332_selStatus;
/* Exported constants --------------------------------------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------------------------------------*/
#define BMS33M332_MASTERADDR     0x47
#define BMS33M332_CLKFREQ        100000

#define BMS33M332_OK                0x01
#define BMS33M332_ERROR             0x02

/*State Register*/
#define BMS33M332_EN_PS             0x00
#define BMS33M332_EN_ALS            0x01
#define BMS33M332_EN_WAIT           0x02
#define BMS33M332_EN_INTELLI_WAIT   0x03
#define BMS33M332_EN_CTAUTOK        0x04
/*LED constant current setting*/
#define BMS33M332_CURRENT_3_125MA   0x00
#define BMS33M332_CURRENT_6_25MA    0x01
#define BMS33M332_CURRENT_12_5MA    0x02
#define BMS33M332_CURRENT_25MA      0x03
#define BMS33M332_CURRENT_50MA      0x04
#define BMS33M332_CURRENT_100MA     0x05
#define BMS33M332_CURRENT_150MA     0x06
/*PS integration time*/
#define BMS33M332_IT_PS_96US        0x00
#define BMS33M332_IT_PS_192US       0x01
#define BMS33M332_IT_PS_384US       0x02
#define BMS33M332_IT_PS_768US       0x03
#define BMS33M332_IT_PS_1_54MS      0x04
#define BMS33M332_IT_PS_3_07MS      0x05
#define BMS33M332_IT_PS_6_14MS      0x06
/*GAIN_PS*/
#define BMS33M332_GAIN_PS_x1        0x00     
#define BMS33M332_GAIN_PS_x2        0x01  
#define BMS33M332_GAIN_PS_x4        0x02  
#define BMS33M332_GAIN_PS_x8        0x03 
/*PS Persistence Setting*/
#define BMS33M332_PRST_PS_x1        0x00
#define BMS33M332_PRST_PS_x2        0x01
#define BMS33M332_PRST_PS_x4        0x02
#define BMS33M332_PRST_PS_x16       0x03

/*ALS integration time*/
#define BMS33M332_IT_ALS_25MS       0x00
#define BMS33M332_IT_ALS_50MS       0x01
#define BMS33M332_IT_ALS_100MS      0x02
#define BMS33M332_IT_ALS_200MS      0x03
#define BMS33M332_IT_ALS_400MS      0x04
#define BMS33M332_IT_ALS_800MS      0x05
#define BMS33M332_IT_ALS_1600MS     0x06
/*GAIN_ALS*/
#define BMS33M332_GAIN_ALS_x1       0x00 
#define BMS33M332_GAIN_ALS_x4       0x01 
#define BMS33M332_GAIN_ALS_x16      0x02 
#define BMS33M332_GAIN_ALS_x64      0x03 
/*ALS Persistence Setting*/
#define BMS33M332_PRST_ALS_x1       0x00
#define BMS33M332_PRST_ALS_x2       0x01
#define BMS33M332_PRST_ALS_x4       0x02
#define BMS33M332_PRST_ALS_x8       0x03   
/*GAIN_C*/
#define BMS33M332_GAIN_C_x1         0x00 
#define BMS33M332_GAIN_C_x4         0x01 
#define BMS33M332_GAIN_C_x16        0x02 
#define BMS33M332_GAIN_C_x64        0x03 

#define BMS33M332_ENABLE            1
#define BMS33M332_DISABLE           0
/*STK3332 REGS*/
#define BMS33M332_STATE_REG                 0x00
#define BMS33M332_PSCTRL_REG                0x01
#define BMS33M332_ALSCTRL_REG               0x02
#define BMS33M332_LEDCTRL_REG               0x03
#define BMS33M332_INTCTRL1_REG              0x04
#define BMS33M332_WAIT_REG                  0x05
#define BMS33M332_THDH1_PS_REG              0x06
#define BMS33M332_THDH2_PS_REG              0x07
#define BMS33M332_THDL1_PS_REG              0x08
#define BMS33M332_THDL2_PS_REG              0x09
#define BMS33M332_THDH1_ALS_REG             0x0A
#define BMS33M332_THDH2_ALS_REG             0x0B
#define BMS33M332_THDL1_ALS_REG             0x0C
#define BMS33M332_THDL2_ALS_REG             0x0D
#define BMS33M332_FLAG_REG                  0x10
#define BMS33M332_DATA1_PS_REG              0x11
#define BMS33M332_DATA2_PS_REG              0x12
#define BMS33M332_DATA1_ALS_REG             0x13
#define BMS33M332_DATA2_ALS_REG             0x14
#define BMS33M332_DATA1_C_REG               0x1B
#define BMS33M332_DATA2_C_REG               0x1C
#define BMS33M332_DATA1_PS_OFFSET_REG       0x1D
#define BMS33M332_DATA2_PS_OFFSET_REG       0x1E
#define BMS33M332_DATA_CTIR1_REG            0x20
#define BMS33M332_DATA_CTIR2_REG            0x21
#define BMS33M332_DATA_CTIR3_REG            0x22
#define BMS33M332_DATA_CTIR4_REG            0x23
#define BMS33M332_PDT_ID_REG                0x3E
#define BMS33M332_RSRVD_REG                 0x3F
#define BMS33M332_ALSCTRL2_REG              0x4E
#define BMS33M332_INTELLI_WAIT_PS_REG       0x4F
#define BMS33M332_SOFT_RESET_REG            0x80
#define BMS33M332_INTCTRL2_REG              0xA5

/*Clear FLAG Register*/
#define BMS33M332_Clr_FLG_NF()                writeRegBit(FLAG_REG, 0, 0); //bit0
#define BMS33M332_Clr_FLG_INVALID_PS_INT()    writeRegBit(FLAG_REG, 1, 0); //bit1
#define BMS33M332_Clr_FLG_ALS_SAT()           writeRegBit(FLAG_REG, 2, 0); //bit2
#define BMS33M332_Clr_FLG_PS_INT()            writeRegBit(FLAG_REG, 4, 0); //bit4
#define BMS33M332_Clr_FLG_ALS_INT()           writeRegBit(FLAG_REG, 5, 0); //bit5
#define BMS33M332_Clr_FLG_PS_DR()             writeRegBit(FLAG_REG, 6, 0); //bit6
#define BMS33M332_Clr_FLG_ALS_DR()            writeRegBit(FLAG_REG, 7, 0); //bit7
/* Exported variables --------------------------------------------------------------------------------------*/
/* Exported functions --------------------------------------------------------------------------------------*/
BMS33M332_selStatus BMS33M332_selWire(uint32_t wire_number);
void BMS33M332_Init(void);

uint16_t BMS33M332_readRawProximity(void);
uint16_t BMS33M332_readRawAmbient(void);
float BMS33M332_readAmbient(void);
uint8_t BMS33M332_getPDTID(void);
void BMS33M332_setINT(uint16_t thdh,uint16_t thdl,bool isEnable);
uint8_t BMS33M332_getINT(void);
uint8_t BMS33M332_getPositionStatus(void);
void BMS33M332_reset(void);

void BMS33M332_writeReg(uint8_t addr, uint8_t data);
uint8_t BMS33M332_readReg(uint8_t addr);
void BMS33M332_readMultipleReg(uint8_t addr, uint8_t rBuf[], uint8_t rLen);
   
uint8_t BMS33M332_getLEDcurrent(void);
uint8_t BMS33M332_getMeasureIntervalTime(void);
uint16_t BMS33M332_getPSHighThreshold(void);
uint16_t BMS33M332_getPSLowThreshold(void);
uint16_t BMS33M332_getALSHighThreshold(void);
uint16_t BMS33M332_getALSLowThreshold(void);
   
void BMS33M332_setLEDcurrent(uint8_t current);
void BMS33M332_setMeasureIntervalTime(uint8_t time,bool isEnable);
void BMS33M332_setPSHighThreshold(uint16_t thdh);
void BMS33M332_setPSLowThreshold(uint16_t thdl);
void BMS33M332_setALSHighThreshold(uint16_t thdh);
void BMS33M332_setALSLowThreshold(uint16_t thdl);
   
/*private*/
uint16_t BMS33M332_readClearChannelValue(void);
void BMS33M332_setALSIntegrationTime(uint8_t time);
void BMS33M332_setPSIntegrationTime(uint8_t time);
void BMS33M332_setALSClearChannelGain(uint8_t gain);
void BMS33M332_setPSGain(uint8_t gain);
void BMS33M332_setALSGain(uint8_t gain);
void BMS33M332_setPSIntelligentPersistence(uint8_t time,bool isEnable);
void BMS33M332_setALSIntelligentPersistence(uint8_t time,bool isEnable);
void BMS33M332_setPSOffset(uint16_t offset);
uint16_t BMS33M332_getPSOffset(void);

void BMS33M332_writeBytes(uint8_t wbuf[], uint8_t wlen);
void BMS33M332_writeRegBit(uint8_t addr,uint8_t bitNum, uint8_t bitValue);
void BMS33M332_readBytes(uint8_t rbuf[], uint8_t rlen);

void BMS33M332_clearBuf(void);
void _BMS33M332_delay(vu32 count);
#ifdef __cplusplus
}
#endif

#endif

