|
楼主 |
发表于 2020-7-20 16:54:23
|
显示全部楼层
您好,引脚配置是对的,读写寄存器的地址也没问题,更改TWI的配置做了更改依然还是卡在那里。
代码是驱动max30102的,根据在github找了两个NRF52调max30102的代码改动了一下,这是驱动代码。请您帮忙看看- #ifndef AT24C02_H__
- #define AT24C02_H__
- #include "nrf_delay.h"
- //I2C引脚定义
- #define TWI_SCL_M 25 //I2C SCL引脚
- #define TWI_SDA_M 27 //I2C SDA引脚
- #define max30102_INT 8
- #define max30102_ADDRESS 0x57
- #define max30102_WR_address 0xAE
- #define I2C_WRITE_ADDR (0xAE)
- #define I2C_READ_ADDR (0xAF)
- #define REG_INTR_STATUS_1 0x00
- #define REG_INTR_STATUS_2 0x01
- #define REG_INTR_ENABLE_1 0x02
- #define REG_INTR_ENABLE_2 0x03
- #define REG_FIFO_WR_PTR 0x04
- #define REG_OVF_COUNTER 0x05
- #define REG_FIFO_RD_PTR 0x06
- #define REG_FIFO_DATA 0x07
- #define REG_FIFO_CONFIG 0x08
- #define REG_MODE_CONFIG 0x09
- #define REG_SPO2_CONFIG 0x0A
- #define REG_LED1_PA 0x0C
- #define REG_LED2_PA 0x0D
- #define REG_PILOT_PA 0x10
- #define REG_MULTI_LED_CTRL1 0x11
- #define REG_MULTI_LED_CTRL2 0x12
- #define REG_TEMP_INTR 0x1F
- #define REG_TEMP_FRAC 0x20
- #define REG_TEMP_CONFIG 0x21
- #define REG_PROX_INT_THRESH 0x30
- #define REG_REV_ID 0xFE
- #define REG_PART_ID 0xFF
- void twi_master_init(void);
- bool max30102_register_write(uint8_t register_address, uint8_t value);
- bool max30102_register_read(uint8_t register_address, uint8_t * destination, uint8_t number_of_bytes);
- void max30102_fifo_read (void);
- void max30102_setup (void);
- void max30102_reset (void);
- void max30102_init (void);
- #endif
- #include <stdbool.h>
- #include <stdint.h>
- #include <string.h>
- #include "nrf_drv_twi.h"
- //#include "twi_master.h"
- //#include "nrf_drv_timer.h"
- #include "max30102.h"
- #include "SEGGER_RTT.h"
- //TWI驱动程序实例ID,ID和外设编号对应,0:TWI0 1:TWI1
- #define TWI_INSTANCE_ID 0
- //TWI传输完成标志
- static volatile bool m_xfer_done = false;
- //定义TWI驱动程序实例,名称为m_twi
- static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
- //TWI事件处理函数
- void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
- {
- //判断TWI事件类型
- switch (p_event->type)
- {
- //传输完成事件
- case NRF_DRV_TWI_EVT_DONE:
- m_xfer_done = true;//置位传输完成标志
- break;
- default:
- break;
- }
- }
- //TWI初始化
- void twi_master_init(void)
- {
- ret_code_t err_code;
- //定义并初始化TWI配置结构体
- const nrf_drv_twi_config_t twi_config = {
- .scl = TWI_SCL_M, //定义TWI SCL引脚
- .sda = TWI_SDA_M , //定义TWI SDA引脚
- .frequency = NRF_DRV_TWI_FREQ_400K, //TWI速率
- .interrupt_priority = APP_IRQ_PRIORITY_HIGH, //TWI优先级
- .clear_bus_init = true//初始化期间不发送9个SCL时钟
- };
- //初始化TWI
- err_code = nrf_drv_twi_init(&m_twi, &twi_config, twi_handler, NULL);
- //检查返回的错误代码
- APP_ERROR_CHECK(err_code);
- //使能TWI
- nrf_drv_twi_enable(&m_twi);
- }
- bool max30102_register_write(uint8_t register_address, uint8_t value)
- {
- ret_code_t err_code;
- uint8_t tx_buf[2];
-
- //准备写入的数据
- tx_buf[0] = register_address;
- tx_buf[1] = value;
- //TWI传输完成标志设置为false
- m_xfer_done = false;
- //写入数据
- err_code = nrf_drv_twi_tx(&m_twi, max30102_ADDRESS, tx_buf, sizeof(tx_buf), false);
- //等待TWI总线传输完成
- while (m_xfer_done == false){}
- if (NRF_SUCCESS != err_code)
- {
- return false;
- }
- return true;
- }
- bool max30102_register_read(uint8_t register_address, uint8_t * destination, uint8_t number_of_bytes)
- {
- ret_code_t err_code;
- //TWI传输完成标志设置为false
- m_xfer_done = false;
- err_code = nrf_drv_twi_tx(&m_twi, max30102_ADDRESS, ®ister_address, 1, true);
- //等待TWI总线传输完成
- while (m_xfer_done == false){}
- if (NRF_SUCCESS != err_code)
- {
- return false;
- }
- //TWI传输完成标志设置为false
- m_xfer_done = false;
- err_code = nrf_drv_twi_rx(&m_twi, max30102_ADDRESS, destination, number_of_bytes);
- //等待TWI总线传输完成
- while (m_xfer_done == false){}
- if (NRF_SUCCESS != err_code)
- {
- return false;
- }
- return true;
- }
- void max30102_setup (void)
- {
-
-
- // max30102_register_write(REG_MODE_CONFIG,0x40); //reset
- // max30102_register_write(REG_MODE_CONFIG,0x40); //reset
-
- (void)max30102_register_write(REG_INTR_ENABLE_1, 0xc0);
- (void)max30102_register_write(REG_INTR_ENABLE_2, 0x00);
-
-
- (void)max30102_register_write(REG_FIFO_WR_PTR, 0x00); // Reset FIFO Write pointer
- SEGGER_RTT_printf(0,"115\n");
- (void)max30102_register_write(REG_OVF_COUNTER, 0x00); // Reset OVERFLOW_CTR pointer
- (void)max30102_register_write(REG_FIFO_RD_PTR, 0x00); // Reset FIFO READ pointer
-
- (void)max30102_register_write(REG_FIFO_CONFIG, 0x4F); //average sampling = 4
- (void)max30102_register_write(REG_MODE_CONFIG, 0x03); // ADC range = 4096nA | Sampling Rate = 100 Hz | LED pulseWidth = 411uS
-
- (void)max30102_register_write(REG_SPO2_CONFIG, 0x27); // 400 samples per second
- (void)max30102_register_write(REG_LED1_PA, 0x5F); // LED1 current = 19 mA
- (void)max30102_register_write(REG_LED2_PA, 0x5F); // LED2 current = 19 mA
- (void)max30102_register_write(REG_PILOT_PA, 0x7F); // Pilot LED ~ 25mA
-
-
- }
- void max30102_reset (void)
- {
-
- max30102_register_write(REG_MODE_CONFIG, 0x40);
-
- }
- void max30102_init (void)
- {
- uint8_t revID;
- uint8_t partID;
- ret_code_t err_code;
- err_code = max30102_register_read (REG_PART_ID, &partID,sizeof(partID));
- APP_ERROR_CHECK(err_code);
-
- printf("MAX30102 PART ID: 0x%02x \r\n", partID);
-
-
- err_code = max30102_register_read (REG_REV_ID, &revID,sizeof(revID));
- APP_ERROR_CHECK(err_code);
-
- printf("MAX30102 REV ID: 0x%02x \r\n", revID);
- }
- void max30102_fifo_read ()
- {
- uint8_t clearINT1;
- uint8_t clearINT2;
- uint32_t LEDdata=0;
- uint8_t dataArray[6];
- ret_code_t err_code;
- // uint8_t FIFO_data;
-
- //Clear Interrupts
- err_code = max30102_register_read(REG_INTR_STATUS_1, &clearINT1,sizeof(clearINT1));
- err_code = max30102_register_read(REG_INTR_STATUS_2, &clearINT2,sizeof(clearINT2));
-
-
- err_code =max30102_register_read(REG_FIFO_DATA,dataArray,sizeof(dataArray));
- APP_ERROR_CHECK(err_code);
-
- //Extract 3 bytes for IR LED
- LEDdata=dataArray[0]<<16|dataArray[1]<<8 |dataArray[2];
-
- printf("%d,",LEDdata) ; // RED LED data
- LEDdata=0;
- //Extract 3 bytes for RED LED
- LEDdata=dataArray[3]<<16|dataArray[4]<<8 |dataArray[5];
- printf("%d\r\n", LEDdata); // IR LED data
-
-
-
-
- }
复制代码 |
|