static uint32_t frame_get()
{
    uint32_t err_code;

    m_current_rx_frame_length = compute_current_frame_length(m_rx_packet_length,
                                                             m_accumulated_rx_packet_length);

    if (!m_trash_payload_flag)
    {
        err_code =
            nrf_drv_spis_buffers_set(&m_spis,
                                     (uint8_t *) m_zero_buff,
                                     m_current_rx_frame_length,
                                     &(m_p_rx_buffer[m_accumulated_rx_packet_length]),
                                     m_current_rx_frame_length);
    }
    else
    {
        err_code = nrf_drv_spis_buffers_set(&m_spis,
                                            (uint8_t *) m_zero_buff,
                                            m_current_rx_frame_length,
                                            m_rx_frame_buffer,
                                            m_current_rx_frame_length);
    }
    return err_code;
}
Ejemplo n.º 2
0
int main(void)
{
    // Enable the constant latency sub power mode to minimize the time it takes
    // for the SPIS peripheral to become active after the CSN line is asserted
    // (when the CPU is in sleep mode).
    NRF_POWER->TASKS_CONSTLAT = 1;

    LEDS_CONFIGURE(BSP_LED_0_MASK);
    LEDS_OFF(BSP_LED_0_MASK);

    APP_ERROR_CHECK(NRF_LOG_INIT());
    NRF_LOG_PRINTF("SPIS example\n");

    nrf_drv_spis_config_t spis_config = NRF_DRV_SPIS_DEFAULT_CONFIG(SPIS_INSTANCE);
    spis_config.csn_pin               = SPIS_CS_PIN;
    
    APP_ERROR_CHECK(nrf_drv_spis_init(&spis, &spis_config, spis_event_handler));

    while(1)
    {
        memset(m_rx_buf, 0, m_length);
        spis_xfer_done = false;

        APP_ERROR_CHECK(nrf_drv_spis_buffers_set(&spis, m_tx_buf, m_length, m_rx_buf, m_length));

        while (!spis_xfer_done)
        {
            __WFE();
        }

        LEDS_INVERT(BSP_LED_0_MASK);
    }
}
Ejemplo n.º 3
0
uint32_t spi_slave_example_init(void)
{
    uint32_t              err_code;
    nrf_drv_spis_config_t spis_config = NRF_DRV_SPIS_DEFAULT_CONFIG(SPIS_INSTANCE_NUMBER); 

    spis_config.miso_pin        = SPIS_MISO_PIN;
    spis_config.mosi_pin        = SPIS_MOSI_PIN;
    spis_config.sck_pin         = SPIS_SCK_PIN;
    spis_config.csn_pin         = SPIS_CSN_PIN;
    spis_config.mode            = NRF_DRV_SPIS_MODE_0;
    spis_config.bit_order       = NRF_DRV_SPIS_BIT_ORDER_LSB_FIRST;
    spis_config.def             = DEF_CHARACTER;
    spis_config.orc             = ORC_CHARACTER;
    
    err_code = nrf_drv_spis_init(&m_spis, &spis_config, spi_slave_event_handle);
    APP_ERROR_CHECK(err_code);
    
    //Initialize buffers.
    spi_slave_buffers_init(m_tx_buf, m_rx_buf, (uint16_t)TX_BUF_SIZE);
    
    //Set buffers.
    err_code = nrf_drv_spis_buffers_set(&m_spis, m_tx_buf, sizeof(m_tx_buf), m_rx_buf, sizeof(m_rx_buf));
    APP_ERROR_CHECK(err_code);            

    return NRF_SUCCESS;
}
static uint32_t header_get()
{
    uint32_t err_code;

    err_code = nrf_drv_spis_buffers_set(&m_spis,
                                        (uint8_t *) m_zero_buff,
                                        SER_PHY_HEADER_SIZE,
                                        m_header_rx_buffer,
                                        SER_PHY_HEADER_SIZE);
    return err_code;
}
static uint32_t header_send(uint16_t len)
{
    uint32_t err_code;

    m_header_tx_buffer[0] = (uint8_t) 0; //this is guard byte
    (void)uint16_encode(len, &(m_header_tx_buffer[1]));
    err_code = nrf_drv_spis_buffers_set(&m_spis,
                                        m_header_tx_buffer,
                                        SER_PHY_HEADER_SIZE + 1,
                                        m_header_rx_buffer,
                                        SER_PHY_HEADER_SIZE + 1);
    return err_code;
}
Ejemplo n.º 6
0
static void slave_event_handler(uint8_t spi_idx,
                                nrf_drv_spis_event_t event)
{
    spi_info_t *p_spi_info = &m_spi_info[spi_idx];

    if (event.evt_type == NRF_DRV_SPIS_XFER_DONE) {
        // Signal that there is some data received that could be read.
        p_spi_info->flag.readable = true;

        // And prepare for the next transfer.
        // Previous data set in 'spi_slave_write' (if any) has been transmitted,
        // now use the default one, until some new is set by 'spi_slave_write'.
        p_spi_info->tx_buf = NRF_DRV_SPIS_DEFAULT_ORC;
        nrf_drv_spis_buffers_set(&m_instances[spi_idx].slave,
            (uint8_t const *)&p_spi_info->tx_buf, 1,
            (uint8_t *)&p_spi_info->rx_buf, 1);
    }
}
Ejemplo n.º 7
0
/**@brief Function for SPI slave event callback.
 *
 * Upon receiving an SPI transaction complete event, LED1 will blink and the buffers will be set.
 *
 * @param[in] event SPI slave driver event.
 */
