Skip to content

Commit e687fdb

Browse files
committed
Add keyword argument 'memaddr_use_16b' to i2c.mem_read and mem_write methods
to allow these methods to transmit 16 bit addresses to an i2c device Add 'memaddr_use_16b' to qstrdefsport.h
1 parent 2097c8b commit e687fdb

2 files changed

Lines changed: 18 additions & 4 deletions

File tree

stmhal/i2c.c

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}
454454
STATIC 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, memaddr_use_16b=False)
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+
/// - `memaddr_use_16b` 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_memaddr_use_16b, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
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
496+
mp_uint_t mem_addr_size = I2C_MEMADD_SIZE_8BIT;
497+
if( vals[4].u_bool ) {
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
}
508515
STATIC 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, memaddr_use_16b=False)
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+
/// - `memaddr_use_16b` 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
549+
mp_uint_t mem_addr_size = I2C_MEMADD_SIZE_8BIT;
550+
if( vals[4].u_bool ) {
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

stmhal/qstrdefsport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ Q(baudrate)
160160
Q(gencall)
161161
Q(data)
162162
Q(memaddr)
163+
Q(memaddr_use_16b)
163164
Q(timeout)
164165
Q(init)
165166
Q(deinit)

0 commit comments

Comments
 (0)