Simplify, styling & spelling

pull/3/head
Harry Ho 2020-12-29 11:08:35 +08:00
parent e9a3a5e550
commit 4ba5052623
5 changed files with 73 additions and 76 deletions

View File

@ -4,7 +4,7 @@
use core::env; use core::env;
extern crate panic_itm; extern crate panic_itm;
use cortex_m::iprintln; use cortex_m::{iprintln, iprint};
use cortex_m_rt::entry; use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin; use embedded_hal::digital::v2::OutputPin;
@ -140,26 +140,30 @@ fn main() -> ! {
// Init // Init
match spi_eth.init_dev(&mut delay) { match spi_eth.init_dev(&mut delay) {
Ok(_) => { Ok(_) => {
iprintln!(stim0, "Ethernet initialised.") iprintln!(stim0, "Ethernet initialized")
} }
Err(_) => { Err(_) => {
panic!("Ethernet initialisation Failed!") panic!("Ethernet initialization failed!")
} }
} }
// Setup SysTick // Setup SysTick
// Reference to stm32-eth:examples/ip.rs // Reference to stm32-eth:examples/ip.rs
timer_setup(delay.free(), clocks); timer_setup(delay.free(), clocks);
iprintln!(stim0, "Timer initialised."); iprintln!(stim0, "Timer initialized");
// Read MAC // Read MAC
let mut eth_mac_addr: [u8; 6] = [0; 6]; let mut eth_mac_addr: [u8; 6] = [0; 6];
spi_eth.read_from_mac(&mut eth_mac_addr); spi_eth.read_from_mac(&mut eth_mac_addr);
iprintln!(stim0, for i in 0..6 {
"MAC Address = {:02x}-{:02x}-{:02x}-{:02x}-{:02x}-{:02x}", let byte = eth_mac_addr[i];
eth_mac_addr[0], eth_mac_addr[1], match i {
eth_mac_addr[2], eth_mac_addr[3], 0 => iprint!(stim0, "MAC Address = {:02x}-", byte),
eth_mac_addr[4], eth_mac_addr[5]); 1..=4 => iprint!(stim0, "{:02x}-", byte),
5 => iprint!(stim0, "{:02x}\n", byte),
_ => ()
};
}
// Init Rx/Tx buffers // Init Rx/Tx buffers
spi_eth.init_rxbuf(); spi_eth.init_rxbuf();
@ -199,8 +203,7 @@ fn main() -> ! {
let mut socket_set = SocketSet::new(&mut socket_set_entries[..]); let mut socket_set = SocketSet::new(&mut socket_set_entries[..]);
let echo_handle = socket_set.add(echo_socket); let echo_handle = socket_set.add(echo_socket);
let greet_handle = socket_set.add(greet_socket); let greet_handle = socket_set.add(greet_socket);
iprintln!(stim0, iprintln!(stim0, "TCP sockets will listen at {}", ip_addr);
"TCP sockets will listen at {}", ip_addr);
// Copied / modified from: // Copied / modified from:
// smoltcp:examples/loopback.rs, examples/server.rs; // smoltcp:examples/loopback.rs, examples/server.rs;
@ -250,8 +253,5 @@ fn main() -> ! {
socket.close(); socket.close();
} }
} }
} }
unreachable!()
} }

View File

