MCPcopy Create free account
hub / github.com/SmingHub/Sming / SDCard_begin

Function SDCard_begin

Sming/Libraries/SDCard/SDCard.cpp:50–100  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

48}; // namespace
49
50bool SDCard_begin(uint8_t slaveSelect, uint32_t freqLimit)
51{
52 chipSelect = slaveSelect;
53 digitalWrite(chipSelect, HIGH);
54 pinMode(chipSelect, OUTPUT);
55 digitalWrite(chipSelect, HIGH);
56
57 if(SDCardSPI == nullptr) {
58 debug_e("SDCard SPI object not created.");
59 return false;
60 }
61
62 auto& settings = SDCardSPI->SPIDefaultSettings;
63
64 settings.dataMode = SPI_MODE0;
65
66 spiInitFreq = 1000000U;
67 if(freqLimit > 0 && freqLimit < spiInitFreq) {
68 spiInitFreq = freqLimit;
69 }
70
71 // Note: Some SD cards struggle at 40MHz
72 settings.speed = std::min(freqLimit, uint32_t(40000000));
73
74 if(!SDCardSPI->begin()) {
75 debug_e("SDCard SPI init failed");
76 return false;
77 }
78
79 /* This must be allocated for the whole program life ~512Bytes*/
80 pFatFs = new FATFS;
81 if(pFatFs == nullptr) {
82 debug_e("No heap for pFatFs");
83 return false;
84 }
85
86 /* Give a work area to the default drive */
87 FRESULT mountRes = f_mount(pFatFs, "", 0);
88 if(FR_OK != mountRes) {
89 debug_e("f_mount: FAIL %d", mountRes);
90 return false;
91 }
92
93 /* open dummy file to force card init */
94 FIL file;
95 if(FR_OK == f_open(&file, "dummy", FA_READ)) {
96 f_close(&file);
97 }
98
99 return true;
100}
101
102namespace
103{

Callers 1

sdInitFunction · 0.85

Calls 7

minFunction · 0.85
f_mountFunction · 0.85
f_openFunction · 0.85
f_closeFunction · 0.85
digitalWriteFunction · 0.50
pinModeFunction · 0.50
beginMethod · 0.45

Tested by

no test coverage detected