serial.c

来自「这是arm10上面的linux环境下的串口通信源程序,方便学习和调试」· C语言 代码 · 共 75 行

C
75
字号
#include "serial.h"#include "pxa-regs.h"/* * Initialise the serial port with the given baudrate. The settings * are always 8 data bits, no parity, 1 stop bit, no start bits. * */void SerialInit(eBauds baudrate){	/* Theory of operations:	 * - Flush the output buffer	 * - switch receiver and transmitter off	 * - clear all sticky bits in control register 3	 * - set the port to sensible defaults (no break, no interrupts,	 *   no parity, 8 databits, 1 stopbit, transmitter and receiver 	 *   enabled	 * - set the baudrate to the requested value	 * - turn the receiver and transmitter back on	 */	while((FFLSR & (0x1 << 5)) == 0) ;	FFLCR = 0x00;	FFLCR = 0x80;	FFDLL = baudrate &0xff;	FFDLH = baudrate >> 8;	FFLCR = 0x00;	FFLCR = 0x03;	FFFCR = 0x00;	FFIER = FFIER | 0x40;}/* * Output a single byte to the serial port. */void SerialOutputByte(const char c){	/* wait for room in the tx FIFO */	while((FFLSR & (0x1 << 5)) == 0) ;	FFRBR = c;	/* If \n, also do \r */	if(c == '\n')		SerialOutputByte('\r');}/* * Write a null terminated string to the serial port. */void SerialOutputString(const char *s) {	while(*s != 0)		SerialOutputByte(*s++);		} /* SerialOutputString *//* * Read a single byte from the serial port. Returns 1 on success, 0 * otherwise. When the function is succesfull, the character read is * written into its argument c. */int SerialInputByte(char *c){	if(FFLSR & 0x01) {		*c = (char)FFRBR;		return(1);	} else {		/* no bit ready */		return(0);	}} /* SerialInputByte */

⌨️ 快捷键说明

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