-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathSdFat_format.ino
210 lines (175 loc) · 5.72 KB
/
SdFat_format.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Adafruit SPI Flash FatFs Format Example
// Author: Tony DiCola
//
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !! NOTE: YOU WILL ERASE ALL DATA BY RUNNING THIS SKETCH! !!
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
// Usage:
// - Modify the pins and type of fatfs object in the config
// section below if necessary (usually not necessary).
// - Upload this sketch to your M0 express board.
// - Open the serial monitor at 115200 baud. You should see a
// prompt to confirm formatting. If you don't see the prompt
// close the serial monitor, press the board reset button,
// wait a few seconds, then open the serial monitor again.
// - Type OK and enter to confirm the format when prompted.
// - Partitioning and formatting will take about 30-60 seconds.
// Once formatted a message will be printed to notify you that
// it is finished.
//
#include "SdFat_Adafruit_Fork.h"
#include <Adafruit_SPIFlash.h>
#include <SPI.h>
// Since SdFat doesn't fully support FAT12 such as format a new flash
// We will use Elm Cham's fatfs f_mkfs() to format
#include "ff.h"
#include "diskio.h"
// up to 11 characters
#define DISK_LABEL "EXT FLASH"
// for flashTransport definition
#include "flash_config.h"
Adafruit_SPIFlash flash(&flashTransport);
FatVolume fatfs;
void format_fat12(void) {
// Working buffer for f_mkfs.
#ifdef __AVR__
uint8_t workbuf[512];
#else
uint8_t workbuf[4096];
#endif
// Elm Cham's fatfs objects
FATFS elmchamFatfs;
// Make filesystem.
FRESULT r = f_mkfs("", FM_FAT, 0, workbuf, sizeof(workbuf));
if (r != FR_OK) {
Serial.print(F("Error, f_mkfs failed with error code: "));
Serial.println(r, DEC);
while (1)
yield();
}
// mount to set disk label
r = f_mount(&elmchamFatfs, "0:", 1);
if (r != FR_OK) {
Serial.print(F("Error, f_mount failed with error code: "));
Serial.println(r, DEC);
while (1)
yield();
}
// Setting label
Serial.println(F("Setting disk label to: " DISK_LABEL));
r = f_setlabel(DISK_LABEL);
if (r != FR_OK) {
Serial.print(F("Error, f_setlabel failed with error code: "));
Serial.println(r, DEC);
while (1)
yield();
}
// unmount
f_unmount("0:");
// sync to make sure all data is written to flash
flash.syncBlocks();
Serial.println(F("Formatted flash!"));
}
void check_fat12(void) {
// Check new filesystem
if (!fatfs.begin(&flash)) {
Serial.println(F("Error, failed to mount newly formatted filesystem!"));
while (1)
delay(1);
}
}
void setup() {
// Initialize serial port and wait for it to open before continuing.
Serial.begin(115200);
while (!Serial)
delay(100);
Serial.println(F("Adafruit SPI Flash FatFs Format Example"));
// Initialize flash library and check its chip ID.
if (!flash.begin()) {
Serial.println(F("Error, failed to initialize flash chip!"));
while (1)
yield();
}
Serial.print(F("Flash chip JEDEC ID: 0x"));
Serial.println(flash.getJEDECID(), HEX);
Serial.print(F("Flash size: "));
Serial.print(flash.size() / 1024);
Serial.println(F(" KB"));
// Uncomment to flash LED while writing to flash
// flash.setIndicator(LED_BUILTIN, true);
// Wait for user to send OK to continue.
Serial.setTimeout(
30000); // Increase timeout to print message less frequently.
do {
Serial.println(F("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
"!!!!!!!!!!!!"));
Serial.println(F("This sketch will ERASE ALL DATA on the flash chip and "
"format it with a new filesystem!"));
Serial.println(F("Type OK (all caps) and press enter to continue."));
Serial.println(F("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
"!!!!!!!!!!!!"));
} while (!Serial.find((char *)"OK"));
// Call fatfs begin and passed flash object to initialize file system
Serial.println(
F("Creating and formatting FAT filesystem (this takes ~60 seconds)..."));
format_fat12();
check_fat12();
// Done!
Serial.println(
F("Flash chip successfully formatted with new empty filesystem!"));
}
void loop() {
// Nothing to be done in the main loop.
}
//--------------------------------------------------------------------+
// fatfs diskio
//--------------------------------------------------------------------+
extern "C" {
DSTATUS disk_status(BYTE pdrv) {
(void)pdrv;
return 0;
}
DSTATUS disk_initialize(BYTE pdrv) {
(void)pdrv;
return 0;
}
DRESULT disk_read(BYTE pdrv, /* Physical drive nmuber to identify the drive */
BYTE *buff, /* Data buffer to store read data */
DWORD sector, /* Start sector in LBA */
UINT count /* Number of sectors to read */
) {
(void)pdrv;
return flash.readBlocks(sector, buff, count) ? RES_OK : RES_ERROR;
}
DRESULT disk_write(BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */
DWORD sector, /* Start sector in LBA */
UINT count /* Number of sectors to write */
) {
(void)pdrv;
return flash.writeBlocks(sector, buff, count) ? RES_OK : RES_ERROR;
}
DRESULT disk_ioctl(BYTE pdrv, /* Physical drive nmuber (0..) */
BYTE cmd, /* Control code */
void *buff /* Buffer to send/receive control data */
) {
(void)pdrv;
switch (cmd) {
case CTRL_SYNC:
flash.syncBlocks();
return RES_OK;
case GET_SECTOR_COUNT:
*((DWORD *)buff) = flash.size() / 512;
return RES_OK;
case GET_SECTOR_SIZE:
*((WORD *)buff) = 512;
return RES_OK;
case GET_BLOCK_SIZE:
*((DWORD *)buff) = 8; // erase block size in units of sector size
return RES_OK;
default:
return RES_PARERR;
}
}
}