Hello community,
I have been recently working with AFE4490 sensor from protocentral (breakout board).
Previously I have ported multiple sensors for my hobby projects and never faced any issue but I am not able to initialize the registers with this sensor in nrf52840.
Here are few things I trieD:
1. I evaluated this sensor with ESP32 C3 module and the sensor LED turns ON after succesful intialization.
2. Ported the C++ code base to nrf52840 SPI peripheral example code and description is mentioned below:
3. Used SPI pin as mentioned below:
#define AFE_MISO_PIN 30//24
#define AFE_MOSI_PIN 29//23
#define AFE_SCK_PIN 28//25
#define AFE_CS_Pin 31//22
4. Initialization routine for SPI is mentioned below:
nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
spi_config.miso_pin = AFE_MISO_PIN;
spi_config.mosi_pin = AFE_MOSI_PIN;
spi_config.sck_pin = AFE_SCK_PIN;
spi_config.ss_pin = AFE_CS_Pin;
spi_config.mode = NRF_DRV_SPI_MODE_1;
spi_config.frequency = NRF_DRV_SPI_FREQ_8M;
//spi_config.irq_priority = APP_IRQ_PRIORITY_HIGH;
//spi_config.orc = 0x54; // fla
APP_ERROR_CHECK(nrf_drv_spi_init(&afe_spi, &spi_config, afe_spi_event_handler, NULL));
nrf_gpio_cfg_output(AFE_MOSI_PIN);
nrf_gpio_cfg_output(AFE_SCK_PIN);
nrf_gpio_cfg_output(AFE_CS_Pin);
nrf_gpio_cfg_output(NRF_GPIO_PIN_MAP(1,11)); //This is for power down pin in AFE4490, used to reset it by toggling GPIO before initialization of registers.
nrf_delay_ms(300);
5. Register initialization is mentioned below: (Taken from working example code of ESP32 - Attached link to it in the above section)
nrf_gpio_pin_clear(NRF_GPIO_PIN_MAP(1,11));
nrf_delay_ms(100);
nrf_gpio_pin_set(NRF_GPIO_PIN_MAP(1,11));
nrf_delay_ms(60);
//nrf_gpio_pin_toggle(NRF_GPIO_PIN_MAP(1,11));
////Example code
//DisableSPIRead();
afe44xxWrite(CONTROL0, 0x000000);
afe44xxWrite(CONTROL0, 0x000008);
afe44xxWrite(TIAGAIN, 0x000000); // CF = 5pF, RF = 500kR
afe44xxWrite(TIA_AMB_GAIN, 0x000001);
afe44xxWrite(LEDCNTRL, 0x001414);
afe44xxWrite(CONTROL2, 0x000000); // LED_RANGE=100mA, LED=50mA
afe44xxWrite(CONTROL1, 0x010707); // Timers ON, average 3 samples
afe44xxWrite(PRPCOUNT, 0X001F3F);
afe44xxWrite(LED2STC, 0X001770);
afe44xxWrite(LED2ENDC, 0X001F3E);
afe44xxWrite(LED2LEDSTC, 0X001770);
afe44xxWrite(LED2LEDENDC, 0X001F3F);
afe44xxWrite(ALED2STC, 0X000000);
afe44xxWrite(ALED2ENDC, 0X0007CE);
afe44xxWrite(LED2CONVST, 0X000002);
afe44xxWrite(LED2CONVEND, 0X0007CF);
afe44xxWrite(ALED2CONVST, 0X0007D2);
afe44xxWrite(ALED2CONVEND, 0X000F9F);
afe44xxWrite(LED1STC, 0X0007D0);
afe44xxWrite(LED1ENDC, 0X000F9E);
afe44xxWrite(LED1LEDSTC, 0X0007D0);
afe44xxWrite(LED1LEDENDC, 0X000F9F);
afe44xxWrite(ALED1STC, 0X000FA0);
afe44xxWrite(ALED1ENDC, 0X00176E);
afe44xxWrite(LED1CONVST, 0X000FA2);
afe44xxWrite(LED1CONVEND, 0X00176F);
afe44xxWrite(ALED1CONVST, 0X001772);
afe44xxWrite(ALED1CONVEND, 0X001F3F);
afe44xxWrite(ADCRSTCNT0, 0X000000);
afe44xxWrite(ADCRSTENDCT0, 0X000000);
afe44xxWrite(ADCRSTCNT1, 0X0007D0);
afe44xxWrite(ADCRSTENDCT1, 0X0007D0);
afe44xxWrite(ADCRSTCNT2, 0X000FA0);
afe44xxWrite(ADCRSTENDCT2, 0X000FA0);
afe44xxWrite(ADCRSTCNT3, 0X001770);
afe44xxWrite(ADCRSTENDCT3, 0X001770);
6. Ported write function is mentioned below:
uint8_t usTxAfeCmdWrite[30];
void afe44xxWrite (uint8_t address, uint32_t data)
{
memset(usTxAfeCmdWrite, 0 ,30);
usTxAfeCmdWrite[0] = address;
usTxAfeCmdWrite[1] = ((data >> 16) & 0xFF);
usTxAfeCmdWrite[2] = ((data >> 8) & 0xFF);
usTxAfeCmdWrite[3] = (data & 0xFF);
CS_OFF;
nrf_drv_spi_transfer(&afe_spi, usTxAfeCmdWrite, 4, 0, 0);
while (!AfebXferDone); //This will be true in the spi_handler function - basically the spi event check callback.
CS_ON;
}
Original working Arduino write function mentioned below:
void AFE44XX :: afe44xxWrite (uint8_t address, uint32_t data)
{
SPI.beginTransaction(SPI_SETTINGS);
digitalWrite (_cs_pin, LOW); // enable device
SPI.transfer (address); // send address to device
SPI.transfer ((data >> 16) & 0xFF); // write top 8 bits
SPI.transfer ((data >> 8) & 0xFF); // write middle 8 bits
SPI.transfer (data & 0xFF); // write bottom 8 bits
digitalWrite (_cs_pin, HIGH); // disable device
SPI.endTransaction();
}
Currently, I am more focused on register initialiozation. Once its success then I will proceed further with reading of SPo2 and heart rate values.
I am not sure where I am going wrong, I tried changing the modes to Mode 0,1,2,3 but nothing works. Its been a week now. Requesting somebody to help me on this.
Thank you.