From 330ed30a805b455d5e00303554ff6c0a8b61d741 Mon Sep 17 00:00:00 2001 From: Frank Voorburg Date: Tue, 7 Aug 2018 21:00:03 +0000 Subject: [PATCH] Refs #591. Improved CAN clock configuration by directly using the APB1 clock frequency. git-svn-id: https://svn.code.sf.net/p/openblt/code/trunk@583 5dc33758-31d5-4daf-9ae8-b24bf3d40d73 --- .../ARMCM0_STM32F0_Nucleo_F091RC_GCC/Prog/boot.c | 8 ++++++-- .../ARMCM0_STM32F0_Nucleo_F091RC_IAR/Prog/boot.c | 8 ++++++-- .../ARMCM0_STM32F0_Nucleo_F091RC_Keil/Prog/boot.c | 8 ++++++-- .../Prog/boot.c | 8 ++++++-- .../Prog/boot.c | 8 ++++++-- .../Prog/boot.c | 8 ++++++-- .../Prog/boot.c | 8 ++++++-- .../ARMCM4_STM32F3_Nucleo_F303K8_GCC/Prog/boot.c | 8 ++++++-- .../ARMCM4_STM32F3_Nucleo_F303K8_IAR/Prog/boot.c | 8 ++++++-- .../Prog/boot.c | 8 ++++++-- .../ARMCM4_STM32L4_Nucleo_L476RG_GCC/Prog/boot.c | 10 +++++++--- .../ARMCM4_STM32L4_Nucleo_L476RG_IAR/Prog/boot.c | 10 +++++++--- .../Prog/boot.c | 10 +++++++--- Target/Source/ARMCM0_STM32F0/can.c | 12 ++++++++++-- Target/Source/ARMCM3_STM32F2/can.c | 14 +++++++++++--- Target/Source/ARMCM4_STM32F3/can.c | 14 +++++++++++--- Target/Source/ARMCM4_STM32L4/can.c | 14 +++++++++++--- 17 files changed, 124 insertions(+), 40 deletions(-) diff --git a/Target/Demo/ARMCM0_STM32F0_Nucleo_F091RC_GCC/Prog/boot.c b/Target/Demo/ARMCM0_STM32F0_Nucleo_F091RC_GCC/Prog/boot.c index f57a084c..e096c730 100644 --- a/Target/Demo/ARMCM0_STM32F0_Nucleo_F091RC_GCC/Prog/boot.c +++ b/Target/Demo/ARMCM0_STM32F0_Nucleo_F091RC_GCC/Prog/boot.c @@ -303,14 +303,18 @@ static unsigned char CanGetSpeedConfig(unsigned short baud, unsigned short *pres unsigned char *tseg1, unsigned char *tseg2) { unsigned char cnt; + unsigned long canClockFreqkHz; + + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = HAL_RCC_GetPCLK1Freq() / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ( (*prescaler > 0) && (*prescaler <= 1024) ) diff --git a/Target/Demo/ARMCM0_STM32F0_Nucleo_F091RC_IAR/Prog/boot.c b/Target/Demo/ARMCM0_STM32F0_Nucleo_F091RC_IAR/Prog/boot.c index 9d6df103..2da9d0cd 100644 --- a/Target/Demo/ARMCM0_STM32F0_Nucleo_F091RC_IAR/Prog/boot.c +++ b/Target/Demo/ARMCM0_STM32F0_Nucleo_F091RC_IAR/Prog/boot.c @@ -303,14 +303,18 @@ static unsigned char CanGetSpeedConfig(unsigned short baud, unsigned short *pres unsigned char *tseg1, unsigned char *tseg2) { unsigned char cnt; + unsigned long canClockFreqkHz; + + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = HAL_RCC_GetPCLK1Freq() / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ( (*prescaler > 0) && (*prescaler <= 1024) ) diff --git a/Target/Demo/ARMCM0_STM32F0_Nucleo_F091RC_Keil/Prog/boot.c b/Target/Demo/ARMCM0_STM32F0_Nucleo_F091RC_Keil/Prog/boot.c index a131ea49..2e5cbc21 100644 --- a/Target/Demo/ARMCM0_STM32F0_Nucleo_F091RC_Keil/Prog/boot.c +++ b/Target/Demo/ARMCM0_STM32F0_Nucleo_F091RC_Keil/Prog/boot.c @@ -303,14 +303,18 @@ static unsigned char CanGetSpeedConfig(unsigned short baud, unsigned short *pres unsigned char *tseg1, unsigned char *tseg2) { unsigned char cnt; + unsigned long canClockFreqkHz; + + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = HAL_RCC_GetPCLK1Freq() / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ( (*prescaler > 0) && (*prescaler <= 1024) ) diff --git a/Target/Demo/ARMCM0_STM32F0_Nucleo_F091RC_TrueStudio/Prog/boot.c b/Target/Demo/ARMCM0_STM32F0_Nucleo_F091RC_TrueStudio/Prog/boot.c index b11e22a4..16dfb737 100644 --- a/Target/Demo/ARMCM0_STM32F0_Nucleo_F091RC_TrueStudio/Prog/boot.c +++ b/Target/Demo/ARMCM0_STM32F0_Nucleo_F091RC_TrueStudio/Prog/boot.c @@ -303,14 +303,18 @@ static unsigned char CanGetSpeedConfig(unsigned short baud, unsigned short *pres unsigned char *tseg1, unsigned char *tseg2) { unsigned char cnt; + unsigned long canClockFreqkHz; + + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = HAL_RCC_GetPCLK1Freq() / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ( (*prescaler > 0) && (*prescaler <= 1024) ) diff --git a/Target/Demo/ARMCM3_STM32F2_Olimex_STM32P207_GCC/Prog/boot.c b/Target/Demo/ARMCM3_STM32F2_Olimex_STM32P207_GCC/Prog/boot.c index b413bdf7..ce0005ea 100644 --- a/Target/Demo/ARMCM3_STM32F2_Olimex_STM32P207_GCC/Prog/boot.c +++ b/Target/Demo/ARMCM3_STM32F2_Olimex_STM32P207_GCC/Prog/boot.c @@ -303,14 +303,18 @@ static unsigned char CanGetSpeedConfig(unsigned short baud, unsigned short *pres unsigned char *tseg1, unsigned char *tseg2) { unsigned char cnt; + unsigned long canClockFreqkHz; + + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = HAL_RCC_GetPCLK1Freq() / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ/4) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ/4)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ( (*prescaler > 0) && (*prescaler <= 1024) ) diff --git a/Target/Demo/ARMCM3_STM32F2_Olimex_STM32P207_IAR/Prog/boot.c b/Target/Demo/ARMCM3_STM32F2_Olimex_STM32P207_IAR/Prog/boot.c index 0b068599..04e6792d 100644 --- a/Target/Demo/ARMCM3_STM32F2_Olimex_STM32P207_IAR/Prog/boot.c +++ b/Target/Demo/ARMCM3_STM32F2_Olimex_STM32P207_IAR/Prog/boot.c @@ -303,14 +303,18 @@ static unsigned char CanGetSpeedConfig(unsigned short baud, unsigned short *pres unsigned char *tseg1, unsigned char *tseg2) { unsigned char cnt; + unsigned long canClockFreqkHz; + + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = HAL_RCC_GetPCLK1Freq() / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ/4) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ/4)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ( (*prescaler > 0) && (*prescaler <= 1024) ) diff --git a/Target/Demo/ARMCM3_STM32F2_Olimex_STM32P207_TrueStudio/Prog/boot.c b/Target/Demo/ARMCM3_STM32F2_Olimex_STM32P207_TrueStudio/Prog/boot.c index f3882f88..5271d430 100644 --- a/Target/Demo/ARMCM3_STM32F2_Olimex_STM32P207_TrueStudio/Prog/boot.c +++ b/Target/Demo/ARMCM3_STM32F2_Olimex_STM32P207_TrueStudio/Prog/boot.c @@ -303,14 +303,18 @@ static unsigned char CanGetSpeedConfig(unsigned short baud, unsigned short *pres unsigned char *tseg1, unsigned char *tseg2) { unsigned char cnt; + unsigned long canClockFreqkHz; + + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = HAL_RCC_GetPCLK1Freq() / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ/4) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ/4)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ( (*prescaler > 0) && (*prescaler <= 1024) ) diff --git a/Target/Demo/ARMCM4_STM32F3_Nucleo_F303K8_GCC/Prog/boot.c b/Target/Demo/ARMCM4_STM32F3_Nucleo_F303K8_GCC/Prog/boot.c index 2924c5e9..2ff550af 100644 --- a/Target/Demo/ARMCM4_STM32F3_Nucleo_F303K8_GCC/Prog/boot.c +++ b/Target/Demo/ARMCM4_STM32F3_Nucleo_F303K8_GCC/Prog/boot.c @@ -303,14 +303,18 @@ static unsigned char CanGetSpeedConfig(unsigned short baud, unsigned short *pres unsigned char *tseg1, unsigned char *tseg2) { unsigned char cnt; + unsigned long canClockFreqkHz; + + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = HAL_RCC_GetPCLK1Freq() / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ/2) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ/2)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ( (*prescaler > 0) && (*prescaler <= 1024) ) diff --git a/Target/Demo/ARMCM4_STM32F3_Nucleo_F303K8_IAR/Prog/boot.c b/Target/Demo/ARMCM4_STM32F3_Nucleo_F303K8_IAR/Prog/boot.c index b8cab87c..fca4acb4 100644 --- a/Target/Demo/ARMCM4_STM32F3_Nucleo_F303K8_IAR/Prog/boot.c +++ b/Target/Demo/ARMCM4_STM32F3_Nucleo_F303K8_IAR/Prog/boot.c @@ -303,14 +303,18 @@ static unsigned char CanGetSpeedConfig(unsigned short baud, unsigned short *pres unsigned char *tseg1, unsigned char *tseg2) { unsigned char cnt; + unsigned long canClockFreqkHz; + + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = HAL_RCC_GetPCLK1Freq() / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ/2) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ/2)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ( (*prescaler > 0) && (*prescaler <= 1024) ) diff --git a/Target/Demo/ARMCM4_STM32F3_Nucleo_F303K8_TrueStudio/Prog/boot.c b/Target/Demo/ARMCM4_STM32F3_Nucleo_F303K8_TrueStudio/Prog/boot.c index 96d99c84..a5d9ad1e 100644 --- a/Target/Demo/ARMCM4_STM32F3_Nucleo_F303K8_TrueStudio/Prog/boot.c +++ b/Target/Demo/ARMCM4_STM32F3_Nucleo_F303K8_TrueStudio/Prog/boot.c @@ -303,14 +303,18 @@ static unsigned char CanGetSpeedConfig(unsigned short baud, unsigned short *pres unsigned char *tseg1, unsigned char *tseg2) { unsigned char cnt; + unsigned long canClockFreqkHz; + + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = HAL_RCC_GetPCLK1Freq() / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ/2) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ/2)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ( (*prescaler > 0) && (*prescaler <= 1024) ) diff --git a/Target/Demo/ARMCM4_STM32L4_Nucleo_L476RG_GCC/Prog/boot.c b/Target/Demo/ARMCM4_STM32L4_Nucleo_L476RG_GCC/Prog/boot.c index 38c1bcad..785d0f54 100644 --- a/Target/Demo/ARMCM4_STM32L4_Nucleo_L476RG_GCC/Prog/boot.c +++ b/Target/Demo/ARMCM4_STM32L4_Nucleo_L476RG_GCC/Prog/boot.c @@ -303,14 +303,18 @@ static unsigned char CanGetSpeedConfig(unsigned short baud, unsigned short *pres unsigned char *tseg1, unsigned char *tseg2) { unsigned char cnt; + unsigned long canClockFreqkHz; + + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = HAL_RCC_GetPCLK1Freq() / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ( (*prescaler > 0) && (*prescaler <= 1024) ) @@ -335,7 +339,7 @@ static unsigned char CanGetSpeedConfig(unsigned short baud, unsigned short *pres ****************************************************************************************/ static void BootComCanInit(void) { - unsigned short prescaler; + unsigned short prescaler = 0; unsigned char tseg1 = 0, tseg2 = 0; CAN_FilterTypeDef filterConfig; unsigned long rxMsgId = BOOT_COM_CAN_RX_MSG_ID; diff --git a/Target/Demo/ARMCM4_STM32L4_Nucleo_L476RG_IAR/Prog/boot.c b/Target/Demo/ARMCM4_STM32L4_Nucleo_L476RG_IAR/Prog/boot.c index ec07aad2..a27aeecb 100644 --- a/Target/Demo/ARMCM4_STM32L4_Nucleo_L476RG_IAR/Prog/boot.c +++ b/Target/Demo/ARMCM4_STM32L4_Nucleo_L476RG_IAR/Prog/boot.c @@ -303,14 +303,18 @@ static unsigned char CanGetSpeedConfig(unsigned short baud, unsigned short *pres unsigned char *tseg1, unsigned char *tseg2) { unsigned char cnt; + unsigned long canClockFreqkHz; + + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = HAL_RCC_GetPCLK1Freq() / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ( (*prescaler > 0) && (*prescaler <= 1024) ) @@ -335,7 +339,7 @@ static unsigned char CanGetSpeedConfig(unsigned short baud, unsigned short *pres ****************************************************************************************/ static void BootComCanInit(void) { - unsigned short prescaler; + unsigned short prescaler = 0; unsigned char tseg1 = 0, tseg2 = 0; CAN_FilterTypeDef filterConfig; unsigned long rxMsgId = BOOT_COM_CAN_RX_MSG_ID; diff --git a/Target/Demo/ARMCM4_STM32L4_Nucleo_L476RG_TrueStudio/Prog/boot.c b/Target/Demo/ARMCM4_STM32L4_Nucleo_L476RG_TrueStudio/Prog/boot.c index e2a3947e..3c8b7b8e 100644 --- a/Target/Demo/ARMCM4_STM32L4_Nucleo_L476RG_TrueStudio/Prog/boot.c +++ b/Target/Demo/ARMCM4_STM32L4_Nucleo_L476RG_TrueStudio/Prog/boot.c @@ -303,14 +303,18 @@ static unsigned char CanGetSpeedConfig(unsigned short baud, unsigned short *pres unsigned char *tseg1, unsigned char *tseg2) { unsigned char cnt; + unsigned long canClockFreqkHz; + + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = HAL_RCC_GetPCLK1Freq() / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ( (*prescaler > 0) && (*prescaler <= 1024) ) @@ -335,7 +339,7 @@ static unsigned char CanGetSpeedConfig(unsigned short baud, unsigned short *pres ****************************************************************************************/ static void BootComCanInit(void) { - unsigned short prescaler; + unsigned short prescaler = 0; unsigned char tseg1 = 0, tseg2 = 0; CAN_FilterTypeDef filterConfig; unsigned long rxMsgId = BOOT_COM_CAN_RX_MSG_ID; diff --git a/Target/Source/ARMCM0_STM32F0/can.c b/Target/Source/ARMCM0_STM32F0/can.c index a0f8ad7f..c4684fb9 100644 --- a/Target/Source/ARMCM0_STM32F0/can.c +++ b/Target/Source/ARMCM0_STM32F0/can.c @@ -32,6 +32,7 @@ ****************************************************************************************/ #include "boot.h" /* bootloader generic header */ #include "stm32f0xx.h" /* STM32 CPU and HAL header */ +#include "stm32f0xx_ll_rcc.h" /* STM32 LL RCC header */ #if (BOOT_COM_CAN_ENABLE > 0) @@ -116,14 +117,21 @@ static blt_bool CanGetSpeedConfig(blt_int16u baud, blt_int16u *prescaler, blt_int8u *tseg1, blt_int8u *tseg2) { blt_int8u cnt; + blt_int32u canClockFreqkHz; + LL_RCC_ClocksTypeDef rccClocks; + + /* read clock frequencies */ + LL_RCC_GetSystemClocksFreq(&rccClocks); + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = rccClocks.PCLK1_Frequency / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ((*prescaler > 0) && (*prescaler <= 1024)) diff --git a/Target/Source/ARMCM3_STM32F2/can.c b/Target/Source/ARMCM3_STM32F2/can.c index 2d116fbd..ff02d913 100644 --- a/Target/Source/ARMCM3_STM32F2/can.c +++ b/Target/Source/ARMCM3_STM32F2/can.c @@ -32,6 +32,7 @@ ****************************************************************************************/ #include "boot.h" /* bootloader generic header */ #include "stm32f2xx.h" /* STM32 CPU and HAL header */ +#include "stm32f2xx_ll_rcc.h" /* STM32 LL RCC header */ #if (BOOT_COM_CAN_ENABLE > 0) @@ -126,14 +127,21 @@ static blt_bool CanGetSpeedConfig(blt_int16u baud, blt_int16u *prescaler, blt_int8u *tseg1, blt_int8u *tseg2) { blt_int8u cnt; + blt_int32u canClockFreqkHz; + LL_RCC_ClocksTypeDef rccClocks; + + /* read clock frequencies */ + LL_RCC_GetSystemClocksFreq(&rccClocks); + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = rccClocks.PCLK1_Frequency / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ/4) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ/4)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ((*prescaler > 0) && (*prescaler <= 1024)) @@ -158,7 +166,7 @@ static blt_bool CanGetSpeedConfig(blt_int16u baud, blt_int16u *prescaler, ****************************************************************************************/ void CanInit(void) { - blt_int16u prescaler; + blt_int16u prescaler = 0; blt_int8u tseg1 = 0, tseg2 = 0; CAN_FilterConfTypeDef filterConfig; blt_int32u rxMsgId = BOOT_COM_CAN_RX_MSG_ID; diff --git a/Target/Source/ARMCM4_STM32F3/can.c b/Target/Source/ARMCM4_STM32F3/can.c index 50253cd6..c0234bd3 100644 --- a/Target/Source/ARMCM4_STM32F3/can.c +++ b/Target/Source/ARMCM4_STM32F3/can.c @@ -32,6 +32,7 @@ ****************************************************************************************/ #include "boot.h" /* bootloader generic header */ #include "stm32f3xx.h" /* STM32 CPU and HAL header */ +#include "stm32f3xx_ll_rcc.h" /* STM32 LL RCC header */ #if (BOOT_COM_CAN_ENABLE > 0) @@ -116,14 +117,21 @@ static blt_bool CanGetSpeedConfig(blt_int16u baud, blt_int16u *prescaler, blt_int8u *tseg1, blt_int8u *tseg2) { blt_int8u cnt; + blt_int32u canClockFreqkHz; + LL_RCC_ClocksTypeDef rccClocks; + + /* read clock frequencies */ + LL_RCC_GetSystemClocksFreq(&rccClocks); + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = rccClocks.PCLK1_Frequency / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ/2) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ/2)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ((*prescaler > 0) && (*prescaler <= 1024)) @@ -148,7 +156,7 @@ static blt_bool CanGetSpeedConfig(blt_int16u baud, blt_int16u *prescaler, ****************************************************************************************/ void CanInit(void) { - blt_int16u prescaler; + blt_int16u prescaler = 0; blt_int8u tseg1 = 0, tseg2 = 0; CAN_FilterConfTypeDef filterConfig; blt_int32u rxMsgId = BOOT_COM_CAN_RX_MSG_ID; diff --git a/Target/Source/ARMCM4_STM32L4/can.c b/Target/Source/ARMCM4_STM32L4/can.c index 8c50e4ae..e89cbde8 100644 --- a/Target/Source/ARMCM4_STM32L4/can.c +++ b/Target/Source/ARMCM4_STM32L4/can.c @@ -32,6 +32,7 @@ ****************************************************************************************/ #include "boot.h" /* bootloader generic header */ #include "stm32l4xx.h" /* STM32 CPU and HAL header */ +#include "stm32l4xx_ll_rcc.h" /* STM32 LL RCC header */ #if (BOOT_COM_CAN_ENABLE > 0) @@ -110,14 +111,21 @@ static blt_bool CanGetSpeedConfig(blt_int16u baud, blt_int16u *prescaler, blt_int8u *tseg1, blt_int8u *tseg2) { blt_int8u cnt; + blt_int32u canClockFreqkHz; + LL_RCC_ClocksTypeDef rccClocks; + + /* read clock frequencies */ + LL_RCC_GetSystemClocksFreq(&rccClocks); + /* store CAN peripheral clock speed in kHz */ + canClockFreqkHz = rccClocks.PCLK1_Frequency / 1000u; /* loop through all possible time quanta configurations to find a match */ for (cnt=0; cnt < sizeof(canTiming)/sizeof(canTiming[0]); cnt++) { - if (((BOOT_CPU_SYSTEM_SPEED_KHZ) % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) + if ((canClockFreqkHz % (baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1))) == 0) { /* compute the prescaler that goes with this TQ configuration */ - *prescaler = (BOOT_CPU_SYSTEM_SPEED_KHZ)/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); + *prescaler = canClockFreqkHz/(baud*(canTiming[cnt].tseg1+canTiming[cnt].tseg2+1)); /* make sure the prescaler is valid */ if ((*prescaler > 0) && (*prescaler <= 1024)) @@ -142,7 +150,7 @@ static blt_bool CanGetSpeedConfig(blt_int16u baud, blt_int16u *prescaler, ****************************************************************************************/ void CanInit(void) { - blt_int16u prescaler; + blt_int16u prescaler = 0; blt_int8u tseg1 = 0, tseg2 = 0; CAN_FilterTypeDef filterConfig; blt_int32u rxMsgId = BOOT_COM_CAN_RX_MSG_ID;