Skip to content

Commit 6b08448

Browse files
committed
Add DREQ methods for PWM/SPI/UART/I2C
1 parent 2f2e629 commit 6b08448

File tree

5 files changed

+61
-1
lines changed
  • src/rp2_common
    • hardware_i2c/include/hardware
    • hardware_pio/include/hardware
    • hardware_pwm/include/hardware
    • hardware_spi/include/hardware
    • hardware_uart/include/hardware

5 files changed

+61
-1
lines changed

src/rp2_common/hardware_i2c/include/hardware/i2c.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "pico.h"
1111
#include "pico/time.h"
1212
#include "hardware/structs/i2c.h"
13+
#include "hardware/regs/dreq.h"
1314

1415
// PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_I2C, Enable/disable assertions in the I2C module, type=bool, default=0, group=hardware_i2c
1516
#ifndef PARAM_ASSERTIONS_ENABLED_I2C
@@ -311,6 +312,19 @@ static inline void i2c_read_raw_blocking(i2c_inst_t *i2c, uint8_t *dst, size_t l
311312
}
312313
}
313314

315+
/*! \brief Return the DREQ to use for pacing transfers to/from a particular I2C instance
316+
* \ingroup hardware_i2c
317+
*
318+
* \param i2c Either \ref i2c0 or \ref i2c1
319+
* \param is_tx true for sending data to the I2C instance, false for received data from the I2C instance
320+
*/
321+
static inline uint i2c_get_dreq(i2c_inst_t *i2c, bool is_tx) {
322+
static_assert(DREQ_I2C0_RX == DREQ_I2C0_TX + 1, "");
323+
static_assert(DREQ_I2C1_RX == DREQ_I2C1_TX + 1, "");
324+
static_assert(DREQ_I2C1_TX == DREQ_I2C0_TX + 2, "");
325+
return DREQ_I2C0_TX + i2c_hw_index(i2c) * 2 + !is_tx;
326+
}
327+
314328
#ifdef __cplusplus
315329
}
316330
#endif

src/rp2_common/hardware_pio/include/hardware/pio.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,14 +436,19 @@ static inline void pio_gpio_init(PIO pio, uint pin) {
436436
gpio_set_function(pin, pio == pio0 ? GPIO_FUNC_PIO0 : GPIO_FUNC_PIO1);
437437
}
438438

