Skip to content

Commit a99a787

Browse files
bessmanAlexander Bessman
authored andcommitted
Split LA channel setup into helper function
1 parent 9c9a813 commit a99a787

File tree

1 file changed

+48
-19
lines changed

1 file changed

+48
-19
lines changed

src/instruments/logic_analyzer.c

Lines changed: 48 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,20 @@ static void cn_callback(Channel channel);
115115
*/
116116
static void cleanup_callback(Channel channel);
117117

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+
118132
/**
119133
* @brief Capture logic level changes on on LA1-4
120134
*
@@ -234,6 +248,36 @@ static void reset(void)
234248
g_n_channels = 0;
235249
}
236250

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+
237281
static enum Status capture(
238282
uint8_t const n_channels,
239283
uint16_t const events,
@@ -255,26 +299,11 @@ static enum Status capture(
255299
status = TMR_set_period(g_TIMER, 1);
256300
if (status) { goto error; }
257301

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; }
277305

306+
// Enable interrupt.
278307
status = configure_trigger(edge, trigger_pin);
279308
if (status) { goto error; }
280309

0 commit comments

Comments
 (0)