@@ -171,13 +171,12 @@ STATIC void pyb_spi_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
171171 mp_printf (print , "<SPI1, SPI.MASTER, baudrate=%u, bits=%u, polarity=%u, phase=%u, nss=%q>" ,
172172 self -> baudrate , (self -> wlen * 8 ), self -> polarity , self -> phase ,
173173 (self -> config & SPI_CS_ACTIVELOW ) ? MP_QSTR_ACTIVE_LOW : MP_QSTR_ACTIVE_HIGH );
174- }
175- else {
174+ } else {
176175 mp_print_str (print , "<SPI1>" );
177176 }
178177}
179178
180- /// \method init(mode, *, baudrate=1000000, bits=8, polarity=0, phase=0, nss=SPI.ACTIVELOW )
179+ /// \method init(mode, *, baudrate=1000000, bits=8, polarity=0, phase=0, nss=SPI.ACTIVE_LOW )
181180///
182181/// Initialise the SPI bus with the given parameters:
183182///
@@ -260,7 +259,6 @@ STATIC mp_obj_t pyb_spi_init_helper(pyb_spi_obj_t *self, mp_uint_t n_args, const
260259/// initialised (it has the settings from the last initialisation of
261260/// the bus, if any). If extra arguments are given, the bus is initialised.
262261/// See `init` for parameters of initialisation.
263- ///
264262STATIC mp_obj_t pyb_spi_make_new (mp_obj_t type_in , mp_uint_t n_args , mp_uint_t n_kw , const mp_obj_t * args ) {
265263 // check arguments
266264 mp_arg_check_num (n_args , n_kw , 1 , MP_OBJ_FUN_ARGS_MAX , true);
@@ -297,39 +295,59 @@ STATIC mp_obj_t pyb_spi_deinit(mp_obj_t self_in) {
297295}
298296STATIC MP_DEFINE_CONST_FUN_OBJ_1 (pyb_spi_deinit_obj , pyb_spi_deinit );
299297
300- /// \method send(send)
298+ /// \method send(send, *, timeout=5000 )
301299/// Send data on the bus:
302300///
303301/// - `send` is the data to send (a byte to send, or a buffer object).
302+ /// - `timeout` is the timeout in milliseconds to wait for the send.
304303///
305- STATIC mp_obj_t pyb_spi_send (mp_obj_t self_in , mp_obj_t send_o ) {
306- pyb_spi_obj_t * self = self_in ;
304+ STATIC mp_obj_t pyb_spi_send (mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
305+ static const mp_arg_t allowed_args [] = {
306+ { MP_QSTR_send , MP_ARG_REQUIRED | MP_ARG_OBJ , },
307+ { MP_QSTR_timeout , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 5000 } },
308+ };
309+
310+ // parse args
311+ pyb_spi_obj_t * self = pos_args [0 ];
312+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
313+ mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
314+
307315 // get the buffer to send from
308316 mp_buffer_info_t bufinfo ;
309317 uint8_t data [1 ];
310- pyb_buf_get_for_send (send_o , & bufinfo , data );
318+ pyb_buf_get_for_send (args [ 0 ]. u_obj , & bufinfo , data );
311319
312320 // just send
313321 pybspi_transfer (self , (const char * )bufinfo .buf , NULL , bufinfo .len );
314322
315323 return mp_const_none ;
316324}
317- STATIC MP_DEFINE_CONST_FUN_OBJ_2 (pyb_spi_send_obj , pyb_spi_send );
325+ STATIC MP_DEFINE_CONST_FUN_OBJ_KW (pyb_spi_send_obj , 1 , pyb_spi_send );
318326
319- /// \method recv(recv)
327+ /// \method recv(recv, *, timeout=5000 )
320328///
321329/// Receive data on the bus:
322330///
323331/// - `recv` can be an integer, which is the number of bytes to receive,
324332/// or a mutable buffer, which will be filled with received bytes.
333+ /// - `timeout` is the timeout in milliseconds to wait for the receive.
325334///
326335/// Return: if `recv` is an integer then a new buffer of the bytes received,
327336/// otherwise the same buffer that was passed in to `recv`.
328- STATIC mp_obj_t pyb_spi_recv (mp_obj_t self_in , mp_obj_t recv_o ) {
329- pyb_spi_obj_t * self = self_in ;
337+ STATIC mp_obj_t pyb_spi_recv (mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
338+ static const mp_arg_t allowed_args [] = {
339+ { MP_QSTR_recv , MP_ARG_REQUIRED | MP_ARG_OBJ , },
340+ { MP_QSTR_timeout , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 5000 } },
341+ };
342+
343+ // parse args
344+ pyb_spi_obj_t * self = pos_args [0 ];
345+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
346+ mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
347+
330348 // get the buffer to receive into
331349 vstr_t vstr ;
332- mp_obj_t o_ret = pyb_buf_get_for_recv (recv_o , & vstr );
350+ mp_obj_t o_ret = pyb_buf_get_for_recv (args [ 0 ]. u_obj , & vstr );
333351
334352 // just receive
335353 pybspi_transfer (self , NULL , vstr .buf , vstr .len );
@@ -341,20 +359,30 @@ STATIC mp_obj_t pyb_spi_recv(mp_obj_t self_in, mp_obj_t recv_o) {
341359 return mp_obj_new_str_from_vstr (& mp_type_bytes , & vstr );
342360 }
343361}
344- STATIC MP_DEFINE_CONST_FUN_OBJ_2 (pyb_spi_recv_obj , pyb_spi_recv );
362+ STATIC MP_DEFINE_CONST_FUN_OBJ_KW (pyb_spi_recv_obj , 1 , pyb_spi_recv );
345363
346- /// \method send_recv(send, recv)
364+ /// \method send_recv(send, recv=None, *, timeout=5000 )
347365///
348366/// Send and receive data on the bus at the same time:
349367///
350368/// - `send` is the data to send (an integer to send, or a buffer object).
351369/// - `recv` is a mutable buffer which will be filled with received bytes.
352370/// It can be the same as `send`, or omitted. If omitted, a new buffer will
353371/// be created.
372+ /// - `timeout` is the timeout in milliseconds to wait for the transaction to complete.
354373///
355374/// Return: the buffer with the received bytes.
356- STATIC mp_obj_t pyb_spi_send_recv (mp_uint_t n_args , const mp_obj_t * args ) {
357- pyb_spi_obj_t * self = args [0 ];
375+ STATIC mp_obj_t pyb_spi_send_recv (mp_uint_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
376+ static const mp_arg_t allowed_args [] = {
377+ { MP_QSTR_send , MP_ARG_REQUIRED | MP_ARG_OBJ , },
378+ { MP_QSTR_recv , MP_ARG_OBJ , {.u_obj = mp_const_none } },
379+ { MP_QSTR_timeout , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 5000 } },
380+ };
381+
382+ // parse args
383+ pyb_spi_obj_t * self = pos_args [0 ];
384+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
385+ mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
358386
359387 // get buffers to send from/receive to
360388 mp_buffer_info_t bufinfo_send ;
@@ -363,35 +391,34 @@ STATIC mp_obj_t pyb_spi_send_recv (mp_uint_t n_args, const mp_obj_t *args) {
363391 vstr_t vstr_recv ;
364392 mp_obj_t o_ret ;
365393
366- if (args [1 ] == args [2 ] ) {
394+ if (args [0 ]. u_obj == args [1 ]. u_obj ) {
367395 // same object for sending and receiving, it must be a r/w buffer
368- mp_get_buffer_raise (args [1 ] , & bufinfo_send , MP_BUFFER_RW );
396+ mp_get_buffer_raise (args [0 ]. u_obj , & bufinfo_send , MP_BUFFER_RW );
369397 bufinfo_recv = bufinfo_send ;
370- o_ret = args [1 ] ;
398+ o_ret = args [0 ]. u_obj ;
371399 } else {
372400 // get the buffer to send from
373- pyb_buf_get_for_send (args [1 ] , & bufinfo_send , data_send );
401+ pyb_buf_get_for_send (args [0 ]. u_obj , & bufinfo_send , data_send );
374402
375403 // get the buffer to receive into
376- if (n_args == 2 ) {
404+ if (args [ 1 ]. u_obj == mp_const_none ) {
377405 // only the send was argument given, so create a fresh buffer of the send length
378406 vstr_init_len (& vstr_recv , bufinfo_send .len );
379407 bufinfo_recv .len = vstr_recv .len ;
380408 bufinfo_recv .buf = vstr_recv .buf ;
381409 o_ret = MP_OBJ_NULL ;
382- }
383- else {
410+ } else {
384411 // recv argument given
385- mp_get_buffer_raise (args [2 ] , & bufinfo_recv , MP_BUFFER_WRITE );
412+ mp_get_buffer_raise (args [1 ]. u_obj , & bufinfo_recv , MP_BUFFER_WRITE );
386413 if (bufinfo_recv .len != bufinfo_send .len ) {
387414 nlr_raise (mp_obj_new_exception_msg (& mp_type_ValueError , mpexception_value_invalid_arguments ));
388415 }
389- o_ret = args [2 ] ;
416+ o_ret = args [1 ]. u_obj ;
390417 }
391418 }
392419
393420 // send and receive
394- pybspi_transfer (self , (const char * )bufinfo_send .buf , vstr_recv .buf , bufinfo_send .len );
421+ pybspi_transfer (self , (const char * )bufinfo_send .buf , bufinfo_recv .buf , bufinfo_send .len );
395422
396423 // return the received data
397424 if (o_ret != MP_OBJ_NULL ) {
@@ -400,7 +427,7 @@ STATIC mp_obj_t pyb_spi_send_recv (mp_uint_t n_args, const mp_obj_t *args) {
400427 return mp_obj_new_str_from_vstr (& mp_type_bytes , & vstr_recv );
401428 }
402429}
403- STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (pyb_spi_send_recv_obj , 2 , 3 , pyb_spi_send_recv );
430+ STATIC MP_DEFINE_CONST_FUN_OBJ_KW (pyb_spi_send_recv_obj , 1 , pyb_spi_send_recv );
404431
405432STATIC const mp_map_elem_t pyb_spi_locals_dict_table [] = {
406433 // instance methods
0 commit comments