GithubHelp home page GithubHelp logo

n-k / cortexm-threads Goto Github PK

View Code? Open in Web Editor NEW
6.0 2.0 2.0 133 KB

Simple context switching library for ARM Cortex-M MCUs in Rust

License: MIT License

Shell 1.08% Rust 78.67% Assembly 12.25% GDB 7.99%
rust embedded cortex-m context-switching

cortexm-threads's Introduction

CORTEXM_THREADS

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

Current State

Processor support:

  • Cortex-M0
  • Cortex-M0+
  • Cortex-M3
  • Cortex-M4
  • Cortex-M4F

Features:

  • Preemptive, priority-based switching
  • Efficient sleep
  • Accept stack memory area as a vec (arrayvec?, smallvec?) instead of &[]
  • Non-privileged mode
  • Mutex implementation aware of thread scheduling

Examples

The example_crates folder contains crates showing how to use cortexm-threads for different boards.

Available examples:

  • stm32f3 - 2 threads with one thread running an LED roulette, and the other periodically printing magnetometer readings. Currently compiles for target thumbv7m-none-eabi instead of thumbv7em-none-eabihf. See Roadmap#1
  • microbit - 2 threads printing messages with co-operative context switching
  • qemu-m4 - (set up to run on qemu) 2 threads printing messages via semi-hosting. Run cargo run from example_crates/qemu-m4 directory to see it running. You must have qemu-system-arm on the system PATH.

Sample:

#![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();
}

License

See LICENSE.md

cortexm-threads's People

Contributors

divi255 avatar n-k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

sjp27 divi255

cortexm-threads's Issues

is the repo alive?

Good day,

I'm looking for a lightweight thread or async solution for our plc rtos project, your seems to work pretty fine.

It misses a couple of things, e.g. support of more CPUs. It would be also nice to let it have own mutex / rwlock with specified cycle delays between locking attempts. And of course, docs + cargo crate.

If you're going to support it, I'll send pull requests. Or it's better to just do a fork?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.