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

📄 serial.c

📁 RT-Thread是发展中的下一代微内核嵌入式实时操作系统
💻 C
字号:
/* * File      : serial.c * This file is part of RT-Thread RTOS * COPYRIGHT (C) 2006, RT-Thread Development Team * * The license and distribution terms for this file may be * found in the file LICENSE in this distribution or at * http://openlab.rt-thread.com/license/LICENSE * * Change Logs: * Date           Author       Notes * 2006-08-23     Bernard      first version */#include <rthw.h>#include <rtthread.h>#include "AT91SAM7X.h"#define AT91C_US_RXRDY			((unsigned int) 0x1 <<  0)	/* US RXRDY Interrupt */#define AT91C_US_TXRDY			((unsigned int) 0x1 <<  1)	/* US TXRDY Interrupt */#define AT91C_US_RSTRX			((unsigned int) 0x1 <<  2)	/* US Reset Receiver */#define AT91C_US_RSTTX			((unsigned int) 0x1 <<  3)	/* US Reset Transmitter */#define AT91C_US_RXEN			((unsigned int) 0x1 <<  4)	/* US Receiver Enable */#define AT91C_US_RXDIS			((unsigned int) 0x1 <<  5)	/* US Receiver Disable */#define AT91C_US_TXEN			((unsigned int) 0x1 <<  6)	/* US Transmitter Enable */#define AT91C_US_TXDIS			((unsigned int) 0x1 <<  7)	/* US Transmitter Disable */#define AT91C_US_RSTSTA			((unsigned int) 0x1 <<  8)	/* US Reset Status Bits */#define AT91C_US_USMODE_NORMAL	((unsigned int) 0x0)		/* USAR) Normal */#define AT91C_US_USMODE_RS485	((unsigned int) 0x1)		/* USAR) RS485 */#define AT91C_US_USMODE_HWHSH	((unsigned int) 0x2)		/* USAR) Hardware Handshaking */#define AT91C_US_USMODE_MODEM	((unsigned int) 0x3)		/* USAR) Modem */#define AT91C_US_USMODE_ISO7816_0	((unsigned int) 0x4)	/* USAR) ISO7816 protocol: T = 0 */#define AT91C_US_USMODE_ISO7816_1	((unsigned int) 0x6)	/* USAR) ISO7816 protocol: T = 1 */#define AT91C_US_USMODE_IRDA	((unsigned int) 0x8)		/* USAR) IrDA */#define AT91C_US_USMODE_SWHSH	((unsigned int) 0xC)		/* USAR) Software Handshaking */#define AT91C_US_CLKS_CLOCK		((unsigned int) 0x0 <<  4)	/* USAR) Clock */#define AT91C_US_CLKS_FDIV1		((unsigned int) 0x1 <<  4)	/* USAR) fdiv1 */#define AT91C_US_CLKS_SLOW		((unsigned int) 0x2 <<  4)	/* USAR) slow_clock (ARM) */#define AT91C_US_CLKS_EXT		((unsigned int) 0x3 <<  4)	/* USAR) External (SCK) */#define AT91C_US_CHRL_5_BITS	((unsigned int) 0x0 <<  6)	/* USAR) Character Length: 5 bits */#define AT91C_US_CHRL_6_BITS	((unsigned int) 0x1 <<  6)	/* USAR) Character Length: 6 bits */#define AT91C_US_CHRL_7_BITS	((unsigned int) 0x2 <<  6)	/* USAR) Character Length: 7 bits */#define AT91C_US_CHRL_8_BITS	((unsigned int) 0x3 <<  6)	/* USAR) Character Length: 8 bits */#define AT91C_US_PAR_EVEN		((unsigned int) 0x0 <<  9)	/* DBGU Even Parity */#define AT91C_US_PAR_ODD		((unsigned int) 0x1 <<  9)	/* DBGU Odd Parity */#define AT91C_US_PAR_SPACE		((unsigned int) 0x2 <<  9)	/* DBGU Parity forced to 0 (Space) */#define AT91C_US_PAR_MARK		((unsigned int) 0x3 <<  9)	/* DBGU Parity forced to 1 (Mark) */#define AT91C_US_PAR_NONE		((unsigned int) 0x4 <<  9)	/* DBGU No Parity */#define AT91C_US_PAR_MULTI_DROP	((unsigned int) 0x6 <<  9)	/* DBGU Multi-drop mode */#define AT91C_US_NBSTOP_1_BIT	((unsigned int) 0x0 << 12)	/* USART 1 stop bit */#define AT91C_US_NBSTOP_15_BIT	((unsigned int) 0x1 << 12)	/* USART Asynchronous (SYNC=0) 2 stop bits Synchronous (SYNC=1) 2 stop bits */#define AT91C_US_NBSTOP_2_BIT	((unsigned int) 0x2 << 12)	/* USART 2 stop bits */#define BR    115200			/* Baud Rate */#define BRD  (MCK/16/BR)		/* Baud Rate Divisor */void rt_serial_init(void);/** * @addtogroup AT91SAM7 *//*@{*//** * This function is used to display a string on console, normally, it's * invoked by rt_kprintf * * @param str the displayed string */void rt_console_puts(const char* str){
	while (*str)	{		rt_serial_putc (*str++);	}
}/** * This function initializes serial */void rt_serial_init(){
	/* enable USART 0 */
	AT91C_PMC_PCER = 1 << AT91C_ID_US0;
	/* Enable RxD0 and TxDO Pin */	AT91C_PIO_PDR = (1 << 5) | (1 << 6);	AT91C_US0_CR = AT91C_US_RSTRX	|		/* Reset Receiver      */				AT91C_US_RSTTX		|		/* Reset Transmitter   */				AT91C_US_RXDIS		|		/* Receiver Disable    */				AT91C_US_TXDIS;				/* Transmitter Disable */	AT91C_US0_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  */	/* set baud rate divisor */	AT91C_US0_BRGR = BRD;	AT91C_US0_CR = AT91C_US_RXEN |	/* Receiver Enable     */				AT91C_US_TXEN;	/* Transmitter Enable  */}/** * This function read a character from serial without interrupt enable mode * * @return the read char */char rt_serial_getc(){	/* Wait for Full Rx Buffer */	while (!(AT91C_US0_CSR & AT91C_US_RXRDY));	/* Read Character */	return (AT91C_US0_RHR);}/** * This function will write a character to serial without interrupt enable mode * * @param c the char to write */void rt_serial_putc(const char c){	/*		to be polite with serial console add a line feed		to the carriage return character	*/	if (c=='\n')
	{
		while (!(AT91C_US0_CSR & AT91C_US_TXRDY));	
		AT91C_US0_THR = '\r';
	}	/* Wait for Empty Tx Buffer */	while (!(AT91C_US0_CSR & AT91C_US_TXRDY));	/* Transmit Character */	AT91C_US0_THR = c;}/*@}*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -