COSA
An Object-Oriented Platform for Arduino Programming
RTT.cpp
Go to the documentation of this file.
1 
21 #include "Cosa/RTT.hh"
22 #include "Cosa/RTT_Config.hh"
23 
24 // Initiated state
25 bool RTT::s_initiated = false;
26 
27 // Micro-seconds counter (fraction in timer register)
28 uint32_t RTT::s_micros = 0UL;
29 
30 // Milli-seconds counter
31 uint32_t RTT::s_millis = 0UL;
32 
33 // Job scheduler
34 RTT::Scheduler* RTT::s_scheduler = NULL;
35 
36 // RTT alarm clock
37 RTT::Clock* RTT::s_clock = NULL;
38 
39 // Timer job
40 Job* RTT::s_job = NULL;
41 
42 bool
44 {
45  // Should not be already initiated
46  if (UNLIKELY(s_initiated)) return (false);
47 
48  synchronized {
49  // Power up the timers
50  Power::timern_enable();
51 
52  // Set prescaling to 64
53  TCCRnB = CSn;
54 
55  // Clear Timer on Compare Match with given Count. Enable interrupt
56  TCCRnA = _BV(WGM01);
57  OCRnA = TIMER_MAX;
58  TIMSKn = _BV(OCIE0A);
59 
60  // Reset the counter and clear interrupts
61  TCNTn = 0;
62  TIFRn = 0;
63  }
64 
65  // Install delay function and mark as initiated
67  s_initiated = true;
68  return (true);
69 }
70 
71 bool
73 {
74  // Check if initiated
75  if (UNLIKELY(!s_initiated)) return (false);
76 
77  synchronized {
78  // Disable the timer interrupts
79  TIMSKn = 0;
80 
81  // Power up the timers
82  Power::timern_disable();
83  }
84 
85  // Mark as not initiated
86  s_initiated = false;
87  return (true);
88 }
89 
90 uint16_t
92 {
93  return (US_PER_TICK);
94 }
95 
96 uint16_t
98 {
99  return (US_PER_TIMER_CYCLE);
100 }
101 
102 uint32_t
104 {
105  uint32_t res;
106  uint8_t cnt;
107 
108  // Read micro-seconds and hardware counter. Adjust if pending interrupt
109  synchronized {
110  res = s_micros;
111  cnt = TCNTn;
112  if ((TIFRn & _BV(OCF0A)) && (cnt < TIMER_MAX)) res += US_PER_TICK;
113  }
114 
115  // Convert ticks to micro-seconds
116  res += ((uint32_t) cnt) * US_PER_TIMER_CYCLE;
117  return (res);
118 }
119 
120 uint32_t
122 {
123  uint32_t res;
124  uint8_t cnt;
125 
126  // Read milli-seconds. Adjust if pending interrupt
127  synchronized {
128  res = s_millis;
129  cnt = TCNTn;
130  if ((TIFRn & _BV(OCF0A)) && (cnt < TIMER_MAX)) res += MS_PER_TICK;
131  }
132  return (res);
133 }
134 
135 void
136 RTT::delay(uint32_t ms)
137 {
138  uint32_t start = RTT::millis();
139  ms += 1;
140  while (RTT::since(start) < ms) yield();
141 }
142 
143 ISR(TIMERn_COMPA_vect)
144 {
145  // Increment micro-seconds counter (fraction in timer)
146  RTT::s_micros += US_PER_TICK;
147 
148  // Increment milli-seconds counter
149  RTT::s_millis += MS_PER_TICK;
150 
151  // Dispatch expired jobs
152  if ((RTT::s_scheduler != NULL) && (RTT::s_job == NULL))
153  RTT::s_scheduler->dispatch();
154 
155  // Clock tick and dispatch expired jobs
156  if (RTT::s_clock != NULL)
157  RTT::s_clock->tick(MS_PER_TICK);
158 }
159 
160 ISR(TIMERn_COMPB_vect)
161 {
162  // Disable the timer match
163  TIMSKn &= ~_BV(OCIE0B);
164 
165  // Dispatch expired jobs
166  RTT::s_job = NULL;
167  if (RTT::s_scheduler != NULL)
168  RTT::s_scheduler->dispatch();
169 }
170 
#define TIMER_MAX
Definition: RTT_Config.hh:27
ISR(TIMERn_COMPA_vect)
Definition: RTT.cpp:143
#define NULL
Definition: Types.h:101
static void delay(uint32_t ms)
Definition: RTT.cpp:136
Definition: Job.hh:33
static bool end()
Definition: RTT.cpp:72
static uint32_t micros()
Definition: RTT.cpp:103
static uint32_t since(uint32_t start)
Definition: RTT.hh:107
#define US_PER_TIMER_CYCLE
Definition: RTT_Config.hh:28
static uint32_t millis()
Definition: RTT.cpp:121
static bool begin()
Definition: RTT.cpp:43
void(* yield)()
static uint16_t us_per_tick()
Definition: RTT.cpp:91
static uint16_t us_per_timer_cycle()
Definition: RTT.cpp:97
void tick(uint16_t ms)
Definition: Clock.hh:94
#define WGM01
Definition: ATtinyX61.hh:253
virtual void dispatch()
#define MS_PER_TICK
Definition: RTT_Config.hh:30
#define UNLIKELY(x)
Definition: Types.h:153
#define US_PER_TICK
Definition: RTT_Config.hh:29