5454/// parameters to init the SPI bus:
5555///
5656/// from pyb import SPI
57- /// spi = SPI(0, SPI.MASTER, baudrate= 2000000, bits=8, submode=0, cs=SPI.ACTIVE_LOW)
57+ /// spi = SPI(2000000, bits=8, submode=0, cs=SPI.ACTIVE_LOW)
5858///
59- /// Only required parameter is mode, which must be SPI.MASTER . Submode may be 0-3.
60- /// Bit accepts 8, 16, 32. Chip select values are ACTIVE_LOW, ACTIVE_HIGH or NONE
59+ /// Only required parameter is the baudrate, in Hz . Submode may be 0-3.
60+ /// Bit accepts 8, 16, 32. Chip select values are ACTIVE_LOW and ACTIVE_HIGH
6161///
6262/// Additional method for SPI:
6363///
@@ -182,18 +182,16 @@ STATIC void pyb_spi_print(void (*print)(void *env, const char *fmt, ...), void *
182182 }
183183}
184184
185- /// \method init(mode, baudrate= 2000000, *, bits=8, submode=0, cs=SPI.ACTIVELOW)
185+ /// \method init(2000000, *, bits=8, submode=0, cs=SPI.ACTIVELOW)
186186///
187187/// Initialise the SPI bus with the given parameters:
188188///
189- /// - `mode` must be `SPI.MASTER`.
190189/// - `baudrate` is the SCK clock rate.
191190/// - `bits` is the transfer width size (8, 16, 32).
192191/// - `submode` is the spi mode (0, 1, 2, 3).
193192/// - `cs` can be ACTIVELOW, ACTIVEHIGH, or NONE
194193static const mp_arg_t pybspi_init_args [] = {
195- { MP_QSTR_mode , MP_ARG_REQUIRED | MP_ARG_INT , {.u_int = SPI_MODE_MASTER } },
196- { MP_QSTR_baudrate , MP_ARG_INT , {.u_int = PYBSPI_DEF_BAUDRATE } },
194+ { MP_QSTR_baudrate , MP_ARG_REQUIRED | MP_ARG_INT , },
197195 { MP_QSTR_bits , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 8 } },
198196 { MP_QSTR_submode , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
199197 { MP_QSTR_cs , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = SPI_CS_ACTIVELOW } },
@@ -204,13 +202,13 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const
204202 mp_arg_val_t args [MP_ARRAY_SIZE (pybspi_init_args )];
205203 mp_arg_parse_all (n_args , pos_args , kw_args , MP_ARRAY_SIZE (pybspi_init_args ), pybspi_init_args , args );
206204
207- uint submode = args [3 ].u_int ;
208- uint cs = args [4 ].u_int ;
205+ uint submode = args [2 ].u_int ;
206+ uint cs = args [3 ].u_int ;
209207 uint bits ;
210208
211209 // save the word length for later use
212- self -> wlen = args [2 ].u_int / 8 ;
213- switch (args [2 ].u_int ) {
210+ self -> wlen = args [1 ].u_int / 8 ;
211+ switch (args [1 ].u_int ) {
214212 case 8 :
215213 bits = SPI_WL_8 ;
216214 break ;
@@ -229,12 +227,12 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const
229227 nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
230228 }
231229
232- if (cs != SPI_CS_ACTIVELOW && cs != SPI_CS_ACTIVEHIGH && cs != PYBSPI_CS_NONE ) {
230+ if (cs != SPI_CS_ACTIVELOW && cs != SPI_CS_ACTIVEHIGH ) {
233231 nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
234232 }
235233
236234 // build the configuration
237- self -> baudrate = args [1 ].u_int ;
235+ self -> baudrate = args [0 ].u_int ;
238236 self -> config = bits | cs | SPI_SW_CTRL_CS | SPI_4PIN_MODE | SPI_TURBO_OFF ;
239237 self -> submode = submode ;
240238
@@ -249,8 +247,8 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const
249247
250248/// \classmethod \constructor(bus, ...)
251249///
252- /// Construct an SPI object on the given bus. `bus` can be only 0 .
253- /// With no additional parameters, the SPI object is created but not
250+ /// Construct an SPI object with the given baudrate .
251+ /// With no parameters, the SPI object is created but not
254252/// initialised (it has the settings from the last initialisation of
255253/// the bus, if any). If extra arguments are given, the bus is initialised.
256254/// See `init` for parameters of initialisation.
@@ -259,21 +257,14 @@ STATIC mp_obj_t pyb_spi_make_new(mp_obj_t type_in, mp_uint_t n_args, mp_uint_t n
259257 // check arguments
260258 mp_arg_check_num (n_args , n_kw , 1 , MP_OBJ_FUN_ARGS_MAX , true);
261259
262- mp_int_t spi_id = mp_obj_get_int (args [0 ]);
263-
264- // check the spi bus id
265- if (spi_id != 0 ) {
266- nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
267- }
268-
269260 pyb_spi_obj_t * self = & pyb_spi_obj ;
270261 self -> base .type = & pyb_spi_type ;
271262
272- if (n_args > 1 || n_kw > 0 ) {
263+ if (n_args > 0 || n_kw > 0 ) {
273264 // start the peripheral
274265 mp_map_t kw_args ;
275266 mp_map_init_fixed_table (& kw_args , n_kw , args + n_args );
276- pyb_spi_init_helper (self , n_args - 1 , args + 1 , & kw_args );
267+ pyb_spi_init_helper (self , n_args , args , & kw_args );
277268 }
278269
279270 return self ;
@@ -408,7 +399,6 @@ STATIC const mp_map_elem_t pyb_spi_locals_dict_table[] = {
408399 { MP_OBJ_NEW_QSTR (MP_QSTR_send_recv ), (mp_obj_t )& pyb_spi_send_recv_obj },
409400
410401 // class constants
411- { MP_OBJ_NEW_QSTR (MP_QSTR_MASTER ), MP_OBJ_NEW_SMALL_INT (SPI_MODE_MASTER ) },
412402 { MP_OBJ_NEW_QSTR (MP_QSTR_ACTIVE_LOW ), MP_OBJ_NEW_SMALL_INT (SPI_CS_ACTIVELOW ) },
413403 { MP_OBJ_NEW_QSTR (MP_QSTR_ACTIVE_HIGH ), MP_OBJ_NEW_SMALL_INT (SPI_CS_ACTIVEHIGH ) },
414404};
0 commit comments