Skip to content

Commit 67e3171

Browse files
committed
Add an example of using using DMA with PIO
Fixes #467
1 parent 1ed4de5 commit 67e3171

File tree

4 files changed

+325
-0
lines changed

4 files changed

+325
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ App|Description
236236
[uart_tx](pio/uart_tx) | Implement the transmit component of a UART serial port, and print hello world.
237237
[ws2812](pio/ws2812) | Examples of driving WS2812 addressable RGB LEDs.
238238
[addition](pio/addition) | Add two integers together using PIO. Only around 8 billion times slower than Cortex-M0+.
239+
[uart_pio_dma](pio/uart_pio_dma) | Send and receive data from a UART implemented using the PIO and DMA
239240

240241
### PWM
241242

pio/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@ if (NOT PICO_NO_HARDWARE)
1919
add_subdirectory(uart_rx)
2020
add_subdirectory(uart_tx)
2121
add_subdirectory(ws2812)
22+
add_subdirectory(uart_pio_dma)
2223
endif ()

pio/uart_pio_dma/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_executable(uart_pio_dma)
2+
pico_generate_pio_header(uart_pio_dma ${CMAKE_CURRENT_LIST_DIR}/../uart_rx/uart_rx.pio)
3+
pico_generate_pio_header(uart_pio_dma ${CMAKE_CURRENT_LIST_DIR}/../uart_tx/uart_tx.pio)
4+
target_sources(uart_pio_dma PRIVATE uart_pio_dma.c)
5+
target_link_libraries(uart_pio_dma PRIVATE
6+
pico_stdlib
7+
hardware_pio
8+
hardware_dma
9+
)
10+
pico_add_extra_outputs(uart_pio_dma)
11+
example_auto_set_url(uart_pio_dma)

pio/uart_pio_dma/uart_pio_dma.c

