Files
drone_core
drone_cortexm
drone_ctypes
drone_nrf_map
drone_nrf_map_periph_uarte
drone_nrf_map_pieces
drone_nrf_map_pieces_1
drone_nrf_map_pieces_10
drone_nrf_map_pieces_11
drone_nrf_map_pieces_12
drone_nrf_map_pieces_2
drone_nrf_map_pieces_3
drone_nrf_map_pieces_4
drone_nrf_map_pieces_5
drone_nrf_map_pieces_6
drone_nrf_map_pieces_7
drone_nrf_map_pieces_8
drone_nrf_map_pieces_9
futures
futures_channel
futures_core
futures_io
futures_sink
futures_task
futures_util
pin_project_lite
pin_utils
proc_macro_nested
typenum
  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
180
181
182
183
184
185
186
187
188
#![cfg_attr(feature = "std", allow(dead_code, unreachable_code))]

use crate::{map::reg::scb, reg::prelude::*};
use drone_core::token::Token;

/// Threads initialization token.
///
/// # Safety
///
/// * Must be defined only once for a particular set of threads.
/// * `ThrTokens` type must contain only thread tokens.
pub unsafe trait ThrsInitToken: Token {
    /// The set of thread tokens.
    type ThrTokens: Token;
}

/// A set of register tokens returned by [`init_extended`].
#[allow(missing_docs)]
pub struct ThrInitExtended {
    pub scb_ccr_bfhfnmign: scb::ccr::Bfhfnmign<Srt>,
    pub scb_ccr_div_0_trp: scb::ccr::Div0Trp<Srt>,
    pub scb_ccr_unalign_trp: scb::ccr::UnalignTrp<Srt>,
    pub scb_ccr_usersetmpend: scb::ccr::Usersetmpend<Srt>,
}

/// Initializes the thread system and returns a set of thread tokens.
///
/// # Examples
///
/// ```no_run
/// # #![feature(const_fn_fn_ptr_basics)]
/// # #![feature(proc_macro_hygiene)]
/// # use drone_core::token::Token;
/// # thr::nvic! {
/// #     thread => pub Thr {};
/// #     local => pub ThrLocal {};
/// #     index => Thrs;
/// #     vtable => Vtable;
/// #     init => ThrsInit;
/// #     threads => {};
/// # }
/// use drone_cortexm::{cortexm_reg_tokens, reg::prelude::*, thr};
///
/// cortexm_reg_tokens! {
///     index => Regs;
///     exclude => {
///         scb_ccr,
///         mpu_type, mpu_ctrl, mpu_rnr, mpu_rbar, mpu_rasr,
///     }
/// }
///
/// fn handler(reg: Regs, thr_init: ThrsInit) {
///     let (thr, extended) = thr::init_extended(thr_init);
///     extended.scb_ccr_div_0_trp.set_bit();
/// }
///
/// # fn main() {
/// #     handler(unsafe { Regs::take() }, unsafe { ThrsInit::take() })
/// # }
/// ```
#[allow(clippy::needless_pass_by_value)]
#[inline]
pub fn init_extended<T: ThrsInitToken>(_token: T) -> (T::ThrTokens, ThrInitExtended) {
    let scb_ccr = unsafe { scb::Ccr::<Srt>::take() };
    scb_ccr.store(|r| r.set_stkalign().set_nonbasethrdena());
    let scb::Ccr {
        stkalign,
        bfhfnmign: scb_ccr_bfhfnmign,
        div_0_trp: scb_ccr_div_0_trp,
        unalign_trp: scb_ccr_unalign_trp,
        usersetmpend: scb_ccr_usersetmpend,
        nonbasethrdena,
    } = scb_ccr;
    #[cfg(feature = "memory-protection-unit")]
    unsafe {
        mpu::reset();
    }
    drop(stkalign);
    drop(nonbasethrdena);
    (unsafe { T::ThrTokens::take() }, ThrInitExtended {
        scb_ccr_bfhfnmign,
        scb_ccr_div_0_trp,
        scb_ccr_unalign_trp,
        scb_ccr_usersetmpend,
    })
}

/// Initializes the thread system and returns a set of thread tokens.
///
/// # Examples
///
/// ```no_run
/// # #![feature(const_fn_fn_ptr_basics)]
/// # #![feature(proc_macro_hygiene)]
/// # use drone_core::token::Token;
/// # thr::nvic! {
/// #     thread => pub Thr {};
/// #     local => pub ThrLocal {};
/// #     index => Thrs;
/// #     vtable => Vtable;
/// #     init => ThrsInit;
/// #     threads => {};
/// # }
/// use drone_cortexm::{cortexm_reg_tokens, thr};
///
/// cortexm_reg_tokens! {
///     index => Regs;
///     exclude => {
///         scb_ccr,
///         mpu_type, mpu_ctrl, mpu_rnr, mpu_rbar, mpu_rasr,
///     }
/// }
///
/// fn handler(reg: Regs, thr_init: ThrsInit) {
///     let thr = thr::init(thr_init);
/// }
///
/// # fn main() {
/// #     handler(unsafe { Regs::take() }, unsafe { ThrsInit::take() })
/// # }
/// ```
#[allow(clippy::needless_pass_by_value)]
#[inline]
pub fn init<T: ThrsInitToken>(token: T) -> T::ThrTokens {
    let (thr, _) = init_extended(token);
    thr
}

#[cfg(feature = "memory-protection-unit")]
mod mpu {
    use crate::{map::reg::mpu, reg::prelude::*};
    use drone_core::token::Token;

    static MPU_RESET_TABLE: [u32; 16] = [
        rbar_reset(0),
        0,
        rbar_reset(1),
        0,
        rbar_reset(2),
        0,
        rbar_reset(3),
        0,
        rbar_reset(4),
        0,
        rbar_reset(5),
        0,
        rbar_reset(6),
        0,
        rbar_reset(7),
        0,
    ];

    pub(super) unsafe fn reset() {
        #[cfg(feature = "std")]
        return unimplemented!();
        let mpu_type = unsafe { mpu::Type::<Srt>::take() };
        let mpu_ctrl = unsafe { mpu::Ctrl::<Srt>::take() };
        if mpu_type.load().dregion() == 0 {
            return;
        }
        mpu_ctrl.reset();
        #[cfg(not(feature = "std"))]
        unsafe {
            asm!(
                "ldmia r0!, {{r2, r3, r4, r5, r8, r9, r10, r11}}",
                "stmia r1,  {{r2, r3, r4, r5, r8, r9, r10, r11}}",
                "ldmia r0!, {{r2, r3, r4, r5, r8, r9, r10, r11}}",
                "stmia r1,  {{r2, r3, r4, r5, r8, r9, r10, r11}}",
                inout("r0") MPU_RESET_TABLE.as_ptr() => _,
                in("r1") mpu::Rbar::<Srt>::ADDRESS,
                out("r2") _,
                out("r3") _,
                out("r4") _,
                out("r5") _,
                out("r8") _,
                out("r9") _,
                out("r10") _,
                out("r11") _,
                options(preserves_flags),
            );
        }
    }

    #[allow(clippy::cast_lossless)]
    const fn rbar_reset(region: u8) -> u32 {
        1 << 4 | region as u32 & 0b1111
    }
}