@@ -104,8 +104,8 @@ int main()
104
104
uart_init (HARD_UART_INST , SERIAL_BAUD );
105
105
#endif
106
106
107
- // setup pio for rx
108
107
#if USE_PIO_FOR_RX
108
+ // setup pio for rx
109
109
if (!pio_claim_free_sm_and_add_program_for_gpio_range (& uart_rx_mini_program , & pio_hw_rx , & pio_sm_rx , & offset_rx , GPIO_RX , 1 , true)) {
110
110
panic ("failed to allocate pio for rx" );
111
111
}
@@ -114,8 +114,8 @@ int main()
114
114
gpio_set_function (GPIO_RX , GPIO_FUNC_UART );
115
115
#endif
116
116
117
- // setup pio for tx
118
117
#if USE_PIO_FOR_TX
118
+ // setup pio for tx
119
119
if (!pio_claim_free_sm_and_add_program_for_gpio_range (& uart_tx_program , & pio_hw_tx , & pio_sm_tx , & offset_tx , GPIO_TX , 1 , true)) {
120
120
panic ("failed to allocate pio for tx" );
121
121
}
@@ -124,8 +124,8 @@ int main()
124
124
gpio_set_function (GPIO_TX , GPIO_FUNC_UART );
125
125
#endif
126
126
127
- // setup pio interrupt
128
127
#if USE_PIO_FOR_RX
128
+ // setup pio interrupt
129
129
if (irq_get_exclusive_handler (pio_get_irq_num (pio_hw_rx , PIO_IRQ_TO_USE ))) {
130
130
panic ("PIO IRQ in use" );
131
131
}
@@ -136,17 +136,17 @@ int main()
136
136
#endif
137
137
#endif
138
138
139
- // add dma handler
140
139
#if USE_DMA_FOR_RX || USE_DMA_FOR_TX
140
+ // add dma handler
141
141
if (irq_get_exclusive_handler (dma_get_irq_num (DMA_IRQ_TO_USE ))) {
142
142
panic ("DMA IRQ in use" );
143
143
}
144
144
irq_add_shared_handler (dma_get_irq_num (DMA_IRQ_TO_USE ), dma_irq_handler , DMA_IRQ_PRIORITY );
145
145
irq_set_enabled (dma_get_irq_num (DMA_IRQ_TO_USE ), true);
146
146
#endif
147
147
148
- // Setup dma for read
149
148
#if USE_DMA_FOR_RX
149
+ // Setup dma for read
150
150
dma_channel_rx = dma_claim_unused_channel (false);
151
151
if (dma_channel_rx < 0 ) {
152
152
panic ("No free dma channels" );
@@ -159,19 +159,19 @@ int main()
159
159
// enable irq for rx
160
160
dma_irqn_set_channel_enabled (DMA_IRQ_TO_USE , dma_channel_rx , true);
161
161
#if USE_PIO_FOR_RX
162
- // read from pio fifo
162
+ // setup dma to read from pio fifo
163
163
channel_config_set_dreq (& config_rx , pio_get_dreq (pio_hw_rx , pio_sm_rx , false));
164
164
// 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!
165
165
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
166
166
#else
167
- // read from uart hardware
167
+ // setup dma to read from uart hardware
168
168
channel_config_set_dreq (& config_rx , uart_get_dreq (HARD_UART_INST , false));
169
169
dma_channel_configure (dma_channel_rx , & config_rx , buffer_rx , & uart_get_hw (HARD_UART_INST )-> dr , read_size , true); // dma started
170
170
#endif
171
171
#endif
172
172
173
- // setup dma for write
174
173
#if USE_DMA_FOR_TX
174
+ // setup dma for write
175
175
dma_channel_tx = dma_claim_unused_channel (false);
176
176
if (dma_channel_tx < 0 ) {
177
177
panic ("No free dma channels" );
@@ -183,32 +183,32 @@ int main()
183
183
// enable irq for tx
184
184
dma_irqn_set_channel_enabled (DMA_IRQ_TO_USE , dma_channel_tx , true);
185
185
#if USE_PIO_FOR_RX
186
- // write to pio fifo
186
+ // setup dma to write to pio fifo
187
187
channel_config_set_dreq (& config_tx , pio_get_dreq (pio_hw_tx , pio_sm_tx , true));
188
188
dma_channel_configure (dma_channel_tx , & config_tx , & pio_hw_rx -> txf [pio_sm_tx ], buffer_tx , sizeof (buffer_tx ) - 1 , true); // dma started
189
189
#else
190
- // write to uart hardware
190
+ // setup dma to write to uart hardware
191
191
channel_config_set_dreq (& config_tx , uart_get_dreq (HARD_UART_INST , true));
192
192
dma_channel_configure (dma_channel_tx , & config_tx , & uart_get_hw (HARD_UART_INST )-> dr , buffer_tx , sizeof (buffer_tx ) - 1 , true); // dma started
193
193
#endif
194
194
#endif
195
195
196
- // send data
197
196
#if USE_DMA_FOR_TX
198
- dma_channel_wait_for_finish_blocking (dma_channel_tx ); // wait for tx
197
+ // Just wait for dma tx to finish
198
+ dma_channel_wait_for_finish_blocking (dma_channel_tx );
199
199
#elif USE_PIO_FOR_TX
200
200
// write to the pio fifo
201
201
int count_pio_tx = 0 ;
202
202
while (count_pio_tx < sizeof (buffer_tx ) - 1 ) {
203
203
uart_tx_program_putc (pio_hw_tx , pio_sm_tx , buffer_tx [count_pio_tx ++ ]);
204
204
}
205
205
#else
206
+ // write to the uart
206
207
uart_puts (HARD_UART_INST , buffer_tx );
207
208
#endif
208
209
209
- // Receive the data
210
210
#if USE_DMA_FOR_RX
211
- // wait for dma rx
211
+ // Just wait for dma rx to finish
212
212
dma_channel_wait_for_finish_blocking (dma_channel_rx );
213
213
#elif USE_PIO_FOR_RX
214
214
// read from the pio fifo
@@ -217,7 +217,7 @@ int main()
217
217
buffer_rx [count_pio_rx ++ ] = uart_rx_program_getc (pio_hw_rx , pio_sm_rx );
218
218
}
219
219
#else
220
- // use the uart hardware
220
+ // read from the uart
221
221
int count_uart_rx = 0 ;
222
222
while (count_uart_rx < sizeof (buffer_tx ) - 1 ) {
223
223
buffer_rx [count_uart_rx ++ ] = uart_getc (HARD_UART_INST );
0 commit comments