Expand description
A simple library for context-switching on ARM Cortex-M ( 0, 0+, 3, 4, 4F ) micro-processors
Supports pre-emptive, priority based switching
This project is meant for learning and should be used only at the user’s risk. For practical and mature rust alternatives, see Awesome Embedded Rust
Example:
#![no_std]
#![no_main]
extern crate panic_semihosting;
use cortex_m::peripheral::syst::SystClkSource;
use cortex_m_rt::{entry, exception};
use cortex_m_semihosting::{hprintln};
use cortexm_threads::{init, create_thread, create_thread_with_config, sleep};
#[entry]
fn main() -> ! {
let cp = cortex_m::Peripherals::take().unwrap();
let mut syst = cp.SYST;
syst.set_clock_source(SystClkSource::Core);
syst.set_reload(80_000);
syst.enable_counter();
syst.enable_interrupt();
let mut stack1 = [0xDEADBEEF; 512];
let mut stack2 = [0xDEADBEEF; 512];
let _ = create_thread(
&mut stack1,
|| {
loop {
let _ = hprintln!("in task 1 !!");
sleep(50); // sleep for 50 ticks
}
});
let _ = create_thread_with_config(
&mut stack2,
|| {
loop {
let _ = hprintln!("in task 2 !!");
sleep(30); // sleep for 30 ticks
}
},
0x01, // priority, higher numeric value means higher priority
true // privileged thread
);
init();
}
Statics§
- ERR_
NO_ CREATE_ PRIV - Returned by create_thread or create_thread_with_config as Err(ERR_NO_CREATE_PRIV) if called from an unprivileged thread
- ERR_
STACK_ TOO_ SMALL - Returned by create_thread or create_thread_with_config as Err(ERR_STACK_TOO_SMALL) if array to be used as stack area is too small. Smallest size is 32 u32’s
- ERR_
TOO_ MANY_ THREADS - Returned by create_thread or create_thread_with_config as Err(ERR_TOO_MANY_THREADS) if creating a thread will cause more than 32 threads to exist (inclusing the idle thread) created by this library
Functions§
- SysTick
- Handle a tick event. Typically, this would be called as SysTick handler, but can be called anytime. Call from thread handler code to yield and switch context.
- create_
thread - Create a thread with default configuration (lowest priority, unprivileged).
- create_
thread_ with_ config - Create a thread with explicit configuration
- disable_
threads - enable_
threads - get_
thread_ id - Get id of current thread
- init
- Initialize the switcher system
- sleep
- Make current thread sleep for
ticks
ticks. Current thread will be put inSleeping
state and another thread will be scheduled immediately. Current thread will not be considered for scheduling untiltick()
is called at leasttick
times.