I am using USART with DMA on at91sam7x256 for reading and writing data. AS i didn't succeeded in it i started creating a simple example, but problem exist already. The main problem is that the interrupt routine just fired the first time and i can not receive data from USART after first time!
#include "at91sam7x256.h"
char Buffer[10]; // holds received characters
unsigned long nChars = 0; // counts number of received chars
char *pBuffer = &Buffer[0]; // pointer into Buffer
void Usart0IrqHandler (void);
void USART0Setup(void);
int main(void)
{
unsigned long j;
unsigned int IdleCount = 0;
// set up USART0
USART0Setup();
// *****************************
// * endless blink loop *
// *****************************
while (1) {
for (j = 1000000; j != 0; j-- ); // wait 1 second 1000000
IdleCount++; // count # of times through the idle loop
for (j = 1000000; j != 0; j-- ); // wait 1 second 1000000
}
}
////////
void Usart0IrqHandler (void) {
volatile AT91PS_USART pUsart0 = AT91C_BASE_US0; // create a pointer to USART0 structure
if ((pUsart0->US_CSR & AT91C_US_ENDRX) == AT91C_US_ENDRX) {
// we have a end-of-receive interrupt (ENDRX)
pUsart0->US_RCR = 10; // restore the receive count - clears ENDRX flag
}
}
void USART0Setup(void) {
volatile AT91PS_USART pUSART0 = AT91C_BASE_US0; // create a pointer to USART0 structure
volatile AT91PS_AIC pAIC = AT91C_BASE_AIC; // pointer to AIC data structure
volatile AT91PS_PIO pPIO = AT91C_BASE_PIOA; // pointer to PIO data structure
volatile AT91PS_PMC pPMC = AT91C_BASE_PMC; // pointer to PMC data structure
pPMC->PMC_PCER = (1<<AT91C_ID_US0); // enable usart0 peripheral clock
pPIO->PIO_PDR = AT91C_PA0_RXD0 | AT91C_PA1_TXD0; // enable peripheral control of PA0 and PA1 (RXD0 and TXD0)
pPIO->PIO_ASR = AT91C_PIO_PA0 | AT91C_PIO_PA1; // assigns the 2 I/O lines to peripheral A function
pPIO->PIO_BSR = 0; // peripheral B function set to "no effect"
pUSART0->US_CR = AT91C_US_RSTRX | // reset receiver
AT91C_US_RSTTX | // reset transmitter
AT91C_US_RXDIS | // disable receiver
AT91C_US_TXDIS; // disable transmitter
pUSART0->US_MR = AT91C_US_PAR_NONE | // no parity
0x3 << 6; // 8-bit characters
pUSART0->US_IER = 0x00; // no usart0 interrupts enabled (no effect)
pUSART0->US_IDR = 0xFFFF; // disable all USART0 interrupts
pUSART0->US_BRGR = 0x139; // CD = 0x139 (313 from above calculation for 9600 baud)
// FP=0 (not used)
pUSART0->US_RTOR = 0; // receiver time-out (disabled)
pUSART0->US_TTGR = 0; // transmitter timeguard (disabled)
pUSART0->US_RPR = (unsigned int)Buffer; // address of DMA input buffer
pUSART0->US_RCR = 10; // we'll read in 10 chars via DMA
pUSART0->US_TPR = (unsigned int)0; // address of DMA output buffer (use same one)
pUSART0->US_TCR = 0; // we'll transmit 0 chars via DMA
pUSART0->US_RNPR = (unsigned int)0; // next DMA receive buffer address
// if set to zero, it is not used
pUSART0->US_RNCR = (unsigned int)0; // next DMA receive counter
// if set to zero, it is not used
pUSART0->US_TNPR = (unsigned int)0; // next DMA transmit buffer address
// if set to zero, it is not used
pUSART0->US_TNCR = (unsigned int)0; // next DMA transmit counter
// if set to zero, it is not used
pUSART0->US_PTCR = AT91C_PDC_RXTEN | // enable receive transfer,
AT91C_PDC_TXTDIS; // disable transmit transfer
// Disable USART0 interrupt in AIC Interrupt Disable Command Register
pAIC->AIC_IDCR = (1<<AT91C_ID_US0);
// Set the USART0 IRQ handler address in AIC Source
pAIC->AIC_SVR[AT91C_ID_US0] =
(unsigned int)Usart0IrqHandler; // Vector Register[6]
// Set the interrupt source type and priority
pAIC->AIC_SMR[AT91C_ID_US0] =
(AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL | 0x4 ); // in AIC Source Mode Register[6]
// Enable the USART0 interrupt in AIC Interrupt Enable Command Register
pAIC->AIC_IECR = (1<<AT91C_ID_US0);
// enable the USART0 receiver and transmitter
pUSART0->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
// enable the USART0 end-of-receive interrupt
pUSART0->US_IER = AT91C_US_ENDRX; // enable ENDRX usart0 end-of-receive interrupt
pUSART0->US_IDR = ~AT91C_US_ENDRX; // disable all interrupts except ENDRX
}
//------------------
// end of file
//------------------
There was two problem:
1- As i use Keil uVision i need to use __irq
for defining interrupt service routine!
2- The surprising one is that the end of interrupts not fired! so use:
*AT91C_AIC_EOICR = 0;
void __irq Usart0IrqHandler (void) {
volatile AT91PS_USART pUsart0 = AT91C_BASE_US0; // create a pointer to USART0 structure
if ((pUsart0->US_CSR & AT91C_US_ENDRX) == AT91C_US_ENDRX) {
pUsart0->US_RPR = (unsigned int)Buffer; // we have a end-of-receive interrupt (ENDRX)
pUsart0->US_RCR = 10; // restore the receive count - clears ENDRX flag
}
*AT91C_AIC_EOICR = 0; // end of interrupts
}