probe_rs/vendor/st/sequences/
stm32_armv6.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
//! Sequences for ARMv6 STM32s: STM32F0, STM32G0, STM32L0.
//!
//! This covers devices where DBGMCU is at 0x40015800 and has the DBG_STANDBY and DBG_STOP bits.

use std::sync::Arc;

use probe_rs_target::CoreType;

use crate::architecture::arm::{
    memory::ArmMemoryInterface, sequences::ArmDebugSequence, ArmError, ArmProbeInterface,
    FullyQualifiedApAddress,
};

/// Supported families for custom sequences on ARMv6 STM32 devices.
#[derive(Debug)]
pub enum Stm32Armv6Family {
    /// STM32F0 family
    F0,

    /// STM32L0 family
    L0,

    /// STM32G0 family
    G0,
}

/// Marker structure for ARMv6 STM32 devices.
#[derive(Debug)]
pub struct Stm32Armv6 {
    family: Stm32Armv6Family,
}

impl Stm32Armv6 {
    /// Create the sequencer for ARMv6 STM32 devices.
    pub fn create(family: Stm32Armv6Family) -> Arc<Self> {
        Arc::new(Self { family })
    }
}

mod rcc {
    use crate::architecture::arm::{memory::ArmMemoryInterface, ArmError};
    use bitfield::bitfield;

    /// The base address of the RCC peripheral
    const RCC: u64 = 0x40021000;

    macro_rules! enable_reg {
        ($name:ident, $offset:literal, $bit:literal) => {
            bitfield! {
                pub struct $name(u32);
                impl Debug;
                pub u8, dbgen, enable_dbg: $bit;
            }
            impl $name {
                const ADDRESS: u64 = $offset;
                /// Read the enable register from memory.
                pub fn read(memory: &mut dyn ArmMemoryInterface) -> Result<Self, ArmError> {
                    let contents = memory.read_word_32(RCC + Self::ADDRESS)?;
                    Ok(Self(contents))
                }

                /// Write the enable register to memory.
                pub fn write(
                    &mut self,
                    memory: &mut dyn ArmMemoryInterface,
                ) -> Result<(), ArmError> {
                    memory.write_word_32(RCC + Self::ADDRESS, self.0)
                }
            }
        };
    }

    // Create enable registers for each device family.
    // On F0 and L0 this is bit 22 in APB2ENR, while on G0 it's bit 27 in APBENR1.
    enable_reg!(EnrF0, 0x18, 22);
    enable_reg!(EnrL0, 0x34, 22);
    enable_reg!(EnrG0, 0x3c, 27);
}

mod dbgmcu {
    use crate::architecture::arm::{memory::ArmMemoryInterface, ArmError};
    use bitfield::bitfield;

    /// The base address of the DBGMCU component
    const DBGMCU: u64 = 0x40015800;

    bitfield! {
        /// The control register (CR) of the DBGMCU. This register is described in "RM0360: STM32F0
        /// family reference manual" section 26.9.3.
        pub struct Control(u32);
        impl Debug;

        pub u8, dbg_standby, enable_standby_debug: 2;
        pub u8, dbg_stop, enable_stop_debug: 1;
    }

    impl Control {
        /// The offset of the Control register in the DBGMCU block.
        const ADDRESS: u64 = 0x04;

        /// Read the control register from memory.
        pub fn read(memory: &mut dyn ArmMemoryInterface) -> Result<Self, ArmError> {
            let contents = memory.read_word_32(DBGMCU + Self::ADDRESS)?;
            Ok(Self(contents))
        }

        /// Write the control register to memory.
        pub fn write(&mut self, memory: &mut dyn ArmMemoryInterface) -> Result<(), ArmError> {
            memory.write_word_32(DBGMCU + Self::ADDRESS, self.0)
        }
    }
}

impl ArmDebugSequence for Stm32Armv6 {
    fn debug_device_unlock(
        &self,
        interface: &mut dyn ArmProbeInterface,
        default_ap: &FullyQualifiedApAddress,
        _permissions: &crate::Permissions,
    ) -> Result<(), ArmError> {
        let mut memory = interface.memory_interface(default_ap)?;

        match self.family {
            Stm32Armv6Family::F0 => {
                let mut enr = rcc::EnrF0::read(&mut *memory)?;
                enr.enable_dbg(true);
                enr.write(&mut *memory)?;
            }
            Stm32Armv6Family::L0 => {
                let mut enr = rcc::EnrL0::read(&mut *memory)?;
                enr.enable_dbg(true);
                enr.write(&mut *memory)?;
            }
            Stm32Armv6Family::G0 => {
                let mut enr = rcc::EnrG0::read(&mut *memory)?;
                enr.enable_dbg(true);
                enr.write(&mut *memory)?;
            }
        }

        let mut cr = dbgmcu::Control::read(&mut *memory)?;
        cr.enable_standby_debug(true);
        cr.enable_stop_debug(true);
        cr.write(&mut *memory)?;

        Ok(())
    }

    fn debug_core_stop(
        &self,
        memory: &mut dyn ArmMemoryInterface,
        _core_type: CoreType,
    ) -> Result<(), ArmError> {
        match self.family {
            Stm32Armv6Family::F0 => {
                let mut enr = rcc::EnrF0::read(&mut *memory)?;
                enr.enable_dbg(false);
                enr.write(&mut *memory)?;
            }
            Stm32Armv6Family::L0 => {
                let mut enr = rcc::EnrL0::read(&mut *memory)?;
                enr.enable_dbg(false);
                enr.write(&mut *memory)?;
            }
            Stm32Armv6Family::G0 => {
                let mut enr = rcc::EnrG0::read(&mut *memory)?;
                enr.enable_dbg(false);
                enr.write(&mut *memory)?;
            }
        }

        let mut cr = dbgmcu::Control::read(&mut *memory)?;
        cr.enable_standby_debug(false);
        cr.enable_stop_debug(false);
        cr.write(&mut *memory)?;

        Ok(())
    }
}