Skip to content

Commit f740d2f

Browse files
committed
Add an mbed API that allows the initialization of the CAN at the correct
CAN bus frequency
1 parent 5ebe295 commit f740d2f

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

drivers/CAN.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ CAN::CAN(PinName rd, PinName td) : _can(), _irq() {
3434
can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this);
3535
}
3636

37+
CAN::CAN(PinName rd, PinName td, int f) : _can(), _irq() {
38+
// No lock needed in constructor
39+
40+
for (int i = 0; i < sizeof _irq / sizeof _irq[0]; i++) {
41+
_irq[i].attach(donothing);
42+
}
43+
44+
can_init_freq(&_can, rd, td, f);
45+
can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this);
46+
}
47+
3748
CAN::~CAN() {
3849
// No lock needed in destructor
3950
can_irq_free(&_can);

drivers/CAN.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,15 @@ class CAN {
111111
* @endcode
112112
*/
113113
CAN(PinName rd, PinName td);
114+
115+
/** Initialize CAN interface and set the frequency
116+
*
117+
* @param rd the rd pin
118+
* @param td the td pin
119+
* @param f the bus frequency in hertz
120+
*/
121+
CAN(PinName rd, PinName td, int f);
122+
114123
virtual ~CAN();
115124

116125
/** Set the frequency of the CAN interface

hal/can_api.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ typedef void (*can_irq_handler)(uint32_t id, CanIrqType type);
5757

5858
typedef struct can_s can_t;
5959

60-
void can_init (can_t *obj, PinName rd, PinName td);
61-
void can_free (can_t *obj);
62-
int can_frequency(can_t *obj, int hz);
60+
void can_init (can_t *obj, PinName rd, PinName td);
61+
void can_init_freq (can_t *obj, PinName rd, PinName td, int hz);
62+
void can_free (can_t *obj);
63+
int can_frequency (can_t *obj, int hz);
6364

6465
void can_irq_init (can_t *obj, can_irq_handler handler, uint32_t id);
6566
void can_irq_free (can_t *obj);

0 commit comments

Comments
 (0)