diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index b69ccdea..3ceec246 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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 diff --git a/freertos-rust-examples/Cargo.toml b/freertos-rust-examples/Cargo.toml index da6599ee..1766fb2b 100644 --- a/freertos-rust-examples/Cargo.toml +++ b/freertos-rust-examples/Cargo.toml @@ -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] @@ -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] @@ -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"} \ No newline at end of file +freertos-cargo-build = {path = "../freertos-cargo-build"} diff --git a/freertos-rust-examples/README.md b/freertos-rust-examples/README.md index b447f5c0..41d4fd5e 100644 --- a/freertos-rust-examples/README.md +++ b/freertos-rust-examples/README.md @@ -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 diff --git a/freertos-rust-examples/examples/stm32-cortex-m4-blackpill/FreeRTOSConfig.h b/freertos-rust-examples/examples/stm32-cortex-m4-blackpill/FreeRTOSConfig.h index 4f964109..deb6af39 100644 --- a/freertos-rust-examples/examples/stm32-cortex-m4-blackpill/FreeRTOSConfig.h +++ b/freertos-rust-examples/examples/stm32-cortex-m4-blackpill/FreeRTOSConfig.h @@ -71,8 +71,8 @@ #define FREERTOS_CONFIG_H #include -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 @@ -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 ) @@ -186,4 +186,3 @@ See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */ // } #endif /* FREERTOS_CONFIG_H */ - diff --git a/freertos-rust-examples/examples/stm32-cortex-m4-blackpill/main.rs b/freertos-rust-examples/examples/stm32-cortex-m4-blackpill/main.rs index 15983bb3..64289538 100644 --- a/freertos-rust-examples/examples/stm32-cortex-m4-blackpill/main.rs +++ b/freertos-rust-examples/examples/stm32-cortex-m4-blackpill/main.rs @@ -1,39 +1,55 @@ #![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)); } } @@ -41,70 +57,49 @@ pub struct MyDevice { d1: D1, } -impl MyDevice -{ +impl MyDevice { pub fn from_pins(d1: D1) -> MyDevice { - 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(); } diff --git a/freertos-rust/src/critical.rs b/freertos-rust/src/critical.rs index 79f8dea9..405d711e 100644 --- a/freertos-rust/src/critical.rs +++ b/freertos-rust/src/critical.rs @@ -37,7 +37,7 @@ impl ExclusiveData { } } - pub fn lock(&self) -> Result, FreeRtosError> { + pub fn lock(&self) -> Result, FreeRtosError> { Ok(ExclusiveDataGuard { __data: &self.data, __lock: CriticalRegion::enter(), @@ -47,7 +47,7 @@ impl ExclusiveData { pub fn lock_from_isr( &self, _context: &mut crate::isr::InterruptContext, - ) -> Result, FreeRtosError> { + ) -> Result, FreeRtosError> { Ok(ExclusiveDataGuardIsr { __data: &self.data }) } } @@ -106,7 +106,7 @@ impl SuspendScheduler { } } - pub fn lock(&self) -> SuspendSchedulerGuard { + pub fn lock(&self) -> SuspendSchedulerGuard<'_,T> { unsafe { freertos_rs_vTaskSuspendAll(); } diff --git a/freertos-rust/src/mutex.rs b/freertos-rust/src/mutex.rs index fb43eff0..6d71542b 100644 --- a/freertos-rust/src/mutex.rs +++ b/freertos-rust/src/mutex.rs @@ -36,7 +36,7 @@ where } /// Try to obtain a lock and mutable access to our inner value - pub fn lock(&self, max_wait: D) -> Result, FreeRtosError> { + pub fn lock(&self, max_wait: D) -> Result, FreeRtosError> { self.mutex.take(max_wait)?; Ok(MutexGuard { diff --git a/freertos-rust/src/semaphore.rs b/freertos-rust/src/semaphore.rs index 75ccef6d..cb5d4768 100644 --- a/freertos-rust/src/semaphore.rs +++ b/freertos-rust/src/semaphore.rs @@ -50,7 +50,7 @@ impl Semaphore { } /// Lock this semaphore in a RAII fashion - pub fn lock(&self, max_wait: D) -> Result { + pub fn lock(&self, max_wait: D) -> Result, FreeRtosError> { self.take(max_wait).map(|()| SemaphoreGuard { owner: self }) }