Skip to content

Commit 5676228

Browse files
authored
gpio mode set clear (#154)
* gpio mode set clear * make clippy happy * demacrofy DMA * defmt and atomic features * more defmt
1 parent 686d9c7 commit 5676228

File tree

21 files changed

+222
-213
lines changed

21 files changed

+222
-213
lines changed

Cargo.toml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ authors = ["Vitaly Domnikov <[email protected]>"]
33
categories = ["embedded", "hardware-support", "no-std"]
44
description = "Peripheral access API for STM32G0 series microcontrollers"
55
documentation = "https://docs.rs/stm32g0xx-hal"
6-
edition = "2018"
6+
edition = "2021"
77
keywords = ["arm", "cortex-m", "stm32g0xx", "hal"]
88
license = "MIT/Apache-2.0"
99
name = "stm32g0xx-hal"
@@ -18,14 +18,16 @@ default-target = "thumbv6m-none-eabi"
1818
[dependencies]
1919
cortex-m = { version = "0.7.7", features = ["critical-section-single-core"] }
2020
nb = "1.1.0"
21+
defmt = { version = "0.3.10", optional = true }
2122
fugit = "0.3.7"
2223
embedded-hal = "1.0.0"
2324
bare-metal = "1.0.0"
24-
25+
portable-atomic = { version = "1.10.0", features = ["critical-section"] }
2526

2627
[dependencies.stm32g0]
2728
package = "stm32g0-staging"
2829
version = "0.16.0"
30+
features = ["atomics"]
2931

3032
[dependencies.void]
3133
default-features = false
@@ -34,19 +36,17 @@ version = "1.0.2"
3436
[dev-dependencies]
3537
cortex-m-rt = "0.7.5"
3638
cortex-m-semihosting = "0.5.0"
37-
defmt = "0.3.8"
3839
defmt-rtt = "0.4.0"
3940
panic-halt = "1.0.0"
4041
panic-semihosting = "0.6.0"
41-
portable-atomic = {version = "1.7.0", features = ["critical-section"]}
4242
rtic = { version = "2.1.1", features = ["thumbv6-backend"] }
4343
panic-probe = "0.3.2"
4444

4545
[features]
4646
default = ["i2c-blocking"]
4747
device-selected = []
4848
rt = ["stm32g0/rt"]
49-
defmt = ["embedded-hal/defmt-03"]
49+
defmt = ["dep:defmt", "embedded-hal/defmt-03", "stm32g0/defmt"]
5050
stm32g030 = ["stm32g0/stm32g030", "stm32g0x0", "device-selected"]
5151
stm32g070 = ["stm32g0/stm32g070", "stm32g0x0", "device-selected"]
5252
stm32g031 = ["stm32g0/stm32g031", "stm32g0x1", "device-selected"]

src/analog/adc.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ pub trait Channel<ADC> {
1313
}
1414

1515
/// ADC Result Alignment
16-
#[derive(Eq, PartialEq)]
16+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
17+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
1718
pub enum Align {
1819
/// Right aligned results (least significant bits)
1920
///
@@ -29,7 +30,8 @@ pub enum Align {
2930
}
3031

3132
/// ADC Sampling Precision
32-
#[derive(Copy, Clone, PartialEq, Eq)]
33+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
34+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
3335
pub enum Precision {
3436
/// 12 bit precision
3537
B_12 = 0b00,
@@ -42,7 +44,8 @@ pub enum Precision {
4244
}
4345

4446
/// ADC Sampling time
45-
#[derive(Copy, Clone, PartialEq, Eq)]
47+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
48+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
4649
pub enum SampleTime {
4750
T_2 = 0b000,
4851
T_4 = 0b001,
@@ -55,7 +58,8 @@ pub enum SampleTime {
5558
}
5659

5760
// ADC Oversampling ratio
58-
#[derive(Copy, Clone, PartialEq, Eq)]
61+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
62+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
5963
pub enum OversamplingRatio {
6064
X_2 = 0b000,
6165
X_4 = 0b001,
@@ -67,20 +71,23 @@ pub enum OversamplingRatio {
6771
X_256 = 0b111,
6872
}
6973

70-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
74+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
75+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
7176
pub enum ClockSource {
7277
Pclk(PclkDiv),
7378
Async(AsyncClockDiv),
7479
}
7580

76-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
81+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
82+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
7783
pub enum PclkDiv {
7884
PclkD1 = 3,
7985
PclkD2 = 1,
8086
PclkD4 = 2,
8187
}
8288

83-
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
89+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
90+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
8491
pub enum AsyncClockDiv {
8592
AsyncD1 = 0,
8693
AsyncD2 = 1,
@@ -94,7 +101,8 @@ pub enum AsyncClockDiv {
94101
}
95102

96103
/// ADC injected trigger source selection
97-
#[derive(Copy, Clone, PartialEq, Eq)]
104+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
105+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
98106
pub enum InjTrigSource {
99107
TRG_0 = 0b000, // TIM1_TRGO2
100108
TRG_1 = 0b001, // TIM1_CC4

src/analog/comparator.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,17 @@ impl Config {
9494
}
9595
}
9696

97-
#[derive(Copy, Clone, Eq, PartialEq)]
97+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
98+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
9899
pub enum Hysteresis {
99100
None = 0b00,
100101
Low = 0b01,
101102
Medium = 0b10,
102103
High = 0b11,
103104
}
104105

105-
#[derive(Copy, Clone, Eq, PartialEq)]
106+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
107+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
106108
pub enum PowerMode {
107109
HighSpeed = 0b00,
108110
MediumSpeed = 0b01,
@@ -183,7 +185,8 @@ negative_input_pin!(COMP2, PB3<Analog>, 0b0110);
183185
negative_input_pin!(COMP2, PB7<Analog>, 0b0111);
184186
negative_input_pin!(COMP2, PA2<Analog>, 0b1000);
185187

186-
#[derive(Copy, Clone, Eq, PartialEq)]
188+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
189+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
187190
pub enum RefintInput {
188191
/// VRefint * 1/4
189192
VRefintM14 = 0b0000,

src/crc.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ impl CrcExt for CRC {
4444
}
4545

4646
/// Polynomial settings.
47+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
48+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
4749
pub enum Polynomial {
4850
/// 7-bit polynomial, only the lowest 7 bits are valid
4951
L7(u8),
@@ -56,6 +58,8 @@ pub enum Polynomial {
5658
}
5759

5860
/// Bit reversal settings.
61+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
62+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
5963
pub enum BitReversal {
6064
/// Reverse bits by byte
6165
ByByte,

src/dma.rs

Lines changed: 67 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
//! Direct Memory Access Engine
22
33
// TODO: add DMA2 for B1, C1
4-
use crate::dmamux::DmaMuxIndex;
5-
use crate::rcc::Rcc;
6-
use crate::stm32::DMAMUX;
4+
use crate::dmamux::{self, DmaMuxExt, DmaMuxIndex};
5+
use crate::rcc::{Enable, Rcc, Reset};
6+
use crate::stm32::{self, DMA1, DMAMUX};
77

88
/// Extension trait to split a DMA peripheral into independent channels
99
pub trait DmaExt {
@@ -18,6 +18,8 @@ pub trait DmaExt {
1818
}
1919

2020
/// Channel priority level
21+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
22+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
2123
pub enum Priority {
2224
/// Low
2325
Low = 0b00,
@@ -41,6 +43,8 @@ impl From<Priority> for u8 {
4143
}
4244

4345
/// DMA transfer direction
46+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
47+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
4448
pub enum Direction {
4549
/// From memory to peripheral
4650
FromMemory,
@@ -57,8 +61,9 @@ impl From<Direction> for bool {
5761
}
5862
}
5963

60-
#[doc = "Peripheral size"]
61-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
64+
/// Peripheral size
65+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
66+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
6267
#[repr(u8)]
6368
pub enum WordSize {
6469
#[doc = "0: 8-bit size"]
@@ -76,6 +81,8 @@ impl From<WordSize> for u8 {
7681
}
7782

7883
/// DMA events
84+
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
85+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
7986
pub enum Event {
8087
/// First half of a transfer is done
8188
HalfTransfer,
@@ -243,6 +250,57 @@ pub trait Channel: private::Channel {
243250
}
244251
}
245252

253+
/// Singleton that represents a DMA channel
254+
pub struct C<const N: u8> {
255+
mux: dmamux::Channel<N>,
256+
}
257+
258+
impl<const N: u8> private::Channel for C<N> {
259+
fn ch(&self) -> &stm32::dma1::CH {
260+
// NOTE(unsafe) $Ci grants exclusive access to this register
261+
unsafe { (*DMA1::ptr()).ch(N as usize) }
262+
}
263+
}
264+
265+
impl<const N: u8> C<N> {
266+
pub fn mux(&mut self) -> &mut dyn dmamux::DmaMuxChannel {
267+
&mut self.mux
268+
}
269+
}
270+
271+
impl<const N: u8> Channel for C<N> {
272+
fn select_peripheral(&mut self, index: DmaMuxIndex) {
273+
self.mux().select_peripheral(index);
274+
}
275+
276+
fn event_occurred(&self, event: Event) -> bool {
277+
use Event::*;
278+
279+
// NOTE(unsafe) atomic read
280+
let flags = unsafe { (*DMA1::ptr()).isr().read() };
281+
match event {
282+
HalfTransfer => flags.htif(N).bit_is_set(),
283+
TransferComplete => flags.tcif(N).bit_is_set(),
284+
TransferError => flags.teif(N).bit_is_set(),
285+
Any => flags.gif(N).bit_is_set(),
286+
}
287+
}
288+
289+
fn clear_event(&mut self, event: Event) {
290+
use Event::*;
291+
292+
// NOTE(unsafe) atomic write to a stateless register
293+
unsafe {
294+
let _ = &(*DMA1::ptr()).ifcr().write(|w| match event {
295+
HalfTransfer => w.chtif(N).set_bit(),
296+
TransferComplete => w.ctcif(N).set_bit(),
297+
TransferError => w.cteif(N).set_bit(),
298+
Any => w.cgif(N).set_bit(),
299+
});
300+
}
301+
}
302+
}
303+
246304
macro_rules! dma {
247305
(
248306
channels: {
@@ -251,11 +309,6 @@ macro_rules! dma {
251309
)+
252310
},
253311
) => {
254-
use crate::dmamux;
255-
use crate::rcc::{Enable, Reset};
256-
use crate::stm32::{self, DMA1 as DMA};
257-
use crate::dmamux::DmaMuxExt;
258-
259312
/// DMA channels
260313
pub struct Channels {
261314
$( pub $chi: $Ci, )+
@@ -269,60 +322,8 @@ macro_rules! dma {
269322
}
270323
}
271324

272-
273325
$(
274-
/// Singleton that represents a DMA channel
275-
pub struct $Ci {
276-
mux: dmamux::Channel<$i>,
277-
}
278-
279-
impl private::Channel for $Ci {
280-
fn ch(&self) -> &stm32::dma1::CH {
281-
// NOTE(unsafe) $Ci grants exclusive access to this register
282-
unsafe { &(*DMA::ptr()).ch($i) }
283-
}
284-
}
285-
286-
impl $Ci {
287-
pub fn mux(&mut self) -> &mut dyn dmamux::DmaMuxChannel {
288-
&mut self.mux
289-
}
290-
}
291-
292-
impl Channel for $Ci {
293-
294-
fn select_peripheral(&mut self, index: DmaMuxIndex) {
295-
self.mux().select_peripheral(index);
296-
}
297-
298-
fn event_occurred(&self, event: Event) -> bool {
299-
use Event::*;
300-
301-
// NOTE(unsafe) atomic read
302-
let flags = unsafe { (*DMA::ptr()).isr().read() };
303-
match event {
304-
HalfTransfer => flags.htif($i).bit_is_set(),
305-
TransferComplete => flags.tcif($i).bit_is_set(),
306-
TransferError => flags.teif($i).bit_is_set(),
307-
Any => flags.gif($i).bit_is_set(),
308-
}
309-
}
310-
311-
fn clear_event(&mut self, event: Event) {
312-
use Event::*;
313-
314-
// NOTE(unsafe) atomic write to a stateless register
315-
unsafe {
316-
let _ = &(*DMA::ptr()).ifcr().write(|w| match event {
317-
HalfTransfer => w.chtif($i).set_bit(),
318-
TransferComplete => w.ctcif($i).set_bit(),
319-
TransferError => w.cteif($i).set_bit(),
320-
Any => w.cgif($i).set_bit(),
321-
});
322-
}
323-
}
324-
325-
}
326+
pub type $Ci = C<$i>;
326327
)+
327328
}
328329
}
@@ -357,19 +358,19 @@ dma!(
357358
},
358359
);
359360

360-
impl DmaExt for DMA {
361+
impl DmaExt for DMA1 {
361362
type Channels = Channels;
362363

363364
fn reset(self, rcc: &mut Rcc) -> Self {
364365
// reset DMA
365-
<DMA as Reset>::reset(rcc);
366+
<DMA1 as Reset>::reset(rcc);
366367
self
367368
}
368369

369370
fn split(self, rcc: &mut Rcc, dmamux: DMAMUX) -> Self::Channels {
370371
let muxchannels = dmamux.split();
371372
// enable DMA clock
372-
DMA::enable(rcc);
373+
DMA1::enable(rcc);
373374

374375
let mut channels = Channels {
375376
ch1: C1 {

0 commit comments

Comments
 (0)