|
| 1 | +// Zinc, the bare metal stack for rust. |
| 2 | +// Copyright 2014 Dzmitry "kvark" Malyshau <kvarkus@gmail.com> |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +//! Routines for initialization of STM32L1. |
| 17 | +//! |
| 18 | +//! This module includes code for setting up the clock, flash, access time and |
| 19 | +//! performing initial peripheral configuration. |
| 20 | +
|
| 21 | +//use hal::mem_init::init_data; |
| 22 | +use core::default::Default; |
| 23 | +use core::intrinsics::abort; |
| 24 | + |
| 25 | +#[path="../../util/ioreg.rs"] mod ioreg; |
| 26 | +#[path="../../util/wait_for.rs"] mod wait_for; |
| 27 | + |
| 28 | +/// Phase-locked loop configuration. |
| 29 | +pub struct PllConfig; |
| 30 | + |
| 31 | +/// Multi-speed internal clock divisor. |
| 32 | +pub enum MsiSpeed { |
| 33 | + /// 65_536 kHz |
| 34 | + Msi65 = 0, |
| 35 | + /// 131_072 kHz |
| 36 | + Msi131 = 1, |
| 37 | + /// 262_144 kHz |
| 38 | + Msi262 = 2, |
| 39 | + /// 524_288 kHz |
| 40 | + Msi524 = 3, |
| 41 | + /// 1048 MHz |
| 42 | + Msi1048 = 4, |
| 43 | + /// 2097 MHz |
| 44 | + Msi2097 = 5, |
| 45 | + /// 4194 MHz |
| 46 | + Msi4194 = 6, |
| 47 | +} |
| 48 | + |
| 49 | +/// System clock source. |
| 50 | +pub enum SystemClockSource { |
| 51 | + /// High-speed internal oscillator, 16MHz. |
| 52 | + SystemClockHSI, |
| 53 | + /// High-speed external oscillator with configurable frequency. |
| 54 | + SystemClockHSE(u32), |
| 55 | + /// PLL. |
| 56 | + SystemClockPLL(PllConfig), |
| 57 | + /// Multi-speed internal clock, |
| 58 | + SystemClockMSI(MsiSpeed), |
| 59 | +} |
| 60 | + |
| 61 | +impl Default for SystemClockSource { |
| 62 | + fn default() -> SystemClockSource { |
| 63 | + SystemClockMSI(Msi2097) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl SystemClockSource { |
| 68 | + /// Get the system clock speed in kHz |
| 69 | + pub fn to_speed_khz(&self) -> u32 { |
| 70 | + match *self { |
| 71 | + SystemClockHSI => 16<<10, |
| 72 | + SystemClockMSI(Msi2097) => 2097, |
| 73 | + _ => unsafe { abort() }, //TODO(kvark) |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +// TODO(farcaller): this mod is pub as it's being used in peripheral_clock.rs. |
| 79 | +// This is not the best design solution and a good reason to |
| 80 | +// split RCC into distinct registers. |
| 81 | +#[allow(missing_doc)] |
| 82 | +pub mod reg { |
| 83 | + use util::volatile_cell::VolatileCell; |
| 84 | + use core::ops::Drop; |
| 85 | + |
| 86 | + ioregs!(RCC = { |
| 87 | + 0x00 => reg32 cr { // clock control |
| 88 | + 31..0 => clock_control : rw, |
| 89 | + }, |
| 90 | + 0x04 => reg32 icscr { // internal clock sources calibration |
| 91 | + 31..0 => clock_calibration : rw, |
| 92 | + }, |
| 93 | + 0x08 => reg32 cfgr { // clock configuration |
| 94 | + 31..0 => clock_config : rw, |
| 95 | + }, |
| 96 | + 0x0C => reg32 cir { // clock interrupt |
| 97 | + 31..0 => clock_interrupt : rw, |
| 98 | + }, |
| 99 | + 0x10 => reg32 ahbrstr { // AHB peripheral reset |
| 100 | + 31..0 => reset : rw, |
| 101 | + }, |
| 102 | + 0x14 => reg32 apb2rstr { // APB2 peripheral reset |
| 103 | + 31..0 => reset : rw, |
| 104 | + }, |
| 105 | + 0x18 => reg32 apb1rstr { // APB1 peripheral reset |
| 106 | + 31..0 => reset : rw, |
| 107 | + }, |
| 108 | + 0x1C => reg32 ahbenr { // AHB peripheral clock enable |
| 109 | + 31..0 => enable : rw, |
| 110 | + }, |
| 111 | + 0x20 => reg32 apb2enr { // APB2 peripheral clock enable |
| 112 | + 31..0 => enable : rw, |
| 113 | + }, |
| 114 | + 0x24 => reg32 apb1enr { // ABB1 peripheral clock enable |
| 115 | + 31..0 => enable : rw, |
| 116 | + }, |
| 117 | + 0x28 => reg32 ahblpenr { // AHB peripheral clock enable in low power mode |
| 118 | + 31..0 => enable_low_power : rw, |
| 119 | + }, |
| 120 | + 0x2C => reg32 apb2lpenr { // APB2 peripheral clock enable in low power mode |
| 121 | + 31..0 => enable_low_power : rw, |
| 122 | + }, |
| 123 | + 0x30 => reg32 apb1lpenr { // APB1 peripheral clock enable in low power mode |
| 124 | + 31..0 => enable_low_power : rw, |
| 125 | + }, |
| 126 | + 0x34 => reg32 csr { // control/status |
| 127 | + 31..0 => status : rw, |
| 128 | + }, |
| 129 | + }) |
| 130 | + |
| 131 | + ioregs!(FLASH = { |
| 132 | + 0x00 => reg32 acr { // access control |
| 133 | + 31..0 => access_control : rw, |
| 134 | + }, |
| 135 | + 0x04 => reg32 pecr { // program/erase control |
| 136 | + 31..0 => program_control : rw, |
| 137 | + }, |
| 138 | + 0x08 => reg32 pdkeyr { // power down key |
| 139 | + 31..0 => power_down : rw, |
| 140 | + }, |
| 141 | + 0x0C => reg32 pekeyr { // program/erase key |
| 142 | + 31..0 => program_key : rw, |
| 143 | + }, |
| 144 | + 0x10 => reg32 prtkeyr { // program memory key |
| 145 | + 31..0 => program_memory : rw, |
| 146 | + }, |
| 147 | + 0x14 => reg32 optkeyr { // option byte key |
| 148 | + 31..0 => option_byte : rw, |
| 149 | + }, |
| 150 | + 0x18 => reg32 sr { // status register |
| 151 | + 31..0 => status : rw, |
| 152 | + }, |
| 153 | + 0x1C => reg32 obr { // option byte |
| 154 | + 31..0 => option : rw, |
| 155 | + }, |
| 156 | + 0x20 => reg32 wrpr { // write protection |
| 157 | + 31..0 => protect : rw, |
| 158 | + }, |
| 159 | + 0x28 => reg32 wrpr1 { // write protection register 1 |
| 160 | + 31..0 => protect : rw, |
| 161 | + }, |
| 162 | + 0x2C => reg32 wrpr2 { // write protection register 2 |
| 163 | + 31..0 => protect : rw, |
| 164 | + }, |
| 165 | + }) |
| 166 | + |
| 167 | + ioregs!(PWR = { |
| 168 | + 0x0 => reg32 cr { // power control |
| 169 | + 31..0 => control : rw, |
| 170 | + }, |
| 171 | + 0x4 => reg32 csr { // power control/status |
| 172 | + 31..0 => status : rw, |
| 173 | + }, |
| 174 | + }) |
| 175 | + |
| 176 | + extern { |
| 177 | + #[link_name="stm32l1_iomem_RCC"] pub static RCC: RCC; |
| 178 | + #[link_name="stm32l1_iomem_FLASH"] pub static FLASH: FLASH; |
| 179 | + #[link_name="stm32l1_iomem_PWR"] pub static PWR: PWR; |
| 180 | + } |
| 181 | +} |
0 commit comments