Skip to content

Commit 29a6527

Browse files
committed
WebServer: Break out print routines, beginSD
1 parent 712bae3 commit 29a6527

File tree

3 files changed

+206
-208
lines changed

3 files changed

+206
-208
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
const int pin_microSD_CS = 25;
2+
3+
typedef struct struct_settings {
4+
bool enableSD = true;
5+
uint16_t spiFrequency = 16; //By default, use 16MHz SPI
6+
} Settings;
7+
8+
Settings settings;
9+
10+
const TickType_t fatSemaphore_shortWait_ms = 10 / portTICK_PERIOD_MS;
11+
const TickType_t fatSemaphore_longWait_ms = 200 / portTICK_PERIOD_MS;
12+
SemaphoreHandle_t xFATSemaphore;
13+
14+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
15+
void displaySDFail(uint16_t displayTime)
16+
{
17+
Serial.println ("SD card failed to initialize!");
18+
}
19+
20+
//Create a test file in file structure to make sure we can
21+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
22+
bool createTestFile()
23+
{
24+
SdFile testFile;
25+
char testFileName[40] = "testfile.txt";
26+
27+
if (xFATSemaphore == NULL)
28+
{
29+
log_d("xFATSemaphore is Null");
30+
return (false);
31+
}
32+
33+
//Attempt to write to file system. This avoids collisions with file writing from other functions like recordSystemSettingsToFile() and F9PSerialReadTask()
34+
if (xSemaphoreTake(xFATSemaphore, fatSemaphore_shortWait_ms) == pdPASS)
35+
{
36+
if (testFile.open(testFileName, O_CREAT | O_APPEND | O_WRITE) == true)
37+
{
38+
testFile.close();
39+
40+
if (sd.exists(testFileName))
41+
sd.remove(testFileName);
42+
xSemaphoreGive(xFATSemaphore);
43+
return (true);
44+
}
45+
xSemaphoreGive(xFATSemaphore);
46+
}
47+
48+
return (false);
49+
}
50+
51+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
52+
void beginSD()
53+
{
54+
pinMode(pin_microSD_CS, OUTPUT);
55+
digitalWrite(pin_microSD_CS, HIGH); //Be sure SD is deselected
56+
57+
if (settings.enableSD == true)
58+
{
59+
//Do a quick test to see if a card is present
60+
int tries = 0;
61+
int maxTries = 5;
62+
while (tries < maxTries)
63+
{
64+
if (sdPresent() == true) break;
65+
log_d("SD present failed. Trying again %d out of %d", tries + 1, maxTries);
66+
67+
//Max power up time is 250ms: https://www.kingston.com/datasheets/SDCIT-specsheet-64gb_en.pdf
68+
//Max current is 200mA average across 1s, peak 300mA
69+
delay(10);
70+
tries++;
71+
}
72+
if (tries == maxTries) return;
73+
74+
//If an SD card is present, allow SdFat to take over
75+
log_d("SD card detected");
76+
77+
if (settings.spiFrequency > 16)
78+
{
79+
Serial.println("Error: SPI Frequency out of range. Default to 16MHz");
80+
settings.spiFrequency = 16;
81+
}
82+
83+
if (sd.begin(SdSpiConfig(pin_microSD_CS, SHARED_SPI, SD_SCK_MHZ(settings.spiFrequency))) == false)
84+
{
85+
tries = 0;
86+
maxTries = 1;
87+
for ( ; tries < maxTries ; tries++)
88+
{
89+
log_d("SD init failed. Trying again %d out of %d", tries + 1, maxTries);
90+
91+
delay(250); //Give SD more time to power up, then try again
92+
if (sd.begin(SdSpiConfig(pin_microSD_CS, SHARED_SPI, SD_SCK_MHZ(settings.spiFrequency))) == true) break;
93+
}
94+
95+
if (tries == maxTries)
96+
{
97+
Serial.println(F("SD init failed. Is card present? Formatted?"));
98+
digitalWrite(pin_microSD_CS, HIGH); //Be sure SD is deselected
99+
online.microSD = false;
100+
return;
101+
}
102+
}
103+
104+
//Change to root directory. All new file creation will be in root.
105+
if (sd.chdir() == false)
106+
{
107+
Serial.println(F("SD change directory failed"));
108+
online.microSD = false;
109+
return;
110+
}
111+
112+
//Setup FAT file access semaphore
113+
if (xFATSemaphore == NULL)
114+
{
115+
xFATSemaphore = xSemaphoreCreateMutex();
116+
if (xFATSemaphore != NULL)
117+
xSemaphoreGive(xFATSemaphore); //Make the file system available for use
118+
}
119+
120+
if (createTestFile() == false)
121+
{
122+
Serial.println(F("Failed to create test file. Format SD card with 'SD Card Formatter'."));
123+
displaySDFail(5000);
124+
online.microSD = false;
125+
return;
126+
}
127+
128+
online.microSD = true;
129+
130+
Serial.println(F("microSD online"));
131+
}
132+
else
133+
{
134+
online.microSD = false;
135+
}
136+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
void printIpAddress(IPAddress ip) {
2+
3+
// Display the IP address
4+
Serial.println(ip);
5+
}
6+
7+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
8+
void printWiFiGatewayIp() {
9+
10+
// Display the subnet mask
11+
Serial.print ("Gateway: ");
12+
IPAddress ip = WiFi.gatewayIP();
13+
printIpAddress(ip);
14+
}
15+
16+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
17+
void printWiFiIpAddress() {
18+
19+
// Display the IP address
20+
Serial.print ("IP Address: ");
21+
IPAddress ip = WiFi.localIP();
22+
printIpAddress(ip);
23+
}
24+
25+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
26+
void printWiFiMacAddress() {
27+
// Display the MAC address
28+
byte mac[6];
29+
WiFi.macAddress(mac);
30+
Serial.print("MAC address: ");
31+
Serial.print(mac[5], HEX);
32+
Serial.print(":");
33+
Serial.print(mac[4], HEX);
34+
Serial.print(":");
35+
Serial.print(mac[3], HEX);
36+
Serial.print(":");
37+
Serial.print(mac[2], HEX);
38+
Serial.print(":");
39+
Serial.print(mac[1], HEX);
40+
Serial.print(":");
41+
Serial.println(mac[0], HEX);
42+
}
43+
44+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
45+
void printWiFiNetwork() {
46+
47+
// Display the SSID
48+
Serial.print("SSID: ");
49+
Serial.println(WiFi.SSID());
50+
51+
// Display the receive signal strength
52+
long rssi = WiFi.RSSI();
53+
Serial.print("Signal strength (RSSI):");
54+
Serial.println(rssi);
55+
}
56+
57+
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
58+
void printWiFiSubnetMask() {
59+
60+
// Display the subnet mask
61+
Serial.print ("Subnet Mask: ");
62+
IPAddress ip = WiFi.subnetMask();
63+
printIpAddress(ip);
64+
}

0 commit comments

Comments
 (0)