@@ -90,21 +90,24 @@ void i2c_init(i2c_t *obj, PinName sda, PinName scl) {
90
90
void i2c_frequency (i2c_t * obj , int hz ) {
91
91
I2C_TypeDef * i2c = (I2C_TypeDef * )(obj -> i2c );
92
92
I2C_InitTypeDef I2C_InitStructure ;
93
+
94
+ if (hz == 0 ) return ;
95
+ if (hz > 400000 ) hz = 400000 ;
96
+
97
+ /* Warning: To use the I2C at 400 kHz (in fast mode), the PCLK1 frequency
98
+ (I2C peripheral input clock) must be a multiple of 10 MHz.
99
+ With the actual clock configuration, the max frequency is measured at 296 kHz */
93
100
94
- if ((hz != 0 ) && (hz <= 400000 )) {
95
- I2C_DeInit (i2c );
96
-
97
- // I2C configuration
98
- I2C_InitStructure .I2C_Mode = I2C_Mode_I2C ;
99
- I2C_InitStructure .I2C_DutyCycle = I2C_DutyCycle_2 ;
100
- I2C_InitStructure .I2C_OwnAddress1 = 0 ;
101
- I2C_InitStructure .I2C_Ack = I2C_Ack_Enable ;
102
- I2C_InitStructure .I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit ;
103
- I2C_InitStructure .I2C_ClockSpeed = hz ;
104
- I2C_Init (i2c , & I2C_InitStructure );
105
-
106
- I2C_Cmd (i2c , ENABLE );
107
- }
101
+ // I2C configuration
102
+ I2C_DeInit (i2c );
103
+ I2C_InitStructure .I2C_Mode = I2C_Mode_I2C ;
104
+ I2C_InitStructure .I2C_DutyCycle = I2C_DutyCycle_2 ;
105
+ I2C_InitStructure .I2C_OwnAddress1 = 0 ;
106
+ I2C_InitStructure .I2C_Ack = I2C_Ack_Enable ;
107
+ I2C_InitStructure .I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit ;
108
+ I2C_InitStructure .I2C_ClockSpeed = hz ;
109
+ I2C_Init (i2c , & I2C_InitStructure );
110
+ I2C_Cmd (i2c , ENABLE );
108
111
}
109
112
110
113
inline int i2c_start (i2c_t * obj ) {
@@ -118,7 +121,6 @@ inline int i2c_start(i2c_t *obj) {
118
121
119
122
// Wait the START condition has been correctly sent
120
123
timeout = FLAG_TIMEOUT ;
121
- //while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_MODE_SELECT) == ERROR) {
122
124
while (I2C_GetFlagStatus (i2c , I2C_FLAG_SB ) == RESET ) {
123
125
timeout -- ;
124
126
if (timeout == 0 ) {
@@ -131,9 +133,25 @@ inline int i2c_start(i2c_t *obj) {
131
133
132
134
inline int i2c_stop (i2c_t * obj ) {
133
135
I2C_TypeDef * i2c = (I2C_TypeDef * )(obj -> i2c );
134
-
135
- I2C_GenerateSTOP (i2c , ENABLE );
136
-
136
+ int timeout ;
137
+ volatile int temp ;
138
+
139
+ if (I2C_GetFlagStatus (i2c , I2C_FLAG_MSL ) == RESET ) {
140
+ timeout = LONG_TIMEOUT ;
141
+ // wait for STOP
142
+ while (I2C_GetFlagStatus (i2c , I2C_FLAG_STOPF ) == RESET ) {
143
+ timeout -- ;
144
+ if (timeout == 0 ) {
145
+ return 0 ;
146
+ }
147
+ }
148
+ temp = i2c -> SR1 ;
149
+ I2C_Cmd (i2c , ENABLE );
150
+ }
151
+ else {
152
+ I2C_GenerateSTOP (i2c , ENABLE );
153
+ }
154
+
137
155
return 0 ;
138
156
}
139
157
@@ -145,17 +163,6 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
145
163
146
164
if (length == 0 ) return 0 ;
147
165
148
- /*
149
- // Wait until the bus is not busy anymore
150
- timeout = LONG_TIMEOUT;
151
- while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
152
- timeout--;
153
- if (timeout == 0) {
154
- return 0;
155
- }
156
- }
157
- */
158
-
159
166
i2c_start (obj );
160
167
161
168
// Send slave address for read
@@ -193,17 +200,6 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
193
200
I2C_TypeDef * i2c = (I2C_TypeDef * )(obj -> i2c );
194
201
int timeout ;
195
202
int count ;
196
-
197
- /*
198
- // Wait until the bus is not busy anymore
199
- timeout = LONG_TIMEOUT;
200
- while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
201
- timeout--;
202
- if (timeout == 0) {
203
- return 0;
204
- }
205
- }
206
- */
207
203
208
204
i2c_start (obj );
209
205
@@ -268,8 +264,7 @@ int i2c_byte_write(i2c_t *obj, int data) {
268
264
I2C_SendData (i2c , (uint8_t )data );
269
265
270
266
// Wait until the byte is transmitted
271
- timeout = FLAG_TIMEOUT ;
272
- //while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_BYTE_TRANSMITTED) == ERROR) {
267
+ timeout = FLAG_TIMEOUT ;
273
268
while ((I2C_GetFlagStatus (i2c , I2C_FLAG_TXE ) == RESET ) &&
274
269
(I2C_GetFlagStatus (i2c , I2C_FLAG_BTF ) == RESET )) {
275
270
timeout -- ;
@@ -319,7 +314,6 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
319
314
#define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
320
315
321
316
int i2c_slave_receive (i2c_t * obj ) {
322
- // TO BE DONE
323
317
return (0 );
324
318
}
325
319
0 commit comments