mbed official / mbed-src

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Revision:
58:3b55b7a41411
Parent:
57:c7e83bc5e387
Child:
70:c1fbde68b492
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c	Thu Dec 12 18:15:12 2013 +0000
+++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c	Fri Dec 13 09:30:05 2013 +0000
@@ -27,20 +27,13 @@
 #define FLAG_TIMEOUT ((int)0x1000)
 #define LONG_TIMEOUT ((int)0x8000)
 
-// Functions exit codes
-#define EXIT_OK      (0)
-#define EXIT_FAIL    (1)
-#define EXIT_TIMEOUT (0xFFFFFFFF)
-
 static const PinMap PinMap_I2C_SDA[] = {
-    //{PB_7,  I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 0)}, // Cannot be used due to TIM4
-    {PB_9,  I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 7)}, // GPIO_Remap_I2C1
+    {PB_9,  I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 8)}, // GPIO_Remap_I2C1
     {NC,    NC,    0}
 };
 
 static const PinMap PinMap_I2C_SCL[] = {
-    //{PB_6,  I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 0)}, // // Cannot be used due to TIM4
-    {PB_8,  I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 7)}, // GPIO_Remap_I2C1
+    {PB_8,  I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 8)}, // GPIO_Remap_I2C1
     {NC,    NC,    0}
 };
 
@@ -107,17 +100,19 @@
     //while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_MODE_SELECT) == ERROR) {
     while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) {
       if ((timeout--) == 0) {
-          return EXIT_TIMEOUT;
+          return 1;
       }
     }
     
-    return EXIT_OK;
+    return 0;
 }
 
 inline int i2c_stop(i2c_t *obj) {
     I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
+  
     I2C_GenerateSTOP(i2c, ENABLE);
-    return EXIT_OK;
+  
+    return 0;
 }
 
 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
@@ -133,7 +128,7 @@
     timeout = LONG_TIMEOUT;
     while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
         if ((timeout--) == 0) {
-            return EXIT_TIMEOUT;
+            return 0;
         }
     }
 */
@@ -147,7 +142,7 @@
     timeout = FLAG_TIMEOUT;
     while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == ERROR) {
       if ((timeout--) == 0) {
-          return EXIT_TIMEOUT;
+          return 0;
       }
     }
     
@@ -180,7 +175,7 @@
     timeout = LONG_TIMEOUT;
     while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
         if ((timeout--) == 0) {
-            return EXIT_TIMEOUT;
+            return 0;
         }
     }
 */
@@ -194,13 +189,14 @@
     timeout = FLAG_TIMEOUT;
     while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR) {
       if ((timeout--) == 0) {
-          return EXIT_TIMEOUT;
+          return 0;
       }
     }
 
     for (count = 0; count < length; count++) {
-        if (i2c_byte_write(obj, data[count]) != EXIT_OK) {
-            return EXIT_FAIL;
+        if (i2c_byte_write(obj, data[count]) != 1) {
+            i2c_stop(obj);
+            return 0;
         }
     }
 
@@ -229,7 +225,7 @@
     timeout = FLAG_TIMEOUT;
     while (I2C_GetFlagStatus(i2c, I2C_FLAG_RXNE) == RESET) {
       if ((timeout--) == 0) {
-          return EXIT_TIMEOUT;
+          return 0;
       }
     }
 
@@ -250,11 +246,11 @@
     while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) &&
            (I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) {
         if ((timeout--) == 0) {
-            return EXIT_TIMEOUT;
+            return 0;
         }
     }
     
-    return EXIT_OK;
+    return 1;
 }
 
 void i2c_reset(i2c_t *obj) {
@@ -288,29 +284,37 @@
     // Nothing to do
 }
 
-#define        NoData         0
-#define        ReadAddressed  1
-#define        WriteGeneral   2
-#define        WriteAddressed 3
+// See I2CSlave.h
+#define NoData         0 // the slave has not been addressed
+#define ReadAddressed  1 // the master has requested a read from this slave (slave = transmitter)
+#define WriteGeneral   2 // the master is writing to all slave
+#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
 
 int i2c_slave_receive(i2c_t *obj) {
-    //I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
-    int retval = NoData;
-    //int status;
-  
-    //if (I2C_GetFlagStatus(i2c, I2C_FLAG_GENCALL) == SET) retval = WriteGeneral;
-    
-    //status = I2C_GetLastEvent(i2c);
-  
-    return(retval);
+    // TO BE DONE
+    return(0);
 }
 
 int i2c_slave_read(i2c_t *obj, char *data, int length) {
-    return 0;
+    int count = 0;
+ 
+    // Read all bytes
+    for (count = 0; count < length; count++) {
+        data[count] = i2c_byte_read(obj, 0);
+    }
+    
+    return count;
 }
 
 int i2c_slave_write(i2c_t *obj, const char *data, int length) {
-    return 0;
+    int count = 0;
+ 
+    // Write all bytes
+    for (count = 0; count < length; count++) {
+        i2c_byte_write(obj, data[count]);
+    }
+    
+    return count;
 }