Skip to content

Instantly share code, notes, and snippets.

@cr1901
Created July 18, 2017 20:47
Show Gist options
  • Select an option

  • Save cr1901/473b90aa1acb5be11d7f98712df6b86f to your computer and use it in GitHub Desktop.

Select an option

Save cr1901/473b90aa1acb5be11d7f98712df6b86f to your computer and use it in GitHub Desktop.
MSP430 Sample Atomic Impl
#![no_std]
#![feature(const_fn)]
#![feature(asm)]
use core::cell::UnsafeCell;
pub struct MspAtomicBool {
data: UnsafeCell<u8>,
}
impl MspAtomicBool {
pub const fn new(v: bool) -> MspAtomicBool {
MspAtomicBool { data: UnsafeCell::new(v) }
}
pub fn store(&self, v: bool) -> () {
let my_bool_ptr = self.data.get();
let bool_as_int = if v {
1
} else {
0
};
unsafe {
asm!("mov $0, $1" : : "r"(v), "m"(my_bool_ptr) : "memory" :);
}
}
pub fn load(&self) -> bool {
let v: u8;
let my_bool_ptr = self.data.get();
unsafe {
asm!("mov $1, $0" : "=r"(v): "m"(my_bool_ptr) : "memory" :);
}
if v == 0 {
false
} else {
true
}
}
}
unsafe impl Sync for MspAtomicBool {}
#[cfg(test)]
mod tests {
#[test]
fn it_works() {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment