Skip to content

Commit e88e3b2

Browse files
committed
Add i2c.init to change the pins and frequency
1 parent b38c3ca commit e88e3b2

File tree

5 files changed

+63
-0
lines changed

5 files changed

+63
-0
lines changed

docs/i2c.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ should be described separately in that device's documentation.
2121
Functions
2222
=========
2323

24+
.. py:function:: init(freq=100000, sda=pin20, scl=pin19)
25+
26+
Re-initialize peripheral with the specified clock frequency ``freq`` on the
27+
specified ``sda`` and ``scl`` pins.
28+
29+
.. warning::
30+
31+
Changing the I²C pins from defaults will make the accelerometer and
32+
compass stop working, as they are connected internally to those pins.
33+
2434

2535
.. py:function:: read(addr, n, repeat=False)
2636

inc/genhdr/qstrdefs.generated.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,9 @@ QDEF(MP_QSTR_addr, (const byte*)"\xb6\x04" "addr")
510510
QDEF(MP_QSTR_n, (const byte*)"\xcb\x01" "n")
511511
QDEF(MP_QSTR_buf, (const byte*)"\x74\x03" "buf")
512512
QDEF(MP_QSTR_repeat, (const byte*)"\xf2\x06" "repeat")
513+
QDEF(MP_QSTR_freq, (const byte*)"\xe5\x04" "freq")
514+
QDEF(MP_QSTR_sda, (const byte*)"\x53\x03" "sda")
515+
QDEF(MP_QSTR_scl, (const byte*)"\xf9\x03" "scl")
513516
QDEF(MP_QSTR_music, (const byte*)"\x04\x05" "music")
514517
QDEF(MP_QSTR_frequency, (const byte*)"\xa1\x09" "frequency")
515518
QDEF(MP_QSTR_duration, (const byte*)"\x7b\x08" "duration")

inc/microbit/modmicrobit.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ MP_DECLARE_CONST_FUN_OBJ(microbit_pin_write_analog_obj);
190190
MP_DECLARE_CONST_FUN_OBJ(microbit_pin_is_touched_obj);
191191
MP_DECLARE_CONST_FUN_OBJ(microbit_pin_set_analog_period_obj);
192192
MP_DECLARE_CONST_FUN_OBJ(microbit_pin_set_analog_period_microseconds_obj);
193+
MP_DECLARE_CONST_FUN_OBJ(microbit_i2c_init_obj);
193194
MP_DECLARE_CONST_FUN_OBJ(microbit_i2c_read_obj);
194195
MP_DECLARE_CONST_FUN_OBJ(microbit_i2c_write_obj);
195196
MP_DECLARE_CONST_FUN_OBJ(microbit_image_width_obj);

inc/microbit/qstrdefsport.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ Q(addr)
203203
Q(n)
204204
Q(buf)
205205
Q(repeat)
206+
Q(freq)
207+
Q(sda)
208+
Q(scl)
206209

207210
Q(music)
208211
Q(frequency)

source/microbit/microbiti2c.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,62 @@
2525
*/
2626

2727
#include "MicroBit.h"
28+
#include "i2c_api.h"
29+
30+
31+
class mp_I2C : public MicroBitI2C {
32+
public:
33+
void set_pins(PinName sda, PinName scl);
34+
};
35+
36+
void mp_I2C::set_pins(PinName sda, PinName scl) {
37+
_i2c.sda = sda;
38+
_i2c.scl = scl;
39+
}
40+
2841

2942
extern "C" {
3043

3144
#include "py/runtime.h"
3245
#include "modmicrobit.h"
46+
#include "microbitobj.h"
47+
3348

3449
typedef struct _microbit_i2c_obj_t {
3550
mp_obj_base_t base;
3651
MicroBitI2C *i2c;
3752
} microbit_i2c_obj_t;
3853

54+
STATIC mp_obj_t microbit_i2c_init(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
55+
static const mp_arg_t allowed_args[] = {
56+
{ MP_QSTR_freq, MP_ARG_INT, {.u_int = 100000} },
57+
{ MP_QSTR_sda, MP_ARG_OBJ, {.u_obj = mp_const_none } },
58+
{ MP_QSTR_scl, MP_ARG_OBJ, {.u_obj = mp_const_none } },
59+
};
60+
61+
// parse args
62+
microbit_i2c_obj_t *self = (microbit_i2c_obj_t*)pos_args[0];
63+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
64+
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
65+
66+
67+
PinName p_sda = MICROBIT_PIN_SDA;
68+
PinName p_scl = MICROBIT_PIN_SCL;
69+
70+
if (args[1].u_obj != mp_const_none) {
71+
p_sda = microbit_obj_get_pin_name(args[1].u_obj);
72+
}
73+
if (args[2].u_obj != mp_const_none) {
74+
p_scl = microbit_obj_get_pin_name(args[2].u_obj);
75+
}
76+
((mp_I2C*)self->i2c)->set_pins(p_sda, p_scl);
77+
78+
self->i2c->frequency(args[0].u_int); // also does i2c_reset()
79+
80+
return mp_const_none;
81+
}
82+
MP_DEFINE_CONST_FUN_OBJ_KW(microbit_i2c_init_obj, 1, microbit_i2c_init);
83+
3984
STATIC mp_obj_t microbit_i2c_read(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
4085
static const mp_arg_t allowed_args[] = {
4186
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
@@ -85,6 +130,7 @@ STATIC mp_obj_t microbit_i2c_write(mp_uint_t n_args, const mp_obj_t *pos_args, m
85130
MP_DEFINE_CONST_FUN_OBJ_KW(microbit_i2c_write_obj, 1, microbit_i2c_write);
86131

87132
STATIC const mp_map_elem_t microbit_i2c_locals_dict_table[] = {
133+
{ MP_OBJ_NEW_QSTR(MP_QSTR_init), (mp_obj_t)&microbit_i2c_init_obj },
88134
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), (mp_obj_t)&microbit_i2c_read_obj },
89135
{ MP_OBJ_NEW_QSTR(MP_QSTR_write), (mp_obj_t)&microbit_i2c_write_obj },
90136
};

0 commit comments

Comments
 (0)