timers.h: Return error codes instead of true/false for failures
This commit is contained in:
parent
f993e51250
commit
ad59322364
2 changed files with 33 additions and 11 deletions
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#define _ESP_TIMER_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <errno.h>
|
||||
#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
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue