Skip to content

Commit f352fe8

Browse files
author
Daniel Campora
committed
tests/wipy: Add I2C tests.
1 parent d265df5 commit f352fe8

4 files changed

Lines changed: 296 additions & 55 deletions

File tree

cc3200/mods/pybi2c.c

Lines changed: 82 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,57 @@ STATIC bool pyb_i2c_transaction(uint cmd) {
137137
return true;
138138
}
139139

140+
STATIC void pyb_i2c_check_init(pyb_i2c_obj_t *self) {
141+
// not initialized
142+
if (!self->baudrate) {
143+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_request_not_possible));
144+
}
145+
}
146+
147+
STATIC bool pyb_i2c_scan_device(byte devAddr) {
148+
// Set I2C codec slave address
149+
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, devAddr, true);
150+
// Initiate the transfer.
151+
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_SINGLE_RECEIVE));
152+
// Since this is a hack, send the stop bit anyway
153+
MAP_I2CMasterControl(I2CA0_BASE, I2C_MASTER_CMD_BURST_SEND_ERROR_STOP);
154+
return true;
155+
}
156+
157+
STATIC bool pyb_i2c_mem_addr_write (byte addr, byte *mem_addr, uint mem_addr_len) {
158+
// Set I2C codec slave address
159+
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, addr, false);
160+
// Write the first byte to the controller.
161+
MAP_I2CMasterDataPut(I2CA0_BASE, *mem_addr++);
162+
// Initiate the transfer.
163+
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_START));
164+
165+
// Loop until the completion of transfer or error
166+
while (--mem_addr_len) {
167+
// Write the next byte of data
168+
MAP_I2CMasterDataPut(I2CA0_BASE, *mem_addr++);
169+
// Transact over I2C to send the next byte
170+
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_CONT));
171+
}
172+
return true;
173+
}
174+
175+
STATIC bool pyb_i2c_mem_write (byte addr, byte *mem_addr, uint mem_addr_len, byte *data, uint data_len) {
176+
if (pyb_i2c_mem_addr_write (addr, mem_addr, mem_addr_len)) {
177+
// Loop until the completion of transfer or error
178+
while (data_len--) {
179+
// Write the next byte of data
180+
MAP_I2CMasterDataPut(I2CA0_BASE, *data++);
181+
// Transact over I2C to send the byte
182+
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_CONT));
183+
}
184+
// send the stop bit
185+
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_STOP));
186+
return true;
187+
}
188+
return false;
189+
}
190+
140191
STATIC bool pyb_i2c_write(byte addr, byte *data, uint len, bool stop) {
141192
// Set I2C codec slave address
142193
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, addr, false);
@@ -153,66 +204,40 @@ STATIC bool pyb_i2c_write(byte addr, byte *data, uint len, bool stop) {
153204
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_CONT));
154205
}
155206

156-
// If a stop bit is to be sent, send it.
207+
// If a stop bit is to be sent, do it.
157208
if (stop) {
158209
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_SEND_STOP));
159210
}
160-
161211
return true;
162212
}
163213

164214
STATIC bool pyb_i2c_read(byte addr, byte *data, uint len) {
165-
uint cmd;
166-
215+
// Initiate a burst or single receive sequence
216+
uint cmd = --len > 0 ? I2C_MASTER_CMD_BURST_RECEIVE_START : I2C_MASTER_CMD_SINGLE_RECEIVE;
167217
// Set I2C codec slave address
168218
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, addr, true);
169-
170-
// Check if its a single receive or burst receive
171-
if (len > 1) {
172-
// Initiate a burst receive sequence
173-
cmd = I2C_MASTER_CMD_BURST_RECEIVE_START;
174-
}
175-
else {
176-
// Configure for a single receive
177-
cmd = I2C_MASTER_CMD_SINGLE_RECEIVE;
178-
}
179-
180219
// Initiate the transfer.
181220
RET_IF_ERR(pyb_i2c_transaction(cmd));
182-
// Decrement the count
183-
len--;
184221
// Loop until the completion of reception or error
185222
while (len) {
186223
// Receive the byte over I2C
187224
*data++ = MAP_I2CMasterDataGet(I2CA0_BASE);
188225
if (--len) {
189226
// Continue with reception
190227
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_RECEIVE_CONT));
191-
}
192-
else {
228+
} else {
193229
// Complete the last reception
194230
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_BURST_RECEIVE_FINISH));
195231
}
196232
}
197233

