feat: Add runtime CAN baud rate change based on UART packet mode

- Implemented functionality to change the CAN baud rate at runtime if the received UART packet has mode set to one.
stable
Rakshitavecmocon 2024-10-17 16:26:33 +05:30
parent 9b2c2312cb
commit bfabca4b6d
4 changed files with 63 additions and 23 deletions

View File

@ -163,16 +163,15 @@ static DL_MCAN_BitTimingParams gMCAN0BitTimes_500 = {
.dataSynchJumpWidth = 0,
};
static const DL_MCAN_BitTimingParams gMCAN0BitTimes_250 = {
/* Arbitration Baud Rate Pre-scaler. */
.nomRatePrescalar = 0,
/* Arbitration Time segment before sample point. */
.nomTimeSeg1 = 138,
.nomTimeSeg1 = 82,
/* Arbitration Time segment after sample point. */
.nomTimeSeg2 = 19,
.nomTimeSeg2 = 11,
/* Arbitration (Re)Synchronization Jump Width Range. */
.nomSynchJumpWidth = 19,
.nomSynchJumpWidth = 11,
/* Data Baud Rate Pre-scaler. */
.dataRatePrescalar = 0,
/* Data Time segment before sample point. */
@ -183,7 +182,26 @@ static const DL_MCAN_BitTimingParams gMCAN0BitTimes_250 = {
.dataSynchJumpWidth = 0,
};
static const DL_MCAN_BitTimingParams gMCAN0BitTimes_125 = {
//static const DL_MCAN_BitTimingParams gMCAN0BitTimes_125 = {
// /* Arbitration Baud Rate Pre-scaler. */
// .nomRatePrescalar = 0,
// /* Arbitration Time segment before sample point. */
// .nomTimeSeg1 = 138,
// /* Arbitration Time segment after sample point. */
// .nomTimeSeg2 = 19,
// /* Arbitration (Re)Synchronization Jump Width Range. */
// .nomSynchJumpWidth = 19,
// /* Data Baud Rate Pre-scaler. */
// .dataRatePrescalar = 0,
// /* Data Time segment before sample point. */
// .dataTimeSeg1 = 0,
// /* Data Time segment after sample point. */
// .dataTimeSeg2 = 0,
// /* Data (Re)Synchronization Jump Width. */
// .dataSynchJumpWidth = 0,
//};
static const DL_MCAN_BitTimingParams gMCAN0BitTimes_150 = {
/* Arbitration Baud Rate Pre-scaler. */
.nomRatePrescalar = 0,
/* Arbitration Time segment before sample point. */
@ -195,34 +213,33 @@ static const DL_MCAN_BitTimingParams gMCAN0BitTimes_125 = {
/* Data Baud Rate Pre-scaler. */
.dataRatePrescalar = 0,
/* Data Time segment before sample point. */
.dataTimeSeg1 = 0,
.dataTimeSeg1 = 19,
/* Data Time segment after sample point. */
.dataTimeSeg2 = 0,
.dataTimeSeg2 = 2,
/* Data (Re)Synchronization Jump Width. */
.dataSynchJumpWidth = 0,
.dataSynchJumpWidth = 2,
};
static const DL_MCAN_BitTimingParams gMCAN0BitTimes_1000 = {
/* Arbitration Baud Rate Pre-scaler. */
.nomRatePrescalar = 0,
/* Arbitration Time segment before sample point. */
.nomTimeSeg1 = 138,
.nomTimeSeg1 = 19,
/* Arbitration Time segment after sample point. */
.nomTimeSeg2 = 19,
.nomTimeSeg2 = 2,
/* Arbitration (Re)Synchronization Jump Width Range. */
.nomSynchJumpWidth = 19,
.nomSynchJumpWidth = 2,
/* Data Baud Rate Pre-scaler. */
.dataRatePrescalar = 0,
/* Data Time segment before sample point. */
.dataTimeSeg1 = 0,
.dataTimeSeg1 = 19,
/* Data Time segment after sample point. */
.dataTimeSeg2 = 0,
.dataTimeSeg2 = 2,
/* Data (Re)Synchronization Jump Width. */
.dataSynchJumpWidth = 0,
.dataSynchJumpWidth = 2,
};
/*____________________________________________________________________________________________________________________________________________________________________________________________*/
@ -465,7 +482,6 @@ IVEC_McalStatus_e xMCAL_MCANInit(MCAN_Regs* MCAN, xCAN_baud_t BAUD)
/* Configure Bit timings. */
if(BAUD==BAUD_500)
{
DL_MCAN_setBitTime(MCAN, (DL_MCAN_BitTimingParams*) &gMCAN0BitTimes_500);
}
else if(BAUD==BAUD_250)
@ -478,7 +494,15 @@ IVEC_McalStatus_e xMCAL_MCANInit(MCAN_Regs* MCAN, xCAN_baud_t BAUD)
}
else if(BAUD==BAUD_150)
{
DL_MCAN_setBitTime(MCAN, (DL_MCAN_BitTimingParams*) &gMCAN0BitTimes_125);
DL_MCAN_setBitTime(MCAN, (DL_MCAN_BitTimingParams*) &gMCAN0BitTimes_150);
}
// else if(BAUD==BAUD_125)
// {
// DL_MCAN_setBitTime(MCAN, (DL_MCAN_BitTimingParams*) &gMCAN0BitTimes_125);
// }
else
{
DL_MCAN_setBitTime(MCAN, (DL_MCAN_BitTimingParams*) &gMCAN0BitTimes_500);
}
/* Configure Message RAM Sections */

View File

@ -625,6 +625,13 @@ static xCoreStatus_t uart_init(McalUartHandle_s* pxUartHandle, McalUartBaudRate_
else if(xBaud==1000000)
{
DL_UART_Main_setBaudRateDivisor(uart_inst, 1, 32);
}
else{
/*
* Target baud rate: 115200
* Actual baud rate: 115211.52
*/
DL_UART_Main_setBaudRateDivisor(uart_inst, 13, 1);
}
// else if(xBaud==1843200)
// {
@ -666,6 +673,10 @@ static xCoreStatus_t uart_init(McalUartHandle_s* pxUartHandle, McalUartBaudRate_
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(uart_inst);
/*Clearing and Enabling Interrupt Requests*/
@ -685,6 +696,10 @@ static xCoreStatus_t uart_init(McalUartHandle_s* pxUartHandle, McalUartBaudRate_
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(uart_inst);
/*Clearing and Enabling Interrupt Requests*/

2
main.c
View File

@ -193,7 +193,7 @@ int main(void)
g_uartSpeed = mcalUART_BAUD_115200;
g_canSpeed = BAUD_250;
xECU_UARTInit(&prvUartHandle, g_uartSpeed);
xECU_CANInit(CANFD0,BAUD_500);
xECU_CANInit(CANFD0,g_canSpeed);
while(1)
{

View File

@ -58,10 +58,11 @@ typedef enum
typedef enum
{
/*CAN Baud Rate Options*/
BAUD_500 = 0,
BAUD_250 = 1,
BAUD_150 = 2,
BAUD_1000 = 3,
BAUD_500 = 500,
BAUD_250 = 250,
BAUD_150 = 150,
BAUD_1000 = 1000,
BAUD_125 = 125,
}xCAN_baud_t;