/ src / piolib / include / piolib.h
piolib.h
  1  // SPDX-License-Identifier: GPL-2.0
  2  /*
  3   * Copyright (c) 2023-24 Raspberry Pi Ltd.
  4   * All rights reserved.
  5   */
  6  
  7  #ifndef _PIOLIB_H
  8  #define _PIOLIB_H
  9  
 10  #ifdef __cplusplus
 11  extern "C" {
 12  #endif
 13  
 14  #include "hardware/clocks.h"
 15  #include "hardware/gpio.h"
 16  #include "pio_platform.h"
 17  
 18  #ifndef PARAM_ASSERTIONS_ENABLED_PIO
 19  #define PARAM_ASSERTIONS_ENABLED_PIO 0
 20  #endif
 21  
 22  #define PIO_ERR(x) ((PIO)(uintptr_t)(x))
 23  #define PIO_IS_ERR(x) (((uintptr_t)(x) >= (uintptr_t)-200))
 24  #define PIO_ERR_VAL(x) ((int)(uintptr_t)(x))
 25  
 26  #define PIO_ORIGIN_ANY ((uint)(~0))
 27  #define PIO_ORIGIN_INVALID PIO_ORIGIN_ANY
 28  
 29  #define pio0 pio_open_helper(0)
 30  
 31  enum pio_fifo_join {
 32      PIO_FIFO_JOIN_NONE = 0,
 33      PIO_FIFO_JOIN_TX = 1,
 34      PIO_FIFO_JOIN_RX = 2,
 35  };
 36  
 37  enum pio_mov_status_type { STATUS_TX_LESSTHAN = 0, STATUS_RX_LESSTHAN = 1 };
 38  
 39  enum pio_xfer_dir { PIO_DIR_TO_SM, PIO_DIR_FROM_SM, PIO_DIR_COUNT };
 40  
 41  #ifndef PIOLIB_INTERNALS
 42  
 43  enum pio_instr_bits {
 44      pio_instr_bits_jmp = 0x0000,
 45      pio_instr_bits_wait = 0x2000,
 46      pio_instr_bits_in = 0x4000,
 47      pio_instr_bits_out = 0x6000,
 48      pio_instr_bits_push = 0x8000,
 49      pio_instr_bits_pull = 0x8080,
 50      pio_instr_bits_mov = 0xa000,
 51      pio_instr_bits_irq = 0xc000,
 52      pio_instr_bits_set = 0xe000,
 53  };
 54  
 55  #ifndef NDEBUG
 56  #define _PIO_INVALID_IN_SRC 0x08u
 57  #define _PIO_INVALID_OUT_DEST 0x10u
 58  #define _PIO_INVALID_SET_DEST 0x20u
 59  #define _PIO_INVALID_MOV_SRC 0x40u
 60  #define _PIO_INVALID_MOV_DEST 0x80u
 61  #else
 62  #define _PIO_INVALID_IN_SRC 0u
 63  #define _PIO_INVALID_OUT_DEST 0u
 64  #define _PIO_INVALID_SET_DEST 0u
 65  #define _PIO_INVALID_MOV_SRC 0u
 66  #define _PIO_INVALID_MOV_DEST 0u
 67  #endif
 68  
 69  enum pio_src_dest {
 70      pio_pins = 0u,
 71      pio_x = 1u,
 72      pio_y = 2u,
 73      pio_null = 3u | _PIO_INVALID_SET_DEST | _PIO_INVALID_MOV_DEST,
 74      pio_pindirs =
 75          4u | _PIO_INVALID_IN_SRC | _PIO_INVALID_MOV_SRC | _PIO_INVALID_MOV_DEST,
 76      pio_exec_mov = 4u | _PIO_INVALID_IN_SRC | _PIO_INVALID_OUT_DEST |
 77                     _PIO_INVALID_SET_DEST | _PIO_INVALID_MOV_SRC,
 78      pio_status = 5u | _PIO_INVALID_IN_SRC | _PIO_INVALID_OUT_DEST |
 79                   _PIO_INVALID_SET_DEST | _PIO_INVALID_MOV_DEST,
 80      pio_pc =
 81          5u | _PIO_INVALID_IN_SRC | _PIO_INVALID_SET_DEST | _PIO_INVALID_MOV_SRC,
 82      pio_isr = 6u | _PIO_INVALID_SET_DEST,
 83      pio_osr = 7u | _PIO_INVALID_OUT_DEST | _PIO_INVALID_SET_DEST,
 84      pio_exec_out = 7u | _PIO_INVALID_IN_SRC | _PIO_INVALID_SET_DEST |
 85                     _PIO_INVALID_MOV_SRC | _PIO_INVALID_MOV_DEST,
 86  };
 87  
 88  #endif
 89  
 90  typedef struct pio_program {
 91      const uint16_t *instructions;
 92      uint8_t length;
 93      int8_t origin; // required instruction memory origin or -1
 94      uint8_t pio_version;
 95  } pio_program_t;
 96  
 97  typedef struct {
 98      uint32_t content[4];
 99  } pio_sm_config;
100  
101  typedef struct pio_instance *PIO;
102  typedef const struct pio_chip PIO_CHIP_T;
103  
104  struct pio_chip {
105      const char *name;
106      const char *compatible;
107      uint16_t instr_count;
108      uint16_t sm_count;
109      uint16_t fifo_depth;
110      void *hw_state;
111  
112      PIO (*create_instance)(PIO_CHIP_T *chip, uint index);
113      int (*open_instance)(PIO pio);
114      void (*close_instance)(PIO pio);
115  
116      int (*pio_sm_config_xfer)(PIO pio, uint sm, uint dir, uint buf_size,
117                                uint buf_count);
118      int (*pio_sm_xfer_data)(PIO pio, uint sm, uint dir, uint data_bytes,
119                              void *data);
120  
121      bool (*pio_can_add_program_at_offset)(PIO pio, const pio_program_t *program,
122                                            uint offset);
123      uint (*pio_add_program_at_offset)(PIO pio, const pio_program_t *program,
124                                        uint offset);
125      bool (*pio_remove_program)(PIO pio, const pio_program_t *program,
126                                 uint loaded_offset);
127      bool (*pio_clear_instruction_memory)(PIO pio);
128      uint (*pio_encode_delay)(PIO pio, uint cycles);
129      uint (*pio_encode_sideset)(PIO pio, uint sideset_bit_count, uint value);
130      uint (*pio_encode_sideset_opt)(PIO pio, uint sideset_bit_count, uint value);
131      uint (*pio_encode_jmp)(PIO pio, uint addr);
132      uint (*pio_encode_jmp_not_x)(PIO pio, uint addr);
133      uint (*pio_encode_jmp_x_dec)(PIO pio, uint addr);
134      uint (*pio_encode_jmp_not_y)(PIO pio, uint addr);
135      uint (*pio_encode_jmp_y_dec)(PIO pio, uint addr);
136      uint (*pio_encode_jmp_x_ne_y)(PIO pio, uint addr);
137      uint (*pio_encode_jmp_pin)(PIO pio, uint addr);
138      uint (*pio_encode_jmp_not_osre)(PIO pio, uint addr);
139      uint (*pio_encode_wait_gpio)(PIO pio, bool polarity, uint gpio);
140      uint (*pio_encode_wait_pin)(PIO pio, bool polarity, uint pin);
141      uint (*pio_encode_wait_irq)(PIO pio, bool polarity, bool relative,
142                                  uint irq);
143      uint (*pio_encode_in)(PIO pio, enum pio_src_dest src, uint count);
144      uint (*pio_encode_out)(PIO pio, enum pio_src_dest dest, uint count);
145      uint (*pio_encode_push)(PIO pio, bool if_full, bool block);
146      uint (*pio_encode_pull)(PIO pio, bool if_empty, bool block);
147      uint (*pio_encode_mov)(PIO pio, enum pio_src_dest dest,
148                             enum pio_src_dest src);
149      uint (*pio_encode_mov_not)(PIO pio, enum pio_src_dest dest,
150                                 enum pio_src_dest src);
151      uint (*pio_encode_mov_reverse)(PIO pio, enum pio_src_dest dest,
152                                     enum pio_src_dest src);
153      uint (*pio_encode_irq_set)(PIO pio, bool relative, uint irq);
154      uint (*pio_encode_irq_wait)(PIO pio, bool relative, uint irq);
155      uint (*pio_encode_irq_clear)(PIO pio, bool relative, uint irq);
156      uint (*pio_encode_set)(PIO pio, enum pio_src_dest dest, uint value);
157      uint (*pio_encode_nop)(PIO pio);
158  
159      bool (*pio_sm_claim)(PIO pio, uint sm);
160      bool (*pio_sm_claim_mask)(PIO pio, uint mask);
161      int (*pio_sm_claim_unused)(PIO pio, bool required);
162      bool (*pio_sm_unclaim)(PIO pio, uint sm);
163      bool (*pio_sm_is_claimed)(PIO pio, uint sm);
164  
165      void (*pio_sm_init)(PIO pio, uint sm, uint initial_pc,
166                          const pio_sm_config *config);
167      void (*pio_sm_set_config)(PIO pio, uint sm, const pio_sm_config *config);
168      void (*pio_sm_exec)(PIO pio, uint sm, uint instr, bool blocking);
169      void (*pio_sm_clear_fifos)(PIO pio, uint sm);
170      void (*pio_sm_set_clkdiv_int_frac)(PIO pio, uint sm, uint16_t div_int,
171                                         uint8_t div_frac);
172      void (*pio_sm_set_clkdiv)(PIO pio, uint sm, float div);
173      void (*pio_sm_set_pins)(PIO pio, uint sm, uint32_t pin_values);
174      void (*pio_sm_set_pins_with_mask)(PIO pio, uint sm, uint32_t pin_values,
175                                        uint32_t pin_mask);
176      void (*pio_sm_set_pindirs_with_mask)(PIO pio, uint sm, uint32_t pin_dirs,
177                                           uint32_t pin_mask);
178      void (*pio_sm_set_consecutive_pindirs)(PIO pio, uint sm, uint pin_base,
179                                             uint pin_count, bool is_out);
180      void (*pio_sm_set_enabled)(PIO pio, uint sm, bool enabled);
181      void (*pio_sm_set_enabled_mask)(PIO pio, uint32_t mask, bool enabled);
182      void (*pio_sm_restart)(PIO pio, uint sm);
183      void (*pio_sm_restart_mask)(PIO pio, uint32_t mask);
184      void (*pio_sm_clkdiv_restart)(PIO pio, uint sm);
185      void (*pio_sm_clkdiv_restart_mask)(PIO pio, uint32_t mask);
186      void (*pio_sm_enable_sync)(PIO pio, uint32_t mask);
187      void (*pio_sm_put)(PIO pio, uint sm, uint32_t data, bool blocking);
188      uint32_t (*pio_sm_get)(PIO pio, uint sm, bool blocking);
189      void (*pio_sm_set_dmactrl)(PIO pio, uint sm, bool is_tx, uint32_t ctrl);
190      bool (*pio_sm_is_rx_fifo_empty)(PIO pio, uint sm);
191      bool (*pio_sm_is_rx_fifo_full)(PIO pio, uint sm);
192      uint (*pio_sm_get_rx_fifo_level)(PIO pio, uint sm);
193      bool (*pio_sm_is_tx_fifo_empty)(PIO pio, uint sm);
194      bool (*pio_sm_is_tx_fifo_full)(PIO pio, uint sm);
195      uint (*pio_sm_get_tx_fifo_level)(PIO pio, uint sm);
196      void (*pio_sm_drain_tx_fifo)(PIO pio, uint sm);
197  
198      pio_sm_config (*pio_get_default_sm_config)(PIO pio);
199      void (*smc_set_out_pins)(PIO pio, pio_sm_config *c, uint out_base,
200                               uint out_count);
201      void (*smc_set_set_pins)(PIO pio, pio_sm_config *c, uint set_base,
202                               uint set_count);
203      void (*smc_set_in_pins)(PIO pio, pio_sm_config *c, uint in_base);
204      void (*smc_set_sideset_pins)(PIO pio, pio_sm_config *c, uint sideset_base);
205      void (*smc_set_sideset)(PIO pio, pio_sm_config *c, uint bit_count,
206                              bool optional, bool pindirs);
207      void (*smc_set_clkdiv_int_frac)(PIO pio, pio_sm_config *c, uint16_t div_int,
208                                      uint8_t div_frac);
209      void (*smc_set_clkdiv)(PIO pio, pio_sm_config *c, float div);
210      void (*smc_set_wrap)(PIO pio, pio_sm_config *c, uint wrap_target,
211                           uint wrap);
212      void (*smc_set_jmp_pin)(PIO pio, pio_sm_config *c, uint pin);
213      void (*smc_set_in_shift)(PIO pio, pio_sm_config *c, bool shift_right,
214                               bool autopush, uint push_threshold);
215      void (*smc_set_out_shift)(PIO pio, pio_sm_config *c, bool shift_right,
216                                bool autopull, uint pull_threshold);
217      void (*smc_set_fifo_join)(PIO pio, pio_sm_config *c,
218                                enum pio_fifo_join join);
219      void (*smc_set_out_special)(PIO pio, pio_sm_config *c, bool sticky,
220                                  bool has_enable_pin, uint enable_pin_index);
221      void (*smc_set_mov_status)(PIO pio, pio_sm_config *c,
222                                 enum pio_mov_status_type status_sel,
223                                 uint status_n);
224  
225      uint32_t (*clock_get_hz)(PIO pio, enum clock_index clk_index);
226      void (*pio_gpio_init)(PIO pio, uint pin);
227      void (*gpio_init)(PIO pio, uint gpio);
228      void (*gpio_set_function)(PIO pio, uint gpio, enum gpio_function fn);
229      void (*gpio_set_pulls)(PIO pio, uint gpio, bool up, bool down);
230      void (*gpio_set_outover)(PIO pio, uint gpio, uint value);
231      void (*gpio_set_inover)(PIO pio, uint gpio, uint value);
232      void (*gpio_set_oeover)(PIO pio, uint gpio, uint value);
233      void (*gpio_set_input_enabled)(PIO pio, uint gpio, bool enabled);
234      void (*gpio_set_drive_strength)(PIO pio, uint gpio,
235                                      enum gpio_drive_strength drive);
236  };
237  
238  struct pio_instance {
239      const PIO_CHIP_T *chip;
240      int in_use;
241      bool errors_are_fatal;
242      bool error;
243  };
244  
245  int pio_init(void);
246  PIO pio_open(uint idx);
247  PIO pio_open_by_name(const char *name);
248  PIO pio_open_helper(uint idx);
249  void pio_close(PIO pio);
250  void pio_panic(const char *msg);
251  int pio_get_index(PIO pio);
252  void pio_select(PIO pio);
253  PIO pio_get_current(void);
254  
255  static inline void pio_error(PIO pio, const char *msg) {
256      pio->error = true;
257      if (pio->errors_are_fatal)
258          pio_panic(msg);
259  }
260  
261  static inline bool pio_get_error(PIO pio) { return pio->error; }
262  
263  static inline void pio_clear_error(PIO pio) { pio->error = false; }
264  
265  static inline void pio_enable_fatal_errors(PIO pio, bool enable) {
266      pio->errors_are_fatal = enable;
267  }
268  
269  static inline uint pio_get_sm_count(PIO pio) { return pio->chip->sm_count; }
270  
271  static inline uint pio_get_instruction_count(PIO pio) {
272      return pio->chip->instr_count;
273  }
274  
275  static inline uint pio_get_fifo_depth(PIO pio) { return pio->chip->fifo_depth; }
276  
277  static inline void check_pio_param(__unused PIO pio) {
278      valid_params_if(PIO, pio_get_index(pio) >= 0);
279  }
280  
281  static inline int pio_sm_config_xfer(PIO pio, uint sm, uint dir, uint buf_size,
282                                       uint buf_count) {
283      check_pio_param(pio);
284      return pio->chip->pio_sm_config_xfer(pio, sm, dir, buf_size, buf_count);
285  }
286  
287  static inline int pio_sm_xfer_data(PIO pio, uint sm, uint dir, uint data_bytes,
288                                     void *data) {
289      check_pio_param(pio);
290      return pio->chip->pio_sm_xfer_data(pio, sm, dir, data_bytes, data);
291  }
292  
293  static inline bool pio_can_add_program(PIO pio, const pio_program_t *program) {
294      check_pio_param(pio);
295      return pio->chip->pio_can_add_program_at_offset(pio, program,
296                                                      PIO_ORIGIN_ANY);
297  }
298  
299  static inline bool pio_can_add_program_at_offset(PIO pio,
300                                                   const pio_program_t *program,
301                                                   uint offset) {
302      check_pio_param(pio);
303      return pio->chip->pio_can_add_program_at_offset(pio, program, offset);
304  }
305  
306  static inline uint pio_add_program(PIO pio, const pio_program_t *program) {
307      uint offset;
308      check_pio_param(pio);
309      offset = pio->chip->pio_add_program_at_offset(pio, program, PIO_ORIGIN_ANY);
310      if (offset == PIO_ORIGIN_INVALID)
311          pio_error(pio, "No program space");
312      return offset;
313  }
314  
315  static inline void
316  pio_add_program_at_offset(PIO pio, const pio_program_t *program, uint offset) {
317      check_pio_param(pio);
318      if (pio->chip->pio_add_program_at_offset(pio, program, offset) ==
319          PIO_ORIGIN_INVALID)
320          pio_error(pio, "No program space");
321  }
322  
323  static inline void pio_remove_program(PIO pio, const pio_program_t *program,
324                                        uint loaded_offset) {
325      check_pio_param(pio);
326      if (!pio->chip->pio_remove_program(pio, program, loaded_offset))
327          pio_error(pio, "Failed to remove program");
328  }
329  
330  static inline void pio_clear_instruction_memory(PIO pio) {
331      check_pio_param(pio);
332      if (!pio->chip->pio_clear_instruction_memory(pio))
333          pio_error(pio, "Failed to clear instruction memory");
334  }
335  
336  static inline uint pio_encode_delay(uint cycles) {
337      PIO pio = pio_get_current();
338      return pio->chip->pio_encode_delay(pio, cycles);
339  }
340  
341  static inline uint pio_encode_sideset(uint sideset_bit_count, uint value) {
342      PIO pio = pio_get_current();
343      return pio->chip->pio_encode_sideset(pio, sideset_bit_count, value);
344  }
345  
346  static inline uint pio_encode_sideset_opt(uint sideset_bit_count, uint value) {
347      PIO pio = pio_get_current();
348      return pio->chip->pio_encode_sideset_opt(pio, sideset_bit_count, value);
349  }
350  
351  static inline uint pio_encode_jmp(uint addr) {
352      PIO pio = pio_get_current();
353      return pio->chip->pio_encode_jmp(pio, addr);
354  }
355  
356  static inline uint pio_encode_jmp_not_x(uint addr) {
357      PIO pio = pio_get_current();
358      return pio->chip->pio_encode_jmp_not_x(pio, addr);
359  }
360  
361  static inline uint pio_encode_jmp_x_dec(uint addr) {
362      PIO pio = pio_get_current();
363      return pio->chip->pio_encode_jmp_x_dec(pio, addr);
364  }
365  
366  static inline uint pio_encode_jmp_not_y(uint addr) {
367      PIO pio = pio_get_current();
368      return pio->chip->pio_encode_jmp_not_y(pio, addr);
369  }
370  
371  static inline uint pio_encode_jmp_y_dec(uint addr) {
372      PIO pio = pio_get_current();
373      return pio->chip->pio_encode_jmp_y_dec(pio, addr);
374  }
375  
376  static inline uint pio_encode_jmp_x_ne_y(uint addr) {
377      PIO pio = pio_get_current();
378      return pio->chip->pio_encode_jmp_x_ne_y(pio, addr);
379  }
380  
381  static inline uint pio_encode_jmp_pin(uint addr) {
382      PIO pio = pio_get_current();
383      return pio->chip->pio_encode_jmp_pin(pio, addr);
384  }
385  
386  static inline uint pio_encode_jmp_not_osre(uint addr) {
387      PIO pio = pio_get_current();
388      return pio->chip->pio_encode_jmp_not_osre(pio, addr);
389  }
390  
391  static inline uint pio_encode_wait_gpio(bool polarity, uint gpio) {
392      PIO pio = pio_get_current();
393      return pio->chip->pio_encode_wait_gpio(pio, polarity, gpio);
394  }
395  
396  static inline uint pio_encode_wait_pin(bool polarity, uint pin) {
397      PIO pio = pio_get_current();
398      return pio->chip->pio_encode_wait_pin(pio, polarity, pin);
399  }
400  
401  static inline uint pio_encode_wait_irq(bool polarity, bool relative, uint irq) {
402      PIO pio = pio_get_current();
403      return pio->chip->pio_encode_wait_irq(pio, polarity, relative, irq);
404  }
405  
406  static inline uint pio_encode_in(enum pio_src_dest src, uint count) {
407      PIO pio = pio_get_current();
408      return pio->chip->pio_encode_in(pio, src, count);
409  }
410  
411  static inline uint pio_encode_out(enum pio_src_dest dest, uint count) {
412      PIO pio = pio_get_current();
413      return pio->chip->pio_encode_out(pio, dest, count);
414  }
415  
416  static inline uint pio_encode_push(bool if_full, bool block) {
417      PIO pio = pio_get_current();
418      return pio->chip->pio_encode_push(pio, if_full, block);
419  }
420  
421  static inline uint pio_encode_pull(bool if_empty, bool block) {
422      PIO pio = pio_get_current();
423      return pio->chip->pio_encode_pull(pio, if_empty, block);
424  }
425  
426  static inline uint pio_encode_mov(enum pio_src_dest dest,
427                                    enum pio_src_dest src) {
428      PIO pio = pio_get_current();
429      return pio->chip->pio_encode_mov(pio, dest, src);
430  }
431  
432  static inline uint pio_encode_mov_not(enum pio_src_dest dest,
433                                        enum pio_src_dest src) {
434      PIO pio = pio_get_current();
435      return pio->chip->pio_encode_mov_not(pio, dest, src);
436  }
437  
438  static inline uint pio_encode_mov_reverse(enum pio_src_dest dest,
439                                            enum pio_src_dest src) {
440      PIO pio = pio_get_current();
441      return pio->chip->pio_encode_mov_reverse(pio, dest, src);
442  }
443  
444  static inline uint pio_encode_irq_set(bool relative, uint irq) {
445      PIO pio = pio_get_current();
446      return pio->chip->pio_encode_irq_set(pio, relative, irq);
447  }
448  
449  static inline uint pio_encode_irq_wait(bool relative, uint irq) {
450      PIO pio = pio_get_current();
451      return pio->chip->pio_encode_irq_wait(pio, relative, irq);
452  }
453  
454  static inline uint pio_encode_irq_clear(bool relative, uint irq) {
455      PIO pio = pio_get_current();
456      return pio->chip->pio_encode_irq_clear(pio, relative, irq);
457  }
458  
459  static inline uint pio_encode_set(enum pio_src_dest dest, uint value) {
460      PIO pio = pio_get_current();
461      return pio->chip->pio_encode_set(pio, dest, value);
462  }
463  
464  static inline uint pio_encode_nop(void) {
465      PIO pio = pio_get_current();
466      return pio->chip->pio_encode_nop(pio);
467  }
468  
469  static inline void pio_sm_claim(PIO pio, uint sm) {
470      check_pio_param(pio);
471      if (!pio->chip->pio_sm_claim(pio, sm))
472          pio_error(pio, "Failed to claim SM");
473  }
474  
475  static inline void pio_claim_sm_mask(PIO pio, uint mask) {
476      check_pio_param(pio);
477      if (!pio->chip->pio_sm_claim_mask(pio, mask))
478          pio_error(pio, "Failed to claim masked SMs");
479  }
480  
481  static inline void pio_sm_unclaim(PIO pio, uint sm) {
482      check_pio_param(pio);
483      pio->chip->pio_sm_unclaim(pio, sm);
484  }
485  
486  static inline int pio_claim_unused_sm(PIO pio, bool required) {
487      check_pio_param(pio);
488      return pio->chip->pio_sm_claim_unused(pio, required);
489  }
490  
491  static inline bool pio_sm_is_claimed(PIO pio, uint sm) {
492      check_pio_param(pio);
493      return pio->chip->pio_sm_is_claimed(pio, sm);
494  }
495  
496  static inline void pio_sm_init(PIO pio, uint sm, uint initial_pc,
497                                 const pio_sm_config *config) {
498      check_pio_param(pio);
499      pio->chip->pio_sm_init(pio, sm, initial_pc, config);
500  }
501  
502  static inline void pio_sm_set_config(PIO pio, uint sm,
503                                       const pio_sm_config *config) {
504      check_pio_param(pio);
505      pio->chip->pio_sm_set_config(pio, sm, config);
506  }
507  
508  static inline void pio_sm_exec(PIO pio, uint sm, uint instr) {
509      check_pio_param(pio);
510      pio->chip->pio_sm_exec(pio, sm, instr, false);
511  }
512  
513  static inline void pio_sm_exec_wait_blocking(PIO pio, uint sm, uint instr) {
514      check_pio_param(pio);
515      pio->chip->pio_sm_exec(pio, sm, instr, true);
516  }
517  
518  static inline void pio_sm_clear_fifos(PIO pio, uint sm) {
519      check_pio_param(pio);
520      pio->chip->pio_sm_clear_fifos(pio, sm);
521  }
522  
523  static inline void pio_sm_set_clkdiv_int_frac(PIO pio, uint sm,
524                                                uint16_t div_int,
525                                                uint8_t div_frac) {
526      check_pio_param(pio);
527      pio->chip->pio_sm_set_clkdiv_int_frac(pio, sm, div_int, div_frac);
528  }
529  
530  static inline void pio_sm_set_clkdiv(PIO pio, uint sm, float div) {
531      check_pio_param(pio);
532      pio->chip->pio_sm_set_clkdiv(pio, sm, div);
533  }
534  
535  static inline void pio_sm_set_pins(PIO pio, uint sm, uint32_t pin_values) {
536      check_pio_param(pio);
537      pio->chip->pio_sm_set_pins(pio, sm, pin_values);
538  }
539  
540  static inline void pio_sm_set_pins_with_mask(PIO pio, uint sm,
541                                               uint32_t pin_values,
542                                               uint32_t pin_mask) {
543      check_pio_param(pio);
544      pio->chip->pio_sm_set_pins_with_mask(pio, sm, pin_values, pin_mask);
545  }
546  
547  static inline void pio_sm_set_pindirs_with_mask(PIO pio, uint sm,
548                                                  uint32_t pin_dirs,
549                                                  uint32_t pin_mask) {
550      check_pio_param(pio);
551      pio->chip->pio_sm_set_pindirs_with_mask(pio, sm, pin_dirs, pin_mask);
552  }
553  
554  static inline void pio_sm_set_consecutive_pindirs(PIO pio, uint sm,
555                                                    uint pin_base, uint pin_count,
556                                                    bool is_out) {
557      check_pio_param(pio);
558      pio->chip->pio_sm_set_consecutive_pindirs(pio, sm, pin_base, pin_count,
559                                                is_out);
560  }
561  
562  static inline void pio_sm_set_enabled(PIO pio, uint sm, bool enabled) {
563      check_pio_param(pio);
564      pio->chip->pio_sm_set_enabled(pio, sm, enabled);
565  }
566  
567  static inline void pio_set_sm_mask_enabled(PIO pio, uint32_t mask,
568                                             bool enabled) {
569      check_pio_param(pio);
570      pio->chip->pio_sm_set_enabled_mask(pio, mask, enabled);
571  }
572  
573  static inline void pio_sm_restart(PIO pio, uint sm) {
574      check_pio_param(pio);
575      pio->chip->pio_sm_restart(pio, sm);
576  }
577  
578  static inline void pio_restart_sm_mask(PIO pio, uint32_t mask) {
579      check_pio_param(pio);
580      pio->chip->pio_sm_restart_mask(pio, mask);
581  }
582  
583  static inline void pio_sm_clkdiv_restart(PIO pio, uint sm) {
584      check_pio_param(pio);
585      pio->chip->pio_sm_clkdiv_restart(pio, sm);
586  }
587  
588  static inline void pio_clkdiv_restart_sm_mask(PIO pio, uint32_t mask) {
589      check_pio_param(pio);
590      pio->chip->pio_sm_clkdiv_restart_mask(pio, mask);
591  }
592  
593  static inline void pio_enable_sm_in_sync_mask(PIO pio, uint32_t mask) {
594      check_pio_param(pio);
595      pio->chip->pio_sm_enable_sync(pio, mask);
596  };
597  
598  static inline void pio_sm_set_dmactrl(PIO pio, uint sm, bool is_tx,
599                                        uint32_t ctrl) {
600      check_pio_param(pio);
601      pio->chip->pio_sm_set_dmactrl(pio, sm, is_tx, ctrl);
602  };
603  
604  static inline bool pio_sm_is_rx_fifo_empty(PIO pio, uint sm) {
605      check_pio_param(pio);
606      return pio->chip->pio_sm_is_rx_fifo_empty(pio, sm);
607  }
608  
609  static inline bool pio_sm_is_rx_fifo_full(PIO pio, uint sm) {
610      check_pio_param(pio);
611      return pio->chip->pio_sm_is_rx_fifo_full(pio, sm);
612  }
613  
614  static inline uint pio_sm_get_rx_fifo_level(PIO pio, uint sm) {
615      check_pio_param(pio);
616      return pio->chip->pio_sm_get_rx_fifo_level(pio, sm);
617  }
618  
619  static inline bool pio_sm_is_tx_fifo_empty(PIO pio, uint sm) {
620      check_pio_param(pio);
621      return pio->chip->pio_sm_is_tx_fifo_empty(pio, sm);
622  }
623  
624  static inline bool pio_sm_is_tx_fifo_full(PIO pio, uint sm) {
625      check_pio_param(pio);
626      return pio->chip->pio_sm_is_tx_fifo_full(pio, sm);
627  }
628  
629  static inline uint pio_sm_get_tx_fifo_level(PIO pio, uint sm) {
630      check_pio_param(pio);
631      return pio->chip->pio_sm_get_tx_fifo_level(pio, sm);
632  }
633  
634  static inline void pio_sm_drain_tx_fifo(PIO pio, uint sm) {
635      check_pio_param(pio);
636      return pio->chip->pio_sm_drain_tx_fifo(pio, sm);
637  }
638  
639  static inline void pio_sm_put(PIO pio, uint sm, uint32_t data) {
640      check_pio_param(pio);
641      pio->chip->pio_sm_put(pio, sm, data, false);
642  }
643  
644  static inline void pio_sm_put_blocking(PIO pio, uint sm, uint32_t data) {
645      check_pio_param(pio);
646      pio->chip->pio_sm_put(pio, sm, data, true);
647  }
648  
649  static inline uint32_t pio_sm_get(PIO pio, uint sm) {
650      check_pio_param(pio);
651      return pio->chip->pio_sm_get(pio, sm, false);
652  }
653  
654  static inline uint32_t pio_sm_get_blocking(PIO pio, uint sm) {
655      check_pio_param(pio);
656      return pio->chip->pio_sm_get(pio, sm, true);
657  }
658  
659  static inline pio_sm_config pio_get_default_sm_config_for_pio(PIO pio) {
660      check_pio_param(pio);
661      return pio->chip->pio_get_default_sm_config(pio);
662  }
663  
664  static inline pio_sm_config pio_get_default_sm_config(void) {
665      PIO pio = pio_get_current();
666      return pio->chip->pio_get_default_sm_config(pio);
667  }
668  
669  static inline void sm_config_set_out_pins(pio_sm_config *c, uint out_base,
670                                            uint out_count) {
671      PIO pio = pio_get_current();
672      pio->chip->smc_set_out_pins(pio, c, out_base, out_count);
673  }
674  
675  static inline void sm_config_set_set_pins(pio_sm_config *c, uint set_base,
676                                            uint set_count) {
677      PIO pio = pio_get_current();
678      pio->chip->smc_set_set_pins(pio, c, set_base, set_count);
679  }
680  
681  static inline void sm_config_set_in_pins(pio_sm_config *c, uint in_base) {
682      PIO pio = pio_get_current();
683      pio->chip->smc_set_in_pins(pio, c, in_base);
684  }
685  
686  static inline void sm_config_set_sideset_pins(pio_sm_config *c,
687                                                uint sideset_base) {
688      PIO pio = pio_get_current();
689      pio->chip->smc_set_sideset_pins(pio, c, sideset_base);
690  }
691  
692  static inline void sm_config_set_sideset(pio_sm_config *c, uint bit_count,
693                                           bool optional, bool pindirs) {
694      PIO pio = pio_get_current();
695      pio->chip->smc_set_sideset(pio, c, bit_count, optional, pindirs);
696  }
697  
698  static inline void sm_config_set_clkdiv_int_frac(pio_sm_config *c,
699                                                   uint16_t div_int,
700                                                   uint8_t div_frac) {
701      PIO pio = pio_get_current();
702      pio->chip->smc_set_clkdiv_int_frac(pio, c, div_int, div_frac);
703  }
704  
705  static inline void sm_config_set_clkdiv(pio_sm_config *c, float div) {
706      PIO pio = pio_get_current();
707      pio->chip->smc_set_clkdiv(pio, c, div);
708  }
709  
710  static inline void sm_config_set_wrap(pio_sm_config *c, uint wrap_target,
711                                        uint wrap) {
712      PIO pio = pio_get_current();
713      pio->chip->smc_set_wrap(pio, c, wrap_target, wrap);
714  }
715  
716  static inline void sm_config_set_jmp_pin(pio_sm_config *c, uint pin) {
717      PIO pio = pio_get_current();
718      pio->chip->smc_set_jmp_pin(pio, c, pin);
719  }
720  
721  static inline void sm_config_set_in_shift(pio_sm_config *c, bool shift_right,
722                                            bool autopush, uint push_threshold) {
723      PIO pio = pio_get_current();
724      pio->chip->smc_set_in_shift(pio, c, shift_right, autopush, push_threshold);
725  }
726  
727  static inline void sm_config_set_out_shift(pio_sm_config *c, bool shift_right,
728                                             bool autopull, uint pull_threshold) {
729      PIO pio = pio_get_current();
730      pio->chip->smc_set_out_shift(pio, c, shift_right, autopull, pull_threshold);
731  }
732  
733  static inline void sm_config_set_fifo_join(pio_sm_config *c,
734                                             enum pio_fifo_join join) {
735      PIO pio = pio_get_current();
736      pio->chip->smc_set_fifo_join(pio, c, join);
737  }
738  
739  static inline void sm_config_set_out_special(pio_sm_config *c, bool sticky,
740                                               bool has_enable_pin,
741                                               uint enable_pin_index) {
742      PIO pio = pio_get_current();
743      pio->chip->smc_set_out_special(pio, c, sticky, has_enable_pin,
744                                     enable_pin_index);
745  }
746  
747  static inline void sm_config_set_mov_status(pio_sm_config *c,
748                                              enum pio_mov_status_type status_sel,
749                                              uint status_n) {
750      PIO pio = pio_get_current();
751      pio->chip->smc_set_mov_status(pio, c, status_sel, status_n);
752  }
753  
754  static inline void pio_gpio_init(PIO pio, uint pin) {
755      check_pio_param(pio);
756      pio->chip->pio_gpio_init(pio, pin);
757  }
758  
759  static inline uint32_t clock_get_hz(enum clock_index clk_index) {
760      PIO pio = pio_get_current();
761      return pio->chip->clock_get_hz(pio, clk_index);
762  }
763  
764  static inline void gpio_init(uint gpio) {
765      PIO pio = pio_get_current();
766      pio->chip->gpio_init(pio, gpio);
767  }
768  
769  static inline void gpio_set_function(uint gpio, enum gpio_function fn) {
770      PIO pio = pio_get_current();
771      pio->chip->gpio_set_function(pio, gpio, fn);
772  }
773  
774  static inline void gpio_set_pulls(uint gpio, bool up, bool down) {
775      PIO pio = pio_get_current();
776      pio->chip->gpio_set_pulls(pio, gpio, up, down);
777  }
778  
779  static inline void gpio_set_outover(uint gpio, uint value) {
780      PIO pio = pio_get_current();
781      pio->chip->gpio_set_outover(pio, gpio, value);
782  }
783  
784  static inline void gpio_set_inover(uint gpio, uint value) {
785      PIO pio = pio_get_current();
786      pio->chip->gpio_set_inover(pio, gpio, value);
787  }
788  
789  static inline void gpio_set_oeover(uint gpio, uint value) {
790      PIO pio = pio_get_current();
791      pio->chip->gpio_set_oeover(pio, gpio, value);
792  }
793  
794  static inline void gpio_set_input_enabled(uint gpio, bool enabled) {
795      PIO pio = pio_get_current();
796      pio->chip->gpio_set_input_enabled(pio, gpio, enabled);
797  }
798  
799  static inline void gpio_set_drive_strength(uint gpio,
800                                             enum gpio_drive_strength drive) {
801      PIO pio = pio_get_current();
802      pio->chip->gpio_set_drive_strength(pio, gpio, drive);
803  }
804  
805  static inline void gpio_pull_up(uint gpio) {
806      gpio_set_pulls(gpio, true, false);
807  }
808  
809  static inline void gpio_pull_down(uint gpio) {
810      gpio_set_pulls(gpio, false, true);
811  }
812  
813  static inline void gpio_disable_pulls(uint gpio) {
814      gpio_set_pulls(gpio, false, false);
815  }
816  
817  static inline void stdio_init_all(void) {}
818  
819  void sleep_us(uint64_t us);
820  
821  static inline void sleep_ms(uint32_t ms) {
822      sleep_us((uint64_t)(ms * (uint64_t)1000));
823  }
824  
825  #ifdef __cplusplus
826  }
827  #endif
828  
829  #endif