📄 uart.c
字号:
/****************************************************************************
*
* MODULE: Wireless UART
*
* COMPONENT: uart.c,v
*
* VERSION:
*
* REVISION: 1.4
*
* DATED: 2006/11/09 12:57:10
*
* STATUS: Exp
*
* AUTHOR: Ian Morris
*
* DESCRIPTION
*
* CHANGE HISTORY:
*
* uart.c,v
* Revision 1.4 2006/11/09 12:57:10 imorr
* Updated copyright information in file header
*
* Revision 1.3 2006/11/06 15:42:10 imorr
* Updated to use interrupt driven comms
*
* Revision 1.2 2006/08/24 15:20:41 imorr
* Fixed bug in baud rate calculation caused by a rounding error
*
* Revision 1.1 2006/08/24 14:58:48 imorr
* Initial version
*
*
*
* LAST MODIFIED BY: imorr
* $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 <AppHardwareApi.h>
#include "config.h"
#include "serialq.h"
#include "uart.h"
/****************************************************************************/
/*** Macro Definitions ***/
/****************************************************************************/
#define UART0_START_ADR 0x30000000UL
#define UART1_START_ADR 0x40000000UL
#define UART_LCR_OFFSET 0x0C
#define UART_DLM_OFFSET 0x04
/****************************************************************************/
/*** Type Definitions ***/
/****************************************************************************/
/****************************************************************************/
/*** Local Function Prototypes ***/
/****************************************************************************/
/****************************************************************************/
/*** Exported Variables ***/
/****************************************************************************/
/****************************************************************************/
/*** Local Variables ***/
/****************************************************************************/
/****************************************************************************/
/*** Exported Functions ***/
/****************************************************************************/
/****************************************************************************/
/*** Local Functions ***/
/****************************************************************************/
PRIVATE void UART_SetBuadRate(uint32 u8UartRegAdr, uint32 u32BaudRate);
PRIVATE void UART_HandleUart0Interrupt(uint32 u32Device, uint32 u32ItemBitmap);
PRIVATE void UART_HandleUart1Interrupt(uint32 u32Device, uint32 u32ItemBitmap);
/****************************************************************************
*
* NAME: UART_Init
*
* DESCRIPTION:
*
* PARAMETERS: Name RW Usage
* None.
*
* RETURNS:
* None.
*
* NOTES:
* None.
****************************************************************************/
PUBLIC void UART_Init(uint8 u8Uart)
{
/* Enable UART 0 */
vAHI_UartEnable(u8Uart);
vAHI_UartReset(u8Uart, TRUE, TRUE);
vAHI_UartReset(u8Uart, FALSE, FALSE);
/* Register function that will handle UART interrupts */
if (u8Uart == E_AHI_UART_0)
{
/* 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 */
UART_SetBuadRate(UART0_START_ADR, UART0_BAUD_RATE);
vAHI_Uart0RegisterCallback(UART_HandleUart0Interrupt);
}
else
{
/* 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 */
UART_SetBuadRate(UART1_START_ADR, UART1_BAUD_RATE);
vAHI_Uart1RegisterCallback(UART_HandleUart1Interrupt);
}
vAHI_UartSetControl(u8Uart, FALSE, FALSE, E_AHI_UART_WORD_LEN_8, TRUE, FALSE);
vAHI_UartSetRTSCTS(u8Uart, FALSE);
vAHI_UartSetInterrupt(u8Uart, FALSE, FALSE, TRUE, TRUE, E_AHI_UART_FIFO_LEVEL_1);
}
/****************************************************************************
*
* NAME: UART_SetBuadRate
*
* DESCRIPTION:
*
* PARAMETERS: Name RW Usage
*
* RETURNS:
*
****************************************************************************/
PRIVATE void UART_SetBuadRate(uint32 u8UartRegAdr, uint32 u32BaudRate)
{
uint8 *pu8Reg;
uint8 u8TempLcr;
uint16 u16Divisor;
uint32 u32Remainder;
/* Put UART into clock divisor setting mode */
pu8Reg = (uint8 *)(u8UartRegAdr + UART_LCR_OFFSET);
u8TempLcr = *pu8Reg;
*pu8Reg = u8TempLcr | 0x80;
/* Write to divisor registers:
Divisor register = 16MHz / (16 x baud rate) */
u16Divisor = (uint16)(16000000UL / (16UL * u32BaudRate));
/* Correct for rounding errors */
u32Remainder = (uint32)(16000000UL % (16UL * u32BaudRate));
if (u32Remainder >= ((16UL * u32BaudRate) / 2))
{
u16Divisor += 1;
}
pu8Reg = (uint8 *)u8UartRegAdr;
*pu8Reg = (uint8)(u16Divisor & 0xFF);
pu8Reg = (uint8 *)(u8UartRegAdr + UART_DLM_OFFSET);
*pu8Reg = (uint8)(u16Divisor >> 8);
/* Put back into normal mode */
pu8Reg = (uint8 *)(u8UartRegAdr + UART_LCR_OFFSET);
u8TempLcr = *pu8Reg;
*pu8Reg = u8TempLcr & 0x7F;
}
/****************************************************************************
*
* NAME: UART_StartTx
*
* DESCRIPTION:
*
* PARAMETERS: Name RW Usage
* None.
*
* RETURNS:
* None.
*
* NOTES:
* None.
****************************************************************************/
PUBLIC void UART_StartTx(uint8 u8Uart)
{
/* Has interrupt driven transmit stalled (tx fifo is empty) */
if (u8AHI_UartReadLineStatus(u8Uart) & E_AHI_UART_LS_THRE)
{
if(!bSerialQ_Empty(TX_QUEUE))
{
UART1_TX();
vAHI_UartWriteData(u8Uart, u8SerialQ_RemoveItem(TX_QUEUE));
}
}
}
/****************************************************************************
*
* NAME: UART_TxCharISR
*
* DESCRIPTION:
*
* PARAMETERS: Name RW Usage
* None.
*
* RETURNS:
* None.
*
* NOTES:
* None.
****************************************************************************/
PUBLIC void UART_TxCharISR(uint8 u8Uart)
{
if(!bSerialQ_Empty(TX_QUEUE))
{
UART1_TX();
vAHI_UartWriteData(u8Uart, u8SerialQ_RemoveItem(TX_QUEUE));
}
}
/****************************************************************************
*
* NAME: UART_RxCharISR
*
* DESCRIPTION:
*
* PARAMETERS: Name RW Usage
* None.
*
* RETURNS:
* None.
*
* NOTES:
* None.
****************************************************************************/
PUBLIC void UART_RxCharISR(uint8 u8Uart, uint8 u8RxChar)
{
vSerialQ_AddItem(RX_QUEUE, u8RxChar);
}
/****************************************************************************
*
* NAME: UART_HandleUart0Interrupt
*
* DESCRIPTION:
*
* PARAMETERS: Name RW Usage
* None.
*
* RETURNS:
* None.
*
* NOTES:
* None.
****************************************************************************/
PRIVATE void UART_HandleUart0Interrupt(uint32 u32Device, uint32 u32ItemBitmap)
{
if (u32Device == E_AHI_DEVICE_UART0)
{
if ((u32ItemBitmap & 0x000000FF) == E_AHI_UART_INT_RXDATA)
{
UART_RxCharISR(E_AHI_UART_0, u8AHI_UartReadData(E_AHI_UART_0));
}
else if (u32ItemBitmap == E_AHI_UART_INT_TX)
{
UART_TxCharISR(E_AHI_UART_0);
}
}
}
/****************************************************************************
*
* NAME: UART_HandleUart1Interrupt
*
* DESCRIPTION:
*
* PARAMETERS: Name RW Usage
* None.
*
* RETURNS:
* None.
*
* NOTES:
* None.
****************************************************************************/
PRIVATE void UART_HandleUart1Interrupt(uint32 u32Device, uint32 u32ItemBitmap)
{
if (u32Device == E_AHI_DEVICE_UART1)
{
if ((u32ItemBitmap & 0x000000FF) == E_AHI_UART_INT_RXDATA)
{
UART1_RX();
UART_RxCharISR(E_AHI_UART_1, u8AHI_UartReadData(E_AHI_UART_1));
}
else if (u32ItemBitmap == E_AHI_UART_INT_TX)
{
UART_TxCharISR(E_AHI_UART_1);
}
}
}
/****************************************************************************/
/*** END OF FILE ***/
/****************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -