Skip to content

Commit cee180f

Browse files
authored
Merge pull request #5667 from productize/fix-stm32-can3
STM32: Fix CAN3
2 parents 992c905 + f08c04d commit cee180f

File tree

4 files changed

+21
-15
lines changed

4 files changed

+21
-15
lines changed

targets/TARGET_STM/TARGET_STM32F4/can_device.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extern "C" {
2525

2626
#ifdef DEVICE_CAN
2727

28-
#if defined(CAN3_BASE) && defined(CAN_3)
28+
#if defined(CAN3_BASE)
2929

3030
#define CAN_NUM 3 // Number of CAN peripherals present in the STM32 serie
3131

targets/TARGET_STM/TARGET_STM32F7/TARGET_STM32F769xI/TARGET_DISCO_F769NI/PeripheralNames.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ typedef enum {
9494

9595
typedef enum {
9696
CAN_1 = (int)CAN1_BASE,
97-
CAN_2 = (int)CAN2_BASE
97+
CAN_2 = (int)CAN2_BASE,
98+
CAN_3 = (int)CAN3_BASE
9899
} CANName;
99100

100101
#ifdef __cplusplus

targets/TARGET_STM/TARGET_STM32F7/can_device.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ extern "C" {
2525

2626
#ifdef DEVICE_CAN
2727

28-
#if defined(CAN3_BASE) && defined(CAN_3)
28+
#if defined(CAN3_BASE)
2929

3030
#define CAN_NUM 3 // Number of CAN peripherals present in the STM32 serie
3131

targets/TARGET_STM/can_api.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,14 @@ void can_init_freq (can_t *obj, PinName rd, PinName td, int hz)
5757
__HAL_RCC_CAN1_CLK_ENABLE();
5858
obj->index = 0;
5959
}
60-
#if defined(CAN2_BASE) && defined(CAN_2)
60+
#if defined(CAN2_BASE) && (CAN_NUM > 1)
6161
else if (can == CAN_2) {
6262
__HAL_RCC_CAN1_CLK_ENABLE(); // needed to set filters
6363
__HAL_RCC_CAN2_CLK_ENABLE();
6464
obj->index = 1;
6565
}
6666
#endif
67-
#if defined(CAN3_BASE) && defined(CAN_3)
67+
#if defined(CAN3_BASE) && (CAN_NUM > 2)
6868
else if (can == CAN_3) {
6969
__HAL_RCC_CAN3_CLK_ENABLE();
7070
obj->index = 2;
@@ -103,7 +103,13 @@ void can_init_freq (can_t *obj, PinName rd, PinName td, int hz)
103103

104104
can_registers_init(obj);
105105

106+
/* Bits 27:14 are available for dual CAN configuration and are reserved for
107+
single CAN configuration: */
108+
#if defined(CAN3_BASE) && (CAN_NUM > 2)
109+
uint32_t filter_number = (can == CAN_1 || can == CAN_3) ? 0 : 14;
110+
#else
106111
uint32_t filter_number = (can == CAN_1) ? 0 : 14;
112+
#endif
107113
can_filter(obj, 0, 0, CANStandard, filter_number);
108114
}
109115

@@ -132,14 +138,14 @@ void can_free(can_t *obj)
132138
__HAL_RCC_CAN1_RELEASE_RESET();
133139
__HAL_RCC_CAN1_CLK_DISABLE();
134140
}
135-
#if defined(CAN2_BASE) && defined(CAN_2)
141+
#if defined(CAN2_BASE) && (CAN_NUM > 1)
136142
if (can == CAN_2) {
137143
__HAL_RCC_CAN2_FORCE_RESET();
138144
__HAL_RCC_CAN2_RELEASE_RESET();
139145
__HAL_RCC_CAN2_CLK_DISABLE();
140146
}
141147
#endif
142-
#if defined(CAN3_BASE) && defined(CAN_3)
148+
#if defined(CAN3_BASE) && (CAN_NUM > 2)
143149
if (can == CAN_3) {
144150
__HAL_RCC_CAN3_FORCE_RESET();
145151
__HAL_RCC_CAN3_RELEASE_RESET();
@@ -562,7 +568,7 @@ void CAN1_SCE_IRQHandler(void)
562568
{
563569
can_irq(CAN_1, 0);
564570
}
565-
#if defined(CAN2_BASE) && defined(CAN_2)
571+
#if defined(CAN2_BASE) && (CAN_NUM > 1)
566572
void CAN2_RX0_IRQHandler(void)
567573
{
568574
can_irq(CAN_2, 1);
@@ -576,18 +582,18 @@ void CAN2_SCE_IRQHandler(void)
576582
can_irq(CAN_2, 1);
577583
}
578584
#endif
579-
#if defined(CAN3_BASE) && defined(CAN_3)
585+
#if defined(CAN3_BASE) && (CAN_NUM > 2)
580586
void CAN3_RX0_IRQHandler(void)
581587
{
582-
can_irq(CAN_3, 1);
588+
can_irq(CAN_3, 2);
583589
}
584590
void CAN3_TX_IRQHandler(void)
585591
{
586-
can_irq(CAN_3, 1);
592+
can_irq(CAN_3, 2);
587593
}
588594
void CAN3_SCE_IRQHandler(void)
589595
{
590-
can_irq(CAN_3, 1);
596+
can_irq(CAN_3, 2);
591597
}
592598
#endif
593599
#endif // else
@@ -630,7 +636,7 @@ void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
630636
return;
631637
}
632638
}
633-
#if defined(CAN2_BASE) && defined(CAN_2)
639+
#if defined(CAN2_BASE) && (CAN_NUM > 1)
634640
else if ((CANName) can == CAN_2) {
635641
switch (type) {
636642
case IRQ_RX:
@@ -663,7 +669,7 @@ void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
663669
}
664670
}
665671
#endif
666-
#if defined(CAN3_BASE) && defined(CAN_3)
672+
#if defined(CAN3_BASE) && (CAN_NUM > 2)
667673
else if ((CANName) can == CAN_3) {
668674
switch (type) {
669675
case IRQ_RX:
@@ -711,4 +717,3 @@ void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
711717
}
712718

713719
#endif // DEVICE_CAN
714-

0 commit comments

Comments
 (0)