CARME-M4 BSP  V1.5
diskio.c
Go to the documentation of this file.
1 
42 #ifdef __cplusplus
43 extern "C" {
44 #endif /* __cplusplus */
45 
46 /*----- Header-Files -------------------------------------------------------*/
47 #include <string.h>
48 #include "diskio.h" /* FatFs lower layer API */
49 #include "stm32f4xx.h"
50 #include "stm32f4_sdio_sd.h"
51 
52 /*----- Macros -------------------------------------------------------------*/
53 #define BLOCK_SIZE 512
55 /*----- Data types ---------------------------------------------------------*/
56 
57 /*----- Function prototypes ------------------------------------------------*/
58 
59 /*----- Data ---------------------------------------------------------------*/
60 
61 /*----- Implementation -----------------------------------------------------*/
72 
73  DSTATUS stat = 0;
74 
75 #ifdef DBGIO
76  printf("disk_initialize %d\n", drv);
77 #endif
78 
79  /* Supports only single drive */
80  if (drv) {
81  stat |= STA_NOINIT;
82  }
83 
84  /* SD Init */
85  if (SD_Init() != SD_OK) {
86 #ifdef DBGIO
87  puts("Initialization Fail");
88 #endif
89  stat |= STA_NOINIT;
90  }
91 
92  return (stat);
93 }
94 
103 DSTATUS disk_status(BYTE drv) {
104 
105  DSTATUS stat = 0;
106 
107  if (SD_Detect() != SD_PRESENT )
108  stat |= STA_NODISK;
109 
110  /* STA_NOTINIT - Subsystem not initialized */
111  /* STA_PROTECTED - Write protected, MMC/SD switch if available */
112 
113  return (stat);
114 }
115 
127 DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, UINT count) {
128 
129  SD_Error Status;
130 
131 #ifdef DBGIO
132  printf("disk_read %d %p %10d %d\n",drv,buff,sector,count);
133 #endif
134 
135  if (SD_Detect() != SD_PRESENT )
136  return (RES_NOTRDY);
137 
138  /* DMA Alignment failure, do single up to aligned buffer */
139  if ((DWORD) buff & 3) {
140  DRESULT res = RES_OK;
141  DWORD scratch[BLOCK_SIZE / 4]; /* Alignment assured, you'll need a sufficiently big stack */
142 
143  while (count--) {
144  res = disk_read(drv, (void *) scratch, sector++, 1);
145 
146  if (res != RES_OK)
147  break;
148 
149  memcpy(buff, scratch, BLOCK_SIZE);
150 
151  buff += BLOCK_SIZE;
152  }
153 
154  return (res);
155  }
156 
157  /* 4GB Compliant */
158  Status = SD_ReadMultiBlocksFIXED(buff, sector, BLOCK_SIZE, count);
159 
160  if (Status == SD_OK) {
161  SDTransferState State;
162 
163  /* Check if the Transfer is finished */
164  Status = SD_WaitReadOperation();
165 
166  /* BUSY, OK (DONE), ERROR (FAIL) */
167  while ((State = SD_GetStatus()) == SD_TRANSFER_BUSY)
168  ;
169 
170  if ((State == SD_TRANSFER_ERROR) || (Status != SD_OK))
171  return (RES_ERROR);
172  else
173  return (RES_OK);
174  }
175  else
176  return (RES_ERROR);
177 }
178 
179 #if _USE_WRITE
180 
191 DRESULT disk_write(BYTE drv, const BYTE *buff, DWORD sector, BYTE count) {
192 
193  SD_Error Status;
194 
195 #ifdef DBGIO
196  printf("disk_write %d %p %10d %d\n",drv,buff,sector,count);
197 #endif
198 
199  if (SD_Detect() != SD_PRESENT )
200  return (RES_NOTRDY);
201 
202  /* DMA Alignment failure, do single up to aligned buffer */
203  if ((DWORD) buff & 3) {
204  DRESULT res = RES_OK;
205  DWORD scratch[BLOCK_SIZE / 4]; /* Alignment assured, you'll need a sufficiently big stack */
206 
207  while (count--) {
208  memcpy(scratch, buff, BLOCK_SIZE);
209 
210  res = disk_write(drv, (void *) scratch, sector++, 1);
211 
212  if (res != RES_OK)
213  break;
214 
215  buff += BLOCK_SIZE;
216  }
217 
218  return (res);
219  }
220 
221  /* 4GB Compliant */
222  Status = SD_WriteMultiBlocksFIXED((uint8_t *) buff, sector, BLOCK_SIZE,
223  count);
224 
225  if (Status == SD_OK) {
226  SDTransferState State;
227 
228  /* Check if the Transfer is finished */
229  Status = SD_WaitWriteOperation();
230 
231  /* BUSY, OK (DONE), ERROR (FAIL) */
232  while ((State = SD_GetStatus()) == SD_TRANSFER_BUSY)
233  ;
234 
235  if ((State == SD_TRANSFER_ERROR) || (Status != SD_OK))
236  return (RES_ERROR);
237  else
238  return (RES_OK);
239  }
240  else
241  return (RES_ERROR);
242 }
243 #endif /* _USE_WRITE */
244 
245 #if _USE_IOCTL
246 
256 DRESULT disk_ioctl(BYTE pdrv, BYTE cmd, void *buff) {
257  return RES_OK;
258 }
259 #endif /* _USE_IOCTL */
260 
261 #ifdef __cplusplus
262 }
263 #endif /* __cplusplus */
264 
SD_Error SD_Init(void)
Initializes the SD Card and put it into StandBy State (Ready for data transfer).
SD_Error SD_WaitWriteOperation(void)
This function waits until the SDIO DMA data transfer is finished. This function should be called afte...
This file contains all the functions prototypes for the SD Card stm324xg_eval_sdio_sd driver firmware...
SD_Error SD_WaitReadOperation(void)
This function waits until the SDIO DMA data transfer is finished. This function should be called afte...
#define STA_NOINIT
Definition: diskio.h:56
SD_Error SD_ReadMultiBlocksFIXED(uint8_t *readbuff, uint32_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
DRESULT disk_read(BYTE drv, BYTE *buff, DWORD sector, UINT count)
Read from the SD Card.
Definition: diskio.c:127
#define STA_NODISK
Definition: diskio.h:57
DRESULT
Results of Disk Functions.
Definition: diskio.h:112
Definition: diskio.h:113
SD_Error
#define BLOCK_SIZE
Definition: diskio.c:53
SDTransferState SD_GetStatus(void)
Gets the cuurent sd card data transfer status.
BYTE DSTATUS
Status of Disk Functions.
Definition: diskio.h:106
#define SD_PRESENT
SD detection on its memory slot.
SDTransferState
SDIO Transfer state.
DSTATUS disk_status(BYTE drv)
Return disk status.
Definition: diskio.c:103
uint8_t SD_Detect(void)
Detect if SD card is correctly plugged in the memory slot.
DSTATUS disk_initialize(BYTE drv)
Initialize the SD Card Returns the disk status.
Definition: diskio.c:71