mFS file system library for EEPROM memory chips.

Revision:
7:5ac5121bb4e0
Parent:
5:a0fe74dce80d
Child:
9:52c01cb100ac
--- a/i2c_eeprom.cpp	Mon Feb 21 18:34:40 2011 +0000
+++ b/i2c_eeprom.cpp	Mon Feb 21 22:37:00 2011 +0000
@@ -1,19 +1,19 @@
 /** @file i2c_eeprom.cpp */
 /*CPP**************************************************************************
-* FILENAME :        i2c_eeprom.cpp                                            *
-*                                                                             *
-* DESCRIPTION :                                                               *
-*       Simple library for external I2C EEEPROM.                              *
-*                                                                             *
-* AUTHOR :    Olli Vanhoja        START DATE :    2011-02-17                  *
-******************************************************************************/
+ * FILENAME :        i2c_eeprom.cpp                                           *
+ *                                                                            *
+ * DESCRIPTION :                                                              *
+ *       Simple library for external I2C EEEPROM.                             *
+ *                                                                            *
+ * AUTHOR :    Olli Vanhoja        START DATE :    2011-02-17                 *
+ *****************************************************************************/
 
 #include "mbed.h"
 #include "i2c_eeprom.h"
 
-I2C i2c(p28, p27);
-DigitalOut BusyLed(LED1);
-DigitalInOut scl_ext(p20); /** \attention Clock override pin connected to SCL */
+I2C i2c(p28, p27); /**< I2C */
+DigitalOut BusyLed(LED1);  /**< Busy led */
+DigitalInOut scl_ext(p20); /**< \attention Clock override pin connected to SCL */
 
 i2c_eeprom::i2c_eeprom(int hwAddr, int speed)
 {
@@ -52,8 +52,12 @@
         i2c_data[2] = *pi2c_data[2];
         
         // Send write command
+        strwrite:
         if(i2c.write(i_i2c_address, i2c_data, 3))
-            error("Write failed!\n\r");
+        {
+            autoreset();
+            goto strwrite;
+        }
         
         iAddr++; // increment address counter
 
@@ -82,27 +86,34 @@
     i2c_data[1] = *pi2c_data[1];
     
     // Send read command
-    srread:
+    strread:
     if(i2c.write(i_i2c_address, i2c_data, 2))
     {
-        i2c.start();
-        scl_ext = 0;          // Setup override pin to pull clock low
-        scl_ext.input();      // Make it input to start with...
-        scl_ext.mode(PullUp); // ...with pull up
-        wait(0.00005);        // Pause after stop
-        scl_ext.output();     // Override clock pin low
-        wait(0.00005);        // Pause
-        scl_ext.input();      // Remove override...
-        scl_ext.mode(PullUp); // ...with pull up
-        wait(0.00005);        // Pause again
-
-        i2c.start();
-        i2c.stop();
-        goto srread;
+        autoreset();
+        goto strread;
     }
     
     if(i2c.read(i_i2c_address, out, n))
-        error("Read failed!\n\r");
+    {
+        autoreset();
+        goto strread;
+    }
     
     BusyLed = 0;
 }
+
+void i2c_eeprom::autoreset()
+{
+    i2c.start();
+    scl_ext = 0;          // Setup override pin to pull clock low
+    scl_ext.input();      // Make it input to start with...
+    scl_ext.mode(PullUp); // ...with pull up
+    wait(0.00005);        // Pause after stop
+    scl_ext.output();     // Override clock pin low
+    wait(0.00005);        // Pause
+    scl_ext.input();      // Remove override...
+    scl_ext.mode(PullUp); // ...with pull up
+    wait(0.00005);        // Pause again
+    i2c.start();
+    i2c.stop();
+}
\ No newline at end of file