Skip to content

Commit 4553ad9

Browse files
committed
rp2pio: Add offset & pc (program counter) properties
If StateMachine.run needs to contain a JMP instruction, the offset property can be used to correctly encode the target address. StateMachine.pc is useful for debugging, as it can give an overall statistical idea of which state machine instructions are executing, or show the instruction where the machine is stalled.
1 parent 5f66400 commit 4553ad9

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

ports/raspberrypi/bindings/rp2pio/StateMachine.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,35 @@ MP_DEFINE_CONST_FUN_OBJ_1(rp2pio_statemachine_get_in_waiting_obj, rp2pio_statema
813813
MP_PROPERTY_GETTER(rp2pio_statemachine_in_waiting_obj,
814814
(mp_obj_t)&rp2pio_statemachine_get_in_waiting_obj);
815815

816+
//| offset: int
817+
//| """The instruction offset where the program was actually loaded"""
818+
//|
819+
820+
static mp_obj_t rp2pio_statemachine_obj_get_offset(mp_obj_t self_in) {
821+
rp2pio_statemachine_obj_t *self = MP_OBJ_TO_PTR(self_in);
822+
check_for_deinit(self);
823+
return MP_OBJ_NEW_SMALL_INT(common_hal_rp2pio_statemachine_get_offset(self));
824+
}
825+
MP_DEFINE_CONST_FUN_OBJ_1(rp2pio_statemachine_get_offset_obj, rp2pio_statemachine_obj_get_offset);
826+
827+
MP_PROPERTY_GETTER(rp2pio_statemachine_offset_obj,
828+
(mp_obj_t)&rp2pio_statemachine_get_offset_obj);
829+
830+
//| pc: int
831+
//| """The current program counter of the state machine"""
832+
//|
833+
834+
static mp_obj_t rp2pio_statemachine_obj_get_pc(mp_obj_t self_in) {
835+
rp2pio_statemachine_obj_t *self = MP_OBJ_TO_PTR(self_in);
836+
check_for_deinit(self);
837+
return MP_OBJ_NEW_SMALL_INT(common_hal_rp2pio_statemachine_get_pc(self));
838+
}
839+
MP_DEFINE_CONST_FUN_OBJ_1(rp2pio_statemachine_get_pc_obj, rp2pio_statemachine_obj_get_pc);
840+
841+
MP_PROPERTY_GETTER(rp2pio_statemachine_pc_obj,
842+
(mp_obj_t)&rp2pio_statemachine_get_pc_obj);
843+
844+
816845
static const mp_rom_map_elem_t rp2pio_statemachine_locals_dict_table[] = {
817846
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&rp2pio_statemachine_deinit_obj) },
818847
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
@@ -836,6 +865,9 @@ static const mp_rom_map_elem_t rp2pio_statemachine_locals_dict_table[] = {
836865
{ MP_ROM_QSTR(MP_QSTR_rxstall), MP_ROM_PTR(&rp2pio_statemachine_rxstall_obj) },
837866
{ MP_ROM_QSTR(MP_QSTR_txstall), MP_ROM_PTR(&rp2pio_statemachine_txstall_obj) },
838867
{ MP_ROM_QSTR(MP_QSTR_in_waiting), MP_ROM_PTR(&rp2pio_statemachine_in_waiting_obj) },
868+
869+
{ MP_ROM_QSTR(MP_QSTR_offset), MP_ROM_PTR(&rp2pio_statemachine_offset_obj) },
870+
{ MP_ROM_QSTR(MP_QSTR_pc), MP_ROM_PTR(&rp2pio_statemachine_pc_obj) },
839871
};
840872
static MP_DEFINE_CONST_DICT(rp2pio_statemachine_locals_dict, rp2pio_statemachine_locals_dict_table);
841873

ports/raspberrypi/bindings/rp2pio/StateMachine.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,7 @@ bool common_hal_rp2pio_statemachine_get_txstall(rp2pio_statemachine_obj_t *self)
6666
void common_hal_rp2pio_statemachine_clear_txstall(rp2pio_statemachine_obj_t *self);
6767
size_t common_hal_rp2pio_statemachine_get_in_waiting(rp2pio_statemachine_obj_t *self);
6868

69+
int common_hal_rp2pio_statemachine_get_offset(rp2pio_statemachine_obj_t *self);
70+
int common_hal_rp2pio_statemachine_get_pc(rp2pio_statemachine_obj_t *self);
71+
6972
void common_hal_rp2pio_statemachine_set_interrupt_handler(rp2pio_statemachine_obj_t *self, void (*handler)(void *), void *arg, int mask);

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,20 @@ int common_hal_rp2pio_statemachine_get_pending(rp2pio_statemachine_obj_t *self)
10881088
return self->pending_buffers;
10891089
}
10901090

1091+
int common_hal_rp2pio_statemachine_get_offset(rp2pio_statemachine_obj_t *self) {
1092+
uint8_t pio_index = pio_get_index(self->pio);
1093+
uint8_t sm = self->state_machine;
1094+
uint8_t offset = _current_program_offset[pio_index][sm];
1095+
return offset;
1096+
}
1097+
1098+
int common_hal_rp2pio_statemachine_get_pc(rp2pio_statemachine_obj_t *self) {
1099+
uint8_t pio_index = pio_get_index(self->pio);
1100+
PIO pio = pio_instances[pio_index];
1101+
uint8_t sm = self->state_machine;
1102+
return pio_sm_get_pc(pio, sm);
1103+
}
1104+
10911105
// Use a compile-time constant for MP_REGISTER_POINTER so the preprocessor will
10921106
// not split the expansion across multiple lines.
10931107
MP_REGISTER_ROOT_POINTER(mp_obj_t background_pio[enum_NUM_DMA_CHANNELS]);

0 commit comments

Comments
 (0)