Skip to content

Commit 5293499

Browse files
committed
pass CAN FD input clock as a parameter to get_canfd_nominal_prescaler/get_canfd_data_prescaler functions
1 parent 1b5eb70 commit 5293499

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

targets/TARGET_STM/can_api.c

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -78,20 +78,9 @@ int can_internal_init(can_t *obj)
7878

7979
/** Get the nominal phase clock prescaler and the number of time quantums for the specified frequency
8080
*/
81-
static uint32_t get_canfd_nominal_prescaler(int f, uint32_t *ntq_out)
81+
static uint32_t get_canfd_nominal_prescaler(uint32_t input_f, int f, uint32_t *ntq_out)
8282
{
83-
#if (defined TARGET_STM32H7)
84-
// STM32H7 doesn't support yet HAL_RCCEx_GetPeriphCLKFreq for FDCAN
85-
PLL1_ClocksTypeDef pll1_clocks;
86-
HAL_RCCEx_GetPLL1ClockFreq(&pll1_clocks);
87-
uint32_t ntq = pll1_clocks.PLL1_Q_Frequency / (uint32_t)f;
88-
#else
89-
#if (defined RCC_PERIPHCLK_FDCAN1)
90-
uint32_t ntq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN1) / (uint32_t)f;
91-
#else
92-
uint32_t ntq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN) / (uint32_t)f;
93-
#endif
94-
#endif
83+
uint32_t ntq = input_f / (uint32_t)f;
9584
uint32_t nominalPrescaler = 1;
9685
// !When the sample point should be lower than 50%, this must be changed to
9786
// !IS_FDCAN_NOMINAL_TSEG2(ntq/nominalPrescaler), since
@@ -108,21 +97,12 @@ static uint32_t get_canfd_nominal_prescaler(int f, uint32_t *ntq_out)
10897

10998
/** Get the data phase clock prescaler and the number of time quantums for the specified frequency
11099
*/
111-
static uint32_t get_canfd_data_prescaler(int data_f, uint32_t *ntq_out)
100+
static uint32_t get_canfd_data_prescaler(uint32_t input_f, int data_f, uint32_t *ntq_out)
112101
{
113-
#if (defined TARGET_STM32H7)
114-
// STM32H7 doesn't support yet HAL_RCCEx_GetPeriphCLKFreq for FDCAN
115-
PLL1_ClocksTypeDef pll1_clocks;
116-
HAL_RCCEx_GetPLL1ClockFreq(&pll1_clocks);
117-
uint32_t ntq_data = pll1_clocks.PLL1_Q_Frequency / (uint32_t)data_f;
118-
#elif (defined RCC_PERIPHCLK_FDCAN1)
119-
uint32_t ntq_data = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN1) / (uint32_t)data_f;
120-
#else
121-
uint32_t ntq_data = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN) / (uint32_t)data_f;
122-
#endif
102+
uint32_t ntq_data = input_f / (uint32_t)data_f;
123103
uint32_t dataPrescaler = 1;
124104
// !When the sample point should be lower than 50%, this must be changed to
125-
// !IS_FDCAN_NOMINAL_TSEG2(ntq/nominalPrescaler), since
105+
// !IS_FDCAN_DATA_TSEG2(ntq_data/dataPrescaler), since
126106
// NTSEG2 and SJW max values are lower. For now the sample point is fix @70%
127107
while (!IS_FDCAN_DATA_TSEG1(ntq_data / dataPrescaler)) {
128108
dataPrescaler ++;
@@ -224,8 +204,20 @@ static void _canfd_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int
224204
* does not work for the desired bitrate, change system_clock settings for FDCAN_CLK
225205
* (default FDCAN_CLK is PLLQ)
226206
*/
207+
#if (defined TARGET_STM32H7)
208+
// STM32H7 doesn't support yet HAL_RCCEx_GetPeriphCLKFreq for FDCAN
209+
PLL1_ClocksTypeDef pll1_clocks;
210+
HAL_RCCEx_GetPLL1ClockFreq(&pll1_clocks);
211+
uint32_t input_hz = pll1_clocks.PLL1_Q_Frequency;
212+
#else
213+
#if (defined RCC_PERIPHCLK_FDCAN1)
214+
uint32_t input_hz = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN1);
215+
#else
216+
uint32_t input_hz = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN);
217+
#endif
218+
#endif
227219
uint32_t ntq = 0;
228-
uint32_t nominalPrescaler = get_canfd_nominal_prescaler(hz, &ntq);
220+
uint32_t nominalPrescaler = get_canfd_nominal_prescaler(input_hz, hz, &ntq);
229221
uint32_t ntq_data = ntq;
230222
uint32_t dataPrescaler = nominalPrescaler;
231223
if(data_hz == 0)
@@ -239,7 +231,7 @@ static void _canfd_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int
239231
else
240232
{
241233
obj->CanHandle.Init.FrameFormat = FDCAN_FRAME_FD_BRS;
242-
dataPrescaler = get_canfd_data_prescaler(data_hz, &ntq_data);
234+
dataPrescaler = get_canfd_data_prescaler(input_hz, data_hz, &ntq_data);
243235
}
244236
obj->CanHandle.Init.Mode = FDCAN_MODE_NORMAL;
245237
obj->CanHandle.Init.AutoRetransmission = ENABLE;
@@ -451,8 +443,20 @@ int canfd_frequency(can_t *obj, int f, int data_f)
451443
* does not work for the desired bitrate, change system_clock settings for FDCAN_CLK
452444
* (default FDCAN_CLK is PLLQ)
453445
*/
446+
#if (defined TARGET_STM32H7)
447+
// STM32H7 doesn't support yet HAL_RCCEx_GetPeriphCLKFreq for FDCAN
448+
PLL1_ClocksTypeDef pll1_clocks;
449+
HAL_RCCEx_GetPLL1ClockFreq(&pll1_clocks);
450+
uint32_t input_f = pll1_clocks.PLL1_Q_Frequency;
451+
#else
452+
#if (defined RCC_PERIPHCLK_FDCAN1)
453+
uint32_t input_f = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN1);
454+
#else
455+
uint32_t input_f = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN);
456+
#endif
457+
#endif
454458
uint32_t ntq = 0;
455-
uint32_t nominalPrescaler = get_canfd_nominal_prescaler(f, &ntq);
459+
uint32_t nominalPrescaler = get_canfd_nominal_prescaler(input_f, f, &ntq);
456460
uint32_t ntq_data = ntq;
457461
uint32_t dataPrescaler = nominalPrescaler;
458462
if(data_f == 0)
@@ -466,7 +470,7 @@ int canfd_frequency(can_t *obj, int f, int data_f)
466470
else
467471
{
468472
obj->CanHandle.Init.FrameFormat = FDCAN_FRAME_FD_BRS;
469-
dataPrescaler = get_canfd_data_prescaler(data_f, &ntq_data);
473+
dataPrescaler = get_canfd_data_prescaler(input_f, data_f, &ntq_data);
470474
}
471475
obj->CanHandle.Init.Mode = FDCAN_MODE_NORMAL;
472476
obj->CanHandle.Init.AutoRetransmission = ENABLE;
@@ -1723,4 +1727,4 @@ const PinMap *can_td_pinmap()
17231727
return PinMap_CAN_RD;
17241728
}
17251729

1726-
#endif // DEVICE_CAN
1730+
#endif // DEVICE_CAN

0 commit comments

Comments
 (0)