diff options
Diffstat (limited to 'cc3200/mods/pybspi.c')
-rw-r--r-- | cc3200/mods/pybspi.c | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/cc3200/mods/pybspi.c b/cc3200/mods/pybspi.c index bcd673225d..390f6cc343 100644 --- a/cc3200/mods/pybspi.c +++ b/cc3200/mods/pybspi.c @@ -167,24 +167,26 @@ STATIC void pyb_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki pyb_spi_obj_t *self = self_in; if (self->baudrate > 0) { - mp_printf(print, "<SPI0, SPI.MASTER, baudrate=%u, config=%u, submode=%u, bits=%u>", - self->baudrate, self->config, self->submode, (self->wlen * 8)); + mp_printf(print, "<SPI1, SPI.MASTER, baudrate=%u, config=%u, submode=%u, bits=%u>", + self->baudrate, self->config, self->submode, (self->wlen * 8)); } else { - mp_print_str(print, "<SPI0>"); + mp_print_str(print, "<SPI1>"); } } -/// \method init(2000000, *, bits=8, submode=0, cs=SPI.ACTIVELOW) +/// \method init(mode, *, baudrate=1000000, bits=8, submode=0, cs=SPI.ACTIVELOW) /// /// Initialise the SPI bus with the given parameters: /// +/// - `mode` must be MASTER. /// - `baudrate` is the SCK clock rate. /// - `bits` is the transfer width size (8, 16, 32). /// - `submode` is the spi mode (0, 1, 2, 3). /// - `cs` can be ACTIVELOW, ACTIVEHIGH, or NONE static const mp_arg_t pybspi_init_args[] = { - { MP_QSTR_baudrate, MP_ARG_REQUIRED | MP_ARG_INT, }, + { MP_QSTR_mode, MP_ARG_REQUIRED | MP_ARG_INT, }, + { MP_QSTR_baudrate, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1000000} }, { MP_QSTR_bits, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, { MP_QSTR_submode, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, { 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 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); - uint submode = args[2].u_int; - uint cs = args[3].u_int; uint bits; - - // save the word length for later use - self->wlen = args[1].u_int / 8; - switch (args[1].u_int) { + switch (args[2].u_int) { case 8: bits = SPI_WL_8; break; @@ -216,16 +213,19 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const break; } + uint submode = args[3].u_int; if (submode < SPI_SUB_MODE_0 || submode > SPI_SUB_MODE_3) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); } + uint cs = args[4].u_int; if (cs != SPI_CS_ACTIVELOW && cs != SPI_CS_ACTIVEHIGH) { nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, mpexception_value_invalid_arguments)); } // build the configuration - self->baudrate = args[0].u_int; + self->baudrate = args[1].u_int; + self->wlen = args[2].u_int >> 3; self->config = bits | cs | SPI_SW_CTRL_CS | SPI_4PIN_MODE | SPI_TURBO_OFF; self->submode = submode; @@ -240,8 +240,8 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const /// \classmethod \constructor(bus, ...) /// -/// Construct an SPI object with the given baudrate. -/// With no parameters, the SPI object is created but not +/// Construct an SPI object with the given baudrate. Bus can only be 1. +/// With no extra parameters, the SPI object is created but not /// initialised (it has the settings from the last initialisation of /// the bus, if any). If extra arguments are given, the bus is initialised. /// 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 // 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; - if (n_args > 0 || n_kw > 0) { + if (n_args > 1 || n_kw > 0) { // start the peripheral mp_map_t kw_args; mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); - pyb_spi_init_helper(self, n_args, args, &kw_args); + pyb_spi_init_helper(self, n_args - 1, args + 1, &kw_args); } return self; @@ -396,6 +401,7 @@ STATIC const mp_map_elem_t pyb_spi_locals_dict_table[] = { { MP_OBJ_NEW_QSTR(MP_QSTR_send_recv), (mp_obj_t)&pyb_spi_send_recv_obj }, // class constants + { MP_OBJ_NEW_QSTR(MP_QSTR_MASTER), MP_OBJ_NEW_SMALL_INT(SPI_MODE_MASTER) }, { MP_OBJ_NEW_QSTR(MP_QSTR_ACTIVE_LOW), MP_OBJ_NEW_SMALL_INT(SPI_CS_ACTIVELOW) }, { MP_OBJ_NEW_QSTR(MP_QSTR_ACTIVE_HIGH), MP_OBJ_NEW_SMALL_INT(SPI_CS_ACTIVEHIGH) }, }; |