static void spi_slave_event_handle(nrf_drv_spis_event_t event)
{
    uint32_t err_code;
    
    if (event.evt_type == NRF_DRV_SPIS_XFER_DONE)
    {
        err_code = bsp_indication_set(BSP_INDICATE_RCV_OK);
        APP_ERROR_CHECK(err_code);
        
        //Check if buffer size is the same as amount of received data.
        APP_ERROR_CHECK_BOOL(event.rx_amount == RX_BUF_SIZE);
        
        //Check if received data is valid.
        bool success = spi_slave_buffer_check(m_rx_buf, event.rx_amount);
        APP_ERROR_CHECK_BOOL(success);
        
        //Set buffers.
        err_code = nrf_drv_spis_buffers_set(&m_spis, m_tx_buf, sizeof(m_tx_buf), m_rx_buf, sizeof(m_rx_buf));
        APP_ERROR_CHECK(err_code);          
    }
}
static uint32_t frame_send()
{
    uint32_t err_code;

    m_current_tx_frame_length = compute_current_frame_length(m_tx_packet_length,
                                                             m_accumulated_tx_packet_length);

    if (m_current_tx_frame_length == SER_PHY_SPI_5W_MTU_SIZE)
    {
        m_current_tx_frame_length -= 1; //extra space for guard byte must be taken into account for MTU
    }
    m_tx_frame_buffer[0] = 0; //guard byte
    copy_buff(&(m_tx_frame_buffer[1]),
              &(m_p_tx_buffer[m_accumulated_tx_packet_length]),
              m_current_tx_frame_length);
    err_code = nrf_drv_spis_buffers_set(&m_spis,
                                        m_tx_frame_buffer,
                                        m_current_tx_frame_length + 1,
                                        m_rx_frame_buffer,
                                        m_current_tx_frame_length + 1);

    return err_code;
}
Ejemplo n.º 9
0
void spi_format(spi_t *obj, int bits, int mode, int slave)
{
    if (bits != 8) {
        error("Only 8-bits SPI is supported\r\n");
    }
    if (mode > 3) {
        error("SPI format error\r\n");
    }

    spi_info_t *p_spi_info = SPI_INFO(obj);

    if (slave)
    {
        nrf_drv_spis_mode_t spi_modes[4] = {
            NRF_DRV_SPIS_MODE_0,
            NRF_DRV_SPIS_MODE_1,
            NRF_DRV_SPIS_MODE_2,
            NRF_DRV_SPIS_MODE_3,
        };
        nrf_drv_spis_mode_t new_mode = spi_modes[mode];

        // If the peripheral is currently working as a master, the SDK driver
        // it uses needs to be switched from SPI to SPIS.
        if (p_spi_info->master) {
            nrf_drv_spi_uninit(MASTER_INST(obj));
        }
        // I the SPI mode has to be changed, the SDK's SPIS driver needs to be
        // re-initialized (there is no other way to change its configuration).
        else if (p_spi_info->spi_mode != (uint8_t)new_mode) {
            nrf_drv_spis_uninit(SLAVE_INST(obj));
        }
        else {
            return;
        }

        p_spi_info->spi_mode = (uint8_t)new_mode;
        p_spi_info->master = false;
        p_spi_info->flag.readable = false;

        // Initialize SDK's SPIS driver with the new configuration.
        nrf_drv_spis_config_t config;
        prepare_slave_config(&config, p_spi_info);
        (void)nrf_drv_spis_init(SLAVE_INST(obj), &config,
            m_slave_event_handlers[SPI_IDX(obj)]);

        // Prepare the slave for transfer.
        p_spi_info->tx_buf = NRF_DRV_SPIS_DEFAULT_ORC;
        nrf_drv_spis_buffers_set(SLAVE_INST(obj),
            (uint8_t const *)&p_spi_info->tx_buf, 1,
            (uint8_t *)&p_spi_info->rx_buf, 1);
    }
    else // master
    {
        nrf_drv_spi_mode_t spi_modes[4] = {
            NRF_DRV_SPI_MODE_0,
            NRF_DRV_SPI_MODE_1,
            NRF_DRV_SPI_MODE_2,
            NRF_DRV_SPI_MODE_3,
        };
        nrf_drv_spi_mode_t new_mode = spi_modes[mode];

        // If the peripheral is currently working as a slave, the SDK driver
        // it uses needs to be switched from SPIS to SPI.
        if (!p_spi_info->master) {
            nrf_drv_spis_uninit(SLAVE_INST(obj));
        }
        // I the SPI mode has to be changed, the SDK's SPI driver needs to be
        // re-initialized (there is no other way to change its configuration).
        else if (p_spi_info->spi_mode != (uint8_t)new_mode) {
            nrf_drv_spi_uninit(MASTER_INST(obj));
        }
        else {
            return;
        }

        p_spi_info->spi_mode = (uint8_t)new_mode;
        p_spi_info->master = true;
        p_spi_info->flag.busy = false;

        // Initialize SDK's SPI driver with the new configuration.
        nrf_drv_spi_config_t config;
        prepare_master_config(&config, p_spi_info);
        (void)nrf_drv_spi_init(MASTER_INST(obj), &config,
            m_master_event_handlers[SPI_IDX(obj)]);
    }
}