Arduino FatFS
Loading...
Searching...
No Matches
RamIO.h
1#pragma once
2
3#include <stdlib.h>
4#include <cstring>
5#include <vector>
6#include "IO.h"
7
8#ifdef ESP32
9#include "Arduino.h"
10#endif
11
12namespace fatfs {
13
19class RamIO : public IO {
20 public:
21 // valid sector sizes are 512, 1024, 2048 and 4096
22 RamIO(int sectorCount, int sectorSize = FF_MAX_SS) {
23 sector_size = sectorSize;
24 sector_count = sectorCount;
25 }
26
27 ~RamIO() {
28 for (int j = 0; j <= sector_count; j++) {
29 free(sectors[j]);
30 }
31 delete[]work_buffer;
32 }
33
34 // custom logic on mount: we need to format the drive - implementation at end of header
35 FRESULT mount(FatFs& fs) override;
36
37 DSTATUS disk_initialize(BYTE pdrv) override {
38 if (pdrv != 0) return STA_NODISK;
39 // allocate sectors
40 if (sectors.size() == 0) {
41 for (int j = 0; j <= sector_count; j++) {
42 uint8_t* ptr = nullptr;
43#ifdef ESP32
44 ptr = (uint8_t*)ps_malloc(sector_count * sector_size);
45#endif
46 if (ptr == nullptr) ptr = (uint8_t*)malloc(sector_size);
47 memset(ptr, 0, sector_size);
48 sectors.push_back(ptr);
49 }
50 }
51 status = STA_CLEAR;
52 return status;
53 }
54
55 DSTATUS disk_status(BYTE pdrv) override {
56 if (pdrv != 0) return STA_NODISK;
57 return status;
58 }
59
60 DRESULT disk_read(BYTE pdrv, BYTE* buffer, LBA_t sectorNo, UINT sectorCount) override {
61 if (pdrv != 0) return RES_NOTRDY;
62 if (status == STA_NOINIT) return RES_NOTRDY;
63 if (sectors.size() == 0 || sectorNo + sectorCount > sector_count) RES_ERROR;
64 for (int j = sectorNo; j < sectorNo + sectorCount; j++) {
65 int idx = j - sectorNo;
66 uint8_t* source = sectors[j];
67 memcpy(buffer + (idx * sector_size), source , sector_size);
68 }
69 return RES_OK;
70 }
71
72 DRESULT disk_write(BYTE pdrv, const BYTE* buffer, LBA_t sectorNo,
73 UINT sectorCount) override {
74 if (pdrv != 0) return RES_NOTRDY;
75 if (status == STA_NOINIT) return RES_NOTRDY;
76 if (sectors.size() == 0 || sectorNo + sectorCount > sector_count) RES_ERROR;
77 for (int j = sectorNo; j < sectorNo + sectorCount; j++) {
78 int idx = j - sectorNo;
79 uint8_t* target = sectors[j];
80 memcpy(target, buffer + (idx * sector_size), sector_size);
81 }
82 return RES_OK;
83 }
84
85 DRESULT disk_ioctl(BYTE pdrv, ioctl_cmd_t cmd, void* buffer) override {
86 DRESULT res;
87 if (pdrv) return RES_PARERR; /* Check parameter */
88 switch (cmd) {
89 case CTRL_SYNC: /* Wait for end of internal write process of the drive */
90 res = RES_OK;
91 break;
92
93 case GET_SECTOR_COUNT: { /* Get drive capacity in unit of sector (DWORD)
94 */
95 DWORD result = sector_count;
96 memcpy(buffer, &result, (sizeof(result)));
97 res = RES_OK;
98 } break;
99
100 case GET_BLOCK_SIZE: { /* Get erase block size in unit of sector (DWORD)
101 */
102 DWORD result = 1;
103 memcpy(buffer, &result, (sizeof(result)));
104 res = RES_OK;
105 } break;
106
107 case CTRL_TRIM: { /* Erase a block of sectors (used when _USE_ERASE == 1)
108 */
109 DWORD range[2];
110 // determine range
111 memcpy(&range, buffer, sizeof(range));
112 // clear memory
113 for (int j = range[0]; j <= range[1]; j++) {
114 memset(sectors[j], 0, sector_size);
115 }
116 res = RES_OK; /* FatFs does not check result of this command */
117 } break;
118
119 default:
120 res = RES_PARERR;
121 break;
122 }
123 return res;
124 }
125
126 protected:
127 std::vector<uint8_t*> sectors;
128 DSTATUS status = STA_NOINIT;
129 int sector_size = 512;
130 size_t sector_count = 0;
131 uint8_t *work_buffer = nullptr;
132};
133
134// Inline implementation (moved from RamIO.cpp)
135inline FRESULT RamIO::mount(FatFs& fs) {
136 // the file system is empty so we need to format it
137 if (work_buffer == nullptr) work_buffer = new uint8_t[FF_MAX_SS];
138 fs.f_mkfs("", nullptr, work_buffer, FF_MAX_SS);
139
140 // standard mount logic
141 return IO::mount(fs);
142}
143
144} // namespace fatfs
API for FatFS See http://elm-chan.org/fsw/ff/00index_e.html.
Definition ff.h:34
FRESULT f_mkfs(const TCHAR *path, const MKFS_PARM *opt, void *work, UINT len)
Definition ff-inc.h:5714
FatFS interface definition.
Definition IO.h:74
virtual FRESULT mount(FatFs &fs)
mount the file system - implementation at end of header to avoid recursive include
Definition IO.h:100
The data is stored in RAM. In a ESP32 when PSRAM has been activated we store it is PSRAM.
Definition RamIO.h:19
FRESULT mount(FatFs &fs) override
mount the file system - implementation at end of header to avoid recursive include
Definition RamIO.h:135