@ -2,7 +2,7 @@
#![no_main] #![no_main]
extern crate panic_itm; extern crate panic_itm;
use cortex_m::iprintln; use cortex_m::{iprintln, iprint};
use cortex_m_rt::entry; use cortex_m_rt::entry;
use embedded_hal::digital::v2::OutputPin; use embedded_hal::digital::v2::OutputPin;
@ -65,25 +65,25 @@ fn main() -> ! {
// Init // Init
match spi_eth.init_dev(&mut delay) { match spi_eth.init_dev(&mut delay) {
Ok(_) => { Ok(_) => {
iprintln!(stim0, "Ethernet initialised.") iprintln!(stim0, "Ethernet initialized")
} }
Err(_) => { Err(_) => {
panic!("Ethernet initialisation Failed!") panic!("Ethernet initialization failed!")
} }
} }
// Read MAC // Read MAC
let mut eth_mac_addr: [u8; 6] = [0; 6]; let mut eth_mac_addr: [u8; 6] = [0; 6];
spi_eth.read_from_mac(&mut eth_mac_addr); spi_eth.read_from_mac(&mut eth_mac_addr);
iprintln!(stim0, for i in 0..6 {
"MAC Address = {:02x}-{:02x}-{:02x}-{:02x}-{:02x}-{:02x}", let byte = eth_mac_addr[i];
eth_mac_addr[0], eth_mac_addr[1], match i {
eth_mac_addr[2], eth_mac_addr[3], 0 => iprint!(stim0, "MAC Address = {:02x}-", byte),
eth_mac_addr[4], eth_mac_addr[5]); 1..=4 => iprint!(stim0, "{:02x}-", byte),
// Set to promiscuous mode 5 => iprint!(stim0, "{:02x}\n", byte),
spi_eth.set_promiscuous(); _ => ()
iprintln!(stim0, };
"Promiscuous Mode ON"); }
// Init Rx/Tx buffers // Init Rx/Tx buffers
spi_eth.init_rxbuf(); spi_eth.init_rxbuf();
@ -102,26 +102,23 @@ fn main() -> ! {
loop { loop {
let mut eth_tx_packet = enc424j600::tx::TxPacket::new(); let mut eth_tx_packet = enc424j600::tx::TxPacket::new();
eth_tx_packet.update_frame(&eth_tx_dat, 64); eth_tx_packet.update_frame(&eth_tx_dat, 64);
iprintln!(stim0, iprint!(stim0,
"Sending packet (len={:}): \ "Sending packet (len={:}): ", eth_tx_packet.get_frame_length());
dest={:02x}-{:02x}-{:02x}-{:02x}-{:02x}-{:02x} \ for i in 0..20 {
src={:02x}-{:02x}-{:02x}-{:02x}-{:02x}-{:02x} \ let byte = eth_tx_packet.get_frame_byte(i);
data={:02x}{:02x}{:02x}{:02x} {:02x}{:02x}{:02x}{:02x} ...", match i {
eth_tx_packet.get_frame_length(), 0 => iprint!(stim0, "dest={:02x}-", byte),
eth_tx_packet.get_frame_byte(0), eth_tx_packet.get_frame_byte(1), eth_tx_packet.get_frame_byte(2), 6 => iprint!(stim0, "src={:02x}-", byte),
eth_tx_packet.get_frame_byte(3), eth_tx_packet.get_frame_byte(4), eth_tx_packet.get_frame_byte(5), 12 => iprint!(stim0, "data={:02x}", byte),
eth_tx_packet.get_frame_byte(6), eth_tx_packet.get_frame_byte(7), eth_tx_packet.get_frame_byte(8), 1..=4 | 7..=10 => iprint!(stim0, "{:02x}-", byte),
eth_tx_packet.get_frame_byte(9), eth_tx_packet.get_frame_byte(10), eth_tx_packet.get_frame_byte(11), 13..=14 | 16..=18 => iprint!(stim0, "{:02x}", byte),
eth_tx_packet.get_frame_byte(12), eth_tx_packet.get_frame_byte(13), 5 | 11 | 15 => iprint!(stim0, "{:02x} ", byte),
eth_tx_packet.get_frame_byte(14), eth_tx_packet.get_frame_byte(15), 19 => iprint!(stim0, "{:02x} ...\n", byte),
eth_tx_packet.get_frame_byte(16), eth_tx_packet.get_frame_byte(17), _ => ()
eth_tx_packet.get_frame_byte(18), eth_tx_packet.get_frame_byte(19) };
); }
spi_eth.send_raw_packet(&eth_tx_packet); spi_eth.send_raw_packet(&eth_tx_packet);
iprintln!(stim0, iprintln!(stim0, "Packet sent");
"Packet sent");
delay.delay_ms(100_u32); delay.delay_ms(100_u32);
} }
unreachable!()
} }