Skip to content

Commit fabe79f

Browse files
author
Daniel Campora
committed
cc3200: Clean up exception handling.
1 parent 124aa00 commit fabe79f

5 files changed

Lines changed: 30 additions & 26 deletions

File tree

cc3200/mods/modwlan.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,9 @@ STATIC mp_obj_t wlan_init_helper(mp_uint_t n_args, const mp_obj_t *pos_args, mp_
657657
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
658658
}
659659

660-
// Force the channel to be between 1-11
661-
uint8_t channel = args[4].u_int > 0 ? args[4].u_int % 12 : 1;
660+
// force the channel to be between 1-11
661+
uint8_t channel = args[4].u_int;
662+
channel = (channel > 0 && channel != 12) ? channel % 12 : 1;
662663

663664
if (MODWLAN_OK != wlan_sl_enable (args[0].u_int, ssid, ssid_len, args[2].u_int, key, key_len, channel)) {
664665
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
@@ -700,15 +701,15 @@ STATIC mp_obj_t wlan_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k
700701
mp_arg_check_num(n_args, n_kw, 0, MP_ARRAY_SIZE(wlan_init_args), true);
701702

702703
if (n_args > 0) {
703-
// Get the mode
704+
// get the mode
704705
SlWlanMode_t mode = mp_obj_get_int(args[0]);
705706
if (mode == ROLE_AP) {
706707
// start the peripheral
707708
mp_map_t kw_args;
708709
mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
709710
wlan_init_helper(n_args, args, &kw_args);
710711
}
711-
// TODO: Only STA mode supported for the moment. What if P2P?
712+
// TODO only STA mode supported for the moment. What if P2P?
712713
else if (n_args == 1) {
713714
if (MODWLAN_OK != wlan_sl_enable (mode, NULL, 0, 0, NULL, 0, 0)) {
714715
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
@@ -717,12 +718,9 @@ STATIC mp_obj_t wlan_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k
717718
else {
718719
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, mpexception_num_type_invalid_arguments));
719720
}
720-
} else if (wlan_obj.mode < 0) {
721-
nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, mpexception_num_type_invalid_arguments));
722721
}
723722

724723
wlan_obj.base.type = (mp_obj_type_t*)&mod_network_nic_type_wlan;
725-
726724
return &wlan_obj;
727725
}
728726

cc3200/mods/pybi2c.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, mp_uint_t n_args, const
280280
mp_arg_val_t vals[PYB_I2C_INIT_NUM_ARGS];
281281
mp_arg_parse_all(n_args, args, kw_args, PYB_I2C_INIT_NUM_ARGS, pyb_i2c_init_args, vals);
282282

283+
// verify that mode is master
284+
if (vals[0].u_int != PYBI2C_MASTER) {
285+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
286+
}
287+
283288
// make sure the baudrate is between the valid range
284289
self->baudrate = MIN(MAX(vals[1].u_int, PYBI2C_MIN_BAUD_RATE_HZ), PYBI2C_MAX_BAUD_RATE_HZ);
285290

@@ -303,11 +308,6 @@ STATIC mp_obj_t pyb_i2c_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
303308
// check arguments
304309
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
305310

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-
311311
// setup the object
312312
pyb_i2c_obj_t *self = &pyb_i2c_obj;
313313
self->base.type = &pyb_i2c_type;