198234
// Receive the last byte over I2C
199235
*data = MAP_I2CMasterDataGet(I2CA0_BASE);
200-
201-
return true;
202-
}
203-
204-
STATIC bool pyb_i2c_scan_device(byte devAddr) {
205-
// Set I2C codec slave address
206-
MAP_I2CMasterSlaveAddrSet(I2CA0_BASE, devAddr, true);
207-
// Initiate the transfer.
208-
RET_IF_ERR(pyb_i2c_transaction(I2C_MASTER_CMD_SINGLE_RECEIVE));
209-
// Since this is a hack, send the stop bit anyway
210-
MAP_I2CMasterControl(I2CA0_BASE, I2C_MASTER_CMD_BURST_SEND_ERROR_STOP);
211-
212236
return true;
213237
}
214238

215239
STATIC void pyb_i2c_read_into (mp_arg_val_t *args, vstr_t *vstr) {
240+
pyb_i2c_check_init(&pyb_i2c_obj);
216241
// get the buffer to receive into
217242
pyb_buf_get_for_recv(args[1].u_obj, vstr);
218243

@@ -223,6 +248,7 @@ STATIC void pyb_i2c_read_into (mp_arg_val_t *args, vstr_t *vstr) {
223248
}
224249

225250
STATIC void pyb_i2c_readmem_into (mp_arg_val_t *args, vstr_t *vstr) {
251+
pyb_i2c_check_init(&pyb_i2c_obj);
226252
// get the buffer to receive into
227253
pyb_buf_get_for_recv(args[2].u_obj, vstr);
228254

@@ -232,8 +258,8 @@ STATIC void pyb_i2c_readmem_into (mp_arg_val_t *args, vstr_t *vstr) {
232258
// determine the width of mem_addr (1 or 2 bytes)
233259
mp_uint_t mem_addr_size = args[3].u_int >> 3;
234260

235-
// write the register address to be read from.
236-
if (pyb_i2c_write (i2c_addr, (byte *)&mem_addr, mem_addr_size, false)) {
261+
// write the register address to be read from
262+
if (pyb_i2c_mem_addr_write (i2c_addr, (byte *)&mem_addr, mem_addr_size)) {
237263
// Read the specified length of data
238264
if (!pyb_i2c_read (i2c_addr, (byte *)vstr->buf, vstr->len)) {
239265
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
@@ -255,7 +281,7 @@ STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
255281

256282
/// \method init()
257283
STATIC const mp_arg_t pyb_i2c_init_args[] = {
258-
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, },
284+
{ MP_QSTR_mode, MP_ARG_INT, {.u_int = PYBI2C_MASTER} },
259285
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
260286
{ MP_QSTR_pins, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
261287
};
@@ -278,7 +304,7 @@ STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, mp_uint_t n_args, const
278304
mp_obj_t pins_o = args[2].u_obj;
279305
if (pins_o != mp_const_none) {
280306
mp_obj_t *pins;
281-
mp_uint_t n_pins;
307+
mp_uint_t n_pins = 2;
282308
if (pins_o == MP_OBJ_NULL) {
283309
// use the default pins
284310
pins = (mp_obj_t *)pyb_i2c_def_pin;
@@ -347,6 +373,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_i2c_deinit_obj, pyb_i2c_deinit);
347373

348374
/// \method scan()
349375
STATIC mp_obj_t pyb_i2c_scan(mp_obj_t self_in) {
376+
pyb_i2c_check_init(&pyb_i2c_obj);
350377
mp_obj_t list = mp_obj_new_list(0, NULL);
351378
for (uint addr = 1; addr <= 127; addr++) {
352379
for (int i = 0; i < 7; i++) {
@@ -362,11 +389,11 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_i2c_scan_obj, pyb_i2c_scan);
362389

363390
STATIC mp_obj_t pyb_i2c_readfrom(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
364391
STATIC const mp_arg_t pyb_i2c_readfrom_args[] = {
365-
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_OBJ, },
366-
{ MP_QSTR_nbytes, MP_ARG_REQUIRED | MP_ARG_INT, },
392+
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, },
393+
{ MP_QSTR_nbytes, MP_ARG_REQUIRED | MP_ARG_OBJ, },
367394
};
368395

369-
// parse pos_args
396+
// parse args
370397
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_readfrom_args)];
371398
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(pyb_i2c_readfrom_args), pyb_i2c_readfrom_args, args);
372399

@@ -376,7 +403,7 @@ STATIC mp_obj_t pyb_i2c_readfrom(mp_uint_t n_args, const mp_obj_t *pos_args, mp_
376403
// return the received data
377404
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
378405
}
379-
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_obj, 2, pyb_i2c_readfrom);
406+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_obj, 3, pyb_i2c_readfrom);
380407

381408
STATIC mp_obj_t pyb_i2c_readfrom_into(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
382409
STATIC const mp_arg_t pyb_i2c_readfrom_into_args[] = {
@@ -394,7 +421,7 @@ STATIC mp_obj_t pyb_i2c_readfrom_into(mp_uint_t n_args, const mp_obj_t *pos_args
394421
// return the number of bytes received
395422
return mp_obj_new_int(vstr.len);
396423
}
397-
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_into_obj, 2, pyb_i2c_readfrom_into);
424+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_into_obj, 3, pyb_i2c_readfrom_into);
398425

399426
STATIC mp_obj_t pyb_i2c_writeto(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
400427
STATIC const mp_arg_t pyb_i2c_writeto_args[] = {
@@ -407,6 +434,8 @@ STATIC mp_obj_t pyb_i2c_writeto(mp_uint_t n_args, const mp_obj_t *pos_args, mp_m
407434
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_writeto_args)];
408435
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(pyb_i2c_writeto_args), pyb_i2c_writeto_args, args);
409436

437+
pyb_i2c_check_init(&pyb_i2c_obj);
438+
410439
// get the buffer to send from
411440
mp_buffer_info_t bufinfo;
412441
uint8_t data[1];
@@ -420,13 +449,13 @@ STATIC mp_obj_t pyb_i2c_writeto(mp_uint_t n_args, const mp_obj_t *pos_args, mp_m
420449
// return the number of bytes written
421450
return mp_obj_new_int(bufinfo.len);
422451
}
423-
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_writeto_obj, 2, pyb_i2c_writeto);
452+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_writeto_obj, 3, pyb_i2c_writeto);
424453

425454
STATIC mp_obj_t pyb_i2c_readfrom_mem(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
426455
STATIC const mp_arg_t pyb_i2c_readfrom_mem_args[] = {
427-
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_OBJ, },
456+
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, },
428457
{ MP_QSTR_memaddr, MP_ARG_REQUIRED | MP_ARG_INT, },
429-
{ MP_QSTR_nbytes, MP_ARG_REQUIRED | MP_ARG_INT, },
458+
{ MP_QSTR_nbytes, MP_ARG_REQUIRED | MP_ARG_OBJ, },
430459
{ MP_QSTR_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
431460
};
432461

@@ -438,10 +467,10 @@ STATIC mp_obj_t pyb_i2c_readfrom_mem(mp_uint_t n_args, const mp_obj_t *pos_args,
438467
pyb_i2c_readmem_into (args, &vstr);
439468
return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
440469
}
441-
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_mem_obj, 3, pyb_i2c_readfrom_mem);
470+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_mem_obj, 4, pyb_i2c_readfrom_mem);
442471

