aboutsummaryrefslogtreecommitdiff
path: root/extmod/machine_i2c.c
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2020-09-16 13:30:48 +1000
committerDamien George <damien@micropython.org>2020-10-01 12:57:10 +1000
commitc35deb2625efc877b3a0d03d5654e27232b2d101 (patch)
treee3706c52baba2498fae9db769ea5167f321ce427 /extmod/machine_i2c.c
parentc711c0049e5f12cae048d2b0e77bc70e68804ea5 (diff)
extmod/machine_i2c: Rename type to SoftI2C and add custom print method.
Also rename machine_i2c_type to mp_machine_soft_i2c_type. These changes make it clear that it's a soft-I2C implementation, and match SoftSPI. Signed-off-by: Damien George <damien@micropython.org>
Diffstat (limited to 'extmod/machine_i2c.c')
-rw-r--r--extmod/machine_i2c.c17
1 files changed, 12 insertions, 5 deletions
diff --git a/extmod/machine_i2c.c b/extmod/machine_i2c.c
index c804cf570..8aad001f1 100644
--- a/extmod/machine_i2c.c
+++ b/extmod/machine_i2c.c
@@ -302,6 +302,12 @@ STATIC int mp_machine_i2c_writeto(mp_obj_base_t *self, uint16_t addr, const uint
/******************************************************************************/
// MicroPython bindings for I2C
+STATIC void mp_machine_soft_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
+ mp_machine_soft_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ mp_printf(print, "SoftI2C(scl=" MP_HAL_PIN_FMT ", sda=" MP_HAL_PIN_FMT ", freq=%u)",
+ mp_hal_pin_name(self->scl), mp_hal_pin_name(self->sda), 500000 / self->us_delay);
+}
+
STATIC void machine_i2c_obj_init_helper(machine_i2c_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
enum { ARG_scl, ARG_sda, ARG_freq, ARG_timeout };
static const mp_arg_t allowed_args[] = {
@@ -318,7 +324,7 @@ STATIC void machine_i2c_obj_init_helper(machine_i2c_obj_t *self, size_t n_args,
mp_hal_i2c_init(self, args[ARG_freq].u_int);
}
-STATIC mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
+STATIC mp_obj_t mp_machine_soft_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) {
// check the id argument, if given
if (n_args > 0) {
if (args[0] != MP_OBJ_NEW_SMALL_INT(-1)) {
@@ -336,7 +342,7 @@ STATIC mp_obj_t machine_i2c_make_new(const mp_obj_type_t *type, size_t n_args, s
// create new soft I2C object
machine_i2c_obj_t *self = m_new_obj(machine_i2c_obj_t);
- self->base.type = &machine_i2c_type;
+ self->base.type = &mp_machine_soft_i2c_type;
mp_map_t kw_args;
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
machine_i2c_obj_init_helper(self, n_args, args, &kw_args);
@@ -700,10 +706,11 @@ STATIC const mp_machine_i2c_p_t mp_machine_soft_i2c_p = {
.transfer = mp_machine_soft_i2c_transfer,
};
-const mp_obj_type_t machine_i2c_type = {
+const mp_obj_type_t mp_machine_soft_i2c_type = {
{ &mp_type_type },
- .name = MP_QSTR_I2C,
- .make_new = machine_i2c_make_new,
+ .name = MP_QSTR_SoftI2C,
+ .print = mp_machine_soft_i2c_print,
+ .make_new = mp_machine_soft_i2c_make_new,
.protocol = &mp_machine_soft_i2c_p,
.locals_dict = (mp_obj_dict_t *)&mp_machine_soft_i2c_locals_dict,
};