Skip to content

Commit

Permalink
Merge pull request #4 from rust-embedded/rt-up
Browse files Browse the repository at this point in the history
bring up to par with cortex-m-rt v0.6.x
  • Loading branch information
cr1901 authored Dec 22, 2019
2 parents e662999 + 5d238d4 commit faee1e3
Show file tree
Hide file tree
Showing 12 changed files with 942 additions and 446 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/*.rs.bk
.#*
Cargo.lock
target/
17 changes: 14 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,26 @@ authors = [
categories = ["embedded", "no-std"]
description = "Minimal runtime / startup for MSP430 microcontrollers"
documentation = "https://docs.rs/msp430-rt"
edition = "2018"
keywords = ["msp430", "runtime", "startup"]
license = "MIT OR Apache-2.0"
name = "msp430-rt"
repository = "https://github.com/pftbest/msp430-rt"
version = "0.1.4"
version = "0.2.0"

[dependencies]
msp430 = "0.1.0"
r0 = "0.2.2"

[build-dependencies]
rustc_version = "0.2.1"
[dependencies.msp430-rt-macros]
version = "0.2.0"
path = "macros"

[features]
device = ["msp430-rt-macros/device"]

[package.metadata.docs.rs]
features = ["device"]

[workspace]
members = ["macros"]
7 changes: 7 additions & 0 deletions asm.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.section .ResetTrampoline, "ax"
.global ResetTrampoline
.type ResetTrampoline,%function
ResetTrampoline:
mov #_stack_start,r1
br #Reset ; XXX "br Reset" should also work, but doesn't on G2553,
; and I don't know why.
15 changes: 15 additions & 0 deletions assemble.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash

set -euxo pipefail

# cflags taken from cc 1.0.22

crate=msp430-rt

# remove existing blobs because otherwise this will append object files to the old blobs
rm -f bin/*.a

msp430-elf-as -mcpu=msp430 asm.s -o bin/$crate.o
ar crs bin/msp430-none-elf.a bin/$crate.o

rm bin/$crate.o
Binary file added bin/msp430-none-elf.a
Binary file not shown.
42 changes: 30 additions & 12 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
extern crate rustc_version;

use std::env;
use std::fs::File;
use std::io::Write;
use std::path::PathBuf;
use std::{env, fs, fs::File, io::Write, path::PathBuf};

fn main() {
println!("cargo:rustc-cfg=has_termination_lang");
let target = env::var("TARGET").unwrap();
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());

if target == "msp430-none-elf" {
fs::copy(format!("bin/{}.a", target), out_dir.join("libmsp430-rt.a")).unwrap();
println!("cargo:rustc-link-lib=static=msp430-rt");
}

// Put the linker script somewhere the linker can find it
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());
File::create(out.join("link.x"))
.unwrap()
.write_all(include_bytes!("link.x"))
.unwrap();
println!("cargo:rustc-link-search={}", out.display());
let link_x = include_bytes!("link.x.in");
if env::var_os("CARGO_FEATURE_DEVICE").is_some() {
let mut f = File::create(out.join("link.x")).unwrap();

f.write_all(link_x).unwrap();

// *IMPORTANT*: The weak aliases (i.e. `PROVIDED`) must come *after* `EXTERN(__INTERRUPTS)`.
// Otherwise the linker will ignore user defined interrupts and always populate the table
// with the weak aliases.
writeln!(
f,
r#"
/* Provides weak aliases (cf. PROVIDED) for device specific interrupt handlers */
/* This will usually be provided by a device crate generated using svd2rust (see `device.x`) */
INCLUDE device.x"#
).unwrap();
} else {
let mut f = File::create(out.join("link.x")).unwrap();
f.write_all(link_x).unwrap();
};

println!("cargo:rustc-link-search={}", out_dir.display());

println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=link.x");
Expand Down
107 changes: 0 additions & 107 deletions link.x

