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

UART+nRFx_SPIM Transmitting and Receiving

Hi,

I have merged nrfx_spim with uart in nrf52840 dk (PCA10056) board .i am able to transmit only string but not integers. from my sensor i use spi to send real-time integer/float data but i am not able to transmit and receive this real-time data.i have tried in debug mode both Rx and Tx are updated in registers buffer when I tried in nrfx_spim separately .but after merging spi code with uart i am not able to get real-time data on terminal window. 

HW Connection:

For UART:

RX and TX Pins make short

For nrfx_spim SENSOR Connection:


CS- P0.29

SCK-P0.03

MOSI- P0.04

MISO-P0.28

May I know is it possible to Display realtime Sensor data on the Terminal window using SPI and UART Example.

Kindly check the code and suggest a way to solve this problem:

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include "app_uart.h"
#include "app_error.h"
#include "nrf_delay.h"
#include "nrf.h"
#include "bsp.h"
#include "MAX30003.h"
#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"
#include "string.h"

#if defined (UART_PRESENT)
#include "nrf_uart.h"
#endif
#if defined (UARTE_PRESENT)
#include "nrf_uarte.h"
#endif

#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 ENABLE_LOOPBACK_TEST  /**< if defined, then this example will be a loopback test, which means that TX should be connected to RX to get data loopback. */
#define ENABLE_LOOPBACK_TEST
#define MAX_TEST_DATA_BYTES     (15U)                /**< max number of test bytes to be used for tx and rx. */
#define UART_TX_BUF_SIZE 256                         /**< UART TX buffer size. */
#define UART_RX_BUF_SIZE 256                         /**< UART RX buffer size. */

#define SPI_INSTANCE  3                                           /**< SPI instance index. */
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. */


 uint8_t       SPI_TX_Buff[5];
 uint8_t       rx_data; 
 uint8_t       SPI_RX_Buff[sizeof(SPI_TX_Buff)];  /**< RX buffer. */

static const uint8_t m_length = sizeof(SPI_TX_Buff);        /**< Transfer length. */

 
void uart_error_handle(app_uart_evt_t * p_event)
{
    if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_communication);
    }
    else if (p_event->evt_type == APP_UART_FIFO_ERROR)
    {
        APP_ERROR_HANDLER(p_event->data.error_code);
    }
}


#ifdef ENABLE_LOOPBACK_TEST
/* Use flow control in loopback test. */
#define UART_HWFC APP_UART_FLOW_CONTROL_ENABLED

/** @brief Function for setting the @ref ERROR_PIN high, and then enter an infinite loop.
 */

uint8_t MAX30003_Reg_Read(uint8_t Reg_address)
{
  nrfx_err_t err_code = 0;	
  uint8_t data;
  //Prepare the tx buffer
  memset(SPI_RX_Buff, 0x00, sizeof(SPI_RX_Buff)); // Erases the buffer
  SPI_TX_Buff[0] = ((Reg_address << 1) | 0x01); 
	
  //Create the transfer descriptor
  nrfx_spim_xfer_desc_t xfer_desc = NRFX_SPIM_XFER_TRX(SPI_TX_Buff, sizeof(SPI_TX_Buff), SPI_RX_Buff, sizeof(SPI_TX_Buff));
  //Initiate the transfer
  spi_xfer_done == false;
  err_code = nrfx_spim_xfer(&spi, &xfer_desc, 0);
  APP_ERROR_CHECK(err_code);
  while (spi_xfer_done == true);
  data |= (SPI_RX_Buff[1] << 16);
  data |= (SPI_RX_Buff[2] << 8);
  data |= SPI_RX_Buff[3];
  return data;
}

void spim_event_handler(nrfx_spim_evt_t const * p_event,
                       void *                  p_context)
{
    spi_xfer_done = true;
    NRF_LOG_INFO("Transfer completed.");
    if (SPI_RX_Buff[0] != 0)
    {
        NRF_LOG_INFO(" Received:");
        NRF_LOG_HEXDUMP_INFO(SPI_RX_Buff, strlen((const char *)SPI_RX_Buff));
    }
}

