General thunking class to allow C-style callbacks to C++ class members - including optional parameters.

Dependents:   cthunk_example

Revision:
5:df90d98292a6
Parent:
4:e4a106e8f3fe
Child:
6:ef94278e2225
--- a/CThunk.h	Wed Aug 20 10:44:31 2014 +0000
+++ b/CThunk.h	Wed Aug 20 12:18:33 2014 +0000
@@ -23,6 +23,13 @@
 #ifndef __CTHUNK_H__
 #define __CTHUNK_H__
 
+#ifdef __CORTEX_M3
+#  define CTHUNK_OPCODE 0x8007E89F;
+#  define CTHUNK_ADDRESS 1
+#else
+#  error "TODO: add support for non-cortex-m3 trampoline, too"
+#endif
+
 /* IRQ/Exception compatible thunk entry function */
 typedef void (*CThunkEntry)(void);
 typedef void (*CThunkCallback)(void* instance, void* context);
@@ -39,6 +46,16 @@
             init(instance, NULL, NULL);
         }
 
+        inline CThunk(T *instance, CCallback callback)
+        {
+            init(instance, callback, NULL);
+        }
+
+        inline CThunk(T *instance, CCallbackSimple callback)
+        {
+            init(instance, (CCallback)callback, NULL);
+        }
+
         inline CThunk(T &instance, CCallback callback)
         {
             init(instance, callback, NULL);
@@ -78,7 +95,7 @@
         inline operator CThunkEntry(void)
         {
             /* TODO: check thumb */
-            return (CThunkEntry)(((uint32_t)&m_thunk)|1);
+            return (CThunkEntry)(((uint32_t)&m_thunk)|CTHUNK_ADDRESS);
         }
 
         /* get thunk entry point for connecting rhunk to an IRQ table */
@@ -91,7 +108,7 @@
         inline void call(void)
         {
             /* TODO: check thumb */
-            ((CThunkEntry)(((uint32_t)&m_thunk)|1))();
+            ((CThunkEntry)(((uint32_t)&m_thunk)|CTHUNK_ADDRESS))();
         }
 
     private:
@@ -117,14 +134,12 @@
 
         inline void init(T *instance, CCallback callback, void* context)
         {
-#ifdef __CORTEX_M3
-            m_thunk.code = 0x8007E89F;
-#else
-#error "TODO: add support for non-cortex-m3 trampoline, too"
-#endif
+            /* remember callback - need to add this level of redirection
+               as pointer size for member functions differs between platforms */
             m_callback = callback;
 
             /* populate thunking trampoline */
+            m_thunk.code = CTHUNK_OPCODE;
             m_thunk.context = (uint32_t)context;
             m_thunk.instance = (uint32_t)instance;
             m_thunk.callback = (uint32_t)&m_callback;