Skip to content

Commit 963aa8b

Browse files
authored
Merge pull request #15065 from jeromecoutant/PR_CAN
STM32L5/STM32U5 : CAN support
2 parents fc62b15 + abe2e48 commit 963aa8b

File tree

4 files changed

+40
-14
lines changed

4 files changed

+40
-14
lines changed

targets/TARGET_STM/TARGET_STM32L5/objects.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,9 @@ struct dac_s {
154154

155155
#if DEVICE_CAN
156156
struct can_s {
157-
CAN_HandleTypeDef CanHandle;
157+
FDCAN_HandleTypeDef CanHandle;
158158
int index;
159159
int hz;
160-
int rxIrqEnabled;
161160
};
162161
#endif
163162

targets/TARGET_STM/TARGET_STM32U5/objects.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,9 @@ struct dac_s {
154154

155155
#if DEVICE_CAN
156156
struct can_s {
157-
CAN_HandleTypeDef CanHandle;
157+
FDCAN_HandleTypeDef CanHandle;
158158
int index;
159159
int hz;
160-
int rxIrqEnabled;
161160
};
162161
#endif
163162

targets/TARGET_STM/can_api.c

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,11 @@ static void _can_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int hz
6868
{
6969
MBED_ASSERT((int)pinmap->peripheral != NC);
7070

71+
#if defined(__HAL_RCC_FDCAN1_CLK_ENABLE)
72+
__HAL_RCC_FDCAN1_CLK_ENABLE();
73+
#else
7174
__HAL_RCC_FDCAN_CLK_ENABLE();
75+
#endif
7276

7377
if (pinmap->peripheral == CAN_1) {
7478
obj->index = 0;
@@ -90,8 +94,13 @@ static void _can_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int hz
9094

9195
// Select PLL1Q as source of FDCAN clock
9296
RCC_PeriphCLKInitTypeDef RCC_PeriphClkInit;
97+
#if (defined RCC_PERIPHCLK_FDCAN1)
98+
RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_FDCAN1;
99+
RCC_PeriphClkInit.Fdcan1ClockSelection = RCC_FDCAN1CLKSOURCE_PLL1;
100+
#else
93101
RCC_PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_FDCAN;
94-
RCC_PeriphClkInit.FdcanClockSelection = RCC_FDCANCLKSOURCE_PLL; // 10 MHz (RCC_OscInitStruct.PLL.PLLQ = 80)
102+
RCC_PeriphClkInit.FdcanClockSelection = RCC_FDCANCLKSOURCE_PLL;
103+
#endif
95104
#if defined(DUAL_CORE) && (TARGET_STM32H7)
96105
while (LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID)) {
97106
}
@@ -128,14 +137,18 @@ static void _can_init_freq_direct(can_t *obj, const can_pinmap_t *pinmap, int hz
128137
// !Attention Not all bitrates can be covered with all fdcan-core-clk values. When a clk
129138
// does not work for the desired bitrate, change system_clock settings for FDCAN_CLK
130139
// (default FDCAN_CLK is PLLQ)
131-
#ifdef TARGET_STM32G4
132-
int ntq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN) / hz;
133-
#else
140+
#if (defined TARGET_STM32H7)
134141
// STM32H7 doesn't support yet HAL_RCCEx_GetPeriphCLKFreq for FDCAN
135142
// We use PLL1.Q clock right now so get its frequency
136143
PLL1_ClocksTypeDef pll1_clocks;
137144
HAL_RCCEx_GetPLL1ClockFreq(&pll1_clocks);
138145
int ntq = pll1_clocks.PLL1_Q_Frequency / hz;
146+
#else
147+
#if (defined RCC_PERIPHCLK_FDCAN1)
148+
int ntq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN1) / hz;
149+
#else
150+
int ntq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN) / hz;
151+
#endif
139152
#endif
140153

141154
int nominalPrescaler = 1;
@@ -250,7 +263,7 @@ void can_irq_free(can_t *obj)
250263
else {
251264
return;
252265
}
253-
#ifndef TARGET_STM32G4
266+
#if (defined TARGET_STM32H7)
254267
HAL_NVIC_DisableIRQ(FDCAN_CAL_IRQn);
255268
#endif
256269
can_irq_ids[obj->index] = 0;
@@ -262,12 +275,21 @@ void can_free(can_t *obj)
262275
while (LL_HSEM_1StepLock(HSEM, CFG_HW_RCC_SEMID)) {
263276
}
264277
#endif /* DUAL_CORE */
278+
#if defined(__HAL_RCC_FDCAN1_FORCE_RESET)
279+
__HAL_RCC_FDCAN1_FORCE_RESET();
280+
__HAL_RCC_FDCAN1_RELEASE_RESET();
281+
#else
265282
__HAL_RCC_FDCAN_FORCE_RESET();
266283
__HAL_RCC_FDCAN_RELEASE_RESET();
284+
#endif
267285
#if defined(DUAL_CORE) && (TARGET_STM32H7)
268286
LL_HSEM_ReleaseLock(HSEM, CFG_HW_RCC_SEMID, HSEM_CR_COREID_CURRENT);
269287
#endif /* DUAL_CORE */
288+
#if defined(__HAL_RCC_FDCAN1_CLK_DISABLE)
289+
__HAL_RCC_FDCAN1_CLK_DISABLE();
290+
#else
270291
__HAL_RCC_FDCAN_CLK_DISABLE();
292+
#endif
271293
}
272294

