Improvements to Olieman's MODI2C library. Supports calls from IRQ.

Dependencies:   FPointer

Dependents:   FreeIMU FreeIMU_external_magnetometer FreeIMU

Fork of MODI2C by Erik -

Revision:
1:eed116eb680a
Parent:
0:ff579e7e8efa
Child:
3:eb8120aa14fd
--- a/MODI2C.h	Sat Jun 30 14:55:14 2012 +0000
+++ b/MODI2C.h	Sat Nov 09 08:59:02 2013 +0000
@@ -1,4 +1,4 @@
-/* 
+/*
 
                             .       .
                            / `.   .' \
@@ -21,7 +21,8 @@
 Face it, dinos much cooler than copyright notices.*/
 
 #include "mbed.h"
-
+#include "FPointer.h"
+#include "rtos.h"
 
 #ifndef MODI2C_H
 #define MODI2C_H
@@ -33,14 +34,14 @@
 #define I2C_FLAG            3
 #define I2C_ASSERT_ACK      2
 
-#define IRQ_I2C_BOTH        0
-#define IRQ_I2C_READ        1
-#define IRQ_I2C_WRITE       2
+#define IRQ_I2C_BOTH        1
+#define IRQ_I2C_READ        2
+#define IRQ_I2C_WRITE       3
 
 
 
 #ifndef I2C_BUFFER
-#define I2C_BUFFER          10
+#define I2C_BUFFER          20
 #endif
 
 
@@ -54,19 +55,19 @@
   * #include "mbed.h"
   * #include "MODI2C.h"
   * #include "MPU6050.h"
-  * 
+  *
   * Serial pc(USBTX, USBRX); // tx, rx
   * MODI2C mod(p9, p10);
-  * 
+  *
   * int main() {
   *     char registerAdd = MPU6050_WHO_AM_I_REG;
   *     char registerResult;
   *     int status;
-  * 
+  *
   *     while (1) {
   *         mod.write(MPU6050_ADDRESS*2, &registerAdd, 1, true);
   *         mod.read_nb(MPU6050_ADDRESS*2, &registerResult, 1, &status);
-  * 
+  *
   *         while (!status) wait_us(1);
   *         pc.printf("Register holds 0x%02X\n\r", registerResult);
   *         wait(2);
@@ -74,7 +75,8 @@
   * }
   * @endcode
   */
-class MODI2C {
+class MODI2C
+{
 public:
     /**
     * Constructor.
@@ -98,8 +100,9 @@
     * @param status - (optional) pointer to integer where the final status code of the I2C transmission is placed. (0x28 is success)
     * @param return - returns zero
     */
-    int write(int address, char *data, int length, bool repeated = false, int *status = NULL);
+    int write(int address, char *data, int length, uint32_t(*function)(uint32_t) = NULL, void *pass_to_irq = NULL, bool repeated = false, int *status = NULL);
     int write(int address, char *data, int length, int *status);
+    int write(int address, char *data, int length, bool repeated);
 
     /**
     * Read data non-blocking from the I2C bus.
@@ -114,9 +117,8 @@
     * @param status - (optional) pointer to integer where the final status code of the I2C transmission is placed. (0x58 is success)
     * @param return - returns zero
     */
-    int read_nb(int address, char *data, int length, bool repeated = false, int *status=NULL);
-    int read_nb(int address, char *data, int length, int *status);
-    
+    int read_nb(int address, char *data, int length, uint32_t(*function)(uint32_t) = NULL, void *pass_to_irq = NULL, bool repeated = false, int *status=NULL);
+
     /**
     * Read data from the I2C bus.
     *
@@ -150,41 +152,6 @@
     * If you use this function you probably break something (but mbed also had it public)
     */
     void stop ( void );
-    
-    /**
-    * Removes attached function
-    */
-    void detach( void );
-    
-    /**
-    * Calls user function when I2C command is finished
-    *
-    * @param function - the function to call.
-    * @param operation - when to call IRQ: IRQ_I2C_BOTH (default) - IRQ_I2C_READ - IRQ_I2C_WRITE
-    */
-    void attach(void (*function)(void), int operation = IRQ_I2C_BOTH);
-    
-    /**
-    * Calls user function when I2C command is finished
-    *
-    * @param object - the object to call the function on.
-    * @param member - the function to call
-    * @param operation - when to call IRQ: IRQ_I2C_BOTH (default) - IRQ_I2C_READ - IRQ_I2C_WRITE
-    */
-    template<typename T>
-        void attach(T *object, void (T::*member)(void), int operation = IRQ_I2C_BOTH);
-        
-    /**
-    * Returns the current number of commands in the queue (including one currently being processed)
-    *
-    * Note that this is the number of commands, not the number of bytes
-    *
-    * @param return - number of commands in queue
-    */
-    int getQueue( void );
-
-
-private:
 
     struct I2CData {
         MODI2C *caller;
@@ -193,41 +160,48 @@
         int  length;
         bool repeated;
         int *status;
+        FPointer callback;
+        uint32_t pass_to_irq;
+        int IRQOp;
+        volatile char *monitor_addr;
     };
-    
+
+private:
     struct I2CBuffer {
-        int queue;
+        //volatile int queue;
         int count;
-        I2CData Data[I2C_BUFFER];
-        };
+        volatile I2CData *curr;
+        MemoryPool<I2CData, I2C_BUFFER> pool;
+        //I2CData Data[I2C_BUFFER];
+        Queue<I2CData, I2C_BUFFER> queue;
+        volatile bool crit_flag_isr;
+        volatile bool crit_flag;
         
+        I2CBuffer():count(0),curr(NULL),crit_flag_isr(false),crit_flag(false){}
+    };
+
     //Settings:
     int duty;
-    
-    FunctionPointer callback;
-    int IRQOp;   
-    void runUserIRQ( I2CData Data );
+
+    void runUserIRQ( I2CData *Data );
 
     //Remove later:
     LPC_I2C_TypeDef *I2CMODULE;
 
-    
-
     //Whole bunch of static stuff, pretty much everything that is ever called from ISR
     static I2CBuffer Buffer1;
     static I2CBuffer Buffer2;
-    
+
     static void IRQHandler(I2CBuffer *Buffer, LPC_I2C_TypeDef *I2CMODULE);
     static void IRQ1Handler(void);
     static void IRQ2Handler(void);
-    
+
     static void bufferHandler(LPC_I2C_TypeDef *I2CMODULE);
-    static bool addBuffer(I2CData Data, LPC_I2C_TypeDef *I2CMODULE);
-    static bool removeBuffer(LPC_I2C_TypeDef *I2CMODULE);
+    static bool addBuffer(I2CData *Data, LPC_I2C_TypeDef *I2CMODULE);
     static void startBuffer(LPC_I2C_TypeDef *I2CMODULE);
 
     static int defaultStatus;
-    
+
     static void _start(LPC_I2C_TypeDef *I2CMODULE);
     static void _stop(LPC_I2C_TypeDef *I2CMODULE);
     static void _clearISR( LPC_I2C_TypeDef *I2CMODULE );