27
27
#define FLAG_TIMEOUT ((int)0x1000)
28
28
#define LONG_TIMEOUT ((int)0x8000)
29
29
30
- // Functions exit codes
31
- #define EXIT_OK (0)
32
- #define EXIT_FAIL (1)
33
- #define EXIT_TIMEOUT (0xFFFFFFFF)
34
-
35
30
static const PinMap PinMap_I2C_SDA [] = {
36
- //{PB_7, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 0)}, // Cannot be used due to TIM4
37
- {PB_9 , I2C_1 , STM_PIN_DATA (GPIO_Mode_AF_OD , 7 )}, // GPIO_Remap_I2C1
31
+ {PB_9 , I2C_1 , STM_PIN_DATA (GPIO_Mode_AF_OD , 8 )}, // GPIO_Remap_I2C1
38
32
{NC , NC , 0 }
39
33
};
40
34
41
35
static const PinMap PinMap_I2C_SCL [] = {
42
- //{PB_6, I2C_1, STM_PIN_DATA(GPIO_Mode_AF_OD, 0)}, // // Cannot be used due to TIM4
43
- {PB_8 , I2C_1 , STM_PIN_DATA (GPIO_Mode_AF_OD , 7 )}, // GPIO_Remap_I2C1
36
+ {PB_8 , I2C_1 , STM_PIN_DATA (GPIO_Mode_AF_OD , 8 )}, // GPIO_Remap_I2C1
44
37
{NC , NC , 0 }
45
38
};
46
39
@@ -107,17 +100,19 @@ inline int i2c_start(i2c_t *obj) {
107
100
//while (I2C_CheckEvent(i2c, I2C_EVENT_MASTER_MODE_SELECT) == ERROR) {
108
101
while (I2C_GetFlagStatus (i2c , I2C_FLAG_SB ) == RESET ) {
109
102
if ((timeout -- ) == 0 ) {
110
- return EXIT_TIMEOUT ;
103
+ return 1 ;
111
104
}
112
105
}
113
106
114
- return EXIT_OK ;
107
+ return 0 ;
115
108
}
116
109
117
110
inline int i2c_stop (i2c_t * obj ) {
118
111
I2C_TypeDef * i2c = (I2C_TypeDef * )(obj -> i2c );
112
+
119
113
I2C_GenerateSTOP (i2c , ENABLE );
120
- return EXIT_OK ;
114
+
115
+ return 0 ;
121
116
}
122
117
123
118
int i2c_read (i2c_t * obj , int address , char * data , int length , int stop ) {
@@ -133,7 +128,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
133
128
timeout = LONG_TIMEOUT;
134
129
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
135
130
if ((timeout--) == 0) {
136
- return EXIT_TIMEOUT ;
131
+ return 0 ;
137
132
}
138
133
}
139
134
*/
@@ -147,7 +142,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) {
147
142
timeout = FLAG_TIMEOUT ;
148
143
while (I2C_CheckEvent (i2c , I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED ) == ERROR ) {
149
144
if ((timeout -- ) == 0 ) {
150
- return EXIT_TIMEOUT ;
145
+ return 0 ;
151
146
}
152
147
}
153
148
@@ -182,7 +177,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
182
177
timeout = LONG_TIMEOUT;
183
178
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
184
179
if ((timeout--) == 0) {
185
- return EXIT_TIMEOUT ;
180
+ return 0 ;
186
181
}
187
182
}
188
183
*/
@@ -196,13 +191,14 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
196
191
timeout = FLAG_TIMEOUT ;
197
192
while (I2C_CheckEvent (i2c , I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) == ERROR ) {
198
193
if ((timeout -- ) == 0 ) {
199
- return EXIT_TIMEOUT ;
194
+ return 0 ;
200
195
}
201
196
}
202
197
203
198
for (count = 0 ; count < length ; count ++ ) {
204
- if (i2c_byte_write (obj , data [count ]) != EXIT_OK ) {
205
- return EXIT_FAIL ;
199
+ if (i2c_byte_write (obj , data [count ]) != 1 ) {
200
+ i2c_stop (obj );
201
+ return 0 ;
206
202
}
207
203
}
208
204
@@ -231,7 +227,7 @@ int i2c_byte_read(i2c_t *obj, int last) {
231
227
timeout = FLAG_TIMEOUT ;
232
228
while (I2C_GetFlagStatus (i2c , I2C_FLAG_RXNE ) == RESET ) {
233
229
if ((timeout -- ) == 0 ) {
234
- return EXIT_TIMEOUT ;
230
+ return 0 ;
235
231
}
236
232
}
237
233
@@ -252,11 +248,11 @@ int i2c_byte_write(i2c_t *obj, int data) {
252
248
while ((I2C_GetFlagStatus (i2c , I2C_FLAG_TXE ) == RESET ) &&
253
249
(I2C_GetFlagStatus (i2c , I2C_FLAG_BTF ) == RESET )) {
254
250
if ((timeout -- ) == 0 ) {
255
- return EXIT_TIMEOUT ;
251
+ return 0 ;
256
252
}
257
253
}
258
254
259
- return EXIT_OK ;
255
+ return 1 ;
260
256
}
261
257
262
258
void i2c_reset (i2c_t * obj ) {
@@ -290,29 +286,37 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
290
286
// Nothing to do
291
287
}
292
288
293
- #define NoData 0
294
- #define ReadAddressed 1
295
- #define WriteGeneral 2
296
- #define WriteAddressed 3
289
+ // See I2CSlave.h
290
+ #define NoData 0 // the slave has not been addressed
291
+ #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
292
+ #define WriteGeneral 2 // the master is writing to all slave
293
+ #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
297
294
298
295
int i2c_slave_receive (i2c_t * obj ) {
299
- //I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
300
- int retval = NoData ;
301
- //int status;
302
-
303
- //if (I2C_GetFlagStatus(i2c, I2C_FLAG_GENCALL) == SET) retval = WriteGeneral;
304
-
305
- //status = I2C_GetLastEvent(i2c);
306
-
307
- return (retval );
296
+ // TO BE DONE
297
+ return (0 );
308
298
}
309
299
310
300
int i2c_slave_read (i2c_t * obj , char * data , int length ) {
311
- return 0 ;
301
+ int count = 0 ;
302
+
303
+ // Read all bytes
304
+ for (count = 0 ; count < length ; count ++ ) {
305
+ data [count ] = i2c_byte_read (obj , 0 );
306
+ }
307
+
308
+ return count ;
312
309
}
313
310
314
311
int i2c_slave_write (i2c_t * obj , const char * data , int length ) {
315
- return 0 ;
312
+ int count = 0 ;
313
+
314
+ // Write all bytes
315
+ for (count = 0 ; count < length ; count ++ ) {
316
+ i2c_byte_write (obj , data [count ]);
317
+ }
318
+
319
+ return count ;
316
320
}
317
321
318
322
0 commit comments