serial.c

来自「JENNIC无线传感器网络 ZIGBEE 文件资料」· C语言 代码 · 共 241 行

C
241
字号
/**************************************************************************** * * MODULE:             Serial * * COMPONENT:          serial.c,v * * VERSION:            AT-Jenie_Release_v1_RC3 * * REVISION:           1.3 * * DATED:              2007/09/25 09:03:20 * * STATUS:             Exp * * AUTHOR:             MRW * * DESCRIPTION:        Generic buffered UART serial communications with RX *                     flow control on RTS * * CHANGE HISTORY: * * serial.c,v * * * MRW * * LAST MODIFIED BY:   mwild *                     $Modtime: $ * **************************************************************************** * *  (c) Copyright 2007 JENNIC Ltd * ****************************************************************************//****************************************************************************//***        Include files                                                 ***//****************************************************************************/#include <jendefs.h>#include <AppHardwareApi.h>#include "interrupt.h"#include "uart.h"#include "queue.h"#include "serial.h"/****************************************************************************//***        Macro Definitions                                             ***//****************************************************************************/#define SERIAL_TX_QUEUE_SIZE                32#define SERIAL_RX_QUEUE_SIZE                32#define SERIAL_RX_QUEUE_HIGH_WATER_MARK     28#define SERIAL_RX_QUEUE_LOW_WATER_MARK      4/****************************************************************************//***        Type Definitions                                              ***//****************************************************************************//****************************************************************************//***        Local Function Prototypes                                     ***//****************************************************************************/PRIVATE void vRxChar(uint8 u8Char);PRIVATE void vTxReady(void);/****************************************************************************//***        Exported Variables                                            ***//****************************************************************************//****************************************************************************//***        Local Variables                                               ***//****************************************************************************/QUEUE_DECLARE_Q(sRxQueue, SERIAL_RX_QUEUE_SIZE); /* [I SP001222_P1 282]*/QUEUE_DECLARE_Q(sTxQueue, SERIAL_TX_QUEUE_SIZE); /* [I SP001222_P1 282]*//****************************************************************************//***        Exported Functions                                            ***//****************************************************************************//****************************************************************************//***        Local Functions                                               ***//****************************************************************************//**************************************************************************** * * NAME: vSerial_Init * * DESCRIPTION: * * PARAMETERS:      Name            RW  Usage * None. * * RETURNS: * None. * * NOTES: * None. ****************************************************************************/PUBLIC void vSerial_Init(void){    vUART_Init(vRxChar, vTxReady);    vUART_RtsStartFlow(); /* [I SP001222_P1 284]*/}/**************************************************************************** * * NAME: vSerial_TxChar * * DESCRIPTION: * * PARAMETERS:      Name            RW  Usage * None. * * RETURNS: * None. * * NOTES: * None. ****************************************************************************/PUBLIC bool_t bSerial_TxChar(uint8 u8Chr){    bool_t bStatus = TRUE;    /*     * prevent race condition where transmitter is busy, but becomes ready     * between reading its status and queuing the character to transmit     */    vInterrupt_Suspend();    if (bQueue_Empty(sTxQueue) && bUART_TxReady()) {        vUART_SetTxInterrupt(TRUE);        vUART_TxChar(u8Chr);    } else {        if (!bQueue_Full(sTxQueue)) {            vQueue_AddItem(sTxQueue, u8Chr);        } else {            bStatus = FALSE;        }    }    vInterrupt_Resume();    return bStatus;}/**************************************************************************** * * NAME: i16Serial_RxChar * * DESCRIPTION: * * PARAMETERS:      Name            RW  Usage * None. * * RETURNS: * None. * * NOTES: * None. ****************************************************************************/PUBLIC int16 i16Serial_RxChar(void){    int16 i16Result = -1;    if(!bQueue_Empty(sRxQueue)) {        i16Result = (int16)u8Queue_RemoveItem(sRxQueue);        if (u16Queue_Count(sRxQueue) == SERIAL_RX_QUEUE_LOW_WATER_MARK) {            vUART_RtsStartFlow(); /* [I SP001222_P1 286]*/        }    }    return i16Result;}/****************************************************************************//***        Local Functions                                               ***//****************************************************************************//**************************************************************************** * * NAME: vTxReady * * DESCRIPTION: * Transmitter ready callback * * PARAMETERS:      Name            RW  Usage * None. * * RETURNS: * None. * * NOTES: * None. ****************************************************************************/PRIVATE void vTxReady(void){    if (!bQueue_Empty(sTxQueue)) {        vUART_TxChar(u8Queue_RemoveItem(sTxQueue));    } else {        vUART_SetTxInterrupt(FALSE);    }}/**************************************************************************** * * NAME: vRxChar * * DESCRIPTION: * Receive character callback * * PARAMETERS:      Name            RW  Usage * None. * * RETURNS: * None. * * NOTES: * None. ****************************************************************************/PRIVATE void vRxChar(uint8 u8Char){    vQueue_AddItem(sRxQueue, u8Char);    if (u16Queue_Count(sRxQueue) == SERIAL_RX_QUEUE_HIGH_WATER_MARK) {        vUART_RtsStopFlow(); /* [I SP001222_P1 285]*/    }}/****************************************************************************//***        END OF FILE                                                   ***//****************************************************************************/

⌨️ 快捷键说明

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