timers.h: Return error codes instead of true/false for failures

This commit is contained in:
Angus Gratton 2015-11-28 16:23:06 +11:00
parent 336bad573d
commit 2887896c76
2 changed files with 33 additions and 11 deletions

View file

@ -78,7 +78,7 @@ uint32_t timer_time_to_count(const timer_frc_t frc, uint32_t us, const timer_clk
} }
} }
bool timer_set_frequency(const timer_frc_t frc, uint32_t freq) int timer_set_frequency(const timer_frc_t frc, uint32_t freq)
{ {
uint32_t counts = 0; uint32_t counts = 0;
timer_clkdiv_t div = timer_freq_to_div(freq); timer_clkdiv_t div = timer_freq_to_div(freq);
@ -86,8 +86,7 @@ bool timer_set_frequency(const timer_frc_t frc, uint32_t freq)
counts = timer_freq_to_count(frc, freq, div); counts = timer_freq_to_count(frc, freq, div);
if(counts == 0) if(counts == 0)
{ {
printf("ABORT: No counter for timer %u frequency %u\r\n", frc, freq); return -EINVAL;
abort();
} }
timer_set_divider(frc, div); timer_set_divider(frc, div);
@ -101,17 +100,17 @@ bool timer_set_frequency(const timer_frc_t frc, uint32_t freq)
/* assume that if this overflows it'll wrap, so we'll get desired behaviour */ /* assume that if this overflows it'll wrap, so we'll get desired behaviour */
TIMER(1).ALARM = counts + TIMER(1).COUNT; TIMER(1).ALARM = counts + TIMER(1).COUNT;
} }
return true; return 0;
} }
bool timer_set_timeout(const timer_frc_t frc, uint32_t us) int _timer_set_timeout_impl(const timer_frc_t frc, uint32_t us)
{ {
uint32_t counts = 0; uint32_t counts = 0;
timer_clkdiv_t div = timer_time_to_div(us); timer_clkdiv_t div = timer_time_to_div(us);
counts = timer_time_to_count(frc, us, div); counts = timer_time_to_count(frc, us, div);
if(counts == 0) if(counts == 0)
return false; /* can't set frequency */ return -EINVAL; /* can't set frequency */
timer_set_divider(frc, div); timer_set_divider(frc, div);
if(frc == FRC1) if(frc == FRC1)
@ -123,5 +122,27 @@ bool timer_set_timeout(const timer_frc_t frc, uint32_t us)
TIMER(1).ALARM = counts + TIMER(1).COUNT; TIMER(1).ALARM = counts + TIMER(1).COUNT;
} }
return true; return 0;
}
int timer_set_timeout(const timer_frc_t frc, uint32_t us)
{
uint32_t counts = 0;
timer_clkdiv_t div = timer_time_to_div(us);
counts = timer_time_to_count(frc, us, div);
if(counts == 0)
return -EINVAL; /* can't set frequency */
timer_set_divider(frc, div);
if(frc == FRC1)
{
timer_set_load(frc, counts);
}
else /* FRC2 */
{
TIMER(1).ALARM = counts + TIMER(1).COUNT;
}
return 0;
} }

View file

@ -10,6 +10,7 @@
#define _ESP_TIMER_H #define _ESP_TIMER_H
#include <stdbool.h> #include <stdbool.h>
#include <errno.h>
#include "esp/timer_regs.h" #include "esp/timer_regs.h"
#include "esp/interrupts.h" #include "esp/interrupts.h"
@ -159,9 +160,9 @@ uint32_t timer_time_to_count(const timer_frc_t frc, uint32_t us, const timer_clk
Does not start/stop the timer, you have to do this manually via Does not start/stop the timer, you have to do this manually via
timer_set_run. timer_set_run.
Returns true on success, false if given frequency could not be set. Returns 0 on success, or -EINVAL if given frequency could not be set.
*/ */
bool timer_set_frequency(const timer_frc_t frc, uint32_t freq); int timer_set_frequency(const timer_frc_t frc, uint32_t freq);
/* Sets the timer for a oneshot interrupt in 'us' microseconds. /* Sets the timer for a oneshot interrupt in 'us' microseconds.
@ -176,9 +177,9 @@ bool timer_set_frequency(const timer_frc_t frc, uint32_t freq);
also disable FRC1 in the timer interrupt handler by calling also disable FRC1 in the timer interrupt handler by calling
timer_set_run(TIMER_FRC1, false); timer_set_run(TIMER_FRC1, false);
Returns true if the timeout was successfully set. Returns 0 on success, or -EINVAL if the given timeout could not be set.
*/ */
bool timer_set_timeout(const timer_frc_t frc, uint32_t us); int timer_set_timeout(const timer_frc_t frc, uint32_t us);
#ifdef __cplusplus #ifdef __cplusplus
} }