-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdcard.rs
More file actions
145 lines (129 loc) · 5.08 KB
/
sdcard.rs
File metadata and controls
145 lines (129 loc) · 5.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
this doesn't work yet
*/
#![no_std]
#![no_main]
#![deny(
clippy::mem_forget,
reason = "mem::forget is generally not safe to do with esp_hal types, especially those \
holding buffers for the duration of a data transfer."
)]
use alloc::rc::Rc;
use alloc::string::String;
use core::cell::RefCell;
// use embedded_hal_bus::spi::ExclusiveDevice;
use embedded_hal_bus::spi::RefCellDevice;
use embedded_sdmmc::Mode::ReadOnly;
use embedded_sdmmc::{SdCard, TimeSource, Timestamp, VolumeIdx, VolumeManager};
use esp_hal::clock::CpuClock;
use esp_hal::delay::Delay;
use esp_hal::gpio::Level::High;
use esp_hal::gpio::{Input, InputConfig, Output, OutputConfig, Pull};
use esp_hal::main;
use esp_hal::spi::master::{Config as SpiConfig, Spi};
use esp_hal::time::{Duration, Instant, Rate};
use log::info;
#[panic_handler]
fn panic(_: &core::panic::PanicInfo) -> ! {
loop {}
}
// This creates a default app-descriptor required by the esp-idf bootloader.
// For more information see: <https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/system/app_image_format.html#application-description>
esp_bootloader_esp_idf::esp_app_desc!();
extern crate alloc;
/// A dummy timesource, which is mostly important for creating files.
#[derive(Default)]
pub struct DummyTimesource();
impl TimeSource for DummyTimesource {
// In theory you could use the RTC of the rp2040 here, if you had
// any external time synchronizing device.
fn get_timestamp(&self) -> Timestamp {
Timestamp {
year_since_1970: 0,
zero_indexed_month: 0,
zero_indexed_day: 0,
hours: 0,
minutes: 0,
seconds: 0,
}
}
}
#[main]
fn main() -> ! {
esp_println::logger::init_logger_from_env();
let config = esp_hal::Config::default().with_cpu_clock(CpuClock::max());
let peripherals = esp_hal::init(config);
esp_alloc::heap_allocator!(size: 72 * 1024);
let BOARD_POWERON = peripherals.GPIO10;
let BOARD_SDCARD_CS = peripherals.GPIO39;
let RADIO_CS_PIN = peripherals.GPIO9;
let BOARD_TFT_CS = peripherals.GPIO12;
let board_spi_miso = peripherals.GPIO38;
let board_spi_sck = peripherals.GPIO40;
let board_spi_mosi = peripherals.GPIO41;
info!("running");
let mut delay = Delay::new();
let mut board_power = Output::new(BOARD_POWERON, High, OutputConfig::default());
board_power.set_high();
delay.delay_millis(2000);
info!("connecting to the bus");
let sdmmc_cs = Output::new(BOARD_SDCARD_CS, High, OutputConfig::default());
let radio_cs = Output::new(RADIO_CS_PIN, High, OutputConfig::default());
let board_tft = Output::new(BOARD_TFT_CS, High, OutputConfig::default());
let board_spi_miso = Input::new(board_spi_miso, InputConfig::default().with_pull(Pull::Up));
let sdmmc_spi_bus = Spi::new(
peripherals.SPI2,
SpiConfig::default().with_frequency(Rate::from_mhz(40)), // .with_mode()
// .with_clock_source(SpiClockSource::LSI)
)
.unwrap()
.with_sck(board_spi_sck)
.with_miso(board_spi_miso)
.with_mosi(board_spi_mosi);
// let sdmmc_spi = SpiDevice::new(sdmmc_spi_bus, sdmmc_cs);
let spi_delay = Delay::new();
let sdmmc_spi_bus = RefCell::new(sdmmc_spi_bus);
let sdmmc_spi = RefCellDevice::new(&sdmmc_spi_bus, sdmmc_cs, spi_delay)
.expect("failed to create spi device");
// let sdmmc_spi =
// ExclusiveDevice::new_no_delay(sdmmc_spi_bus, sdmmc_cs).expect("Failed to create SpiDevice");
info!("open the card");
let card = SdCard::new(sdmmc_spi, delay);
info!("size of card in bytes: {}", card.num_bytes().unwrap());
info!("type of card: {:?}", card.get_card_type());
info!("opening the volume manager");
let mut volume_mgr = VolumeManager::new(card, DummyTimesource {});
info!("getting volume 0");
match volume_mgr.open_volume(VolumeIdx(0)) {
Ok(volume) => {
info!("opened the volume {:?}", volume);
let root_dir = volume.open_root_dir().unwrap();
info!("root dir is {:?}", root_dir);
root_dir
.iterate_dir(|de| {
info!("dir entry {:?} is {} bytes", de.name, de.size);
})
.unwrap();
info!("reading the file README.MD");
let my_file = root_dir.open_file_in_dir("README.MD", ReadOnly).unwrap();
// read 32 bytes at a time until the file ends
while !my_file.is_eof() {
let mut buffer = [0u8; 32];
let num_read = my_file.read(&mut buffer).unwrap();
let slice = &buffer[0..num_read];
let line = String::from_utf8_lossy(slice);
info!("{}", line);
}
my_file.close().unwrap();
root_dir.close().unwrap();
volume.close().unwrap();
}
Err(err) => {
info!("failed to open the volume {:?}", err);
}
}
loop {
let delay_start = Instant::now();
while delay_start.elapsed() < Duration::from_millis(5000) {}
}
}