439-
/*! \brief Return the DREQ to use for pacing transfers to a particular state machine
439+
/*! \brief Return the DREQ to use for pacing transfers to/from a particular state machine FIFO
440440
* \ingroup hardware_pio
441441
*
442442
* \param pio The PIO instance; either \ref pio0 or \ref pio1
443443
* \param sm State machine index (0..3)
444444
* \param is_tx true for sending data to the state machine, false for received data from the state machine
445445
*/
446446
static inline uint pio_get_dreq(PIO pio, uint sm, bool is_tx) {
447+
static_assert(DREQ_PIO0_TX1 - DREQ_PIO0_TX0 == 1, "");
448+
static_assert(DREQ_PIO0_TX2 - DREQ_PIO0_TX0 == 2, "");
449+
static_assert(DREQ_PIO0_TX3 - DREQ_PIO0_TX0 == 3, "");
450+
static_assert(DREQ_PIO0_RX0 - DREQ_PIO0_TX0 == NUM_PIO_STATE_MACHINES, "");
451+
static_assert(DREQ_PIO1_RX0 - DREQ_PIO1_TX0 == NUM_PIO_STATE_MACHINES, "");
447452
check_pio_param(pio);
448453
check_sm_param(sm);
449454
return sm + (is_tx ? 0 : NUM_PIO_STATE_MACHINES) + (pio == pio0 ? DREQ_PIO0_TX0 : DREQ_PIO1_TX0);

src/rp2_common/hardware_pwm/include/hardware/pwm.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "pico.h"
1111
#include "hardware/structs/pwm.h"
12+
#include "hardware/regs/dreq.h"
1213

1314
#ifdef __cplusplus
1415
extern "C" {
@@ -539,6 +540,18 @@ static inline void pwm_force_irq(uint slice_num) {
539540
pwm_hw->intf = 1u << slice_num;
540541
}
541542

543+
/*! \brief Return the DREQ to use for pacing transfers to a particular PWM slice
544+
* \ingroup hardware_pwm
545+
*
546+
* \param slice_num PWM slice number
547+
*/
548+
static inline uint pwm_get_dreq(uint slice_num) {
549+
static_assert(DREQ_PWM_WRAP1 == DREQ_PWM_WRAP0 + 1, "");
550+
static_assert(DREQ_PWM_WRAP7 == DREQ_PWM_WRAP0 + 7, "");
551+
check_slice_num_param(slice_num);
552+
return DREQ_PWM_WRAP0 + slice_num;
553+
}
554+
542555
#ifdef __cplusplus
543556
}
544557
#endif

src/rp2_common/hardware_spi/include/hardware/spi.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "pico.h"
1111
#include "pico/time.h"
1212
#include "hardware/structs/spi.h"
13+
#include "hardware/regs/dreq.h"
1314

1415
// PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_SPI, Enable/disable assertions in the SPI module, type=bool, default=0, group=hardware_spi
1516
#ifndef PARAM_ASSERTIONS_ENABLED_SPI
@@ -337,6 +338,19 @@ int spi_write16_blocking(spi_inst_t *spi, const uint16_t *src, size_t len);
337338
*/
338339
int spi_read16_blocking(spi_inst_t *spi, uint16_t repeated_tx_data, uint16_t *dst, size_t len);
339340

341+
/*! \brief Return the DREQ to use for pacing transfers to/from a particular SPI instance
342+
* \ingroup hardware_spi
343+
*
344+
* \param spi SPI instance specifier, either \ref spi0 or \ref spi1
345+
* \param is_tx true for sending data to the SPI instance, false for received data from the SPI instance
346+
*/
347+
static inline uint spi_get_dreq(spi_inst_t *spi, bool is_tx) {
348+
static_assert(DREQ_SPI0_RX == DREQ_SPI0_TX + 1, "");
349+
static_assert(DREQ_SPI1_RX == DREQ_SPI1_TX + 1, "");
350+
static_assert(DREQ_SPI1_TX == DREQ_SPI0_TX + 2, "");
351+
return DREQ_SPI0_TX + spi_get_index(spi) * 2 + !is_tx;
352+
}
353+
340354
#ifdef __cplusplus
341355
}
342356
#endif

src/rp2_common/hardware_uart/include/hardware/uart.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include "pico.h"
1111
#include "hardware/structs/uart.h"
12+
#include "hardware/regs/dreq.h"
1213

1314
// PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_UART, Enable/disable assertions in the UART module, type=bool, default=0, group=hardware_uart
1415
#ifndef PARAM_ASSERTIONS_ENABLED_UART
@@ -432,6 +433,19 @@ static inline void uart_default_tx_wait_blocking(void) {
432433
*/
433434
bool uart_is_readable_within_us(uart_inst_t *uart, uint32_t us);
434435

436+
/*! \brief Return the DREQ to use for pacing transfers to/from a particular UART instance
437+
* \ingroup hardware_uart
438+
*
439+
* \param uart UART instance. \ref uart0 or \ref uart1
440+
* \param is_tx true for sending data to the UART instance, false for received data from the SPI instance
441+
*/
442+
static inline uint uart_get_dreq(uart_inst_t *uart, bool is_tx) {
443+
static_assert(DREQ_UART0_RX == DREQ_UART0_TX + 1, "");
444+
static_assert(DREQ_UART1_RX == DREQ_UART1_TX + 1, "");
445+
static_assert(DREQ_UART1_TX == DREQ_UART0_TX + 2, "");
446+
return DREQ_UART0_TX + uart_get_index(uart) * 2 + !is_tx;
447+
}
448+
435449
#ifdef __cplusplus
436450
}
437451
#endif

0 commit comments

Comments
 (0)