From ad593223648987a0f024fa025c5b3917e42bb413 Mon Sep 17 00:00:00 2001 From: Angus Gratton Date: Sat, 28 Nov 2015 16:23:06 +1100 Subject: [PATCH] timers.h: Return error codes instead of true/false for failures --- core/esp_timer.c | 35 ++++++++++++++++++++++++++++------- core/include/esp/timer.h | 9 +++++---- 2 files changed, 33 insertions(+), 11 deletions(-) diff --git a/core/esp_timer.c b/core/esp_timer.c index c2b45a0..9282e26 100644 --- a/core/esp_timer.c +++ b/core/esp_timer.c @@ -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; 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); if(counts == 0) { - printf("ABORT: No counter for timer %u frequency %u\r\n", frc, freq); - abort(); + return -EINVAL; } 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 */ 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; timer_clkdiv_t div = timer_time_to_div(us); counts = timer_time_to_count(frc, us, div); if(counts == 0) - return false; /* can't set frequency */ + return -EINVAL; /* can't set frequency */ timer_set_divider(frc, div); 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; } - 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; } diff --git a/core/include/esp/timer.h b/core/include/esp/timer.h index fc4e6f8..fc74992 100644 --- a/core/include/esp/timer.h +++ b/core/include/esp/timer.h @@ -10,6 +10,7 @@ #define _ESP_TIMER_H #include +#include #include "esp/timer_regs.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 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. @@ -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 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 }