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

Intan RHD2216 SPI communication with Nordic 52840

Hello Nordic,

I am using Nordic 52840 (SPI Master) with Intan RHD2216 (SPI Slave) to get data through SPI from the Intan chip.

I have used the Nordic software development kit SPI example as a reference to build my application.

Although everything seems to be working fine in terms of writing the registers for the chip and SPI communication, when the sampling commands are sent to the Intan RHD 2216, I am not able to receive any meaningful data.

I have attached my code along with a link for the data sheet for Intan RHD 2216.

I have tried a lot changing my code a number of times, but nothing seems to work.

Please let me know if you guys have any suggestions that could fix the problem

#include "nrf_drv_spi.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 "app_uart.h"
#include "nrf.h"
#include "nrf_drv_timer.h"
#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"
#endif

#define SPI_INSTANCE  0 /**< SPI instance index. */
#define UART_RX_BUF_SIZE 64
#define UART_TX_BUF_SIZE 64
#define WriteMask 0x8000 /** 1000_0000_0000_0000*/
#define ConvertMask 0x0000
#define ConvertMaskHP 0x0001
#define Calibrate 0x5500
#define clear 0x6500
#define ReadMask 0xA000

const nrf_drv_timer_t TIMER_SPI = NRF_DRV_TIMER_INSTANCE(0);
static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);  /**< SPI instance. */
static volatile bool spi_xfer_done;  /**< Flag used to indicate that SPI instance completed the transfer. */
static uint8_t       m_tx_buf[] = {235,0};           /**< TX buffer. */
static uint8_t       m_rx_buf[sizeof(m_tx_buf)];    /**< RX buffer. */
static const uint8_t m_length = sizeof(m_tx_buf);        /**< Transfer length. */
static uint8_t flag = 0;
static bool set = false;
/**
 * @brief SPI user event handler.
 * @param event
 */
void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
                       void *                    p_context)
{
    spi_xfer_done = true;
    NRF_LOG_INFO("Transfer completed.");
    ret_code_t ret_val;

    if (m_tx_buf[0] == 0b10001010 && m_tx_buf[1] == 44){
      app_uart_put(m_rx_buf[1]);
    }
    if( m_tx_buf[1] == 0b00000000 && m_tx_buf[0] == 0b00000100){
        ret_val = app_uart_put(m_rx_buf[1]);
        ret_val = app_uart_put(m_rx_buf[0]);
    }


}

void timer_handler(nrf_timer_event_t event_type, void* p_context)
{

    switch (event_type)
    {
        case NRF_TIMER_EVENT_COMPARE0:
            if (spi_xfer_done == true && m_tx_buf[1] == 0b00000000 && m_tx_buf[0] == 0b00000100){
              spi_xfer_done == false;
              memset(m_rx_buf, 0, m_length);
              APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
            }
            break;

        default:
            //Do nothing.
            break;
    }
}

void timer_init(){
 
    uint32_t time_ms = 1; //Time(in miliseconds) between consecutive compare events.
    uint32_t time_ticks;
    uint32_t err_code = NRF_SUCCESS;

    
    nrf_drv_timer_config_t timer_cfg = NRF_DRV_TIMER_DEFAULT_CONFIG;
    err_code = nrf_drv_timer_init(&TIMER_SPI, &timer_cfg, timer_handler);
    APP_ERROR_CHECK(err_code);

    time_ticks = nrf_drv_timer_ms_to_ticks(&TIMER_SPI, time_ms);

    nrf_drv_timer_extended_compare(
         &TIMER_SPI, NRF_TIMER_CC_CHANNEL0, time_ticks, NRF_TIMER_SHORT_COMPARE0_CLEAR_MASK, true);
    nrf_drv_timer_enable(&TIMER_SPI);
}

void uart_event_handle(app_uart_evt_t * p_event)
{
    static uint16_t index = 0;
    uint32_t ret_val;

    switch (p_event->evt_type)
    {
        /**@snippet [Handling data from UART] */
        /**@snippet [Handling data from UART] */
        case APP_UART_COMMUNICATION_ERROR:
            NRF_LOG_ERROR("Communication error occurred while handling UART.");
            APP_ERROR_HANDLER(p_event->data.error_communication);
            break;

        case APP_UART_FIFO_ERROR:
            NRF_LOG_ERROR("Error occurred in FIFO module used by UART.");
            APP_ERROR_HANDLER(p_event->data.error_code);
            break;

        default:
            break;
    }
}


