Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GMAC API #33

Draft
wants to merge 10 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## [GMAC API]

### Added
- API for Gmac Peripheral

## [Unreleased]

### Added
Expand Down
27 changes: 26 additions & 1 deletion boards/atsame70_xpro/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions boards/atsame70_xpro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ panic-rtt-target = { version = "0.1.2", features = ["cortex-m"] }
rtt-target = { version = "0.3.1", features = ["cortex-m"] }
usbd-serial = "0.1.1"
heapless = "0.7"
smoltcp = { version = "0.8.0", default-features = false, features = ["medium-ethernet", "medium-ip", "proto-ipv4", "socket-raw", "socket-tcp", "socket-dhcpv4"] }

[dependencies.atsamx7x-hal]
version = "0.3.0"
path = "../../hal"
features = ["same70q21b-rt", "unproven"]
features = ["same70n21b-rt", "unproven"]

[profile.dev]
debug = true
lto = true
lto = true
165 changes: 165 additions & 0 deletions boards/atsame70_xpro/examples/gmac.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
//! GMAC Example
//! This example should echo messages received via TCP.
//! NOTE: This code has not been tested.
#![no_std]
#![no_main]

use core::sync::atomic::{AtomicUsize, Ordering};
use panic_halt as _;
// use defmt_rtt as _;
// use defmt;
//
// static COUNT: AtomicUsize = AtomicUsize::new(0);
// defmt::timestamp!("{=usize}", {
// // NOTE(no-CAS) `timestamps` runs with interrupts disabled
// let n = COUNT.load(Ordering::Relaxed);
// COUNT.store(n + 1, Ordering::Relaxed);
// n
// });
#[rtic::app(device = hal::target_device, peripherals = true, dispatchers = [IXC])]
mod app {
use atsamx7x_hal as hal;
use hal::ehal::digital::v2::ToggleableOutputPin;
use hal::pio::*;
use hal::pmc::*;
use hal::gmac::*;
use smoltcp::iface::{Neighbor, InterfaceBuilder, SocketStorage, NeighborCache, Interface, Route, Routes};
use smoltcp::phy::{Device, RxToken, TxToken};
use smoltcp::socket::{TcpSocketBuffer, TcpSocket, Dhcpv4Event, Dhcpv4Socket};
use smoltcp::time::Instant;
use smoltcp::wire::{IpCidr, IpAddress, EthernetAddress, HardwareAddress, Ipv4Address, Ipv4Cidr};
use cortex_m::singleton;

use rtt_target::rtt_init_print;
use rtt_target::rprintln;

#[shared]
struct Shared {}

#[local]
struct Local {
iface: Interface<'static, Gmac>,
}

#[init]
fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) {

rtt_init_print!();
rprintln!("Init");

let mut efc = hal::efc::Efc::new(ctx.device.EFC, hal::efc::VddioLevel::V3);
let mut pmc = hal::pmc::Pmc::new(ctx.device.PMC, &ctx.device.WDT.into());
let slck = pmc.get_slck(ctx.device.SUPC, SlowCkSource::InternalRC);

let bankd = hal::pio::BankD::new(ctx.device.PIOD, &mut pmc, BankConfiguration::default());
// Configure PD[1,2,3,4,5,6,8,9,11,12,14,15,16] for ethernet
let txck: Pin<_,Peripheral<A>> = bankd.pd0.into_peripheral();
let txen: Pin<_,Peripheral<A>> = bankd.pd1.into_peripheral();
let tx0: Pin<_,Peripheral<A>> = bankd.pd2.into_peripheral();
let tx1: Pin<_,Peripheral<A>> = bankd.pd3.into_peripheral();
let rxdv: Pin<_,Peripheral<A>> = bankd.pd4.into_peripheral();
let rx0: Pin<_,Peripheral<A>> = bankd.pd5.into_peripheral();
let rx1: Pin<_,Peripheral<A>> = bankd.pd6.into_peripheral();
let rxer: Pin<_,Peripheral<A>> = bankd.pd7.into_peripheral();
let mdc: Pin<_,Peripheral<A>> = bankd.pd8.into_peripheral();
let mdio: Pin<_,Peripheral<A>> = bankd.pd9.into_peripheral();
let crs: Pin<_,Peripheral<A>> = bankd.pd10.into_peripheral();
let rx2: Pin<_,Peripheral<A>> = bankd.pd11.into_peripheral();
let rx3: Pin<_,Peripheral<A>> = bankd.pd12.into_peripheral();
let col: Pin<_,Peripheral<A>> = bankd.pd13.into_peripheral();
let rxck: Pin<_,Peripheral<A>> = bankd.pd14.into_peripheral();
let tx2: Pin<_,Peripheral<A>> = bankd.pd15.into_peripheral();
let tx3: Pin<_,Peripheral<A>> = bankd.pd16.into_peripheral();
let txer: Pin<_,Peripheral<A>> = bankd.pd17.into_peripheral();

let gmac = ctx.device.GMAC;
let mut gmac = Gmac::new_gmac(gmac, (txck, txen, tx3, tx2, tx1, tx0, txer, rxck, rxdv, rx3, rx2, rx1, rx0, rxer, crs, col, mdc, mdio), GmacConfiguration{}, &mut pmc).unwrap();
{
// enables the peripheral clock
// pmc.enable_periph_clk(39).unwrap();

gmac.init();
// defmt::debug!("miim_post_setup might not return");
gmac.miim_post_setup();
// defmt::debug!("miim_post_setup did return, all is good.");
}


let ip_addrs: &'static mut _ = singleton!(: [IpCidr; 1] = [IpCidr::new(IpAddress::v4(169, 254, 33, 1), 24)]).unwrap();
let neighbor_cache: &'static mut _ = singleton!(: [Option<(IpAddress, Neighbor)>; 8] = [None; 8]).unwrap();
let sockets: &'static mut _ = singleton!(: [SocketStorage<'static>; 8] = [SocketStorage::EMPTY; 8]).unwrap();
let routes_storage: &'static mut _ = singleton!(: [Option<(IpCidr, Route)>; 1] = [None; 1]).unwrap();
let routes = Routes::new(routes_storage.as_mut_slice());

