From fabe79f7afe6f4ee0bdc8ad41c83acb2a0e9eac8 Mon Sep 17 00:00:00 2001 From: Daniel Campora Date: Tue, 26 May 2015 12:27:13 +0200 Subject: [PATCH] cc3200: Clean up exception handling. --- cc3200/mods/modwlan.c | 12 +++++------- cc3200/mods/pybi2c.c | 10 +++++----- cc3200/mods/pybpin.c | 13 ++++++++----- cc3200/mods/pybspi.c | 17 ++++++++++------- cc3200/mods/pybuart.c | 4 ++-- 5 files changed, 30 insertions(+), 26 deletions(-) diff --git a/cc3200/mods/modwlan.c b/cc3200/mods/modwlan.c index f5a7c962bd..1efecf41a6 100644 --- a/cc3200/mods/modwlan.c +++ b/cc3200/mods/modwlan.c @@ -657,8 +657,9 @@ STATIC mp_obj_t wlan_init_helper(mp_uint_t n_args, const mp_obj_t *pos_args, mp_ nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); } - // Force the channel to be between 1-11 - uint8_t channel = args[4].u_int > 0 ? args[4].u_int % 12 : 1; + // force the channel to be between 1-11 + uint8_t channel = args[4].u_int; + channel = (channel > 0 && channel != 12) ? channel % 12 : 1; if (MODWLAN_OK != wlan_sl_enable (args[0].u_int, ssid, ssid_len, args[2].u_int, key, key_len, channel)) { nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); @@ -700,7 +701,7 @@ STATIC mp_obj_t wlan_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k mp_arg_check_num(n_args, n_kw, 0, MP_ARRAY_SIZE(wlan_init_args), true); if (n_args > 0) { - // Get the mode + // get the mode SlWlanMode_t mode = mp_obj_get_int(args[0]); if (mode == ROLE_AP) { // start the peripheral @@ -708,7 +709,7 @@ STATIC mp_obj_t wlan_make_new (mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n_k mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); wlan_init_helper(n_args, args, &kw_args); } - // TODO: Only STA mode supported for the moment. What if P2P? + // TODO only STA mode supported for the moment. What if P2P? else if (n_args == 1) { if (MODWLAN_OK != wlan_sl_enable (mode, NULL, 0, 0, NULL, 0, 0)) { 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 else { nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, mpexception_num_type_invalid_arguments)); } - } else if (wlan_obj.mode < 0) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_TypeError, mpexception_num_type_invalid_arguments)); } wlan_obj.base.type = (mp_obj_type_t*)&mod_network_nic_type_wlan; - return &wlan_obj; } diff --git a/cc3200/mods/pybi2c.c b/cc3200/mods/pybi2c.c index 7d4f70e867..1b9c6ca16c 100644 --- a/cc3200/mods/pybi2c.c +++ b/cc3200/mods/pybi2c.c @@ -280,6 +280,11 @@ STATIC mp_obj_t pyb_i2c_init_helper(pyb_i2c_obj_t *self, mp_uint_t n_args, const 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); + // verify that mode is master + if (vals[0].u_int != PYBI2C_MASTER) { + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); + } + // make sure the baudrate is between the valid range self->baudrate = MIN(MAX(vals[1].u_int, PYBI2C_MIN_BAUD_RATE_HZ), PYBI2C_MAX_BAUD_RATE_HZ); @@ -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 // check arguments mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); - // work out the i2c bus id - if (mp_obj_get_int(args[0]) != 1) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable)); - } - // setup the object pyb_i2c_obj_t *self = &pyb_i2c_obj; self->base.type = &pyb_i2c_type; diff --git a/cc3200/mods/pybpin.c b/cc3200/mods/pybpin.c index e4661641f1..64a1250284 100644 --- a/cc3200/mods/pybpin.c +++ b/cc3200/mods/pybpin.c @@ -367,32 +367,35 @@ STATIC mp_obj_t pin_obj_init_helper(pin_obj_t *self, mp_uint_t n_args, const mp_ // get the af uint af = args[0].u_int; if (af < PIN_MODE_0 || af > PIN_MODE_15) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); + goto invalid_args; } // get the io mode uint mode = args[1].u_int; // checking the mode only makes sense if af == GPIO if (af == PIN_MODE_0) { if (mode != GPIO_DIR_MODE_IN && mode != GPIO_DIR_MODE_OUT) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); + goto invalid_args; } } // get the type uint type = args[2].u_int; if (type != PIN_TYPE_STD && type != PIN_TYPE_STD_PU && type != PIN_TYPE_STD_PD && type != PIN_TYPE_OD && type != PIN_TYPE_OD_PU && type != PIN_TYPE_OD_PD) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); + goto invalid_args; } // get the strenght uint strength = args[3].u_int; if (strength != PIN_STRENGTH_2MA && strength != PIN_STRENGTH_4MA && strength != PIN_STRENGTH_6MA) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); + goto invalid_args; } // configure the pin as requested pin_config (self, af, mode, type, strength); return mp_const_none; + +invalid_args: + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); } /// \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 uint intmode = args[0].u_int; if (intmode != GPIO_FALLING_EDGE && intmode != GPIO_RISING_EDGE && intmode != GPIO_BOTH_EDGES && intmode != GPIO_LOW_LEVEL && intmode != GPIO_HIGH_LEVEL) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); + goto invalid_args; } uint pwrmode = args[4].u_int; diff --git a/cc3200/mods/pybspi.c b/cc3200/mods/pybspi.c index ebd8159e41..3c8ff5b170 100644 --- a/cc3200/mods/pybspi.c +++ b/cc3200/mods/pybspi.c @@ -201,6 +201,11 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const mp_arg_val_t args[MP_ARRAY_SIZE(pybspi_init_args)]; mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(pybspi_init_args), pybspi_init_args, args); + // verify that mode is master + if (args[0].u_int != SPI_MODE_MASTER) { + goto invalid_args; + } + uint bits; switch (args[2].u_int) { 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 bits = SPI_WL_32; break; default: - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); + goto invalid_args; break; } @@ -240,7 +245,7 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const uint nss = args[5].u_int; if (nss != SPI_CS_ACTIVELOW && nss != SPI_CS_ACTIVEHIGH) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); + goto invalid_args; } // 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 pybsleep_add((const mp_obj_t)self, (WakeUpCB_t)pybspi_init); return mp_const_none; + +invalid_args: + nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); } /// \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 // check arguments mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); - // work out the spi bus id - if (mp_obj_get_int(args[0]) != 1) { - nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_resource_not_avaliable)); - } - pyb_spi_obj_t *self = &pyb_spi_obj; self->base.type = &pyb_spi_type; diff --git a/cc3200/mods/pybuart.c b/cc3200/mods/pybuart.c index b4fca81fa6..4586cb8c56 100644 --- a/cc3200/mods/pybuart.c +++ b/cc3200/mods/pybuart.c @@ -540,7 +540,7 @@ STATIC mp_obj_t pyb_uart_writechar(mp_obj_t self_in, mp_obj_t char_in) { // send the character if (!uart_tx_char(self, data)) { - nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ETIMEDOUT))); + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); } 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 // write the data if (!uart_tx_strn(self, buf, size)) { - nlr_raise(mp_obj_new_exception_arg1(&mp_type_OSError, MP_OBJ_NEW_SMALL_INT(ETIMEDOUT))); + nlr_raise(mp_obj_new_exception_msg(&mp_type_OSError, mpexception_os_operation_failed)); } return size; }