Browse Source

Add CODECONVENTIONS, and modify i2c module to conform.

pull/3/head
Damien 11 years ago
parent
commit
7f7636e41c
  1. 48
      CODECONVENTIONS.md
  2. 25
      stm/i2c.c

48
CODECONVENTIONS.md

@ -0,0 +1,48 @@
Code conventions
================
When writing new code, please adhere to the following conventions.
White space:
- Expand tabs to 4 spaces.
- Don't leave trailing whitespace at the end of a line.
- For control blocks (if, for, while), put 1 space between the
keyword and the opening parenthesis.
- Put 1 space after a comma, and 1 space around operators.
Braces:
- Use braces for all blocks, even no-line and single-line pieces of
code.
- Put opening braces on the end of the line it belongs to, not on
a new line.
- For else-statements, put the else on the same line as the previous
closing brace.
Include directives:
- Don't include within a header file.
Type names and declarations:
- When defining a type, put '_t' after it.
Examples
--------
Braces and spaces:
int foo(int x, int y) {
if (x < y) {
foo(y, x);
} else {
foo(x + 1, y - 1);
}
for (int i = 0; i < x; i++) {
}
}
Type declarations:
typedef struct _my_struct_t {
int member;
void *data;
} my_struct_t;

25
stm/i2c.c

@ -24,12 +24,13 @@ typedef enum {
bool i2c1_port_initialized = false;
bool i2c2_port_initialized = false;
static I2C_TypeDef * _i2c_port_addr(pyb_i2c_t i2c_port)
{
if (i2c_port == PYB_I2C_1)
static I2C_TypeDef * _i2c_port_addr(pyb_i2c_t i2c_port) {
if (i2c_port == PYB_I2C_1) {
return I2C1;
if (i2c_port == PYB_I2C_2)
}
if (i2c_port == PYB_I2C_2) {
return I2C2;
}
return NULL;
}
@ -37,8 +38,7 @@ static I2C_TypeDef * _i2c_port_addr(pyb_i2c_t i2c_port)
// this function would fail if the i2c pins have already been defined for
// use by another python object
// as it is, this always returns true (unless i2c_port is invalid)
static bool _i2c_init (pyb_i2c_t i2c_port)
{
static bool _i2c_init(pyb_i2c_t i2c_port) {
GPIO_InitTypeDef GPIO_InitStructure;
I2C_TypeDef *i2c = _i2c_port_addr(i2c_port);
@ -46,7 +46,9 @@ static bool _i2c_init (pyb_i2c_t i2c_port)
return false;
if (i2c_port == PYB_I2C_1) {
if (i2c1_port_initialized == true) return true;
if (i2c1_port_initialized == true) {
return true;
}
RCC->APB1ENR |= RCC_APB1ENR_I2C1EN; // enable I2C1
// PB6=SCL, PB7=SDA
@ -66,7 +68,9 @@ static bool _i2c_init (pyb_i2c_t i2c_port)
}
if (i2c_port == PYB_I2C_2) {
if (i2c2_port_initialized == true) return true;
if (i2c2_port_initialized == true) {
return true;
}
RCC->APB1ENR |= RCC_APB1ENR_I2C2EN; // enable I2C2
// PB10=SCL, PB11=SDA
@ -81,7 +85,6 @@ static bool _i2c_init (pyb_i2c_t i2c_port)
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_I2C2);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_I2C2);
i2c2_port_initialized = true;
}
@ -105,7 +108,6 @@ static bool _i2c_init (pyb_i2c_t i2c_port)
// enable the I2C peripheral
i2c->CR1 |= I2C_CR1_PE;
return true;
}
@ -353,8 +355,9 @@ mp_obj_t pyb_I2C(mp_obj_t i2c_id, mp_obj_t i2c_addr) {
case 1: i2c_port = PYB_I2C_2; break;
default: return mp_const_none;
}
if (_i2c_init(i2c_port) == false)
if (_i2c_init(i2c_port) == false) {
return mp_const_none;
}
pyb_i2c_obj_t *o = m_new_obj(pyb_i2c_obj_t);
o->base.type = &i2c_obj_type;
o->i2c_port = i2c_port;

Loading…
Cancel
Save