@@ -41,6 +41,32 @@ impl PWR {
41
41
Self ( pwr)
42
42
}
43
43
44
+ /// Switch voltage range of internal regulator
45
+ ///
46
+ /// Please note that switching Vcore has consequences, so please make sure
47
+ /// you know what you're doing. See STM32L0x2 reference manual, sections
48
+ /// 6.1.3 and following.
49
+ pub fn switch_vcore_range ( & mut self , range : VcoreRange ) {
50
+ // The STM32L0x2 reference manual, section 6.1.5 describes the procedure
51
+ // being followed here.
52
+
53
+ while self . 0 . csr . read ( ) . vosf ( ) . bit_is_set ( ) { }
54
+
55
+ // Safe, as `VcoreRange` only provides valid bit patterns.
56
+ self . 0 . cr . modify ( |_, w| unsafe { w. vos ( ) . bits ( range as u8 ) } ) ;
57
+
58
+ while self . 0 . csr . read ( ) . vosf ( ) . bit_is_set ( ) { }
59
+ }
60
+
61
+ /// Returns currently configured internal regulator voltage range
62
+ pub fn get_vcore_range ( & mut self ) -> VcoreRange {
63
+ let vos = self . 0 . cr . read ( ) . vos ( ) . bits ( ) ;
64
+
65
+ // Shouldn't panic, as reading the field from the register should always
66
+ // return a valid value.
67
+ VcoreRange :: from_bits ( vos)
68
+ }
69
+
44
70
/// Returns a struct that can be used to enter Sleep mode
45
71
pub fn sleep_mode < ' r > ( & ' r mut self , scb : & ' r mut SCB ) -> SleepMode < ' r > {
46
72
SleepMode {
@@ -74,6 +100,39 @@ impl PWR {
74
100
}
75
101
76
102
103
+ /// Voltage range selection for internal voltage regulator
104
+ ///
105
+ /// Used as an argument for [`PWR::switch_vcore_range`].
106
+ #[ repr( u8 ) ]
107
+ pub enum VcoreRange {
108
+ /// Range 1 (1.8 V)
109
+ Range1 = 0b01 ,
110
+
111
+ /// Range 2 (1.5 V)
112
+ Range2 = 0b10 ,
113
+
114
+ /// Range 3 (1.2 V)
115
+ Range3 = 0b11 ,
116
+ }
117
+
118
+ impl VcoreRange {
119
+ /// Creates a `VcoreRange` instance from a bit pattern
120
+ ///
121
+ /// # Panics
122
+ ///
123
+ /// Panics, if an invalid value is passed. See STM32L0x2 reference manual,
124
+ /// section 6.4.1 (documentation of VOS field) for valid values.
125
+ pub fn from_bits ( bits : u8 ) -> Self {
126
+ match bits {
127
+ 0b01 => VcoreRange :: Range1 ,
128
+ 0b10 => VcoreRange :: Range2 ,
129
+ 0b11 => VcoreRange :: Range3 ,
130
+ bits => panic ! ( "Bits don't represent valud Vcore range: {}" , bits) ,
131
+ }
132
+ }
133
+ }
134
+
135
+
77
136
/// Implemented for all low-power modes
78
137
pub trait PowerMode {
79
138
/// Enters the low-power mode
0 commit comments