let iface = InterfaceBuilder::new(gmac, sockets.as_mut_slice())
.hardware_addr(EthernetAddress::from_bytes(&[0x04, 0x91, 0x62, 0x01, 0x02, 0x03]).into())
.neighbor_cache(NeighborCache::new(neighbor_cache.as_mut_slice()))
.routes(routes)
.ip_addrs(ip_addrs.as_mut_slice())
.finalize();

(Shared {}, Local {iface}, init::Monotonics())
}

#[idle(local = [iface])]
fn idle(ctx: idle::Context) -> ! {
let mut iface = ctx.local.iface;
rprintln!("Idle");

let server_socket = {
let rx_data: &'static mut [u8] = singleton!(: [u8; 1024] = [0; 1024]).unwrap();
let tx_data: &'static mut [u8] = singleton!(: [u8; 1024] = [0; 1024]).unwrap();
let tcp_rx_buffer = TcpSocketBuffer::new(rx_data);
let tcp_tx_buffer = TcpSocketBuffer::new(tx_data);
TcpSocket::new(tcp_rx_buffer,tcp_tx_buffer)
};

let server_handle = iface.add_socket(server_socket);
let mut last_state = smoltcp::socket::TcpState::Closed;
loop {
match iface.poll(Instant::from_micros(48)) {
Ok(_) => {},
Err(e) => {
}
}


let mut buf = [0u8;1024];
let socket = iface.get_socket::<TcpSocket>(server_handle);
let state = socket.state();
if state != last_state {
// defmt::println!("STATE CHANGE: {=?} => {=?}", last_state, state);
last_state = state;
}

if state == smoltcp::socket::TcpState::CloseWait {
socket.close();
}

if !socket.is_active() && !socket.is_listening() {
// defmt::info!("Listening...");
socket.listen(4321).unwrap();
}


let mut to_send = None;
if socket.can_recv() {
socket.recv(|buffer| {
// defmt::info!("Receive!");
// defmt::info!("Len: {}", buffer.len());
// defmt::info!("dat {}", buffer);
// defmt::info!("{}", cmd_string.as_str());

buf[..buffer.len()].copy_from_slice(&buffer[..buffer.len()]);
to_send = Some(&buf[..buffer.len()]);

(buffer.len(), ())
}).unwrap();
}
if socket.can_send() && to_send != None {
let tx = to_send.unwrap();
socket.send_slice(tx).unwrap();
}
}
}
}
25 changes: 25 additions & 0 deletions boards/atsamv71_xult/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions boards/atsamv71_xult/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cortex-m-rtic = "1.0"
cortex-m = "0.7"
panic-rtt-target = { version = "0.1.2", features = ["cortex-m"] }
rtt-target = { version = "0.3.1", features = ["cortex-m"] }
smoltcp = { version = "0.8.1", default-features = false, features = ["medium-ethernet", "medium-ip", "proto-ipv4", "socket-raw", "socket-tcp", "socket-dhcpv4", "socket-udp"] }
heapless = "0.7"
usbd-serial = "0.1.1"
dwt-systick-monotonic = "1.0.0"
Expand All @@ -21,6 +22,9 @@ version = "0.3.0"
path = "../../hal"
features = ["samv71q21b-rt", "unproven"]

[features]
static-ip = []

[profile.dev]
debug = true
lto = true
Expand Down
Loading