@@ -1060,42 +1060,51 @@ uint8_t rp2pio_statemachine_program_offset(rp2pio_statemachine_obj_t *self) {
10601060 return _current_program_offset [pio_index ][sm ];
10611061}
10621062
1063- bool common_hal_rp2pio_statemachine_background_write (rp2pio_statemachine_obj_t * self ,
1064- const sm_buf_info * once_write_buf , const sm_buf_info * loop_write_buf , const sm_buf_info * loop2_write_buf ,
1065- uint8_t stride_in_bytes , bool swap ) {
1063+ bool common_hal_rp2pio_statemachine_background_write (rp2pio_statemachine_obj_t * self , uint8_t stride_in_bytes , bool swap ) {
10661064
10671065 uint8_t pio_index = pio_get_index (self -> pio );
10681066 uint8_t sm = self -> state_machine ;
10691067
1070- int pending_buffers_write = (once_write_buf -> info .len != 0 ) + (loop_write_buf -> info .len != 0 ) + (loop2_write_buf -> info .len != 0 );
1068+ self -> switched_write_buffers = false;
1069+
1070+ 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 );
10711071
10721072 // If all buffer arguments have nonzero length, write once_write_buf, loop_write_buf, loop2_write_buf and repeat last two forever
10731073
1074- if (!once_write_buf -> info .len ) {
1075- if (!loop_write_buf -> info .len ) {
1074+ if (!( self -> once_write_buf_info . info .len ) ) {
1075+ if (!self -> loop_write_buf_info . info .len ) {
10761076 // If once_write_buf and loop_write_buf have zero length, write loop2_write_buf forever
1077- once_write_buf = loop2_write_buf ;
1078- loop_write_buf = loop2_write_buf ;
1077+ self -> once_write_buf_info = self -> loop2_write_buf_info ;
1078+ self -> once_write_buf_obj = self -> loop2_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 ;
10791081 } else {
1080- if (!loop2_write_buf -> info .len ) {
1082+ if (!( self -> loop2_write_buf_info . info .len ) ) {
10811083 // If once_write_buf and loop2_write_buf have zero length, write loop_write_buf forever
1082- once_write_buf = loop_write_buf ;
1083- loop2_write_buf = loop_write_buf ;
1084+ self -> once_write_buf_info = self -> loop_write_buf_info ;
1085+ self -> once_write_buf_obj = self -> loop_write_buf_obj ;
1086+ self -> loop2_write_buf_info = self -> loop_write_buf_info ;
1087+ self -> loop2_write_buf_obj = self -> loop_write_buf_obj ;
10841088 } else {
10851089 // If only once_write_buf has zero length, write loop_write_buf, loop2_write_buf, and repeat last two forever
1086- once_write_buf = loop_write_buf ;
1087- loop_write_buf = loop2_write_buf ;
1088- loop2_write_buf = once_write_buf ;
1090+ self -> once_write_buf_info = self -> loop_write_buf_info ;
1091+ self -> once_write_buf_obj = self -> loop_write_buf_obj ;
1092+ self -> loop_write_buf_info = self -> loop2_write_buf_info ;
1093+ self -> loop_write_buf_obj = self -> loop2_write_buf_obj ;
1094+ self -> loop2_write_buf_info = self -> once_write_buf_info ;
1095+ self -> loop2_write_buf_obj = self -> once_write_buf_obj ;
10891096 }
10901097 }
10911098 } else {
1092- if (!loop_write_buf -> info .len ) {
1099+ if (!( self -> loop_write_buf_info . info .len ) ) {
10931100 // 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
1094- loop_write_buf = loop2_write_buf ;
1101+ self -> loop_write_buf_info = self -> loop2_write_buf_info ;
1102+ self -> loop_write_buf_obj = self -> loop2_write_buf_obj ;
10951103 } else {
1096- if (!loop2_write_buf -> info .len ) {
1104+ if (!self -> loop2_write_buf_info . info .len ) {
10971105 // 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
1098- loop2_write_buf = loop_write_buf ;
1106+ self -> loop2_write_buf_info = self -> loop_write_buf_info ;
1107+ self -> loop2_write_buf_obj = self -> loop_write_buf_obj ;
10991108 }
11001109 }
11011110 }
@@ -1118,9 +1127,12 @@ bool common_hal_rp2pio_statemachine_background_write(rp2pio_statemachine_obj_t *
11181127 }
11191128
11201129 common_hal_mcu_disable_interrupts ();
1121- self -> next_write_buf_1 = * once_write_buf ;
1122- self -> next_write_buf_2 = * loop_write_buf ;
1123- self -> next_write_buf_3 = * loop2_write_buf ;
1130+ self -> next_write_buf_1 = self -> once_write_buf_info ;
1131+ self -> next_write_buf_1_obj = self -> once_write_buf_obj ;
1132+ self -> next_write_buf_2 = self -> loop_write_buf_info ;
1133+ self -> next_write_buf_2_obj = self -> loop_write_buf_obj ;
1134+ self -> next_write_buf_3 = self -> loop2_write_buf_info ;
1135+ self -> next_write_buf_3_obj = self -> loop2_write_buf_obj ;
11241136 self -> pending_buffers_write = pending_buffers_write ;
11251137
11261138 if (self -> dma_completed_write && self -> next_write_buf_1 .info .len ) {
@@ -1146,13 +1158,18 @@ bool common_hal_rp2pio_statemachine_background_write(rp2pio_statemachine_obj_t *
11461158
11471159 dma_channel_config c_write ;
11481160
1149- self -> current_write_buf = * once_write_buf ;
1150- self -> next_write_buf_1 = * loop_write_buf ;
1151- self -> next_write_buf_2 = * loop2_write_buf ;
1152- self -> next_write_buf_3 = * loop_write_buf ;
1161+ self -> current_write_buf = self -> once_write_buf_info ;
1162+ self -> current_write_buf_obj = self -> once_write_buf_obj ;
1163+ self -> next_write_buf_1 = self -> loop_write_buf_info ;
1164+ self -> next_write_buf_1_obj = self -> loop_write_buf_obj ;
1165+ self -> next_write_buf_2 = self -> loop2_write_buf_info ;
1166+ self -> next_write_buf_2_obj = self -> loop2_write_buf_obj ;
1167+ self -> next_write_buf_3 = self -> loop_write_buf_info ;
1168+ self -> next_write_buf_3_obj = self -> loop_write_buf_obj ;
11531169
11541170 self -> pending_buffers_write = pending_buffers_write ;
11551171 self -> dma_completed_write = false;
1172+
11561173 self -> background_stride_in_bytes = stride_in_bytes ;
11571174 self -> byteswap = swap ;
11581175
@@ -1164,8 +1181,8 @@ bool common_hal_rp2pio_statemachine_background_write(rp2pio_statemachine_obj_t *
11641181 channel_config_set_bswap (& c_write , swap );
11651182 dma_channel_configure (channel_write , & c_write ,
11661183 tx_destination ,
1167- once_write_buf -> info .buf ,
1168- once_write_buf -> info .len / stride_in_bytes ,
1184+ self -> once_write_buf_info . info .buf ,
1185+ self -> once_write_buf_info . info .len / stride_in_bytes ,
11691186 false);
11701187
11711188 common_hal_mcu_disable_interrupts ();
@@ -1184,9 +1201,13 @@ bool common_hal_rp2pio_statemachine_background_write(rp2pio_statemachine_obj_t *
11841201
11851202void rp2pio_statemachine_dma_complete_write (rp2pio_statemachine_obj_t * self , int channel_write ) {
11861203 self -> current_write_buf = self -> next_write_buf_1 ;
1204+ self -> current_write_buf_obj = self -> next_write_buf_1_obj ;
11871205 self -> next_write_buf_1 = self -> next_write_buf_2 ;
1206+ self -> next_write_buf_1_obj = self -> next_write_buf_2_obj ;
11881207 self -> next_write_buf_2 = self -> next_write_buf_3 ;
1208+ self -> next_write_buf_2_obj = self -> next_write_buf_3_obj ;
11891209 self -> next_write_buf_3 = self -> next_write_buf_1 ;
1210+ self -> next_write_buf_3_obj = self -> next_write_buf_1_obj ;
11901211
11911212 if (self -> current_write_buf .info .buf ) {
11921213 if (self -> pending_buffers_write > 0 ) {
@@ -1198,6 +1219,8 @@ void rp2pio_statemachine_dma_complete_write(rp2pio_statemachine_obj_t *self, int
11981219 self -> dma_completed_write = true;
11991220 self -> pending_buffers_write = 0 ; // should be a no-op
12001221 }
1222+
1223+ self -> switched_write_buffers = true;
12011224}
12021225
12031226bool common_hal_rp2pio_statemachine_stop_background_write (rp2pio_statemachine_obj_t * self ) {
@@ -1227,7 +1250,7 @@ bool common_hal_rp2pio_statemachine_background_read(rp2pio_statemachine_obj_t *s
12271250 uint8_t pio_index = pio_get_index (self -> pio );
12281251 uint8_t sm = self -> state_machine ;
12291252
1230- self -> switched_buffers = false;
1253+ self -> switched_read_buffers = false;
12311254
12321255 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 );
12331256
@@ -1331,6 +1354,9 @@ bool common_hal_rp2pio_statemachine_background_read(rp2pio_statemachine_obj_t *s
13311354 self -> pending_buffers_read = pending_buffers_read ;
13321355 self -> dma_completed_read = false;
13331356
1357+ self -> background_stride_in_bytes = stride_in_bytes ;
1358+ self -> byteswap = swap ;
1359+
13341360 c_read = dma_channel_get_default_config (channel_read );
13351361 channel_config_set_transfer_data_size (& c_read , _stride_to_dma_size (stride_in_bytes ));
13361362 channel_config_set_dreq (& c_read , self -> rx_dreq );
@@ -1377,7 +1403,7 @@ void rp2pio_statemachine_dma_complete_read(rp2pio_statemachine_obj_t *self, int
13771403 self -> pending_buffers_read = 0 ; // should be a no-op
13781404 }
13791405
1380- self -> switched_buffers = true;
1406+ self -> switched_read_buffers = true;
13811407}
13821408
13831409bool common_hal_rp2pio_statemachine_stop_background_read (rp2pio_statemachine_obj_t * self ) {
@@ -1424,15 +1450,19 @@ mp_obj_t common_hal_rp2pio_statemachine_get_rxfifo(rp2pio_statemachine_obj_t *se
14241450}
14251451
14261452mp_obj_t common_hal_rp2pio_statemachine_get_last_read (rp2pio_statemachine_obj_t * self ) {
1427- if (self -> switched_buffers ) {
1428- self -> switched_buffers = false;
1453+ if (self -> switched_read_buffers ) {
1454+ self -> switched_read_buffers = false;
14291455 return self -> next_read_buf_1_obj ;
14301456 }
14311457 return mp_const_empty_bytes ;
14321458}
14331459
14341460mp_obj_t common_hal_rp2pio_statemachine_get_last_write (rp2pio_statemachine_obj_t * self ) {
1435- return mp_const_none ;
1461+ if (self -> switched_write_buffers ) {
1462+ self -> switched_write_buffers = false;
1463+ return self -> next_write_buf_1_obj ;
1464+ }
1465+ return mp_const_empty_bytes ;
14361466}
14371467
14381468
0 commit comments