From 7e314410a013d8c7fe00e8465c3e0b68eabdf985 Mon Sep 17 00:00:00 2001 From: topquark12 Date: Wed, 2 Nov 2022 17:18:33 +0800 Subject: [PATCH] rustfmt, squash warnings, add bmp support --- .cargo/config | 2 +- Cargo.lock | 1 - Cargo.toml | 3 +- bmp.gdb | 6 ++ flake.nix | 2 +- src/device/boot.rs | 21 +++-- src/device/delay.rs | 4 +- src/device/gpio.rs | 12 +-- src/device/mod.rs | 2 +- src/laser_diode/current_sources.rs | 4 +- src/main.rs | 44 ++++------ src/network/mod.rs | 2 +- src/network/network.rs | 124 ++++++++++++++++------------- 13 files changed, 120 insertions(+), 107 deletions(-) create mode 100644 bmp.gdb diff --git a/.cargo/config b/.cargo/config index 8db196c..e75e445 100644 --- a/.cargo/config +++ b/.cargo/config @@ -1,5 +1,5 @@ [target.thumbv7em-none-eabihf] -runner = "gdb -q -x openocd.gdb" +runner = "gdb -batch -q -x bmp.gdb" rustflags = [ "-C", "link-arg=-Tlink.x", ] diff --git a/Cargo.lock b/Cargo.lock index 96ca44c..ebe979a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -683,7 +683,6 @@ checksum = "72165c4af59f5f19c7fb774b88b95660591b612380305b5f4503157341a9f7ee" dependencies = [ "bitflags", "byteorder", - "log", "managed", ] diff --git a/Cargo.toml b/Cargo.toml index 6613f93..ac5beb3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ nb = "1" cortex-m-log = { version = "0.7.0", features = ["log-integration", "semihosting"] } stm32f4xx-hal = { version = "0.13.2", features = ["rt", "stm32f407", "usb_fs", "rtic-monotonic"] } stm32-eth = { git = "https://github.com/stm32-rs/stm32-eth", features = ["stm32f407", "smoltcp-phy", "smoltcp"] } -smoltcp = { version = "0.8.1", default-features = false, features = ["proto-ipv4", "socket-tcp", "log", "medium-ethernet"] } +smoltcp = { version = "0.8.1", default-features = false, features = ["proto-ipv4", "socket-tcp", "medium-ethernet"] } ieee802_3_miim = "0.7.2" num-traits = { version = "0.2.15", default-features = false, features = ["libm"] } usb-device = "0.2.9" @@ -35,6 +35,7 @@ cortex-m-rtic = "1.0.0" systick-monotonic = "1.0.0" [features] +default = ["RTT"] semihosting = ["cortex-m-log/semihosting"] RTT = [] diff --git a/bmp.gdb b/bmp.gdb new file mode 100644 index 0000000..1671e58 --- /dev/null +++ b/bmp.gdb @@ -0,0 +1,6 @@ +target extended-remote /dev/ttyBmpGdb +monitor swdp_scan +attach 1 +load +compare-sections +kill diff --git a/flake.nix b/flake.nix index 1ce69ee..cfad675 100644 --- a/flake.nix +++ b/flake.nix @@ -66,7 +66,7 @@ buildInputs = with pkgs; [ rustPlatform.rust.rustc rustPlatform.rust.cargo - openocd dfu-util glibc + openocd dfu-util glibc picocom gdb ] ++ (with python3Packages; [ numpy matplotlib pyqtgraph ]); diff --git a/src/device/boot.rs b/src/device/boot.rs index 7daf35d..f7aec0e 100644 --- a/src/device/boot.rs +++ b/src/device/boot.rs @@ -1,8 +1,7 @@ -use super::{gpio, usb, delay}; +use super::{delay, gpio, usb}; use crate::laser_diode::current_sources::*; -use smoltcp::iface::{SocketHandle, Interface}; -use stm32_eth::stm32::ETHERNET_DMA; -use systick_monotonic::Systick; +use crate::network::network; +use crate::network::network::EthernetPeripherals; use fugit::ExtU32; use log::info; use stm32f4xx_hal::{ @@ -11,15 +10,22 @@ use stm32f4xx_hal::{ time::MegaHertz, watchdog::IndependentWatchdog, }; -use crate::network::network; -use crate::network::network::EthernetPeripherals; +use systick_monotonic::Systick; #[cfg(not(feature = "semihosting"))] const WATCHDOG_PERIOD: u32 = 1000; #[cfg(feature = "semihosting")] const WATCHDOG_PERIOD: u32 = 30000; -pub fn bootup(mut core_perif: CorePeripherals, perif: Peripherals, server_storage: &'static mut network::ServerStorage) -> (IndependentWatchdog, Systick<1000_u32>, network::ServerHandle) { +pub fn bootup( + mut core_perif: CorePeripherals, + perif: Peripherals, + server_storage: &'static mut network::ServerStorage, +) -> ( + IndependentWatchdog, + Systick<1000_u32>, + network::ServerHandle, +) { core_perif.SCB.enable_icache(); core_perif.SCB.enable_dcache(&mut core_perif.CPUID); @@ -43,7 +49,6 @@ pub fn bootup(mut core_perif: CorePeripherals, perif: Peripherals, server_storag perif.GPIOB, perif.GPIOC, perif.GPIOD, - perif.GPIOG, perif.SPI2, perif.OTG_FS_GLOBAL, perif.OTG_FS_DEVICE, diff --git a/src/device/delay.rs b/src/device/delay.rs index 7698854..c0a15eb 100644 --- a/src/device/delay.rs +++ b/src/device/delay.rs @@ -1,7 +1,7 @@ //! Basic blocking delay //! //! This module provides a basic asm-based blocking delay. -//! Borrowed from our good friends at Quartiq: +//! Borrowed from our good friends at Quartiq: //! https://github.com/quartiq/stabilizer/blob/master/src/hardware/delay.rs //! @@ -43,4 +43,4 @@ where fn delay_ms(&mut self, ms: U) { cortex_m::asm::delay(self.frequency_ms * ms.into()) } -} \ No newline at end of file +} diff --git a/src/device/gpio.rs b/src/device/gpio.rs index c8e856f..fd27bc7 100644 --- a/src/device/gpio.rs +++ b/src/device/gpio.rs @@ -2,9 +2,9 @@ use crate::laser_diode::current_sources::*; use fugit::RateExtU32; use stm32_eth::EthPins; use stm32f4xx_hal::{ - gpio::{GpioExt, AF11, Speed, Alternate}, + gpio::{Alternate, GpioExt, Speed}, otg_fs::USB, - pac::{GPIOA, GPIOB, GPIOC, GPIOD, GPIOG, OTG_FS_DEVICE, OTG_FS_GLOBAL, OTG_FS_PWRCLK, SPI2}, + pac::{GPIOA, GPIOB, GPIOC, GPIOD, OTG_FS_DEVICE, OTG_FS_GLOBAL, OTG_FS_PWRCLK, SPI2}, rcc::Clocks, spi, spi::{NoMiso, Spi}, @@ -18,7 +18,6 @@ pub fn setup( gpiob: GPIOB, gpioc: GPIOC, gpiod: GPIOD, - gpiog: GPIOG, spi2: SPI2, otg_fs_global: OTG_FS_GLOBAL, otg_fs_device: OTG_FS_DEVICE, @@ -28,15 +27,13 @@ pub fn setup( USB, CurrentSourcePhyConstruct, stm32f4xx_hal::gpio::PA2>, - stm32f4xx_hal::gpio::PC1> - // photo_diode_phy, - // thermostat_phy + stm32f4xx_hal::gpio::PC1>, // photo_diode_phy, + // thermostat_phy ) { let gpioa = gpioa.split(); let gpiob = gpiob.split(); let gpioc = gpioc.split(); let gpiod = gpiod.split(); - let gpiog = gpiog.split(); let usb = USB { usb_global: otg_fs_global, @@ -61,7 +58,6 @@ pub fn setup( let mut mdc = gpioc.pc1.into_alternate::<11>(); mdio.set_speed(Speed::VeryHigh); mdc.set_speed(Speed::VeryHigh); - let current_source_phy = CurrentSourcePhyConstruct { max5719_spi: Spi::new( diff --git a/src/device/mod.rs b/src/device/mod.rs index baaface..435ab97 100644 --- a/src/device/mod.rs +++ b/src/device/mod.rs @@ -1,6 +1,6 @@ pub mod boot; +pub mod delay; pub mod gpio; pub mod log_setup; pub mod rtt_logger; -pub mod delay; pub mod usb; diff --git a/src/laser_diode/current_sources.rs b/src/laser_diode/current_sources.rs index 5e84c28..ee4d790 100644 --- a/src/laser_diode/current_sources.rs +++ b/src/laser_diode/current_sources.rs @@ -3,7 +3,7 @@ use stm32f4xx_hal::{ gpio::{gpioa::*, gpiob::*, gpiod::*, Alternate, Output, PushPull, PD9}, hal::{blocking::spi::Write, digital::v2::OutputPin}, pac::SPI2, - spi::{NoMiso, Spi, TransferModeNormal} + spi::{NoMiso, Spi, TransferModeNormal}, }; pub trait CurrentSourcePhy { @@ -43,7 +43,7 @@ impl CurrentSourcePhy for CurrentSourcePhyCh0 { use crate::device::delay; impl CurrentSource { - pub fn setup(&mut self, mut delay : delay::AsmDelay) { + pub fn setup(&mut self, mut delay: delay::AsmDelay) { let _ = self.phy.max5719_load.set_high(); let _ = self.phy.max5719_cs.set_high(); let _ = self.phy.current_source_ldo_en.set_high(); diff --git a/src/main.rs b/src/main.rs index 79db30f..5ab13cf 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,10 +8,7 @@ mod network; // If RTT is used, print panic info through RTT #[cfg(feature = "RTT")] -use { - core::panic::PanicInfo, - rtt_target::rprintln, -}; +use {core::panic::PanicInfo, rtt_target::rprintln}; #[cfg(feature = "RTT")] #[panic_handler] fn panic(info: &PanicInfo) -> ! { @@ -26,52 +23,47 @@ use panic_halt as _; #[app(device = stm32f4xx_hal::pac, peripherals = true, dispatchers = [TIM8_CC, TIM8_BRK_TIM12])] mod app { - use log::info; - use stm32_eth::stm32::ETHERNET_DMA; - use systick_monotonic::Systick; - use fugit::ExtU32; - use stm32f4xx_hal::watchdog::IndependentWatchdog; use crate::device::{boot::bootup, log_setup}; - use smoltcp::iface::{SocketHandle, Interface}; use crate::network::network; - - fn now_fn() -> smoltcp::time::Instant { - let time = monotonics::now().duration_since_epoch().to_millis(); - smoltcp::time::Instant::from_millis(time as i64) - } + use fugit::ExtU32; + use log::info; + use stm32f4xx_hal::watchdog::IndependentWatchdog; + use systick_monotonic::Systick; #[monotonic(binds = SysTick, default = true)] type SystickTimer = Systick<1000>; #[shared] - struct Shared { - - } + struct Shared {} #[local] struct Local { wd: IndependentWatchdog, // server_storage: network::network::ServerStorage::new(), - server_handle: network::ServerHandle + server_handle: network::ServerHandle, } - #[init( local = [server_storage: network::ServerStorage = network::ServerStorage::new()] )] + #[init( local = [server_storage: network::ServerStorage = network::ServerStorage::new()] )] fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { log_setup::init_log(); info!("Kirdy init"); - + let core_perif = cx.core; let perif = cx.device; let server_storage = cx.local.server_storage; let (wd, systick, server_handle) = bootup(core_perif, perif, server_storage); - + wd_feed::spawn().unwrap(); // server_poll::spawn().unwrap(); - (Shared {}, Local {wd, server_handle}, init::Monotonics(systick)) + ( + Shared {}, + Local { wd, server_handle }, + init::Monotonics(systick), + ) } #[task(priority = 5, local = [wd])] - fn wd_feed (cx: wd_feed::Context) { + fn wd_feed(cx: wd_feed::Context) { let start = monotonics::now(); let wd = cx.local.wd; // info!("feed wd"); @@ -80,7 +72,7 @@ mod app { } #[task(binds = ETH, priority = 2, local = [server_handle, data: [u8; 512] = [0u8; 512]])] - fn server_poll (cx: server_poll::Context) { + fn server_poll(cx: server_poll::Context) { let data = cx.local.data; let server_handle = cx.local.server_handle; server_handle.poll(data); @@ -93,6 +85,4 @@ mod app { cortex_m::asm::nop(); } } - } - diff --git a/src/network/mod.rs b/src/network/mod.rs index 8ce92ac..a61610b 100644 --- a/src/network/mod.rs +++ b/src/network/mod.rs @@ -1 +1 @@ -pub mod network; \ No newline at end of file +pub mod network; diff --git a/src/network/network.rs b/src/network/network.rs index 99d4ccc..4410c7d 100644 --- a/src/network/network.rs +++ b/src/network/network.rs @@ -1,29 +1,34 @@ -use fugit::Instant; -use smoltcp::iface::{InterfaceBuilder, NeighborCache, SocketHandle, Interface, SocketStorage}; -use smoltcp::socket::{TcpSocket, TcpSocketBuffer, self, Socket}; -use smoltcp::storage::RingBuffer; -use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address}; -use stm32f4xx_hal::gpio::{gpioa::*, gpiob::*, gpioc::*, gpiog::*, Input}; -use stm32_eth::{EthPins, TxDescriptor, RxDescriptor, EthernetMAC}; -use stm32_eth::stm32::{ETHERNET_DMA, ETHERNET_MAC, ETHERNET_MMC}; -use stm32_eth::RingEntry; -use stm32_eth::TxRingEntry; -use stm32_eth::RxRingEntry; -use stm32f4xx_hal::rcc::Clocks; -use smoltcp::iface::Neighbor; -use stm32_eth::*; -use log::{info, warn, debug}; -use smoltcp::iface::Route; -use smoltcp::wire::Ipv4Cidr; -use stm32f4xx_hal::gpio::Alternate; +// Modified by Wong Tat Hang (aw@m-labs.hk), with original source from +// https://github.com/stm32-rs/stm32-eth/blob/master/examples/rtic-echo.rs + +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at + +// http://www.apache.org/licenses/LICENSE-2.0 + +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use log::{info, warn}; use smoltcp::{ - iface::{self}, - socket::{TcpState}, + iface::{ + Interface, InterfaceBuilder, Neighbor, NeighborCache, Route, SocketHandle, SocketStorage, + }, + socket::{TcpSocket, TcpSocketBuffer, TcpState}, + wire::{EthernetAddress, IpAddress, IpCidr, Ipv4Address, Ipv4Cidr}, +}; +use stm32_eth::stm32::{ETHERNET_DMA, ETHERNET_MAC, ETHERNET_MMC}; +use stm32_eth::*; +use stm32f4xx_hal::{ + gpio::{gpioa::*, gpiob::*, gpioc::*, Alternate, Input}, + rcc::Clocks, }; - -use crate::app::monotonics::{self, now}; - +use crate::app::monotonics; pub type EthernetPins = EthPins, PA7, PB11, PB12, PB13, PC4, PC5>; @@ -31,8 +36,16 @@ pub type EthernetPins = pub type EthInterface = Interface<'static, &'static mut EthernetDMA<'static, 'static>>; const IPV4_ADDR: (u8, u8, u8, u8) = (192, 168, 1, 132); -const ADDRESS: (IpAddress, u16) = (IpAddress::Ipv4(Ipv4Address::new(IPV4_ADDR.0, IPV4_ADDR.1,IPV4_ADDR.2,IPV4_ADDR.3)), 1337); -const MAC: [u8; 6] = [0x02,0x5f,0x25,0x37,0x93,0x0e]; +const ADDRESS: (IpAddress, u16) = ( + IpAddress::Ipv4(Ipv4Address::new( + IPV4_ADDR.0, + IPV4_ADDR.1, + IPV4_ADDR.2, + IPV4_ADDR.3, + )), + 1337, +); +const MAC: [u8; 6] = [0x02, 0x5f, 0x25, 0x37, 0x93, 0x0e]; pub struct EthernetPeripherals { pub dma: ETHERNET_DMA, pub mac: ETHERNET_MAC, @@ -41,8 +54,8 @@ pub struct EthernetPeripherals { pub struct ServerHandle { // storage: &'static mut ServerStorage, - socket_handle: SocketHandle, - iface: EthInterface + socket_handle: SocketHandle, + iface: EthInterface, } pub struct ServerStorage { @@ -54,11 +67,20 @@ pub struct ServerStorage { impl ServerStorage { pub const fn new() -> Self { - ServerStorage { - rx_ring: [RxRingEntry::new(),RxRingEntry::new(),RxRingEntry::new(),RxRingEntry::new(),RxRingEntry::new(),RxRingEntry::new(),RxRingEntry::new(),RxRingEntry::new()], - tx_ring: [TxRingEntry::new(),TxRingEntry::new()], + ServerStorage { + rx_ring: [ + RxRingEntry::new(), + RxRingEntry::new(), + RxRingEntry::new(), + RxRingEntry::new(), + RxRingEntry::new(), + RxRingEntry::new(), + RxRingEntry::new(), + RxRingEntry::new(), + ], + tx_ring: [TxRingEntry::new(), TxRingEntry::new()], storage: NetworkStorage::new(), - dma: core::mem::MaybeUninit::uninit(), + dma: core::mem::MaybeUninit::uninit(), } } } @@ -73,8 +95,10 @@ pub struct NetworkStorage { } impl NetworkStorage { - const IP_INIT: IpCidr = - IpCidr::Ipv4(Ipv4Cidr::new(Ipv4Address::new(IPV4_ADDR.0, IPV4_ADDR.1,IPV4_ADDR.2,IPV4_ADDR.3), 24)); + const IP_INIT: IpCidr = IpCidr::Ipv4(Ipv4Cidr::new( + Ipv4Address::new(IPV4_ADDR.0, IPV4_ADDR.1, IPV4_ADDR.2, IPV4_ADDR.3), + 24, + )); pub const fn new() -> Self { NetworkStorage { @@ -109,16 +133,14 @@ fn now_fn() -> smoltcp::time::Instant { } impl ServerHandle { - pub fn new( - eth_pins: EthernetPins, - ethernet: EthernetPeripherals, - clocks: Clocks, + eth_pins: EthernetPins, + ethernet: EthernetPeripherals, + clocks: Clocks, storage: &'static mut ServerStorage, mdio: stm32f4xx_hal::gpio::PA2>, - mdc: stm32f4xx_hal::gpio::PC1> + mdc: stm32f4xx_hal::gpio::PC1>, ) -> ServerHandle { - let (dma, mac) = stm32_eth::new_with_mii( ethernet.mac, ethernet.mmc, @@ -128,7 +150,7 @@ impl ServerHandle { clocks, eth_pins, mdio, - mdc + mdc, ) .unwrap(); @@ -137,12 +159,13 @@ impl ServerHandle { .add_default_ipv4_route(Ipv4Address::new(192, 168, 1, 1)) .ok(); - let dma = storage.dma.write(dma); dma.enable_interrupt(); - - let rx_buffer = TcpSocketBuffer::new(&mut storage.storage.tcp_socket_storage.rx_storage[..]); - let tx_buffer = TcpSocketBuffer::new(&mut storage.storage.tcp_socket_storage.tx_storage[..]); + + let rx_buffer = + TcpSocketBuffer::new(&mut storage.storage.tcp_socket_storage.rx_storage[..]); + let tx_buffer = + TcpSocketBuffer::new(&mut storage.storage.tcp_socket_storage.tx_storage[..]); let socket = TcpSocket::new(rx_buffer, tx_buffer); let mut iface = InterfaceBuilder::new(dma, &mut storage.storage.sockets[..]) @@ -153,11 +176,11 @@ impl ServerHandle { .finalize(); let mut server = ServerHandle { socket_handle: iface.add_socket(socket), - iface: iface + iface: iface, }; let socket = server.iface.get_socket::(server.socket_handle); socket.listen(ADDRESS).ok(); - server.iface.poll(now_fn()); + server.iface.poll(now_fn()).ok(); if let Ok(mut phy) = EthernetPhy::from_miim(mac, 0) { info!( "Resetting PHY as an extra step. Type: {}", @@ -173,11 +196,6 @@ impl ServerHandle { } pub fn poll(&mut self, buffer: &mut [u8]) { - - // info!("poll eth"); - let interrupt_reason = self.iface.device_mut().interrupt_handler(); - // debug!("Reason: {:?}", interrupt_reason); - self.iface.poll(now_fn()).ok(); let socket = self.iface.get_socket::(self.socket_handle); @@ -195,11 +213,9 @@ impl ServerHandle { } self.iface.poll(now_fn()).ok(); - } } - use ieee802_3_miim::{ phy::{ lan87xxa::{LAN8720A, LAN8742A}, @@ -277,4 +293,4 @@ impl EthernetPhy { } } } -} \ No newline at end of file +}