diskio_rawflash_mh.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <string.h>
  7. #include "diskio_impl_mh.h"
  8. #include "ffconf_mh.h"
  9. #include "ff_mh.h"
  10. #include "esp_log.h"
  11. #include "diskio_rawflash_mh.h"
  12. #include "esp_compiler.h"
  13. #include "spi_flash_mmap.h"
  14. static const char* TAG = "diskio_rawflash";
  15. const esp_partition_t* ff_raw_handles[FF_VOLUMES];
  16. DSTATUS ff_raw_initialize (BYTE pdrv)
  17. {
  18. return 0;
  19. }
  20. DSTATUS ff_raw_status (BYTE pdrv)
  21. {
  22. return 0;
  23. }
  24. DRESULT ff_raw_read (BYTE pdrv, BYTE *buff, DWORD sector, UINT count)
  25. {
  26. ESP_LOGV(TAG, "ff_raw_read - pdrv=%i, sector=%i, count=%in", (unsigned int)pdrv, (unsigned int)sector, (unsigned int)count);
  27. const esp_partition_t* part = ff_raw_handles[pdrv];
  28. assert(part);
  29. esp_err_t err = esp_partition_read(part, sector * SPI_FLASH_SEC_SIZE, buff, count * SPI_FLASH_SEC_SIZE);
  30. if (unlikely(err != ESP_OK)) {
  31. ESP_LOGE(TAG, "esp_partition_read failed (0x%x)", err);
  32. return RES_ERROR;
  33. }
  34. return RES_OK;
  35. }
  36. DRESULT ff_raw_write (BYTE pdrv, const BYTE *buff, DWORD sector, UINT count)
  37. {
  38. return RES_ERROR;
  39. }
  40. DRESULT ff_raw_ioctl (BYTE pdrv, BYTE cmd, void *buff)
  41. {
  42. const esp_partition_t* part = ff_raw_handles[pdrv];
  43. ESP_LOGV(TAG, "ff_raw_ioctl: cmd=%in", cmd);
  44. assert(part);
  45. switch (cmd) {
  46. case CTRL_SYNC:
  47. return RES_OK;
  48. case GET_SECTOR_COUNT:
  49. *((DWORD *) buff) = part->size / SPI_FLASH_SEC_SIZE;
  50. return RES_OK;
  51. case GET_SECTOR_SIZE:
  52. *((WORD *) buff) = SPI_FLASH_SEC_SIZE;
  53. return RES_OK;
  54. case GET_BLOCK_SIZE:
  55. return RES_ERROR;
  56. }
  57. return RES_ERROR;
  58. }
  59. esp_err_t ff_diskio_register_raw_partition(BYTE pdrv, const esp_partition_t* part_handle)
  60. {
  61. if (pdrv >= FF_VOLUMES) {
  62. return ESP_ERR_INVALID_ARG;
  63. }
  64. static const ff_diskio_impl_t raw_impl = {
  65. .init = &ff_raw_initialize,
  66. .status = &ff_raw_status,
  67. .read = &ff_raw_read,
  68. .write = &ff_raw_write,
  69. .ioctl = &ff_raw_ioctl
  70. };
  71. ff_diskio_register(pdrv, &raw_impl);
  72. ff_raw_handles[pdrv] = part_handle;
  73. return ESP_OK;
  74. }
  75. BYTE ff_diskio_get_pdrv_raw(const esp_partition_t* part_handle)
  76. {
  77. for (int i = 0; i < FF_VOLUMES; i++) {
  78. if (part_handle == ff_raw_handles[i]) {
  79. return i;
  80. }
  81. }
  82. return 0xff;
  83. }