Skip to content

Commit 7ec8a80

Browse files
bessmanAlexander Bessman
authored andcommitted
Don't TRY
1 parent 8651534 commit 7ec8a80

File tree

4 files changed

+42
-42
lines changed

4 files changed

+42
-42
lines changed

src/instruments/logic_analyzer.c

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -234,14 +234,6 @@ static void reset(void)
234234
g_n_channels = 0;
235235
}
236236

237-
static enum Status setup_buffer(uint16_t const n_items)
238-
{
239-
g_buffer = malloc(n_items * sizeof(*g_buffer));
240-
if (!g_buffer) { return E_MEMORY_INSUFFICIENT; }
241-
g_buffer_n_items = n_items;
242-
return E_OK;
243-
}
244-
245237
static enum Status capture(
246238
uint8_t const n_channels,
247239
uint16_t const events,
@@ -251,28 +243,41 @@ static enum Status capture(
251243
if (g_buffer) { return E_RESOURCE_BUSY; }
252244

253245
enum Status status = E_OK;
254-
TRY(setup_buffer(n_channels * events));
246+
247+
uint16_t const n_items = n_channels * events;
248+
g_buffer = malloc(n_items * sizeof(*g_buffer));
249+
if (!g_buffer) { return E_MEMORY_INSUFFICIENT; }
250+
251+
g_buffer_n_items = n_items;
255252
g_n_channels = n_channels;
256253

257254
// Set timer period to one to send sync signal immediately on timer start.
258-
TRY(TMR_set_period(g_TIMER, 1));
255+
status = TMR_set_period(g_TIMER, 1);
256+
if (status) { goto error; }
259257

260258
for (Channel i = CHANNEL_1; i < n_channels; ++i) {
261259
uint16_t *address = g_buffer + i * events;
262-
TRY(DMA_setup(i, events, (size_t)address, DMA_SOURCE_IC));
260+
status = DMA_setup(i, events, (size_t)address, DMA_SOURCE_IC);
261+
if (status) { goto error; }
262+
263263
/* DMA interrupt is enabled here, but the DMA transfer itself is
264264
* started in trigger callback. */
265-
TRY(DMA_interrupt_enable(i, cleanup_callback));
265+
status = DMA_interrupt_enable(i, cleanup_callback);
266+
if (status) { goto error; }
267+
266268
/* IC is started here. IC will now begin copying the value of ICxTMR to
267269
* ICxBUF whenever an event occurs. Until the trigger event starts the
268270
* clock source, ICxTMR will be held at zero. This is not a problem,
269271
* because although zeros will be copied to ICxBUF, they won't be
270272
* copied to the buffer until DMA is started by the trigger callback.
271273
*/
272-
TRY(IC_start(i, edge, timer2ictsel(g_TIMER)));
274+
status = IC_start(i, edge, timer2ictsel(g_TIMER));
275+
if (status) { goto error; }
273276
}
274277

275-
TRY(configure_trigger(edge, trigger_pin));
278+
status = configure_trigger(edge, trigger_pin);
279+
if (status) { goto error; }
280+
276281
return status;
277282

278283
error:
@@ -293,11 +298,16 @@ static enum Status configure_trigger(Edge const edge, Channel const trigger_pin)
293298
if (edge == EDGE_ANY) {
294299
/* Input capture cannot interrupt on both falling and rising edge, only
295300
* one or the other. Must use Change Notification instead. */
296-
TRY(CN_pin_enable(trigger_pin));
297-
TRY(CN_interrupt_enable(cn_callback));
301+
status = CN_pin_enable(trigger_pin);
302+
if (status) { goto error; }
303+
304+
status = CN_interrupt_enable(cn_callback);
305+
if (status) { goto error; }
298306
}
299307

300-
TRY(IC_interrupt_enable(trigger_pin, ic_callback));
308+
status = IC_interrupt_enable(trigger_pin, ic_callback);
309+
if (status) { goto error; }
310+
301311
return status;
302312

303313
error:

src/registers_ng/cn.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ enum Status CN_pin_enable(Channel const channel)
186186
enum Status CN_pin_disable(Channel const channel)
187187
{
188188
if (!CHANNEL_check(channel)) { return E_BAD_ARGUMENT; }
189-
pin_disable(~g_CHANNEL_PIN_MAP[channel]);
189+
pin_disable(g_CHANNEL_PIN_MAP[channel]);
190190
return E_OK;
191191
}
192192

src/registers_ng/tmr.c

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,9 @@ static inline void set_prescaler(
139139
enum Status TMR_reset(TMR_Timer const tmr)
140140
{
141141
enum Status status = E_OK;
142-
TRY(check_timer(tmr));
142+
143+
status = check_timer(tmr);
144+
if (status) { return status; }
143145

144146
struct TMRConf {
145147
struct TMRCONbits conbits;
@@ -164,9 +166,6 @@ enum Status TMR_reset(TMR_Timer const tmr)
164166
}
165167

166168
return status;
167-
168-
error:
169-
return status;
170169
}
171170

172171
enum Status TMR_get_period_prescaler(
@@ -201,34 +200,32 @@ enum Status TMR_set_prescaler(
201200
enum TMR_Prescaler const tckps
202201
) {
203202
enum Status status = E_OK;
204-
TRY(check_timer(tmr));
205-
TRY(tckps < TMR_N_PRESCALERS ? E_OK : E_BAD_ARGUMENT);
206-
set_prescaler(tmr, tckps);
207-
return status;
203+
status = check_timer(tmr);
204+
if (status) { return status; }
205+
206+
if (tckps >= TMR_N_PRESCALERS) { return E_BAD_ARGUMENT; }
208207

209-
error:
208+
set_prescaler(tmr, tckps);
210209
return status;
211210
}
212211

213212
enum Status TMR_set_period(TMR_Timer const tmr, uint16_t const pr)
214213
{
215214
enum Status status = E_OK;
216-
TRY(check_timer(tmr));
217-
set_period(tmr, pr);
218-
return status;
215+
status = check_timer(tmr);
216+
if (status) { return status; }
219217

220-
error:
218+
set_period(tmr, pr);
221219
return status;
222220
}
223221

224222

225223
enum Status TMR_start(TMR_Timer const tmr)
226224
{
227225
enum Status status = E_OK;
228-
TRY(check_timer(tmr));
229-
g_TMR_REGS[tmr].p_conbits->TON = 1;
230-
return status;
226+
status = check_timer(tmr);
227+
if (status) { return status; }
231228

232-
error:
229+
g_TMR_REGS[tmr].p_conbits->TON = 1;
233230
return status;
234231
}

src/types.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,6 @@
77
#ifndef PSLAB_TYPES_H
88
#define PSLAB_TYPES_H
99

10-
#define TRY(x) do { \
11-
status = (x); \
12-
if (status != E_OK) { \
13-
goto error; \
14-
} \
15-
} while(0)
16-
1710
/** Function return codes. */
1811
enum Status {
1912
// No error.

0 commit comments

Comments
 (0)