pub
Fork of CANnucleo by
Diff: CANnucleo.h
- Revision:
- 25:353237492903
- Parent:
- 24:c5d348e65e24
- Child:
- 28:eed6929956ea
diff -r c5d348e65e24 -r 353237492903 CANnucleo.h --- a/CANnucleo.h Sat Aug 13 12:44:12 2016 +0000 +++ b/CANnucleo.h Tue Aug 16 21:09:11 2016 +0000 @@ -22,7 +22,8 @@ #include "platform.h" #include "cannucleo_api.h" #include "can_helper.h" -#include "FunctionPointer.h" +#include "Callback.h" +#include "PlatformMutex.h" #undef DEVICE_CAN namespace CANnucleo @@ -97,7 +98,7 @@ } #if DEBUG else { - printf("Error: Cannot append data because it exceeds CAN data length!\r\n"); + printf("Error: Cannot append data. Exceeding CAN data length!\r\n"); } #endif return *this; @@ -114,7 +115,7 @@ } #if DEBUG else { - printf("Error: Cannot extract data because it exceeds CAN data length!\r\n"); + printf("Error: Cannot extract data. Exceeding CAN data length!\r\n"); } #endif return *this; @@ -276,30 +277,45 @@ /** Attach a function to call whenever a CAN frame received interrupt is * generated. * - * @param fptr A pointer to a void function, or 0 to set as none - * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, CAN::TxIrq for transmitted or aborted, CAN::EwIrq for error warning, CAN::DoIrq for data overrun, CAN::WuIrq for wake-up, CAN::EpIrq for error passive, CAN::AlIrq for arbitration lost, CAN::BeIrq for bus error) + * @param func A pointer to a void function, or 0 to set as none + * @param event Which CAN interrupt to attach the member function to (only CAN::RxIrq for message received is supported) */ - void attach(void (*fptr)(void), IrqType type=RxIrq); + void attach(mbed::Callback<void()> func, IrqType type=RxIrq); - /** Attach a member function to call whenever a CAN frame received interrupt - * is generated. - * - * @param tptr pointer to the object to call the member function on - * @param mptr pointer to the member function to be called - * @param event Which CAN interrupt to attach the member function to (CAN::RxIrq for message received, TxIrq for transmitted or aborted, EwIrq for error warning, DoIrq for data overrun, WuIrq for wake-up, EpIrq for error passive, AlIrq for arbitration lost, BeIrq for bus error) - */ + /** Attach a member function to call whenever a CAN frame received interrupt + * is generated. + * + * @param obj pointer to the object to call the member function on + * @param method pointer to the member function to be called + * @param event Which CAN interrupt to attach the member function to (only CAN::RxIrq for message received is supported) + */ template<typename T> - void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { - HAL_NVIC_DisableIRQ(CAN_IRQ); - if((tptr != NULL) && (mptr != NULL)) - _irq[type].attach(tptr, mptr); - HAL_NVIC_EnableIRQ(CAN_IRQ); + void attach(T* obj, void (T::*method)(), IrqType type=RxIrq) { + // Underlying call thread safe + attach(Callback<void()>(obj, method), type); } + /** Attach a member function to call whenever a CAN frame received interrupt + * is generated. + * + * @param obj pointer to the object to call the member function on + * @param method pointer to the member function to be called + * @param event Which CAN interrupt to attach the member function to (only CAN::RxIrq for message received is supported) + */ + template<typename T> + void attach(T* obj, void (*method)(T*), IrqType type=RxIrq) { + // Underlying call thread safe + attach(Callback<void()>(obj, method), type); + } + + static void _irq_handler(uint32_t id, CanIrqType type); protected: - mbed::FunctionPointer _irq[9]; + virtual void lock(); + virtual void unlock(); + mbed::Callback<void()> _irq[9]; + PlatformMutex _mutex; }; } // namespace CANnucleo