6262//| program: ReadableBuffer,
6363//| frequency: int,
6464//| *,
65+ //| may_exec: Optional[ReadableBuffer] = None,
6566//| init: Optional[ReadableBuffer] = None,
6667//| first_out_pin: Optional[microcontroller.Pin] = None,
6768//| out_pin_count: int = 1,
9394//| user_interruptible: bool = True,
9495//| wrap_target: int = 0,
9596//| wrap: int = -1,
97+ //| offset: int = -1,
9698//| ) -> None:
9799//| """Construct a StateMachine object on the given pins with the given program.
98100//|
99101//| :param ReadableBuffer program: the program to run with the state machine
100102//| :param int frequency: the target clock frequency of the state machine. Actual may be less. Use 0 for system clock speed.
101103//| :param ReadableBuffer init: a program to run once at start up. This is run after program
102104//| is started so instructions may be intermingled
105+ //| :param ReadableBuffer may_exec: Instructions that may be executed via `StateMachine.run` calls.
106+ //| Some elements of the `StateMachine`'s configuration are inferred from the instructions used;
107+ //| for instance, if there is no ``in`` or ``push`` instruction, then the `StateMachine` is configured without a receive FIFO.
108+ //| In this case, passing a ``may_exec`` program containing an ``in`` instruction such as ``in x``, a receive FIFO will be configured.
103109//| :param ~microcontroller.Pin first_out_pin: the first pin to use with the OUT instruction
104110//| :param int out_pin_count: the count of consecutive pins to use with OUT starting at first_out_pin
105111//| :param int initial_out_pin_state: the initial output value for out pins starting at first_out_pin
146152//| :param int wrap: The instruction after which to wrap to the ``wrap``
147153//| instruction. As a special case, -1 (the default) indicates the
148154//| last instruction of the program.
155+ //| :param int offset: A specific offset in the state machine's program memory where the program must be loaded.
156+ //| The default value, -1, allows the program to be loaded at any offset.
157+ //| This is appropriate for most programs.
149158//| """
150159//| ...
151160
152161STATIC 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 ) {
153162 rp2pio_statemachine_obj_t * self = m_new_obj (rp2pio_statemachine_obj_t );
154163 self -> base .type = & rp2pio_statemachine_type ;
155- enum { ARG_program , ARG_frequency , ARG_init ,
164+ enum { ARG_program , ARG_frequency , ARG_init , ARG_may_exec ,
156165 ARG_first_out_pin , ARG_out_pin_count , ARG_initial_out_pin_state , ARG_initial_out_pin_direction ,
157166 ARG_first_in_pin , ARG_in_pin_count ,
158167 ARG_pull_in_pin_up , ARG_pull_in_pin_down ,
@@ -166,11 +175,13 @@ STATIC mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
166175 ARG_auto_push , ARG_push_threshold , ARG_in_shift_right ,
167176 ARG_user_interruptible ,
168177 ARG_wrap_target ,
169- ARG_wrap ,};
178+ ARG_wrap ,
179+ ARG_offset ,};
170180 static const mp_arg_t allowed_args [] = {
171181 { MP_QSTR_program , MP_ARG_REQUIRED | MP_ARG_OBJ },
172182 { MP_QSTR_frequency , MP_ARG_REQUIRED | MP_ARG_INT },
173183 { MP_QSTR_init , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
184+ { MP_QSTR_may_exec , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
174185
175186 { MP_QSTR_first_out_pin , MP_ARG_KW_ONLY | MP_ARG_OBJ , {.u_obj = mp_const_none } },
176187 { MP_QSTR_out_pin_count , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 1 } },
@@ -209,6 +220,7 @@ STATIC mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
209220
210221 { MP_QSTR_wrap_target , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = 0 } },
211222 { MP_QSTR_wrap , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
223+ { MP_QSTR_offset , MP_ARG_KW_ONLY | MP_ARG_INT , {.u_int = -1 } },
212224 };
213225 mp_arg_val_t args [MP_ARRAY_SIZE (allowed_args )];
214226 mp_arg_parse_all_kw_array (n_args , n_kw , all_args , MP_ARRAY_SIZE (allowed_args ), allowed_args , args );
@@ -220,6 +232,10 @@ STATIC mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
220232 init_bufinfo .len = 0 ;
221233 mp_get_buffer (args [ARG_init ].u_obj , & init_bufinfo , MP_BUFFER_READ );
222234
235+ mp_buffer_info_t may_exec_bufinfo ;
236+ may_exec_bufinfo .len = 0 ;
237+ mp_get_buffer (args [ARG_may_exec ].u_obj , & may_exec_bufinfo , MP_BUFFER_READ );
238+
223239 // We don't validate pin in use here because we may be ok sharing them within a PIO.
224240 const mcu_pin_obj_t * first_out_pin =
225241 validate_obj_is_pin_or_none (args [ARG_first_out_pin ].u_obj , MP_QSTR_first_out_pin );
@@ -264,6 +280,7 @@ STATIC mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
264280 bufinfo .buf , bufinfo .len / 2 ,
265281 args [ARG_frequency ].u_int ,
266282 init_bufinfo .buf , init_bufinfo .len / 2 ,
283+ may_exec_bufinfo .buf , may_exec_bufinfo .len / 2 ,
267284 first_out_pin , out_pin_count , args [ARG_initial_out_pin_state ].u_int , args [ARG_initial_out_pin_direction ].u_int ,
268285 first_in_pin , in_pin_count , args [ARG_pull_in_pin_up ].u_int , args [ARG_pull_in_pin_down ].u_int ,
269286 first_set_pin , set_pin_count , args [ARG_initial_set_pin_state ].u_int , args [ARG_initial_set_pin_direction ].u_int ,
@@ -276,7 +293,7 @@ STATIC mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
276293 args [ARG_wait_for_txstall ].u_bool ,
277294 args [ARG_auto_push ].u_bool , push_threshold , args [ARG_in_shift_right ].u_bool ,
278295 args [ARG_user_interruptible ].u_bool ,
279- wrap_target , wrap );
296+ wrap_target , wrap , args [ ARG_offset ]. u_int );
280297 return MP_OBJ_FROM_PTR (self );
281298}
282299
0 commit comments