diff --git a/Cargo.lock b/Cargo.lock index 7551fec..fd0b65c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1100,6 +1100,7 @@ dependencies = [ "embassy-time", "embedded-io-async", "embedded-nal-async", + "nb 1.1.0", "panic-probe", "portable-atomic", "rand", diff --git a/debug-listener/src/main.rs b/debug-listener/src/main.rs index 00c8c08..bf1316d 100644 --- a/debug-listener/src/main.rs +++ b/debug-listener/src/main.rs @@ -6,7 +6,7 @@ const PORT_NAME: &str = "/dev/cu.usbserial-150"; fn open_serial_port() -> serialport::Result> { serialport::new(PORT_NAME, 19_200) - .timeout(Duration::from_secs(120)) + .timeout(Duration::from_secs(60 * 5)) .data_bits(DataBits::Eight) .stop_bits(StopBits::One) .parity(Parity::Even) @@ -47,6 +47,15 @@ async fn main() -> serialport::Result<()> { "message {count} - bytes_read: ({:?}) {:?} {:#b} {:#b} ", bytes_read, buffer, buffer[0], buffer[1] ); + + // Wait as the fan would wait too + tokio::time::sleep(std::time::Duration::from_secs(1)).await; + // Echo + let result = port.write(&buffer[..bytes_read]); + println!("Echoed message back {:?}", result); + let result = port.flush(); + println!("Flushed message back {:?}", result); + // let inverse: Vec = buffer.iter().map(|byte| byte.to_be()).collect::>(); // println!("message {count} - inverse: {:?}", inverse); count += 1; diff --git a/fan-controller/Cargo.toml b/fan-controller/Cargo.toml index be51ae4..77b2fab 100644 --- a/fan-controller/Cargo.toml +++ b/fan-controller/Cargo.toml @@ -19,6 +19,7 @@ embassy-sync = "0.6.0" embassy-time = { version = "0.3.1", features = ["defmt"] } embedded-io-async = "0.6.1" embedded-nal-async = "0.7.1" +nb = "1.1.0" panic-probe = "0.3.2" portable-atomic = { version = "1.7", features = ["critical-section"] } rand = { version = "0.8.5", default-features = false } diff --git a/fan-controller/README.md b/fan-controller/README.md new file mode 100644 index 0000000..24c9b0b --- /dev/null +++ b/fan-controller/README.md @@ -0,0 +1,2 @@ +# How to run +install [probe.rs](https://probe.rs) `cargo run` diff --git a/fan-controller/src/fan.rs b/fan-controller/src/fan.rs index 8769a16..10821ea 100644 --- a/fan-controller/src/fan.rs +++ b/fan-controller/src/fan.rs @@ -3,7 +3,7 @@ use crate::modbus; use cortex_m::prelude::_embedded_hal_serial_Write; -use defmt::{info, Format}; +use defmt::{error, info, Format}; use embassy_rp::dma::Channel; use embassy_rp::gpio::{Level, Output, Pin}; use embassy_rp::interrupt::typelevel::Binding; @@ -71,7 +71,7 @@ impl<'a, UART: uart::Instance, PIN: Pin> FanClient<'a, UART, PIN> { if set_point > MAX_SET_POINT { return; } - + // Send update through UART to MAX845 to modbus fans // Form message to fan 1 let mut message: [u8; 8] = [ @@ -100,12 +100,13 @@ impl<'a, UART: uart::Instance, PIN: Pin> FanClient<'a, UART, PIN> { // Set pin setting DE (driver enable) to on (high) on the MAX845 to send data self.driver_enable.set_high(); let result = self.uart.write(&message).await; - info!("uart result: {:?}", result); + info!("uart write result: {:?}", result); // Before closing we need to flush the buffer to ensure that all data is written - let result = self.uart.flush(); + // This requires blocking or we get a WouldBlock error. I don't understand why (TODO) + let result = self.uart.blocking_flush(); if let Err(error) = result { - info!("uart flush error"); + error!("uart flush error"); } // Wait to avoid cutting off last byte when turning off driver enable @@ -116,6 +117,7 @@ impl<'a, UART: uart::Instance, PIN: Pin> FanClient<'a, UART, PIN> { // Read response from fan 1 let mut response_buffer: [u8; 8] = [0; 8]; + info!("Waiting for response from fan 1"); let response = self.uart.read(&mut response_buffer).await; info!("response from fan 1: {:?} {:?}", response, response_buffer); //TODO validate response from fan 1 @@ -139,7 +141,7 @@ impl<'a, UART: uart::Instance, PIN: Pin> FanClient<'a, UART, PIN> { info!("uart result: {:?}", result); // Before closing we need to flush the buffer to ensure that all data is written - let result = self.uart.flush(); + let result = self.uart.blocking_flush(); if let Err(error) = result { info!("uart flush error"); } diff --git a/fan-controller/src/main.rs b/fan-controller/src/main.rs index de65a9a..e1aff14 100644 --- a/fan-controller/src/main.rs +++ b/fan-controller/src/main.rs @@ -387,7 +387,7 @@ async fn mqtt_task( } static PING_RESPONSE: Signal = Signal::new(); - /// Callback handler for [listen](crate::listen) + /// Callback handler for pings received when listening async fn handle_ping_response(ping_response: PingResponse) { info!("Received ping response"); PING_RESPONSE.signal(ping_response);