@@ -396,18 +396,23 @@ STATIC mp_obj_t rp2pio_statemachine_write(size_t n_args, const mp_obj_t *pos_arg
396396
397397 mp_buffer_info_t bufinfo ;
398398 mp_get_buffer_raise (args [ARG_buffer ].u_obj , & bufinfo , MP_BUFFER_READ );
399+ int stride_in_bytes = mp_binary_get_size ('@' , bufinfo .typecode , NULL );
400+ if (stride_in_bytes > 4 ) {
401+ mp_raise_ValueError (translate ("Buffer elements must be 4 bytes long or less" ));
402+ }
399403 int32_t start = args [ARG_start ].u_int ;
400- size_t length = bufinfo .len ;
404+ size_t length = bufinfo .len / stride_in_bytes ;
405+ // Normalize in element size units, not bytes.
401406 normalize_buffer_bounds (& start , args [ARG_end ].u_int , & length );
407+
408+ // Treat start and length in terms of bytes from now on.
409+ start *= stride_in_bytes ;
410+ length *= stride_in_bytes ;
411+
402412 if (length == 0 ) {
403413 return mp_const_none ;
404414 }
405415
406- int stride_in_bytes = mp_binary_get_size ('@' , bufinfo .typecode , NULL );
407- if (stride_in_bytes > 4 ) {
408- mp_raise_ValueError (translate ("Buffer elements must be 4 bytes long or less" ));
409- }
410-
411416 bool ok = common_hal_rp2pio_statemachine_write (self , ((uint8_t * )bufinfo .buf ) + start , length , stride_in_bytes , args [ARG_swap ].u_bool );
412417 if (mp_hal_is_interrupted ()) {
413418 return mp_const_none ;
@@ -603,19 +608,21 @@ STATIC mp_obj_t rp2pio_statemachine_readinto(size_t n_args, const mp_obj_t *pos_
603608
604609 mp_buffer_info_t bufinfo ;
605610 mp_get_buffer_raise (args [ARG_buffer ].u_obj , & bufinfo , MP_BUFFER_WRITE );
611+ int stride_in_bytes = mp_binary_get_size ('@' , bufinfo .typecode , NULL );
612+ if (stride_in_bytes > 4 ) {
613+ mp_raise_ValueError (translate ("Buffer elements must be 4 bytes long or less" ));
614+ }
606615 int32_t start = args [ARG_start ].u_int ;
607- size_t length = bufinfo .len ;
616+ size_t length = bufinfo .len / stride_in_bytes ;
608617 normalize_buffer_bounds (& start , args [ARG_end ].u_int , & length );
609618
619+ // Treat start and length in terms of bytes from now on.
620+ start *= stride_in_bytes ;
621+ length *= stride_in_bytes ;
610622 if (length == 0 ) {
611623 return mp_const_none ;
612624 }
613625
614- int stride_in_bytes = mp_binary_get_size ('@' , bufinfo .typecode , NULL );
615- if (stride_in_bytes > 4 ) {
616- mp_raise_ValueError (translate ("Buffer elements must be 4 bytes long or less" ));
617- }
618-
619626 bool ok = common_hal_rp2pio_statemachine_readinto (self , ((uint8_t * )bufinfo .buf ) + start , length , stride_in_bytes , args [ARG_swap ].u_bool );
620627 if (!ok ) {
621628 mp_raise_OSError (MP_EIO );
@@ -674,28 +681,32 @@ STATIC mp_obj_t rp2pio_statemachine_write_readinto(size_t n_args, const mp_obj_t
674681
675682 mp_buffer_info_t buf_out_info ;
676683 mp_get_buffer_raise (args [ARG_buffer_out ].u_obj , & buf_out_info , MP_BUFFER_READ );
684+ int out_stride_in_bytes = mp_binary_get_size ('@' , buf_out_info .typecode , NULL );
685+ if (out_stride_in_bytes > 4 ) {
686+ mp_raise_ValueError (translate ("Out-buffer elements must be <= 4 bytes long" ));
687+ }
677688 int32_t out_start = args [ARG_out_start ].u_int ;
678- size_t out_length = buf_out_info .len ;
689+ size_t out_length = buf_out_info .len / out_stride_in_bytes ;
679690 normalize_buffer_bounds (& out_start , args [ARG_out_end ].u_int , & out_length );
680691
681692 mp_buffer_info_t buf_in_info ;
682693 mp_get_buffer_raise (args [ARG_buffer_in ].u_obj , & buf_in_info , MP_BUFFER_WRITE );
683- int32_t in_start = args [ARG_in_start ].u_int ;
684- size_t in_length = buf_in_info .len ;
685- normalize_buffer_bounds (& in_start , args [ARG_in_end ].u_int , & in_length );
686-
687- if (out_length == 0 && in_length == 0 ) {
688- return mp_const_none ;
689- }
690-
691694 int in_stride_in_bytes = mp_binary_get_size ('@' , buf_in_info .typecode , NULL );
692695 if (in_stride_in_bytes > 4 ) {
693696 mp_raise_ValueError (translate ("In-buffer elements must be <= 4 bytes long" ));
694697 }
698+ int32_t in_start = args [ARG_in_start ].u_int ;
699+ size_t in_length = buf_in_info .len / in_stride_in_bytes ;
700+ normalize_buffer_bounds (& in_start , args [ARG_in_end ].u_int , & in_length );
695701
696- int out_stride_in_bytes = mp_binary_get_size ('@' , buf_out_info .typecode , NULL );
697- if (out_stride_in_bytes > 4 ) {
698- mp_raise_ValueError (translate ("Out-buffer elements must be <= 4 bytes long" ));
702+ // Treat start and length in terms of bytes from now on.
703+ out_start *= out_stride_in_bytes ;
704+ out_length *= out_stride_in_bytes ;
705+ in_start *= in_stride_in_bytes ;
706+ in_length *= in_stride_in_bytes ;
707+
708+ if (out_length == 0 && in_length == 0 ) {
709+ return mp_const_none ;
699710 }
700711
701712 bool ok = common_hal_rp2pio_statemachine_write_readinto (self ,
0 commit comments