From a82908b35e9f106852ebab8a452e6874f2f2a2ff Mon Sep 17 00:00:00 2001 From: "John W. Terrell" Date: Sat, 5 Sep 2020 12:44:36 -0700 Subject: [PATCH] Updates (not building) --- .gitignore | 2 +- Cargo.toml | 14 ++-- boards/sam4e_xplained_pro/blink/.cargo/config | 9 +++ boards/sam4e_xplained_pro/blink/Cargo.toml | 39 ++++++++++ boards/sam4e_xplained_pro/blink/build.rs | 16 ++++ boards/sam4e_xplained_pro/blink/memory.x | 6 ++ boards/sam4e_xplained_pro/blink/src/main.rs | 37 +++++++++ src/lib.rs | 26 ++----- src/sam4e/clock.rs | 76 +++++++++++++++++++ 9 files changed, 199 insertions(+), 26 deletions(-) create mode 100644 boards/sam4e_xplained_pro/blink/.cargo/config create mode 100644 boards/sam4e_xplained_pro/blink/Cargo.toml create mode 100644 boards/sam4e_xplained_pro/blink/build.rs create mode 100644 boards/sam4e_xplained_pro/blink/memory.x create mode 100644 boards/sam4e_xplained_pro/blink/src/main.rs diff --git a/.gitignore b/.gitignore index 96ef6c0..a9d37c5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ -/target +target Cargo.lock diff --git a/Cargo.toml b/Cargo.toml index c3b841f..4027ede 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "atsam4_hal" +name = "atsam4-hal" version = "0.1.0" authors = ["John W. Terrell "] edition = "2018" @@ -9,12 +9,12 @@ categories = ["embedded", "hardware-support", "no-std"] license = "MIT OR Apache-2.0" repository = "https://github.com/atsam4-rs/atsam4-hal" +[dependencies] + [dependencies.cast] version = "0.2.2" default-features = false -[dependencies] - [dependencies.cortex-m] version = "0.5.0" @@ -24,9 +24,9 @@ version = "0.2.0" [dependencies.nb] version = "0.1.0" -[dependencies.atsam4e16e] +[dependencies.atsam4e16e-pac] version = "0.1.0" -git = "https://github.com/atsam4-rs/atsam4e16e" +path = "../atsam4e16e" optional = true [dependencies.void] @@ -34,5 +34,5 @@ version = "1.0.2" default-features = false [features] -sam4e = [] -sam4e16e = ["sam4e"] +atsam4e = [] +atsam4e16e = ["atsam4e", "atsam4e16e-pac", "atsam4e16e-pac/rt"] diff --git a/boards/sam4e_xplained_pro/blink/.cargo/config b/boards/sam4e_xplained_pro/blink/.cargo/config new file mode 100644 index 0000000..fd7ff15 --- /dev/null +++ b/boards/sam4e_xplained_pro/blink/.cargo/config @@ -0,0 +1,9 @@ +# vim:ft=toml: +[target.thumbv7em-none-eabihf] +runner = 'arm-none-eabi-gdb' + +[build] +target = "thumbv7em-none-eabihf" +rustflags = [ + "-C", "link-arg=-Tlink.x", +] diff --git a/boards/sam4e_xplained_pro/blink/Cargo.toml b/boards/sam4e_xplained_pro/blink/Cargo.toml new file mode 100644 index 0000000..b303a3b --- /dev/null +++ b/boards/sam4e_xplained_pro/blink/Cargo.toml @@ -0,0 +1,39 @@ +[package] +name = "sam4e_xplained_pro_blink" +version = "0.1.0" +authors = ["John Terrell "] +description = "Example LED blink for SAM4E_XPLAINED_PRO" +keywords = ["no-std", "arm", "cortex-m", "embedded-hal"] +license = "MIT OR Apache-2.0" +readme = "README.md" + +[dependencies] +cortex-m = "~0.6" +embedded-hal = "~0.2.3" +nb = "~0.1" + +[dependencies.cortex-m-rt] +version = "~0.6.12" +optional = true + +[dependencies.panic-abort] +version = "~0.3" +optional = true + +[dependencies.panic-halt] +version = "~0.2" +optional = true + +[dependencies.panic-semihosting] +version = "~0.5" +optional = true + +[dependencies.atsam4-hal] +path = "../../.." + +[features] +default = ["atsam4-hal/atsam4e16e", "panic_halt"] +panic_halt = ["panic-halt"] +panic_abort = ["panic-abort"] +panic_semihosting = ["panic-semihosting"] +use_semihosting = [] diff --git a/boards/sam4e_xplained_pro/blink/build.rs b/boards/sam4e_xplained_pro/blink/build.rs new file mode 100644 index 0000000..4bed468 --- /dev/null +++ b/boards/sam4e_xplained_pro/blink/build.rs @@ -0,0 +1,16 @@ +use std::env; +use std::fs::File; +use std::io::Write; +use std::path::PathBuf; +fn main() { + if env::var_os("CARGO_FEATURE_RT").is_some() { + let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap()); + File::create(out.join("memory.x")) + .unwrap() + .write_all(include_bytes!("memory.x")) + .unwrap(); + println!("cargo:rustc-link-search={}", out.display()); + println!("cargo:rerun-if-changed=memory.x"); + } + println!("cargo:rerun-if-changed=build.rs"); +} diff --git a/boards/sam4e_xplained_pro/blink/memory.x b/boards/sam4e_xplained_pro/blink/memory.x new file mode 100644 index 0000000..ef4c2b0 --- /dev/null +++ b/boards/sam4e_xplained_pro/blink/memory.x @@ -0,0 +1,6 @@ +MEMORY +{ + FLASH (rx) : ORIGIN = 0x00400000, LENGTH = 1024K + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K +} +_stack_start = ORIGIN(RAM) + LENGTH(RAM); diff --git a/boards/sam4e_xplained_pro/blink/src/main.rs b/boards/sam4e_xplained_pro/blink/src/main.rs new file mode 100644 index 0000000..e6c837f --- /dev/null +++ b/boards/sam4e_xplained_pro/blink/src/main.rs @@ -0,0 +1,37 @@ +#![no_std] +#![no_main] + +extern crate atsam4_hal as hal; + +#[cfg(not(feature = "use_semihosting"))] +extern crate panic_halt; +#[cfg(feature = "use_semihosting")] +extern crate panic_semihosting; + +use hal::clock::ClockController; +use hal::pac::{CorePeripherals, Peripherals}; +use hal::prelude::*; + +#[entry] +fn main() -> ! { + let mut peripherals = Peripherals::take().unwrap(); + let core = CorePeripherals::take().unwrap(); + + let mut clocks = ClockController::with_internal_32kosc( + peripherals.GCLK, + &mut peripherals.PM, + &mut peripherals.SYSCTRL, + &mut peripherals.NVMCTRL, + ); + + // let mut pins = hal::Pins::new(peripherals.PORT); + // let mut red_led = pins.d2.into_open_drain_output(&mut pins.port); + // let mut delay = Delay::new(core.SYST, &mut clocks); + + // loop { + // delay.delay_ms(200u8); + // red_led.set_high().unwrap(); + // delay.delay_ms(200u8); + // red_led.set_low().unwrap(); + // } +} diff --git a/src/lib.rs b/src/lib.rs index 7c63fb6..ca55321 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,32 +16,22 @@ //! //! [cortex-m-quickstart]: https://docs.rs/cortex-m-quickstart/~0.3 //! -//! # Examples -//! -//! Examples of *using* these abstractions can be found in the documentation of the [`f3`] crate. -//! -//! [`f3`]: https://docs.rs/f3/~0.6 #![deny(missing_docs)] #![deny(warnings)] #![no_std] -// extern crate cast; -// extern crate cortex_m; -// extern crate embedded_hal as hal; -// extern crate nb; -// extern crate void; - pub mod common; -#[cfg(feature = "sam4e")] +#[cfg(feature = "atsam4e16e")] +pub use atsam4e16e_pac as pac; + +#[cfg(feature = "atsam4e")] pub mod sam4e; -#[cfg(feature = "sam4e")] +#[cfg(feature = "atsam4e")] pub use self::sam4e::*; -#[cfg(feature = "sam4e16e")] -pub use atsam4e16e as target_device; -#[cfg(feature = "sam4e16e")] -pub mod sam4e; -#[cfg(feature = "sam4e16e")] +#[cfg(feature = "atsam4e16e")] +pub mod sam4e16e; +#[cfg(feature = "atsam4e16e")] pub use self::sam4e16e::*; diff --git a/src/sam4e/clock.rs b/src/sam4e/clock.rs index e69de29..9fb3ca7 100644 --- a/src/sam4e/clock.rs +++ b/src/sam4e/clock.rs @@ -0,0 +1,76 @@ +// See Chapter 28 of ATSAM4 Datasheet +use crate::pac::{EFC, PMC}; + +// pub enum Oscillator { +// OSC_SLCK_32K_RC, +// OSC_SLCK_32K_XTAL, +// OSC_SLCK_32K_BYPASS, +// OSC_MAINCK_4M_RC, +// OSC_MAINCK_8M_RC, +// OSC_MAINCK_12M_RC, +// OSC_MAINCK_XTAL, +// OSC_MAINCK_BYPASS, +// } + +// impl Oscillator { +// pub fn enable() { + +// } +// } + +pub struct ClockController { +} + +impl ClockController { + pub fn with_internal_32kosc(pmc: PMC, efc: &mut EFC) -> Self { + Self::new(pmc, efc, false) + } + + pub fn with_external_32kosc(pmc: PMC, efc: &mut EFC) -> Self { + Self::new(pmc, efc, true) + } + + pub fn new(pmc: PMC, efc: &mut EFC, use_external_oscillator: bool) -> Self { + Self::set_flash_wait_states_to_maximum(efc); + + ClockController {} + } + + fn get_flash_wait_states_for_clock_frequency(clock_frequency: usize) -> u8 { + match clock_frequency { + c if c < 20000000 => 0, + c if c < 40000000 => 1, + c if c < 60000000 => 2, + c if c < 80000000 => 3, + c if c < 100000000 => 4, + c if c < 123000000 => 5, + _ => panic!("Invalid frequency provided to get_flash_wait_states(): {} ", clock_frequency), + } + } + + fn set_flash_wait_states_to_maximum(efc: &mut EFC) { + efc.fmr.modify(|_, w| w.fws().set(5).cloe.set_bit()); + } + + fn set_flash_wait_states_for_clock_frequency(efc: &mut EFC, clock_frequency: usize) { + let wait_state_count = Self::get_flash_wait_states_for_clock_frequency(clock_frequency); + /* + if clock_frequency < 20000000 { + wait_states = 0; + } else if clock_frequency < 40000000 { + wait_states = 1; + } else if clock_frequency < 60000000 { + wait_states = 2; + } else if clock_frequency < 80000000 { + wait_states = 3; + } else if clock_frequency < 100000000 { + wait_states = 4; + } else if clock_frequency < 123000000 { + wait_states = 5; + } else { + } + */ + + efc.fmr.modify(|_, w| w.fws().set(wait_state_count).cloe.set_bit()); + } +}