I'm trying to build up a communication between Raspberry Pi 3B+ and Arduino Due via SPI. I have chosen the Raspberry Pi to act as master and the Arduino as slave. Therefore I have written some C Code for the Raspberry that configurates the interface and sends two byte of data via the MOSI line. I observed the signals with a logic analyzer, they are as expected, the Chip Select drops to zero at the start of the send process and rises afterwards again.
For programming the Arduino Due I am working with the Arduino IDE. Since the Arduino SPI.h does not support slave mode, I want to access the corresponding registers on the ATSAM3X8E directly. First, I define the Mode of the SPI Pins (Pin numbers from the 'unofficial' Due Pinout) as Input / Output. Afterwards I configure the registers of the ATSAM3X8E for SPI slave operation. The settings for CPOL and CPHA are the same as on the Raspberry. Since the devider for the serial clock baud rate is not allowed to be zero, I have chosen the biggest available devider, though I assume that this setting has no effect, because the master defines the transmission speed. This is the code for configuration:
pinMode(74, OUTPUT); // PIN_SPI_MISO
pinMode(75, INPUT); // PIN_SPI_MOSI
pinMode(76, INPUT); // PIN_SPI_SCK
pinMode(10, INPUT); // Slave Select
REG_SPI0_CR = 0x00000001; // SPI Enable
REG_SPI0_MR = 0x00000000; // Slave-Mode, Fixed Peripheral Select,
// Chip Select directly connected
// Mode Fault detection enabled,
// Loopback disabled
REG_SPI0_WPMR = 0x00000000; // Write Protection disabled
REG_SPI0_CSR = 0x0000ff02; // Chip select Register
// CPOL = 0, CPHA = 1
// 8 Bit per transfer
// serial Clock Baud Rate of 84 MHz/255=330 kHz
After that, in a loop the bits of the Data Receive Register are stored in a buffer :
unsigned int buf = 0;
buf = REG_SPI0_RDR & 0x0000ffff; // store 16 Bits of Receive Data Registers in buffer
However, the buffer remains empty. If I read the bits of the status register, all bits are zero, even the bit for "NSS Rising Edge Detected" or "SPI Enbled" are zero. Therefore I am assuming, the initialisation of the interface is not correct / complete. Did I miss anything ?
Any help is greatly appreciated.
Using
pinMode(74, OUTPUT);
and similar Arduino function calls you just change pin direction but not change pin usage as GPIO. For using these pins as SPI lines you should setup pin multiplexer.See in ATSAM3X8E datasheet