static void show_error(void)
{

    bsp_board_leds_on();
    while (true)
    {
        // Do nothing.
    }
}


/** @brief Function for testing UART loop back.
 *  @details Transmitts one character at a time to check if the data received from the loopback is same as the transmitted data.
 *  @note  @ref TX_PIN_NUMBER must be connected to @ref RX_PIN_NUMBER)
 */
static void uart_loopback_test()
{
    uint8_t * tx_data= SPI_TX_Buff;
   

    // Start sending one byte and see if you get the same
    for (uint32_t i = 0; i < MAX_TEST_DATA_BYTES; i++)
    {
        uint32_t err_code;
        while (app_uart_put(tx_data[i]) != NRF_SUCCESS);

        nrf_delay_ms(10);
        err_code = app_uart_get(&rx_data);

        if ((rx_data!= tx_data[i]) || (err_code != NRF_SUCCESS))
        {
            show_error();
        }
    }
    return;
}
#else
/* When UART is used for communication with the host do not use flow control.*/
#define UART_HWFC APP_UART_FLOW_CONTROL_DISABLED
#endif

/**
 * @brief Function for main application entry.
 */
int main(void)
{
    uint32_t err_code; 
  bsp_board_init(BSP_INIT_LEDS);
  
  MAX30003_Reg_Read(ECG_FIFO); //0x0F
  

	APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
  NRF_LOG_DEFAULT_BACKENDS_INIT();
	
    const app_uart_comm_params_t comm_params =
      {
          RX_PIN_NUMBER,
          TX_PIN_NUMBER,
          RTS_PIN_NUMBER,
          CTS_PIN_NUMBER,
          UART_HWFC,
          false,
#if defined (UART_PRESENT)
          NRF_UART_BAUDRATE_115200
#else
          NRF_UARTE_BAUDRATE_115200
#endif
      };

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

    APP_ERROR_CHECK(err_code);

nrfx_spim_config_t spi_config = NRFX_SPIM_DEFAULT_CONFIG;
spi_config.frequency = SPIM_FREQUENCY_FREQUENCY_K125;
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;
spi_config.mode = NRF_SPIM_MODE_2; // SCK active low, sample on trailing edge of clock
nrfx_spim_xfer_desc_t xfer_desc = NRFX_SPIM_XFER_TRX(SPI_TX_Buff, sizeof(SPI_TX_Buff), SPI_RX_Buff, sizeof(SPI_TX_Buff));
APP_ERROR_CHECK(nrfx_spim_init(&spi, &spi_config, spim_event_handler, NULL));

//memcpy(&my_sample_1,SPI_RX_Buff , (sizeof(uint8_t)));
NRF_LOG_INFO("NRFX SPIM example started.");
#ifndef ENABLE_LOOPBACK_TEST
    printf("\r\nUART example started.\r\n");

    while (1)
    {
        uint8_t cr;
			memset(SPI_RX_Buff, 0, m_length);
      spi_xfer_done = false;
      APP_ERROR_CHECK(nrfx_spim_xfer_dcx(&spi, &xfer_desc, 0, 15)); 
        while (true)
            {
              __WFE();  // Do nothing.
            }
			  NRF_LOG_FLUSH();
			  bsp_board_led_invert(BSP_BOARD_LED_0);
          while (true)
            {
              __WFE();  // Do nothing.
            }
			  NRF_LOG_FLUSH();
			  bsp_board_led_invert(BSP_BOARD_LED_0); 		
						
        while (app_uart_get(&cr) != NRF_SUCCESS);
        while (app_uart_put(cr) != NRF_SUCCESS);
          
        if (cr == 'q' || cr == 'Q')
        {
            printf(" \r\nExit!\r\n");
        }
				
    }
#else

    // This part of the example is just for testing the loopback .
    while (true)
    {
        uart_loopback_test();
    }
		        
#endif
}

Parents Reply Children
No Data
Related