📄 serial.c
字号:
/******************************************************************************/
/* */
/* SERIAL.C: Low Level Serial Routines */
/* */
/******************************************************************************/
/* 20080928: modify codes for ARM platform (NXP LPC2132) */
/* 20071212: modify code for mcuzone's mp3 player use debug uart --by michael */
/*
10/2005 by Martin Thomas <eversmith@heizung-thomas.de>
based on an example-code from Keil GmbH
- modified for the WinARM example
- changed interface to avoid potential conflicts with "stdio.h"
*/
// included in board.h: #include <AT91SAM7S64.H> /* AT91SAMT7S64 definitions */
//#include "Board.h"
#include "global.h"
#define BR 115200ul /* Baud Rate */
//#define BRD (MCK/16/BR) /* Baud Rate Divisor */
//AT91S_USART * pUSART = AT91C_BASE_US0; /* Global Pointer to USART0 */
//AT91S_USART * pUSART = AT91C_BASE_DBGU; /* Global Pointer to USART0 */
void uart0_init (void) { /* Initialize Serial Interface */
uint16 Fdiv;
PINSEL0 = (PINSEL0 & 0xfffffff0) | 0x05; /* Select the pins for Uart 选择管脚为UART0 */
U0LCR = 0x80; /* Enable to access the frequenc regecter
允许访问分频因子寄存器 */
Fdiv = (Fpclk / 16) / BR; /* Set the baudrate设置波特率 */
U0DLM = Fdiv / 256;
U0DLL = Fdiv % 256;
U0LCR = 0x03; /* Disable to access the frequenc regecter
禁止访问分频因子寄存器 */
/* set to 8,1,n 且设置为8,1,n */
U0IER = 0x00; /* Disable interrupt禁止中断 */
U0FCR = 0x00; /* initial FIFO 初始化FIFO */
#if 0
/* mt: n.b: uart0 clock must be enabled to use it */
//*AT91C_PIOA_PDR = AT91C_PA5_RXD0 | /* Enable RxD0 Pin */
// AT91C_PA6_TXD0; /* Enalbe TxD0 Pin */
*AT91C_PIOA_PDR = AT91C_PA9_DRXD | /* Enable RxD0 Pin */
AT91C_PA10_DTXD; /* Enalbe TxD0 Pin */
pUSART->US_CR = AT91C_US_RSTRX | /* Reset Receiver */
AT91C_US_RSTTX | /* Reset Transmitter */
AT91C_US_RXDIS | /* Receiver Disable */
AT91C_US_TXDIS; /* Transmitter Disable */
pUSART->US_MR = AT91C_US_USMODE_NORMAL | /* Normal Mode */
AT91C_US_CLKS_CLOCK | /* Clock = MCK */
AT91C_US_CHRL_8_BITS | /* 8-bit Data */
AT91C_US_PAR_NONE | /* No Parity */
AT91C_US_NBSTOP_1_BIT; /* 1 Stop Bit */
pUSART->US_BRGR = BRD; /* Baud Rate Divisor */
pUSART->US_CR = AT91C_US_RXEN | /* Receiver Enable */
AT91C_US_TXEN; /* Transmitter Enable */
#endif
}
int uart0_putc(int ch)
{
U0THR=ch;
while((U0LSR&&0x40)==0);
return 0;
#if 0
while (!(pUSART->US_CSR & AT91C_US_TXRDY)); /* Wait for Empty Tx Buffer */
return (pUSART->US_THR = ch); /* Transmit Character */
#endif
}
int uart0_putchar (int ch) { /* Write Character to Serial Port */
if (ch == '\n') { /* Check for LF */
uart0_putc( '\r' ); /* Output CR */
}
return uart0_putc( ch ); /* Transmit Character */
}
int uart0_puts ( char* s )
{
int i = 0;
while ( *s ) {
uart0_putc( *s++ );
i++;
}
return i;
}
int uart0_prints ( char* s )
{
int i = 0;
while ( *s ) {
uart0_putchar( *s++ );
i++;
}
return i;
}
int uart0_kbhit( void ) /* returns true if character in receive buffer */
{
#if 0
if ( pUSART->US_CSR & AT91C_US_RXRDY) {
return 1;
}
else {
return 0;
}
#endif
return 0;
}
int uart0_getc ( void ) /* Read Character from Serial Port */
{
#if 0
while (!(pUSART->US_CSR & AT91C_US_RXRDY)); /* Wait for Full Rx Buffer */
return (pUSART->US_RHR); /* Read Character */
#endif
return 0;
}
#define CR 0x0D
/* implementation of putchar (also used by printf function to output data) */
int sendchar (int ch) { /* Write character to Serial Port */
if (ch == '\n') {
while (!(U0LSR & 0x20));
U1THR = CR; /* output CR */
}
while (!(U0LSR & 0x20));
return (U0THR = ch);
}
int getkey (void) { /* Read character from Serial Port */
while (!(U0LSR & 0x01));
return (U0RBR);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -