mbed library sources

Dependents:   Encrypted my_mbed lklk CyaSSL_DTLS_Cellular ... more

Superseded

This library was superseded by mbed-dev - https://os.mbed.com/users/mbed_official/code/mbed-dev/.

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Revision:
601:248b0d2dd755
Parent:
576:99a3d3d9c43f
Child:
619:034e698bc035
--- a/targets/hal/TARGET_WIZNET/TARGET_W7500x/i2c_api.c	Fri Jul 31 14:15:09 2015 +0100
+++ b/targets/hal/TARGET_WIZNET/TARGET_W7500x/i2c_api.c	Fri Jul 31 15:00:09 2015 +0100
@@ -47,303 +47,135 @@
 #define FLAG_TIMEOUT ((int)0x1000)
 #define LONG_TIMEOUT ((int)0xFFFF)
 
-I2C_TypeDef * I2cHandle;
-
-int i2c0_inited = 0;
-int i2c1_inited = 0;
 
 void i2c_init(i2c_t *obj, PinName sda, PinName scl)
 {
-    // Determine the I2C to use
+    I2C_ConfigStruct conf;
+    
+    //Determine the I2C to use
     I2CName i2c_sda = (I2CName)pinmap_peripheral(sda, PinMap_I2C_SDA);
     I2CName i2c_scl = (I2CName)pinmap_peripheral(scl, PinMap_I2C_SCL);
-
-    obj->i2c = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
-    MBED_ASSERT(obj->i2c != (I2CName)NC);
-
-    // Enable I2C1 clock and pinout if not done
-    if ((obj->i2c == I2C_0) && !i2c0_inited) {
-        i2c0_inited = 1;
-        // Configure I2C pins
-        pinmap_pinout(sda, PinMap_I2C_SDA);
-        pinmap_pinout(scl, PinMap_I2C_SCL);
-        pin_mode(sda, PullUp);
-        pin_mode(scl, PullUp);
-    }
-
-    // Enable I2C2 clock and pinout if not done
-    if ((obj->i2c == I2C_1) && !i2c1_inited) {
-        i2c1_inited = 1;
-        // Configure I2C pins
-        pinmap_pinout(sda, PinMap_I2C_SDA);
-        pinmap_pinout(scl, PinMap_I2C_SCL);
-        pin_mode(sda, PullUp);
-        pin_mode(scl, PullUp);
-    }
-
-    // Reset to clear pending flags if any
-    i2c_reset(obj);
-
-    // I2C configuration
-    i2c_frequency(obj, 100000); // 100 kHz per default
+    obj->I2Cx = (I2CName)pinmap_merge(i2c_sda, i2c_scl);
+    MBED_ASSERT(obj->I2Cx != (I2CName)NC);
+    
+    obj->sda = sda;
+    obj->scl = scl;
+    obj->ADDRESS = 0x0;
     obj->is_setAddress = 0;
+    
+    conf.sda = (I2C_PinName)obj->sda;
+    conf.scl = (I2C_PinName)obj->scl;
+    
+    I2C_Init(&conf);
 }
 
 void i2c_frequency(i2c_t *obj, int hz)
 {
-    //MBED_ASSERT((hz == 100000) || (hz == 400000) || (hz == 1000000));
-    MBED_ASSERT((hz == 100000));
-    I2cHandle = (I2C_TypeDef *)(obj->i2c);
-
-    // wait before init
-    I2C_ConfigStruct conf;
-
-    conf.mode = I2C_Master;
-    conf.master.timeout = LONG_TIMEOUT;
-    conf.master.prescale  = 0x61;      // Standard mode with Rise Time = 400ns and Fall Time = 100ns
-
-    // Common settings: I2C clock = 48 MHz, Analog filter = ON, Digital filter coefficient = 0
-//    switch (hz) {
-//        case 100000:
-//            conf.master.prescale  = 0x61;      // Standard mode with Rise Time = 400ns and Fall Time = 100ns
-//            break;
-//        case 400000:
-//            break;
-//        case 1000000:
-//            break;
-//        default:
-//            break;
-//    }
-
-    // I2C configuration
-    I2C_Init(I2cHandle, conf);
+    
 }
 
 inline int i2c_start(i2c_t *obj)
 {
-    obj->is_setAddress = 0;
-
+    I2C_ConfigStruct conf;
+    conf.sda = (I2C_PinName)obj->sda;
+    conf.scl = (I2C_PinName)obj->scl;
+    
+    I2C_Start(&conf);
+    
     return 0;
 }
 
 inline int i2c_stop(i2c_t *obj)
