📄 sermem.c
字号:
/** @file Sermem.c* @brief 串口部分内存管理文件, 如有问题请联系作者* @Author Mars.zhu@hotmail.com 2007-9-26 19:13* @Modify*/#include "option.h"#include "tcuart.h"#include "Sermem.h"/** 定义每个串口对应的缓冲区*/static uint8 U0_TxBuf[U0_TXBUF_LEN];static uint8 U0_RxBuf[U0_RXBUF_LEN];static uint8 U1_TxBuf[U1_TXBUF_LEN];static uint8 U1_RxBuf[U1_RXBUF_LEN];/** 把收发各个缓冲区程度定义在一个数组中*/uint16 U_TXBUF_LEN[] = { U0_TXBUF_LEN, U1_TXBUF_LEN,};uint16 U_RXBUF_LEN[] = { U0_RXBUF_LEN, U1_RXBUF_LEN,};/** 定义串口缓冲*/static S_UartBuf_t Uart_Buf[UART_CNT];/** @func UartInitFifo* @brief 初始化串口对应缓冲区*/voidUartInitBuf(uint8 fd){ switch (fd) { case 0: { Uart_Buf[0].RecvBufPtr = U0_RxBuf; Uart_Buf[0].Recv_in = U0_RxBuf; Uart_Buf[0].Recv_out = U0_RxBuf; Uart_Buf[0].Recv_BufFull= 0; Uart_Buf[0].SendBufPtr = U0_TxBuf; Uart_Buf[0].Send_in = U0_TxBuf; Uart_Buf[0].Send_out = U0_TxBuf; Uart_Buf[0].Send_BufFull= 0; Uart_Buf[0].Tx_Idle = 1; break; } case 1: { Uart_Buf[1].RecvBufPtr = U1_RxBuf; Uart_Buf[1].Recv_in = U1_RxBuf; Uart_Buf[1].Recv_out = U1_RxBuf; Uart_Buf[1].Recv_BufFull= 0; Uart_Buf[1].SendBufPtr = U1_TxBuf; Uart_Buf[1].Send_in = U1_TxBuf; Uart_Buf[1].Send_out = U1_TxBuf; Uart_Buf[1].Send_BufFull= 0; Uart_Buf[1].Tx_Idle = 1; break; } default: break; }}/** @func UartPutc* @brief 串口写数据到缓冲区* 底层接口函数* @Retval D_OK 成功* D_ERR 无数据*/D_ResultUartPutc(uint8 fd, uint8 dat){ uint8 *ptr; /** 接收缓冲区已满*/ if (Uart_Buf[fd].Recv_BufFull) { return D_ERR; } ptr = Uart_Buf[fd].Recv_in; ptr++; if (ptr == Uart_Buf[fd].RecvBufPtr+U_RXBUF_LEN[fd]) { ptr = Uart_Buf[fd].RecvBufPtr; } if (ptr == Uart_Buf[fd].Recv_out) { Uart_Buf[fd].Recv_BufFull = 1; return D_ERR; } *Uart_Buf[fd].Recv_in = dat; Uart_Buf[fd].Recv_in = ptr; return D_OK;}/** @func UartGetc* @brief 串口从缓冲区读取数据* 底层接口函数* @Retval D_OK 成功* D_ERR 无数据*/D_ResultUartGetc(uint8 fd, uint8 *dat){ /** 发送缓冲区无数据*/ if (Uart_Buf[fd].Send_out == Uart_Buf[fd].Send_in) { Uart_Buf[fd].Tx_Idle = 1; return D_ERR; } *dat = *(Uart_Buf[fd].Send_out++); if (Uart_Buf[fd].Send_out == Uart_Buf[fd].SendBufPtr+U_TXBUF_LEN[fd]) { Uart_Buf[fd].Send_out = Uart_Buf[fd].SendBufPtr; } /** 处理标志位*/ if (Uart_Buf[fd].Send_BufFull) { Uart_Buf[fd].Send_BufFull = 0; } return 0;}/** @func UsrPutc* @brief 用户写数据到缓冲区* API函数* @Retval D_OK 成功* D_ERR BUFF已满*/D_ResultUsrPutc(uint8 fd, uint8 dat){ uint8 *ptr; if (Uart_Buf[fd].Send_BufFull) { return D_ERR; } ptr = Uart_Buf[fd].Send_in; ptr++; if (ptr == Uart_Buf[fd].SendBufPtr+U_TXBUF_LEN[fd]) ptr = Uart_Buf[fd].SendBufPtr; if (ptr == Uart_Buf[fd].Send_out) { Uart_Buf[fd].Send_BufFull = 1; UartFlush(fd); return D_ERR; } *Uart_Buf[fd].Send_in = dat; Uart_Buf[fd].Send_in = ptr; return D_OK;}/** @func UsrGetc* @brief 用户写数据到缓冲区* API函数* @Retval D_OK 成功* D_ERR BUFF已满*/D_ResultUsrGetc(uint8 fd, uint8 *dat){ /** 接收缓冲区无数据*/ if (Uart_Buf[fd].Recv_out == Uart_Buf[fd].Recv_in) { return D_ERR; } *dat = *(Uart_Buf[fd].Recv_out++); if (Uart_Buf[fd].Recv_out == Uart_Buf[fd].RecvBufPtr+U_RXBUF_LEN[fd]) { Uart_Buf[fd].Recv_out = Uart_Buf[fd].RecvBufPtr; } /** 处理标志位*/ if (Uart_Buf[fd].Recv_BufFull) { Uart_Buf[fd].Recv_BufFull = 0; } return D_OK;}/** @func UsrGetc* @brief 开始发送缓冲区中数据*/D_ResultUartFlush(uint8 fd){ switch (fd) { case 0: case 1: { if (Uart_Buf[fd].Tx_Idle == 1) { Uart_Buf[fd].Tx_Idle = 0; __UART_Flush(fd); } return D_OK; } default: break; } return D_ERR;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -