chg-stn-motherboard-ti-mcu/ivec_ECU/ivec_ecu_can/src/ivec_ecu_can.c

118 lines
3.3 KiB
C

#include "../ivec_ECU/ivec_ecu_can/inc/ivec_ecu_can.h"
#include "../ivec_ECU/ivec_ecu_uart/inc/ivec_ecu_uart.h"
#include "../Core/Include/ivec_mcal_uart.h"
static can_buff_t g_canBuffer[1024] = {0}; /*All the CAN Id, Len and Data are stored in this array*/
QUEUE(g_canQueue, g_canBuffer);
void mcu_FDCAN_RxFifo_Callback(uint32_t Identifier, uint8_t *data, uint16_t DataLength)
{
if (!IS_FULL(g_canQueue)) {
store_msg_from_isr_to_queue(Identifier, data, DataLength);
}
}
uint8_t store_msg_from_isr_to_queue(uint32_t id, uint8_t* data, uint8_t len)
{
can_buff_t buff = {0};
buff.id = id;
buff.length = len;
memcpy(buff.data, data, len);
ENQUEUE(g_canQueue, buff);
return 0;
}
IVEC_EcuCommonErr_e xECU_WriteDataOverCAN(uint8_t* pucBuf, uint32_t ulId, int retCode, uint32_t BufNum)
{
IVEC_EcuCommonErr_e l_xFuncStatus = commonECU_WRITE_FAIL;
uint8_t l_i32Ret;
uint16_t TxData[8] = {0}; // Define a buffer for the CAN payload data
for (int i = 0; i < retCode; i++) {
TxData[i] = (uint16_t)(pucBuf[i] ^ 0x0000);
}
int Bytes = retCode;
l_i32Ret = xMCAL_MCANTx(CANFD0, ulId, TxData, BufNum, Bytes);
if(l_i32Ret == IVEC_MCAL_STATUS_SUCCESS)
{
l_xFuncStatus = commonECU_SUCCESS;
}
return l_xFuncStatus;
}
IVEC_EcuCommonErr_e xECU_CANGetData(can_buff_t *pxBuff)
{
IVEC_EcuCommonErr_e l_xFuncStatus = commonECU_FAIL;
if (!IS_EMPTY(g_canQueue))
{
can_buff_t xBuff = { 0x00 };
DEQUEUE(g_canQueue, xBuff);
memcpy(pxBuff,&xBuff,sizeof(can_buff_t));
l_xFuncStatus = commonECU_SUCCESS;
}
return l_xFuncStatus;
}
IVEC_EcuCommonErr_e xECU_CanReInit(MCAN_Regs* MCAN,uint16_t speed)
{
IVEC_EcuCommonErr_e l_xFuncStatus = commonECU_SUCCESS;
uint8_t l_i32Ret;
l_i32Ret = xMCAL_MCANDeInit(MCAN);
if(l_i32Ret != IVEC_MCAL_STATUS_SUCCESS)
{
l_xFuncStatus = commonECU_DEINIT_FAIL;
}
//DL_MCAN_reset(MCAN);
// DL_GPIO_enablePower(GPIOA);
// DL_GPIO_enablePower(GPIOB);
DL_MCAN_enablePower(MCAN);
delay_cycles(POWER_STARTUP_DELAY);
DL_GPIO_initPeripheralOutputFunction(GPIO_MCAN0_IOMUX_CAN_TX, GPIO_MCAN0_IOMUX_CAN_TX_FUNC);
DL_GPIO_initPeripheralInputFunction(GPIO_MCAN0_IOMUX_CAN_RX, GPIO_MCAN0_IOMUX_CAN_RX_FUNC);
l_i32Ret = xMCAL_MCANInit(MCAN,speed);
if(l_i32Ret != IVEC_MCAL_STATUS_SUCCESS)
{
l_xFuncStatus = commonECU_INIT_FAIL;
}
return l_xFuncStatus;
}
IVEC_EcuCommonErr_e xECU_CANInit(MCAN_Regs* MCAN, xCAN_baud_t BAUD)
{
IVEC_EcuCommonErr_e l_xFuncStatus = commonECU_SUCCESS;
uint8_t l_i32Ret;
l_i32Ret = xMCAL_MCANInit(MCAN,BAUD);
if(l_i32Ret != IVEC_MCAL_STATUS_SUCCESS)
{
l_xFuncStatus = commonECU_INIT_FAIL;
}
return l_xFuncStatus;
}
IVEC_EcuCommonErr_e xECU_CANDeInit(MCAN_Regs* MCAN)
{
IVEC_EcuCommonErr_e l_xFuncStatus = commonECU_SUCCESS;
uint8_t l_i32Ret;
l_i32Ret = xMCAL_MCANDeInit(MCAN);
if(l_i32Ret != IVEC_MCAL_STATUS_SUCCESS)
{
l_xFuncStatus = commonECU_INIT_FAIL;
}
return l_xFuncStatus;
}
IVEC_EcuCommonErr_e xECU_GetCanStatus(MCAN_Regs* MCAN, uint16_t speed)
{
char l_ucErrorString[32] = {0};
if( xMCAL_getMCAN_ErrorStatus(&l_ucErrorString) == IVEC_MCAL_STATUS_ERROR )
{
xECU_CanReInit(MCAN, speed);
}
}