📄 uart.c
字号:
/***************************************************************************//*! *\MODULE Wireless UART with Flow Control * *\COMPONENT $RCSfile: Uart.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 Controls transmission and reception of data on the UART. * * This file operates the UART using an interrupt handler that is called when * characters are received or are ready to transmit: * * - Data received by the UART is added to the receive serial queue. * - Data for transmission by the UART is removed from the transmit queue * and transmitted by the UART. * * Hardware flow control using RTS and CTS is implemented in one of two ways: * * \par Manual Hardware Flow Control: * is used when UART_AUTOFLOW in Uart.h is defined to FALSE. * In this mode the RTS line is lowered by software as the receive queue begins to * fill and raised as the the queue begins to empty. An interrupt is programmed to * occurr when the CTS line is changed, the state of the CTS line is read and * the transmission of data is allowed or denied dependent upon the state of the * CTS line. * * \par Automatic Hardware Flow Control: * can be enabled in the UART by setting * the UART_AUTOFLOW in Uart.h define to TRUE. In this mode when the 16 byte receive * FIFO begins to receive the 16th byte the UART's RTS line is lowered and * raised once space is available in the FIFO again . When the UART's CTS * line is low the UART will not transmit any data from the transmit FIFO. * * \attention The FTDI 3V Serial to USB cables supplied with the evaluation kits * and their drivers do not react quickly enough to the RTS being lowered which * results in an extra character being sent to the UART, which is then lost. When using * two UARTs with automatic flow control they should react quickly enough to prevent * such data loss. Manual Hardware Flow Control is therefore recommended when using the * evaluation kits and FTDI cables. *//*\CHANGE HISTORY * * $Log: Uart.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 <JPI.h>#include <gdb.h>#include "SerialQ.h"#include "Uart.h"/****************************************************************************//*** Macro Definitions ***//****************************************************************************/#if UART == E_JPI_UART_0 #define UART_START_ADR 0x30000000UL /**< Address of start of UART's registers */#else #define UART_START_ADR 0x40000000UL /**< Address of start of UART's registers */#endif#define UART_DLM_OFFSET 0x04 /**< Offset of UART's DLM register */#define UART_LCR_OFFSET 0x0C /**< Offset of UART's LCR register */#define UART_MCR_OFFSET 0x10 /**< Offset of UART's MCR register */#define UART_EFR_OFFSET 0x20 /**< Offset of UART's EFR register *//****************************************************************************//*** Type Definitions ***//****************************************************************************//****************************************************************************//*** Local Function Prototypes ***//****************************************************************************//****************************************************************************//*** Exported Variables ***//****************************************************************************//****************************************************************************//*** Local Variables ***//****************************************************************************/PRIVATE bool_t bRxEnable; /**< UART receive enabled */PRIVATE bool_t bTxEnable; /**< UART transmit enabled */PRIVATE bool_t bModemInt; /**< UART modem status change interrupt enabled */PRIVATE bool_t bTxIntServiced; /**< LAst UART transmit interrupt was serviced, expect another transmit interrupt to follow *//****************************************************************************//*** Exported Functions ***//****************************************************************************//****************************************************************************//*** Local Functions ***//****************************************************************************/PRIVATE void vUart_SetBaudRate(uint32 u32BaudRate);PRIVATE void vUart_SetRts(bool_t);PRIVATE void vUart_SetAutoFlow(bool_t);PRIVATE void vUart_HandleUartInterrupt(uint32 u32Device, uint32 u32ItemBitmap);/**************************************************************************** * * NAME: vUart_Init *//*! *\DESCRIPTION Initialise UART. * * Sets baud rate, interrupt handling and flow control for the UART. *//* PARAMETERS Name RW Usage * None. * * RETURNS: * None. * * NOTES: * None. ****************************************************************************/PUBLIC void vUart_Init(void){ /* Start with Receive and transmit enabled */ bRxEnable = TRUE; bTxEnable = TRUE; /* Modem status chane interrupt is dependent upon flow contorl mode */ #if UART_AUTOFLOW bModemInt = FALSE; #else bModemInt = TRUE; #endif /* Note we are not currently servicing transmit interrupts */ bTxIntServiced = FALSE; /* Enable UART 0 */ vJPI_UartEnable(UART); /* Reset UART */ vJPI_UartReset(UART, TRUE, TRUE); vJPI_UartReset(UART, FALSE, FALSE); /* Register function that will handle UART interrupts */ #if UART == E_JPI_UART_0 vJPI_Uart0RegisterCallback(vUart_HandleUartInterrupt); #else vJPI_Uart1RegisterCallback(vUart_HandleUartInterrupt); #endif /* Allow use of RTS/CTS pins with UUART */ vJPI_UartSetRTSCTS(UART, TRUE); /* Set automatic flow control */ vUart_SetAutoFlow(UART_AUTOFLOW); /* Set the clock divisor register to give required buad, this has to be done directly as the normal routines (in ROM) do not support all baud rates */ vUart_SetBaudRate(UART_BAUD_RATE); /* Set remaining settings */ vJPI_UartSetControl(UART, FALSE, FALSE, E_JPI_UART_WORD_LEN_8, TRUE, FALSE); /* Turn on RTS */ vUart_SetRts(bRxEnable); /* Is CTS bit set meaning CTS is off ? */ if (u8JPI_UartReadModemStatus(UART) & 0x10) { /* Disable transmit */ vUart_SetTxEnable(FALSE); } /* Is CTS bit is clear meaning CTS is on ? */ else { /* Enable transmit */ vUart_SetTxEnable(TRUE); } /* Turn on modem status, tx, rx interrupts */ vJPI_UartSetInterrupt(UART, bModemInt, FALSE, TRUE, bRxEnable, E_JPI_UART_FIFO_LEVEL_1);}/**************************************************************************** * * NAME: vUart_StartTx *//*! *\DESCRIPTION Start transmitting from UART. * * If we are not currently servicing transmit interrupts and we have data * to transmit, begin transmission by writing the initial character to * the transmit buffer. *//* PARAMETERS Name RW Usage * None. * * RETURNS: * None. * * NOTES: * None. ****************************************************************************/PUBLIC void vUart_StartTx(void){ /* Did we not service the last tx interrupt ? */ if (bTxIntServiced == FALSE) { /* Has interrupt driven transmit stalled (tx fifo is empty) */ if (u8JPI_UartReadLineStatus(UART) & E_JPI_UART_LS_THRE) { if(!bSerialQ_Empty(TX_QUEUE) && bTxEnable) { vJPI_UartWriteData(UART, u8SerialQ_RemoveItem(TX_QUEUE)); } } }}/**************************************************************************** * * NAME: vUart_TxCharIsr *//*! *\DESCRIPTION Transmit character interrupt service routine. *//* PARAMETERS Name RW Usage * None. * * RETURNS: * None. * * NOTES: * None. ****************************************************************************/PUBLIC void vUart_TxCharIsr(void){ if(!bSerialQ_Empty(TX_QUEUE) && bTxEnable) { vJPI_UartWriteData(UART, u8SerialQ_RemoveItem(TX_QUEUE)); /* Note we serviced the interrupt, tx interrupts will continue */ bTxIntServiced = TRUE; } else { /* Note we didn't service the interrupt, tx interrupts will now stop */ bTxIntServiced = FALSE; }}/**************************************************************************** * * NAME: vUart_RxCharIsr *//*! *\DESCRIPTION Receive character interrupt service routeine. *//* PARAMETERS Name RW Usage * None. * * RETURNS: * None. * * NOTES: * None. ****************************************************************************/PUBLIC void vUart_RxCharIsr(uint8 u8RxChar) /**< Received character */{ vSerialQ_AddItem(RX_QUEUE, u8RxChar);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -