2017-08-06 19:52:11 +08:00
|
|
|
#![feature(used, const_fn, core_float, asm, lang_items)]
|
2017-05-04 17:35:26 +08:00
|
|
|
#![no_std]
|
|
|
|
|
|
|
|
extern crate cortex_m;
|
|
|
|
extern crate cortex_m_rt;
|
2017-08-06 12:59:30 +08:00
|
|
|
#[macro_use(interrupt)]
|
2017-05-04 17:35:26 +08:00
|
|
|
extern crate tm4c129x;
|
2017-08-02 00:33:33 +08:00
|
|
|
extern crate smoltcp;
|
2017-05-04 17:35:26 +08:00
|
|
|
|
2017-05-06 17:17:41 +08:00
|
|
|
use core::cell::{Cell, RefCell};
|
2017-05-09 13:16:00 +08:00
|
|
|
use core::fmt;
|
2017-05-06 17:17:41 +08:00
|
|
|
use cortex_m::interrupt::Mutex;
|
2017-08-05 15:51:54 +08:00
|
|
|
use smoltcp::Error;
|
|
|
|
use smoltcp::wire::{EthernetAddress, IpAddress};
|
|
|
|
use smoltcp::iface::{ArpCache, SliceArpCache, EthernetInterface};
|
|
|
|
use smoltcp::socket::{AsSocket, SocketSet};
|
|
|
|
use smoltcp::socket::{TcpSocket, TcpSocketBuffer};
|
2017-05-04 17:35:26 +08:00
|
|
|
|
2017-05-11 14:55:00 +08:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! print {
|
|
|
|
($($arg:tt)*) => ({
|
|
|
|
use core::fmt::Write;
|
|
|
|
write!($crate::UART0, $($arg)*).unwrap()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! println {
|
|
|
|
($fmt:expr) => (print!(concat!($fmt, "\n")));
|
|
|
|
($fmt:expr, $($arg:tt)*) => (print!(concat!($fmt, "\n"), $($arg)*));
|
|
|
|
}
|
|
|
|
|
2017-08-06 19:52:11 +08:00
|
|
|
#[no_mangle]
|
|
|
|
#[lang = "panic_fmt"]
|
|
|
|
fn panic_fmt(args: core::fmt::Arguments, file: &'static str, line: u32) -> ! {
|
|
|
|
println!("panic at {}:{}: {}", file, line, args);
|
|
|
|
loop {}
|
|
|
|
}
|
|
|
|
|
2017-05-11 14:55:00 +08:00
|
|
|
#[macro_use]
|
2017-05-09 15:57:54 +08:00
|
|
|
mod board;
|
2017-08-02 00:33:33 +08:00
|
|
|
mod ethmac;
|
2017-05-06 17:17:41 +08:00
|
|
|
mod pid;
|
2017-05-09 15:57:54 +08:00
|
|
|
mod loop_anode;
|
|
|
|
mod loop_cathode;
|
2017-05-11 23:15:01 +08:00
|
|
|
mod electrometer;
|
2017-08-05 15:51:54 +08:00
|
|
|
mod http;
|
|
|
|
mod pages;
|
2017-05-06 17:17:41 +08:00
|
|
|
|
2017-08-06 11:37:24 +08:00
|
|
|
static ADC_IRQ_COUNT: Mutex<Cell<u64>> = Mutex::new(Cell::new(0));
|
2017-05-11 14:55:00 +08:00
|
|
|
|
2017-08-06 11:37:24 +08:00
|
|
|
fn get_time_ms() -> u64 {
|
|
|
|
let adc_irq_count = cortex_m::interrupt::free(|cs| {
|
|
|
|
ADC_IRQ_COUNT.borrow(cs).get()
|
|
|
|
});
|
|
|
|
adc_irq_count*6/125
|
2017-05-11 14:55:00 +08:00
|
|
|
}
|
2017-05-06 17:17:41 +08:00
|
|
|
|
2017-05-09 15:57:54 +08:00
|
|
|
static LOOP_ANODE: Mutex<RefCell<loop_anode::Controller>> = Mutex::new(RefCell::new(
|
|
|
|
loop_anode::Controller::new()));
|
2017-05-06 12:32:13 +08:00
|
|
|
|
2017-05-09 15:57:54 +08:00
|
|
|
static LOOP_CATHODE: Mutex<RefCell<loop_cathode::Controller>> = Mutex::new(RefCell::new(
|
|
|
|
loop_cathode::Controller::new()));
|
2017-05-09 13:16:00 +08:00
|
|
|
|
2017-05-11 23:15:01 +08:00
|
|
|
static ELECTROMETER: Mutex<RefCell<electrometer::Electrometer>> = Mutex::new(RefCell::new(
|
|
|
|
electrometer::Electrometer::new()));
|
|
|
|
|
2017-05-09 13:16:00 +08:00
|
|
|
|
|
|
|
pub struct UART0;
|
|
|
|
|
|
|
|
impl fmt::Write for UART0 {
|
|
|
|
fn write_str(&mut self, s: &str) -> Result<(), fmt::Error> {
|
|
|
|
for c in s.bytes() {
|
|
|
|
unsafe {
|
|
|
|
let uart_0 = tm4c129x::UART0.get();
|
|
|
|
while (*uart_0).fr.read().txff().bit() {}
|
|
|
|
(*uart_0).dr.write(|w| w.data().bits(c))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-05 15:51:54 +08:00
|
|
|
const TCP_RX_BUFFER_SIZE: usize = 256;
|
|
|
|
const TCP_TX_BUFFER_SIZE: usize = 8192;
|
|
|
|
|
|
|
|
|
|
|
|
macro_rules! create_socket_storage {
|
|
|
|
($rx_storage:ident, $tx_storage:ident) => (
|
|
|
|
let mut $rx_storage = [0; TCP_RX_BUFFER_SIZE];
|
|
|
|
let mut $tx_storage = [0; TCP_TX_BUFFER_SIZE];
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
macro_rules! create_socket {
|
|
|
|
($set:ident, $rx_storage:ident, $tx_storage:ident, $target:ident) => (
|
|
|
|
let tcp_rx_buffer = TcpSocketBuffer::new(&mut $rx_storage[..]);
|
|
|
|
let tcp_tx_buffer = TcpSocketBuffer::new(&mut $tx_storage[..]);
|
|
|
|
let tcp_socket = TcpSocket::new(tcp_rx_buffer, tcp_tx_buffer);
|
|
|
|
let $target = $set.add(tcp_socket);
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-05-04 17:35:26 +08:00
|
|
|
fn main() {
|
2017-05-09 15:57:54 +08:00
|
|
|
board::init();
|
2017-05-04 19:42:22 +08:00
|
|
|
|
2017-05-09 15:57:54 +08:00
|
|
|
cortex_m::interrupt::free(|cs| {
|
2017-05-10 00:10:52 +08:00
|
|
|
let mut loop_anode = LOOP_ANODE.borrow(cs).borrow_mut();
|
|
|
|
let mut loop_cathode = LOOP_CATHODE.borrow(cs).borrow_mut();
|
2017-06-08 21:50:39 +08:00
|
|
|
|
2017-05-17 00:06:31 +08:00
|
|
|
// ZJ-10
|
|
|
|
let anode = 165.0;
|
|
|
|
let cathode_bias = 50.0;
|
|
|
|
let emission = 0.5e-3;
|
2017-06-08 21:50:39 +08:00
|
|
|
|
2017-05-17 00:06:31 +08:00
|
|
|
// ZJ-27
|
2017-06-08 21:50:39 +08:00
|
|
|
/*let anode = 225.0;
|
2017-05-17 00:06:31 +08:00
|
|
|
let cathode_bias = 25.0;
|
2017-06-08 21:50:39 +08:00
|
|
|
let emission = 1.0e-3;*/
|
|
|
|
|
2017-05-17 00:06:31 +08:00
|
|
|
// ZJ-12
|
2017-06-07 15:06:50 +08:00
|
|
|
/*let anode = 200.0;
|
2017-05-17 00:06:31 +08:00
|
|
|
let cathode_bias = 50.0;
|
2017-06-07 15:06:50 +08:00
|
|
|
let emission = 4.0e-3;*/
|
2017-06-08 21:50:39 +08:00
|
|
|
|
|
|
|
// G8130
|
|
|
|
/*let anode = 180.0;
|
2017-06-07 15:06:50 +08:00
|
|
|
let cathode_bias = 30.0;
|
2017-06-08 21:50:39 +08:00
|
|
|
let emission = 4.0e-3;*/
|
|
|
|
|
2017-05-17 00:06:31 +08:00
|
|
|
loop_anode.set_target(anode);
|
|
|
|
loop_cathode.set_emission_target(emission);
|
2017-05-10 00:10:52 +08:00
|
|
|
loop_cathode.set_bias_target(cathode_bias);
|
2017-05-04 17:35:26 +08:00
|
|
|
});
|
2017-05-06 12:33:38 +08:00
|
|
|
|
2017-05-11 14:55:00 +08:00
|
|
|
println!(r#"
|
|
|
|
_ _
|
|
|
|
(_) | |
|
|
|
|
_ ___ _ __ _ __ __ _| |
|
|
|
|
| |/ _ \| '_ \| '_ \ / _` | |/ /
|
|
|
|
| | (_) | | | | |_) | (_| | <
|
|
|
|
|_|\___/|_| |_| .__/ \__,_|_|\_\
|
|
|
|
| |
|
|
|
|
|_|
|
2017-08-05 15:51:54 +08:00
|
|
|
"#);
|
|
|
|
|
2017-08-06 19:12:10 +08:00
|
|
|
//let hardware_addr = EthernetAddress(board::get_mac_address());
|
|
|
|
let hardware_addr = EthernetAddress([0x10, 0xE2, 0xD5, 0x22, 0x23, 0x12]);
|
2017-08-05 15:51:54 +08:00
|
|
|
let mut protocol_addrs = [IpAddress::v4(192, 168, 69, 1)];
|
|
|
|
println!("MAC {} IP {}", hardware_addr, protocol_addrs[0]);
|
|
|
|
let mut arp_cache_entries: [_; 8] = Default::default();
|
|
|
|
let mut arp_cache = SliceArpCache::new(&mut arp_cache_entries[..]);
|
2017-08-06 02:18:33 +08:00
|
|
|
let mut device = ethmac::EthernetDevice::new(hardware_addr);
|
2017-08-05 15:51:54 +08:00
|
|
|
let mut iface = EthernetInterface::new(
|
|
|
|
&mut device, &mut arp_cache as &mut ArpCache,
|
|
|
|
hardware_addr, &mut protocol_addrs[..]);
|
|
|
|
|
|
|
|
create_socket_storage!(tcp_rx_storage0, tcp_tx_storage0);
|
|
|
|
create_socket_storage!(tcp_rx_storage1, tcp_tx_storage1);
|
|
|
|
create_socket_storage!(tcp_rx_storage2, tcp_tx_storage2);
|
|
|
|
create_socket_storage!(tcp_rx_storage3, tcp_tx_storage3);
|
|
|
|
create_socket_storage!(tcp_rx_storage4, tcp_tx_storage4);
|
|
|
|
create_socket_storage!(tcp_rx_storage5, tcp_tx_storage5);
|
|
|
|
create_socket_storage!(tcp_rx_storage6, tcp_tx_storage6);
|
|
|
|
create_socket_storage!(tcp_rx_storage7, tcp_tx_storage7);
|
|
|
|
|
|
|
|
let mut socket_set_entries: [_; 8] = Default::default();
|
|
|
|
let mut sockets = SocketSet::new(&mut socket_set_entries[..]);
|
|
|
|
|
|
|
|
create_socket!(sockets, tcp_rx_storage0, tcp_tx_storage0, tcp_handle0);
|
|
|
|
create_socket!(sockets, tcp_rx_storage1, tcp_tx_storage1, tcp_handle1);
|
|
|
|
create_socket!(sockets, tcp_rx_storage2, tcp_tx_storage2, tcp_handle2);
|
|
|
|
create_socket!(sockets, tcp_rx_storage3, tcp_tx_storage3, tcp_handle3);
|
|
|
|
create_socket!(sockets, tcp_rx_storage4, tcp_tx_storage4, tcp_handle4);
|
|
|
|
create_socket!(sockets, tcp_rx_storage5, tcp_tx_storage5, tcp_handle5);
|
|
|
|
create_socket!(sockets, tcp_rx_storage6, tcp_tx_storage6, tcp_handle6);
|
|
|
|
create_socket!(sockets, tcp_rx_storage7, tcp_tx_storage7, tcp_handle7);
|
|
|
|
|
|
|
|
let mut sessions = [
|
|
|
|
(http::Request::new(), tcp_handle0),
|
|
|
|
(http::Request::new(), tcp_handle1),
|
|
|
|
(http::Request::new(), tcp_handle2),
|
|
|
|
(http::Request::new(), tcp_handle3),
|
|
|
|
(http::Request::new(), tcp_handle4),
|
|
|
|
(http::Request::new(), tcp_handle5),
|
|
|
|
(http::Request::new(), tcp_handle6),
|
|
|
|
(http::Request::new(), tcp_handle7),
|
|
|
|
];
|
2017-05-11 14:55:00 +08:00
|
|
|
|
2017-08-06 19:06:47 +08:00
|
|
|
board::start_adc();
|
|
|
|
|
2017-05-11 14:55:00 +08:00
|
|
|
let mut next_blink = 0;
|
|
|
|
let mut next_info = 0;
|
|
|
|
let mut led_state = true;
|
|
|
|
let mut latch_reset_time = None;
|
2017-05-06 12:33:38 +08:00
|
|
|
loop {
|
2017-08-06 11:37:24 +08:00
|
|
|
let time = get_time_ms();
|
2017-05-11 14:55:00 +08:00
|
|
|
|
2017-08-05 15:51:54 +08:00
|
|
|
for &mut(ref mut request, ref tcp_handle) in sessions.iter_mut() {
|
|
|
|
let socket: &mut TcpSocket = sockets.get_mut(*tcp_handle).as_socket();
|
|
|
|
if !socket.is_open() {
|
|
|
|
socket.listen(80).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
if socket.may_recv() {
|
|
|
|
let request_status = {
|
|
|
|
let data = socket.recv(TCP_RX_BUFFER_SIZE).unwrap();
|
|
|
|
request.input(data)
|
|
|
|
};
|
|
|
|
match request_status {
|
|
|
|
Ok(true) => {
|
|
|
|
if socket.can_send() {
|
|
|
|
pages::serve(socket, &request);
|
|
|
|
}
|
|
|
|
request.reset();
|
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
Ok(false) => (),
|
|
|
|
Err(err) => {
|
|
|
|
println!("failed HTTP request: {}", err);
|
|
|
|
request.reset();
|
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if socket.may_send() {
|
|
|
|
request.reset();
|
|
|
|
socket.close();
|
|
|
|
}
|
|
|
|
}
|
2017-08-06 11:37:24 +08:00
|
|
|
match iface.poll(&mut sockets, time) {
|
2017-08-05 15:51:54 +08:00
|
|
|
Ok(()) | Err(Error::Exhausted) => (),
|
|
|
|
Err(e) => println!("poll error: {}", e)
|
|
|
|
}
|
|
|
|
|
2017-05-11 14:55:00 +08:00
|
|
|
if time > next_blink {
|
|
|
|
led_state = !led_state;
|
2017-08-06 11:37:24 +08:00
|
|
|
next_blink = time + 500;
|
2017-05-11 14:55:00 +08:00
|
|
|
board::set_led(1, led_state);
|
|
|
|
}
|
|
|
|
|
2017-06-12 19:22:23 +08:00
|
|
|
if time >= next_info {
|
|
|
|
let (anode, cathode, electrometer) = cortex_m::interrupt::free(|cs| {
|
|
|
|
(LOOP_ANODE.borrow(cs).borrow().get_status(),
|
|
|
|
LOOP_CATHODE.borrow(cs).borrow().get_status(),
|
|
|
|
ELECTROMETER.borrow(cs).borrow().get_status())
|
|
|
|
});
|
|
|
|
|
|
|
|
println!("");
|
|
|
|
anode.debug_print();
|
|
|
|
cathode.debug_print();
|
|
|
|
electrometer.debug_print();
|
|
|
|
if cathode.fbi.is_some() && electrometer.ic.is_some() {
|
|
|
|
let fbi = cathode.fbi.unwrap();
|
|
|
|
let ic = electrometer.ic.unwrap();
|
|
|
|
let pressure = ic/fbi/18.75154;
|
|
|
|
println!("{:.1e} mbar", pressure);
|
|
|
|
}
|
|
|
|
|
2017-08-06 11:37:24 +08:00
|
|
|
next_info = next_info + 1000;
|
2017-05-11 14:55:00 +08:00
|
|
|
}
|
|
|
|
|
2017-08-05 15:51:54 +08:00
|
|
|
board::process_errors();
|
2017-05-11 14:55:00 +08:00
|
|
|
if board::error_latched() {
|
|
|
|
match latch_reset_time {
|
|
|
|
None => {
|
|
|
|
println!("Protection latched");
|
2017-08-06 11:37:24 +08:00
|
|
|
latch_reset_time = Some(time + 5000);
|
2017-05-11 14:55:00 +08:00
|
|
|
}
|
|
|
|
Some(t) => if time > t {
|
|
|
|
latch_reset_time = None;
|
|
|
|
cortex_m::interrupt::free(|cs| {
|
|
|
|
// reset PID loops as they have accumulated large errors
|
|
|
|
// while the protection was active, which would cause
|
|
|
|
// unnecessary overshoots.
|
|
|
|
LOOP_ANODE.borrow(cs).borrow_mut().reset();
|
|
|
|
LOOP_CATHODE.borrow(cs).borrow_mut().reset();
|
2017-05-11 23:15:01 +08:00
|
|
|
board::reset_error();
|
2017-05-11 14:55:00 +08:00
|
|
|
});
|
|
|
|
println!("Protection reset");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-05-06 12:33:38 +08:00
|
|
|
}
|
2017-05-04 17:35:26 +08:00
|
|
|
}
|
|
|
|
|
2017-08-06 12:59:30 +08:00
|
|
|
interrupt!(ADC0SS0, adc0_ss0);
|
|
|
|
fn adc0_ss0() {
|
2017-05-05 19:31:12 +08:00
|
|
|
cortex_m::interrupt::free(|cs| {
|
|
|
|
let adc0 = tm4c129x::ADC0.borrow(cs);
|
|
|
|
if adc0.ostat.read().ov0().bit() {
|
|
|
|
panic!("ADC FIFO overflowed")
|
|
|
|
}
|
2017-05-07 00:00:01 +08:00
|
|
|
adc0.isc.write(|w| w.in0().bit(true));
|
2017-05-05 19:31:12 +08:00
|
|
|
|
2017-05-11 14:55:00 +08:00
|
|
|
let ic_sample = adc0.ssfifo0.read().data().bits();
|
2017-05-09 15:57:54 +08:00
|
|
|
let fbi_sample = adc0.ssfifo0.read().data().bits();
|
|
|
|
let fv_sample = adc0.ssfifo0.read().data().bits();
|
|
|
|
let fd_sample = adc0.ssfifo0.read().data().bits();
|
|
|
|
let av_sample = adc0.ssfifo0.read().data().bits();
|
|
|
|
let fbv_sample = adc0.ssfifo0.read().data().bits();
|
|
|
|
|
|
|
|
let mut loop_anode = LOOP_ANODE.borrow(cs).borrow_mut();
|
|
|
|
let mut loop_cathode = LOOP_CATHODE.borrow(cs).borrow_mut();
|
2017-05-11 23:15:01 +08:00
|
|
|
let mut electrometer = ELECTROMETER.borrow(cs).borrow_mut();
|
2017-05-09 15:57:54 +08:00
|
|
|
loop_anode.adc_input(av_sample);
|
|
|
|
loop_cathode.adc_input(fbi_sample, fd_sample, fv_sample, fbv_sample);
|
2017-05-11 23:15:01 +08:00
|
|
|
electrometer.adc_input(ic_sample);
|
2017-05-09 15:57:54 +08:00
|
|
|
|
2017-08-06 11:37:24 +08:00
|
|
|
let adc_irq_count = ADC_IRQ_COUNT.borrow(cs);
|
|
|
|
adc_irq_count.set(adc_irq_count.get() + 1);
|
2017-05-10 00:10:52 +08:00
|
|
|
});
|
2017-05-05 19:31:12 +08:00
|
|
|
}
|