|
4 | 4 |
|
5 | 5 | ## Compiling
|
6 | 6 |
|
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` |
9 | 31 |
|
10 | 32 | ## How it works
|
11 | 33 |
|
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. |
13 | 52 |
|
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 |
15 | 54 |
|
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. |
17 | 61 |
|
18 | 62 | ## Board
|
19 | 63 |
|
|
0 commit comments