3535#include "lib/utils/context_manager_helpers.h"
3636#include "py/mperrno.h"
3737#include "py/runtime.h"
38+
3839//| .. currentmodule:: bitbangio
3940//|
4041//| :class:`I2C` --- Two wire serial protocol
4950//| :param ~microcontroller.Pin scl: The clock pin
5051//| :param ~microcontroller.Pin sda: The data pin
5152//| :param int frequency: The clock frequency of the bus
53+ //| :param int timeout: The maximum clock stretching timeout in microseconds
5254//|
5355STATIC 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 ) {
5456mp_arg_check_num (n_args ,n_kw ,0 ,MP_OBJ_FUN_ARGS_MAX , true);
@@ -57,19 +59,20 @@ STATIC mp_obj_t bitbangio_i2c_make_new(const mp_obj_type_t *type, size_t n_args,
5759self -> base .type = & bitbangio_i2c_type ;
5860mp_map_t kw_args ;
5961mp_map_init_fixed_table (& kw_args ,n_kw ,pos_args + n_args );
60- enum {ARG_scl ,ARG_sda ,ARG_frequency };
62+ enum {ARG_scl ,ARG_sda ,ARG_frequency , ARG_timeout };
6163static const mp_arg_t allowed_args []= {
6264 {MP_QSTR_scl ,MP_ARG_REQUIRED |MP_ARG_OBJ },
6365 {MP_QSTR_sda ,MP_ARG_REQUIRED |MP_ARG_OBJ },
6466 {MP_QSTR_frequency ,MP_ARG_KW_ONLY |MP_ARG_INT , {.u_int = 400000 } },
67+ {MP_QSTR_timeout ,MP_ARG_KW_ONLY |MP_ARG_INT , {.u_int = 255 } },
6568 };
6669mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
6770mp_arg_parse_all (n_args ,pos_args ,& kw_args ,MP_ARRAY_SIZE (allowed_args ),allowed_args ,args );
6871assert_pin (args [ARG_scl ].u_obj , false);
6972assert_pin (args [ARG_sda ].u_obj , false);
7073const mcu_pin_obj_t * scl = MP_OBJ_TO_PTR (args [ARG_scl ].u_obj );
7174const mcu_pin_obj_t * sda = MP_OBJ_TO_PTR (args [ARG_sda ].u_obj );
72- shared_module_bitbangio_i2c_construct (self ,scl ,sda ,args [ARG_frequency ].u_int );
75+ shared_module_bitbangio_i2c_construct (self ,scl ,sda ,args [ARG_frequency ].u_int , args [ ARG_timeout ]. u_int );
7376return (mp_obj_t )self ;
7477}
7578