@@ -104,49 +104,53 @@ 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
}
112
112
uart_rx_mini_program_init (pio_hw_rx , pio_sm_rx , offset_rx , GPIO_RX , SERIAL_BAUD );
113
113
#else
114
+ // setup the rx gpio for the uart hardware
114
115
gpio_set_function (GPIO_RX , GPIO_FUNC_UART );
115
116
#endif
116
117
117
- // setup pio for tx
118
118
#if USE_PIO_FOR_TX
119
+ // setup pio for tx
119
120
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
121
panic ("failed to allocate pio for tx" );
121
122
}
122
123
uart_tx_program_init (pio_hw_tx , pio_sm_tx , offset_tx , GPIO_TX , SERIAL_BAUD );
123
124
#else
125
+ // setup the tx gpio for the uart hardware
124
126
gpio_set_function (GPIO_TX , GPIO_FUNC_UART );
125
127
#endif
126
128
127
- // setup pio interrupt
128
129
#if USE_PIO_FOR_RX
130
+ // check the pio irq is available
129
131
if (irq_get_exclusive_handler (pio_get_irq_num (pio_hw_rx , PIO_IRQ_TO_USE ))) {
130
132
panic ("PIO IRQ in use" );
131
133
}
132
134
#if USE_DMA_FOR_RX
135
+ // add a shared pio handler
133
136
irq_add_shared_handler (pio_get_irq_num (pio_hw_rx , PIO_IRQ_TO_USE ), pio_irq_handler , PIO_IRQ_PRIORITY );
134
137
pio_set_irqn_source_enabled (pio_hw_rx , PIO_IRQ_TO_USE , pis_sm0_rx_fifo_not_empty + pio_sm_rx , true);
135
138
irq_set_enabled (pio_get_irq_num (pio_hw_rx , PIO_IRQ_TO_USE ), true);
136
139
#endif
137
140
#endif
138
141
139
- // add dma handler
140
142
#if USE_DMA_FOR_RX || USE_DMA_FOR_TX
143
+ // check the dma irq is available
141
144
if (irq_get_exclusive_handler (dma_get_irq_num (DMA_IRQ_TO_USE ))) {
142
145
panic ("DMA IRQ in use" );
143
146
}
147
+ // add a shared dma handler
144
148
irq_add_shared_handler (dma_get_irq_num (DMA_IRQ_TO_USE ), dma_irq_handler , DMA_IRQ_PRIORITY );
145
149
irq_set_enabled (dma_get_irq_num (DMA_IRQ_TO_USE ), true);
146
150
#endif
147
151
148
- // Setup dma for read
149
152
#if USE_DMA_FOR_RX
153
+ // Setup dma for read
150
154
dma_channel_rx = dma_claim_unused_channel (false);
151
155
if (dma_channel_rx < 0 ) {
152
156
panic ("No free dma channels" );
@@ -159,19 +163,19 @@ int main()
159
163
// enable irq for rx
160
164
dma_irqn_set_channel_enabled (DMA_IRQ_TO_USE , dma_channel_rx , true);
161
165
#if USE_PIO_FOR_RX
162
- // read from pio fifo
166
+ // setup dma to read from pio fifo
163
167
channel_config_set_dreq (& config_rx , pio_get_dreq (pio_hw_rx , pio_sm_rx , false));
164
168
// 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
169
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
170
#else
167
- // read from uart hardware
171
+ // setup dma to read from uart hardware
168
172
channel_config_set_dreq (& config_rx , uart_get_dreq (HARD_UART_INST , false));
169
173
dma_channel_configure (dma_channel_rx , & config_rx , buffer_rx , & uart_get_hw (HARD_UART_INST )-> dr , read_size , true); // dma started
170
174
#endif
171
175
#endif
172
176
173
- // setup dma for write
174
177
#if USE_DMA_FOR_TX
178
+ // setup dma for write
175
179
dma_channel_tx = dma_claim_unused_channel (false);
176
180
if (dma_channel_tx < 0 ) {
177
181
panic ("No free dma channels" );
@@ -183,32 +187,32 @@ int main()
183
187
// enable irq for tx
184
188
dma_irqn_set_channel_enabled (DMA_IRQ_TO_USE , dma_channel_tx , true);
185
189
#if USE_PIO_FOR_RX
186
- // write to pio fifo
190
+ // setup dma to write to pio fifo
187
191
channel_config_set_dreq (& config_tx , pio_get_dreq (pio_hw_tx , pio_sm_tx , true));
188
192
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
193
#else
190
- // write to uart hardware
194
+ // setup dma to write to uart hardware
191
195
channel_config_set_dreq (& config_tx , uart_get_dreq (HARD_UART_INST , true));
192
196
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
197
#endif
194
198
#endif
195
199
196
- // send data
197
200
#if USE_DMA_FOR_TX
198
- dma_channel_wait_for_finish_blocking (dma_channel_tx ); // wait for tx
201
+ // Just wait for dma tx to finish
202
+ dma_channel_wait_for_finish_blocking (dma_channel_tx );
199
203
#elif USE_PIO_FOR_TX
200
204
// write to the pio fifo
201
205
int count_pio_tx = 0 ;
202
206
while (count_pio_tx < sizeof (buffer_tx ) - 1 ) {
203
207
uart_tx_program_putc (pio_hw_tx , pio_sm_tx , buffer_tx [count_pio_tx ++ ]);
204
208
}
205
209
#else
210
+ // write to the uart
206
211
uart_puts (HARD_UART_INST , buffer_tx );
207
212
#endif
208
213
209
- // Receive the data
210
214
#if USE_DMA_FOR_RX
211
- // wait for dma rx
215
+ // Just wait for dma rx to finish
212
216
dma_channel_wait_for_finish_blocking (dma_channel_rx );
213
217
#elif USE_PIO_FOR_RX
214
218
// read from the pio fifo
@@ -217,14 +221,14 @@ int main()
217
221
buffer_rx [count_pio_rx ++ ] = uart_rx_program_getc (pio_hw_rx , pio_sm_rx );
218
222
}
219
223
#else
220
- // use the uart hardware
224
+ // read from the uart
221
225
int count_uart_rx = 0 ;
222
226
while (count_uart_rx < sizeof (buffer_tx ) - 1 ) {
223
227
buffer_rx [count_uart_rx ++ ] = uart_getc (HARD_UART_INST );
224
228
}
225
229
#endif
226
230
227
- // check
231
+ // check the buffer we received
228
232
if (memcmp (buffer_rx , buffer_tx , sizeof (buffer_tx ) - 1 ) == 0 ) {
229
233
printf ("Test passed\n" );
230
234
} else {
0 commit comments