Skip to content
Snippets Groups Projects
Commit 3ba499d5 authored by Jorge Aparicio's avatar Jorge Aparicio
Browse files

initial commit

parents
No related branches found
No related tags found
No related merge requests found
[target.thumbv7m-none-eabi]
runner = 'arm-none-eabi-gdb'
rustflags = [
"-C", "link-arg=-Tlink.x",
"-C", "linker=lld",
"-Z", "linker-flavor=ld.lld",
"-Z", "thinlto=no",
]
**/*.rs.bk
.#*
/target
Cargo.lock
[package]
name = "indin2018"
version = "0.1.0"
authors = ["Jorge Aparicio <jorge@japaric.io>"]
[dependencies]
heapless = "0.2.5"
[target.'cfg(not(target_os = "none"))'.dependencies.serde-json-core]
git = "https://github.com/japaric/serde-json-core"
features = ["std"]
rev = "a2c4cf0c5d3435a09f7835178c359486567f9cdb"
[target.'cfg(target_os = "none")'.dependencies]
cortex-m-rt = "0.4.0"
cortex-m-rtfm = "0.3.1"
panic-abort = "0.1.0"
stm32f103xx = { version = "0.8.0", features = ["rt"] }
[target.'cfg(not(target_os = "none"))'.dependencies]
scoped_threadpool = "0.1.9"
serde_derive = "1.0.37"
serde_json = "1.0.13"
[profile.release]
lto = true
opt-level = "s"
\ No newline at end of file
fn main() {
let mut xs = vec![0, 1, 2];
alias(&mut xs, &xs[0]);
//~ error: cannot borrow `xs` as immutable because it is also borrowed as mutable
}
fn alias(xs: &mut Vec<i32>, x: &i32) {
xs.push(3);
println!("{}", *x);
}
fn main() {
let mut xs = vec![0, 1, 2];
// temporarily borrow `xs`
push(&mut xs, 3);
// `xs` is still owned by `main`
xs.push(4);
// give away ownership of `xs`
drop(xs);
}
fn push(xs: &mut Vec<i32>, x: i32) {
xs.push(x);
}
fn main() {}
fn dangling<'a>() -> &'a i32 {
// `x` is allocated on the stack frame of the `dangling` function
let x = 0;
&x
} // `x` is deallocated here along with the stack frame
fn main() {
// Allocates an array on the heap that contains these elements
let xs = vec![0, 1, 2];
// ..
} // `xs` is implicitly `drop`ped here and memory is freed
#![deny(unsafe_code)]
#![deny(warnings)]
extern crate heapless;
use std::fmt::Write;
use heapless::String;
fn main() {
let mut s: String<[u8; 8]> = String::new();
write!(s, "The quick brow fox jumps over the lazy dog.").unwrap();
}
#![deny(unsafe_code)]
#![deny(warnings)]
extern crate heapless;
use std::fmt::Write;
use heapless::String;
fn main() {
let mut s: String<[u8; 16]> = String::new();
let a = 42;
let b = 24;
write!(&mut s, "{} * {} = {}", a, b, a * b).unwrap();
assert_eq!(&*s, "42 * 24 = 1008"); // expected string
}
#![deny(unsafe_code)]
#![deny(warnings)]
extern crate heapless;
use std::io::{self, Bytes, Read, Stdin};
use heapless::String;
fn main() {
let mut serial = Serial::new();
// pre-allocate a buffer with size of 64 bytes
let mut buf = String::<[u8; 64]>::new();
loop {
// reads a single byte from a serial interface
let byte: u8 = serial.read();
if byte.is_ascii() {
if byte == b'\n' {
// process command stored in `buf`
match &*buf {
"command1" => { /* .. */ }
// ..
_ => println!("unknown command: {}", &*buf),
}
buf.clear();
} else {
if buf.push(byte as char).is_err() {
// buffer full: omitted show error message
buf.clear();
}
}
} else {
// non-ascii byte: omitted show error message
buf.clear();
}
}
}
// Mocking a serial device using stdin
struct Serial {
bytes: Bytes<Stdin>,
}
impl Serial {
fn new() -> Self {
Serial {
bytes: io::stdin().bytes(),
}
}
fn read(&mut self) -> u8 {
self.bytes.next().unwrap().unwrap()
}
}
#![deny(unsafe_code)]
#![deny(warnings)]
#[macro_use]
extern crate serde_derive;
extern crate heapless;
extern crate serde_json;
extern crate serde_json_core;
#[derive(Deserialize, Serialize)]
struct Led {
state: bool,
}
fn main() {
let led = Led { state: true };
// serde-json -- `_json` is allocated on the heap
let _json_h: Vec<u8> = serde_json::ser::to_vec(&led).unwrap();
// serde-json-core -- `json` is allocated on the stack
let json: heapless::Vec<u8, [u8; 16]> =
serde_json_core::ser::to_vec(&led).unwrap();
assert_eq!(&*json, "{\"state\":true}".as_bytes()); // expected string
}
#![deny(unsafe_code)]
#![deny(warnings)]
extern crate heapless;
extern crate scoped_threadpool;
use std::thread;
use std::time::Duration;
use heapless::RingBuffer;
use scoped_threadpool::Pool;
fn main() {
// allocated on the stack
let mut ring_buffer = RingBuffer::<i32, [i32; 8]>::new();
let (mut producer, mut consumer) = ring_buffer.split();
// ring_buffer.enqueue(1);
//~^ error: cannot borrow `ring_buffer` as mutable more than once at a time
// spawn two threads with the same lifetime
// thread A sends data to thread B
Pool::new(2).scoped(move |scope| {
// thread A
scope.execute(move || {
let mut i = 0;
loop {
// enqueue new data; if the buffer is full try again
while producer.enqueue(i).is_err() {
thread::sleep(Duration::from_millis(1));
}
thread::sleep(Duration::from_secs(1));
i += 1;
}
});
// thread B
scope.execute(move || {
// producer.enqueue(0);
//~^ error: capture of moved value: `producer`
loop {
// dequeue data
if let Some(x) = consumer.dequeue() {
println!("{}", x);
} else {
thread::sleep(Duration::from_millis(1));
}
}
});
});
// `Pool` will block the execution of the `main` thread until
// threads A and B finish
// `p`, `c` and `rb` are destroyed here
}
#![deny(unsafe_code)]
use std::thread;
fn main() {
let mut xs = vec![0, 1, 2];
thread::spawn(move || {
// rename `xs` to `ys`
let mut ys = xs;
// insert a new element into `ys`
ys.push(3);
// `ys` is `drop`ped here
});
// attempt to remove an element from `xs`
let _x = xs.pop();
//~^ error: use of moved value: `xs`
}
#![deny(unsafe_code)]
#![deny(warnings)]
#![feature(proc_macro)]
#![no_std]
extern crate cortex_m_rtfm as rtfm;
extern crate heapless;
extern crate panic_abort;
extern crate stm32f103xx;
use heapless::ring_buffer::{Consumer, Producer, RingBuffer};
use rtfm::{app, Threshold};
use stm32f103xx::Interrupt;
// Specification of tasks and resources
app! {
device: stm32f103xx,
resources: {
// allocated on a static variable
static RING_BUFFER: RingBuffer<i32, [i32; 8]> = RingBuffer::new();
// resources initialized at runtime
static PRODUCER: Producer<'static, i32, [i32; 8]>;
static CONSUMER: Consumer<'static, i32, [i32; 8]>;
},
init: {
resources: [RING_BUFFER],
},
tasks: {
EXTI0: {
path: task_a,
resources: [PRODUCER],
priority: 2,
},
EXTI1: {
path: task_b,
resources: [CONSUMER],
priority: 1,
},
}
}
// the initialization phase runs before any task can start
fn init(_p: init::Peripherals, r: init::Resources) -> init::LateResources {
let rb: &'static mut RingBuffer<i32, [i32; 8]> = r.RING_BUFFER;
let (p, c) = rb.split();
// initialize resources
init::LateResources {
PRODUCER: p,
CONSUMER: c,
}
}
fn idle() -> ! {
loop {
rtfm::wfi()
}
}
fn task_a(_t: &mut Threshold, mut r: EXTI0::Resources) {
// enqueue new data
r.PRODUCER.enqueue(1).unwrap();
// schedule task B for execution
rtfm::set_pending(Interrupt::EXTI0);
}
fn task_b(_t: &mut Threshold, mut r: EXTI1::Resources) {
while let Some(_x) = r.CONSUMER.dequeue() {
// process `_x`
}
}
#![no_std]
extern crate heapless;
extern crate panic_abort;
extern crate stm32f103xx;
use core::{mem, ptr};
use heapless::Vec;
fn main() {
unsafe {
let xs = mem::uninitialized();
ptr::write_volatile(0x2000_0000 as *mut u32, imperative(&xs));
ptr::write_volatile(0x2000_0004 as *mut u32, functional(&xs));
}
}
#[inline(never)]
fn imperative(xs: &Vec<u32, [u32; 64]>) -> u32 {
let mut sum = 0;
for i in 0..xs.len() {
sum += xs[i]; // bound checked
}
sum
}
#[inline(never)]
fn functional(xs: &Vec<u32, [u32; 64]>) -> u32 {
xs.iter().sum()
}
/* Linker script for the STM32F103C8T6 */
MEMORY
{
FLASH : ORIGIN = 0x08000000, LENGTH = 64K
RAM : ORIGIN = 0x20000000, LENGTH = 20K
}
max_width = 80
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment