@@ -396,18 +396,23 @@ STATIC mp_obj_t rp2pio_statemachine_write(size_t n_args, const mp_obj_t *pos_arg
396
396
397
397
mp_buffer_info_t bufinfo ;
398
398
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
+ }
399
403
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.
401
406
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
+
402
412
if (length == 0 ) {
403
413
return mp_const_none ;
404
414
}
405
415
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
-
411
416
bool ok = common_hal_rp2pio_statemachine_write (self , ((uint8_t * )bufinfo .buf ) + start , length , stride_in_bytes , args [ARG_swap ].u_bool );
412
417
if (mp_hal_is_interrupted ()) {
413
418
return mp_const_none ;
@@ -603,19 +608,21 @@ STATIC mp_obj_t rp2pio_statemachine_readinto(size_t n_args, const mp_obj_t *pos_
603
608
604
609
mp_buffer_info_t bufinfo ;
605
610
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
+ }
606
615
int32_t start = args [ARG_start ].u_int ;
607
- size_t length = bufinfo .len ;
616
+ size_t length = bufinfo .len / stride_in_bytes ;
608
617
normalize_buffer_bounds (& start , args [ARG_end ].u_int , & length );
609
618
619
+ // Treat start and length in terms of bytes from now on.
620
+ start *= stride_in_bytes ;
621
+ length *= stride_in_bytes ;
610
622
if (length == 0 ) {
611
623
return mp_const_none ;
612
624
}
613
625
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
-
619
626
bool ok = common_hal_rp2pio_statemachine_readinto (self , ((uint8_t * )bufinfo .buf ) + start , length , stride_in_bytes , args [ARG_swap ].u_bool );
620
627
if (!ok ) {
621
628
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
674
681
675
682
mp_buffer_info_t buf_out_info ;
676
683
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
+ }
677
688
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 ;
679
690
normalize_buffer_bounds (& out_start , args [ARG_out_end ].u_int , & out_length );
680
691
681
692
mp_buffer_info_t buf_in_info ;
682
693
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
-
691
694
int in_stride_in_bytes = mp_binary_get_size ('@' , buf_in_info .typecode , NULL );
692
695
if (in_stride_in_bytes > 4 ) {
693
696
mp_raise_ValueError (translate ("In-buffer elements must be <= 4 bytes long" ));
694
697
}
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 );
695
701
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 ;
699
710
}
700
711
701
712
bool ok = common_hal_rp2pio_statemachine_write_readinto (self ,
0 commit comments