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

how use nRF5 SDK14.2.0 SPI driver functions?

Hi DevZone,

I am working on a CAN Bus library for communication between nRF52840 Preview DK and MCP2515 chipset. I am trying to modify base on an Arduino MCP2515 CAN Bus library but find many difficulties as I am a very junior Nordic board developer.  

Can I ask some specific API functions provided by nRF5 SDK14.2.0 SPI driver?

1. How to read and write the registers such as SPDR, SPSR and how to access the bit such as SPIF.

2. How to set and reset the PIN: MOSI, MISO, SCK and CS?

Best regards

Ree

  • Hi,

    There is an SPI master example application in our SDK, that show how to use the SPI master driver. The driver is also described in details in the SDK documentation.

    Best regards,
    Jørgen

  • Thanks, 

    I find function nrf_drv_spi_transfer is used to transfer data from nRF to SPI peripheral device. Could you please tell me 

    1) which function is used to read the data send from SPI peripheral device?

    2) Is it possible to read and write the registers such as SPDR, SPSR and how to access the bit such as SPIF?

    Sorry about these specific questions. The SPI peripheral I am using is a Arudino shield which uses a 3rd party library and AVR library, in that library, it uses SPDR, SPSR register and set register's bit.  

  • nrf_drv_spi_transfer() is used for both trasmitting and receiving data on SPI. SPI is normally implemented as a shift register, meaning you will have push bytes into the slave to get bytes back. Depending on what parameters you passed to nrf_drv_spi_init when you setup the SPI driver, the driver can operate in blocking or non-blocking mode. If the value NULL were passed to the parameter handler in nrf_drv_spi_init call, the received data will be available in p_rx_buffer when the call to nrf_drv_spi_transfer returns. If you have passed a event handler, the driver will operate in non-blocking mode. The received data is then available in the buffer when you receive the NRF_DRV_SPI_EVENT_DONE event in the handler.

    How you read/write registers should be described in the datasheet of the chip you are interfacing. Normally, you transmitt the address of the register you want to read/write to the slave, and then keep on transfering the data, or read the data returned by the slave. Note that the SPI peripheral will fill the RX buffer while transmitting as well, which means that the RX buffer will have to have the size of the transmitted bytes pluss the received bytes, in case of a read operation.

Related