Skip to content

Commit 712e071

Browse files
committed
move to xargo and nightly
1 parent 8edb8bd commit 712e071

File tree

9 files changed

+75
-62628
lines changed

9 files changed

+75
-62628
lines changed

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ volatile-register = "0.1.2"
1010
debug = true
1111

1212
[profile.release]
13-
debug = true
1413
lto = true

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
TARGET = target/msp430g2553/release/msp
1+
DEVICE = msp430g2553
2+
TARGET = target/$(DEVICE)/release/msp
23

34
all:
4-
RUSTFLAGS="--sysroot sysroot" cargo build --release --target=msp430g2553
5+
xargo build --release --target $(DEVICE)
56
msp430-elf-objdump -Cd $(TARGET) > $(TARGET).lst
67
msp430-elf-size $(TARGET)
78

README.md

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,60 @@
44
55
## Compiling
66

7-
Unfortunately, it is not possible to build this project without patches
8-
form [#38286](https://github.com/rust-lang/rust/pull/38286) and [#38240](https://github.com/rust-lang/rust/pull/38240), so we need to wait until they land in nightly.
7+
This project can be compiled using nightly rust and [xargo](https://github.com/japaric/xargo).
8+
9+
Tested using version `1.15.0-nightly (8f02c429a 2016-12-15)`.
10+
11+
Steps:
12+
* First, install msp430-elf-gcc compiler, and make sure it is in your $PATH.
13+
You can get it from [here](http://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSPGCC/latest/index_FDS.html).
14+
* Install nightly rust
15+
16+
`$ rustup default nightly`
17+
* Install xargo
18+
19+
`$ cargo install xargo`
20+
* Build the project using
21+
22+
`$ make`
23+
24+
* or you can build it using xargo directly (if you don't like `make`)
25+
26+
`$ xargo build --release --target msp430g2553`
27+
28+
* Flash the firmware using `mspdebug`
29+
30+
`$ make prog`
931

1032
## How it works
1133

12-
* First issue you may run into is incompatibility between MSP430 and MSP430X. When `rustc` compiles code it does not pass `-mmcu` flag to the gcc, so gcc choses default ISA. For `msp430-elf-gcc` it is MSP430X, and it is not what you want for `msp430g2553` MCU. To work around this, you should create a *shim* compiler like [`msp-gcc.sh`](https://github.com/pftbest/rust_on_msp/blob/master/msp-gcc.sh) that will pass the necessary flags to gcc.
34+
* One of the issues you may run into is incompatibility between MSP430 and
35+
MSP430X. When `rustc` compiles code it does not pass `-mmcu` flag to the gcc,
36+
so gcc choses default ISA. For `msp430-elf-gcc` it is MSP430X, and it is not
37+
what you want for `msp430g2553` MCU. To work around this, you should create
38+
a *shim* compiler like [`msp-gcc.sh`](https://github.com/pftbest/rust_on_msp/blob/master/msp-gcc.sh)
39+
that will pass the necessary flags to gcc.
40+
41+
* This project is does not use default startup code from gcc, so a reset handler should be defined like this:
42+
```rust
43+
#[no_mangle]
44+
#[link_section = "__interrupt_vector_reset"]
45+
pub static RESET_VECTOR: unsafe extern "C" fn() -> ! = main;
46+
```
47+
RESET_VECTOR is just a function pointer that gets placed inside a special section called
48+
`__interrupt_vector_reset` and is pointing to `main` function, so it will be called on reset.
49+
Other interrupts may be defined the same way for example `__interrupt_vector_timer0_a0`, but it
50+
is not possible to use interrupts yet (other than reset), because rust doesn't have a special
51+
calling convention for MSP430 interrupts.
1352

14-
* Second issue is that rust compiler will pass the `-nodefaultlibs` flag to gcc by default, and for some reason (unknown to me) gcc will fail to emit startup code with this flag present. To fix this you should set `"no-default-libraries": false` option in json file for your target.
53+
## Proting to other boards and MCUs
1554

16-
* Third issue, well it is not an issue, just an inconvenience. I was using a custom rust compiler with patches mentioned above, and found out that `xargo` does not support custom compilers. So I had two options: either add `libcore` crate sources as a dependency in my `Cargo.toml`, or to compile `libcore` separately and put its binary into `sysroot`. I chose the latter case, because it allows me to use other crates like `volatile-register` without changing their source code.
55+
To run this code on the other boards and MCUs, you need to change it in few places:
56+
* Get a linker script for your MCU from msp430-elf-gcc include directory, and place it
57+
inside `ldscripts` folder.
58+
* Rename `msp430g2553.json` and modify it to point to the right liker script from step 1
59+
* Modify `msp-gcc.sh` so it would pass the right mcu name to the gcc.
60+
* Modify `DEVICE` variable inside a Makefile.
1761

1862
## Board
1963

File renamed without changes.
File renamed without changes.

msp430g2553.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,16 @@
55
"features": "-ext",
66
"llvm-target": "msp430",
77
"max-atomic-width": 0,
8-
"no-default-libraries": false,
98
"no-integrated-as": true,
109
"os": "none",
1110
"panic-strategy": "abort",
1211
"relocation-model": "static",
1312
"target-endian": "little",
1413
"target-pointer-width": "16",
15-
"linker": "./msp-gcc.sh"
14+
"linker": "./msp-gcc.sh",
15+
"post-link-args": [
16+
"-Lldscripts",
17+
"-Tmsp430g2553.ld",
18+
"-nostartfiles"
19+
]
1620
}

src/main.rs

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ extern "C" {
1212
static mut P1OUT: RW<u8>;
1313
}
1414

15+
#[no_mangle]
16+
#[link_section = "__interrupt_vector_reset"]
17+
pub static RESET_VECTOR: unsafe extern "C" fn() -> ! = main;
18+
19+
pub unsafe extern "C" fn main() -> ! {
20+
WDTCTL.write(0x5A00 + 0x80);
21+
P1DIR.write(0b0100_0001);
22+
P1OUT.write(0x01);
23+
loop {
24+
P1OUT.modify(|x| !x);
25+
delay(40000);
26+
}
27+
}
28+
1529
#[allow(unused_variables)]
1630
#[allow(unused_assignments)]
1731
fn delay(mut n: u16) {
@@ -23,19 +37,7 @@ fn delay(mut n: u16) {
2337
}
2438

2539
#[no_mangle]
26-
pub extern "C" fn main() {
27-
unsafe {
28-
WDTCTL.write(0x5A00 + 0x80);
29-
P1DIR.write(0b0100_0001);
30-
P1OUT.write(0x01);
31-
}
32-
loop {
33-
unsafe {
34-
P1OUT.modify(|x| {!x});
35-
}
36-
delay(40000);
37-
}
38-
}
39-
4040
#[lang = "panic_fmt"]
41-
extern "C" fn panic_fmt() {}
41+
pub extern "C" fn panic_fmt() -> ! {
42+
loop {}
43+
}

0 commit comments

Comments
 (0)