@ -318,7 +318,7 @@ MP_DEFINE_CONST_FUN_OBJ_KW(machine_i2c_init_obj, 1, machine_i2c_obj_init);
STATIC mp_obj_t machine_i2c_scan ( mp_obj_t self_in ) {
mp_obj_base_t * self = MP_OBJ_TO_PTR ( self_in ) ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) self - > type - > protocol ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) mp_proto_get ( self , QSTR_protocol_i2c ) ;
mp_obj_t list = mp_obj_new_list ( 0 , NULL ) ;
// 7-bit addresses 0b0000xxx and 0b1111xxx are reserved
for ( int addr = 0x08 ; addr < 0x78 ; + + addr ) {
@ -333,7 +333,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_scan_obj, machine_i2c_scan);
STATIC mp_obj_t machine_i2c_start ( mp_obj_t self_in ) {
mp_obj_base_t * self = ( mp_obj_base_t * ) MP_OBJ_TO_PTR ( self_in ) ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) self - > type - > protocol ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) mp_proto_get ( self , QSTR_protocol_i2c ) ;
if ( i2c_p - > start = = NULL ) {
mp_raise_msg ( & mp_type_OSError , translate ( " I2C operation not supported " ) ) ;
}
@ -347,7 +347,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_start_obj, machine_i2c_start);
STATIC mp_obj_t machine_i2c_stop ( mp_obj_t self_in ) {
mp_obj_base_t * self = ( mp_obj_base_t * ) MP_OBJ_TO_PTR ( self_in ) ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) self - > type - > protocol ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) mp_proto_get ( self , QSTR_protocol_i2c ) ;
if ( i2c_p - > stop = = NULL ) {
mp_raise_msg ( & mp_type_OSError , translate ( " I2C operation not supported " ) ) ;
}
@ -361,7 +361,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(machine_i2c_stop_obj, machine_i2c_stop);
STATIC mp_obj_t machine_i2c_readinto ( size_t n_args , const mp_obj_t * args ) {
mp_obj_base_t * self = ( mp_obj_base_t * ) MP_OBJ_TO_PTR ( args [ 0 ] ) ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) self - > type - > protocol ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) mp_proto_get ( self , QSTR_protocol_i2c ) ;
if ( i2c_p - > read = = NULL ) {
mp_raise_msg ( & mp_type_OSError , translate ( " I2C operation not supported " ) ) ;
}
@ -385,7 +385,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_readinto_obj, 2, 3, machine_i2c_
STATIC mp_obj_t machine_i2c_write ( mp_obj_t self_in , mp_obj_t buf_in ) {
mp_obj_base_t * self = ( mp_obj_base_t * ) MP_OBJ_TO_PTR ( self_in ) ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) self - > type - > protocol ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) mp_proto_get ( self , QSTR_protocol_i2c ) ;
if ( i2c_p - > write = = NULL ) {
mp_raise_msg ( & mp_type_OSError , translate ( " I2C operation not supported " ) ) ;
}
@ -407,7 +407,7 @@ MP_DEFINE_CONST_FUN_OBJ_2(machine_i2c_write_obj, machine_i2c_write);
STATIC mp_obj_t machine_i2c_readfrom ( size_t n_args , const mp_obj_t * args ) {
mp_obj_base_t * self = ( mp_obj_base_t * ) MP_OBJ_TO_PTR ( args [ 0 ] ) ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) self - > type - > protocol ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) mp_proto_get ( self , QSTR_protocol_i2c ) ;
mp_int_t addr = mp_obj_get_int ( args [ 1 ] ) ;
vstr_t vstr ;
vstr_init_len ( & vstr , mp_obj_get_int ( args [ 2 ] ) ) ;
@ -422,7 +422,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_readfrom_obj, 3, 4, machine_i2c_
STATIC mp_obj_t machine_i2c_readfrom_into ( size_t n_args , const mp_obj_t * args ) {
mp_obj_base_t * self = ( mp_obj_base_t * ) MP_OBJ_TO_PTR ( args [ 0 ] ) ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) self - > type - > protocol ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) mp_proto_get ( self , QSTR_protocol_i2c ) ;
mp_int_t addr = mp_obj_get_int ( args [ 1 ] ) ;
mp_buffer_info_t bufinfo ;
mp_get_buffer_raise ( args [ 2 ] , & bufinfo , MP_BUFFER_WRITE ) ;
@ -437,7 +437,7 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_readfrom_into_obj, 3, 4, machine
STATIC mp_obj_t machine_i2c_writeto ( size_t n_args , const mp_obj_t * args ) {
mp_obj_base_t * self = ( mp_obj_base_t * ) MP_OBJ_TO_PTR ( args [ 0 ] ) ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) self - > type - > protocol ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) mp_proto_get ( self , QSTR_protocol_i2c ) ;
mp_int_t addr = mp_obj_get_int ( args [ 1 ] ) ;
mp_buffer_info_t bufinfo ;
mp_get_buffer_raise ( args [ 2 ] , & bufinfo , MP_BUFFER_READ ) ;
@ -453,7 +453,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_i2c_writeto_obj, 3, 4, machin
STATIC int read_mem ( mp_obj_t self_in , uint16_t addr , uint32_t memaddr , uint8_t addrsize , uint8_t * buf , size_t len ) {
mp_obj_base_t * self = ( mp_obj_base_t * ) MP_OBJ_TO_PTR ( self_in ) ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) self - > type - > protocol ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) mp_proto_get ( self , QSTR_protocol_i2c ) ;
uint8_t memaddr_buf [ 4 ] ;
size_t memaddr_len = 0 ;
for ( int16_t i = addrsize - 8 ; i > = 0 ; i - = 8 ) {
@ -473,7 +473,7 @@ STATIC int read_mem(mp_obj_t self_in, uint16_t addr, uint32_t memaddr, uint8_t a
STATIC int write_mem ( mp_obj_t self_in , uint16_t addr , uint32_t memaddr , uint8_t addrsize , const uint8_t * buf , size_t len ) {
mp_obj_base_t * self = ( mp_obj_base_t * ) MP_OBJ_TO_PTR ( self_in ) ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) self - > type - > protocol ;
mp_machine_i2c_p_t * i2c_p = ( mp_machine_i2c_p_t * ) mp_proto_get ( self , QSTR_protocol_i2c ) ;
// need some memory to create the buffer to send; try to use stack if possible
uint8_t buf2_stack [ MAX_MEMADDR_SIZE + BUF_STACK_SIZE ] ;
@ -621,6 +621,7 @@ int mp_machine_soft_i2c_write(mp_obj_base_t *self_in, const uint8_t *src, size_t
}
STATIC const mp_machine_i2c_p_t mp_machine_soft_i2c_p = {
MP_PROTO_IMPLEMENT ( MP_QSTR_protocol_i2c )
. start = ( int ( * ) ( mp_obj_base_t * ) ) mp_hal_i2c_start ,
. stop = ( int ( * ) ( mp_obj_base_t * ) ) mp_hal_i2c_stop ,
. read = mp_machine_soft_i2c_read ,