This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathFiles.ino
183 lines (137 loc) · 4.26 KB
/
Files.ino
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/****************************************************************************************************************************
Files.ino
For all RP2040 boads using Arduimo-mbed or arduino-pico core
RP2040_SD is a library enable the usage of SD on RP2040-based boards
This Library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License
as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This Library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with the Arduino SdFat Library.
If not, see <http://www.gnu.org/licenses/>.
Based on and modified from Arduino SdFat Library (https://github.com/arduino/Arduino)
(C) Copyright 2009 by William Greiman
(C) Copyright 2010 SparkFun Electronics
(C) Copyright 2021 by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/RP2040_SD
Licensed under GPL-3.0 license
*****************************************************************************************************************************/
/*
SD card connection
This example shows how to read and write data to and from an SD card file
The circuit:
SD card attached to SPI bus as follows:
// Arduino-pico core
** MISO - pin 16
** MOSI - pin 19
** CS - pin 17
** SCK - pin 18
// Arduino-mbed core
** MISO - pin 4
** MOSI - pin 3
** CS - pin 5
** SCK - pin 2
*/
#if !defined(ARDUINO_ARCH_RP2040)
#error For RP2040 only
#endif
#if defined(ARDUINO_ARCH_MBED)
#define PIN_SD_MOSI PIN_SPI_MOSI
#define PIN_SD_MISO PIN_SPI_MISO
#define PIN_SD_SCK PIN_SPI_SCK
#define PIN_SD_SS PIN_SPI_SS
#else
#define PIN_SD_MOSI PIN_SPI0_MOSI
#define PIN_SD_MISO PIN_SPI0_MISO
#define PIN_SD_SCK PIN_SPI0_SCK
#define PIN_SD_SS PIN_SPI0_SS
#endif
#define _RP2040_SD_LOGLEVEL_ 4
#include <SPI.h>
#include <RP2040_SD.h>
#define fileName "example.txt"
File myFile;
uint32_t writeData = 0xDEADBEEF;
uint32_t readData = 0;
bool checkFileExist(const char * fileNameInput)
{
// Check to see if the file exists now
if (SD.exists(fileNameInput) )
{
Serial.print(fileNameInput);
Serial.println(" exists.");
return true;
}
else
{
Serial.print(fileNameInput);
Serial.println(" doesn't exist.");
return false;
}
}
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial);
delay(1000);
#if defined(ARDUINO_ARCH_MBED)
Serial.print("Starting SD Card Files on MBED ");
#else
Serial.print("Starting SD Card Files on ");
#endif
Serial.println(BOARD_NAME);
Serial.println(RP2040_SD_VERSION);
Serial.print("Initializing SD card with SS = ");
Serial.println(PIN_SD_SS);
Serial.print("SCK = ");
Serial.println(PIN_SD_SCK);
Serial.print("MOSI = ");
Serial.println(PIN_SD_MOSI);
Serial.print("MISO = ");
Serial.println(PIN_SD_MISO);
if (!SD.begin(PIN_SD_SS))
{
Serial.println("Initialization failed!");
return;
}
Serial.println("Initialization done.");
checkFileExist(fileName);
// open a new file and immediately close it:
Serial.print("Creating ");
Serial.println(fileName);
myFile = SD.open(fileName, FILE_WRITE);
if (myFile)
{
myFile.write((uint8_t *) &writeData, sizeof(writeData));
Serial.print("writeData = 0x");
Serial.println(writeData, HEX);
myFile.close();
}
else
{
Serial.print("Error open for writing ");
Serial.println(fileName);
}
myFile = SD.open(fileName, FILE_READ);
if (myFile)
{
myFile.read((uint8_t *) &readData, sizeof(readData));
Serial.print("readData = 0x");
Serial.println(readData, HEX);
myFile.close();
}
else
{
Serial.print("Error open for reading ");
Serial.println(fileName);
}
checkFileExist(fileName);
// delete the file:
Serial.println("Removing example.txt...");
SD.remove(fileName);
checkFileExist(fileName);
}
void loop()
{
// nothing happens after setup finishes.
}