@@ -68,41 +68,50 @@ STATIC void mp_machine_spi_transfer(mp_obj_t self, size_t len, const void *src,
6868STATIC mp_obj_t mp_machine_spi_read (size_t n_args , const mp_obj_t * args ) {
6969 vstr_t vstr ;
7070 vstr_init_len (& vstr , mp_obj_get_int (args [1 ]));
71- memset (vstr .buf , n_args == 3 ? mp_obj_get_int (args [2 ]) : 0 , vstr .len );
72- mp_machine_spi_transfer (args [0 ], vstr .len , vstr .buf , vstr .buf );
71+ memset (vstr .buf , n_args >= 3 ? mp_obj_get_int (args [2 ]) : 0 , vstr .len );
72+ uint8_t bits = n_args == 4 ? mp_obj_get_int (args [3 ]) : 0 ;
73+ mp_machine_spi_transfer (args [0 ], vstr .len , vstr .buf , vstr .buf , bits );
7374 return mp_obj_new_str_from_vstr (& mp_type_bytes , & vstr );
7475}
75- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mp_machine_spi_read_obj , 2 , 3 , mp_machine_spi_read );
76+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mp_machine_spi_read_obj , 2 , 4 , mp_machine_spi_read );
7677
7778STATIC mp_obj_t mp_machine_spi_readinto (size_t n_args , const mp_obj_t * args ) {
7879 mp_buffer_info_t bufinfo ;
7980 mp_get_buffer_raise (args [1 ], & bufinfo , MP_BUFFER_WRITE );
80- memset (bufinfo .buf , n_args == 3 ? mp_obj_get_int (args [2 ]) : 0 , bufinfo .len );
81- mp_machine_spi_transfer (args [0 ], bufinfo .len , bufinfo .buf , bufinfo .buf );
81+ memset (bufinfo .buf , n_args >= 3 ? mp_obj_get_int (args [2 ]) : 0 , bufinfo .len );
82+ uint8_t bits = n_args == 4 ? mp_obj_get_int (args [3 ]) : 0 ;
83+ mp_machine_spi_transfer (args [0 ], bufinfo .len , bufinfo .buf , bufinfo .buf , bits );
8284 return mp_const_none ;
8385}
84- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mp_machine_spi_readinto_obj , 2 , 3 , mp_machine_spi_readinto );
86+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mp_machine_spi_readinto_obj , 2 , 4 , mp_machine_spi_readinto );
8587
86- STATIC mp_obj_t mp_machine_spi_write (mp_obj_t self , mp_obj_t wr_buf ) {
88+ STATIC mp_obj_t mp_machine_spi_write (size_t n_args , const mp_obj_t * args ) {
89+ mp_obj_t self = args [0 ];
90+ mp_obj_t wr_buf = args [1 ];
91+ uint8_t bits = n_args == 3 ? mp_obj_get_int (args [2 ]) : 0 ;
8792 mp_buffer_info_t src ;
8893 mp_get_buffer_raise (wr_buf , & src , MP_BUFFER_READ );
8994 mp_machine_spi_transfer (self , src .len , (const uint8_t * )src .buf , NULL );
9095 return mp_const_none ;
9196}
92- MP_DEFINE_CONST_FUN_OBJ_2 (mp_machine_spi_write_obj , mp_machine_spi_write );
97+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mp_machine_spi_write_obj , 2 , 3 , mp_machine_spi_write );
9398
94- STATIC mp_obj_t mp_machine_spi_write_readinto (mp_obj_t self , mp_obj_t wr_buf , mp_obj_t rd_buf ) {
99+ STATIC mp_obj_t mp_machine_spi_write_readinto (size_t n_args , const mp_obj_t * args ) {
100+ mp_obj_t self = args [0 ];
101+ mp_obj_t wr_buf = args [1 ];
102+ mp_obj_t rd_buf = args [2 ];
103+ uint8_t bits = n_args == 4 ? mp_obj_get_int (args [3 ]) : 0 ;
95104 mp_buffer_info_t src ;
96105 mp_get_buffer_raise (wr_buf , & src , MP_BUFFER_READ );
97106 mp_buffer_info_t dest ;
98107 mp_get_buffer_raise (rd_buf , & dest , MP_BUFFER_WRITE );
99108 if (src .len != dest .len ) {
100109 mp_raise_ValueError (MP_ERROR_TEXT ("buffers must be the same length" ));
101110 }
102- mp_machine_spi_transfer (self , src .len , src .buf , dest .buf );
111+ mp_machine_spi_transfer (self , src .len , src .buf , dest .buf , bits );
103112 return mp_const_none ;
104113}
105- MP_DEFINE_CONST_FUN_OBJ_3 (mp_machine_spi_write_readinto_obj , mp_machine_spi_write_readinto );
114+ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (mp_machine_spi_write_readinto_obj , 3 , 4 , mp_machine_spi_write_readinto );
106115
107116STATIC const mp_rom_map_elem_t machine_spi_locals_dict_table [] = {
108117 { MP_ROM_QSTR (MP_QSTR_init ), MP_ROM_PTR (& machine_spi_init_obj ) },
@@ -179,8 +188,9 @@ STATIC mp_obj_t mp_machine_soft_spi_make_new(const mp_obj_type_t *type, size_t n
179188 self -> spi .delay_half = baudrate_to_delay_half (args [ARG_baudrate ].u_int );
180189 self -> spi .polarity = args [ARG_polarity ].u_int ;
181190 self -> spi .phase = args [ARG_phase ].u_int ;
182- if (args [ARG_bits ].u_int != 8 ) {
183- mp_raise_ValueError (MP_ERROR_TEXT ("bits must be 8" ));
191+ self -> spi .bits = args [ARG_bits ].u_int ;
192+ if (self -> spi .bits == 0 ) {
193+ mp_raise_ValueError ("bits must be > 0" );
184194 }
185195 if (args [ARG_firstbit ].u_int != MICROPY_PY_MACHINE_SPI_MSB ) {
186196 mp_raise_ValueError (MP_ERROR_TEXT ("firstbit must be MSB" ));
0 commit comments