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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
// #![allow(non_camel_case_types)] #![no_std] // Meh, maybe `hal_traits` instead? use embedded_hal as hal; pub extern crate lpc55s6x_pac as raw; pub mod clock; pub mod gpio; pub mod iocon; pub mod rng; pub mod sleep; pub mod syscon; pub mod utick; #[macro_use] pub(crate) mod reg_proxy; // currently, all sorts of traits pub mod prelude; /// Contains types that encode the state of hardware initialization /// /// The types in this module are used by structs representing peripherals or /// other hardware components, to encode the initialization state of the /// underlying hardware as part of the type. pub mod init_state { /// Indicates that the hardware component is enabled /// /// This usually indicates that the hardware has been initialized and can be /// used for its intended purpose. Contains an optional payload that APIs /// can use to keep data that is only available while enabled. pub struct Enabled<T = ()>(pub T); /// Indicates that the hardware component is disabled pub struct Disabled; } /// /// This is the entry point to the HAL API. Before you can do anything else, you /// need to get an instance of this struct via [`Peripherals::take`] or /// [`Peripherals::steal`]. /// /// The HAL API tracks the state of peripherals at compile-time, to prevent /// potential bugs before the program can even run. Many parts of this /// documentation call this "type state". The peripherals available in this /// struct are set to their initial state (i.e. their state after a system /// reset). See user manual, section 5.6.14. /// /// # Safe Use of the API /// /// Since it should be impossible (outside of unsafe code) to access the /// peripherals before this struct is initialized, you can rely on the /// peripheral states being correct, as long as there's no bug in the API, and /// you're not using unsafe code to do anything that the HAL API can't account /// for. /// /// If you directly use unsafe code to access peripherals or manipulate this /// API, this will be really obvious from the code. But please note that if /// you're using other APIs to access the hardware, such conflicting hardware /// access might not be obvious, as the other API might use unsafe code under /// the hood to access the hardware (just like this API does). /// /// If you do access the peripherals in any way not intended by this API, please /// make sure you know what you're doing. In specific terms, this means you /// should be fully aware of what your code does, and whether that is a valid /// use of the hardware. #[allow(non_snake_case)] pub struct Peripherals { /// General-purpose I/O (GPIO) /// /// The GPIO peripheral is enabled by default. /// TODO: do *not* rely on this pub GPIO: gpio::GPIO<init_state::Disabled>, /// I/O configuration pub IOCON: iocon::IOCON, /// System configuration pub SYSCON: syscon::SYSCON, /// Micro-Tick Timer pub UTICK: utick::UTICK<init_state::Disabled>, /// Analog-to-Digital Converter (ADC) /// /// A HAL API for this peripheral has not been implemented yet. In the /// meantime, this field provides you with the raw register mappings, which /// allow you full, unprotected access to the peripheral. pub ADC0: raw::ADC0, /// CRC engine /// /// A HAL API for this peripheral has not been implemented yet. In the /// meantime, this field provides you with the raw register mappings, which /// allow you full, unprotected access to the peripheral. pub CRC_ENGINE: raw::CRC_ENGINE, /// Standard counter/timer (CTIMER) /// /// A HAL API for this peripheral has not been implemented yet. In the /// meantime, this field provides you with the raw register mappings, which /// allow you full, unprotected access to the peripheral. pub CTIMER0: raw::CTIMER0, /// Flash /// /// A HAL API for this peripheral has not been implemented yet. In the /// meantime, this field provides you with the raw register mappings, which /// allow you full, unprotected access to the peripheral. pub FLASH: raw::FLASH, /// CPUID /// /// This is a core peripherals that's available on all ARM Cortex-M0+ cores. pub CPUID: raw::CPUID, /// Debug Control Block (DCB) /// /// This is a core peripherals that's available on all ARM Cortex-M0+ cores. pub DCB: raw::DCB, /// Data Watchpoint and Trace unit (DWT) /// /// This is a core peripherals that's available on all ARM Cortex-M0+ cores. pub DWT: raw::DWT, /// Memory Protection Unit (MPU) /// /// This is a core peripherals that's available on all ARM Cortex-M0+ cores. pub MPU: raw::MPU, /// Nested Vector Interrupt Controller (NVIC) /// /// This is a core peripherals that's available on all ARM Cortex-M0+ cores. pub NVIC: raw::NVIC, /// System Control Block (SCB) /// /// This is a core peripherals that's available on all ARM Cortex-M0+ cores. pub SCB: raw::SCB, /// SysTick: System Timer /// /// This is a core peripherals that's available on all ARM Cortex-M0+ cores. pub SYST: raw::SYST, } impl Peripherals { /// Take the peripherals safely /// /// This method can only be called one time to access the peripherals. It /// will return `Some(Peripherals)` when called for the first time, then /// `None` on any subsequent calls. /// /// Applications should call this method once, at the beginning of their /// main method, to get access to the full API. Any other parts of the /// program should just expect to be passed whatever parts of the HAL API /// they need. /// /// Calling this method from a library is considered an anti-pattern. /// Libraries should just require whatever they need to be passed as /// arguments and leave the initialization to the application that calls /// them. /// /// For an alternative way to gain access to the hardware, please take a /// look at [`Peripherals::steal`]. /// /// # Example /// /// ``` no_run /// use lpc55s6x_hal::Peripherals; /// /// // This code should be at the beginning of your program. As long as this /// // is the only place that calls `take`, the following should never /// // panic. /// let p = Peripherals::take().unwrap(); /// ``` pub fn take() -> Option<Self> { Some(Self::new( raw::Peripherals::take()?, raw::CorePeripherals::take()?, )) } /// Steal the peripherals /// /// This function returns an instance of `Peripherals`, whether or not such /// an instance exists somewhere else. This is highly unsafe, as it can lead /// to conflicting access of the hardware, mismatch between actual hardware /// state and peripheral state as tracked by this API at compile-time, and /// in general a full nullification of all safety guarantees that this API /// would normally make. /// /// If at all possible, you should always prefer `Peripherals::take` to this /// method. The only legitimate use of this API is code that can't access /// `Peripherals` the usual way, like a panic handler, or maybe temporary /// debug code in an interrupt handler. /// /// # Safety /// /// This method returns an instance of `Peripherals` that might conflict /// with either other instances of `Peripherals` that exist in the program, /// or other means of accessing the hardware. This is only sure, if you make /// sure of the following: /// 1. No other code can access the hardware at the same time. /// 2. You don't change the hardware state in any way that could invalidate /// the type state of other `Peripherals` instances. /// 3. The type state in your `Peripherals` instance matches the actual /// state of the hardware. /// /// Items 1. and 2. are really tricky, so it is recommended to avoid any /// situations where they apply, and restrict the use of this method to /// situations where the program has effectively ended and the hardware will /// be reset right after (like a panic handler). /// /// Item 3. applies to all uses of this method, and is generally very tricky /// to get right. The best way to achieve that is probably to force the API /// into a type state that allows you to execute operations that are known /// to put the hardware in a safe state. Like forcing the type state for a /// peripheral API to the "disabled" state, then enabling it, to make sure /// it is enabled, regardless of wheter it was enabled before. /// /// Since there are no means within this API to forcibly change type state, /// you will need to resort to something like [`core::mem::transmute`]. pub unsafe fn steal() -> Self { Self::new(raw::Peripherals::steal(), raw::CorePeripherals::steal()) } fn new(p: raw::Peripherals, cp: raw::CorePeripherals) -> Self { Peripherals { // HAL peripherals // NOTE(unsafe) The init state of the gpio peripheral is enabled, // thus it's safe to create an already initialized gpio port GPIO: gpio::GPIO::new(p.GPIO), IOCON: iocon::IOCON::new(p.IOCON), SYSCON: syscon::SYSCON::new(p.SYSCON), UTICK: utick::UTICK::new(p.UTICK), // Raw peripherals ADC0: p.ADC0, CRC_ENGINE: p.CRC_ENGINE, CTIMER0: p.CTIMER0, FLASH: p.FLASH, // Core peripherals CPUID: cp.CPUID, DCB: cp.DCB, DWT: cp.DWT, MPU: cp.MPU, NVIC: cp.NVIC, SCB: cp.SCB, SYST: cp.SYST, } } } pub fn get_cycle_count() -> u32 { raw::DWT::get_cycle_count() }