NRFX_SPIS_DRIVER and ZypherOS

Hello team,

i have a working custom SPI slave application that uses NRF SPI DRIVER. with this application i able to test two way SPI communication successfully. 


 i want to migrate radio test example based on zephyr OS that uses zephyr's shell module to handle command  from UART.

i want to use SPI instead of UART.

my question is, zephyr os based radio test example ( if successfully port from UART  to SPI  ) will work with NRFX_SPI_DRIVER?

we want to receive radio command over SPI from STM32 master device

The scenario would be:

1. STM32 master device will receive commands  over UART (CLI)

2. STM32 master device passed the received command to NRF52 SOC over SPI

Please suggest steps need to follow 

please share SPI slave example in zephyr if available 

Attached file : example of SPI slave using NRFX_SPI_DRIVER 

//#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <nrfx_spis.h>

/** @brief Symbol specifying SPIS instance to be used. */
#define SPIS_INST_IDX 0

/** @brief Symbol specifying the master's command. */
#define NORDIC_CMD "STM32L4"
/** @brief Symbol specifying the slave's response to the @ref NORDIC_CMD command. */
#define NORDIC_RESP "Nordic "

#define NORDIC_HELLOW_CMD "HELLOW "
#define NORDIC_HELP_CMD "HELP   "

#define NORDIC_HELP_RESP "WELCOME"
#define NORDIC_HELLOW_RESP "WORLD  "

/** @brief Symbol specifying some unrecognized by the slave command to test slave's @ref ERROR_RESP response. */
#define UNKNOWN_CMD "Unknown"

/** @brief Symbol specifying the slave's response to some unknown command. */
#define ERROR_RESP "ERROR"

/** @brief Symbol specifying the master's command to uninitialize SPIS instance. */
#define UNINIT_CMD "Uninit"

/** @brief Symbol specifying slave's pin number for SCK. */
#define SCK_PIN_SLAVE 6

/** @brief Symbol specifying slave's pin number for MOSI. */
#define MOSI_PIN_SLAVE 9
/** @brief Symbol specifying slave's pin number for MISO. */
#define MISO_PIN_SLAVE 10

/** @brief Symbol specifying slave's pin number for CS. */
#define CSN_PIN_SLAVE 12

/** @brief Union used to calculate a value of the @ref BUFF_SIZE symbol. */
union messages
{
    uint8_t cmd1[sizeof(NORDIC_CMD)];   ///< Array with size to store @ref NORDIC_CMD command.
    uint8_t cmd2[sizeof(UNKNOWN_CMD)];  ///< Array with size to store @ref UNKNOWN_CMD command.
    uint8_t cmd3[sizeof(UNINIT_CMD)];   ///< Array with size to store @ref UNINIT_CMD command.
    uint8_t cmd4[sizeof(NORDIC_HELLOW_CMD)];
    uint8_t cmd5[sizeof(NORDIC_HELP_CMD)];
    uint8_t resp1[sizeof(NORDIC_RESP)]; ///< Array with size to store @ref NORDIC_RESP respond.
    uint8_t resp2[sizeof(ERROR_RESP)];  ///< Array with size to store @ref ERROR_RESP respond.
    uint8_t resp3[sizeof(NORDIC_HELLOW_RESP)];
    uint8_t resp4[sizeof(NORDIC_HELP_RESP)];
 
};


/** @brief Symbol specifying the length of the buffers. */
#define BUFF_SIZE sizeof(union messages)


/** @brief Slave's transmit buffer defined with the size of @ref BUFF_SIZE symbol. */
static uint8_t m_tx_buffer_slave[BUFF_SIZE];

/** @brief Slave's receive buffer defined with the size of @ref BUFF_SIZE symbol. */
static uint8_t m_rx_buffer_slave[BUFF_SIZE];

/** @brief Flag indicating that @ref UNINIT_CMD message was received by SPIS. */
static bool m_spis_uninit_cmd_received;

/** @brief Flag indicating that SPIS instance was uninitialized. */
static bool m_spis_uninitialized;


 nrfx_spis_t spis_inst = NRFX_SPIS_INSTANCE(SPIS_INST_IDX);
/**
 * @brief Function for handling SPIS driver events.
 *
 * @param[in] p_event   Pointer to the SPIM driver event.
 * @param[in] p_context Pointer to the context passed from the driver.
 */