This file was deleted.

100 changes: 100 additions & 0 deletions link.x.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
INCLUDE memory.x

/* Entry point */
ENTRY(Reset);
EXTERN(__RESET_VECTOR);

/* Create an undefined reference to the INTERRUPTS symbol. This is required to
force the linker to *not* drop the INTERRUPTS symbol if it comes from an
object file that's passed to the linker *before* this crate */
EXTERN(__INTERRUPTS);

/* # Pre-initialization function */
/* If the user overrides this using the `pre_init!` macro or by creating a `__pre_init` function,
then the function this points to will be called before the RAM is initialized. */
PROVIDE(PreInit = PreInit_);

/* # Default interrupt handler */
EXTERN(DefaultHandler); /* If this line is not here, all unused interrupt
handlers will be zeroed out instead of doing
to the DefaultHandler! */
PROVIDE(DefaultHandler = DefaultHandler_);

/* XXX Are there use cases for making this user overridable? */
_stack_start = ORIGIN(RAM) + LENGTH(RAM);

SECTIONS
{
.vector_table ORIGIN(VECTORS) : ALIGN(2)
{
KEEP(*(.vector_table.interrupts));
KEEP(*(.__RESET_VECTOR));
} > VECTORS

.text ORIGIN(ROM) :
{
/* Put the reset handler and its trampoline at the beginning of the .text section */
KEEP(*(.ResetTrampoline));
KEEP(*(.Reset));

*(.text .text.*);
} > ROM

.rodata : ALIGN(2)
{
*(.rodata .rodata.*);
. = ALIGN(2);
} > ROM

.bss : ALIGN(2)
{
_sbss = .;
*(.bss .bss.*);
. = ALIGN(2);
_ebss = .;
} > RAM

.data : ALIGN(2)
{
_sidata = LOADADDR(.data);
_sdata = .;
*(.data .data.*);
. = ALIGN(2);
_edata = .;
} > RAM AT > ROM

/* fake output .got section */
/* Dynamic relocations are unsupported. This section is only used to detect
relocatable code in the input files and raise an error if relocatable code
is found */
.got :
{
_sgot = .;
KEEP(*(.got .got.*));
_egot = .;
} > RAM AT > ROM

/* The heap starts right after the .bss + .data section ends */
_sheap = _edata;
}

/* Do not exceed this mark in the error messages below | */
ASSERT(ORIGIN(VECTORS) + LENGTH(VECTORS) == 0x10000, "
ERROR(msp430-rt): The VECTORS memory region must end at address 0x10000. Check memory.x");

ASSERT(ADDR(.vector_table) + SIZEOF(.vector_table) == 0x10000, "
ERROR(msp430-rt): .vector_table is shorter than expected.
Possible solutions, from most likely to less likely:
- Link to a svd2rust generated pac crate, if you are not
- Fix _sinterrupts in memory.x; it doesn't match the number of interrupts provided by the
pac crate
- Disable the 'device' feature of msp430-rt to build a generic application; a dependency
may be enabling it
");

ASSERT(_sgot == _egot, "
ERROR(msp430-rt): .got section detected in the input object files
Dynamic relocations are not supported. If you are linking to C code compiled using
the 'cc' crate then modify your build script to compile the C code _without_
the -fPIC flag. See the documentation of the `cc::Build.pic` method for details.");
/* Do not exceed this mark in the error messages above | */
22 changes: 22 additions & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "msp430-rt-macros"
version = "0.2.0"
authors = ["Jorge Aparicio <[email protected]>"]

[lib]
proc-macro = true

[dependencies]
quote = "0.6.10"
proc-macro2 = "0.4.23"

[dependencies.rand]
default-features = false
version = "0.6.0"

[dependencies.syn]
features = ["extra-traits", "full"]
version = "0.15.20"

[features]
device = []
Loading

0 comments on commit faee1e3

Please sign in to comment.