diff --git a/Cargo.lock b/Cargo.lock index a179a36..8eb3a32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1118,6 +1118,15 @@ dependencies = [ "nom", ] +[[package]] +name = "nu-ansi-term" +version = "0.50.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" +dependencies = [ + "windows-sys 0.61.2", +] + [[package]] name = "num" version = "0.4.3" @@ -1395,6 +1404,8 @@ dependencies = [ "tokio-serial", "tokio-util", "tower-http", + "tracing", + "tracing-subscriber", ] [[package]] @@ -1641,6 +1652,15 @@ dependencies = [ "windows-sys 0.52.0", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "shlex" version = "1.3.0" @@ -1821,6 +1841,15 @@ dependencies = [ "syn 2.0.111", ] +[[package]] +name = "thread_local" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f60246a4944f24f6e018aa17cdeffb7818b76356965d03b07d6a9886e8962185" +dependencies = [ + "cfg-if", +] + [[package]] name = "time" version = "0.3.44" @@ -1959,9 +1988,21 @@ checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" dependencies = [ "log", "pin-project-lite", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.111", +] + [[package]] name = "tracing-core" version = "0.1.36" @@ -1969,6 +2010,32 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2f30143827ddab0d256fd843b7a66d164e9f271cfa0dde49142c5ca0ca291f1e" +dependencies = [ + "nu-ansi-term", + "sharded-slab", + "smallvec", + "thread_local", + "tracing-core", + "tracing-log", ] [[package]] @@ -2059,6 +2126,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "valuable" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba73ea9cf16a25df0c8caa16c51acb937d5712a8429db78a3ee29d5dcacd3a65" + [[package]] name = "vcell" version = "0.1.3" diff --git a/daemon/Cargo.toml b/daemon/Cargo.toml index 2cda2bc..79b7d20 100644 --- a/daemon/Cargo.toml +++ b/daemon/Cargo.toml @@ -14,8 +14,6 @@ serde_json = "1.0.118" serialport = "4.5.1" tokio = {version = "1.37.0", features = ["full"]} tower-http = { version = "0.6.8", features = ["fs", "trace"] } -radomctl-protocol = { path = "../protocol" } -postcard = {version = "1.0.10", features = ["use-std"]} dfu-libusb = "0.5.5" rusb = "0.9" clap = { version = "4.5.19", features = ["derive"] } @@ -25,3 +23,8 @@ tokio-util = { version = "0.7.13", features = ["codec", "rt"] } bytes = "1.9.0" futures = "0.3.31" nom-language = "0.1.0" +tracing = "0.1.40" +tracing-subscriber = "0.3.18" +radomctl-protocol = { path = "../protocol" } +postcard = {version = "1.0.10", features = ["use-std"]} + diff --git a/daemon/src/bin/trigger-dfu.rs b/daemon/src/bin/trigger-dfu.rs new file mode 100644 index 0000000..472f081 --- /dev/null +++ b/daemon/src/bin/trigger-dfu.rs @@ -0,0 +1,16 @@ +use std::time::Duration; + +use postcard::{from_bytes_cobs, to_stdvec_cobs}; +use radomctl_protocol::*; +use radomctld::logger::setup_logger; + +pub fn main() -> () { + let mut port = serialport::new("/dev/ttyACM0", 115_200) + .timeout(Duration::from_millis(10)) + .open() + .expect("Failed to open port"); + + let host_msg = HostMessage::TriggerDFUBootloader; + let msg_bytes = to_stdvec_cobs(&host_msg).unwrap(); + port.write_all(&msg_bytes).unwrap(); +} diff --git a/daemon/src/main.rs b/daemon/src/main.rs index 29f94a2..c4aad25 100644 --- a/daemon/src/main.rs +++ b/daemon/src/main.rs @@ -12,13 +12,17 @@ use tokio::{ task::JoinSet, }; use tokio_serial; -use tower_http::{services::ServeFile, trace::TraceLayer}; use radomctld::{ logger::setup_logger, rotctlprotocol::{parse_command, Command}, rotor::control_rotor, }; +use tower_http::{ + services::{ServeDir, ServeFile}, + trace::TraceLayer, +}; + async fn process_socket( socket: TcpStream, diff --git a/firmware/src/bootloader.rs b/firmware/src/bootloader.rs index c186b18..0c87fe3 100644 --- a/firmware/src/bootloader.rs +++ b/firmware/src/bootloader.rs @@ -1,5 +1,5 @@ use core::mem::MaybeUninit; - +use core::ptr::addr_of_mut; use stm32f4xx_hal::pac; fn jump_to_bootloader() { @@ -23,19 +23,25 @@ fn jump_to_bootloader() { const BOOTLOADER_REQUESTED: u32 = 0xdecafbad; -#[link_section = ".uninit.DFU_FLAG"] -static mut DFU_FLAG: MaybeUninit = MaybeUninit::uninit(); +fn magic_mut_ptr() -> *mut u32 { + extern "C" { + #[link_name = "_dfu_magic"] + static mut magic: u32; + } + + unsafe { addr_of_mut!(magic) } +} fn get_bootloader_flag() -> u32 { - unsafe { DFU_FLAG.assume_init() } + unsafe { magic_mut_ptr().read_volatile() } } fn set_bootloader_flag() { - unsafe { DFU_FLAG.write(BOOTLOADER_REQUESTED) }; + unsafe { magic_mut_ptr().write_volatile(BOOTLOADER_REQUESTED) }; } fn clear_bootloader_flag() { - unsafe { DFU_FLAG.write(0) }; + unsafe { magic_mut_ptr().write_volatile(BOOTLOADER_REQUESTED) }; } pub fn init() { diff --git a/firmware/src/main.rs b/firmware/src/main.rs index 4738239..a651168 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -23,10 +23,15 @@ mod app { use as5048a::AS5048A; + use crate::bootloader; use core::fmt::Write; use heapless::{String, Vec}; use num_traits::{Float, FloatConst}; use postcard::{from_bytes_cobs, to_vec_cobs}; + use qmc5883l::{self, QMC5883L}; + use radomctl_protocol::*; + use radomctl_protocol::{HostMessage, *}; + use rtic_monotonics::systick::prelude::*; use stm32f4xx_hal::{ gpio::{gpioa, gpiob, gpioc, Output, PushPull}, i2c, @@ -39,20 +44,9 @@ mod app { use usb_device::prelude::*; use usb_device::{class_prelude::UsbBusAllocator, device}; use usbd_serial::SerialPort; - use xca9548a::{SlaveAddr, Xca9548a}; - use qmc5883l::{self, QMC5883L}; - - use radomctl_protocol::{HostMessage, *}; - - use crate::bootloader; - - use radomctl_protocol::*; - - use rtic_monotonics::systick::prelude::*; - - systick_monotonic!(Mono, 4000); + systick_monotonic!(Mono, 1000); const USB_BUFFER_SIZE: usize = 64; @@ -104,6 +98,8 @@ mod app { Mono::start(cx.core.SYST, rcc.clocks.sysclk().to_Hz()); + defmt::info!("Clock Setup done"); + // Acquire the GPIO peripherials let gpioa = cx.device.GPIOA.split(&mut rcc); let gpiob = cx.device.GPIOB.split(&mut rcc); @@ -132,22 +128,11 @@ mod app { let usb_serial = usbd_serial::SerialPort::new(unsafe { USB_BUS.as_ref().unwrap() }); - let serial = unsafe { - let u_id0 = 0x1FFF_7A10 as *const u32; - let u_id2 = 0x1FFF_7A18 as *const u32; - - defmt::debug!("UID0: {:x}", u_id0.read()); - defmt::debug!("UID2: {:x}", u_id2.read()); - - // See https://community.st.com/t5/stm32-mcus-products/usb-bootloader-serial-number/td-p/432148 - (u_id0.read() as u64 + u_id2.read() as u64) << 16 - | (u_id2.read() as u64 & 0xFF00) >> 8 - | (u_id2.read() as u64 & 0x00FF) << 8 - }; + let uid = signature::Uid::get(); static mut SERIAL: String<16> = String::new(); unsafe { - write!(SERIAL, "{:X}", serial).unwrap(); + write!(SERIAL, "{}{:x}{:x}", uid.lot_num(), uid.x(), uid.y()).unwrap(); } let usb_dev = unsafe { @@ -177,7 +162,7 @@ mod app { defmt::info!("I2C Setup done"); let mut i2cmux = Xca9548a::new(i2c, SlaveAddr::default()); - i2cmux.select_channels(0b0000_0001).unwrap(); + //i2cmux.select_channels(0b0000_0001).unwrap(); defmt::info!("I2C MUX Setup done"); diff --git a/memory.x b/memory.x index 78b279a..33fd2a3 100644 --- a/memory.x +++ b/memory.x @@ -1,9 +1,23 @@ MEMORY { FLASH : ORIGIN = 0x08000000, LENGTH = 256K - RAM : ORIGIN = 0x20000000, LENGTH = 64K + UNINIT : ORIGIN = 0x20000000, LENGTH = 0x10 + RAM : ORIGIN = 0x20000010, LENGTH = 64K - LENGTH(UNINIT) } +SECTIONS { + .uninit_flags (NOLOAD) : ALIGN(4) + { + . = ALIGN(4); + __suninit_flags = .; + _dfu_magic = .; . += 4; + *(.uninit_flags .uninit_flags.*) + . = ALIGN(4); + __euninit_flags = .; + } > UNINIT +} + + /* This is where the call stack will be allocated. */ /* The stack is of the full descending type. */ /* NOTE Do NOT modify `_stack_start` unless you know what you are doing */