Skip to content

Commit

Permalink
moved device config onto flash
Browse files Browse the repository at this point in the history
  • Loading branch information
gferraro committed Nov 13, 2024
1 parent 0696b58 commit 9aa3990
Show file tree
Hide file tree
Showing 8 changed files with 279 additions and 30 deletions.
22 changes: 22 additions & 0 deletions src/byte_slice_cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ impl<'a> CursorMut<'a> {
let len = self.pos.min(self.inner.as_mut().len());
&mut self.inner.as_mut()[len..]
}

pub fn data(&mut self) -> &mut [u8] {
&mut self.inner.as_mut()[..self.pos]
}

pub fn write_bytes(&mut self, bytes: &[u8]) -> fmt::Result {
// Skip over already-copied data
let remainder = &mut self.inner[self.pos..];
// Check if there is space remaining (return error instead of panicking)
if remainder.len() < bytes.len() {
return Err(core::fmt::Error);
}
// Make the two slices the same length
let remainder = &mut remainder[..bytes.len()];
// Copy
remainder.copy_from_slice(bytes);

// Update offset to avoid overwriting
self.pos += bytes.len();

Ok(())
}
}

impl<'a> ErrorType for Cursor<'a> {
Expand Down
5 changes: 3 additions & 2 deletions src/core0_audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ pub fn audio_task(
PullDown,
>,
) -> ! {
let mut device_config = DeviceConfig::load_existing_config_from_flash().unwrap();

watchdog.feed();

let core = unsafe { pac::CorePeripherals::steal() };
Expand Down Expand Up @@ -138,6 +136,9 @@ pub fn audio_task(
flash_storage.take_spi(peripherals.SPI1, &mut peripherals.RESETS, clock_freq.Hz());
flash_storage.init();

let mut device_config =
DeviceConfig::load_existing_config_from_flash(&mut flash_storage).unwrap();

let (pio1, _, sm1, _, _) = peripherals.PIO1.split(&mut peripherals.RESETS);
let mut delay = Delay::new(core.SYST, clock_freq);
let mut shared_i2c = SharedI2C::new(i2c_config, unlocked_pin, &mut delay);
Expand Down
9 changes: 6 additions & 3 deletions src/core1_sub_tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::device_config::DeviceConfig;
use crate::event_logger::{EventLogger, LoggerEvent, LoggerEventKind};
use crate::ext_spi_transfers::{ExtSpiTransfers, ExtTransferMessage};
use crate::onboard_flash::OnboardFlash;
use crate::rp2040_flash::write_device_config_to_rp2040_flash;
// use crate::rp2040_flash::write_device_config_to_rp2040_flash;
use crate::FIRMWARE_VERSION;
use byteorder::{ByteOrder, LittleEndian};
use cortex_m::delay::Delay;
Expand Down Expand Up @@ -407,8 +407,11 @@ pub fn get_existing_device_config_or_config_from_pi_on_initial_handshake(

new_config_bytes[length_used..length_used + 2400]
.copy_from_slice(&new_config.motion_detection_mask.inner);
let slice_to_write = &new_config_bytes[0..length_used + 2400];
write_device_config_to_rp2040_flash(slice_to_write);
let mut slice_to_write = &mut new_config_bytes[0..length_used + 2400];
if let Some(spi_free) = pi_spi.disable() {
flash_storage.take_spi(spi_free, resets, clock_freq);
}
flash_storage.write_device_config(&mut slice_to_write);
new_config.cursor_position += 2400;
config_was_updated = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/core1_task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ pub fn core_1_task(
let radiometry_enabled = sio.fifo.read_blocking();
info!("Core 1 got radiometry enabled: {}", radiometry_enabled == 2);
let lepton_version = if radiometry_enabled == 2 { 35 } else { 3 };
let existing_config = DeviceConfig::load_existing_config_from_flash();
let existing_config = DeviceConfig::load_existing_config_from_flash(&mut flash_storage);

if let Some(existing_config) = &existing_config {
info!("Existing config {:#?}", existing_config.config());
Expand Down
33 changes: 23 additions & 10 deletions src/device_config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::byte_slice_cursor::Cursor;
use crate::motion_detector::DetectionMask;
use crate::rp2040_flash::read_device_config_from_rp2040_flash;
use crate::onboard_flash::OnboardFlash;
use crate::{byte_slice_cursor::Cursor, onboard_flash};
// use crate::rp2040_flash::read_device_config_from_rp2040_flash;
use crate::sun_times::sun_times;
use chrono::{Duration, NaiveDateTime, NaiveTime, Timelike};
use defmt::{info, Format, Formatter};
Expand Down Expand Up @@ -270,16 +271,28 @@ impl Default for DeviceConfig {
}

impl DeviceConfig {
pub fn load_existing_config_from_flash() -> Option<DeviceConfig> {
let slice = read_device_config_from_rp2040_flash();
let device_config = DeviceConfig::from_bytes(slice);
device_config
pub fn load_existing_config_from_flash(
flash_storage: &mut OnboardFlash,
) -> Option<DeviceConfig> {
let slice = flash_storage.read_device_config();
if let Ok(slice) = slice {
let device_config = DeviceConfig::from_bytes(&slice);
device_config
} else {
None
}
}

pub fn load_existing_inner_config_from_flash() -> Option<(DeviceConfigInner, usize)> {
let slice = read_device_config_from_rp2040_flash();
let device_config = DeviceConfig::inner_from_bytes(slice);
device_config
pub fn load_existing_inner_config_from_flash(
flash_storage: &mut OnboardFlash,
) -> Option<(DeviceConfigInner, usize)> {
let slice = flash_storage.read_device_config();
if let Ok(slice) = slice {
let device_config = DeviceConfig::inner_from_bytes(&slice);
device_config
} else {
None
}
}

pub fn inner_from_bytes(bytes: &[u8]) -> Option<(DeviceConfigInner, usize)> {
Expand Down
69 changes: 60 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,55 @@ impl FrameBuffer {
}
}

use crate::onboard_flash::OnboardFlash;
use rp2040_hal::dma::DMAExt;

#[entry]
fn main() -> ! {
info!("Startup tc2-firmware {}", FIRMWARE_VERSION);
// TODO: Check wake_en and sleep_en registers to make sure we're not enabling any clocks we don't need.
let mut peripherals: Peripherals = Peripherals::take().unwrap();
let mut config = DeviceConfig::load_existing_inner_config_from_flash();
let mut is_audio: bool = config.is_some() && config.as_mut().unwrap().0.is_audio_device();
let sio = Sio::new(peripherals.SIO);
let dma_channels = peripherals.DMA.split(&mut peripherals.RESETS);
let pins = rp2040_hal::gpio::Pins::new(
peripherals.IO_BANK0,
peripherals.PADS_BANK0,
sio.gpio_bank0,
&mut peripherals.RESETS,
);
let core1 = Core1Pins {
pi_ping: pins.gpio5.into_pull_down_input(),

pi_miso: pins.gpio15.into_floating_disabled(),
pi_mosi: pins.gpio12.into_floating_disabled(),
pi_cs: pins.gpio13.into_floating_disabled(),
pi_clk: pins.gpio14.into_floating_disabled(),

fs_cs: pins.gpio9.into_push_pull_output(),
fs_miso: pins.gpio8.into_pull_down_disabled().into_pull_type(),
fs_mosi: pins.gpio11.into_pull_down_disabled().into_pull_type(),
fs_clk: pins.gpio10.into_pull_down_disabled().into_pull_type(),
};

// init flash
let mut flash_page_buf = [0xffu8; 4 + 2048 + 128];
let mut flash_page_buf_2 = [0xffu8; 4 + 2048 + 128];
let flash_page_buf = unsafe { extend_lifetime_generic_mut(&mut flash_page_buf) };
let flash_page_buf_2 = unsafe { extend_lifetime_generic_mut(&mut flash_page_buf_2) };

let mut flash_storage = OnboardFlash::new(
core1.fs_cs,
core1.fs_mosi,
core1.fs_clk,
core1.fs_miso,
flash_page_buf,
flash_page_buf_2,
dma_channels.ch1,
dma_channels.ch2,
true,
None,
);

let (clocks, rosc) = clock_utils::setup_rosc_as_system_clock(
peripherals.CLOCKS,
peripherals.XOSC,
Expand All @@ -132,6 +174,16 @@ fn main() -> ! {
clocks.system_clock.freq().to_MHz()
);

flash_storage.take_spi(
peripherals.SPI1,
&mut peripherals.RESETS,
system_clock_freq.Hz(),
);
flash_storage.init();

let mut config = DeviceConfig::load_existing_inner_config_from_flash(&mut flash_storage);
let mut is_audio: bool = config.is_some() && config.as_mut().unwrap().0.is_audio_device();

// Watchdog ticks are required to run the timer peripheral, since they're shared between both.

let mut watchdog = bsp::hal::Watchdog::new(peripherals.WATCHDOG);
Expand All @@ -145,14 +197,13 @@ fn main() -> ! {

let core = pac::CorePeripherals::take().unwrap();
let mut delay = Delay::new(core.SYST, system_clock_freq);
let sio = Sio::new(peripherals.SIO);

let pins = rp2040_hal::gpio::Pins::new(
peripherals.IO_BANK0,
peripherals.PADS_BANK0,
sio.gpio_bank0,
&mut peripherals.RESETS,
);
// let pins = rp2040_hal::gpio::Pins::new(
// peripherals.IO_BANK0,
// peripherals.PADS_BANK0,
// sio.gpio_bank0,
// &mut peripherals.RESETS,
// );

// Attiny + RTC comms
let sda_pin = pins.gpio6.into_function::<FunctionI2C>();
Expand Down
Loading

0 comments on commit 9aa3990

Please sign in to comment.