Skip to content

Commit

Permalink
Merge pull request adafruit#746 from blmorris/master
Browse files Browse the repository at this point in the history
Enable 16-bit memory addresses for i2c.mem_read and i2c_mem_write
  • Loading branch information
dpgeorge committed Jul 22, 2014
2 parents 3e0bce3 + 7a03b5f commit 7cfae96
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
21 changes: 17 additions & 4 deletions stmhal/i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -453,14 +453,15 @@ STATIC mp_obj_t pyb_i2c_recv(uint n_args, const mp_obj_t *args, mp_map_t *kw_arg
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_recv_obj, 1, pyb_i2c_recv);

/// \method mem_read(data, addr, memaddr, timeout=5000)
/// \method mem_read(data, addr, memaddr, timeout=5000, addr_size=8)
///
/// Read from the memory of an I2C device:
///
/// - `data` can be an integer or a buffer to read into
/// - `addr` is the I2C device address
/// - `memaddr` is the memory location within the I2C device
/// - `timeout` is the timeout in milliseconds to wait for the read
/// - `addr_size` selects width of memaddr: 8 or 16 bits
///
/// Returns the read data.
/// This is only valid in master mode.
Expand All @@ -469,6 +470,7 @@ STATIC const mp_arg_t pyb_i2c_mem_read_args[] = {
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_memaddr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 5000} },
{ MP_QSTR_addr_size, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
};
#define PYB_I2C_MEM_READ_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_mem_read_args)

Expand All @@ -490,8 +492,13 @@ STATIC mp_obj_t pyb_i2c_mem_read(uint n_args, const mp_obj_t *args, mp_map_t *kw
// get the addresses
mp_uint_t i2c_addr = vals[1].u_int << 1;
mp_uint_t mem_addr = vals[2].u_int;
// determine width of mem_addr; default is 8 bits, entering any other value gives 16 bit width
mp_uint_t mem_addr_size = I2C_MEMADD_SIZE_8BIT;
if (vals[4].u_int != 8) {
mem_addr_size = I2C_MEMADD_SIZE_16BIT;
}

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);
HAL_StatusTypeDef status = HAL_I2C_Mem_Read(self->i2c, i2c_addr, mem_addr, mem_addr_size, bufinfo.buf, bufinfo.len, vals[3].u_int);

if (status != HAL_OK) {
// TODO really need a HardwareError object, or something
Expand All @@ -507,14 +514,15 @@ STATIC mp_obj_t pyb_i2c_mem_read(uint n_args, const mp_obj_t *args, mp_map_t *kw
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_mem_read_obj, 1, pyb_i2c_mem_read);

/// \method mem_write(data, addr, memaddr, timeout=5000)
/// \method mem_write(data, addr, memaddr, timeout=5000, addr_size=8)
///
/// Write to the memory of an I2C device:
///
/// - `data` can be an integer or a buffer to write from
/// - `addr` is the I2C device address
/// - `memaddr` is the memory location within the I2C device
/// - `timeout` is the timeout in milliseconds to wait for the write
/// - `addr_size` selects width of memaddr: 8 or 16 bits
///
/// Returns `None`.
/// This is only valid in master mode.
Expand All @@ -537,8 +545,13 @@ STATIC mp_obj_t pyb_i2c_mem_write(uint n_args, const mp_obj_t *args, mp_map_t *k
// get the addresses
mp_uint_t i2c_addr = vals[1].u_int << 1;
mp_uint_t mem_addr = vals[2].u_int;
// determine width of mem_addr; default is 8 bits, entering any other value gives 16 bit width
mp_uint_t mem_addr_size = I2C_MEMADD_SIZE_8BIT;
if (vals[4].u_int != 8) {
mem_addr_size = I2C_MEMADD_SIZE_16BIT;
}

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);
HAL_StatusTypeDef status = HAL_I2C_Mem_Write(self->i2c, i2c_addr, mem_addr, mem_addr_size, bufinfo.buf, bufinfo.len, vals[3].u_int);

if (status != HAL_OK) {
// TODO really need a HardwareError object, or something
Expand Down
1 change: 1 addition & 0 deletions stmhal/qstrdefsport.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ Q(baudrate)
Q(gencall)
Q(data)
Q(memaddr)
Q(addr_size)
Q(timeout)
Q(init)
Q(deinit)
Expand Down

0 comments on commit 7cfae96

Please sign in to comment.