Skip to content

Commit 11cd3c1

Browse files
committed
add last_write property
1 parent 4e4a688 commit 11cd3c1

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
@@ -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

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

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

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

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

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

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)