📄 serialq.c
字号:
/***************************************************************************//*! *\MODULE Wireless UART with Flow Control * *\COMPONENT $RCSfile: SerialQ.c,v $ * *\VERSION $Name: $ * *\REVISION $Revision: 1.1 $ * *\DATED $Date: 2008/01/21 10:15:05 $ * *\STATUS $State: Exp $ * *\AUTHOR Ian Morris * \n Martin Looker * *\DESCRIPTION Circular queue for serial port use. * * This file maintains a pair of circular queues for use with the UART. * * The receive queue is filled with data received by the UART. When the queue * fills past a certain level the UART is disabled from receiving further data, * causing the hardware flow control to turn on. * * The receive queue is * emptied by the protocol transmitting the data over the radio. When the queue * empties below a certain level the UART is enabled to receive data again * causing the hardware flow control to turn off. * * The transmit queue is filled with data received by the protocol over * the radio. The protocol will not allow * the other board to send data if there is insufficient space in the * transmit queue. * * The transmit queue is emptied as data is transmitted by the UART. *//*\CHANGE HISTORY * * $Log: SerialQ.c,v $ * Revision 1.1 2008/01/21 10:15:05 mlook * Initial checkin * * Revision 1.1 2007/11/02 12:32:53 mlook * Adding new application notes * * * *\LAST MODIFIED BY $Author: mlook $ * $Modtime: $ * **************************************************************************** * * This software is owned by Jennic and/or its supplier and is protected * under applicable copyright laws. All rights are reserved. We grant You, * and any third parties, a license to use this software solely and * exclusively on Jennic products. You, and any third parties must reproduce * the copyright and warranty notice and any other legend of ownership on each * copy or partial copy of the software. * * THIS SOFTWARE IS PROVIDED "AS IS". JENNIC MAKES NO WARRANTIES, WHETHER * EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, * ACCURACY OR LACK OF NEGLIGENCE. JENNIC SHALL NOT, IN ANY CIRCUMSTANCES, * BE LIABLE FOR ANY DAMAGES, INCLUDING, BUT NOT LIMITED TO, SPECIAL, * INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY REASON WHATSOEVER. * * Copyright Jennic Ltd 2005, 2006, 2007. All rights reserved * ****************************************************************************//****************************************************************************//*** Include files ***//****************************************************************************/#include <jendefs.h>#include "gdb.h"#include "SerialQ.h"#include "Uart.h"/****************************************************************************//*** Macro Definitions ***//****************************************************************************/#define SERIALQ_MASK 0x03FFU /**< Mask for indicies into queues */#define SERIALQ_SIZE SERIALQ_MASK+1 /**< Size of queues, (mask plus 1) */#define SERIALQ_COUNT 2 /**< Number of queues */#define SERIALQ_FREE_LOW 64 /**< Low on free space level */#define SERIALQ_FREE_HIGH 128 /**< High on free space level *//****************************************************************************//*** Type Definitions ***//****************************************************************************//** Circular buffer */typedef struct{ uint16 u16Head; /**< Position in queue to add to */ uint16 u16Tail; /**< Position in queue to remove from */ uint16 u16In; /**< Input character counter */ uint16 u16Out; /**< Output character counter */ uint8 u8Buff[SERIALQ_SIZE]; /**< Queue buffer */} tsCircBuff;/****************************************************************************//*** Local Function Prototypes ***//****************************************************************************//****************************************************************************//*** Exported Variables ***//****************************************************************************//****************************************************************************//*** Local Variables ***//****************************************************************************/PRIVATE tsCircBuff sRxQueue; /**< Receive queue */PRIVATE tsCircBuff sTxQueue; /**< Transmit queue */PRIVATE const tsCircBuff *apsQueueList[SERIALQ_COUNT] /** Array of queue pointers */ = { &sRxQueue, &sTxQueue };/****************************************************************************//*** Exported Functions ***//****************************************************************************//****************************************************************************//*** Local Functions ***//****************************************************************************/PRIVATE void vSerialQ_Flush(teQueueRef eQueue);/**************************************************************************** * * NAME: vSerialQ_Init *//*! *\DESCRIPTION Initiliase the serial queues. *//* PARAMETERS Name RW Usage * None. * * RETURNS: * None. * * NOTES: * None. ****************************************************************************/PUBLIC void vSerialQ_Init(void){ vSerialQ_Flush(RX_QUEUE); vSerialQ_Flush(TX_QUEUE);}/**************************************************************************** * * NAME: vSerialQ_AddString *//*! *\DESCRIPTION Add a string to a serial queue *//* PARAMETERS Name RW Usage * None. * * RETURNS: * None. * * NOTES: * None. ****************************************************************************/PUBLIC void vSerialQ_AddString(teQueueRef eQueue, /**< Queue to operate on. */ char *psString) /**< String to add to queue */{ while (! bSerialQ_Full(eQueue) && *psString != '\0') { vSerialQ_AddItem(eQueue, (uint8) *psString); psString++; }}/**************************************************************************** * * NAME: vSerialQ_AddHex *//*! *\DESCRIPTION Convert a number to a hex string and add to a serial queue. *//* PARAMETERS Name RW Usage * None. * * RETURNS: * None. * * NOTES: * None. ****************************************************************************/PUBLIC void vSerialQ_AddHex(teQueueRef eQueue, /**< Queue to operate on. */ uint16 u16Value, /**< Value to convert to hex */ uint8 u8Digits) /**< Number of hex digits, maxumum 4 */{ uint8 u8Pos; char sHex[5]; char sChar[17] = "0123456789ABCDEF"; /* Sanity check digits */ if (u8Digits > 4) u8Digits = 4; /* Do hex conversion */ for (u8Pos = 0; u8Pos < u8Digits; u8Pos++) { sHex[u8Pos] = sChar[(u16Value >> ((u8Digits-u8Pos-1)*4)) &0xF]; sHex[u8Pos+1] = '\0'; } /* Write out string */ vSerialQ_AddString(eQueue, sHex);}/**************************************************************************** * * NAME: vSerialQ_AddItem *//*! *\DESCRIPTION Add a byte to a serial queue * * If the receive queue and it begins to fill disable receive on the UART. *//* PARAMETERS Name RW Usage * None. * * RETURNS: * None. * * NOTES: * None. ****************************************************************************/PUBLIC void vSerialQ_AddItem(teQueueRef eQueue, /**< Queue to operate on. */ uint8 u8Item) /**< Byte to add to queue */{ tsCircBuff *psQueue; uint16 u16NextLocation; uint16 u16Tail; uint16 u16Free; /* Set pointer to the requested queue */ psQueue = (tsCircBuff *)apsQueueList[eQueue]; /* Get local copy of the tail - the remove function might use it */ u16Tail = psQueue->u16Tail; u16NextLocation = (psQueue->u16Head + 1) & SERIALQ_MASK; if (u16NextLocation != u16Tail) { /* Space available in buffer so add data */ psQueue->u8Buff[psQueue->u16Head] = u8Item; psQueue->u16Head = u16NextLocation; psQueue->u16In++; /* Receive queue ? */ if (eQueue == RX_QUEUE) { /* Calculate the free characters */ if (u16Tail > psQueue->u16Head) u16Free = u16Tail - psQueue->u16Head; else u16Free = SERIALQ_SIZE + u16Tail - psQueue->u16Head; /* Did we just get low on free space ? */ if (u16Free == SERIALQ_FREE_LOW) { /* If UART Rx currently enabled - disable UART Rx */ if (bUart_GetRxEnable()) vUart_SetRxEnable(FALSE); } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -