Skip to content

Commit 2dd4723

Browse files
author
Daniel Campora
committed
cc3200: Make API more similar to stmhal.
In general the changes are: 1. Peripheral (UART, SPI, ADC, I2C, Timer) IDs start from 1, not zero. 2. Make I2C and SPI require the ID even when there's only one bus. 3. Make I2C and SPI accept 'mode' parameter even though only MASTER is supported.
1 parent 6545336 commit 2dd4723

9 files changed

Lines changed: 94 additions & 69 deletions

File tree

cc3200/boards/LAUNCHXL/mpconfigboard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#define MICROPY_HW_ENABLE_RNG (1)
3535
#define MICROPY_HW_ENABLE_RTC (1)
3636

37-
#define MICROPY_STDIO_UART PYB_UART_0
37+
#define MICROPY_STDIO_UART 1
3838
#define MICROPY_STDIO_UART_BAUD 115200
3939
#define MICROPY_STDIO_UART_RX_BUF_SIZE 128
4040

cc3200/boards/WIPY-SD/mpconfigboard.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@
3434
#define MICROPY_HW_ENABLE_RNG (1)
3535
#define MICROPY_HW_ENABLE_RTC (1)
3636

37-
#define MICROPY_STDIO_UART PYB_UART_0
38-
#define MICROPY_STDIO_UART_BAUD 115200
39-
#define MICROPY_STDIO_UART_RX_BUF_SIZE 128
40-
4137
#define MICROPY_SYS_LED_PRCM PRCM_GPIOA3
4238
#define MICROPY_SAFE_BOOT_PRCM PRCM_GPIOA3
4339
#define MICROPY_SYS_LED_PORT GPIOA3_BASE

cc3200/boards/WIPY/mpconfigboard.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,6 @@
3434
#define MICROPY_HW_ENABLE_RNG (1)
3535
#define MICROPY_HW_ENABLE_RTC (1)
3636

37-
#define MICROPY_STDIO_UART PYB_UART_0
38-
#define MICROPY_STDIO_UART_BAUD 115200
39-
#define MICROPY_STDIO_UART_RX_BUF_SIZE 128
40-
4137
#define MICROPY_SYS_LED_PRCM PRCM_GPIOA3
4238
#define MICROPY_SAFE_BOOT_PRCM PRCM_GPIOA3
4339
#define MICROPY_SYS_LED_PORT GPIOA3_BASE

cc3200/mods/pybadc.c

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
///
5858
/// Usage:
5959
///
60-
/// adc = pyb.ADC(channel) # create an adc object on the given channel (0 to 3)
60+
/// adc = pyb.ADC(channel) # create an adc object on the given channel (1 to 4)
6161
/// this automatically configures the pin associated to
6262
/// that analog channel.
6363
/// adc.read() # read channel value
@@ -76,7 +76,7 @@
7676
typedef struct {
7777
mp_obj_base_t base;
7878
byte channel;
79-
byte num;
79+
byte idx;
8080
} pyb_adc_obj_t;
8181

