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
@@ -180,7 +175,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
180
175
timeout = LONG_TIMEOUT;
181
176
while (I2C_GetFlagStatus(i2c, I2C_FLAG_BUSY) == SET) {
182
177
if ((timeout--) == 0) {
183
- return EXIT_TIMEOUT ;
178
+ return 0 ;
184
179
}
185
180
}
186
181
*/
@@ -194,13 +189,14 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) {
194
189
timeout = FLAG_TIMEOUT ;
195
190
while (I2C_CheckEvent (i2c , I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) == ERROR ) {
196
191
if ((timeout -- ) == 0 ) {
197
- return EXIT_TIMEOUT ;
192
+ return 0 ;
198
193
}
199
194
}
200
195
201
196
for (count = 0 ; count < length ; count ++ ) {
202
- if (i2c_byte_write (obj , data [count ]) != EXIT_OK ) {
203
- return EXIT_FAIL ;
197
+ if (i2c_byte_write (obj , data [count ]) != 1 ) {
198
+ i2c_stop (obj );
199
+ return 0 ;
204
200
}
205
201
}
206
202
@@ -229,7 +225,7 @@ int i2c_byte_read(i2c_t *obj, int last) {
229
225
timeout = FLAG_TIMEOUT ;
230
226
while (I2C_GetFlagStatus (i2c , I2C_FLAG_RXNE ) == RESET ) {
231
227
if ((timeout -- ) == 0 ) {
232
- return EXIT_TIMEOUT ;
228
+ return 0 ;
233
229
}
234
230
}
235
231
@@ -250,11 +246,11 @@ int i2c_byte_write(i2c_t *obj, int data) {
250
246
while ((I2C_GetFlagStatus (i2c , I2C_FLAG_TXE ) == RESET ) &&
251
247
(I2C_GetFlagStatus (i2c , I2C_FLAG_BTF ) == RESET )) {
252
248
if ((timeout -- ) == 0 ) {
253
- return EXIT_TIMEOUT ;
249
+ return 0 ;
254
250
}
255
251
}
256
252
257
- return EXIT_OK ;
253
+ return 1 ;
258
254
}
259
255
260
256
void i2c_reset (i2c_t * obj ) {
@@ -288,29 +284,37 @@ void i2c_slave_mode(i2c_t *obj, int enable_slave) {
288
284
// Nothing to do
289
285
}
290
286
291
- #define NoData 0
292
- #define ReadAddressed 1
293
- #define WriteGeneral 2
294
- #define WriteAddressed 3
287
+ // See I2CSlave.h
288
+ #define NoData 0 // the slave has not been addressed
289
+ #define ReadAddressed 1 // the master has requested a read from this slave (slave = transmitter)
290
+ #define WriteGeneral 2 // the master is writing to all slave
291
+ #define WriteAddressed 3 // the master is writing to this slave (slave = receiver)
295
292
296
293
int i2c_slave_receive (i2c_t * obj ) {
297
- //I2C_TypeDef *i2c = (I2C_TypeDef *)(obj->i2c);
298
- int retval = NoData ;
299
- //int status;
300
-
301
- //if (I2C_GetFlagStatus(i2c, I2C_FLAG_GENCALL) == SET) retval = WriteGeneral;
302
-
303
- //status = I2C_GetLastEvent(i2c);
304
-
305
- return (retval );
294
+ // TO BE DONE
295
+ return (0 );
306
296
}
307
297
308
298
int i2c_slave_read (i2c_t * obj , char * data , int length ) {
309
- return 0 ;
299
+ int count = 0 ;
300
+
301
+ // Read all bytes
302
+ for (count = 0 ; count < length ; count ++ ) {
303
+ data [count ] = i2c_byte_read (obj , 0 );
304
+ }
305
+
306
+ return count ;
310
307
}
311
308
312
309
int i2c_slave_write (i2c_t * obj , const char * data , int length ) {
313
- return 0 ;
310
+ int count = 0 ;
311
+
312
+ // Write all bytes
313
+ for (count = 0 ; count < length ; count ++ ) {
314
+ i2c_byte_write (obj , data [count ]);
315
+ }
316
+
317
+ return count ;
314
318
}
315
319
316
320
0 commit comments