Repository: raspberrypi/pico-sdk
File: src/rp2_common/hardware_pio/include/hardware/pio.h (C/C++)

Code before:
static inline void pio_set_irq1_source_mask_enabled(PIO pio, uint32_t source_mask, bool enabled) {
    check_pio_param(pio);
    invalid_params_if(PIO, source_mask > PIO_INTR_BITS);
    if (enabled) {
        hw_set_bits(&pio->inte1, source_mask);
    } else {
        hw_clear_bits(&pio->inte1, source_mask);
    }
}

/*! \brief  Enable/Disable a single source PIO's specified (0/1) IRQ index
 *  \ingroup hardware_pio
 *
 * \param pio The PIO instance; either \ref pio0 or \ref pio1
 * \param irq_index the IRQ index; either 0 or 1
 * \param source the source number (see \ref pio_interrupt_source)
 * \param enabled true to enable IRQ 0 for the source, false to disable.
 */
static inline void pio_set_irqn_source_enabled(PIO pio, uint irq_index, enum pio_interrupt_source source, bool enabled) {
    invalid_params_if(PIO, irq_index > 1);
    if (irq_index) {
        pio_set_irq1_source_enabled(pio, source, enabled);
    } else {
        pio_set_irq0_source_enabled(pio, source, enabled);
    }
}

/*! \brief  Enable/Disable multiple sources on a PIO's specified (0/1) IRQ index
 *  \ingroup hardware_pio
 *
 * \param pio The PIO instance; either \ref pio0 or \ref pio1
 * \param source_mask Mask of bits, one for each source number (see \ref pio_interrupt_source) to affect
 * \param irq_index the IRQ index; either 0 or 1
 * \param enabled true to enable all the source specified in the mask on IRQ 1, false to disable all the source specified in the mask on IRQ 1
 */
static inline void pio_set_irqn_source_mask_enabled(PIO pio, uint irq_index, uint32_t source_mask, bool enabled) {
    invalid_params_if(PIO, irq_index > 1);
    if (irq_index) {
        pio_set_irq0_source_mask_enabled(pio, source_mask, enabled);
    } else {
        pio_set_irq1_source_mask_enabled(pio, source_mask, enabled);
    }
}

/*! \brief  Determine if a particular PIO interrupt is set
 *  \ingroup hardware_pio
 *
 * \param pio The PIO instance; either \ref pio0 or \ref pio1
 * \param pio_interrupt_num the PIO interrupt number 0-7
 * \return true if corresponding PIO interrupt is currently set
 */

Review comment (refactor):
It would involve a little bit of refactorting, but given how much the functionality of `pio_set_irq0_source_enabled` and `pio_set_irq1_source_enabled` overlap, I wonder if it might be worth reworking these three functions like:

```
static inline void pio_set_irq0_source_enabled(PIO pio, enum pio_interrupt_source source, bool enabled) {
    pio_set_irqn_source_enabled(pio, 0, source, enabled);
}

static inline void pio_set_irq1_source_enabled(PIO pio, enum pio_interrupt_source source, bool enabled) {
    pio_set_irqn_source_enabled(pio, 1, source, enabled);
}

static inline void pio_set_irqn_source_enabled(PIO pio, uint irq_index, enum pio_interrupt_source source, bool enabled) {
    check_pio_param(pio);
    invalid_params_if(PIO, irq_index > 1);
    invalid_params_if(PIO, source >= 12);
    if (enabled)
        hw_set_bits(&(irq_index ? pio->inte1 : pio->inte0), 1u << source);
    else
        hw_clear_bits(&(irq_index ? pio->inte1 : pio->inte0), 1u << source);
}
```
(and similarly for the other PIO `_irqn_` functions)

Given that these are all `inline` functions I guess it makes no difference to the compiled code, and reducing duplicated logic in the source might be a good thing? :man_shrugging: 

Code after:
static inline void pio_set_irq1_source_mask_enabled(PIO pio, uint32_t source_mask, bool enabled) {
    check_pio_param(pio);
    invalid_params_if(PIO, source_mask > PIO_INTR_BITS);
    if (enabled) {
        hw_set_bits(&pio->inte1, source_mask);
    } else {
        hw_clear_bits(&pio->inte1, source_mask);
    }
}

/*! \brief  Enable/Disable a single source on a PIO's specified (0/1) IRQ index
 *  \ingroup hardware_pio
 *
 * \param pio The PIO instance; either \ref pio0 or \ref pio1
 * \param irq_index the IRQ index; either 0 or 1
 * \param source the source number (see \ref pio_interrupt_source)
 * \param enabled true to enable the source on the specified IRQ, false to disable.
 */
static inline void pio_set_irqn_source_enabled(PIO pio, uint irq_index, enum pio_interrupt_source source, bool enabled) {
    invalid_params_if(PIO, irq_index > 1);
    if (irq_index) {
        pio_set_irq1_source_enabled(pio, source, enabled);
    } else {
        pio_set_irq0_source_enabled(pio, source, enabled);
    }
}

/*! \brief  Enable/Disable multiple sources on a PIO's specified (0/1) IRQ index
 *  \ingroup hardware_pio
 *
 * \param pio The PIO instance; either \ref pio0 or \ref pio1
 * \param irq_index the IRQ index; either 0 or 1
 * \param source_mask Mask of bits, one for each source number (see \ref pio_interrupt_source) to affect
 * \param enabled true to enable all the sources specified in the mask on the specified IRQ, false to disable all the sources specified in the mask on the specified IRQ
 */
static inline void pio_set_irqn_source_mask_enabled(PIO pio, uint irq_index, uint32_t source_mask, bool enabled) {
    invalid_params_if(PIO, irq_index > 1);
    if (irq_index) {
        pio_set_irq0_source_mask_enabled(pio, source_mask, enabled);
    } else {
        pio_set_irq1_source_mask_enabled(pio, source_mask, enabled);
    }
}

/*! \brief  Determine if a particular PIO interrupt is set
 *  \ingroup hardware_pio
 *
 * \param pio The PIO instance; either \ref pio0 or \ref pio1
 * \param pio_interrupt_num the PIO interrupt number 0-7
 * \return true if corresponding PIO interrupt is currently set
 */