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.

Committer:
mbed_official
Date:
Tue Jun 30 09:45:08 2015 +0100
Revision:
576:99a3d3d9c43f
Parent:
558:0880f51c4036
Child:
601:248b0d2dd755
Synchronized with git revision 1c13bc80fedd0fdfaedfab5c33183dc2b6f9b1bb

Full URL: https://github.com/mbedmicro/mbed/commit/1c13bc80fedd0fdfaedfab5c33183dc2b6f9b1bb/

Wiznet - Update and bug fix.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 558:0880f51c4036 1 #include "W7500x.h"
mbed_official 558:0880f51c4036 2
mbed_official 558:0880f51c4036 3 /** @defgroup I2C_Private_Functions
mbed_official 558:0880f51c4036 4 * @{
mbed_official 558:0880f51c4036 5 */
mbed_official 558:0880f51c4036 6
mbed_official 576:99a3d3d9c43f 7 GPIO_InitTypeDef GPIO_InitDef;
mbed_official 576:99a3d3d9c43f 8 void i2c_loop_us(int us);
mbed_official 576:99a3d3d9c43f 9
mbed_official 576:99a3d3d9c43f 10 #define SCL GPIO_Pin_9
mbed_official 576:99a3d3d9c43f 11 #define SDA GPIO_Pin_10
mbed_official 576:99a3d3d9c43f 12 uint16_t buf[] ={0x00,0x01};
mbed_official 576:99a3d3d9c43f 13
mbed_official 558:0880f51c4036 14 /**
mbed_official 558:0880f51c4036 15 * @brief Initializes the I2Cx peripheral according to the specified
mbed_official 558:0880f51c4036 16 * parameters in the I2C_InitStruct.
mbed_official 558:0880f51c4036 17 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 558:0880f51c4036 18 * @param I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
mbed_official 558:0880f51c4036 19 * contains the configuration information for the specified I2C peripheral.
mbed_official 558:0880f51c4036 20 * @retval None
mbed_official 558:0880f51c4036 21 */
mbed_official 558:0880f51c4036 22
mbed_official 558:0880f51c4036 23 uint32_t I2C_Init(I2C_TypeDef* I2Cx, I2C_ConfigStruct conf)
mbed_official 558:0880f51c4036 24 {
mbed_official 558:0880f51c4036 25 uint32_t mode;
mbed_official 558:0880f51c4036 26 uint8_t prescale;
mbed_official 558:0880f51c4036 27 uint16_t timeout;
mbed_official 558:0880f51c4036 28 uint16_t slave_address;
mbed_official 558:0880f51c4036 29
mbed_official 558:0880f51c4036 30
mbed_official 558:0880f51c4036 31 mode = conf.mode;
mbed_official 558:0880f51c4036 32 slave_address = conf.slave_address;
mbed_official 558:0880f51c4036 33 if(mode == I2C_Master)
mbed_official 558:0880f51c4036 34 {
mbed_official 558:0880f51c4036 35 prescale = conf.master.prescale;
mbed_official 558:0880f51c4036 36 timeout = conf.master.timeout;
mbed_official 558:0880f51c4036 37
mbed_official 558:0880f51c4036 38 I2C_CoreEn(I2Cx,ENABLE);
mbed_official 558:0880f51c4036 39 I2C_MasterSlave(I2Cx,ENABLE);
mbed_official 558:0880f51c4036 40
mbed_official 558:0880f51c4036 41 I2C_Prescale(I2Cx,prescale); // 0x61 //When PLL clk is 20MHz and Prescale value set 0x61, SCL is 100KHz
mbed_official 558:0880f51c4036 42 I2C_TimeoutSet(I2Cx,timeout); // 0xFFFF
mbed_official 558:0880f51c4036 43
mbed_official 558:0880f51c4036 44 I2C_CoreEn(I2Cx,DISABLE);
mbed_official 558:0880f51c4036 45 }
mbed_official 558:0880f51c4036 46 else if(conf.mode == I2C_Slave)
mbed_official 558:0880f51c4036 47 {
mbed_official 558:0880f51c4036 48 I2C_AcknowledgeConfig(I2Cx,ENABLE);
mbed_official 558:0880f51c4036 49 I2C_SetSlavAddress(I2Cx,slave_address);
mbed_official 558:0880f51c4036 50 }
mbed_official 558:0880f51c4036 51 else
mbed_official 558:0880f51c4036 52 return ERROR;
mbed_official 558:0880f51c4036 53
mbed_official 558:0880f51c4036 54
mbed_official 558:0880f51c4036 55 I2C_AcknowledgeConfig(I2Cx,ENABLE);
mbed_official 558:0880f51c4036 56
mbed_official 558:0880f51c4036 57 return SUCCESS;
mbed_official 558:0880f51c4036 58 }
mbed_official 558:0880f51c4036 59
mbed_official 558:0880f51c4036 60
mbed_official 558:0880f51c4036 61 void I2C_DeInit(I2C_TypeDef* I2Cx)
mbed_official 558:0880f51c4036 62 {
mbed_official 558:0880f51c4036 63 I2C_InterRst(I2Cx,ENABLE);
mbed_official 558:0880f51c4036 64 I2C_CoreEn(I2Cx, ENABLE);
mbed_official 558:0880f51c4036 65 I2C_InterRst(I2Cx,DISABLE);
mbed_official 558:0880f51c4036 66 I2C_CoreEn(I2Cx, DISABLE);
mbed_official 558:0880f51c4036 67 }
mbed_official 558:0880f51c4036 68
mbed_official 558:0880f51c4036 69
mbed_official 558:0880f51c4036 70 ErrorStatus I2C_Start(I2C_TypeDef* I2Cx, uint16_t slave_address, I2C_CTR ctr)
mbed_official 558:0880f51c4036 71 {
mbed_official 558:0880f51c4036 72 ErrorStatus ret;
mbed_official 576:99a3d3d9c43f 73
mbed_official 558:0880f51c4036 74 I2C_GenerateSTART(I2Cx,ENABLE);
mbed_official 558:0880f51c4036 75 I2C_SendSlaveAddress(I2Cx,slave_address,(I2C_CTR)ctr);
mbed_official 558:0880f51c4036 76 I2C_GenerateSTART(I2Cx,DISABLE);
mbed_official 558:0880f51c4036 77
mbed_official 558:0880f51c4036 78 ret=I2C_CheckEvent(I2Cx,I2C_ACKR);
mbed_official 558:0880f51c4036 79
mbed_official 558:0880f51c4036 80 return ret;
mbed_official 558:0880f51c4036 81 }
mbed_official 558:0880f51c4036 82
mbed_official 558:0880f51c4036 83 void I2C_Stop(I2C_TypeDef* I2Cx)
mbed_official 558:0880f51c4036 84 {
mbed_official 558:0880f51c4036 85 I2C_GenerateSTOP(I2Cx,ENABLE);
mbed_official 558:0880f51c4036 86 I2C_GenerateSTOP(I2Cx,DISABLE);
mbed_official 576:99a3d3d9c43f 87 GPIO_InitDef.GPIO_Pin = GPIO_Pin_9; // Set to Pin_9 (SCL0))
mbed_official 576:99a3d3d9c43f 88 GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT; // Set to Mode Output
mbed_official 576:99a3d3d9c43f 89 HAL_GPIO_Init(GPIOA, &GPIO_InitDef);
mbed_official 576:99a3d3d9c43f 90 HAL_PAD_AFConfig(PAD_PA,GPIO_Pin_9, PAD_AF0); // PAD Config - LED used 2nd Function
mbed_official 558:0880f51c4036 91 }
mbed_official 558:0880f51c4036 92
mbed_official 558:0880f51c4036 93 void I2C_Reset(I2C_TypeDef* I2Cx)
mbed_official 558:0880f51c4036 94 {
mbed_official 558:0880f51c4036 95 I2C_CoreEn(I2Cx,ENABLE);
mbed_official 558:0880f51c4036 96 // Maybe, it needs a little delay
mbed_official 558:0880f51c4036 97 I2C_CoreEn(I2Cx,DISABLE);
mbed_official 558:0880f51c4036 98 }
mbed_official 558:0880f51c4036 99
mbed_official 558:0880f51c4036 100 void I2C_SendData(I2C_TypeDef* I2Cx,uint16_t Data)
mbed_official 558:0880f51c4036 101 {
mbed_official 558:0880f51c4036 102 I2Cx -> TXR = (uint16_t)Data;
mbed_official 558:0880f51c4036 103 }
mbed_official 558:0880f51c4036 104
mbed_official 558:0880f51c4036 105 int8_t I2C_SendDataAck(I2C_TypeDef* I2Cx,uint16_t Data)
mbed_official 558:0880f51c4036 106 {
mbed_official 576:99a3d3d9c43f 107 buf[0] = Data;
mbed_official 576:99a3d3d9c43f 108 if(buf[0] == buf[1])
mbed_official 558:0880f51c4036 109 {
mbed_official 576:99a3d3d9c43f 110 I2C_GPIO();
mbed_official 576:99a3d3d9c43f 111 WriteByte(Data);
mbed_official 576:99a3d3d9c43f 112 i2c_loop_us(1);
mbed_official 576:99a3d3d9c43f 113 GPIO_I2C();
mbed_official 576:99a3d3d9c43f 114 }
mbed_official 576:99a3d3d9c43f 115 else
mbed_official 576:99a3d3d9c43f 116 {
mbed_official 576:99a3d3d9c43f 117 I2Cx -> TXR = (uint16_t)Data;
mbed_official 576:99a3d3d9c43f 118 if(I2C_CheckEvent(I2Cx,I2C_ACKR) == ERROR)
mbed_official 576:99a3d3d9c43f 119 {
mbed_official 576:99a3d3d9c43f 120 return ERROR;
mbed_official 576:99a3d3d9c43f 121 }
mbed_official 576:99a3d3d9c43f 122 }
mbed_official 576:99a3d3d9c43f 123 buf[1] = buf[0];
mbed_official 558:0880f51c4036 124 return SUCCESS;
mbed_official 558:0880f51c4036 125 }
mbed_official 558:0880f51c4036 126
mbed_official 558:0880f51c4036 127 int I2C_ReceiveData(I2C_TypeDef* I2Cx, int last)
mbed_official 558:0880f51c4036 128 {
mbed_official 558:0880f51c4036 129 if(last)
mbed_official 558:0880f51c4036 130 {
mbed_official 558:0880f51c4036 131 I2C_AcknowledgeConfig(I2Cx,DISABLE);
mbed_official 576:99a3d3d9c43f 132 if( I2C_CheckEvent(I2Cx,I2C_ACKT) == ERROR ) {
mbed_official 558:0880f51c4036 133 return -1;
mbed_official 576:99a3d3d9c43f 134 }
mbed_official 576:99a3d3d9c43f 135 }
mbed_official 576:99a3d3d9c43f 136 else if( I2C_CheckEvent(I2Cx,I2C_ACKT) == ERROR ) {
mbed_official 576:99a3d3d9c43f 137 return -1;
mbed_official 576:99a3d3d9c43f 138 }
mbed_official 558:0880f51c4036 139
mbed_official 558:0880f51c4036 140 return (uint8_t)I2Cx -> RXR;
mbed_official 558:0880f51c4036 141 }
mbed_official 558:0880f51c4036 142
mbed_official 558:0880f51c4036 143
mbed_official 558:0880f51c4036 144 int I2C_Burst_Read(I2C_TypeDef* I2Cx, uint16_t address, uint8_t *data, int length, int stop)
mbed_official 558:0880f51c4036 145 {
mbed_official 558:0880f51c4036 146 int recv_cnt;
mbed_official 558:0880f51c4036 147
mbed_official 558:0880f51c4036 148 if( I2C_Start(I2Cx,address,I2C_READ_SA7) == ERROR){
mbed_official 558:0880f51c4036 149 return -1;
mbed_official 558:0880f51c4036 150 }
mbed_official 558:0880f51c4036 151
mbed_official 558:0880f51c4036 152 for(recv_cnt=0;recv_cnt<length;recv_cnt++)
mbed_official 558:0880f51c4036 153 {
mbed_official 558:0880f51c4036 154 }
mbed_official 558:0880f51c4036 155
mbed_official 558:0880f51c4036 156 return recv_cnt;
mbed_official 558:0880f51c4036 157 }
mbed_official 558:0880f51c4036 158
mbed_official 558:0880f51c4036 159
mbed_official 558:0880f51c4036 160 int I2C_Burst_Write(I2C_TypeDef* I2Cx, uint16_t address, uint8_t *data, int length, int stop)
mbed_official 558:0880f51c4036 161 {
mbed_official 558:0880f51c4036 162 int cnt;
mbed_official 558:0880f51c4036 163
mbed_official 558:0880f51c4036 164 if( I2C_Start(I2Cx,address,I2C_WRITE_SA7) == ERROR)
mbed_official 558:0880f51c4036 165 {
mbed_official 558:0880f51c4036 166 return -1;
mbed_official 558:0880f51c4036 167 }
mbed_official 558:0880f51c4036 168
mbed_official 558:0880f51c4036 169 for(cnt=0;cnt<length;cnt++)
mbed_official 558:0880f51c4036 170 {
mbed_official 558:0880f51c4036 171 if( I2C_SendDataAck(I2Cx,data[cnt]) == ERROR )
mbed_official 558:0880f51c4036 172 {
mbed_official 558:0880f51c4036 173 I2C_Stop(I2Cx);
mbed_official 558:0880f51c4036 174 return -1;
mbed_official 558:0880f51c4036 175 }
mbed_official 558:0880f51c4036 176 }
mbed_official 558:0880f51c4036 177
mbed_official 558:0880f51c4036 178 // If not repeated start, send stop
mbed_official 558:0880f51c4036 179 if(stop)
mbed_official 558:0880f51c4036 180 {
mbed_official 558:0880f51c4036 181 I2C_Stop(I2Cx);
mbed_official 558:0880f51c4036 182 }
mbed_official 558:0880f51c4036 183
mbed_official 558:0880f51c4036 184 return length;
mbed_official 558:0880f51c4036 185 }
mbed_official 558:0880f51c4036 186
mbed_official 558:0880f51c4036 187
mbed_official 558:0880f51c4036 188 /**
mbed_official 558:0880f51c4036 189 * @brief Generates I2Cx communication START condition.
mbed_official 558:0880f51c4036 190 * @param I2Cx: where x can be 0 or 1 to select the I2C peripheral.
mbed_official 558:0880f51c4036 191 * @param NewState: NewState of the I2C START condition generation.
mbed_official 558:0880f51c4036 192 * This parameter can be: ENABLE or DISABLE.
mbed_official 558:0880f51c4036 193 * @retval None.
mbed_official 558:0880f51c4036 194 */
mbed_official 558:0880f51c4036 195 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 558:0880f51c4036 196 {
mbed_official 576:99a3d3d9c43f 197 if(NewState != DISABLE)
mbed_official 576:99a3d3d9c43f 198 {
mbed_official 576:99a3d3d9c43f 199 I2Cx->CMDR = I2C_CMDR_STA;
mbed_official 576:99a3d3d9c43f 200 }
mbed_official 576:99a3d3d9c43f 201 else
mbed_official 576:99a3d3d9c43f 202 {
mbed_official 576:99a3d3d9c43f 203 I2Cx->CMDR = I2C_CMDR_STA;
mbed_official 576:99a3d3d9c43f 204 }
mbed_official 558:0880f51c4036 205 }
mbed_official 558:0880f51c4036 206 /**
mbed_official 558:0880f51c4036 207 * @brief Generates I2Cx communication STOP condition.
mbed_official 558:0880f51c4036 208 * @param I2Cx: where x can be 0 or 1 to select the I2C peripheral.
mbed_official 558:0880f51c4036 209 * @param NewState: NewState of the I2C STOP condition generation.
mbed_official 558:0880f51c4036 210 * This parameter can be: ENABLE or DISABLE.
mbed_official 558:0880f51c4036 211 * @retval None.
mbed_official 558:0880f51c4036 212 */
mbed_official 558:0880f51c4036 213 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 558:0880f51c4036 214 {
mbed_official 576:99a3d3d9c43f 215 if(NewState != DISABLE)
mbed_official 576:99a3d3d9c43f 216 {
mbed_official 576:99a3d3d9c43f 217
mbed_official 576:99a3d3d9c43f 218 I2Cx->CMDR = I2C_CMDR_STO;
mbed_official 576:99a3d3d9c43f 219
mbed_official 576:99a3d3d9c43f 220 }
mbed_official 576:99a3d3d9c43f 221 else
mbed_official 576:99a3d3d9c43f 222 {
mbed_official 576:99a3d3d9c43f 223 I2Cx->CMDR = I2C_CMDR_STO;
mbed_official 576:99a3d3d9c43f 224 }
mbed_official 558:0880f51c4036 225 }
mbed_official 558:0880f51c4036 226
mbed_official 558:0880f51c4036 227 /**
mbed_official 558:0880f51c4036 228 * @brief Enables or disables the specified I2C acknowledge feature.
mbed_official 558:0880f51c4036 229 * @param I2Cx: where x can be 0 or 1 to select the I2C peripheral.
mbed_official 558:0880f51c4036 230 * @param NewState: NewState of the I2C Acknowledgement.
mbed_official 558:0880f51c4036 231 * This parameter can be: ENABLE or DISABLE.
mbed_official 558:0880f51c4036 232 * @retval None.
mbed_official 558:0880f51c4036 233 */
mbed_official 558:0880f51c4036 234 void I2C_AcknowledgeConfig(I2C_TypeDef* I2Cx, FunctionalState NewState)
mbed_official 558:0880f51c4036 235 {
mbed_official 558:0880f51c4036 236 if(NewState != DISABLE) I2Cx -> CMDR = I2C_CMDR_ACK;
mbed_official 558:0880f51c4036 237 else I2Cx -> CMDR = I2C_CMDR_ACK;
mbed_official 558:0880f51c4036 238 }
mbed_official 558:0880f51c4036 239
mbed_official 558:0880f51c4036 240 /**
mbed_official 558:0880f51c4036 241 * @brief Generates I2Cx communication REGenerateSTART condition
mbed_official 558:0880f51c4036 242 * @param I2Cx: where x can be 0 or 1 to select the I2C peripheral.
mbed_official 558:0880f51c4036 243 * @param NewState: NewState of the I2C Acknowledgement.
mbed_official 558:0880f51c4036 244 * This parameter can be: ENABLE or DISABLE.
mbed_official 558:0880f51c4036 245 * @retval None.
mbed_official 558:0880f51c4036 246 */
mbed_official 558:0880f51c4036 247 void I2C_RESTART(I2C_TypeDef * I2Cx, FunctionalState NewState)
mbed_official 558:0880f51c4036 248 {
mbed_official 558:0880f51c4036 249 if(NewState != DISABLE) I2Cx->CMDR = I2C_CMDR_RESTA;
mbed_official 558:0880f51c4036 250 else I2Cx->CMDR = I2C_CMDR_RESTA;
mbed_official 558:0880f51c4036 251 }
mbed_official 558:0880f51c4036 252
mbed_official 558:0880f51c4036 253 /**
mbed_official 558:0880f51c4036 254 * @brief Enable or disable the specified I2C Core_en feature
mbed_official 558:0880f51c4036 255 * @param I2Cx: where x can be 0 or 1 to select the I2C peripheral.
mbed_official 558:0880f51c4036 256 * @param NewState: NewState of the I2C Acknowledgement.
mbed_official 558:0880f51c4036 257 * This parameter can be: ENABLE or DISABLE.
mbed_official 558:0880f51c4036 258 * @retval None.
mbed_official 558:0880f51c4036 259 */
mbed_official 558:0880f51c4036 260 void I2C_CoreEn(I2C_TypeDef* I2Cx,FunctionalState NewState)
mbed_official 558:0880f51c4036 261 {
mbed_official 558:0880f51c4036 262 /*Control*/
mbed_official 558:0880f51c4036 263 if(NewState != DISABLE) I2Cx -> CTR = I2C_CTR_COREEN;
mbed_official 558:0880f51c4036 264 else I2Cx -> CTR = I2C_CTR_COREEN;
mbed_official 558:0880f51c4036 265 }
mbed_official 558:0880f51c4036 266
mbed_official 558:0880f51c4036 267
mbed_official 558:0880f51c4036 268 void I2C_InterEn(I2C_TypeDef* I2Cx,FunctionalState NewState)
mbed_official 558:0880f51c4036 269 {
mbed_official 558:0880f51c4036 270 /*Control Interrupt Enable*/
mbed_official 558:0880f51c4036 271 if(NewState != DISABLE) I2Cx -> CTR = I2C_CTR_INTEREN;
mbed_official 558:0880f51c4036 272 else I2Cx -> CTR = I2C_CTR_INTEREN;
mbed_official 558:0880f51c4036 273 }
mbed_official 558:0880f51c4036 274
mbed_official 558:0880f51c4036 275
mbed_official 558:0880f51c4036 276 void I2C_MasterSlave(I2C_TypeDef* I2Cx,FunctionalState NewState)
mbed_official 558:0880f51c4036 277 {
mbed_official 558:0880f51c4036 278 /*Control MasterSlave select*/
mbed_official 558:0880f51c4036 279 if(NewState == ENABLE)
mbed_official 558:0880f51c4036 280 {
mbed_official 558:0880f51c4036 281 if( (I2Cx->CTR & I2C_CTR_MODE) != I2C_CTR_MODE )
mbed_official 558:0880f51c4036 282 {
mbed_official 558:0880f51c4036 283 I2Cx->CTR = I2C_CTR_MODE;
mbed_official 558:0880f51c4036 284 }
mbed_official 558:0880f51c4036 285 }
mbed_official 558:0880f51c4036 286 else // DISABLE
mbed_official 558:0880f51c4036 287 {
mbed_official 558:0880f51c4036 288 if( (I2Cx->CTR & I2C_CTR_MODE) == I2C_CTR_MODE )
mbed_official 558:0880f51c4036 289 {
mbed_official 558:0880f51c4036 290 I2Cx->CTR = I2C_CTR_MODE;
mbed_official 558:0880f51c4036 291 }
mbed_official 558:0880f51c4036 292 }
mbed_official 558:0880f51c4036 293 }
mbed_official 558:0880f51c4036 294
mbed_official 558:0880f51c4036 295
mbed_official 558:0880f51c4036 296 void I2C_ControlRW(I2C_TypeDef* I2Cx,FunctionalState NewState)
mbed_official 558:0880f51c4036 297 {
mbed_official 558:0880f51c4036 298 /*Control Read(receive)*/
mbed_official 558:0880f51c4036 299 if(NewState == ENABLE)
mbed_official 558:0880f51c4036 300 {
mbed_official 558:0880f51c4036 301 if( (I2Cx->CTR & I2C_CTR_CTRRWN) != I2C_CTR_CTRRWN )
mbed_official 558:0880f51c4036 302 {
mbed_official 558:0880f51c4036 303 I2Cx->CTR = I2C_CTR_CTRRWN;
mbed_official 558:0880f51c4036 304 }
mbed_official 558:0880f51c4036 305 }
mbed_official 558:0880f51c4036 306 else // DISABLE
mbed_official 558:0880f51c4036 307 {
mbed_official 558:0880f51c4036 308 if( (I2Cx->CTR & I2C_CTR_CTRRWN) == I2C_CTR_CTRRWN )
mbed_official 558:0880f51c4036 309 {
mbed_official 558:0880f51c4036 310 I2Cx->CTR = I2C_CTR_CTRRWN;
mbed_official 558:0880f51c4036 311 }
mbed_official 558:0880f51c4036 312 }
mbed_official 558:0880f51c4036 313 }
mbed_official 558:0880f51c4036 314
mbed_official 558:0880f51c4036 315
mbed_official 558:0880f51c4036 316 void I2C_ControlEn(I2C_TypeDef* I2Cx,FunctionalState NewState)
mbed_official 558:0880f51c4036 317 {
mbed_official 558:0880f51c4036 318 /*Control*/
mbed_official 558:0880f51c4036 319 if(NewState == ENABLE)
mbed_official 558:0880f51c4036 320 {
mbed_official 558:0880f51c4036 321 if( (I2Cx->CTR & I2C_CTR_CTEN) != I2C_CTR_CTEN )
mbed_official 558:0880f51c4036 322 {
mbed_official 558:0880f51c4036 323 I2Cx->CTR = I2C_CTR_CTEN;
mbed_official 558:0880f51c4036 324 }
mbed_official 558:0880f51c4036 325 }
mbed_official 558:0880f51c4036 326 else // DISABLE
mbed_official 558:0880f51c4036 327 {
mbed_official 558:0880f51c4036 328 if( (I2Cx->CTR & I2C_CTR_CTEN) == I2C_CTR_CTEN )
mbed_official 558:0880f51c4036 329 {
mbed_official 558:0880f51c4036 330 I2Cx->CTR = I2C_CTR_CTEN;
mbed_official 558:0880f51c4036 331 }
mbed_official 558:0880f51c4036 332 }
mbed_official 558:0880f51c4036 333 }
mbed_official 558:0880f51c4036 334
mbed_official 558:0880f51c4036 335
mbed_official 558:0880f51c4036 336 void I2C_InterRst(I2C_TypeDef* I2Cx,FunctionalState NewState)
mbed_official 558:0880f51c4036 337 {
mbed_official 558:0880f51c4036 338 /*Control*/
mbed_official 558:0880f51c4036 339 if(NewState == ENABLE)
mbed_official 558:0880f51c4036 340 {
mbed_official 558:0880f51c4036 341 if( (I2Cx->ISCR & I2C_ISCR_RST) != I2C_ISCR_RST )
mbed_official 558:0880f51c4036 342 {
mbed_official 558:0880f51c4036 343 I2Cx->ISCR = I2C_ISCR_RST;
mbed_official 558:0880f51c4036 344 }
mbed_official 558:0880f51c4036 345 }
mbed_official 558:0880f51c4036 346 else // DISABLE
mbed_official 558:0880f51c4036 347 {
mbed_official 558:0880f51c4036 348 if( (I2Cx->ISCR & I2C_ISCR_RST) == I2C_ISCR_RST )
mbed_official 558:0880f51c4036 349 {
mbed_official 558:0880f51c4036 350 I2Cx->ISCR = I2C_ISCR_RST;
mbed_official 558:0880f51c4036 351 }
mbed_official 558:0880f51c4036 352 }
mbed_official 558:0880f51c4036 353 }
mbed_official 558:0880f51c4036 354
mbed_official 558:0880f51c4036 355 void I2C_Prescale(I2C_TypeDef* I2Cx,uint16_t Data)
mbed_official 558:0880f51c4036 356 {
mbed_official 558:0880f51c4036 357 I2Cx -> PRER = (uint16_t)Data;
mbed_official 558:0880f51c4036 358 }
mbed_official 558:0880f51c4036 359
mbed_official 558:0880f51c4036 360 void I2C_TimeoutSet(I2C_TypeDef* I2Cx,uint16_t Data)
mbed_official 558:0880f51c4036 361 {
mbed_official 558:0880f51c4036 362 I2Cx -> TSR = (uint16_t)Data;
mbed_official 558:0880f51c4036 363 }
mbed_official 558:0880f51c4036 364 void I2C_SetSlavAddress(I2C_TypeDef* I2Cx,uint16_t Data)
mbed_official 558:0880f51c4036 365 {
mbed_official 558:0880f51c4036 366 I2Cx -> SADDR = (uint16_t)Data;
mbed_official 558:0880f51c4036 367 }
mbed_official 558:0880f51c4036 368
mbed_official 558:0880f51c4036 369
mbed_official 558:0880f51c4036 370 uint8_t I2C_StatusRead(I2C_TypeDef* I2Cx)
mbed_official 558:0880f51c4036 371 {
mbed_official 558:0880f51c4036 372 return (uint8_t)I2Cx -> SR;
mbed_official 558:0880f51c4036 373 }
mbed_official 558:0880f51c4036 374
mbed_official 558:0880f51c4036 375
mbed_official 558:0880f51c4036 376 ErrorStatus WaitEvent(I2C_TypeDef* I2Cx, uint32_t flag, FlagStatus status)
mbed_official 558:0880f51c4036 377 {
mbed_official 558:0880f51c4036 378 int Timeout=0,loopcnt=0;
mbed_official 558:0880f51c4036 379
mbed_official 558:0880f51c4036 380 Timeout = I2Cx->TSR;
mbed_official 558:0880f51c4036 381 if(status == SET)
mbed_official 558:0880f51c4036 382 {
mbed_official 558:0880f51c4036 383 for(loopcnt=Timeout; loopcnt>0; loopcnt--)
mbed_official 558:0880f51c4036 384 {
mbed_official 558:0880f51c4036 385 if( ((I2Cx->SR) & flag) == flag )
mbed_official 558:0880f51c4036 386 return SUCCESS;
mbed_official 558:0880f51c4036 387 }
mbed_official 558:0880f51c4036 388 }
mbed_official 558:0880f51c4036 389 else
mbed_official 558:0880f51c4036 390 {
mbed_official 558:0880f51c4036 391 for(loopcnt=Timeout; loopcnt>0; loopcnt--)
mbed_official 558:0880f51c4036 392 {
mbed_official 558:0880f51c4036 393 if( ((I2Cx->SR) & flag) != flag )
mbed_official 558:0880f51c4036 394 return SUCCESS;
mbed_official 558:0880f51c4036 395 }
mbed_official 558:0880f51c4036 396 }
mbed_official 558:0880f51c4036 397
mbed_official 558:0880f51c4036 398
mbed_official 558:0880f51c4036 399 return ERROR;
mbed_official 558:0880f51c4036 400 }
mbed_official 558:0880f51c4036 401
mbed_official 558:0880f51c4036 402
mbed_official 558:0880f51c4036 403 /**
mbed_official 558:0880f51c4036 404 * @brief Checks whether the specified I2C flag is set or not.
mbed_official 558:0880f51c4036 405 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
mbed_official 558:0880f51c4036 406 * @param I2C_EVENT: specifies the event to be checked.
mbed_official 558:0880f51c4036 407 */
mbed_official 558:0880f51c4036 408 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx,I2C_SR sr)
mbed_official 558:0880f51c4036 409 {
mbed_official 558:0880f51c4036 410 switch(sr)
mbed_official 558:0880f51c4036 411 {
mbed_official 558:0880f51c4036 412 case(I2C_ACKR):
mbed_official 558:0880f51c4036 413 if( WaitEvent(I2Cx, I2C_SR_ACKR, SET) == ERROR) return ERROR;
mbed_official 558:0880f51c4036 414 if( WaitEvent(I2Cx, I2C_SR_ACKR, RESET) == ERROR) return ERROR;
mbed_official 558:0880f51c4036 415 break;
mbed_official 558:0880f51c4036 416
mbed_official 558:0880f51c4036 417 case(I2C_ACKT ):
mbed_official 558:0880f51c4036 418 if( WaitEvent(I2Cx, I2C_SR_ACKT, SET) == ERROR) return ERROR;
mbed_official 558:0880f51c4036 419 if( WaitEvent(I2Cx, I2C_SR_ACKT, RESET) == ERROR) return ERROR;
mbed_official 558:0880f51c4036 420 break;
mbed_official 558:0880f51c4036 421
mbed_official 558:0880f51c4036 422 case(I2C_OACKR):
mbed_official 558:0880f51c4036 423 if( WaitEvent(I2Cx, I2C_SR_ACKR, SET) == ERROR) return ERROR;
mbed_official 558:0880f51c4036 424 break;
mbed_official 558:0880f51c4036 425
mbed_official 558:0880f51c4036 426 case(I2C_SACKR ):
mbed_official 558:0880f51c4036 427 if( WaitEvent(I2Cx, I2C_SR_ACKR, RESET) == ERROR) return ERROR;
mbed_official 558:0880f51c4036 428 break;
mbed_official 558:0880f51c4036 429
mbed_official 558:0880f51c4036 430 case(I2C_BT ):
mbed_official 558:0880f51c4036 431 if( WaitEvent(I2Cx, I2C_SR_BT, RESET) == ERROR) return ERROR;
mbed_official 558:0880f51c4036 432 break;
mbed_official 558:0880f51c4036 433
mbed_official 558:0880f51c4036 434 default:
mbed_official 558:0880f51c4036 435 return ERROR;
mbed_official 558:0880f51c4036 436 }
mbed_official 558:0880f51c4036 437
mbed_official 558:0880f51c4036 438 return SUCCESS;
mbed_official 558:0880f51c4036 439 }
mbed_official 558:0880f51c4036 440
mbed_official 558:0880f51c4036 441
mbed_official 558:0880f51c4036 442 void I2C_SendSlaveAddress(I2C_TypeDef* I2Cx, uint8_t SlaveAddress,I2C_CTR Ctr)
mbed_official 558:0880f51c4036 443 {
mbed_official 558:0880f51c4036 444 switch(Ctr)
mbed_official 558:0880f51c4036 445 {
mbed_official 558:0880f51c4036 446 case(I2C_READ_SA7):
mbed_official 558:0880f51c4036 447 I2C_SendData(I2Cx,SlaveAddress|I2C_READ);
mbed_official 558:0880f51c4036 448 break;
mbed_official 558:0880f51c4036 449
mbed_official 558:0880f51c4036 450
mbed_official 558:0880f51c4036 451 case(I2C_WRITE_SA7):
mbed_official 558:0880f51c4036 452 I2C_SendData(I2Cx,SlaveAddress|I2C_WRITE);
mbed_official 558:0880f51c4036 453 break;
mbed_official 576:99a3d3d9c43f 454
mbed_official 558:0880f51c4036 455 case(I2C_CTRWRITE_SA7):
mbed_official 558:0880f51c4036 456 case(I2C_CTRREAD_SA7):
mbed_official 558:0880f51c4036 457 I2C_SendData(I2Cx,SlaveAddress);
mbed_official 558:0880f51c4036 458 break;
mbed_official 558:0880f51c4036 459
mbed_official 558:0880f51c4036 460 default:
mbed_official 558:0880f51c4036 461 return;
mbed_official 558:0880f51c4036 462
mbed_official 558:0880f51c4036 463 }
mbed_official 558:0880f51c4036 464 }
mbed_official 558:0880f51c4036 465
mbed_official 558:0880f51c4036 466 int8_t I2C_Restart_Structure(I2C_TypeDef * I2Cx,uint32_t SlaveAddress,I2C_CTR Ctr)
mbed_official 558:0880f51c4036 467 {
mbed_official 558:0880f51c4036 468
mbed_official 558:0880f51c4036 469 I2C_RESTART(I2Cx,ENABLE);
mbed_official 558:0880f51c4036 470 I2C_SendSlaveAddress(I2Cx,SlaveAddress,Ctr);
mbed_official 558:0880f51c4036 471 if((I2C_CheckEvent(I2Cx,I2C_OACKR)) == ERROR )
mbed_official 558:0880f51c4036 472 {
mbed_official 558:0880f51c4036 473 return 0;
mbed_official 558:0880f51c4036 474 }
mbed_official 558:0880f51c4036 475 I2C_RESTART(I2Cx,DISABLE);
mbed_official 558:0880f51c4036 476 if((I2C_CheckEvent(I2Cx,I2C_SACKR)) == ERROR)
mbed_official 558:0880f51c4036 477 {
mbed_official 558:0880f51c4036 478 return 0;
mbed_official 558:0880f51c4036 479 }
mbed_official 558:0880f51c4036 480 return 1;
mbed_official 558:0880f51c4036 481 }
mbed_official 558:0880f51c4036 482
mbed_official 558:0880f51c4036 483 /**
mbed_official 558:0880f51c4036 484 * @brief Reads the specified I2C register and returns its value.
mbed_official 558:0880f51c4036 485 * @param I2C_Register: specifies the register to read.
mbed_official 558:0880f51c4036 486 * This parameter can be one of the following values:
mbed_official 558:0880f51c4036 487 * @arg I2C_Register_CR1: CR1 register.
mbed_official 558:0880f51c4036 488 * @arg I2C_Register_CR2: CR2 register.
mbed_official 558:0880f51c4036 489 * @arg I2C_Register_OAR1: OAR1 register.
mbed_official 558:0880f51c4036 490 * @arg I2C_Register_OAR2: OAR2 register.
mbed_official 558:0880f51c4036 491 * @arg I2C_Register_DR: DR register.
mbed_official 558:0880f51c4036 492 * @arg I2C_Register_SR1: SR1 register.
mbed_official 558:0880f51c4036 493 * @arg I2C_Register_SR2: SR2 register.
mbed_official 558:0880f51c4036 494 * @arg I2C_Register_CCR: CCR register.
mbed_official 558:0880f51c4036 495 * @arg I2C_Register_TRISE: TRISE register.
mbed_official 558:0880f51c4036 496 * @retval The value of the read register.
mbed_official 558:0880f51c4036 497 */
mbed_official 558:0880f51c4036 498 uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register)
mbed_official 558:0880f51c4036 499 {
mbed_official 558:0880f51c4036 500 __IO uint32_t tmp = 0;
mbed_official 558:0880f51c4036 501
mbed_official 558:0880f51c4036 502
mbed_official 558:0880f51c4036 503 tmp = (uint32_t) I2Cx;
mbed_official 558:0880f51c4036 504 tmp += I2C_Register;
mbed_official 558:0880f51c4036 505
mbed_official 558:0880f51c4036 506 /* Return the selected register value */
mbed_official 558:0880f51c4036 507 return (*(__IO uint16_t *) tmp);
mbed_official 558:0880f51c4036 508 }
mbed_official 558:0880f51c4036 509
mbed_official 576:99a3d3d9c43f 510
mbed_official 576:99a3d3d9c43f 511 void I2C_GPIO(void )
mbed_official 576:99a3d3d9c43f 512 {
mbed_official 576:99a3d3d9c43f 513 GPIO_InitDef.GPIO_Pin = GPIO_Pin_9; // Set to Pin_9 (SCL0))
mbed_official 576:99a3d3d9c43f 514 GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT; // Set to Mode Output
mbed_official 576:99a3d3d9c43f 515 HAL_GPIO_Init(GPIOA, &GPIO_InitDef);
mbed_official 576:99a3d3d9c43f 516 HAL_PAD_AFConfig(PAD_PA,GPIO_Pin_9, PAD_AF1); // PAD Config - LED used 2nd Function
mbed_official 576:99a3d3d9c43f 517
mbed_official 576:99a3d3d9c43f 518 GPIO_InitDef.GPIO_Pin = GPIO_Pin_10; // Set to Pin_9 (SCL0))
mbed_official 576:99a3d3d9c43f 519 GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT; // Set to Mode Output
mbed_official 576:99a3d3d9c43f 520 HAL_GPIO_Init(GPIOA, &GPIO_InitDef);
mbed_official 576:99a3d3d9c43f 521 HAL_PAD_AFConfig(PAD_PA,GPIO_Pin_10, PAD_AF1); // PAD Config - LED used 2nd Function
mbed_official 576:99a3d3d9c43f 522
mbed_official 576:99a3d3d9c43f 523 }
mbed_official 576:99a3d3d9c43f 524 void GPIO_I2C(void )
mbed_official 576:99a3d3d9c43f 525 {
mbed_official 576:99a3d3d9c43f 526 GPIO_InitDef.GPIO_Pin = GPIO_Pin_9; // Set to Pin_9 (SCL0))
mbed_official 576:99a3d3d9c43f 527 GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT; // Set to Mode Output
mbed_official 576:99a3d3d9c43f 528 HAL_GPIO_Init(GPIOA, &GPIO_InitDef);
mbed_official 576:99a3d3d9c43f 529 HAL_PAD_AFConfig(PAD_PA,GPIO_Pin_9, PAD_AF0); // PAD Config - LED used 2nd Function
mbed_official 576:99a3d3d9c43f 530
mbed_official 576:99a3d3d9c43f 531 GPIO_InitDef.GPIO_Pin = GPIO_Pin_10; // Set to Pin_10 (SDA0))
mbed_official 576:99a3d3d9c43f 532 GPIO_InitDef.GPIO_Mode = GPIO_Mode_IN; // Set to Mode Output
mbed_official 576:99a3d3d9c43f 533 HAL_GPIO_Init(GPIOA, &GPIO_InitDef);
mbed_official 576:99a3d3d9c43f 534 HAL_PAD_AFConfig(PAD_PA,GPIO_Pin_10, PAD_AF0); // PAD Config - LED used 2nd Functio
mbed_official 576:99a3d3d9c43f 535
mbed_official 576:99a3d3d9c43f 536 }
mbed_official 576:99a3d3d9c43f 537
mbed_official 576:99a3d3d9c43f 538
mbed_official 576:99a3d3d9c43f 539 void WriteByte(uint8_t val)
mbed_official 576:99a3d3d9c43f 540 {
mbed_official 576:99a3d3d9c43f 541 int i;
mbed_official 576:99a3d3d9c43f 542 GPIO_TypeDef* GPIOx;
mbed_official 576:99a3d3d9c43f 543 GPIOx = GPIOA;
mbed_official 576:99a3d3d9c43f 544
mbed_official 576:99a3d3d9c43f 545 for(i=0;i<8;i++)
mbed_official 576:99a3d3d9c43f 546 {
mbed_official 576:99a3d3d9c43f 547 if((val << i) & 0x80){
mbed_official 576:99a3d3d9c43f 548 digitalWrite(GPIOx,SDA, Bit_SET);
mbed_official 576:99a3d3d9c43f 549 }else{
mbed_official 576:99a3d3d9c43f 550 digitalWrite(GPIOx,SDA, Bit_RESET);
mbed_official 576:99a3d3d9c43f 551 }
mbed_official 576:99a3d3d9c43f 552 i2c_loop_us(1);
mbed_official 576:99a3d3d9c43f 553 digitalWrite(GPIOx,SCL, Bit_SET);
mbed_official 576:99a3d3d9c43f 554 i2c_loop_us(2);
mbed_official 576:99a3d3d9c43f 555 digitalWrite(GPIOx,SCL, Bit_RESET);
mbed_official 576:99a3d3d9c43f 556 }
mbed_official 576:99a3d3d9c43f 557 digitalWrite(GPIOx,SDA, Bit_SET);
mbed_official 576:99a3d3d9c43f 558 i2c_loop_us(1);
mbed_official 576:99a3d3d9c43f 559 digitalWrite(GPIOx,SCL, Bit_SET);
mbed_official 576:99a3d3d9c43f 560 i2c_loop_us(2);
mbed_official 576:99a3d3d9c43f 561 digitalWrite(GPIOx,SCL, Bit_RESET);
mbed_official 576:99a3d3d9c43f 562 }
mbed_official 576:99a3d3d9c43f 563
mbed_official 576:99a3d3d9c43f 564
mbed_official 576:99a3d3d9c43f 565 void digitalWrite(GPIO_TypeDef* GPIOx,uint16_t pin, uint16_t val)
mbed_official 576:99a3d3d9c43f 566 {
mbed_official 576:99a3d3d9c43f 567
mbed_official 576:99a3d3d9c43f 568 if(val == Bit_SET)
mbed_official 576:99a3d3d9c43f 569 {
mbed_official 576:99a3d3d9c43f 570 GPIOx -> OUTENCLR = pin;
mbed_official 576:99a3d3d9c43f 571 }
mbed_official 576:99a3d3d9c43f 572 else
mbed_official 576:99a3d3d9c43f 573 {
mbed_official 576:99a3d3d9c43f 574 GPIOx -> OUTENSET |= pin;
mbed_official 576:99a3d3d9c43f 575
mbed_official 576:99a3d3d9c43f 576 }
mbed_official 576:99a3d3d9c43f 577 }
mbed_official 576:99a3d3d9c43f 578
mbed_official 576:99a3d3d9c43f 579
mbed_official 576:99a3d3d9c43f 580 void i2c_loop_us(int us)
mbed_official 576:99a3d3d9c43f 581 {
mbed_official 576:99a3d3d9c43f 582 volatile uint32_t delay = us; // approximate loops per ms at 24 MHz, Debug config
mbed_official 576:99a3d3d9c43f 583 for(; delay != 0; delay--)
mbed_official 576:99a3d3d9c43f 584 __NOP();
mbed_official 576:99a3d3d9c43f 585 }
mbed_official 576:99a3d3d9c43f 586 void i2c_loop_ms(int count) {
mbed_official 576:99a3d3d9c43f 587 i2c_loop_us(count*1000);
mbed_official 576:99a3d3d9c43f 588 }
mbed_official 576:99a3d3d9c43f 589