Skip to content

Commit 2ac8548

Browse files
committed
Make the M3 example work with Rust stable toolchain
1 parent 5b1fb67 commit 2ac8548

File tree

8 files changed

+91
-142
lines changed

8 files changed

+91
-142
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ jobs:
4242
freertos-rust-stable:
4343
name: Build freertos-rust using stable
4444
runs-on: ubuntu-latest
45-
env:
46-
RUSTFLAGS: -D warnings # Warnings disabled only in CI
45+
# env:
46+
# RUSTFLAGS: -D warnings # Warnings disabled only in CI
4747
steps:
4848
- name: Clone
4949
uses: actions/checkout@v3
@@ -70,8 +70,8 @@ jobs:
7070
# target: x86_64-pc-windows-gnu
7171
#- example: linux
7272
# target: x86_64-unknown-linux-gnu
73-
#- example: stm32-cortex-m3
74-
# target: thumbv7m-none-eabi
73+
- example: stm32-cortex-m3
74+
target: thumbv7m-none-eabi
7575
- example: stm32-cortex-m4-blackpill
7676
target: thumbv7em-none-eabihf
7777
#- example: nrf9160

README.md

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,61 +13,68 @@ In contrast to freertos.rs this crate differs in these points:
1313

1414
## How it works
1515