cc3200/mods/pybpin.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -367,32 +367,35 @@ STATIC mp_obj_t pin_obj_init_helper(pin_obj_t *self, mp_uint_t n_args, const mp_
367367
// get the af
368368
uint af = args[0].u_int;
369369
if (af < PIN_MODE_0 || af > PIN_MODE_15) {
370-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
370+
goto invalid_args;
371371
}
372372
// get the io mode
373373
uint mode = args[1].u_int;
374374
// checking the mode only makes sense if af == GPIO
375375
if (af == PIN_MODE_0) {
376376
if (mode != GPIO_DIR_MODE_IN && mode != GPIO_DIR_MODE_OUT) {
377-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
377+
goto invalid_args;
378378
}
379379
}
380380
// get the type
381381
uint type = args[2].u_int;
382382
if (type != PIN_TYPE_STD && type != PIN_TYPE_STD_PU && type != PIN_TYPE_STD_PD &&
383383
type != PIN_TYPE_OD && type != PIN_TYPE_OD_PU && type != PIN_TYPE_OD_PD) {
384-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
384+
goto invalid_args;
385385
}
386386
// get the strenght
387387
uint strength = args[3].u_int;
388388
if (strength != PIN_STRENGTH_2MA && strength != PIN_STRENGTH_4MA && strength != PIN_STRENGTH_6MA) {
389-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
389+
goto invalid_args;
390390
}
391391

392392
// configure the pin as requested
393393
pin_config (self, af, mode, type, strength);
394394

395395
return mp_const_none;
396+
397+
invalid_args:
398+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
396399
}
397400

398401
/// \method print()
@@ -560,7 +563,7 @@ STATIC mp_obj_t pin_callback (mp_uint_t n_args, const mp_obj_t *pos_args, mp_map
560563
uint intmode = args[0].u_int;
561564
if (intmode != GPIO_FALLING_EDGE && intmode != GPIO_RISING_EDGE && intmode != GPIO_BOTH_EDGES &&
562565
intmode != GPIO_LOW_LEVEL && intmode != GPIO_HIGH_LEVEL) {
563-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
566+
goto invalid_args;
564567
}
565568

566569
uint pwrmode = args[4].u_int;

cc3200/mods/pybspi.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,11 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const
201201
mp_arg_val_t args[MP_ARRAY_SIZE(pybspi_init_args)];
202202
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(pybspi_init_args), pybspi_init_args, args);
203203

204+
// verify that mode is master
205+
if (args[0].u_int != SPI_MODE_MASTER) {
206+
goto invalid_args;
207+
}
208+
204209
uint bits;
205210
switch (args[2].u_int) {
206211
case 8:
@@ -213,7 +218,7 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const
213218
bits = SPI_WL_32;
214219
break;
215220
default:
216-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
221+
goto invalid_args;
217222
break;
218223
}
219224

@@ -240,7 +245,7 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const
240245

241246
uint nss = args[5].u_int;
242247
if (nss != SPI_CS_ACTIVELOW && nss != SPI_CS_ACTIVEHIGH) {
243-
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
248+
goto invalid_args;
244249
}
245250

246251
// build the configuration
@@ -258,6 +263,9 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const
258263
pybsleep_add((const mp_obj_t)self, (WakeUpCB_t)pybspi_init);
259264

260265
return mp_const_none;
266+
267+
invalid_args:
268+
nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments));
261269
}
262270

263271
/// \classmethod \constructor(bus, ...)
@@ -272,11 +280,6 @@ STATIC mp_obj_t pyb_spi_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
272280
// check arguments
273281
mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
274282

275-
// work out the spi bus id
276-
if (mp_obj_get_int(args[0]) != 1) {
277-
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable));
278-
}
279-
280283
pyb_spi_obj_t *self = &pyb_spi_obj;
281284
self->base.type = &pyb_spi_type;
282285

cc3200/mods/pybuart.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ STATIC mp_obj_t pyb_uart_writechar(mp_obj_t self_in, mp_obj_t char_in) {
540540

541541
// send the character
542542
if (!uart_tx_char(self, data)) {
543-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ETIMEDOUT)));
543+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
544544
}
545545

546546
return mp_const_none;
@@ -624,7 +624,7 @@ STATIC mp_uint_t pyb_uart_write(mp_obj_t self_in, const void *buf_in, mp_uint_t
624624

625625
// write the data
626626
if (!uart_tx_strn(self, buf, size)) {
627-
nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ETIMEDOUT)));
627+
nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed));
628628
}
629629
return size;
630630
}

0 commit comments

Comments
 (0)