General thunking class to allow C-style callbacks to C++ class members - including optional parameters.
Revision 9:d3b51b06ac54, committed 2014-08-21
- Comitter:
- meriac
- Date:
- Thu Aug 21 17:09:11 2014 +0000
- Parent:
- 8:12fc2cda71af
- Commit message:
- Added CThunkIRQ class
Changed in this revision
diff -r 12fc2cda71af -r d3b51b06ac54 CThunk.h --- a/CThunk.h Wed Aug 20 14:37:44 2014 +0000 +++ b/CThunk.h Thu Aug 21 17:09:11 2014 +0000 @@ -89,23 +89,28 @@ { m_thunk.context = context; } + + inline uint32_t entry(void) + { + return (((uint32_t)&m_thunk)|CTHUNK_ADDRESS); + } /* get thunk entry point for connecting rhunk to an IRQ table */ inline operator CThunkEntry(void) { - return (CThunkEntry)(((uint32_t)&m_thunk)|CTHUNK_ADDRESS); + return (CThunkEntry)entry(); } /* get thunk entry point for connecting rhunk to an IRQ table */ inline operator uint32_t(void) { - return (uint32_t)&m_thunk; + return entry(); } /* simple test function */ inline void call(void) { - ((CThunkEntry)(((uint32_t)&m_thunk)|CTHUNK_ADDRESS))(); + ((CThunkEntry)(entry())(); } private:
diff -r 12fc2cda71af -r d3b51b06ac54 CThunkIRQ.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CThunkIRQ.h Thu Aug 21 17:09:11 2014 +0000 @@ -0,0 +1,93 @@ +/* General C++ Object Thunking class + * + * - allows direct callbacks to non-static C++ class functions + * - keeps track for the corresponding class instance + * - supports an optional context parameter for the called function + * - ideally suited for class object receiving interrupts (NVIC_SetVector) + * + * Copyright (c) 2014 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __CTHUNKIRQ_H__ +#define __CTHUNKIRQ_H__ + +#include "CThunk.h" + +template<class T> +class CThunkIRQ: CThunk<T> { + +public: + typedef void (T::*CCallbackSimple)(void); + typedef void (T::*CCallback)(void* context); + + inline CThunkIRQ(T *instance, IRQn_Type IRQn) + :CThunk<T>(instance) + { + register_irq(IRQn); + } + + inline CThunkIRQ(T *instance, IRQn_Type IRQn, CCallback callback) + :CThunk<T>(instance, callback) + { + register_irq(IRQn); + } + + inline CThunkIRQ(T *instance, IRQn_Type IRQn, CCallbackSimple callback) + :CThunk<T>(instance, callback) + { + register_irq(IRQn); + } + + inline CThunkIRQ(T &instance, IRQn_Type IRQn, CCallback callback) + :CThunk<T>(instance, callback) + { + register_irq(IRQn); + } + + inline CThunkIRQ(T &instance, IRQn_Type IRQn, CCallbackSimple callback) + :CThunk<T>(instance, callback) + { + register_irq(IRQn); + } + + inline CThunkIRQ(T &instance, IRQn_Type IRQn, CCallback callback, void* context) + :CThunk<T>(instance, callback, context) + { + register_irq(IRQn); + } + + inline ~CThunkIRQ(void) + { + NVIC_DisableIRQ(m_irqn); + NVIC_SetVector(m_irqn, m_prev_handler); + } + +private: + IRQn_Type m_irqn; + uint32_t m_prev_handler; + inline void register_irq(IRQn_Type IRQn) + { + /* remember IRQn for destructor */ + m_irqn = IRQn; + + /* update IRQ handler atomically */ + NVIC_DisableIRQ(IRQn); + m_prev_handler = NVIC_GetVector(IRQn); + NVIC_SetVector(IRQn, CThunk<T>::entry()); + NVIC_EnableIRQ(IRQn); + } +}; + +#endif/*__CTHUNKIRQ_H__*/
diff -r 12fc2cda71af -r d3b51b06ac54 thunk.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/thunk.h Thu Aug 21 17:09:11 2014 +0000 @@ -0,0 +1,29 @@ +/* General C++ Object Thunking classes + * + * - allows direct callbacks to non-static C++ class functions + * - keeps track for the corresponding class instance + * - supports an optional context parameter for the called function + * - ideally suited for class object receiving interrupts (NVIC_SetVector) + * + * Copyright (c) 2014 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + #ifndef __THUNK_H__ + #define __THUNK_H__ + + #include "CThunk.h" + #include "CThunkIRQ.h" + + #endif/*__THUNK_H__*/ \ No newline at end of file