//*----------------------------------------------------------------------------
//* ATMEL Microcontroller Software Support - ROUSSET -
//*----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*----------------------------------------------------------------------------
//* File Name : interrupt_Usart.c
//* Object : USART Interrupt Management
//*
//* 1.0 03/Jun/03 JPP : Creation
//* 1.1 29/Aug/05 JPP : Update AIC definion
//*----------------------------------------------------------------------------
// Include Standard LIB files
#include "Board.h"
#include "Main.h"
#define TRACE
#define USART_INTERRUPT_LEVEL 7
static const char atmel_header[]=
{
"\n\r *** ATMEL Usart PDC ***\n\r"
"Copyright (C) 2004 ATMEL Corporations Version: 1.0\n\r"
};
AT91PS_USART COM0;
#define USART_BAUD_RATE 115200
static char *CurrentAddress;
static unsigned short BlockSize;
static char BlockMode;
static int RestSize;
//*------------------------- Internal Function --------------------------------
//*------------------------- Interrupt Function -------------------------------
//*----------------------------------------------------------------------------
//* Function Name : Usart_c_irq_handler
//* Object : C handler interrupt function called by the interrupts
//* assembling routine
//* Input Parameters : <RTC_pt> time rtc descriptor
//* Output Parameters : increment count_timer0_interrupt
//*----------------------------------------------------------------------------
void Usart_c_irq_handler(void)
{
AT91PS_USART USART_pt= COM0;
unsigned int status;
//* get Usart status register
status = USART_pt->US_CSR;
if (BlockMode)
{
//* check if interrupt is present and available
if ( (status & AT91C_US_ENDTX) & (USART_pt->US_IMR & AT91C_US_ENDTX) ){
#ifdef TRACE
AT91F_DBGU_Printk("TX\n\r");
#endif
//* Last block has been transmitted
//* ---- Send more One Block
//* Set the next pointer register
CurrentAddress += BlockSize;
USART_pt->US_TNPR = (unsigned int) CurrentAddress;
if(RestSize <= BlockSize) {
//* ---- Send last tow Blocks
USART_pt->US_TNCR = RestSize;
RestSize = 0;
AT91F_US_DisableIt(USART_pt,AT91C_US_ENDTX );
} else {
//* ---- Send another Blocks
USART_pt->US_TNCR = BlockSize;
RestSize -= BlockSize;
}
}
// check if interrupt is present and available
if ( (status & AT91C_US_TXBUFE) & (USART_pt->US_IMR & AT91C_US_TXBUFE) ){
BlockMode = false;
AT91F_US_DisableIt(USART_pt, AT91C_US_TXBUFE );
#ifdef TRACE
AT91F_DBGU_Printk("End Interrupt\n\r");
#endif
#ifdef TRACE
sprintf((char *)message,"IT:ass 0x%X size %d\n\r",CurrentAddress,RestSize);
AT91F_DBGU_Printk((char *)message);
#endif
}
}
else
{
if ( status & AT91C_US_ENDRX){
//* Acknowledge Interrupt
AT91F_US_ReceiveFrame(USART_pt,(char *)message,10,0,0);
//* Get byte and send
//* Trace on DBGU
AT91F_US_SendFrame((AT91PS_USART) AT91C_BASE_DBGU, "ENDRX\n\r",7,0,0);
}
// check if interrupt is present and available
if ( (status & AT91C_US_ENDTX) & (USART_pt->US_IMR & AT91C_US_ENDTX) ){
//* Acknowledge Interrupt by mask for next send
AT91F_US_DisableIt(USART_pt, AT91C_US_ENDTX );
//* Trace on DBGU
AT91F_US_SendFrame((AT91PS_USART) AT91C_BASE_DBGU, "ENDTX\n\r",7,0,0);
}
// check if interrupt is present and available
if ( (status & AT91C_US_TXBUFE) & (USART_pt->US_IMR & AT91C_US_TXBUFE) ){
//* Acknowledge Interrupt by mask for next send
AT91F_US_DisableIt(USART_pt, AT91C_US_TXBUFE );
//* Trace on DBGU
AT91F_US_SendFrame((AT91PS_USART) AT91C_BASE_DBGU, "TXBUFE\n\r",8,0,0);
}
if ( status & AT91C_US_OVRE) {
//* clear US_RXRDY
AT91F_US_GetChar(USART_pt);
//* Trace on DBGU
AT91F_US_SendFrame((AT91PS_USART) AT91C_BASE_DBGU, "O",1,0,0);
}
//* Check error
if ( status & AT91C_US_PARE) {
//* Trace on DBGU
AT91F_US_SendFrame((AT91PS_USART) AT91C_BASE_DBGU, "P",1,0,0);
}
if ( status & AT91C_US_FRAME) {
//* Trace on DBGU
AT91F_US_SendFrame((AT91PS_USART) AT91C_BASE_DBGU, "F",1,0,0);
}
if ( status & AT91C_US_TIMEOUT){
USART_pt->US_CR = AT91C_US_STTTO;
//* Trace on DBGU
AT91F_US_SendFrame((AT91PS_USART) AT91C_BASE_DBGU, "T",1,0,0);
}
}
//* Reset the satus bit
USART_pt->US_CR = AT91C_US_RSTSTA;
}
//*-------------------------- External Function -------------------------------
//*----------------------------------------------------------------------------
//* \fn AT91F_US_Printk
//* \brief This function is used to send a string through the US channel
//*----------------------------------------------------------------------------
void AT91F_US_Printk( char *buffer) // \arg pointer to a string ending by \0
{
while(*buffer != '\0') {
while (!AT91F_US_TxReady(COM0));
AT91F_US_PutChar(COM0, *buffer++);
}
}
//*----------------------------------------------------------------------------
//* \fn AT91F_US_Print_frame
//* \brief This function is used to send a Frame through the US channel
//*----------------------------------------------------------------------------
void AT91F_US_Print_frame(char *buffer, unsigned short counter) // \arg pointer to a string ending by \0
{
//* Enable USART IT error and AT91C_US_ENDRX
AT91F_US_SendFrame(COM0,buffer,counter,0,0);
//* enable IT
AT91F_US_EnableIt(COM0, AT91C_US_ENDTX );
}
//*----------------------------------------------------------------------------
//* \fn AT91F_US_Print_2_frame
//* \brief This function is used to send a Frame through the US channel
//* (Very low level debugging)
//*----------------------------------------------------------------------------
void AT91F_US_Print_2_frame(char *buffer, unsigned short counter,char *buffer2,unsigned short counter2) // \arg pointer to a string ending by \0
{
//* Enable USART IT error and AT91C_US_ENDRX
AT91F_US_SendFrame(COM0,buffer,counter,buffer2,counter2);
//* enable IT
AT91F_US_EnableIt(COM0, AT91C_US_ENDTX |AT91C_US_TXBUFE);
}
//*----------------------------------------------------------------------------
//* \fn AT91F_US_PDC_status
//* \brief This function is used Get the transmission block status
//* (Very low level debugging)
//*----------------------------------------------------------------------------
unsigned int AT91F_US_PDC_status (void)
{
return (BlockMode);
}
//*----------------------------------------------------------------------------
//* \fn AT91F_US_STT0
//* \brief This function is used Generate a time out
//* (Very low level debugging)
//*----------------------------------------------------------------------------
void AT91F_US_STT0 (void)
{
// *Enable usart SSTO
COM0->US_CR = AT91C_US_STTTO | AT91C_US_RETTO;
AT91F_US_EnableIt(COM0,AT91C_US_TIMEOUT);
}
//*----------------------------------------------------------------------------
//* \fn AT91F_US_PDC
//* \brief This function is used to send a block through PDC US channel
//* (Very low level debugging)
//*----------------------------------------------------------------------------
void AT91F_US_PDC (char *buffer, unsigned int size, unsigned short blo