-
Notifications
You must be signed in to change notification settings - Fork 4
/
SdFat_format.ino
191 lines (157 loc) · 5.34 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
// 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 buttton,
// 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 <SPI.h>
#include <SdFat.h>
#include <Adafruit_SPIFlash.h>
// up to 11 characters
#define DISK_LABEL "EXT FLASH"
// 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"
FATFS elmchamFatfs;
uint8_t workbuf[4096]; // Working buffer for f_fdisk function.
// On-board external flash (QSPI or SPI) macros should already
// defined in your board variant if supported
// - EXTERNAL_FLASH_USE_QSPI
// - EXTERNAL_FLASH_USE_CS/EXTERNAL_FLASH_USE_SPI
SPIClass mflashSPI(PB5,PB4,PB3);
Adafruit_FlashTransport_SPI flashTransport(PB6, &mflashSPI);
Adafruit_SPIFlash flash(&flashTransport);
// file system object from SdFat
FatFileSystem fatfs;
void setup() {
// Initialize serial port and wait for it to open before continuing.
Serial.begin(115200);
while (!Serial) delay(100);
Serial.println("Adafruit SPI Flash FatFs Format Example");
// Initialize flash library and check its chip ID.
if (!flash.begin()) {
Serial.println("Error, failed to initialize flash chip!");
while(1) yield();
}
Serial.print("Flash chip JEDEC ID: 0x"); Serial.println(flash.getJEDECID(), HEX);
// Wait for user to send OK to continue.
Serial.setTimeout(30000); // Increase timeout to print message less frequently.
do {
Serial.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
Serial.println("This sketch will ERASE ALL DATA on the flash chip and format it with a new filesystem!");
Serial.println("Type OK (all caps) and press enter to continue.");
Serial.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
} while ( !Serial.find("OK"));
// Call fatfs begin and passed flash object to initialize file system
Serial.println("Creating and formatting FAT filesystem (this takes ~60 seconds)...");
// Make filesystem.
FRESULT r = f_mkfs("", FM_FAT | FM_SFD, 0, workbuf, sizeof(workbuf));
if (r != FR_OK) {
Serial.print("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("Error, f_mount failed with error code: "); Serial.println(r, DEC);
while(1) yield();
}
// Setting label
Serial.println("Setting disk label to: " DISK_LABEL);
r = f_setlabel(DISK_LABEL);
if (r != FR_OK) {
Serial.print("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("Formatted flash!");
// Check new filesystem
if (!fatfs.begin(&flash)) {
Serial.println("Error, failed to mount newly formatted filesystem!");
while(1) delay(1);
}
// Done!
Serial.println("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;
}
}
}