Browse Source

Enable local unit testing and debugging support

Kevin Lee 2 years ago
parent
commit
da1aae8438

+ 6 - 3
Nixie_Firmware_Rust/.cargo/config

@@ -6,7 +6,7 @@
 # uncomment ONE of these three option to make `cargo run` start a GDB session
 # which option to pick depends on your system
 # runner = "arm-none-eabi-gdb -q -x openocd.gdb"
-# runner = "gdb-multiarch -q -x openocd.gdb"
+runner = "gdb-multiarch -q -x openocd.gdb"
 # runner = "gdb -q -x openocd.gdb"
 
 rustflags = [
@@ -33,8 +33,11 @@ rustflags = [
 # Pick ONE of these compilation targets
 # target = "thumbv6m-none-eabi"        # Cortex-M0 and Cortex-M0+
 # target = "thumbv7m-none-eabi"        # Cortex-M3
-target = "thumbv7em-none-eabi"       # Cortex-M4 and Cortex-M7 (no FPU)
-# target = "thumbv7em-none-eabihf"     # Cortex-M4F and Cortex-M7F (with FPU)
+# target = "thumbv7em-none-eabi"       # Cortex-M4 and Cortex-M7 (no FPU)
+target = "thumbv7em-none-eabihf"     # Cortex-M4F and Cortex-M7F (with FPU)
 # target = "thumbv8m.base-none-eabi"   # Cortex-M23
 # target = "thumbv8m.main-none-eabi"   # Cortex-M33 (no FPU)
 # target = "thumbv8m.main-none-eabihf" # Cortex-M33 (with FPU)
+
+[alias]
+unittest = "test --target=x86_64-unknown-linux-gnu"

+ 1 - 1
Nixie_Firmware_Rust/.vscode/extensions.json

@@ -4,7 +4,7 @@
 
 	// List of extensions which should be recommended for users of this workspace.
 	"recommendations": [
-		"rust-lang.rust",
+		"matklad.rust-analyzer",
 		"marus25.cortex-debug",
 	],
 	// List of extensions recommended by VS Code that should not be recommended for users of this workspace.

+ 47 - 35
Nixie_Firmware_Rust/.vscode/launch.json

@@ -6,47 +6,59 @@
      */
     "version": "0.2.0",
     "configurations": [
+        // {
+        //     "type": "cortex-debug",
+        //     "request": "launch",
+        //     "name": "Debug (QEMU)",
+        //     "servertype": "qemu",
+        //     "cwd": "${workspaceRoot}",
+        //     "preLaunchTask": "Cargo Build (debug)",
+        //     "runToMain": true,
+        //     "executable": "./target/thumbv7m-none-eabi/debug/nixie-clock-in-8",
+        //     /* Run `cargo build --example hello` and uncomment this line to run semi-hosting example */
+        //     //"executable": "./target/thumbv7m-none-eabi/debug/examples/hello",
+        //     "cpu": "cortex-m3",
+        //     "machine": "lm3s6965evb",
+        // },
+        // {
+        //     /* Configuration for the STM32F303 Discovery board */
+        //     "type": "cortex-debug",
+        //     "request": "launch",
+        //     "name": "Debug (OpenOCD)",
+        //     "servertype": "openocd",
+        //     "cwd": "${workspaceRoot}",
+        //     "preLaunchTask": "Cargo Build (debug)",
+        //     "runToMain": true,
+        //     "executable": "./target/thumbv7em-none-eabihf/debug/nixie-clock-in-8",
+        //     /* Run `cargo build --example itm` and uncomment this line to run itm example */
+        //     // "executable": "./target/thumbv7em-none-eabihf/debug/examples/itm",
+        //     "device": "STM32F303VCT6",
+        //     "configFiles": [
+        //         "interface/stlink-v2-1.cfg",
+        //         "target/stm32f3x.cfg"
+        //     ],
+        //     "svdFile": "${workspaceRoot}/.vscode/STM32F303.svd",
+        //     "swoConfig": {
+        //         "enabled": true,
+        //         "cpuFrequency": 8000000,
+        //         "swoFrequency": 2000000,
+        //         "source": "probe",
+        //         "decoders": [
+        //             { "type": "console", "label": "ITM", "port": 0 }
+        //         ]
+        //     }
+        // }
         {
             "type": "cortex-debug",
             "request": "launch",
-            "name": "Debug (QEMU)",
-            "servertype": "qemu",
+            "name": "Debug (gdb-remote)",
+            "servertype": "external",
             "cwd": "${workspaceRoot}",
             "preLaunchTask": "Cargo Build (debug)",
             "runToMain": true,
-            "executable": "./target/thumbv7m-none-eabi/debug/{{project-name}}",
-            /* Run `cargo build --example hello` and uncomment this line to run semi-hosting example */
-            //"executable": "./target/thumbv7m-none-eabi/debug/examples/hello",
-            "cpu": "cortex-m3",
-            "machine": "lm3s6965evb",
-        },
-        {
-            /* Configuration for the STM32F303 Discovery board */
-            "type": "cortex-debug",
-            "request": "launch",
-            "name": "Debug (OpenOCD)",
-            "servertype": "openocd",
-            "cwd": "${workspaceRoot}",
-            "preLaunchTask": "Cargo Build (debug)",
-            "runToMain": true,
-            "executable": "./target/thumbv7em-none-eabihf/debug/{{project-name}}",
-            /* Run `cargo build --example itm` and uncomment this line to run itm example */
-            // "executable": "./target/thumbv7em-none-eabihf/debug/examples/itm",
-            "device": "STM32F303VCT6",
-            "configFiles": [
-                "interface/stlink-v2-1.cfg",
-                "target/stm32f3x.cfg"
-            ],
-            "svdFile": "${workspaceRoot}/.vscode/STM32F303.svd",
-            "swoConfig": {
-                "enabled": true,
-                "cpuFrequency": 8000000,
-                "swoFrequency": 2000000,
-                "source": "probe",
-                "decoders": [
-                    { "type": "console", "label": "ITM", "port": 0 }
-                ]
-            }
+            "executable": "./target/thumbv7em-none-eabihf/debug/nixie-clock-in-8",
+            "gdbTarget": "10.3.0.19:3333",
+            "gdbPath": "/usr/bin/gdb-multiarch"
         }
     ]
 }

+ 14 - 6
Nixie_Firmware_Rust/Cargo.toml

@@ -1,8 +1,8 @@
 [package]
-authors = ["Kevin Lee"]
+authors = ["Kevin Lee <Kevin@0x7D.com>"]
 edition = "2018"
 readme = "README.md"
-name = "nixie_firmware_rust"
+name = "nixie-clock-in-8"
 version = "0.1.0"
 
 [dependencies]
@@ -24,11 +24,19 @@ panic-halt = "0.2.0"
 # features = ["stm32f303", "rt"]
 # version = "0.7.1"
 
+# [dependencies.stm32l4]
+# version = "0.13.0"
+# features = ["stm32l4x2", "rt"]
+
+[dependencies.stm32l4xx-hal]
+version = "0.6.0"
+features = ["stm32l4x2"]
+
 # this lets you use `cargo fix`!
-[[bin]]
-name = "nixie_firmware_rust"
-test = false
-bench = false
+# [[bin]]
+# name = "nixie-clock-in-8"
+# test = false
+# bench = false
 
 [profile.release]
 codegen-units = 1 # better optimizations

+ 2 - 3
Nixie_Firmware_Rust/memory.x

@@ -2,9 +2,8 @@ MEMORY
 {
   /* NOTE 1 K = 1 KiBi = 1024 bytes */
   /* TODO Adjust these memory regions to match your device memory layout */
-  /* These values correspond to the LM3S6965, one of the few devices QEMU can emulate */
-  FLASH : ORIGIN = 0x00000000, LENGTH = 256K
-  RAM : ORIGIN = 0x20000000, LENGTH = 48K
+  FLASH : ORIGIN = 0x08000000, LENGTH = 256K
+  RAM : ORIGIN = 0x20000000, LENGTH = 64K
 }
 
 /* This is where the call stack will be allocated. */

Nixie_Firmware_Rust/examples/allocator.rs → Nixie_Firmware_Rust/old_examples/allocator.rs


Nixie_Firmware_Rust/examples/crash.rs → Nixie_Firmware_Rust/old_examples/crash.rs


Nixie_Firmware_Rust/examples/device.rs → Nixie_Firmware_Rust/old_examples/device.rs


Nixie_Firmware_Rust/examples/exception.rs → Nixie_Firmware_Rust/old_examples/exception.rs


Nixie_Firmware_Rust/examples/hello.rs → Nixie_Firmware_Rust/old_examples/hello.rs


Nixie_Firmware_Rust/examples/itm.rs → Nixie_Firmware_Rust/old_examples/itm.rs


Nixie_Firmware_Rust/examples/panic.rs → Nixie_Firmware_Rust/old_examples/panic.rs


Nixie_Firmware_Rust/examples/test_on_host.rs → Nixie_Firmware_Rust/old_examples/test_on_host.rs


+ 1 - 1
Nixie_Firmware_Rust/openocd.gdb

@@ -1,4 +1,4 @@
-target extended-remote :3333
+target extended-remote 10.3.0.19:3333
 
 # print demangled symbols
 set print asm-demangle on

+ 35 - 4
Nixie_Firmware_Rust/src/main.rs

@@ -1,20 +1,51 @@
-#![no_std]
-#![no_main]
+#![cfg_attr(test, allow(unused_imports))]
+
+#![cfg_attr(not(test), no_std)]
+#![cfg_attr(not(test), no_main)]
 
 // pick a panicking behavior
+#[cfg(not(test))]
 use panic_halt as _; // you can put a breakpoint on `rust_begin_unwind` to catch panics
 // use panic_abort as _; // requires nightly
 // use panic_itm as _; // logs messages over ITM; requires ITM support
 // use panic_semihosting as _; // logs messages to the host stderr; requires a debugger
 
-use cortex_m::asm;
+// use cortex_m::asm;
 use cortex_m_rt::entry;
+// use stm32l4::stm32l4x2;
+use stm32l4xx_hal::{prelude::*, pac, rcc};
+use cortex_m_semihosting::{debug, hprintln};
 
+#[cfg(not(test))]
 #[entry]
 fn main() -> ! {
-    asm::nop(); // To not have main optimize to abort in release mode, remove when you add code
+
+    // asm::nop(); // To not have main optimize to abort in release mode, remove when you add code
+    hprintln!("Hello, world!").unwrap();
+
+    let peripherals = pac::Peripherals::take().unwrap();
+    let _core = cortex_m::Peripherals::take().unwrap();
+
+
+    let gpioa = &peripherals.GPIOA;
+    gpioa.odr.modify(|_, w| w.odr0().set_bit());
 
     loop {
         // your code goes here
     }
 }
+
+fn add(a: i32, b: i32) -> i32 {
+    a + b
+}
+
+#[cfg(test)]
+mod test {
+  use super::*;
+
+  #[test]
+  fn foo() {
+    println!("tests work!");
+    assert!(2 == add(1,1));
+  }
+}