Skip to content

Commit dc81c0a

Browse files
committed
Add hardware chip select polarity setting
1 parent 177925c commit dc81c0a

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- There is no more `PCS0` type state associated with the LPSPI bus.
2323

2424
Introduce a hardware chip select and SPI mode into each LPSPI transaction.
25+
Add an LPSPI configuration for hardware chip selects.
2526

2627
## [0.5.9] 2024-11-24
2728

src/common/lpspi.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,26 @@ pub enum Pcs {
152152
Pcs3,
153153
}
154154

155+
/// The hardware chip select polarity.
156+
///
157+
/// Use [`Disabled::set_chip_select_polarity`] to configure
158+
/// each chip select's polarity. Consult your peripheral's
159+
/// documentation to understand which polarity is expected.
160+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
161+
#[repr(u32)]
162+
pub enum PcsPolarity {
163+
/// The chip select is active low.
164+
///
165+
/// When idle, the chip select is high. This is
166+
/// the default state.
167+
#[default]
168+
ActiveLow,
169+
/// The chip select is active high.
170+
///
171+
/// When idle, the chip select is low.
172+
ActiveHigh,
173+
}
174+
155175
/// An LPSPI transaction definition.
156176
///
157177
/// The transaction defines how many bits the driver sends or recieves.
@@ -1214,6 +1234,21 @@ impl<'a, const N: u8> Disabled<'a, N> {
12141234
pub fn set_peripheral_enable(&mut self, enable: bool) {
12151235
ral::modify_reg!(ral::lpspi, self.lpspi, CFGR1, MASTER: !enable as u32);
12161236
}
1237+
1238+
/// Set the polarity for the `pcs` hardware chip select.
1239+
///
1240+
/// By default, all polarities are active low.
1241+
#[inline]
1242+
pub fn set_chip_select_polarity(&mut self, pcs: Pcs, polarity: PcsPolarity) {
1243+
let pcspol = ral::read_reg!(ral::lpspi, self.lpspi, CFGR1, PCSPOL);
1244+
let mask = 1 << pcs as u32;
1245+
let pcspol = if polarity == PcsPolarity::ActiveHigh {
1246+
pcspol | mask
1247+
} else {
1248+
pcspol & !mask
1249+
};
1250+
ral::modify_reg!(ral::lpspi, self.lpspi, CFGR1, PCSPOL: pcspol);
1251+
}
12171252
}
12181253

12191254
impl<const N: u8> Drop for Disabled<'_, N> {

0 commit comments

Comments
 (0)