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

Added STM32F412G Discovery kit #1916

Merged
merged 17 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ members = [
"boards/opentitan",
"boards/redboard_artemis_nano",
"boards/stm32f3discovery",
"boards/stm32f412gdiscovery",
"capsules",
"chips/apollo3",
"chips/arty_e21_chip",
Expand All @@ -35,6 +36,7 @@ members = [
"chips/stm32f303xc",
"chips/stm32f429zi",
"chips/stm32f446re",
"chips/stm32f412g",
"chips/stm32f4xx",
"kernel",
"libraries/enum_primitive",
Expand Down
1 change: 1 addition & 0 deletions boards/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ that Tock supports.
| [ST Nucleo F446RE](nucleo_f446re/README.md) | ARM Cortex-M4 | STM32F446 | openocd | custom |
| [ST Nucleo F429ZI](nucleo_f429zi/README.md) | ARM Cortex-M4 | STM32F429 | openocd | custom |
| [STM32F3Discovery kit](stm32f3discovery/README.md) | ARM Cortex-M4 | STM32F303VCT6 | openocd | custom |
| [STM32F412G Discovery kit](stm32f412gdiscovery/README.md) | ARM Cortex-M4 | STM32F412G | openocd | custom |
| [SparkFun RedBoard Artemis Nano](redboard_artemis_nano/README.md) | ARM Cortex-M4 | Apollo3 | custom | custom |
| [SiFive HiFive1](hifive1/README.md) | RISC-V | FE310-G000 | openocd | tockloader |
| [Digilent Arty A-7 100T](arty_e21/README.md) | RISC-V RV32IMAC | SiFive E21 | openocd | tockloader |
Expand Down
63 changes: 63 additions & 0 deletions boards/components/src/ft6206.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//! Components for the Ft6206 Touch Panel.
//!
//! Usage
//! -----
//! ```rust
//! let ft6206 = components::ft6206::Ft6206Component::new()
//! .finalize(components::ft6206_i2c_component_helper!(mux_i2c));

//! ```
use capsules::ft6206::Ft6206;
use capsules::virtual_i2c::I2CDevice;
use core::mem::MaybeUninit;
use kernel::component::Component;
use kernel::hil::gpio;
use kernel::static_init_half;

// Setup static space for the objects.
#[macro_export]
macro_rules! ft6206_i2c_component_helper {
($i2c_mux: expr) => {{
use capsules::ft6206::Ft6206;
use capsules::virtual_i2c::I2CDevice;
use core::mem::MaybeUninit;
let i2c = components::i2c::I2CComponent::new($i2c_mux, 0x38)
.finalize(components::i2c_component_helper!());
static mut ft6206: MaybeUninit<Ft6206<'static>> = MaybeUninit::uninit();
(&i2c, &mut ft6206)
};};
}

pub struct Ft6206Component {
interupt_pin: &'static dyn gpio::InterruptPin,
}

impl Ft6206Component {
pub fn new(pin: &'static dyn gpio::InterruptPin) -> Ft6206Component {
Ft6206Component { interupt_pin: pin }
}
}

impl Component for Ft6206Component {
type StaticInput = (
&'static I2CDevice<'static>,
&'static mut MaybeUninit<Ft6206<'static>>,
);
type Output = &'static Ft6206<'static>;

unsafe fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
let ft6206 = static_init_half!(
static_buffer.1,
Ft6206<'static>,
Ft6206::new(
static_buffer.0,
self.interupt_pin,
&mut capsules::ft6206::BUFFER
)
);
static_buffer.0.set_client(ft6206);
self.interupt_pin.set_client(ft6206);

ft6206
}
}
1 change: 1 addition & 0 deletions boards/components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod console;
pub mod crc;
pub mod debug_queue;
pub mod debug_writer;
pub mod ft6206;
pub mod gpio;
pub mod hd44780;
pub mod hmac;
Expand Down
13 changes: 13 additions & 0 deletions boards/stm32f412gdiscovery/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "stm32f412gdiscovery"
version = "0.1.0"
authors = ["Tock Project Developers <tock-dev@googlegroups.com>"]
build = "build.rs"
edition = "2018"

[dependencies]
components = { path = "../components" }
cortexm4 = { path = "../../arch/cortex-m4" }
capsules = { path = "../../capsules" }
kernel = { path = "../../kernel" }
stm32f412g = { path = "../../chips/stm32f412g" }
26 changes: 26 additions & 0 deletions boards/stm32f412gdiscovery/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Makefile for building the tock kernel for the stm32412gdiscovery platform
#
TARGET=thumbv7em-none-eabi
PLATFORM=stm32f412gdiscovery

include ../Makefile.common

OPENOCD=openocd
OPENOCD_OPTIONS=-f openocd.cfg

APP=../../../libtock-c/examples/buttons/build/cortex-m4/cortex-m4.tbf
KERNEL=$(TOCK_ROOT_DIRECTORY)/target/$(TARGET)/debug/$(PLATFORM).elf
KERNEL_WITH_APP=$(TOCK_ROOT_DIRECTORY)/target/$(TARGET)/debug/$(PLATFORM)-app.elf

.PHONY: flash-debug
flash-debug: $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/debug/$(PLATFORM).elf
$(OPENOCD) $(OPENOCD_OPTIONS) -c "init; reset halt; flash write_image erase $<; verify_image $<; reset; shutdown"

.PHONY: flash
flash: $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/release/$(PLATFORM).elf
$(OPENOCD) $(OPENOCD_OPTIONS) -c "init; reset halt; flash write_image erase $<; verify_image $<; reset; shutdown"

.PHONY: program
program: $(TOCK_ROOT_DIRECTORY)target/$(TARGET)/debug/$(PLATFORM).elf
arm-none-eabi-objcopy --update-section .apps=$(APP) $(KERNEL) $(KERNEL_WITH_APP)
$(OPENOCD) $(OPENOCD_OPTIONS) -c "init; reset halt; flash write_image erase $(KERNEL_WITH_APP); verify_image $(KERNEL_WITH_APP); reset; shutdown"
77 changes: 77 additions & 0 deletions boards/stm32f412gdiscovery/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
STM32F4 Discovery Kit with STM32F412G MCU
======================================================

For more details [visit STM32F412G Discovery Kit
website](https://www.st.com/en/evaluation-tools/32f412gdiscovery.html).

## Flashing the kernel

The kernel can be programmed using OpenOCD. `cd` into `boards/discovery_f412g`
directory and run:

```bash
$ make flash

(or)

$ make flash-debug
```

> **Note:** Unlike other Tock platforms, the default kernel image for this
> board will clear flashed apps when the kernel is loaded. This is to support
> the non-tockloader based app flash procedure below. To preserve loaded apps,
> comment out the `APP_HACK` variable in `src/main.rs`.

## Flashing app

Apps are built out-of-tree. Once an app is built, you can use
`arm-none-eabi-objcopy` with `--update-section` to create an ELF image with the
apps included.

```bash
$ arm-none-eabi-objcopy \
--update-section .apps=../../../libtock-c/examples/c_hello/build/cortex-m4/cortex-m4.tbf \
target/thumbv7em-none-eabi/debug/discovery_f412g.elf \
target/thumbv7em-none-eabi/debug/discovery_f412g-app.elf
```

For example, you can update `Makefile` as follows.

```
APP=../../../libtock-c/examples/c_hello/build/cortex-m4/cortex-m4.tbf
KERNEL=$(TOCK_ROOT_DIRECTORY)/target/$(TARGET)/debug/$(PLATFORM).elf
KERNEL_WITH_APP=$(TOCK_ROOT_DIRECTORY)/target/$(TARGET)/debug/$(PLATFORM)-app.elf

.PHONY: program
program: target/$(TARGET)/debug/$(PLATFORM).elf
arm-none-eabi-objcopy --update-section .apps=$(APP) $(KERNEL) $(KERNEL_WITH_APP)
$(OPENOCD) $(OPENOCD_OPTIONS) -c "init; reset halt; flash write_image erase $(KERNEL_WITH_APP); verify_image $(KERNEL_WITH_APP); reset; shutdown"
```

After setting `APP`, `KERNEL`, `KERNEL_WITH_APP`, and `program` target
dependency, you can do

```bash
$ make program
```

to flash the image.

## OpenOCD Note
The release version of openocd does not fully support stm32412g discovery kit. Uploading seems to work
with the setup for nucelo429zi. The openocd.cfg file contains both setups, one being commented.

To install an openocd that full supports stm32f412g you have to build openocd.

```bash
$ git clone --recursive https://git.code.sf.net/p/openocd/code openocd-code
$ cd openocd-code
$ git fetch http://openocd.zylin.com/openocd refs/changes/21/4321/7 && git cherry-pick FETCH_HEAD
$ ./bootstrap
$ ./configure --disable-werror
$ make
# optinally use sudo make install
```

> Please note that you may have some conflicts in a file containing a list of
> sources when patching. Accept both changes.
5 changes: 5 additions & 0 deletions boards/stm32f412gdiscovery/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fn main() {
println!("cargo:rerun-if-changed=layout.ld");
println!("cargo:rerun-if-changed=chip_layout.ld");
println!("cargo:rerun-if-changed=../kernel_layout.ld");
}
14 changes: 14 additions & 0 deletions boards/stm32f412gdiscovery/chip_layout.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* Memory layout for the STM32F412G
* rom = 1MB (LENGTH = 0x01000000)
* kernel = 256KB
* user = 256KB
* ram = 256KB */

MEMORY
{
rom (rx) : ORIGIN = 0x08000000, LENGTH = 0x00040000
prog (rx) : ORIGIN = 0x08040000, LENGTH = 0x00040000
ram (rwx) : ORIGIN = 0x20000000, LENGTH = 0x0003FFFF
}

MPU_MIN_ALIGN = 8K;
2 changes: 2 additions & 0 deletions boards/stm32f412gdiscovery/layout.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
INCLUDE ./chip_layout.ld
INCLUDE ../kernel_layout.ld
21 changes: 21 additions & 0 deletions boards/stm32f412gdiscovery/openocd.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#interface
interface hla
hla_layout stlink
hla_device_desc "ST-LINK/V2-1"
hla_vid_pid 0x0483 0x374b

set WORKAREASIZE 0x40000

source [find target/stm32f4x.cfg]

# patched openocd
# this setup requires a patched version of openocd that fully supports stm32f412g-disco

# #interface
# source [find board/stm32f412g-disco.cfg]
# source [find interface/stlink.cfg]
# hla_serial "\x30\x36\x37\x31\x46\x46\x33\x33\x33\x30\x33\x36\x34\x33\x34\x42\x34\x33\x30\x37\x33\x30\x30\x39"

# set WORKAREASIZE 0x40000


79 changes: 79 additions & 0 deletions boards/stm32f412gdiscovery/src/io.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use core::fmt::Write;
use core::panic::PanicInfo;

use cortexm4;

use kernel::debug;
use kernel::debug::IoWrite;
use kernel::hil::led;
use kernel::hil::uart;
use kernel::hil::uart::Configure;

use stm32f412g;
use stm32f412g::gpio::PinId;

use crate::CHIP;
use crate::PROCESSES;

/// Writer is used by kernel::debug to panic message to the serial port.
pub struct Writer {
initialized: bool,
}

/// Global static for debug writer
pub static mut WRITER: Writer = Writer { initialized: false };

impl Writer {
/// Indicate that USART has already been initialized. Trying to double
/// initialize USART2 causes STM32F412G to go into in in-deterministic state.
pub fn set_initialized(&mut self) {
self.initialized = true;
}
}

impl Write for Writer {
fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
self.write(s.as_bytes());
Ok(())
}
}

impl IoWrite for Writer {
fn write(&mut self, buf: &[u8]) {
let uart = unsafe { &mut stm32f412g::usart::USART2 };

if !self.initialized {
self.initialized = true;

uart.configure(uart::Parameters {
baud_rate: 115200,
stop_bits: uart::StopBits::One,
parity: uart::Parity::None,
hw_flow_control: false,
width: uart::Width::Eight,
});
}

for &c in buf {
uart.send_byte(c);
}
}
}

/// Panic handler.
#[no_mangle]
#[panic_handler]
pub unsafe extern "C" fn panic_fmt(info: &PanicInfo) -> ! {
// User LD2 is connected to PB07
let led = &mut led::LedHigh::new(PinId::PE02.get_pin_mut().as_mut().unwrap());
let writer = &mut WRITER;

debug::panic(
&mut [led],
writer,
info,
&cortexm4::support::nop,
&PROCESSES,
&CHIP,
)
}