📄 uart.c
字号:
/* Standard includes. */
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdio.h>
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
/* application includes. */
#include "uip_task.h"
#include "Board.h"
#include "App.h"
extern void UARTisr_entry( void );
__arm void UART_isr( void );
extern unsigned int ISR_flag ;
/*----------------------------------------------------------------------------*/
void UART0_init ( void ){
AT91C_BASE_AIC->AIC_EOICR = 0; // When using the JTAG debugger the hardware is not always initialised to
// the correct default state. This line just ensures that this does not
// cause all interrupts to be masked at the start.
//--------------------UART0 INIT --------------------------
AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_US0);
AT91C_BASE_PIOA->PIO_PDR = BSP_GPIOA_UART0;
AT91C_BASE_PIOA->PIO_PDR = BSP_GPIOA_UART0;
AT91C_BASE_US0->US_IDR = AT91C_US_TXRDY;
AT91C_BASE_US0->US_IER = AT91C_US_RXRDY;
AT91C_BASE_US0->US_CR = AT91C_US_RXEN
| AT91C_US_TXEN;
AT91C_BASE_US0->US_MR = AT91C_US_USMODE_NORMAL // RS232C mode selected
| AT91C_US_CLKS_CLOCK // USART input CLK is MCK
| AT91C_US_CHRL_8_BITS // 8 bit data to be sent
| AT91C_US_PAR_NONE // No parity bit selected
| AT91C_US_NBSTOP_1_BIT; // 1 stop bit selected
AT91C_BASE_US0->US_BRGR = (CPU_INT16U)((MCK) / 115200 / 16);// Set the USART baud rate
AT91C_BASE_AIC->AIC_IDCR |= (1 << AT91C_ID_US0); //disabling interrupts .
AT91C_BASE_AIC->AIC_SVR[AT91C_ID_US0] = (signed int)UARTisr_entry ; //passing the handler address.
AT91C_BASE_AIC->AIC_SMR[AT91C_ID_US0] = 0X00000007; //+ve edge TRIGGER AND PRIORITY 7 (ITS given highets priority).
AT91C_BASE_AIC->AIC_IECR |= (1 << AT91C_ID_US0); //Enable interrupts .
}
//--------------------------------------------------------------
void UART0_WrByte(CPU_INT08U tx_byte)
{
while( !(AT91C_BASE_US0->US_CSR & AT91C_US_TXRDY));
AT91C_BASE_US0->US_THR = tx_byte;
}
//--------------------------------------------------------------
void UART0_Printk(char *format, ...)
{
static char buffer[80 + 1];
unsigned char *tx_str;
va_list vArgs;
va_start(vArgs, format);
vsprintf((char *)buffer, (char const *)format, vArgs);
va_end(vArgs);
tx_str = (unsigned char*) buffer ;
while ((*tx_str) != 0) {
UART0_WrByte(*tx_str++);
}
}
//------------------------------------------------
__arm void UART_isr( void ){
static int irq_id;
char temp0;
AT91C_BASE_AIC->AIC_IVR = 0 ;
irq_id = AT91C_BASE_AIC->AIC_ISR & 0x1F;
AT91C_BASE_AIC->AIC_ICCR |= (1 << irq_id);
temp0 = (char)AT91C_BASE_US0->US_RHR;
temp0++;
ISR_flag = irq_id ;
AT91C_BASE_AIC->AIC_EOICR = 0;
AT91C_BASE_AIC->AIC_ICCR |= (1 << irq_id);
}
//-----------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -