@@ -15,75 +15,78 @@ typedef enum {
15
15
} pyb_i2c_t ;
16
16
17
17
typedef enum {
18
- I2C_STATE_IDLE = 0 ,
19
- I2C_STATE_WRITE = 1 ,
20
- I2C_STATE_READ = 2 ,
18
+ I2C_STATE_IDLE = 0 ,
19
+ I2C_STATE_WRITE = 1 ,
20
+ I2C_STATE_READ = 2 ,
21
21
} i2c_state_t ;
22
22
23
23
// set to true if the port has already been initialized
24
24
bool i2c1_port_initialized = false;
25
25
bool i2c2_port_initialized = false;
26
26
27
- static I2C_TypeDef * _i2c_port_addr (pyb_i2c_t i2c_port )
28
- {
29
- if (i2c_port == PYB_I2C_1 )
30
- return I2C1 ;
31
- if (i2c_port == PYB_I2C_2 )
32
- return I2C2 ;
33
- return NULL ;
27
+ static I2C_TypeDef * _i2c_port_addr (pyb_i2c_t i2c_port ) {
28
+ if (i2c_port == PYB_I2C_1 ) {
29
+ return I2C1 ;
30
+ }
31
+ if (i2c_port == PYB_I2C_2 ) {
32
+ return I2C2 ;
33
+ }
34
+ return NULL ;
34
35
}
35
36
36
37
// todo - perhaps there should be some global resource management for gpio
37
38
// this function would fail if the i2c pins have already been defined for
38
39
// use by another python object
39
40
// as it is, this always returns true (unless i2c_port is invalid)
40
- static bool _i2c_init (pyb_i2c_t i2c_port )
41
- {
41
+ static bool _i2c_init (pyb_i2c_t i2c_port ) {
42
42
GPIO_InitTypeDef GPIO_InitStructure ;
43
43
44
- I2C_TypeDef * i2c = _i2c_port_addr (i2c_port );
45
- if (i2c == NULL )
46
- return false;
47
-
48
- if (i2c_port == PYB_I2C_1 ) {
49
- if (i2c1_port_initialized == true) return true;
50
- RCC -> APB1ENR |= RCC_APB1ENR_I2C1EN ; // enable I2C1
44
+ I2C_TypeDef * i2c = _i2c_port_addr (i2c_port );
45
+ if (i2c == NULL )
46
+ return false;
51
47
52
- // PB6=SCL, PB7=SDA
53
- GPIO_InitStructure .GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 ;
54
- GPIO_InitStructure .GPIO_Mode = GPIO_Mode_AF ;
55
- GPIO_InitStructure .GPIO_OType = GPIO_OType_OD ;
56
- GPIO_InitStructure .GPIO_Speed = GPIO_Speed_25MHz ;
57
- GPIO_InitStructure .GPIO_PuPd = GPIO_PuPd_NOPULL ;
58
- GPIO_Init (GPIOB , & GPIO_InitStructure );
48
+ if (i2c_port == PYB_I2C_1 ) {
49
+ if (i2c1_port_initialized == true) {
50
+ return true;
51
+ }
52
+ RCC -> APB1ENR |= RCC_APB1ENR_I2C1EN ; // enable I2C1
59
53
60
- // alternate functions for SCL and SDA
61
- GPIO_PinAFConfig (GPIOB , GPIO_PinSource6 , GPIO_AF_I2C1 );
62
- GPIO_PinAFConfig (GPIOB , GPIO_PinSource7 , GPIO_AF_I2C1 );
54
+ // PB6=SCL, PB7=SDA
55
+ GPIO_InitStructure .GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 ;
56
+ GPIO_InitStructure .GPIO_Mode = GPIO_Mode_AF ;
57
+ GPIO_InitStructure .GPIO_OType = GPIO_OType_OD ;
58
+ GPIO_InitStructure .GPIO_Speed = GPIO_Speed_25MHz ;
59
+ GPIO_InitStructure .GPIO_PuPd = GPIO_PuPd_NOPULL ;
60
+ GPIO_Init (GPIOB , & GPIO_InitStructure );
63
61
62
+ // alternate functions for SCL and SDA
63
+ GPIO_PinAFConfig (GPIOB , GPIO_PinSource6 , GPIO_AF_I2C1 );
64
+ GPIO_PinAFConfig (GPIOB , GPIO_PinSource7 , GPIO_AF_I2C1 );
64
65
65
- i2c1_port_initialized = true;
66
- }
67
66
68
- if (i2c_port == PYB_I2C_2 ) {
69
- if (i2c2_port_initialized == true) return true;
70
- RCC -> APB1ENR |= RCC_APB1ENR_I2C2EN ; // enable I2C2
67
+ i2c1_port_initialized = true;
68
+ }
71
69
72
- // PB10=SCL, PB11=SDA
73
- GPIO_InitStructure .GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 ;
74
- GPIO_InitStructure .GPIO_Mode = GPIO_Mode_AF ;
75
- GPIO_InitStructure .GPIO_OType = GPIO_OType_OD ;
76
- GPIO_InitStructure .GPIO_Speed = GPIO_Speed_25MHz ;
77
- GPIO_InitStructure .GPIO_PuPd = GPIO_PuPd_NOPULL ;
78
- GPIO_Init (GPIOB , & GPIO_InitStructure );
70
+ if (i2c_port == PYB_I2C_2 ) {
71
+ if (i2c2_port_initialized == true) {
72
+ return true;
73
+ }
74
+ RCC -> APB1ENR |= RCC_APB1ENR_I2C2EN ; // enable I2C2
79
75
80
- // alternate functions for SCL and SDA
81
- GPIO_PinAFConfig (GPIOB , GPIO_PinSource10 , GPIO_AF_I2C2 );
82
- GPIO_PinAFConfig (GPIOB , GPIO_PinSource11 , GPIO_AF_I2C2 );
76
+ // PB10=SCL, PB11=SDA
77
+ GPIO_InitStructure .GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 ;
78
+ GPIO_InitStructure .GPIO_Mode = GPIO_Mode_AF ;
79
+ GPIO_InitStructure .GPIO_OType = GPIO_OType_OD ;
80
+ GPIO_InitStructure .GPIO_Speed = GPIO_Speed_25MHz ;
81
+ GPIO_InitStructure .GPIO_PuPd = GPIO_PuPd_NOPULL ;
82
+ GPIO_Init (GPIOB , & GPIO_InitStructure );
83
83
84
+ // alternate functions for SCL and SDA
85
+ GPIO_PinAFConfig (GPIOB , GPIO_PinSource10 , GPIO_AF_I2C2 );
86
+ GPIO_PinAFConfig (GPIOB , GPIO_PinSource11 , GPIO_AF_I2C2 );
84
87
85
- i2c2_port_initialized = true;
86
- }
88
+ i2c2_port_initialized = true;
89
+ }
87
90
88
91
// get clock speeds
89
92
RCC_ClocksTypeDef rcc_clocks ;
@@ -105,23 +108,22 @@ static bool _i2c_init (pyb_i2c_t i2c_port)
105
108
// enable the I2C peripheral
106
109
i2c -> CR1 |= I2C_CR1_PE ;
107
110
108
-
109
- return true;
111
+ return true;
110
112
}
111
113
112
114
static uint32_t _i2c_get_sr (pyb_i2c_t i2c_port ) {
113
115
// must read SR1 first, then SR2, as the read can clear some flags
114
- I2C_TypeDef * i2c = _i2c_port_addr (i2c_port );
115
- if (i2c == NULL ) return 0 ;
116
+ I2C_TypeDef * i2c = _i2c_port_addr (i2c_port );
117
+ if (i2c == NULL ) return 0 ;
116
118
117
119
uint32_t sr1 = i2c -> SR1 ;
118
120
uint32_t sr2 = i2c -> SR2 ;
119
121
return (sr2 << 16 ) | sr1 ;
120
122
}
121
123
122
124
static bool _i2c_restart (pyb_i2c_t i2c_port , uint8_t addr , int write ) {
123
- I2C_TypeDef * i2c = _i2c_port_addr (i2c_port );
124
- if (i2c == NULL ) return false;
125
+ I2C_TypeDef * i2c = _i2c_port_addr (i2c_port );
126
+ if (i2c == NULL ) return false;
125
127
126
128
// send start condition
127
129
i2c -> CR1 |= I2C_CR1_START ;
@@ -162,8 +164,8 @@ static bool _i2c_restart(pyb_i2c_t i2c_port, uint8_t addr, int write) {
162
164
}
163
165
164
166
static bool _i2c_send_byte (pyb_i2c_t i2c_port , uint8_t data ) {
165
- I2C_TypeDef * i2c = _i2c_port_addr (i2c_port );
166
- if (i2c == NULL ) return false;
167
+ I2C_TypeDef * i2c = _i2c_port_addr (i2c_port );
168
+ if (i2c == NULL ) return false;
167
169
168
170
// send byte
169
171
i2c -> DR = data ;
@@ -179,8 +181,8 @@ static bool _i2c_send_byte(pyb_i2c_t i2c_port, uint8_t data) {
179
181
}
180
182
181
183
static uint8_t _i2c_read_ack (pyb_i2c_t i2c_port ) {
182
- I2C_TypeDef * i2c = _i2c_port_addr (i2c_port );
183
- if (i2c == NULL ) return 0 ;
184
+ I2C_TypeDef * i2c = _i2c_port_addr (i2c_port );
185
+ if (i2c == NULL ) return 0 ;
184
186
185
187
// enable ACK of received byte
186
188
i2c -> CR1 |= I2C_CR1_ACK ;
@@ -198,8 +200,8 @@ static uint8_t _i2c_read_ack(pyb_i2c_t i2c_port) {
198
200
}
199
201
200
202
static uint8_t _i2c_read_nack (pyb_i2c_t i2c_port ) {
201
- I2C_TypeDef * i2c = _i2c_port_addr (i2c_port );
202
- if (i2c == NULL ) return 0 ;
203
+ I2C_TypeDef * i2c = _i2c_port_addr (i2c_port );
204
+ if (i2c == NULL ) return 0 ;
203
205
204
206
// disable ACK of received byte (to indicate end of receiving)
205
207
i2c -> CR1 &= (uint16_t )~((uint16_t )I2C_CR1_ACK );
@@ -219,8 +221,8 @@ static uint8_t _i2c_read_nack(pyb_i2c_t i2c_port) {
219
221
}
220
222
221
223
static bool _i2c_start (pyb_i2c_t i2c_port ) {
222
- I2C_TypeDef * i2c = _i2c_port_addr (i2c_port );
223
- if (i2c == NULL ) return false;
224
+ I2C_TypeDef * i2c = _i2c_port_addr (i2c_port );
225
+ if (i2c == NULL ) return false;
224
226
225
227
// wait until I2C is not busy
226
228
uint32_t timeout = 1000000 ;
@@ -233,8 +235,8 @@ static bool _i2c_start(pyb_i2c_t i2c_port) {
233
235
}
234
236
235
237
static void _i2c_stop (pyb_i2c_t i2c_port ) {
236
- I2C_TypeDef * i2c = _i2c_port_addr (i2c_port );
237
- if (i2c == NULL ) return ;
238
+ I2C_TypeDef * i2c = _i2c_port_addr (i2c_port );
239
+ if (i2c == NULL ) return ;
238
240
239
241
// send stop condition
240
242
i2c -> CR1 |= I2C_CR1_STOP ;
@@ -259,62 +261,62 @@ void i2c_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp
259
261
mp_obj_t i2c_obj_start (mp_obj_t self_in ) {
260
262
pyb_i2c_obj_t * self = self_in ;
261
263
if (self -> i2c_state != I2C_STATE_IDLE ) {
262
- _i2c_stop (self -> i2c_port );
263
- self -> i2c_state = I2C_STATE_IDLE ;
264
+ _i2c_stop (self -> i2c_port );
265
+ self -> i2c_state = I2C_STATE_IDLE ;
264
266
}
265
267
if (_i2c_start (self -> i2c_port ) == true)
266
- return mp_const_true ;
268
+ return mp_const_true ;
267
269
return mp_const_false ;
268
270
}
269
271
270
272
mp_obj_t i2c_obj_write (mp_obj_t self_in , mp_obj_t data_in ) {
271
273
pyb_i2c_obj_t * self = self_in ;
272
- if (self -> i2c_state != I2C_STATE_WRITE ) {
273
- if (_i2c_restart (self -> i2c_port , self -> i2c_addr , 1 ) == false) {
274
- _i2c_stop (self -> i2c_port );
275
- self -> i2c_state = I2C_STATE_IDLE ;
276
- return mp_const_false ;
277
- }
278
- self -> i2c_state = I2C_STATE_WRITE ;
279
- }
280
- uint8_t data = mp_obj_get_int (data_in );
281
- if (_i2c_send_byte (self -> i2c_port , data ) == false)
282
- return mp_const_false ;
283
- return mp_const_true ;
274
+ if (self -> i2c_state != I2C_STATE_WRITE ) {
275
+ if (_i2c_restart (self -> i2c_port , self -> i2c_addr , 1 ) == false) {
276
+ _i2c_stop (self -> i2c_port );
277
+ self -> i2c_state = I2C_STATE_IDLE ;
278
+ return mp_const_false ;
279
+ }
280
+ self -> i2c_state = I2C_STATE_WRITE ;
281
+ }
282
+ uint8_t data = mp_obj_get_int (data_in );
283
+ if (_i2c_send_byte (self -> i2c_port , data ) == false)
284
+ return mp_const_false ;
285
+ return mp_const_true ;
284
286
}
285
287
286
288
mp_obj_t i2c_obj_read (mp_obj_t self_in ) {
287
289
pyb_i2c_obj_t * self = self_in ;
288
- if (self -> i2c_state != I2C_STATE_READ ) {
289
- if (_i2c_restart (self -> i2c_port , self -> i2c_addr , 0 ) == false) {
290
- _i2c_stop (self -> i2c_port );
291
- self -> i2c_state = I2C_STATE_IDLE ;
292
- return mp_const_false ;
293
- }
294
- self -> i2c_state = I2C_STATE_READ ;
295
- }
296
- uint8_t data = _i2c_read_ack (self -> i2c_port );
297
- return mp_obj_new_int (data );
290
+ if (self -> i2c_state != I2C_STATE_READ ) {
291
+ if (_i2c_restart (self -> i2c_port , self -> i2c_addr , 0 ) == false) {
292
+ _i2c_stop (self -> i2c_port );
293
+ self -> i2c_state = I2C_STATE_IDLE ;
294
+ return mp_const_false ;
295
+ }
296
+ self -> i2c_state = I2C_STATE_READ ;
297
+ }
298
+ uint8_t data = _i2c_read_ack (self -> i2c_port );
299
+ return mp_obj_new_int (data );
298
300
}
299
301
300
302
mp_obj_t i2c_obj_readAndStop (mp_obj_t self_in ) {
301
303
pyb_i2c_obj_t * self = self_in ;
302
- if (self -> i2c_state != I2C_STATE_READ ) {
303
- if (_i2c_restart (self -> i2c_port , self -> i2c_addr , 0 ) == false) {
304
- _i2c_stop (self -> i2c_port );
305
- self -> i2c_state = I2C_STATE_IDLE ;
306
- return mp_const_false ;
307
- }
308
- }
309
- uint8_t data = _i2c_read_nack (self -> i2c_port );
310
- self -> i2c_state = I2C_STATE_IDLE ;
311
- return mp_obj_new_int (data );
304
+ if (self -> i2c_state != I2C_STATE_READ ) {
305
+ if (_i2c_restart (self -> i2c_port , self -> i2c_addr , 0 ) == false) {
306
+ _i2c_stop (self -> i2c_port );
307
+ self -> i2c_state = I2C_STATE_IDLE ;
308
+ return mp_const_false ;
309
+ }
310
+ }
311
+ uint8_t data = _i2c_read_nack (self -> i2c_port );
312
+ self -> i2c_state = I2C_STATE_IDLE ;
313
+ return mp_obj_new_int (data );
312
314
}
313
315
314
316
mp_obj_t i2c_obj_stop (mp_obj_t self_in ) {
315
317
pyb_i2c_obj_t * self = self_in ;
316
- _i2c_stop (self -> i2c_port );
317
- self -> i2c_state = I2C_STATE_IDLE ;
318
+ _i2c_stop (self -> i2c_port );
319
+ self -> i2c_state = I2C_STATE_IDLE ;
318
320
return mp_const_none ;
319
321
}
320
322
@@ -335,9 +337,9 @@ static const mp_obj_type_t i2c_obj_type = {
335
337
NULL , // iternext
336
338
{ // method list
337
339
{ "start" , & i2c_obj_start_obj },
338
- { "write" , & i2c_obj_write_obj },
339
- { "read" , & i2c_obj_read_obj },
340
- { "readAndStop" , & i2c_obj_readAndStop_obj },
340
+ { "write" , & i2c_obj_write_obj },
341
+ { "read" , & i2c_obj_read_obj },
342
+ { "readAndStop" , & i2c_obj_readAndStop_obj },
341
343
{ "stop" , & i2c_obj_stop_obj },
342
344
{ NULL , NULL },
343
345
}
@@ -347,14 +349,15 @@ static const mp_obj_type_t i2c_obj_type = {
347
349
// currently support either I2C1 (i2c_id = 0) or I2C2 (i2c_id = 1)
348
350
349
351
mp_obj_t pyb_I2C (mp_obj_t i2c_id , mp_obj_t i2c_addr ) {
350
- pyb_i2c_t i2c_port ;
351
- switch (mp_obj_get_int (i2c_id )) {
352
- case 0 : i2c_port = PYB_I2C_1 ; break ;
353
- case 1 : i2c_port = PYB_I2C_2 ; break ;
354
- default : return mp_const_none ;
355
- }
356
- if (_i2c_init (i2c_port ) == false)
357
- return mp_const_none ;
352
+ pyb_i2c_t i2c_port ;
353
+ switch (mp_obj_get_int (i2c_id )) {
354
+ case 0 : i2c_port = PYB_I2C_1 ; break ;
355
+ case 1 : i2c_port = PYB_I2C_2 ; break ;
356
+ default : return mp_const_none ;
357
+ }
358
+ if (_i2c_init (i2c_port ) == false) {
359
+ return mp_const_none ;
360
+ }
358
361
pyb_i2c_obj_t * o = m_new_obj (pyb_i2c_obj_t );
359
362
o -> base .type = & i2c_obj_type ;
360
363
o -> i2c_port = i2c_port ;
0 commit comments