I have an SPI slave working well with an Arduino Mega 2560, but want to port the project to the Uno R4 WiFi, and it's proving to be very challenging -
Basically there is no official support for SPI slave mode in the Arduino core libraries - but with the AVR based designs the config was pretty straight forward and there were plenty of examples.
For the Uno R4, the only reference I found was : here
Which is helpful in that it has some very low level register access decoded, but it does not work for my case when I try the code. I believe I've updated the IO to the correct ports (P102, P103 and P411), but I'm getting 0s in the received data.
One other thing about my use-case. The format is 'SPI' in that it has a CS transition, and then there is clock+data. The data however is 96 bits long. In the AVR case, it would simply read every 8 bits and write it to the SPDR, which I would pop off. Does anyone know if the RA4M1 SPI receiver would work similarly, or if it detects more data, would it simply drop/abort the transaction? I fear that may be the reason, but cannot be sure.
It is an old heating controller that is driving an SPI-like protocol, and I'm trying to tap-into that bus to observe the data it is sending. Its has CS, DI, SCK, and a 96-bit long fixed frame size.
This is working with the SPI HW in the Arduino Mega 2560, reading 8-bit chunks at a time.
Potentially switching to 3-wire mode may work with the Uno R4, as I think in that case it cannot get upset if the frame length as denoted by CS is not between 8 to 32 bits - at least that's my working theory for how I can receive 96-bits of data with an SPI block that has a max width of 32 bits!
Thanks. I started looking at the FSP. It seems this is what the Arduino Core SPI Class (master only) uses.
One thing that is confusing is the SPI vs SCI - It seems for the Uno R4 Wifi, the default SPI pins (D13-D10) are mapped to the hardware SPI0 - is this the case? And the SCI would be mapped to different pins? The device is so configurable, it's quite confusing, so if you have any insight into that it would be appreciated!
Just an update - I will follow up with a more comprehensive one later -
I started looking at the FSP code, and the code within the Arduino SPI.cpp (master only), which uses FSP. This led me to discover that some of the pins were not correctly configured:
The bare metal code was created for the R4 Minima, so the pins were different for the R4 Wifi, requiring using Port4 (410 and 411) - and there was a typo in the code where pins 410 and 411 were actually associated with 110 and 111 - so the IO was not getting connected to SPI.
The SPI.cpp does not seem to use SS at all - when it sets up the pins it only uses MISO/MOSI/SCK - and as the slave mode needs SS to frame the transfer, it does not work. This actually confuses me a bit - when in master mode, does it not drive SS at all? You'd expect it would have to...
After than I was able to make something work, though it's still half-way between using FSP and the baremetal approach. I need to clean it up before posting.... but I'm glad to say I have something functional
That's interesting. I would have assumed its necessary to frame transfers.
Without SS being used, how does a slave ensure its seeing the right bits - i.e. if a master is transmitting and a slave powers up, it could receive what is its first bit 1/2 way through a transfer, and it would be from that point on out of sync with the master - so I had naturally assumed that SS would be part of every transfer in some form - though I supposed because it is flexible, perhaps that is why it is left to the user to figure out. It would still useful if the SPI.cpp setup the pin so it could be used though, as it needs some PFS (Port Function Select) bits set on it for the Uno R4 - right now SPI.cpp does not enable it at all.
Any update on using R4 Wifi as a slave? I've been looking into how to do this over the past few days and am curious what your experience using it has been like
Yes, I manged to get it to work. It was a bit messy, but I basically setup the IO using code take from the Arduino master SPI library, and then accessed the low level SPI registers to read the SPI data register while also sending dummy transmit data (a requirement for it to work). I didn't use interrupt mode, which is what I had used in an AVR based Arduino previously. Using the renesas FSP library may be a better option in future, but as what I have works the pressure to update it is low.
I was planning on uploading my code to GitHub, as part of my overall project (hot tub wifi controller using the Uno) but have not done it yet...