Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MicroBitI2C.set/get_frequency(). #296

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions inc/genhdr/qstrdefs.generated.h
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@ QDEF(MP_QSTR_clear_calibration, (const byte*)"\x49\x11" "clear_calibration")
QDEF(MP_QSTR_get_field_strength, (const byte*)"\xf4\x12" "get_field_strength")
QDEF(MP_QSTR_MicroBitI2C, (const byte*)"\xb8\x0b" "MicroBitI2C")
QDEF(MP_QSTR_i2c, (const byte*)"\x5d\x03" "i2c")
QDEF(MP_QSTR_set_frequency, (const byte*)"\x5c\x0d" "set_frequency")
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")
Expand Down
1 change: 1 addition & 0 deletions inc/microbit/modmicrobit.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,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_set_frequency_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
1 change: 1 addition & 0 deletions inc/microbit/qstrdefsport.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ Q(get_field_strength)

Q(MicroBitI2C)
Q(i2c)
Q(set_frequency)
Q(read)
Q(write)
Q(addr)
Expand Down
1 change: 1 addition & 0 deletions source/microbit/help.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ STATIC const mp_doc_t help_table_instances[] = {
{&microbit_pin_is_touched_obj, "If pin is_touched() on micro:bit, return True. If nothing is touching the pin,\nreturn False.\n"},
// I2C
{&microbit_i2c_obj, "Communicate with one or more named devices connected to micro:bit. Each named\ndevice has an 'address', communicates using I2C, and connects to the I/O pins.\n"},
{&microbit_i2c_set_frequency_obj, "Use set_frequency(f) to set the I2C bus frequency.\n"},
{&microbit_i2c_read_obj, "Use read(address, n) to read 'n' bytes from the device with this address.\n"},
{&microbit_i2c_write_obj, "Use write(address, buffer) to write to the 'buffer' of the device at this 'address'.\n"},
// Image
Expand Down
32 changes: 31 additions & 1 deletion source/microbit/microbiti2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,35 @@ typedef struct _microbit_i2c_obj_t {
MicroBitI2C *i2c;
} microbit_i2c_obj_t;

STATIC mp_obj_t microbit_i2c_set_frequency(mp_obj_t self_in, mp_obj_t frequency_in) {
microbit_i2c_obj_t *self = (microbit_i2c_obj_t*)self_in;
int frequency = mp_obj_get_int(frequency_in);

/* Workaround: This is built with knowledge from
* mbed-classic/targets/hal/TARGET_NORDIC/TARGET_MCU_NRF51822/i2c_api.c
* which clamps the frequency to one of the following values. The mbed
* API doesn't support return values and DAL doesn't support getting the
* frequency (it's protected with no getter). We prefer to raise a
* ValueError when the frequency isn't one of the supported frequencies
* instead of clamping it silently.
*
* Note: If the default frequency changes, please update the initialized
* value at the bottom of this file.
*/
switch (frequency) {
case 100000: /* 100 KHz */
case 250000: /* 250 KHz */
case 400000: /* 400 KHz */
break;
default:
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, "Invalid frequency"));
}

self->i2c->frequency(frequency);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(microbit_i2c_set_frequency_obj, microbit_i2c_set_frequency);

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 +114,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_set_frequency), (mp_obj_t)&microbit_i2c_set_frequency_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 All @@ -111,7 +141,7 @@ const mp_obj_type_t microbit_i2c_type = {

const microbit_i2c_obj_t microbit_i2c_obj = {
{&microbit_i2c_type},
.i2c = &uBit.i2c
.i2c = &uBit.i2c,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can revert the command the line would then be unchanged.

};

}