443472
STATIC const mp_arg_t pyb_i2c_readfrom_mem_into_args[] = {
444-
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_OBJ, },
473+
{ MP_QSTR_addr, MP_ARG_REQUIRED | MP_ARG_INT, },
445474
{ MP_QSTR_memaddr, MP_ARG_REQUIRED | MP_ARG_INT, },
446475
{ MP_QSTR_buf, MP_ARG_REQUIRED | MP_ARG_OBJ, },
447476
{ MP_QSTR_addrsize, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
@@ -457,13 +486,15 @@ STATIC mp_obj_t pyb_i2c_readfrom_mem_into(mp_uint_t n_args, const mp_obj_t *pos_
457486
pyb_i2c_readmem_into (args, &vstr);
458487
return mp_obj_new_int(vstr.len);
459488
}
460-
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_mem_into_obj, 3, pyb_i2c_readfrom_mem_into);
489+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_readfrom_mem_into_obj, 4, pyb_i2c_readfrom_mem_into);
461490

462491
STATIC mp_obj_t pyb_i2c_writeto_mem(mp_uint_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
463492
// parse args
464493
mp_arg_val_t args[MP_ARRAY_SIZE(pyb_i2c_readfrom_mem_into_args)];
465494
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(pyb_i2c_readfrom_mem_into_args), pyb_i2c_readfrom_mem_into_args, args);
466495

