]> granicus.if.org Git - esp-idf/blob - components/fatfs/src/diskio_wl.c
esp32: Allow SPIRAM_MALLOC_RESERVE_INTERNAL to span multiple regions of memory
[esp-idf] / components / fatfs / src / diskio_wl.c
1 // Copyright 2015-2017 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 #include <string.h>
16 #include "diskio.h"
17 #include "ffconf.h"
18 #include "ff.h"
19 #include "esp_log.h"
20 #include "diskio_wl.h"
21 #include "wear_levelling.h"
22
23 static const char* TAG = "ff_diskio_spiflash";
24
25 wl_handle_t ff_wl_handles[FF_VOLUMES] = {
26         WL_INVALID_HANDLE,
27         WL_INVALID_HANDLE,
28 };
29
30 DSTATUS ff_wl_initialize (BYTE pdrv)
31 {
32     return 0;
33 }
34
35 DSTATUS ff_wl_status (BYTE pdrv)
36 {
37     return 0;
38 }
39
40 DRESULT ff_wl_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
41 {
42     ESP_LOGV(TAG, "ff_wl_read - pdrv=%i, sector=%i, count=%i\n", (unsigned int)pdrv, (unsigned int)sector, (unsigned int)count);
43     wl_handle_t wl_handle = ff_wl_handles[pdrv];
44     assert(wl_handle + 1);
45     esp_err_t err = wl_read(wl_handle, sector * wl_sector_size(wl_handle), buff, count * wl_sector_size(wl_handle));
46     if (err != ESP_OK) {
47         ESP_LOGE(TAG, "wl_read failed (%d)", err);
48         return RES_ERROR;
49     }
50     return RES_OK;
51 }
52
53 DRESULT ff_wl_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
54 {
55     ESP_LOGV(TAG, "ff_wl_write - pdrv=%i, sector=%i, count=%i\n", (unsigned int)pdrv, (unsigned int)sector, (unsigned int)count);
56     wl_handle_t wl_handle = ff_wl_handles[pdrv];
57     assert(wl_handle + 1);
58     esp_err_t err = wl_erase_range(wl_handle, sector * wl_sector_size(wl_handle), count * wl_sector_size(wl_handle));
59     if (err != ESP_OK) {
60         ESP_LOGE(TAG, "wl_erase_range failed (%d)", err);
61         return RES_ERROR;
62     }
63     err = wl_write(wl_handle, sector * wl_sector_size(wl_handle), buff, count * wl_sector_size(wl_handle));
64     if (err != ESP_OK) {
65         ESP_LOGE(TAG, "wl_write failed (%d)", err);
66         return RES_ERROR;
67     }
68     return RES_OK;
69 }
70
71 DRESULT ff_wl_ioctl (BYTE pdrv, BYTE cmd, void *buff)
72 {
73     wl_handle_t wl_handle = ff_wl_handles[pdrv];
74     ESP_LOGV(TAG, "ff_wl_ioctl: cmd=%i\n", cmd);
75     assert(wl_handle + 1);
76     switch (cmd) {
77     case CTRL_SYNC:
78         return RES_OK;
79     case GET_SECTOR_COUNT:
80         *((DWORD *) buff) = wl_size(wl_handle) / wl_sector_size(wl_handle);
81         return RES_OK;
82     case GET_SECTOR_SIZE:
83         *((WORD *) buff) = wl_sector_size(wl_handle);
84         return RES_OK;
85     case GET_BLOCK_SIZE:
86         return RES_ERROR;
87     }
88     return RES_ERROR;
89 }
90
91
92 esp_err_t ff_diskio_register_wl_partition(BYTE pdrv, wl_handle_t flash_handle)
93 {
94     if (pdrv >= FF_VOLUMES) {
95         return ESP_ERR_INVALID_ARG;
96     }
97     static const ff_diskio_impl_t wl_impl = {
98         .init = &ff_wl_initialize,
99         .status = &ff_wl_status,
100         .read = &ff_wl_read,
101         .write = &ff_wl_write,
102         .ioctl = &ff_wl_ioctl
103     };
104     ff_wl_handles[pdrv] = flash_handle;
105     ff_diskio_register(pdrv, &wl_impl);
106     return ESP_OK;
107 }
108
109 BYTE ff_diskio_get_pdrv_wl(wl_handle_t flash_handle)
110 {
111     for (int i = 0; i < FF_VOLUMES; i++) {
112         if (flash_handle == ff_wl_handles[i]) {
113             return i;
114         }
115     }
116     return 0xff;
117 }