📄 stream.c
字号:
/****************************************Copyright (c)****************************************************
** Guangzhou ZHIYUAN electronics Co.,LTD.
**
** http://www.embedtools.com
**
**--------------File Info---------------------------------------------------------------------------------
** File name: stream.c
** Last modified Date: 2007-12-18
** Last Version: 1.0.1
** Descriptions: 2440 UART 流设备操作库
**
**--------------------------------------------------------------------------------------------------------
** Created by: Hanhui
** Created date: 2007-12-18
** Version: 1.0.0
** Descriptions: 2440 UART 流设备操作库
**
**--------------------------------------------------------------------------------------------------------
** Modified by: lixintian
** Modified date: 2008-04-07
** Version: 1.0.1
** Descriptions: 移植到threadX
**
*********************************************************************************************************/
#include "config.h"
#include "stream.h"
#include "uart.h"
/*********************************************************************************************************
全局变量
*********************************************************************************************************/
static STREAM_CLASS __GsrtclassCom[3]; /* 串口通道 */
static TX_SEMAPHORE __GsemCom[3][2];
/*********************************************************************************************************
函数声明
*********************************************************************************************************/
void __uartInt0(void);
void __uartInt1(void);
void __uartInt2(void);
/*********************************************************************************************************
** Function name: __streamInit
** Descriptions: 流类型初始化
** input parameters: iCom 串口号
** iInFrared 是否使用红外模式
** iData 数据位数
** iStopBit 结束位
** iCheck 校验方法
** iBaud 波特率
** output parameters: NONE
** Returned value: NONE
** Created by: Hanhui
** Created Date: 2007/12/18
**--------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
**--------------------------------------------------------------------------------------------------------
*********************************************************************************************************/
static void __streamInit (int iCom,
int iInFrared,
int iData,
int iStopBit,
int iCheck,
int iBaud)
{
switch (iCom) {
case COM0:
uartInit(iCom, iInFrared, iData, iStopBit, iCheck, iBaud, (void *)__uartInt0);
break;
case COM1:
uartInit(iCom, iInFrared, iData, iStopBit, iCheck, iBaud, (void *)__uartInt1);
break;
case COM2:
uartInit(iCom, iInFrared, iData, iStopBit, iCheck, iBaud, (void *)__uartInt2);
break;
}
}
/*********************************************************************************************************
** Function name: streamCreate
** Descriptions: 创建流类型
** input parameters: uiCom 串口号
** pcBuffer 缓冲区
** uiMaxBytes 缓冲区大小
** output parameters: NONE
** Returned value: 流类型句柄, 错误返回 NULL
** Created by: Hanhui
** Created Date: 2007/12/18
**--------------------------------------------------------------------------------------------------------
** Modified by: lixintian
** Modified date: 2008/04/07
**--------------------------------------------------------------------------------------------------------
*********************************************************************************************************/
PSTREAM_CLASS streamCreate (unsigned int uiCom,
char *pcBuffer,
unsigned int uiMaxBytes)
{
STREAM_CLASS *psrtclassCom = &__GsrtclassCom[uiCom];
if (uiCom > 2) { /* 参数检查 */
return ((void *)0);
}
if (!pcBuffer) {
return ((void *)0);
}
if (!uiMaxBytes) {
return ((void *)0);
}
if (psrtclassCom->uiIsCreate) { /* 已经建立了 */
return ((void *)0);
}
psrtclassCom->uiIsCreate = 1; /* 已经建立 */
// changed by lixintian 2008.04.03
//zypipeCreate(&psrtclassCom->pzypipeRead, pcBuffer, uiMaxBytes); /* 创建接收管道 */
//psrtclassCom->peventWriteLock = OSSemCreate(1); /* 写操作互斥 */
_tx_semaphore_create(&__GsemCom[0][0], "COM0_W", 1); /* 创建信号量 */
_tx_semaphore_create(&__GsemCom[0][1], "COM0_R", 1); /* 创建信号量 */
psrtclassCom->WriteLock = &__GsemCom[0][0];
psrtclassCom->ReadLock = &__GsemCom[0][1];
psrtclassCom->pBufferStart = pcBuffer;
psrtclassCom->pBufferEnd = pcBuffer + uiMaxBytes;
psrtclassCom->pReadIndex = pcBuffer;
psrtclassCom->pWriteIndex = pcBuffer;
psrtclassCom->iByfferSize = uiMaxBytes;
psrtclassCom->iRecvCnt = 0;
psrtclassCom->iErr = 0;
if (!psrtclassCom->WriteLock) {
psrtclassCom->uiIsCreate = 0; /* 建立失败 */
return ((void *)0);
}
psrtclassCom->uiCom = uiCom;
__streamInit(uiCom, UNUSE_INF, 8, ONE_STOPBIT, CHK_NONE, 115200); /* 初始化串口 */
psrtclassCom->uiBaud = 115200;
psrtclassCom->uiStopBit = 1;
psrtclassCom->uiDataBit = 8;
psrtclassCom->uiCheck = CHK_NONE;
return (psrtclassCom); /* 返回控制块 */
}
/*********************************************************************************************************
** Function name: streamWrite
** Descriptions: 写流类型
** input parameters: psrtclassCom 控制块指针
** pcBuffer 缓冲区
** uiBytes 发送的字节数
** output parameters: NONE
** Returned value: 实际发送的字节数
** Created by: Hanhui
** Created Date: 2007/12/18
**--------------------------------------------------------------------------------------------------------
** Modified by: lixintian
** Modified date: 2008/04/07
**--------------------------------------------------------------------------------------------------------
*********************************************************************************************************/
int streamWrite (PSTREAM_CLASS psrtclassCom,
char *pcBuffer,
unsigned int uiBytes)
{
unsigned int uiError;
if (!psrtclassCom) {
return (-1);
}
if (!pcBuffer) {
return (-1);
}
if (!uiBytes) {
return (0);
}
uiError = tx_semaphore_get(psrtclassCom->WriteLock, TX_WAIT_FOREVER);
if (uiError != TX_SUCCESS) {
return (-1);
}
uartSendByteCnt(psrtclassCom->uiCom, (unsigned char *)pcBuffer, uiBytes);
tx_semaphore_put(psrtclassCom->WriteLock);
return (uiBytes);
}
/*********************************************************************************************************
** Function name: streamRead
** Descriptions: 读流类型
** input parameters: psrtclassCom 控制块指针
** pcBuffer 缓冲区
** uiMaxBytes 缓冲的字节数
** output parameters: NONE
** Returned value: 实际读出的字节数
** Created by: Hanhui
** Created Date: 2007/12/18
**--------------------------------------------------------------------------------------------------------
** Modified by: lixintian
** Modified date: 2008/04/07
**--------------------------------------------------------------------------------------------------------
*********************************************************************************************************/
int streamRead (PSTREAM_CLASS psrtclassCom,
char *pcBuffer,
unsigned int uiMaxBytes)
{
TX_INTERRUPT_SAVE_AREA
unsigned int uiReadBytes;
unsigned int i;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -