feat: Parse UART data from CC and forward over CAN

- Implemented parsing of incoming UART data from CC and transmitting it over CAN.
- Added functionality to send received CAN data back to CC via UART.
stable
Rakshita 2025-03-10 18:22:39 +05:30
parent 4704529a3b
commit dfdb8d0c64
10 changed files with 322 additions and 4 deletions

View File

@ -400,6 +400,7 @@
</option>
<option IS_BUILTIN_EMPTY="false" IS_VALUE_EMPTY="false" id="com.ti.ccstudio.buildDefinitions.TMS470_GNU_9.0.compilerID.INCLUDE_PATH.1406977247" name="Include paths (-I)" superClass="com.ti.ccstudio.buildDefinitions.TMS470_GNU_9.0.compilerID.INCLUDE_PATH" valueType="includePath">
<listOptionValue builtIn="false" value="${COM_TI_MSPM0_SDK_INCLUDE_PATH}"/>
<listOptionValue builtIn="false" value="${workspace_loc:/${ProjName}/ivec_cmplx_vframe/inc}"/>
<listOptionValue builtIn="false" value="${SYSCONFIG_TOOL_INCLUDE_PATH}"/>
<listOptionValue builtIn="false" value="${workspace_loc:/${ProjName}/ivec_ECU/ivec_ecu_nfc/inc}"/>
<listOptionValue builtIn="false" value="${workspace_loc:/${ProjName}/Generated Codes}"/>

View File

@ -38,12 +38,12 @@
<link>
<name>lib</name>
<type>2</type>
<location>C:/ti/mspm0_sdk_2_02_00_05/source/ti/driverlib/lib</location>
<location>C:/ti/mspm0_sdk_2_03_00_07/source/ti/driverlib/lib</location>
</link>
<link>
<name>m0p</name>
<type>2</type>
<location>C:/ti/mspm0_sdk_2_02_00_05/source/ti/driverlib/m0p</location>
<location>C:/ti/mspm0_sdk_2_03_00_07/source/ti/driverlib/m0p</location>
</link>
</linkedResources>
<variableList>

View File

@ -201,12 +201,16 @@ encoding//Test__GNU/ivec_ESDK/ivec_esdk_nfc_pn532/src/subdir_rules.mk=UTF-8
encoding//Test__GNU/ivec_ESDK/ivec_esdk_nfc_pn532/src/subdir_vars.mk=UTF-8
encoding//Test__GNU/ivec_RTE/src/subdir_rules.mk=UTF-8
encoding//Test__GNU/ivec_RTE/src/subdir_vars.mk=UTF-8
encoding//Test__GNU/ivec_cmplx_vframe/src/subdir_rules.mk=UTF-8
encoding//Test__GNU/ivec_cmplx_vframe/src/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/gcc/m0p/mspm0c110x/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/gcc/m0p/mspm0c110x/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/gcc/m0p/mspm0g1x0x_g3x0x/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/gcc/m0p/mspm0g1x0x_g3x0x/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/gcc/m0p/mspm0gx51x/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/gcc/m0p/mspm0gx51x/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/gcc/m0p/mspm0l111x/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/gcc/m0p/mspm0l111x/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/gcc/m0p/mspm0l11xx_l13xx/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/gcc/m0p/mspm0l11xx_l13xx/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/gcc/m0p/mspm0l122x_l222x/subdir_rules.mk=UTF-8
@ -217,6 +221,8 @@ encoding//Test__GNU/lib/iar/m0p/mspm0g1x0x_g3x0x/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/iar/m0p/mspm0g1x0x_g3x0x/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/iar/m0p/mspm0gx51x/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/iar/m0p/mspm0gx51x/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/iar/m0p/mspm0l111x/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/iar/m0p/mspm0l111x/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/iar/m0p/mspm0l11xx_l13xx/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/iar/m0p/mspm0l11xx_l13xx/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/iar/m0p/mspm0l122x_l222x/subdir_rules.mk=UTF-8
@ -227,6 +233,8 @@ encoding//Test__GNU/lib/keil/m0p/mspm0g1x0x_g3x0x/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/keil/m0p/mspm0g1x0x_g3x0x/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/keil/m0p/mspm0gx51x/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/keil/m0p/mspm0gx51x/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/keil/m0p/mspm0l111x/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/keil/m0p/mspm0l111x/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/keil/m0p/mspm0l11xx_l13xx/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/keil/m0p/mspm0l11xx_l13xx/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/keil/m0p/mspm0l122x_l222x/subdir_rules.mk=UTF-8
@ -237,6 +245,8 @@ encoding//Test__GNU/lib/ticlang/m0p/mspm0g1x0x_g3x0x/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/ticlang/m0p/mspm0g1x0x_g3x0x/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/ticlang/m0p/mspm0gx51x/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/ticlang/m0p/mspm0gx51x/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/ticlang/m0p/mspm0l111x/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/ticlang/m0p/mspm0l111x/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/ticlang/m0p/mspm0l11xx_l13xx/subdir_rules.mk=UTF-8
encoding//Test__GNU/lib/ticlang/m0p/mspm0l11xx_l13xx/subdir_vars.mk=UTF-8
encoding//Test__GNU/lib/ticlang/m0p/mspm0l122x_l222x/subdir_rules.mk=UTF-8

View File

@ -55,6 +55,7 @@ typedef enum{
IVEC_MCAL_UART_PORT_1=0,
IVEC_MCAL_UART_PORT_2,
IVEC_MCAL_UART_PORT_3,
IVEC_MCAL_UART_PORT_4,
IVEC_MCAL_UART_PORT_MAX
}IVEC_McalUartPort_e;

View File

@ -139,6 +139,11 @@ void UART2_IRQHandler()
prv_vRxCallback(UART2, DL_UART_Main_getPendingInterrupt(UART2));
}
void UART3_IRQHandler()
{
prv_vRxCallback(UART3, DL_UART_Main_getPendingInterrupt(UART3));
}
/**
* @brief Retrieves the UART instance for the specified port number.
*
@ -158,6 +163,8 @@ static UART_Regs* __prv_McalGetUartInstance(IVEC_McalUartPort_e eUartPortNumber)
return UART1;
case IVEC_MCAL_UART_PORT_3:
return UART2;
case IVEC_MCAL_UART_PORT_4:
return UART3;
default:
return NULL; // Invalid UART port
}
@ -183,6 +190,8 @@ static IVEC_McalUartPort_e __prv_McalGetUartPort(UART_Regs* pxUartInstance)
return IVEC_MCAL_UART_PORT_2;
case (uint32_t)UART2:
return IVEC_MCAL_UART_PORT_3;
case (uint32_t)UART3:
return IVEC_MCAL_UART_PORT_4;
default:
return IVEC_MCAL_UART_PORT_MAX; // Invalid UART port
}
@ -242,7 +251,12 @@ static IVEC_McalCommonErr_e _prvMCAL_UartDeInitInstance(IVEC_McalUartHandle_s* p
NVIC_ClearPendingIRQ(UART2_INT_IRQn);
// Reset the UART2 init flag if needed (b_UART2_init_flag = 0)
}
else if (pxUartInstance == UART3)
{
NVIC_DisableIRQ(UART3_INT_IRQn);
NVIC_ClearPendingIRQ(UART3_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);
@ -526,6 +540,13 @@ static IVEC_McalCommonErr_e __prv_McalUartInitInstance(IVEC_McalUartHandle_s* px
{
DL_UART_Main_reset(UART2);
DL_UART_Main_enablePower(UART2);
DL_GPIO_initPeripheralOutputFunction(IOMUX_PINCM36, IOMUX_PINCM36_PF_UART3_TX);
DL_GPIO_initPeripheralInputFunction(IOMUX_PINCM35, IOMUX_PINCM35_PF_UART3_RX);
}
else if (pxUartHandle->eUartPortNumber == IVEC_MCAL_UART_PORT_4)
{
DL_UART_Main_reset(UART3);
DL_UART_Main_enablePower(UART3);
DL_GPIO_initPeripheralOutputFunction(IOMUX_PINCM32, IOMUX_PINCM32_PF_UART2_TX);
DL_GPIO_initPeripheralInputFunction(IOMUX_PINCM33, IOMUX_PINCM33_PF_UART2_RX);
}
@ -705,6 +726,26 @@ static IVEC_McalCommonErr_e __prv_McalUartInitInstance(IVEC_McalUartHandle_s* px
NVIC_ClearPendingIRQ(UART2_INT_IRQn);
NVIC_EnableIRQ(UART2_INT_IRQn);
}
else if (pxUartInstance == UART3)
{
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(UART3);
DL_UART_Main_setRXFIFOThreshold(UART3, DL_UART_RX_FIFO_LEVEL_ONE_ENTRY);
DL_UART_Main_setTXFIFOThreshold(UART3, DL_UART_TX_FIFO_LEVEL_1_2_EMPTY);
DL_UART_Main_enable(pxUartInstance);
NVIC_ClearPendingIRQ(UART3_INT_IRQn);
NVIC_EnableIRQ(UART3_INT_IRQn);
}
return commonMCAL_SUCCESS;
}

View File

@ -55,6 +55,7 @@ typedef enum
IVEC_ECU_UART_PORT1 = 0,
IVEC_ECU_UART_PORT2,
IVEC_ECU_UART_PORT3,
IVEC_ECU_UART_PORT4,
IVEC_ECU_UART_PORT_MAX
} IVEC_ECU_UartPortNumber_e;

View File

@ -34,6 +34,10 @@ static void __prv_EcuCanOverUartMsgCallback(IVEC_McalUartPort_e eUartPort, IVEC_
if (eEventType == IVEC_ECU_UART_EVENT_RX_ARRIVED) {
u8CMPLX_FifoEnqueue((CmplxFifoQueueHandle_s*)&__gprv_xUartResponseQueue[IVEC_ECU_UART_PORT3], pu8Buffer, u32Size);
}
case IVEC_MCAL_UART_PORT_4:
if (eEventType == IVEC_ECU_UART_EVENT_RX_ARRIVED) {
u8CMPLX_FifoEnqueue((CmplxFifoQueueHandle_s*)&__gprv_xUartResponseQueue[IVEC_ECU_UART_PORT4], pu8Buffer, u32Size);
}
break;
default:
break;
@ -116,6 +120,10 @@ IVEC_EcuCommonErr_e xECU_UartInit(IVEC_EcuUartHandle_s* pxUartHandle)
__gprv_xUartResponseQueue[pxUartHandle->eUartPortNumber].i32TotalElements = pxUartHandle->u16QbufSize;
__gprv_xUartResponseQueue[pxUartHandle->eUartPortNumber].pu8Buffer = pxUartHandle->u8Qbuffer;
break;
case IVEC_ECU_UART_PORT4:
__gprv_xUartResponseQueue[pxUartHandle->eUartPortNumber].i32TotalElements = pxUartHandle->u16QbufSize;
__gprv_xUartResponseQueue[pxUartHandle->eUartPortNumber].pu8Buffer = pxUartHandle->u8Qbuffer;
break;
default:
__gprv_xUartResponseQueue[pxUartHandle->eUartPortNumber].pu8Buffer = NULL;
break;
@ -555,3 +563,9 @@ int32_t iECU_UartInitiateTransmit(IVEC_EcuUartHandle_s* pxUartHandle, uint32_t u
memcpy(&l_u8Buf[ecuUART_PKT_HEADER_u8], pu8Data, u8Len);
return (xECU_UartFormatPacket(pxUartHandle, l_u8Buf, u8Len, u32Id) == ecuUART_PACKET_SUCCESS_u8) ? 0 : -1;
}
uint32_t xECU_data_length(IVEC_EcuUartHandle_s* pxUartHandle)
{
uint32_t length = i32CMPLX_FifoCounts((CmplxFifoQueueHandle_s*)&__gprv_xUartResponseQueue[pxUartHandle->eUartPortNumber]);
return(length);
}

View File

@ -7,6 +7,7 @@
#include "ivec_rte.h"
#include "ivec_cmplx_queue.h"
#include "ivec_cmplx_vFrame.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"
@ -18,6 +19,7 @@
// UART Handles
IVEC_EcuUartHandle_s g_xUartHandle = {0};
IVEC_EcuUartHandle_s __gprv_UartCcHandle = {0};
IVEC_EcuCANHandle_s g_xCanHandle = {0};
// Configuration Macros
@ -26,7 +28,7 @@ IVEC_EcuCANHandle_s g_xCanHandle = {0};
#define rteCONFIG_MOTHER_BOARD_u8 3
// UART Configuration
#define rteUART_PIN_SELECTION_u8 rteCONFIG_BASIL_u8
#define rteUART_PIN_SELECTION_u8 rteCONFIG_BASIL_BATTERY_SMART_u8
uint8_t g_pu8UartBuffer[ecuUART_MAX_PACKET_LENGTH_u8] = {0};
volatile uint32_t g_u32CanId = 0x1FFFFFFF;
@ -50,6 +52,14 @@ static uint8_t __gprv_U8Index = 0;
extern ExtU_socTouchDisplay_T socTouchDisplay_U;
extern ExtY_socTouchDisplay_T socTouchDisplay_Y;
#define CAN_UART_BUFFER_MAX_SIZE 300
uint8_t g_prv_u8CANUartDataBuffer[CAN_UART_BUFFER_MAX_SIZE];
uint32_t g_prv_u32CanUartDataAvailable = 0;
// CAN UART Buffer
#define eteCAN_UART_Cc_BUFFER_MAX_SIZE_u32 4096
volatile uint8_t __gprv_u8CcUartDataBuffer[eteCAN_UART_Cc_BUFFER_MAX_SIZE_u32];
/**
* @brief Sets the state of the MCU temperature data pin (SDA) for TM1650.
@ -135,6 +145,17 @@ void vRTE_MatlabInit(void)
tm1650_showDot(TM_1650_DIG_3,false);
socTouchDisplay_initialize();
}
void vRTE_InitUartCcEcho(void)
{
__gprv_UartCcHandle.u8Qbuffer = __gprv_u8CcUartDataBuffer;
__gprv_UartCcHandle.u16QbufSize = eteCAN_UART_Cc_BUFFER_MAX_SIZE_u32;
__gprv_UartCcHandle.eUartPortNumber = IVEC_ECU_UART_PORT4;
__gprv_UartCcHandle.u32BaudRate = IVEC_ECU_UART_BAUD_1000000;
xECU_UartInit(&__gprv_UartCcHandle);
}
/**
* @brief Runs the MATLAB interface for MCU and TM1650 during operation.
*
@ -219,6 +240,7 @@ void vRTE_AppInit(void)
{
#if rteUART_PIN_SELECTION_u8 == 1
vRTE_MatlabInit();
vRTE_InitUartCcEcho();
#endif
vRTE_InitUartCanEcho();
@ -386,6 +408,11 @@ void vRTE_AppRun(void)
{
vRTE_ProcessUartData();
vRTE_ProcessCanData();
#if rteUART_PIN_SELECTION_u8 == 1
vRTE_CcUartRxProcess();
#endif
#if rteUART_PIN_SELECTION_u8 == 3
vRTE_UartNfcProcess();
#endif
@ -446,6 +473,90 @@ void vRTE_CanFilterSaveVal(uint8_t u8Idx, uint32_t u32Filter, bool bl_bIsExtende
// Store filter value
g_xCanHandle.u32FilterValues[g_xCanHandle.i32FilterCount] = u32Filter;
}
void vCcUartRxToCanTx(IVEC_EcuCommonCanFrame_s* pxCanMsg)
{
if(pxCanMsg->u32CanId != 0x00)
{
xECU_WriteDataOverCAN(&g_xCanHandle, &pxCanMsg->pucCanData[0], pxCanMsg->u32CanId, pxCanMsg->ucCanDlc, 0);
}
}
void vRTE_CcUartRxProcess(void)
{
static int __prv_i32BfrIdx = 0;
g_prv_u32CanUartDataAvailable = xECU_data_length(&__gprv_UartCcHandle);
int l_u32AvailableData = g_prv_u32CanUartDataAvailable > (CAN_UART_BUFFER_MAX_SIZE - __prv_i32BfrIdx) ? (CAN_UART_BUFFER_MAX_SIZE - __prv_i32BfrIdx) : g_prv_u32CanUartDataAvailable;
if ((l_u32AvailableData <= 10 && g_prv_u32CanUartDataAvailable > 10)|| (__prv_i32BfrIdx+l_u32AvailableData)>=CAN_UART_BUFFER_MAX_SIZE)
{
// printf("Data Overflow detected:%d-%d\n", g_prv_u32CanUartDataAvailable, __prv_i32BfrIdx);
__prv_i32BfrIdx = 0;
l_u32AvailableData = 0;
}
if (1)
{
//record uart recv
uint8_t l_u8Goon=0;
if (l_u32AvailableData && xECU_UartGetData(&__gprv_UartCcHandle, &g_prv_u8CANUartDataBuffer[__prv_i32BfrIdx], l_u32AvailableData, 3000) == commonECU_SUCCESS)
l_u8Goon=1;
if(l_u32AvailableData<=0 && __prv_i32BfrIdx>0)
l_u8Goon=1;
if(l_u8Goon)
{
// g_prv_u32CanUartDataAvailable -= l_u32AvailableData;
__prv_i32BfrIdx += l_u32AvailableData;
uint8_t* l_ucStart = g_prv_u8CANUartDataBuffer;
uint8_t* l_ucFrameStart = NULL;
uint8_t* l_ucFrameEnd = NULL;
IVEC_ECU_LOG(LOG_STRING, "New Data:%d-%d", l_u32AvailableData, __prv_i32BfrIdx);
while (1)
{
// IVEC_ECU_LOG(LOG_STRING, "Current Idx:%d", __prv_i32BfrIdx);
int l_i32Len = __prv_i32BfrIdx;
if (u16CMPLX_vFrameFind(l_ucStart, l_i32Len, &l_ucFrameStart, &l_ucFrameEnd) == 2)//cksum error
{
// xCANhandle->xMyMetrics.u32CANCommFrameErrors++;
}
if (l_ucFrameStart == NULL)
{
// __prv_i32BfrIdx = 0;
break;
}
if (l_ucFrameEnd == NULL)
{
if (memmove(g_prv_u8CANUartDataBuffer, l_ucFrameStart, __prv_i32BfrIdx) != g_prv_u8CANUartDataBuffer)
{
__prv_i32BfrIdx = 0;
IVEC_ECU_LOG(LOG_STRING, "Never Print");
}
__prv_i32BfrIdx-=(l_ucFrameStart-g_prv_u8CANUartDataBuffer);
if(__prv_i32BfrIdx<0)
__prv_i32BfrIdx=0;
break;
}
IVEC_EcuCommonCanFrame_s l_xCanFrame = { 0 };
uint8_t l_u8UartFrameLen = l_ucFrameEnd - l_ucFrameStart + 1;
int l_CanDlc=0;
u16CMPLX_vFrameDecode(l_ucFrameStart, l_u8UartFrameLen, &l_xCanFrame.pucCanData,8,&l_CanDlc,&l_xCanFrame.u32CanId);
l_xCanFrame.ucCanDlc = l_CanDlc;
vCcUartRxToCanTx(&l_xCanFrame);
memset(l_ucFrameStart, 0, l_u8UartFrameLen);
l_ucStart = ++l_ucFrameEnd;
__prv_i32BfrIdx -= l_u8UartFrameLen;
if (__prv_i32BfrIdx < 0)
__prv_i32BfrIdx = 0;
}
// printf( "Out Current Idx:%d\n", __prv_i32BfrIdx);
}
}
}
/**
* @brief Processes UART data to manage CAN and UART communication.
*
@ -560,7 +671,21 @@ void vRTE_ProcessCanData(void)
{
vMCAL_softReset();
}
#if rteUART_PIN_SELECTION_u8 == 1
int l_i32RetSize = 0;
uint8_t l_u8UartBuffer[30] = { 0 };
int l_i32Status = -1;
l_i32RetSize = u16CMPLX_vFrameEncode((uint32_t)l_xCanBuff.u32UlId, (uint8_t*)&l_xCanBuff.u8Data[0], (int32_t)l_xCanBuff.u8Length, l_u8UartBuffer, 30);
l_i32Status = IVEC_ECUUartWrite(&__gprv_UartCcHandle, l_u8UartBuffer, l_i32RetSize);
// if(l_i32Status != 0)
// {
// xECU_WriteDataOverCAN(&g_xCanHandle, NULL , 0x10, 1, 0);
// }
#endif
iECU_UartInitiateTransmit(&g_xUartHandle, (uint32_t)l_xCanBuff.u32UlId, (uint8_t*)&l_xCanBuff.u8Data[0], (uint8_t)l_xCanBuff.u8Length);
socTouchDisplay_U.Input[__gprv_U8Index].ID = l_xCanBuff.u32UlId;
socTouchDisplay_U.Input[__gprv_U8Index].Length = l_xCanBuff.u8Length;
memcpy(&socTouchDisplay_U.Input[__gprv_U8Index].Data[0], &l_xCanBuff.u8Data[0], 8);

View File

@ -0,0 +1,12 @@
#ifndef IVEC_RTE_INC_IVEC_RTE_H_
#define IVEC_RTE_INC_IVEC_RTE_H_
#include "ivec_bsw_common.h"
#include "stdint.h"
#include "stdio.h"
int u16CMPLX_vFrameFind(uint8_t* pcBuffer, int i32Len, uint8_t** pcFrameStartPt, uint8_t** pcFrameEndPt);
uint16_t u16CMPLX_vFrameDecode( const uint8_t* pInData, int i32InLen, uint8_t* pu8FrameData, int u8FrameBufLen ,int *pi32FrameDlc,uint32_t *pu32Id);
uint16_t u16CMPLX_vFrameEncode(uint32_t u32Id, const uint8_t* pInData, int i32InLen, uint8_t* pu8FrameBuffer, int pu8FrameBufferLen);
#endif /* IVEC_RTE_INC_IVEC_RTE_H_ */

View File

@ -0,0 +1,113 @@
#include "ivec_cmplx_vFrame.h"
#define IVEC_VFRAME_MIN_LEN 14
#define IVEC_VFRAME_DLC 2
#define IVEC_VFRAME_DLC_LEN 2
#define IVEC_VFRAME_ID 4
#define IVEC_VFRAME_ID_LEN 4
#define IVEC_VFRAME_DATA_START 12
#define IVEC_VFRAME_SYNC_CHAR_0 0xb5
#define IVEC_VFRAME_SYNC_CHAR_1 0x63
#define LOG_STRING "ivec-cmplx-vframe"
static void __prv_CalculateChecksum(uint8_t* pkt, int len, uint8_t* ck)
{
uint8_t mck_a = 0, mck_b = 0;
/*Incremented to ignore Sync data*/
for (int i = 2; i < len - 2; i++) {
mck_a += pkt[i];
mck_b += mck_a;
}
mck_a &= 0xFF;
mck_b &= 0xFF;
ck[0] = mck_a;
ck[1] = mck_b;
}
uint16_t u16CMPLX_vFrameEncode(uint32_t u32Id, const uint8_t* pInData, int i32InLen, uint8_t* pu8FrameBuffer, int pu8FrameBufferLen)
{
if (i32InLen > (pu8FrameBufferLen- IVEC_VFRAME_MIN_LEN) || pu8FrameBuffer==NULL)
return 0;
uint16_t u16Dlc = i32InLen;
pu8FrameBuffer[0] = IVEC_VFRAME_SYNC_CHAR_0;
pu8FrameBuffer[1] = IVEC_VFRAME_SYNC_CHAR_1;
memcpy(&pu8FrameBuffer[IVEC_VFRAME_ID], &u32Id, IVEC_VFRAME_ID_LEN);
if (pInData)
{
memcpy(&pu8FrameBuffer[IVEC_VFRAME_DATA_START], pInData, u16Dlc);
}
else
{
u16Dlc = 0;
}
memcpy(&pu8FrameBuffer[IVEC_VFRAME_DLC], &u16Dlc, IVEC_VFRAME_DLC_LEN);
__prv_CalculateChecksum(pu8FrameBuffer, u16Dlc + IVEC_VFRAME_MIN_LEN, &pu8FrameBuffer[IVEC_VFRAME_MIN_LEN + u16Dlc - 2]);
return u16Dlc + IVEC_VFRAME_MIN_LEN;
}
uint16_t u16CMPLX_vFrameDecode( const uint8_t* pInData, int i32InLen, uint8_t* pu8FrameData, int i32FrameBufLen ,int *pi32FrameDlc,uint32_t *pu32Id)
{
if (pInData==NULL || i32InLen<IVEC_VFRAME_MIN_LEN )
return 0;
if(pu32Id)
memcpy(pu32Id,&pInData[IVEC_VFRAME_ID], IVEC_VFRAME_ID_LEN);
if (pu8FrameData && pi32FrameDlc && i32FrameBufLen>0)
{
memcpy(pi32FrameDlc,&pInData[IVEC_VFRAME_DLC], IVEC_VFRAME_DLC_LEN);
if(*pi32FrameDlc<=i32FrameBufLen)
memcpy(pu8FrameData, &pInData[IVEC_VFRAME_DATA_START], *pi32FrameDlc);
}
return 1;
}
int u16CMPLX_vFrameFind(uint8_t* pcBuffer, int i32Len, uint8_t** pcFrameStartPt, uint8_t** pcFrameEndPt)
{
IVEC_BSW_LOG(LOG_STRING, "Scanning Frame : 0x%x-%d", pcBuffer, i32Len);
uint8_t* l_pcFrameStartPt = NULL;
uint8_t* l_pcFrameEndPt = NULL;
int l_i32Ret=0;
if (pcBuffer == NULL || i32Len <= 0)
goto exit;
int i32Ijk = 0;
for (; i32Ijk < i32Len; i32Ijk++)
{
if (pcBuffer[i32Ijk] == IVEC_VFRAME_SYNC_CHAR_0 && (i32Ijk <= (i32Len - 2)) && (pcBuffer[i32Ijk + 1] == IVEC_VFRAME_SYNC_CHAR_1))
{
l_pcFrameStartPt = &pcBuffer[i32Ijk];
break;
}
}
int i32RemainingLen = i32Len - i32Ijk;
IVEC_BSW_LOG(LOG_STRING, "Remaining len: %d", i32RemainingLen);
if (l_pcFrameStartPt == NULL || (i32RemainingLen < IVEC_VFRAME_MIN_LEN))
goto exit;
uint16_t u16Dlc = 0;
memcpy(&u16Dlc,l_pcFrameStartPt + IVEC_VFRAME_DLC,IVEC_VFRAME_DLC_LEN);
IVEC_BSW_LOG(LOG_STRING, "DLC Found: %d", ucDlc);
if (i32RemainingLen < (u16Dlc + IVEC_VFRAME_MIN_LEN))
goto exit;
l_pcFrameEndPt = l_pcFrameStartPt + u16Dlc + IVEC_VFRAME_MIN_LEN - 1;
uint8_t ucCheckSum[2];
__prv_CalculateChecksum(l_pcFrameStartPt, u16Dlc + IVEC_VFRAME_MIN_LEN, ucCheckSum);
IVEC_BSW_LOG(LOG_STRING, "Checksum: %d-%d-%d-%d", *(l_pcFrameEndPt - 1), *(l_pcFrameEndPt), ucCheckSum[0], ucCheckSum[1]);
if (ucCheckSum[0] == *(l_pcFrameEndPt - 1) && ucCheckSum[1] == *(l_pcFrameEndPt))
{
goto exit;
}
l_pcFrameStartPt = l_pcFrameEndPt;
l_pcFrameEndPt = NULL;
l_i32Ret=2;//cksum failure
exit:
*pcFrameStartPt = l_pcFrameStartPt;
*pcFrameEndPt = l_pcFrameEndPt;
IVEC_BSW_LOG(LOG_STRING, "Frame :[0x%x-0x%x|0x%x-0x%x]",l_pcFrameStartPt,l_pcFrameEndPt, *pcFrameStartPt, *pcFrameEndPt);
if (l_pcFrameStartPt && l_pcFrameEndPt)
return 1;
return l_i32Ret;
}