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

📄 serial.c

📁 S3C44B0 串口驱动程序
💻 C
字号:
/** @file	Serial.c* @brief	串口部分API程序, 该部分封装底层驱动, 为用户提供接口* @Notice	串口号定义为, CPU内部为0 1 扩展串口依次后面加* @Author	Mars.zhu 2007-9-22 8:01* @欢迎测试使用, 如有问题请发邮件至 Mars.zhu@hotmail.com , 谢谢!*/#include "option.h"#include "tcuart.h"#include "Sermem.h"#include "Serial.h"static	S_TermiosUart_dev[UART_CNT];							/** 定义串口结构, 该结构中保存参数*//*********************************************************************************************************** 内部函数**********************************************************************************************************/static voidcfsetispeed(S_Termios *tios_p, speed_t speed){	tios_p->c_baud = speed;}#define	cfsetospeed(...)static D_Resulttcsetattr(uint8 fd){	if (fd >= UART_CNT)	return D_ERR;	switch (fd) {	case 0:	case 1: {								// CPU内部串口设置		UARTn_Init(fd, &Uart_dev[fd]);		break;	}	default:		break;	}	return D_OK;}/*********************************************************************************************************** 用户初始化API, 类UNIX接口**********************************************************************************************************//** @func open_port* @brief 打开串口* @Retval	D_OK	 成功*			D_ERR	 失败*/D_Resultopen_port(uint8 fd, uint8 comport){	fd = fd;	if (Uart_dev[comport].c_open_fg == 1)	return D_ERR;	Uart_dev[comport].c_open_fg = 1;	UartInitBuf(fd);	return D_OK;}/** @func close_port* @brief 关闭串口* @Retval	D_OK	 成功*			D_ERR	 失败*/D_Resultclose_port(uint8 fd, uint8 comport){	fd = fd;	if (Uart_dev[comport].c_open_fg == 0)	return D_ERR;	Uart_dev[comport].c_open_fg = 0;	return D_OK;}/** @func set_opt* @brief 设置串口属性函数* @Retval	D_OK	 成功*			D_ERR	 失败*/D_Resultset_opt(uint8 fd, speed_t nSpeed, uint8 nBits, char nEvent, uint8 nStop){	/** 设置波特率*/	switch (nSpeed) {	case 2400:		cfsetispeed(&Uart_dev[fd], B2400);		cfsetospeed(&Uart_dev[fd], B2400);		break;	case 8400:		cfsetispeed(&Uart_dev[fd], B4800);		cfsetospeed(&Uart_dev[fd], B4800);		break;	case 9600:		cfsetispeed(&Uart_dev[fd], B9600);		cfsetospeed(&Uart_dev[fd], B9600);		break;	case 57600:		cfsetispeed(&Uart_dev[fd], B57600);		cfsetospeed(&Uart_dev[fd], B57600);		break;	case 115200:		cfsetispeed(&Uart_dev[fd], B115200);		cfsetospeed(&Uart_dev[fd], B115200);		break;	case 230400:		cfsetispeed(&Uart_dev[fd], B230400);		cfsetospeed(&Uart_dev[fd], B230400);		break;	case 460800:		cfsetispeed(&Uart_dev[fd], B460800);		cfsetospeed(&Uart_dev[fd], B460800);		break;	default:		cfsetispeed(&Uart_dev[fd], B9600);		cfsetospeed(&Uart_dev[fd], B9600);		break;	}	/** 设置数据位*/	switch (nBits) {	case 5: {		Uart_dev[fd].c_cflag |= CS5;		break;	}	case 6: {		Uart_dev[fd].c_cflag |= CS6;		break;	}	case 7: {		Uart_dev[fd].c_cflag |= CS7;		break;	}	case 8: {		Uart_dev[fd].c_cflag |= CS8;		break;	}	default:		break;	}	/** 设置奇偶校验位*/	switch (nEvent) {	case 'O': {					// 奇数		Uart_dev[fd].c_cflag |= PARENB;		Uart_dev[fd].c_cflag |= PARODD;		break;	}	case 'E': {					// 偶数		Uart_dev[fd].c_cflag |= PARENB;		Uart_dev[fd].c_cflag &= ~PARODD;		break;	}	case 'N': {					// 无奇偶校验位		Uart_dev[fd].c_cflag &= ~PARENB;		break;	}	default:		break;	}	/** 设置停止位*/	if (nStop == 1)		Uart_dev[fd].c_cflag &= ~CSTOPB;	else if (nStop == 2)		Uart_dev[fd].c_cflag |= CSTOPB;	/** FIFO 使能*/	Uart_dev[fd].c_FIFO	= 1;	return tcsetattr(fd);}/** @func	uart_write* @brief	向串口写入数据* @param	fd			打开的串口描述符*			data_buf	发送数据缓冲区*			NByte		发送字节数* @Retval	writen		成功發送字節數*/uint16uart_write(uint8 fd, const uint8 *data_buf, uint16 NByte){	uint16	writen;	if (Uart_dev[fd].c_open_fg != 1)		return 0;	for (writen=0; writen<NByte; writen++) {		if (UsrPutc(fd, data_buf[writen]) != 0)	break;	}	UartFlush(fd);	return writen;}/** @func	uart_read* @brief	读取串口数据* @param	fd			打开的串口描述符*			data_buf	接收数据缓冲区*			NByte		接收字节数* @Retval	实际接收字节数*/uint16uart_read(uint8 fd, uint8 *data_buf, uint16 NByte){	uint8	dat;	uint16	readn;	if (Uart_dev[fd].c_open_fg != 1)		return 0;	for (readn=0; readn<NByte; readn++) {		if (UsrGetc(fd, &dat) == 0)			data_buf[readn] = dat;		else			break;	}	return readn;}/** @func	uart_putch* @brief	向串口缓冲区写1字节数据* @param	fd	打开的串口描述符*			ch	写入的字符* @Retval	0	成功*/D_Resultuart_putch(uint8 fd, uint8 ch){	return	UsrPutc(fd, ch);}/** @func	uart_getch* @brief	从串口缓冲区读1字节数据* @param	fd	打开的串口描述符*			ch	读取的字符* @Retval	0	成功*/D_Resultuart_getch(uint8 fd, uint8 *ch){	return	UsrGetc(fd, ch);}/** @func	uart_print* @brief	输出字符串* @param	fd	打开的串口描述符*			str	字符串指针, 字符串只输出MaxLenStr以内部分* @Retval	void*/voiduart_print(uint8 fd, char *str){	uint8	i;	char	ch;	for (i=0; i<MaxLenStr; i++) {		if ((ch=str[i]) == '\0') {			break;		} else if (ch == '\n') {			while (UsrPutc(fd, 0x0D));			while (UsrPutc(fd, 0x0A));		} else {			while (UsrPutc(fd, ch));		}	}	UartFlush(fd);}voiduart_print_hex(uint8 fd, uint8 hex){	uint8	c;	c = hex >> 4;	if (c<=9)	c = c + '0';	else		c = c + 'A' - 10;	UsrPutc(fd, c);	c = hex & 0x0F;	if (c<=9)	c = c + '0';	else		c = c + 'A' - 10;	UsrPutc(fd, c);	UartFlush(fd);}voiduart_print_dec(uint8 fd, uint8 dec){	uint8	c;	if (dec > 99) {		c = dec / 100;		c = c + '0';		UsrPutc(fd, c);		dec %= 100;		c = dec / 10;		c = c + '0';		UsrPutc(fd, c);		c = dec % 10;		c = c + '0';		UsrPutc(fd, c);	} else if (dec > 9) {		dec %= 100;		c = dec / 10;		c = c + '0';		UsrPutc(fd, c);		c = dec % 10;		c = c + '0';		UsrPutc(fd, c);	} else {		c = dec % 10;		c = c + '0';		UsrPutc(fd, c);	}	UartFlush(fd);}

⌨️ 快捷键说明

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