@@ -115,6 +115,20 @@ static void cn_callback(Channel channel);
115
115
*/
116
116
static void cleanup_callback (Channel channel );
117
117
118
+ /**
119
+ * @brief Set the up DMA and IC for active channels
120
+ *
121
+ * @param n_channels
122
+ * @param events
123
+ * @param edge
124
+ * @return enum Status
125
+ */
126
+ static enum Status setup_channels (
127
+ uint8_t const n_channels ,
128
+ uint16_t const events ,
129
+ Edge const edge
130
+ );
131
+
118
132
/**
119
133
* @brief Capture logic level changes on on LA1-4
120
134
*
@@ -234,6 +248,36 @@ static void reset(void)
234
248
g_n_channels = 0 ;
235
249
}
236
250
251
+ static enum Status setup_channels (
252
+ uint8_t const n_channels ,
253
+ uint16_t const events ,
254
+ Edge const edge
255
+ ) {
256
+ enum Status status ;
257
+
258
+ for (Channel i = CHANNEL_1 ; i <= n_channels ; ++ i ) {
259
+ uint16_t * address = g_buffer + i * events ;
260
+ status = DMA_setup (i , events , (size_t )address , DMA_SOURCE_IC );
261
+ if (status ) { return status ; }
262
+
263
+ /* DMA interrupt is enabled here, but the DMA transfer itself is
264
+ * started in trigger callback. */
265
+ status = DMA_interrupt_enable (i , cleanup_callback );
266
+ if (status ) { return status ; }
267
+
268
+ /* IC is started here. IC will now begin copying the value of ICxTMR to
269
+ * ICxBUF whenever an event occurs. Until the trigger event starts the
270
+ * clock source, ICxTMR will be held at zero. This is not a problem,
271
+ * because although zeros will be copied to ICxBUF, they won't be
272
+ * copied to the buffer until DMA is started by the trigger callback.
273
+ */
274
+ status = IC_start (i , edge , timer2ictsel (g_TIMER ));
275
+ if (status ) { return status ; }
276
+ }
277
+
278
+ return E_OK ;
279
+ }
280
+
237
281
static enum Status capture (
238
282
uint8_t const n_channels ,
239
283
uint16_t const events ,
@@ -255,26 +299,11 @@ static enum Status capture(
255
299
status = TMR_set_period (g_TIMER , 1 );
256
300
if (status ) { goto error ; }
257
301
258
- for (Channel i = CHANNEL_1 ; i <= n_channels ; ++ i ) {
259
- uint16_t * address = g_buffer + i * events ;
260
- status = DMA_setup (i , events , (size_t )address , DMA_SOURCE_IC );
261
- if (status ) { goto error ; }
262
-
263
- /* DMA interrupt is enabled here, but the DMA transfer itself is
264
- * started in trigger callback. */
265
- status = DMA_interrupt_enable (i , cleanup_callback );
266
- if (status ) { goto error ; }
267
-
268
- /* IC is started here. IC will now begin copying the value of ICxTMR to
269
- * ICxBUF whenever an event occurs. Until the trigger event starts the
270
- * clock source, ICxTMR will be held at zero. This is not a problem,
271
- * because although zeros will be copied to ICxBUF, they won't be
272
- * copied to the buffer until DMA is started by the trigger callback.
273
- */
274
- status = IC_start (i , edge , timer2ictsel (g_TIMER ));
275
- if (status ) { goto error ; }
276
- }
302
+ // Set up DMA and IC.
303
+ status = setup_channels (n_channels , events , edge );
304
+ if (status ) { goto error ; }
277
305
306
+ // Enable interrupt.
278
307
status = configure_trigger (edge , trigger_pin );
279
308
if (status ) { goto error ; }
280
309
0 commit comments