Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ jobs:
target
key: ${{ runner.os }}-${{ runner.arch }}
- name: Install Rust
uses: dtolnay/rust-toolchain@nightly
uses: dtolnay/rust-toolchain@stable
with:
targets: ${{ matrix.target }}
- name: Install cross deps
Expand Down
12 changes: 5 additions & 7 deletions freertos-rust-examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ repository = "https://github.com/lobaro/FreeRTOS-rust"
freertos-rust = {path = "../freertos-rust"}

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

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

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

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

[build-dependencies]
freertos-cargo-build = {path = "../freertos-cargo-build"}
freertos-cargo-build = {path = "../freertos-cargo-build"}
5 changes: 3 additions & 2 deletions freertos-rust-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,14 @@ Create hex file to be flashed:

### Run STM32 Cortex-M4 Demo

As the previous example, we need nightly toolchain and the target is thumbv7em-none-eabihf:
We use stable toolchain and the target is thumbv7em-none-eabihf:

rustup target add thumbv7em-none-eabihf

Build the binary:

cargo build --package freertos-rust-examples --example stm32-cortex-m4-blackpill --target thumbv7em-none-eabihf

Create hex file to be flashed:

cargo objcopy --example stm32-cortex-m4-blackpill --target thumbv7em-none-eabihf -- -O ihex stm32-cortex-m4-blackpill.hex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@
#define FREERTOS_CONFIG_H
#include <stdbool.h>

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

#define vPortSVCHandler SVCall
#define xPortPendSVHandler PendSV
Expand All @@ -93,7 +93,7 @@ extern void vAssertCalled( unsigned long ulLine, const char * const pcFileName )
#define configUSE_PREEMPTION 1
#define configUSE_IDLE_HOOK 0
#define configUSE_TICK_HOOK 0
#define configCPU_CLOCK_HZ ( 4200000UL ) //also systick runs at this frequency
#define configCPU_CLOCK_HZ ( 100000000UL ) //also systick runs at this frequency
#define configTICK_RATE_HZ ( ( TickType_t ) 1000 ) //1000=1ms per tick, 100=10ms per tick
#define configMAX_PRIORITIES ( 5 )
#define configMINIMAL_STACK_SIZE ( ( unsigned short ) 80 )
Expand Down Expand Up @@ -186,4 +186,3 @@ See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */
// }

#endif /* FREERTOS_CONFIG_H */

123 changes: 59 additions & 64 deletions freertos-rust-examples/examples/stm32-cortex-m4-blackpill/main.rs
Original file line number Diff line number Diff line change
@@ -1,110 +1,105 @@
#![no_std]
#![no_main]
// For allocator
#![feature(lang_items)]
#![feature(alloc_error_handler)]

use core::panic::PanicInfo;
use cortex_m::asm;
use cortex_m_rt::exception;
use cortex_m_rt::{entry, ExceptionFrame};
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
use freertos_rust::*;
use core::alloc::Layout;
use stm32f4xx_hal::gpio::*;

use cortex_m;
use stm32f4xx_hal as hal;

use crate::hal::{
stm32::{Peripherals},
};

extern crate panic_halt; // panic handler
use crate::hal::{pac, prelude::*};

#[global_allocator]
static GLOBAL: FreeRtosAllocator = FreeRtosAllocator;

