How to use SPI with nRF52 Boards (Adafruit Feather)

I have 2 boards:

  • nRF52840 (Transmitter)
  • nRF52832 (Receiver)

The transmitter has already been coded and i want to receive data off of it from the receiver but after trying for days, i can not figure out how to do it. As most people mentioned, there is a line SPCR = (1 << SPIE) | (1 << SPE) ; but this does not work for the boards i am using, i tried to follow the Nordic Documentation but there are no examples which could actually help me implement it.

Also, most of the guides i followed set pins to high low then transfer then set it to high but the transmission code seems to do the opposite (High, transfer, low).

Receiver Code:

#include <SPI.h>
#include "RTTStream.h"

RTTStream rtt;
uint8_t receivedData[128];

SPISettings mySettings(250000, MSBFIRST, NRF_SPIM_MODE_0);
SPIClass lfrSPI(NRF_SPIM0, MISO, SCK, MOSI);

void setup()
{
	rtt.println("\nStarting RFID Chip");

	pinMode(MISO, INPUT);
	lfrSPI.begin();
	
}

void loop()
{
	rtt.println("Starting SPI transfer");

	lfrSPI.beginTransaction(mySettings);
	lfrSPI.transfer(receivedData, 128);
	lfrSPI.endTransaction();

	rtt.println("Transfer Done");
	for (int i = 0; i < sizeof(receivedData); i++)
	{
		rtt.printf("%d", receivedData[i]);
	}
	rtt.println();

	delay(101);
}

I am getting all 0s on the receiver side. It does not seem to get any data.

If anyone could tell me what i am doing wrong, or better yet. lead me to an example script for my use-case, i would greatly appreciate it. Thanks!

Fundamentally, the Arduino core’s SPI library should provide SPI slave functionality. Looks in this case like it isn’t there, so at least for the SPI slave, called confusingly “transmitter” in your example, you must use the native APIs. Seems like there’s a good basis for this at https://github.com/adafruit/Adafruit_nRF52_Arduino/issues/753#issuecomment-1496892777. For the SPI master side (confusingly called “receiver” in your post), you should be able to use the standard SPI library as-is.

Edit: Ahh, it seems actually “receiver” is the SPI Slave, and “transmitter” is the SPI master. Still, both do transmitting and receiving in the course of a SPI transaction well-

1 Like

Thank you for your reply. I am rather new to embedded programming so forgive me if i say stuff that sounds silly

The issue you pointed out seems to be closed, should that not fix the issue that i am having?

I understand SPI is a duplex protocol but my main use-case for it is for one side to send and the other to receive as shown in my code. The library seems to work for the transmitter (Master) but it does not seem to work for the slave.

Is there anything other that i can do other than using the native APIs and what exactly do you mean by native APIs, are they the libraries with functions like nrf_drv....

Could you point me in the right direction for my requirements so i start working on this, thanks!

No, they closed the issue as “fixed” by adding in the missing SPIS (SPI slave) driver code in the core, otherwise you wouldn’t be able to call into that API at all. In my opinion that issue shouldn’t have been closed as it wasn’t integrated into the SPI library at all.

nrfx_spis_init, etc.

1 Like