Skip to content

Commit 2ed6fb0

Browse files
SybexXcaco3
andauthored
ATA-Trim support (jomjol#2781)
* Add files via upload * Update main.cpp * Update main.cpp * Update main.cpp * Update Helper.cpp * Update Helper.h * Update CMakeLists.txt * Update CMakeLists.txt * Update diskio_sdmmc_mh.c * Update diskio_sdmmc_mh.h * Update ff_mh.c * Update vfs_fat_sdmmc_mh.c * Update sdmmc_common_mh.h * Update sdmmc_common_mh.c * Update Helper.cpp * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update README.md * Update ff_mh.c --------- Co-authored-by: CaCO3 <[email protected]>
1 parent b5213b0 commit 2ed6fb0

39 files changed

+30756
-44
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
set(srcs "diskio/diskio_mh.c"
2+
"diskio/diskio_rawflash_mh.c"
3+
"diskio/diskio_sdmmc_mh.c"
4+
"diskio/diskio_wl_mh.c"
5+
"src/ff_mh.c"
6+
"port/freertos/ffsystem_mh.c"
7+
"src/ffunicode_mh.c"
8+
"vfs/vfs_fat_mh.c"
9+
"vfs/vfs_fat_sdmmc_mh.c"
10+
"vfs/vfs_fat_spiflash_mh.c")
11+
12+
idf_component_register(SRCS ${srcs}
13+
INCLUDE_DIRS "." diskio vfs src
14+
REQUIRES wear_levelling esp-sdmmc
15+
PRIV_REQUIRES vfs spi_flash
16+
)
17+
18+
target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")

code/components/esp-fatfs/README.md

Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
# AIOTED related changes, see https://github.com/jomjol/AI-on-the-edge-device/pull/2781
2+
These files/folders were copied from `[email protected]/components/` and adapted to our own needs.
3+
Since not every SD/MMC was recognized and this was due to the implementation of ATA trim support, this was revised.
4+
Furthermore, files that we don't need were deleted from it.
5+
6+
## The most relevant changes are:
7+
### fatfs/diskio/diskio_sdmmc.c
8+
DRESULT ff_sdmmc_ioctl (BYTE pdrv, BYTE cmd, void* buff), at lines 106 to 110 changed from:
9+
```c
10+
#if FF_USE_TRIM
11+
case CTRL_TRIM:
12+
return ff_sdmmc_trim (pdrv, *((DWORD*)buff), //start_sector
13+
(*((DWORD*)buff + 1) - *((DWORD*)buff) + 1)); //sector_count
14+
#endif //FF_USE_TRIM
15+
```
16+
to:
17+
```c
18+
#if (FF_USE_TRIM)
19+
case CTRL_TRIM:
20+
if(FF_CAN_TRIM){
21+
return ff_sdmmc_trim (pdrv, *((DWORD*)buff), //start_sector
22+
(*((DWORD*)buff + 1) - *((DWORD*)buff) + 1)); //sector_count
23+
}
24+
else{
25+
return RES_ERROR;
26+
}
27+
#endif //FF_USE_TRIM
28+
```
29+
30+
### fatfs/src/ff.c
31+
added:
32+
```c
33+
#include "sdmmc_cmd.h"
34+
```
35+
36+
static FRESULT remove_chain(FFOBJID* obj, DWORD clst, DWORD pclst), at lines 1437 to 1454 changed from:
37+
```c
38+
#if FF_FS_EXFAT || FF_USE_TRIM
39+
if (ecl + 1 == nxt) { /* Is next cluster contiguous? */
40+
ecl = nxt;
41+
} else { /* End of contiguous cluster block */
42+
#if FF_FS_EXFAT
43+
if (fs->fs_type == FS_EXFAT) {
44+
res = change_bitmap(fs, scl, ecl - scl + 1, 0); /* Mark the cluster block 'free' on the bitmap */
45+
if (res != FR_OK) return res;
46+
}
47+
#endif
48+
#if FF_USE_TRIM
49+
rt[0] = clst2sect(fs, scl); /* Start of data area to be freed */
50+
rt[1] = clst2sect(fs, ecl) + fs->csize - 1; /* End of data area to be freed */
51+
disk_ioctl(fs->pdrv, CTRL_TRIM, rt); /* Inform storage device that the data in the block may be erased */
52+
#endif
53+
scl = ecl = nxt;
54+
}
55+
#endif
56+
```
57+
58+
to:
59+
```c
60+
#if FF_FS_EXFAT || FF_USE_TRIM
61+
if(FF_FS_EXFAT || FF_CAN_TRIM){
62+
if (ecl + 1 == nxt) { /* Is next cluster contiguous? */
63+
ecl = nxt;
64+
}
65+
else { /* End of contiguous cluster block */
66+
#if FF_FS_EXFAT
67+
if (fs->fs_type == FS_EXFAT) {
68+
res = change_bitmap(fs, scl, ecl - scl + 1, 0); /* Mark the cluster block 'free' on the bitmap */
69+
if (res != FR_OK) return res;
70+
}
71+
#endif
72+
#if FF_USE_TRIM
73+
if(FF_CAN_TRIM){
74+
rt[0] = clst2sect(fs, scl); /* Start of data area to be freed */
75+
rt[1] = clst2sect(fs, ecl) + fs->csize - 1; /* End of data area to be freed */
76+
disk_ioctl(fs->pdrv, CTRL_TRIM, rt); /* Inform storage device that the data in the block may be erased */
77+
}
78+
#endif
79+
scl = ecl = nxt;
80+
}
81+
}
82+
#endif
83+
```
84+
85+
FRESULT f_mkfs(const TCHAR* path, const MKFS_PARM* opt, void* work, UINT len), at lines 5946 to 5949 changed from:
86+
```c
87+
#if FF_USE_TRIM
88+
lba[0] = b_vol; lba[1] = b_vol + sz_vol - 1; /* Inform storage device that the volume area may be erased */
89+
disk_ioctl(pdrv, CTRL_TRIM, lba);
90+
#endif
91+
```
92+
to:
93+
```c
94+
#if FF_USE_TRIM
95+
if(FF_CAN_TRIM){
96+
lba[0] = b_vol; lba[1] = b_vol + sz_vol - 1; /* Inform storage device that the volume area may be erased */
97+
disk_ioctl(pdrv, CTRL_TRIM, lba);
98+
}
99+
#endif
100+
```
101+
102+
FRESULT f_mkfs(const TCHAR* path, const MKFS_PARM* opt, void* work, UINT len), at lines 6175 to 6178 changed from:
103+
```c
104+
#if FF_USE_TRIM
105+
lba[0] = b_vol; lba[1] = b_vol + sz_vol - 1; /* Inform storage device that the volume area may be erased */
106+
disk_ioctl(pdrv, CTRL_TRIM, lba);
107+
#endif
108+
```
109+
to:
110+
```c
111+
#if FF_USE_TRIM
112+
if(FF_CAN_TRIM){
113+
lba[0] = b_vol; lba[1] = b_vol + sz_vol - 1; /* Inform storage device that the volume area may be erased */
114+
disk_ioctl(pdrv, CTRL_TRIM, lba);
115+
}
116+
#endif
117+
```
118+
119+
### sdmmc/sdmmc_cmd.c
120+
added:
121+
```c
122+
int FF_CAN_TRIM = 0;
123+
```
124+
125+
esp_err_t sdmmc_can_trim(sdmmc_card_t* card), at lines 630 to 636 changed from:
126+
```c
127+
esp_err_t sdmmc_can_trim(sdmmc_card_t* card)
128+
{
129+
if ((card->is_mmc) && (card->ext_csd.sec_feature & EXT_CSD_SEC_GB_CL_EN)) {
130+
return ESP_OK;
131+
}
132+
return ESP_FAIL;
133+
}
134+
```
135+
to:
136+
```c
137+
esp_err_t sdmmc_can_trim(sdmmc_card_t* card)
138+
{
139+
if ((card->is_mmc) && (card->ext_csd.sec_feature & EXT_CSD_SEC_GB_CL_EN)) {
140+
FF_CAN_TRIM = 1;
141+
return ESP_OK;
142+
}
143+
FF_CAN_TRIM = 0;
144+
return ESP_FAIL;
145+
}
146+
```
147+
148+
### sdmmc/include/sdmmc_cmd.h
149+
150+
added:
151+
```c
152+
extern int FF_CAN_TRIM;
153+
```
154+
155+
# Espressif IoT Development Framework
156+
157+
* [中文版](./README_CN.md)
158+
159+
ESP-IDF is the development framework for Espressif SoCs supported on Windows, Linux and macOS.
160+
161+
# ESP-IDF Release Support Schedule
162+
163+
![Support Schedule](https://dl.espressif.com/dl/esp-idf/support-periods.svg)
164+
165+
- Please read [the support policy](SUPPORT_POLICY.md) and [the documentation](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/versions.html) for more information about ESP-IDF versions.
166+
- Please see the [End-of-Life Advisories](https://www.espressif.com/en/support/documents/advisories?keys=&field_type_of_advisory_tid%5B%5D=817) for information about ESP-IDF releases with discontinued support.
167+
168+
# ESP-IDF Release and SoC Compatibility
169+
170+
The following table shows ESP-IDF support of Espressif SoCs where ![alt text][preview] and ![alt text][supported] denote preview status and support, respectively. The preview support is usually limited in time and intended for beta versions of chips. Please use an ESP-IDF release where the desired SoC is already supported.
171+
172+
|Chip | v4.1 | v4.2 | v4.3 | v4.4 | v5.0 | |
173+
|:----------- |:---------------------:| :---------------------:| :---------------------:| :---------------------:| :---------------------:|:------------------------------------------------------------------------------------ |
174+
|ESP32 |![alt text][supported] | ![alt text][supported] | ![alt text][supported] | ![alt text][supported] | ![alt text][supported] | |
175+
|ESP32-S2 | | ![alt text][supported] | ![alt text][supported] | ![alt text][supported] | ![alt text][supported] | |
176+
|ESP32-C3 | | | ![alt text][supported] | ![alt text][supported] | ![alt text][supported] | |
177+
|ESP32-S3 | | | | ![alt text][supported] | ![alt text][supported] | [Announcement](https://www.espressif.com/en/news/ESP32_S3) |
178+
|ESP32-C2 | | | | | ![alt text][supported] | [Announcement](https://blog.espressif.com/esp32-c2-and-why-it-matter-s-bcf4d7d0b2c6) |
179+
|ESP32-H2 | | | | ![alt text][preview] | ![alt text][preview] | [Announcement](https://www.espressif.com/en/news/ESP32_H2) |
180+
181+
[supported]: https://img.shields.io/badge/-supported-green "supported"
182+
[preview]: https://img.shields.io/badge/-preview-orange "preview"
183+
184+
Espressif SoCs released before 2016 (ESP8266 and ESP8285) are supported by [RTOS SDK](https://github.com/espressif/ESP8266_RTOS_SDK) instead.
185+
186+
# Developing With ESP-IDF
187+
188+
## Setting Up ESP-IDF
189+
190+
See https://idf.espressif.com/ for links to detailed instructions on how to set up the ESP-IDF depending on chip you use.
191+
192+
**Note:** Each SoC series and each ESP-IDF release has its own documentation. Please see Section [Versions](https://docs.espressif.com/projects/esp-idf/en/latest/esp32/versions.html) on how to find documentation and how to checkout specific release of ESP-IDF.
193+
194+
### Non-GitHub forks
195+
196+
ESP-IDF uses relative locations as its submodules URLs ([.gitmodules](.gitmodules)). So they link to GitHub. If ESP-IDF is forked to a Git repository which is not on GitHub, you will need to run the script [tools/set-submodules-to-github.sh](tools/set-submodules-to-github.sh) after git clone.
197+
198+
The script sets absolute URLs for all submodules, allowing `git submodule update --init --recursive` to complete. If cloning ESP-IDF from GitHub, this step is not needed.
199+
200+
## Finding a Project
201+
202+
As well as the [esp-idf-template](https://github.com/espressif/esp-idf-template) project mentioned in Getting Started, ESP-IDF comes with some example projects in the [examples](examples) directory.
203+
204+
Once you've found the project you want to work with, change to its directory and you can configure and build it.
205+
206+
To start your own project based on an example, copy the example project directory outside of the ESP-IDF directory.
207+
208+
# Quick Reference
209+
210+
See the Getting Started guide links above for a detailed setup guide. This is a quick reference for common commands when working with ESP-IDF projects:
211+
212+
## Setup Build Environment
213+
214+
(See the Getting Started guide listed above for a full list of required steps with more details.)
215+
216+
* Install host build dependencies mentioned in the Getting Started guide.
217+
* Run the install script to set up the build environment. The options include `install.bat` or `install.ps1` for Windows, and `install.sh` or `install.fish` for Unix shells.
218+
* Run the export script on Windows (`export.bat`) or source it on Unix (`source export.sh`) in every shell environment before using ESP-IDF.
219+
220+
## Configuring the Project
221+
222+
* `idf.py set-target <chip_name>` sets the target of the project to `<chip_name>`. Run `idf.py set-target` without any arguments to see a list of supported targets.
223+
* `idf.py menuconfig` opens a text-based configuration menu where you can configure the project.
224+
225+
## Compiling the Project
226+
227+
`idf.py build`
228+
229+
... will compile app, bootloader and generate a partition table based on the config.
230+
231+
## Flashing the Project
232+
233+
When the build finishes, it will print a command line to use esptool.py to flash the chip. However you can also do this automatically by running:
234+
235+
`idf.py -p PORT flash`
236+
237+
Replace PORT with the name of your serial port (like `COM3` on Windows, `/dev/ttyUSB0` on Linux, or `/dev/cu.usbserial-X` on MacOS. If the `-p` option is left out, `idf.py flash` will try to flash the first available serial port.
238+
239+
This will flash the entire project (app, bootloader and partition table) to a new chip. The settings for serial port flashing can be configured with `idf.py menuconfig`.
240+
241+
You don't need to run `idf.py build` before running `idf.py flash`, `idf.py flash` will automatically rebuild anything which needs it.
242+
243+
## Viewing Serial Output
244+
245+
The `idf.py monitor` target uses the [idf_monitor tool](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/idf-monitor.html) to display serial output from Espressif SoCs. idf_monitor also has a range of features to decode crash output and interact with the device. [Check the documentation page for details](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/idf-monitor.html).
246+
247+
Exit the monitor by typing Ctrl-].
248+
249+
To build, flash and monitor output in one pass, you can run:
250+
251+
`idf.py flash monitor`
252+
253+
## Compiling & Flashing Only the App
254+
255+
After the initial flash, you may just want to build and flash just your app, not the bootloader and partition table:
256+
257+
* `idf.py app` - build just the app.
258+
* `idf.py app-flash` - flash just the app.
259+
260+
`idf.py app-flash` will automatically rebuild the app if any source files have changed.
261+
262+
(In normal development there's no downside to reflashing the bootloader and partition table each time, if they haven't changed.)
263+
264+
## Erasing Flash
265+
266+
The `idf.py flash` target does not erase the entire flash contents. However it is sometimes useful to set the device back to a totally erased state, particularly when making partition table changes or OTA app updates. To erase the entire flash, run `idf.py erase-flash`.
267+
268+
This can be combined with other targets, ie `idf.py -p PORT erase-flash flash` will erase everything and then re-flash the new app, bootloader and partition table.
269+
270+
# Resources
271+
272+
* Documentation for the latest version: https://docs.espressif.com/projects/esp-idf/. This documentation is built from the [docs directory](docs) of this repository.
273+
274+
* The [esp32.com forum](https://esp32.com/) is a place to ask questions and find community resources.
275+
276+
* [Check the Issues section on github](https://github.com/espressif/esp-idf/issues) if you find a bug or have a feature request. Please check existing Issues before opening a new one.
277+
278+
* If you're interested in contributing to ESP-IDF, please check the [Contributions Guide](https://docs.espressif.com/projects/esp-idf/en/latest/contribute/index.html).
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Copyright 2017-2019 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#pragma once
16+
17+
#ifdef __cplusplus
18+
extern "C" {
19+
#endif
20+
21+
#include <stdint.h>
22+
typedef unsigned int UINT;
23+
typedef unsigned char BYTE;
24+
typedef uint32_t DWORD;
25+
26+
#define FF_DRV_NOT_USED 0xFF
27+
28+
#include "diskio_mh.h"
29+
#include "esp_err.h"
30+
31+
/**
32+
* Structure of pointers to disk IO driver functions.
33+
*
34+
* See FatFs documentation for details about these functions
35+
*/
36+
typedef struct {
37+
DSTATUS (*init) (unsigned char pdrv); /*!< disk initialization function */
38+
DSTATUS (*status) (unsigned char pdrv); /*!< disk status check function */
39+
DRESULT (*read) (unsigned char pdrv, unsigned char* buff, uint32_t sector, unsigned count); /*!< sector read function */
40+
DRESULT (*write) (unsigned char pdrv, const unsigned char* buff, uint32_t sector, unsigned count); /*!< sector write function */
41+
DRESULT (*ioctl) (unsigned char pdrv, unsigned char cmd, void* buff); /*!< function to get info about disk and do some misc operations */
42+
} ff_diskio_impl_t;
43+
44+
/**
45+
* Register or unregister diskio driver for given drive number.
46+
*
47+
* When FATFS library calls one of disk_xxx functions for driver number pdrv,
48+
* corresponding function in discio_impl for given pdrv will be called.
49+
*
50+
* @param pdrv drive number
51+
* @param discio_impl pointer to ff_diskio_impl_t structure with diskio functions
52+
* or NULL to unregister and free previously registered drive
53+
*/
54+
void ff_diskio_register(BYTE pdrv, const ff_diskio_impl_t* discio_impl);
55+
56+
#define ff_diskio_unregister(pdrv_) ff_diskio_register(pdrv_, NULL)
57+
58+
59+
/**
60+
* Get next available drive number
61+
*
62+
* @param out_pdrv pointer to the byte to set if successful
63+
*
64+
* @return ESP_OK on success
65+
* ESP_ERR_NOT_FOUND if all drives are attached
66+
*/
67+
esp_err_t ff_diskio_get_drive(BYTE* out_pdrv);
68+
69+
70+
#ifdef __cplusplus
71+
}
72+
#endif

0 commit comments

Comments
 (0)