Compare commits

...

8 Commits

Author SHA1 Message Date
Astro c5dfaf0ee2 delint 2019-09-02 00:01:18 +02:00
Astro 3f7da6e328 ad7172: rm debug 2019-09-02 00:00:50 +02:00
Astro f94d915328 identify adc 2019-09-01 23:56:45 +02:00
Astro ea395460c8 add cortex-m-semihosting 2019-09-01 23:56:27 +02:00
Astro 01a9decfa1 board: fix gpio initialization 2019-09-01 23:47:15 +02:00
Astro e947415242 board: refactor gpio to use indexed access 2019-09-01 22:57:35 +02:00
Astro 5ba09db517 board: wait gpio portb ready 2019-09-01 22:46:15 +02:00
Astro fad1050556 gpio: fix PE4 idx 2019-09-01 22:28:46 +02:00
7 changed files with 76 additions and 55 deletions

52
firmware/Cargo.lock generated
View File

@ -24,7 +24,7 @@ dependencies = [
[[package]]
name = "bare-metal"
version = "0.2.4"
version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
@ -51,18 +51,18 @@ version = "0.5.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aligned 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
"bare-metal 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"cortex-m 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)",
"bare-metal 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"cortex-m 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
"volatile-register 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cortex-m"
version = "0.6.0"
version = "0.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"aligned 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)",
"bare-metal 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"bare-metal 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"volatile-register 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -83,7 +83,15 @@ dependencies = [
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 0.6.13 (registry+https://github.com/rust-lang/crates.io-index)",
"rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.42 (registry+https://github.com/rust-lang/crates.io-index)",
"syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "cortex-m-semihosting"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"cortex-m 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -108,7 +116,7 @@ name = "generic-array"
version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)",
"typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -117,6 +125,7 @@ version = "1.0.0"
dependencies = [
"cortex-m 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
"cortex-m-rt 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
"cortex-m-semihosting 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
"crc 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)",
"embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"libm 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)",
@ -184,12 +193,12 @@ name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rand_core"
version = "0.4.0"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@ -239,7 +248,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "syn"
version = "0.15.42"
version = "0.15.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"proc-macro2 0.4.30 (registry+https://github.com/rust-lang/crates.io-index)",
@ -252,15 +261,15 @@ name = "tm4c129x"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"bare-metal 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)",
"bare-metal 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)",
"cortex-m 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)",
"cortex-m-rt 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)",
"vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"vcell 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "typenum"
version = "1.10.0"
version = "1.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@ -270,7 +279,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "vcell"
version = "0.1.0"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
@ -283,7 +292,7 @@ name = "volatile-register"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)",
"vcell 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -310,14 +319,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum aligned 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d39da9b88ae1a81c03c9c082b8db83f1d0e93914126041962af61034ab44c4a5"
"checksum aligned 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d3a316c7ea8e1e9ece54862c992def5a7ac14de9f5832b69d71760680efeeefa"
"checksum as-slice 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "293dac66b274fab06f95e7efb05ec439a6b70136081ea522d270bc351ae5bb27"
"checksum bare-metal 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)" = "a3caf393d93b2d453e80638d0674597020cef3382ada454faacd43d1a55a735a"
"checksum bare-metal 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3"
"checksum bitflags 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3d155346769a6855b86399e9bc3814ab343cd3d62c7e985113d46a0ec3c281fd"
"checksum build_const 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "39092a32794787acd8525ee150305ff051b0aa6cc2abaf193924f5ab05425f39"
"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5"
"checksum cortex-m 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)" = "3c0b159a1e8306949579de3698c841dba58058197b65c60807194e4fa1e7a554"
"checksum cortex-m 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f3c18719fdc57db65668bfc977db9a0fa1a41d718c5d9cd4f652c9d4b0e0956a"
"checksum cortex-m 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "145da2fc379bbd378ed425e75e1748214add9bbd800d4d5b77abb54ca423dbca"
"checksum cortex-m-rt 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "17805910e3ecf029bdbfcc42b7384d9e3d9e5626153fa810002c1ef9839338ac"
"checksum cortex-m-rt-macros 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "d7ae692573e0acccb1579fef1abf5a5bf1d2f3f0149a22b16870ec9309aee25f"
"checksum cortex-m-semihosting 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "113ef0ecffee2b62b58f9380f4469099b30e9f9cbee2804771b4203ba1762cfa"
"checksum crc 1.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d663548de7f5cca343f1e0a48d14dcfb0e9eb4e079ec58883b7251539fa10aeb"
"checksum embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "ee4908a155094da7723c2d60d617b820061e3b4efcc3d9e293d206a5a76c170b"
"checksum generic-array 0.12.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c68f0274ae0e023facc3c97b2e00f076be70e254bc851d972503b328db79b2ec"
@ -330,18 +340,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum r0 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e2a38df5b15c8d5c7e8654189744d8e396bddc18ad48041a500ce52d6948941f"
"checksum rand 0.5.6 (registry+https://github.com/rust-lang/crates.io-index)" = "c618c47cd3ebd209790115ab837de41425723956ad3ce2e6a7f09890947cacb9"
"checksum rand_core 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
"checksum rand_core 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d0e7a549d590831370895ab7ba4ea0c1b6b011d106b5ff2da6eee112615e6dc0"
"checksum rand_core 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
"checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
"checksum same-file 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "d931a44fdaa43b8637009e7632a02adc4f2b2e0733c08caa4cf00e8da4a117a7"
"checksum semver 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d7eb9ef2c18661902cc47e535f9bc51b78acd254da71d375c2f6720d9a40403"
"checksum semver-parser 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
"checksum smoltcp 0.4.0 (git+https://github.com/m-labs/smoltcp?rev=cd893e6)" = "<none>"
"checksum stable_deref_trait 1.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "dba1a27d3efae4351c8051072d619e3ade2820635c3958d826bfea39d59b54c8"
"checksum syn 0.15.42 (registry+https://github.com/rust-lang/crates.io-index)" = "eadc09306ca51a40555dd6fc2b415538e9e18bc9f870e47b1a524a79fe2dcf5e"
"checksum syn 0.15.44 (registry+https://github.com/rust-lang/crates.io-index)" = "9ca4b3b69a77cbe1ffc9e198781b7acb0c7365a883670e8f1c1bc66fba79a5c5"
"checksum tm4c129x 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7d430ed4ed06dd9fff3d4517a37343e1b53789218f2f608bf1e0432f67abf624"
"checksum typenum 1.10.0 (registry+https://github.com/rust-lang/crates.io-index)" = "612d636f949607bdf9b123b4a6f6d966dedf3ff669f7f045890d3a4a73948169"
"checksum typenum 1.11.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6d2783fe2d6b8c1101136184eb41be8b1ad379e4657050b8aaff0c79ee7575f9"
"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc"
"checksum vcell 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "45c297f0afb6928cd08ab1ff9d95e99392595ea25ae1b5ecf822ff8764e57a0d"
"checksum vcell 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "876e32dcadfe563a4289e994f7cb391197f362b6315dc45e8ba4aa6f564a4b3c"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
"checksum volatile-register 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "0d67cb4616d99b940db1d6bd28844ff97108b498a6ca850e5b6191a532063286"
"checksum walkdir 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "bb08f9e670fab86099470b97cd2b252d6527f0b3cc1401acdb595ffc9dd288ff"

