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:
576:99a3d3d9c43f
Parent:
558:0880f51c4036
Child:
601:248b0d2dd755
--- a/targets/cmsis/TARGET_WIZNET/TARGET_W7500x/W7500x_i2c.c	Wed Jun 24 09:45:13 2015 +0100
+++ b/targets/cmsis/TARGET_WIZNET/TARGET_W7500x/W7500x_i2c.c	Tue Jun 30 09:45:08 2015 +0100
@@ -4,6 +4,13 @@
   * @{
   */
 
+GPIO_InitTypeDef GPIO_InitDef;
+void i2c_loop_us(int us);
+
+#define SCL GPIO_Pin_9
+#define SDA GPIO_Pin_10
+uint16_t buf[] ={0x00,0x01};
+
 /**
   * @brief  Initializes the I2Cx peripheral according to the specified 
   *   parameters in the I2C_InitStruct.
@@ -12,10 +19,6 @@
   *   contains the configuration information for the specified I2C peripheral.
   * @retval None
   */
-void I2C_Delay(uint32_t nCount)
-{
-    for(; nCount != 0; nCount--);
-}
 
 uint32_t I2C_Init(I2C_TypeDef* I2Cx, I2C_ConfigStruct conf)
 {
@@ -67,7 +70,7 @@
 ErrorStatus I2C_Start(I2C_TypeDef* I2Cx, uint16_t slave_address, I2C_CTR ctr)
 {
     ErrorStatus ret;
-
+          
     I2C_GenerateSTART(I2Cx,ENABLE);
     I2C_SendSlaveAddress(I2Cx,slave_address,(I2C_CTR)ctr);
     I2C_GenerateSTART(I2Cx,DISABLE);
@@ -81,6 +84,10 @@
 {
     I2C_GenerateSTOP(I2Cx,ENABLE);
     I2C_GenerateSTOP(I2Cx,DISABLE);
+    GPIO_InitDef.GPIO_Pin = GPIO_Pin_9; // Set to Pin_9 (SCL0))
+    GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT; // Set to Mode Output
+    HAL_GPIO_Init(GPIOA, &GPIO_InitDef);
+    HAL_PAD_AFConfig(PAD_PA,GPIO_Pin_9, PAD_AF0); // PAD Config - LED used 2nd Function
 }
 
 void I2C_Reset(I2C_TypeDef* I2Cx)
@@ -97,28 +104,38 @@
 
 int8_t I2C_SendDataAck(I2C_TypeDef* I2Cx,uint16_t Data)
 {
-    I2Cx -> TXR = (uint16_t)Data;
-    if(I2C_CheckEvent(I2Cx,I2C_ACKR) == ERROR)
+    buf[0] = Data;
+    if(buf[0] == buf[1])
     {
-        return ERROR;
-    }           
+        I2C_GPIO();
+        WriteByte(Data);     
+        i2c_loop_us(1);
+        GPIO_I2C();
+    }
+    else
+    {
+        I2Cx -> TXR = (uint16_t)Data;
+        if(I2C_CheckEvent(I2Cx,I2C_ACKR) == ERROR)
+        {
+            return ERROR;
+        }           
+    }
+    buf[1] = buf[0];
     return SUCCESS;
 }
 
 int I2C_ReceiveData(I2C_TypeDef* I2Cx, int last)    
 {
-    if( I2C_CheckEvent(I2Cx,I2C_ACKT) == ERROR )
-        return -1;    
-     
     if(last)
     {   
         I2C_AcknowledgeConfig(I2Cx,DISABLE);
-        if( I2C_CheckEvent(I2Cx,I2C_ACKT) == ERROR )
+        if( I2C_CheckEvent(I2Cx,I2C_ACKT) == ERROR ) {
             return -1; 
-
-        I2C_Stop(I2Cx);
-    }   
-
+        }
+    }
+    else if( I2C_CheckEvent(I2Cx,I2C_ACKT) == ERROR ) {
+            return -1;    
+    }
      
     return (uint8_t)I2Cx -> RXR;    
 }
@@ -177,8 +194,14 @@
   */
 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
 {
-    if(NewState != DISABLE)     I2Cx->CMDR = I2C_CMDR_STA;
-    else                        I2Cx->CMDR = I2C_CMDR_STA;
+    if(NewState != DISABLE)     
+    {    
+        I2Cx->CMDR = I2C_CMDR_STA;  
+    }
+    else                        
+    {
+        I2Cx->CMDR = I2C_CMDR_STA;
+    }
  }
 /**
   * @brief  Generates I2Cx communication STOP condition.
@@ -189,8 +212,16 @@
   */
 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
 {
-    if(NewState != DISABLE)     I2Cx->CMDR = I2C_CMDR_STO;
-    else                        I2Cx->CMDR = I2C_CMDR_STO;
+    if(NewState != DISABLE)     
+    {   
+
+        I2Cx->CMDR = I2C_CMDR_STO;
+
+    }
+    else               
+    {
+        I2Cx->CMDR = I2C_CMDR_STO;
+    }
 }
 
 /**
@@ -408,81 +439,6 @@
 }
 
 
-/*
-void I2C_MasterInit(I2C_TypeDef *  I2Cx,uint8_t Prescale,uint16_t Timeout,I2C_CTR Ctr)       
-{
-    switch(Ctr)
-    {
-        case(I2C_WRITE_SA7):     //000
-        case(I2C_READ_SA7):      //001
-            I2C_CoreEn(I2Cx,ENABLE);
-        break;
-        
-        case(I2C_WRITE_SA10):     //010
-        case(I2C_READ_SA10):      //011
-            I2C_CoreEn(I2Cx,ENABLE);
-            I2C_SlaveAddressLength(I2Cx,ENABLE);
-        break;
-        
-        case(I2C_CTRWRITE_SA7):     //100
-            I2C_CoreEn(I2Cx,ENABLE);
-            I2C_ControlEn(I2Cx,ENABLE);
-        break;
-    
-        case(I2C_CTRREAD_SA7):      //101
-            I2C_CoreEn(I2Cx,ENABLE);
-            I2C_ControlRW(I2Cx,ENABLE);
-            I2C_ControlEn(I2Cx,ENABLE);
-        break;
-
-        case(I2C_CTRWRITE_SA10):    //110
-            I2C_CoreEn(I2Cx,ENABLE);
-            I2C_ControlRW(I2Cx,DISABLE);
-            I2C_ControlEn(I2Cx,ENABLE);
-            I2C_SlaveAddressLength(I2Cx,ENABLE);
-        break;
-        case(I2C_CTRREAD_SA10):     //111
-            I2C_CoreEn(I2Cx,ENABLE);
-            I2C_ControlRW(I2Cx,ENABLE);
-            I2C_ControlEn(I2Cx,ENABLE);
-            I2C_SlaveAddressLength(I2Cx,ENABLE);
-        break;
-        
-        default:
-            return;
-        
-     }     
-
-    I2C_MasterSlave(I2Cx,ENABLE);
-    I2C_MasterSlave(I2Cx,DISABLE);
-    I2C_MasterSlave(I2Cx,ENABLE);
-    
-    I2C_Prescale(I2Cx,Prescale);            // 0x61         //When PLL clk is 20MHz and Prescale value set 0x61, SCL is 100KHz
-  	I2C_TimeoutSet(I2Cx,Timeout);          // 0xFFFF        
-  	 
-#if defined(I2C_INT)
-    I2C_CoreEn(I2Cx,DISABLE);
-    I2C_InterEn(I2Cx,ENABLE);
-#else
-    I2C_CoreEn(I2Cx,DISABLE);
-  
-#endif
-
-}	
- 
-void I2C_SlaveInit(I2C_TypeDef * I2Cx, FunctionalState NewState,uint16_t data)
-{
-    if(NewState != DISABLE)
-    {
-        I2C_SlaveAddressLength(I2Cx,ENABLE);
-    }   
-    else
-        I2C_AcknowledgeConfig(I2Cx,ENABLE);
-
-    I2C_SetSlavAddress(I2Cx,data);
-}
-*/	
- 
 void I2C_SendSlaveAddress(I2C_TypeDef* I2Cx, uint8_t SlaveAddress,I2C_CTR Ctr)
 {
     switch(Ctr)
@@ -495,19 +451,9 @@
         case(I2C_WRITE_SA7):
             I2C_SendData(I2Cx,SlaveAddress|I2C_WRITE);
         break;
-        
-//        case(I2C_READ_SA10):
-//            I2C_SendData(I2Cx,SlaveAddress|I2C_READ);
-//        break;
-//
-//        case(I2C_WRITE_SA10):
-//            I2C_SendData(I2Cx,SlaveAddress|I2C_WRITE);
-//        break;
-        
+ 
         case(I2C_CTRWRITE_SA7):
         case(I2C_CTRREAD_SA7):
-//        case(I2C_CTRWRITE_SA10):
-//        case(I2C_CTRREAD_SA10):
              I2C_SendData(I2Cx,SlaveAddress);
         break;
         
@@ -561,3 +507,83 @@
   return (*(__IO uint16_t *) tmp);
 }
 
+    
+void I2C_GPIO(void )
+{
+        GPIO_InitDef.GPIO_Pin = GPIO_Pin_9; // Set to Pin_9 (SCL0))
+        GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT; // Set to Mode Output
+        HAL_GPIO_Init(GPIOA, &GPIO_InitDef);
+        HAL_PAD_AFConfig(PAD_PA,GPIO_Pin_9, PAD_AF1); // PAD Config - LED used 2nd Function
+        
+        GPIO_InitDef.GPIO_Pin = GPIO_Pin_10; // Set to Pin_9 (SCL0))
+        GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT; // Set to Mode Output
+        HAL_GPIO_Init(GPIOA, &GPIO_InitDef);
+        HAL_PAD_AFConfig(PAD_PA,GPIO_Pin_10, PAD_AF1); // PAD Config - LED used 2nd Function
+ 
+}
+void GPIO_I2C(void )
+{
+       GPIO_InitDef.GPIO_Pin = GPIO_Pin_9; // Set to Pin_9 (SCL0))
+       GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT; // Set to Mode Output
+       HAL_GPIO_Init(GPIOA, &GPIO_InitDef);
+       HAL_PAD_AFConfig(PAD_PA,GPIO_Pin_9, PAD_AF0); // PAD Config - LED used 2nd Function
+        
+       GPIO_InitDef.GPIO_Pin = GPIO_Pin_10; // Set to Pin_10 (SDA0))
+       GPIO_InitDef.GPIO_Mode = GPIO_Mode_IN; // Set to Mode Output
+       HAL_GPIO_Init(GPIOA, &GPIO_InitDef);
+       HAL_PAD_AFConfig(PAD_PA,GPIO_Pin_10, PAD_AF0); // PAD Config - LED used 2nd Functio
+
+}
+
+
+void WriteByte(uint8_t val)
+{
+    int  i;
+    GPIO_TypeDef* GPIOx;
+    GPIOx = GPIOA;
+
+	for(i=0;i<8;i++)
+	{
+		if((val << i) & 0x80){
+			digitalWrite(GPIOx,SDA, Bit_SET);
+		}else{
+			digitalWrite(GPIOx,SDA, Bit_RESET);
+		}
+        i2c_loop_us(1);
+		digitalWrite(GPIOx,SCL, Bit_SET);
+        i2c_loop_us(2);
+		digitalWrite(GPIOx,SCL, Bit_RESET);
+	}
+	digitalWrite(GPIOx,SDA, Bit_SET);
+    i2c_loop_us(1);
+	digitalWrite(GPIOx,SCL, Bit_SET);
+	i2c_loop_us(2);
+    digitalWrite(GPIOx,SCL, Bit_RESET);
+}
+
+
+void digitalWrite(GPIO_TypeDef* GPIOx,uint16_t pin, uint16_t val)
+{
+    
+    if(val == Bit_SET)
+    {
+        GPIOx -> OUTENCLR = pin;
+    }
+    else
+    {
+        GPIOx -> OUTENSET |= pin;
+        
+    }
+}
+
+
+void i2c_loop_us(int us)
+{
+    volatile uint32_t delay = us; // approximate loops per ms at 24 MHz, Debug config
+    for(; delay != 0; delay--)
+        __NOP();
+}
+void i2c_loop_ms(int count) {
+    i2c_loop_us(count*1000);
+}
+