@@ -453,14 +453,15 @@ STATIC mp_obj_t pyb_i2c_recv(uint n_args, const mp_obj_t *args, mp_map_t *kw_arg
453453}
454454STATIC MP_DEFINE_CONST_FUN_OBJ_KW (pyb_i2c_recv_obj , 1 , pyb_i2c_recv );
455455
456- /// \method mem_read(data, addr, memaddr, timeout=5000)
456+ /// \method mem_read(data, addr, memaddr, timeout=5000, addr_size=8 )
457457///
458458/// Read from the memory of an I2C device:
459459///
460460/// - `data` can be an integer or a buffer to read into
461461/// - `addr` is the I2C device address
462462/// - `memaddr` is the memory location within the I2C device
463463/// - `timeout` is the timeout in milliseconds to wait for the read
464+ /// - `addr_size` selects width of memaddr: 8 or 16 bits
464465///
465466/// Returns the read data.
466467/// This is only valid in master mode.
@@ -469,6 +470,7 @@ STATIC const mp_arg_t pyb_i2c_mem_read_args[] = {
469470 { MP_QSTR_addr , MP_ARG_REQUIRED | MP_ARG_INT , {.u_int = 0 } },
470471 { MP_QSTR_memaddr , MP_ARG_REQUIRED | MP_ARG_INT , {.u_int = 0 } },
471472 { MP_QSTR_timeout , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 5000 } },
473+ { MP_QSTR_addr_size , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 8 } },
472474};
473475#define PYB_I2C_MEM_READ_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_mem_read_args)
474476
@@ -490,8 +492,13 @@ STATIC mp_obj_t pyb_i2c_mem_read(uint n_args, const mp_obj_t *args, mp_map_t *kw
490492 // get the addresses
491493 mp_uint_t i2c_addr = vals [1 ].u_int << 1 ;
492494 mp_uint_t mem_addr = vals [2 ].u_int ;
495+ // determine width of mem_addr; default is 8 bits, entering any other value gives 16 bit width
496+ mp_uint_t mem_addr_size = I2C_MEMADD_SIZE_8BIT ;
497+ if (vals [4 ].u_int != 8 ) {
498+ mem_addr_size = I2C_MEMADD_SIZE_16BIT ;
499+ }
493500
494- HAL_StatusTypeDef status = HAL_I2C_Mem_Read (self -> i2c , i2c_addr , mem_addr , I2C_MEMADD_SIZE_8BIT , bufinfo .buf , bufinfo .len , vals [3 ].u_int );
501+ HAL_StatusTypeDef status = HAL_I2C_Mem_Read (self -> i2c , i2c_addr , mem_addr , mem_addr_size , bufinfo .buf , bufinfo .len , vals [3 ].u_int );
495502
496503 if (status != HAL_OK ) {
497504 // TODO really need a HardwareError object, or something
@@ -507,14 +514,15 @@ STATIC mp_obj_t pyb_i2c_mem_read(uint n_args, const mp_obj_t *args, mp_map_t *kw
507514}
508515STATIC MP_DEFINE_CONST_FUN_OBJ_KW (pyb_i2c_mem_read_obj , 1 , pyb_i2c_mem_read );
509516
510- /// \method mem_write(data, addr, memaddr, timeout=5000)
517+ /// \method mem_write(data, addr, memaddr, timeout=5000, addr_size=8 )
511518///
512519/// Write to the memory of an I2C device:
513520///
514521/// - `data` can be an integer or a buffer to write from
515522/// - `addr` is the I2C device address
516523/// - `memaddr` is the memory location within the I2C device
517524/// - `timeout` is the timeout in milliseconds to wait for the write
525+ /// - `addr_size` selects width of memaddr: 8 or 16 bits
518526///
519527/// Returns `None`.
520528/// This is only valid in master mode.
@@ -537,8 +545,13 @@ STATIC mp_obj_t pyb_i2c_mem_write(uint n_args, const mp_obj_t *args, mp_map_t *k
537545 // get the addresses
538546 mp_uint_t i2c_addr = vals [1 ].u_int << 1 ;
539547 mp_uint_t mem_addr = vals [2 ].u_int ;
548+ // determine width of mem_addr; default is 8 bits, entering any other value gives 16 bit width
549+ mp_uint_t mem_addr_size = I2C_MEMADD_SIZE_8BIT ;
550+ if (vals [4 ].u_int != 8 ) {
551+ mem_addr_size = I2C_MEMADD_SIZE_16BIT ;
552+ }
540553
541- HAL_StatusTypeDef status = HAL_I2C_Mem_Write (self -> i2c , i2c_addr , mem_addr , I2C_MEMADD_SIZE_8BIT , bufinfo .buf , bufinfo .len , vals [3 ].u_int );
554+ HAL_StatusTypeDef status = HAL_I2C_Mem_Write (self -> i2c , i2c_addr , mem_addr , mem_addr_size , bufinfo .buf , bufinfo .len , vals [3 ].u_int );
542555
543556 if (status != HAL_OK ) {
544557 // TODO really need a HardwareError object, or something
0 commit comments