View File

@ -14,6 +14,7 @@ crc = { version = "1.7", default-features = false }
tm4c129x = { version = "0.8", features = ["rt"] }
embedded-hal = { version = "0.2", features = ["unproven"] }
nb = "0.1"
cortex-m-semihosting = "0.3"
[dependencies.smoltcp]
git = "https://github.com/m-labs/smoltcp"

View File

@ -94,10 +94,6 @@ impl<SPI: Transfer<u8>, NSS: OutputPin> Adc<SPI, NSS> {
fn read_reg(&mut self, reg: Register, buffer: &'_ mut [u8]) -> Result<(), SPI::Error> {
buffer[0] = 0x40 | (reg as u8);
self.transfer(buffer)?;
use core::fmt::Write;
use cortex_m_semihosting::hio;
let mut stdout = hio::hstdout().unwrap();
writeln!(stdout, "ad rreg {}: {:?}", reg as u8, buffer);
Ok(())
}

View File

@ -1,5 +1,4 @@
use core::mem::transmute;
use core::slice::from_raw_parts_mut;
use core::slice::{from_raw_parts, from_raw_parts_mut};
use embedded_hal::digital::v2::{InputPin, OutputPin};
pub trait Gpio where Self: Sized {
@ -12,17 +11,31 @@ pub struct GpioOutput<PIN>(PIN);
macro_rules! def_gpio {
($PORT: tt, $PIN: tt, $idx: expr) => (
impl $PIN {
fn data(&self) -> &u32 {
let gpio = tm4c129x::$PORT::ptr();
let data = unsafe { from_raw_parts(gpio as *const _ as *mut u32, 0x100) };
&data[(1 << $idx) as usize]
}
fn data_mut(&mut self) -> &mut u32 {
let gpio = tm4c129x::$PORT::ptr();
let data = unsafe { from_raw_parts_mut(gpio as *const _ as *mut u32, 0x100) };
&mut data[(1 << $idx) as usize]
}
}
impl Gpio for $PIN {
fn into_output(self) -> GpioOutput<Self> {
let gpio = unsafe { &*tm4c129x::$PORT::ptr() };
gpio.dir.modify(|_, w| w.dir().bits(1 << $idx));
gpio.den.modify(|_, w| w.den().bits(1 << $idx));
gpio.dir.modify(|r, w| w.dir().bits(r.dir().bits() | (1 << $idx)));
gpio.den.modify(|r, w| w.den().bits(r.den().bits() | (1 << $idx)));
GpioOutput(self)
}
fn into_input(self) -> GpioInput<Self> {
let gpio = unsafe { &*tm4c129x::$PORT::ptr() };
gpio.dir.modify(|r, w| w.dir().bits(r.dir().bits() & !(1 << $idx)));
gpio.den.modify(|_, w| w.den().bits(1 << $idx));
gpio.den.modify(|r, w| w.den().bits(r.den().bits() | (1 << $idx)));
GpioInput(self)
}
}
@ -30,27 +43,21 @@ macro_rules! def_gpio {
impl InputPin for GpioInput<$PIN> {
type Error = ();
fn is_high(&self) -> Result<bool, Self::Error> {
let gpio = unsafe { &*tm4c129x::$PORT::ptr() };
Ok(gpio.data.read().data().bits() & (1 << $idx) == (1 << $idx))
Ok(*self.0.data() != 0)
}
fn is_low(&self) -> Result<bool, Self::Error> {
let gpio = unsafe { &*tm4c129x::$PORT::ptr() };
Ok(gpio.data.read().data().bits() & (1 << $idx) != (1 << $idx))
Ok(*self.0.data() == 0)
}
}
impl OutputPin for GpioOutput<$PIN> {
type Error = ();
fn set_low(&mut self) -> Result<(), Self::Error> {
let gpio = unsafe { &*tm4c129x::$PORT::ptr() };
let data = masked_data(unsafe { transmute(&gpio.data) }, (1 << $idx));
*data = 0;
*self.0.data_mut() = 0;
Ok(())
}
fn set_high(&mut self) -> Result<(), Self::Error> {
let gpio = unsafe { &*tm4c129x::$PORT::ptr() };
let data = masked_data(unsafe { transmute(&gpio.data) }, (1 << $idx));
*data = 1 << $idx;
*self.0.data_mut() = 0xFF;
Ok(())
}
}
@ -62,12 +69,6 @@ def_gpio!(GPIO_PORTB_AHB, PB4, 4);
pub struct PB5;
def_gpio!(GPIO_PORTB_AHB, PB5, 5);
pub struct PE4;
def_gpio!(GPIO_PORTE_AHB, PE4, 5);
def_gpio!(GPIO_PORTE_AHB, PE4, 4);
pub struct PE5;
def_gpio!(GPIO_PORTE_AHB, PE5, 5);
/// Setting of GPIO pins is optimized by address masking
fn masked_data<'a>(data: *mut u32, bits: u8) -> &'a mut u32 {
let data = unsafe { from_raw_parts_mut(data, 0x400) };
&mut data[usize::from(bits)]
}

View File

@ -6,11 +6,6 @@ pub mod softspi;
pub mod delay;
const LED1: u8 = 0x10; // PK4
const LED2: u8 = 0x40; // PK6
pub const PWM_LOAD: u16 = (/*pwmclk*/120_000_000u32 / /*freq*/100_000) as u16;
const UART_DIV: u32 = (((/*sysclk*/120_000_000 * 8) / /*baud*/115200) + 1) / 2;
pub fn init() {
@ -49,6 +44,7 @@ pub fn init() {
.r14().bit(true)
});
while !sysctl.prgpio.read().r0().bit() {}
while !sysctl.prgpio.read().r1().bit() {}
while !sysctl.prgpio.read().r3().bit() {}
while !sysctl.prgpio.read().r4().bit() {}
while !sysctl.prgpio.read().r5().bit() {}

View File

@ -1,7 +1,6 @@
use embedded_hal::spi::FullDuplex;
use embedded_hal::digital::v2::{InputPin, OutputPin};
use embedded_hal::blocking::spi::Transfer;
use embedded_hal::blocking::delay::DelayUs;
use nb::Error::WouldBlock;
/// Bit-banged Mode3 SPI

View File

@ -11,14 +11,15 @@ extern crate smoltcp;
extern crate crc;
extern crate embedded_hal;
extern crate nb;
extern crate cortex_m_semihosting;
use core::fmt::{self, Write};
use embedded_hal::blocking::delay::DelayUs;
use cortex_m::interrupt::Mutex;
use smoltcp::time::Instant;
use smoltcp::wire::{IpCidr, IpAddress, EthernetAddress};
use smoltcp::iface::{NeighborCache, EthernetInterfaceBuilder};
use smoltcp::socket::{SocketSet, TcpSocket, TcpSocketBuffer};
use cortex_m_semihosting::hio;
#[macro_export]
macro_rules! print {
@ -82,7 +83,10 @@ macro_rules! create_socket {
#[entry]
fn main() -> ! {
let mut stdout = hio::hstdout().unwrap();
writeln!(stdout, "ionpak boot").unwrap();
board::init();
writeln!(stdout, "board initialized").unwrap();
println!(r#"
_ _
@ -103,13 +107,26 @@ fn main() -> ! {
let pe4 = board::gpio::PE4.into_output();
// MISO
let pe5 = board::gpio::PE5.into_input();
// max 2 MHz = 0.5 us
let mut delay_fn = || delay.delay_us(1u32);
let spi = board::softspi::SyncSoftSpi::new(
board::softspi::SoftSpi::new(pb5, pe4, pe5),
&mut delay_fn
);
let mut adc = ad7172::Adc::new(spi, pb4).unwrap();
loop {
let r = adc.identify();
match r {
None =>
writeln!(stdout, "Cannot identify ADC!").unwrap(),
Some(id) if id & 0xFFF0 == 0x00D0 => {
writeln!(stdout, "ADC id: {:04X}", id).unwrap();
break;
}
Some(id) =>
writeln!(stdout, "Corrupt ADC id: {:04X}", id).unwrap(),
};
}
let mut hardware_addr = EthernetAddress(board::get_mac_address());
if hardware_addr.is_multicast() {
println!("programmed MAC address is invalid, using default");
@ -166,6 +183,7 @@ fn main() -> ! {
.map(|channel| {
adc.read_data()
.map(|new_data| {
writeln!(stdout, "adc data: {:?}", new_data).unwrap();
data = new_data;
if channel == 0 {
for p in socket_pending.iter_mut() {