-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathff_SdFat_format.cpp
184 lines (154 loc) · 4.81 KB
/
ff_SdFat_format.cpp
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
// clang-format off
// Please do not reformat, so it can be compared to original.
// Modified: Barry Hansen 2024-12-18
// This is minimally-modified copy of SdFat_format.ino
// from examples/SdFat_format
// 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 <SPI.h>
#include <SdFat.h>
#include <Adafruit_SPIFlash.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 "ff_diskio.h"
// up to 11 characters
#define DISK_LABEL "EXT FLASH"
// for flashTransport definition
#include "flash_config.h"
Adafruit_SPIFlash flash(&flashTransport);
FatVolume fatfs;
int 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);
return r;
}
Serial.println(F("Success, f_mkfs"));
// 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);
return r;
}
Serial.println(F("Success, f_mount"));
// 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);
return r;
}
Serial.println(F("Success f_setlabel"));
// unmount
f_unmount("0:");
// sync to make sure all data is written to flash
flash.syncBlocks();
Serial.println(F("Formatted flash!"));
return FR_OK;
}
int check_fat12(void) {
FRESULT r = FR_OK; // assume success
// Check new filesystem
if (!fatfs.begin(&flash)) {
Serial.println(F("Error, failed to mount newly formatted filesystem!"));
r = FR_NO_FILESYSTEM;
}
return r;
}
void ff_setup(void) {
// Initialize flash library and check its chip ID.
if (!flash.begin()) {
Serial.println(F("Error, failed to initialize flash chip!"));
}
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"));
}
//--------------------------------------------------------------------+
// fatfs diskio
// Implement disk functions required by ff.c
//--------------------------------------------------------------------+
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;
}
}
}