fn delay() {
let mut _i = 0;
for _ in 0..2_00 {
_i += 1;
}
#[entry]
fn main() -> ! {
Task::new()
.name("default")
.stack_size(1000)
.start(move |_| {
app_main();
})
.unwrap();
FreeRtosUtils::start_scheduler();
}

fn delay_n(n: i32) {
for _ in 0..n {
delay();
fn app_main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let rcc = dp.RCC.constrain();
let _clocks = rcc.cfgr.use_hse(25.MHz()).sysclk(100.MHz()).freeze();

let gpioc = dp.GPIOC.split();
let mut device = MyDevice::from_pins(gpioc.pc13.into_open_drain_output());
device.set_led(false);
Task::new()
.name("hello")
.stack_size(128)
.priority(TaskPriority(2))
.start(move |_| loop {
CurrentTask::delay(Duration::ms(1000));
device.set_led(true);
CurrentTask::delay(Duration::ms(1000));
device.set_led(false);
})
.unwrap();

loop {
CurrentTask::delay(Duration::ms(1000));
}
}

pub struct MyDevice<D1: OutputPin> {
d1: D1,
}

impl<D1: OutputPin> MyDevice<D1>
{
impl<D1: OutputPin> MyDevice<D1> {
pub fn from_pins(d1: D1) -> MyDevice<D1> {
MyDevice {
d1
}
MyDevice { d1 }
}
pub fn set_led(&mut self,on:bool){

pub fn set_led(&mut self, on: bool) {
if on {
self.d1.set_high();
self.d1.set_low().ok();
} else {
self.d1.set_low();
self.d1.set_high().ok();
}
}
}

#[entry]
fn main() -> ! {
let dp = Peripherals::take().unwrap();
let gpioc = dp.GPIOC.split();
let mut device = MyDevice::from_pins(gpioc.pc13.into_push_pull_output());
device.set_led(false);
Task::new().name("hello").stack_size(128).priority(TaskPriority(2)).start(move |_| {
loop{
freertos_rust::CurrentTask::delay(Duration::ms(1000));
device.set_led(true);
freertos_rust::CurrentTask::delay(Duration::ms(1000));
device.set_led(false);
}
}).unwrap();
FreeRtosUtils::start_scheduler();
}

#[allow(non_snake_case)]
#[exception]
fn DefaultHandler(_irqn: i16) {
// custom default handler
// irqn is negative for Cortex-M exceptions
// irqn is positive for device specific (line IRQ)
// set_led(true);(true);
// panic!("Exception: {}", irqn);
unsafe fn DefaultHandler(_irqn: i16) {
// custom default handler
// irqn is negative for Cortex-M exceptions
// irqn is positive for device specific (line IRQ)
// set_led(true);(true);
// panic!("Exception: {}", irqn);
asm::bkpt();
loop {}
}

#[allow(non_snake_case)]
#[exception]
fn HardFault(_ef: &ExceptionFrame) -> ! {
// Blink 3 times long when exception occures
delay_n(10);
for _ in 0..3 {
// set_led(true);
// delay_n(1000);
// set_led(false);
// delay_n(555);
}
unsafe fn HardFault(_ef: &ExceptionFrame) -> ! {
asm::bkpt();
loop {}
}

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

#[allow(non_snake_case)]
#[no_mangle]
fn vApplicationStackOverflowHook(pxTask: FreeRtosTaskHandle, pcTaskName: FreeRtosCharPtr) {
fn vApplicationStackOverflowHook(_pxTask: FreeRtosTaskHandle, _pcTaskName: FreeRtosCharPtr) {
asm::bkpt();
}
6 changes: 3 additions & 3 deletions freertos-rust/src/critical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl<T> ExclusiveData<T> {
}
}

pub fn lock(&self) -> Result<ExclusiveDataGuard<T>, FreeRtosError> {
pub fn lock(&self) -> Result<ExclusiveDataGuard<'_, T>, FreeRtosError> {
Ok(ExclusiveDataGuard {
__data: &self.data,
__lock: CriticalRegion::enter(),
Expand All @@ -47,7 +47,7 @@ impl<T> ExclusiveData<T> {
pub fn lock_from_isr(
&self,
_context: &mut crate::isr::InterruptContext,
) -> Result<ExclusiveDataGuardIsr<T>, FreeRtosError> {
) -> Result<ExclusiveDataGuardIsr<'_, T>, FreeRtosError> {
Ok(ExclusiveDataGuardIsr { __data: &self.data })
}
}
Expand Down Expand Up @@ -106,7 +106,7 @@ impl<T> SuspendScheduler<T> {
}
}

pub fn lock(&self) -> SuspendSchedulerGuard<T> {
pub fn lock(&self) -> SuspendSchedulerGuard<'_,T> {
unsafe {
freertos_rs_vTaskSuspendAll();
}
Expand Down
2 changes: 1 addition & 1 deletion freertos-rust/src/mutex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ where
}

/// Try to obtain a lock and mutable access to our inner value
pub fn lock<D: DurationTicks>(&self, max_wait: D) -> Result<MutexGuard<T, M>, FreeRtosError> {
pub fn lock<D: DurationTicks>(&self, max_wait: D) -> Result<MutexGuard<'_, T, M>, FreeRtosError> {
self.mutex.take(max_wait)?;

Ok(MutexGuard {
Expand Down
2 changes: 1 addition & 1 deletion freertos-rust/src/semaphore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ impl Semaphore {
}

/// Lock this semaphore in a RAII fashion
pub fn lock<D: DurationTicks>(&self, max_wait: D) -> Result<SemaphoreGuard, FreeRtosError> {
pub fn lock<D: DurationTicks>(&self, max_wait: D) -> Result<SemaphoreGuard<'_>, FreeRtosError> {
self.take(max_wait).map(|()| SemaphoreGuard { owner: self })
}

Expand Down