Compare commits
No commits in common. "82ef0df46461412eda390c04f7d8b06c98fefacc" and "a6927ac39e631eb549e327f17a3662561f0f12b4" have entirely different histories.
82ef0df464
...
a6927ac39e
Binary file not shown.
72
src/main.rs
72
src/main.rs
|
@ -21,14 +21,14 @@ use embassy_rp::spi::Spi;
|
|||
use embassy_rp::{gpio, spi};
|
||||
use embassy_sync::blocking_mutex::raw::ThreadModeRawMutex;
|
||||
use embassy_sync::channel::Channel;
|
||||
use embedded_hal_bus::spi::{ExclusiveDevice, NoDelay};
|
||||
use embedded_sdmmc::sdcard::{self, SdCard};
|
||||
use embedded_hal_bus::spi::ExclusiveDevice;
|
||||
use embedded_sdmmc::sdcard::SdCard;
|
||||
use embedded_sdmmc::{Block, BlockDevice, BlockIdx, File, VolumeIdx};
|
||||
use gpio::{Level, Output};
|
||||
use {defmt_rtt as _, panic_probe as _};
|
||||
|
||||
use embassy_rp::i2c::{self, Config};
|
||||
use embassy_time::{Delay, Duration, Timer};
|
||||
use embassy_time::{Duration, Timer};
|
||||
|
||||
bind_interrupts!(struct Irqs {
|
||||
I2C1_IRQ => i2c::InterruptHandler<embassy_rp::peripherals::I2C1>;
|
||||
|
@ -112,7 +112,6 @@ async fn main(spawner: Spawner) {
|
|||
loop {
|
||||
let read_value_if_available = async {
|
||||
let status = co2::get_status(&mut i2c).await?;
|
||||
info!("Sensor status was: {}", status);
|
||||
// Bit 3 is `DATA_READY`
|
||||
if status & 0b00001000u8 != 0 {
|
||||
let measured_value = co2::get_measurement(&mut i2c).await?;
|
||||
|
@ -153,7 +152,7 @@ async fn write_to_sd(spi1: SPI1, pin10: PIN_10, pin11: PIN_11, pin12: PIN_12, pi
|
|||
let mut config = spi::Config::default();
|
||||
config.frequency = 400_000;
|
||||
let spi = Spi::new_blocking(spi1, pin10, pin11, pin12, config);
|
||||
let cs = Output::new(pin16, Level::Low);
|
||||
let cs = Output::new(pin16, Level::High);
|
||||
let spi_dev = ExclusiveDevice::new_no_delay(spi, cs);
|
||||
|
||||
let sdcard = SdCard::new(spi_dev, embassy_time::Delay);
|
||||
|
@ -187,26 +186,35 @@ async fn write_to_sd(spi1: SPI1, pin10: PIN_10, pin11: PIN_11, pin12: PIN_12, pi
|
|||
.unwrap();
|
||||
|
||||
// Open the root directory (mutably borrows from the volume).
|
||||
let mut root_dir = volume0.open_root_dir().unwrap();
|
||||
|
||||
let mut file_count: usize = 0;
|
||||
root_dir.iterate_dir(|_| file_count += 1).unwrap();
|
||||
info!("Found {} files", file_count);
|
||||
|
||||
// ShortFileName::FILENAME_MAX_LEN == 11. Therefore, we can't go any longer
|
||||
// However, this should be fine as we have 8 chars => 10^8 = 100000000
|
||||
// possible files.
|
||||
let mut file_name_buffer = [0; 11];
|
||||
let mut cursor = Cursor::new(&mut file_name_buffer[..]);
|
||||
core::write!(&mut cursor, "{}.txt", file_count + 1)
|
||||
.expect("File count + extension should definetly fit into 11 bytes");
|
||||
let final_position = cursor.position() as usize;
|
||||
let file_name = core::str::from_utf8(&file_name_buffer[..final_position])
|
||||
.expect("We only put ASCII into the buffer");
|
||||
|
||||
let (len, file_name_buffer) = generate_file_name(&mut volume0);
|
||||
let file_name = Some(
|
||||
core::str::from_utf8(&file_name_buffer[..len]).expect("We only put ASCII into the buffer"),
|
||||
);
|
||||
info!("Creating new text file: {}", file_name);
|
||||
|
||||
let mut root_dir = volume0.open_root_dir().unwrap();
|
||||
let mut file = root_dir
|
||||
.open_file_in_dir(
|
||||
file_name.unwrap(),
|
||||
embedded_sdmmc::Mode::ReadWriteCreateOrAppend,
|
||||
)
|
||||
.open_file_in_dir(file_name, embedded_sdmmc::Mode::ReadWriteCreateOrAppend)
|
||||
.unwrap();
|
||||
loop {
|
||||
let new_value = MEASUREMENT_VALUES.receive().await;
|
||||
|
||||
info!(
|
||||
"Writing measurement value {} to file {}",
|
||||
new_value, file_name
|
||||
"Writing measurement value {} to file {}.TXT",
|
||||
new_value,
|
||||
file_count + 1
|
||||
);
|
||||
|
||||
// Space for five characters to fit numbers up to 65535 + '\n'
|
||||
|
@ -222,43 +230,13 @@ async fn write_to_sd(spi1: SPI1, pin10: PIN_10, pin11: PIN_11, pin12: PIN_12, pi
|
|||
};
|
||||
if let Err(e) = write_flush() {
|
||||
error!(
|
||||
"Failed to write measuement: {:?}.\nTrying to reinitialize card...",
|
||||
"Failed to write measuement: {:?}.\nIgnoring missing value...",
|
||||
defmt::Debug2Format(&e)
|
||||
);
|
||||
sdcard.mark_card_uninit();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type SdCardConcrete<'a, 'b> = SdCard<
|
||||
ExclusiveDevice<Spi<'a, SPI1, embassy_rp::spi::Blocking>, Output<'b, PIN_16>, NoDelay>,
|
||||
Delay,
|
||||
>;
|
||||
|
||||
/// Generate a file name for the new data file.
|
||||
///
|
||||
/// The generated name is just going to be "<number-of-files-in-volume>.TXT"
|
||||
pub fn generate_file_name(
|
||||
volume: &mut embedded_sdmmc::Volume<'_, SdCardConcrete, DummyTimesource, 4, 4, 1>,
|
||||
) -> (usize, [u8; 11]) {
|
||||
// Open the root directory (mutably borrows from the volume).
|
||||
let mut root_dir = volume.open_root_dir().unwrap();
|
||||
|
||||
let mut file_count: usize = 0;
|
||||
root_dir.iterate_dir(|_| file_count += 1).unwrap();
|
||||
info!("Found {} files", file_count);
|
||||
|
||||
// ShortFileName::FILENAME_MAX_LEN == 11. Therefore, we can't go any longer
|
||||
// However, this should be fine as we have 8 chars => 10^8 = 100000000
|
||||
// possible files.
|
||||
let mut file_name_buffer = [0; 11];
|
||||
let mut cursor = Cursor::new(&mut file_name_buffer[..]);
|
||||
core::write!(&mut cursor, "{}.txt", file_count + 1)
|
||||
.expect("File count + extension should definetly fit into 11 bytes");
|
||||
let final_position = cursor.position() as usize;
|
||||
(final_position, file_name_buffer)
|
||||
}
|
||||
|
||||
pub fn valid_volumes<D>(block_device: &mut D) -> [Option<(VolumeIdx, [u8; 11])>; 4]
|
||||
where
|
||||
D: BlockDevice,
|
||||
|
|
Loading…
Reference in a new issue