Skip to content

Commit 946f28c

Browse files
committed
Add method for switchting Vcore range
1 parent 2716579 commit 946f28c

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

src/pwr.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,32 @@ impl PWR {
4141
Self(pwr)
4242
}
4343

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+
4470
/// Returns a struct that can be used to enter Sleep mode
4571
pub fn sleep_mode<'r>(&'r mut self, scb: &'r mut SCB) -> SleepMode<'r> {
4672
SleepMode {
@@ -74,6 +100,39 @@ impl PWR {
74100
}
75101

76102

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+
77136
/// Implemented for all low-power modes
78137
pub trait PowerMode {
79138
/// Enters the low-power mode

0 commit comments

Comments
 (0)