-{
-    I2cHandle = (I2C_TypeDef *)(obj->i2c);
-
-    // Generate the STOP condition
-    I2C_Stop(I2cHandle);
-    I2C_Reset(I2cHandle);
+{  
+    I2C_ConfigStruct conf;
+    conf.sda = (I2C_PinName)obj->sda;
+    conf.scl = (I2C_PinName)obj->scl;
     
-    obj->is_setAddress = 0;
+    I2C_Stop(&conf);
    
     return 0;
 }
 
 int i2c_read(i2c_t *obj, int address, char *data, int length, int stop)
 {
-    I2cHandle = (I2C_TypeDef *)(obj->i2c);
-    int count;
-    int value;
+    I2C_ConfigStruct conf;
+
+    conf.sda = (I2C_PinName)obj->sda;
+    conf.scl = (I2C_PinName)obj->scl;
     
-    if(!obj->is_setAddress)
-    {
-       if( I2C_Start(I2cHandle, address, I2C_READ_SA7) == ERROR )
-        {
-              return -1;
-        }
-       obj->is_setAddress = 1;
-       obj->ADDRESS = address;
+    
+    if(stop)
+    {  
+        if(I2C_Read(&conf, address,  data, length) != 0)
+            return -1;
     }
     else
     {
-        I2C_Restart_Structure(I2cHandle, address, I2C_READ_SA7);
-        obj->ADDRESS = address;
-    }
-
-    // Read all bytes
-    for (count = 0; count < (length-1); count++) {
-        if( (value = i2c_byte_read(obj, 0)) == -1) {
-              return value;
-        }
-        data[count] = (char)value;
-    }
-
-    if(stop){
-        if( (value = i2c_byte_read(obj, 1)) == -1) {
-            return value;
-        }
-        data[count] = (char)value;
-      
-        i2c_stop(obj);
-        obj->is_setAddress =1;
-        count++;
-      }
-    else{
-        if( (value = i2c_byte_read(obj, 0)) == -1) {
-     
-            return value;
-        }
-        data[count] = (char)value;
-        count++;
+        if(I2C_ReadRepeated(&conf, address,  data, length) != 0)
+            return -1;
     }
     
-    return count;
+    return length;
     
 }
 
 int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop)
 {
-    I2cHandle = (I2C_TypeDef *)(obj->i2c);
-    int count =0;
+    I2C_ConfigStruct conf;
+    
+    conf.sda = (I2C_PinName)obj->sda;
+    conf.scl = (I2C_PinName)obj->scl;
+    
     
-    if(!obj->is_setAddress)
-    {
-       if( I2C_Start(I2cHandle, address, I2C_WRITE_SA7) == ERROR )
-        {
-                    return -1;
-        }
-       obj->is_setAddress = 1;
-       obj->ADDRESS = address;
-      }
+    if(stop)
+    {  
+        if(I2C_Write(&conf, address,  data, length) != 0)
+           return -1;
+    }
     else
     {
-        I2C_Restart_Structure(I2cHandle, address, I2C_WRITE_SA7);
-        obj->ADDRESS = address;
-      
+        if(I2C_WriteRepeated(&conf, address,  data, length) != 0)
+           return -1;
     }
-
-    for (count = 0; count < length; count++) {
-        i2c_byte_write(obj, data[count]);
-        wait_us(1);
-    }
-
     
-    if(length == 0x00)
-    {
-        I2C_GPIO();
-        i2c_byte_write(obj, 0xff);
-        GPIO_I2C();
-    }
-    // If not repeated start, send stop
-    if (stop) {
-        i2c_stop(obj);
-      }
-    else
-    {
-        i2c_reset(obj);
-        
-    }
-        return count;
+    return length;
 }
 
 int i2c_byte_read(i2c_t *obj, int last)
 {
-    I2cHandle = (I2C_TypeDef *)(obj->i2c);
-
-    return I2C_ReceiveData(I2cHandle, last);
+    uint8_t ret;
+    I2C_ConfigStruct conf;
+    
+    conf.sda = (I2C_PinName)obj->sda;
+    conf.scl = (I2C_PinName)obj->scl;
+    
+    ret = I2C_ReadByte(&conf);
+    
+    if(last)
+        I2C_SendNACK(&conf);
+    else
+        I2C_SendACK(&conf);
+    
+    return (int)ret;
 }
 
 int i2c_byte_write(i2c_t *obj, int data)
 {
-    I2cHandle = (I2C_TypeDef *)(obj->i2c);
-    return I2C_SendDataAck(I2cHandle,(uint8_t)data);
+    I2C_ConfigStruct conf;
+    
+    conf.sda = (I2C_PinName)obj->sda;
+    conf.scl = (I2C_PinName)obj->scl;
+    
+    if(I2C_WriteByte(&conf, (uint8_t)data)) // NACK
+        return 0;
+    else //ack
+        return 1;
 }
 
 void i2c_reset(i2c_t *obj)
 {
-    I2cHandle = (I2C_TypeDef *)(obj->i2c);
-
-    I2C_Reset(I2cHandle);
+   
 }
 
-//#if DEVICE_I2CSLAVE
-//
-//void i2c_slave_address(i2c_t *obj, int idx, uint32_t address, uint32_t mask)
-//{
-//    I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
-//    uint16_t tmpreg = 0;
-//
-//    // disable
-//    i2c->OAR1 &= (uint32_t)(~I2C_OAR1_OA1EN);
-//    // Get the old register value
-//    tmpreg = i2c->OAR1;
-//    // Reset address bits
-//    tmpreg &= 0xFC00;
-//    // Set new address
-//    tmpreg |= (uint16_t)((uint16_t)address & (uint16_t)0x00FE); // 7-bits
-//    // Store the new register value
-//    i2c->OAR1 = tmpreg;
-//    // enable
-//    i2c->OAR1 |= I2C_OAR1_OA1EN;
-//}
-//
-//void i2c_slave_mode(i2c_t *obj, int enable_slave)
-//{
-//    I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
-//    uint16_t tmpreg;
-//
-//    // Get the old register value
-//    tmpreg = i2c->OAR1;
-//
-//    // Enable / disable slave
-//    if (enable_slave == 1) {
-//        tmpreg |= I2C_OAR1_OA1EN;
-//    } else {
-//        tmpreg &= (uint32_t)(~I2C_OAR1_OA1EN);
-//    }
-//
-//    // Set new mode
-//    i2c->OAR1 = tmpreg;
-//}
-//
-//// 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)
-//{
-//    I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
-//    int retValue = NoData;
-//
-//    if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_BUSY) == 1) {
-//        if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_ADDR) == 1) {
-//            if (__HAL_I2C_GET_FLAG(&I2cHandle, I2C_FLAG_DIR) == 1)
-//                retValue = ReadAddressed;
-//            else
-//                retValue = WriteAddressed;
-//
-//            __HAL_I2C_CLEAR_FLAG(&I2cHandle, I2C_FLAG_ADDR);
-//        }
-//    }
-//
-//    return (retValue);
-//}
-//
-//int i2c_slave_read(i2c_t *obj, char *data, int length)
-//{
-//    char size = 0;
-//
-//    while (size < length) data[size++] = (char)i2c_byte_read(obj, 0);
-//
-//    return size;
-//}
-//
-//int i2c_slave_write(i2c_t *obj, const char *data, int length)
-//{
-//    char size = 0;
-//    I2cHandle.Instance = (I2C_TypeDef *)(obj->i2c);
-//
-//    do {
-//        i2c_byte_write(obj, data[size]);
-//        size++;
-//    } while (size < length);
-//
-//    return size;
-//}
-//
-//
-//#endif // DEVICE_I2CSLAVE
 
 #endif // DEVICE_I2C