16-
The `freertos-cargo-build` build-dependency compiles the FreeRTOS code from its original "C" sources files into an
17-
archive to be linked against your Rust app. Internally it uses the [cc crate](https://docs.rs/crate/cc) and some meta
16+
The `freertos-cargo-build` build-dependency compiles the FreeRTOS code from its original "C" sources files into an
17+
archive to be linked against your Rust app. Internally it uses the [cc crate](https://docs.rs/crate/cc) and some meta
1818
info provided by your apps `build.rs`:
19-
19+
2020
1. A path to the [FreeRTOS](https://github.com/FreeRTOS/FreeRTOS-Kernel) `Sources`
2121
1. A path to the app specific `FreeRTOSConfig.h`
2222
1. A relative path to the `FreeRTOS port` to be used, e.g. for ARM Cortex-M3 cores.
2323
1. Optional: Additional C code to be compiled
24-
25-
The `freertos-rust` dependency provides an interface to access all FreeRTOS functionality from your (embedded)
24+
25+
The `freertos-rust` dependency provides an interface to access all FreeRTOS functionality from your (embedded)
2626
Rust app.
27-
27+
2828
## Usage
2929

30-
1. Checkout FreeRTOS: https://github.com/FreeRTOS/FreeRTOS-Kernel
30+
1. Checkout FreeRTOS: https://github.com/FreeRTOS/FreeRTOS-Kernel
3131

3232
1. Add dependencies to your Rust apps `Cargo.toml`
33-
34-
```
33+
```toml
3534
[dependencies]
3635
freertos-rust = "*"
37-
36+
3837
[build-dependencies]
3938
freertos-cargo-build = "*"
4039
```
41-
40+
4241
1. Add this snippet to your apps `build.rs`:
43-
```
42+
```rust
4443
fn main() {
4544
let mut b = freertos_cargo_build::Builder::new();
46-
45+
4746
// Path to FreeRTOS kernel or set ENV "FREERTOS_SRC" instead
4847
b.freertos("path/to/FreeRTOS-Kernel");
49-
b.freertos_config("src"); // Location of `FreeRTOSConfig.h`
50-
b.freertos_port("GCC/ARM_CM3"); // Port dir relativ to 'FreeRTOS-Kernel/portable'
51-
b.heap("heap_4.c"); // Set the heap_?.c allocator to use from
52-
// 'FreeRTOS-Kernel/portable/MemMang' (Default: heap_4.c)
53-
48+
b.freertos_config("src"); // Location of `FreeRTOSConfig.h`
49+
b.freertos_port("GCC/ARM_CM3"); // Port dir relativ to 'FreeRTOS-Kernel/portable'
50+
b.heap("heap_4.c"); // Set the heap_?.c allocator to use from
51+
// 'FreeRTOS-Kernel/portable/MemMang' (Default: heap_4.c)
52+
5453
// b.get_cc().file("More.c"); // Optional additional C-Code to be compiled
55-
54+
5655
b.compile().unwrap_or_else(|e| { panic!("{}", e.to_string()) });
5756
}
58-
```
57+
```
5958

6059
### Used C compiler
6160
`freertos-cargo-build` depends on the [cc crate](https://docs.rs/crate/cc). So the C compiler
62-
used can be set by using the `CC` enviroment variable or otherwise defined by internal
61+
used can be set by using the `CC` enviroment variable or otherwise defined by internal
6362
defaults. For the ARM architecture this is the `arm-none-eabi-gcc` which can be found [here](https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads).
6463

64+
Install:
65+
```shell
66+
# on Ubuntu
67+
sudo apt-get install -y gcc-arm-none-eabi
68+
# on Windows
69+
scoop install gcc-arm-none-eabi
70+
```
71+
6572
## Examples
6673
To get started there are examples in [freertos-rust-examples](freertos-rust-examples) for:
6774

6875
* Cortex M33 (nRF9160)
69-
* Cortex M3 (STM32L151CBU6A)
70-
* Cortex M4 (STM32F411CEU6)
76+
* Cortex M3 (STM32F103C8)
77+
* Cortex M4 (STM32F411CE)
7178
* Windows
7279
* ...more to come...
7380

@@ -80,7 +87,3 @@ To get started there are examples in [freertos-rust-examples](freertos-rust-exam
8087
This repository is using the MIT License. Some parts might state different licenses that need to be respected when used.
8188

8289
* The [Linux port](https://github.com/michaelbecker/freertos-addons) is licensed under GPLv2
83-
84-
85-
86-

freertos-rust-examples/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ cortex-m-rt = "0.7"
1818

1919
# Example: stm32-cortex-m3
2020
[target.thumbv7m-none-eabi.dependencies]
21-
panic-halt = "0.2.0"
22-
stm32l1xx-hal = {version = "0.1.0", features = ["stm32l151"], default-features = false}
21+
stm32f1xx-hal = {version = "0.10", features = ["rt", "stm32f103", "medium"]}
2322

2423
# Example: stm32-cortex-m4-blackpill
2524
[target.thumbv7em-none-eabihf.dependencies]

freertos-rust-examples/README.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,16 +80,14 @@ Run the example
8080

8181
### Run STM32 Cortex-M3 Demo
8282

83-
we need the nightly build for some features like allocator_api:
83+
We use stable toolchain and the target is thumbv7m-none-eabi:
8484

85-
rustup default nightly-x86_64-pc-windows-gnu // TODO: Build does not finish with GNU Toolchain
86-
rustup default nightly-x86_64-pc-windows-msvc
8785
rustup target add thumbv7m-none-eabi
88-
86+
8987
Build the binary:
9088

9189
cargo build --package freertos-rust-examples --example stm32-cortex-m3 --target thumbv7m-none-eabi
92-
90+
9391
Create hex file to be flashed:
9492

9593
cargo objcopy --example stm32-cortex-m3 --target thumbv7m-none-eabi -- -O ihex stm32-cortex-m3.hex

freertos-rust-examples/examples/stm32-cortex-m3/FreeRTOSConfig.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ extern void vAssertCalled( const char * const pcFileName, unsigned long ulLine )
9393
#define configUSE_PREEMPTION 1
9494
#define configUSE_IDLE_HOOK 0
9595
#define configUSE_TICK_HOOK 0
96-
#define configCPU_CLOCK_HZ ( 4200000UL ) //also systick runs at this frequency
96+
#define configCPU_CLOCK_HZ ( 72000000UL ) //also systick runs at this frequency
9797
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 ) //1000=1ms per tick, 100=10ms per tick
9898
#define configMAX_PRIORITIES ( 5 )
9999
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 80 )
@@ -104,7 +104,7 @@ extern void vAssertCalled( const char * const pcFileName, unsigned long ulLine )
104104
#define configIDLE_SHOULD_YIELD 1
105105
#define configUSE_MUTEXES 1
106106
#define configQUEUE_REGISTRY_SIZE 5
107-
#define configCHECK_FOR_STACK_OVERFLOW 2
107+
#define configCHECK_FOR_STACK_OVERFLOW 0
108108
#define configUSE_RECURSIVE_MUTEXES 0
109109
#define configUSE_MALLOC_FAILED_HOOK 0 //TR
110110
#define configUSE_APPLICATION_TASK_TAG 0

freertos-rust-examples/examples/stm32-cortex-m3/layout.ld

Lines changed: 0 additions & 5 deletions
This file was deleted.
Lines changed: 48 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,121 +1,75 @@
11
#![no_std]
22
#![no_main]
3-
// For allocator
4-
#![feature(lang_items)]
5-
#![feature(alloc_error_handler)]
6-
7-
extern crate panic_halt; // you can put a breakpoint on `rust_begin_unwind` to catch panics
83

4+
use core::panic::PanicInfo;
95
use cortex_m::asm;
106
use cortex_m_rt::exception;
117
use cortex_m_rt::{entry, ExceptionFrame};
128
use freertos_rust::*;
13-
use core::alloc::Layout;
14-
use stm32l1xx_hal as hal;
15-
use stm32l1xx_hal::hal::digital::v2::*;
16-
use stm32l1xx_hal::gpio::*;
17-
use stm32l1xx_hal::gpio::gpioa::PA1;
18-
9+
use stm32f1xx_hal::{gpio::PinState, pac, prelude::*};
1910

2011
#[global_allocator]
2112
static GLOBAL: FreeRtosAllocator = FreeRtosAllocator;
2213

23-
fn delay() {
24-
let mut _i = 0;
25-
for _ in 0..2_00 {
26-
_i += 1;
27-
}
28-
}
29-
30-
fn delay_n(n: i32) {
31-
for _ in 0..n {
32-
delay();
33-
}
14+
#[entry]
15+
fn main() -> ! {
16+
Task::new()
17+
.name("default")
18+
.stack_size(1000)
19+
.start(move |_| {
20+
app_main();
21+
})
22+
.unwrap();
23+
FreeRtosUtils::start_scheduler();
3424
}
3525

36-
static mut LED: Option<PA1<Output<PushPull>>> = None;
37-
38-
fn set_led(on: bool) {
39-
unsafe {
40-
let mut led = LED.take().unwrap();
41-
if on {
42-
led.set_low();
43-
} else {
44-
led.set_high();
45-
}
46-
}
47-
}
26+
fn app_main() -> ! {
27+
let dp = pac::Peripherals::take().unwrap();
28+
let mut flash = dp.FLASH.constrain();
29+
let rcc = dp.RCC.constrain();
30+
let _clocks = rcc
31+
.cfgr
32+
.use_hse(8.MHz())
33+
.sysclk(72.MHz())
34+
.freeze(&mut flash.acr);
35+
36+
let mut gpio = dp.GPIOB.split();
37+
let mut led = gpio
38+
.pb0
39+
.into_open_drain_output_with_state(&mut gpio.crl, PinState::High);
4840

49-
// Setup IO for the LED and blink, does not return.
50-
fn do_blink_forever() -> ! {
5141
loop {
52-
delay();
53-
set_led(true);
54-
delay();
55-
set_led(false);
42+
CurrentTask::delay(Duration::ms(500));
43+
led.set_low();
44+
CurrentTask::delay(Duration::ms(500));
45+
led.set_high();
5646
}
5747
}
5848

59-
#[entry]
60-
fn main() -> ! {
61-
let dp = hal::stm32::Peripherals::take().unwrap();
62-
63-
// Set up the LED, it's connected to pin PA1.
64-
let gpioa: stm32l1xx_hal::gpio::gpioa::Parts = dp.GPIOA.split();
65-
unsafe { LED = Some(gpioa.pa1.into_push_pull_output()); }
66-
67-
// Initial blink
68-
set_led(true);
69-
delay_n(10);
70-
set_led(false);
71-
delay_n(10);
72-
73-
// Just blink (does NOT work!)
74-
do_blink_forever();
75-
76-
// TODO: What comes now does not work yet!
77-
// Initialize Tasks and start FreeRTOS
78-
Task::new().name("hello").stack_size(128).priority(TaskPriority(1)).start(|_this_task| {
79-
// Just blink
80-
freertos_rust::CurrentTask::delay(Duration::ms(1000));
81-
set_led(true);
82-
freertos_rust::CurrentTask::delay(Duration::ms(1000));
83-
set_led(false);
84-
}).unwrap();
85-
86-
87-
// TODO: Starting the scheduler fails, we need debugging to find the issue
88-
// Seems like we don't even get an assert
89-
FreeRtosUtils::start_scheduler();
90-
}
91-
49+
#[allow(non_snake_case)]
9250
#[exception]
93-
fn DefaultHandler(_irqn: i16) {
94-
// custom default handler
95-
// irqn is negative for Cortex-M exceptions
96-
// irqn is positive for device specific (line IRQ)
97-
// set_led(true);(true);
98-
// panic!("Exception: {}", irqn);
51+
unsafe fn DefaultHandler(_irqn: i16) {
52+
// custom default handler
53+
// irqn is negative for Cortex-M exceptions
54+
// irqn is positive for device specific (line IRQ)
55+
// set_led(true);(true);
56+
// panic!("Exception: {}", irqn);
57+
asm::bkpt();
58+
loop {}
9959
}
10060

101-
61+
#[allow(non_snake_case)]
10262
#[exception]
103-
fn HardFault(_ef: &ExceptionFrame) -> ! {
104-
// Blink 3 times long when exception occures
105-
delay_n(10);
106-
for _ in 0..3 {
107-
set_led(true);
108-
delay_n(10);
109-
set_led(false);
110-
delay_n(5);
111-
}
63+
unsafe fn HardFault(_ef: &ExceptionFrame) -> ! {
64+
// Blink 3 times long when exception occures
65+
asm::bkpt();
11266
loop {}
11367
}
11468

115-
// define what happens in an Out Of Memory (OOM) condition
116-
#[alloc_error_handler]
117-
fn alloc_error(_layout: Layout) -> ! {
118-
set_led(true);
69+
// We no longer need to use #[alloc_error_handler] since v1.68.
70+
// It will automatically call the panic handler.
71+
#[panic_handler]
72+
fn panic(_info: &PanicInfo) -> ! {
11973
asm::bkpt();
12074
loop {}
121-
}
75+
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
MEMORY
22
{
3-
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 128K
4-
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 32K
5-
}
3+
FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 64K
4+
RAM (rwx) : ORIGIN = 0x20000000, LENGTH = 20K
5+
}

0 commit comments

Comments
 (0)