diff --git a/hal/api/CAN.h b/hal/api/CAN.h index db613f6616..241cd4c822 100644 --- a/hal/api/CAN.h +++ b/hal/api/CAN.h @@ -22,7 +22,7 @@ #include "can_api.h" #include "can_helper.h" -#include "FunctionPointer.h" +#include "Callback.h" namespace mbed { @@ -206,34 +206,40 @@ public: /** 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 func 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) */ - void attach(void (*fptr)(void), IrqType type=RxIrq); + void attach(Callback 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 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 (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) */ - template - void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { - if((mptr != NULL) && (tptr != NULL)) { - _irq[type].attach(tptr, mptr); - can_irq_set(&_can, (CanIrqType)type, 1); - } - else { - can_irq_set(&_can, (CanIrqType)type, 0); - } + template + void attach(T* obj, void (T::*method)(), IrqType type=RxIrq) { + attach(Callback(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 (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) + */ + template + void attach(T* obj, void (*method)(T*), IrqType type=RxIrq) { + attach(Callback(obj, method), type); } static void _irq_handler(uint32_t id, CanIrqType type); protected: - can_t _can; - FunctionPointer _irq[9]; + can_t _can; + Callback _irq[9]; }; } // namespace mbed diff --git a/hal/api/CallChain.h b/hal/api/CallChain.h index ebb796a3cd..babdacbf91 100644 --- a/hal/api/CallChain.h +++ b/hal/api/CallChain.h @@ -16,7 +16,7 @@ #ifndef MBED_CALLCHAIN_H #define MBED_CALLCHAIN_H -#include "FunctionPointer.h" +#include "Callback.h" #include namespace mbed { @@ -57,7 +57,7 @@ namespace mbed { * @endcode */ -typedef FunctionPointer* pFunctionPointer_t; +typedef Callback *pFunctionPointer_t; class CallChain { public: @@ -70,34 +70,34 @@ public: /** Add a function at the end of the chain * - * @param function A pointer to a void function + * @param func A pointer to a void function * * @returns - * The function object created for 'function' + * The function object created for 'func' */ - pFunctionPointer_t add(void (*function)(void)); + pFunctionPointer_t add(Callback func); /** Add a function at the end of the chain * - * @param tptr pointer to the object to call the member function on - * @param mptr pointer to the member function to be called + * @param obj pointer to the object to call the member function on + * @param method pointer to the member function to be called * * @returns - * The function object created for 'tptr' and 'mptr' + * The function object created for 'obj' and 'method' */ - template - pFunctionPointer_t add(T *tptr, void (T::*mptr)(void)) { - return common_add(new FunctionPointer(tptr, mptr)); + template + pFunctionPointer_t add(T *obj, M method) { + return add(Callback(obj, method)); } /** Add a function at the beginning of the chain * - * @param function A pointer to a void function + * @param func A pointer to a void function * * @returns - * The function object created for 'function' + * The function object created for 'func' */ - pFunctionPointer_t add_front(void (*function)(void)); + pFunctionPointer_t add_front(Callback func); /** Add a function at the beginning of the chain * @@ -107,9 +107,9 @@ public: * @returns * The function object created for 'tptr' and 'mptr' */ - template - pFunctionPointer_t add_front(T *tptr, void (T::*mptr)(void)) { - return common_add_front(new FunctionPointer(tptr, mptr)); + template + pFunctionPointer_t add_front(T *obj, M method) { + return add_front(Callback(obj, method)); } /** Get the number of functions in the chain @@ -162,8 +162,6 @@ public: private: void _check_size(); - pFunctionPointer_t common_add(pFunctionPointer_t pf); - pFunctionPointer_t common_add_front(pFunctionPointer_t pf); pFunctionPointer_t* _chain; int _size; diff --git a/hal/api/InterruptIn.h b/hal/api/InterruptIn.h index 88bc4308e8..0e519faef4 100644 --- a/hal/api/InterruptIn.h +++ b/hal/api/InterruptIn.h @@ -22,7 +22,7 @@ #include "gpio_api.h" #include "gpio_irq_api.h" -#include "FunctionPointer.h" +#include "Callback.h" namespace mbed { @@ -70,36 +70,34 @@ public: /** Attach a function to call when a rising edge occurs on the input * - * @param fptr A pointer to a void function, or 0 to set as none + * @param func A pointer to a void function, or 0 to set as none */ - void rise(void (*fptr)(void)); + void rise(Callback func); /** Attach a member function to call when a rising edge occurs on the input * - * @param tptr pointer to the object to call the member function on - * @param mptr pointer to the member function to be called + * @param obj pointer to the object to call the member function on + * @param method pointer to the member function to be called */ - template - void rise(T* tptr, void (T::*mptr)(void)) { - _rise.attach(tptr, mptr); - gpio_irq_set(&gpio_irq, IRQ_RISE, 1); + template + void rise(T *obj, M method) { + rise(Callback(obj, method)); } /** Attach a function to call when a falling edge occurs on the input * - * @param fptr A pointer to a void function, or 0 to set as none + * @param func A pointer to a void function, or 0 to set as none */ - void fall(void (*fptr)(void)); + void fall(Callback func); /** Attach a member function to call when a falling edge occurs on the input * - * @param tptr pointer to the object to call the member function on - * @param mptr pointer to the member function to be called + * @param obj pointer to the object to call the member function on + * @param method pointer to the member function to be called */ - template - void fall(T* tptr, void (T::*mptr)(void)) { - _fall.attach(tptr, mptr); - gpio_irq_set(&gpio_irq, IRQ_FALL, 1); + template + void fall(T *obj, M method) { + fall(Callback(obj, method)); } /** Set the input pin mode @@ -124,8 +122,8 @@ protected: gpio_t gpio; gpio_irq_t gpio_irq; - FunctionPointer _rise; - FunctionPointer _fall; + Callback _rise; + Callback _fall; }; } // namespace mbed diff --git a/hal/api/SerialBase.h b/hal/api/SerialBase.h index 51aeb33e36..ed2e744790 100644 --- a/hal/api/SerialBase.h +++ b/hal/api/SerialBase.h @@ -21,7 +21,7 @@ #if DEVICE_SERIAL #include "Stream.h" -#include "FunctionPointer.h" +#include "Callback.h" #include "serial_api.h" #if DEVICE_SERIAL_ASYNCH @@ -89,25 +89,31 @@ public: /** Attach a function to call whenever a serial interrupt is generated * - * @param fptr A pointer to a void function, or 0 to set as none + * @param func A pointer to a void function, or 0 to set as none * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) */ - void attach(void (*fptr)(void), IrqType type=RxIrq); + void attach(Callback func, IrqType type=RxIrq); /** Attach a member function to call whenever a serial 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 obj pointer to the object to call the member function on + * @param method pointer to the member function to be called * @param type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) */ template - void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) { - if((mptr != NULL) && (tptr != NULL)) { - _irq[type].attach(tptr, mptr); - serial_irq_set(&_serial, (SerialIrq)type, 1); - } else { - serial_irq_set(&_serial, (SerialIrq)type, 0); - } + void attach(T *obj, void (T::*method)(), IrqType type=RxIrq) { + attach(Callback(obj, method), type); + } + + /** Attach a member function to call whenever a serial 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 type Which serial interrupt to attach the member function to (Seriall::RxIrq for receive, TxIrq for transmit buffer empty) + */ + template + void attach(T *obj, void (*method)(T*), IrqType type=RxIrq) { + attach(Callback(obj, method), type); } /** Generate a break condition on the serial line @@ -210,9 +216,9 @@ protected: DMAUsage _rx_usage; #endif - serial_t _serial; - FunctionPointer _irq[2]; - int _baud; + serial_t _serial; + Callback _irq[2]; + int _baud; }; diff --git a/hal/api/Ticker.h b/hal/api/Ticker.h index fda205da9a..817394039b 100644 --- a/hal/api/Ticker.h +++ b/hal/api/Ticker.h @@ -17,7 +17,7 @@ #define MBED_TICKER_H #include "TimerEvent.h" -#include "FunctionPointer.h" +#include "Callback.h" namespace mbed { @@ -65,22 +65,22 @@ public: /** Attach a function to be called by the Ticker, specifiying the interval in seconds * - * @param fptr pointer to the function to be called + * @param func pointer to the function to be called * @param t the time between calls in seconds */ - void attach(void (*fptr)(void), float t) { - attach_us(fptr, t * 1000000.0f); + void attach(Callback func, float t) { + attach_us(func, t * 1000000.0f); } /** Attach a member function to be called by the Ticker, specifiying the interval in seconds * - * @param tptr pointer to the object to call the member function on - * @param mptr pointer to the member function to be called + * @param obj pointer to the object to call the member function on + * @param method pointer to the member function to be called * @param t the time between calls in seconds */ - template - void attach(T* tptr, void (T::*mptr)(void), float t) { - attach_us(tptr, mptr, t * 1000000.0f); + template + void attach(T *obj, M method, float t) { + attach(Callback(obj, method), t); } /** Attach a function to be called by the Ticker, specifiying the interval in micro-seconds @@ -88,8 +88,8 @@ public: * @param fptr pointer to the function to be called * @param t the time between calls in micro-seconds */ - void attach_us(void (*fptr)(void), timestamp_t t) { - _function.attach(fptr); + void attach_us(Callback func, timestamp_t t) { + _function.attach(func); setup(t); } @@ -99,10 +99,9 @@ public: * @param mptr pointer to the member function to be called * @param t the time between calls in micro-seconds */ - template - void attach_us(T* tptr, void (T::*mptr)(void), timestamp_t t) { - _function.attach(tptr, mptr); - setup(t); + template + void attach_us(T *obj, M method, timestamp_t t) { + attach_us(Callback(obj, method), t); } virtual ~Ticker() { @@ -118,8 +117,8 @@ protected: virtual void handler(); protected: - timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */ - FunctionPointer _function; /**< Callback. */ + timestamp_t _delay; /**< Time delay (in microseconds) for re-setting the multi-shot callback. */ + Callback _function; /**< Callback. */ }; } // namespace mbed diff --git a/hal/common/CAN.cpp b/hal/common/CAN.cpp index 407e00bf17..b717f80461 100644 --- a/hal/common/CAN.cpp +++ b/hal/common/CAN.cpp @@ -67,9 +67,9 @@ int CAN::filter(unsigned int id, unsigned int mask, CANFormat format, int handle return can_filter(&_can, id, mask, format, handle); } -void CAN::attach(void (*fptr)(void), IrqType type) { - if (fptr) { - _irq[(CanIrqType)type].attach(fptr); +void CAN::attach(Callback func, IrqType type) { + if (func) { + _irq[(CanIrqType)type].attach(func); can_irq_set(&_can, (CanIrqType)type, 1); } else { can_irq_set(&_can, (CanIrqType)type, 0); diff --git a/hal/common/CallChain.cpp b/hal/common/CallChain.cpp index e950903059..4c4a9cba4d 100644 --- a/hal/common/CallChain.cpp +++ b/hal/common/CallChain.cpp @@ -12,12 +12,19 @@ CallChain::~CallChain() { delete _chain; } -pFunctionPointer_t CallChain::add(void (*function)(void)) { - return common_add(new FunctionPointer(function)); +pFunctionPointer_t CallChain::add(Callback func) { + _check_size(); + _chain[_elements] = new Callback(func); + _elements ++; + return _chain[_elements]; } -pFunctionPointer_t CallChain::add_front(void (*function)(void)) { - return common_add_front(new FunctionPointer(function)); +pFunctionPointer_t CallChain::add_front(Callback func) { + _check_size(); + memmove(_chain + 1, _chain, _elements * sizeof(pFunctionPointer_t)); + _chain[0] = new Callback(func); + _elements ++; + return _chain[0]; } int CallChain::size() const { @@ -72,19 +79,4 @@ void CallChain::_check_size() { _chain = new_chain; } -pFunctionPointer_t CallChain::common_add(pFunctionPointer_t pf) { - _check_size(); - _chain[_elements] = pf; - _elements ++; - return pf; -} - -pFunctionPointer_t CallChain::common_add_front(pFunctionPointer_t pf) { - _check_size(); - memmove(_chain + 1, _chain, _elements * sizeof(pFunctionPointer_t)); - _chain[0] = pf; - _elements ++; - return pf; -} - } // namespace mbed diff --git a/hal/common/InterruptIn.cpp b/hal/common/InterruptIn.cpp index 92f70f2a5e..414529869f 100644 --- a/hal/common/InterruptIn.cpp +++ b/hal/common/InterruptIn.cpp @@ -39,9 +39,9 @@ void InterruptIn::mode(PinMode pull) { gpio_mode(&gpio, pull); } -void InterruptIn::rise(void (*fptr)(void)) { - if (fptr) { - _rise.attach(fptr); +void InterruptIn::rise(Callback func) { + if (func) { + _rise.attach(func); gpio_irq_set(&gpio_irq, IRQ_RISE, 1); } else { _rise.attach(NULL); @@ -49,9 +49,9 @@ void InterruptIn::rise(void (*fptr)(void)) { } } -void InterruptIn::fall(void (*fptr)(void)) { - if (fptr) { - _fall.attach(fptr); +void InterruptIn::fall(Callback func) { + if (func) { + _fall.attach(func); gpio_irq_set(&gpio_irq, IRQ_FALL, 1); } else { _fall.attach(NULL); diff --git a/hal/common/InterruptManager.cpp b/hal/common/InterruptManager.cpp index e92fb68d4e..183404719f 100644 --- a/hal/common/InterruptManager.cpp +++ b/hal/common/InterruptManager.cpp @@ -66,13 +66,6 @@ bool InterruptManager::remove_handler(pFunctionPointer_t handler, IRQn_Type irq) return false; if (!_chains[irq_pos]->remove(handler)) return false; - // If there's a single function left in the chain, swith the interrupt vector - // to call that function directly. This way we save both time and space. - if (_chains[irq_pos]->size() == 1 && NULL != _chains[irq_pos]->get(0)->get_function()) { - NVIC_SetVector(irq, (uint32_t)_chains[irq_pos]->get(0)->get_function()); - delete _chains[irq_pos]; - _chains[irq_pos] = (CallChain*) NULL; - } return true; } diff --git a/hal/common/SerialBase.cpp b/hal/common/SerialBase.cpp index 880a0212e6..94eca327d9 100644 --- a/hal/common/SerialBase.cpp +++ b/hal/common/SerialBase.cpp @@ -48,9 +48,9 @@ int SerialBase::writeable() { return serial_writable(&_serial); } -void SerialBase::attach(void (*fptr)(void), IrqType type) { - if (fptr) { - _irq[type].attach(fptr); +void SerialBase::attach(Callback func, IrqType type) { + if (func) { + _irq[type].attach(func); serial_irq_set(&_serial, (SerialIrq)type, 1); } else { serial_irq_set(&_serial, (SerialIrq)type, 0);