1 /*
2 * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include "diskio_impl.h"
8 #include "ffconf.h"
9 #include "ff.h"
10 #include "sdmmc_cmd.h"
11 #include "esp_log.h"
12 #include "esp_compiler.h"
13
14 static sdmmc_card_t* s_cards[FF_VOLUMES] = { NULL };
15
16 static const char* TAG = "diskio_sdmmc";
17
18 //Check if SD/MMC card is present
ff_sdmmc_card_available(BYTE pdrv)19 static DSTATUS ff_sdmmc_card_available(BYTE pdrv)
20 {
21 sdmmc_card_t* card = s_cards[pdrv];
22 assert(card);
23 esp_err_t err = sdmmc_get_status(card);
24 if (unlikely(err != ESP_OK)) {
25 ESP_LOGE(TAG, "Check status failed (0x%x)", err);
26 return STA_NOINIT;
27 }
28 return 0;
29 }
30
31 /**
32 * ff_sdmmc_status() and ff_sdmmc_initialize() return STA_NOINIT when sdmmc_get_status()
33 * fails. This error value is checked throughout the FATFS code.
34 * Both functions return 0 on success.
35 */
ff_sdmmc_initialize(BYTE pdrv)36 DSTATUS ff_sdmmc_initialize (BYTE pdrv)
37 {
38 return ff_sdmmc_card_available(pdrv);
39 }
40
ff_sdmmc_status(BYTE pdrv)41 DSTATUS ff_sdmmc_status (BYTE pdrv)
42 {
43 return ff_sdmmc_card_available(pdrv);
44 }
45
ff_sdmmc_read(BYTE pdrv,BYTE * buff,DWORD sector,UINT count)46 DRESULT ff_sdmmc_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count)
47 {
48 sdmmc_card_t* card = s_cards[pdrv];
49 assert(card);
50 esp_err_t err = sdmmc_read_sectors(card, buff, sector, count);
51 if (unlikely(err != ESP_OK)) {
52 ESP_LOGE(TAG, "sdmmc_read_blocks failed (%d)", err);
53 return RES_ERROR;
54 }
55 return RES_OK;
56 }
57
ff_sdmmc_write(BYTE pdrv,const BYTE * buff,DWORD sector,UINT count)58 DRESULT ff_sdmmc_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count)
59 {
60 sdmmc_card_t* card = s_cards[pdrv];
61 assert(card);
62 esp_err_t err = sdmmc_write_sectors(card, buff, sector, count);
63 if (unlikely(err != ESP_OK)) {
64 ESP_LOGE(TAG, "sdmmc_write_blocks failed (%d)", err);
65 return RES_ERROR;
66 }
67 return RES_OK;
68 }
69
ff_sdmmc_ioctl(BYTE pdrv,BYTE cmd,void * buff)70 DRESULT ff_sdmmc_ioctl (BYTE pdrv, BYTE cmd, void* buff)
71 {
72 sdmmc_card_t* card = s_cards[pdrv];
73 assert(card);
74 switch(cmd) {
75 case CTRL_SYNC:
76 return RES_OK;
77 case GET_SECTOR_COUNT:
78 *((DWORD*) buff) = card->csd.capacity;
79 return RES_OK;
80 case GET_SECTOR_SIZE:
81 *((WORD*) buff) = card->csd.sector_size;
82 return RES_OK;
83 case GET_BLOCK_SIZE:
84 return RES_ERROR;
85 }
86 return RES_ERROR;
87 }
88
ff_diskio_register_sdmmc(BYTE pdrv,sdmmc_card_t * card)89 void ff_diskio_register_sdmmc(BYTE pdrv, sdmmc_card_t* card)
90 {
91 static const ff_diskio_impl_t sdmmc_impl = {
92 .init = &ff_sdmmc_initialize,
93 .status = &ff_sdmmc_status,
94 .read = &ff_sdmmc_read,
95 .write = &ff_sdmmc_write,
96 .ioctl = &ff_sdmmc_ioctl
97 };
98 s_cards[pdrv] = card;
99 ff_diskio_register(pdrv, &sdmmc_impl);
100 }
101
ff_diskio_get_pdrv_card(const sdmmc_card_t * card)102 BYTE ff_diskio_get_pdrv_card(const sdmmc_card_t* card)
103 {
104 for (int i = 0; i < FF_VOLUMES; i++) {
105 if (card == s_cards[i]) {
106 return i;
107 }
108 }
109 return 0xff;
110 }
111