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

SPI Slave Question

When NRF52832 as SPI Slave, how can I know how many bytes the slave should read,the API nrf_drv_spis_buffers_set(&spis, m_tx_buf, length, m_rx_buf, length)),does not have the real receive byte

Parents Reply Children
  • I put nrf_drv_spis_buffers_set in a task, and block until csn_pin is low, then read/write to master. if I define the protocol

    cmd:0x01, return 2 bytes, so the master can read 3 bytes(one byte cmd)

    cmd:0x02,return 4 bytes, so the master can read 5 bytes(one byte cmd)

    The question is the slave can not know which cmd will receive, so how can set write length in nrf_drv_spis_buffers_set at task.

    void station_gpiote_evt_handler_t(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
    {
        BaseType_t higher_priority_task_woken, result;
        higher_priority_task_woken = pdFALSE;
    
        if(station_event_handle == NULL)
        {
            NRF_LOG_WARNING("Task event is not create now\r\n");
            return;
        }
    
        if((pin == SPIS_CS_PIN) && (action == NRF_GPIOTE_POLARITY_HITOLO))
        {
            result = xEventGroupSetBitsFromISR(station_event_handle, 
                                               SPIS_CSN_EVENT, 
                                               &higher_priority_task_woken);
            if(result != pdFAIL)
            {
                portYIELD_FROM_ISR( higher_priority_task_woken );
            }
        }
    }
    
    
    static void station_main_task(void *args)
    {
        UNUSED_PARAMETER(args);
        ret_code_t err_code;
        EventBits_t event;
    
        while(1)
        {
            event = xEventGroupWaitBits(station_event_handle,
                                        SPIS_CSN_EVENT,
                                        pdTRUE,  /* bit should be cleared before returning. */
                                        pdFALSE, /* Don't wait for both bits, either bit will do. */
                                        portMAX_DELAY);
            if((event & SPIS_CSN_EVENT) == SPIS_CSN_EVENT)
            {
                NRF_LOG_INFO("CS High->Low, now read spi bus");
                nrf_drv_spis_buffers_set(&spis, m_tx_buf, length, m_rx_buf, length)
            }
        }
    }

  • the slave can not know which cmd will receive, so how can set write length

    Again, it is the Slave which defines this - so it's up to you.

    The Master has to abide by what the Slave does; ie, the agreed protocol.

    You will have to design-in handling of invalid conditions.

    This has nothing specifically to do with nRF - this applies to any comms interface.

  • how to know the command?When CS pin is low, first receive one bye to judgement which command have received?Then know the length and read once again?

  • I have write a test SPI master, when execute read operation, SPI slave produce NRF_DRV_SPIS_XFER_DONE event, then the whole data is in buffer, I have no time to process the data and judgement which command have receive

  • May be my question is not description clearly, in short ,I don't know what's data to put into m_tx_buf before first command received.So Now my master must read twice that can get the correct result.

Related