Sonsivri

Electronics => AVR, 8051 Family and ARM Area => Topic started by: pamkmitnb on July 21, 2008, 08:28:26 08:28



Title: interrupts when it receives a byte.
Post by: pamkmitnb on July 21, 2008, 08:28:26 08:28
Im making a system where I'm using all the above mentioned interrupts.
I have had the TC0, PWM and the ADC running nicely until now, but when
I add the UART0 the TC0 goes dead (it's running a 1 Khz clock)

I want the UART0 to give me interrupts when it receives a byte and
only when that occours. When I send data I don't want to receive any
interrupts (It's a Master/Slave configuration where I send som
commands and the remote unit answers)

I have configured TC0 at Vic slot 0, the ADC at 1 and now the UART0 at 2.

I've read some posts that could indicate that all those interrupts
might not work together. On the other hand, it could be my init or ISR
that is wrong ...

My init and ISR for the UART0 is as follows:
void UART::init()
{
PINSEL0 |= 0x000000005; // Enable RxD0 and TxD0
U0LCR = 0x03; // 8 bits, no Parity, 1 Stop bit
U0IER = 0; // Disable UART0 RX

setBaudRate(9600); // 9600 Baud Rate @ 15MHz VPB Clock

VICVectAddr2 = (unsigned long)UART_ISR;
VICVectCntl2 = 0x20 | 6; /* UART1 Interrupt */
VICIntEnable = 1 << 6; /* Enable UART1 Interrupt */

U0FCR = 0x07; // Enable FIFOs and reset them
U0IER = 1; // Enable UART0 RX
uint32_t dummy = U0IIR; // Read IrqID - Required to Get Interrupts
Started
}

void UART::setBaudRate(uint32_t baudRate)
{
uint32_t DLreload;
//Hardcoded to 15 MHz PClk (VPBDIV = 0x0)
DLreload = static_cast<uint32_t>((15000000u / baudRate) / 16u);

U0LCR |= 0x80; /* Set DLAB */
U0DLL = DLreload;
U0DLM = (DLreload >> 8);
U0LCR &= ~0x80; /* Clear DLAB */
}

void UART_ISR(void)
{

uint8_t interruptMask = U0LSR; //Also resets interrupts in LSR

uint8_t IIR = U0IIR;

//Active low
if((IIR & 0x1) == 0)
{
//Data ready in RxBuffer
if((IIR & 0x04) != 0)
{
if(!(interruptMask & 0x01))
{
uint8_t byte = U0RBR;
//Handle byte ......
}
}
}

// Acknowledge interrupt in VIC
VICVectAddr = 0u;
}


Title: Re: interrupts when it receives a byte.
Post by: trunghaudt2 on July 23, 2008, 07:42:38 07:42
#include "LPC214x.H"                        /* LPC21xx definitions */
#include "type.h"
#include "target.h"
#include "irq.h"
#include "uart.h"

DWORD UART0Status;
BYTE UART0TxEmpty = 1;
BYTE UART0Buffer[BUFSIZE];
DWORD UART0Count = 0;

/*****************************************************************************

void UART0Handler (void) __irq
{
    BYTE IIRValue, LSRValue;
    BYTE Dummy;

    IENABLE;            /* handles nested interrupt */   
    IIRValue = U0IIR;
   
    IIRValue >>= 1;         /* skip pending bit in IIR */
    IIRValue &= 0x07;         /* check bit 1~3, interrupt identification */
    if ( IIRValue == IIR_RLS )      /* Receive Line Status */
    {
   LSRValue = U0LSR;
   /* Receive Line Status */
   if ( LSRValue & (LSR_OE|LSR_PE|LSR_FE|LSR_RXFE|LSR_BI) )
   {
       /* There are errors or break interrupt */
       /* Read LSR will clear the interrupt */
       UART0Status = LSRValue;
       Dummy = U0RBR;      /* Dummy read on RX to clear
               interrupt, then bail out */
       IDISABLE;
       VICVectAddr = 0;      /* Acknowledge Interrupt */
       return;
   }
   if ( LSRValue & LSR_RDR )   /* Receive Data Ready */         
   {
       /* If no error on RLS, normal ready, save into the data buffer. */
       /* Note: read RBR will clear the interrupt */
       UART0Buffer[UART0Count] = U0RBR;
       UART0Count++;
       if ( UART0Count == BUFSIZE )
       {
      UART0Count = 0;      /* buffer overflow */
       }   
   }
    }
    else if ( IIRValue == IIR_RDA )   /* Receive Data Available */
    {
   /* Receive Data Available */
   UART0Buffer[UART0Count] = U0RBR;
   UART0Count++;
   if ( UART0Count == BUFSIZE )
   {
       UART0Count = 0;      /* buffer overflow */
   }
    }
    else if ( IIRValue == IIR_CTI )   /* Character timeout indicator */
    {
   /* Character Time-out indicator */
   UART0Status |= 0x100;      /* Bit 9 as the CTI error */
    }
    else if ( IIRValue == IIR_THRE )   /* THRE, transmit holding register empty */
    {
   /* THRE interrupt */
   LSRValue = U0LSR;      /* Check status in the LSR to see if
               valid data in U0THR or not */
   if ( LSRValue & LSR_THRE )
   {
       UART0TxEmpty = 1;
   }
   else
   {
       UART0TxEmpty = 0;
   }
    }
   
    IDISABLE;
    VICVectAddr = 0;      /* Acknowledge Interrupt */
}

/*****************************************************************************

*****************************************************************************/
DWORD UARTInit( DWORD baudrate )
{
    DWORD Fdiv;

    PINSEL0 = 0x00050005;       /* Enable RxD1 and TxD1, RxD0 and TxD0 */

    U0LCR = 0x83;               /* 8 bits, no Parity, 1 Stop bit    */
    Fdiv = ( Fpclk / 16 ) / baudrate ;   /*baud rate */
    U0DLM = Fdiv / 256;                     
    U0DLL = Fdiv % 256;   
    U0LCR = 0x03;               /* DLAB = 0                         */
    U0FCR = 0x07;      /* Enable and reset TX and RX FIFO. */

    if ( install_irq( UART0_INT, (void *)UART0Handler ) == FALSE )
    {
   return (FALSE);
    }
   
    U0IER = IER_RBR | IER_THRE | IER_RLS;   /* Enable UART0 interrupt */
    return (TRUE);
}

*****************************************************************************

void UARTSend(BYTE *BufferPtr, DWORD Length )
{
    while ( Length != 0 )
    {
   while ( !(UART0TxEmpty & 0x01) );   /* THRE status, contain valid
                  data */
   U0THR = *BufferPtr;
   UART0TxEmpty = 0;   /* not empty in the THR until it shifts out */
   BufferPtr++;
   Length--;
    }
    return;
}
084987876966


Title: Re: interrupts when it receives a byte.
Post by: pamkmitnb on July 25, 2008, 01:16:01 13:16
Thank You. ....... :) ;)