Skip to content

Commit 114e019

Browse files
authored
Merge pull request #5486 from bcostm/fix_CAN3
STM32: Add support of CAN3
2 parents d6d9afc + 2d8d8ae commit 114e019

File tree

8 files changed

+117
-19
lines changed

8 files changed

+117
-19
lines changed

targets/TARGET_STM/TARGET_STM32F0/can_device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
#define MBED_CAN_DEVICE_H
1818

1919
#include "cmsis.h"
20-
#include "stm32f0xx_hal.h"
20+
#include "stm32f0xx.h"
2121

2222
#ifdef __cplusplus
2323
extern "C" {
2424
#endif
2525

2626
#ifdef DEVICE_CAN
2727

28-
#define CAN_NUM 1 // Number of CAN peripherals present in the STM32 serie
28+
#define CAN_NUM 1 // Number of CAN peripherals present in the STM32 serie
2929

3030
#define CAN1_IRQ_RX_IRQN CEC_CAN_IRQn
3131
#define CAN1_IRQ_RX_VECT CAN_IRQHandler

targets/TARGET_STM/TARGET_STM32F1/can_device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
#define MBED_CAN_DEVICE_H
1818

1919
#include "cmsis.h"
20-
#include "stm32f1xx_hal.h"
20+
#include "stm32f1xx.h"
2121

2222
#ifdef __cplusplus
2323
extern "C" {
2424
#endif
2525

2626
#ifdef DEVICE_CAN
2727

28-
#define CAN_NUM 1 // Number of CAN peripherals present in the STM32 serie
28+
#define CAN_NUM 1 // Number of CAN peripherals present in the STM32 serie
2929

3030
#define CAN1_IRQ_RX_IRQN CAN1_RX0_IRQn
3131
#define CAN1_IRQ_RX_VECT CAN1_RX0_IRQHandler

targets/TARGET_STM/TARGET_STM32F2/can_device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
#define MBED_CAN_DEVICE_H
1818

1919
#include "cmsis.h"
20-
#include "stm32f2xx_hal.h"
20+
#include "stm32f2xx.h"
2121

2222
#ifdef __cplusplus
2323
extern "C" {
2424
#endif
2525

2626
#ifdef DEVICE_CAN
2727

28-
#define CAN_NUM 2 // Number of CAN peripherals present in the STM32 serie (1 or 2)
28+
#define CAN_NUM 2 // Number of CAN peripherals present in the STM32 serie
2929

3030
#define CAN1_IRQ_RX_IRQN CAN1_RX0_IRQn
3131
#define CAN1_IRQ_RX_VECT CAN1_RX0_IRQHandler

targets/TARGET_STM/TARGET_STM32F3/can_device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
#define MBED_CAN_DEVICE_H
1818

1919
#include "cmsis.h"
20-
#include "stm32f3xx_hal.h"
20+
#include "stm32f3xx.h"
2121

2222
#ifdef __cplusplus
2323
extern "C" {
2424
#endif
2525

2626
#ifdef DEVICE_CAN
2727

28-
#define CAN_NUM 1 // Number of CAN peripherals present in the STM32 serie
28+
#define CAN_NUM 1 // Number of CAN peripherals present in the STM32 serie
2929

3030
#define CAN1_IRQ_RX_IRQN CAN_RX0_IRQn
3131
#define CAN1_IRQ_RX_VECT CAN_RX0_IRQHandler

targets/TARGET_STM/TARGET_STM32F4/can_device.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,34 @@
1717
#define MBED_CAN_DEVICE_H
1818

1919
#include "cmsis.h"
20-
#include "stm32f4xx_hal.h"
20+
#include "stm32f4xx.h"
2121

2222
#ifdef __cplusplus
2323
extern "C" {
2424
#endif
2525

2626
#ifdef DEVICE_CAN
2727

28-
#define CAN_NUM 2 // Number of CAN peripherals present in the STM32 serie (1 or 2)
28+
#if defined(CAN3_BASE) && defined(CAN_3)
29+
30+
#define CAN_NUM 3 // Number of CAN peripherals present in the STM32 serie
31+
32+
#define CAN3_IRQ_RX_IRQN CAN3_RX0_IRQn
33+
#define CAN3_IRQ_RX_VECT CAN3_RX0_IRQHandler
34+
#define CAN3_IRQ_TX_IRQN CAN3_TX_IRQn
35+
#define CAN3_IRQ_TX_VECT CAN3_TX_IRQHandler
36+
#define CAN3_IRQ_ERROR_IRQN CAN3_SCE_IRQn
37+
#define CAN3_IRQ_ERROR_VECT CAN3_SCE_IRQHandler
38+
#define CAN3_IRQ_PASSIVE_IRQN CAN3_SCE_IRQn
39+
#define CAN3_IRQ_PASSIVE_VECT CAN3_SCE_IRQHandler
40+
#define CAN3_IRQ_BUS_IRQN CAN3_SCE_IRQn
41+
#define CAN3_IRQ_BUS_VECT CAN3_SCE_IRQHandler
42+
43+
#else
44+
45+
#define CAN_NUM 2 // Number of CAN peripherals present in the STM32 serie
46+
47+
#endif
2948

3049
#define CAN1_IRQ_RX_IRQN CAN1_RX0_IRQn
3150
#define CAN1_IRQ_RX_VECT CAN1_RX0_IRQHandler

targets/TARGET_STM/TARGET_STM32F7/can_device.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,34 @@
1717
#define MBED_CAN_DEVICE_H
1818

1919
#include "cmsis.h"
20-
#include "stm32f7xx_hal.h"
20+
#include "stm32f7xx.h"
2121

2222
#ifdef __cplusplus
2323
extern "C" {
2424
#endif
2525

2626
#ifdef DEVICE_CAN
2727

28-
#define CAN_NUM 2 // Number of CAN peripherals present in the STM32 serie (1 or 2)
28+
#if defined(CAN3_BASE) && defined(CAN_3)
29+
30+
#define CAN_NUM 3 // Number of CAN peripherals present in the STM32 serie
31+
32+
#define CAN3_IRQ_RX_IRQN CAN3_RX0_IRQn
33+
#define CAN3_IRQ_RX_VECT CAN3_RX0_IRQHandler
34+
#define CAN3_IRQ_TX_IRQN CAN3_TX_IRQn
35+
#define CAN3_IRQ_TX_VECT CAN3_TX_IRQHandler
36+
#define CAN3_IRQ_ERROR_IRQN CAN3_SCE_IRQn
37+
#define CAN3_IRQ_ERROR_VECT CAN3_SCE_IRQHandler
38+
#define CAN3_IRQ_PASSIVE_IRQN CAN3_SCE_IRQn
39+
#define CAN3_IRQ_PASSIVE_VECT CAN3_SCE_IRQHandler
40+
#define CAN3_IRQ_BUS_IRQN CAN3_SCE_IRQn
41+
#define CAN3_IRQ_BUS_VECT CAN3_SCE_IRQHandler
42+
43+
#else
44+
45+
#define CAN_NUM 2 // Number of CAN peripherals present in the STM32 serie
46+
47+
#endif
2948

3049
#define CAN1_IRQ_RX_IRQN CAN1_RX0_IRQn
3150
#define CAN1_IRQ_RX_VECT CAN1_RX0_IRQHandler

targets/TARGET_STM/TARGET_STM32L4/can_device.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@
1717
#define MBED_CAN_DEVICE_H
1818

1919
#include "cmsis.h"
20-
#include "stm32l4xx_hal.h"
20+
#include "stm32l4xx.h"
2121

2222
#ifdef __cplusplus
2323
extern "C" {
2424
#endif
2525

2626
#ifdef DEVICE_CAN
2727

28-
#define CAN_NUM 1 // Number of CAN peripherals present in the STM32 serie
28+
#define CAN_NUM 1 // Number of CAN peripherals present in the STM32 serie
2929

3030
#define CAN1_IRQ_RX_IRQN CAN1_RX0_IRQn
3131
#define CAN1_IRQ_RX_VECT CAN1_RX0_IRQHandler

targets/TARGET_STM/can_api.c

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,18 @@ 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) && (CAN_NUM == 2)
60+
#if defined(CAN2_BASE) && defined(CAN_2)
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
}
66+
#endif
67+
#if defined(CAN3_BASE) && defined(CAN_3)
68+
else if (can == CAN_3) {
69+
__HAL_RCC_CAN3_CLK_ENABLE();
70+
obj->index = 2;
71+
}
6672
#endif
6773
else {
6874
return;
@@ -126,13 +132,20 @@ void can_free(can_t *obj)
126132
__HAL_RCC_CAN1_RELEASE_RESET();
127133
__HAL_RCC_CAN1_CLK_DISABLE();
128134
}
129-
#if defined(CAN2_BASE) && (CAN_NUM == 2)
135+
#if defined(CAN2_BASE) && defined(CAN_2)
130136
if (can == CAN_2) {
131137
__HAL_RCC_CAN2_FORCE_RESET();
132138
__HAL_RCC_CAN2_RELEASE_RESET();
133139
__HAL_RCC_CAN2_CLK_DISABLE();
134140
}
135141
#endif
142+
#if defined(CAN3_BASE) && defined(CAN_3)
143+
if (can == CAN_3) {
144+
__HAL_RCC_CAN3_FORCE_RESET();
145+
__HAL_RCC_CAN3_RELEASE_RESET();
146+
__HAL_RCC_CAN3_CLK_DISABLE();
147+
}
148+
#endif
136149
}
137150

138151
// The following table is used to program bit_timing. It is an adjustment of the sample
@@ -549,7 +562,7 @@ void CAN1_SCE_IRQHandler(void)
549562
{
550563
can_irq(CAN_1, 0);
551564
}
552-
#if defined(CAN2_BASE) && (CAN_NUM == 2)
565+
#if defined(CAN2_BASE) && defined(CAN_2)
553566
void CAN2_RX0_IRQHandler(void)
554567
{
555568
can_irq(CAN_2, 1);
@@ -562,7 +575,21 @@ void CAN2_SCE_IRQHandler(void)
562575
{
563576
can_irq(CAN_2, 1);
564577
}
565-
#endif // defined(CAN2_BASE) && (CAN_NUM == 2)
578+
#endif
579+
#if defined(CAN3_BASE) && defined(CAN_3)
580+
void CAN3_RX0_IRQHandler(void)
581+
{
582+
can_irq(CAN_3, 1);
583+
}
584+
void CAN3_TX_IRQHandler(void)
585+
{
586+
can_irq(CAN_3, 1);
587+
}
588+
void CAN3_SCE_IRQHandler(void)
589+
{
590+
can_irq(CAN_3, 1);
591+
}
592+
#endif
566593
#endif // else
567594

568595
void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
@@ -603,7 +630,7 @@ void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
603630
return;
604631
}
605632
}
606-
#if defined(CAN2_BASE) && (CAN_NUM == 2)
633+
#if defined(CAN2_BASE) && defined(CAN_2)
607634
else if ((CANName) can == CAN_2) {
608635
switch (type) {
609636
case IRQ_RX:
@@ -635,6 +662,39 @@ void can_irq_set(can_t *obj, CanIrqType type, uint32_t enable)
635662
return;
636663
}
637664
}
665+
#endif
666+
#if defined(CAN3_BASE) && defined(CAN_3)
667+
else if ((CANName) can == CAN_3) {
668+
switch (type) {
669+
case IRQ_RX:
670+
ier = CAN_IT_FMP0;
671+
irq_n = CAN3_IRQ_RX_IRQN;
672+
vector = (uint32_t)&CAN3_IRQ_RX_VECT;
673+
break;
674+
case IRQ_TX:
675+
ier = CAN_IT_TME;
676+
irq_n = CAN3_IRQ_TX_IRQN;
677+
vector = (uint32_t)&CAN3_IRQ_TX_VECT;
678+
break;
679+
case IRQ_ERROR:
680+
ier = CAN_IT_ERR;
681+
irq_n = CAN3_IRQ_ERROR_IRQN;
682+
vector = (uint32_t)&CAN3_IRQ_ERROR_VECT;
683+
break;
684+
case IRQ_PASSIVE:
685+
ier = CAN_IT_EPV;
686+
irq_n = CAN3_IRQ_PASSIVE_IRQN;
687+
vector = (uint32_t)&CAN3_IRQ_PASSIVE_VECT;
688+
break;
689+
case IRQ_BUS:
690+
ier = CAN_IT_BOF;
691+
irq_n = CAN3_IRQ_BUS_IRQN;
692+
vector = (uint32_t)&CAN3_IRQ_BUS_VECT;
693+
break;
694+
default:
695+
return;
696+
}
697+
}
638698
#endif
639699
else {
640700
return;

0 commit comments

Comments
 (0)