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 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
//! API for system configuration (SYSCON) //! //! The entry point to this API is [`SYSCON`]. Please refer to [`SYSCON`]'s //! documentation for additional information. //! //! This module mostly provides infrastructure required by other parts of the //! HAL API. For this reason, only a small subset of SYSCON functionality is //! currently implemented. //! //! The SYSCON peripheral is described in the user manual, chapter 5. use core::marker::PhantomData; use crate::{ clock, init_state, raw::{ self, syscon::{ pdruncfg, presetctrl, starterp1, sysahbclkctrl, PDRUNCFG, PRESETCTRL, STARTERP1, SYSAHBCLKCTRL, UARTCLKDIV, UARTFRGDIV, UARTFRGMULT, }, }, reg_proxy::RegProxy, }; /// Entry point to the SYSCON API /// /// The SYSCON API is split into multiple parts, which are all available through /// [`syscon::Parts`]. You can use [`SYSCON::split`] to gain access to /// [`syscon::Parts`]. /// /// You can also use this struct to gain access to the raw peripheral using /// [`SYSCON::free`]. This is the main reason this struct exists, as it's no /// longer possible to do this after the API has been split. /// /// Use [`Peripherals`] to gain access to an instance of this struct. /// /// Please refer to the [module documentation] for more information. /// /// [`syscon::Parts`]: struct.Parts.html /// [`Peripherals`]: ../struct.Peripherals.html /// [module documentation]: index.html pub struct SYSCON { syscon: raw::SYSCON, } impl SYSCON { pub(crate) fn new(syscon: raw::SYSCON) -> Self { SYSCON { syscon } } /// Splits the SYSCON API into its component parts /// /// This is the regular way to access the SYSCON API. It exists as an /// explicit step, as it's no longer possible to gain access to the raw /// peripheral using [`SYSCON::free`] after you've called this method. pub fn split(self) -> Parts { Parts { handle: Handle { pdruncfg : RegProxy::new(), presetctrl : RegProxy::new(), starterp1 : RegProxy::new(), sysahbclkctrl: RegProxy::new(), }, bod : BOD(PhantomData), flash : FLASH(PhantomData), irc : IRC(PhantomData), ircout: IRCOUT(PhantomData), mtb : MTB(PhantomData), ram0_1: RAM0_1(PhantomData), rom : ROM(PhantomData), sysosc: SYSOSC(PhantomData), syspll: SYSPLL(PhantomData), uartfrg: UARTFRG { uartclkdiv : RegProxy::new(), uartfrgdiv : RegProxy::new(), uartfrgmult: RegProxy::new(), }, irc_derived_clock: IrcDerivedClock::new(), } } /// Return the raw peripheral /// /// This method serves as an escape hatch from the HAL API. It returns the /// raw peripheral, allowing you to do whatever you want with it, without /// limitations imposed by the API. /// /// If you are using this method because a feature you need is missing from /// the HAL API, please [open an issue] or, if an issue for your feature /// request already exists, comment on the existing issue, so we can /// prioritize it accordingly. /// /// [open an issue]: https://github.com/braun-robotics/rust-lpc82x-hal/issues pub fn free(self) -> raw::SYSCON { self.syscon } } /// The main API for the SYSCON peripheral /// /// Provides access to all types that make up the SYSCON API. Please refer to /// the [module documentation] for more information. /// /// [module documentation]: index.html pub struct Parts { /// The handle to the SYSCON peripheral pub handle: Handle, /// Brown-out detection pub bod: BOD, /// Flash memory pub flash: FLASH, /// IRC pub irc: IRC, /// IRC output pub ircout: IRCOUT, /// Micro Trace Buffer pub mtb: MTB, /// Random access memory pub ram0_1: RAM0_1, /// Read-only memory pub rom: ROM, /// System oscillator pub sysosc: SYSOSC, /// PLL pub syspll: SYSPLL, /// UART Fractional Baud Rate Generator pub uartfrg: UARTFRG, /// The 750 kHz IRC-derived clock pub irc_derived_clock: IrcDerivedClock<init_state::Enabled>, } /// Handle to the SYSCON peripheral /// /// This handle to the SYSCON peripheral provides access to the main part of the /// SYSCON API. It is also required by other parts of the HAL API to synchronize /// access the the underlying registers, wherever this is required. /// /// Please refer to the [module documentation] for more information about the /// PMU. /// /// [module documentation]: index.html pub struct Handle { pdruncfg : RegProxy<PDRUNCFG>, presetctrl : RegProxy<PRESETCTRL>, starterp1 : RegProxy<STARTERP1>, sysahbclkctrl: RegProxy<SYSAHBCLKCTRL>, } impl Handle { /// Enable peripheral clock /// /// Enables the clock for a peripheral or other hardware component. HAL /// users usually won't have to call this method directly, as other /// peripheral APIs will do this for them. pub fn enable_clock<P: ClockControl>(&mut self, peripheral: &P) { self.sysahbclkctrl.modify(|_, w| peripheral.enable_clock(w)); } /// Disable peripheral clock pub fn disable_clock<P: ClockControl>(&mut self, peripheral: &P) { self.sysahbclkctrl.modify(|_, w| peripheral.disable_clock(w)); } /// Assert peripheral reset pub fn assert_reset<P: ResetControl>(&mut self, peripheral: &P) { self.presetctrl.modify(|_, w| peripheral.assert_reset(w)); } /// Clear peripheral reset /// /// Clears the reset for a peripheral or other hardware component. HAL users /// usually won't have to call this method directly, as other peripheral /// APIs will do this for them. pub fn clear_reset<P: ResetControl>(&mut self, peripheral: &P) { self.presetctrl.modify(|_, w| peripheral.clear_reset(w)); } /// Provide power to an analog block /// /// HAL users usually won't have to call this method themselves, as other /// peripheral APIs will do this for them. pub fn power_up<P: AnalogBlock>(&mut self, peripheral: &P) { self.pdruncfg.modify(|_, w| peripheral.power_up(w)); } /// Remove power from an analog block pub fn power_down<P: AnalogBlock>(&mut self, peripheral: &P) { self.pdruncfg.modify(|_, w| peripheral.power_down(w)); } /// Enable interrupt wake-up from deep-sleep and power-down modes /// /// To use an interrupt for waking up the system from the deep-sleep and /// power-down modes, it needs to be enabled using this method, in addition /// to being enabled in the NVIC. /// /// This method is not required when using the regular sleep mode. pub fn enable_interrupt_wakeup<I>(&mut self) where I: WakeUpInterrupt { self.starterp1.modify(|_, w| I::enable(w)); } /// Disable interrupt wake-up from deep-sleep and power-down modes pub fn disable_interrupt_wakeup<I>(&mut self) where I: WakeUpInterrupt { self.starterp1.modify(|_, w| I::disable(w)); } } /// Brown-out detection /// /// Can be used to control brown-out detection using various methods on /// [`syscon::Handle`]. /// /// [`syscon::Handle`]: struct.Handle.html pub struct BOD(PhantomData<*const ()>); /// Flash memory /// /// Can be used to control flash memory using various methods on /// [`syscon::Handle`]. /// /// [`syscon::Handle`]: struct.Handle.html pub struct FLASH(PhantomData<*const ()>); /// IRC /// /// Can be used to control the IRC using various methods on [`syscon::Handle`]. /// /// [`syscon::Handle`]: struct.Handle.html pub struct IRC(PhantomData<*const ()>); /// IRC output /// /// Can be used to control IRC output using various methods on /// [`syscon::Handle`]. /// /// [`syscon::Handle`]: struct.Handle.html pub struct IRCOUT(PhantomData<*const ()>); /// Micro Trace Buffer /// /// Can be used to control the Micro Trace Buffer using various methods on /// [`syscon::Handle`]. /// /// [`syscon::Handle`]: struct.Handle.html pub struct MTB(PhantomData<*const ()>); /// Random access memory /// /// Can be used to control the RAM using various methods on [`syscon::Handle`]. /// /// [`syscon::Handle`]: struct.Handle.html #[allow(non_camel_case_types)] pub struct RAM0_1(PhantomData<*const ()>); /// Read-only memory /// /// Can be used to control the ROM using various methods on [`syscon::Handle`]. /// /// [`syscon::Handle`]: struct.Handle.html pub struct ROM(PhantomData<*const ()>); /// System oscillator /// /// Can be used to control the system oscillator using various methods on /// [`syscon::Handle`]. /// /// [`syscon::Handle`]: struct.Handle.html pub struct SYSOSC(PhantomData<*const ()>); /// PLL /// /// Can be used to control the PLL using various methods on [`syscon::Handle`]. /// /// [`syscon::Handle`]: struct.Handle.html pub struct SYSPLL(PhantomData<*const ()>); /// UART Fractional Baud Rate Generator /// /// Controls the common clock for all UART peripherals (U_PCLK). /// /// Can also be used to control the UART FRG using various methods on /// [`syscon::Handle`]. /// /// [`syscon::Handle`]: struct.Handle.html pub struct UARTFRG { uartclkdiv : RegProxy<UARTCLKDIV>, uartfrgdiv : RegProxy<UARTFRGDIV>, uartfrgmult: RegProxy<UARTFRGMULT>, } impl UARTFRG { /// Set UART clock divider value (UARTCLKDIV) /// /// See user manual, section 5.6.15. pub fn set_clkdiv(&mut self, value: u8) { self.uartclkdiv.write(|w| unsafe { w.div().bits(value) } ); } /// Set UART fractional generator multiplier value (UARTFRGMULT) /// /// See user manual, section 5.6.20. pub fn set_frgmult(&mut self, value: u8) { self.uartfrgmult.write(|w| unsafe { w.mult().bits(value) } ); } /// Set UART fractional generator divider value (UARTFRGDIV) /// /// See user manual, section 5.6.19. pub fn set_frgdiv(&mut self, value: u8) { self.uartfrgdiv.write(|w| unsafe { w.div().bits(value) } ); } } /// Internal trait for controlling peripheral clocks /// /// This trait is an internal implementation detail and should neither be /// implemented nor used outside of LPC82x HAL. Any changes to this trait won't /// be considered breaking changes. /// /// Please refer to [`syscon::Handle::enable_clock`] and /// [`syscon::Handle::disable_clock`] for the public API that uses this trait. /// /// [`syscon::Handle::enable_clock`]: struct.Handle.html#method.enable_clock /// [`syscon::Handle::disable_clock`]: struct.Handle.html#method.disable_clock pub trait ClockControl { /// Internal method to enable a peripheral clock fn enable_clock<'w>(&self, w: &'w mut sysahbclkctrl::W) -> &'w mut sysahbclkctrl::W; /// Internal method to disable a peripheral clock fn disable_clock<'w>(&self, w: &'w mut sysahbclkctrl::W) -> &'w mut sysahbclkctrl::W; } macro_rules! impl_clock_control { ($clock_control:ty, $clock:ident) => { impl ClockControl for $clock_control { fn enable_clock<'w>(&self, w: &'w mut sysahbclkctrl::W) -> &'w mut sysahbclkctrl::W { w.$clock().enable() } fn disable_clock<'w>(&self, w: &'w mut sysahbclkctrl::W) -> &'w mut sysahbclkctrl::W { w.$clock().disable() } } } } impl_clock_control!(ROM , rom ); impl_clock_control!(RAM0_1 , ram0_1 ); impl_clock_control!(raw::FLASHCTRL, flashreg); impl_clock_control!(FLASH , flash ); impl_clock_control!(raw::I2C0 , i2c0 ); impl_clock_control!(raw::GPIO_PORT, gpio ); impl_clock_control!(raw::SWM , swm ); impl_clock_control!(raw::SCT , sct ); impl_clock_control!(raw::WKT , wkt ); impl_clock_control!(raw::MRT , mrt ); impl_clock_control!(raw::SPI0 , spi0 ); impl_clock_control!(raw::SPI1 , spi1 ); impl_clock_control!(raw::CRC , crc ); impl_clock_control!(raw::USART0 , uart0 ); impl_clock_control!(raw::USART1 , uart1 ); impl_clock_control!(raw::USART2 , uart2 ); impl_clock_control!(raw::WWDT , wwdt ); impl_clock_control!(raw::IOCON , iocon ); impl_clock_control!(raw::CMP , acmp ); impl_clock_control!(raw::I2C1 , i2c1 ); impl_clock_control!(raw::I2C2 , i2c2 ); impl_clock_control!(raw::I2C3 , i2c3 ); impl_clock_control!(raw::ADC , adc ); impl_clock_control!(MTB , mtb ); impl_clock_control!(raw::DMA , dma ); /// Internal trait for controlling peripheral reset /// /// This trait is an internal implementation detail and should neither be /// implemented nor used outside of LPC82x HAL. Any incompatible changes to this /// trait won't be considered breaking changes. /// /// Please refer to [`syscon::Handle::assert_reset`] and /// [`syscon::Handle::clear_reset`] for the public API that uses this trait. /// /// [`syscon::Handle::assert_reset`]: struct.Handle.html#method.assert_reset /// [`syscon::Handle::clear_reset`]: struct.Handle.html#method.clear_reset pub trait ResetControl { /// Internal method to assert peripheral reset fn assert_reset<'w>(&self, w: &'w mut presetctrl::W) -> &'w mut presetctrl::W; /// Internal method to clear peripheral reset fn clear_reset<'w>(&self, w: &'w mut presetctrl::W) -> &'w mut presetctrl::W; } macro_rules! impl_reset_control { ($reset_control:ty, $field:ident) => { impl<'a> ResetControl for $reset_control { fn assert_reset<'w>(&self, w: &'w mut presetctrl::W) -> &'w mut presetctrl::W { w.$field().clear_bit() } fn clear_reset<'w>(&self, w: &'w mut presetctrl::W) -> &'w mut presetctrl::W { w.$field().set_bit() } } } } impl_reset_control!(raw::SPI0 , spi0_rst_n ); impl_reset_control!(raw::SPI1 , spi1_rst_n ); impl_reset_control!(UARTFRG , uartfrg_rst_n); impl_reset_control!(raw::USART0 , uart0_rst_n ); impl_reset_control!(raw::USART1 , uart1_rst_n ); impl_reset_control!(raw::USART2 , uart2_rst_n ); impl_reset_control!(raw::I2C0 , i2c0_rst_n ); impl_reset_control!(raw::MRT , mrt_rst_n ); impl_reset_control!(raw::SCT , sct_rst_n ); impl_reset_control!(raw::WKT , wkt_rst_n ); impl_reset_control!(raw::GPIO_PORT, gpio_rst_n ); impl_reset_control!(raw::FLASHCTRL, flash_rst_n ); impl_reset_control!(raw::CMP , acmp_rst_n ); impl_reset_control!(raw::I2C1 , i2c1_rst_n ); impl_reset_control!(raw::I2C2 , i2c2_rst_n ); impl_reset_control!(raw::I2C3 , i2c3_rst_n ); impl_reset_control!(raw::ADC , adc_rst_n ); impl_reset_control!(raw::DMA , dma_rst_n ); /// Internal trait for powering analog blocks /// /// This trait is an internal implementation detail and should neither be /// implemented nor used outside of LPC82x HAL. Any changes to this trait won't /// be considered breaking changes. /// /// Please refer to [`syscon::Handle::power_up`] and /// [`syscon::Handle::power_down`] for the public API that uses this trait. /// /// [`syscon::Handle::power_up`]: struct.Handle.html#method.power_up /// [`syscon::Handle::power_down`]: struct.Handle.html#method.power_down pub trait AnalogBlock { /// Internal method to power up an analog block fn power_up<'w>(&self, w: &'w mut pdruncfg::W) -> &'w mut pdruncfg::W; /// Internal method to power down an analog block fn power_down<'w>(&self, w: &'w mut pdruncfg::W) -> &'w mut pdruncfg::W; } macro_rules! impl_analog_block { ($analog_block:ty, $field:ident) => { impl<'a> AnalogBlock for $analog_block { fn power_up<'w>(&self, w: &'w mut pdruncfg::W) -> &'w mut pdruncfg::W { w.$field().powered() } fn power_down<'w>(&self, w: &'w mut pdruncfg::W) -> &'w mut pdruncfg::W { w.$field().powered_down() } } } } impl_analog_block!(IRCOUT , ircout_pd ); impl_analog_block!(IRC , irc_pd ); impl_analog_block!(FLASH , flash_pd ); impl_analog_block!(BOD , bod_pd ); impl_analog_block!(raw::ADC , adc_pd ); impl_analog_block!(SYSOSC , sysosc_pd ); impl_analog_block!(raw::WWDT, wdtosc_pd ); impl_analog_block!(SYSPLL , syspll_pd ); impl_analog_block!(raw::CMP , acmp ); /// The 750 kHz IRC-derived clock /// /// This is one of the clocks that can be used to run the self-wake-up timer /// (WKT). See user manual, section 18.5.1. pub struct IrcDerivedClock<State = init_state::Enabled> { _state: State, } impl IrcDerivedClock<init_state::Enabled> { pub(crate) fn new() -> Self { IrcDerivedClock { _state: init_state::Enabled(()), } } } impl IrcDerivedClock<init_state::Disabled> { /// Enable the IRC-derived clock /// /// This method is only available, if `IrcDerivedClock` is in the /// [`Disabled`] state. Code that attempts to call this method when the /// clock is already enabled will not compile. /// /// Consumes this instance of `IrcDerivedClock` and returns another instance /// that has its `State` type parameter set to [`Enabled`]. That new /// instance implements [`clock::Enabled`], which might be required by APIs /// that need an enabled clock. /// /// Also consumes the handles to [`IRC`] and [`IRCOUT`], to make it /// impossible (outside of unsafe code) to break API guarantees. /// /// [`Disabled`]: ../init_state/struct.Disabled.html /// [`Enabled`]: ../init_state/struct.Enabled.html /// [`clock::Enabled`]: ../clock/trait.Enabled.html pub fn enable(self, syscon: &mut Handle, mut irc: IRC, mut ircout: IRCOUT) -> IrcDerivedClock<init_state::Enabled> { syscon.power_up(&mut irc); syscon.power_up(&mut ircout); IrcDerivedClock { _state: init_state::Enabled(()), } } } impl<State> clock::Frequency for IrcDerivedClock<State> { fn hz(&self) -> u32 { 750_000 } } impl clock::Enabled for IrcDerivedClock<init_state::Enabled> {} /// Internal trait used to configure interrupt wake-up /// /// This trait is an internal implementation detail and should neither be /// implemented nor used outside of LPC82x HAL. Any changes to this trait won't /// be considered breaking changes. /// /// Please refer to [`syscon::Handle::enable_interrupt_wakeup`] and /// [`syscon::Handle::disable_interrupt_wakeup`] for the public API that uses /// this trait. /// /// [`syscon::Handle::enable_interrupt_wakeup`]: struct.Handle.html#method.enable_interrupt_wakeup /// [`syscon::Handle::disable_interrupt_wakeup`]: struct.Handle.html#method.disable_interrupt_wakeup pub trait WakeUpInterrupt { /// Internal method to configure interrupt wakeup behavior fn enable(w: &mut starterp1::W) -> &mut starterp1::W; /// Internal method to configure interrupt wakeup behavior fn disable(w: &mut starterp1::W) -> &mut starterp1::W; } macro_rules! wakeup_interrupt { ($name:ident, $field:ident) => { /// Can be used to enable/disable interrupt wake-up behavior /// /// See [`syscon::Handle::enable_interrupt_wakeup`] and /// [`syscon::Handle::disable_interrupt_wakeup`]. /// /// [`syscon::Handle::enable_interrupt_wakeup`]: struct.Handle.html#method.enable_interrupt_wakeup /// [`syscon::Handle::disable_interrupt_wakeup`]: struct.Handle.html#method.disable_interrupt_wakeup pub struct $name; impl WakeUpInterrupt for $name { fn enable(w: &mut starterp1::W) -> &mut starterp1::W { w.$field().enabled() } fn disable(w: &mut starterp1::W) -> &mut starterp1::W { w.$field().disabled() } } } } wakeup_interrupt!(Spi0Wakeup , spi0); wakeup_interrupt!(Spi1Wakeup , spi1); wakeup_interrupt!(Usart0Wakeup, usart0); wakeup_interrupt!(Usart1Wakeup, usart1); wakeup_interrupt!(Usart2Wakeup, usart2); wakeup_interrupt!(I2c1Wakeup , i2c1 ); wakeup_interrupt!(I2c0Wakeup , i2c0 ); wakeup_interrupt!(WwdtWakeup , wwdt ); wakeup_interrupt!(BodWakeup , bod ); wakeup_interrupt!(WktWakeup , wkt ); wakeup_interrupt!(I2c2Wakeup , i2c2 ); wakeup_interrupt!(I2c3Wakeup , i2c3 ); reg!(PDRUNCFG , PDRUNCFG , raw::SYSCON, pdruncfg ); reg!(PRESETCTRL , PRESETCTRL , raw::SYSCON, presetctrl ); reg!(STARTERP1 , STARTERP1 , raw::SYSCON, starterp1 ); reg!(SYSAHBCLKCTRL, SYSAHBCLKCTRL, raw::SYSCON, sysahbclkctrl); reg!(UARTCLKDIV , UARTCLKDIV , raw::SYSCON, uartclkdiv ); reg!(UARTFRGDIV , UARTFRGDIV , raw::SYSCON, uartfrgdiv ); reg!(UARTFRGMULT, UARTFRGMULT , raw::SYSCON, uartfrgmult);