|
|
|
@ -35,6 +35,7 @@
|
|
|
|
|
#include "lib/utils/context_manager_helpers.h"
|
|
|
|
|
#include "py/mperrno.h"
|
|
|
|
|
#include "py/runtime.h"
|
|
|
|
|
|
|
|
|
|
//| .. currentmodule:: bitbangio
|
|
|
|
|
//|
|
|
|
|
|
//| :class:`I2C` --- Two wire serial protocol
|
|
|
|
@ -49,6 +50,7 @@
|
|
|
|
|
//| :param ~microcontroller.Pin scl: The clock pin
|
|
|
|
|
//| :param ~microcontroller.Pin sda: The data pin
|
|
|
|
|
//| :param int frequency: The clock frequency of the bus
|
|
|
|
|
//| :param int timeout: The maximum clock stretching timeout in microseconds
|
|
|
|
|
//|
|
|
|
|
|
STATIC mp_obj_t bitbangio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
|
|
|
|
|
mp_arg_check_num(n_args, n_kw, 0, MP_OBJ_FUN_ARGS_MAX, true);
|
|
|
|
@ -57,11 +59,12 @@ STATIC mp_obj_t bitbangio_i2c_make_new(const mp_obj_type_t *type, size_t n_args,
|
|
|
|
|
self->base.type = &bitbangio_i2c_type;
|
|
|
|
|
mp_map_t kw_args;
|
|
|
|
|
mp_map_init_fixed_table(&kw_args, n_kw, pos_args + n_args);
|
|
|
|
|
enum { ARG_scl, ARG_sda, ARG_frequency };
|
|
|
|
|
enum { ARG_scl, ARG_sda, ARG_frequency, ARG_timeout };
|
|
|
|
|
static const mp_arg_t allowed_args[] = {
|
|
|
|
|
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
|
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_OBJ },
|
|
|
|
|
{ MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} },
|
|
|
|
|
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 255} },
|
|
|
|
|
};
|
|
|
|
|
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
|
|
|
|
|
mp_arg_parse_all(n_args, pos_args, &kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
|
|
|
|
@ -69,7 +72,7 @@ STATIC mp_obj_t bitbangio_i2c_make_new(const mp_obj_type_t *type, size_t n_args,
|
|
|
|
|
assert_pin(args[ARG_sda].u_obj, false);
|
|
|
|
|
const mcu_pin_obj_t* scl = MP_OBJ_TO_PTR(args[ARG_scl].u_obj);
|
|
|
|
|
const mcu_pin_obj_t* sda = MP_OBJ_TO_PTR(args[ARG_sda].u_obj);
|
|
|
|
|
shared_module_bitbangio_i2c_construct(self, scl, sda, args[ARG_frequency].u_int);
|
|
|
|
|
shared_module_bitbangio_i2c_construct(self, scl, sda, args[ARG_frequency].u_int, args[ARG_timeout].u_int);
|
|
|
|
|
return (mp_obj_t)self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|