Skip to content

Commit 0ef45ca

Browse files
committed
add last_write property
1 parent 4aaa867 commit 0ef45ca

File tree

4 files changed

+68
-42
lines changed

4 files changed

+68
-42
lines changed

ports/raspberrypi/bindings/rp2pio/StateMachine.c

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -565,24 +565,21 @@ static mp_obj_t rp2pio_statemachine_background_write(size_t n_args, const mp_obj
565565
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
566566
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
567567

568-
sm_buf_info once_info;
569-
sm_buf_info loop_info;
570-
sm_buf_info loop2_info;
571568
size_t stride_in_bytes = 0;
572-
fill_buf_info(&once_info, args[ARG_once].u_obj, &stride_in_bytes, MP_BUFFER_READ);
573-
fill_buf_info(&loop_info, args[ARG_loop].u_obj, &stride_in_bytes, MP_BUFFER_READ);
574-
fill_buf_info(&loop2_info, args[ARG_loop2].u_obj, &stride_in_bytes, MP_BUFFER_READ);
575569

576570
self->once_write_buf_obj = args[ARG_once].u_obj;
577571
self->loop_write_buf_obj = args[ARG_loop].u_obj;
578572
self->loop2_write_buf_obj = args[ARG_loop2].u_obj;
579573

574+
fill_buf_info(&self->once_write_buf_info, args[ARG_once].u_obj, &stride_in_bytes, MP_BUFFER_READ);
575+
fill_buf_info(&self->loop_write_buf_info, args[ARG_loop].u_obj, &stride_in_bytes, MP_BUFFER_READ);
576+
fill_buf_info(&self->loop2_write_buf_info, args[ARG_loop2].u_obj, &stride_in_bytes, MP_BUFFER_READ);
577+
580578
if (!stride_in_bytes) {
581579
return mp_const_none;
582580
}
583581

584-
bool ok = common_hal_rp2pio_statemachine_background_write(self, &once_info, &loop_info, &loop2_info,
585-
stride_in_bytes, args[ARG_swap].u_bool);
582+
bool ok = common_hal_rp2pio_statemachine_background_write(self, stride_in_bytes, args[ARG_swap].u_bool);
586583

587584
if (mp_hal_is_interrupted()) {
588585
return mp_const_none;

ports/raspberrypi/bindings/rp2pio/StateMachine.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ void common_hal_rp2pio_statemachine_run(rp2pio_statemachine_obj_t *self, const u
5252
bool common_hal_rp2pio_statemachine_write(rp2pio_statemachine_obj_t *self, const uint8_t *data, size_t len, uint8_t stride_in_bytes, bool swap);
5353

5454
bool common_hal_rp2pio_statemachine_background_write(rp2pio_statemachine_obj_t *self,
55-
const sm_buf_info *once_write_buf, const sm_buf_info *loop_write_buf, const sm_buf_info *loop2_write_buf,
5655
uint8_t stride_in_bytes, bool swap);
5756

5857
bool common_hal_rp2pio_statemachine_background_read(rp2pio_statemachine_obj_t *self,

ports/raspberrypi/common-hal/rp2pio/StateMachine.c

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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

11851202
void 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

12031226
bool 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

13831409
bool 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

14261452
mp_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

14341460
mp_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

ports/raspberrypi/common-hal/rp2pio/StateMachine.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ typedef struct {
6767
mp_obj_t current_write_buf_obj, next_write_buf_1_obj, next_write_buf_2_obj, next_write_buf_3_obj;
6868
sm_buf_info current_write_buf, next_write_buf_1, next_write_buf_2, next_write_buf_3;
6969

70-
bool switched_buffers;
70+
bool switched_write_buffers, switched_read_buffers;
7171

7272
int background_stride_in_bytes;
7373
bool dma_completed_write, byteswap;

0 commit comments

Comments
 (0)