@@ -378,7 +378,7 @@ STATIC mp_obj_t rp2pio_statemachine_stop(mp_obj_t self_obj) {
378
378
}
379
379
MP_DEFINE_CONST_FUN_OBJ_1 (rp2pio_statemachine_stop_obj , rp2pio_statemachine_stop );
380
380
381
- //| def write(self, buffer: ReadableBuffer, *, start: int = 0, end: Optional[int] = None) -> None:
381
+ //| def write(self, buffer: ReadableBuffer, *, start: int = 0, end: Optional[int] = None, swap: bool = False ) -> None:
382
382
//| """Write the data contained in ``buffer`` to the state machine. If the buffer is empty, nothing happens.
383
383
//|
384
384
//| Writes to the FIFO will match the input buffer's element size. For example, bytearray elements
@@ -390,15 +390,17 @@ MP_DEFINE_CONST_FUN_OBJ_1(rp2pio_statemachine_stop_obj, rp2pio_statemachine_stop
390
390
//|
391
391
//| :param ~circuitpython_typing.ReadableBuffer buffer: Write out the data in this buffer
392
392
//| :param int start: Start of the slice of ``buffer`` to write out: ``buffer[start:end]``
393
- //| :param int end: End of the slice; this index is not included. Defaults to ``len(buffer)``"""
393
+ //| :param int end: End of the slice; this index is not included. Defaults to ``len(buffer)``
394
+ //| :param bool swap: For 2- and 4-byte elements, swap (reverse) the byte order"""
394
395
//| ...
395
396
//|
396
397
STATIC mp_obj_t rp2pio_statemachine_write (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
397
- enum { ARG_buffer , ARG_start , ARG_end };
398
+ enum { ARG_buffer , ARG_start , ARG_end , ARG_swap };
398
399
static const mp_arg_t allowed_args [] = {
399
400
{ MP_QSTR_buffer , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
400
401
{ MP_QSTR_start , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
401
402
{ MP_QSTR_end , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = INT_MAX } },
403
+ { MP_QSTR_swap , MP_ARG_KW_ONLY | MP_ARG_BOOL , {.u_bool = false} },
402
404
};
403
405
rp2pio_statemachine_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
404
406
check_for_deinit (self );
@@ -420,7 +422,7 @@ STATIC mp_obj_t rp2pio_statemachine_write(size_t n_args, const mp_obj_t *pos_arg
420
422
mp_raise_ValueError (translate ("Buffer elements must be 4 bytes long or less" ));
421
423
}
422
424
423
- bool ok = common_hal_rp2pio_statemachine_write (self , ((uint8_t * )bufinfo .buf ) + start , length , stride_in_bytes );
425
+ bool ok = common_hal_rp2pio_statemachine_write (self , ((uint8_t * )bufinfo .buf ) + start , length , stride_in_bytes , args [ ARG_swap ]. u_bool );
424
426
if (mp_hal_is_interrupted ()) {
425
427
return mp_const_none ;
426
428
}
@@ -431,7 +433,7 @@ STATIC mp_obj_t rp2pio_statemachine_write(size_t n_args, const mp_obj_t *pos_arg
431
433
}
432
434
MP_DEFINE_CONST_FUN_OBJ_KW (rp2pio_statemachine_write_obj , 2 , rp2pio_statemachine_write );
433
435
434
- //| def background_write(self, once: Optional[ReadableBuffer]=None, *, loop: Optional[ReadableBuffer]=None) -> None:
436
+ //| def background_write(self, once: Optional[ReadableBuffer]=None, *, loop: Optional[ReadableBuffer]=None, swap: bool=False ) -> None:
435
437
//| """Write data to the TX fifo in the background, with optional looping.
436
438
//|
437
439
//| First, if any previous ``once`` or ``loop`` buffer has not been started, this function blocks until they have.
@@ -459,8 +461,13 @@ MP_DEFINE_CONST_FUN_OBJ_KW(rp2pio_statemachine_write_obj, 2, rp2pio_statemachine
459
461
//| where a change in duty cycle requires a special transitional buffer to be used exactly once. Most
460
462
//| use cases will probably only use one of ``once`` or ``loop``.
461
463
//|
464
+ //| Having neither ``once`` nor ``loop`` terminates an existing
465
+ //| background looping write after exactly a whole loop. This is in contrast to
466
+ //| `stop_background_write`, which interrupts an ongoing DMA operation.
467
+ //|
462
468
//| :param ~Optional[circuitpython_typing.ReadableBuffer] once: Data to be written once
463
469
//| :param ~Optional[circuitpython_typing.ReadableBuffer] loop: Data to be written repeatedly
470
+ //| :param bool swap: For 2- and 4-byte elements, swap (reverse) the byte order
464
471
//| """
465
472
//| ...
466
473
//|
@@ -483,10 +490,11 @@ STATIC void fill_buf_info(sm_buf_info *info, mp_obj_t obj, size_t *stride_in_byt
483
490
}
484
491
485
492
STATIC mp_obj_t rp2pio_statemachine_background_write (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
486
- enum { ARG_once , ARG_loop };
493
+ enum { ARG_once , ARG_loop , ARG_swap };
487
494
static const mp_arg_t allowed_args [] = {
488
495
{ MP_QSTR_once , MP_ARG_OBJ , {.u_obj = mp_const_none } },
489
496
{ MP_QSTR_loop , MP_ARG_OBJ | MP_ARG_KW_ONLY , {.u_obj = mp_const_none } },
497
+ { MP_QSTR_swap , MP_ARG_KW_ONLY | MP_ARG_BOOL , {.u_bool = false} },
490
498
};
491
499
rp2pio_statemachine_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
492
500
check_for_deinit (self );
@@ -502,7 +510,7 @@ STATIC mp_obj_t rp2pio_statemachine_background_write(size_t n_args, const mp_obj
502
510
return mp_const_none ;
503
511
}
504
512
505
- bool ok = common_hal_rp2pio_statemachine_background_write (self , & once_info , & loop_info , stride_in_bytes );
513
+ bool ok = common_hal_rp2pio_statemachine_background_write (self , & once_info , & loop_info , stride_in_bytes , args [ ARG_swap ]. u_bool );
506
514
507
515
if (mp_hal_is_interrupted ()) {
508
516
return mp_const_none ;
@@ -515,7 +523,9 @@ STATIC mp_obj_t rp2pio_statemachine_background_write(size_t n_args, const mp_obj
515
523
MP_DEFINE_CONST_FUN_OBJ_KW (rp2pio_statemachine_background_write_obj , 1 , rp2pio_statemachine_background_write );
516
524
517
525
//| def stop_background_write(self) -> None:
518
- //| """Immediately stop a background write, if one is in progress. Items already in the TX FIFO are not affected."""
526
+ //| """Immediately stop a background write, if one is in progress. Any
527
+ //| DMA in progress is halted, but items already in the TX FIFO are not
528
+ //| affected."""
519
529
//|
520
530
STATIC mp_obj_t rp2pio_statemachine_obj_stop_background_write (mp_obj_t self_in ) {
521
531
rp2pio_statemachine_obj_t * self = MP_OBJ_TO_PTR (self_in );
@@ -567,7 +577,7 @@ const mp_obj_property_t rp2pio_statemachine_pending_obj = {
567
577
MP_ROM_NONE },
568
578
};
569
579
570
- //| def readinto(self, buffer: WriteableBuffer, *, start: int = 0, end: Optional[int] = None) -> None:
580
+ //| def readinto(self, buffer: WriteableBuffer, *, start: int = 0, end: Optional[int] = None, swap: bool=False ) -> None:
571
581
//| """Read into ``buffer``. If the number of bytes to read is 0, nothing happens. The buffer
572
582
//| includes any data added to the fifo even if it was added before this was called.
573
583
//|
@@ -581,16 +591,18 @@ const mp_obj_property_t rp2pio_statemachine_pending_obj = {
581
591
//|
582
592
//| :param ~circuitpython_typing.WriteableBuffer buffer: Read data into this buffer
583
593
//| :param int start: Start of the slice of ``buffer`` to read into: ``buffer[start:end]``
584
- //| :param int end: End of the slice; this index is not included. Defaults to ``len(buffer)``"""
594
+ //| :param int end: End of the slice; this index is not included. Defaults to ``len(buffer)``
595
+ //| :param bool swap: For 2- and 4-byte elements, swap (reverse) the byte order"""
585
596
//| ...
586
597
//|
587
598
588
599
STATIC mp_obj_t rp2pio_statemachine_readinto (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
589
- enum { ARG_buffer , ARG_start , ARG_end };
600
+ enum { ARG_buffer , ARG_start , ARG_end , ARG_swap };
590
601
static const mp_arg_t allowed_args [] = {
591
602
{ MP_QSTR_buffer , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
592
603
{ MP_QSTR_start , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
593
604
{ MP_QSTR_end , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = INT_MAX } },
605
+ { MP_QSTR_swap , MP_ARG_KW_ONLY | MP_ARG_BOOL , {.u_bool = false} },
594
606
};
595
607
rp2pio_statemachine_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
596
608
check_for_deinit (self );
@@ -613,7 +625,7 @@ STATIC mp_obj_t rp2pio_statemachine_readinto(size_t n_args, const mp_obj_t *pos_
613
625
mp_raise_ValueError (translate ("Buffer elements must be 4 bytes long or less" ));
614
626
}
615
627
616
- bool ok = common_hal_rp2pio_statemachine_readinto (self , ((uint8_t * )bufinfo .buf ) + start , length , stride_in_bytes );
628
+ bool ok = common_hal_rp2pio_statemachine_readinto (self , ((uint8_t * )bufinfo .buf ) + start , length , stride_in_bytes , args [ ARG_swap ]. u_bool );
617
629
if (!ok ) {
618
630
mp_raise_OSError (MP_EIO );
619
631
}
@@ -638,19 +650,23 @@ MP_DEFINE_CONST_FUN_OBJ_KW(rp2pio_statemachine_readinto_obj, 2, rp2pio_statemach
638
650
//| :param int out_start: Start of the slice of buffer_out to write out: ``buffer_out[out_start:out_end]``
639
651
//| :param int out_end: End of the slice; this index is not included. Defaults to ``len(buffer_out)``
640
652
//| :param int in_start: Start of the slice of ``buffer_in`` to read into: ``buffer_in[in_start:in_end]``
641
- //| :param int in_end: End of the slice; this index is not included. Defaults to ``len(buffer_in)``"""
653
+ //| :param int in_end: End of the slice; this index is not included. Defaults to ``len(buffer_in)``
654
+ //| :param bool swap_out: For 2- and 4-byte elements, swap (reverse) the byte order for the buffer being transmitted (written)
655
+ //| :param bool swap_in: For 2- and 4-rx elements, swap (reverse) the byte order for the buffer being received (read)"""
642
656
//| ...
643
657
//|
644
658
645
659
STATIC mp_obj_t rp2pio_statemachine_write_readinto (size_t n_args , const mp_obj_t * pos_args , mp_map_t * kw_args ) {
646
- enum { ARG_buffer_out , ARG_buffer_in , ARG_out_start , ARG_out_end , ARG_in_start , ARG_in_end };
660
+ enum { ARG_buffer_out , ARG_buffer_in , ARG_out_start , ARG_out_end , ARG_in_start , ARG_in_end , ARG_swap_out , ARG_swap_in };
647
661
static const mp_arg_t allowed_args [] = {
648
662
{ MP_QSTR_buffer_out , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
649
663
{ MP_QSTR_buffer_in , MP_ARG_REQUIRED | MP_ARG_OBJ , {.u_obj = MP_OBJ_NULL } },
650
664
{ MP_QSTR_out_start , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
651
665
{ MP_QSTR_out_end , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = INT_MAX } },
652
666
{ MP_QSTR_in_start , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
653
667
{ MP_QSTR_in_end , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = INT_MAX } },
668
+ { MP_QSTR_swap_out , MP_ARG_KW_ONLY | MP_ARG_BOOL , {.u_bool = false} },
669
+ { MP_QSTR_swap_in , MP_ARG_KW_ONLY | MP_ARG_BOOL , {.u_bool = false} },
654
670
};
655
671
rp2pio_statemachine_obj_t * self = MP_OBJ_TO_PTR (pos_args [0 ]);
656
672
check_for_deinit (self );
@@ -689,7 +705,7 @@ STATIC mp_obj_t rp2pio_statemachine_write_readinto(size_t n_args, const mp_obj_t
689
705
out_stride_in_bytes ,
690
706
((uint8_t * )buf_in_info .buf ) + in_start ,
691
707
in_length ,
692
- in_stride_in_bytes );
708
+ in_stride_in_bytes , args [ ARG_swap_out ]. u_bool , args [ ARG_swap_in ]. u_bool );
693
709
if (!ok ) {
694
710
mp_raise_OSError (MP_EIO );
695
711
}
@@ -708,6 +724,18 @@ STATIC mp_obj_t rp2pio_statemachine_obj_clear_rxfifo(mp_obj_t self_in) {
708
724
}
709
725
MP_DEFINE_CONST_FUN_OBJ_1 (rp2pio_statemachine_clear_rxfifo_obj , rp2pio_statemachine_obj_clear_rxfifo );
710
726
727
+ //| def clear_txstall(self) -> None:
728
+ //| """Clears the txstall flag."""
729
+ //| ...
730
+ //|
731
+ STATIC mp_obj_t rp2pio_statemachine_obj_clear_txstall (mp_obj_t self_in ) {
732
+ rp2pio_statemachine_obj_t * self = MP_OBJ_TO_PTR (self_in );
733
+ common_hal_rp2pio_statemachine_clear_txstall (self );
734
+ return mp_const_none ;
735
+ }
736
+ MP_DEFINE_CONST_FUN_OBJ_1 (rp2pio_statemachine_clear_txstall_obj , rp2pio_statemachine_obj_clear_txstall );
737
+
738
+
711
739
//| frequency: int
712
740
//| """The actual state machine frequency. This may not match the frequency requested
713
741
//| due to internal limitations."""
@@ -733,6 +761,26 @@ MP_PROPERTY_GETSET(rp2pio_statemachine_frequency_obj,
733
761
(mp_obj_t )& rp2pio_statemachine_get_frequency_obj ,
734
762
(mp_obj_t )& rp2pio_statemachine_set_frequency_obj );
735
763
764
+ //| txstall: bool
765
+ //| """True when the state machine has stalled due to a full TX FIFO since the last
766
+ //| `clear_txstall` call."""
767
+ //|
768
+
769
+ STATIC mp_obj_t rp2pio_statemachine_obj_get_txstall (mp_obj_t self_in ) {
770
+ rp2pio_statemachine_obj_t * self = MP_OBJ_TO_PTR (self_in );
771
+ check_for_deinit (self );
772
+ return MP_OBJ_NEW_SMALL_INT (common_hal_rp2pio_statemachine_get_txstall (self ));
773
+ }
774
+ MP_DEFINE_CONST_FUN_OBJ_1 (rp2pio_statemachine_get_txstall_obj , rp2pio_statemachine_obj_get_txstall );
775
+
776
+ const mp_obj_property_t rp2pio_statemachine_txstall_obj = {
777
+ .base .type = & mp_type_property ,
778
+ .proxy = {(mp_obj_t )& rp2pio_statemachine_get_txstall_obj ,
779
+ MP_ROM_NONE ,
780
+ MP_ROM_NONE },
781
+ };
782
+
783
+
736
784
//| rxstall: bool
737
785
//| """True when the state machine has stalled due to a full RX FIFO since the last
738
786
//| `clear_rxfifo` call."""
@@ -771,6 +819,7 @@ STATIC const mp_rom_map_elem_t rp2pio_statemachine_locals_dict_table[] = {
771
819
{ MP_ROM_QSTR (MP_QSTR_restart ), MP_ROM_PTR (& rp2pio_statemachine_restart_obj ) },
772
820
{ MP_ROM_QSTR (MP_QSTR_run ), MP_ROM_PTR (& rp2pio_statemachine_run_obj ) },
773
821
{ MP_ROM_QSTR (MP_QSTR_clear_rxfifo ), MP_ROM_PTR (& rp2pio_statemachine_clear_rxfifo_obj ) },
822
+ { MP_ROM_QSTR (MP_QSTR_clear_txstall ), MP_ROM_PTR (& rp2pio_statemachine_clear_txstall_obj ) },
774
823
775
824
{ MP_ROM_QSTR (MP_QSTR_readinto ), MP_ROM_PTR (& rp2pio_statemachine_readinto_obj ) },
776
825
{ MP_ROM_QSTR (MP_QSTR_write ), MP_ROM_PTR (& rp2pio_statemachine_write_obj ) },
@@ -782,6 +831,7 @@ STATIC const mp_rom_map_elem_t rp2pio_statemachine_locals_dict_table[] = {
782
831
783
832
{ MP_ROM_QSTR (MP_QSTR_frequency ), MP_ROM_PTR (& rp2pio_statemachine_frequency_obj ) },
784
833
{ MP_ROM_QSTR (MP_QSTR_rxstall ), MP_ROM_PTR (& rp2pio_statemachine_rxstall_obj ) },
834
+ { MP_ROM_QSTR (MP_QSTR_txstall ), MP_ROM_PTR (& rp2pio_statemachine_txstall_obj ) },
785
835
{ MP_ROM_QSTR (MP_QSTR_in_waiting ), MP_ROM_PTR (& rp2pio_statemachine_in_waiting_obj ) },
786
836
};
787
837
STATIC MP_DEFINE_CONST_DICT (rp2pio_statemachine_locals_dict , rp2pio_statemachine_locals_dict_table );
0 commit comments