diskio_sdmmc.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 "diskio_impl.h"
  15. #include "ffconf.h"
  16. #include "ff.h"
  17. #include "sdmmc_cmd.h"
  18. #include "esp_log.h"
  19. #include "esp_compiler.h"
  20. static sdmmc_card_t* s_cards[FF_VOLUMES] = { NULL };
  21. static const char* TAG = "diskio_sdmmc";
  22. DSTATUS ff_sdmmc_initialize (BYTE pdrv)
  23. {
  24. return 0;
  25. }
  26. DSTATUS ff_sdmmc_status (BYTE pdrv)
  27. {
  28. return 0;
  29. }
  30. DRESULT ff_sdmmc_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count)
  31. {
  32. sdmmc_card_t* card = s_cards[pdrv];
  33. assert(card);
  34. esp_err_t err = sdmmc_read_sectors(card, buff, sector, count);
  35. if (unlikely(err != ESP_OK)) {
  36. ESP_LOGE(TAG, "sdmmc_read_blocks failed (%d)", err);
  37. return RES_ERROR;
  38. }
  39. return RES_OK;
  40. }
  41. DRESULT ff_sdmmc_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count)
  42. {
  43. sdmmc_card_t* card = s_cards[pdrv];
  44. assert(card);
  45. esp_err_t err = sdmmc_write_sectors(card, buff, sector, count);
  46. if (unlikely(err != ESP_OK)) {
  47. ESP_LOGE(TAG, "sdmmc_write_blocks failed (%d)", err);
  48. return RES_ERROR;
  49. }
  50. return RES_OK;
  51. }
  52. DRESULT ff_sdmmc_ioctl (BYTE pdrv, BYTE cmd, void* buff)
  53. {
  54. sdmmc_card_t* card = s_cards[pdrv];
  55. assert(card);
  56. switch(cmd) {
  57. case CTRL_SYNC:
  58. return RES_OK;
  59. case GET_SECTOR_COUNT:
  60. *((DWORD*) buff) = card->csd.capacity;
  61. return RES_OK;
  62. case GET_SECTOR_SIZE:
  63. *((WORD*) buff) = card->csd.sector_size;
  64. return RES_OK;
  65. case GET_BLOCK_SIZE:
  66. return RES_ERROR;
  67. }
  68. return RES_ERROR;
  69. }
  70. void ff_diskio_register_sdmmc(BYTE pdrv, sdmmc_card_t* card)
  71. {
  72. static const ff_diskio_impl_t sdmmc_impl = {
  73. .init = &ff_sdmmc_initialize,
  74. .status = &ff_sdmmc_status,
  75. .read = &ff_sdmmc_read,
  76. .write = &ff_sdmmc_write,
  77. .ioctl = &ff_sdmmc_ioctl
  78. };
  79. s_cards[pdrv] = card;
  80. ff_diskio_register(pdrv, &sdmmc_impl);
  81. }
  82. BYTE ff_diskio_get_pdrv_card(const sdmmc_card_t* card)
  83. {
  84. for (int i = 0; i < FF_VOLUMES; i++) {
  85. if (card == s_cards[i]) {
  86. return i;
  87. }
  88. }
  89. return 0xff;
  90. }