Lines changed: 312 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,312 @@
1+
/**
2+
* Copyright (c) 2024 Raspberry Pi (Trading) Ltd.
3+
*
4+
* SPDX-License-Identifier: BSD-3-Clause
5+
*/
6+
7+
#include <stdio.h>
8+
#include <string.h>
9+
10+
#include "pico/stdlib.h"
11+
#include "hardware/pio.h"
12+
#include "hardware/uart.h"
13+
#include "hardware/dma.h"
14+
#include "uart_rx.pio.h"
15+
#include "uart_tx.pio.h"
16+
17+
// Sends data via GPIO 4 and receives it on GPIO 5
18+
// Connect these pins with a wire
19+
#define GPIO_TX 4
20+
#define GPIO_RX 5
21+
22+
#define SERIAL_BAUD 921600
23+
#define HARD_UART_INST uart1
24+
25+
#define USE_PIO_FOR_RX 1
26+
#define USE_DMA_FOR_RX 1
27+
#define USE_PIO_FOR_TX 1
28+
#define USE_DMA_FOR_TX 1
29+
30+
#ifndef DMA_IRQ_PRIORITY
31+
#define DMA_IRQ_PRIORITY PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY
32+
#endif
33+
34+
#ifndef PIO_IRQ_PRIORITY
35+
#define PIO_IRQ_PRIORITY PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY
36+
#endif
37+
38+
#define PIO_IRQ_NUM 0
39+
#define DMA_IRQ_NUM 0
40+
41+
#define DMA_IRQX(N) DMA_IRQ_ ## N
42+
#define DMA_IRQN(N) DMA_IRQX(N)
43+
44+
// dma channels
45+
static uint dma_channel_rx;
46+
static uint dma_channel_tx;
47+
48+
// pio hardware
49+
#if USE_PIO_FOR_RX
50+
static PIO pio_hw_rx;
51+
static uint pio_sm_rx;
52+
static uint offset_rx;
53+
#endif
54+
55+
#if USE_PIO_FOR_TX
56+
static PIO pio_hw_tx;
57+
static uint pio_sm_tx;
58+
static uint offset_tx;
59+
#endif
60+
61+
// read request size for progress
62+
static uint32_t read_size;
63+
64+
// PIO interrupt handler, called when the state machine fifo is not empty
65+
// note: shouldn't printf in an irq normally!
66+
static void pio_irq_handler() {
67+
dma_channel_hw_t *dma_chan = dma_channel_hw_addr(dma_channel_rx);
68+
printf("pio_rx dma_rx=%u/%u\n", read_size - dma_chan->transfer_count, read_size);
69+
}
70+
71+
// DMA interrupt handler, called when a DMA channel has transmitted its data
72+
static void dma_irq_handler() {
73+
if (dma_channel_tx >= 0 && dma_irqn_get_channel_status(DMA_IRQ_NUM, dma_channel_tx)) {
74+
dma_irqn_acknowledge_channel(DMA_IRQ_NUM, dma_channel_tx);
75+
printf("dma_tx done\n");
76+
}
77+
if (dma_channel_rx >= 0 && dma_irqn_get_channel_status(DMA_IRQ_NUM, dma_channel_rx)) {
78+
dma_irqn_acknowledge_channel(DMA_IRQ_NUM, dma_channel_rx);
79+
printf("dma_rx done\n");
80+
}
81+
}
82+
83+
// Return a pointer to pio hardware
84+
static PIO pio_hardware(int n) {
85+
static_assert(NUM_PIOS == 2, "");
86+
const PIO pios[] = {pio0, pio1};
87+
return pios[n];
88+
}
89+
90+
// Return irqn for the pio
91+
static int pio_irqn(PIO pio_hw, int irqn) {
92+
assert(irqn < (PIO1_IRQ_0 - PIO0_IRQ_0));
93+
return PIO0_IRQ_0 + (PIO1_IRQ_0 - PIO0_IRQ_0) * pio_get_index(pio_hw) + irqn;
94+
}
95+
96+
// Allocates a pio and statemachine and loads the pio program into memory
97+
// Return true on success
98+
static bool pio_claim_free_sm_add_program(const pio_program_t *program, PIO *pio_hw, uint *sm, uint *offset) {
99+
// Find a free pio
100+
int count = NUM_PIOS;
101+
while(count--) {
102+
*pio_hw = pio_hardware(count);
103+
if (!pio_can_add_program(*pio_hw, program)) {
104+
continue;
105+
}
106+
// Find a state machine
107+
*sm = (int8_t)pio_claim_unused_sm(*pio_hw, false);
108+
if (*sm < 0) {
109+
continue;
110+
}
111+
*offset = pio_add_program(*pio_hw, program);
112+
return true;
113+
}
114+
return false;
115+
}
116+
117+
// free up pio resources
118+
static void pio_remove_program_unclaim_sm(const pio_program_t *program, PIO pio_hw, uint sm, uint offset) {
119+
pio_remove_program(pio_hw, program, offset);
120+
pio_sm_unclaim(pio_hw, sm);
121+
}
122+
123+
static void dump_bytes(const char *bptr, uint32_t len) {
124+
unsigned int i = 0;
125+
for (i = 0; i < len;) {
126+
if ((i & 0x0f) == 0) {
127+
printf("\n");
128+
} else if ((i & 0x07) == 0) {
129+
printf(" ");
130+
}
131+
printf("%02x ", bptr[i++]);
132+
}
133+
printf("\n");
134+
}
135+
136+
int main()
137+
{
138+
setup_default_uart();
139+
140+
// setup text we are going to send and what we expect to get back
141+
const char buffer_tx[] = "the quick brown fox jumps over the lazy dog";
142+
143+
// Buffer for receiving data
144+
char buffer_rx[sizeof(buffer_tx) - 1] = {0};
145+
146+
#if !USE_PIO_FOR_RX || !USE_PIO_FOR_TX
147+
uart_init(HARD_UART_INST, SERIAL_BAUD);
148+
#endif
149+
150+
// setup pio for rx
151+
#if USE_PIO_FOR_RX
152+
if (!pio_claim_free_sm_add_program(&uart_rx_mini_program, &pio_hw_rx, &pio_sm_rx, &offset_rx)) {
153+
panic("failed to allocate pio for rx");
154+
}
155+
uart_rx_mini_program_init(pio_hw_rx, pio_sm_rx, offset_rx, GPIO_RX, SERIAL_BAUD);
156+
#else
157+
gpio_set_function(GPIO_RX, GPIO_FUNC_UART);
158+
#endif
159+
160+
// setup pio for tx
161+
#if USE_PIO_FOR_TX
162+
if (!pio_claim_free_sm_add_program(&uart_tx_program, &pio_hw_tx, &pio_sm_tx, &offset_tx)) {
163+
panic("failed to allocate pio for tx");
164+
}
165+
uart_tx_program_init(pio_hw_tx, pio_sm_tx, offset_tx, GPIO_TX, SERIAL_BAUD);
166+
#else
167+
gpio_set_function(GPIO_TX, GPIO_FUNC_UART);
168+
#endif
169+
170+
// setup pio interrupt
171+
#if USE_PIO_FOR_RX
172+
if (irq_get_exclusive_handler(pio_irqn(pio_hw_rx, PIO_IRQ_NUM))) {
173+
panic("PIO IRQ in use");
174+
}
175+
#if USE_DMA_FOR_RX
176+
irq_add_shared_handler(pio_irqn(pio_hw_rx, PIO_IRQ_NUM), pio_irq_handler, PIO_IRQ_PRIORITY);
177+
pio_set_irqn_source_enabled(pio_hw_rx, PIO_IRQ_NUM, pis_sm0_rx_fifo_not_empty + pio_sm_rx, true);
178+
irq_set_enabled(pio_irqn(pio_hw_rx, PIO_IRQ_NUM), true);
179+
#endif
180+
#endif
181+
182+
// add dma handler
183+
#if USE_DMA_FOR_RX || USE_DMA_FOR_TX
184+
if (irq_get_exclusive_handler(DMA_IRQN(DMA_IRQ_NUM))) {
185+
panic("DMA IRQ in use");
186+
}
187+
irq_add_shared_handler(DMA_IRQN(DMA_IRQ_NUM), dma_irq_handler, DMA_IRQ_PRIORITY);
188+
irq_set_enabled(DMA_IRQN(DMA_IRQ_NUM), true);
189+
#endif
190+
191+
// Setup dma for read
192+
#if USE_DMA_FOR_RX
193+
dma_channel_rx = dma_claim_unused_channel(false);
194+
if (dma_channel_rx < 0) {
195+
panic("No free dma channels");
196+
}
197+
dma_channel_config config_rx = dma_channel_get_default_config(dma_channel_rx);
198+
channel_config_set_transfer_data_size(&config_rx, DMA_SIZE_8);
199+
channel_config_set_read_increment(&config_rx, false);
200+
channel_config_set_write_increment(&config_rx, true);
201+
read_size = sizeof(buffer_tx) - 1;
202+
// enable irq for rx
203+
dma_irqn_set_channel_enabled(DMA_IRQ_NUM, dma_channel_rx, true);
204+
#if USE_PIO_FOR_RX
205+
// read from pio fifo
206+
channel_config_set_dreq(&config_rx, pio_get_dreq(pio_hw_rx, pio_sm_rx, false));
207+
// 8-bit read from the uppermost byte of the FIFO, as data is left-justified so need to add 3. Don't forget the cast!
208+
dma_channel_configure(dma_channel_rx, &config_rx, buffer_rx, (io_rw_8*)&pio_hw_rx->rxf[pio_sm_rx] + 3, read_size, true); // dma started
209+
#else
210+
// read from uart hardware
211+
channel_config_set_dreq(&config_rx, uart_get_dreq(HARD_UART_INST, false));
212+
dma_channel_configure(dma_channel_rx, &config_rx, buffer_rx, &uart_get_hw(HARD_UART_INST)->dr, read_size, true); // dma started
213+
#endif
214+
#endif
215+
216+
// setup dma for write
217+
#if USE_DMA_FOR_TX
218+
dma_channel_tx = dma_claim_unused_channel(false);
219+
if (dma_channel_tx < 0) {
220+
panic("No free dma channels");
221+
}
222+
dma_channel_config config_tx = dma_channel_get_default_config(dma_channel_tx);
223+
channel_config_set_transfer_data_size(&config_tx, DMA_SIZE_8);
224+
channel_config_set_read_increment(&config_tx, true);
225+
channel_config_set_write_increment(&config_tx, false);
226+
// enable irq for tx
227+
dma_irqn_set_channel_enabled(DMA_IRQ_NUM, dma_channel_tx, true);
228+
#if USE_PIO_FOR_RX
229+
// write to pio fifo
230+
channel_config_set_dreq(&config_tx, pio_get_dreq(pio_hw_tx, pio_sm_tx, true));
231+
dma_channel_configure(dma_channel_tx, &config_tx, &pio_hw_rx->txf[pio_sm_tx], buffer_tx, sizeof(buffer_tx) - 1, true); // dma started
232+
#else
233+
// write to uart hardware
234+
channel_config_set_dreq(&config_tx, uart_get_dreq(HARD_UART_INST, true));
235+
dma_channel_configure(dma_channel_tx, &config_tx, &uart_get_hw(HARD_UART_INST)->dr, buffer_tx, sizeof(buffer_tx) - 1, true); // dma started
236+
#endif
237+
#endif
238+
239+
// send data
240+
#if USE_DMA_FOR_TX
241+
dma_channel_wait_for_finish_blocking(dma_channel_tx); // wait for tx
242+
#elif USE_PIO_FOR_TX
243+
// write to the pio fifo
244+
int count_pio_tx = 0;
245+
while(count_pio_tx < sizeof(buffer_tx) - 1) {
246+
uart_tx_program_putc(pio_hw_tx, pio_sm_tx, buffer_tx[count_pio_tx++]);
247+
}
248+
#else
249+
uart_puts(HARD_UART_INST, buffer_tx);
250+
#endif
251+
252+
// Receive the data
253+
#if USE_DMA_FOR_RX
254+
// wait for dma rx
255+
dma_channel_wait_for_finish_blocking(dma_channel_rx);
256+
#elif USE_PIO_FOR_RX
257+
// read from the pio fifo
258+
int count_pio_rx = 0;
259+
while(count_pio_rx < sizeof(buffer_tx) - 1) {
260+
buffer_rx[count_pio_rx++] = uart_rx_program_getc(pio_hw_rx, pio_sm_rx);
261+
}
262+
#else
263+
// use the uart hardware
264+
int count_uart_rx = 0;
265+
while(count_uart_rx < sizeof(buffer_tx) - 1) {
266+
buffer_rx[count_uart_rx++] = uart_getc(HARD_UART_INST);
267+
}
268+
#endif
269+
270+
// check
271+
if (memcmp(buffer_rx, buffer_tx, sizeof(buffer_tx) - 1) == 0) {
272+
printf("Test passed\n");
273+
} else {
274+
printf("buffer_tx: >%s<\n", buffer_tx);
275+
dump_bytes(buffer_tx, sizeof(buffer_tx));
276+
printf("result: >%s<\n", buffer_rx);
277+
dump_bytes(buffer_rx, sizeof(buffer_rx));
278+
printf("Test failed\n");
279+
assert(0);
280+
}
281+
282+
// cleanup
283+
#if USE_DMA_FOR_TX
284+
dma_irqn_set_channel_enabled(DMA_IRQ_NUM, dma_channel_tx, false);
285+
dma_channel_unclaim(dma_channel_tx);
286+
#endif
287+
#if USE_DMA_FOR_RX
288+
dma_irqn_set_channel_enabled(DMA_IRQ_NUM, dma_channel_rx, false);
289+
dma_channel_unclaim(dma_channel_rx);
290+
#endif
291+
#if USE_DMA_FOR_RX || USE_DMA_FOR_TX
292+
irq_remove_handler(DMA_IRQN(DMA_IRQ_NUM), dma_irq_handler);
293+
if (!irq_has_shared_handler(DMA_IRQN(DMA_IRQ_NUM))) {
294+
irq_set_enabled(DMA_IRQN(DMA_IRQ_NUM), false);
295+
}
296+
#endif
297+
#if USE_PIO_FOR_RX
298+
pio_set_irqn_source_enabled(pio_hw_rx, PIO_IRQ_NUM, pis_sm0_rx_fifo_not_empty + pio_sm_rx, false);
299+
irq_remove_handler(pio_irqn(pio_hw_rx, PIO_IRQ_NUM), pio_irq_handler);
300+
if (!irq_has_shared_handler(pio_irqn(pio_hw_rx, PIO_IRQ_NUM))) {
301+
irq_set_enabled(pio_irqn(pio_hw_rx, PIO_IRQ_NUM), false);
302+
}
303+
pio_remove_program_unclaim_sm(&uart_rx_mini_program, pio_hw_rx, pio_sm_rx, offset_rx);
304+
#endif
305+
#if USE_PIO_FOR_TX
306+
pio_remove_program_unclaim_sm(&uart_tx_program, pio_hw_tx, pio_sm_tx, offset_tx);
307+
#endif
308+
#if !USE_PIO_FOR_RX || !USE_PIO_FOR_TX
309+
uart_deinit(HARD_UART_INST);
310+
#endif
311+
return 0;
312+
}

0 commit comments

Comments
 (0)