Skip to content

Commit 77461c7

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

File tree

4 files changed

+329
-0
lines changed

4 files changed

+329
-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: 316 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,316 @@
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_init(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+
break;
105+
}
106+
if (count == 0) {
107+
return false;
108+
}
109+
}
110+
// Find a state machine
111+
*sm = (int8_t)pio_claim_unused_sm(*pio_hw, false);
112+
if (*sm < 0) {
113+
return false;
114+
}
115+
*offset = pio_add_program(*pio_hw, program);
116+
return true;
117+
}
118+
119+
// free up pio resources
120+
static void pio_deinit(const pio_program_t *program, PIO pio_hw, uint sm, uint offset) {
121+
pio_remove_program(pio_hw, program, offset);
122+
pio_sm_unclaim(pio_hw, sm);
123+
}
124+
125+
static void dump_bytes(const char *bptr, uint32_t len) {
126+
unsigned int i = 0;
127+
for (i = 0; i < len;) {
128+
if ((i & 0x0f) == 0) {
129+
printf("\n");
130+
} else if ((i & 0x07) == 0) {
131+
printf(" ");
132+
}
133+
printf("%02x ", bptr[i++]);
134+
}
135+
printf("\n");
136+
}
137+
138+
int main()
139+
{
140+
setup_default_uart();
141+
142+
// setup text we are going to send and what we expect to get back
143+
const char buffer_tx[] = "the quick brown fox jumps over the lazy dog";
144+
145+
// Buffer for receiving data
146+
char buffer_rx[sizeof(buffer_tx) - 1] = {0};
147+
148+
#if !USE_PIO_FOR_RX || !USE_PIO_FOR_TX
149+
uart_init(HARD_UART_INST, SERIAL_BAUD);
150+
#endif
151+
152+
// setup pio for rx
153+
#if USE_PIO_FOR_RX
154+
if (!pio_init(&uart_rx_mini_program, &pio_hw_rx, &pio_sm_rx, &offset_rx)) {
155+
panic("failed to allocate pio for rx");
156+
}
157+
uart_rx_mini_program_init(pio_hw_rx, pio_sm_rx, offset_rx, GPIO_RX, SERIAL_BAUD);
158+
#else
159+
gpio_set_function(GPIO_RX, GPIO_FUNC_UART);
160+
#endif
161+
162+
// setup pio for tx
163+
#if USE_PIO_FOR_TX
164+
if (!pio_init(&uart_tx_program, &pio_hw_tx, &pio_sm_tx, &offset_tx)) {
165+
panic("failed to allocate pio for tx");
166+
}
167+
uart_tx_program_init(pio_hw_tx, pio_sm_tx, offset_tx, GPIO_TX, SERIAL_BAUD);
168+
#else
169+
gpio_set_function(GPIO_TX, GPIO_FUNC_UART);
170+
#endif
171+
172+
// Setup dma for read
173+
#if USE_DMA_FOR_RX
174+
dma_channel_rx = dma_claim_unused_channel(false);
175+
if (dma_channel_rx < 0) {
176+
panic("No free dma channels");
177+
}
178+
dma_channel_config config_rx = dma_channel_get_default_config(dma_channel_rx);
179+
channel_config_set_transfer_data_size(&config_rx, DMA_SIZE_8);
180+
channel_config_set_read_increment(&config_rx, false);
181+
channel_config_set_write_increment(&config_rx, true);
182+
read_size = sizeof(buffer_tx) - 1;
183+
#if USE_PIO_FOR_RX
184+
// read from pio fifo
185+
channel_config_set_dreq(&config_rx, pio_get_dreq(pio_hw_rx, pio_sm_rx, false));
186+
// 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!
187+
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
188+
#else
189+
// read from uart hardware
190+
channel_config_set_dreq(&config_rx, uart_get_dreq(HARD_UART_INST, false));
191+
dma_channel_configure(dma_channel_rx, &config_rx, buffer_rx, &uart_get_hw(HARD_UART_INST)->dr, read_size, true); // dma started
192+
#endif
193+
#endif
194+
195+
// setup dma for write
196+
#if USE_DMA_FOR_TX
197+
dma_channel_tx = dma_claim_unused_channel(false);
198+
if (dma_channel_tx < 0) {
199+
panic("No free dma channels");
200+
}
201+
dma_channel_config config_tx = dma_channel_get_default_config(dma_channel_tx);
202+
channel_config_set_transfer_data_size(&config_tx, DMA_SIZE_8);
203+
channel_config_set_read_increment(&config_tx, true);
204+
channel_config_set_write_increment(&config_tx, false);
205+
#if USE_PIO_FOR_RX
206+
// write to pio fofo
207+
channel_config_set_dreq(&config_tx, pio_get_dreq(pio_hw_tx, pio_sm_tx, true));
208+
dma_channel_configure(dma_channel_tx, &config_tx, &pio_hw_rx->txf[pio_sm_tx], buffer_tx, sizeof(buffer_tx) - 1, true); // dma started
209+
#else
210+
// write to uart hardware
211+
channel_config_set_dreq(&config_tx, uart_get_dreq(HARD_UART_INST, true));
212+
dma_channel_configure(dma_channel_tx, &config_tx, &uart_get_hw(HARD_UART_INST)->dr, buffer_tx, sizeof(buffer_tx) - 1, true); // dma started
213+
#endif
214+
#endif
215+
216+
// setup pio interrupt
217+
#if USE_PIO_FOR_RX
218+
if (irq_get_exclusive_handler(pio_irqn(pio_hw_rx, PIO_IRQ_NUM))) {
219+
panic("PIO IRQ in use");
220+
}
221+
#if USE_DMA_FOR_RX
222+
irq_add_shared_handler(pio_irqn(pio_hw_rx, PIO_IRQ_NUM), pio_irq_handler, PIO_IRQ_PRIORITY);
223+
pio_set_irqn_source_enabled(pio_hw_rx, PIO_IRQ_NUM, pis_sm0_rx_fifo_not_empty + pio_sm_rx, true);
224+
irq_set_enabled(pio_irqn(pio_hw_rx, PIO_IRQ_NUM), true);
225+
#endif
226+
#endif
227+
228+
// setup dma interrupt
229+
#if USE_DMA_FOR_RX || USE_DMA_FOR_TX
230+
if (irq_get_exclusive_handler(DMA_IRQN(DMA_IRQ_NUM))) {
231+
panic("DMA IRQ in use");
232+
}
233+
irq_add_shared_handler(DMA_IRQN(DMA_IRQ_NUM), dma_irq_handler, DMA_IRQ_PRIORITY);
234+
#if USE_DMA_FOR_TX
235+
dma_irqn_set_channel_enabled(DMA_IRQ_NUM, dma_channel_tx, true);
236+
#endif
237+
#if USE_DMA_FOR_RX
238+
dma_irqn_set_channel_enabled(DMA_IRQ_NUM, dma_channel_rx, true);
239+
#endif
240+
irq_set_enabled(DMA_IRQN(DMA_IRQ_NUM), true);
241+
#endif
242+
243+
// send data
244+
#if USE_DMA_FOR_TX
245+
dma_channel_wait_for_finish_blocking(dma_channel_tx); // wait for tx
246+
#elif USE_PIO_FOR_TX
247+
// write to the pio fifo
248+
int count_pio_tx = 0;
249+
while(count_pio_tx < sizeof(buffer_tx) - 1) {
250+
uart_tx_program_putc(pio_hw_tx, pio_sm_tx, buffer_tx[count_pio_tx++]);
251+
}
252+
#else
253+
uart_puts(HARD_UART_INST, buffer_tx);
254+
#endif
255+
256+
// Receive the data
257+
#if USE_DMA_FOR_RX
258+
// wait for dma rx
259+
dma_channel_wait_for_finish_blocking(dma_channel_rx);
260+
#elif USE_PIO_FOR_RX
261+
// read from the pio fifo
262+
int count_pio_rx = 0;
263+
while(count_pio_rx < sizeof(buffer_tx) - 1) {
264+
buffer_rx[count_pio_rx++] = uart_rx_program_getc(pio_hw_rx, pio_sm_rx);
265+
}
266+
#else
267+
// use the uart hardware
268+
int count_uart_rx = 0;
269+
while(count_uart_rx < sizeof(buffer_tx) - 1) {
270+
buffer_rx[count_uart_rx++] = uart_getc(HARD_UART_INST);
271+
}
272+
#endif
273+
274+
// check
275+
if (memcmp(buffer_rx, buffer_tx, sizeof(buffer_tx) - 1) == 0) {
276+
printf("Test passed\n");
277+
} else {
278+
printf("buffer_tx: >%s<\n", buffer_tx);
279+
dump_bytes(buffer_tx, sizeof(buffer_tx));
280+
printf("result: >%s<\n", buffer_rx);
281+
dump_bytes(buffer_rx, sizeof(buffer_rx));
282+
printf("Test failed\n");
283+
assert(0);
284+
}
285+
286+
// cleanup
287+
#if USE_DMA_FOR_TX
288+
dma_irqn_set_channel_enabled(DMA_IRQ_NUM, dma_channel_tx, false);
289+
dma_channel_unclaim(dma_channel_tx);
290+
#endif
291+
#if USE_DMA_FOR_RX
292+
dma_irqn_set_channel_enabled(DMA_IRQ_NUM, dma_channel_rx, false);
293+
dma_channel_unclaim(dma_channel_rx);
294+
#endif
295+
#if USE_DMA_FOR_RX || USE_DMA_FOR_TX
296+
irq_remove_handler(DMA_IRQN(DMA_IRQ_NUM), dma_irq_handler);
297+
if (!irq_has_shared_handler(DMA_IRQN(DMA_IRQ_NUM))) {
298+
irq_set_enabled(DMA_IRQN(DMA_IRQ_NUM), false);
299+
}
300+
#endif
301+
#if USE_PIO_FOR_RX
302+
pio_set_irqn_source_enabled(pio_hw_rx, PIO_IRQ_NUM, pis_sm0_rx_fifo_not_empty + pio_sm_rx, false);
303+
irq_remove_handler(pio_irqn(pio_hw_rx, PIO_IRQ_NUM), pio_irq_handler);
304+
if (!irq_has_shared_handler(pio_irqn(pio_hw_rx, PIO_IRQ_NUM))) {
305+
irq_set_enabled(pio_irqn(pio_hw_rx, PIO_IRQ_NUM), false);
306+
}
307+
pio_deinit(&uart_rx_mini_program, pio_hw_rx, pio_sm_rx, offset_rx);
308+
#endif
309+
#if USE_PIO_FOR_TX
310+
pio_deinit(&uart_tx_program, pio_hw_tx, pio_sm_tx, offset_tx);
311+
#endif
312+
#if !USE_PIO_FOR_RX || !USE_PIO_FOR_TX
313+
uart_deinit(HARD_UART_INST);
314+
#endif
315+
return 0;
316+
}

0 commit comments

Comments
 (0)