This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF52840-DK with MAX30003 ECG Board

Hello,

I am trying to code nRF52840- DK with MAX30003 ECG Board Using SPI. I have referred Arduino Uno Code for MAX30003 ECG.

I need some help for Driver Development if anybody worked on this please share any info.

Thanks and Regards,

Rohit  

Parents Reply Children
  • I suggest you start with the nrfx SPI Master Example first, then if you want you can use the SPI Transaction Manager Example. The transaction manager is just another layer of abstraction useful when dealing with multiple slaves with different SPI configurations on the same bus. If you're only interfacing with this one sensor then there's no need for the SPI transaction manager.

    Se also the SPI master docs and SPIM API references. 

  • Hi Haakonsh,

    Thank you for your suggestion, I am using nRFX_spim Example now. 

    I have done REG read function for reading MAX30003ECG Register value, In function, I have to read SPI transmit and SPI Receive.

    I want to store Tx & Rx data in a CHAR_ARRAY but it is not storing in the SPI_temp_32b[i]. Please check it in below code

    void MAX30003_Reg_Read(uint8_t Reg_address)
    {

    nrf_gpio_pin_clear(NRFX_SPIM_SS_PIN);
    SPI_TX_Buff[1] = (Reg_address<<1 ) | RREG;

    // Please clarify below function correct or not //
    nrfx_spim_xfer_desc_t xfer_desc4 = NRFX_SPIM_XFER_TRX(SPI_TX_Buff[1], sizeof(SPI_TX_Buff[1]), SPI_RX_Buff, sizeof(SPI_TX_Buff[1]));

    for ( i = 0; i < 3; i++)
    {
    SPI_temp_32b[i]= NRFX_SPIM_XFER_TRX(A, sizeof(A), SPI_RX_Buff, sizeof(A));
    }

    nrf_gpio_pin_set(NRFX_SPIM_SS_PIN);
    }

    Regards,

    Rohit

  • I have to read SPI transmit and SPI Receive.

    That makes no sense!

    Transmit is what you send - so why do you need to read it??

    Use the 'Insert Code' feature to get properly formatted source code in your post:

  • From MAX30003 spec:

    The device is programmed and accessed by a 32 cycle SPI instruction framed by a CSB low interval. The content of the SPI operation consists of a one byte command word (comprised of a seven bit address and a Read/Write mode indicator, i.e., A[6:0] + R/W) followed by a three-byte data word.

    Read mode operations will access the requested data on the 8th SCLK rising edge, and present the MSB of the requested data on the following SCLK falling edge, allowing the µC to sample the data MSB on the 9th SCLK rising edge. Configuration, Status, and FIFO data are all available via normal mode read back sequences. If more than 32 SCLK rising edges are provided in a normal read sequence then the excess edges will be ignored and the device will read back zeros.

    uint8_t SPI_TX_Buff[4]; // 32 bit transfers
    
    void MAX30003_Reg_Read(uint8_t Reg_address)
    {
        nrfx_err_t err_code = NRF_SUCSESS;
    
    // nrf_gpio_pin_clear(NRFX_SPIM_SS_PIN); The driver should handle the SS pin automatically.
    
    //Prepare the tx buffer
    memset(SPI_TX_Buff[0], 0x00, sizeof(SPI_TX_Buff)); // Erases the buffer
    memset(SPI_RX_Buff[0], 0x00, sizeof(SPI_TX_Buff)); // Erases the buffer
    SPI_TX_Buff[0] = (Reg_address<<1 ) | RREG; // should be SPI_TXBuff[0], I assume RREG = 0x1;
    
    //Create the transfer descriptor
    nrfx_spim_xfer_desc_t xfer_desc4 = NRFX_SPIM_XFER_TRX(SPI_TX_Buff[0], sizeof(SPI_TX_Buff), SPI_RX_Buff[0], sizeof(SPI_TX_Buff));
    
    //Initiate the transfer
    err_code = nrfx_spim_xfer(&Your_SPIM_Instance, &xfer_desc4, 0);
    
    // nrf_gpio_pin_set(NRFX_SPIM_SS_PIN); The driver should handle the SS pin automatically.
    }
    
    // When the SPIM event handler is called with the NRFX_SPIM_EVENT_DONE event, the SPI_RX_Buff[1] to 3 will contain the received data.



    Also, the first thing you'll want to do is to read the INFO register in order to verify that your SPI protocol is working, however the MAXIM device requires you to read from or write to any other register before you can read the INFO register: 

    INFO (0x0F) INFO is a read-only register that provides information about the MAX30003. The first nibble contains an alternating bit pattern to aide in interface verification. The second nibble contains the revision ID. The third nibble includes part ID information. The final 3 nibbles contain a serial number for Maxim internal use

    —note that individual units are not given unique serial numbers, and these bits should not be used as serial numbers for end products, though they may be useful during initial development efforts. Note: due to internal initialization procedures, this command will not read-back valid data if it is the first command executed following either a power-cycle event, or a SW_RST event.

    You should also scope the SPI communication with a digital logical analyzer. I suggest a Saleae Logic 8 or similar devices. 

  • #include "nrfx_spim.h"
    #include "app_util_platform.h"
    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    #include "boards.h"
    #include "app_error.h"
    #include <string.h>
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    #include "MAX30003.h"
    
    #define NRFX_SPIM_SCK_PIN  3
    #define NRFX_SPIM_MOSI_PIN 4
    #define NRFX_SPIM_MISO_PIN 28
    #define NRFX_SPIM_SS_PIN   29
    #define NRFX_SPIM_DCX_PIN  30
    
    #define SPI_INSTANCE  3                                           /**< SPI instance index. */
    
    /* FOR MAX30003 ECG PART */
    uint8_t SPI_RX_Buff[6];    /**< RX buffer. */
    uint8_t SPI_TX_Buff[6];
    static const uint8_t ECG_SIZE = sizeof(SPI_TX_Buff);        /**< Transfer length. */
    
    //volatile char SPI_RX_Buff[5] ;
    volatile char *SPI_RX_Buff_Ptr;
     	uint8_t A=0xFF;
      uint8_t A1=0x00;
    int i=0;
    unsigned long uintECGraw = 0;
    signed long intECGraw=0;
    uint8_t DataPacketHeader[20];
    uint8_t data_len = 8;
    signed long ecgdata;
    unsigned long data;
    char SPI_temp_32b;
    char SPI_temp_Burst[100];
    
    static const nrfx_spim_t spi = NRFX_SPIM_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
    
    static volatile bool spi_xfer_done;  /**< Flag used to indicate that SPI instance completed the transfer. */
    
    #define TEST_STRING "rohit"
    static uint8_t       m_tx_buf[] = TEST_STRING;           /**< TX buffer. */
    static uint8_t       m_rx_buf[sizeof(TEST_STRING) + 1];  /**< RX buffer. */
    static const uint8_t m_length = sizeof(m_tx_buf);        /**< Transfer length. */
    
    void timerIsr()                 //Its configuration is over
    {
      	nrf_gpio_pin_toggle(NRF_FCLK_PIN);    // NRF_GPIO_PIN_MAP(1, 00) toggle Digital6 attached to FCLK  of MAX30003
    }
    
    void MAX30003_Reg_Write (unsigned char WRITE_ADDRESS, unsigned long data)
    {
       nrf_gpio_pin_clear(NRFX_SPIM_SS_PIN);
       uint8_t  dataToSend = (WRITE_ADDRESS<<1) | WREG;
    	 uint8_t temp_1 = (data>>16);  //number of register to wr
    	 uint8_t temp_2 = (data>>8);
    	 nrf_delay_ms(200);
    	 
    	 nrfx_spim_xfer_desc_t xfer_desc = NRFX_SPIM_XFER_TRX(dataToSend, sizeof(dataToSend), SPI_RX_Buff, sizeof(dataToSend));
    	 nrfx_spim_xfer_desc_t xfer_desc1= NRFX_SPIM_XFER_TRX(temp_1,  sizeof(temp_1),  SPI_RX_Buff, sizeof(temp_1));
       nrfx_spim_xfer_desc_t xfer_desc2= NRFX_SPIM_XFER_TRX(temp_2,  sizeof(temp_2),  SPI_RX_Buff, sizeof(temp_2));
       nrfx_spim_xfer_desc_t xfer_desc3= NRFX_SPIM_XFER_TRX(data,       sizeof(data),       SPI_RX_Buff,  sizeof(data));
    	 nrf_delay_ms(200);
    	 nrf_gpio_pin_set(NRFX_SPIM_SS_PIN);
    }
    
    void max30003_sw_reset(void)
    {
      MAX30003_Reg_Write(SW_RST,0x000000);     
      nrf_delay_ms(100);
    }
    void MAX30003_Reg_Read(uint8_t Reg_address)
    {
    
        nrf_gpio_pin_clear(NRFX_SPIM_SS_PIN);
        SPI_TX_Buff[1] = (Reg_address<<1 ) | RREG;
        nrfx_spim_xfer_desc_t xfer_desc4 = NRFX_SPIM_XFER_TRX(SPI_TX_Buff[1], sizeof(SPI_TX_Buff[1]), SPI_RX_Buff, sizeof(SPI_TX_Buff[1]));
       
       for ( i = 0; i < 3; i++)
       {
             SPI_temp_32b[i]= NRFX_SPIM_XFER_TRX(A, sizeof(A), SPI_RX_Buff, sizeof(A));
       }
    
       nrf_gpio_pin_set(NRFX_SPIM_SS_PIN);
    }
    void spim_event_handler(nrfx_spim_evt_t const * p_event,
                           void *                  p_context)
    {
        spi_xfer_done = true;
        NRF_LOG_INFO("Transfer completed.");
        if (m_rx_buf[0] != 0)
        {
            NRF_LOG_INFO(" Received:");
            NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
        }
    }
    
    int main(void)
    {
        bsp_board_init(BSP_INIT_LEDS);
    
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        nrfx_spim_xfer_desc_t xfer_desc = NRFX_SPIM_XFER_TRX(m_tx_buf, m_length, m_rx_buf, m_length);
    
        nrfx_spim_config_t spi_config = NRFX_SPIM_DEFAULT_CONFIG;
        spi_config.frequency      = NRF_SPIM_FREQ_1M;
        spi_config.ss_pin         = NRFX_SPIM_SS_PIN;
        spi_config.miso_pin       = NRFX_SPIM_MISO_PIN;
        spi_config.mosi_pin       = NRFX_SPIM_MOSI_PIN;
        spi_config.sck_pin        = NRFX_SPIM_SCK_PIN;
        spi_config.dcx_pin        = NRFX_SPIM_DCX_PIN;
        spi_config.use_hw_ss      = true;
        spi_config.ss_active_high = false;
        APP_ERROR_CHECK(nrfx_spim_init(&spi, &spi_config, spim_event_handler, NULL));
    
        NRF_LOG_INFO("NRFX SPIM example started.");
         MAX30003_Reg_Read( 0x21); 
    	   MAX30003_Reg_Read( 0x25); 
    	 
        while (1)
        {
            // Reset rx buffer and transfer done flag
            memset(m_rx_buf, 0, m_length);
            spi_xfer_done = false;
    
            APP_ERROR_CHECK(nrfx_spim_xfer_dcx(&spi, &xfer_desc, 0, 15));
    
            while (!spi_xfer_done)
            {
                __WFE();
            }
    
            NRF_LOG_FLUSH();
    
            bsp_board_led_invert(BSP_BOARD_LED_0);
            nrf_delay_ms(200);
        }
    }
    

Related