123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237 |
- #include "max30102.h"
- #include "iot_errno.h"
- #include "iot_gpio.h"
- #include "iot_gpio_ex.h"
- #include "iot_i2c.h"
- #include "iot_i2c_ex.h"
- #include <stdio.h>
- #define WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA 6
- #define WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL 6
- #define WIFI_IOT_I2C_IDX_1 1
- #define WIFI_IOT_IO_NAME_GPIO_7 7
- #define WIFI_IOT_IO_NAME_GPIO_8 8
- #define WIFI_IOT_IO_NAME_GPIO_0 0
- #define WIFI_IOT_IO_NAME_GPIO_1 1
- #define MAX30102_INIT_GPIO 7
- /***************************************************************
- * 函数名称: GpioInit
- * 说 明: GPIO初始化
- * 参 数: 无
- * 返 回 值: 无
- ***************************************************************/
- void BoardInit(void)
- {
- // IoTGpioInit(WIFI_IOT_IO_NAME_GPIO_8);
- // IoTGpioSetFunc(WIFI_IOT_IO_NAME_GPIO_8, WIFI_IOT_IO_FUNC_GPIO_8_GPIO);
- // IoTGpioSetDir(WIFI_IOT_IO_NAME_GPIO_8, IOT_GPIO_DIR_OUT); // 设置GPIO_8为输出模式
- IoTGpioInit(MAX30102_INIT_GPIO);
- IoTGpioSetFunc(MAX30102_INIT_GPIO, IOT_GPIO_FUNC_GPIO_7_GPIO);
- IoTGpioSetDir(MAX30102_INIT_GPIO, IOT_GPIO_DIR_IN); // 设置GPIO_7为输出模式
- IoTGpioInit(WIFI_IOT_IO_NAME_GPIO_0);
- IoTGpioSetFunc(WIFI_IOT_IO_NAME_GPIO_0, WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA); // GPIO_0复用为I2C1_SDA
- IoTGpioInit(WIFI_IOT_IO_NAME_GPIO_1);
- IoTGpioSetFunc(WIFI_IOT_IO_NAME_GPIO_1, WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL); // GPIO_1复用为I2C1_SCL
- IoTI2cInit(WIFI_IOT_I2C_IDX_1, 400000); /* baudrate: 400kbps */
- }
- uint8_t GetInit(void)
- {
- IotGpioValue temp = {0};
- IoTGpioGetInputVal(MAX30102_INIT_GPIO,&temp);
- return temp;
- }
- /**
- * \brief Write a value to a MAX30102 register
- * \par Details
- * This function writes a value to a MAX30102 register
- *
- * \param[in] uch_addr - register address
- * \param[in] uch_data - register data
- *
- * \retval true on success
- */
- static int maxim_max30102_write_reg(uint8_t uch_addr, uint8_t uch_data)
- {
- uint32_t ret;
- uint8_t send_data[2] = {uch_addr, uch_data};
- ret = IoTI2cWrite(WIFI_IOT_I2C_IDX_1, (MAX30102_SLAVE_ADDRESS << 1) | 0x00, send_data, 2);
- if (ret != 0) {
- printf("===== Error: I2C write ret = 0x%x! =====\r\n", ret);
- return -1;
- }
- return 0;
- }
- /**
- * \brief Read a MAX30102 register
- * \par Details
- * This function reads a MAX30102 register
- *
- * \param[in] uch_addr - register address
- * \param[out] puch_data - pointer that stores the register data
- *
- * \retval true on success
- */
- uint8_t maxim_max30102_read_reg(uint8_t uch_addr, uint8_t *puch_data)
- {
- uint32_t ret = 0;
- IotI2cData max30102_i2c_data = {0};
- uint8_t buffer[1] = {uch_addr};
- max30102_i2c_data.sendBuf = buffer;
- max30102_i2c_data.sendLen = 1;
- max30102_i2c_data.receiveBuf = puch_data;
- max30102_i2c_data.receiveLen = 1;
- ret = IoTI2cWriteread(WIFI_IOT_I2C_IDX_1, (MAX30102_SLAVE_ADDRESS << 1) | 0x00, &max30102_i2c_data);
- if (ret != 0) {
- printf("===== Error: I2C writeread ret = 0x%x! =====\r\n", ret);
- return -1;
- }
- return 0;
- }
- uint8_t maxim_max30102_init()
- {
-
- if(maxim_max30102_write_reg(REG_INTR_ENABLE_1,0xc0)!=IOT_SUCCESS) // INTR setting
- return 0;
- if(maxim_max30102_write_reg(REG_INTR_ENABLE_2,0x00)!=IOT_SUCCESS)
- return 0;
- if(maxim_max30102_write_reg(REG_FIFO_WR_PTR,0x00)!=IOT_SUCCESS) //FIFO_WR_PTR[4:0]
- return 0;
- if(maxim_max30102_write_reg(REG_OVF_COUNTER,0x00)!=IOT_SUCCESS) //OVF_COUNTER[4:0]
- return 0;
- if(maxim_max30102_write_reg(REG_FIFO_RD_PTR,0x00)!=IOT_SUCCESS) //FIFO_RD_PTR[4:0]
- return 0;
- if(maxim_max30102_write_reg(REG_FIFO_CONFIG,0x0f)!=IOT_SUCCESS) //sample avg = 1, fifo rollover=false, fifo almost full = 17
- return 0;
- if(maxim_max30102_write_reg(REG_MODE_CONFIG,0x03)!=IOT_SUCCESS) //0x02 for Red only, 0x03 for SpO2 mode 0x07 multimode LED
- return 0;
- if(maxim_max30102_write_reg(REG_SPO2_CONFIG,0x27)!=IOT_SUCCESS) // SPO2_ADC range = 4096nA, SPO2 sample rate (100 Hz), LED pulseWidth (400uS)
- return 0;
-
- if(maxim_max30102_write_reg(REG_LED1_PA,0x32)!=IOT_SUCCESS) //Choose value for ~ 7mA for LED1
- return 0;
- if(maxim_max30102_write_reg(REG_LED2_PA,0x32)!=IOT_SUCCESS) // Choose value for ~ 7mA for LED2
- return 0;
- if(maxim_max30102_write_reg(REG_PILOT_PA,0x7f)!=IOT_SUCCESS) // Choose value for ~ 25mA for Pilot LED
- return 0;
- return 1;
- }
- /**
- * \brief Reset the MAX30102
- * \par Details
- * This function resets the MAX30102
- *
- * \param None
- *
- * \retval true on success
- */
- uint8_t maxim_max30102_reset()
- {
- if(maxim_max30102_write_reg(REG_MODE_CONFIG,0x40)!=IOT_SUCCESS)
- return 0;
- else
- return 1;
- }
- /**
- * \brief Read a set of samples from the MAX30102 FIFO register
- * \par Details
- * This function reads a set of samples from the MAX30102 FIFO register
- *
- * \param[out] *pun_red_led - pointer that stores the red LED reading data
- * \param[out] *pun_ir_led - pointer that stores the IR LED reading data
- *
- * \retval true on success
- */
- uint8_t max30102_FIFO_ReadBytes(uint8_t Register_Address,uint8_t* Data)
- {
- uint32_t un_temp;
- unsigned char uch_temp;
-
- //read and clear status register
- maxim_max30102_read_reg(REG_INTR_STATUS_1, &uch_temp);
- maxim_max30102_read_reg(REG_INTR_STATUS_2, &uch_temp);
-
- // ach_i2c_data[0]=REG_FIFO_DATA;
-
- uint32_t ret = 0;
- IotI2cData max30102_i2c_data = {0};
- uint8_t buffer[1] = {REG_FIFO_DATA};
- max30102_i2c_data.sendBuf = buffer;
- max30102_i2c_data.sendLen = 1;
- max30102_i2c_data.receiveBuf = Data;
- max30102_i2c_data.receiveLen = 6;
- ret = IoTI2cWriteread(WIFI_IOT_I2C_IDX_1, (MAX30102_SLAVE_ADDRESS << 1) | 0x00, &max30102_i2c_data);
- if (ret != 0) {
- printf("===== Error: I2C writeread ret = 0x%x! =====\r\n", ret);
- return -1;
- }
- return 0;
- }
- /**
- * \brief Read a set of samples from the MAX30102 FIFO register
- * \par Details
- * This function reads a set of samples from the MAX30102 FIFO register
- *
- * \param[out] *pun_red_led - pointer that stores the red LED reading data
- * \param[out] *pun_ir_led - pointer that stores the IR LED reading data
- *
- * \retval true on success
- */
- uint8_t maxim_max30102_read_fifo(uint32_t *pun_red_led, uint32_t *pun_ir_led)
- {
- uint32_t un_temp;
- unsigned char uch_temp;
- *pun_red_led=0;
- *pun_ir_led=0;
- uint8_t ach_i2c_data[6];
-
- //read and clear status register
- maxim_max30102_read_reg(REG_INTR_STATUS_1, &uch_temp);
- maxim_max30102_read_reg(REG_INTR_STATUS_2, &uch_temp);
-
- // ach_i2c_data[0]=REG_FIFO_DATA;
-
- uint32_t ret = 0;
- IotI2cData max30102_i2c_data = {0};
- uint8_t buffer[1] = {REG_FIFO_DATA};
- max30102_i2c_data.sendBuf = buffer;
- max30102_i2c_data.sendLen = 1;
- max30102_i2c_data.receiveBuf = ach_i2c_data;
- max30102_i2c_data.receiveLen = 6;
- ret = IoTI2cWriteread(WIFI_IOT_I2C_IDX_1, (MAX30102_SLAVE_ADDRESS << 1) | 0x00, &max30102_i2c_data);
- if (ret != 0) {
- printf("===== Error: I2C writeread ret = 0x%x! =====\r\n", ret);
- return -1;
- }
- un_temp=(unsigned char) ach_i2c_data[0];
- un_temp<<=16;
- *pun_red_led+=un_temp;
- un_temp=(unsigned char) ach_i2c_data[1];
- un_temp<<=8;
- *pun_red_led+=un_temp;
- un_temp=(unsigned char) ach_i2c_data[2];
- *pun_red_led+=un_temp;
-
- un_temp=(unsigned char) ach_i2c_data[3];
- un_temp<<=16;
- *pun_ir_led+=un_temp;
- un_temp=(unsigned char) ach_i2c_data[4];
- un_temp<<=8;
- *pun_ir_led+=un_temp;
- un_temp=(unsigned char) ach_i2c_data[5];
- *pun_ir_led+=un_temp;
- *pun_red_led&=0x03FFFF; //Mask MSB [23:18]
- *pun_ir_led&=0x03FFFF; //Mask MSB [23:18]
- return 1;
- }
|