📄 interrupt_usart.c
字号:
/******************************************************************************
* 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
* Creation : JPP 03/Jun/03
* 1.0 13/Aug/03 JPP : Add the PDC block management
******************************************************************************/
// Include Standard LIB files
#include "Board.h"
#include "main.h"
#include "AT91SAM7S64a.H" /* AT91SAMT7S64 definitions */
#include "lib_AT91SAM7S64a.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
*****************************************************************************/
__irq 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 string 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 block_size) // \arg pointer to a string ending by \0
{
//* Init Global value for interrup
CurrentAddress = buffer;
RestSize = size;
BlockSize = block_size;
//* clean all param
AT91F_US_DisableIt(COM0, AT91C_US_ENDTX | AT91C_US_TXBUFE);
COM0->US_TCR = 0;
COM0->US_TNCR = 0;
BlockMode = (char) true;
//* Start PDC
//* ---- Set the first Block
COM0->US_TPR = (unsigned int) CurrentAddress;
if (RestSize <= BlockSize) {
//* ---- Send only One Block
COM0->US_TCR = RestSize;
AT91F_US_EnableIt(COM0,AT91C_US_TXBUFE );
}
else
{
//* ---- Send more One Block
COM0->US_TCR = BlockSize;
RestSize -= BlockSize;
//* Set the next pointer register
CurrentAddress += BlockSize;
COM0->US_TNPR = (unsigned int) CurrentAddress;
if(RestSize <= BlockSize) {
//* ---- Send only tow Blocks
COM0->US_TNCR = RestSize;
RestSize = 0;
} else {
//* ---- Send more than tow Blocks
COM0->US_TNCR = BlockSize;
RestSize -= BlockSize;
}
//* Enable USART IT
AT91F_US_EnableIt(COM0, AT91C_US_ENDTX);
AT91F_US_EnableIt(COM0,AT91C_US_TXBUFE );
}//* Else BlockSize
}
/******************************************************************************
* Function Name : Usart_init
* Object : USART initialization
* Input Parameters : none
* Output Parameters : TRUE
******************************************************************************/
void Usart_init ( void )
//* Begin
{
COM0= AT91C_BASE_US0;
//* Define RXD and TXD as peripheral
AT91F_US0_CfgPIO() ;
// First, enable the clock of the PIOB
AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1<<AT91C_ID_US0 ) ;
// Usart Configure
AT91F_US_Configure (COM0, MCK,AT91C_US_ASYNC_MODE,USART_BAUD_RATE , 0);
// Enable usart
COM0->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
//* Enable USART IT error and AT91C_US_ENDRX
AT91F_US_EnableIt(COM0,AT91C_US_TIMEOUT | AT91C_US_FRAME | AT91C_US_OVRE |AT91C_US_ENDRX);
//* Set the PDC Interrupt mode
BlockMode=false;
//* open Usart 1 interrupt
AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_US0, USART_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, Usart_c_irq_handler);
AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_US0);
//* ------ set time out US_RTOR
//* Arm time out after 255 * 4 bits time
//* for 115200 bit time = 1/115200 = 8.6 micro sec time out unuti= 4* 8.6 = 34 micro
//*
COM0->US_RTOR = 0xFFFF;
//* Enable USART IT error and AT91C_US_ENDRX
AT91F_US_ReceiveFrame(COM0,(char *)message,10,0,0);
AT91F_US_EnableIt(COM0,AT91C_US_ENDRX);
//* first
AT91F_US_SendFrame(COM0,(char *)atmel_header,sizeof(atmel_header),0,0);
AT91F_US_EnableIt(COM0, AT91C_US_ENDTX );
//* End
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -