19
19
@brief Constructs an instance of the Wippersnapper SD card class.
20
20
*/
21
21
/* *************************************************************************/
22
- ws_sdcard::ws_sdcard () {
22
+ ws_sdcard::ws_sdcard ()
23
+ : _sd_spi_cfg(WsV2.pin_sd_cs, DEDICATED_SPI, SPI_SD_CLOCK) {
23
24
is_mode_offline = false ;
24
25
_use_test_data = false ;
25
26
_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
+ }
26
41
}
27
42
28
43
/* *************************************************************************/
@@ -37,45 +52,26 @@ ws_sdcard::~ws_sdcard() {
37
52
}
38
53
}
39
54
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 () {
60
56
// Calculate the maximum number of log files that can be stored on the SD card
57
+ csd_t csd;
61
58
if (!_sd.card ()->readCSD (&csd)) {
62
59
WS_DEBUG_PRINTLN (" ERROR: Could not read sdcard information" );
63
- return is_mode_offline ;
60
+ return ;
64
61
}
62
+
65
63
// get the complete sdcard capacity in bytes
66
64
_sd_capacity = (uint64_t )512 * csd.capacity ();
67
65
// account for 3-5% fatfs overhead utilization
68
66
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
70
69
_max_sz_log_file = sd_capacity_usable / 10 ;
71
70
// Regardless of sd card size, cap log files to 512MB
72
71
if (_max_sz_log_file > MAX_SZ_LOG_FILE) {
73
72
_max_sz_log_file = MAX_SZ_LOG_FILE;
74
73
}
75
74
_sd_max_num_log_files = sd_capacity_usable / _max_sz_log_file;
76
-
77
- is_mode_offline = true ;
78
- return is_mode_offline;
79
75
}
80
76
81
77
/* *************************************************************************/
0 commit comments