OpenMPTL - STM32F4
C++ Microprocessor Template Library
flash.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 ARCH_FLASH_HPP_INCLUDED
22 #define ARCH_FLASH_HPP_INCLUDED
23 
24 #include <arch/reg/flash.hpp>
25 #include <freq.hpp>
26 #include <voltage.hpp>
27 
28 namespace mptl {
29 
30 class flash
31 {
32  template< typename pwr_type >
33  struct prefetch_buffer_enable_impl {
34  static_assert(pwr_type::system_voltage >= volt(2.1), "prefetch buffer must be disabled when the supply voltage is below 2.1V");
35  using type = regval< FLASH::ACR::PRFTEN, 1>;
36  };
37 
38  static constexpr FLASH::ACR::LATENCY::value_type min_latency(freq_t hclk_freq, voltage_t system_voltage)
39  {
40  return
41  (system_voltage <= volt(2.1)) ?
42  ((hclk_freq <= mhz(20)) ? 0 :
43  (hclk_freq <= mhz(40)) ? 1 :
44  (hclk_freq <= mhz(60)) ? 2 :
45  (hclk_freq <= mhz(80)) ? 3 :
46  (hclk_freq <= mhz(100)) ? 4 :
47  (hclk_freq <= mhz(120)) ? 5 :
48  (hclk_freq <= mhz(140)) ? 6 :
49  (hclk_freq <= mhz(160)) ? 7 :
50  0xff ) :
51  (system_voltage <= volt(2.4)) ?
52  ((hclk_freq <= mhz(22)) ? 0 :
53  (hclk_freq <= mhz(44)) ? 1 :
54  (hclk_freq <= mhz(66)) ? 2 :
55  (hclk_freq <= mhz(88)) ? 3 :
56  (hclk_freq <= mhz(110)) ? 4 :
57  (hclk_freq <= mhz(132)) ? 5 :
58  (hclk_freq <= mhz(154)) ? 6 :
59  (hclk_freq <= mhz(168)) ? 7 :
60  0xff ) :
61  (system_voltage <= volt(2.7)) ?
62  ((hclk_freq <= mhz(24)) ? 0 :
63  (hclk_freq <= mhz(48)) ? 1 :
64  (hclk_freq <= mhz(72)) ? 2 :
65  (hclk_freq <= mhz(96)) ? 3 :
66  (hclk_freq <= mhz(120)) ? 4 :
67  (hclk_freq <= mhz(144)) ? 5 :
68  (hclk_freq <= mhz(168)) ? 6 :
69  0xff ) :
70  (system_voltage <= volt(3.6)) ?
71  ((hclk_freq <= mhz(30)) ? 0 :
72  (hclk_freq <= mhz(60)) ? 1 :
73  (hclk_freq <= mhz(90)) ? 2 :
74  (hclk_freq <= mhz(120)) ? 3 :
75  (hclk_freq <= mhz(150)) ? 4 :
76  (hclk_freq <= mhz(168)) ? 5 :
77  0xff ) : 0xff;
78  };
79 
80 public: /* ------ configuration traits ------ */
81 
82  template< typename pwr_type >
84 
86 
89 
92 
93  struct latency {
94  template< typename system_clock_type, typename pwr_type >
95  using minimum = regval<
97  min_latency(system_clock_type::hclk_freq, pwr_type::system_voltage)
98  >;
99 
100  template< unsigned ws >
102  };
103 
104 public: /* ------ static member functions ------ */
105 
106  /**
107  * Configure FLASH register using configuration traits (Tp).
108  */
109  template< typename... Tp >
110  static void configure(void) {
111  reglist< Tp... >::template strict_reset_to< FLASH::ACR >();
112  }
113 };
114 
115 } // namespace mptl
116 
117 #endif // ARCH_FLASH_HPP_INCLUDED
118 
119 
unsigned int freq_t
Definition: flash.hpp:30
typename Tp::value_type value_type
typename prefetch_buffer_enable_impl< pwr_type >::type prefetch_buffer_enable
Definition: flash.hpp:83
static constexpr voltage_t volt(long double x)
static constexpr freq_t mhz(unsigned long long x)
regbits< type, 0, 3 > LATENCY
Latency.
Definition: flash.hpp:53
static void configure(void)
Configure FLASH register using configuration traits (Tp).
Definition: flash.hpp:110
unsigned int voltage_t
Definition: flash.hpp:93