/******************************************************************************
*
* [url=home.php?mod=space&uid=247401]@brief[/url] providing common UART API.
*
******************************************************************************/
#include "UART_app.h"
static uint8_t *pUART_TxBuff[MAX_UART_NO] = {NULL}; /* pointer to RxBuf */
static uint8_t *pUART_RxBuff[MAX_UART_NO] = {NULL}; /* pointer to TxBuf */
static uint16_t gu16UART_TxBuffPos[MAX_UART_NO] = {0}; /* write position to RxBuf */
static uint16_t gu16UART_RxBuffPos[MAX_UART_NO] = {0}; /* read position to TxBuf */
static uint32_t gu32UART_BuffSize[MAX_UART_NO] = {0}; /* buffer size*/
UART_TxDoneCallbackType UART_TxDoneCallback[MAX_UART_NO] = {NULL};
UART_RxDoneCallbackType UART_RxDoneCallback[MAX_UART_NO] = {NULL};
/*****************************************************************************//*!
*
* @brief send a series of charecters using interrupt mode.
*
* @param[in] pUART base of UART port
* @param[in] pSendBuff pointer of charecters to send
* @param[in] u32Length number of charecters
*
* [url=home.php?mod=space&uid=266161]@return[/url] none
*
* [url=home.php?mod=space&uid=72445]@[/url] Pass/ Fail criteria:
*****************************************************************************/
void UART_SendInt(UART_Type *pUART, uint8_t *pSendBuff, uint32_t u32Length)
{
uint8_t u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
/* save user defined send buffer pointers and size */
pUART_TxBuff[u8Port] = pSendBuff;
gu32UART_BuffSize[u8Port] = u32Length;
gu16UART_TxBuffPos[u8Port] = 0;
UART_EnableTxBuffEmptyInt(pUART); /* enable tx interrupt */
}
/*****************************************************************************//*!
*
* @brief receive a series of charecters using interrupt mode.
*
* @param[in] pUART base of UART port
* @param[in] pSendBuff pointer of charecters to send
* @param[in] u32Length number of charecters
*
* @return none
*
* @ Pass/ Fail criteria:
*****************************************************************************/
void UART_ReceiveInt(UART_Type *pUART, uint8_t *pReceiveBuff, uint32_t u32Length)
{
uint8_t u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
/* save user defined read buffer pointers and size */
pUART_RxBuff[u8Port] = pReceiveBuff;
gu32UART_BuffSize[u8Port] = u32Length;
gu16UART_RxBuffPos[u8Port] = 0;
UART_EnableRxBuffFullInt(pUART); /* enable rx interrupt */
}
void UART_HandleInt(UART_Type *pUART)
{
uint8_t u8Port;
uint8_t *pRdBuff;
uint8_t *pWrBuff;
volatile uint8_t read_temp = 0;
u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
/* check overrun flag */
if(UART_CheckFlag(pUART,UART_FlagOR))
{
read_temp = UART_ReadDataReg(pUART);
}
/* check receiver */
else if(UART_IsRxBuffFull(pUART))
{
/* there's data received in rx buffer */
pRdBuff = pUART_RxBuff[u8Port]; /* get user read buffer */
read_temp = UART_ReadDataReg(pUART);
pRdBuff[gu16UART_RxBuffPos[u8Port]++]= read_temp; /* save received data */
//pRdBuff[gu16UART_RxBuffPos[u8Port]++] = UART_ReadDataReg(pUART);
if(gu16UART_RxBuffPos[u8Port] == gu32UART_BuffSize[u8Port])
{
/* User read buffer is full, end receive */
UART_DisableRxBuffFullInt(pUART);
if (UART_RxDoneCallback[u8Port])
{
/* call back function to tell user that rx is done */
UART_RxDoneCallback[u8Port]();
}
}
}
/* check transmitter */
else if(UART_IsTxBuffEmpty(pUART))
{
if(gu16UART_TxBuffPos[u8Port] != gu32UART_BuffSize[u8Port])
{
/* user tx buffer not empty */
pWrBuff = pUART_TxBuff[u8Port];
UART_WriteDataReg(pUART, pWrBuff[gu16UART_TxBuffPos[u8Port]++]);
}
else
{
UART_DisableTxBuffEmptyInt(pUART);
if (UART_TxDoneCallback[u8Port])
{
/* call back function to tell user that tx is done */
UART_TxDoneCallback[u8Port]();
}
}
}
else
{
/* default interrupt */
}
}
void UART_SetTxDoneCallback(UART_Type *pUART, UART_TxDoneCallbackType pfnCallback)
{
uint8_t u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
UART_TxDoneCallback[u8Port] = pfnCallback;
}
void UART_SetRxDoneCallback(UART_Type *pUART, UART_RxDoneCallbackType pfnCallback)
{
uint8_t u8Port = ((uint32_t)pUART-(uint32_t)UART0)>>12;
UART_RxDoneCallback[u8Port] = pfnCallback;
}
|