From bfabca4b6d78c7a858e5ff4de92a6cb59f92c98d Mon Sep 17 00:00:00 2001 From: Rakshitavecmocon Date: Thu, 17 Oct 2024 16:26:33 +0530 Subject: [PATCH] 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. --- Core/Source/ivec_mcal_mcan.c | 60 +++++++++++++++++++++++++----------- Core/Source/ivec_mcal_uart.c | 15 +++++++++ main.c | 2 +- utils/utils.h | 9 +++--- 4 files changed, 63 insertions(+), 23 deletions(-) diff --git a/Core/Source/ivec_mcal_mcan.c b/Core/Source/ivec_mcal_mcan.c index c16d68d..7f9da63 100644 --- a/Core/Source/ivec_mcal_mcan.c +++ b/Core/Source/ivec_mcal_mcan.c @@ -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 */ diff --git a/Core/Source/ivec_mcal_uart.c b/Core/Source/ivec_mcal_uart.c index 8685d4d..103ee89 100644 --- a/Core/Source/ivec_mcal_uart.c +++ b/Core/Source/ivec_mcal_uart.c @@ -626,6 +626,13 @@ static xCoreStatus_t uart_init(McalUartHandle_s* pxUartHandle, McalUartBaudRate_ { 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) // { // DL_UART_Main_setBaudRateDivisor(uart_inst, 156, 16); @@ -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*/ diff --git a/main.c b/main.c index eaf7b59..f7a30a0 100644 --- a/main.c +++ b/main.c @@ -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) { diff --git a/utils/utils.h b/utils/utils.h index 235073d..50accfc 100644 --- a/utils/utils.h +++ b/utils/utils.h @@ -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;