COSA
An Object-Oriented Platform for Arduino Programming
RTT.hh
Go to the documentation of this file.
1 
21 #ifndef COSA_RTT_HH
22 #define COSA_RTT_HH
23 
24 #include "Cosa/Types.h"
25 #include "Cosa/Job.hh"
26 #include "Cosa/Clock.hh"
27 
37 class RTT {
38 public:
43  static bool begin();
44 
49  static bool end();
50 
55  static uint16_t us_per_tick();
56 
61  static uint16_t us_per_timer_cycle();
62 
67  static uint32_t micros();
68 
74  static void micros(uint32_t usec)
75  {
76  synchronized {
77  s_micros = usec;
78  s_millis = usec / 1000L;
79  }
80  }
81 
86  static uint32_t millis();
87 
93  static void millis(uint32_t ms)
94  {
95  synchronized {
96  s_micros = ms * 1000L;
97  s_millis = ms;
98  }
99  }
100 
107  static uint32_t since(uint32_t start)
108  __attribute__((always_inline))
109  {
110  uint32_t now = millis();
111  return (now - start);
112  }
113 
119  static void delay(uint32_t ms);
120 
124  static void await()
125  __attribute__((always_inline))
126  {
127  delay(0);
128  }
129 
133  class Scheduler : public Job::Scheduler {
134  public:
140  {
141  RTT::s_scheduler = this;
142  }
143 
150  virtual bool start(Job* job);
151 
156  virtual void dispatch();
157 
162  virtual uint32_t time();
163  };
164 
170  {
171  s_scheduler = scheduler;
172  }
173 
179  {
180  return (s_scheduler);
181  }
182 
186  class Clock : public ::Clock {
187  public:
191  Clock() : ::Clock()
192  {
193  RTT::s_clock = this;
194  }
195  };
196 
201  static void wall(Clock* clock)
202  {
203  s_clock = clock;
204  }
205 
210  static Clock* clock()
211  {
212  return (s_clock);
213  }
214 
215 private:
216  static bool s_initiated;
217  static uint32_t s_micros;
218  static uint32_t s_millis;
219  static Scheduler* s_scheduler;
220  static Job* s_job;
221  static Clock* s_clock;
222 
226  RTT() {}
227 
229 #if defined(TIMER2_COMPA_vect)
230  friend void TIMER2_COMPA_vect(void);
231  friend void TIMER2_COMPB_vect(void);
232 #elif defined(TCNT0)
233  friend void TIMER0_COMPA_vect(void);
234  friend void TIMER0_COMPB_vect(void);
235 #endif
236 
238  friend class RTT::Scheduler;
239 };
240 
241 #endif
Clock()
Definition: RTT.hh:191
void TIMER2_COMPA_vect(void)
static RTT::Scheduler * scheduler()
Definition: RTT.hh:178
virtual uint32_t time()
static void delay(uint32_t ms)
Definition: RTT.cpp:136
Definition: Job.hh:33
static bool end()
Definition: RTT.cpp:72
#define TIMER0_COMPB_vect
Definition: ATtinyX4.hh:236
Definition: RTT.hh:37
static uint32_t micros()
Definition: RTT.cpp:103
virtual bool start(Job *job)
static void millis(uint32_t ms)
Definition: RTT.hh:93
static uint32_t since(uint32_t start)
Definition: RTT.hh:107
static uint32_t millis()
Definition: RTT.cpp:121
static bool begin()
Definition: RTT.cpp:43
static Clock * clock()
Definition: RTT.hh:210
static uint16_t us_per_tick()
Definition: RTT.cpp:91
void TIMER2_COMPB_vect(void)
static void wall(Clock *clock)
Definition: RTT.hh:201
static uint16_t us_per_timer_cycle()
Definition: RTT.cpp:97
virtual void dispatch()
static void await()
Definition: RTT.hh:124
static void job(RTT::Scheduler *scheduler)
Definition: RTT.hh:169
static void micros(uint32_t usec)
Definition: RTT.hh:74
#define TIMER0_COMPA_vect
Definition: ATtinyX4.hh:235