mbed library sources

Dependents:   frdm_kl05z_gpio_test

Fork of mbed-src by mbed official

Revision:
80:66393a7b209d
Parent:
70:c1fbde68b492
Child:
167:d5744491c362
diff -r 6fdc4ea80ae0 -r 66393a7b209d targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c
--- a/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c	Thu Jan 30 09:45:05 2014 +0000
+++ b/targets/hal/TARGET_STM/TARGET_NUCLEO_F103RB/i2c_api.c	Thu Jan 30 12:15:05 2014 +0000
@@ -71,10 +71,10 @@
     }
 
     // Configure I2C pins
+    pinmap_pinout(scl, PinMap_I2C_SCL);
+    pin_mode(scl, OpenDrain);
     pinmap_pinout(sda, PinMap_I2C_SDA);
-    pinmap_pinout(scl, PinMap_I2C_SCL);
     pin_mode(sda, OpenDrain);
-    pin_mode(scl, OpenDrain);
     
     // Reset to clear pending flags if any
     i2c_reset(obj);
@@ -88,6 +88,8 @@
     I2C_InitTypeDef I2C_InitStructure;
   
     if ((hz != 0) && (hz <= 400000)) {
+        I2C_DeInit(i2c);
+      
         // I2C configuration
         I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
         I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
@@ -95,8 +97,9 @@
         I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
         I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
         I2C_InitStructure.I2C_ClockSpeed = hz;
+        I2C_Init(i2c, &I2C_InitStructure);
+      
         I2C_Cmd(i2c, ENABLE);
-        I2C_Init(i2c, &I2C_InitStructure);  
     }
 }
 
@@ -113,9 +116,10 @@
     timeout = FLAG_TIMEOUT;
     //while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_MODE_SELECT) == ERROR) {
     while (I2C_GetFlagStatus(i2c, I2C_FLAG_SB) == RESET) {
-      if ((timeout--) == 0) {
-          return 1;
-      }
+        timeout--;
+        if (timeout == 0) {
+            return 1;
+        }
     }
     
     return 0;
@@ -141,7 +145,8 @@
     // Wait until the bus is not busy anymore
     timeout = LONG_TIMEOUT;
     while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
-        if ((timeout--) == 0) {
+        timeout--;
+        if (timeout == 0) {
             return 0;
         }
     }
@@ -155,9 +160,10 @@
     // Wait address is acknowledged
     timeout = FLAG_TIMEOUT;
     while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED) == ERROR) {
-      if ((timeout--) == 0) {
-          return 0;
-      }
+        timeout--;
+        if (timeout == 0) {
+            return 0;
+        }
     }
     
     // Read all bytes except last one
@@ -188,7 +194,8 @@
     // Wait until the bus is not busy anymore
     timeout = LONG_TIMEOUT;
     while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
-        if ((timeout--) == 0) {
+        timeout--;
+        if (timeout == 0) {
             return 0;
         }
     }
@@ -202,9 +209,10 @@
     // Wait address is acknowledged
     timeout = FLAG_TIMEOUT;
     while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED) == ERROR) {
-      if ((timeout--) == 0) {
-          return 0;
-      }
+        timeout--;
+        if (timeout == 0) {
+            return 0;
+        }
     }
 
     for (count = 0; count < length; count++) {
@@ -238,9 +246,10 @@
     // Wait until the byte is received
     timeout = FLAG_TIMEOUT;
     while (I2C_GetFlagStatus(i2c, I2C_FLAG_RXNE) == RESET) {
-      if ((timeout--) == 0) {
-          return 0;
-      }
+        timeout--;
+        if (timeout == 0) {
+            return 0;
+        }
     }
 
     data = I2C_ReceiveData(i2c);
@@ -259,7 +268,8 @@
     //while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_BYTE_TRANSMITTED) == ERROR) {
     while ((I2C_GetFlagStatus(i2c, I2C_FLAG_TXE) == RESET) &&
            (I2C_GetFlagStatus(i2c, I2C_FLAG_BTF) == RESET)) {
-        if ((timeout--) == 0) {
+        timeout--;
+        if (timeout == 0) {
             return 0;
         }
     }