static void uart_init(void)
{
    ret_code_t err_code;

    app_uart_comm_params_t const comm_params =
    {
        .rx_pin_no    = RX_PIN_NUMBER,
        .tx_pin_no    = TX_PIN_NUMBER,
        .rts_pin_no   = RTS_PIN_NUMBER,
        .cts_pin_no   = CTS_PIN_NUMBER,
        .flow_control = APP_UART_FLOW_CONTROL_DISABLED,
        .use_parity   = false,
        .baud_rate    = UART_BAUDRATE_BAUDRATE_Baud115200
    };

    APP_UART_FIFO_INIT(&comm_params,
                       UART_RX_BUF_SIZE,
                       UART_TX_BUF_SIZE,
                       uart_event_handle,
                       APP_IRQ_PRIORITY_LOWEST,
                       err_code);

    APP_ERROR_CHECK(err_code);
    
}

static void callCommand(void){

    spi_xfer_done = false;

    APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));
    while (!spi_xfer_done)
    {
        __WFE();
    }
}

int main(void)
{
    bsp_board_init(BSP_INIT_LEDS);
    uart_init();
    timer_init();

    bsp_board_leds_on();

    nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
    spi_config.ss_pin   = SPI_SS_PIN;
    spi_config.miso_pin = SPI_MISO_PIN;
    spi_config.mosi_pin = SPI_MOSI_PIN;
    spi_config.sck_pin  = SPI_SCK_PIN;
    spi_config.frequency = NRF_SPI_FREQ_125K;
    APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));

    NRF_LOG_INFO("SPI started.");
 
    //Register 0 -> 222
 
    m_tx_buf[0] = 0b10000000;
    m_tx_buf[1] = 0b11011110;
    callCommand();

    //Register 1 -> 32
    m_tx_buf[0] = 0b10000001;
    m_tx_buf[1] = 0b00100000;
    callCommand();
    

    //Register 2 - > 40
  
    m_tx_buf[0] = 0b10000010;
    m_tx_buf[1] = 0b00101000;
    callCommand();
    
    //Register 3 -> 2
    m_tx_buf[0] = 0b10000011;
    m_tx_buf[1] = 0b00000000;
    callCommand();

      //Register 4 -> 192
    memset(m_rx_buf, 0, m_length);
    spi_xfer_done = false;
    m_tx_buf[0] = 0b10000100;
    m_tx_buf[1] = 0b11000100;
    callCommand();
    //Register 5 -> 0
     m_tx_buf[0] = 0b10000101;
     m_tx_buf[1] = 0b00000000;
    callCommand();

        
    //Register 6 - 0
  
    m_tx_buf[0] = 0b10000110;
    m_tx_buf[1] = 0b00000000;
    callCommand();

      //Register 7 - > 0
    m_tx_buf[0] = 0b10000111;
    m_tx_buf[1] = 0b00000000;
    callCommand();
    // Register 8 - > 27
    m_tx_buf[0] = 0b10001000;
    m_tx_buf[1] = 27;
    callCommand();
    //Register 9 - > 1
    m_tx_buf[0] = 0b10001001;
    m_tx_buf[1] = 1;
    callCommand();
     
    //Register 10 - > 44
    m_tx_buf[0] = 0b10001010;
    m_tx_buf[1] = 44;
    callCommand();
     
    //Register 11 -> 1
    m_tx_buf[0] = 0b10001011;
    m_tx_buf[1] = 1;
    callCommand();

    //Register 12 -> 5
    m_tx_buf[0] = 0b10001100;
    m_tx_buf[1] = 5;
    callCommand();
     
     //Register 13 - > 1
     m_tx_buf[0] = 0b10001101;
     m_tx_buf[1] = 0b00000001;
    callCommand();
    
     //Register 14 - > 255
     m_tx_buf[0] = 0b10001110;
     m_tx_buf[1] = 0b01010101;
    callCommand();
     
     //Register 15
     m_tx_buf[0] = 0b10001111;
     m_tx_buf[1] = 0b00000000;
    callCommand();
    
     //Register 16
     m_tx_buf[0] = 0b10010000;
     m_tx_buf[1] = 0b00000000;
     callCommand();
     
     //Register 17
     m_tx_buf[0] = 0b10010001;
     m_tx_buf[1] = 0b00000000;
     callCommand();

    //Calibrate

    m_tx_buf[0] = 0b01010101;
    m_tx_buf[1] = 0b00000000;
    callCommand();

   
    //dummy

     m_tx_buf[0] = 235;
     m_tx_buf[1] = 0;
     for( int i = 0; i < 9; i++){
        callCommand();
     }

     callCommand();
    
     //Convert H
     m_tx_buf[1] = 0b00000001;
     m_tx_buf[0] = 0b00000100;

     callCommand();


     //Convert command with channel
     m_tx_buf[1] = 0b00000000;
     m_tx_buf[0] = 0b00000100;


    while(1)
    {
       __WFI();
    }

    
    

}

http://intantech.com/files/Intan_RHD2000_series_datasheet.pdf

Related