diskio_wl.c 3.7 KB

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