Go Down

Topic: UART Interrupt with serialEvent() (Read 2286 times) previous topic - next topic


Hey guys,

i'm trying to start/stop an timer via UART.
I thought i could use the serialEvent()-function, but it is not working.

The example from the IDE sends no inputString, since the function is never called...

Has anyone had the same problem / solution to it?

Thanks in advance!


Unfortunately there are no UART interrupts in the Due version of the Arduino IDE. You have to check for new characters on the UART via polling. There really isn't any reason for that... I mean, the cortex 3M chip does have the capability; it is just not hooked up right now.

I put this line in my Due sketch:
Code: [Select]

if (Serial.available()) serialEvent();


thanks for the input, i'll put it in the "almighty" loop ;-)


Hello...I have a doubt..you said due doesn't have interrupts...u mean by directly using the registers of SAM 3X8E ,interrupt still cannot be used or the due board doesn't have the functionality??. I am trying to use UART interrupt of Due board by directly accessing registers of the controller in it. But no success yet...
Please have a look at it.....any help always welcomed......

#define __CORTEX_M                (0x03)

// enum IRQn = {8};
//   UART_IRQn = 8;
volatile unsigned int recbyte;
unsigned int sr_reg;

#define PMC_MCKR* (volatile unsigned long*) (0x400E0630)
#define PMC_PCER0* (volatile unsigned long*) (0x400E0610)
#define PMC_WPMR* (volatile unsigned long*) (0x400E06E4)
#define PIO_PDR* (volatile unsigned long*) (0x400E0E04)
#define UART_CR* (volatile unsigned long*) (0x400E0800)
#define UART_MR* (volatile unsigned long*) (0x400E0804)
#define UART_BRGR* (volatile unsigned long*) (0x400E0820)
#define UART_THR* (volatile unsigned long*) (0x400E081C)
#define UART_SR* (volatile unsigned long*) (0x400E0814)
#define UART_RHR* (volatile unsigned long*) (0x400E0818)
//#define PIO_PUER* (volatile unsigned long*) (0x400E0E640)
#define PIO_ABSR* (volatile unsigned long*) (0x400E0E70)
#define UART_IER* (volatile unsigned long*) (0x400E0808)
#define UART_IDR* (volatile unsigned long*) (0x400E080C)
#define NVIC_ISER0* (volatile unsigned long*) (0xE000E100)
//void NVIC_SetPriorityGrouping(void);

void NVIC_EnableIRQ(void);
void NVIC_DisableIRQ(void);

void setup()
  //void NVIC_SetPriorityGrouping(void);
  //void NVIC_EnableIRQ(void);

void UART_IRQHandler(void)
     sr_reg = UART_SR;
  if((UART_SR & 0x0000001) == 0x01)
    recbyte = UART_RHR;
    UART_THR = recbyte; 

void clk_define(void)
void uart_define(void)
  UART_CR=0x50;  //control reg
  UART_MR=0x800;  //mode reg
  UART_BRGR=0x222;  //baud rate
  UART_IER=0x01;  //interrupt enable
//  NVIC_EnableIRQ(UART_IRQn);

void loop()


My previous posting was sort of wrong. You see, the arduino core for Due does use interrupts for the serial comm. It has pretty much always been there. But, until around 1.5.2 or 1.5.4 they didn't internally to the core have the line:
if (Serial.available()) serialEvent();

So you'd have to do this yourself. That line causes your function called serialEvent to be called if there is available serial data. If you look at the defintion for the method available you'll see that it is checking the serial buffer for characters. Those characters are placed there by the serial interrupt handler. Whether or not the line above is there you can always poll yourself with:
if (Serial.available())

Now, this feels like polling and it is on your end but actually you are polling a buffer that is filled by an interrupt handler. No, you don't ever get a chance to directly implement your own serial interrupt handler. The arduino core has its own handler.

These lines are automatically run every time your loop function is run:
  if (Serial.available()) serialEvent();
  if (Serial1.available()) serialEvent1();
  if (Serial2.available()) serialEvent2();
  if (Serial3.available()) serialEvent3();

As you can see, this allows you to have functions that are automatically called for four serial ports if one of them gets incoming data. All the serial ports are really interrupt driven though. Your best bet is really to implement the appropriate serialEvent function and keep your loop function short.

Go Up