2023-12-15 13:11:05 +08:00
|
|
|
#![cfg_attr(not(test), no_main)]
|
|
|
|
#![cfg_attr(not(test), no_std)]
|
2022-10-20 15:17:43 +08:00
|
|
|
|
|
|
|
use cortex_m_rt::entry;
|
2024-02-02 14:07:25 +08:00
|
|
|
use log::{info, debug};
|
2022-10-22 15:49:01 +08:00
|
|
|
use stm32f4xx_hal::pac::{CorePeripherals, Peripherals};
|
2022-10-20 20:57:24 +08:00
|
|
|
mod device;
|
2022-10-21 12:05:55 +08:00
|
|
|
mod laser_diode;
|
2023-12-20 12:08:48 +08:00
|
|
|
mod thermostat;
|
2024-01-31 17:16:10 +08:00
|
|
|
mod net;
|
2022-10-22 21:00:17 +08:00
|
|
|
|
2024-01-11 13:15:19 +08:00
|
|
|
use device::{boot::bootup, log_setup, sys_timer};
|
2024-03-05 16:44:03 +08:00
|
|
|
use crate::net::net::IpSettings;
|
2024-03-04 16:11:22 +08:00
|
|
|
use serde::{Deserialize, Serialize};
|
2024-02-16 16:47:17 +08:00
|
|
|
use stm32f4xx_hal::pac::SCB;
|
2024-03-04 16:11:22 +08:00
|
|
|
|
2022-10-22 21:00:17 +08:00
|
|
|
// If RTT is used, print panic info through RTT
|
2023-12-15 13:11:05 +08:00
|
|
|
#[cfg(all(feature = "RTT", not(test)))]
|
2023-12-20 12:08:48 +08:00
|
|
|
use {core::panic::PanicInfo, rtt_target::rprintln};
|
2023-12-15 13:11:05 +08:00
|
|
|
#[cfg(all(feature = "RTT", not(test)))]
|
2022-10-22 21:00:17 +08:00
|
|
|
#[panic_handler]
|
|
|
|
fn panic(info: &PanicInfo) -> ! {
|
|
|
|
rprintln!("{}", info);
|
|
|
|
loop {}
|
|
|
|
}
|
|
|
|
// Otherwise use panic halt
|
2023-12-15 13:11:05 +08:00
|
|
|
#[cfg(all(not(feature = "RTT"), not(test)))]
|
2022-10-22 21:00:17 +08:00
|
|
|
use panic_halt as _;
|
2022-10-20 15:17:43 +08:00
|
|
|
|
2024-02-02 14:07:25 +08:00
|
|
|
static mut ETH_DATA_BUFFER: [u8; 1024] = [0; 1024];
|
|
|
|
|
2024-03-04 16:11:22 +08:00
|
|
|
#[derive(Deserialize, Serialize, Clone, Copy, Debug)]
|
2024-02-19 15:08:00 +08:00
|
|
|
pub struct DeviceSettings{
|
2024-03-05 16:44:03 +08:00
|
|
|
ip_settings: IpSettings,
|
2024-02-19 15:08:00 +08:00
|
|
|
}
|
|
|
|
|
2024-03-04 16:11:22 +08:00
|
|
|
const CONFIG_KEY: [&str; 3] = ["Device", "Laser_0", "Thermostat_0"];
|
|
|
|
|
2024-03-04 11:08:26 +08:00
|
|
|
#[derive(Default)]
|
2024-03-04 16:11:22 +08:00
|
|
|
pub enum State {
|
2024-03-04 11:08:26 +08:00
|
|
|
#[default]
|
|
|
|
LoadFlashSettings,
|
|
|
|
MainLoop,
|
|
|
|
SaveFlashSettings,
|
2024-03-08 16:30:15 +08:00
|
|
|
PrepareForHardReset,
|
2024-03-04 11:08:26 +08:00
|
|
|
HardReset,
|
|
|
|
}
|
|
|
|
|
2023-12-15 13:11:05 +08:00
|
|
|
#[cfg(not(test))]
|
2022-10-20 15:17:43 +08:00
|
|
|
#[entry]
|
|
|
|
fn main() -> ! {
|
2022-10-22 21:00:17 +08:00
|
|
|
log_setup::init_log();
|
|
|
|
info!("Kirdy init");
|
|
|
|
|
2022-10-22 15:49:01 +08:00
|
|
|
let core_perif = CorePeripherals::take().unwrap();
|
2022-10-20 20:57:24 +08:00
|
|
|
let perif = Peripherals::take().unwrap();
|
|
|
|
|
2024-03-04 16:11:22 +08:00
|
|
|
let (mut wd, mut flash_store, mut laser, mut thermostat,) = bootup(core_perif, perif);
|
2024-02-16 16:47:17 +08:00
|
|
|
|
2024-02-19 15:08:00 +08:00
|
|
|
let mut device_settings = DeviceSettings {
|
2024-03-05 16:44:03 +08:00
|
|
|
ip_settings: IpSettings::default()
|
2024-02-19 15:08:00 +08:00
|
|
|
};
|
|
|
|
|
2024-03-19 17:38:29 +08:00
|
|
|
let mut active_report: [bool; net::net::NUM_OF_SOCKETS] = [false; net::net::NUM_OF_SOCKETS];
|
|
|
|
|
2024-03-04 11:08:26 +08:00
|
|
|
let mut state = State::default();
|
|
|
|
|
2022-10-20 20:57:24 +08:00
|
|
|
loop {
|
|
|
|
wd.feed();
|
2024-01-24 17:03:06 +08:00
|
|
|
|
2024-03-06 12:15:08 +08:00
|
|
|
net::net::eth_poll_and_update_link_speed();
|
|
|
|
|
2024-03-04 11:08:26 +08:00
|
|
|
match state {
|
|
|
|
State::LoadFlashSettings => {
|
|
|
|
// State Transition
|
|
|
|
state = State::MainLoop;
|
2024-03-04 16:11:22 +08:00
|
|
|
|
|
|
|
wd.feed();
|
|
|
|
let device_settings_flash: DeviceSettings;
|
|
|
|
match flash_store.read_value(CONFIG_KEY[0]) {
|
|
|
|
Ok(Some(config)) => {
|
|
|
|
device_settings_flash = config;
|
|
|
|
debug!("Found Device Settings");
|
|
|
|
}
|
|
|
|
Ok(None) => {
|
|
|
|
debug!("Flash does not have Device Settings");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
debug!("Cannot Store Flash: {:?}", e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wd.feed();
|
|
|
|
let laser_settings: laser_diode::laser_diode::LdSettingsSummary;
|
|
|
|
match flash_store.read_value(CONFIG_KEY[1]) {
|
|
|
|
Ok(Some(config)) => {
|
|
|
|
laser_settings = config;
|
|
|
|
debug!("Found Laser Diode Settings");
|
|
|
|
}
|
|
|
|
Ok(None) => {
|
|
|
|
debug!("Does not have laser diode Settings");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
debug!("Cannot Store Flash: {:?}", e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wd.feed();
|
|
|
|
let thermostat_settings: thermostat::thermostat::ThermostatSettingsSummary;
|
|
|
|
match flash_store.read_value(CONFIG_KEY[2]) {
|
|
|
|
Ok(Some(config)) => {
|
|
|
|
thermostat_settings = config;
|
|
|
|
debug!("Found Thermostat Settings");
|
|
|
|
}
|
|
|
|
Ok(None) => {
|
|
|
|
debug!("Does not have thermostat Settings");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
debug!("Cannot Store Flash: {:?}", e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wd.feed();
|
|
|
|
device_settings = device_settings_flash;
|
|
|
|
thermostat.load_settings_from_summary(thermostat_settings);
|
|
|
|
laser.load_settings_from_summary(laser_settings);
|
2024-03-04 11:08:26 +08:00
|
|
|
}
|
|
|
|
State::MainLoop => {
|
|
|
|
let mut eth_is_pending = false;
|
2024-02-16 16:47:17 +08:00
|
|
|
|
2024-03-04 11:08:26 +08:00
|
|
|
laser.poll_and_update_output_current();
|
2024-02-16 16:47:17 +08:00
|
|
|
|
2024-03-04 11:08:26 +08:00
|
|
|
if thermostat.poll_adc() {
|
|
|
|
thermostat.update_pid();
|
|
|
|
if thermostat.get_temp_mon_status().over_temp_alarm {
|
|
|
|
laser.power_down();
|
|
|
|
thermostat.set_pid_engaged(false);
|
|
|
|
thermostat.power_down();
|
|
|
|
}
|
2024-03-06 16:29:02 +08:00
|
|
|
|
2024-03-19 17:38:29 +08:00
|
|
|
net::net::for_each(|mut socket, id| {
|
|
|
|
if net::net::eth_is_socket_active(socket) && net::net::eth_is_socket_connected(socket) {
|
|
|
|
if active_report[id] {
|
2024-03-06 16:29:02 +08:00
|
|
|
unsafe {
|
|
|
|
net::cmd_handler::send_status_report(&mut ETH_DATA_BUFFER, &mut laser, &mut thermostat, &mut socket);
|
|
|
|
}
|
2024-03-04 11:08:26 +08:00
|
|
|
}
|
2024-02-19 15:08:00 +08:00
|
|
|
}
|
2024-03-19 17:38:29 +08:00
|
|
|
else {
|
|
|
|
active_report[id] = false;
|
|
|
|
}
|
2024-03-06 16:29:02 +08:00
|
|
|
});
|
2024-03-20 14:31:29 +08:00
|
|
|
|
|
|
|
thermostat.start_tec_readings_conversion();
|
2024-02-19 15:08:00 +08:00
|
|
|
}
|
2024-03-20 14:25:35 +08:00
|
|
|
cortex_m::interrupt::free(|cs|
|
|
|
|
{
|
|
|
|
eth_is_pending = net::net::is_pending(cs);
|
|
|
|
net::net::clear_pending(cs);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
if eth_is_pending {
|
|
|
|
net::net::for_each(|mut socket, id| {
|
|
|
|
if net::net::eth_is_socket_active(socket) && net::net::eth_is_socket_connected(socket){
|
2024-03-06 16:29:02 +08:00
|
|
|
unsafe{
|
|
|
|
let bytes = net::net::eth_recv(&mut ETH_DATA_BUFFER, socket);
|
|
|
|
if bytes != 0 {
|
|
|
|
info!("Ts: {:?}", sys_timer::now());
|
|
|
|
debug!("Number of bytes recv: {:?}", bytes);
|
|
|
|
// State Transition
|
2024-03-19 17:38:29 +08:00
|
|
|
net::cmd_handler::execute_cmd(&mut ETH_DATA_BUFFER, bytes, &mut socket, &mut laser, &mut thermostat, &mut state, &mut device_settings, &mut active_report[id]);
|
2024-03-06 16:29:02 +08:00
|
|
|
}
|
2024-03-04 11:08:26 +08:00
|
|
|
}
|
|
|
|
}
|
2024-03-20 14:25:35 +08:00
|
|
|
})
|
|
|
|
};
|
2024-02-02 14:07:25 +08:00
|
|
|
}
|
2024-03-04 11:08:26 +08:00
|
|
|
State::SaveFlashSettings => {
|
|
|
|
// State Transition
|
|
|
|
state = State::MainLoop;
|
2024-03-04 16:11:22 +08:00
|
|
|
|
|
|
|
wd.feed();
|
|
|
|
let mut store_value_buf = [0u8; 1024];
|
|
|
|
match flash_store.write_value(CONFIG_KEY[0], &device_settings, &mut store_value_buf) {
|
|
|
|
Ok(()) => {
|
|
|
|
debug!("Device Settings is stored in flash");
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
debug!("Cannot Store Flash: {:?}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wd.feed();
|
|
|
|
match flash_store.write_value(CONFIG_KEY[1], &laser.get_settings_summary(), &mut store_value_buf) {
|
|
|
|
Ok(()) => {
|
|
|
|
debug!("Laser Diode Settings is stored in flash");
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
debug!("Cannot Store Flash: {:?}", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wd.feed();
|
|
|
|
match flash_store.write_value(CONFIG_KEY[2], &thermostat.get_settings_summary(), &mut store_value_buf) {
|
|
|
|
Ok(()) => {
|
|
|
|
debug!("Thermostat Settings is stored in flash");
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
debug!("Cannot Store Flash: {:?}", e);
|
|
|
|
}
|
|
|
|
}
|
2024-02-19 15:08:00 +08:00
|
|
|
}
|
2024-03-08 16:30:15 +08:00
|
|
|
State::PrepareForHardReset => {
|
|
|
|
// State Transition
|
|
|
|
state = State::HardReset;
|
|
|
|
|
|
|
|
wd.feed();
|
|
|
|
laser.power_down();
|
|
|
|
thermostat.power_down();
|
2024-03-19 17:38:29 +08:00
|
|
|
net::net::for_each(|mut socket, _| {
|
2024-03-08 16:30:15 +08:00
|
|
|
if net::net::eth_is_socket_active(socket) {
|
|
|
|
unsafe {
|
|
|
|
net::cmd_handler::send_response(&mut ETH_DATA_BUFFER, net::cmd_handler::ResponseEnum::HardReset, None, &mut socket);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2024-03-04 11:08:26 +08:00
|
|
|
State::HardReset => {
|
|
|
|
wd.feed();
|
|
|
|
laser.power_down();
|
|
|
|
thermostat.power_down();
|
|
|
|
let mut any_socket_alive = false;
|
2024-03-19 17:38:29 +08:00
|
|
|
net::net::for_each(|socket, _| {
|
2024-03-06 16:29:02 +08:00
|
|
|
if net::net::eth_is_socket_active(socket) {
|
|
|
|
net::net::eth_close_socket(socket);
|
2024-03-19 17:38:29 +08:00
|
|
|
any_socket_alive = true;
|
2024-03-06 16:29:02 +08:00
|
|
|
}
|
|
|
|
});
|
2024-03-04 11:08:26 +08:00
|
|
|
|
|
|
|
// Must let loop run for one more cycle to poll server for RST to be sent,
|
|
|
|
// this makes sure system does not reset right after socket.abort() is called.
|
|
|
|
if !any_socket_alive {
|
|
|
|
SCB::sys_reset();
|
|
|
|
}
|
2024-02-16 16:47:17 +08:00
|
|
|
}
|
2024-02-02 14:07:25 +08:00
|
|
|
}
|
2022-10-20 20:57:24 +08:00
|
|
|
}
|
2022-10-22 15:49:01 +08:00
|
|
|
}
|