diff --git a/drivers/CAN.cpp b/drivers/CAN.cpp index 93d6f6315d..219b6d0c29 100644 --- a/drivers/CAN.cpp +++ b/drivers/CAN.cpp @@ -34,6 +34,17 @@ CAN::CAN(PinName rd, PinName td) : _can(), _irq() { can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); } +CAN::CAN(PinName rd, PinName td, int f) : _can(), _irq() { + // No lock needed in constructor + + for (int i = 0; i < sizeof _irq / sizeof _irq[0]; i++) { + _irq[i].attach(donothing); + } + + can_init_freq(&_can, rd, td, f); + can_irq_init(&_can, (&CAN::_irq_handler), (uint32_t)this); +} + CAN::~CAN() { // No lock needed in destructor can_irq_free(&_can); diff --git a/drivers/CAN.h b/drivers/CAN.h index 9a81e9c787..edbb39228c 100644 --- a/drivers/CAN.h +++ b/drivers/CAN.h @@ -111,6 +111,15 @@ public: * @endcode */ CAN(PinName rd, PinName td); + + /** Initialize CAN interface and set the frequency + * + * @param rd the rd pin + * @param td the td pin + * @param f the bus frequency in hertz + */ + CAN(PinName rd, PinName td, int f); + virtual ~CAN(); /** Set the frequency of the CAN interface diff --git a/hal/can_api.h b/hal/can_api.h index 2a9182187e..723342f082 100644 --- a/hal/can_api.h +++ b/hal/can_api.h @@ -57,9 +57,10 @@ typedef void (*can_irq_handler)(uint32_t id, CanIrqType type); typedef struct can_s can_t; -void can_init (can_t *obj, PinName rd, PinName td); -void can_free (can_t *obj); -int can_frequency(can_t *obj, int hz); +void can_init (can_t *obj, PinName rd, PinName td); +void can_init_freq (can_t *obj, PinName rd, PinName td, int hz); +void can_free (can_t *obj); +int can_frequency (can_t *obj, int hz); void can_irq_init (can_t *obj, can_irq_handler handler, uint32_t id); void can_irq_free (can_t *obj);