From abbbb91fa8813ac332e7a6618c45a2ebc2d82803 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Tue, 23 Feb 2021 16:25:02 -0800 Subject: Add state machine divisor check This causes an exception when setting a state machine too slow or too fast. Fixes #4222 --- ports/raspberrypi/common-hal/rp2pio/StateMachine.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c index f0ee842ef..5973f47a2 100644 --- a/ports/raspberrypi/common-hal/rp2pio/StateMachine.c +++ b/ports/raspberrypi/common-hal/rp2pio/StateMachine.c @@ -489,6 +489,10 @@ void common_hal_rp2pio_statemachine_set_frequency(rp2pio_statemachine_obj_t* sel if (frequency256 % div256 > 0) { div256 += 1; } + // 0 is interpreted as 0x10000 so it's valid. + if (div256 / 256 > 0x10000 || frequency > clock_get_hz(clk_sys)) { + mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_frequency); + } self->actual_frequency = frequency256 / div256; pio_sm_set_clkdiv_int_frac(self->pio, self->state_machine, div256 / 256, div256 % 256); -- cgit v1.2.3