diff --git a/nrf52/examples/1-yield.rs b/nrf52/examples/1-yield.rs index e5465898890ad288dd4b4364cd34976ba8eec973..8de0eff1d55ea2e7019a616e8a8bdde74c4e98b9 100644 --- a/nrf52/examples/1-yield.rs +++ b/nrf52/examples/1-yield.rs @@ -11,7 +11,7 @@ //! ``` #![deny(unsafe_code)] -#![deny(warnings)] +// #![deny(warnings)] #![no_main] #![no_std] @@ -25,6 +25,10 @@ use panic_udf as _; // panic handler #[entry] fn main() -> ! { // task A + hprintln!("here").ok(); + + //loop { continue; } + task::spawn(async { loop { hprintln!("A: yield").ok(); @@ -35,19 +39,21 @@ fn main() -> ! { // task B task::block_on(async { - hprintln!("B: yield").ok(); + loop { + hprintln!("B1: yield").ok(); // context switch to A task::r#yield().await; - hprintln!("B: yield").ok(); + hprintln!("B2: yield").ok(); task::r#yield().await; hprintln!("DONE").ok(); - loop { - asm::bkpt(); + // loop { + // asm::bkpt(); + // } } }) } diff --git a/nrf52/examples/3-mutex.rs b/nrf52/examples/3-mutex.rs index 6a8c2203c7696c2d7a121d30ab3ba83857c61b6c..288b781254d568dfe60d5665fffdc3c17a6a7892 100644 --- a/nrf52/examples/3-mutex.rs +++ b/nrf52/examples/3-mutex.rs @@ -38,34 +38,45 @@ use panic_udf as _; // panic handler #[entry] fn main() -> ! { static mut X: Mutex<i64> = Mutex::new(0); + + task::block_on(async { + loop { + hprintln!("A: before lock").ok(); + + let mut lock = X.lock().await; + + hprintln!("A: before write {}", *lock).ok(); + *lock += 1; + drop(lock); - let mut lock = X.try_lock().unwrap(); - - task::spawn(async { - hprintln!("A: before write").ok(); - *lock = 42; - drop(lock); - - hprintln!("A: after releasing the lock").ok(); + hprintln!("A: after releasing the lock").ok(); - loop { - hprintln!("A: yield").ok(); + hprintln!("A: manual yield").ok(); task::r#yield().await; } }); task::block_on(async { - hprintln!("B: before lock").ok(); - - // cannot immediately make progress; context switch to A - let lock = X.lock().await; - - hprintln!("B: {}", *lock).ok(); + loop { + hprintln!("B: before lock").ok(); - hprintln!("DONE").ok(); + // cannot immediately make progress; context switch to A + let mut lock = X.lock().await; - loop { - asm::bkpt(); + hprintln!("B: {}", *lock).ok(); + *lock += 1; + + if *lock == 10 { + loop { + asm::bkpt(); + } + } else { + drop(lock); + hprintln!("X: yield").ok(); + task::r#yield().await; + } } - }) + }); + + loop { continue; } } diff --git a/nrf52/examples/3-mutex2.rs b/nrf52/examples/3-mutex2.rs new file mode 100644 index 0000000000000000000000000000000000000000..bb8dbceae686c05015db4474a3fce626f75fbd80 --- /dev/null +++ b/nrf52/examples/3-mutex2.rs @@ -0,0 +1,57 @@ +#![deny(unsafe_code)] +#![deny(warnings)] +#![no_main] +#![no_std] + +use async_cortex_m::{task, unsync::Mutex}; +use cortex_m::asm; +use cortex_m_rt::entry; +use cortex_m_semihosting::hprintln; +use nrf52 as _; // memory layout +use panic_udf as _; // panic handler + +#[entry] +fn main() -> ! { + static mut X: Mutex<i64> = Mutex::new(0); + let c: &'static _ = X; + task::spawn(async move { + loop { + hprintln!("A: before lock").ok(); + + let mut lock = c.lock().await; + + hprintln!("A: before write {}", *lock).ok(); + *lock += 1; + drop(lock); + + hprintln!("A: after releasing the lock").ok(); + + hprintln!("A: manual yield").ok(); + task::r#yield().await; + } + }); + + task::block_on(async { + loop { + hprintln!("B: before lock").ok(); + + // cannot immediately make progress; context switch to A + let mut lock = c.lock().await; + + hprintln!("B: {}", *lock).ok(); + *lock += 1; + + if *lock > 10 { + loop { + asm::bkpt(); + } + } else { + drop(lock); + hprintln!("B: yield").ok(); + task::r#yield().await; + } + } + }); + + loop { continue; } +} \ No newline at end of file diff --git a/nrf52/examples/6-hello.rs b/nrf52/examples/6-hello.rs index 0aaf53e271aee0df5e141cc94b5fe6ff90505d71..4d4df083641b4620a1f56409131701293d13f9a4 100644 --- a/nrf52/examples/6-hello.rs +++ b/nrf52/examples/6-hello.rs @@ -26,9 +26,9 @@ fn main() -> ! { timer.wait(dur).await; Red.off(); timer.wait(dur).await; - Red.on(); - timer.wait(dur).await; - Red.off(); + // Red.on(); + // timer.wait(dur).await; + // Red.off(); timer.wait(12 * dur).await; } }); diff --git a/nrf52/memory.x b/nrf52/memory.x index 73962afe19148ffc3b1e7e50867d5e4b32289a25..dbcd285013f39ae817a158482e2b4c0d5a4f2e3b 100644 --- a/nrf52/memory.x +++ b/nrf52/memory.x @@ -2,5 +2,5 @@ MEMORY { /* NOTE 1 K = 1 KiBi = 1024 bytes */ FLASH : ORIGIN = 0x00000000, LENGTH = 1M - RAM : ORIGIN = 0x20000000, LENGTH = 256K + RAM : ORIGIN = 0x20000000, LENGTH = 32K } diff --git a/nrf52/openocd.cfg b/nrf52/openocd.cfg index 64a8edc44e7648564250f715b5ac126920d87a85..080b5b48c55cf5e6214b07ab5b798e5c41616330 100644 --- a/nrf52/openocd.cfg +++ b/nrf52/openocd.cfg @@ -1,2 +1,6 @@ -source [find interface/cmsis-dap.cfg] +#source [find interface/cmsis-dap.cfg] +source [find interface/jlink.cfg] + +transport select swd + source [find target/nrf52.cfg] \ No newline at end of file