chg-stn-motherboard-ti-mcu/Core/Source/ivec_mcal_uart.c

1165 lines
34 KiB
C

#include "../Core/Include/ivec_mcal_uart.h"
#include "string.h"
//#include "ivec_mcal_uart.h"
#define LOG_STRING "ivec-mcal-uart"
McalUartHandle_s* g_pxUartHandles[IVEC_MCAL_UART_MAX_PORT] = { 0 };
static uint32_t __gprv_u32DataCount = 0;
static volatile uint8_t u8rxbuffer1=0;
void UART_ReadCallback(UART_Regs *uart, uint8_t *buf, bool status)
{
if(status == true)
{
__gprv_u32DataCount += 1;
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 )
{
if (g_pxUartHandles[i]->pvUartRecvCallback != NULL)
g_pxUartHandles[i]->pvUartRecvCallback(IVEC_MCAL_UART_EVENT_RX_ARRIVED, (char *)buf, 1);
break;
}
}
}
}
void _prv_vrxcallback()
{
switch (DL_UART_Main_getPendingInterrupt(UART0))
{
case DL_UART_MAIN_IIDX_RX:
{
bool status = 0;
status = DL_UART_receiveDataCheck(UART0, (uint8_t *)&u8rxbuffer1);
if(status == true)
{
UART_ReadCallback(UART0,(uint8_t *)&u8rxbuffer1,status);
}
break;
}
// case DL_UART_MAIN_IIDX_EOT_DONE:
// {
// g_TxCplt = true;
// }
case DL_UART_MAIN_IIDX_OVERRUN_ERROR:
__asm("nop");
break;
case DL_UART_MAIN_IIDX_BREAK_ERROR:
__asm("nop");
break;
case DL_UART_MAIN_IIDX_PARITY_ERROR:
__asm("nop");
break;
case DL_UART_MAIN_IIDX_FRAMING_ERROR:
__asm("nop");
break;
case DL_UART_MAIN_IIDX_RX_TIMEOUT_ERROR:
__asm("nop");
break;
case DL_UART_MAIN_IIDX_NOISE_ERROR:
__asm("nop");
break;
default:
break;
}
switch (DL_UART_Main_getPendingInterrupt(UART1))
{
case DL_UART_MAIN_IIDX_RX:
{
bool status = 0;
status = DL_UART_receiveDataCheck(UART1, (uint8_t *)&u8rxbuffer1);
if(status == true)
{
UART_ReadCallback(UART1,(uint8_t *)&u8rxbuffer1,status);
}
break;
}
// case DL_UART_MAIN_IIDX_EOT_DONE:
// {
// g_TxCplt = true;
// }
case DL_UART_MAIN_IIDX_OVERRUN_ERROR:
__asm("nop");
break;
case DL_UART_MAIN_IIDX_BREAK_ERROR:
__asm("nop");
break;
case DL_UART_MAIN_IIDX_PARITY_ERROR:
__asm("nop");
break;
case DL_UART_MAIN_IIDX_FRAMING_ERROR:
__asm("nop");
break;
case DL_UART_MAIN_IIDX_RX_TIMEOUT_ERROR:
__asm("nop");
break;
case DL_UART_MAIN_IIDX_NOISE_ERROR:
__asm("nop");
break;
default:
break;
}
switch (DL_UART_Main_getPendingInterrupt(UART2))
{
case DL_UART_MAIN_IIDX_RX:
{
bool status = 0;
status = DL_UART_receiveDataCheck(UART2, (uint8_t *)&u8rxbuffer1);
if(status == true)
{
UART_ReadCallback(UART2,(uint8_t *)&u8rxbuffer1,status);
}
break;
}
// case DL_UART_MAIN_IIDX_EOT_DONE:
// {
// g_TxCplt = true;
// }
case DL_UART_MAIN_IIDX_OVERRUN_ERROR:
__asm("nop");
break;
case DL_UART_MAIN_IIDX_BREAK_ERROR:
__asm("nop");
break;
case DL_UART_MAIN_IIDX_PARITY_ERROR:
__asm("nop");
break;
case DL_UART_MAIN_IIDX_FRAMING_ERROR:
__asm("nop");
break;
case DL_UART_MAIN_IIDX_RX_TIMEOUT_ERROR:
__asm("nop");
break;
case DL_UART_MAIN_IIDX_NOISE_ERROR:
__asm("nop");
break;
default:
break;
}
}
void UART0_IRQHandler()
{
_prv_vrxcallback();
}
void UART1_IRQHandler()
{
_prv_vrxcallback();
}
void UART2_IRQHandler()
{
_prv_vrxcallback();
}
// Get the UART instance based on the enum
static UART_Regs* GetUartInstance(McalUartPortNumber_e eUartPortNumber)
{
switch (eUartPortNumber)
{
case mcalUART_PORT1:
return UART0;
case mcalUART_PORT2:
return UART1;
case mcalUART_PORT3:
return UART2;
default:
return NULL; // Invalid UART port
}
}
//////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////DEINIT/////////////////////////////////////////////////////////
static xCoreStatus_t uart_deinit(McalUartHandle_s* pxUartHandle)
{
// assert(b_UART1_init_flag == 1 || b_UART3_init_flag == 1);
// assert(uart_inst == UART1 || uart_inst == UART3);
UART_Regs* uart_inst = GetUartInstance(pxUartHandle->eUartPortNumber);
// Check if the UART instance is valid
if (uart_inst == NULL)
{
return STATUS_ERROR;
}
if(uart_inst==UART0)
{
NVIC_DisableIRQ(UART0_INT_IRQn);
DL_UART_disablePower(uart_inst);
//b_UART1_init_flag=0;
}
else if(uart_inst==UART1)
{
NVIC_DisableIRQ(UART1_INT_IRQn);
DL_UART_disablePower(uart_inst);
//b_UART1_init_flag=0;
}
else if(uart_inst==UART2)
{
NVIC_DisableIRQ(UART2_INT_IRQn);
DL_UART_disablePower(uart_inst);
//b_UART3_init_flag=0;
}
return STATUS_SUCCESS;
}
/**
* @brief Function to De-init UART Peripheral.
* @warning Donot call if UART is not initilized earlier.
* @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 De-Init operation.
*/
IVEC_McalCommonErr_e xMCAL_UartDeInit(McalUartHandle_s* pxUartHandle)
{
int l_i32Ret;
IVEC_MCAL_FUNC_ENTRY(LOG_STRING);
IVEC_McalCommonErr_e l_xFuncStatus = commonMCAL_SUCCESS;
if (pxUartHandle == NULL)
{
l_xFuncStatus = commonMCAL_INVALID_PARAM;
goto exit;
}
l_i32Ret = uart_deinit(pxUartHandle);
IVEC_MCAL_LOG(LOG_STRING, "DeInitilising UART status %d", l_i32Ret);
if (l_i32Ret != STATUS_SUCCESS)
{
l_xFuncStatus = commonMCAL_DEINIT_FAIL;
goto exit;
}
exit:
IVEC_MCAL_FUNC_EXIT(LOG_STRING);
return l_xFuncStatus;
}
///////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
//static xCoreStatus_t uart_read(McalUartHandle_s* pxUartHandle, unsigned char* pucData, unsigned int len)
//{
// uint16_t ijk = 0;
// while((!u8CMPLX_FifoQueueEmpty(&__gprv_MyEcuCANResponseQueue)) && ijk < len )
// {
// if( u8CMPLX_FifoDequeue(&__gprv_MyEcuCANResponseQueue, &pucData[ijk], &len, 1) == 1 )
// ijk++;
// }
// if (ijk == len)
// return STATUS_SUCCESS;
//
// return STATUS_ERROR;
//
//}
//
//
///**
// * @brief Function to Read data at from UART Port
// * @pre UART should be initilized first.
// * @param [in] pxUartHandle pointer to \link McalUartHandle_s \endlink
// * @param [in] pucData pointer to unsigned char. Used to read data from UART.
// * @param [in] u32DataLength Length of the data to read.
// * @return \link IVEC_McalCommonErr_e \endlink returns a status based on the success or failure of the UART Read operation.
// */
//IVEC_McalCommonErr_e xMCAL_UartRead(McalUartHandle_s* pxUartHandle, unsigned char* pucData, unsigned int u32DataLength)
//{
// int l_i32Ret;
// IVEC_MCAL_FUNC_ENTRY(LOG_STRING);
// IVEC_McalCommonErr_e l_xFuncStatus = commonMCAL_SUCCESS;
// if (pxUartHandle == NULL)
// {
// l_xFuncStatus = commonMCAL_INVALID_PARAM;
// goto exit;
// }
// l_i32Ret = uart_read(pxUartHandle, pucData, u32DataLength);
// IVEC_MCAL_LOG(LOG_STRING, "Reading UART status %d", l_i32Ret);
// if (l_i32Ret == STATUS_ERROR)
// {
// l_xFuncStatus = commonMCAL_READ_FAIL;
// goto exit;
// }
//exit:
// IVEC_MCAL_FUNC_EXIT(LOG_STRING);
// return l_xFuncStatus;
//}
static xCoreStatus_t uart_read(McalUartHandle_s* pxUartHandle, unsigned char* pucData)
{
// Get the UART instance based on the port number in the handle
UART_Regs* uart_inst = GetUartInstance(pxUartHandle->eUartPortNumber);
// Check if the UART instance is valid
if (uart_inst == NULL)
{
return STATUS_ERROR;
}
bool status = false;
while (!DL_UART_Main_isRXFIFOEmpty(uart_inst)) {
*pucData = DL_UART_Main_receiveData(uart_inst);
status = true;
}
//*pucData = DL_UART_receiveData(uart_inst);
if(status == false)
{
return STATUS_ERROR;
}
return STATUS_SUCCESS;
}
/**
* @brief Function to Read data at from UART Port
* @pre UART should be initilized first.
* @param [in] pxUartHandle pointer to \link McalUartHandle_s \endlink
* @param [in] pucData pointer to unsigned char. Used to read data from UART.
* @param [in] u32DataLength Length of the data to read.
* @return \link IVEC_McalCommonErr_e \endlink returns a status based on the success or failure of the UART Read operation.
*/
IVEC_McalCommonErr_e xMCAL_UartRead(McalUartHandle_s* pxUartHandle, unsigned char* pucData, unsigned int u32DataLength)
{
int l_i32Ret;
IVEC_MCAL_FUNC_ENTRY(LOG_STRING);
IVEC_McalCommonErr_e l_xFuncStatus = commonMCAL_SUCCESS;
if (pxUartHandle == NULL)
{
l_xFuncStatus = commonMCAL_INVALID_PARAM;
goto exit;
}
l_i32Ret = uart_read(pxUartHandle, pucData);
IVEC_MCAL_LOG(LOG_STRING, "Reading UART status %d", l_i32Ret);
if (l_i32Ret == STATUS_ERROR)
{
l_xFuncStatus = commonMCAL_READ_FAIL;
goto exit;
}
exit:
IVEC_MCAL_FUNC_EXIT(LOG_STRING);
return l_xFuncStatus;
}
///////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////
static xCoreStatus_t uart_transmit(McalUartHandle_s* pxUartHandle, uint8_t *u8txdata, uint32_t u32size)
{
// assert(b_UART1_init_flag == 1 || b_UART3_init_flag == 1);
// assert(uart_inst == UART1 || uart_inst == UART3);
// Get the UART instance based on the port number in the handle
UART_Regs* uart_inst = GetUartInstance(pxUartHandle->eUartPortNumber);
// Check if the UART instance is valid
if (uart_inst == NULL)
{
return STATUS_ERROR;
}
// Check if data length is valid
if(u32size==0)
{
return STATUS_ERROR;
}
for(int j=0;j<u32size;j++)
{
DL_UART_transmitData(uart_inst,u8txdata[j]);
//DL_UART_transmitDataBlocking(uart_inst, u8txdata[j]);
//while (DL_UART_isBusy(uart_inst));
while(DL_UART_Main_isBusy(uart_inst));
}
return STATUS_SUCCESS;
}
/**
* @brief Function to Write data at UART Port
* @pre UART should be initilized first.
* @param [in] pxUartHandle pointer to \link McalUartHandle_s \endlink
* @param [in] pucData pointer to unsigned char. Used to send data to UART.
* @param [in] u32DataLength Length of the Data that need to be write.
* @return \link IVEC_McalCommonErr_e \endlink returns a status based on the success or failure of the UART Write operation.
*/
IVEC_McalCommonErr_e xMCAL_UartWrite(McalUartHandle_s* pxUartHandle, unsigned char* pucData, unsigned int u32DataLength)
{
int l_i32Ret;
IVEC_MCAL_FUNC_ENTRY(LOG_STRING);
IVEC_McalCommonErr_e l_xFuncStatus = commonMCAL_SUCCESS;
if (pxUartHandle == NULL)
{
l_xFuncStatus = commonMCAL_INVALID_PARAM;
goto exit;
}
l_i32Ret = uart_transmit(pxUartHandle, pucData, u32DataLength);
IVEC_MCAL_LOG(LOG_STRING, "Writing UART status %d", l_i32Ret);
if (l_i32Ret != STATUS_SUCCESS)
{
l_xFuncStatus = commonMCAL_WRITE_FAIL;
goto exit;
}
exit:
IVEC_MCAL_FUNC_EXIT(LOG_STRING);
return l_xFuncStatus;
}
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
static xCoreStatus_t uart_init(McalUartHandle_s* pxUartHandle, McalUartBaudRate_e xBaud)
{
// DL_GPIO_initPeripheralOutputFunction(IOMUX_PINCM21, IOMUX_PINCM21_PF_UART0_TX);
// DL_GPIO_initPeripheralInputFunction(IOMUX_PINCM22, IOMUX_PINCM22_PF_UART0_RX);
// DL_GPIO_initPeripheralOutputFunction(IOMUX_PINCM20, IOMUX_PINCM20_PF_UART1_RX);
// DL_GPIO_initPeripheralInputFunction(IOMUX_PINCM19, IOMUX_PINCM19_PF_UART1_TX);
// assert(uart_inst == UART1 || uart_inst == UART3);IOMUX_PINCM16
// assert(xBaud == BAUD_115200 || xBaud == BAUD_9600);
// assert(!(b_UART1_init_flag == 1 && b_UART3_init_flag == 1));
DL_GPIO_initPeripheralOutputFunction(IOMUX_PINCM32, IOMUX_PINCM32_PF_UART2_TX);
DL_GPIO_initPeripheralInputFunction(IOMUX_PINCM33, IOMUX_PINCM33_PF_UART2_RX);
// Get the UART instance based on the port number in the handle
UART_Regs* uart_inst = GetUartInstance(pxUartHandle->eUartPortNumber);
// Check if the UART instance is valid
if (uart_inst == NULL)
{
return STATUS_ERROR;
}
DL_UART_Config xprvUartConfig = { 0 };
xprvUartConfig.direction = DL_UART_MAIN_DIRECTION_TX_RX;
xprvUartConfig.mode = DL_UART_MAIN_MODE_NORMAL;
//xprvUartConfig.wordLength = pxUartHandle->xUartConfig.eUartDataBit;
uint8_t datalength = pxUartHandle->xUartConfig.eUartDataBit;
if(datalength == mcalUART_DATABIT_7)
{
xprvUartConfig.wordLength = DL_UART_WORD_LENGTH_7_BITS;
}
else if(datalength == mcalUART_DATABIT_8)
{
xprvUartConfig.wordLength = DL_UART_WORD_LENGTH_8_BITS;
}
//xprvUartConfig.stopBits = pxUartHandle->xUartConfig.eUartStopBit;
uint8_t stopbit = pxUartHandle->xUartConfig.eUartStopBit;
if(stopbit == mcalUART_STOP_1)
{
xprvUartConfig.stopBits = DL_UART_STOP_BITS_ONE;
}
else if(stopbit == mcalUART_STOP_2)
{
xprvUartConfig.stopBits = DL_UART_STOP_BITS_TWO;
}
//xprvUartConfig.parity = pxUartHandle->xUartConfig.eUartParityBit;
uint8_t paritybit = pxUartHandle->xUartConfig.eUartParityBit;
if(paritybit == mcalUART_PARITY_NONE)
{
xprvUartConfig.parity = DL_UART_PARITY_NONE;
}
else if(paritybit == mcalUART_PARITY_ODD)
{
xprvUartConfig.parity = DL_UART_PARITY_ODD;
}
else if(paritybit == mcalUART_PARITY_EVEN)
{
xprvUartConfig.parity = DL_UART_PARITY_EVEN;
}
//xprvUartConfig.flowControl = pxUartHandle->xUartConfig.eUartFlowCtrl;
xprvUartConfig.flowControl = DL_UART_FLOW_CONTROL_NONE;
// uint8_t flowcontrolmode = pxUartHandle->xUartConfig.eUartFlowCtrl;
// if(flowcontrolmode == mcalUART_FC_NONE)
// {
// xprvUartConfig.flowControl = DL_UART_FLOW_CONTROL_NONE;
// }
// else if(flowcontrolmode == mcalUARTFC_HW)
// {
// xprvUartConfig.flowControl = ;
// }
DL_UART_ClockConfig gUART_0ClockConfig ={0};
gUART_0ClockConfig.clockSel = DL_UART_MAIN_CLOCK_BUSCLK;
gUART_0ClockConfig.divideRatio = DL_UART_MAIN_CLOCK_DIVIDE_RATIO_1;
DL_UART_Main_setClockConfig(uart_inst, (DL_UART_Main_ClockConfig *) &gUART_0ClockConfig);
DL_UART_Main_init(uart_inst, (DL_UART_Main_Config *) &xprvUartConfig);
/* Configure baud rate by setting oversampling and baud rate divisors.*/
if(xBaud==115200)
{
/*
* Target baud rate: 115200
* Actual baud rate: 115211.52
*/
DL_UART_Main_setOversampling(uart_inst, DL_UART_OVERSAMPLING_RATE_16X);
DL_UART_Main_setBaudRateDivisor(uart_inst, 17, 23);
}
else if(xBaud==9600)
{
DL_UART_Main_setOversampling(uart_inst, DL_UART_OVERSAMPLING_RATE_16X);
DL_UART_Main_setBaudRateDivisor(uart_inst, 208, 21);
}
/* Configure Interrupts */
if(uart_inst==UART0)
{
DL_UART_Main_enableInterrupt(uart_inst,
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_enable(uart_inst);
/*Clearing and Enabling Interrupt Requests*/
NVIC_ClearPendingIRQ(UART0_INT_IRQn);
NVIC_EnableIRQ(UART0_INT_IRQn);
//b_UART0_init_flag=1;
}
if(uart_inst==UART1)
{
DL_UART_Main_enableInterrupt(uart_inst,
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_enable(uart_inst);
/*Clearing and Enabling Interrupt Requests*/
NVIC_ClearPendingIRQ(UART1_INT_IRQn);
NVIC_EnableIRQ(UART1_INT_IRQn);
//b_UART1_init_flag=1;
}
else if(uart_inst==UART2)
{
DL_UART_Main_enableInterrupt(uart_inst,
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_enable(uart_inst);
/*Clearing and Enabling Interrupt Requests*/
NVIC_ClearPendingIRQ(UART2_INT_IRQn);
NVIC_EnableIRQ(UART2_INT_IRQn);
//b_UART2_init_flag=1;
}
return STATUS_SUCCESS;//TODO: FIX RETURN BUG
}
/*
IVEC_McalCommonErr_e _prvMCAL_UartPinInit(McalUartHandle_s *pxUartHandle)
{
int l_i32Ret;
IVEC_MCAL_FUNC_ENTRY(LOG_STRING);
McalCommonErr_e l_xFuncStatus = commonMCAL_SUCCESS;
if (pxUartHandle == NULL)
{
l_xFuncStatus = commonMCAL_INVALID_PARAM;
goto exit;
}
if(pxUartHandle->eUartPortNumber == mcalUART_PORT1)
goto exit;
else if(pxUartHandle->eUartPortNumber == mcalUART_PORT2)
{
l_i32Ret = ql_pin_set_func(pxUartHandle->u8UartTxPin, 3);
l_i32Ret = ql_pin_set_func(pxUartHandle->u8UartRxPin, 3);
if (l_i32Ret != QL_UART_SUCCESS)
{
l_xFuncStatus = commonMCAL_INIT_FAIL;
goto exit;
}
}
else if(pxUartHandle->eUartPortNumber == mcalUART_PORT3)
{
l_i32Ret = ql_pin_set_func(pxUartHandle->u8UartTxPin, 4);
l_i32Ret = ql_pin_set_func(pxUartHandle->u8UartRxPin, 4);
if (l_i32Ret != QL_UART_SUCCESS)
{
l_xFuncStatus = commonMCAL_INIT_FAIL;
goto exit;
}
}
exit:
IVEC_MCAL_FUNC_EXIT(LOG_STRING);
return l_xFuncStatus;
}*/
/**
* @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(McalUartHandle_s* pxUartHandle)
{
g_pxUartHandles[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(McalUartHandle_s* 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 = uart_init(pxUartHandle, pxUartHandle->xUartConfig.eUartBaudrate);
if (l_i32Ret != STATUS_SUCCESS)
{
l_xFuncStatus = commonMCAL_INIT_FAIL;
goto exit;
}
exit:
IVEC_MCAL_FUNC_EXIT(LOG_STRING);
return l_xFuncStatus;
}
//
//
///**
// * @file ivec_mcal_uart.c
// * @author Akshat Dabas (akshat@vecmocon.com)
// * @brief This source file contains API and Private members for UART Functionality
// * @version 0.1.0
// * @date 2024-Feb-17
// *
// * @copyright Copyright (c) 2024
// *
// */
//
//#include "../Core/Include/ivec_mcal_uart.h"
//#include "string.h"
//
///*GENERALIZED UART PERIPHERAL CONFIG*/
//
///*=======================================================================================PRIVATE_MEMBERS======================================================================================*/
//
//static volatile bool b_UART1_init_flag=0;
//
//static volatile bool b_UART3_init_flag=0;
//#define BUFFER_SIZE 100U
//
//static volatile uint8_t u8rxbuffer1[BUFFER_SIZE]={0};
//
//static volatile uint8_t u8rxbuffer3[BUFFER_SIZE]={0};
//
//static volatile int i32bufferIdx1=0;
//
//static volatile int i32bufferIdx3=0;
//
//
//static const DL_UART_Main_ClockConfig gUART_0ClockConfig = {
// .clockSel = DL_UART_MAIN_CLOCK_BUSCLK,
// .divideRatio = DL_UART_MAIN_CLOCK_DIVIDE_RATIO_1
//};
//
//static const DL_UART_Main_Config gUART_0Config = {
// .mode = DL_UART_MAIN_MODE_RS485,//DL_UART_MAIN_MODE_NORMAL,
// .direction = DL_UART_MAIN_DIRECTION_TX_RX,
// .flowControl = DL_UART_MAIN_FLOW_CONTROL_NONE,
// .parity = DL_UART_MAIN_PARITY_NONE,
// .wordLength = DL_UART_MAIN_WORD_LENGTH_8_BITS,
// .stopBits = DL_UART_MAIN_STOP_BITS_ONE
//};
//
//static void _prv_vrxcallback();
//static uint8_t _prv_u8MCAL_uart_receive(UART_Regs *uart_inst);
//
///*____________________________________________________________________________________________________________________________________________________________________________________________*/
//
//
///*=============================================================================================================================================================================================
// PRIVATE_DECLARATIONS
//==============================================================================================================================================================================================*/
//
///**
// * @brief Default Interrupt Callback for UART
// *
// */
//void UART3_IRQHandler()
//{
// _prv_vrxcallback();
//}
//
//void UART1_IRQHandler()
//{
// _prv_vrxcallback();
//}
///**
// * @brief Function to read byte by byte in IRQ Handler
// *
// */
//void _prv_vrxcallback()
//{
// switch (DL_UART_Main_getPendingInterrupt(UART1))
// {
// case DL_UART_MAIN_IIDX_RX:
//
// if(i32bufferIdx1>BUFFER_SIZE-1)
// {
// i32bufferIdx1=0;
// }
// while (!DL_UART_Main_isRXFIFOEmpty(UART1)) {
// u8rxbuffer1[i32bufferIdx1++] = DL_UART_Main_receiveData(UART1);
// }
//// u8rxbuffer1[i32bufferIdx1]=_prv_u8MCAL_uart_receive(UART1);
//// i32bufferIdx1++;
// break;
//
// case DL_UART_MAIN_IIDX_OVERRUN_ERROR:
// __asm("nop");
// break;
// case DL_UART_MAIN_IIDX_BREAK_ERROR:
// __asm("nop");
// break;
// case DL_UART_MAIN_IIDX_PARITY_ERROR:
// __asm("nop");
// break;
// case DL_UART_MAIN_IIDX_FRAMING_ERROR:
// __asm("nop");
// break;
// case DL_UART_MAIN_IIDX_RX_TIMEOUT_ERROR:
// __asm("nop");
// break;
// case DL_UART_MAIN_IIDX_NOISE_ERROR:
// __asm("nop");
// break;
// default:
// break;
// }
//
// switch (DL_UART_Main_getPendingInterrupt(UART3))
// {
// case DL_UART_MAIN_IIDX_RX:
//
// if(i32bufferIdx3>BUFFER_SIZE-1)
// {
// i32bufferIdx3=0;
// }
// u8rxbuffer3[i32bufferIdx3]=_prv_u8MCAL_uart_receive(UART3);
// i32bufferIdx3++;
// break;
//
// case DL_UART_MAIN_IIDX_OVERRUN_ERROR:
// __asm("nop");
// break;
// case DL_UART_MAIN_IIDX_BREAK_ERROR:
// __asm("nop");
// break;
// case DL_UART_MAIN_IIDX_PARITY_ERROR:
// __asm("nop");
// break;
// case DL_UART_MAIN_IIDX_FRAMING_ERROR:
// __asm("nop");
// break;
// case DL_UART_MAIN_IIDX_RX_TIMEOUT_ERROR:
// __asm("nop");
// break;
// case DL_UART_MAIN_IIDX_NOISE_ERROR:
// __asm("nop");
// break;
// default:
// break;
// }
//
//}
//
//
///**
// * @brief Receive in static buffer Function for UART
// *
// * @param uart_inst
// * @return uint8_t
// */
//static uint8_t _prv_u8MCAL_uart_receive(UART_Regs *uart_inst)
//{
//
// uint8_t temp;
// temp=DL_UART_receiveData(uart_inst);
// return temp;
//
//}
///*____________________________________________________________________________________________________________________________________________________________________________________________*/
//
//
//
//
///*=============================================================================================================================================================================================
// API
//==============================================================================================================================================================================================*/
//
//
///**
// * @brief Initialize UART Peripheral
// *
// * @param uart_inst Pointer to UART config registers
// * @param xBaud enum for desired UART baud rate
// * @return xCoreStatus_t
// */
//
//xCoreStatus_t xMCAL_uart_init(UART_Regs *uart_inst, xUart_baud_t xBaud)
//{
//// DL_GPIO_initPeripheralOutputFunction(IOMUX_PINCM21, IOMUX_PINCM21_PF_UART0_TX);
//// DL_GPIO_initPeripheralInputFunction(IOMUX_PINCM22, IOMUX_PINCM22_PF_UART0_RX);
//
//// DL_GPIO_initPeripheralOutputFunction(IOMUX_PINCM20, IOMUX_PINCM20_PF_UART1_RX);
//// DL_GPIO_initPeripheralInputFunction(IOMUX_PINCM19, IOMUX_PINCM19_PF_UART1_TX);
//// assert(uart_inst == UART1 || uart_inst == UART3);IOMUX_PINCM16
//// assert(xBaud == BAUD_115200 || xBaud == BAUD_9600);
//// assert(!(b_UART1_init_flag == 1 && b_UART3_init_flag == 1));
//
//
// DL_UART_Main_setClockConfig(uart_inst, (DL_UART_Main_ClockConfig *) &gUART_0ClockConfig);
// DL_UART_Main_init(uart_inst, (DL_UART_Main_Config *) &gUART_0Config);
//
// /* Configure baud rate by setting oversampling and baud rate divisors.*/
//
// if(xBaud==BAUD_115200)
// {
//
// /*
// * Target baud rate: 115200
// * Actual baud rate: 115211.52
// */
//
// DL_UART_Main_setOversampling(uart_inst, DL_UART_OVERSAMPLING_RATE_16X);
// DL_UART_Main_setBaudRateDivisor(uart_inst, 17, 23);
//
// }
// else if(xBaud==BAUD_9600)
// {
// DL_UART_Main_setOversampling(uart_inst, DL_UART_OVERSAMPLING_RATE_16X);
// DL_UART_Main_setBaudRateDivisor(uart_inst, 208, 21);
//
// }
//
// /* Configure Interrupts */
//
//// if(uart_inst==UART1)
//// {
// DL_UART_Main_enableInterrupt(uart_inst,
// 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_enable(uart_inst);
// /*Clearing and Enabling Interrupt Requests*/
//
// NVIC_ClearPendingIRQ(UART1_INT_IRQn);
// NVIC_EnableIRQ(UART1_INT_IRQn);
// b_UART1_init_flag=1;
// //}
//// else if(uart_inst==UART3)
//// {
////
////
//// DL_UART_Main_enableInterrupt(uart_inst,
//// 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_enable(uart_inst);
//// /*Clearing and Enabling Interrupt Requests*/
////
//// NVIC_ClearPendingIRQ(UART3_INT_IRQn);
//// NVIC_EnableIRQ(UART3_INT_IRQn);
//// b_UART3_init_flag=1;
//// }
//
// return STATUS_SUCCESS;//TODO: FIX RETURN BUG
//}
//
///**
// * @brief
// *
// * @param uart_inst Pointer to UART config registers
// * @return xCoreStatus_t
// */
//xCoreStatus_t xMCAL_uart_deinit(UART_Regs* uart_inst)
//{
//// assert(b_UART1_init_flag == 1 || b_UART3_init_flag == 1);
//// assert(uart_inst == UART1 || uart_inst == UART3);
//
//// if(uart_inst==UART1)
//// {
// NVIC_DisableIRQ(UART1_INT_IRQn);
// DL_UART_disablePower(uart_inst);
// b_UART1_init_flag=0;
// //}
//// else if(uart_inst==UART3)
//// {
//// NVIC_DisableIRQ(UART3_INT_IRQn);
//// DL_UART_disablePower(uart_inst);
//// b_UART3_init_flag=0;
//// }
// return STATUS_SUCCESS;
//}
//
///**
// * @brief Function to send received data to main
// *
// * @param uart_inst Pointer to UART config registers
// * @param u8rxdata Pointer to data buffer to receive data in
// * @return xCoreStatus_t
// */
//xCoreStatus_t xMCAL_uart_getdata(UART_Regs *uart_inst, uint8_t *u8rxdata, uint32_t u32rxbuffer_size)
//{
//// assert(b_UART1_init_flag == 1 || b_UART3_init_flag == 1);
//// assert(uart_inst == UART1 || uart_inst == UART3);
//
//// if((i32bufferIdx1==0)&&(i32bufferIdx3==0))
//// {
//// return STATUS_ERROR;
//// }
//
//
// //if(uart_inst==UART1)
// //{
//
//// for(int i=0;i<u32rxbuffer_size;i++)
//// {
// for(int j=0;j<=i32bufferIdx1;j++)
// {
// u8rxdata[j]=u8rxbuffer1[j];
// }
// //}
// return STATUS_SUCCESS;
//// }
//// else if(uart_inst==UART3)
//// {
//// for(int i=0;i<u32rxbuffer_size;i++)
//// {
//// for(int j=0;j<=i32bufferIdx3;j++)
//// {
//// u8rxdata[j]=u8rxbuffer3[j];
//// }
//// }
////
//// xMCAL_uart_bufferReset(uart_inst);
//// return STATUS_SUCCESS;
//// }
// return STATUS_ERROR;
//}
//
//
///**
// * @brief Function to directly transmit through UART
// *
// * @param uart_inst Pointer to UART config registers
// * @param u8txdata Pointer to data buffer to be sent
// * @param u32size Size of the passed buffer
// * @return xCoreStatus_t
// */
//xCoreStatus_t xMCAL_uart_transmit(UART_Regs *uart_inst, uint8_t *u8txdata, uint32_t u32size)
//{
//// assert(b_UART1_init_flag == 1 || b_UART3_init_flag == 1);
//// assert(uart_inst == UART1 || uart_inst == UART3);
//
//// if(u32size==0)
//// {
//// return STATUS_ERROR;
//// }
//
// for(int j=0;j<u32size;j++)
// {
//
// DL_UART_transmitData(uart_inst,u8txdata[j]);
// //DL_UART_transmitDataBlocking(uart_inst, u8txdata[j]);
// //while (DL_UART_isBusy(uart_inst));
// while(DL_UART_Main_isBusy(uart_inst));
// }
//
// return STATUS_SUCCESS;
//
//}
//
///**
// * @brief Function to Reset static u8rxbuffer on command
// *
// * @param uart_inst Pointer to UART config registers
// * @return xCoreStatus_t
// */
//xCoreStatus_t xMCAL_uart_bufferReset(UART_Regs *uart_inst)
//{
//// assert(b_UART1_init_flag == 1 || b_UART3_init_flag == 1);
//// assert(uart_inst == UART1 || uart_inst == UART3);
//
// //if(uart_inst==UART1)
// {
// i32bufferIdx1=0;
// memset((uint8_t *)&u8rxbuffer1[0], 0, sizeof(u8rxbuffer1));
// }
//// else if(uart_inst==UART3)
//// {
//// i32bufferIdx3=0;
//// memset((uint8_t *)&u8rxbuffer3[0], 0, sizeof(u8rxbuffer3));
//// }
//
// return STATUS_SUCCESS;
//}
///*____________________________________________________________________________________________________________________________________________________________________________________________*/
//