cc3200: Remove superflous params from the I2C API.
This commit is contained in:
parent
26d230419c
commit
7f41f650de
@ -57,8 +57,8 @@
|
|||||||
/// from pyb import I2C
|
/// from pyb import I2C
|
||||||
///
|
///
|
||||||
/// i2c = I2C() # create
|
/// i2c = I2C() # create
|
||||||
/// i2c = I2C(I2C.MASTER) # create and init as a master
|
/// i2c = I2C(50000) # create and init with a 50KHz baudrate
|
||||||
/// i2c.init(I2C.MASTER, baudrate=20000) # init as a master
|
/// i2c.init(100000) # init with a 100KHz baudrate
|
||||||
/// i2c.deinit() # turn off the peripheral
|
/// i2c.deinit() # turn off the peripheral
|
||||||
///
|
///
|
||||||
/// Printing the i2c object gives you information about its configuration.
|
/// Printing the i2c object gives you information about its configuration.
|
||||||
@ -74,25 +74,21 @@
|
|||||||
/// data = bytearray(3) # create a buffer
|
/// data = bytearray(3) # create a buffer
|
||||||
/// i2c.recv(data) # receive 3 bytes, writing them into data
|
/// i2c.recv(data) # receive 3 bytes, writing them into data
|
||||||
///
|
///
|
||||||
/// You can specify a timeout (in ms):
|
|
||||||
///
|
|
||||||
/// i2c.send(b'123', timeout=20) # timout after 20ms if the other end is not responding
|
|
||||||
///
|
|
||||||
/// A master must specify the recipient's address:
|
/// A master must specify the recipient's address:
|
||||||
///
|
///
|
||||||
/// i2c.init(I2C.MASTER)
|
/// i2c.init(100000)
|
||||||
/// i2c.send('123', 0x42) # send 3 bytes to slave with address 0x42
|
/// i2c.send('123', 0x42) # send 3 bytes to slave with address 0x42
|
||||||
/// i2c.send(b'456', addr=0x42) # keyword for address
|
/// i2c.send(b'456', addr=0x42) # keyword for address
|
||||||
///
|
///
|
||||||
/// Master also has other methods:
|
/// Master also has other methods:
|
||||||
///
|
///
|
||||||
/// i2c.is_ready(0x42) # check if slave 0x42 is ready
|
/// i2c.is_ready(0x42) # check if slave 0x42 is ready
|
||||||
/// i2c.scan() # scan for slaves on the bus, returning
|
/// i2c.scan() # scan for slaves on the bus, returning
|
||||||
/// # a list of valid addresses
|
/// # a list of valid addresses
|
||||||
/// i2c.mem_read(3, 0x42, 2) # read 3 bytes from memory of slave 0x42,
|
/// i2c.mem_read(3, 0x42, 2) # read 3 bytes from memory of slave 0x42,
|
||||||
/// # starting at address 2 in the slave
|
/// # starting at address 2 in the slave
|
||||||
/// i2c.mem_write('abc', 0x42, 2, timeout=10)
|
/// i2c.mem_write('abc', 0x42, 2) # write 3 bytes to memory of slave 0x42,
|
||||||
|
/// # starting at address 2 in the slave
|
||||||
|
|
||||||
typedef struct _pyb_i2c_obj_t {
|
typedef struct _pyb_i2c_obj_t {
|
||||||
mp_obj_base_t base;
|
mp_obj_base_t base;
|
||||||
@ -102,14 +98,10 @@ typedef struct _pyb_i2c_obj_t {
|
|||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
DEFINE CONSTANTS
|
DEFINE CONSTANTS
|
||||||
******************************************************************************/
|
******************************************************************************/
|
||||||
#define PYBI2C_MODE_MASTER (0)
|
|
||||||
#define PYBI2C_MODE_SLAVE (1)
|
|
||||||
|
|
||||||
#define PYBI2C_MIN_BAUD_RATE_HZ (50000)
|
#define PYBI2C_MIN_BAUD_RATE_HZ (50000)
|
||||||
#define PYBI2C_DEF_BAUD_RATE_HZ (100000)
|
|
||||||
#define PYBI2C_MAX_BAUD_RATE_HZ (400000)
|
#define PYBI2C_MAX_BAUD_RATE_HZ (400000)
|
||||||
|
|
||||||
#define PYBI2C_DEF_TIMEOUT_MS (5)
|
#define PYBI2C_TRANSC_TIMEOUT_MS (10)
|
||||||
#define PYBI2C_TRANSAC_WAIT_DELAY_US (10)
|
#define PYBI2C_TRANSAC_WAIT_DELAY_US (10)
|
||||||
|
|
||||||
#define PYBI2C_TIMEOUT_TO_COUNT(to_us, baud) (((baud) * to_us) / 16000000)
|
#define PYBI2C_TIMEOUT_TO_COUNT(to_us, baud) (((baud) * to_us) / 16000000)
|
||||||
@ -144,10 +136,10 @@ STATIC void i2c_deinit(void) {
|
|||||||
pyb_i2c_obj.baudrate = 0;
|
pyb_i2c_obj.baudrate = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
STATIC bool pyb_i2c_transaction(uint cmd, int timeout) {
|
STATIC bool pyb_i2c_transaction(uint cmd) {
|
||||||
|
// Convert the timeout to microseconds
|
||||||
|
int32_t timeout = PYBI2C_TRANSC_TIMEOUT_MS * 1000;
|
||||||
// Sanity check, t_timeout must be between 1 and 255
|
// Sanity check, t_timeout must be between 1 and 255
|
||||||
// convert timeout to microseconds
|
|
||||||
timeout = timeout > 0 ? (timeout * 1000) : 1000;
|
|
||||||
uint t_timeout = MIN(PYBI2C_TIMEOUT_TO_COUNT(timeout, pyb_i2c_obj.baudrate), 255);
|
uint t_timeout = MIN(PYBI2C_TIMEOUT_TO_COUNT(timeout, pyb_i2c_obj.baudrate), 255);
|
||||||
// Clear all interrupts
|
// Clear all interrupts
|
||||||
MAP_I2CMasterIntClearEx(I2CA0_BASE, MAP_I2CMasterIntStatusEx(I2CA0_BASE, false));
|
MAP_I2CMasterIntClearEx(I2CA0_BASE, MAP_I2CMasterIntStatusEx(I2CA0_BASE, false));
|
||||||
@ -162,11 +154,12 @@ STATIC bool pyb_i2c_transaction(uint cmd, int timeout) {
|
|||||||
UtilsDelay(UTILS_DELAY_US_TO_COUNT(PYBI2C_TRANSAC_WAIT_DELAY_US));
|
UtilsDelay(UTILS_DELAY_US_TO_COUNT(PYBI2C_TRANSAC_WAIT_DELAY_US));
|
||||||
timeout -= PYBI2C_TRANSAC_WAIT_DELAY_US;
|
timeout -= PYBI2C_TRANSAC_WAIT_DELAY_US;
|
||||||
if (timeout < 0) {
|
if (timeout < 0) {
|
||||||
|
// the peripheral is not responding, so stop
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for any errors in transfer
|
// Check for any errors in the transfer
|
||||||
if (MAP_I2CMasterErr(I2CA0_BASE) != I2C_MASTER_ERR_NONE) {
|
if (MAP_I2CMasterErr(I2CA0_BASE) != I2C_MASTER_ERR_NONE) {
|
||||||
switch(cmd) {
|
switch(cmd) {
|
||||||
case I2C_MASTER_CMD_BURST_SEND_START:
|
case I2C_MASTER_CMD_BURST_SEND_START:
|
||||||
@ -184,35 +177,34 @@ STATIC bool pyb_i2c_transaction(uint cmd, int timeout) {
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
STATIC bool pyb_i2c_write(byte devAddr, byte *data, uint len, bool stop, int timeout) {
|
STATIC bool pyb_i2c_write(byte devAddr, byte *data, uint len, bool stop) {
|
||||||
// Set I2C codec slave address
|
// Set I2C codec slave address
|
||||||
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, devAddr, false);
|
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, devAddr, false);
|
||||||
// Write the first byte to the controller.
|
// Write the first byte to the controller.
|
||||||
MAP_I2CMasterDataPut(I2CA0_BASE, *data++);
|
MAP_I2CMasterDataPut(I2CA0_BASE, *data++);
|
||||||
// Initiate the transfer.
|
// Initiate the transfer.
|
||||||
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_START, timeout));
|
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_START));
|
||||||
|
|
||||||
// Loop until the completion of transfer or error
|
// Loop until the completion of transfer or error
|
||||||
while (--len) {
|
while (--len) {
|
||||||
// Write the next byte of data
|
// Write the next byte of data
|
||||||
MAP_I2CMasterDataPut(I2CA0_BASE, *data++);
|
MAP_I2CMasterDataPut(I2CA0_BASE, *data++);
|
||||||
// Transact over I2C to send the byte
|
// Transact over I2C to send the byte
|
||||||
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_CONT, timeout));
|
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_CONT));
|
||||||
}
|
}
|
||||||
|
|
||||||
// If a stop bit is to be sent, send it.
|
// If a stop bit is to be sent, send it.
|
||||||
if (stop) {
|
if (stop) {
|
||||||
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_STOP, timeout));
|
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_STOP));
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
STATIC bool pyb_i2c_read(byte devAddr, byte *data, uint len, int timeout) {
|
STATIC bool pyb_i2c_read(byte devAddr, byte *data, uint len) {
|
||||||
uint cmd;
|
uint cmd;
|
||||||
|
|
||||||
// Set I2C codec slave address
|
// Set I2C codec slave address
|
||||||
@ -229,7 +221,7 @@ STATIC bool pyb_i2c_read(byte devAddr, byte *data, uint len, int timeout) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Initiate the transfer.
|
// Initiate the transfer.
|
||||||
RET_IF_ERR(pyb_i2c_transaction(cmd, timeout));
|
RET_IF_ERR(pyb_i2c_transaction(cmd));
|
||||||
// Decrement the count
|
// Decrement the count
|
||||||
len--;
|
len--;
|
||||||
// Loop until the completion of reception or error
|
// Loop until the completion of reception or error
|
||||||
@ -238,11 +230,11 @@ STATIC bool pyb_i2c_read(byte devAddr, byte *data, uint len, int timeout) {
|
|||||||
*data++ = MAP_I2CMasterDataGet(I2CA0_BASE);
|
*data++ = MAP_I2CMasterDataGet(I2CA0_BASE);
|
||||||
if (--len) {
|
if (--len) {
|
||||||
// Continue with reception
|
// Continue with reception
|
||||||
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_RECEIVE_CONT, timeout));
|
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_RECEIVE_CONT));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Complete the last reception
|
// Complete the last reception
|
||||||
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_RECEIVE_FINISH, timeout));
|
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_RECEIVE_FINISH));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -252,11 +244,11 @@ STATIC bool pyb_i2c_read(byte devAddr, byte *data, uint len, int timeout) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
STATIC bool pyb_i2c_scan_device(byte devAddr, int timeout) {
|
STATIC bool pyb_i2c_scan_device(byte devAddr) {
|
||||||
// Set I2C codec slave address
|
// Set I2C codec slave address
|
||||||
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, devAddr, true);
|
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, devAddr, true);
|
||||||
// Initiate the transfer.
|
// Initiate the transfer.
|
||||||
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_SINGLE_RECEIVE, timeout));
|
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_SINGLE_RECEIVE));
|
||||||
// Since this is a hack, send the stop bit anyway
|
// Since this is a hack, send the stop bit anyway
|
||||||
MAP_I2CMasterControl(I2CA0_BASE, I2C_MASTER_CMD_BURST_SEND_ERROR_STOP);
|
MAP_I2CMasterControl(I2CA0_BASE, I2C_MASTER_CMD_BURST_SEND_ERROR_STOP);
|
||||||
|
|
||||||
@ -267,34 +259,15 @@ STATIC bool pyb_i2c_scan_device(byte devAddr, int timeout) {
|
|||||||
/* Micro Python bindings */
|
/* Micro Python bindings */
|
||||||
/******************************************************************************/
|
/******************************************************************************/
|
||||||
|
|
||||||
/// \method init(mode, *, addr=0x12, baudrate=100000)
|
/// \method init(100000)
|
||||||
///
|
///
|
||||||
/// Initialise the I2C bus with the given parameters:
|
/// Initialise the I2C bus as a master with the given baudrate.
|
||||||
///
|
///
|
||||||
/// - `mode` must be either `I2C.MASTER` or `I2C.SLAVE`
|
STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self_in, mp_obj_t baudrate) {
|
||||||
/// - `addr` is the 7-bit address (only sensible for a slave)
|
|
||||||
/// - `baudrate` is the SCL clock rate (only sensible for a master)
|
|
||||||
STATIC const mp_arg_t pyb_i2c_init_args[] = {
|
|
||||||
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = PYBI2C_MODE_MASTER} },
|
|
||||||
{ MP_QSTR_addr, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
|
|
||||||
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PYBI2C_DEF_BAUD_RATE_HZ} },
|
|
||||||
};
|
|
||||||
#define PYB_I2C_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_init_args)
|
|
||||||
|
|
||||||
STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self_in, mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
|
||||||
pyb_i2c_obj_t *self = self_in;
|
pyb_i2c_obj_t *self = self_in;
|
||||||
|
|
||||||
// parse args
|
|
||||||
mp_arg_val_t vals[PYB_I2C_INIT_NUM_ARGS];
|
|
||||||
mp_arg_parse_all(n_args, args, kw_args, PYB_I2C_INIT_NUM_ARGS, pyb_i2c_init_args, vals);
|
|
||||||
|
|
||||||
if (vals[0].u_int != PYBI2C_MODE_MASTER) {
|
|
||||||
// thrown an exception since only master mode is supported
|
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure the baudrate is between the valid range
|
// make sure the baudrate is between the valid range
|
||||||
self->baudrate = MIN(MAX(vals[2].u_int, PYBI2C_MIN_BAUD_RATE_HZ), PYBI2C_MAX_BAUD_RATE_HZ);
|
self->baudrate = MIN(MAX(mp_obj_get_int(baudrate), PYBI2C_MIN_BAUD_RATE_HZ), PYBI2C_MAX_BAUD_RATE_HZ);
|
||||||
|
|
||||||
// init the I2C bus
|
// init the I2C bus
|
||||||
i2c_init(self);
|
i2c_init(self);
|
||||||
@ -316,23 +289,13 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
|
|||||||
// check arguments
|
// check arguments
|
||||||
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
|
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
|
||||||
|
|
||||||
// get i2c number
|
|
||||||
mp_int_t i2c_id = mp_obj_get_int(args[0]);
|
|
||||||
|
|
||||||
// check the i2c number
|
|
||||||
if (i2c_id != 0) {
|
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
|
|
||||||
}
|
|
||||||
|
|
||||||
// setup the object
|
// setup the object
|
||||||
pyb_i2c_obj_t *self = &pyb_i2c_obj;
|
pyb_i2c_obj_t *self = &pyb_i2c_obj;
|
||||||
self->base.type = &pyb_i2c_type;
|
self->base.type = &pyb_i2c_type;
|
||||||
|
|
||||||
if (n_args > 1 || n_kw > 0) {
|
if (n_args > 0) {
|
||||||
// start the peripheral
|
// start the peripheral
|
||||||
mp_map_t kw_args;
|
pyb_i2c_init_helper(self, *args);
|
||||||
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
|
|
||||||
pyb_i2c_init_helper(self, n_args - 1, args + 1, &kw_args);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return (mp_obj_t)self;
|
return (mp_obj_t)self;
|
||||||
@ -348,10 +311,10 @@ STATIC void pyb_i2c_print(void (*print)(void *env, const char *fmt, ...), void *
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
STATIC mp_obj_t pyb_i2c_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
|
STATIC mp_obj_t pyb_i2c_init(mp_obj_t self_in, mp_obj_t baudrate) {
|
||||||
return pyb_i2c_init_helper(args[0], n_args - 1, args + 1, kw_args);
|
return pyb_i2c_init_helper(self_in, baudrate);
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_init_obj, 1, pyb_i2c_init);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_i2c_init_obj, pyb_i2c_init);
|
||||||
|
|
||||||
/// \method deinit()
|
/// \method deinit()
|
||||||
/// Turn off the I2C bus.
|
/// Turn off the I2C bus.
|
||||||
@ -367,12 +330,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_i2c_deinit_obj, pyb_i2c_deinit);
|
|||||||
/// Check if an I2C device responds to the given address. Only valid when in master mode.
|
/// Check if an I2C device responds to the given address. Only valid when in master mode.
|
||||||
STATIC mp_obj_t pyb_i2c_is_ready(mp_obj_t self_in, mp_obj_t i2c_addr_o) {
|
STATIC mp_obj_t pyb_i2c_is_ready(mp_obj_t self_in, mp_obj_t i2c_addr_o) {
|
||||||
mp_uint_t i2c_addr = mp_obj_get_int(i2c_addr_o);
|
mp_uint_t i2c_addr = mp_obj_get_int(i2c_addr_o);
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
if (pyb_i2c_scan_device(i2c_addr, PYBI2C_DEF_TIMEOUT_MS)) {
|
if (pyb_i2c_scan_device(i2c_addr)) {
|
||||||
return mp_const_true;
|
return mp_const_true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return mp_const_false;
|
return mp_const_false;
|
||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_i2c_is_ready_obj, pyb_i2c_is_ready);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_i2c_is_ready_obj, pyb_i2c_is_ready);
|
||||||
@ -383,8 +345,8 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_i2c_is_ready_obj, pyb_i2c_is_ready);
|
|||||||
STATIC mp_obj_t pyb_i2c_scan(mp_obj_t self_in) {
|
STATIC mp_obj_t pyb_i2c_scan(mp_obj_t self_in) {
|
||||||
mp_obj_t list = mp_obj_new_list(0, NULL);
|
mp_obj_t list = mp_obj_new_list(0, NULL);
|
||||||
for (uint addr = 1; addr <= 127; addr++) {
|
for (uint addr = 1; addr <= 127; addr++) {
|
||||||
for (int i = 0; i < 10; i++) {
|
for (int i = 0; i < 7; i++) {
|
||||||
if (pyb_i2c_scan_device(addr, PYBI2C_DEF_TIMEOUT_MS)) {
|
if (pyb_i2c_scan_device(addr)) {
|
||||||
mp_obj_list_append(list, mp_obj_new_int(addr));
|
mp_obj_list_append(list, mp_obj_new_int(addr));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -400,12 +362,10 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_i2c_scan_obj, pyb_i2c_scan);
|
|||||||
///
|
///
|
||||||
/// - `send` is the data to send (an integer to send, or a buffer object)
|
/// - `send` is the data to send (an integer to send, or a buffer object)
|
||||||
/// - `addr` is the address to send to (only required in master mode)
|
/// - `addr` is the address to send to (only required in master mode)
|
||||||
/// - `timeout` is the timeout in milliseconds to wait for the other end to respond
|
|
||||||
/// Return value: `None`.
|
/// Return value: `None`.
|
||||||
STATIC const mp_arg_t pyb_i2c_send_args[] = {
|
STATIC const mp_arg_t pyb_i2c_send_args[] = {
|
||||||
{ MP_QSTR_send, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
{ MP_QSTR_send, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||||
{ MP_QSTR_addr, MP_ARG_INT, {.u_int = 0} },
|
{ MP_QSTR_addr, MP_ARG_INT, {.u_int = 0} },
|
||||||
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PYBI2C_DEF_TIMEOUT_MS} },
|
|
||||||
};
|
};
|
||||||
#define PYB_I2C_SEND_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_send_args)
|
#define PYB_I2C_SEND_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_send_args)
|
||||||
|
|
||||||
@ -420,7 +380,7 @@ STATIC mp_obj_t pyb_i2c_send(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k
|
|||||||
pyb_buf_get_for_send(vals[0].u_obj, &bufinfo, data);
|
pyb_buf_get_for_send(vals[0].u_obj, &bufinfo, data);
|
||||||
|
|
||||||
// send the data
|
// send the data
|
||||||
if (!pyb_i2c_write(vals[1].u_int, bufinfo.buf, bufinfo.len, true, vals[2].u_int)) {
|
if (!pyb_i2c_write(vals[1].u_int, bufinfo.buf, bufinfo.len, true)) {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -428,21 +388,19 @@ STATIC mp_obj_t pyb_i2c_send(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k
|
|||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_send_obj, 1, pyb_i2c_send);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_send_obj, 1, pyb_i2c_send);
|
||||||
|
|
||||||
/// \method recv(recv, addr=0x00, timeout=10)
|
/// \method recv(recv, addr=0x00)
|
||||||
///
|
///
|
||||||
/// Receive data on the bus:
|
/// Receive data on the bus:
|
||||||
///
|
///
|
||||||
/// - `recv` can be an integer, which is the number of bytes to receive,
|
/// - `recv` can be an integer, which is the number of bytes to receive,
|
||||||
/// or a mutable buffer, which will be filled with received bytes
|
/// or a mutable buffer, which will be filled with received bytes
|
||||||
/// - `addr` is the address to receive from (only required in master mode)
|
/// - `addr` is the address to receive from (only required in master mode)
|
||||||
/// - `timeout` is the timeout in milliseconds to wait for the other end to respond
|
|
||||||
///
|
///
|
||||||
/// Return value: if `recv` is an integer then a new buffer of the bytes received,
|
/// Return value: if `recv` is an integer then a new buffer of the bytes received,
|
||||||
/// otherwise the same buffer that was passed in to `recv`.
|
/// otherwise the same buffer that was passed in to `recv`.
|
||||||
STATIC const mp_arg_t pyb_i2c_recv_args[] = {
|
STATIC const mp_arg_t pyb_i2c_recv_args[] = {
|
||||||
{ MP_QSTR_recv, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
{ MP_QSTR_recv, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||||
{ MP_QSTR_addr, MP_ARG_INT, {.u_int = 0} },
|
{ MP_QSTR_addr, MP_ARG_INT, {.u_int = 0} },
|
||||||
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PYBI2C_DEF_TIMEOUT_MS} },
|
|
||||||
};
|
};
|
||||||
#define PYB_I2C_RECV_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_recv_args)
|
#define PYB_I2C_RECV_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_recv_args)
|
||||||
|
|
||||||
@ -456,7 +414,7 @@ STATIC mp_obj_t pyb_i2c_recv(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k
|
|||||||
mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &vstr);
|
mp_obj_t o_ret = pyb_buf_get_for_recv(vals[0].u_obj, &vstr);
|
||||||
|
|
||||||
// receive the data
|
// receive the data
|
||||||
if (!pyb_i2c_read(vals[1].u_int, (byte *)vstr.buf, vstr.len, vals[2].u_int)) {
|
if (!pyb_i2c_read(vals[1].u_int, (byte *)vstr.buf, vstr.len)) {
|
||||||
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
|
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -470,15 +428,14 @@ STATIC mp_obj_t pyb_i2c_recv(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *k
|
|||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_recv_obj, 1, pyb_i2c_recv);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_recv_obj, 1, pyb_i2c_recv);
|
||||||
|
|
||||||
/// \method mem_read(data, addr, memaddr, timeout=10, addr_size=8)
|
/// \method mem_read(data, addr, memaddr, addr_size=8)
|
||||||
///
|
///
|
||||||
/// Read from the memory of an I2C device:
|
/// Read from the memory of an I2C device:
|
||||||
///
|
///
|
||||||
/// - `data` can be an integer or a buffer to read into
|
/// - `data` can be an integer or a buffer to read into
|
||||||
/// - `addr` is the I2C device address
|
/// - `addr` is the I2C device address
|
||||||
/// - `memaddr` is the memory location within the I2C device
|
/// - `memaddr` is the memory location within the I2C device
|
||||||
/// - `timeout` is the timeout in milliseconds to wait for the other end to respond
|
/// - `addr_size` selects the width of memaddr: 8 or 16 bits
|
||||||
/// - `addr_size` selects width of memaddr: 8 or 16 bits
|
|
||||||
///
|
///
|
||||||
/// Returns the read data.
|
/// Returns the read data.
|
||||||
/// This is only valid in master mode.
|
/// This is only valid in master mode.
|
||||||
@ -486,7 +443,6 @@ STATIC const mp_arg_t pyb_i2c_mem_read_args[] = {
|
|||||||
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
|
||||||
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
|
{ 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_memaddr, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = 0} },
|
||||||
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = PYBI2C_DEF_TIMEOUT_MS} },
|
|
||||||
{ MP_QSTR_addr_size, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
|
{ 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)
|
#define PYB_I2C_MEM_READ_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_mem_read_args)
|
||||||
@ -503,13 +459,13 @@ STATIC mp_obj_t pyb_i2c_mem_read(mp_uint_t n_args, const mp_obj_t *args, mp_map_
|
|||||||
// get the addresses
|
// get the addresses
|
||||||
mp_uint_t i2c_addr = vals[1].u_int;
|
mp_uint_t i2c_addr = vals[1].u_int;
|
||||||
mp_uint_t mem_addr = vals[2].u_int;
|
mp_uint_t mem_addr = vals[2].u_int;
|
||||||
// determine width of mem_addr (1 or 2 bytes)
|
// determine the width of mem_addr (1 or 2 bytes)
|
||||||
mp_uint_t mem_addr_size = vals[4].u_int >> 3;
|
mp_uint_t mem_addr_size = vals[3].u_int >> 3;
|
||||||
|
|
||||||
// Write the register address to be read from.
|
// Write the register address to be read from.
|
||||||
if (pyb_i2c_write (i2c_addr, (byte *)&mem_addr, mem_addr_size, false, vals[3].u_int)) {
|
if (pyb_i2c_write (i2c_addr, (byte *)&mem_addr, mem_addr_size, false)) {
|
||||||
// Read the specified length of data
|
// Read the specified length of data
|
||||||
if (pyb_i2c_read (i2c_addr, (byte *)vstr.buf, vstr.len, vals[3].u_int)) {
|
if (pyb_i2c_read (i2c_addr, (byte *)vstr.buf, vstr.len)) {
|
||||||
// return the read data
|
// return the read data
|
||||||
if (o_ret != MP_OBJ_NULL) {
|
if (o_ret != MP_OBJ_NULL) {
|
||||||
return o_ret;
|
return o_ret;
|
||||||
@ -525,15 +481,14 @@ STATIC mp_obj_t pyb_i2c_mem_read(mp_uint_t n_args, const mp_obj_t *args, mp_map_
|
|||||||
}
|
}
|
||||||
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_mem_read_obj, 1, pyb_i2c_mem_read);
|
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_mem_read_obj, 1, pyb_i2c_mem_read);
|
||||||
|
|
||||||
/// \method mem_write(data, addr, memaddr, timeout=10, addr_size=8)
|
/// \method mem_write(data, addr, memaddr, addr_size=8)
|
||||||
///
|
///
|
||||||
/// Write to the memory of an I2C device:
|
/// Write to the memory of an I2C device:
|
||||||
///
|
///
|
||||||
/// - `data` can be an integer or a buffer to write from
|
/// - `data` can be an integer or a buffer to write from
|
||||||
/// - `addr` is the I2C device address
|
/// - `addr` is the I2C device address
|
||||||
/// - `memaddr` is the memory location within the I2C device
|
/// - `memaddr` is the memory location within the I2C device
|
||||||
/// - `timeout` is the timeout in milliseconds to wait for the other end to respond
|
/// - `addr_size` selects the width of memaddr: 8 or 16 bits
|
||||||
/// - `addr_size` selects width of memaddr: 8 or 16 bits
|
|
||||||
///
|
///
|
||||||
/// Returns `None`.
|
/// Returns `None`.
|
||||||
/// This is only valid in master mode.
|
/// This is only valid in master mode.
|
||||||
@ -550,13 +505,13 @@ STATIC mp_obj_t pyb_i2c_mem_write(mp_uint_t n_args, const mp_obj_t *args, mp_map
|
|||||||
// get the addresses
|
// get the addresses
|
||||||
mp_uint_t i2c_addr = vals[1].u_int;
|
mp_uint_t i2c_addr = vals[1].u_int;
|
||||||
mp_uint_t mem_addr = vals[2].u_int;
|
mp_uint_t mem_addr = vals[2].u_int;
|
||||||
// determine width of mem_addr (1 or 2 bytes)
|
// determine the width of mem_addr (1 or 2 bytes)
|
||||||
mp_uint_t mem_addr_size = vals[4].u_int >> 3;
|
mp_uint_t mem_addr_size = vals[3].u_int >> 3;
|
||||||
|
|
||||||
// Write the register address to write to.
|
// Write the register address to write to.
|
||||||
if (pyb_i2c_write (i2c_addr, (byte *)&mem_addr, mem_addr_size, false, vals[3].u_int)) {
|
if (pyb_i2c_write (i2c_addr, (byte *)&mem_addr, mem_addr_size, false)) {
|
||||||
// Write the specified length of data
|
// Write the specified length of data
|
||||||
if (pyb_i2c_write (i2c_addr, bufinfo.buf, bufinfo.len, true, vals[3].u_int)) {
|
if (pyb_i2c_write (i2c_addr, bufinfo.buf, bufinfo.len, true)) {
|
||||||
return mp_const_none;
|
return mp_const_none;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -577,10 +532,6 @@ STATIC const mp_map_elem_t pyb_i2c_locals_dict_table[] = {
|
|||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&pyb_i2c_recv_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&pyb_i2c_recv_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_read), (mp_obj_t)&pyb_i2c_mem_read_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_read), (mp_obj_t)&pyb_i2c_mem_read_obj },
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_write), (mp_obj_t)&pyb_i2c_mem_write_obj },
|
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_write), (mp_obj_t)&pyb_i2c_mem_write_obj },
|
||||||
|
|
||||||
// class constants
|
|
||||||
/// \constant MASTER - for initialising the bus to master mode
|
|
||||||
{ MP_OBJ_NEW_QSTR(MP_QSTR_MASTER), MP_OBJ_NEW_SMALL_INT(PYBI2C_MODE_MASTER) },
|
|
||||||
};
|
};
|
||||||
|
|
||||||
STATIC MP_DEFINE_CONST_DICT(pyb_i2c_locals_dict, pyb_i2c_locals_dict_table);
|
STATIC MP_DEFINE_CONST_DICT(pyb_i2c_locals_dict, pyb_i2c_locals_dict_table);
|
||||||
|
@ -174,7 +174,7 @@ STATIC void pyb_spi_print(void (*print)(void *env, const char *fmt, ...), void *
|
|||||||
pyb_spi_obj_t *self = self_in;
|
pyb_spi_obj_t *self = self_in;
|
||||||
|
|
||||||
if (self->baudrate > 0) {
|
if (self->baudrate > 0) {
|
||||||
print(env, "<SPI0, SPI.MASTER, baudrate=%u, config=%u, submode=%u, bits=%u",
|
print(env, "<SPI0, SPI.MASTER, baudrate=%u, config=%u, submode=%u, bits=%u>",
|
||||||
self->baudrate, self->config, self->submode, (self->wlen * 8));
|
self->baudrate, self->config, self->submode, (self->wlen * 8));
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -126,13 +126,11 @@ Q(FLOW_TXRX)
|
|||||||
|
|
||||||
// for I2C class
|
// for I2C class
|
||||||
Q(I2C)
|
Q(I2C)
|
||||||
Q(mode)
|
|
||||||
Q(addr)
|
Q(addr)
|
||||||
Q(baudrate)
|
Q(baudrate)
|
||||||
Q(data)
|
Q(data)
|
||||||
Q(memaddr)
|
Q(memaddr)
|
||||||
Q(addr_size)
|
Q(addr_size)
|
||||||
Q(timeout)
|
|
||||||
Q(init)
|
Q(init)
|
||||||
Q(deinit)
|
Q(deinit)
|
||||||
Q(is_ready)
|
Q(is_ready)
|
||||||
@ -141,7 +139,6 @@ Q(send)
|
|||||||
Q(recv)
|
Q(recv)
|
||||||
Q(mem_read)
|
Q(mem_read)
|
||||||
Q(mem_write)
|
Q(mem_write)
|
||||||
Q(MASTER)
|
|
||||||
|
|
||||||
// for ADC class
|
// for ADC class
|
||||||
Q(ADC)
|
Q(ADC)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user