diff --git a/firmware/src/command_interface.rs b/firmware/src/command_interface.rs index 0947ae2..3aa53db 100644 --- a/firmware/src/command_interface.rs +++ b/firmware/src/command_interface.rs @@ -1,7 +1,8 @@ use smoltcp::socket::tcp::{Socket, State}; use crate::proto::{ - serialize_response_error, serialize_response_value, PacketParser, ResponsePacket, Settings, ErrorCodes + serialize_response_error, serialize_response_value, ErrorCodes, PacketParser, ResponsePacket, + Settings, }; pub struct CommandInterface { @@ -70,7 +71,8 @@ impl CommandInterface { return (0, ()); } - let response = serialize_response_error(packet.setting, ErrorCodes::InvalidSetting); + let response = + serialize_response_error(packet.setting, ErrorCodes::InvalidSetting); &tx_buf[0..8].copy_from_slice(&response); return (8, ()); }); @@ -79,7 +81,12 @@ impl CommandInterface { } }; - defmt::debug!("Valid packet: {:?}, is_write: {}, value: {}", packet.setting, packet.is_write, packet.value); + defmt::debug!( + "Valid packet: {:?}, is_write: {}, value: {}", + packet.setting, + packet.is_write, + packet.value + ); // TODO validate setting values diff --git a/firmware/src/eth.rs b/firmware/src/eth.rs index 5c62360..8e3f657 100644 --- a/firmware/src/eth.rs +++ b/firmware/src/eth.rs @@ -69,7 +69,10 @@ impl LiteEthDevice { write_reg(csr_addr + ETHMAC_SRAM_WRITER_EV_ENABLE, 0u32); // Return a new device - Some(Self { csr_addr, ethmac_addr }) + Some(Self { + csr_addr, + ethmac_addr, + }) } } @@ -196,13 +199,9 @@ impl smoltcp::phy::RxToken for LiteEthRxToken { F: FnOnce(&mut [u8]) -> R, { // Read the slot number - let slot = unsafe { - read_reg::(self.csr_addr + ETHMAC_SRAM_WRITER_SLOT) - }; + let slot = unsafe { read_reg::(self.csr_addr + ETHMAC_SRAM_WRITER_SLOT) }; // Read the available length - let len = unsafe { - read_reg::(self.csr_addr + ETHMAC_SRAM_WRITER_LENGTH) - }; + let len = unsafe { read_reg::(self.csr_addr + ETHMAC_SRAM_WRITER_LENGTH) }; let rx_slot_addr: u32 = self.ethmac_addr + slot * SLOT_LEN; let rx_slot: &mut [u8] = diff --git a/firmware/src/logging.rs b/firmware/src/logging.rs index 59d2e9a..b7cff2c 100644 --- a/firmware/src/logging.rs +++ b/firmware/src/logging.rs @@ -1,4 +1,4 @@ -use core::{fmt::Write, any::Any}; +use core::{any::Any, fmt::Write}; use defmt; @@ -29,7 +29,6 @@ unsafe impl defmt::Logger for DefmtLogger { UART = Some(LitexUart::new(0xf000_4000)); } - let mut dev = UART.unwrap(); for byte in bytes { while let Err(_) = dev.try_put_char(*byte) {} diff --git a/firmware/src/main.rs b/firmware/src/main.rs index fe34436..c9d1c86 100644 --- a/firmware/src/main.rs +++ b/firmware/src/main.rs @@ -1,6 +1,5 @@ #![no_std] #![no_main] - // TODO remove #![allow(unused)] @@ -15,24 +14,24 @@ use core::{ use embedded_hal::prelude::{_embedded_hal_blocking_i2c_Read, _embedded_hal_blocking_i2c_Write}; use mcp4726::Status; use riscv_rt::entry; -use smoltcp::socket::{Socket, self}; +use smoltcp::socket::{self, Socket}; use smoltcp::time::Duration; use smoltcp::wire::{IpAddress, Ipv4Address}; use smoltcp::{ iface::{SocketSet, SocketStorage}, - time::Instant, - wire::HardwareAddress, socket::tcp::Socket as TcpSocket, socket::tcp::SocketBuffer, + time::Instant, + wire::HardwareAddress, }; +mod command_interface; mod eth; mod i2c; -mod mcp4726; -mod uart; mod logging; +mod mcp4726; mod proto; -mod command_interface; +mod uart; const MAC: [u8; 6] = [0xA0, 0xBB, 0xCC, 0xDD, 0xEE, 0xF0]; @@ -94,16 +93,15 @@ fn main() -> ! { sock.set_timeout(Some(Duration::from_secs(10))) } - let mut logger_tx_storage = [0u8; 128]; let mut logger_rx_storage = [0u8; 16]; let mut logger_tx_buf = SocketBuffer::new(&mut logger_tx_storage[..]); let mut logger_rx_buf = SocketBuffer::new(&mut logger_rx_storage[..]); let mut logger_socket = socket_set.add(TcpSocket::new(logger_tx_buf, logger_rx_buf)); - unsafe { logging::set_logger_socket(Some(socket_set.get_mut::(logger_socket))); } - - + unsafe { + logging::set_logger_socket(Some(socket_set.get_mut::(logger_socket))); + } let mut last_blink: u32 = 0; let mut toggle = false; @@ -137,7 +135,7 @@ fn main() -> ! { toggle = !toggle; write_led(if toggle { 1 } else { 0 }); - let val: u32 = unsafe {read_reg(0x8000_2000)}; + let val: u32 = unsafe { read_reg(0x8000_2000) }; } // TODO I think the timer might actually stop until the event is cleared? this may pose @@ -151,45 +149,45 @@ fn main() -> ! { //if iface.poll(Instant::from_millis(now), &mut device, &mut socket_set) { - //let sock = socket_set.get_mut::(command_socket); + //let sock = socket_set.get_mut::(command_socket); - //if !sock.is_open() { - // sock.listen(2000); - //} + //if !sock.is_open() { + // sock.listen(2000); + //} - //let mut echo_tx_buf = [0u8; 32]; - //match sock.recv(|buf| { - // if buf.len() > 0 { - // // Copy into send buffer - // let rd_len = core::cmp::min(buf.len(), echo_tx_buf.len()); - // &echo_tx_buf[..rd_len].copy_from_slice(&buf[..rd_len]); - // defmt::trace!("RX data command"); + //let mut echo_tx_buf = [0u8; 32]; + //match sock.recv(|buf| { + // if buf.len() > 0 { + // // Copy into send buffer + // let rd_len = core::cmp::min(buf.len(), echo_tx_buf.len()); + // &echo_tx_buf[..rd_len].copy_from_slice(&buf[..rd_len]); + // defmt::trace!("RX data command"); - // // Return slice to send - // (rd_len, Some(&echo_tx_buf[..rd_len])) - // } else { - // (0, None) - // } - //}) { - // Err(_) => { - // // Close the socket, re-open it later - // //sock.abort(); - // // Is doing this immediately legal? - // //sock.listen(2000); - // } - // // Some data to send - // Ok(Some(tx_data)) => { - // match sock.send_slice(tx_data) { - // Err(_) => { - // //sock.abort(); - // //sock.listen(2000); - // } - // _ => (), - // } - // } - // // No data to send - // Ok(None) => (), - //} + // // Return slice to send + // (rd_len, Some(&echo_tx_buf[..rd_len])) + // } else { + // (0, None) + // } + //}) { + // Err(_) => { + // // Close the socket, re-open it later + // //sock.abort(); + // // Is doing this immediately legal? + // //sock.listen(2000); + // } + // // Some data to send + // Ok(Some(tx_data)) => { + // match sock.send_slice(tx_data) { + // Err(_) => { + // //sock.abort(); + // //sock.listen(2000); + // } + // _ => (), + // } + // } + // // No data to send + // Ok(None) => (), + //} //} } @@ -206,7 +204,6 @@ fn handle_timer_event() { write_reg(0xf000_3818, 1u32); SECONDS += 1; } - } fn busy_wait(ms: u32) { @@ -217,8 +214,10 @@ fn busy_wait(ms: u32) { // } //} - for i in 0..ms*20_000 { - unsafe {asm!("nop");} + for i in 0..ms * 20_000 { + unsafe { + asm!("nop"); + } } } diff --git a/firmware/src/proto.rs b/firmware/src/proto.rs index 76d9f3f..5732cea 100644 --- a/firmware/src/proto.rs +++ b/firmware/src/proto.rs @@ -8,7 +8,7 @@ //! because the crc python package had it as well. //! //! The 4 bytes of data are little-endian. -//! +//! //! For the settings that can be set, see [Settings]. //! //! ## Sending a command