i want to implement a SPI Slave on my Arduino Nano 33 BLE. I've already detected, that the SPI.h library from Arduino is not working. It's because of the processor. The Nano 33 BLE has the nRF52840 and the registers have different names.
#include "mbed.h"
SPISlave device(D12, D11, D13, D10); // mosi, miso, sclk, ssel
int main()
{
device.reply(0x00); // Prime SPI with first reply
while (1) {
if (device.receive()) {
int v = device.read(); // Read byte from master
v = (v + 1) % 0x100; // Add one to it, modulo 256
device.reply(v); // Make this the next reply
}
}
}
My problem now is, that I get the error, that SPISlave doesn't exists as a type:
src\main.cpp:9:1: error: 'SPISlave' does not name a type; did you mean 'SPIName'?
SPISlave device(D12, D11, D13, D10); // mosi, miso, sclk, ssel
^~~~~~~~
SPIName
src\main.cpp: In function 'int main()':
src\main.cpp:14:5: error: 'device' was not declared in this scope
device.reply(0x00); // Prime SPI with first reply
^~~~~~
src\main.cpp:14:5: note: suggested alternative: 'dev_t'
device.reply(0x00); // Prime SPI with first reply
^~~~~~
dev_t
C:\Users\mabs.platformio\packages\framework-arduino-nrf52-mbedos\cores\arduino\USB\USBCDC.cpp: In member function 'virtual const uint8_t* arduino::USBCDC::configuration_desc(uint8_t)':
C:\Users\mabs.platformio\packages\framework-arduino-nrf52-mbedos\cores\arduino\USB\USBCDC.cpp:544:35: warning: narrowing conversion of '((((arduino::USBCDC*)this)->arduino::USBCDC::extraDescriptor != 0) ? 5 : 0)' from 'int'
to 'uint8_t' {aka 'unsigned char'} inside { } [-Wnarrowing]
(extraDescriptor != NULL) ? 0x5 : 0x0, // iInterface
*** [.pio\build\nano33ble\src\main.cpp.o] Error 1
---
Does anyone know what the problem could be? In the SPISlave.h library are some code parts inactive, but it should be possible to use a object. I didn't change anything in the library, here is the part with the constructor:
---
class SPISlave : private NonCopyable<SPISlave> {
public:
/** Create a SPI slave connected to the specified pins.
*
* @note Either mosi or miso can be specified as NC if not used.
*
* @param mosi SPI Master Out, Slave In pin.
* @param miso SPI Master In, Slave Out pin.
* @param sclk SPI Clock pin.
* @param ssel SPI Chip Select pin.
*/
SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel);
I use the Arduino IDE and Platform.io together with VS Code. I'm new to SPI so have no experience how to implement a Slave without the SPI.h library.
If someone knows what the problem is or if somebody has other solutions to implement a SPI Slave I would be very grateful.
If more information is needed just comment and I try to find more information.
I've already detected, that the SPI.h library from Arduino is not working. It's because of the processor.
Bullshit. If you select the correct board in the IDE the SPI library works great but it doesn't support SPI slave mode. The Arduino SPI library is only made for master mode. Some derivation for some platforms also support slave mode in the library but that was never part of the standard.
I use the Arduino IDE and Platform.io together with VS Code. I'm new to SPI so have no experience how to implement a Slave without the SPI.h library.
It seems that the chosen platform (Nano 33 BLE) doesn't support SPI slave mode. Is both, the board and SPI in slave mode fixed for your project? Do you have other options?
pylon:
Bullshit. If you select the correct board in the IDE the SPI library works great but it doesn't support SPI slave mode. The Arduino SPI library is only made for master mode. Some derivation for some platforms also support slave mode in the library but that was never part of the standard.
This was the answer from Arduino:
all arduino libraries are portable as soon as you don't use any architecture specific code in your sketch. In this case SPDR and friends are registers that only exists in AVR microcontrollers. If you need to implement an SPI slave there isn't a wrapper yet for Nano33BLE.
So the SPI.h is not working for Arduino Nano 33 BLE in Slave Mode. In the forum of Nick Gammon (Gammon Forum : Electronics : Microprocessors : SPI - Serial Peripheral Interface - for Arduino) they use SPI.h for Slave mode. But the code doesn't work brcause the registers are not available on the nRF52840.
pylon:
It seems that the chosen platform (Nano 33 BLE) doesn't support SPI slave mode. Is both, the board and SPI in slave mode fixed for your project? Do you have other options?
No the board is not fixed, but all other Arduinos witg the ATMega are too slow, because the SPI Speed has to be 10MHz.
SPI in Slave Mode is fixed, my task is to reply on a Master using SPI.
But the Nano 33 BLE should support SPI in Slave Mode. The problem is there is no wrapper yet. Arduino referenced me to this side (SPISlave - API references and tutorials | Mbed OS 6 Documentation) but that does not work.
Do you have any other ideas?
I changed my code a bit but now I have new problems:
#include "mbed.h"
using namespace mbed; //new line
SPISlave device(D12, D11, D13, D10); // mosi, miso, sclk, ssel
int main()
{
device.reply(0x00); // Prime SPI with first reply
while (1) {
if (device.receive()) {
int v = device.read(); // Read byte from master
v = (v + 1) % 0x100; // Add one to it, modulo 256
device.reply(v); // Make this the next reply
}
}
}
The error is now:
src\main.cpp:8:35: error: invalid conversion from 'int' to 'PinName' [-fpermissive]
SPISlave device(D11, D12, D13, D10); // mosi, miso, sclk, ssel
^
In file included from C:\Users\mabs.platformio\packages\framework-arduino-nrf52-mbedos\cores\arduino/mbed/mbed.h:67,
from C:\Users\mabs.platformio\packages\framework-arduino-nrf52-mbedos\cores\arduino/mbed.h:14,
from src\main.cpp:5:
C:\Users\mabs.platformio\packages\framework-arduino-nrf52-mbedos\cores\arduino/mbed/drivers/SPISlave.h:72:22: note: initializing argument 1 of 'mbed::SPISlave::SPISlave(PinName, PinName, PinName, PinName)'
SPISlave(PinName mosi, PinName miso, PinName sclk, PinName ssel);
---
I think there is a problem in the library. Does anyone know how to fix that error?
No the board is not fixed, but all other Arduinos witg the ATMega are too slow, because the SPI Speed has to be 10MHz.
SPI in Slave Mode is fixed, my task is to reply on a Master using SPI.
The Nano BLE runs a 64MHz ARM core, that won't be fast enough to run a more than trivial SPI slave at 10MHz.
If you use the low level mbed interface you must not use definitions of the Arduino platform (D10):
The CPU has about 6-8 clock cycles to react on an input and provide the output byte to the hardware. If the logic is not trivial that's not enough (remember the controlling MCU usually don't wait for the device, it just send clock cycles in the chosen frequency). If you tell us what exact device you're trying to imitate we may give you a better advice.