Arduino DUE, Serial interrupt procedure to wakeup from sleep mode

3.8k views Asked by At

i'd like to put the SAM3X chip on sleepmode until a character arrives on the serial port. i was thinking of using an ausiliary flag in the Serial interrupt procedure in order to trigger the wake up procedure? what do you think abou? any advice or any other way i should follow or try?

1

There are 1 answers

0
mpflaga On

I recommend reviewing .\arduino-1.5.2\hardware\arduino\sam\cores\arduino\UARTClass.cpp as its UARTClass::begin will detail how the Arduino Framework initializes the SAM's Serial IRQ with:

// Configure interrupts
_pUart->UART_IDR = 0xFFFFFFFF;
_pUart->UART_IER = UART_IER_RXRDY | UART_IER_OVRE | UART_IER_FRAME;

// Enable UART interrupt in NVIC
NVIC_EnableIRQ(_dwIrq);

// Enable receiver and transmitter
_pUart->UART_CR = UART_CR_RXEN | UART_CR_TXEN ;

Where you will need to ensure the baud rate generator is not stopped while in sleepmode.

Along with reading the SAM3X data sheet starting with 5.5.3. which leaves the peripheral clocks enabled.

looks like you may be able to insert the wake up into the UARTClass::IrqHandler

void UARTClass::IrqHandler( void )
{
  uint32_t status = _pUart->UART_SR;

  // Did we receive data ?
  if ((status & UART_SR_RXRDY) == UART_SR_RXRDY)
  {
    // wake up!!!! Not sure if you even need to wakeup, it should from the sleepmode
    _rx_buffer->store_char(_pUart->UART_RHR);
  }

With regards to sleeping: \arduino-1.5.2\hardware\arduino\sam\system\libsam\source\pmc.c

Line 972: void pmc_enable_sleepmode(uint8_t uc_type)
Line 988: void pmc_enable_waitmode(void)
Line 1009: void pmc_enable_backupmode(void)

So I would suspect the following:

pmc_enable_sleepmode(WFI);

would put the unit a sleep and the IrqHandler of the UART_SR_RXRDY would wake itself up, without any code change.


One other alternative would be to use the serial pin's IO as to trigger an interrupt.

attachInterrupt(0, EnableSerialRX, CHANGE);

It would though, loose at least the first byte. the trade off is that you can use the lower power modem of pmc_enable_backupmode() rather than sleep