diskio_wl.c 3.7 KB

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