Skip to content

Commit

Permalink
fixed the timer
Browse files Browse the repository at this point in the history
  • Loading branch information
Athryx committed Mar 4, 2024
1 parent 6722c48 commit d71acef
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 7 deletions.
10 changes: 5 additions & 5 deletions application_processor/src/ap_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()]);
Expand Down
109 changes: 109 additions & 0 deletions log_serial.py
Original file line number Diff line number Diff line change
@@ -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 = (
"<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | "
"{extra[extra]: <6} | "
"<level>{level: <8}</level> | "
"<level>{message}</level> "
)

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()

14 changes: 12 additions & 2 deletions max78000_hal/src/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
}
}

0 comments on commit d71acef

Please sign in to comment.