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
  • This is usually defined by the Slave. So, in the usual use-case of the microcontroller being the master, you would look it up in the Slave's documentation.

    As you are creating the Slave here, it is you that defines what it does.

    If you want it to be variable, then you will have to devise some sort of protocol to exchange the information.

  • 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)
            }
        }
    }

Reply
  • 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)
            }
        }
    }

Children
Related