Pages: [1]   Go Down
Author Topic: UART Interrupt with serialEvent()  (Read 921 times)
0 Members and 1 Guest are viewing this topic.
tyrol
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!
Logged

Earth
Offline Offline
Sr. Member
****
Karma: 14
Posts: 330
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
if (Serial.available()) serialEvent();
Logged

tyrol
Offline Offline
Newbie
*
Karma: 0
Posts: 3
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 10
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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)
#include<stdio.h>
#include<stdint.h>
#define __CM3_CMSIS_VERSION       ((__CM3_CMSIS_VERSION_MAIN << 16) | __CM3_CMSIS_VERSION_SUB)

// 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()
{
  //clk_define();
  uart_define();
  //void NVIC_SetPriorityGrouping(void);
  //void NVIC_EnableIRQ(void);
  //NVIC_SetPriorityGrouping(UART_IRQN,0x0);
   NVIC_EnableIRQ(UART_IRQn);

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

void clk_define(void)
{
  PMC_WPMR=0x00;
  PMC_MCKR=0x01;
  PMC_PCER0=0x100;
}
void uart_define(void)
{
  PMC_WPMR=0x00;
  PIO_ABSR=0x00;
  PIO_PDR=0x300;
  UART_CR=0x50;  //control reg
  UART_MR=0x800;  //mode reg
  UART_IDR=0XFFFFFFFF; 
  UART_BRGR=0x222;  //baud rate
  UART_IER=0x01;  //interrupt enable
  NVIC_ISER0=0x100; 
//  NVIC_EnableIRQ(UART_IRQn);
  //NVIC_SetPriorityGrouping(UART_IRQN,0x0);


}
void loop()
{
}
Logged

Earth
Offline Offline
Sr. Member
****
Karma: 14
Posts: 330
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: