From d71aceff48a22b4f4712ecde91ca46b7efc4e292 Mon Sep 17 00:00:00 2001 From: jack Date: Mon, 4 Mar 2024 01:02:21 -0500 Subject: [PATCH] fixed the timer --- application_processor/src/ap_driver.rs | 10 +-- log_serial.py | 109 +++++++++++++++++++++++++ max78000_hal/src/timer.rs | 14 +++- 3 files changed, 126 insertions(+), 7 deletions(-) create mode 100644 log_serial.py diff --git a/application_processor/src/ap_driver.rs b/application_processor/src/ap_driver.rs index 9342f15..6cb0a48 100644 --- a/application_processor/src/ap_driver.rs +++ b/application_processor/src/ap_driver.rs @@ -106,15 +106,15 @@ impl ApDriver { fn recieve_packet(&mut self, address: I2cAddr) -> Result<&[u8], ApError> { let mut recv_len = 0; - loop { - // delay to allow component to get work done while requesting response, and delay is needed between next read - self.sleep(Duration::from_millis(10)); + self.sleep(Duration::from_millis(3)); + loop { self.i2c.recv(address, slice::from_mut(&mut recv_len))?; - if recv_len != 0 { - self.sleep(Duration::from_millis(5)); + // delay to allow component to get work done while requesting response, and delay is needed between next read + self.sleep(Duration::from_millis(5)); + if recv_len != 0 { self.i2c.recv(address, &mut self.i2c_recv_buffer[..recv_len.into()])?; return Ok(&self.i2c_recv_buffer[..recv_len.into()]); diff --git a/log_serial.py b/log_serial.py new file mode 100644 index 0000000..13c77a9 --- /dev/null +++ b/log_serial.py @@ -0,0 +1,109 @@ +# @file list_tool.py +# @author Frederich Stine +# @brief host tool for listing installed components +# @date 2024 +# +# This source file is part of an example system for MITRE's 2024 Embedded CTF (eCTF). +# This code is being provided only for educational purposes for the 2024 MITRE eCTF +# competition, and may not meet MITRE standards for quality. Use this code at your +# own risk! +# +# @copyright Copyright (c) 2024 The MITRE Corporation + +import argparse +import serial +import re +from loguru import logger +import sys + +# Logger formatting +fmt = ( + "{time:YYYY-MM-DD HH:mm:ss.SSS} | " + "{extra[extra]: <6} | " + "{level: <8} | " + "{message} " +) + +logger.remove(0) +logger.add(sys.stdout, format=fmt) + + +# List function +def list(args): + ser = serial.Serial( + port=args.application_processor, + baudrate=115200, + parity=serial.PARITY_NONE, + stopbits=serial.STOPBITS_ONE, + bytesize=serial.EIGHTBITS, + ) + + # Send command + output = "" + # Receive messages until done + while True: + byte = ser.read() + char = byte.decode("utf-8") + output += char + output = process_output(output) + + +def process_output(output): + # Find INFO level messages + match = re.search("%info: ((.|\n|\r)*?)%", output) + if match != None: + # Output all of the data and remove from buffer + output = output[:match.start()] + output[match.end():] + for line in match.group(1).strip().split('\n'): + logger.bind(extra="OUTPUT").info(line.strip()) + # Find DEBUG level messages + match = re.search("%debug: ((.|\n|\r)*?)%", output) + if match != None: + # Output all of the data and remove from buffer + output = output[:match.start()] + output[match.end():] + for line in match.group(1).strip().split('\n'): + logger.bind(extra="OUTPUT").debug(line.strip()) + # Find ACK level messages + match = re.search("%ack%", output) + if match != None: + # Ignore + output = output[:match.start()] + output[match.end():] + # Find SUCCESS level messages + match = re.search("%success: ((.|\n|\r)*?)%", output) + if match != None: + # Output all of the data and remove from buffer + output = output[:match.start()] + output[match.end():] + for line in match.group(1).strip().split('\n'): + logger.bind(extra="OUTPUT").success(line.strip()) + exit(0) + # Find ERROR level messages + match = re.search("%error: ((.|\n|\r)*?)%", output) + if match != None: + # Output all of the data and remove from buffer + output = output[:match.start()] + output[match.end():] + for line in match.group(1).strip().split('\n'): + logger.bind(extra="OUTPUT").error(line.strip()) + exit(1) + # Return the spliced output + return output + + +# Main function +def main(): + parser = argparse.ArgumentParser( + prog="eCTF List Host Tool", + description="List the components connected to the medical device", + ) + + parser.add_argument( + "-a", "--application-processor", required=True, help="Serial device of the AP" + ) + + args = parser.parse_args() + + list(args) + + +if __name__ == "__main__": + main() + diff --git a/max78000_hal/src/timer.rs b/max78000_hal/src/timer.rs index 3f72ce2..971a30f 100644 --- a/max78000_hal/src/timer.rs +++ b/max78000_hal/src/timer.rs @@ -23,21 +23,31 @@ impl Timer { } pub fn sleep(&mut self, duration: Duration) { + // NOTE: this systick thing counts down, not up + let start_tick = SYST::get_current(); + let usecs = duration.as_micros() as u64; let sysclock_freq = Gcr::with(|gcr| gcr.get_sysclock_frequency()) as u64; // total number of ticks we need to wait for - let delay_ticks = ((usecs * sysclock_freq) / 1000000) as u32 + SYST::get_current(); + let delay_ticks = ((usecs * sysclock_freq) / 1000000) as u32; let mut wrap_count = delay_ticks / SYSTICK_RELOAD_VAL; let tick_count = delay_ticks % SYSTICK_RELOAD_VAL; + let end_tick = if tick_count > start_tick { + wrap_count += 1; + (SYSTICK_RELOAD_VAL - tick_count) + start_tick + } else { + start_tick - tick_count + }; + while wrap_count > 0 { if self.systick.has_wrapped() { wrap_count -= 1; } } - while SYST::get_current() < tick_count && !self.systick.has_wrapped() {} + while SYST::get_current() > end_tick && !self.systick.has_wrapped() {} } } \ No newline at end of file