OpenMPTL - ARM Cortex (common)
C++ Microprocessor Template Library
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
systick.hpp
Go to the documentation of this file.
1 /*
2  * OpenMPTL - C++ Microprocessor Template Library
3  *
4  * Copyright (C) 2012-2017 Axel Burri <axel@tty0.ch>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 #ifndef ARM_CORTEX_COMMON_SYSTICK_HPP_INCLUDED
22 #define ARM_CORTEX_COMMON_SYSTICK_HPP_INCLUDED
23 
24 #include <arch/nvic.hpp>
25 #include <typelist.hpp>
26 #include <freq.hpp>
27 
28 
29 namespace mptl {
30 
31 
33 {
34  /** Select external clock (HCLK_DIV8) as systick clock source */
35  template< typename system_clock_type, freq_t _freq >
36  struct external {
37  static constexpr freq_t freq = _freq;
38  static constexpr freq_t counter_freq = system_clock_type::hclk_freq / 8;
39 
40  using resources = reglist<
42  regval< SCB::STRVR::regbits_type, (system_clock_type::hclk_freq / (freq * 8)) >
43  >;
44  };
45 
46  /** Select core clock (HCLK) as systick clock source */
47  template< typename system_clock_type, freq_t _freq >
48  struct core {
49  static constexpr freq_t freq = _freq;
50  static constexpr freq_t counter_freq = system_clock_type::hclk_freq;
51 
52  using resources = reglist<
54  regval< SCB::STRVR::regbits_type, (system_clock_type::hclk_freq / freq) >
55  >;
56  };
57 };
58 
59 
60 template< typename clock_source_type >
61 class systick
62 {
63 public:
64  static constexpr freq_t freq = clock_source_type::freq;
65  static constexpr freq_t counter_freq = clock_source_type::counter_freq;
66  static constexpr uint32_t reload_value = counter_freq / freq;
67 
68  /** picoseconds per counter tick
69  *
70  * min: 72MHz, hclk: 13'888
71  * max: 24MHz, hclk/8: 333'333
72  */
73  static constexpr uint32_t ps_per_tick = (1000 * 1000 * 1000) / (counter_freq / 1000);
74 
75  using resources = typename clock_source_type::resources;
76 
77  using irq = mptl::irq::systick; /**< System Tick Interrupt */
78 
79  static void set_reload(SCB::STRVR::value_type reload) {
80  // assert((reload >= 1) && (reload <= 0xFFFFFF));
81  SCB::STRVR::store(reload);
82  }
83 
84  static void enable_counter(void) {
86  }
87  static void disable_counter(void) {
89  }
90  static void clear_counter(void) {
92  }
94  return SCB::STCVR::load();
95  }
96 
97  static void enable_interrupt(void) {
99  }
100  static void disable_interrupt(void) {
102  }
103 
104  static bool get_count_flag(void) {
106  }
107  static bool get_skew_flag(void) {
108  return SCB::STCR::SKEW::test();
109  }
110  static bool get_no_ref_flag(void) {
111  return SCB::STCR::NOREF::test();
112  }
113 
114  static void enable(void) {
115  clear_counter();
116  enable_counter();
117  }
118 };
119 
120 } // namespace mptl
121 
122 #endif // ARM_CORTEX_COMMON_SYSTICK_HPP_INCLUDED
core_exception<-1 > systick
Definition: nvic.hpp:132
static bool get_count_flag(void)
Definition: systick.hpp:104
Select external clock (HCLK_DIV8) as systick clock source.
Definition: systick.hpp:36
unsigned int freq_t
static constexpr freq_t counter_freq
Definition: systick.hpp:38
static bool get_skew_flag(void)
Definition: systick.hpp:107
Definition: nvic.hpp:44
static void enable_counter(void)
Definition: systick.hpp:84
regbits< type, 0, 24 > regbits_type
Definition: scb.hpp:57
static void enable_interrupt(void)
Definition: systick.hpp:97
static void disable_counter(void)
Definition: systick.hpp:87
static void enable(void)
Definition: systick.hpp:114
static SCB::STCVR::value_type get_counter(void)
Definition: systick.hpp:93
Definition: systick.hpp:32
static bool get_no_ref_flag(void)
Definition: systick.hpp:110
static void clear_counter(void)
Definition: systick.hpp:90
typename clock_source_type::resources resources
Definition: systick.hpp:75
static void set_reload(SCB::STRVR::value_type reload)
Definition: systick.hpp:79
static __always_inline void store(Tp const value)
Select core clock (HCLK) as systick clock source.
Definition: systick.hpp:48
static constexpr freq_t freq
Definition: systick.hpp:37
static void disable_interrupt(void)
Definition: systick.hpp:100
Definition: systick.hpp:61