10000 Add CODECONVENTIONS, and modify i2c module to conform. · jpralves/micropython@7f7636e · GitHub
[go: up one dir, main page]

Skip to content

Commit 7f7636e

Browse files
committed
Add CODECONVENTIONS, and modify i2c module to conform.
1 parent 1e6a258 commit 7f7636e

File tree

2 files changed

+161
-110
lines changed

2 files changed

+161
-110
lines changed

CODECONVENTIONS.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Code conventions
2+
================
3+
4+
When writing new code, please adhere to the following conventions.
5+
6+
White space:
7+
- Expand tabs to 4 spaces.
8+
- Don't leave trailing whitespace at the end of a line.
9+
- For control blocks (if, for, while), put 1 space between the
10+
keyword and the opening parenthesis.
11+
- Put 1 space after a comma, and 1 space around operators.
12+
13+
Braces:
14+
- Use braces for all blocks, even no-line and single-line pieces of
15+
code.
16+
- Put opening braces on the end of the line it belongs to, not on
17+
a new line.
18+
- For else-statements, put the else on the same line as the previous
19+
closing brace.
20+
21+
Include directives:
22+
- Don't include within a header file.
23+
24+
Type names and declarations:
25+
- When defining a type, put '_t' after it.
26+
27+
Examples
28+
--------
29+
30+
Braces and spaces:
31+
32+
int foo(int x, int y) {
33+
if (x < y) {
34+
foo(y, x);
35+
} else {
36+
foo(x + 1, y - 1);
37+
}
38+
39+
for (int i = 0; i < x; i++) {
40+
}
41+
}
42+
43+
Type declarations:
44+
45+
typedef struct _my_struct_t {
46+
int member;
47+
void *data;
48+
} my_struct_t;

stm/i2c.c

Lines changed: 113 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -15,75 +15,78 @@ typedef enum {
1515
} pyb_i2c_t;
1616

1717
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,
2121
} i2c_state_t;
2222

2323
// set to true if the port has already been initialized
2424
bool i2c1_port_initialized = false;
2525
bool i2c2_port_initialized = false;
2626

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;
3435
}
3536

3637
// todo - perhaps there should be some global resource management for gpio
3738
// this function would fail if the i2c pins have already been defined for
3839
// use by another python object
3940
// 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) {
4242
GPIO_InitTypeDef GPIO_InitStructure;
4343

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;
5147

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
5953

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);
6361

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);
6465

65-
i2c1_port_initialized = true;
66-
}
6766

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+
}
7169

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
7975

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);
8383

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);
8487

85-
i2c2_port_initialized = true;
86-
}
88+
i2c2_port_initialized = true;
89+
}
8790

8891
// get clock speeds
8992
RCC_ClocksTypeDef rcc_clocks;
@@ -105,23 +108,22 @@ static bool _i2c_init (pyb_i2c_t i2c_port)
105108
// enable the I2C peripheral
106109
i2c->CR1 |= I2C_CR1_PE;
107110

108-
109-
return true;
111+
return true;
110112
}
111113

112114
static uint32_t _i2c_get_sr(pyb_i2c_t i2c_port) {
113115
// 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;
116118

117119
uint32_t sr1 = i2c->SR1;
118120
uint32_t sr2 = i2c->SR2;
119121
return (sr2 << 16) | sr1;
120122
}
121123

122124
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;
125127

126128
// send start condition
127129
i2c->CR1 |= I2C_CR1_START;
@@ -162,8 +164,8 @@ static bool _i2c_restart(pyb_i2c_t i2c_port, uint8_t addr, int write) {
162164
}
163165

164166
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;
167169

168170
// send byte
169171
i2c->DR = data;
@@ -179,8 +181,8 @@ static bool _i2c_send_byte(pyb_i2c_t i2c_port, uint8_t data) {
179181
}
180182

181183
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;
184186

185187
// enable ACK of received byte
186188
i2c->CR1 |= I2C_CR1_ACK;
@@ -198,8 +200,8 @@ static uint8_t _i2c_read_ack(pyb_i2c_t i2c_port) {
198200
}
199201

200202
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;
203205

204206
// disable ACK of received byte (to indicate end of receiving)
205207
i2c->CR1 &= (uint16_t)~((uint16_t)I2C_CR1_ACK);
@@ -219,8 +221,8 @@ static uint8_t _i2c_read_nack(pyb_i2c_t i2c_port) {
219221
}
220222

221223
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;
224226

225227
// wait until I2C is not busy
226228
uint32_t timeout = 1000000;
@@ -233,8 +235,8 @@ static bool _i2c_start(pyb_i2c_t i2c_port) {
233235
}
234236

235237
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;
238240

239241
// send stop condition
240242
i2c->CR1 |= I2C_CR1_STOP;
@@ -259,62 +261,62 @@ void i2c_obj_print(void (*print)(void *env, const char *fmt, ...), void *env, mp
259261
mp_obj_t i2c_obj_start(mp_obj_t self_in) {
260262
pyb_i2c_obj_t *self = self_in;
261263
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;
264266
}
265267
if (_i2c_start(self->i2c_port) == true)
266-
return mp_const_true;
268+
return mp_const_true;
267269
return mp_const_false;
268270
}
269271

270272
mp_obj_t i2c_obj_write(mp_obj_t self_in, mp_obj_t data_in) {
271273
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;
284286
}
285287

286288
mp_obj_t i2c_obj_read(mp_obj_t self_in) {
287289
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);
298300
}
299301

300302
mp_obj_t i2c_obj_readAndStop(mp_obj_t self_in) {
301303
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);
312314
}
313315

314316
mp_obj_t i2c_obj_stop(mp_obj_t self_in) {
315317
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;
318320
return mp_const_none;
319321
}
320322

@@ -335,9 +337,9 @@ static const mp_obj_type_t i2c_obj_type = {
335337
NULL, // iternext
336338
{ // method list
337339
{ "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 },
341343
{ "stop", &i2c_obj_stop_obj },
342344
{ NULL, NULL },
343345
}
@@ -347,14 +349,15 @@ static const mp_obj_type_t i2c_obj_type = {
347349
// currently support either I2C1 (i2c_id = 0) or I2C2 (i2c_id = 1)
348350

349351
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+
}
358361
pyb_i2c_obj_t *o = m_new_obj(pyb_i2c_obj_t);
359362
o->base.type = &i2c_obj_type;
360363
o->i2c_port = i2c_port;

0 commit comments

Comments
 (0)
0