273295

@@ -296,13 +318,17 @@ int can_frequency(can_t *obj, int f)
296318
* does not work for the desired bitrate, change system_clock settings for FDCAN_CLK
297319
* (default FDCAN_CLK is PLLQ)
298320
*/
299-
#ifdef TARGET_STM32G4
300-
int ntq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN) / f;
301-
#else
321+
#if (defined TARGET_STM32H7)
302322
// STM32H7 doesn't support yet HAL_RCCEx_GetPeriphCLKFreq for FDCAN
303323
PLL1_ClocksTypeDef pll1_clocks;
304324
HAL_RCCEx_GetPLL1ClockFreq(&pll1_clocks);
305325
int ntq = pll1_clocks.PLL1_Q_Frequency / f;
326+
#else
327+
#if (defined RCC_PERIPHCLK_FDCAN1)
328+
int ntq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN1) / f;
329+
#else
330+
int ntq = HAL_RCCEx_GetPeriphCLKFreq(RCC_PERIPHCLK_FDCAN) / f;
331+
#endif
306332
#endif
307333

308334
int nominalPrescaler = 1;
@@ -520,7 +546,7 @@ static void can_irq(CANName name, int id)
520546
irq_handler(can_irq_ids[id], IRQ_TX);
521547
}
522548
}
523-
#ifndef TARGET_STM32G4
549+
#if (defined FDCAN_IT_RX_BUFFER_NEW_MESSAGE)
524550
if (__HAL_FDCAN_GET_IT_SOURCE(&CanHandle, FDCAN_IT_RX_BUFFER_NEW_MESSAGE)) {
525551
if (__HAL_FDCAN_GET_FLAG(&CanHandle, FDCAN_IT_RX_BUFFER_NEW_MESSAGE)) {
526552
__HAL_FDCAN_CLEAR_FLAG(&CanHandle, FDCAN_IT_RX_BUFFER_NEW_MESSAGE);
@@ -602,7 +628,7 @@ void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
602628
interrupts = FDCAN_IT_TX_COMPLETE;
603629
break;
604630
case IRQ_RX:
605-
#ifndef TARGET_STM32G4
631+
#if (defined FDCAN_IT_RX_BUFFER_NEW_MESSAGE)
606632
interrupts = FDCAN_IT_RX_BUFFER_NEW_MESSAGE;
607633
#else
608634
interrupts = FDCAN_IT_RX_FIFO0_NEW_MESSAGE;

targets/targets.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4309,6 +4309,7 @@
43094309
},
43104310
"device_has_add": [
43114311
"ANALOGOUT",
4312+
"CAN",
43124313
"CRC",
43134314
"FLASH",
43144315
"MPU",
@@ -4424,6 +4425,7 @@
44244425
},
44254426
"device_has_add": [
44264427
"ANALOGOUT",
4428+
"CAN",
44274429
"CRC",
44284430
"FLASH",
44294431
"MPU",

0 commit comments

Comments
 (0)