Chrispy
High-performance multichannel ADC sampling and audio recording.
Loading...
Searching...
No Matches
Timer.h
Go to the documentation of this file.
1#include <stddef.h>
2#include <stdint.h>
3
4typedef uint32_t clk_t;
5typedef uint16_t pre_t;
6
7#ifndef ssize_t
8typedef int32_t ssize_t;
9#endif
10
14enum struct TimerRc : uint8_t {
15 Okay, /* !< Operation successful. */
16 ImpossibleClock, /* !< Clock rate cannot be achieved with parameters.*/
17 ZeroDiv, /* !< Operation would cause divide by zero error. */
18 ErrorRange, /* !< Invalid range of values. */
19 TooLow, /* !< Value is too hgih for range. */
20 TooHigh, /* !< Value is too low for range. */
21};
22
30const char* error_str(TimerRc rc);
31
36enum struct Skew : uint8_t {
37 Low, /* !< Prefer a lower clock rate. */
38 High, /* !< Prefer a higher clock rate. */
39 None, /* !< No preference. */
40};
41
73 double error;
74
84 TimerConfig(clk_t src_clock, clk_t desired_clock, Skew preference)
85 : src(src_clock), desired(desired_clock), skew(preference) {}
86
102 TimerRc compute(size_t nprescalers, pre_t* prescalers, clk_t max_compare,
103 double max_error);
104
108 void pprint();
109};
110
119
123void deactivate_t1();
TimerRc
Possible return codes when trying to compute the best timer configuration.
Definition Timer.h:14
@ ImpossibleClock
int32_t ssize_t
Definition Timer.h:8
TimerRc activate_t1(TimerConfig &cfg)
Activate the 16-bit timer 1 on ATMEGA2560 with a given configuration.
Definition Timer.cpp:88
uint16_t pre_t
Definition Timer.h:5
uint32_t clk_t
Definition Timer.h:4
const char * error_str(TimerRc rc)
Retrieve a string description of a timer return code.
Definition Timer.cpp:183
Skew
Preference for how the computed timer configuration should skew if it cannot get the precise value.
Definition Timer.h:36
void deactivate_t1()
Deactivate 16-bit timer 1 on ATMEGA2560.
Definition Timer.cpp:96
Configuration for a hardware timer.
Definition Timer.h:45
clk_t src
Input clock frequency (Hz).
Definition Timer.h:57
pre_t prescaler
Prescaler value to use with timer.
Definition Timer.h:49
clk_t compare
Comparison value to trigger timer at.
Definition Timer.h:53
double error
Percentage error from desired clock rate.
Definition Timer.h:73
void pprint()
Pretty print the timer configuration.
Definition Timer.cpp:168
TimerConfig(clk_t src_clock, clk_t desired_clock, Skew preference)
Constructor for a timer config which initializes required variables for a configuration but does not ...
Definition Timer.h:84
clk_t actual
Actual clock rate achieved.
Definition Timer.h:69
clk_t desired
Target clock frequency (Hz) given input.
Definition Timer.h:61
TimerRc compute(size_t nprescalers, pre_t *prescalers, clk_t max_compare, double max_error)
Compute the first timer configuration satisfying the error constraint, or find the best possible time...
Definition Timer.cpp:98
Skew skew
Preference for whether a lower/high clock rate.
Definition Timer.h:65