Skip to content

Example code: Early data read clears RXC interrupt flag #5

@paperman5

Description

@paperman5

Hi there,
Thanks very much for creating this library. I was trying to get the example code to work for SERCOM4 on my Adafruit Feather M0 Adalogger and was finding that the Data Received Interrupt was never being triggered, and spent quite some time tracking down the issue. It looks like there is a misunderstanding in the example code as to how the RXC flag is cleared:

void SERCOM4_Handler()
/*
Reference: Atmel-42181G-SAM-D21_Datasheet section 26.8.6 on page 503
*/

{
  #ifdef DEBUG
    Serial.println("In SPI Interrupt");
  #endif
  uint8_t data = 0;
  data = (uint8_t)SERCOM4->SPI.DATA.reg;
  uint8_t interrupts = SERCOM4->SPI.INTFLAG.reg; // Read SPI interrupt register
...
  // Data Received Complete interrupt: this is where the data is received, which is used in the main loop
  if (interrupts & (1 << 2)) // 0100 = bit 2 = RXC // page 503
  {
    #ifdef DEBUG
      Serial.println("SPI Data Received Complete interrupt");
    #endif
    data = SERCOM4->SPI.DATA.reg; // Read data register
    SERCOM4->SPI.INTFLAG.bit.RXC = 1; // Clear Receive Complete interrupt
  }
...

However the SAMD21 datasheet states on pg 503 (INTFLAG reference):

Bit 2 – RXC: Receive Complete
This flag is cleared by reading the Data (DATA) register or by disabling the receiver.
This flag is set when there are unread data in the receive buffer. If address matching is enabled, the first data
received in a transaction will be an address.
Writing a zero to this bit has no effect.
Writing a one to this bit has no effect.

So the DATA register read at the beginning of the interrupt handler in the example code is inadvertently clearing the RXC flag right before the interrupt flags are read, and therefore the RXC codepath is never hit.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions