Skip to content

Commit 5b1fb67

Browse files
committed
Make the M4 example work with Rust stable toolchain
1 parent e12a084 commit 5b1fb67

File tree

5 files changed

+72
-78
lines changed

5 files changed

+72
-78
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ jobs:
9393
target
9494
key: ${{ runner.os }}-${{ runner.arch }}
9595
- name: Install Rust
96-
uses: dtolnay/rust-toolchain@nightly
96+
uses: dtolnay/rust-toolchain@stable
9797
with:
9898
targets: ${{ matrix.target }}
9999
- name: Install cross deps

freertos-rust-examples/Cargo.toml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,8 @@ repository = "https://github.com/lobaro/FreeRTOS-rust"
1313
freertos-rust = {path = "../freertos-rust"}
1414

1515
[target.'cfg(target_arch = "arm")'.dependencies]
16-
cortex-m = "0.6.0"
17-
cortex-m-rt = {version = "0.6.12"}
18-
nrf9160-pac = "0.2.1"
16+
cortex-m = "0.7"
17+
cortex-m-rt = "0.7"
1918

2019
# Example: stm32-cortex-m3
2120
[target.thumbv7m-none-eabi.dependencies]
@@ -24,9 +23,8 @@ stm32l1xx-hal = {version = "0.1.0", features = ["stm32l151"], default-features =
2423

2524
# Example: stm32-cortex-m4-blackpill
2625
[target.thumbv7em-none-eabihf.dependencies]
27-
panic-halt = "0.2.0"
28-
embedded-hal = "0.2.3"
29-
stm32f4xx-hal = {version = "0.8.3", features = ["rt", "stm32f411"]}
26+
embedded-hal = "1.0"
27+
stm32f4xx-hal = {version = "0.22", features = ["stm32f411"]}
3028

3129
# Example: nrf9160
3230
[target."thumbv8m.main-none-eabihf".dependencies]
@@ -39,4 +37,4 @@ nrf9160-pac = "0.2.1"
3937
[target.x86_64-unknown-linux-gnu.dependencies]
4038

4139
[build-dependencies]
42-
freertos-cargo-build = {path = "../freertos-cargo-build"}
40+
freertos-cargo-build = {path = "../freertos-cargo-build"}

freertos-rust-examples/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,14 @@ Create hex file to be flashed:
9696

9797
### Run STM32 Cortex-M4 Demo
9898

99-
As the previous example, we need nightly toolchain and the target is thumbv7em-none-eabihf:
99+
We use stable toolchain and the target is thumbv7em-none-eabihf:
100+
100101
rustup target add thumbv7em-none-eabihf
101102

102103
Build the binary:
103104

104105
cargo build --package freertos-rust-examples --example stm32-cortex-m4-blackpill --target thumbv7em-none-eabihf
105-
106+
106107
Create hex file to be flashed:
107108

108109
cargo objcopy --example stm32-cortex-m4-blackpill --target thumbv7em-none-eabihf -- -O ihex stm32-cortex-m4-blackpill.hex

freertos-rust-examples/examples/stm32-cortex-m4-blackpill/FreeRTOSConfig.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@
7171
#define FREERTOS_CONFIG_H
7272
#include <stdbool.h>
7373

74-
extern void vAssertCalled( unsigned long ulLine, const char * const pcFileName );
75-
#define configASSERT( x ) if( ( x ) == 0 ) vAssertCalled( __LINE__, __FILE__ )
74+
extern void vAssertCalled( const char * const pcFileName, unsigned long ulLine );
75+
#define configASSERT( x ) if( ( x ) == 0 ) vAssertCalled( __FILE__, __LINE__ )
7676

7777
#define vPortSVCHandler SVCall
7878
#define xPortPendSVHandler PendSV
@@ -93,7 +93,7 @@ extern void vAssertCalled( unsigned long ulLine, const char * const pcFileName )
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 ( 100000000UL ) //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 )
@@ -186,4 +186,3 @@ See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
186186
// }
187187

188188
#endif /* FREERTOS_CONFIG_H */
189-
Lines changed: 60 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,106 @@
11
#![no_std]
22
#![no_main]
3-
// For allocator
4-
#![feature(lang_items)]
5-
#![feature(alloc_error_handler)]
63

4+
use core::panic::PanicInfo;
75
use cortex_m::asm;
86
use cortex_m_rt::exception;
97
use cortex_m_rt::{entry, ExceptionFrame};
10-
use embedded_hal::digital::v2::OutputPin;
8+
use embedded_hal::digital::OutputPin;
119
use freertos_rust::*;
12-
use core::alloc::Layout;
1310
use stm32f4xx_hal::gpio::*;
1411

15-
use cortex_m;
1612
use stm32f4xx_hal as hal;
1713

18-
use crate::hal::{
19-
stm32::{Peripherals},
20-
};
21-
22-
extern crate panic_halt; // panic handler
14+
use crate::hal::{pac, prelude::*};
2315

2416
#[global_allocator]
2517
static GLOBAL: FreeRtosAllocator = FreeRtosAllocator;
2618

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

34-
fn delay_n(n: i32) {
35-
for _ in 0..n {
36-
delay();
31+
fn app_main() -> ! {
32+
let dp = pac::Peripherals::take().unwrap();
33+
let rcc = dp.RCC.constrain();
34+
let _clocks = rcc.cfgr.use_hse(25.MHz()).sysclk(100.MHz()).freeze();
35+
36+
let gpioc = dp.GPIOC.split();
37+
let mut device = MyDevice::from_pins(gpioc.pc13.into_open_drain_output());
38+
device.set_led(false);
39+
Task::new()
40+
.name("hello")
41+
.stack_size(128)
42+
.priority(TaskPriority(2))
43+
.start(move |_| loop {
44+
CurrentTask::delay(Duration::ms(500));
45+
device.set_led(true);
46+
CurrentTask::delay(Duration::ms(500));
47+
device.set_led(false);
48+
})
49+
.unwrap();
50+
51+
loop {
52+
CurrentTask::delay(Duration::ms(1000));
3753
}
3854
}
3955

4056
pub struct MyDevice<D1: OutputPin> {
4157
d1: D1,
4258
}
4359

44-
impl<D1: OutputPin> MyDevice<D1>
45-
{
60+
impl<D1: OutputPin> MyDevice<D1> {
4661
pub fn from_pins(d1: D1) -> MyDevice<D1> {
47-
MyDevice {
48-
d1
49-
}
62+
MyDevice { d1 }
5063
}
51-
pub fn set_led(&mut self,on:bool){
64+
65+
pub fn set_led(&mut self, on: bool) {
5266
if on {
53-
self.d1.set_high();
67+
self.d1.set_low().ok();
5468
} else {
55-
self.d1.set_low();
69+
self.d1.set_high().ok();
5670
}
5771
}
5872
}
5973

60-
#[entry]
61-
fn main() -> ! {
62-
let dp = Peripherals::take().unwrap();
63-
let gpioc = dp.GPIOC.split();
64-
let mut device = MyDevice::from_pins(gpioc.pc13.into_push_pull_output());
65-
device.set_led(false);
66-
Task::new().name("hello").stack_size(128).priority(TaskPriority(2)).start(move |_| {
67-
loop{
68-
freertos_rust::CurrentTask::delay(Duration::ms(1000));
69-
device.set_led(true);
70-
freertos_rust::CurrentTask::delay(Duration::ms(1000));
71-
device.set_led(false);
72-
}
73-
}).unwrap();
74-
FreeRtosUtils::start_scheduler();
75-
}
76-
74+
#[allow(non_snake_case)]
7775
#[exception]
78-
fn DefaultHandler(_irqn: i16) {
79-
// custom default handler
80-
// irqn is negative for Cortex-M exceptions
81-
// irqn is positive for device specific (line IRQ)
82-
// set_led(true);(true);
83-
// panic!("Exception: {}", irqn);
76+
unsafe fn DefaultHandler(_irqn: i16) {
77+
// custom default handler
78+
// irqn is negative for Cortex-M exceptions
79+
// irqn is positive for device specific (line IRQ)
80+
// set_led(true);(true);
81+
// panic!("Exception: {}", irqn);
82+
asm::bkpt();
83+
loop {}
8484
}
8585

86+
#[allow(non_snake_case)]
8687
#[exception]
87-
fn HardFault(_ef: &ExceptionFrame) -> ! {
88-
// Blink 3 times long when exception occures
89-
delay_n(10);
90-
for _ in 0..3 {
91-
// set_led(true);
92-
// delay_n(1000);
93-
// set_led(false);
94-
// delay_n(555);
95-
}
88+
unsafe fn HardFault(_ef: &ExceptionFrame) -> ! {
89+
// Blink 3 times long when exception occures
90+
asm::bkpt();
9691
loop {}
9792
}
9893

99-
// define what happens in an Out Of Memory (OOM) condition
100-
#[alloc_error_handler]
101-
fn alloc_error(_layout: Layout) -> ! {
102-
//set_led(true);
94+
// We no longer need to use #[alloc_error_handler] since v1.68.
95+
// It will automatically call the panic handler.
96+
#[panic_handler]
97+
fn panic(_info: &PanicInfo) -> ! {
10398
asm::bkpt();
10499
loop {}
105100
}
106101

102+
#[allow(non_snake_case)]
107103
#[no_mangle]
108-
fn vApplicationStackOverflowHook(pxTask: FreeRtosTaskHandle, pcTaskName: FreeRtosCharPtr) {
104+
fn vApplicationStackOverflowHook(_pxTask: FreeRtosTaskHandle, _pcTaskName: FreeRtosCharPtr) {
109105
asm::bkpt();
110106
}

0 commit comments

Comments
 (0)