Skip to content

Commit

Permalink
Add i2c.init to change the pins and frequency
Browse files Browse the repository at this point in the history
  • Loading branch information
deshipu committed Nov 11, 2016
1 parent b38c3ca commit e88e3b2
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/i2c.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ should be described separately in that device's documentation.
Functions
=========

.. py:function:: init(freq=100000, sda=pin20, scl=pin19)
Re-initialize peripheral with the specified clock frequency ``freq`` on the
specified ``sda`` and ``scl`` pins.

.. warning::

Changing the I²C pins from defaults will make the accelerometer and
compass stop working, as they are connected internally to those pins.


.. py:function:: read(addr, n, repeat=False)
Expand Down
3 changes: 3 additions & 0 deletions inc/genhdr/qstrdefs.generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -510,6 +510,9 @@ QDEF(MP_QSTR_addr, (const byte*)"\xb6\x04" "addr")
QDEF(MP_QSTR_n, (const byte*)"\xcb\x01" "n")
QDEF(MP_QSTR_buf, (const byte*)"\x74\x03" "buf")
QDEF(MP_QSTR_repeat, (const byte*)"\xf2\x06" "repeat")
QDEF(MP_QSTR_freq, (const byte*)"\xe5\x04" "freq")
QDEF(MP_QSTR_sda, (const byte*)"\x53\x03" "sda")
QDEF(MP_QSTR_scl, (const byte*)"\xf9\x03" "scl")
QDEF(MP_QSTR_music, (const byte*)"\x04\x05" "music")
QDEF(MP_QSTR_frequency, (const byte*)"\xa1\x09" "frequency")
QDEF(MP_QSTR_duration, (const byte*)"\x7b\x08" "duration")
Expand Down
1 change: 1 addition & 0 deletions inc/microbit/modmicrobit.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ MP_DECLARE_CONST_FUN_OBJ(microbit_pin_write_analog_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_pin_is_touched_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_pin_set_analog_period_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_pin_set_analog_period_microseconds_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_i2c_init_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_i2c_read_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_i2c_write_obj);
MP_DECLARE_CONST_FUN_OBJ(microbit_image_width_obj);
Expand Down
3 changes: 3 additions & 0 deletions inc/microbit/qstrdefsport.h
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,9 @@ Q(addr)
Q(n)
Q(buf)
Q(repeat)
Q(freq)
Q(sda)
Q(scl)

Q(music)
Q(frequency)
Expand Down
46 changes: 46 additions & 0 deletions source/microbit/microbiti2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,62 @@
*/

#include "MicroBit.h"
#include "i2c_api.h"


class mp_I2C : public MicroBitI2C {
public:
void set_pins(PinName sda, PinName scl);
};

void mp_I2C::set_pins(PinName sda, PinName scl) {
_i2c.sda = sda;
_i2c.scl = scl;
}


extern "C" {

#include "py/runtime.h"
#include "modmicrobit.h"
#include "microbitobj.h"


typedef struct _microbit_i2c_obj_t {
mp_obj_base_t base;
MicroBitI2C *i2c;
} microbit_i2c_obj_t;

STATIC mp_obj_t microbit_i2c_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_freq, MP_ARG_INT, {.u_int = 100000} },
{ MP_QSTR_sda, MP_ARG_OBJ, {.u_obj = mp_const_none } },
{ MP_QSTR_scl, MP_ARG_OBJ, {.u_obj = mp_const_none } },
};

// parse args
microbit_i2c_obj_t *self = (microbit_i2c_obj_t*)pos_args[0];
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);


PinName p_sda = MICROBIT_PIN_SDA;
PinName p_scl = MICROBIT_PIN_SCL;

if (args[1].u_obj != mp_const_none) {
p_sda = microbit_obj_get_pin_name(args[1].u_obj);
}
if (args[2].u_obj != mp_const_none) {
p_scl = microbit_obj_get_pin_name(args[2].u_obj);
}
((mp_I2C*)self->i2c)->set_pins(p_sda, p_scl);

self->i2c->frequency(args[0].u_int); // also does i2c_reset()

return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_KW(microbit_i2c_init_obj, 1, microbit_i2c_init);

STATIC mp_obj_t microbit_i2c_read(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
Expand Down Expand Up @@ -85,6 +130,7 @@ STATIC mp_obj_t microbit_i2c_write(mp_uint_t n_args, const mp_obj_t *pos_args, m
MP_DEFINE_CONST_FUN_OBJ_KW(microbit_i2c_write_obj, 1, microbit_i2c_write);

STATIC const mp_map_elem_t microbit_i2c_locals_dict_table[] = {
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&microbit_i2c_init_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&microbit_i2c_read_obj },
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&microbit_i2c_write_obj },
};
Expand Down

0 comments on commit e88e3b2

Please sign in to comment.