diskio_rawflash.c 2.8 KB

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