From 5af5dcab8734461964cf3c126ca1a7e9f3659ed1 Mon Sep 17 00:00:00 2001
From: Per Lindgren <per.lindgren@ltu.se>
Date: Sun, 11 Feb 2018 23:35:00 +0100
Subject: [PATCH] rtfm examples

---
 .vscode/launch.json            | 14 ++++++
 examples/rtfm-serial-dma-rx.rs | 88 ++++++++++++++++++++++++++++++++++
 examples/serial-dma-rx.rs      |  5 ++
 3 files changed, 107 insertions(+)
 create mode 100644 examples/rtfm-serial-dma-rx.rs

diff --git a/.vscode/launch.json b/.vscode/launch.json
index 9cdce3d..963cbbd 100644
--- a/.vscode/launch.json
+++ b/.vscode/launch.json
@@ -124,6 +124,20 @@
             "servertype": "openocd",
             "name": "c serial-dma-rx",
             "executable": "./target/thumbv7em-none-eabihf/debug/examples/serial-dma-rx",
+            //"debugger_args": "so .gdbint",
+            "configFiles": [
+                "interface/stlink.cfg",
+                "target/stm32f4x.cfg"
+            ],
+            "cwd": "${workspaceRoot}"
+        },
+        {
+            "type": "cortex-debug",
+            "request": "launch",
+            "servertype": "openocd",
+            "name": "c rtfm-serial-dma-rx",
+            "executable": "./target/thumbv7em-none-eabihf/debug/examples/rtfm-serial-dma-rx",
+            //"debugger_args": "so .gdbint",
             "configFiles": [
                 "interface/stlink.cfg",
                 "target/stm32f4x.cfg"
diff --git a/examples/rtfm-serial-dma-rx.rs b/examples/rtfm-serial-dma-rx.rs
new file mode 100644
index 0000000..97b29e0
--- /dev/null
+++ b/examples/rtfm-serial-dma-rx.rs
@@ -0,0 +1,88 @@
+//! Serial interface echo server
+//!
+//! In this example every received byte will be sent back to the sender. You can test this example
+//! with serial terminal emulator like `minicom`.
+//!
+//!
+#![deny(unsafe_code)]
+//#![deny(warnings)]
+#![feature(proc_macro)]
+#![no_std]
+
+#[macro_use(singleton)]
+extern crate cortex_m;
+extern crate cortex_m_rtfm as rtfm;
+extern crate stm32f4x_hal as hal;
+
+use hal::prelude::*;
+use hal::serial::{Rx, Serial, Tx};
+use hal::dma::Event;
+use hal::stm32f4x;
+use rtfm::{app, Threshold};
+use cortex_m::asm;
+
+app! {
+    device: stm32f4x,
+
+    resources : {
+        static X: u32;
+    //    static CB: CircBuffer<[u8; 8], dma1::S6<C5>>;
+    },
+
+    tasks: {
+        DMA1_STREAM6: {
+            path: tx,
+            resources : [X]
+        },
+        DMA1_STREAM5: {
+            path: rx,
+            resources : [X]
+        }
+    }
+}
+
+fn init(p: init::Peripherals) -> init::LateResources {
+    let mut flash = p.device.FLASH.constrain();
+    let mut rcc = p.device.RCC.constrain();
+    let mut gpioa = p.device.GPIOA.split(&mut rcc.ahb1);
+    let streams = p.device.DMA1.split(&mut rcc.ahb1);
+    let mut tx_stream = streams.1.into_channel4(); // S6<C4>
+    let mut rx_stream = streams.0.into_channel4(); // S5<C4>
+
+    let clocks = rcc.cfgr.freeze(&mut flash.acr);
+
+    let tx = gpioa.pa2.into_af7(&mut gpioa.moder, &mut gpioa.afrl);
+    let rx = gpioa.pa3.into_af7(&mut gpioa.moder, &mut gpioa.afrl);
+
+    let mut serial = Serial::usart2(
+        p.device.USART2,
+        (tx, rx),
+        115_200.bps(),
+        clocks,
+        &mut rcc.apb1,
+    );
+
+    let (tx, rx) = serial.split();
+    //tx_stream.listen(Event::TransferComplete);
+    rx_stream.listen(Event::TransferComplete);
+
+    let buf = singleton!(: [u8; 8] = [0; 8]).unwrap();
+
+    let _ = tx.write_all(tx_stream, b"The quick brown fox");
+    init::LateResources { X: 0 }
+}
+
+fn idle() -> ! {
+    // sleep
+    loop {
+        // rtfm::wfi();
+    }
+}
+
+fn tx(_: &mut Threshold, mut r: DMA1_STREAM6::Resources) {
+    asm::bkpt();
+}
+
+fn rx(_: &mut Threshold, mut r: DMA1_STREAM5::Resources) {
+    asm::bkpt();
+}
diff --git a/examples/serial-dma-rx.rs b/examples/serial-dma-rx.rs
index 90cd775..f48d41e 100644
--- a/examples/serial-dma-rx.rs
+++ b/examples/serial-dma-rx.rs
@@ -8,6 +8,8 @@
 
 #[macro_use(singleton)]
 extern crate cortex_m;
+#[macro_use]
+extern crate cortex_m_debug;
 extern crate stm32f4x_hal as f4;
 
 use cortex_m::asm;
@@ -16,6 +18,8 @@ use f4::serial::Serial;
 use f4::stm32f4x;
 
 fn main() {
+    ipln!("serial-dma-rx");
+
     let p = stm32f4x::Peripherals::take().unwrap();
 
     let mut flash = p.FLASH.constrain();
@@ -35,6 +39,7 @@ fn main() {
     let buf = singleton!(: [u8; 8] = [0; 8]).unwrap();
 
     let (_buf, _c, _rx) = rx.read_exact(rx_stream, buf).wait();
+    ipln!("{:?}", _buf);
 
     asm::bkpt();
 }
-- 
GitLab