@@ -189,39 +189,92 @@ STATIC mp_obj_t nativeio_spi_obj_unlock(mp_obj_t self_in) {
189189}
190190MP_DEFINE_CONST_FUN_OBJ_1 (nativeio_spi_unlock_obj , nativeio_spi_obj_unlock );
191191
192- //| .. method:: SPI.write(buf )
192+ //| .. method:: SPI.write(buffer, \*, start=0, end=len(buffer) )
193193//|
194194//| Write the data contained in ``buf``. Requires the SPI being locked.
195195//|
196- STATIC mp_obj_t nativeio_spi_write (mp_obj_t self_in , mp_obj_t wr_buf ) {
197- mp_buffer_info_t src ;
198- mp_get_buffer_raise (wr_buf , & src , MP_BUFFER_READ );
199- nativeio_spi_obj_t * self = MP_OBJ_TO_PTR (self_in );
196+ //| :param bytearray buffer: buffer containing the bytes to write
197+ //| :param int start: Index to start writing from
198+ //| :param int end: Index to read up to but not include
199+ //|
200+ STATIC mp_obj_t nativeio_spi_write (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
201+ enum { ARG_buffer , ARG_start , ARG_end };
202+ static const mp_arg_t allowed_args [] = {
203+ { MP_QSTR_buffer , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
204+ { MP_QSTR_start , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
205+ { MP_QSTR_end , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = INT_MAX } },
206+ };
207+ nativeio_spi_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
200208 check_lock (self );
201- bool ok = common_hal_nativeio_spi_write (self , src .buf , src .len );
209+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
210+ mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
211+
212+ mp_buffer_info_t bufinfo ;
213+ mp_get_buffer_raise (args [ARG_buffer ].u_obj , & bufinfo , MP_BUFFER_READ );
214+ int32_t end = args [ARG_end ].u_int ;
215+ if (end < 0 ) {
216+ end += bufinfo .len ;
217+ }
218+ uint32_t start = args [ARG_start ].u_int ;
219+ uint32_t len = end - start ;
220+ if ((uint32_t ) end < start ) {
221+ len = 0 ;
222+ } else if (len > bufinfo .len ) {
223+ len = bufinfo .len ;
224+ }
225+
226+ bool ok = common_hal_nativeio_spi_write (self , ((uint8_t * )bufinfo .buf ) + start , len );
202227 if (!ok ) {
203228 nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , "SPI bus error" ));
204229 }
205230 return mp_const_none ;
206231}
207- MP_DEFINE_CONST_FUN_OBJ_2 (nativeio_spi_write_obj , nativeio_spi_write );
232+ MP_DEFINE_CONST_FUN_OBJ_KW (nativeio_spi_write_obj , 2 , nativeio_spi_write );
208233
209234
210- //| .. method:: SPI.readinto(buf )
235+ //| .. method:: SPI.readinto(buffer, \*, start=0, end=len(buffer), write_value=0 )
211236//|
212237//| Read into the buffer specified by ``buf`` while writing zeroes. Requires the SPI being locked.
213238//|
214- STATIC mp_obj_t nativeio_spi_readinto (size_t n_args , const mp_obj_t * args ) {
239+ //| :param bytearray buffer: buffer to write into
240+ //| :param int start: Index to start writing at
241+ //| :param int end: Index to write up to but not include
242+ //| :param int write_value: Value to write reading. (Usually ignored.)
243+ //|
244+ STATIC mp_obj_t nativeio_spi_readinto (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
245+ enum { ARG_buffer , ARG_start , ARG_end , ARG_write_value };
246+ static const mp_arg_t allowed_args [] = {
247+ { MP_QSTR_buffer , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
248+ { MP_QSTR_start , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
249+ { MP_QSTR_end , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = INT_MAX } },
250+ { MP_QSTR_write_value ,MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
251+ };
252+ nativeio_spi_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
253+ check_lock (self );
254+ mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
255+ mp_arg_parse_all (n_args - 1 , pos_args + 1 , kw_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
256+
215257 mp_buffer_info_t bufinfo ;
216- mp_get_buffer_raise (args [1 ], & bufinfo , MP_BUFFER_WRITE );
217- check_lock (args [0 ]);
218- bool ok = common_hal_nativeio_spi_read (args [0 ], bufinfo .buf , bufinfo .len );
258+ mp_get_buffer_raise (args [ARG_buffer ].u_obj , & bufinfo , MP_BUFFER_WRITE );
259+ int32_t end = args [ARG_end ].u_int ;
260+ if (end < 0 ) {
261+ end += bufinfo .len ;
262+ }
263+ uint32_t start = args [ARG_start ].u_int ;
264+ uint32_t len = end - start ;
265+ if ((uint32_t ) end < start ) {
266+ len = 0 ;
267+ } else if (len > bufinfo .len ) {
268+ len = bufinfo .len ;
269+ }
270+
271+ bool ok = common_hal_nativeio_spi_read (self , ((uint8_t * )bufinfo .buf ) + start , len , args [ARG_write_value ].u_int );
219272 if (!ok ) {
220273 nlr_raise (mp_obj_new_exception_msg (& mp_type_OSError , "SPI bus error" ));
221274 }
222275 return mp_const_none ;
223276}
224- MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (nativeio_spi_readinto_obj , 2 , 2 , nativeio_spi_readinto );
277+ MP_DEFINE_CONST_FUN_OBJ_KW (nativeio_spi_readinto_obj , 2 , nativeio_spi_readinto );
225278
226279STATIC const mp_rom_map_elem_t nativeio_spi_locals_dict_table [] = {
227280 { MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& nativeio_spi_deinit_obj ) },
0 commit comments