Hello,
I am trying to implement a Heart Rate Monitor Interface (HRMI) board with my nRF52, using the TWI_sensor example from SDK 12.2. I am running into trouble Writing to commands to the peripheral I2C address. Here is the driver I have written to do so:
#define HRMI_I2C_ADDR1 127
#define HRMI_SET_MODE 0x53
#define HRMI_GET_HR_DATA 0x47
ret_code_t HRMI_set_mode() {
ret_code_t ret_code;
uint8_t command_address2 = HRMI_SET_MODE; // <0x53>
ret_code = nrf_drv_twi_tx(&m_twi, HRMI_I2C_ADDR, &command_address2, 1, false);
return ret_code;}
ret_code_t start_HR() {
ret_code_t ret_code;
uint8_t command_address = HRMI_GET_HR_DATA; // <0x47>
ret_code = nrf_drv_twi_tx(&m_twi, HRMI_I2C_ADDR, &command_address, 3, false);
return ret_code;}
ret_code_t fetch_HR(uint8_t * hr){
ret_code_t ret_code;
uint8_t returned_over_I2C[3]; //Array to hold returned data
int data;
ret_code = nrf_drv_twi_rx(&m_twi, HRMI_I2C_ADDR, returned_over_I2C, sizeof(returned_over_I2C));
nrf_delay_ms(10);
*hr = returned_over_I2C[2];
return ret_code;}
int main(void){
ret_code_t err_code;
uint8_t hr;
/* Initializing TWI master */
err_code = twi_master_init();
HRMI_set_mode(); //write command <0x53> to I2C address to set heart rate algorithm mode
APP_ERROR_CHECK(err_code);
while (true)
{
nrf_delay_ms(500);
start_HR(); //write command <0x47> to I2C address to Get Heart Rate Data
nrf_delay_ms(500);
fetch_HR(&hr);
SEGGER_RTT_printf(0, "Heart Rate: ");
SEGGER_RTT_printf(0, "%d\n", hr);}}
My code is not working and I believe I know why. The HRMI datasheet lists the I2C commands in a different way than I've seen using the nRF52. For example, the I2C command to set the mode is <0x53><N>
, where is a 3-bit mask value where bit-place zero should be set to '1' to set average mode.
Additionally, the I2C command to Get Heart Rate Data is <0x47><N>
where <N>
is the number of HR values requested. I would like to access 1 value.
I believe I am not writing the correct values to the I2C register because of the format with these <N>
values. Currently, the nRF52 shows a *hr = returned_over_I2C[2];
value of ASCII "244", and this appears even if my SDA and SCL lines are disconnected from the nRF52.
My question is: How can I make sure my Write commands are for the command and the following arguments using the nrf_drv_twi_tx() function?
Thank you for your time.