Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HAL: Add PMC, EFC configuration APIs #16

Closed
wants to merge 45 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
4addd5d
manifest: increase internal feature flag granularity
tmplt Apr 22, 2022
c552e47
add pmc, efc modules from @jamesmunns's work
jamesmunns Apr 22, 2022
1c8fcd2
move feature checking to a build.rs instead
tmplt Apr 25, 2022
d7e0f57
efc: find FWS via TryFrom impls
tmplt Apr 25, 2022
7c5d8b6
hal: apply fmt
tmplt Apr 25, 2022
ecb51b5
features: separate device-selected master feature from MCU info
tmplt Apr 25, 2022
d77f43e
Add Voltage Level Checks To build.rs
martinmortsell Apr 26, 2022
83de611
Fix Return Types
martinmortsell Apr 26, 2022
3f1f25f
Add get_ Methods For All The Clocks (Except SLCK)
martinmortsell Apr 26, 2022
762e919
efc: set VDDIO level via ctor instead of feature
tmplt Apr 27, 2022
be19501
build: ensure a chip feature is enabled
tmplt Apr 27, 2022
e42837e
pmc: Add Methods For Configuring Plla And Mck
martinmortsell Apr 27, 2022
5d2dd11
Merge branch 'feat/pmc' of https://github.com/GrepitAB/atsams70-rust …
martinmortsell Apr 27, 2022
d8edfe1
pmc: Add Non-functional External Oscillator Support
martinmortsell Apr 27, 2022
daa8a2c
pmc: Fix Main Clock Selection
martinmortsell May 3, 2022
9faca53
pmc: Add PCK Configuration
martinmortsell May 5, 2022
14660e7
Update Documentation
martinmortsell May 6, 2022
4ce66d6
pmc: Change Clock Source From Enum To Trait
martinmortsell May 9, 2022
745c86a
Add get_slck() Method
martinmortsell May 13, 2022
3d8af21
pmc: apply formatting
tmplt May 24, 2022
41c83aa
boards/atsame70_xpro: init example skeleton
tmplt May 24, 2022
6a614a8
boards/atsame70_xpro: doc how to erase fauly firmware
tmplt May 24, 2022
465f7d7
pmc: improve get_mainck
tmplt May 24, 2022
c08d50f
hal/pmc: refer to PMC as pmc, instead of periph
tmplt May 24, 2022
672c168
hal/pmc: add main crystal osc in normal mode back
tmplt May 25, 2022
d213282
hal/pmc: get_pllack: deconstruct PllaConfig
tmplt May 25, 2022
fef7e49
hal/pmc: refactor get_hclk
tmplt May 25, 2022
0b5694b
hal/pmc: record clock freq for MAINCK, PLLACK, and HCLK
tmplt May 25, 2022
c47096b
hal/pmc: get_hclk: set EFC wait states before switching clocks
tmplt May 25, 2022
451e3c1
hal/pmc: correctly configure UPLLCK and record its freq
tmplt May 25, 2022
22c9964
hal/pmc: implement get_upllckdiv
tmplt May 30, 2022
e035fae
hal/pmc: use Hertz instead of Megahertz when logical
tmplt May 30, 2022
2d4389c
hal/pmc: record SLCK freq
tmplt May 30, 2022
5adcf9f
hal/pmc: remove unnecessary Results
tmplt May 30, 2022
dbe39b1
hal/pmc: handle unused_variables/dead_code warnings
tmplt May 30, 2022
b88ffd6
atsame70_xpro: apply example clock hierarchy configuration
tmplt May 30, 2022
27f5bc0
hal/pmc: improve struct/enum docs
tmplt May 30, 2022
0e6a9c5
hal/pmc: remove unused enum
tmplt May 30, 2022
dd1a9dc
hal/pmc: get_slck: remove impl details from docs
tmplt May 30, 2022
009d478
hal/pmc: improve top-level docs
tmplt May 30, 2022
f74507d
atsame70_xpro: expose UPLLCKDIV on PCK2, @ 2.4MHz
tmplt May 30, 2022
82a87de
hal/pmc: globalize magic values
tmplt May 31, 2022
828299f
hal/pmc: apply formatting
tmplt May 31, 2022
b89177d
hal/pmc: refactor SCLK configuration to match MAINCK
tmplt May 31, 2022
d249979
hal/pmc: remove outdated comment
tmplt May 31, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[workspace]
members = [
"hal",
"pac/*",
]
[workspace]
members = [
"hal",
"pac/*",
]
exclude = [
"boards/atsame70_xpro"
]
1 change: 1 addition & 0 deletions boards/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/Cargo.lock
8 changes: 8 additions & 0 deletions boards/atsame70_xpro/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[target.thumbv7em-none-eabihf]
runner = "arm-none-eabi-gdb -q -x openocd.gdb"

