666 lines
21 KiB
C
666 lines
21 KiB
C
/*
|
|
* ivec_mcal_uart.c
|
|
*
|
|
* Created on: 28-Oct-2024
|
|
* Author: altam
|
|
*/
|
|
#include "../Core/Include/ivec_mcal_uart.h"
|
|
#include "string.h"
|
|
#include "../../utils/utils.h"
|
|
#define LOG_STRING "ivec-mcal-uart"
|
|
|
|
/* Global UART Handles Array */
|
|
xMcalUartHandle* gpxMcalUartHandles[IVEC_MCAL_UART_MAX_PORT] = { 0 };
|
|
|
|
/* Static Variables */
|
|
static uint32_t prv_u32DataCount = 0; /* Data count for tracking received bytes */
|
|
|
|
/* Static Function Prototypes */
|
|
static eMcalUartPortNumber _prvMCAL_GetUartPort(UART_Regs* pxUartInstance);
|
|
|
|
void vMCAL_UartReadCallback(UART_Regs* pxUartInstance, uint8_t* pu8Buffer, bool bStatus)
|
|
{
|
|
if (bStatus)
|
|
{
|
|
prv_u32DataCount++;
|
|
|
|
IVEC_MCAL_LOG(LOG_STRING, "UART Receive Callback: %d", prv_u32DataCount);
|
|
|
|
for (uint32_t i = 0; i < IVEC_MCAL_UART_MAX_PORT; i++)
|
|
{
|
|
if (gpxMcalUartHandles[i] != NULL)
|
|
{
|
|
if (gpxMcalUartHandles[i]->pvUartRecvCallback != NULL)
|
|
{
|
|
gpxMcalUartHandles[i]->pvUartRecvCallback(_prvMCAL_GetUartPort(pxUartInstance),eIvecMcalUartEventRxArrived,(char*)pu8Buffer,1);
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void prv_vRxCallback(UART_Regs* pxUartInstance, uint32_t u32Event)
|
|
{
|
|
switch (u32Event)
|
|
{
|
|
case DL_UART_MAIN_IIDX_RX:
|
|
{
|
|
uint8_t u8RxBuffer[64] = { 0 }; // Adjust size as needed
|
|
uint32_t u32BytesRead = 0;
|
|
|
|
// Drain the RX FIFO and store the data in buffer
|
|
u32BytesRead = DL_UART_drainRXFIFO(pxUartInstance, u8RxBuffer, sizeof(u8RxBuffer));
|
|
|
|
// Clear the RX interrupt flag
|
|
DL_UART_clearInterruptStatus(pxUartInstance, DL_UART_MAIN_IIDX_RX);
|
|
|
|
// Process each byte of received data
|
|
for (uint32_t u32Index = 0; u32Index < u32BytesRead; u32Index++)
|
|
{
|
|
vMCAL_UartReadCallback(pxUartInstance, &u8RxBuffer[u32Index], true);
|
|
}
|
|
break;
|
|
}
|
|
case DL_UART_MAIN_IIDX_OVERRUN_ERROR:
|
|
DL_UART_clearInterruptStatus(pxUartInstance, DL_UART_MAIN_IIDX_OVERRUN_ERROR);
|
|
__asm("nop");
|
|
break;
|
|
case DL_UART_MAIN_IIDX_BREAK_ERROR:
|
|
DL_UART_clearInterruptStatus(pxUartInstance, DL_UART_MAIN_IIDX_BREAK_ERROR);
|
|
__asm("nop");
|
|
break;
|
|
case DL_UART_MAIN_IIDX_PARITY_ERROR:
|
|
DL_UART_clearInterruptStatus(pxUartInstance, DL_UART_MAIN_IIDX_PARITY_ERROR);
|
|
__asm("nop");
|
|
break;
|
|
case DL_UART_MAIN_IIDX_FRAMING_ERROR:
|
|
DL_UART_clearInterruptStatus(pxUartInstance, DL_UART_MAIN_IIDX_FRAMING_ERROR);
|
|
__asm("nop");
|
|
break;
|
|
case DL_UART_MAIN_IIDX_RX_TIMEOUT_ERROR:
|
|
DL_UART_clearInterruptStatus(pxUartInstance, DL_UART_MAIN_IIDX_RX_TIMEOUT_ERROR);
|
|
__asm("nop");
|
|
break;
|
|
case DL_UART_MAIN_IIDX_NOISE_ERROR:
|
|
DL_UART_clearInterruptStatus(pxUartInstance, DL_UART_MAIN_IIDX_NOISE_ERROR);
|
|
__asm("nop");
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
void UART0_IRQHandler()
|
|
{
|
|
prv_vRxCallback(UART0, DL_UART_Main_getPendingInterrupt(UART0));
|
|
}
|
|
|
|
void UART1_IRQHandler()
|
|
{
|
|
prv_vRxCallback(UART1, DL_UART_Main_getPendingInterrupt(UART1));
|
|
}
|
|
|
|
void UART2_IRQHandler()
|
|
{
|
|
prv_vRxCallback(UART2, DL_UART_Main_getPendingInterrupt(UART2));
|
|
}
|
|
|
|
/**
|
|
* @brief Get the UART instance based on the enum.
|
|
*
|
|
* @param eUartPortNumber UART port number enumeration.
|
|
* @return UART_Regs* Pointer to the UART registers.
|
|
*/
|
|
static UART_Regs* _prvMCAL_GetUartInstance(eMcalUartPortNumber eUartPortNumber)
|
|
{
|
|
switch (eUartPortNumber)
|
|
{
|
|
case eMcalUartPort1:
|
|
return UART0;
|
|
case eMcalUartPort2:
|
|
return UART1;
|
|
case eMcalUartPort3:
|
|
return UART2;
|
|
default:
|
|
return NULL; // Invalid UART port
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Get the UART port based on the instance.
|
|
*
|
|
* @param pxUartInstance Pointer to the UART registers.
|
|
* @return eMcalUartPortNumber UART port number enumeration.
|
|
*/
|
|
static eMcalUartPortNumber _prvMCAL_GetUartPort(UART_Regs* pxUartInstance)
|
|
{
|
|
switch ((uint32_t)pxUartInstance)
|
|
{
|
|
case (uint32_t)UART0:
|
|
return eMcalUartPort1;
|
|
case (uint32_t)UART1:
|
|
return eMcalUartPort2;
|
|
case (uint32_t)UART2:
|
|
return eMcalUartPort3;
|
|
default:
|
|
return eMcalUartPortMax; // Invalid UART port
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Private function to deinitialize UART instance.
|
|
*
|
|
* @param pxUartHandle Pointer to \link xMcalUartHandle \endlink.
|
|
* @return IVEC_CoreStatus_e Status of the UART deinitialization.
|
|
*/
|
|
static IVEC_CoreStatus_e _prvMCAL_UartDeInitInstance(xMcalUartHandle* pxUartHandle)
|
|
{
|
|
// Get the UART instance based on the port number in the handle
|
|
UART_Regs* pxUARTInstance = _prvMCAL_GetUartInstance(pxUartHandle->eUartPortNumber);
|
|
|
|
// Check if the UART instance is valid
|
|
if (pxUARTInstance == NULL)
|
|
{
|
|
return IVEC_CORE_STATUS_ERROR;
|
|
}
|
|
|
|
// Disable UART instance
|
|
DL_UART_Main_disable(pxUARTInstance);
|
|
|
|
// Disable interrupts for the UART instance
|
|
DL_UART_Main_disableInterrupt(pxUARTInstance,
|
|
DL_UART_MAIN_INTERRUPT_BREAK_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_FRAMING_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_NOISE_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_OVERRUN_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_PARITY_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_RX |
|
|
DL_UART_MAIN_INTERRUPT_RX_TIMEOUT_ERROR);
|
|
|
|
// Clear and disable NVIC interrupt requests based on UART instance
|
|
if (pxUARTInstance == UART0)
|
|
{
|
|
NVIC_DisableIRQ(UART0_INT_IRQn);
|
|
NVIC_ClearPendingIRQ(UART0_INT_IRQn);
|
|
// Reset the UART0 init flag if needed (b_UART0_init_flag = 0)
|
|
}
|
|
else if (pxUARTInstance == UART1)
|
|
{
|
|
NVIC_DisableIRQ(UART1_INT_IRQn);
|
|
NVIC_ClearPendingIRQ(UART1_INT_IRQn);
|
|
// Reset the UART1 init flag if needed (b_UART1_init_flag = 0)
|
|
}
|
|
else if (pxUARTInstance == UART2)
|
|
{
|
|
NVIC_DisableIRQ(UART2_INT_IRQn);
|
|
NVIC_ClearPendingIRQ(UART2_INT_IRQn);
|
|
// Reset the UART2 init flag if needed (b_UART2_init_flag = 0)
|
|
}
|
|
|
|
// Optionally, reset the UART clock configuration if needed
|
|
DL_UART_Main_setClockConfig(pxUARTInstance, NULL);
|
|
|
|
return IVEC_CORE_STATUS_SUCCESS;
|
|
}
|
|
|
|
/**
|
|
* @brief Function to deinitialize the UART peripheral.
|
|
*
|
|
* @param pxUartHandle Pointer to \link xMcalUartHandle \endlink.
|
|
* @return IVEC_McalCommonErr_e Returns a status based on the success or failure of the UART deinitialization operation.
|
|
*/
|
|
IVEC_McalCommonErr_e xMCAL_UartDeInit(xMcalUartHandle* pxUartHandle)
|
|
{
|
|
IVEC_CoreStatus_e xRetStatus;
|
|
IVEC_MCAL_FUNC_ENTRY(LOG_STRING);
|
|
IVEC_McalCommonErr_e eFuncStatus = commonMCAL_SUCCESS;
|
|
|
|
// Check for null pointer
|
|
if (pxUartHandle == NULL)
|
|
{
|
|
eFuncStatus = commonMCAL_INVALID_PARAM;
|
|
goto exit;
|
|
}
|
|
|
|
// Call the private deinit function
|
|
xRetStatus = _prvMCAL_UartDeInitInstance(pxUartHandle);
|
|
IVEC_MCAL_LOG(LOG_STRING, "Deinitializing UART status: %d", xRetStatus);
|
|
|
|
// Check the return status
|
|
if (xRetStatus != IVEC_CORE_STATUS_SUCCESS)
|
|
{
|
|
eFuncStatus = commonMCAL_DEINIT_FAIL;
|
|
goto exit;
|
|
}
|
|
|
|
exit:
|
|
IVEC_MCAL_FUNC_EXIT(LOG_STRING);
|
|
return eFuncStatus;
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
* @brief Internal function to read a single byte from UART.
|
|
* @param [in] pxUartHandle Pointer to \link McalUartHandle_t \endlink.
|
|
* @param [out] pucData Pointer to store the received byte.
|
|
* @return \link CoreStatus_t \endlink Returns CORE_STATUS_SUCCESS on success or CORE_STATUS_ERROR on failure.
|
|
*/
|
|
static IVEC_McalCommonErr_e prvIvecMcalUart_ReadByte(xMcalUartHandle* pxUartHandle, uint8_t* pucData)
|
|
{
|
|
/* Get the UART instance based on the port number in the handle */
|
|
UART_Regs* pUartInstance = _prvMCAL_GetUartInstance(pxUartHandle->eUartPortNumber);
|
|
|
|
/* Check if the UART instance is valid */
|
|
if (pUartInstance == NULL)
|
|
{
|
|
return commonMCAL_FAIL;
|
|
}
|
|
|
|
bool bDataReceived = false;
|
|
uint32_t u32StartTick = i32MCAL_GetTicks();
|
|
|
|
/* Wait until data is received or timeout occurs */
|
|
while (!DL_UART_isTXFIFOEmpty(pUartInstance) &&
|
|
((i32MCAL_GetTicks() - u32StartTick) < 50))
|
|
{
|
|
*pucData = DL_UART_Main_receiveData(pUartInstance);
|
|
bDataReceived = true;
|
|
}
|
|
|
|
/* Return error if no data was received */
|
|
if (!bDataReceived)
|
|
{
|
|
return commonMCAL_FAIL;
|
|
}
|
|
|
|
return commonMCAL_SUCCESS;
|
|
}
|
|
|
|
/**
|
|
* @brief Reads data from the UART port.
|
|
* @pre UART should be initialized before calling this function.
|
|
* @param [in] pxUartHandle Pointer to \link McalUartHandle_t \endlink.
|
|
* @param [out] pucData Pointer to buffer for storing received data.
|
|
* @param [in] u32DataLength Length of the data to read.
|
|
* @return \link IvecMcalCommonErr_t \endlink Returns the status of the UART read operation.
|
|
*/
|
|
IVEC_McalCommonErr_e xIvecMcalUart_Read(xMcalUartHandle* pxUartHandle, uint8_t* pucData, uint32_t u32DataLength)
|
|
{
|
|
int32_t i32RetVal;
|
|
IVEC_McalCommonErr_e eFuncStatus = commonMCAL_SUCCESS;
|
|
|
|
IVEC_MCAL_FUNC_ENTRY(LOG_STRING);
|
|
|
|
/* Validate the UART handle */
|
|
if (pxUartHandle == NULL)
|
|
{
|
|
eFuncStatus = commonMCAL_INVALID_PARAM;
|
|
goto exit;
|
|
}
|
|
|
|
/* Attempt to read a single byte from UART */
|
|
i32RetVal = prvIvecMcalUart_ReadByte(pxUartHandle, pucData);
|
|
|
|
/* Log the read operation status */
|
|
IVEC_MCAL_LOG(LOG_STRING, "UART read status: %d", i32RetVal);
|
|
|
|
/* Check if the read operation failed */
|
|
if (i32RetVal == commonMCAL_SUCCESS)
|
|
{
|
|
eFuncStatus = commonMCAL_FAIL;
|
|
goto exit;
|
|
}
|
|
|
|
exit:
|
|
IVEC_MCAL_FUNC_EXIT(LOG_STRING);
|
|
return eFuncStatus;
|
|
}
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////////////
|
|
////////////////////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
* @brief Static function to transmit data over UART.
|
|
* @param [in] pxUartHandle Pointer to UART handle structure.
|
|
* @param [in] pu8TxData Pointer to the data buffer to transmit.
|
|
* @param [in] u32Size Number of bytes to transmit.
|
|
* @return xCoreStatus_t Status of the transmission (STATUS_SUCCESS or STATUS_ERROR).
|
|
*/
|
|
static IVEC_CoreStatus_e _prvMCAL_UartTransmit(xMcalUartHandle* pxUartHandle, uint8_t* pu8TxData, uint32_t u32Size)
|
|
{
|
|
/* Retrieve UART instance based on the port number */
|
|
UART_Regs* pxUartInstance = _prvMCAL_GetUartInstance(pxUartHandle->eUartPortNumber);
|
|
|
|
/* Validate the UART instance and data size */
|
|
if (pxUartInstance == NULL || u32Size == 0)
|
|
{
|
|
return IVEC_CORE_STATUS_ERROR;
|
|
}
|
|
|
|
/* Track transmission timeout */
|
|
uint32_t u32TickStart = i32MCAL_GetTicks();
|
|
for (uint32_t u32Index = 0; u32Index < u32Size; u32Index++)
|
|
{
|
|
/* Transmit data byte */
|
|
DL_UART_transmitData(pxUartInstance, pu8TxData[u32Index]);
|
|
|
|
/* Wait for TX FIFO to become available, with timeout */
|
|
while (DL_UART_isTXFIFOFull(pxUartInstance) &&
|
|
((i32MCAL_GetTicks() - u32TickStart) < 100));
|
|
}
|
|
|
|
return IVEC_CORE_STATUS_SUCCESS;
|
|
}
|
|
|
|
/**
|
|
* @brief Function to write data to the UART port.
|
|
* @pre UART must be initialized before calling this function.
|
|
* @param [in] pxUartHandle Pointer to UART handle structure.
|
|
* @param [in] pu8Data Pointer to the data buffer to send.
|
|
* @param [in] u32DataLength Length of the data to be sent.
|
|
* @return IVEC_McalCommonErr_e Status of the write operation.
|
|
*/
|
|
IVEC_McalCommonErr_e xMCAL_UartWrite(xMcalUartHandle* pxUartHandle, uint8_t* pu8Data, uint32_t u32DataLength)
|
|
{
|
|
IVEC_McalCommonErr_e eFuncStatus = commonMCAL_SUCCESS;
|
|
int32_t i32Ret;
|
|
|
|
/* Log function entry */
|
|
IVEC_MCAL_FUNC_ENTRY(LOG_STRING);
|
|
|
|
/* Validate input parameters */
|
|
if (pxUartHandle == NULL)
|
|
{
|
|
eFuncStatus = commonMCAL_INVALID_PARAM;
|
|
goto exit;
|
|
}
|
|
|
|
/* Call static transmit function */
|
|
i32Ret = _prvMCAL_UartTransmit(pxUartHandle, pu8Data, u32DataLength);
|
|
|
|
/* Log transmit status */
|
|
IVEC_MCAL_LOG(LOG_STRING, "UART write status: %d", i32Ret);
|
|
|
|
/* Handle transmit errors */
|
|
if (i32Ret != IVEC_CORE_STATUS_SUCCESS)
|
|
{
|
|
eFuncStatus = commonMCAL_WRITE_FAIL;
|
|
goto exit;
|
|
}
|
|
|
|
exit:
|
|
/* Log function exit */
|
|
IVEC_MCAL_FUNC_EXIT(LOG_STRING);
|
|
return eFuncStatus;
|
|
}
|
|
|
|
|
|
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
/////////////////////////////////////////////////////////////////////////////////////
|
|
static IVEC_CoreStatus_e prvMCAL_UART_InitInstance(xMcalUartHandle* pxUartHandle, eMcalUartBaudRate xBaud)
|
|
{
|
|
if (pxUartHandle->eUartPortNumber == eMcalUartPort2)
|
|
{
|
|
DL_UART_Main_reset(UART1);
|
|
DL_UART_Main_enablePower(UART1);
|
|
DL_GPIO_initPeripheralOutputFunction(IOMUX_PINCM19, IOMUX_PINCM19_PF_UART1_TX);
|
|
DL_GPIO_initPeripheralInputFunction(IOMUX_PINCM20, IOMUX_PINCM20_PF_UART1_RX);
|
|
}
|
|
else if (pxUartHandle->eUartPortNumber == eMcalUartPort3)
|
|
{
|
|
DL_UART_Main_reset(UART2);
|
|
DL_UART_Main_enablePower(UART2);
|
|
DL_GPIO_initPeripheralOutputFunction(IOMUX_PINCM32, IOMUX_PINCM32_PF_UART2_TX);
|
|
DL_GPIO_initPeripheralInputFunction(IOMUX_PINCM33, IOMUX_PINCM33_PF_UART2_RX);
|
|
}
|
|
|
|
UART_Regs* pxUartInstance = _prvMCAL_GetUartInstance(pxUartHandle->eUartPortNumber);
|
|
|
|
if (pxUartInstance == NULL)
|
|
{
|
|
return commonMCAL_FAIL;
|
|
}
|
|
|
|
DL_UART_Config xUartConfig = { 0 };
|
|
xUartConfig.direction = DL_UART_MAIN_DIRECTION_TX_RX;
|
|
xUartConfig.mode = DL_UART_MAIN_MODE_NORMAL;
|
|
|
|
uint8_t ucDataLength = pxUartHandle->xUartConfig.eUartDataBit;
|
|
if (ucDataLength == eMcalUartDataBit7)
|
|
{
|
|
xUartConfig.wordLength = DL_UART_WORD_LENGTH_7_BITS;
|
|
}
|
|
else if (ucDataLength == eMcalUartDataBit8)
|
|
{
|
|
xUartConfig.wordLength = DL_UART_WORD_LENGTH_8_BITS;
|
|
}
|
|
|
|
uint8_t ucStopBit = pxUartHandle->xUartConfig.eUartStopBit;
|
|
if (ucStopBit == eMcalUartStopBit1)
|
|
{
|
|
xUartConfig.stopBits = DL_UART_STOP_BITS_ONE;
|
|
}
|
|
else if (ucStopBit == eMcalUartStopBit2)
|
|
{
|
|
xUartConfig.stopBits = DL_UART_STOP_BITS_TWO;
|
|
}
|
|
|
|
uint8_t ucParityBit = pxUartHandle->xUartConfig.eUartParityBit;
|
|
if (ucParityBit == eMcalUartParityNone)
|
|
{
|
|
xUartConfig.parity = DL_UART_PARITY_NONE;
|
|
}
|
|
else if (ucParityBit == eMcalUartParityOdd)
|
|
{
|
|
xUartConfig.parity = DL_UART_PARITY_ODD;
|
|
}
|
|
else if (ucParityBit == eMcalUartParityEven)
|
|
{
|
|
xUartConfig.parity = DL_UART_PARITY_EVEN;
|
|
}
|
|
|
|
xUartConfig.flowControl = DL_UART_FLOW_CONTROL_NONE;
|
|
|
|
DL_UART_ClockConfig xUartClockConfig = { 0 };
|
|
xUartClockConfig.clockSel = DL_UART_CLOCK_BUSCLK;
|
|
xUartClockConfig.divideRatio = DL_UART_MAIN_CLOCK_DIVIDE_RATIO_1;
|
|
|
|
DL_UART_Main_setClockConfig(pxUartInstance, (DL_UART_Main_ClockConfig*) &xUartClockConfig);
|
|
DL_UART_Main_init(pxUartInstance, (DL_UART_Main_Config*) &xUartConfig);
|
|
|
|
DL_UART_Main_setOversampling(pxUartInstance, DL_UART_OVERSAMPLING_RATE_16X);
|
|
|
|
|
|
if (xBaud == eMcalUartBaud115200)
|
|
{
|
|
DL_UART_Main_setBaudRateDivisor(pxUartInstance, 19, 34);
|
|
}
|
|
else if (xBaud == eMcalUartBaud9600)
|
|
{
|
|
DL_UART_Main_setBaudRateDivisor(pxUartInstance, 234, 24);
|
|
}
|
|
else if (xBaud == eMcalUartBaud2400)
|
|
{
|
|
DL_UART_Main_setBaudRateDivisor(pxUartInstance, 937, 32);
|
|
}
|
|
else if (xBaud == eMcalUartBaud4800)
|
|
{
|
|
DL_UART_Main_setBaudRateDivisor(pxUartInstance, 468, 48);
|
|
}
|
|
else if (xBaud == eMcalUartBaud14400)
|
|
{
|
|
DL_UART_Main_setBaudRateDivisor(pxUartInstance, 156, 16);
|
|
}
|
|
else if (xBaud == eMcalUartBaud19200)
|
|
{
|
|
DL_UART_Main_setBaudRateDivisor(pxUartInstance, 117, 12);
|
|
}
|
|
else if (xBaud == eMcalUartBaud28800)
|
|
{
|
|
DL_UART_Main_setBaudRateDivisor(pxUartInstance, 78, 8);
|
|
}
|
|
else if (xBaud == eMcalUartBaud33600)
|
|
{
|
|
DL_UART_Main_setBaudRateDivisor(pxUartInstance, 66, 62);
|
|
}
|
|
else if (xBaud == eMcalUartBaud38400)
|
|
{
|
|
DL_UART_Main_setBaudRateDivisor(pxUartInstance, 58, 38);
|
|
}
|
|
else if (xBaud == eMcalUartBaud57600)
|
|
{
|
|
DL_UART_Main_setBaudRateDivisor(pxUartInstance, 39, 4);
|
|
}
|
|
else if (xBaud == eMcalUartBaud230400)
|
|
{
|
|
DL_UART_Main_setBaudRateDivisor(pxUartInstance, 9, 49);
|
|
}
|
|
else if (xBaud == eMcalUartBaud460800)
|
|
{
|
|
DL_UART_Main_setBaudRateDivisor(pxUartInstance, 4, 57);
|
|
}
|
|
else if (xBaud == eMcalUartBaud921600)
|
|
{
|
|
DL_UART_Main_setBaudRateDivisor(pxUartInstance, 2, 28);
|
|
}
|
|
else if (xBaud == eMcalUartBaud1000000)
|
|
{
|
|
DL_UART_Main_setBaudRateDivisor(pxUartInstance, 2, 16);
|
|
}
|
|
else
|
|
{
|
|
DL_UART_Main_setBaudRateDivisor(pxUartInstance, 13, 1);
|
|
}
|
|
|
|
if (pxUartInstance == UART0)
|
|
{
|
|
DL_UART_Main_enableInterrupt(pxUartInstance,
|
|
DL_UART_MAIN_INTERRUPT_BREAK_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_FRAMING_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_NOISE_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_OVERRUN_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_PARITY_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_RX |
|
|
DL_UART_MAIN_INTERRUPT_RX_TIMEOUT_ERROR);
|
|
|
|
DL_UART_Main_enableFIFOs(UART0);
|
|
DL_UART_Main_setRXFIFOThreshold(UART0, DL_UART_RX_FIFO_LEVEL_ONE_ENTRY);
|
|
DL_UART_Main_setTXFIFOThreshold(UART0, DL_UART_TX_FIFO_LEVEL_1_2_EMPTY);
|
|
|
|
DL_UART_Main_enable(pxUartInstance);
|
|
NVIC_ClearPendingIRQ(UART0_INT_IRQn);
|
|
NVIC_EnableIRQ(UART0_INT_IRQn);
|
|
}
|
|
else if (pxUartInstance == UART1)
|
|
{
|
|
DL_UART_Main_enableInterrupt(pxUartInstance,
|
|
DL_UART_MAIN_INTERRUPT_BREAK_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_FRAMING_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_NOISE_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_OVERRUN_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_PARITY_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_RX |
|
|
DL_UART_MAIN_INTERRUPT_RX_TIMEOUT_ERROR);
|
|
|
|
DL_UART_Main_enableFIFOs(UART1);
|
|
DL_UART_Main_setRXFIFOThreshold(UART1, DL_UART_RX_FIFO_LEVEL_ONE_ENTRY);
|
|
DL_UART_Main_setTXFIFOThreshold(UART1, DL_UART_TX_FIFO_LEVEL_1_2_EMPTY);
|
|
|
|
DL_UART_Main_enable(pxUartInstance);
|
|
NVIC_ClearPendingIRQ(UART1_INT_IRQn);
|
|
NVIC_EnableIRQ(UART1_INT_IRQn);
|
|
}
|
|
else if (pxUartInstance == UART2)
|
|
{
|
|
DL_UART_Main_enableInterrupt(pxUartInstance,
|
|
DL_UART_MAIN_INTERRUPT_BREAK_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_FRAMING_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_NOISE_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_OVERRUN_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_PARITY_ERROR |
|
|
DL_UART_MAIN_INTERRUPT_RX |
|
|
DL_UART_MAIN_INTERRUPT_RX_TIMEOUT_ERROR);
|
|
|
|
DL_UART_Main_enableFIFOs(UART2);
|
|
DL_UART_Main_setRXFIFOThreshold(UART2, DL_UART_RX_FIFO_LEVEL_ONE_ENTRY);
|
|
DL_UART_Main_setTXFIFOThreshold(UART2, DL_UART_TX_FIFO_LEVEL_1_2_EMPTY);
|
|
|
|
DL_UART_Main_enable(pxUartInstance);
|
|
NVIC_ClearPendingIRQ(UART2_INT_IRQn);
|
|
NVIC_EnableIRQ(UART2_INT_IRQn);
|
|
}
|
|
|
|
return commonMCAL_SUCCESS;
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
* @brief Function to trigger the UART interrupt that is configured in the function pointer pvUartRecvCallback in \link McalUartHandle_s \endlink handle.
|
|
* @param [in] ind_type This is used as indication to the uart callback function. See \link IVEC_McalUartEvents_e \endlink
|
|
* @param [in] port UART Port number.
|
|
* @param [in] size Size of Data.
|
|
* @return nothing.
|
|
*/
|
|
#if 0
|
|
static void __prvMCAL_UartNotifyRecvCb(uint32 ind_type, ql_uart_port_number_e port, uint32 size)
|
|
{
|
|
IVEC_MCAL_LOG(LOG_STRING, "Uart Recv Callback:%d", ind_type);
|
|
for (int i = 0;i < IVEC_MCAL_UART_MAX_PORT; i++)
|
|
{
|
|
if (g_pxUartHandles[i] != NULL && port == (ql_uart_port_number_e)g_pxUartHandles[i]->eUartPortNumber)
|
|
{
|
|
if (g_pxUartHandles[i]->pvUartRecvCallback != NULL)
|
|
g_pxUartHandles[i]->pvUartRecvCallback((IVEC_McalUartEvents_e)ind_type & 0xffff, NULL, size);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
/**
|
|
* @brief Function to register UART handle used for registering UART receive callback fucntion.
|
|
* @warning This is a private function. It should not be call outside the file \link ivec_mcal_uart.c \endlink.
|
|
* @param pxUartHandle pointer to \link McalUartHandle_s \endlink
|
|
* @return Nothing
|
|
*/
|
|
static void prvMCAL_UartRegisterHandle(xMcalUartHandle* pxUartHandle)
|
|
{
|
|
gpxMcalUartHandles[pxUartHandle->eUartPortNumber] = pxUartHandle;
|
|
}
|
|
/**
|
|
* @brief Function to Init UART peripheral
|
|
* @pre Need to configure UART configuration/properties in \link McalUartHandle_s \endlink
|
|
* @param pxUartHandle pointer to \link McalUartHandle_s \endlink
|
|
* @return \link IVEC_McalCommonErr_e \endlink returns a status based on the success or failure of the UART Init operation.
|
|
*/
|
|
IVEC_McalCommonErr_e xMCAL_UartInit(xMcalUartHandle* pxUartHandle)
|
|
{
|
|
|
|
IVEC_MCAL_FUNC_ENTRY(LOG_STRING);
|
|
IVEC_McalCommonErr_e l_xFuncStatus = commonMCAL_SUCCESS;
|
|
if (pxUartHandle == NULL)
|
|
{
|
|
l_xFuncStatus = commonMCAL_INVALID_PARAM;
|
|
goto exit;
|
|
}
|
|
prvMCAL_UartRegisterHandle(pxUartHandle);
|
|
|
|
|
|
int l_i32Ret = prvMCAL_UART_InitInstance(pxUartHandle, pxUartHandle->xUartConfig.eUartBaudrate);
|
|
if (l_i32Ret != IVEC_CORE_STATUS_SUCCESS)
|
|
{
|
|
l_xFuncStatus = commonMCAL_INIT_FAIL;
|
|
goto exit;
|
|
}
|
|
|
|
exit:
|
|
IVEC_MCAL_FUNC_EXIT(LOG_STRING);
|
|
return l_xFuncStatus;
|
|
}
|