MAX30102的开源库非常多,移植到MAX32625上,主要是编写他的几个接口函数,这一篇将详细介绍如何实现。
【工程文件添加】
在github下找到一个开源的MAX30102的驱动,在驱中有MAX30100.c、blood.c、algorithm.c文件以及他的.h,将他添加到Keil工程中:
【IIC接口文件适配】
1、IIC接口文件在max30100.c中,IIC_Write_Byte、IIC_Read_Byte、IIC_Read_Array为基础接口。移植就是修改这三个函数。
2、Max32625可以实现IIC硬件驱动,由于本次使用为与OLED共用的I2C0(max32625只引出来了mxc_I2C0),因此硬件的初始化不需要在这里提供,先初始化OLED就可以了。
硬件I2C读写的接口在i2cm.c中实现,函数原型为:
/** * @defgroup i2cm_blocking I2CM Blocking Functions * @{ */ /** * @brief Read I2CM data. Will block until transaction is complete. * * @note Command is an optional feature where the master will write the @c * cmd_data before reading from the slave. If command is undesired, * set the @c *cmd_data parameter to NULL and pass 0 for the @c * cmd_len parameter. * @note If there is a command, the master will send a repeated start * sequence before attempting to read from the slave. * @note This function blocks until the transaction has completed. * * @param i2cm Pointer to the I2CM registers, see #mxc_i2cm_regs_t. * @param addr I2C address of the slave. * @param cmd_data Data to write before reading. * @param cmd_len Number of bytes to write before reading. * @param data Where to store the data read. * @param len Number of bytes to read. * * @return Number of bytes read if successful, error code if unsuccessful. */ int I2CM_Read(mxc_i2cm_regs_t *i2cm, uint8_t addr, const uint8_t *cmd_data, uint32_t cmd_len, uint8_t* data, uint32_t len); /** * @brief Write data to a slave device. * * @note Command is an optional feature where the master will write the @c * cmd_data before writing the @c data to the slave. If command is * not needed, set the @c cmd_data to @c NULL and set @c cmd_len to * 0. If there is a command, the master will send a repeated start * sequence before attempting to read from the slave. * @note This function blocks until the transaction has completed. * * @param i2cm Pointer to the I2CM registers, see #mxc_i2cm_regs_t. * @param addr I2C address of the slave. * @param cmd_data Data to write before writing data. * @param cmd_len Number of bytes to write before writing data. * @param data Data to be written. * @param len Number of bytes to Write. * * @return Number of bytes writen if successful or an @ref MXC_Error_Codes * "Error Code" if unsuccessful. */ int I2CM_Write(mxc_i2cm_regs_t *i2cm, uint8_t addr, const uint8_t *cmd_data, uint32_t cmd_len, uint8_t* data, uint32_t len); /**@} end of i2cm_blocking functions */
3、具体实现代码如下:
uint8_t IIC_Write_Byte(uint8_t device_addr,uint8_t register_addr,uint8_t data) { uint8_t send_data[2]; send_data[0] = register_addr; send_data[1] = data; return I2CM_Write(MXC_I2CM0, device_addr, NULL, 0, send_data, 2); } uint8_t IIC_Read_Byte(uint8_t device_addr,uint8_t register_addr) { uint8_t data; I2CM_Read(MXC_I2CM0, device_addr, ®ister_addr, 1, &data, 1); return data; } uint8_t IIC_Read_Array(uint8_t device_addr,uint16_t register_addr,uint8_t *Data,uint16_t Num) { uint8_t data[Num*2]; uint8_t i=0; uint8_t addr; addr = (uint8_t)register_addr; I2CM_Read(MXC_I2CM0, device_addr, &addr, 1, data, Num*2); while(Num) { *Data= data[i*2]<<8 | data[i*2+1]; Data ++; Num--; } }
【测试】
修改好上面三个接口函数,在main中执行复位max30102,然后进行配置:max30102_config,再在大循环里执行blood_loop就行实现Max30102的读取与分析。