[build]
target = "thumbv7em-none-eabihf"
rustflags = [
"-C", "link-arg=-Tlink.x",
]
22 changes: 22 additions & 0 deletions boards/atsame70_xpro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "atsame70_xpro"
version = "0.1.0"
edition = "2021"
authors = [
"Viktor Sonesten <[email protected]>"
]
license = "0BSD"

[dependencies]
cortex-m-rtic = "1.0"
cortex-m = "0.7"
panic-halt = "0.2"

[dependencies.atsamx7x-hal]
version = "0.1.0"
path = "../../hal"
features = ["same70q21b-rt"]

[profile.dev]
debug = true
lto = true
31 changes: 31 additions & 0 deletions boards/atsame70_xpro/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# SAM E70 Xplained Pro Evaluation Kit examples

This crate provides HAL usage examples for working with the [SAM E70 Xplained Pro Evaulation Kit](https://www.microchip.com/en-us/development-tool/DM320113).
The examples are written in [RTIC](https://rtic.rs).

## Prerequisites
* Install the cross-compilation toolchain: `rustup target add thumbv7em-none-eabihf`.
* Install [openocd `v0.11.0`](https://openocd.org/openocd-0-11-0-released.html) (or above).

## Flashing an example
First, the General-Purpose Non-Volatile-Memory (GPNVM) boot bit must be set in order to map the flashed firmware to address `0x0`;
required for the interrupts and software resets to work as expected:
1. Connect the board via the "Debug USB" port and run
```shell
$ openocd -f openocd.cfg -c "atsamv gpnvm set 1" -c exit
```
2. Power-cycle the board, and the verify with
```shell
$ openocd -f openocd.cfg -c "atsamv gpnvm show 1" -c exit
[...]
samv-gpnvm1: 1
```

The example can now be flashed after first, in a seperate shell, `openocd -f openocd.cfg`
```shell
$ cargo run
```

## Erasing bad firmware
In case the board has been flashed with software that sets it in a bad state before the debugger can attach, bridge the "ERASE" header (J400; north of the MCU) between power cycles, and remove the bridge.
The flash area has now been zeroed.
31 changes: 31 additions & 0 deletions boards/atsame70_xpro/memory.x
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
MEMORY
{
FLASH : ORIGIN = 0x400000, LENGTH = 2M
RAM : ORIGIN = 0x20400000, LENGTH = 256K /* 256K or 384K; p. 54 */
}

/* This is where the call stack will be allocated. */
/* The stack is of the full descending type. */
/* You may want to use this variable to locate the call stack and static
variables in different memory regions. Below is shown the default value */
/* _stack_start = ORIGIN(RAM) + LENGTH(RAM); */

/* You can use this symbol to customize the location of the .text section */
/* If omitted the .text section will be placed right after the .vector_table
section */
/* This is required only on microcontrollers that store some configuration right
after the vector table */
/* _stext = ORIGIN(FLASH) + 0x400; */

/* Example of putting non-initialized variables into custom RAM locations. */
/* This assumes you have defined a region RAM2 above, and in the Rust
sources added the attribute `#[link_section = ".ram2bss"]` to the data
you want to place there. */
/* Note that the section will not be zero-initialized by the runtime! */
/* SECTIONS {
.ram2bss (NOLOAD) : ALIGN(4) {
*(.ram2bss);
. = ALIGN(4);
} > RAM2
} INSERT AFTER .bss;
*/
6 changes: 6 additions & 0 deletions boards/atsame70_xpro/openocd.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
source [find interface/cmsis-dap.cfg]
set CHIPNAME atsame70n21
source [find target/atsamv.cfg]

init
reset halt
35 changes: 35 additions & 0 deletions boards/atsame70_xpro/openocd.gdb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
target extended-remote :3333

set print asm-demangle on

# detect unhandled exceptions, hard faults and panics
break DefaultHandler
break HardFault
break rust_begin_unwind

# *try* to stop at the user entry point (it might be gone due to inlining)
# break main

monitor arm semihosting enable
monitor halt

load

# start the process but immediately halt the processor
stepi

# Helpers
define reload
monitor reset halt
continue
end

define reflash
!cargo build
monitor reset halt
load
continue
end

alias rl = reload
alias rf = reflash
94 changes: 94 additions & 0 deletions boards/atsame70_xpro/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#![no_std]
#![no_main]

use panic_halt as _;

#[rtic::app(device = atsamx7x_hal::target_device, peripherals = true, dispatchers = [IXC])]
mod app {
use atsamx7x_hal as hal;
use hal::ehal::watchdog::WatchdogDisable;

#[shared]
struct Shared {}

#[local]
struct Local {}

#[init]
fn init(mut ctx: init::Context) -> (Shared, Local, init::Monotonics) {
cortex_m::asm::bkpt();

// Disable the watchdog.
hal::watchdog::Watchdog::new(ctx.device.WDT).disable();

let mut efc = {
use hal::efc::{Efc, VddioLevel};
Efc::new(ctx.device.EFC, VddioLevel::V3)
};

// Configure the clock hierarchy
{
use hal::pmc::{
HostClockConfig, MainCkSource, MckDivider, MckPrescaler, Megahertz, PckId,
PllaConfig, Pmc, UpllDivider,
};

let mut pmc = Pmc::new(ctx.device.PMC);
let mainck = pmc
.get_mainck(MainCkSource::ExternalBypass(Megahertz::from_raw(12)))
.unwrap();
let _plla = pmc
.get_pllack(PllaConfig { div: 1, mult: 8 }, &mainck)
.unwrap();
let _hclk = pmc
.get_hclk(
HostClockConfig {
pres: MckPrescaler::CLK_1,
div: MckDivider::EQ_PCK,
},
&mainck,
&mut efc,
)
.unwrap();

let upllck = pmc.get_upllck(&mainck, &mut ctx.device.UTMI).unwrap();
let upllckdiv = pmc.get_upllckdiv(&upllck, UpllDivider::Div2);
let _pck2 = pmc.get_pck(&upllckdiv, 100 - 1, PckId::Pck2); // @ 2.4MHz
}
Comment on lines +29 to +57
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't be more readable if these are separate functions?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean. What functions here could be further separated?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole function is long but seems to have clear separation into stages. To improve readability they could be shorter private functions with descriptive names.


// Configure PA03 as PCK2 output
{
let pioa = ctx.device.PIOA;

// Configure pins for function C: UART4 (0b10)
pioa.pio_abcdsr[1].modify(|_, w| w.p3().set_bit());
pioa.pio_abcdsr[0].modify(|_, w| w.p3().clear_bit());

// Give pins to the peripheral.
pioa.pio_pdr.write(|w| w.p3().set_bit());
cortex_m::asm::dsb();
assert!(pioa.pio_psr.read().p3().bit_is_clear());

// disable multidrive
pioa.pio_mddr.write(|w| w.p3().set_bit());
cortex_m::asm::dsb();
assert!(pioa.pio_mdsr.read().p3().bit_is_clear());

// ensure we dont pull the pin up/down
pioa.pio_pudr.write(|w| w.p3().set_bit());
pioa.pio_ppddr.write(|w| w.p3().set_bit());
cortex_m::asm::dsb();
assert!(pioa.pio_pusr.read().p3().bit_is_set());
assert!(pioa.pio_ppdsr.read().p3().bit_is_set());
}

(Shared {}, Local {}, init::Monotonics())
}

#[idle]
fn idle(_: idle::Context) -> ! {
loop {
cortex_m::asm::nop();
}
}
}
Loading