I2CRTOS Driver by Helmut Schmücker. Removed included mbed-rtos library to prevent multiple definition. Make sure to include mbed-rtos library in your program!

Fork of I2cRtosDriver by Helmut Schmücker

Revision:
13:530968937ccb
Parent:
9:65aae53a34de
Child:
14:352609d395c1
--- a/I2CDriver.h	Fri May 10 07:34:24 2013 +0000
+++ b/I2CDriver.h	Fri May 10 20:38:35 2013 +0000
@@ -5,7 +5,6 @@
 
 #include "I2C.h"
 
-#include "Thread.h"
 #include "Mutex.h"
 
 namespace mbed
@@ -115,7 +114,7 @@
      *  general call address.
      */
     void addressSlave(int address) {
-        m_slaveAdr=address;
+        m_slaveAdr=(address & 0xff) | 1;
     }
 
     /** Checks to see if this I2C Slave has been addressed.
@@ -137,6 +136,7 @@
      *  @returns
      *       0 on success,
      *   non-0 otherwise
+     * ... no! instead it returns number of bytes read minus one ... weird, guess its a bug in the official lib
      */
     int readSlave(char *data, int length);
 
@@ -154,7 +154,7 @@
      *
      *  @returns
      *       0 on success,
-     *   non-0 otherwise
+     *   non-0 otherwise 
      */
     int writeSlave(const char *data, int length);
 
@@ -175,8 +175,10 @@
     ///Creates a stop condition on the I2C bus
     void stopSlave(void);
 
-    ///Creates a stop condition on the I2C bus
-    void stopMaster(void);
+    /// Creates a stop condition on the I2C bus
+    /// If unsccessful because someone on the bus holds the scl line down it returns "false" after 23µs 
+    /// In normal operation the stop shouldn't take longer than 12µs @ 100kHz and 3-4µs @ 400kHz. 
+    bool stopMaster(void);
 
     /// Wait until the i2c driver becomes available.
     ///
@@ -189,52 +191,30 @@
     void unlock();
 
 protected:
-    // commands sent from user to drive thread
-    enum Command {
-        START,
-        STOP,
-        READ_MST,
-        READ_MST_REG,
-        READ_SLV,
-        READ_BYTE,
-        WRITE_MST,
-        WRITE_SLV,
-        WRITE_BYTE,
-        RECEIVE
+    void config();
+    void lockNconfig() {
+        lock();
+        config();
+    }
+
+    // structure that holds I2C channels status
+    struct Channel {
+        rtos::Mutex mutex;
+        i2c_t i2c;
+        int freq;
+        int slaveAdr;
+        bool modeSlave;
     };
 
-    // data transfer struct for communication between user and driver thread
-    struct Transfer {
-        Command cmd;
-        int ret;
-        int freq;
-        int adr;
-        char* dta;
-        const char* wdta;
-        int len;
-        int ack;
-        bool rep;
-        uint8_t reg;
-        bool slv;
-        uint32_t tmout;
-        osThreadId caller;
-    };
+    // curren i2c configuration of this driver interface
+    int m_freq;
+    int m_slaveAdr;
+    bool m_modeSlave;
 
-    // structure that holds handles/locks for accessing the I2C channels
-    struct Channel {
-        osThreadId driver;
-        rtos::Mutex  mutex;
-        volatile Transfer transfer;
-    };
-
-    // current frequency setting
-    int m_freq;
-    // current slave address setting
-    int m_slaveAdr;
-    // prio of current caller thread
+    // id and prio of current caller thread
+    osThreadId m_callerID;
     osPriority m_callerPrio;
-    // ID of current caller thread
-    osThreadId m_callerID;
+    
 
     // i2c driver prio
     static const osPriority c_drvPrio = osPriorityRealtime;
@@ -245,14 +225,9 @@
     // static storage for the I2C channel access objects
     static Channel* s_channels[2];
 
-    // i2c channel object of this driver interface, in fact just pointer
+    // i2c channel object of this driver interface, in fact just a pointer
     /// to one of the entries in s_channels
     Channel* m_channel;
-
-    // the driver thread function
-    static void threadFun(void const *args);
-
-    int sendNwait();
 };
 }
 #endif