/**********************************************************************
* Title: uart.c
*
* Description: This is the standard code file for UART.
*
* Author: Guido Brunken
*
* Configuration ID: C_10014034_02_UART
*
*********************************************************************/
/**********************************************************************
* Include header files
*********************************************************************/
#include "uart.inc"
/*********************************************************************/
/* Dependent "optimize.cmd" */
/*********************************************************************/
/**********************************************************************
* File level pragmas
*********************************************************************/
/**********************************************************************
* Constant and Macro Definitions using #define
*********************************************************************/
/**********************************************************************
* Enumerations and Structures and Typedefs
*********************************************************************/
/*
// Information for each UART channel is stored in variables of this type:
typedef struct uart_chan_tag
{
uint16_t rx_in; // Rx buffer input index
uint16_t rx_out; // Rx buffer output index
uint16_t rx_count; // Rx buffer byte count
uint16_t rx_buf_size;
UART_Rx_Func_Ptr rx_func; // Rx callback function pointer
uint8_t *rx_buf; // Rx ring buffer
uint16_t tx_in; // Tx buffer input index
uint16_t tx_out; // Tx buffer output index
uint16_t tx_count; // Tx buffer byte counter
uint16_t tx_buf_size;
uint8_t *tx_buf; // Tx ring buffer
volatile uart_reg_t* ptr_reg; // Pointer to hardware registers
bool tx_progress; // Tx in progress
} uart_chan_t;
typedef struct uart_chan_init_data_tag
{
uint32_t channel; // channel index
uint8_t *ptr_rx_buf; // Rx ring buffer
uint8_t *ptr_tx_buf; // Tx ring buffer
uint16_t rx_size; // Rx buffer byte count
uint16_t tx_size; // Tx buffer byte counter
uart_reg_init_data_t reg_init_data; // init values for hardware registers
UART_Rx_Func_Ptr rx_func; // Rx callback function pointer
} uart_chan_init_data_t;
*/
/**********************************************************************
* Global and Const Variable Defining Definitions / Initializations
*********************************************************************/
/**********************************************************************
* Static Variables and Const Variables With File Level Scope
*********************************************************************/
static uart_chan_t uart_chan_ram_data[UART_NUM_HW_DEVICES_USED];
#if (BLUETOOTH_IS || (External_Bluetooth_IS && M12_NAV_MODEL_IS))
static uint8_t uart3_rx_buffer[100]; // Buffer of current receive message
static uint8_t uart3_tx_buffer[100]; // Buffer of current transmit message
static uint8_t uart3_tx_retry_count; // Count of retries (0..BT_TX_MAX_TRIES)
static bool uart3_rx_active; // currently receiving data from bluetooth module
#endif
/**********************************************************************
* ROM Const Variables With File Level Scope
*********************************************************************/
#if __v850__
#pragma BEGIN_ROM_CONST
#endif // __v850__
#if __v850__
#pragma END_ROM_CONST
#endif // __v850__
/**********************************************************************
* Function Prototypes for Private Functions with File Level Scope
*********************************************************************/
#if UART_CONF_USE_TX_INTERRUPT_IS
static void uart_do_tx(Uart_Devices_Enum_T device); // Transmit helper function
#endif //#if UART_CONF_USE_TX_INTERRUPT_IS
#if UART_CONF_USE_RX_INTERRUPT_IS
static void uart_rx_isr_handler ( Uart_Devices_Enum_T device );
#endif // #if UART_CONF_USE_RX_INTERRUPT_IS
/**********************************************************************
* Add User defined functions
*********************************************************************/
#include "uart.cu"
/**********************************************************************
* SCR Checks
*********************************************************************/
/**********************************************************************
* Function Definitions
*********************************************************************/
#if UART_CONF_USE_TX_INTERRUPT_IS
/*******************************************************************************
* Function: uart_do_tx
*
* Parameters: Channel
* Returns: Nothing
* Description: Transmit helper function. Takes one byte from transmit queue
* and sends it to the hardware. Provides common code for first
* byte transmission (before transmit interrupt is enabled) and
* successive byte transmission (from transmit interrupt).
*******************************************************************************/
static void uart_do_tx (Uart_Devices_Enum_T device)
{
uart_chan_t *pChan;
if ( ( device < UART_NUM_HW_DEVICES) && ( NULL != uart_chan[device] ) )
{
pChan = uart_chan[device]; /* optimize ROM size of function */
pChan->tx_count--; // Decrement tx buffer byte count
pChan->tx_out++; // Increment index
if ((pChan->tx_out) >= pChan->tx_buf_size )
{
pChan->tx_out = 0; // Wrap index
}
/* UART Transmission Mode shall be enabled previously */
// Write to hardware transmit register
pChan->ptr_reg->tx = pChan->tx_buf[pChan->tx_out];
}
else
{
/* invalid device */
}
}
#endif // #if UART_CONF_USE_TX_INTERRUPT_IS
#if UART_CONF_USE_RX_INTERRUPT_IS
/*******************************************************************************
* Function: uart_rx_isr_handler
*
* Parameters: None
* Returns: None
* Description: called rx ISR
*******************************************************************************/
static void uart_rx_isr_handler ( Uart_Devices_Enum_T device )
{
uart_chan_t *pChan;
volatile UINT8 data;
volatile UINT8 err;
pChan = uart_chan[device]; /* optimize ROM size of function */
data = pChan->ptr_reg->rx; // Read data from hardware register
err = pChan->ptr_reg->str; // Read status from hardware register
#if UART_CONF_USE_RX_CALLBACK_IS
// Can either read to buffer or use callback function
if ( NULL != pChan->rx_func )
{
// Callback
if ( UART_RX_DONE == pChan->rx_func(data, err) ) // Pass data and status to callback
{
return;
}
else
{
/* do nothing */
}
}
else
{
/* do nothing */
}
#endif // #if UART_CONF_USE_RX_CALLBACK_IS
if ( ( 0 == ( ( UART_ERR_FRAME | UART_ERR_PARITY ) & err ) ) && ( NULL != pChan->rx_buf ) )
{
// Buffer
if (pChan->rx_count < pChan->rx_buf_size ) // Check for buffer full
{
pChan->rx_buf[pChan->rx_in] = data; // Copy data to receive buffer
pChan->rx_count++; // Increment buffered byte count
pChan->rx_in++; // Increment rx buffer input index
if ((pChan->rx_in) >= pChan->rx_buf_size )
{
pChan->rx_in = 0; // Wrap index
}
}
else
评论0