ClearCore Library
SysTiming.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2020 Teknic, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
28 // include guard
29 #ifndef __SYSTIMING_H__
30 #define __SYSTIMING_H__
31 
32 #include <stdint.h>
33 
35 #ifndef CPU_CLK
36 #define CPU_CLK 120000000
37 #endif // CPU_CLK
38 
42 #ifndef _CLEARCORE_SAMPLE_RATE_HZ
43 #define _CLEARCORE_SAMPLE_RATE_HZ (5000)
44 #endif
45 
49 #define MS_TO_SAMPLES (_CLEARCORE_SAMPLE_RATE_HZ / 1000)
50 
53 #define CYCLES_PER_INTERRUPT (CPU_CLK / _CLEARCORE_SAMPLE_RATE_HZ)
54 
57 #define SAMPLE_PERIOD_MICROSECONDS (1000000UL / _CLEARCORE_SAMPLE_RATE_HZ)
58 
61 #define CYCLES_PER_MICROSECOND (CPU_CLK / 1000000)
62 
65 #define CYCLES_PER_MILLISECOND (CPU_CLK / 1000)
66 
69 #define CYCLES_PER_SECOND (CPU_CLK)
70 
71 
72 
73 namespace ClearCore {
74 
80 
87 class SysTiming {
88  friend class SysManager;
89 
90 public:
91 #ifndef HIDE_FROM_DOXYGEN
92 
102  void GetIsrLoading(uint32_t &minSlot, uint32_t &maxSlot);
103 
107  static SysTiming &Instance();
108 #endif
109 
130  uint32_t Microseconds();
131 
144  void ResetMicroseconds();
145 
166  volatile const uint32_t &Milliseconds() {
167  return m_msTickCnt;
168  }
169 
182  void ResetMilliseconds();
183 
184 #ifndef HIDE_FROM_DOXYGEN
185 
198  bool SysTickPeriodMicroSec(uint32_t microseconds = 1000);
199 #endif
200 
201 private:
202  uint32_t m_isrStartCycle;
203  uint32_t m_isrMinCycles;
204  uint32_t m_isrMaxCycles;
205  uint32_t m_isrLastCycles;
206  uint32_t m_msTickCnt;
207  uint8_t m_fractMsTick;
208  uint32_t m_lastIsrStartCnt;
209  uint32_t m_microAdj;
210  uint32_t m_microAdjHigh;
211  uint32_t m_microAdjLow;
212  uint32_t m_microAdjHighRemainder;
213  uint32_t m_microAdjLowRemainder;
214 
215 
219  SysTiming();
220 
226  void IsrStart();
233  void IsrEnd();
241  void Update();
242 
243 };
244 
245 }
246 
247 #ifdef __cplusplus
248 extern "C" {
249 #endif // __cplusplus
250 
261 uint32_t Milliseconds(void);
262 
273 uint32_t Microseconds(void);
274 
280 void Delay_cycles(uint64_t cycles);
281 
287 inline void Delay_ms(uint32_t ms) {
288  return Delay_cycles(static_cast<uint64_t>(ms) * CYCLES_PER_MILLISECOND);
289 }
290 
296 inline void Delay_us(uint32_t usec) {
297  return Delay_cycles(static_cast<uint64_t>(usec) * CYCLES_PER_MICROSECOND);
298 }
299 
300 #ifdef __cplusplus
301 }
302 #endif // __cplusplus
303 
304 // end of include guard
305 #endif // __SYSTIMING_H__
uint32_t Microseconds()
Number of microseconds elapsed since the ClearCore was initialized.
void Delay_cycles(uint64_t cycles)
Blocks for operations cycles CPU cycles.
ClearCore system timing class.
Definition: SysTiming.h:87
#define CYCLES_PER_MICROSECOND
Definition: SysTiming.h:61
#define CYCLES_PER_MILLISECOND
Definition: SysTiming.h:65
volatile const uint32_t & Milliseconds()
Number of milliseconds elapsed since the ClearCore was initialized.
Definition: SysTiming.h:166
#define _CLEARCORE_SAMPLE_RATE_HZ
Definition: SysTiming.h:43
Namespace to encompass the ClearCore board API.
Definition: AdcManager.h:36
const uint16_t SampleRateHz
Definition: SysTiming.h:79
void Delay_ms(uint32_t ms)
Blocks operations for ms milliseconds.
Definition: SysTiming.h:287
ClearCore Board Supervisory System Manager.
Definition: SysManager.h:58
void Delay_us(uint32_t usec)
Blocks for operations usec microseconds.
Definition: SysTiming.h:296