1 // Copyright 2015-2017 Espressif Systems (Shanghai) PTE LTD
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 #include "diskio_wl.h"
21 #include "wear_levelling.h"
23 static const char* TAG = "ff_diskio_spiflash";
25 wl_handle_t ff_wl_handles[FF_VOLUMES] = {
30 DSTATUS ff_wl_initialize (BYTE pdrv)
35 DSTATUS ff_wl_status (BYTE pdrv)
40 DRESULT ff_wl_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
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));
47 ESP_LOGE(TAG, "wl_read failed (%d)", err);
53 DRESULT ff_wl_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
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));
60 ESP_LOGE(TAG, "wl_erase_range failed (%d)", err);
63 err = wl_write(wl_handle, sector * wl_sector_size(wl_handle), buff, count * wl_sector_size(wl_handle));
65 ESP_LOGE(TAG, "wl_write failed (%d)", err);
71 DRESULT ff_wl_ioctl (BYTE pdrv, BYTE cmd, void *buff)
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);
79 case GET_SECTOR_COUNT:
80 *((DWORD *) buff) = wl_size(wl_handle) / wl_sector_size(wl_handle);
83 *((WORD *) buff) = wl_sector_size(wl_handle);
92 esp_err_t ff_diskio_register_wl_partition(BYTE pdrv, wl_handle_t flash_handle)
94 if (pdrv >= FF_VOLUMES) {
95 return ESP_ERR_INVALID_ARG;
97 static const ff_diskio_impl_t wl_impl = {
98 .init = &ff_wl_initialize,
99 .status = &ff_wl_status,
101 .write = &ff_wl_write,
102 .ioctl = &ff_wl_ioctl
104 ff_wl_handles[pdrv] = flash_handle;
105 ff_diskio_register(pdrv, &wl_impl);
109 BYTE ff_diskio_get_pdrv_wl(wl_handle_t flash_handle)
111 for (int i = 0; i < FF_VOLUMES; i++) {
112 if (flash_handle == ff_wl_handles[i]) {