Arduino FatFS
SDSTM32DiskIO.h
1 
19 /* Includes ------------------------------------------------------------------*/
20 #pragma once
21 
22 #include "BaseIO.h"
23 
24 #define _USE_WRITE 1 /* 1: Enable disk_write function */
25 #define _USE_IOCTL 1 /* 1: Enable disk_ioctl function */
26 
27 /* use the default SD timout as defined in the platform BSP driver*/
28 #if defined(SDMMC_DATATIMEOUT)
29 #define SD_TIMEOUT SDMMC_DATATIMEOUT
30 #elif defined(SD_DATATIMEOUT)
31 #define SD_TIMEOUT SD_DATATIMEOUT
32 #else
33 #define SD_TIMEOUT 30 * 1000
34 #endif
35 
36 #define SD_DEFAULT_BLOCK_SIZE 512
37 
38 namespace fatfs {
39 
47 class SDSTM32DiskIO : public BaseIO {
48  public:
54  DSTATUS disk_initialize(BYTE lun) override {
55  stat = STA_NOINIT;
56 #if !defined(DISABLE_SD_INIT)
57 
58  if (BSP_SD_Init() == MSD_OK) {
59  stat = SD_CheckStatus(lun);
60  }
61 
62 #else
63  Stat = SD_CheckStatus(lun);
64 #endif
65  return stat;
66  }
67 
73  DSTATUS disk_status(BYTE lun) override { return SD_CheckStatus(lun); }
74 
83  DRESULT disk_read(BYTE lun, BYTE *buff, DWORD sector, UINT count) {
84  (void)lun;
85  DRESULT res = RES_ERROR;
86 
87  if (BSP_SD_ReadBlocks((uint32_t *)buff, (uint32_t)(sector), count,
88  SD_TIMEOUT) == MSD_OK) {
89  /* wait until the read operation is finished */
90  while (BSP_SD_GetCardState() != MSD_OK) {
91  }
92  res = RES_OK;
93  }
94 
95  return res;
96  }
97 
106 #if _USE_WRITE == 1
107  DRESULT disk_write(BYTE lun, const BYTE *buff, DWORD sector, UINT count) {
108  (void)lun;
109  DRESULT res = RES_ERROR;
110 
111  if (BSP_SD_WriteBlocks((uint32_t *)buff, (uint32_t)(sector), count,
112  SD_TIMEOUT) == MSD_OK) {
113  /* wait until the Write operation is finished */
114  while (BSP_SD_GetCardState() != MSD_OK) {
115  }
116  res = RES_OK;
117  }
118 
119  return res;
120  }
121 #endif /* _USE_WRITE == 1 */
122 
130 #if _USE_IOCTL == 1
131  DRESULT disk_ioctl(BYTE lun, ioctl_cmd_t cmd, void *buff) {
132  (void)lun;
133  DRESULT res = RES_ERROR;
134  BSP_SD_CardInfo CardInfo;
135 
136  if (stat & STA_NOINIT) return RES_NOTRDY;
137 
138  switch (cmd) {
139  /* Make sure that no pending write process */
140  case CTRL_SYNC:
141  res = RES_OK;
142  break;
143 
144  /* Get number of sectors on the disk (DWORD) */
145  case GET_SECTOR_COUNT:
146  BSP_SD_GetCardInfo(&CardInfo);
147  *(DWORD *)buff = CardInfo.LogBlockNbr;
148  res = RES_OK;
149  break;
150 
151  /* Get R/W sector size (WORD) */
152  case GET_SECTOR_SIZE:
153  BSP_SD_GetCardInfo(&CardInfo);
154  *(WORD *)buff = CardInfo.LogBlockSize;
155  res = RES_OK;
156  break;
157 
158  /* Get erase block size in unit of sector (DWORD) */
159  case GET_BLOCK_SIZE:
160  BSP_SD_GetCardInfo(&CardInfo);
161  *(DWORD *)buff = CardInfo.LogBlockSize / SD_DEFAULT_BLOCK_SIZE;
162  res = RES_OK;
163  break;
164 
165  default:
166  res = RES_PARERR;
167  }
168 
169  return res;
170  }
171 #endif /* _USE_IOCTL == 1 */
172 
173 protected:
174  /* Disk status */
175  volatile DSTATUS stat = STA_NOINIT;
176 
177  DSTATUS SD_CheckStatus(BYTE lun) {
178  (void)lun;
179  stat = STA_NOINIT;
180 
181  if (BSP_SD_GetCardState() == MSD_OK) {
182  stat &= ~STA_NOINIT;
183  }
184 
185  return stat;
186  }
187 };
188 
189 } // namespace fatfs
Empty IO implementation that we can use to test the compilation.
Definition: BaseIO.h:10
Depending on the usecase, the SD card initialization could be done at the application level,...
Definition: SDSTM32DiskIO.h:47
DRESULT disk_ioctl(BYTE lun, ioctl_cmd_t cmd, void *buff)
I/O control operation.
Definition: SDSTM32DiskIO.h:131
DRESULT disk_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)
Reads Sector(s)
Definition: SDSTM32DiskIO.h:83
DSTATUS disk_initialize(BYTE lun) override
Initializes a Drive.
Definition: SDSTM32DiskIO.h:54
DRESULT disk_write(BYTE lun, const BYTE *buff, DWORD sector, UINT count)
Writes Sector(s)
Definition: SDSTM32DiskIO.h:107
DSTATUS disk_status(BYTE lun) override
Gets Disk Status.
Definition: SDSTM32DiskIO.h:73