Compare commits
No commits in common. "master" and "feature/nal" have entirely different histories.
master
...
feature/na
|
@ -1,117 +0,0 @@
|
||||||
name: Continuous Integration
|
|
||||||
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
branches: [master]
|
|
||||||
|
|
||||||
pull_request:
|
|
||||||
branches: [master]
|
|
||||||
|
|
||||||
env:
|
|
||||||
CARGO_TERM_COLOR: always
|
|
||||||
|
|
||||||
jobs:
|
|
||||||
style:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- uses: actions-rs/toolchain@v1
|
|
||||||
with:
|
|
||||||
profile: minimal
|
|
||||||
toolchain: stable
|
|
||||||
override: true
|
|
||||||
components: rustfmt
|
|
||||||
|
|
||||||
- name: Style Check
|
|
||||||
uses: actions-rs/cargo@v1
|
|
||||||
with:
|
|
||||||
command: fmt
|
|
||||||
args: --all -- --check
|
|
||||||
|
|
||||||
clippy:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
|
|
||||||
- uses: actions-rs/toolchain@v1
|
|
||||||
with:
|
|
||||||
profile: minimal
|
|
||||||
toolchain: stable
|
|
||||||
target: thumbv7em-none-eabihf
|
|
||||||
override: true
|
|
||||||
components: clippy
|
|
||||||
|
|
||||||
- name: Clippy Check
|
|
||||||
uses: actions-rs/cargo@v1
|
|
||||||
with:
|
|
||||||
command: clippy
|
|
||||||
args: --all-features
|
|
||||||
|
|
||||||
documentation:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
|
|
||||||
- uses: actions-rs/toolchain@v1
|
|
||||||
with:
|
|
||||||
profile: minimal
|
|
||||||
toolchain: stable
|
|
||||||
target: thumbv7em-none-eabihf
|
|
||||||
override: true
|
|
||||||
|
|
||||||
- name: Cargo Doc
|
|
||||||
uses: actions-rs/cargo@v1
|
|
||||||
with:
|
|
||||||
command: doc
|
|
||||||
args: --all-features
|
|
||||||
|
|
||||||
audit:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
- name: Cargo Audit
|
|
||||||
uses: actions-rs/audit-check@v1
|
|
||||||
with:
|
|
||||||
token: ${{ secrets.GITHUB_TOKEN }}
|
|
||||||
|
|
||||||
compile:
|
|
||||||
runs-on: ubuntu-latest
|
|
||||||
strategy:
|
|
||||||
matrix:
|
|
||||||
toolchain:
|
|
||||||
- stable
|
|
||||||
- beta
|
|
||||||
|
|
||||||
steps:
|
|
||||||
- uses: actions/checkout@v2
|
|
||||||
|
|
||||||
- name: Install Rust ${{ matrix.toolchain }}
|
|
||||||
uses: actions-rs/toolchain@v1
|
|
||||||
with:
|
|
||||||
toolchain: ${{ matrix.toolchain }}
|
|
||||||
target: thumbv7em-none-eabihf
|
|
||||||
override: true
|
|
||||||
|
|
||||||
- name: Cargo Check
|
|
||||||
uses: actions-rs/cargo@v1
|
|
||||||
with:
|
|
||||||
command: check
|
|
||||||
args: --verbose --all-features
|
|
||||||
|
|
||||||
- name: Cargo Build
|
|
||||||
uses: actions-rs/cargo@v1
|
|
||||||
with:
|
|
||||||
command: build
|
|
||||||
args: --all-features
|
|
||||||
|
|
||||||
- name: Cargo Build [Release]
|
|
||||||
uses: actions-rs/cargo@v1
|
|
||||||
with:
|
|
||||||
command: build
|
|
||||||
args: --release --all-features
|
|
||||||
|
|
||||||
- name: Cargo Build [Examples]
|
|
||||||
uses: actions-rs/cargo@v1
|
|
||||||
with:
|
|
||||||
command: build
|
|
||||||
args: --examples --all-features
|
|
39
Cargo.toml
39
Cargo.toml
|
@ -1,9 +1,9 @@
|
||||||
[package]
|
[package]
|
||||||
categories = ["embedded", "no-std"]
|
categories = ["embedded", "no-std"]
|
||||||
name = "enc424j600"
|
name = "enc424j600"
|
||||||
description = "Embbeded Rust Ethernet driver for ENC424J600 Ethernet controller with SPI interface"
|
description = "Embbeded Rust Ethernet driver for ENC424J600 Ethernet controller with SPI interface, compatible with STM32F4xx"
|
||||||
authors = ["Harry Ho <hh@m-labs.hk>", "Dip Cheung <dc@m-labs.hk>"]
|
authors = ["Harry Ho <hh@m-labs.hk>"]
|
||||||
version = "0.3.0"
|
version = "0.2.0"
|
||||||
keywords = ["ethernet", "eth", "enc424j600", "stm32", "stm32f4xx"]
|
keywords = ["ethernet", "eth", "enc424j600", "stm32", "stm32f4xx"]
|
||||||
repository = "https://git.m-labs.hk/M-Labs/ENC424J600"
|
repository = "https://git.m-labs.hk/M-Labs/ENC424J600"
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
@ -13,30 +13,37 @@ license = "BSD-2-Clause"
|
||||||
volatile-register = "0.2"
|
volatile-register = "0.2"
|
||||||
aligned = "0.3"
|
aligned = "0.3"
|
||||||
embedded-hal = "0.2"
|
embedded-hal = "0.2"
|
||||||
smoltcp = { version = "0.7.0", default-features = false, features = [ "socket-raw", "proto-ipv4",
|
smoltcp = { version = "0.7.0", default-features = false, features = ["proto-ipv4", "proto-ipv6", "socket-icmp", "socket-udp", "socket-tcp", "log", "verbose", "ethernet"], optional = true }
|
||||||
"proto-ipv6", "socket-tcp", "ethernet"], optional = true }
|
|
||||||
cortex-m = {version = "0.5", optional = true }
|
|
||||||
|
|
||||||
# Optional dependencies for building examples
|
# Optional dependencies for building examples
|
||||||
[dev-dependencies]
|
stm32f4xx-hal = { version = "0.8", optional = true }
|
||||||
stm32f4xx-hal = { version = "0.8", features = ["stm32f407", "rt"] }
|
cortex-m = { version = "0.5", optional = true }
|
||||||
cortex-m-rt = "0.6"
|
cortex-m-rt = { version = "0.6", optional = true }
|
||||||
cortex-m-rtic = "0.5.3"
|
cortex-m-rtic = { version = "0.5.3", optional = true }
|
||||||
panic-itm = "0.4"
|
panic-itm = { version = "0.4", optional = true }
|
||||||
log = "0.4"
|
log = { version = "0.4", optional = true }
|
||||||
|
embedded-time = { version = "0.10.1", optional = true }
|
||||||
|
embedded-nal = { version = "0.1.0", optional = true }
|
||||||
|
heapless = { version = "0.5.6", optional = true }
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
smoltcp-phy = ["smoltcp"]
|
smoltcp-phy = ["smoltcp"]
|
||||||
cortex-m-cpu = ["cortex-m"]
|
smoltcp-phy-all = [
|
||||||
|
"smoltcp/socket-raw", "smoltcp/socket-udp", "smoltcp/socket-tcp",
|
||||||
|
"smoltcp/proto-ipv4", "smoltcp/proto-ipv6"
|
||||||
|
]
|
||||||
|
# Example-based features
|
||||||
|
tx_stm32f407 = ["stm32f4xx-hal/stm32f407", "cortex-m", "cortex-m-rtic", "panic-itm", "log"]
|
||||||
|
tcp_stm32f407 = ["stm32f4xx-hal/stm32f407", "cortex-m", "cortex-m-rt", "cortex-m-rtic", "smoltcp-phy-all", "smoltcp/log", "panic-itm", "log"]
|
||||||
|
nal = [ "embedded-time", "embedded-nal", "smoltcp-phy", "heapless" ]
|
||||||
default = []
|
default = []
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "tx_stm32f407"
|
name = "tx_stm32f407"
|
||||||
required-features = ["smoltcp", "cortex-m-cpu"]
|
required-features = ["tx_stm32f407"]
|
||||||
|
|
||||||
[[example]]
|
[[example]]
|
||||||
name = "tcp_stm32f407"
|
name = "tcp_stm32f407"
|
||||||
required-features = ["smoltcp", "cortex-m-cpu"]
|
required-features = ["tcp_stm32f407"]
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
codegen-units = 1
|
codegen-units = 1
|
||||||
|
|
|
@ -2,31 +2,43 @@
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
extern crate panic_itm;
|
extern crate panic_itm;
|
||||||
use cortex_m::{iprint, iprintln};
|
use cortex_m::{iprintln, iprint};
|
||||||
|
|
||||||
use embedded_hal::{blocking::delay::DelayMs, digital::v2::OutputPin};
|
use embedded_hal::{
|
||||||
use enc424j600::smoltcp_phy;
|
digital::v2::OutputPin,
|
||||||
use stm32f4xx_hal::{
|
blocking::delay::DelayMs
|
||||||
delay::Delay, gpio::GpioExt, rcc::RccExt, spi::Spi, stm32::ITM, time::Hertz, time::U32Ext,
|
|
||||||
};
|
};
|
||||||
|
use stm32f4xx_hal::{
|
||||||
|
rcc::RccExt,
|
||||||
|
gpio::GpioExt,
|
||||||
|
time::U32Ext,
|
||||||
|
stm32::ITM,
|
||||||
|
delay::Delay,
|
||||||
|
spi::Spi,
|
||||||
|
time::Hertz
|
||||||
|
};
|
||||||
|
use enc424j600;
|
||||||
|
use enc424j600::{smoltcp_phy, EthController};
|
||||||
|
|
||||||
use core::fmt::Write;
|
use smoltcp::wire::{
|
||||||
use core::str;
|
EthernetAddress, IpAddress, IpCidr, Ipv6Cidr
|
||||||
use smoltcp::iface::{EthernetInterface, EthernetInterfaceBuilder, NeighborCache};
|
};
|
||||||
|
use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder, EthernetInterface};
|
||||||
use smoltcp::socket::{SocketSet, TcpSocket, TcpSocketBuffer};
|
use smoltcp::socket::{SocketSet, TcpSocket, TcpSocketBuffer};
|
||||||
use smoltcp::wire::{EthernetAddress, IpAddress, IpCidr, Ipv6Cidr};
|
use core::str;
|
||||||
|
use core::fmt::Write;
|
||||||
|
|
||||||
/// Timer
|
/// Timer
|
||||||
use core::cell::RefCell;
|
use core::cell::RefCell;
|
||||||
use cortex_m::interrupt::Mutex;
|
use cortex_m::interrupt::Mutex;
|
||||||
use cortex_m_rt::exception;
|
use cortex_m_rt::exception;
|
||||||
use smoltcp::time::Instant;
|
|
||||||
use stm32f4xx_hal::{
|
use stm32f4xx_hal::{
|
||||||
rcc::Clocks,
|
rcc::Clocks,
|
||||||
stm32::SYST,
|
|
||||||
time::MilliSeconds,
|
time::MilliSeconds,
|
||||||
timer::{Event as TimerEvent, Timer},
|
timer::{Timer, Event as TimerEvent},
|
||||||
|
stm32::SYST
|
||||||
};
|
};
|
||||||
|
use smoltcp::time::Instant;
|
||||||
/// Rate in Hz
|
/// Rate in Hz
|
||||||
const TIMER_RATE: u32 = 20;
|
const TIMER_RATE: u32 = 20;
|
||||||
/// Interval duration in milliseconds
|
/// Interval duration in milliseconds
|
||||||
|
@ -44,34 +56,32 @@ fn timer_setup(syst: SYST, clocks: Clocks) {
|
||||||
#[exception]
|
#[exception]
|
||||||
fn SysTick() {
|
fn SysTick() {
|
||||||
cortex_m::interrupt::free(|cs| {
|
cortex_m::interrupt::free(|cs| {
|
||||||
*TIMER_MS.borrow(cs).borrow_mut() += TIMER_DELTA;
|
*TIMER_MS.borrow(cs)
|
||||||
|
.borrow_mut() += TIMER_DELTA;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Obtain current time in milliseconds
|
/// Obtain current time in milliseconds
|
||||||
pub fn timer_now() -> MilliSeconds {
|
pub fn timer_now() -> MilliSeconds {
|
||||||
let ms = cortex_m::interrupt::free(|cs| *TIMER_MS.borrow(cs).borrow());
|
let ms = cortex_m::interrupt::free(|cs| {
|
||||||
|
*TIMER_MS.borrow(cs)
|
||||||
|
.borrow()
|
||||||
|
});
|
||||||
ms.ms()
|
ms.ms()
|
||||||
}
|
}
|
||||||
|
|
||||||
///
|
///
|
||||||
use stm32f4xx_hal::{
|
use stm32f4xx_hal::{
|
||||||
gpio::{
|
|
||||||
gpioa::{PA4, PA5, PA6, PA7},
|
|
||||||
Alternate, Output, PushPull, AF5,
|
|
||||||
},
|
|
||||||
stm32::SPI1,
|
stm32::SPI1,
|
||||||
|
gpio::{
|
||||||
|
gpioa::{PA5, PA6, PA7, PA4},
|
||||||
|
Alternate, AF5, Output, PushPull
|
||||||
|
}
|
||||||
};
|
};
|
||||||
type SpiEth = enc424j600::Enc424j600<
|
type BoosterSpiEth = enc424j600::SpiEth<
|
||||||
Spi<
|
Spi<SPI1, (PA5<Alternate<AF5>>, PA6<Alternate<AF5>>, PA7<Alternate<AF5>>)>,
|
||||||
SPI1,
|
|
||||||
(
|
|
||||||
PA5<Alternate<AF5>>,
|
|
||||||
PA6<Alternate<AF5>>,
|
|
||||||
PA7<Alternate<AF5>>,
|
|
||||||
),
|
|
||||||
>,
|
|
||||||
PA4<Output<PushPull>>,
|
PA4<Output<PushPull>>,
|
||||||
|
fn(u32) -> ()
|
||||||
>;
|
>;
|
||||||
|
|
||||||
pub struct NetStorage {
|
pub struct NetStorage {
|
||||||
|
@ -81,15 +91,19 @@ pub struct NetStorage {
|
||||||
|
|
||||||
static mut NET_STORE: NetStorage = NetStorage {
|
static mut NET_STORE: NetStorage = NetStorage {
|
||||||
// Placeholder for the real IP address, which is initialized at runtime.
|
// Placeholder for the real IP address, which is initialized at runtime.
|
||||||
ip_addrs: [IpCidr::Ipv6(Ipv6Cidr::SOLICITED_NODE_PREFIX)],
|
ip_addrs: [IpCidr::Ipv6(
|
||||||
|
Ipv6Cidr::SOLICITED_NODE_PREFIX,
|
||||||
|
)],
|
||||||
neighbor_cache: [None; 8],
|
neighbor_cache: [None; 8],
|
||||||
};
|
};
|
||||||
|
|
||||||
#[rtic::app(device = stm32f4xx_hal::stm32, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)]
|
#[rtic::app(device = stm32f4xx_hal::stm32, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)]
|
||||||
const APP: () = {
|
const APP: () = {
|
||||||
struct Resources {
|
struct Resources {
|
||||||
eth_iface: EthernetInterface<'static, smoltcp_phy::SmoltcpDevice<SpiEth>>,
|
eth_iface: EthernetInterface<
|
||||||
itm: ITM,
|
'static,
|
||||||
|
smoltcp_phy::SmoltcpDevice<BoosterSpiEth>>,
|
||||||
|
itm: ITM
|
||||||
}
|
}
|
||||||
|
|
||||||
#[init()]
|
#[init()]
|
||||||
|
@ -101,10 +115,7 @@ const APP: () = {
|
||||||
c.core.DWT.enable_cycle_counter();
|
c.core.DWT.enable_cycle_counter();
|
||||||
c.core.DCB.enable_trace();
|
c.core.DCB.enable_trace();
|
||||||
|
|
||||||
let clocks = c
|
let clocks = c.device.RCC.constrain()
|
||||||
.device
|
|
||||||
.RCC
|
|
||||||
.constrain()
|
|
||||||
.cfgr
|
.cfgr
|
||||||
.sysclk(168.mhz())
|
.sysclk(168.mhz())
|
||||||
.hclk(168.mhz())
|
.hclk(168.mhz())
|
||||||
|
@ -117,7 +128,8 @@ const APP: () = {
|
||||||
let mut itm = c.core.ITM;
|
let mut itm = c.core.ITM;
|
||||||
let stim0 = &mut itm.stim[0];
|
let stim0 = &mut itm.stim[0];
|
||||||
|
|
||||||
iprintln!(stim0, "Eth TCP Server on STM32-F407 via NIC100/ENC424J600");
|
iprintln!(stim0,
|
||||||
|
"Eth TCP Server on STM32-F407 via NIC100/ENC424J600");
|
||||||
|
|
||||||
// NIC100 / ENC424J600 Set-up
|
// NIC100 / ENC424J600 Set-up
|
||||||
let spi1 = c.device.SPI1;
|
let spi1 = c.device.SPI1;
|
||||||
|
@ -137,18 +149,19 @@ const APP: () = {
|
||||||
let eth_iface = {
|
let eth_iface = {
|
||||||
let mut spi_eth = {
|
let mut spi_eth = {
|
||||||
let spi_eth_port = Spi::spi1(
|
let spi_eth_port = Spi::spi1(
|
||||||
spi1,
|
spi1, (spi1_sck, spi1_miso, spi1_mosi),
|
||||||
(spi1_sck, spi1_miso, spi1_mosi),
|
|
||||||
enc424j600::spi::interfaces::SPI_MODE,
|
enc424j600::spi::interfaces::SPI_MODE,
|
||||||
Hertz(enc424j600::spi::interfaces::SPI_CLOCK_FREQ),
|
Hertz(enc424j600::spi::interfaces::SPI_CLOCK_FREQ),
|
||||||
clocks,
|
clocks);
|
||||||
);
|
|
||||||
|
|
||||||
SpiEth::new(spi_eth_port, spi1_nss).cpu_freq_mhz(168)
|
let delay_ns_fp: fn(u32) -> () = |time_ns| {
|
||||||
|
cortex_m::asm::delay((time_ns*21)/125 + 1)
|
||||||
|
};
|
||||||
|
enc424j600::SpiEth::new(spi_eth_port, spi1_nss, delay_ns_fp)
|
||||||
};
|
};
|
||||||
|
|
||||||
// Init controller
|
// Init controller
|
||||||
match spi_eth.reset(&mut delay) {
|
match spi_eth.init_dev() {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
iprintln!(stim0, "Initializing Ethernet...")
|
iprintln!(stim0, "Initializing Ethernet...")
|
||||||
}
|
}
|
||||||
|
@ -159,14 +172,14 @@ const APP: () = {
|
||||||
|
|
||||||
// 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_mac_addr(&mut eth_mac_addr);
|
spi_eth.read_from_mac(&mut eth_mac_addr);
|
||||||
for i in 0..6 {
|
for i in 0..6 {
|
||||||
let byte = eth_mac_addr[i];
|
let byte = eth_mac_addr[i];
|
||||||
match i {
|
match i {
|
||||||
0 => iprint!(stim0, "MAC Address = {:02x}-", byte),
|
0 => iprint!(stim0, "MAC Address = {:02x}-", byte),
|
||||||
1..=4 => iprint!(stim0, "{:02x}-", byte),
|
1..=4 => iprint!(stim0, "{:02x}-", byte),
|
||||||
5 => iprint!(stim0, "{:02x}\n", byte),
|
5 => iprint!(stim0, "{:02x}\n", byte),
|
||||||
_ => (),
|
_ => ()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -199,7 +212,10 @@ const APP: () = {
|
||||||
timer_setup(delay.free(), clocks);
|
timer_setup(delay.free(), clocks);
|
||||||
iprintln!(stim0, "Timer initialized");
|
iprintln!(stim0, "Timer initialized");
|
||||||
|
|
||||||
init::LateResources { eth_iface, itm }
|
init::LateResources {
|
||||||
|
eth_iface,
|
||||||
|
itm
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[idle(resources=[eth_iface, itm])]
|
#[idle(resources=[eth_iface, itm])]
|
||||||
|
@ -229,11 +245,8 @@ const APP: () = {
|
||||||
let greet_handle = socket_set.add(greet_socket);
|
let greet_handle = socket_set.add(greet_socket);
|
||||||
{
|
{
|
||||||
let store = unsafe { &mut NET_STORE };
|
let store = unsafe { &mut NET_STORE };
|
||||||
iprintln!(
|
iprintln!(stim0,
|
||||||
stim0,
|
"TCP sockets will listen at {}", store.ip_addrs[0].address());
|
||||||
"TCP sockets will listen at {}",
|
|
||||||
store.ip_addrs[0].address()
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Copied / modified from:
|
// Copied / modified from:
|
||||||
|
@ -245,7 +258,8 @@ const APP: () = {
|
||||||
let now = timer_now().0;
|
let now = timer_now().0;
|
||||||
let instant = Instant::from_millis(now as i64);
|
let instant = Instant::from_millis(now as i64);
|
||||||
match iface.poll(&mut socket_set, instant) {
|
match iface.poll(&mut socket_set, instant) {
|
||||||
Ok(_) => {}
|
Ok(_) => {
|
||||||
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
iprintln!(stim0, "[{}] Poll error: {:?}", instant, e)
|
iprintln!(stim0, "[{}] Poll error: {:?}", instant, e)
|
||||||
}
|
}
|
||||||
|
@ -254,40 +268,33 @@ const APP: () = {
|
||||||
{
|
{
|
||||||
let mut socket = socket_set.get::<TcpSocket>(echo_handle);
|
let mut socket = socket_set.get::<TcpSocket>(echo_handle);
|
||||||
if !socket.is_open() {
|
if !socket.is_open() {
|
||||||
iprintln!(
|
iprintln!(stim0,
|
||||||
stim0,
|
"[{}] Listening to port 1234 for echoing, time-out in 10s", instant);
|
||||||
"[{}] Listening to port 1234 for echoing, time-out in 10s",
|
|
||||||
instant
|
|
||||||
);
|
|
||||||
socket.listen(1234).unwrap();
|
socket.listen(1234).unwrap();
|
||||||
socket.set_timeout(Some(smoltcp::time::Duration::from_millis(10000)));
|
socket.set_timeout(Some(smoltcp::time::Duration::from_millis(10000)));
|
||||||
}
|
}
|
||||||
if socket.can_recv() {
|
if socket.can_recv() {
|
||||||
iprintln!(
|
iprintln!(stim0,
|
||||||
stim0,
|
"[{}] Received packet: {:?}", instant, socket.recv(|buffer| {
|
||||||
"[{}] Received packet: {:?}",
|
(buffer.len(), str::from_utf8(buffer).unwrap())
|
||||||
instant,
|
}));
|
||||||
socket.recv(|buffer| { (buffer.len(), str::from_utf8(buffer).unwrap()) })
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Control the "greeting" socket (:4321)
|
// Control the "greeting" socket (:4321)
|
||||||
{
|
{
|
||||||
let mut socket = socket_set.get::<TcpSocket>(greet_handle);
|
let mut socket = socket_set.get::<TcpSocket>(greet_handle);
|
||||||
if !socket.is_open() {
|
if !socket.is_open() {
|
||||||
iprintln!(
|
iprintln!(stim0,
|
||||||
stim0,
|
|
||||||
"[{}] Listening to port 4321 for greeting, \
|
"[{}] Listening to port 4321 for greeting, \
|
||||||
please connect to the port",
|
please connect to the port", instant);
|
||||||
instant
|
|
||||||
);
|
|
||||||
socket.listen(4321).unwrap();
|
socket.listen(4321).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
if socket.can_send() {
|
if socket.can_send() {
|
||||||
let greeting = "Welcome to the server demo for STM32-F407!";
|
let greeting = "Welcome to the server demo for STM32-F407!";
|
||||||
write!(socket, "{}\n", greeting).unwrap();
|
write!(socket, "{}\n", greeting).unwrap();
|
||||||
iprintln!(stim0, "[{}] Greeting sent, socket closed", instant);
|
iprintln!(stim0,
|
||||||
|
"[{}] Greeting sent, socket closed", instant);
|
||||||
socket.close();
|
socket.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,38 +2,41 @@
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
extern crate panic_itm;
|
extern crate panic_itm;
|
||||||
use cortex_m::{iprint, iprintln};
|
use cortex_m::{iprintln, iprint};
|
||||||
|
|
||||||
use embedded_hal::{blocking::delay::DelayMs, digital::v2::OutputPin};
|
use embedded_hal::{
|
||||||
use enc424j600::EthPhy;
|
digital::v2::OutputPin,
|
||||||
use stm32f4xx_hal::{
|
blocking::delay::DelayMs
|
||||||
delay::Delay, gpio::GpioExt, rcc::RccExt, spi::Spi, stm32::ITM, time::Hertz, time::U32Ext,
|
|
||||||
};
|
};
|
||||||
|
use stm32f4xx_hal::{
|
||||||
|
rcc::RccExt,
|
||||||
|
gpio::GpioExt,
|
||||||
|
time::U32Ext,
|
||||||
|
stm32::ITM,
|
||||||
|
delay::Delay,
|
||||||
|
spi::Spi,
|
||||||
|
time::Hertz
|
||||||
|
};
|
||||||
|
use enc424j600;
|
||||||
|
use enc424j600::EthController;
|
||||||
|
|
||||||
///
|
///
|
||||||
use stm32f4xx_hal::{
|
use stm32f4xx_hal::{
|
||||||
gpio::{
|
|
||||||
gpioa::{PA4, PA5, PA6, PA7},
|
|
||||||
Alternate, Output, PushPull, AF5,
|
|
||||||
},
|
|
||||||
stm32::SPI1,
|
stm32::SPI1,
|
||||||
|
gpio::{
|
||||||
|
gpioa::{PA5, PA6, PA7, PA4},
|
||||||
|
Alternate, AF5, Output, PushPull
|
||||||
|
},
|
||||||
};
|
};
|
||||||
type SpiEth = enc424j600::Enc424j600<
|
type BoosterSpiEth = enc424j600::SpiEth<
|
||||||
Spi<
|
Spi<SPI1, (PA5<Alternate<AF5>>, PA6<Alternate<AF5>>, PA7<Alternate<AF5>>)>,
|
||||||
SPI1,
|
|
||||||
(
|
|
||||||
PA5<Alternate<AF5>>,
|
|
||||||
PA6<Alternate<AF5>>,
|
|
||||||
PA7<Alternate<AF5>>,
|
|
||||||
),
|
|
||||||
>,
|
|
||||||
PA4<Output<PushPull>>,
|
PA4<Output<PushPull>>,
|
||||||
>;
|
fn(u32)>;
|
||||||
|
|
||||||
#[rtic::app(device = stm32f4xx_hal::stm32, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)]
|
#[rtic::app(device = stm32f4xx_hal::stm32, peripherals = true, monotonic = rtic::cyccnt::CYCCNT)]
|
||||||
const APP: () = {
|
const APP: () = {
|
||||||
struct Resources {
|
struct Resources {
|
||||||
spi_eth: SpiEth,
|
spi_eth: BoosterSpiEth,
|
||||||
delay: Delay,
|
delay: Delay,
|
||||||
itm: ITM,
|
itm: ITM,
|
||||||
}
|
}
|
||||||
|
@ -43,10 +46,7 @@ const APP: () = {
|
||||||
c.core.SCB.enable_icache();
|
c.core.SCB.enable_icache();
|
||||||
c.core.SCB.enable_dcache(&mut c.core.CPUID);
|
c.core.SCB.enable_dcache(&mut c.core.CPUID);
|
||||||
|
|
||||||
let clocks = c
|
let clocks = c.device.RCC.constrain()
|
||||||
.device
|
|
||||||
.RCC
|
|
||||||
.constrain()
|
|
||||||
.cfgr
|
.cfgr
|
||||||
.sysclk(168.mhz())
|
.sysclk(168.mhz())
|
||||||
.hclk(168.mhz())
|
.hclk(168.mhz())
|
||||||
|
@ -60,7 +60,8 @@ const APP: () = {
|
||||||
// Init ITM
|
// Init ITM
|
||||||
let mut itm = c.core.ITM;
|
let mut itm = c.core.ITM;
|
||||||
let stim0 = &mut itm.stim[0];
|
let stim0 = &mut itm.stim[0];
|
||||||
iprintln!(stim0, "Eth TX Pinging on STM32-F407 via NIC100/ENC424J600");
|
iprintln!(stim0,
|
||||||
|
"Eth TX Pinging on STM32-F407 via NIC100/ENC424J600");
|
||||||
|
|
||||||
// NIC100 / ENC424J600 Set-up
|
// NIC100 / ENC424J600 Set-up
|
||||||
let spi1 = c.device.SPI1;
|
let spi1 = c.device.SPI1;
|
||||||
|
@ -78,18 +79,19 @@ const APP: () = {
|
||||||
// Create SPI1 for HAL
|
// Create SPI1 for HAL
|
||||||
let mut spi_eth = {
|
let mut spi_eth = {
|
||||||
let spi_eth_port = Spi::spi1(
|
let spi_eth_port = Spi::spi1(
|
||||||
spi1,
|
spi1, (spi1_sck, spi1_miso, spi1_mosi),
|
||||||
(spi1_sck, spi1_miso, spi1_mosi),
|
|
||||||
enc424j600::spi::interfaces::SPI_MODE,
|
enc424j600::spi::interfaces::SPI_MODE,
|
||||||
Hertz(enc424j600::spi::interfaces::SPI_CLOCK_FREQ),
|
Hertz(enc424j600::spi::interfaces::SPI_CLOCK_FREQ),
|
||||||
clocks,
|
clocks);
|
||||||
);
|
|
||||||
|
|
||||||
SpiEth::new(spi_eth_port, spi1_nss).cpu_freq_mhz(168)
|
let delay_ns: fn(u32) -> () = |time_ns| {
|
||||||
|
cortex_m::asm::delay((time_ns*21)/125 + 1)
|
||||||
|
};
|
||||||
|
enc424j600::SpiEth::new(spi_eth_port, spi1_nss, delay_ns)
|
||||||
};
|
};
|
||||||
|
|
||||||
// Init
|
// Init
|
||||||
match spi_eth.reset(&mut delay) {
|
match spi_eth.init_dev() {
|
||||||
Ok(_) => {
|
Ok(_) => {
|
||||||
iprintln!(stim0, "Initializing Ethernet...")
|
iprintln!(stim0, "Initializing Ethernet...")
|
||||||
}
|
}
|
||||||
|
@ -100,14 +102,14 @@ const APP: () = {
|
||||||
|
|
||||||
// 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_mac_addr(&mut eth_mac_addr);
|
spi_eth.read_from_mac(&mut eth_mac_addr);
|
||||||
for i in 0..6 {
|
for i in 0..6 {
|
||||||
let byte = eth_mac_addr[i];
|
let byte = eth_mac_addr[i];
|
||||||
match i {
|
match i {
|
||||||
0 => iprint!(stim0, "MAC Address = {:02x}-", byte),
|
0 => iprint!(stim0, "MAC Address = {:02x}-", byte),
|
||||||
1..=4 => iprint!(stim0, "{:02x}-", byte),
|
1..=4 => iprint!(stim0, "{:02x}-", byte),
|
||||||
5 => iprint!(stim0, "{:02x}\n", byte),
|
5 => iprint!(stim0, "{:02x}\n", byte),
|
||||||
_ => (),
|
_ => ()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,20 +130,20 @@ const APP: () = {
|
||||||
let stim0 = &mut c.resources.itm.stim[0];
|
let stim0 = &mut c.resources.itm.stim[0];
|
||||||
// Testing Eth TX
|
// Testing Eth TX
|
||||||
let eth_tx_dat: [u8; 64] = [
|
let eth_tx_dat: [u8; 64] = [
|
||||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x08, 0x60, 0x6e, 0x44, 0x42, 0x95, 0x08, 0x06,
|
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x08, 0x60,
|
||||||
0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0x08, 0x60, 0x6e, 0x44, 0x42, 0x95,
|
0x6e, 0x44, 0x42, 0x95, 0x08, 0x06, 0x00, 0x01,
|
||||||
0xc0, 0xa8, 0x01, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x01, 0xe7,
|
0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0x08, 0x60,
|
||||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
0x6e, 0x44, 0x42, 0x95, 0xc0, 0xa8, 0x01, 0x64,
|
||||||
0x00, 0x00, 0x00, 0x00, 0x69, 0xd0, 0x85, 0x9f,
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8,
|
||||||
|
0x01, 0xe7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||||
|
0x00, 0x00, 0x00, 0x00, 0x69, 0xd0, 0x85, 0x9f
|
||||||
];
|
];
|
||||||
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(ð_tx_dat, 64);
|
eth_tx_packet.update_frame(ð_tx_dat, 64);
|
||||||
iprint!(
|
iprint!(stim0,
|
||||||
stim0,
|
"Sending packet (len={:}): ", eth_tx_packet.get_frame_length());
|
||||||
"Sending packet (len={:}): ",
|
|
||||||
eth_tx_packet.get_frame_length()
|
|
||||||
);
|
|
||||||
for i in 0..20 {
|
for i in 0..20 {
|
||||||
let byte = eth_tx_packet.get_frame_byte(i);
|
let byte = eth_tx_packet.get_frame_byte(i);
|
||||||
match i {
|
match i {
|
||||||
|
@ -152,10 +154,10 @@ const APP: () = {
|
||||||
13..=14 | 16..=18 => iprint!(stim0, "{:02x}", byte),
|
13..=14 | 16..=18 => iprint!(stim0, "{:02x}", byte),
|
||||||
5 | 11 | 15 => iprint!(stim0, "{:02x} ", byte),
|
5 | 11 | 15 => iprint!(stim0, "{:02x} ", byte),
|
||||||
19 => iprint!(stim0, "{:02x} ...\n", byte),
|
19 => iprint!(stim0, "{:02x} ...\n", byte),
|
||||||
_ => (),
|
_ => ()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
c.resources.spi_eth.send_packet(ð_tx_packet);
|
c.resources.spi_eth.send_raw_packet(ð_tx_packet);
|
||||||
iprintln!(stim0, "Packet sent");
|
iprintln!(stim0, "Packet sent");
|
||||||
c.resources.delay.delay_ms(100_u32);
|
c.resources.delay.delay_ms(100_u32);
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
{ stdenv, fetchFromGitHub, rustPlatform, pkg-config }:
|
||||||
|
|
||||||
|
rustPlatform.buildRustPackage rec {
|
||||||
|
version = "2019-11-15";
|
||||||
|
pname = "itm-tools";
|
||||||
|
|
||||||
|
src = fetchFromGitHub {
|
||||||
|
owner = "japaric";
|
||||||
|
repo = "itm-tools";
|
||||||
|
rev = "e94155e44019d893ac8e6dab51cc282d344ab700";
|
||||||
|
sha256 = "19xkjym0i7y52cfhvis49c59nzvgw4906cd8bkz8ka38mbgfqgiy";
|
||||||
|
};
|
||||||
|
|
||||||
|
cargoPatches = [ ./itm-tools-cargo-lock.patch ];
|
||||||
|
|
||||||
|
cargoSha256 = "0is702s14pgvd5i2m8aaw3zcsshqrwj97mjgg3wikbc627pagzg7";
|
||||||
|
|
||||||
|
nativeBuildInputs = [ pkg-config ];
|
||||||
|
|
||||||
|
doCheck = false;
|
||||||
|
}
|
|
@ -10,7 +10,7 @@ let
|
||||||
];
|
];
|
||||||
rustChannel =
|
rustChannel =
|
||||||
lib.rustLib.fromManifestFile rustManifest {
|
lib.rustLib.fromManifestFile rustManifest {
|
||||||
inherit stdenv lib fetchurl patchelf;
|
inherit stdenv fetchurl patchelf;
|
||||||
};
|
};
|
||||||
rust =
|
rust =
|
||||||
rustChannel.rust.override {
|
rustChannel.rust.override {
|
||||||
|
|
|
@ -8,6 +8,8 @@ with pkgs;
|
||||||
let
|
let
|
||||||
rustPlatform = callPackage ./nix/rustPlatform.nix {};
|
rustPlatform = callPackage ./nix/rustPlatform.nix {};
|
||||||
|
|
||||||
|
itm-tools = callPackage ./nix/itm-tools.nix { inherit rustPlatform; };
|
||||||
|
|
||||||
runHelp = writeShellScriptBin "run-help" ''
|
runHelp = writeShellScriptBin "run-help" ''
|
||||||
echo "[Common Tools]"
|
echo "[Common Tools]"
|
||||||
echo " run-openocd-f4x"
|
echo " run-openocd-f4x"
|
||||||
|
@ -101,7 +103,7 @@ in
|
||||||
stdenv.mkDerivation {
|
stdenv.mkDerivation {
|
||||||
name = "enc424j600-stm32-env";
|
name = "enc424j600-stm32-env";
|
||||||
buildInputs = with rustPlatform.rust; [
|
buildInputs = with rustPlatform.rust; [
|
||||||
rustc cargo pkgs.gdb pkgs.openocd pkgs.tmux pkgs.itm-tools
|
rustc cargo pkgs.gdb pkgs.openocd pkgs.tmux itm-tools
|
||||||
runHelp runTmuxEnv killTmuxEnv
|
runHelp runTmuxEnv killTmuxEnv
|
||||||
runOpenOcdF4x runItmDemuxFollow
|
runOpenOcdF4x runItmDemuxFollow
|
||||||
exTxStm32f407 exTcpStm32f407
|
exTxStm32f407 exTcpStm32f407
|
||||||
|
|
241
src/lib.rs
241
src/lib.rs
|
@ -2,7 +2,9 @@
|
||||||
|
|
||||||
pub mod spi;
|
pub mod spi;
|
||||||
use embedded_hal::{
|
use embedded_hal::{
|
||||||
blocking::{delay::DelayUs, spi::Transfer},
|
blocking::{
|
||||||
|
spi::Transfer,
|
||||||
|
},
|
||||||
digital::v2::OutputPin,
|
digital::v2::OutputPin,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -12,165 +14,125 @@ pub mod tx;
|
||||||
#[cfg(feature="smoltcp")]
|
#[cfg(feature="smoltcp")]
|
||||||
pub mod smoltcp_phy;
|
pub mod smoltcp_phy;
|
||||||
|
|
||||||
|
#[cfg(feature="nal")]
|
||||||
|
pub mod nal;
|
||||||
|
|
||||||
/// Max raw frame array size
|
/// Max raw frame array size
|
||||||
pub const RAW_FRAME_LENGTH_MAX: usize = 1518;
|
pub const RAW_FRAME_LENGTH_MAX: usize = 1518;
|
||||||
|
|
||||||
/// Trait representing PHY layer of ENC424J600
|
pub trait EthController {
|
||||||
pub trait EthPhy {
|
fn init_dev(&mut self) -> Result<(), EthControllerError>;
|
||||||
fn recv_packet(&mut self, is_poll: bool) -> Result<rx::RxPacket, Error>;
|
fn init_rxbuf(&mut self) -> Result<(), EthControllerError>;
|
||||||
fn send_packet(&mut self, packet: &tx::TxPacket) -> Result<(), Error>;
|
fn init_txbuf(&mut self) -> Result<(), EthControllerError>;
|
||||||
|
fn receive_next(&mut self, is_poll: bool) -> Result<rx::RxPacket, EthControllerError>;
|
||||||
|
fn send_raw_packet(&mut self, packet: &tx::TxPacket) -> Result<(), EthControllerError>;
|
||||||
|
fn set_promiscuous(&mut self) -> Result<(), EthControllerError>;
|
||||||
|
fn read_from_mac(&mut self, mac: &mut [u8]) -> Result<(), EthControllerError>;
|
||||||
|
fn write_mac_address(&mut self, mac: &[u8]) -> Result<(), EthControllerError>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// TODO: Improve these error types
|
/// TODO: Improve these error types
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum Error {
|
pub enum EthControllerError {
|
||||||
SpiPortError,
|
SpiPortError,
|
||||||
RegisterError,
|
GeneralError,
|
||||||
// TODO: Better name?
|
// TODO: Better name?
|
||||||
NoRxPacketError,
|
NoRxPacketError
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<spi::Error> for Error {
|
impl From<spi::SpiPortError> for EthControllerError {
|
||||||
fn from(_: spi::Error) -> Error {
|
fn from(_: spi::SpiPortError) -> EthControllerError {
|
||||||
Error::SpiPortError
|
EthControllerError::SpiPortError
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// ENC424J600 controller in SPI mode
|
/// Ethernet controller using SPI interface
|
||||||
pub struct Enc424j600<SPI: Transfer<u8>, NSS: OutputPin> {
|
pub struct SpiEth<SPI: Transfer<u8>,
|
||||||
spi_port: spi::SpiPort<SPI, NSS>,
|
NSS: OutputPin,
|
||||||
|
F: FnMut(u32) -> ()> {
|
||||||
|
spi_port: spi::SpiPort<SPI, NSS, F>,
|
||||||
rx_buf: rx::RxBuffer,
|
rx_buf: rx::RxBuffer,
|
||||||
tx_buf: tx::TxBuffer,
|
tx_buf: tx::TxBuffer
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<SPI: Transfer<u8>, NSS: OutputPin> Enc424j600<SPI, NSS> {
|
impl <SPI: Transfer<u8>,
|
||||||
pub fn new(spi: SPI, nss: NSS) -> Self {
|
NSS: OutputPin,
|
||||||
Enc424j600 {
|
F: FnMut(u32) -> ()> SpiEth<SPI, NSS, F> {
|
||||||
spi_port: spi::SpiPort::new(spi, nss),
|
pub fn new(spi: SPI, nss: NSS, delay_ns: F) -> Self {
|
||||||
|
SpiEth {
|
||||||
|
spi_port: spi::SpiPort::new(spi, nss, delay_ns),
|
||||||
rx_buf: rx::RxBuffer::new(),
|
rx_buf: rx::RxBuffer::new(),
|
||||||
tx_buf: tx::TxBuffer::new(),
|
tx_buf: tx::TxBuffer::new()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "cortex-m-cpu")]
|
impl <SPI: Transfer<u8>,
|
||||||
pub fn cpu_freq_mhz(mut self, freq: u32) -> Self {
|
NSS: OutputPin,
|
||||||
self.spi_port = self.spi_port.cpu_freq_mhz(freq);
|
F: FnMut(u32) -> ()> EthController for SpiEth<SPI, NSS, F> {
|
||||||
self
|
fn init_dev(&mut self) -> Result<(), EthControllerError> {
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init(&mut self, delay: &mut impl DelayUs<u16>) -> Result<(), Error> {
|
|
||||||
self.reset(delay)?;
|
|
||||||
self.init_rxbuf()?;
|
|
||||||
self.init_txbuf()?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn reset(&mut self, delay: &mut impl DelayUs<u16>) -> Result<(), Error> {
|
|
||||||
// Write 0x1234 to EUDAST
|
// Write 0x1234 to EUDAST
|
||||||
self.spi_port.write_reg_16b(spi::addrs::EUDAST, 0x1234)?;
|
self.spi_port.write_reg_16b(spi::addrs::EUDAST, 0x1234)?;
|
||||||
// Verify that EUDAST is 0x1234
|
// Verify that EUDAST is 0x1234
|
||||||
let mut eudast = self.spi_port.read_reg_16b(spi::addrs::EUDAST)?;
|
let mut eudast = self.spi_port.read_reg_16b(spi::addrs::EUDAST)?;
|
||||||
if eudast != 0x1234 {
|
if eudast != 0x1234 {
|
||||||
return Err(Error::RegisterError);
|
return Err(EthControllerError::GeneralError)
|
||||||
}
|
}
|
||||||
// Poll CLKRDY (ESTAT<12>) to check if it is set
|
// Poll CLKRDY (ESTAT<12>) to check if it is set
|
||||||
loop {
|
loop {
|
||||||
let estat = self.spi_port.read_reg_16b(spi::addrs::ESTAT)?;
|
let estat = self.spi_port.read_reg_16b(spi::addrs::ESTAT)?;
|
||||||
if estat & 0x1000 == 0x1000 {
|
if estat & 0x1000 == 0x1000 { break }
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
// Set ETHRST (ECON2<4>) to 1
|
||||||
// Issue system reset - set ETHRST (ECON2<4>) to 1
|
let econ2 = self.spi_port.read_reg_8b(spi::addrs::ECON2)?;
|
||||||
self.spi_port.send_opcode(spi::opcodes::SETETHRST)?;
|
self.spi_port.write_reg_8b(spi::addrs::ECON2, 0x10 | (econ2 & 0b11101111))?;
|
||||||
delay.delay_us(25);
|
self.spi_port.delay_us(25);
|
||||||
// Verify that EUDAST is 0x0000
|
// Verify that EUDAST is 0x0000
|
||||||
eudast = self.spi_port.read_reg_16b(spi::addrs::EUDAST)?;
|
eudast = self.spi_port.read_reg_16b(spi::addrs::EUDAST)?;
|
||||||
if eudast != 0x0000 {
|
if eudast != 0x0000 {
|
||||||
return Err(Error::RegisterError);
|
return Err(EthControllerError::GeneralError)
|
||||||
}
|
}
|
||||||
delay.delay_us(256);
|
self.spi_port.delay_us(256);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_rxbuf(&mut self) -> Result<(), Error> {
|
fn init_rxbuf(&mut self) -> Result<(), EthControllerError> {
|
||||||
// Set ERXST pointer
|
// Set ERXST pointer
|
||||||
self.spi_port
|
self.spi_port.write_reg_16b(spi::addrs::ERXST, self.rx_buf.get_wrap_addr())?;
|
||||||
.write_reg_16b(spi::addrs::ERXST, self.rx_buf.get_start_addr())?;
|
|
||||||
// Set ERXTAIL pointer
|
// Set ERXTAIL pointer
|
||||||
self.spi_port
|
self.spi_port.write_reg_16b(spi::addrs::ERXTAIL, self.rx_buf.get_tail_addr())?;
|
||||||
.write_reg_16b(spi::addrs::ERXTAIL, self.rx_buf.get_tail_addr())?;
|
|
||||||
// Set MAMXFL to maximum number of bytes in each accepted packet
|
// Set MAMXFL to maximum number of bytes in each accepted packet
|
||||||
self.spi_port
|
self.spi_port.write_reg_16b(spi::addrs::MAMXFL, RAW_FRAME_LENGTH_MAX as u16)?;
|
||||||
.write_reg_16b(spi::addrs::MAMXFL, RAW_FRAME_LENGTH_MAX as u16)?;
|
// Enable RXEN (ECON1<0>)
|
||||||
// Enable RX - set RXEN (ECON1<0>) to 1
|
let econ1 = self.spi_port.read_reg_16b(spi::addrs::ECON1)?;
|
||||||
self.spi_port.send_opcode(spi::opcodes::ENABLERX)?;
|
self.spi_port.write_reg_16b(spi::addrs::ECON1, 0x1 | (econ1 & 0xfffe))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_txbuf(&mut self) -> Result<(), Error> {
|
fn init_txbuf(&mut self) -> Result<(), EthControllerError> {
|
||||||
// Set EGPWRPT pointer
|
// Set EGPWRPT pointer
|
||||||
self.spi_port.write_reg_16b(spi::addrs::EGPWRPT, 0x0000)?;
|
self.spi_port.write_reg_16b(spi::addrs::EGPWRPT, 0x0000)?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set controller to Promiscuous Mode
|
|
||||||
pub fn set_promiscuous(&mut self) -> Result<(), Error> {
|
|
||||||
// From Section 10.12, ENC424J600 Data Sheet:
|
|
||||||
// "To accept all incoming frames regardless of content (Promiscuous mode),
|
|
||||||
// set the CRCEN, RUNTEN, UCEN, NOTMEEN and MCEN bits."
|
|
||||||
let erxfcon_lo = self.spi_port.read_reg_8b(spi::addrs::ERXFCON)?;
|
|
||||||
self.spi_port.write_reg_8b(
|
|
||||||
spi::addrs::ERXFCON,
|
|
||||||
0b0101_1110 | (erxfcon_lo & 0b1010_0001),
|
|
||||||
)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Read MAC to [u8; 6]
|
|
||||||
pub fn read_mac_addr(&mut self, mac: &mut [u8]) -> Result<(), Error> {
|
|
||||||
mac[0] = self.spi_port.read_reg_8b(spi::addrs::MAADR1)?;
|
|
||||||
mac[1] = self.spi_port.read_reg_8b(spi::addrs::MAADR1 + 1)?;
|
|
||||||
mac[2] = self.spi_port.read_reg_8b(spi::addrs::MAADR2)?;
|
|
||||||
mac[3] = self.spi_port.read_reg_8b(spi::addrs::MAADR2 + 1)?;
|
|
||||||
mac[4] = self.spi_port.read_reg_8b(spi::addrs::MAADR3)?;
|
|
||||||
mac[5] = self.spi_port.read_reg_8b(spi::addrs::MAADR3 + 1)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn write_mac_addr(&mut self, mac: &[u8]) -> Result<(), Error> {
|
|
||||||
self.spi_port.write_reg_8b(spi::addrs::MAADR1, mac[0])?;
|
|
||||||
self.spi_port.write_reg_8b(spi::addrs::MAADR1 + 1, mac[1])?;
|
|
||||||
self.spi_port.write_reg_8b(spi::addrs::MAADR2, mac[2])?;
|
|
||||||
self.spi_port.write_reg_8b(spi::addrs::MAADR2 + 1, mac[3])?;
|
|
||||||
self.spi_port.write_reg_8b(spi::addrs::MAADR3, mac[4])?;
|
|
||||||
self.spi_port.write_reg_8b(spi::addrs::MAADR3 + 1, mac[5])?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<SPI: Transfer<u8>, NSS: OutputPin> EthPhy for Enc424j600<SPI, NSS> {
|
|
||||||
/// Receive the next packet and return it
|
/// Receive the next packet and return it
|
||||||
/// Set is_poll to true for returning until PKTIF is set;
|
/// Set is_poll to true for returning until PKTIF is set;
|
||||||
/// Set is_poll to false for returning Err when PKTIF is not set
|
/// Set is_poll to false for returning Err when PKTIF is not set
|
||||||
fn recv_packet(&mut self, is_poll: bool) -> Result<rx::RxPacket, Error> {
|
fn receive_next(&mut self, is_poll: bool) -> Result<rx::RxPacket, EthControllerError> {
|
||||||
// Poll PKTIF (EIR<4>) to check if it is set
|
// Poll PKTIF (EIR<4>) to check if it is set
|
||||||
loop {
|
loop {
|
||||||
let eir = self.spi_port.read_reg_16b(spi::addrs::EIR)?;
|
let eir = self.spi_port.read_reg_16b(spi::addrs::EIR)?;
|
||||||
if eir & 0x40 == 0x40 {
|
if eir & 0x40 == 0x40 { break }
|
||||||
break;
|
|
||||||
}
|
|
||||||
if !is_poll {
|
if !is_poll {
|
||||||
return Err(Error::NoRxPacketError);
|
return Err(EthControllerError::NoRxPacketError)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// Set ERXRDPT pointer to next_addr
|
// Set ERXRDPT pointer to next_addr
|
||||||
self.spi_port
|
self.spi_port.write_reg_16b(spi::addrs::ERXRDPT, self.rx_buf.get_next_addr())?;
|
||||||
.write_reg_16b(spi::addrs::ERXRDPT, self.rx_buf.get_next_addr())?;
|
|
||||||
// Read 2 bytes to update next_addr
|
// Read 2 bytes to update next_addr
|
||||||
let mut next_addr_buf = [0; 3];
|
let mut next_addr_buf = [0; 3];
|
||||||
self.spi_port.read_rxdat(&mut next_addr_buf, 2)?;
|
self.spi_port.read_rxdat(&mut next_addr_buf, 2)?;
|
||||||
self.rx_buf
|
self.rx_buf.set_next_addr((next_addr_buf[1] as u16) | ((next_addr_buf[2] as u16) << 8));
|
||||||
.set_next_addr((next_addr_buf[1] as u16) | ((next_addr_buf[2] as u16) << 8));
|
|
||||||
// Read 6 bytes to update rsv
|
// Read 6 bytes to update rsv
|
||||||
let mut rsv_buf = [0; 7];
|
let mut rsv_buf = [0; 7];
|
||||||
self.spi_port.read_rxdat(&mut rsv_buf, 6)?;
|
self.spi_port.read_rxdat(&mut rsv_buf, 6)?;
|
||||||
|
@ -182,61 +144,78 @@ impl<SPI: Transfer<u8>, NSS: OutputPin> EthPhy for Enc424j600<SPI, NSS> {
|
||||||
rx_packet.update_frame_length();
|
rx_packet.update_frame_length();
|
||||||
// Read frame bytes
|
// Read frame bytes
|
||||||
let mut frame_buf = [0; RAW_FRAME_LENGTH_MAX];
|
let mut frame_buf = [0; RAW_FRAME_LENGTH_MAX];
|
||||||
self.spi_port
|
self.spi_port.read_rxdat(&mut frame_buf, rx_packet.get_frame_length())?;
|
||||||
.read_rxdat(&mut frame_buf, rx_packet.get_frame_length())?;
|
|
||||||
rx_packet.copy_frame_from(&frame_buf[1..]);
|
rx_packet.copy_frame_from(&frame_buf[1..]);
|
||||||
// Set ERXTAIL pointer to (next_addr - 2)
|
// Set ERXTAIL pointer to (next_addr - 2)
|
||||||
// * Assume head, tail, next and wrap addresses are word-aligned (even)
|
if self.rx_buf.get_next_addr() > rx::ERXST_DEFAULT {
|
||||||
// - If next_addr is at least (start_addr+2), then set tail pointer to the word right before next_addr
|
self.spi_port.write_reg_16b(spi::addrs::ERXTAIL, self.rx_buf.get_next_addr() - 2)?;
|
||||||
if self.rx_buf.get_next_addr() > self.rx_buf.get_start_addr() {
|
|
||||||
self.spi_port
|
|
||||||
.write_reg_16b(spi::addrs::ERXTAIL, self.rx_buf.get_next_addr() - 2)?;
|
|
||||||
// - Otherwise, next_addr will wrap, so set tail pointer to the last word address of RX buffer
|
|
||||||
} else {
|
} else {
|
||||||
self.spi_port
|
self.spi_port.write_reg_16b(spi::addrs::ERXTAIL, rx::RX_MAX_ADDRESS - 1)?;
|
||||||
.write_reg_16b(spi::addrs::ERXTAIL, rx::RX_MAX_ADDRESS - 1)?;
|
|
||||||
}
|
}
|
||||||
// Decrement PKTCNT - set PKTDEC (ECON1<8>)
|
// Set PKTDEC (ECON1<88>) to decrement PKTCNT
|
||||||
self.spi_port.send_opcode(spi::opcodes::SETPKTDEC)?;
|
let econ1_hi = self.spi_port.read_reg_8b(spi::addrs::ECON1 + 1)?;
|
||||||
|
self.spi_port.write_reg_8b(spi::addrs::ECON1 + 1, 0x01 | (econ1_hi & 0xfe))?;
|
||||||
// Return the RxPacket
|
// Return the RxPacket
|
||||||
Ok(rx_packet)
|
Ok(rx_packet)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Send an established packet
|
/// Send an established packet
|
||||||
fn send_packet(&mut self, packet: &tx::TxPacket) -> Result<(), Error> {
|
fn send_raw_packet(&mut self, packet: &tx::TxPacket) -> Result<(), EthControllerError> {
|
||||||
// Set EGPWRPT pointer to next_addr
|
// Set EGPWRPT pointer to next_addr
|
||||||
self.spi_port
|
self.spi_port.write_reg_16b(spi::addrs::EGPWRPT, self.tx_buf.get_next_addr())?;
|
||||||
.write_reg_16b(spi::addrs::EGPWRPT, self.tx_buf.get_next_addr())?;
|
|
||||||
// Copy packet data to SRAM Buffer
|
// Copy packet data to SRAM Buffer
|
||||||
// 1-byte Opcode is included
|
// 1-byte Opcode is included
|
||||||
let mut txdat_buf: [u8; RAW_FRAME_LENGTH_MAX + 1] = [0; RAW_FRAME_LENGTH_MAX + 1];
|
let mut txdat_buf: [u8; RAW_FRAME_LENGTH_MAX + 1] = [0; RAW_FRAME_LENGTH_MAX + 1];
|
||||||
packet.write_frame_to(&mut txdat_buf[1..]);
|
packet.write_frame_to(&mut txdat_buf[1..]);
|
||||||
self.spi_port
|
self.spi_port.write_txdat(&mut txdat_buf, packet.get_frame_length())?;
|
||||||
.write_txdat(&mut txdat_buf, packet.get_frame_length())?;
|
|
||||||
// Set ETXST to packet start address
|
// Set ETXST to packet start address
|
||||||
self.spi_port
|
self.spi_port.write_reg_16b(spi::addrs::ETXST, self.tx_buf.get_next_addr())?;
|
||||||
.write_reg_16b(spi::addrs::ETXST, self.tx_buf.get_next_addr())?;
|
|
||||||
// Set ETXLEN to packet length
|
// Set ETXLEN to packet length
|
||||||
self.spi_port
|
self.spi_port.write_reg_16b(spi::addrs::ETXLEN, packet.get_frame_length() as u16)?;
|
||||||
.write_reg_16b(spi::addrs::ETXLEN, packet.get_frame_length() as u16)?;
|
// Set TXRTS (ECON1<1>) to start transmission
|
||||||
// Send packet - set TXRTS (ECON1<1>) to start transmission
|
let mut econ1_lo = self.spi_port.read_reg_8b(spi::addrs::ECON1)?;
|
||||||
self.spi_port.send_opcode(spi::opcodes::SETTXRTS)?;
|
self.spi_port.write_reg_8b(spi::addrs::ECON1, 0x02 | (econ1_lo & 0xfd))?;
|
||||||
// Poll TXRTS (ECON1<1>) to check if it is reset
|
// Poll TXRTS (ECON1<1>) to check if it is reset
|
||||||
loop {
|
loop {
|
||||||
let econ1_lo = self.spi_port.read_reg_8b(spi::addrs::ECON1)?;
|
econ1_lo = self.spi_port.read_reg_8b(spi::addrs::ECON1)?;
|
||||||
if econ1_lo & 0x02 == 0 {
|
if econ1_lo & 0x02 == 0 { break }
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// TODO: Read ETXSTAT to understand Ethernet transmission status
|
// TODO: Read ETXSTAT to understand Ethernet transmission status
|
||||||
// (See: Register 9-2, ENC424J600 Data Sheet)
|
// (See: Register 9-2, ENC424J600 Data Sheet)
|
||||||
// Update TX buffer start address
|
// Update TX buffer start address
|
||||||
// * Assume TX buffer consumes the entire general-purpose SRAM block
|
self.tx_buf.set_next_addr((self.tx_buf.get_next_addr() + packet.get_frame_length() as u16) %
|
||||||
self.tx_buf.set_next_addr(
|
tx::GPBUFEN_DEFAULT);
|
||||||
(self.tx_buf.get_next_addr() + packet.get_frame_length() as u16)
|
Ok(())
|
||||||
% self.rx_buf.get_start_addr()
|
}
|
||||||
- self.tx_buf.get_start_addr(),
|
|
||||||
);
|
/// Set controller to Promiscuous Mode
|
||||||
|
fn set_promiscuous(&mut self) -> Result<(), EthControllerError> {
|
||||||
|
// From Section 10.12, ENC424J600 Data Sheet:
|
||||||
|
// "To accept all incoming frames regardless of content (Promiscuous mode),
|
||||||
|
// set the CRCEN, RUNTEN, UCEN, NOTMEEN and MCEN bits."
|
||||||
|
let erxfcon_lo = self.spi_port.read_reg_8b(spi::addrs::ERXFCON)?;
|
||||||
|
self.spi_port.write_reg_8b(spi::addrs::ERXFCON, 0b0101_1110 | (erxfcon_lo & 0b1010_0001))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read MAC to [u8; 6]
|
||||||
|
fn read_from_mac(&mut self, mac: &mut [u8]) -> Result<(), EthControllerError> {
|
||||||
|
mac[0] = self.spi_port.read_reg_8b(spi::addrs::MAADR1)?;
|
||||||
|
mac[1] = self.spi_port.read_reg_8b(spi::addrs::MAADR1 + 1)?;
|
||||||
|
mac[2] = self.spi_port.read_reg_8b(spi::addrs::MAADR2)?;
|
||||||
|
mac[3] = self.spi_port.read_reg_8b(spi::addrs::MAADR2 + 1)?;
|
||||||
|
mac[4] = self.spi_port.read_reg_8b(spi::addrs::MAADR3)?;
|
||||||
|
mac[5] = self.spi_port.read_reg_8b(spi::addrs::MAADR3 + 1)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_mac_address(&mut self, mac: &[u8]) -> Result<(), EthControllerError> {
|
||||||
|
self.spi_port.write_reg_8b(spi::addrs::MAADR1, mac[0])?;
|
||||||
|
self.spi_port.write_reg_8b(spi::addrs::MAADR1 + 1, mac[1])?;
|
||||||
|
self.spi_port.write_reg_8b(spi::addrs::MAADR2, mac[2])?;
|
||||||
|
self.spi_port.write_reg_8b(spi::addrs::MAADR2 + 1, mac[3])?;
|
||||||
|
self.spi_port.write_reg_8b(spi::addrs::MAADR3, mac[4])?;
|
||||||
|
self.spi_port.write_reg_8b(spi::addrs::MAADR3 + 1, mac[5])?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,245 @@
|
||||||
|
use core::cell::RefCell;
|
||||||
|
use core::convert::TryFrom;
|
||||||
|
use heapless::{consts, Vec};
|
||||||
|
use embedded_nal as nal;
|
||||||
|
use nal::nb;
|
||||||
|
use smoltcp as net;
|
||||||
|
use embedded_hal::{
|
||||||
|
blocking::spi::Transfer,
|
||||||
|
digital::v2::OutputPin
|
||||||
|
};
|
||||||
|
pub use embedded_time as time;
|
||||||
|
use time::duration::*;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum NetworkError {
|
||||||
|
NoSocket,
|
||||||
|
ConnectionFailure,
|
||||||
|
ReadFailure,
|
||||||
|
WriteFailure,
|
||||||
|
Unsupported,
|
||||||
|
TimeFault,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type NetworkInterface<SPI, NSS> = net::iface::EthernetInterface<
|
||||||
|
'static,
|
||||||
|
crate::smoltcp_phy::SmoltcpDevice<
|
||||||
|
crate::SpiEth<SPI, NSS, fn(u32)>
|
||||||
|
>,
|
||||||
|
>;
|
||||||
|
|
||||||
|
pub struct NetworkStack<'a, SPI, NSS, IntClock>
|
||||||
|
where
|
||||||
|
SPI: 'static + Transfer<u8>,
|
||||||
|
NSS: 'static + OutputPin,
|
||||||
|
IntClock: time::Clock<T = u32>,
|
||||||
|
{
|
||||||
|
network_interface: RefCell<NetworkInterface<SPI, NSS>>,
|
||||||
|
sockets: RefCell<net::socket::SocketSet<'a>>,
|
||||||
|
next_port: RefCell<u16>,
|
||||||
|
unused_handles: RefCell<Vec<net::socket::SocketHandle, consts::U16>>,
|
||||||
|
time_ms: RefCell<u32>,
|
||||||
|
last_update_instant: RefCell<Option<time::Instant<IntClock>>>,
|
||||||
|
clock: IntClock
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, SPI, NSS, IntClock> NetworkStack<'a, SPI, NSS, IntClock>
|
||||||
|
where
|
||||||
|
SPI: Transfer<u8>,
|
||||||
|
NSS: OutputPin,
|
||||||
|
IntClock: time::Clock<T = u32>,
|
||||||
|
{
|
||||||
|
pub fn new(interface: NetworkInterface<SPI, NSS>, sockets: net::socket::SocketSet<'a>, clock: IntClock) -> Self {
|
||||||
|
let mut unused_handles: Vec<net::socket::SocketHandle, consts::U16> = Vec::new();
|
||||||
|
for socket in sockets.iter() {
|
||||||
|
unused_handles.push(socket.handle()).unwrap();
|
||||||
|
}
|
||||||
|
NetworkStack {
|
||||||
|
network_interface: RefCell::new(interface),
|
||||||
|
sockets: RefCell::new(sockets),
|
||||||
|
next_port: RefCell::new(49152),
|
||||||
|
unused_handles: RefCell::new(unused_handles),
|
||||||
|
time_ms: RefCell::new(0),
|
||||||
|
last_update_instant: RefCell::new(None),
|
||||||
|
clock,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Include auto_time_update to allow Instant::now() to not be called
|
||||||
|
// Instant::now() is not safe to call in `init()` context
|
||||||
|
pub fn update(&self, auto_time_update: bool) -> Result<bool, NetworkError> {
|
||||||
|
if auto_time_update {
|
||||||
|
// Check if it is the first time the stack has updated the time itself
|
||||||
|
let now = match *self.last_update_instant.borrow() {
|
||||||
|
// If it is the first time, do not advance time
|
||||||
|
// Simply store the current instant to initiate time updating
|
||||||
|
None => self.clock.try_now().map_err(|_| NetworkError::TimeFault)?,
|
||||||
|
// If it was updated before, advance time and update last_update_instant
|
||||||
|
Some(instant) => {
|
||||||
|
// Calculate elapsed time
|
||||||
|
let now = self.clock.try_now().map_err(|_| NetworkError::TimeFault)?;
|
||||||
|
let duration = now.checked_duration_since(&instant).ok_or(NetworkError::TimeFault)?;
|
||||||
|
let duration_ms = time::duration::Milliseconds::<u32>::try_from(duration).map_err(|_| NetworkError::TimeFault)?;
|
||||||
|
// Adjust duration into ms (note: decimal point truncated)
|
||||||
|
self.advance_time(*duration_ms.integer());
|
||||||
|
now
|
||||||
|
}
|
||||||
|
};
|
||||||
|
self.last_update_instant.replace(Some(now));
|
||||||
|
}
|
||||||
|
match self.network_interface.borrow_mut().poll(
|
||||||
|
&mut self.sockets.borrow_mut(),
|
||||||
|
net::time::Instant::from_millis(*self.time_ms.borrow() as i64),
|
||||||
|
) {
|
||||||
|
Ok(changed) => Ok(!changed),
|
||||||
|
Err(_e) => {
|
||||||
|
Ok(true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn advance_time(&self, duration: u32) {
|
||||||
|
let time = self.time_ms.try_borrow().unwrap().wrapping_add(duration);
|
||||||
|
self.time_ms.replace(time);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_ephemeral_port(&self) -> u16 {
|
||||||
|
// Get the next ephemeral port
|
||||||
|
let current_port = self.next_port.borrow().clone();
|
||||||
|
let (next, wrap) = self.next_port.borrow().overflowing_add(1);
|
||||||
|
*self.next_port.borrow_mut() = if wrap { 49152 } else { next };
|
||||||
|
return current_port;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'a, SPI, NSS, IntClock> nal::TcpStack for NetworkStack<'a, SPI, NSS, IntClock>
|
||||||
|
where
|
||||||
|
SPI: Transfer<u8>,
|
||||||
|
NSS: OutputPin,
|
||||||
|
IntClock: time::Clock<T = u32>,
|
||||||
|
{
|
||||||
|
type TcpSocket = net::socket::SocketHandle;
|
||||||
|
type Error = NetworkError;
|
||||||
|
fn open(&self, _mode: nal::Mode) -> Result<Self::TcpSocket, Self::Error> {
|
||||||
|
match self.unused_handles.borrow_mut().pop() {
|
||||||
|
Some(handle) => {
|
||||||
|
// Abort any active connections on the handle.
|
||||||
|
let mut sockets = self.sockets.borrow_mut();
|
||||||
|
let internal_socket: &mut net::socket::TcpSocket = &mut *sockets.get(handle);
|
||||||
|
internal_socket.abort();
|
||||||
|
Ok(handle)
|
||||||
|
}
|
||||||
|
None => Err(NetworkError::NoSocket),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ideally connect is only to be performed in `init()` of `main.rs`
|
||||||
|
// Calling `Instant::now()` of `rtic::cyccnt` would face correctness issue during `init()`
|
||||||
|
fn connect(
|
||||||
|
&self,
|
||||||
|
socket: Self::TcpSocket,
|
||||||
|
remote: nal::SocketAddr,
|
||||||
|
) -> Result<Self::TcpSocket, Self::Error> {
|
||||||
|
let address = {
|
||||||
|
let mut sockets = self.sockets.borrow_mut();
|
||||||
|
let internal_socket: &mut net::socket::TcpSocket = &mut *sockets.get(socket);
|
||||||
|
// If we're already in the process of connecting, ignore the request silently.
|
||||||
|
if internal_socket.is_open() {
|
||||||
|
return Ok(socket);
|
||||||
|
}
|
||||||
|
match remote.ip() {
|
||||||
|
nal::IpAddr::V4(addr) => {
|
||||||
|
let address =
|
||||||
|
net::wire::Ipv4Address::from_bytes(&addr.octets()[..]);
|
||||||
|
internal_socket
|
||||||
|
.connect((address, remote.port()), self.get_ephemeral_port())
|
||||||
|
.map_err(|_| NetworkError::ConnectionFailure)?;
|
||||||
|
net::wire::IpAddress::Ipv4(address)
|
||||||
|
}
|
||||||
|
nal::IpAddr::V6(addr) => {
|
||||||
|
let address = net::wire::Ipv6Address::from_parts(&addr.segments()[..]);
|
||||||
|
internal_socket.connect((address, remote.port()), self.get_ephemeral_port())
|
||||||
|
.map_err(|_| NetworkError::ConnectionFailure)?;
|
||||||
|
net::wire::IpAddress::Ipv6(address)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// Blocking connect
|
||||||
|
loop {
|
||||||
|
match self.is_connected(&socket) {
|
||||||
|
Ok(true) => break,
|
||||||
|
_ => {
|
||||||
|
let mut sockets = self.sockets.borrow_mut();
|
||||||
|
let internal_socket: &mut net::socket::TcpSocket = &mut *sockets.get(socket);
|
||||||
|
// If the connect got ACK->RST, it will end up in Closed TCP state
|
||||||
|
// Perform reconnection in this case
|
||||||
|
if internal_socket.state() == net::socket::TcpState::Closed {
|
||||||
|
internal_socket.close();
|
||||||
|
internal_socket
|
||||||
|
.connect((address, remote.port()), self.get_ephemeral_port())
|
||||||
|
.map_err(|_| NetworkError::ConnectionFailure)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Avoid using Instant::now() and Advance time manually
|
||||||
|
self.update(false)?;
|
||||||
|
{
|
||||||
|
self.advance_time(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(socket)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_connected(&self, socket: &Self::TcpSocket) -> Result<bool, Self::Error> {
|
||||||
|
let mut sockets = self.sockets.borrow_mut();
|
||||||
|
let socket: &mut net::socket::TcpSocket = &mut *sockets.get(*socket);
|
||||||
|
Ok(socket.may_send() && socket.may_recv())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write(&self, socket: &mut Self::TcpSocket, buffer: &[u8]) -> nb::Result<usize, Self::Error> {
|
||||||
|
let mut non_queued_bytes = &buffer[..];
|
||||||
|
while non_queued_bytes.len() != 0 {
|
||||||
|
let result = {
|
||||||
|
let mut sockets = self.sockets.borrow_mut();
|
||||||
|
let socket: &mut net::socket::TcpSocket = &mut *sockets.get(*socket);
|
||||||
|
let result = socket.send_slice(non_queued_bytes);
|
||||||
|
result
|
||||||
|
};
|
||||||
|
match result {
|
||||||
|
Ok(num_bytes) => {
|
||||||
|
// In case the buffer is filled up, push bytes into ethernet driver
|
||||||
|
if num_bytes != non_queued_bytes.len() {
|
||||||
|
self.update(true)?;
|
||||||
|
}
|
||||||
|
// Process the unwritten bytes again, if any
|
||||||
|
non_queued_bytes = &non_queued_bytes[num_bytes..]
|
||||||
|
}
|
||||||
|
Err(_) => return Err(nb::Error::Other(NetworkError::WriteFailure)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(buffer.len())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn read(
|
||||||
|
&self,
|
||||||
|
socket: &mut Self::TcpSocket,
|
||||||
|
buffer: &mut [u8],
|
||||||
|
) -> nb::Result<usize, Self::Error> {
|
||||||
|
// Enqueue received bytes into the TCP socket buffer
|
||||||
|
self.update(true)?;
|
||||||
|
let mut sockets = self.sockets.borrow_mut();
|
||||||
|
let socket: &mut net::socket::TcpSocket = &mut *sockets.get(*socket);
|
||||||
|
let result = socket.recv_slice(buffer);
|
||||||
|
match result {
|
||||||
|
Ok(num_bytes) => Ok(num_bytes),
|
||||||
|
Err(_) => Err(nb::Error::Other(NetworkError::ReadFailure)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn close(&self, socket: Self::TcpSocket) -> Result<(), Self::Error> {
|
||||||
|
let mut sockets = self.sockets.borrow_mut();
|
||||||
|
let internal_socket: &mut net::socket::TcpSocket = &mut *sockets.get(socket);
|
||||||
|
internal_socket.close();
|
||||||
|
self.unused_handles.borrow_mut().push(socket).unwrap();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
24
src/rx.rs
24
src/rx.rs
|
@ -11,25 +11,25 @@ pub const RSV_LENGTH: usize = 6;
|
||||||
/// Struct for RX Buffer on the hardware
|
/// Struct for RX Buffer on the hardware
|
||||||
/// TODO: Should be a singleton
|
/// TODO: Should be a singleton
|
||||||
pub struct RxBuffer {
|
pub struct RxBuffer {
|
||||||
start_addr: u16,
|
wrap_addr: u16,
|
||||||
next_addr: u16,
|
next_addr: u16,
|
||||||
tail_addr: u16,
|
tail_addr: u16
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RxBuffer {
|
impl RxBuffer {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
RxBuffer {
|
RxBuffer {
|
||||||
start_addr: ERXST_DEFAULT,
|
wrap_addr: ERXST_DEFAULT,
|
||||||
next_addr: ERXST_DEFAULT,
|
next_addr: ERXST_DEFAULT,
|
||||||
tail_addr: ERXTAIL_DEFAULT,
|
tail_addr: ERXTAIL_DEFAULT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_start_addr(&mut self, addr: u16) {
|
pub fn set_wrap_addr(&mut self, addr: u16) {
|
||||||
self.start_addr = addr;
|
self.wrap_addr = addr;
|
||||||
}
|
}
|
||||||
pub fn get_start_addr(&self) -> u16 {
|
pub fn get_wrap_addr(& self) -> u16{
|
||||||
self.start_addr
|
self.wrap_addr
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_next_addr(&mut self, addr: u16) {
|
pub fn set_next_addr(&mut self, addr: u16) {
|
||||||
|
@ -52,7 +52,7 @@ impl RxBuffer {
|
||||||
pub struct RxPacket {
|
pub struct RxPacket {
|
||||||
rsv: Rsv,
|
rsv: Rsv,
|
||||||
frame: [u8; RAW_FRAME_LENGTH_MAX],
|
frame: [u8; RAW_FRAME_LENGTH_MAX],
|
||||||
frame_length: usize,
|
frame_length: usize
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RxPacket {
|
impl RxPacket {
|
||||||
|
@ -60,7 +60,7 @@ impl RxPacket {
|
||||||
RxPacket {
|
RxPacket {
|
||||||
rsv: Rsv::new(),
|
rsv: Rsv::new(),
|
||||||
frame: [0; RAW_FRAME_LENGTH_MAX],
|
frame: [0; RAW_FRAME_LENGTH_MAX],
|
||||||
frame_length: 0,
|
frame_length: 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,14 +106,14 @@ impl RxPacket {
|
||||||
struct Rsv {
|
struct Rsv {
|
||||||
raw_rsv: [u8; RSV_LENGTH],
|
raw_rsv: [u8; RSV_LENGTH],
|
||||||
// TODO: Add more definitions
|
// TODO: Add more definitions
|
||||||
frame_length: u16,
|
frame_length: u16
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rsv {
|
impl Rsv {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
Rsv {
|
Rsv {
|
||||||
raw_rsv: [0; RSV_LENGTH],
|
raw_rsv: [0; RSV_LENGTH],
|
||||||
frame_length: 0_u16,
|
frame_length: 0_u16
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,30 +1,32 @@
|
||||||
use crate::{tx, EthPhy, RAW_FRAME_LENGTH_MAX};
|
use crate::{
|
||||||
|
EthController, tx, RAW_FRAME_LENGTH_MAX
|
||||||
|
};
|
||||||
use core::cell;
|
use core::cell;
|
||||||
use smoltcp::{
|
use smoltcp::{
|
||||||
phy::{Device, DeviceCapabilities, RxToken, TxToken},
|
phy::{Device, DeviceCapabilities, RxToken, TxToken},
|
||||||
time::Instant,
|
time::Instant,
|
||||||
Error,
|
Error
|
||||||
};
|
};
|
||||||
|
|
||||||
pub struct SmoltcpDevice<E: EthPhy> {
|
pub struct SmoltcpDevice<EC: EthController> {
|
||||||
pub eth_phy: cell::RefCell<E>,
|
pub eth_controller: cell::RefCell<EC>,
|
||||||
rx_packet_buf: [u8; RAW_FRAME_LENGTH_MAX],
|
rx_packet_buf: [u8; RAW_FRAME_LENGTH_MAX],
|
||||||
tx_packet_buf: [u8; RAW_FRAME_LENGTH_MAX],
|
tx_packet_buf: [u8; RAW_FRAME_LENGTH_MAX]
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<E: EthPhy> SmoltcpDevice<E> {
|
impl<EC: EthController> SmoltcpDevice<EC> {
|
||||||
pub fn new(eth_phy: E) -> Self {
|
pub fn new(eth_controller: EC) -> Self {
|
||||||
SmoltcpDevice {
|
SmoltcpDevice {
|
||||||
eth_phy: cell::RefCell::new(eth_phy),
|
eth_controller: cell::RefCell::new(eth_controller),
|
||||||
rx_packet_buf: [0; RAW_FRAME_LENGTH_MAX],
|
rx_packet_buf: [0; RAW_FRAME_LENGTH_MAX],
|
||||||
tx_packet_buf: [0; RAW_FRAME_LENGTH_MAX],
|
tx_packet_buf: [0; RAW_FRAME_LENGTH_MAX]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, E: 'a + EthPhy> Device<'a> for SmoltcpDevice<E> {
|
impl<'a, EC: 'a + EthController> Device<'a> for SmoltcpDevice<EC> {
|
||||||
type RxToken = EthRxToken<'a>;
|
type RxToken = EthRxToken<'a>;
|
||||||
type TxToken = EthTxToken<'a, E>;
|
type TxToken = EthTxToken<'a, EC>;
|
||||||
|
|
||||||
fn capabilities(&self) -> DeviceCapabilities {
|
fn capabilities(&self) -> DeviceCapabilities {
|
||||||
let mut caps = DeviceCapabilities::default();
|
let mut caps = DeviceCapabilities::default();
|
||||||
|
@ -33,33 +35,33 @@ impl<'a, E: 'a + EthPhy> Device<'a> for SmoltcpDevice<E> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
|
fn receive(&'a mut self) -> Option<(Self::RxToken, Self::TxToken)> {
|
||||||
let self_p = (&mut *self) as *mut SmoltcpDevice<E>;
|
let self_p = (&mut *self) as *mut SmoltcpDevice<EC>;
|
||||||
match self.eth_phy.borrow_mut().recv_packet(false) {
|
match self.eth_controller.borrow_mut().receive_next(false) {
|
||||||
Ok(rx_packet) => {
|
Ok(rx_packet) => {
|
||||||
// Write received packet to RX packet buffer
|
// Write received packet to RX packet buffer
|
||||||
rx_packet.write_frame_to(&mut self.rx_packet_buf);
|
rx_packet.write_frame_to(&mut self.rx_packet_buf);
|
||||||
// Construct a RxToken
|
// Construct a RxToken
|
||||||
let rx_token = EthRxToken {
|
let rx_token = EthRxToken {
|
||||||
buf: &mut self.rx_packet_buf,
|
buf: &mut self.rx_packet_buf,
|
||||||
len: rx_packet.get_frame_length(),
|
len: rx_packet.get_frame_length()
|
||||||
};
|
};
|
||||||
// Construct a blank TxToken
|
// Construct a blank TxToken
|
||||||
let tx_token = EthTxToken {
|
let tx_token = EthTxToken {
|
||||||
buf: &mut self.tx_packet_buf,
|
buf: &mut self.tx_packet_buf,
|
||||||
dev: self_p,
|
dev: self_p
|
||||||
};
|
};
|
||||||
Some((rx_token, tx_token))
|
Some((rx_token, tx_token))
|
||||||
}
|
},
|
||||||
Err(_) => None,
|
Err(_) => None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn transmit(&'a mut self) -> Option<Self::TxToken> {
|
fn transmit(&'a mut self) -> Option<Self::TxToken> {
|
||||||
let self_p = (&mut *self) as *mut SmoltcpDevice<E>;
|
let self_p = (&mut *self) as *mut SmoltcpDevice<EC>;
|
||||||
// Construct a blank TxToken
|
// Construct a blank TxToken
|
||||||
let tx_token = EthTxToken {
|
let tx_token = EthTxToken {
|
||||||
buf: &mut self.tx_packet_buf,
|
buf: &mut self.tx_packet_buf,
|
||||||
dev: self_p,
|
dev: self_p
|
||||||
};
|
};
|
||||||
Some(tx_token)
|
Some(tx_token)
|
||||||
}
|
}
|
||||||
|
@ -67,7 +69,7 @@ impl<'a, E: 'a + EthPhy> Device<'a> for SmoltcpDevice<E> {
|
||||||
|
|
||||||
pub struct EthRxToken<'a> {
|
pub struct EthRxToken<'a> {
|
||||||
buf: &'a mut [u8],
|
buf: &'a mut [u8],
|
||||||
len: usize,
|
len: usize
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> RxToken for EthRxToken<'a> {
|
impl<'a> RxToken for EthRxToken<'a> {
|
||||||
|
@ -79,12 +81,12 @@ impl<'a> RxToken for EthRxToken<'a> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct EthTxToken<'a, E: EthPhy> {
|
pub struct EthTxToken<'a, EC: EthController> {
|
||||||
buf: &'a mut [u8],
|
buf: &'a mut [u8],
|
||||||
dev: *mut SmoltcpDevice<E>,
|
dev: *mut SmoltcpDevice<EC>
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, E: 'a + EthPhy> TxToken for EthTxToken<'a, E> {
|
impl<'a, EC: 'a + EthController> TxToken for EthTxToken<'a, EC> {
|
||||||
fn consume<R, F>(self, _timestamp: Instant, len: usize, f: F) -> Result<R, Error>
|
fn consume<R, F>(self, _timestamp: Instant, len: usize, f: F) -> Result<R, Error>
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut [u8]) -> Result<R, Error>,
|
F: FnOnce(&mut [u8]) -> Result<R, Error>,
|
||||||
|
@ -95,10 +97,12 @@ impl<'a, E: 'a + EthPhy> TxToken for EthTxToken<'a, E> {
|
||||||
// Update frame length and write frame bytes
|
// Update frame length and write frame bytes
|
||||||
tx_packet.update_frame(&mut self.buf[..len], len);
|
tx_packet.update_frame(&mut self.buf[..len], len);
|
||||||
// Send the packet as raw
|
// Send the packet as raw
|
||||||
let eth_phy = unsafe { &mut (*self.dev).eth_phy };
|
let eth_controller = unsafe {
|
||||||
match eth_phy.borrow_mut().send_packet(&tx_packet) {
|
&mut (*self.dev).eth_controller
|
||||||
Ok(_) => result,
|
};
|
||||||
Err(_) => Err(Error::Exhausted),
|
match eth_controller.borrow_mut().send_raw_packet(&tx_packet) {
|
||||||
|
Ok(_) => { result },
|
||||||
|
Err(_) => Err(Error::Exhausted)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
244
src/spi.rs
244
src/spi.rs
|
@ -1,4 +1,7 @@
|
||||||
use embedded_hal::{blocking::spi::Transfer, digital::v2::OutputPin};
|
use embedded_hal::{
|
||||||
|
blocking::spi::Transfer,
|
||||||
|
digital::v2::OutputPin,
|
||||||
|
};
|
||||||
|
|
||||||
pub mod interfaces {
|
pub mod interfaces {
|
||||||
use embedded_hal::spi;
|
use embedded_hal::spi;
|
||||||
|
@ -12,21 +15,11 @@ pub mod interfaces {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod opcodes {
|
pub mod opcodes {
|
||||||
/// 1-byte Instructions
|
/// SPI Opcodes
|
||||||
pub const SETETHRST: u8 = 0b1100_1010;
|
|
||||||
pub const SETPKTDEC: u8 = 0b1100_1100;
|
|
||||||
pub const SETTXRTS: u8 = 0b1101_0100;
|
|
||||||
pub const ENABLERX: u8 = 0b1110_1000;
|
|
||||||
/// 3-byte Instructions
|
|
||||||
pub const WRXRDPT: u8 = 0b0110_0100; // 8-bit opcode followed by data
|
|
||||||
pub const RRXRDPT: u8 = 0b0110_0110; // 8-bit opcode followed by data
|
|
||||||
pub const WGPWRPT: u8 = 0b0110_1100; // 8-bit opcode followed by data
|
|
||||||
pub const RGPWRPT: u8 = 0b0110_1110; // 8-bit opcode followed by data
|
|
||||||
/// N-byte Instructions
|
|
||||||
pub const RCRU: u8 = 0b0010_0000;
|
pub const RCRU: u8 = 0b0010_0000;
|
||||||
pub const WCRU: u8 = 0b0010_0010;
|
pub const WCRU: u8 = 0b0010_0010;
|
||||||
pub const RRXDATA: u8 = 0b0010_1100; // 8-bit opcode followed by data
|
pub const RERXDATA: u8 = 0b0010_1100; // 8-bit opcode followed by data
|
||||||
pub const WGPDATA: u8 = 0b0010_1010; // 8-bit opcode followed by data
|
pub const WEGPDATA: u8 = 0b0010_1010; // 8-bit opcode followed by data
|
||||||
}
|
}
|
||||||
|
|
||||||
pub mod addrs {
|
pub mod addrs {
|
||||||
|
@ -58,160 +51,157 @@ pub mod addrs {
|
||||||
|
|
||||||
/// Struct for SPI I/O interface on ENC424J600
|
/// Struct for SPI I/O interface on ENC424J600
|
||||||
/// Note: stm32f4xx_hal::spi's pins include: SCK, MISO, MOSI
|
/// Note: stm32f4xx_hal::spi's pins include: SCK, MISO, MOSI
|
||||||
pub struct SpiPort<SPI: Transfer<u8>, NSS: OutputPin> {
|
pub struct SpiPort<SPI: Transfer<u8>,
|
||||||
|
NSS: OutputPin,
|
||||||
|
F: FnMut(u32) -> ()> {
|
||||||
spi: SPI,
|
spi: SPI,
|
||||||
nss: NSS,
|
nss: NSS,
|
||||||
#[cfg(feature = "cortex-m-cpu")]
|
delay_ns: F,
|
||||||
cpu_freq_mhz: f32,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Error {
|
pub enum SpiPortError {
|
||||||
OpcodeError,
|
TransferError
|
||||||
TransferError,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(unused_must_use)]
|
#[allow(unused_must_use)]
|
||||||
impl<SPI: Transfer<u8>, NSS: OutputPin> SpiPort<SPI, NSS> {
|
impl <SPI: Transfer<u8>,
|
||||||
|
NSS: OutputPin,
|
||||||
|
F: FnMut(u32) -> ()> SpiPort<SPI, NSS, F> {
|
||||||
// TODO: return as Result()
|
// TODO: return as Result()
|
||||||
pub fn new(spi: SPI, mut nss: NSS) -> Self {
|
pub fn new(spi: SPI, mut nss: NSS, delay_ns: F) -> Self {
|
||||||
nss.set_high();
|
nss.set_high();
|
||||||
|
|
||||||
SpiPort {
|
SpiPort {
|
||||||
spi,
|
spi,
|
||||||
nss,
|
nss,
|
||||||
#[cfg(feature = "cortex-m-cpu")]
|
delay_ns,
|
||||||
cpu_freq_mhz: 0.,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "cortex-m-cpu")]
|
pub fn read_reg_8b(&mut self, addr: u8) -> Result<u8, SpiPortError> {
|
||||||
pub fn cpu_freq_mhz(mut self, freq: u32) -> Self {
|
|
||||||
self.cpu_freq_mhz = freq as f32;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn read_reg_8b(&mut self, addr: u8) -> Result<u8, Error> {
|
|
||||||
// Using RCRU instruction to read using unbanked (full) address
|
// Using RCRU instruction to read using unbanked (full) address
|
||||||
let mut buf: [u8; 4] = [0; 4];
|
let r_data = self.rw_addr_u8(opcodes::RCRU, addr, 0)?;
|
||||||
buf[1] = addr;
|
Ok(r_data)
|
||||||
self.rw_n(&mut buf, opcodes::RCRU, 2)?;
|
|
||||||
Ok(buf[2])
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_reg_16b(&mut self, lo_addr: u8) -> Result<u16, Error> {
|
pub fn read_reg_16b(&mut self, lo_addr: u8) -> Result<u16, SpiPortError> {
|
||||||
// Unless the register can be written with specific opcode,
|
let r_data_lo = self.read_reg_8b(lo_addr)?;
|
||||||
// use WCRU instruction to write using unbanked (full) address
|
let r_data_hi = self.read_reg_8b(lo_addr + 1)?;
|
||||||
let mut buf: [u8; 4] = [0; 4];
|
// Combine top and bottom 8-bit to return 16-bit
|
||||||
let mut data_offset = 0; // number of bytes separating
|
Ok(((r_data_hi as u16) << 8) | r_data_lo as u16)
|
||||||
// actual data from opcode
|
|
||||||
match lo_addr {
|
|
||||||
addrs::ERXRDPT | addrs::EGPWRPT => {}
|
|
||||||
_ => {
|
|
||||||
buf[1] = lo_addr;
|
|
||||||
data_offset = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
self.rw_n(
|
|
||||||
&mut buf,
|
|
||||||
match lo_addr {
|
|
||||||
addrs::ERXRDPT => opcodes::RRXRDPT,
|
|
||||||
addrs::EGPWRPT => opcodes::RGPWRPT,
|
|
||||||
_ => opcodes::RCRU,
|
|
||||||
},
|
|
||||||
2 + data_offset, // extra 8-bit lo_addr before data
|
|
||||||
)?;
|
|
||||||
Ok(buf[data_offset + 1] as u16 | (buf[data_offset + 2] as u16) << 8)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Currently requires manual slicing (buf[1..]) for the data read back
|
// Currently requires manual slicing (buf[1..]) for the data read back
|
||||||
pub fn read_rxdat<'a>(&mut self, buf: &'a mut [u8], data_length: usize) -> Result<(), Error> {
|
pub fn read_rxdat<'a>(&mut self, buf: &'a mut [u8], data_length: usize)
|
||||||
self.rw_n(buf, opcodes::RRXDATA, data_length)
|
-> Result<(), SpiPortError> {
|
||||||
|
let r_valid = self.r_n(buf, opcodes::RERXDATA, data_length)?;
|
||||||
|
Ok(r_valid)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Currently requires actual data to be stored in buf[1..] instead of buf[0..]
|
// Currenly requires actual data to be stored in buf[1..] instead of buf[0..]
|
||||||
// TODO: Maybe better naming?
|
// TODO: Maybe better naming?
|
||||||
pub fn write_txdat<'a>(&mut self, buf: &'a mut [u8], data_length: usize) -> Result<(), Error> {
|
pub fn write_txdat<'a>(&mut self, buf: &'a mut [u8], data_length: usize)
|
||||||
self.rw_n(buf, opcodes::WGPDATA, data_length)
|
-> Result<(), SpiPortError> {
|
||||||
|
let w_valid = self.w_n(buf, opcodes::WEGPDATA, data_length)?;
|
||||||
|
Ok(w_valid)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_reg_8b(&mut self, addr: u8, data: u8) -> Result<(), Error> {
|
pub fn write_reg_8b(&mut self, addr: u8, data: u8) -> Result<(), SpiPortError> {
|
||||||
|
// TODO: addr should be separated from w_data
|
||||||
// Using WCRU instruction to write using unbanked (full) address
|
// Using WCRU instruction to write using unbanked (full) address
|
||||||
let mut buf: [u8; 3] = [0; 3];
|
self.rw_addr_u8(opcodes::WCRU, addr, data)?;
|
||||||
buf[1] = addr;
|
Ok(())
|
||||||
buf[2] = data;
|
|
||||||
self.rw_n(&mut buf, opcodes::WCRU, 2)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_reg_16b(&mut self, lo_addr: u8, data: u16) -> Result<(), Error> {
|
pub fn write_reg_16b(&mut self, lo_addr: u8, data: u16) -> Result<(), SpiPortError> {
|
||||||
// Unless the register can be written with specific opcode,
|
self.write_reg_8b(lo_addr, (data & 0xff) as u8)?;
|
||||||
// use WCRU instruction to write using unbanked (full) address
|
self.write_reg_8b(lo_addr + 1, ((data & 0xff00) >> 8) as u8)?;
|
||||||
let mut buf: [u8; 4] = [0; 4];
|
Ok(())
|
||||||
let mut data_offset = 0; // number of bytes separating
|
|
||||||
// actual data from opcode
|
|
||||||
match lo_addr {
|
|
||||||
addrs::ERXRDPT | addrs::EGPWRPT => {}
|
|
||||||
_ => {
|
|
||||||
buf[1] = lo_addr;
|
|
||||||
data_offset = 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
buf[1 + data_offset] = data as u8;
|
|
||||||
buf[2 + data_offset] = (data >> 8) as u8;
|
|
||||||
self.rw_n(
|
|
||||||
&mut buf,
|
|
||||||
match lo_addr {
|
|
||||||
addrs::ERXRDPT => opcodes::WRXRDPT,
|
|
||||||
addrs::EGPWRPT => opcodes::WGPWRPT,
|
|
||||||
_ => opcodes::WCRU,
|
|
||||||
},
|
|
||||||
2 + data_offset, // extra 8-bit lo_addr before data
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn send_opcode(&mut self, opcode: u8) -> Result<(), Error> {
|
pub fn delay_us(&mut self, duration: u32) {
|
||||||
match opcode {
|
(self.delay_ns)(duration * 1000)
|
||||||
opcodes::SETETHRST | opcodes::SETPKTDEC | opcodes::SETTXRTS | opcodes::ENABLERX => {
|
|
||||||
let mut buf: [u8; 1] = [0];
|
|
||||||
self.rw_n(&mut buf, opcode, 0)
|
|
||||||
}
|
|
||||||
_ => Err(Error::OpcodeError),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Actual data should start from buf[0], not buf[1]
|
// TODO: Generalise transfer functions
|
||||||
// Completes an SPI transfer for reading data to the given buffer,
|
// TODO: (Make data read/write as reference to array)
|
||||||
// or writing data from the buffer.
|
// Currently requires 1-byte addr, read/write data is only 1-byte
|
||||||
// It sends an 8-bit instruction, followed by either
|
fn rw_addr_u8(&mut self, opcode: u8, addr: u8, data: u8)
|
||||||
// receiving or sending n*8-bit data.
|
-> Result<u8, SpiPortError> {
|
||||||
// The slice of buffer provided must begin with the 8-bit instruction.
|
// Enable chip select
|
||||||
// If n = 0, the transfer will only involve sending the instruction.
|
self.nss.set_low();
|
||||||
fn rw_n<'a>(&mut self, buf: &'a mut [u8], opcode: u8, data_length: usize) -> Result<(), Error> {
|
// Start writing to SLAVE
|
||||||
assert!(buf.len() > data_length);
|
// TODO: don't just use 3 bytes
|
||||||
|
let mut buf: [u8; 3] = [0; 3];
|
||||||
|
buf[0] = opcode;
|
||||||
|
buf[1] = addr;
|
||||||
|
buf[2] = data;
|
||||||
|
match self.spi.transfer(&mut buf) {
|
||||||
|
Ok(_) => {
|
||||||
|
// Disable chip select
|
||||||
|
(self.delay_ns)(60);
|
||||||
|
self.nss.set_high();
|
||||||
|
(self.delay_ns)(30);
|
||||||
|
Ok(buf[2])
|
||||||
|
},
|
||||||
|
// TODO: Maybe too naive?
|
||||||
|
Err(_) => {
|
||||||
|
// Disable chip select
|
||||||
|
(self.delay_ns)(60);
|
||||||
|
self.nss.set_high();
|
||||||
|
(self.delay_ns)(30);
|
||||||
|
Err(SpiPortError::TransferError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: Generalise transfer functions
|
||||||
|
// Currently does NOT accept addr, read data is N-byte long
|
||||||
|
// Returns a reference to the data returned
|
||||||
|
// Note: buf must be at least (data_length + 1)-byte long
|
||||||
|
// TODO: Check and raise error for array size < (data_length + 1)
|
||||||
|
fn r_n<'a>(&mut self, buf: &'a mut [u8], opcode: u8, data_length: usize)
|
||||||
|
-> Result<(), SpiPortError> {
|
||||||
// Enable chip select
|
// Enable chip select
|
||||||
self.nss.set_low();
|
self.nss.set_low();
|
||||||
// >=50ns min. CS_n setup time
|
|
||||||
#[cfg(feature = "cortex-m-cpu")]
|
|
||||||
cortex_m::asm::delay((0.05 * (self.cpu_freq_mhz + 1.)) as u32);
|
|
||||||
// Start writing to SLAVE
|
// Start writing to SLAVE
|
||||||
buf[0] = opcode;
|
buf[0] = opcode;
|
||||||
let result = self.spi.transfer(&mut buf[..data_length + 1]);
|
match self.spi.transfer(&mut buf[..data_length+1]) {
|
||||||
match opcode {
|
Ok(_) => {
|
||||||
opcodes::RCRU | opcodes::WCRU | opcodes::RRXDATA | opcodes::WGPDATA => {
|
|
||||||
// Disable chip select
|
// Disable chip select
|
||||||
// >=50ns min. CS_n hold time
|
|
||||||
#[cfg(feature = "cortex-m-cpu")]
|
|
||||||
cortex_m::asm::delay((0.05 * (self.cpu_freq_mhz + 1.)) as u32);
|
|
||||||
self.nss.set_high();
|
self.nss.set_high();
|
||||||
// >=20ns min. CS_n disable time
|
Ok(())
|
||||||
#[cfg(feature = "cortex-m-cpu")]
|
},
|
||||||
cortex_m::asm::delay((0.02 * (self.cpu_freq_mhz + 1.)) as u32);
|
|
||||||
}
|
|
||||||
_ => {}
|
|
||||||
}
|
|
||||||
match result {
|
|
||||||
Ok(_) => Ok(()),
|
|
||||||
// TODO: Maybe too naive?
|
// TODO: Maybe too naive?
|
||||||
Err(_) => Err(Error::TransferError),
|
Err(_) => {
|
||||||
|
// Disable chip select
|
||||||
|
self.nss.set_high();
|
||||||
|
Err(SpiPortError::TransferError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: buf[0] is currently reserved for opcode to overwrite
|
||||||
|
// TODO: Actual data should start from buf[0], not buf[1]
|
||||||
|
fn w_n<'a>(&mut self, buf: &'a mut [u8], opcode: u8, data_length: usize)
|
||||||
|
-> Result<(), SpiPortError> {
|
||||||
|
// Enable chip select
|
||||||
|
self.nss.set_low();
|
||||||
|
// Start writing to SLAVE
|
||||||
|
buf[0] = opcode;
|
||||||
|
// TODO: Maybe need to copy data to buf later on
|
||||||
|
match self.spi.transfer(&mut buf[..data_length+1]) {
|
||||||
|
Ok(_) => {
|
||||||
|
// Disable chip select
|
||||||
|
self.nss.set_high();
|
||||||
|
Ok(())
|
||||||
|
},
|
||||||
|
// TODO: Maybe too naive?
|
||||||
|
Err(_) => {
|
||||||
|
// Disable chip select
|
||||||
|
self.nss.set_high();
|
||||||
|
Err(SpiPortError::TransferError)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
26
src/tx.rs
26
src/tx.rs
|
@ -1,28 +1,32 @@
|
||||||
use crate::RAW_FRAME_LENGTH_MAX;
|
use crate::RAW_FRAME_LENGTH_MAX;
|
||||||
|
|
||||||
|
/// SRAM Addresses
|
||||||
|
pub const GPBUFST_DEFAULT: u16 = 0x0000; // Start of General-Purpose SRAM Buffer
|
||||||
|
pub const GPBUFEN_DEFAULT: u16 = 0x5340; // End of General-Purpose SRAM Buffer == ERXST default
|
||||||
|
|
||||||
/// Struct for TX Buffer on the hardware
|
/// Struct for TX Buffer on the hardware
|
||||||
/// TODO: Should be a singleton
|
/// TODO: Should be a singleton
|
||||||
pub struct TxBuffer {
|
pub struct TxBuffer {
|
||||||
start_addr: u16,
|
wrap_addr: u16,
|
||||||
// The following two fields are controlled by firmware
|
// The following two fields are controlled by firmware
|
||||||
next_addr: u16,
|
next_addr: u16,
|
||||||
tail_addr: u16,
|
tail_addr: u16
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TxBuffer {
|
impl TxBuffer {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
TxBuffer {
|
TxBuffer {
|
||||||
start_addr: 0x0000,
|
wrap_addr: GPBUFST_DEFAULT,
|
||||||
next_addr: 0x0001,
|
next_addr: GPBUFST_DEFAULT + 1,
|
||||||
tail_addr: 0x0000,
|
tail_addr: GPBUFST_DEFAULT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_start_addr(&mut self, addr: u16) {
|
pub fn set_wrap_addr(&mut self, addr: u16) {
|
||||||
self.start_addr = addr;
|
self.wrap_addr = addr;
|
||||||
}
|
}
|
||||||
pub fn get_start_addr(&self) -> u16 {
|
pub fn get_wrap_addr(& self) -> u16{
|
||||||
self.start_addr
|
self.wrap_addr
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_next_addr(&mut self, addr: u16) {
|
pub fn set_next_addr(&mut self, addr: u16) {
|
||||||
|
@ -44,14 +48,14 @@ impl TxBuffer {
|
||||||
/// TODO: Generalise MAC addresses
|
/// TODO: Generalise MAC addresses
|
||||||
pub struct TxPacket {
|
pub struct TxPacket {
|
||||||
frame: [u8; RAW_FRAME_LENGTH_MAX],
|
frame: [u8; RAW_FRAME_LENGTH_MAX],
|
||||||
frame_length: usize,
|
frame_length: usize
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TxPacket {
|
impl TxPacket {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
TxPacket {
|
TxPacket {
|
||||||
frame: [0; RAW_FRAME_LENGTH_MAX],
|
frame: [0; RAW_FRAME_LENGTH_MAX],
|
||||||
frame_length: 0,
|
frame_length: 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue