100100//| :param int frequency: the target clock frequency of the state machine. Actual may be less. Use 0 for system clock speed.
101101//| :param ReadableBuffer init: a program to run once at start up. This is run after program
102102//| is started so instructions may be intermingled
103+ //| :param ReadableBuffer may_exec: Instructions that may be executed via `StateMachine.exec` calls.
104+ //| Some elements of the `StateMachine`'s configuration are inferred from the instructions used;
105+ //| for instance, if there is no ``in`` or ``push`` instruction, then the `StateMachine` is configured without a receive FIFO.
106+ //| In this case, passing a ``may_exec`` program containing an ``in`` instruction such as ``in x``, a receive FIFO will be configured.
103107//| :param ~microcontroller.Pin first_out_pin: the first pin to use with the OUT instruction
104108//| :param int out_pin_count: the count of consecutive pins to use with OUT starting at first_out_pin
105109//| :param int initial_out_pin_state: the initial output value for out pins starting at first_out_pin
152156STATIC mp_obj_t rp2pio_statemachine_make_new (const mp_obj_type_t * type , size_t n_args , size_t n_kw , const mp_obj_t * all_args ) {
153157 rp2pio_statemachine_obj_t * self = m_new_obj (rp2pio_statemachine_obj_t );
154158 self -> base .type = & rp2pio_statemachine_type ;
155- enum { ARG_program , ARG_frequency , ARG_init ,
159+ enum { ARG_program , ARG_frequency , ARG_init , ARG_may_exec ,
156160 ARG_first_out_pin , ARG_out_pin_count , ARG_initial_out_pin_state , ARG_initial_out_pin_direction ,
157161 ARG_first_in_pin , ARG_in_pin_count ,
158162 ARG_pull_in_pin_up , ARG_pull_in_pin_down ,
@@ -171,6 +175,7 @@ STATIC mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
171175 { MP_QSTR_program , MP_ARG_REQUIRED | MP_ARG_OBJ },
172176 { MP_QSTR_frequency , MP_ARG_REQUIRED | MP_ARG_INT },
173177 { MP_QSTR_init , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
178+ { MP_QSTR_may_exec , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
174179
175180 { MP_QSTR_first_out_pin , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
176181 { MP_QSTR_out_pin_count , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 1 } },
@@ -220,6 +225,10 @@ STATIC mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
220225 init_bufinfo .len = 0 ;
221226 mp_get_buffer (args [ARG_init ].u_obj , & init_bufinfo , MP_BUFFER_READ );
222227
228+ mp_buffer_info_t may_exec_bufinfo ;
229+ may_exec_bufinfo .len = 0 ;
230+ mp_get_buffer (args [ARG_may_exec ].u_obj , & may_exec_bufinfo , MP_BUFFER_READ );
231+
223232 // We don't validate pin in use here because we may be ok sharing them within a PIO.
224233 const mcu_pin_obj_t * first_out_pin =
225234 validate_obj_is_pin_or_none (args [ARG_first_out_pin ].u_obj , MP_QSTR_first_out_pin );
@@ -264,6 +273,7 @@ STATIC mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
264273 bufinfo .buf , bufinfo .len / 2 ,
265274 args [ARG_frequency ].u_int ,
266275 init_bufinfo .buf , init_bufinfo .len / 2 ,
276+ may_exec_bufinfo .buf , bufinfo .len / 2 ,
267277 first_out_pin , out_pin_count , args [ARG_initial_out_pin_state ].u_int , args [ARG_initial_out_pin_direction ].u_int ,
268278 first_in_pin , in_pin_count , args [ARG_pull_in_pin_up ].u_int , args [ARG_pull_in_pin_down ].u_int ,
269279 first_set_pin , set_pin_count , args [ARG_initial_set_pin_state ].u_int , args [ARG_initial_set_pin_direction ].u_int ,
0 commit comments