8282
/******************************************************************************
@@ -102,7 +102,7 @@ STATIC pyb_adc_obj_t pyb_adc_obj[PYB_ADC_NUM_CHANNELS];
102102

103103
STATIC void adc_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
104104
pyb_adc_obj_t *self = self_in;
105-
mp_printf(print, "<ADC, channel=%u>", self->num);
105+
mp_printf(print, "<ADC, channel=%u>", (self->idx + 1));
106106
}
107107

108108
/// \classmethod \constructor(channel)
@@ -113,10 +113,10 @@ STATIC mp_obj_t adc_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw,
113113
mp_arg_check_num(n_args, n_kw, 1, 1, false);
114114

115115
// the first argument is the channel number
116-
uint num = mp_obj_get_int(args[0]);
116+
int32_t idx = mp_obj_get_int(args[0]) - 1;
117117
const pin_obj_t *pin;
118118
uint channel;
119-
switch (num) {
119+
switch (idx) {
120120
case 0:
121121
channel = ADC_CH_0;
122122
pin = &pin_GPIO2;
@@ -139,10 +139,10 @@ STATIC mp_obj_t adc_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw,
139139
}
140140

141141
// disable the callback before re-configuring
142-
pyb_adc_obj_t *self = &pyb_adc_obj[num];
142+
pyb_adc_obj_t *self = &pyb_adc_obj[idx];
143143
self->base.type = &pyb_adc_type;
144144
self->channel = channel;
145-
self->num = num;
145+
self->idx = idx;
146146

147147
// configure the pin in analog mode
148148
pin_config ((pin_obj_t *)pin, PIN_MODE_0, GPIO_DIR_MODE_IN, PYBPIN_ANALOG_TYPE, PIN_STRENGTH_2MA);

cc3200/mods/pybi2c.c

Lines changed: 48 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@
5656
///
5757
/// from pyb import I2C
5858
///
59-
/// i2c = I2C() # create
60-
/// i2c = I2C(50000) # create and init with a 50KHz baudrate
61-
/// i2c.init(100000) # init with a 100KHz baudrate
62-
/// i2c.deinit() # turn off the peripheral
59+
/// i2c = I2C(1) # create
60+
/// i2c = I2C(1, 50000) # create and init with a 50KHz baudrate
61+
/// i2c.init(100000) # init with a 100KHz baudrate
62+
/// i2c.deinit() # turn off the peripheral
6363
///
6464
/// Printing the i2c object gives you information about its configuration.
6565
///
@@ -76,7 +76,7 @@
7676
///
7777
/// A master must specify the recipient's address:
7878
///
79-
/// i2c.init(100000)
79+
/// i2c.init(1, 100000)
8080
/// i2c.send('123', 0x42) # send 3 bytes to slave with address 0x42
8181
/// i2c.send(b'456', addr=0x42) # keyword for address
8282
///
@@ -98,6 +98,8 @@ typedef struct _pyb_i2c_obj_t {
9898
/******************************************************************************
9999
DEFINE CONSTANTS
100100
******************************************************************************/
101+
#define PYBI2C_MASTER (0)
102+
101103
#define PYBI2C_MIN_BAUD_RATE_HZ (50000)
102104
#define PYBI2C_MAX_BAUD_RATE_HZ (400000)
103105

@@ -251,16 +253,35 @@ STATIC bool pyb_i2c_scan_device(byte devAddr) {
251253
/******************************************************************************/
252254
/* Micro Python bindings */
253255
/******************************************************************************/
256+
STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
257+
pyb_i2c_obj_t *self = self_in;
258+
if (self->baudrate > 0) {
259+
mp_printf(print, "<I2C1, I2C.MASTER, baudrate=%u>)", self->baudrate);
260+
}
261+
else {
262+
mp_print_str(print, "<I2C1>");
263+
}
264+
}
254265

255-
/// \method init(100000)
266+
/// \method init(mode, *, baudrate=100000)
256267
///
257-
/// Initialise the I2C bus as a master with the given baudrate.
268+
/// Initialise the I2C bus with the given parameters:
258269
///
259-
STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self_in, mp_obj_t baudrate) {
260-
pyb_i2c_obj_t *self = self_in;
270+
/// - `mode` must be either `I2C.MASTER` or `I2C.SLAVE`
271+
/// - `baudrate` is the SCL clock rate (only sensible for a master)
272+
STATIC const mp_arg_t pyb_i2c_init_args[] = {
273+
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, },
274+
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
275+
};
276+
#define PYB_I2C_INIT_NUM_ARGS MP_ARRAY_SIZE(pyb_i2c_init_args)
277+
278+
STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
279+
// parse args
280+
mp_arg_val_t vals[PYB_I2C_INIT_NUM_ARGS];
281+
mp_arg_parse_all(n_args, args, kw_args, PYB_I2C_INIT_NUM_ARGS, pyb_i2c_init_args, vals);
261282

262283
// make sure the baudrate is between the valid range
263-
self->baudrate = MIN(MAX(mp_obj_get_int(baudrate), PYBI2C_MIN_BAUD_RATE_HZ), PYBI2C_MAX_BAUD_RATE_HZ);
284+
self->baudrate = MIN(MAX(vals[1].u_int, PYBI2C_MIN_BAUD_RATE_HZ), PYBI2C_MAX_BAUD_RATE_HZ);
264285

265286
// init the I2C bus
266287
i2c_init(self);
@@ -273,7 +294,7 @@ STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self_in, mp_obj_t baudrate) {
273294

274295
/// \classmethod \constructor(bus, ...)
275296
///
276-
/// Construct an I2C object on the given bus. `bus` can only be 0.
297+
/// Construct an I2C object on the given bus. `bus` can only be 1.
277298
/// With no additional parameters, the I2C object is created but not
278299
/// initialised (it has the settings from the last initialisation of
279300
/// the bus, if any). If extra arguments are given, the bus is initialised.
@@ -282,32 +303,29 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
282303
// check arguments
283304
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
284305

306+
// work out the i2c bus id
307+
if (mp_obj_get_int(args[0]) != 1) {
308+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
309+
}
310+
285311
// setup the object
286312
pyb_i2c_obj_t *self = &pyb_i2c_obj;
287313
self->base.type = &pyb_i2c_type;
288314

289-
if (n_args > 0) {
315+
if (n_args > 1 || n_kw > 0) {
290316
// start the peripheral
291-
pyb_i2c_init_helper(self, *args);
317+
mp_map_t kw_args;
318+
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
319+
pyb_i2c_init_helper(self, n_args - 1, args + 1, &kw_args);
292320
}
293321

294322
return (mp_obj_t)self;
295323
}
296324

297-
STATIC void pyb_i2c_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
298-
pyb_i2c_obj_t *self = self_in;
299-
if (self->baudrate > 0) {
300-
mp_printf(print, "<I2C0, I2C.MASTER, baudrate=%u>)", self->baudrate);
301-
}
302-
else {
303-
mp_print_str(print, "<I2C0>");
304-
}
325+
STATIC mp_obj_t pyb_i2c_init(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
326+
return pyb_i2c_init_helper(args[0], n_args - 1, args + 1, kw_args);
305327
}
306-
307-
STATIC mp_obj_t pyb_i2c_init(mp_obj_t self_in, mp_obj_t baudrate) {
308-
return pyb_i2c_init_helper(self_in, baudrate);
309-
}
310-
STATIC MP_DEFINE_CONST_FUN_OBJ_2(pyb_i2c_init_obj, pyb_i2c_init);
328+
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(pyb_i2c_init_obj, 1, pyb_i2c_init);
311329

312330
/// \method deinit()
313331
/// Turn off the I2C bus.
@@ -529,6 +547,10 @@ STATIC const mp_map_elem_t pyb_i2c_locals_dict_table[] = {
529547
{ MP_OBJ_NEW_QSTR(MP_QSTR_recv), (mp_obj_t)&pyb_i2c_recv_obj },
530548
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_read), (mp_obj_t)&pyb_i2c_mem_read_obj },
531549
{ MP_OBJ_NEW_QSTR(MP_QSTR_mem_write), (mp_obj_t)&pyb_i2c_mem_write_obj },
550+
551+
// class constants
552+
/// \constant MASTER - for initialising the bus to master mode
553+
{ MP_OBJ_NEW_QSTR(MP_QSTR_MASTER), MP_OBJ_NEW_SMALL_INT(PYBI2C_MASTER) },
532554
};
533555

534556
STATIC MP_DEFINE_CONST_DICT(pyb_i2c_locals_dict, pyb_i2c_locals_dict_table);

cc3200/mods/pybspi.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -167,24 +167,26 @@ STATIC void pyb_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
167167
pyb_spi_obj_t *self = self_in;
168168

169169
if (self->baudrate > 0) {
170-
mp_printf(print, "<SPI0, SPI.MASTER, baudrate=%u, config=%u, submode=%u, bits=%u>",
171-
self->baudrate, self->config, self->submode, (self->wlen * 8));
170+
mp_printf(print, "<SPI1, SPI.MASTER, baudrate=%u, config=%u, submode=%u, bits=%u>",
171+
self->baudrate, self->config, self->submode, (self->wlen * 8));
172172
}
173173
else {
174-
mp_print_str(print, "<SPI0>");
174+
mp_print_str(print, "<SPI1>");
175175
}
176176
}
177177

178-
/// \method init(2000000, *, bits=8, submode=0, cs=SPI.ACTIVELOW)
178+
/// \method init(mode, *, baudrate=1000000, bits=8, submode=0, cs=SPI.ACTIVELOW)
179179
///
180180
/// Initialise the SPI bus with the given parameters:
181181
///
182+
/// - `mode` must be MASTER.
182183
/// - `baudrate` is the SCK clock rate.
183184
/// - `bits` is the transfer width size (8, 16, 32).
184185
/// - `submode` is the spi mode (0, 1, 2, 3).
185186
/// - `cs` can be ACTIVELOW, ACTIVEHIGH, or NONE
186187
static const mp_arg_t pybspi_init_args[] = {
187-
{ MP_QSTR_baudrate, MP_ARG_REQUIRED | MP_ARG_INT, },
188+
{ MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, },
189+
{ MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000000} },
188190
{ MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} },
189191
{ MP_QSTR_submode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
190192
{ MP_QSTR_cs, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = SPI_CS_ACTIVELOW} },
@@ -195,13 +197,8 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const
195197
mp_arg_val_t args[MP_ARRAY_SIZE(pybspi_init_args)];
196198
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(pybspi_init_args), pybspi_init_args, args);
197199

198-
uint submode = args[2].u_int;
199-
uint cs = args[3].u_int;
200200
uint bits;
201-
202-
// save the word length for later use
203-
self->wlen = args[1].u_int / 8;
204-
switch (args[1].u_int) {
201+
switch (args[2].u_int) {
205202
case 8:
206203
bits = SPI_WL_8;
207204
break;
@@ -216,16 +213,19 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const
216213
break;
217214
}
218215

216+
uint submode = args[3].u_int;
219217
if (submode < SPI_SUB_MODE_0 || submode > SPI_SUB_MODE_3) {
220218
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
221219
}
222220

221+
uint cs = args[4].u_int;
223222
if (cs != SPI_CS_ACTIVELOW && cs != SPI_CS_ACTIVEHIGH) {
224223
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
225224
}
226225

227226
// build the configuration
228-
self->baudrate = args[0].u_int;
227+
self->baudrate = args[1].u_int;
228+
self->wlen = args[2].u_int >> 3;
229229
self->config = bits | cs | SPI_SW_CTRL_CS | SPI_4PIN_MODE | SPI_TURBO_OFF;
230230
self->submode = submode;
231231

@@ -240,8 +240,8 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const
240240

241241
/// \classmethod \constructor(bus, ...)
242242
///
243-
/// Construct an SPI object with the given baudrate.
244-
/// With no parameters, the SPI object is created but not
243+
/// Construct an SPI object with the given baudrate. Bus can only be 1.
244+
/// With no extra parameters, the SPI object is created but not
245245
/// initialised (it has the settings from the last initialisation of
246246
/// the bus, if any). If extra arguments are given, the bus is initialised.
247247
/// See `init` for parameters of initialisation.
@@ -250,14 +250,19 @@ STATIC mp_obj_t pyb_spi_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
250250
// check arguments
251251
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
252252

253+
// work out the spi bus id
254+
if (mp_obj_get_int(args[0]) != 1) {
255+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
256+
}
257+
253258
pyb_spi_obj_t *self = &pyb_spi_obj;
254259
self->base.type = &pyb_spi_type;
255260

256-
if (n_args > 0 || n_kw > 0) {
261+
if (n_args > 1 || n_kw > 0) {
257262
// start the peripheral
258263
mp_map_t kw_args;
259264
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
260-
pyb_spi_init_helper(self, n_args, args, &kw_args);
265+
pyb_spi_init_helper(self, n_args - 1, args + 1, &kw_args);
261266
}
262267

263268
return self;
@@ -396,6 +401,7 @@ STATIC const mp_map_elem_t pyb_spi_locals_dict_table[] = {
396401
{ MP_OBJ_NEW_QSTR(MP_QSTR_send_recv), (mp_obj_t)&pyb_spi_send_recv_obj },
397402

398403
// class constants
404+
{ MP_OBJ_NEW_QSTR(MP_QSTR_MASTER), MP_OBJ_NEW_SMALL_INT(SPI_MODE_MASTER) },
399405
{ MP_OBJ_NEW_QSTR(MP_QSTR_ACTIVE_LOW), MP_OBJ_NEW_SMALL_INT(SPI_CS_ACTIVELOW) },
400406
{ MP_OBJ_NEW_QSTR(MP_QSTR_ACTIVE_HIGH), MP_OBJ_NEW_SMALL_INT(SPI_CS_ACTIVEHIGH) },
401407
};

cc3200/mods/pybtimer.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@
6060
///
6161
/// Example usage to toggle an LED at a fixed frequency:
6262
///
63-
/// tim = pyb.Timer(3) # create a timer object using timer 4
63+
/// tim = pyb.Timer(4) # create a timer object using timer 4
6464
/// tim.init(mode=Timer.PERIODIC) # initialize it in periodic mode
6565
/// tim_ch = tim.channel(Timer.A, freq=2) # configure channel A at a frequency of 2Hz
6666
/// tim_ch.callback(handler=lambda t:led.toggle()) # toggle a led on every cycle of the timer
6767
///
6868
/// Further examples:
6969
///
7070
/// tim1 = pyb.Timer(2, mode=Timer.EVENT_COUNT) # initialize it capture mode
71-
/// tim2 = pyb.Timer(0, mode=Timer.PWM) # initialize it in PWM mode
71+
/// tim2 = pyb.Timer(1, mode=Timer.PWM) # initialize it in PWM mode
7272
/// tim_ch = tim1.channel(Timer.A, freq=1, polarity=Timer.POSITIVE) # start the PWM on channel B with a 50% duty cycle
7373
/// tim_ch = tim2.channel(Timer.B, freq=10000, duty_cycle=50) # start the event counter with a frequency of 1Hz and triggered by positive edges
7474
/// tim_ch.time() # get the current time in usec (can also be set)
@@ -303,7 +303,7 @@ STATIC void pyb_timer_print(const mp_print_t *print, mp_obj_t self_in, mp_print_
303303
default:
304304
break;
305305
}
306-
mp_printf(print, "<Timer%u, mode=Timer.%q>", tim->id, mode_qst);
306+
mp_printf(print, "<Timer%u, mode=Timer.%q>", (tim->id + 1), mode_qst);
307307
}
308308

309309
/// \method init(mode, *, width)
@@ -360,13 +360,13 @@ STATIC mp_obj_t pyb_timer_init_helper(pyb_timer_obj_t *tim, mp_uint_t n_args, co
360360
/// \classmethod \constructor(id, ...)
361361
/// Construct a new timer object of the given id. If additional
362362
/// arguments are given, then the timer is initialised by `init(...)`.
363-
/// `id` can be 0 to 3
363+
/// `id` can be 1 to 4
364364
STATIC mp_obj_t pyb_timer_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_kw, const mp_obj_t *args) {
365365
// check arguments
366366
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
367367

368368
// create a new Timer object
369-
uint32_t timer_idx = mp_obj_get_int(args[0]);
369+
int32_t timer_idx = mp_obj_get_int(args[0]) - 1;
370370
if (timer_idx < 0 || timer_idx > (PYBTIMER_NUM_TIMERS - 1)) {
371371
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
372372
}
@@ -579,7 +579,7 @@ STATIC void pyb_timer_channel_print(const mp_print_t *print, mp_obj_t self_in, m
579579
}
580580

581581
mp_printf(print, "<%q %s, timer=%u, %q=%u", MP_QSTR_TimerChannel,
582-
ch_id, ch->timer->id, MP_QSTR_freq, ch->frequency);
582+
ch_id, (ch->timer->id + 1), MP_QSTR_freq, ch->frequency);
583583

584584
uint32_t mode = ch->timer->config & 0xFF;
585585
if (mode == TIMER_CFG_A_CAP_COUNT || mode == TIMER_CFG_A_CAP_TIME || mode == TIMER_CFG_A_PWM) {

0 commit comments

Comments
 (0)