@@ -1047,42 +1047,51 @@ uint8_t rp2pio_statemachine_program_offset(rp2pio_statemachine_obj_t *self) {
10471047 return _current_program_offset [pio_index ][sm ];
10481048}
10491049
1050- bool common_hal_rp2pio_statemachine_background_write (rp2pio_statemachine_obj_t * self ,
1051- const sm_buf_info * once_write_buf , const sm_buf_info * loop_write_buf , const sm_buf_info * loop2_write_buf ,
1052- uint8_t stride_in_bytes , bool swap ) {
1050+ bool common_hal_rp2pio_statemachine_background_write (rp2pio_statemachine_obj_t * self , uint8_t stride_in_bytes , bool swap ) {
10531051
10541052 uint8_t pio_index = pio_get_index (self -> pio );
10551053 uint8_t sm = self -> state_machine ;
10561054
1057- int pending_buffers_write = (once_write_buf -> info .len != 0 ) + (loop_write_buf -> info .len != 0 ) + (loop2_write_buf -> info .len != 0 );
1055+ self -> switched_write_buffers = false;
1056+
1057+ int pending_buffers_write = (self -> once_write_buf_info .info .len != 0 ) + (self -> loop_write_buf_info .info .len != 0 ) + (self -> loop2_write_buf_info .info .len != 0 );
10581058
10591059 // If all buffer arguments have nonzero length, write once_write_buf, loop_write_buf, loop2_write_buf and repeat last two forever
10601060
1061- if (!once_write_buf -> info .len ) {
1062- if (!loop_write_buf -> info .len ) {
1061+ if (!( self -> once_write_buf_info . info .len ) ) {
1062+ if (!self -> loop_write_buf_info . info .len ) {
10631063 // If once_write_buf and loop_write_buf have zero length, write loop2_write_buf forever
1064- once_write_buf = loop2_write_buf ;
1065- loop_write_buf = loop2_write_buf ;
1064+ self -> once_write_buf_info = self -> loop2_write_buf_info ;
1065+ self -> once_write_buf_obj = self -> loop2_write_buf_obj ;
1066+ self -> loop_write_buf_info = self -> loop2_write_buf_info ;
1067+ self -> loop_write_buf_obj = self -> loop2_write_buf_obj ;
10661068 } else {
1067- if (!loop2_write_buf -> info .len ) {
1069+ if (!( self -> loop2_write_buf_info . info .len ) ) {
10681070 // If once_write_buf and loop2_write_buf have zero length, write loop_write_buf forever
1069- once_write_buf = loop_write_buf ;
1070- loop2_write_buf = loop_write_buf ;
1071+ self -> once_write_buf_info = self -> loop_write_buf_info ;
1072+ self -> once_write_buf_obj = self -> loop_write_buf_obj ;
1073+ self -> loop2_write_buf_info = self -> loop_write_buf_info ;
1074+ self -> loop2_write_buf_obj = self -> loop_write_buf_obj ;
10711075 } else {
10721076 // If only once_write_buf has zero length, write loop_write_buf, loop2_write_buf, and repeat last two forever
1073- once_write_buf = loop_write_buf ;
1074- loop_write_buf = loop2_write_buf ;
1075- loop2_write_buf = once_write_buf ;
1077+ self -> once_write_buf_info = self -> loop_write_buf_info ;
1078+ self -> once_write_buf_obj = self -> loop_write_buf_obj ;
1079+ self -> loop_write_buf_info = self -> loop2_write_buf_info ;
1080+ self -> loop_write_buf_obj = self -> loop2_write_buf_obj ;
1081+ self -> loop2_write_buf_info = self -> once_write_buf_info ;
1082+ self -> loop2_write_buf_obj = self -> once_write_buf_obj ;
10761083 }
10771084 }
10781085 } else {
1079- if (!loop_write_buf -> info .len ) {
1086+ if (!( self -> loop_write_buf_info . info .len ) ) {
10801087 // If once_write_buf has nonzero length and loop_write_buf has zero length, write once_write_buf, loop2_write_buf and repeat last buf forever
1081- loop_write_buf = loop2_write_buf ;
1088+ self -> loop_write_buf_info = self -> loop2_write_buf_info ;
1089+ self -> loop_write_buf_obj = self -> loop2_write_buf_obj ;
10821090 } else {
1083- if (!loop2_write_buf -> info .len ) {
1091+ if (!self -> loop2_write_buf_info . info .len ) {
10841092 // If once_write_buf has nonzero length and loop2_write_buf have zero length, write once_write_buf, loop_write_buf and repeat last buf forever
1085- loop2_write_buf = loop_write_buf ;
1093+ self -> loop2_write_buf_info = self -> loop_write_buf_info ;
1094+ self -> loop2_write_buf_obj = self -> loop_write_buf_obj ;
10861095 }
10871096 }
10881097 }
@@ -1105,9 +1114,12 @@ bool common_hal_rp2pio_statemachine_background_write(rp2pio_statemachine_obj_t *
11051114 }
11061115
11071116 common_hal_mcu_disable_interrupts ();
1108- self -> next_write_buf_1 = * once_write_buf ;
1109- self -> next_write_buf_2 = * loop_write_buf ;
1110- self -> next_write_buf_3 = * loop2_write_buf ;
1117+ self -> next_write_buf_1 = self -> once_write_buf_info ;
1118+ self -> next_write_buf_1_obj = self -> once_write_buf_obj ;
1119+ self -> next_write_buf_2 = self -> loop_write_buf_info ;
1120+ self -> next_write_buf_2_obj = self -> loop_write_buf_obj ;
1121+ self -> next_write_buf_3 = self -> loop2_write_buf_info ;
1122+ self -> next_write_buf_3_obj = self -> loop2_write_buf_obj ;
11111123 self -> pending_buffers_write = pending_buffers_write ;
11121124
11131125 if (self -> dma_completed_write && self -> next_write_buf_1 .info .len ) {
@@ -1133,13 +1145,18 @@ bool common_hal_rp2pio_statemachine_background_write(rp2pio_statemachine_obj_t *
11331145
11341146 dma_channel_config c_write ;
11351147
1136- self -> current_write_buf = * once_write_buf ;
1137- self -> next_write_buf_1 = * loop_write_buf ;
1138- self -> next_write_buf_2 = * loop2_write_buf ;
1139- self -> next_write_buf_3 = * loop_write_buf ;
1148+ self -> current_write_buf = self -> once_write_buf_info ;
1149+ self -> current_write_buf_obj = self -> once_write_buf_obj ;
1150+ self -> next_write_buf_1 = self -> loop_write_buf_info ;
1151+ self -> next_write_buf_1_obj = self -> loop_write_buf_obj ;
1152+ self -> next_write_buf_2 = self -> loop2_write_buf_info ;
1153+ self -> next_write_buf_2_obj = self -> loop2_write_buf_obj ;
1154+ self -> next_write_buf_3 = self -> loop_write_buf_info ;
1155+ self -> next_write_buf_3_obj = self -> loop_write_buf_obj ;
11401156
11411157 self -> pending_buffers_write = pending_buffers_write ;
11421158 self -> dma_completed_write = false;
1159+
11431160 self -> background_stride_in_bytes = stride_in_bytes ;
11441161 self -> byteswap = swap ;
11451162
@@ -1151,8 +1168,8 @@ bool common_hal_rp2pio_statemachine_background_write(rp2pio_statemachine_obj_t *
11511168 channel_config_set_bswap (& c_write , swap );
11521169 dma_channel_configure (channel_write , & c_write ,
11531170 tx_destination ,
1154- once_write_buf -> info .buf ,
1155- once_write_buf -> info .len / stride_in_bytes ,
1171+ self -> once_write_buf_info . info .buf ,
1172+ self -> once_write_buf_info . info .len / stride_in_bytes ,
11561173 false);
11571174
11581175 common_hal_mcu_disable_interrupts ();
@@ -1169,9 +1186,13 @@ bool common_hal_rp2pio_statemachine_background_write(rp2pio_statemachine_obj_t *
11691186
11701187void rp2pio_statemachine_dma_complete_write (rp2pio_statemachine_obj_t * self , int channel_write ) {
11711188 self -> current_write_buf = self -> next_write_buf_1 ;
1189+ self -> current_write_buf_obj = self -> next_write_buf_1_obj ;
11721190 self -> next_write_buf_1 = self -> next_write_buf_2 ;
1191+ self -> next_write_buf_1_obj = self -> next_write_buf_2_obj ;
11731192 self -> next_write_buf_2 = self -> next_write_buf_3 ;
1193+ self -> next_write_buf_2_obj = self -> next_write_buf_3_obj ;
11741194 self -> next_write_buf_3 = self -> next_write_buf_1 ;
1195+ self -> next_write_buf_3_obj = self -> next_write_buf_1_obj ;
11751196
11761197 if (self -> current_write_buf .info .buf ) {
11771198 if (self -> pending_buffers_write > 0 ) {
@@ -1183,6 +1204,8 @@ void rp2pio_statemachine_dma_complete_write(rp2pio_statemachine_obj_t *self, int
11831204 self -> dma_completed_write = true;
11841205 self -> pending_buffers_write = 0 ; // should be a no-op
11851206 }
1207+
1208+ self -> switched_write_buffers = true;
11861209}
11871210
11881211bool common_hal_rp2pio_statemachine_stop_background_write (rp2pio_statemachine_obj_t * self ) {
@@ -1212,7 +1235,7 @@ bool common_hal_rp2pio_statemachine_background_read(rp2pio_statemachine_obj_t *s
12121235 uint8_t pio_index = pio_get_index (self -> pio );
12131236 uint8_t sm = self -> state_machine ;
12141237
1215- self -> switched_buffers = false;
1238+ self -> switched_read_buffers = false;
12161239
12171240 int pending_buffers_read = (self -> once_read_buf_info .info .len != 0 ) + (self -> loop_read_buf_info .info .len != 0 ) + (self -> loop2_read_buf_info .info .len != 0 );
12181241
@@ -1316,6 +1339,9 @@ bool common_hal_rp2pio_statemachine_background_read(rp2pio_statemachine_obj_t *s
13161339 self -> pending_buffers_read = pending_buffers_read ;
13171340 self -> dma_completed_read = false;
13181341
1342+ self -> background_stride_in_bytes = stride_in_bytes ;
1343+ self -> byteswap = swap ;
1344+
13191345 c_read = dma_channel_get_default_config (channel_read );
13201346 channel_config_set_transfer_data_size (& c_read , _stride_to_dma_size (stride_in_bytes ));
13211347 channel_config_set_dreq (& c_read , self -> rx_dreq );
@@ -1362,7 +1388,7 @@ void rp2pio_statemachine_dma_complete_read(rp2pio_statemachine_obj_t *self, int
13621388 self -> pending_buffers_read = 0 ; // should be a no-op
13631389 }
13641390
1365- self -> switched_buffers = true;
1391+ self -> switched_read_buffers = true;
13661392}
13671393
13681394bool common_hal_rp2pio_statemachine_stop_background_read (rp2pio_statemachine_obj_t * self ) {
@@ -1409,15 +1435,19 @@ mp_obj_t common_hal_rp2pio_statemachine_get_rxfifo(rp2pio_statemachine_obj_t *se
14091435}
14101436
14111437mp_obj_t common_hal_rp2pio_statemachine_get_last_read (rp2pio_statemachine_obj_t * self ) {
1412- if (self -> switched_buffers ) {
1413- self -> switched_buffers = false;
1438+ if (self -> switched_read_buffers ) {
1439+ self -> switched_read_buffers = false;
14141440 return self -> next_read_buf_1_obj ;
14151441 }
14161442 return mp_const_empty_bytes ;
14171443}
14181444
14191445mp_obj_t common_hal_rp2pio_statemachine_get_last_write (rp2pio_statemachine_obj_t * self ) {
1420- return mp_const_none ;
1446+ if (self -> switched_write_buffers ) {
1447+ self -> switched_write_buffers = false;
1448+ return self -> next_write_buf_1_obj ;
1449+ }
1450+ return mp_const_empty_bytes ;
14211451}
14221452
14231453
0 commit comments