parent
2746c2fe00
commit
8e4fbaf80e
76
src/main.rs
76
src/main.rs
|
@ -19,7 +19,7 @@ use core::fmt::Write;
|
||||||
use cortex_m_rt::{entry, exception};
|
use cortex_m_rt::{entry, exception};
|
||||||
use stm32h7::stm32h7x3::{self as stm32, Peripherals, CorePeripherals, interrupt};
|
use stm32h7::stm32h7x3::{self as stm32, Peripherals, CorePeripherals, interrupt};
|
||||||
use cortex_m::interrupt::Mutex;
|
use cortex_m::interrupt::Mutex;
|
||||||
use heapless::{String, consts::*};
|
use heapless::{String, Vec, consts::*};
|
||||||
|
|
||||||
use smoltcp as net;
|
use smoltcp as net;
|
||||||
|
|
||||||
|
@ -647,6 +647,7 @@ fn main() -> ! {
|
||||||
let mut socket_set_entries: [_; 8] = Default::default();
|
let mut socket_set_entries: [_; 8] = Default::default();
|
||||||
let mut sockets = net::socket::SocketSet::new(&mut socket_set_entries[..]);
|
let mut sockets = net::socket::SocketSet::new(&mut socket_set_entries[..]);
|
||||||
create_socket!(sockets, tcp_rx_storage0, tcp_tx_storage0, tcp_handle0);
|
create_socket!(sockets, tcp_rx_storage0, tcp_tx_storage0, tcp_handle0);
|
||||||
|
create_socket!(sockets, tcp_rx_storage0, tcp_tx_storage0, tcp_handle1);
|
||||||
|
|
||||||
unsafe { eth::enable_interrupt(); }
|
unsafe { eth::enable_interrupt(); }
|
||||||
unsafe { cp.NVIC.set_priority(stm32::Interrupt::ETH, 196); } // mid prio
|
unsafe { cp.NVIC.set_priority(stm32::Interrupt::ETH, 196); } // mid prio
|
||||||
|
@ -667,7 +668,7 @@ fn main() -> ! {
|
||||||
{
|
{
|
||||||
let socket = &mut *sockets.get::<net::socket::TcpSocket>(tcp_handle0);
|
let socket = &mut *sockets.get::<net::socket::TcpSocket>(tcp_handle0);
|
||||||
if !(socket.is_open() || socket.is_listening()) {
|
if !(socket.is_open() || socket.is_listening()) {
|
||||||
socket.listen(80).unwrap_or_else(|e| warn!("TCP listen error: {:?}", e));
|
socket.listen(1234).unwrap_or_else(|e| warn!("TCP listen error: {:?}", e));
|
||||||
} else if last != time && socket.can_send() {
|
} else if last != time && socket.can_send() {
|
||||||
last = time;
|
last = time;
|
||||||
let s = unsafe { Status{
|
let s = unsafe { Status{
|
||||||
|
@ -677,11 +678,18 @@ fn main() -> ! {
|
||||||
x1: IIR_STATE[1][0],
|
x1: IIR_STATE[1][0],
|
||||||
y1: IIR_STATE[1][2],
|
y1: IIR_STATE[1][2],
|
||||||
}};
|
}};
|
||||||
let mut u: String<U128> = to_string(&s).unwrap();
|
send_response(socket, &s);
|
||||||
u.push('\n').unwrap();
|
|
||||||
socket.write_str(&u).unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
let socket = &mut *sockets.get::<net::socket::TcpSocket>(tcp_handle1);
|
||||||
|
if !(socket.is_open() || socket.is_listening()) {
|
||||||
|
socket.listen(1235).unwrap_or_else(|e| warn!("TCP listen error: {:?}", e));
|
||||||
|
} else {
|
||||||
|
handle_command(socket);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if !match iface.poll(&mut sockets, net::time::Instant::from_millis(time as i64)) {
|
if !match iface.poll(&mut sockets, net::time::Instant::from_millis(time as i64)) {
|
||||||
Ok(changed) => changed,
|
Ok(changed) => changed,
|
||||||
Err(net::Error::Unrecognized) => true,
|
Err(net::Error::Unrecognized) => true,
|
||||||
|
@ -692,13 +700,63 @@ fn main() -> ! {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize,Serialize)]
|
||||||
#[derive(Deserialize)]
|
struct Request {
|
||||||
struct Update {
|
channel: i8,
|
||||||
servo: i8,
|
|
||||||
iir: IIR,
|
iir: IIR,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct Response<'a> {
|
||||||
|
code: i32,
|
||||||
|
message: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn send_response<T: Serialize>(socket: &mut net::socket::TcpSocket, msg: &T) {
|
||||||
|
let mut u: String<U128> = to_string(msg).unwrap();
|
||||||
|
u.push('\n').unwrap();
|
||||||
|
socket.write_str(&u).unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn handle_command(socket: &mut net::socket::TcpSocket) {
|
||||||
|
let mut data: Vec<u8, U256> = Vec::new();
|
||||||
|
let mut discard: bool = false;
|
||||||
|
while socket.can_recv() {
|
||||||
|
if socket.recv(|buf| {
|
||||||
|
let (len, found) = match buf.iter().position(|&c| c as char == '\n') {
|
||||||
|
Some(end) => (end + 1, true),
|
||||||
|
None => (buf.len(), false),
|
||||||
|
};
|
||||||
|
if data.len() + len >= data.capacity() {
|
||||||
|
discard = true;
|
||||||
|
data.clear();
|
||||||
|
} else if !discard && len > 0 {
|
||||||
|
data.extend_from_slice(&buf[..len - 1]).unwrap();
|
||||||
|
}
|
||||||
|
(len, found)
|
||||||
|
}).unwrap() {
|
||||||
|
let resp = if discard {
|
||||||
|
discard = false;
|
||||||
|
Response{ code: 500, message: "command buffer overflow" }
|
||||||
|
} else {
|
||||||
|
match from_slice::<Request>(&data) {
|
||||||
|
Ok(request) => {
|
||||||
|
cortex_m::interrupt::free(|_| {
|
||||||
|
unsafe { IIR_CH[request.channel as usize] = request.iir; };
|
||||||
|
});
|
||||||
|
Response{ code: 200, message: "ok" }
|
||||||
|
},
|
||||||
|
Err(err) => {
|
||||||
|
warn!("parse error {}", err);
|
||||||
|
Response{ code: 550, message: "parse error" }
|
||||||
|
},
|
||||||
|
}
|
||||||
|
};
|
||||||
|
send_response(socket, &resp);
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Status {
|
struct Status {
|
||||||
|
|
Loading…
Reference in New Issue