Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

nRF52832 SPI function

Hi,

I am trying to make a simple function for writing via SPI. I am using the SPI example and it works fine. Now I would just like to make two SPI functions that I can use in my project:

- initSPI()

- ReadWriteSPI()

I have made the following so far and the event handler and initSPI seems to work fine if I keep the original example code for the spi transfer. It is just the ReadWriteSPI function which doesn't work. I am sure it has something to do with my function arguments. I am not too familiar with using pointers. The code below compiles without errors, but the LED does not flash as it is the case with the original sample code. See my code below.

Any help is appreciated.

Regards,

Jan

Here is my code so far

void spi_event_handler(nrf_drv_spi_evt_t const * p_event)
{
spi_xfer_done = true;
NRF_LOG_INFO("Transfer completed.\r\n");
SEGGER_RTT_WriteString(0, "SPI transfer completed!\n");
if (m_rx_buf[0] != 0)
{
NRF_LOG_INFO(" Received: \r\n");
SEGGER_RTT_WriteString(0, "SPI received!\n");
NRF_LOG_HEXDUMP_INFO(m_rx_buf, strlen((const char *)m_rx_buf));
}
}


static void initSPI(void)
{
nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
spi_config.ss_pin = SPI_SS_PIN; // Slave Select pin
spi_config.miso_pin = SPI_MISO_PIN; // Output
spi_config.mosi_pin = SPI_MOSI_PIN; // Input
spi_config.sck_pin = SPI_SCK_PIN; // Clock pin
APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler));
}

void ReadWriteSPI(nrf_drv_spi_t spi, uint8_t m_tx_buf, uint8_t m_rx_buf, uint8_t m_length)
{
// Reset rx buffer and transfer done flag
memset(m_rx_buf, 0, m_length);
spi_xfer_done = false;
APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, m_tx_buf, m_length, m_rx_buf, m_length));


while (!spi_xfer_done)
{
__WFE(); // Wait For Event
}

NRF_LOG_FLUSH();

bsp_board_led_invert(BSP_BOARD_LED_0);
nrf_delay_ms(1000);

}

int main(void)

{

 initSPI();

    while (1)

     {

      ReadWriteSPI(spi, m_tx_buf, m_rx_buf, m_length);

     }

}

Parents Reply Children
No Data
Related