NetBurner 3.5.7
PDF Version
HiResTimer.h
1/*NB_REVISION*/
2
3/*NB_COPYRIGHT*/
4
5/* Select the card type */
6#ifndef _HIRESTIMER_H
7#define _HIRESTIMER_H
8
9#include <limits.h>
10#include <nbrtos.h>
11#include <sim.h>
12#include <stdio.h>
13
14#define LOGME \
15 iprintf("We got to line %d in file %s\r\n", __LINE__, __FILE__); \
16 OSTimeDly(2);
17
18#define DEFAULT_TIMER -1
19#define TIMER_COUNT 4
20#define TIMER_RELEASED 2
21
22// Clock speed macros
23extern unsigned long CPU_CLOCK;
24#define NB_CPU_CLK (CPU_CLOCK / 2)
25
26#define TIME_PER_CLK (1.0 / NB_CPU_CLK)
27
28class HiResTimer
29{
30 static HiResTimer timers[TIMER_COUNT];
31 static uint8_t timerCreated[TIMER_COUNT];
32
33 int timer_; // Timer number
34 volatile timerstruct *simTimer_; // Timer struct from sim.h
35 volatile int prescaler_; // The value of the hardware prescaler
36 volatile uint32_t resetCount_; // Holds the count of the timer resets
37 OS_SEM delaySem_; // OS semaphore struct
38 uint32_t firstInit_; // Have we set up this timer before ?
39 volatile uint32_t delayCalled_; // Is the timer being used for a delay?
40
41 // The method that will be called when the timer triggers an interupt
42 void (*interruptFunction_)();
43
44 // Disable construction and copy construction
45 HiResTimer(){};
46 HiResTimer(const HiResTimer &from){};
47 HiResTimer &operator=(const HiResTimer &from);
48 // Only actual constructor
49 HiResTimer(int timer);
50 ~HiResTimer(){};
51
52 // Fire the object's interrupt method
53 void fireInterrupt();
54
55 public:
56 // class function to fire the interrupt function for the given timer
57 // called by the Interrupt macro routine
58 static void fireInterrupt(int timer);
59
60 void init(double InterruptTime = 0); // Initializes the timer for use
61 void init_ticks(uint32_t ReferenceTicks, int prescale = 0);
62
63 /*
64 * Starts the selected timer. The user MUST call HiResTimerInit for the given timer
65 * before calling this method.
66 */
67 inline void start()
68 {
69 // Setting the CLK bits to 01 on the DTMR register, starting the timer
70#ifdef MOD5213
71 simTimer_->dtmr |= 0x2;
72#else
73 simTimer_->tmr |= 0x2;
74#endif
75 };
76
77 inline uint32_t readHigh() { return resetCount_; }; // Reads the number of timer overflows/resets/interrupts
78 inline uint32_t readLow()
79 {
80 // Reads the timer register value
81#ifdef MOD5213
82 return simTimer_->dtcn;
83#else
84 return simTimer_->tcn;
85#endif
86 };
87 inline int getPrescaler() { return prescaler_; }; // Returns the value of the prescaler
88 double readTime(); // Ouputs the number of seconds since the timer was started
89
90 inline void stop()
91 {
92 // Stops the timer
93 simTimer_->tmr &= ~(0x6);
94 };
95
96 inline void stopClear()
97 {
98 simTimer_->tmr &= ~(0x6);
99 simTimer_->tcn = 0x0;
100 simTimer_->ter = 0x3;
101 resetCount_ = 0;
102 }
103
104 void delay(
105 double DelayTime); // Starts a precise delay that will switch tasks while waiting (Do not use for shorter than ~150 useconds)
106 void delay_uSec(uint32_t DelayTime); // Starts a precise delay that will switch tasks while waiting
107 void pollingDelay(double DelayTime); // Starts a precise delay that will busy wait
108 inline void pollingDelay_uSec(uint32_t delayTime)
109 {
110 if (delayTime < (INT_MAX / NB_CPU_CLK)) { pollingDelay_refTicks(delayTime * NB_CPU_CLK / 1000000UL); }
111 else
112 {
113 pollingDelay_refTicks(delayTime >> 2 * (NB_CPU_CLK / 250000UL));
114 }
115 }
116 void pollingDelay_refTicks(uint32_t ticks);
117
118 void clockGenerator(double Frequency); // Set up a clock at the chosen frequency
119
120 inline void setInterruptFunction(void (*interruptFunction)())
121 {
122 interruptFunction_ = interruptFunction;
123 }; // Set the method to be called on interrupt triggers
124 inline void clearInterruptFunction() { interruptFunction_ = NULL; }; // Clear the interrupt method
125
126 char *toString(); // Converts the timer object to a printable c string
127
128 // Returns a pointer for the given timer number, returns NULL if the timer does not exist
129 static HiResTimer *getHiResTimer(int timer = DEFAULT_TIMER);
130 void releaseTimer();
131
132 int getTimerNumber() { return timer_; }
133};
134
135#endif /* _HIRESTIMER_H */
void init()
System initialization. Ideally called at the beginning of all applications, since the easiest Recover...
Semaphores are used to control access to shared resources or or to communicate between tasks in a mul...
Definition nbrtos.h:407