static void spis_handler(nrfx_spis_evt_t const * p_event, void * p_context)
{

    if (p_event->evt_type == NRFX_SPIS_XFER_DONE)
    {
     //   nrfx_err_t status;
      //  (void)status;
    volatile unsigned int status =0;
        nrfx_spis_t * p_spis_inst = p_context;

        size_t tx_length = p_event->tx_amount;
        size_t rx_length = p_event->rx_amount;

        //NRFX_LOG_INFO("SPIS finished.");

        if (tx_length > 0)
        {
           // NRFX_LOG_INFO("SPIS: Message transmitted: %s", m_tx_buffer_slave);
            status = nrfx_spis_buffers_set(p_spis_inst, NULL, 0, m_rx_buffer_slave, BUFF_SIZE);
            NRFX_ASSERT(status == NRFX_SUCCESS);
        }

        if (rx_length > 0)
        {
          //  NRFX_LOG_INFO("SPIS: Message received: %s", m_rx_buffer_slave);
            memset(m_tx_buffer_slave, 0, BUFF_SIZE);

            if (!memcmp(m_rx_buffer_slave, NORDIC_CMD, sizeof(NORDIC_CMD)))
            {
                memcpy(m_tx_buffer_slave, NORDIC_RESP, sizeof(NORDIC_RESP));
            }
            else if (!memcmp(m_rx_buffer_slave, UNINIT_CMD, sizeof(UNINIT_CMD)))
            {
                m_spis_uninit_cmd_received = true;
                return;
            }
            else if(!memcmp(m_rx_buffer_slave, NORDIC_HELLOW_CMD, sizeof(NORDIC_HELLOW_CMD)))
            {
                   memcpy(m_tx_buffer_slave, NORDIC_HELLOW_RESP, sizeof(NORDIC_HELLOW_RESP));
            }
            else if(!memcmp(m_rx_buffer_slave, NORDIC_HELP_CMD, sizeof(NORDIC_HELP_CMD)))
            {
                   memcpy(m_tx_buffer_slave, NORDIC_HELP_RESP, sizeof(NORDIC_HELP_RESP));
            }
            else
            {
                memcpy(m_tx_buffer_slave, ERROR_RESP, BUFF_SIZE);
            }

            status = nrfx_spis_buffers_set(p_spis_inst, m_tx_buffer_slave, BUFF_SIZE, NULL, 0);
            NRFX_ASSERT(status == NRFX_SUCCESS);
       //     status = nrfx_spis_buffers_set(&spis_inst, NULL, 0, m_rx_buffer_slave, BUFF_SIZE);
        //    NRFX_ASSERT(status == NRFX_SUCCESS);
        }
    }
}

void Spi_slave_init()
{
 //   nrfx_err_t status;
//    (void)status;
    volatile unsigned int status =0;
   // nrfx_spis_t spis_inst = NRFX_SPIS_INSTANCE(SPIS_INST_IDX);
    nrfx_spis_config_t spis_config = NRFX_SPIS_DEFAULT_CONFIG(SCK_PIN_SLAVE,
                                                              MOSI_PIN_SLAVE,
                                                              MISO_PIN_SLAVE,
                                                              CSN_PIN_SLAVE);
    status = nrfx_spis_init(&spis_inst, &spis_config, spis_handler, &spis_inst);
    NRFX_ASSERT(status == NRFX_SUCCESS);

#if defined(__ZEPHYR__)
//    IRQ_DIRECT_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_SPIM_INST_GET(SPIM_INST_IDX)), IRQ_PRIO_LOWEST,
  //                     NRFX_SPIM_INST_HANDLER_GET(SPIM_INST_IDX), 0);
    IRQ_DIRECT_CONNECT(NRFX_IRQ_NUMBER_GET(NRF_SPIS_INST_GET(SPIS_INST_IDX)), IRQ_PRIO_LOWEST,
                       NRFX_SPIS_INST_HANDLER_GET(SPIS_INST_IDX), 0);
#endif
status = nrfx_spis_buffers_set(&spis_inst, NULL, 0, m_rx_buffer_slave, BUFF_SIZE);
NRFX_ASSERT(status == NRFX_SUCCESS);

}
/**
 * @brief Function for application main entry.
 *
 * @return Nothing.
 */

int main(void)
{
    Spi_slave_init();

        while(1)
        {
             
            if (!m_spis_uninit_cmd_received)
            {
                if (m_spis_uninit_cmd_received && !m_spis_uninitialized)
                {
                  //  nrfx_spis_uninit(&spis_inst);
                   // m_spis_uninitialized = true;
                }
            }
        }

        return 0;
}

Related