440 lines
14 KiB
C
440 lines
14 KiB
C
/*
|
|
* ivec_rte.c
|
|
*
|
|
* Created on: 28-Oct-2024
|
|
* Author: altam
|
|
*/
|
|
#include "ivec_rte.h"
|
|
|
|
#include "ivec_cmplx_queue.h"
|
|
#include "../ivec_ECU/ivec_ecu_uart/inc/ivec_ecu_uart.h"
|
|
#include "../ivec_ECU/ivec_ecu_can/inc/ivec_ecu_can.h"
|
|
#include "../../Core/Include/ivec_mcal_gpio.h"
|
|
#include "socTouchDisplay.h"
|
|
#include "../../TM1650_SDK/inc/ivec_TM1650.h"
|
|
|
|
|
|
McalUartHandle_s g_xUartHandle;
|
|
uint32_t g_u32UartSpeed = 0;
|
|
xCAN_baud_t g_u16CanSpeed = 0;
|
|
|
|
#define MAX_FILTERS 10
|
|
uint32_t maskValues[MAX_FILTERS];
|
|
uint32_t filterValues[MAX_FILTERS];
|
|
bool isExtendedID[MAX_FILTERS];
|
|
|
|
uint32_t maskCount = 0;
|
|
// Buffers to store parsed data
|
|
uint32_t filterCount = 0;
|
|
uint16_t extendedFilter = 0;
|
|
uint16_t standardFilter = 0;
|
|
|
|
static uint8_t __gprv_u8Idx = 0;
|
|
|
|
extern ExtU_socTouchDisplay_T socTouchDisplay_U;
|
|
extern ExtY_socTouchDisplay_T socTouchDisplay_Y;
|
|
extern volatile bool b_MCAN_InitFlag; /*!< CAN initialization flag */
|
|
|
|
//DL_MCAN_StdMsgIDFilterElement stdFilterElement;
|
|
#define MCAN_FILTER_SIZE 0u
|
|
|
|
// New configuration instance to override filter settings dynamically
|
|
static DL_MCAN_ConfigParams gMCAN0ConfigParamsFiltered = {
|
|
.monEnable = false,
|
|
.asmEnable = false,
|
|
.tsPrescalar = 15,
|
|
.tsSelect = 0,
|
|
.timeoutSelect = DL_MCAN_TIMEOUT_SELECT_CONT,
|
|
.timeoutPreload = 65535,
|
|
.timeoutCntEnable = false,
|
|
// Dynamic filter settings based on need
|
|
.filterConfig.rrfs = false,
|
|
.filterConfig.rrfe = false,
|
|
.filterConfig.anfe = 3, // Accept extended ID filter elements
|
|
.filterConfig.anfs = 3, // Accept standard ID filter elements
|
|
};
|
|
|
|
// Adjust MsgRAMConfigParams as necessary for filter list sizes
|
|
|
|
static DL_MCAN_MsgRAMConfigParams gMCAN0MsgRAMConfigParamsFiltered ={
|
|
|
|
/* Standard ID Filter List Start Address. */
|
|
.flssa = 1,
|
|
/* List Size: Standard ID. */
|
|
.lss = MCAN_FILTER_SIZE,
|
|
/* Extended ID Filter List Start Address. */
|
|
.flesa = 48 ,
|
|
/* List Size: Extended ID. */
|
|
.lse = 0,
|
|
/* Tx Buffers Start Address. */
|
|
.txStartAddr = 10 ,
|
|
/* Number of Dedicated Transmit Buffers. */
|
|
.txBufNum = 10 ,
|
|
.txFIFOSize = 10,
|
|
/* Tx Buffer Element Size. */
|
|
.txBufMode = 0,
|
|
.txBufElemSize = DL_MCAN_ELEM_SIZE_8BYTES,
|
|
/* Tx Event FIFO Start Address. */
|
|
.txEventFIFOStartAddr = 640 ,
|
|
/* Event FIFO Size. */
|
|
.txEventFIFOSize = 10 ,
|
|
/* Level for Tx Event FIFO watermark interrupt. */
|
|
.txEventFIFOWaterMark = 0,
|
|
/* Rx FIFO0 Start Address. */
|
|
.rxFIFO0startAddr = 170 ,
|
|
/* Number of Rx FIFO elements. */
|
|
.rxFIFO0size = 10 ,
|
|
/* Rx FIFO0 Watermark. */
|
|
.rxFIFO0waterMark = 0,
|
|
.rxFIFO0OpMode = 0,
|
|
/* Rx FIFO1 Start Address. */
|
|
.rxFIFO1startAddr = 190 ,
|
|
/* Number of Rx FIFO elements. */
|
|
.rxFIFO1size = 10 ,
|
|
/* Level for Rx FIFO 1 watermark interrupt. */
|
|
.rxFIFO1waterMark = 10,
|
|
/* FIFO blocking mode. */
|
|
.rxFIFO1OpMode = 0,
|
|
/* Rx Buffer Start Address. */
|
|
.rxBufStartAddr = 208 ,
|
|
/* Rx Buffer Element Size. */
|
|
.rxBufElemSize = DL_MCAN_ELEM_SIZE_8BYTES,
|
|
/* Rx FIFO0 Element Size. */
|
|
.rxFIFO0ElemSize = DL_MCAN_ELEM_SIZE_8BYTES,
|
|
/* Rx FIFO1 Element Size. */
|
|
.rxFIFO1ElemSize = DL_MCAN_ELEM_SIZE_8BYTES,
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
* @brief Function to use SDA pin of TM1650
|
|
*
|
|
* @param state the value to write on SDA pin (0 or 1)
|
|
* @returns none
|
|
*/
|
|
void mcu_tempDataPin(uint8_t state)
|
|
{
|
|
if(state == 0){
|
|
vMCAL_gpioWrite(TM1650_PORT, TM1650_SDA_PIN_PIN, 0);
|
|
}
|
|
else if(state == 1){
|
|
vMCAL_gpioWrite(TM1650_PORT, TM1650_SDA_PIN_PIN, 1);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Function to use SCL pin of TM1650
|
|
*
|
|
* @param state the value to write on SDA pin (0 or 1)
|
|
* @returns none
|
|
*/
|
|
void mcu_tempClkPin(uint8_t state){
|
|
if(state == 0){
|
|
vMCAL_gpioWrite(TM1650_PORT, TM1650_SCL_PIN_PIN, 0);
|
|
}
|
|
else if(state == 1){
|
|
vMCAL_gpioWrite(TM1650_PORT, TM1650_SCL_PIN_PIN, 1);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @brief Function to read SDA pin of TM1650
|
|
*
|
|
* @param none
|
|
* @returns Value of GPIO (0 or 1)
|
|
*/
|
|
uint8_t mcu_tempDataReadPin(void){
|
|
uint8_t readBuff = 0;
|
|
vMCAL_set_gpioDirection(TM1650_SDA_PIN_IOMUX,false);
|
|
readBuff = u32MCAL_gpioRead(TM1650_PORT, TM1650_SDA_PIN_PIN);
|
|
vMCAL_set_gpioDirection(TM1650_SDA_PIN_IOMUX,true);
|
|
return readBuff;
|
|
}
|
|
|
|
|
|
void vRTE_MatlabInit(void)
|
|
{
|
|
u8MCAL_gpioInit();
|
|
tm1650_Init(TM_1650_BRIGHT_8, TM_1650_Segment_8, TM_1650_Normal_Mode, TM_1650_Screen_ON, TM_1650_DIG_3, (void*)&mcu_tempDataPin , (void*)&mcu_tempClkPin , &mcu_tempDataReadPin);
|
|
tm1650_displaySwitch(TM_1650_Screen_OFF);
|
|
vMCAL_DelayTicks(500);
|
|
tm1650_showDot(TM_1650_DIG_1,false);
|
|
tm1650_showDot(TM_1650_DIG_2,false);
|
|
tm1650_showDot(TM_1650_DIG_3,false);
|
|
socTouchDisplay_initialize();
|
|
}
|
|
|
|
void vRTE_MatlabRun(void)
|
|
{
|
|
socTouchDisplay_U.in_bTouchDetected = (u32MCAL_gpioRead(GPIOB, DL_GPIO_PIN_17) == DL_GPIO_PIN_17) ? 1 : 0;
|
|
socTouchDisplay_U.ip_u32DisplayDuration_msec = 2000;
|
|
socTouchDisplay_U.ip_u32TouchDuration_msec = 100;
|
|
|
|
socTouchDisplay_step();
|
|
|
|
memset(&socTouchDisplay_U.Input[0], 0, sizeof(CAN_MESSAGE_BUS)*MAX_CAN_MESSAGE_INSTANCE);
|
|
__gprv_u8Idx = 0;
|
|
|
|
if( socTouchDisplay_Y.op_bDisplayStatus )
|
|
{
|
|
tm1650_displaySwitch(TM_1650_Screen_ON);
|
|
tm1650_showDot(TM_1650_DIG_1,false);
|
|
tm1650_showDot(TM_1650_DIG_2,false);
|
|
tm1650_showDot(TM_1650_DIG_3,false);
|
|
|
|
tm1650_showNum(TM_1650_DIG_3, socTouchDisplay_Y.op_u8OnesPlace);
|
|
tm1650_showNum(TM_1650_DIG_2, socTouchDisplay_Y.op_u8TensPlace);
|
|
tm1650_showNum(TM_1650_DIG_1, socTouchDisplay_Y.op_u8HundredsPlace);
|
|
}
|
|
if( socTouchDisplay_Y.op_bErrorStatus )
|
|
{
|
|
tm1650_displaySwitch(TM_1650_Screen_ON);
|
|
tm1650_showDot(TM_1650_DIG_1,false);
|
|
tm1650_showDot(TM_1650_DIG_2,false);
|
|
tm1650_showDot(TM_1650_DIG_3,false);
|
|
|
|
char l_cData = 'R';
|
|
l_cData = 'C';
|
|
tm1650_showAlphabet(TM_1650_DIG_2, (char*)&l_cData);
|
|
l_cData = 'E';
|
|
tm1650_showAlphabet(TM_1650_DIG_3, (char*)&l_cData);
|
|
}
|
|
if( !socTouchDisplay_Y.op_bDisplayStatus && !socTouchDisplay_Y.op_bErrorStatus )
|
|
{
|
|
tm1650_displaySwitch(TM_1650_Screen_OFF);
|
|
}
|
|
|
|
}
|
|
|
|
void vRTE_InitUARTCANEcho(void)
|
|
{
|
|
g_u32UartSpeed = mcalUART_BAUD_115200;
|
|
g_u16CanSpeed = BAUD_500;
|
|
xECU_UARTInit(&g_xUartHandle, g_u32UartSpeed);
|
|
xECU_CANInit(CANFD0,g_u16CanSpeed);
|
|
}
|
|
|
|
void vCanFilterMaskSaveVal(uint8_t ucIdx, uint32_t mask, bool isExtended)
|
|
{
|
|
maskCount = ucIdx;
|
|
maskValues[maskCount] = mask;
|
|
// isExtendedID[maskCount++] = isExtended;
|
|
// if(isExtended)
|
|
// {
|
|
// extendedFilter++;
|
|
// }
|
|
// else
|
|
// {
|
|
// standardFilter++;
|
|
// }
|
|
}
|
|
|
|
|
|
void vCanFilterSaveVal(uint8_t ucIdx, uint32_t Filter, bool isExtended)
|
|
{
|
|
filterCount = ucIdx;
|
|
// Store filter value
|
|
filterValues[filterCount] = Filter;
|
|
// isExtendedID[filterCount] = isExtended;
|
|
// if(isExtended)
|
|
// {
|
|
// extendedFilter++;
|
|
// }
|
|
// else
|
|
// {
|
|
// standardFilter++;
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
void vCanFilterReset() {
|
|
uint32_t i;
|
|
|
|
|
|
DL_MCAN_setOpMode(CANFD0, DL_MCAN_OPERATION_MODE_SW_INIT);
|
|
|
|
// Reset all standard ID filters
|
|
for (i = 0; i < MAX_FILTERS; i++) {
|
|
DL_MCAN_StdMsgIDFilterElement stdFilterElement = {0};
|
|
stdFilterElement.sfec = 001; // Disable filter element
|
|
stdFilterElement.sft = 11; // Disable filter type
|
|
DL_MCAN_addStdMsgIDFilter(CANFD0, i, &stdFilterElement);
|
|
}
|
|
|
|
// Reset all extended ID filters
|
|
for (i = 0; i < MAX_FILTERS; i++) {
|
|
DL_MCAN_ExtMsgIDFilterElement extFilterElement = {0};
|
|
extFilterElement.efec = 000; // Disable filter element
|
|
extFilterElement.eft = 11; // Disable filter type
|
|
DL_MCAN_addExtMsgIDFilter(CANFD0, i, &extFilterElement);
|
|
}
|
|
|
|
/* Set Extended ID Mask. */
|
|
DL_MCAN_setExtIDAndMask(CANFD0, (0x1FFFFFFFU));
|
|
/* Take MCAN out of the SW initialization mode */
|
|
DL_MCAN_setOpMode(CANFD0, DL_MCAN_OPERATION_MODE_NORMAL);
|
|
|
|
|
|
}
|
|
|
|
// Function to configure CAN filters
|
|
void vCanConfigFilter() {
|
|
|
|
xECU_CanReInit(CANFD0,g_u16CanSpeed);
|
|
|
|
DL_MCAN_setOpMode(CANFD0, DL_MCAN_OPERATION_MODE_SW_INIT);
|
|
|
|
for (int i = 0; i <= maskCount; i++)
|
|
{
|
|
if((filterValues[i] > 0x7FF) || (maskValues[i] > 0x7FF))
|
|
{
|
|
isExtendedID[i] = 1;
|
|
extendedFilter++;
|
|
}
|
|
else
|
|
{
|
|
isExtendedID[i] = 0;
|
|
standardFilter++;
|
|
}
|
|
}
|
|
|
|
gMCAN0MsgRAMConfigParamsFiltered.lse = extendedFilter; //0; // Set dynamically based on actual filters
|
|
gMCAN0MsgRAMConfigParamsFiltered.lss = standardFilter; //1; // Set dynamically based on actual filters
|
|
DL_MCAN_config(CANFD0, (DL_MCAN_ConfigParams*) &gMCAN0ConfigParamsFiltered);
|
|
DL_MCAN_msgRAMConfig(CANFD0, (DL_MCAN_MsgRAMConfigParams*) &gMCAN0MsgRAMConfigParamsFiltered);
|
|
|
|
uint8_t extendedFilterNumber = 0;
|
|
uint8_t stadardFilterNumber = 0;
|
|
|
|
for (int i = 0; i <= maskCount; i++) {
|
|
if (isExtendedID[i]) {
|
|
// Extended ID filter
|
|
DL_MCAN_ExtMsgIDFilterElement extFilterElement;
|
|
extFilterElement.efid1 = filterValues[i];
|
|
extFilterElement.efid2 = maskValues[i];
|
|
extFilterElement.efec = 001;
|
|
extFilterElement.eft = 10;
|
|
DL_MCAN_addExtMsgIDFilter(CANFD0, extendedFilterNumber, (DL_MCAN_StdMsgIDFilterElement *) &extFilterElement);
|
|
extendedFilterNumber++;
|
|
|
|
}
|
|
else {
|
|
// Standard ID filter
|
|
DL_MCAN_StdMsgIDFilterElement stdFilterElement;
|
|
stdFilterElement.sfid1 = filterValues[i];
|
|
stdFilterElement.sfid2 = maskValues[i];
|
|
stdFilterElement.sfec = 001;
|
|
stdFilterElement.sft = 10;
|
|
DL_MCAN_addStdMsgIDFilter(CANFD0, stadardFilterNumber,(DL_MCAN_StdMsgIDFilterElement *) &stdFilterElement);
|
|
stadardFilterNumber++;
|
|
|
|
}
|
|
}
|
|
|
|
/* Set Extended ID Mask. */
|
|
DL_MCAN_setExtIDAndMask(CANFD0, (0x1FFFFFFFU));
|
|
/* Take MCAN out of the SW initialization mode */
|
|
DL_MCAN_setOpMode(CANFD0, DL_MCAN_OPERATION_MODE_NORMAL);
|
|
|
|
|
|
// Reset counters after applying filters
|
|
maskCount = 1;
|
|
filterCount = 0;
|
|
extendedFilterNumber = 0;
|
|
stadardFilterNumber = 0;
|
|
extendedFilter = 0;
|
|
standardFilter = 0;
|
|
|
|
}
|
|
|
|
void vRTE_UARTDataProcess(void)
|
|
{
|
|
PacketRetCode_t retCode = PACKET_FAIL;
|
|
uint8_t pucBuf[MAX_PACKET_LENGTH] = {0};
|
|
uint32_t ulId = 0xffffffff;
|
|
|
|
retCode= xECU_ReadCANDataOverUART(&g_xUartHandle,pucBuf,&ulId);
|
|
if(retCode > -1)
|
|
{
|
|
if(retCode > 0 && ulId == 0x00)
|
|
{
|
|
uint32_t baudrate = 0;
|
|
uint8_t mode = pucBuf[PKT_HEADER];
|
|
memcpy(&baudrate, &pucBuf[PKT_HEADER+1], (uint32_t)retCode);
|
|
vECU_InitiateUartToCanTransmit(&g_xUartHandle, 0x01, pucBuf, 0);
|
|
if( mode == 0 )
|
|
{
|
|
g_u32UartSpeed = baudrate;
|
|
xECU_UARTReInit(&g_xUartHandle, g_u32UartSpeed);
|
|
}
|
|
else if( mode == 1 )
|
|
{
|
|
g_u16CanSpeed = (uint16_t)baudrate;
|
|
xECU_CanReInit(CANFD0, g_u16CanSpeed);
|
|
}
|
|
else if( mode == 2 )
|
|
{
|
|
|
|
if( pucBuf[PKT_HEADER+1] != 0 ){//22(20+2) rx filter available send each id in a frame
|
|
uint32_t filterId = 0;
|
|
memcpy(&filterId, &pucBuf[PKT_HEADER+3], sizeof(uint32_t));
|
|
bool isExtended = 0;
|
|
isExtended = (filterId > 0x7FF); // Standard IDs are <= 0x7FF
|
|
vCanFilterSaveVal((pucBuf[PKT_HEADER+1] - 1), filterId, isExtended);
|
|
if( pucBuf[PKT_HEADER+2] )//All filter received. Trigger Filter Settings
|
|
{
|
|
vCanFilterReset();
|
|
vCanConfigFilter();
|
|
}
|
|
else
|
|
return;
|
|
}
|
|
else{
|
|
vCanFilterReset();
|
|
vCanConfigFilter();
|
|
}
|
|
}
|
|
else if ( mode == 3 )
|
|
{
|
|
if( (pucBuf[PKT_HEADER+1] < 1) || (pucBuf[PKT_HEADER+1] > 8) )//0-7 maximum received pkts
|
|
return;
|
|
uint32_t maskId = 0;
|
|
memcpy(&maskId, &pucBuf[PKT_HEADER+3], sizeof(uint32_t));
|
|
bool isExtended = 0;
|
|
isExtended = (maskId > 0x7FF); // Standard IDs are <= 0x7FF
|
|
vCanFilterMaskSaveVal((pucBuf[PKT_HEADER+1] - 1), maskId, isExtended);
|
|
}
|
|
vMCAL_DelayTicks(100);
|
|
vECU_InitiateUartToCanTransmit(&g_xUartHandle, 0x01, pucBuf, 0);
|
|
}
|
|
if ( retCode == 0 && ulId == 0){
|
|
vECU_InitiateUartToCanTransmit(&g_xUartHandle, 0x0, pucBuf, 0);
|
|
}
|
|
if ( retCode >= 0 && ulId > 0x00 && ulId < 0xffffffff )
|
|
{
|
|
xECU_WriteDataOverCAN(pucBuf, ulId, retCode, 0);
|
|
}
|
|
}
|
|
|
|
}
|
|
void vRTE_CANDataProcess(void)
|
|
{
|
|
IVEC_EcuCommonErr_e retCode = commonECU_FAIL;
|
|
can_buff_t xBuff = { 0x00 };
|
|
|
|
|
|
retCode = xECU_CANGetData(&xBuff);
|
|
if(retCode == commonECU_SUCCESS)
|
|
{
|
|
vECU_InitiateUartToCanTransmit(&g_xUartHandle, (uint32_t)xBuff.id, (uint8_t*)&xBuff.data[0], (uint8_t)xBuff.length);
|
|
socTouchDisplay_U.Input[__gprv_u8Idx].ID = xBuff.id;
|
|
socTouchDisplay_U.Input[__gprv_u8Idx].Length = xBuff.length;
|
|
memcpy(&socTouchDisplay_U.Input[__gprv_u8Idx].Data[0], &xBuff.data[0], 8);
|
|
__gprv_u8Idx = (__gprv_u8Idx + 1) % MAX_CAN_MESSAGE_INSTANCE;
|
|
}
|
|
}
|