diskio_rawflash.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2015-2018 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_rawflash.h"
  20. #include "esp_compiler.h"
  21. static const char* TAG = "diskio_rawflash";
  22. const esp_partition_t* ff_raw_handles[FF_VOLUMES];
  23. DSTATUS ff_raw_initialize (BYTE pdrv)
  24. {
  25. return 0;
  26. }
  27. DSTATUS ff_raw_status (BYTE pdrv)
  28. {
  29. return 0;
  30. }
  31. DRESULT ff_raw_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
  32. {
  33. ESP_LOGV(TAG, "ff_raw_read - pdrv=%i, sector=%i, count=%in", (unsigned int)pdrv, (unsigned int)sector, (unsigned int)count);
  34. const esp_partition_t* part = ff_raw_handles[pdrv];
  35. assert(part);
  36. esp_err_t err = esp_partition_read(part, sector * SPI_FLASH_SEC_SIZE, buff, count * SPI_FLASH_SEC_SIZE);
  37. if (unlikely(err != ESP_OK)) {
  38. ESP_LOGE(TAG, "esp_partition_read failed (0x%x)", err);
  39. return RES_ERROR;
  40. }
  41. return RES_OK;
  42. }
  43. DRESULT ff_raw_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
  44. {
  45. return RES_ERROR;
  46. }
  47. DRESULT ff_raw_ioctl (BYTE pdrv, BYTE cmd, void *buff)
  48. {
  49. const esp_partition_t* part = ff_raw_handles[pdrv];
  50. ESP_LOGV(TAG, "ff_raw_ioctl: cmd=%in", cmd);
  51. assert(part);
  52. switch (cmd) {
  53. case CTRL_SYNC:
  54. return RES_OK;
  55. case GET_SECTOR_COUNT:
  56. *((DWORD *) buff) = part->size / SPI_FLASH_SEC_SIZE;
  57. return RES_OK;
  58. case GET_SECTOR_SIZE:
  59. *((WORD *) buff) = SPI_FLASH_SEC_SIZE;
  60. return RES_OK;
  61. case GET_BLOCK_SIZE:
  62. return RES_ERROR;
  63. }
  64. return RES_ERROR;
  65. }
  66. esp_err_t ff_diskio_register_raw_partition(BYTE pdrv, const esp_partition_t* part_handle)
  67. {
  68. if (pdrv >= FF_VOLUMES) {
  69. return ESP_ERR_INVALID_ARG;
  70. }
  71. static const ff_diskio_impl_t raw_impl = {
  72. .init = &ff_raw_initialize,
  73. .status = &ff_raw_status,
  74. .read = &ff_raw_read,
  75. .write = &ff_raw_write,
  76. .ioctl = &ff_raw_ioctl
  77. };
  78. ff_diskio_register(pdrv, &raw_impl);
  79. ff_raw_handles[pdrv] = part_handle;
  80. return ESP_OK;
  81. }
  82. BYTE ff_diskio_get_pdrv_raw(const esp_partition_t* part_handle)
  83. {
  84. for (int i = 0; i < FF_VOLUMES; i++) {
  85. if (part_handle == ff_raw_handles[i]) {
  86. return i;
  87. }
  88. }
  89. return 0xff;
  90. }