Go Down

Topic: (Solved) Simple UART recieve interrupt not working  (Read 854 times) previous topic - next topic

Linkyyy

Hello all!
Im having some problems with the UART interrupt.
The transmitter is working fine, but it doesnt seem to enter the recieve interrupt routine when i send a character to the board. Did i miss some step for enabling the interrupt vector?

This is the code:

Code: [Select]

#include "sam.h"

volatile uint8_t RecChar = 0;
volatile uint8_t REC_FLAG = 0;

void UART0_Handler(void)
{
PIOB->PIO_SODR |= PIO_PB27;

if ((UART->UART_SR & UART_SR_RXRDY) == UART_SR_RXRDY)
{
RecChar = UART->UART_RHR;
REC_FLAG = 1;
}
}

void InitUART(void)
{
REG_PIOA_ABSR &=  ~(PIO_ABSR_P8); //Peripheral A på pin PA8 (dvs. aktiv pullup)
REG_PIOA_ABSR &=  ~(PIO_ABSR_P9); //Peripheral A på pin PA9 (dvs. aktiv pullup)
REG_PIOA_PDR |= PIO_PDR_P8;  //Disable normal pin funktion på PA8
REG_PIOA_PDR |= PIO_PDR_P9; //Disable normal pin funktion på PA9

PIOA->PIO_PUER = PIO_PA8A_URXD | PIO_PA9A_UTXD; //Enable pullup på RX og TX

PMC->PMC_PCER0 |= PMC_PCER0_PID8; //Enable UART0 clock

UART->UART_MR |= UART_MR_PAR_NO; //Ingen parity bit

//baud rate:
//CD = frequency of the peripheral clock / (16 x Desired Baud Rate)
//Sætter til 19200 baud
//CD = 84MHz/(16* 19200)
UART->UART_BRGR = 273;

UART->UART_IER |= UART_IER_RXRDY; //Enable recieve interrupt
NVIC_EnableIRQ(UART_IRQn); //enable UART i interrupt controlleren

UART->UART_CR = UART_CR_TXEN |UART_CR_RXEN; //Enable transmit og recieve
}

int main(void)
{
    /* Initialize the SAM system */
    SystemInit();
    __enable_irq();
    InitUART();


PIOB->PIO_PER |= PIO_PB27;
PIOB->PIO_OER |= PIO_PB27;
PIOB->PIO_CODR |= PIO_PB27;


    while (1)
    {
if(REC_FLAG == 1)
{
REC_FLAG = 0;
while (!(UART->UART_SR & UART_SR_TXRDY));
while(!(UART->UART_SR & UART_SR_TXEMPTY)); //Vent på at byte bliver sendt, hvis den ikke er blevet sendt endnu

REG_UART_THR = RecChar;
}

    }
}

ard_newbie


Instead of :
void UART0_Handler(void) {...

I would try this:

void UART_Handler(void) __attribute__((weak));

void UART_Handler(void)
{ ......

Linkyyy

Hey, i tried adding that prototype, but it doesnt seem to help :(

ard_newbie


Your code has several issues, one of them was the UART0_Handler which doesn't exist AFAIK.

An other one might be the pull-ups.

Comment the line PIOA->PIO_PUER.....

See this other thread : http://forum.arduino.cc/index.php?topic=143080.0

Linkyyy

#4
Jan 14, 2017, 04:02 pm Last Edit: Jan 14, 2017, 04:09 pm by Linkyyy
Oh my... I did not realize i had written UART0, bad habit from mega2560 where all the USART modules end with a number :P

Now it works, although i had to remove that attribute prototype too, but thanks for your quick help!

Go Up