Skip to content

Commit 462fa5f

Browse files
Gadgetoiddpgeorge
authored andcommitted
rp2/rp2_pio: Replace PIO_NUM macro with pio_get_index.
The `PIO_NUM` macro was defined when `rp2_pio.c` was first conceived. There's now a Pico SDK function for this, `pio_get_index()`, which is already used in some parts of the code. This commit removes `PIO_NUM` in favour of using `pio_get_index()` everywhere. Signed-off-by: Phil Howard <[email protected]>
1 parent 75350f9 commit 462fa5f

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

ports/rp2/rp2_pio.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
#include "hardware/irq.h"
3838
#include "hardware/pio.h"
3939

40-
#define PIO_NUM(pio) ((pio) == pio0 ? 0 : 1)
41-
4240
typedef struct _rp2_pio_obj_t {
4341
mp_obj_base_t base;
4442
PIO pio;
@@ -82,15 +80,15 @@ static void pio_irq0(PIO pio) {
8280
pio->irq = ints >> 8;
8381

8482
// Call handler if it is registered, for PIO irqs.
85-
rp2_pio_irq_obj_t *irq = MP_STATE_PORT(rp2_pio_irq_obj[PIO_NUM(pio)]);
83+
rp2_pio_irq_obj_t *irq = MP_STATE_PORT(rp2_pio_irq_obj[pio_get_index(pio)]);
8684
if (irq != NULL && (ints & irq->trigger)) {
8785
irq->flags = ints & irq->trigger;
8886
mp_irq_handler(&irq->base);
8987
}
9088

9189
// Call handler if it is registered, for StateMachine irqs.
9290
for (size_t i = 0; i < 4; ++i) {
93-
rp2_state_machine_irq_obj_t *irq = MP_STATE_PORT(rp2_state_machine_irq_obj[PIO_NUM(pio) * 4 + i]);
91+
rp2_state_machine_irq_obj_t *irq = MP_STATE_PORT(rp2_state_machine_irq_obj[pio_get_index(pio) * 4 + i]);
9492
if (irq != NULL && ((ints >> (8 + i)) & irq->trigger)) {
9593
irq->flags = 1;
9694
mp_irq_handler(&irq->base);
@@ -280,7 +278,7 @@ static mp_obj_t rp2_pio_add_program(mp_obj_t self_in, mp_obj_t prog_in) {
280278
uint offset = rp2_pio_add_managed_program(self->pio, &pio_program);
281279

282280
// Store the program offset in the program object.
283-
prog[PROG_OFFSET_PIO0 + PIO_NUM(self->pio)] = MP_OBJ_NEW_SMALL_INT(offset);
281+
prog[PROG_OFFSET_PIO0 + pio_get_index(self->pio)] = MP_OBJ_NEW_SMALL_INT(offset);
284282

285283
return mp_const_none;
286284
}
@@ -301,12 +299,12 @@ static mp_obj_t rp2_pio_remove_program(size_t n_args, const mp_obj_t *args) {
301299
mp_buffer_info_t bufinfo;
302300
mp_get_buffer_raise(prog[PROG_DATA], &bufinfo, MP_BUFFER_READ);
303301
length = bufinfo.len / 2;
304-
offset = mp_obj_get_int(prog[PROG_OFFSET_PIO0 + PIO_NUM(self->pio)]);
302+
offset = mp_obj_get_int(prog[PROG_OFFSET_PIO0 + pio_get_index(self->pio)]);
305303
if (offset < 0) {
306304
mp_raise_ValueError("prog not in instruction memory");
307305
}
308306
// Invalidate the program offset in the program object.
309-
prog[PROG_OFFSET_PIO0 + PIO_NUM(self->pio)] = MP_OBJ_NEW_SMALL_INT(-1);
307+
prog[PROG_OFFSET_PIO0 + pio_get_index(self->pio)] = MP_OBJ_NEW_SMALL_INT(-1);
310308
}
311309

312310
// Remove the program from the instruction memory.
@@ -354,7 +352,7 @@ static mp_obj_t rp2_pio_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
354352
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
355353

356354
// Get the IRQ object.
357-
rp2_pio_irq_obj_t *irq = MP_STATE_PORT(rp2_pio_irq_obj[PIO_NUM(self->pio)]);
355+
rp2_pio_irq_obj_t *irq = MP_STATE_PORT(rp2_pio_irq_obj[pio_get_index(self->pio)]);
358356

359357
// Allocate the IRQ object if it doesn't already exist.
360358
if (irq == NULL) {
@@ -364,7 +362,7 @@ static mp_obj_t rp2_pio_irq(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
364362
irq->base.parent = MP_OBJ_FROM_PTR(self);
365363
irq->base.handler = mp_const_none;
366364
irq->base.ishard = false;
367-
MP_STATE_PORT(rp2_pio_irq_obj[PIO_NUM(self->pio)]) = irq;
365+
MP_STATE_PORT(rp2_pio_irq_obj[pio_get_index(self->pio)]) = irq;
368366
}
369367

370368
if (n_args > 1 || kw_args->used != 0) {
@@ -426,7 +424,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
426424

427425
static mp_uint_t rp2_pio_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) {
428426
rp2_pio_obj_t *self = MP_OBJ_TO_PTR(self_in);
429-
rp2_pio_irq_obj_t *irq = MP_STATE_PORT(rp2_pio_irq_obj[PIO_NUM(self->pio)]);
427+
rp2_pio_irq_obj_t *irq = MP_STATE_PORT(rp2_pio_irq_obj[pio_get_index(self->pio)]);
430428
irq_set_enabled(self->irq, false);
431429
irq->flags = 0;
432430
irq->trigger = new_trigger;
@@ -436,7 +434,7 @@ static mp_uint_t rp2_pio_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) {
436434

437435
static mp_uint_t rp2_pio_irq_info(mp_obj_t self_in, mp_uint_t info_type) {
438436
rp2_pio_obj_t *self = MP_OBJ_TO_PTR(self_in);
439-
rp2_pio_irq_obj_t *irq = MP_STATE_PORT(rp2_pio_irq_obj[PIO_NUM(self->pio)]);
437+
rp2_pio_irq_obj_t *irq = MP_STATE_PORT(rp2_pio_irq_obj[pio_get_index(self->pio)]);
440438
if (info_type == MP_IRQ_INFO_FLAGS) {
441439
return irq->flags;
442440
} else if (info_type == MP_IRQ_INFO_TRIGGERS) {
@@ -537,10 +535,10 @@ static mp_obj_t rp2_state_machine_init_helper(const rp2_state_machine_obj_t *sel
537535
mp_obj_get_array_fixed_n(args[ARG_prog].u_obj, PROG_MAX_FIELDS, &prog);
538536

539537
// Get and the program offset, and load it into memory if it's not already there.
540-
mp_int_t offset = mp_obj_get_int(prog[PROG_OFFSET_PIO0 + PIO_NUM(self->pio)]);
538+
mp_int_t offset = mp_obj_get_int(prog[PROG_OFFSET_PIO0 + pio_get_index(self->pio)]);
541539
if (offset < 0) {
542-
rp2_pio_add_program(&rp2_pio_obj[PIO_NUM(self->pio)], args[ARG_prog].u_obj);
543-
offset = mp_obj_get_int(prog[PROG_OFFSET_PIO0 + PIO_NUM(self->pio)]);
540+
rp2_pio_add_program(&rp2_pio_obj[pio_get_index(self->pio)], args[ARG_prog].u_obj);
541+
offset = mp_obj_get_int(prog[PROG_OFFSET_PIO0 + pio_get_index(self->pio)]);
544542
}
545543
rp2_state_machine_initial_pc[self->id] = offset;
546544

@@ -907,7 +905,7 @@ MP_DEFINE_CONST_OBJ_TYPE(
907905

908906
static mp_uint_t rp2_state_machine_irq_trigger(mp_obj_t self_in, mp_uint_t new_trigger) {
909907
rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(self_in);
910-
rp2_state_machine_irq_obj_t *irq = MP_STATE_PORT(rp2_state_machine_irq_obj[PIO_NUM(self->pio)]);
908+
rp2_state_machine_irq_obj_t *irq = MP_STATE_PORT(rp2_state_machine_irq_obj[pio_get_index(self->pio)]);
911909
irq_set_enabled(self->irq, false);
912910
irq->flags = 0;
913911
irq->trigger = new_trigger;
@@ -917,7 +915,7 @@ static mp_uint_t rp2_state_machine_irq_trigger(mp_obj_t self_in, mp_uint_t new_t
917915

918916
static mp_uint_t rp2_state_machine_irq_info(mp_obj_t self_in, mp_uint_t info_type) {
919917
rp2_state_machine_obj_t *self = MP_OBJ_TO_PTR(self_in);
920-
rp2_state_machine_irq_obj_t *irq = MP_STATE_PORT(rp2_state_machine_irq_obj[PIO_NUM(self->pio)]);
918+
rp2_state_machine_irq_obj_t *irq = MP_STATE_PORT(rp2_state_machine_irq_obj[pio_get_index(self->pio)]);
921919
if (info_type == MP_IRQ_INFO_FLAGS) {
922920
return irq->flags;
923921
} else if (info_type == MP_IRQ_INFO_TRIGGERS) {

0 commit comments

Comments
 (0)