Recently I had the chance of spending time programming an Arduino Due.
The idea was to make it work with SPI in a slave mode. Unfortunally, the Arduino library only has support for master mode, so I had to go through the SAM3X8E datasheet and find out how I would do it.
This was also the first time I ever bare-metal programmed an 32 bit chip, normally for hobbies, I use the Arduino library.
Apart from sharing the code that might be useful for someone else, I want to share some other stuff that I leanerd while trying to establish the SPI communication.
First thing: 32 bit microcontrollers are much more complex than 8 bit ones. Ok, it might sound obvious but it really is much more complex!
Pin handling: Pins are controlled by a PIO which stands for Parallel Input/Output Controller. Problem is, imagine that the pin also has another function, like SPI MISO line. It is necessary to disable the controller in order to access it!
That’s why I use something like this in the code:
I disable the PIO A from controlling the P25 to P28, which are the ones we need to SPI protocol.
From the page 40 of the datasheet, we can see that:
As a peripheral A, PA25 has SPI0_MISO and as a peripheral B has the A18, which I suppose that is an ADC pin.
Almost everything has to be powered on: apart from enabling the access to the SPI peripheral, it is also necessary to turn on the peripheral power for the instance ID.
The SPI0 instance ID is 24, so we can turn it on by: