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

Code before:
/*! \brief  Encode the specified transfer length into an "encoded_transfer_length" value suitable for the referenced methods
 *  \ingroup hardware_dma
 * \param transfer_count the number of transfers (NOT bytes, see \ref channel_config_set_transfer_data_size)
 *
 * \if rp2040_specific
 * On RP2040 the valid range is 0 -> 2^32 - 1
 * \endif
 *
 * \if rp2350_specific
 * On RP2350 the valid range is 0 -> 2^28 - 1
 * \endif
 * \return the encoded_transfer_count
 * \sa dma_channel_set_transfer_count
 * \sa dma_channel_configure
 * \sa dma_channel_transfer_from_buffer_now
 * \sa dma_channel_transfer_to_buffer_now
 */
static inline uint32_t dma_encode_transfer_count(uint transfer_count) {
#if !PICO_RP2040
    invalid_params_if(HARDWARE_DMA, transfer_count & DMA_CH0_TRANS_COUNT_MODE_BITS);
    return transfer_count & DMA_CH0_TRANS_COUNT_COUNT_BITS;
#else
    return transfer_count;
#endif
}

/*! \brief  Encode the specified transfer length, along with a flag to indicate the DMA transfer should be self-triggering, into an "encoded_transfer_length" value suitable for the referenced methods
 *  \ingroup hardware_dma
 * \param transfer_count the number of transfers (NOT bytes, see \ref channel_config_set_transfer_data_size)
 *
 * \if rp2040_specific
 * On RP2040 self-triggering DMA is not supported, so this method may not be used
 * \endif
 *
 * \if rp2350_specific
 * On RP2350 the valid range is 0 -> 2^28 - 1
 * \endif
 * \return the encoded_transfer_count
 * \sa dma_channel_set_transfer_count
 * \sa dma_channel_configure
 * \sa dma_channel_transfer_from_buffer_now
 * \sa dma_channel_transfer_to_buffer_now
 */
static inline uint32_t dma_encode_transfer_count_with_self_trigger(uint transfer_count) {
#if PICO_RP2040
    panic_unsupported();
#else
    return dma_encode_transfer_count(transfer_count) | (DMA_CH0_TRANS_COUNT_MODE_VALUE_TRIGGER_SELF << DMA_CH0_TRANS_COUNT_MODE_LSB);
#endif
}


Review comment (suggestion):
Would this flag a `transfer_count` of 0 as an invalid_param? The doxygen comment above implies that 0 is in the valid range? Also, I _think_ that perhaps this should be using `<` rather than `&` (with the comparison-value obviously modified as appropriate), because otherwise a value above 2^28 that has some of the lower bits also set, would still count as "valid"? :thinking: 

Code after:
        dma_channel_hw_addr(channel)->write_addr = (uintptr_t) write_addr;
    } else {
        dma_channel_hw_addr(channel)->al2_write_addr_trig = (uintptr_t) write_addr;
    }
}

/*! \brief  Encode the specified transfer length into an "encoded_transfer_length" value suitable for the referenced methods
 *  \ingroup hardware_dma
 * \param transfer_count the number of transfers (NOT bytes, see \ref channel_config_set_transfer_data_size)
 *
 * \if rp2040_specific
 * On RP2040 the valid range is 0 -> 2^32 - 1
 * \endif
 *
 * \if rp2350_specific
 * On RP2350 the valid range is 0 -> 2^28 - 1
 * \endif
 * \return the encoded_transfer_count
 * \sa dma_channel_set_transfer_count
 * \sa dma_channel_configure
 * \sa dma_channel_transfer_from_buffer_now
 * \sa dma_channel_transfer_to_buffer_now
 */
static inline uint32_t dma_encode_transfer_count(uint transfer_count) {
#if !PICO_RP2040
    invalid_params_if(HARDWARE_DMA, transfer_count & DMA_CH0_TRANS_COUNT_MODE_BITS);
    return transfer_count & DMA_CH0_TRANS_COUNT_COUNT_BITS;
#else
    return transfer_count;
#endif
}

/*! \brief  Encode the specified transfer length, along with a flag to indicate the DMA transfer should be self-triggering, into an "encoded_transfer_length" value suitable for the referenced methods
 *  \ingroup hardware_dma
 * \param transfer_count the number of transfers (NOT bytes, see \ref channel_config_set_transfer_data_size)
 *
 * \if rp2040_specific
 * On RP2040 self-triggering DMA is not supported, so this method should not be used
 * \endif
 *
 * \if rp2350_specific
 * On RP2350 the valid range is 0 -> 2^28 - 1
 * \endif
 * \return the encoded_transfer_count
 * \sa dma_channel_set_transfer_count
 * \sa dma_channel_configure
 * \sa dma_channel_transfer_from_buffer_now
 * \sa dma_channel_transfer_to_buffer_now
 */
static inline uint32_t dma_encode_transfer_count_with_self_trigger(uint transfer_count) {
#if PICO_RP2040