⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 interrupt_usart.c

📁 ARM9 at91rm9200的linux驱动程序全集!超值!
💻 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
//*
//* 1.0 03/Jun/03 JPP   : Creation
//* 1.1 13/Aug/03 JPP   : Add the PDC block management
//* 1.2 19/Aug/03 JPP   : Change ENDTX and  PDC block management
//*----------------------------------------------------------------------------

// Include Standard LIB  files 
#include "AT91RM9200.h"
#include "lib_AT91RM9200.h"

#define TRACE
#define	MCK	60000000

extern void usart_asm_irq_handler(void);
#define USART_INTERRUPT_LEVEL		7

static const char atmel_header[]=
{
"\n\r  *** ATMEL Usart IRQ ***\n\r"
"Copyright (C) 2003 ATMEL Corporations Version: 1.0\n\r"
};

char message[80];
AT91PS_USART COM0;

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(AT91PS_USART USART_pt)
{
	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 = 0;
	            AT91F_US_DisableIt(USART_pt, AT91C_US_TXBUFE );
#ifdef TRACE
	AT91F_DBGU_Printk("End Interrupt\n\r");	
#endif 

#ifdef TRACE
	sprintf(message,"IT:ass 0x%08X size %d\n\r",CurrentAddress,RestSize);
	AT91F_DBGU_Printk(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 US1 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 US1 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 US1 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_PDC
//* \brief This function is used to  send a block through PDC US1 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 =1;
	
    //* 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 );
         }   

}

//*----------------------------------------------------------------------------
//* Function Name       : Usart_init
//* Object              : USART initialization 
//* Input Parameters    : none
//* Output Parameters   : TRUE
//*----------------------------------------------------------------------------
void Usart_init ( void )
//* Begin
{
		
	COM0= AT91C_BASE_US1;

        //* Define RXD and TXD as peripheral
        AT91F_PIO_CfgPeriph(AT91C_BASE_PIOB,AT91C_PB21_RXD1 | AT91C_PB20_TXD1,0);

    	// First, enable the clock of the PIOB
    	AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1<<AT91C_ID_US1 ) ;
	
	// Usart Configure
	AT91F_US_Configure (COM0, MCK,AT91C_US_ASYNC_MODE, 115200, 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=0;

    //* open Usart 1 interrupt
	AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_US1, USART_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE, usart_asm_irq_handler); 
	AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_US1); 

   //* ------ 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 SSTO
	COM0->US_CR = AT91C_US_STTTO;

	
	AT91F_US_PutChar (COM0,'X');
	
    //* 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 + -