[SPI] DUE as SPI Slave

Hey guys,

While I did managed to get the DUE configured as a Slave, the code is behaving chaotically - in the sense that it works perfectly for 15 seconds, after which the data on the MISO line gets distorted

What i mean by distorted is that I do write 0x4AD3 in the TD register, but on the oscilloscope I see something like 0xAD34. Any thoughts?

void setup()
{
  SPI.begin(ChipSelectPin);
  
  //Change the SPI Write Protection Mode Register
  //with the key and the Write Protection disable
  REG_SPI0_WPMR=0x53504900;

  REG_SPI0_MR = (0 << SPI_MR_MSTR);
  
  //SPI enable
  REG_SPI0_CR=0x1;
  
  //Changes the SPI clock and the number of bits transferred
  SPI0->SPI_CSR[0]=0x00000B80; 
}

  //wait for a valid reception: SR & SR_RDRF (Receive Data Register Full) will be set to 1
  while ((SPI0->SPI_SR & SPI_SR_RDRF) == 0)
    ;
  int16_t d = SPI0->SPI_RDR & 0x0000FFFF;

  //send a response: TDR(Transmit Data Register)
  while ((SPI0->SPI_SR & SPI_SR_TDRE) == 0)
    ;
  SPI0->SPI_TDR = 0x1234 | SPI_PCS(10);// on the oscilloscope, on the MOSI line  I get 0x4123;

Post a sketch which compiles and without magic numbers. Use header files and Sam3x datasheet to find names in place of magic numbers.

https://android.googlesource.com/platform/external/arduino-ide/+/f876b2abdebd02acfa4ba21e607327be4f9668d4/hardware/arduino/sam/system/CMSIS/Device/ATMEL/sam3xa/include/component