496+
pyb_i2c_check_init(&pyb_i2c_obj);
497+
467498
// get the buffer to write from
468499
mp_buffer_info_t bufinfo;
469500
uint8_t data[1];
@@ -476,17 +507,14 @@ STATIC mp_obj_t pyb_i2c_writeto_mem(mp_uint_t n_args, const mp_obj_t *pos_args,
476507
mp_uint_t mem_addr_size = args[3].u_int >> 3;
477508

478509
// write the register address to write to.
479-
if (pyb_i2c_write (i2c_addr, (byte *)&mem_addr, mem_addr_size, false)) {
480-
// Write the specified length of data
481-
if (pyb_i2c_write (i2c_addr, bufinfo.buf, bufinfo.len, true)) {
482-
// return the number of bytes written
483-
return mp_obj_new_int(bufinfo.len);
484-
}
510+
if (pyb_i2c_mem_write (i2c_addr, (byte *)&mem_addr, mem_addr_size, bufinfo.buf, bufinfo.len)) {
511+
// return the number of bytes written
512+
return mp_obj_new_int(bufinfo.len);
485513
}
486514

487515
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
488516
}
489-
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_writeto_mem_obj, 3, pyb_i2c_writeto_mem);
517+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_writeto_mem_obj, 4, pyb_i2c_writeto_mem);
490518

491519
STATIC const mp_map_elem_t pyb_i2c_locals_dict_table[] = {
492520
// instance methods
@@ -497,7 +525,7 @@ STATIC const mp_map_elem_t pyb_i2c_locals_dict_table[] = {
497525
{ MP_OBJ_NEW_QSTR(MP_QSTR_readfrom_into), (mp_obj_t)&pyb_i2c_readfrom_into_obj },
498526
{ MP_OBJ_NEW_QSTR(MP_QSTR_writeto), (mp_obj_t)&pyb_i2c_writeto_obj },
499527
{ MP_OBJ_NEW_QSTR(MP_QSTR_readfrom_mem), (mp_obj_t)&pyb_i2c_readfrom_mem_obj },
500-
{ MP_OBJ_NEW_QSTR(MP_QSTR_readfrom_mem_into), (mp_obj_t)&pyb_i2c_readfrom_mem_into },
528+
{ MP_OBJ_NEW_QSTR(MP_QSTR_readfrom_mem_into), (mp_obj_t)&pyb_i2c_readfrom_mem_into_obj },
501529
{ MP_OBJ_NEW_QSTR(MP_QSTR_writeto_mem), (mp_obj_t)&pyb_i2c_writeto_mem_obj },
502530

503531
// class constants

cc3200/mods/pybuart.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ STATIC mp_obj_t pyb_uart_init_helper(pyb_uart_obj_t *self, mp_uint_t n_args, con
401401
uint flowcontrol = UART_FLOWCONTROL_NONE;
402402
if (pins_o != mp_const_none) {
403403
mp_obj_t *pins;
404-
mp_uint_t n_pins;
404+
mp_uint_t n_pins = 2;
405405
if (pins_o == MP_OBJ_NULL) {
406406
// use the default pins
407407
pins = (mp_obj_t *)pyb_uart_def_pin[self->uart_id];

0 commit comments

Comments
 (0)