Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
ky28059 committed Feb 10, 2024
2 parents c62482a + ffc78a9 commit 9f571db
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 14 deletions.
24 changes: 13 additions & 11 deletions application_processor/src/ap_functions.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::str::from_utf8;
use bytemuck::checked::{try_from_bytes, CheckedCastError};
use core::str::{from_utf8, Utf8Error};
use bytemuck::checked::{try_pod_read_unaligned, CheckedCastError};
use bytemuck::{from_bytes_mut, Pod, Zeroable};
use max78000_hal::prelude::*;
use max78000_hal::i2c::{I2cError, MAX_I2C_MESSAGE_LEN};
Expand All @@ -20,14 +20,16 @@ pub enum ApError {
InvalidInput,
#[error("Command not recognized")]
InvalidCommand,
#[error("Message with invalid utf8 recieved: {0}")]
InvalidUtf8(#[from] Utf8Error),
}

// Data structure for sending commands to component
// Params allows for up to MAX_I2C_MESSAGE_LEN - 1 bytes to be send
// along with the opcode through board_link. This is not utilized by the example
// design but can be utilized by your design.
#[repr(C)]
#[derive(Zeroable, Pod, Copy, Clone)]
#[derive(Debug, Zeroable, Pod, Copy, Clone)]
struct CommandMessage {
opcode: u8,
params: [u8; MAX_I2C_MESSAGE_LEN-1]
Expand Down Expand Up @@ -80,11 +82,11 @@ pub fn scan_components(i2c: &mut I2cController) -> Result<(), ApError> {
command.opcode = ComponentCommand::ComponentCmdScan as u8;

// Send out command and receive result
let Ok(response) = i2c.send_and_receive_packet(addr, &transmit_buffer) else {
let Ok(response) = i2c.send_and_receive_packet(addr, &transmit_buffer[..1]) else {
continue;
};

let scan = try_from_bytes::<ScanMessage>(response)?;
let scan = try_pod_read_unaligned::<ScanMessage>(response)?;

// sucess, repsonce is long enough for scan message, so device is present
uprintln_info!("F>0x{:08x}", scan.component_id);
Expand All @@ -109,9 +111,9 @@ pub fn validate_components(i2c: &mut I2cController) -> Result<(), ApError> {
let command = from_bytes_mut::<CommandMessage>(&mut transmit_buffer);
command.opcode = ComponentCommand::ComponentCmdValidate as u8;

let response = i2c.send_and_receive_packet(addr, &transmit_buffer)?;
let response = i2c.send_and_receive_packet(addr, &transmit_buffer[..1])?;

let validate_message = try_from_bytes::<ValidateMessage>(response)?;
let validate_message = try_pod_read_unaligned::<ValidateMessage>(response)?;

if validate_message.component_id != flash_data.component_ids[i] {
uprintln_error!("Component ID: 0x{:08x} invalid", flash_data.component_ids[i]);
Expand All @@ -136,14 +138,14 @@ pub fn boot_components(i2c: &mut I2cController) -> Result<(), ApError> {
let command = from_bytes_mut::<CommandMessage>(&mut transmit_buffer);
command.opcode = ComponentCommand::ComponentCmdBoot as u8;

let response = i2c.send_and_receive_packet(addr, &transmit_buffer)?;
let response = i2c.send_and_receive_packet(addr, &transmit_buffer[..1])?;

// Print boot message from component
uprintln_info!(
"0x{:x}>{}",
flash_data.component_ids[i],
// TODO: see if this panic will cause a problem (might halt board when we don't want to)
from_utf8(response).expect("Receive buffer not valid utf8 str")
from_utf8(response)?,
);
}

Expand All @@ -161,12 +163,12 @@ pub fn attest_component(i2c: &mut I2cController, component_id: u32) -> Result<()
command.opcode = ComponentCommand::ComponentCmdAttest as u8;

// Send out command and receive result
let response = i2c.send_and_receive_packet(addr, &transmit_buffer)?;
let response = i2c.send_and_receive_packet(addr, &transmit_buffer[..1])?;

// Print out attestation data
uprintln_info!("C>0x{:08x}", component_id);
// TODO: see if this panic will cause a problem (might halt board when we don't want to)
uprintln_info!("{}", from_utf8(response).expect("Receive buffer not valid utf8 str"));
uprintln_info!("{}", from_utf8(response)?);

Ok(())
}
2 changes: 1 addition & 1 deletion application_processor/src/flash_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub static FLASH_DATA: Mutex<RefCell<Option<FlashData>>> = Mutex::new(RefCell::n

/// Datatype for information stored in flash
#[repr(C)]
#[derive(Default, Clone, Copy, Pod, Zeroable)]
#[derive(Debug, Default, Clone, Copy, Pod, Zeroable)]
pub struct FlashData {
pub(crate) flash_magic: u32,
pub(crate) component_cnt: u32,
Expand Down
4 changes: 3 additions & 1 deletion application_processor/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ pub extern "C" fn main(_argc: isize, _argv: *const *const u8) -> ! {
continue;
};

let result = match command.trim() {
let command = command.trim();

let result = match command {
"list" => scan_components(&mut i2c),
"boot" => attempt_boot(&mut i2c),
"replace" => attempt_replace(),
Expand Down
5 changes: 4 additions & 1 deletion max78000_hal/src/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use crate::bindings::{
IRQn_Type_I2C2_IRQn,
MXC_Delay,
};
use crate::prelude::*;


const MXC_BASE_I2C0: u32 = 0x4001D000u32;
Expand Down Expand Up @@ -115,12 +116,14 @@ impl I2cController {
}

pub fn read_register(addr: I2cAddr, reg: I2cRegister, buf: &mut [u8]) -> Result<(), I2cError> {
let mut reg_value = reg as u8;

// FIXME; make sure none of the lens overflow
let mut request = mxc_i2c_req_t {
i2c: I2C_INTERFACE as *mut mxc_i2c_regs_t,
addr: addr as u32,
tx_len: 1,
tx_buf: &mut (reg as u8) as *mut u8,
tx_buf: &mut reg_value as *mut u8,
rx_len: buf.len() as u32,
rx_buf: buf.as_mut_ptr(),
restart: 0,
Expand Down

0 comments on commit 9f571db

Please sign in to comment.