1919 @brief Constructs an instance of the Wippersnapper SD card class.
2020*/
2121/* *************************************************************************/
22- ws_sdcard::ws_sdcard () {
22+ ws_sdcard::ws_sdcard ()
23+ : _sd_spi_cfg(WsV2.pin_sd_cs, DEDICATED_SPI, SPI_SD_CLOCK) {
2324 is_mode_offline = false ;
2425 _use_test_data = false ;
2526 _sz_cur_log_file = 0 ;
27+
28+ if (WsV2.pin_sd_cs == PIN_SD_CS_ERROR)
29+ is_mode_offline = false ;
30+
31+ if (!_sd.begin (_sd_spi_cfg)) {
32+ WS_DEBUG_PRINTLN (
33+ " SD initialization failed.\n Do not reformat the card!\n Is the card "
34+ " correctly inserted?\n Is there a wiring/soldering problem\n " );
35+ is_mode_offline = false ;
36+ } else {
37+ // Card initialized - calculate file limits
38+ is_mode_offline = true ;
39+ calculateFileLimits ();
40+ }
2641}
2742
2843/* *************************************************************************/
@@ -37,45 +52,26 @@ ws_sdcard::~ws_sdcard() {
3752 }
3853}
3954
40- /* *************************************************************************/
41- /* !
42- @brief Attempts to initialize the SD card and filesystem.
43- @returns True if the SD card was successfully initialized, False
44- otherwise.
45- */
46- /* *************************************************************************/
47- bool ws_sdcard::InitSDCard () {
48- is_mode_offline = false ;
49- csd_t csd;
50- if (WsV2.pin_sd_cs == PIN_SD_CS_ERROR)
51- return is_mode_offline;
52-
53- if (!_sd.begin (WsV2.pin_sd_cs )) {
54- WS_DEBUG_PRINTLN (
55- " SD initialization failed.\n Do not reformat the card!\n Is the card "
56- " correctly inserted?\n Is there a wiring/soldering problem\n " );
57- return is_mode_offline;
58- }
59-
55+ void ws_sdcard::calculateFileLimits () {
6056 // Calculate the maximum number of log files that can be stored on the SD card
57+ csd_t csd;
6158 if (!_sd.card ()->readCSD (&csd)) {
6259 WS_DEBUG_PRINTLN (" ERROR: Could not read sdcard information" );
63- return is_mode_offline ;
60+ return ;
6461 }
62+
6563 // get the complete sdcard capacity in bytes
6664 _sd_capacity = (uint64_t )512 * csd.capacity ();
6765 // account for 3-5% fatfs overhead utilization
6866 size_t sd_capacity_usable = _sd_capacity * (1 - 0.05 );
69- // proportionally set sz of each log file to 10% of the SD card's usable capacity
67+ // proportionally set sz of each log file to 10% of the SD card's usable
68+ // capacity
7069 _max_sz_log_file = sd_capacity_usable / 10 ;
7170 // Regardless of sd card size, cap log files to 512MB
7271 if (_max_sz_log_file > MAX_SZ_LOG_FILE) {
7372 _max_sz_log_file = MAX_SZ_LOG_FILE;
7473 }
7574 _sd_max_num_log_files = sd_capacity_usable / _max_sz_log_file;
76-
77- is_mode_offline = true ;
78- return is_mode_offline;
7975}
8076
8177/* *************************************************************************/
0 commit comments