CircleOS  1
diskio.c
Go to the documentation of this file.
1 /*-----------------------------------------------------------------------*/
2 /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2007 */
3 /*-----------------------------------------------------------------------*/
4 /* This is a stub disk I/O module that acts as front end of the existing */
5 /* disk I/O modules and attach it to FatFs module with common interface. */
6 /*-----------------------------------------------------------------------*/
7 
8 #define __IO volatile
9 
10 #include "circle.h"
11 
12 #if SDCARD_SDIO
13 #include "sdio_sd.h" // SDIO interface
14 #else
15 #include "spi_sd.h" // SPI interface
16 #endif
17 
18 #include "diskio.h"
19 
20 extern SD_CardInfo SDCardInfo;
21 
22 /* Defines --------------------------------------------------------------*/
23 #define SECTOR_SIZE 512 // sector size in bytes
24 
25 /* Private function prototypes ------------------------------------------*/
26 DRESULT SD_ConvertErr( SD_Error error );
27 
28 /*-----------------------------------------------------------------------*/
29 /* Correspondence between physical drive number and physical drive. */
30 /*-----------------------------------------------------------------------*/
31 
32 #define ATA 0
33 #define MMC 1
34 #define USB 2
35 SD_CardInfo SDCardInfo2;
36 
37 /*-----------------------------------------------------------------------*/
38 /* Inidialize a Drive */
39 /*-----------------------------------------------------------------------*/
40 
41 NODEBUG DSTATUS disk_initialize(
42  BYTE drv /* Physical drive nmuber (0..) */
43 )
44 {
45 #if SDCARD_SDIO
46  NVIC_InitTypeDef NVIC_InitStructure;
47  NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
48  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
49  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
50  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
51  NVIC_Init( &NVIC_InitStructure );
52 
53  NVIC_InitStructure.NVIC_IRQChannel = SD_SDIO_DMA_IRQn;
54  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
55  NVIC_Init( &NVIC_InitStructure );
56 #endif
57 
58  return SD_ConvertErr( SD_Init() );
59 }
60 
61 
62 
63 /*-----------------------------------------------------------------------*/
64 /* Return Disk Status */
65 /*-----------------------------------------------------------------------*/
66 
67 NODEBUG DSTATUS disk_status(
68  BYTE drv /* Physical drive nmuber (0) */
69 )
70 {
71  // TO_IMPLEMENT
72  if ( drv ) return STA_NOINIT; /* Supports only single drive */
73  return 0;
74 }
75 
76 
77 /*-----------------------------------------------------------------------*/
78 /* Read Sector(s) */
79 /*-----------------------------------------------------------------------*/
80 
81 NODEBUG DRESULT disk_read(
82  BYTE drv, /* Physical drive nmuber (0..) */
83  BYTE* buff, /* Data buffer to store read data */
84  DWORD sector, /* Sector address (LBA) */
85  BYTE count /* Number of sectors to read (1..255) */
86 )
87 {
88  // Read the SD card in multiblock mode
89  if (SD_ConvertErr( SD_ReadMultiBlocks( ( uint8_t* )buff, sector << 9, SECTOR_SIZE, count )))
90  return RES_ERROR;
91 
92 #if SDCARD_SDIO
93 
94  __IO SDTransferState Status = SD_TRANSFER_OK;
95 
96  // Check the end of DMA transfer
97  if ( SD_WaitReadOperation() == SD_OK )
98  {
99  // Check the end of SD operation
100  do
101  {
102  Status = SD_GetStatus();
103  }
104  while ( Status == SD_TRANSFER_BUSY );
105 
106  if ( Status == SD_TRANSFER_OK )
107  return RES_OK;
108  else
109  return RES_ERROR;
110  }
111  else
112  return RES_ERROR;
113 
114 #endif
115 
116  return RES_OK;
117 }
118 
119 
120 
121 /*-----------------------------------------------------------------------*/
122 /* Write Sector(s) */
123 /*-----------------------------------------------------------------------*/
124 /* The FatFs module will issue multiple sector transfer request
125 / (count > 1) to the disk I/O layer. The disk function should process
126 / the multiple sector transfer properly Do. not translate it into
127 / multiple single sector transfers to the media, or the data read/write
128 / performance may be drasticaly decreased. */
129 
130 #if _READONLY == 0
131 NODEBUG DRESULT disk_write(
132  BYTE drv, /* Physical drive nmuber (0..) */
133  const BYTE* buff, /* Data to be written */
134  DWORD sector, /* Sector address (LBA) */
135  BYTE count /* Number of sectors to write (1..255) */
136 )
137 {
138 
139  // Write the SD card in multiblock mode
140  if (SD_ConvertErr( SD_WriteMultiBlocks( ( uint8_t* )buff, sector << 9, SECTOR_SIZE, count )))
141  return RES_ERROR;
142 
143 #if SDCARD_SDIO
144 
145  __IO SDTransferState Status = SD_TRANSFER_OK;
146 
147  // Check the end of DMA transfer
148  if ( SD_WaitReadOperation() == SD_OK )
149  {
150  // Check the end of SD operation
151  do
152  {
153  Status = SD_GetStatus();
154  }
155  while ( Status == SD_TRANSFER_BUSY );
156 
157  if ( Status == SD_TRANSFER_OK )
158  return RES_OK;
159  else
160  return RES_ERROR;
161  }
162  else
163  return RES_ERROR;
164 
165 #endif
166 
167  return RES_OK;
168 }
169 #endif /* _READONLY */
170 
171 /*-----------------------------------------------------------------------*/
172 /* Get current time */
173 /*-----------------------------------------------------------------------*/
174 /*
175 Current time is returned with packed into a DWORD value.
176 The bit field is as follows:
177 bit31:25
178  Year from 1980 (0..127)
179 bit24:21
180  Month (1..12)
181 bit20:16
182  Day in month(1..31)
183 bit15:11
184  Hour (0..23)
185 bit10:5
186  Minute (0..59)
187 bit4:0
188  Second / 2 (0..29)
189 */
190 NODEBUG DWORD get_fattime()
191 {
192  u32 dw ;
193  u8 month, day, year;
194  month = day = 1;
195  year = 13;
196 
197  u8 hours, min, sec;
198  RTC_GetTime( &hours, &min, &sec );
199 
200  // The FAT file system can store dates from january 1, 1980 to december 31, 2107.
201  // This explains the - 1980.
202  // STM32 stores the year like this : 98 for 1998, 10 for 2010, 11 for 2011, etc.
203  // This explains the + 2000.
204  dw = (( ( u32 )( year + 2000 - 1980) << 25 )
205  | ( u32 )( month << 21 )
206  | ( u32 )( day << 16 )
207  | ( u32 )( hours << 11 )
208  | ( u32 )( min << 5 )
209  | ( u32 )( sec >> 1 ));
210 
211  return dw;
212 }
213 
214 NODEBUG int8_t STORAGE_GetCapacity( uint8_t lun, uint32_t* block_num, uint32_t* block_size )
215 {
216 #if! SDCARD_SDIO
217  uint32_t temp_block_mul = 0;
218  SD_CSD SD_csd;
219  uint32_t DeviceSizeMul = 0;
220 #endif
221 
222  if ( SD_GetStatus() != 0 )
223  {
224  return ( -1 );
225  }
226 
227  *block_size = 512;
228 
229 #if SDCARD_SDIO
230 
231  *block_num = SDCardInfo.CardCapacity / 512;
232 
233 #else
234 
235  SD_GetCSDRegister( &SD_csd );
236  DeviceSizeMul = SD_csd.DeviceSizeMul + 2;
237  temp_block_mul = ( 1 << SD_csd.RdBlockLen ) / 512;
238  *block_num = ( ( SD_csd.DeviceSize + 1 ) * ( 1 << ( DeviceSizeMul ) ) ) * temp_block_mul;
239 
240 #endif
241 
242  return ( 0 );
243 
244 }
245 
246 /*-----------------------------------------------------------------------*/
247 /* Miscellaneous Functions */
248 /*-----------------------------------------------------------------------*/
249 
250 NODEBUG DRESULT disk_ioctl(
251  BYTE drv, /* Physical drive nmuber (0..) */
252  BYTE ctrl, /* Control code */
253  void* buff /* Buffer to send/receive control data */
254 )
255 {
256  uint32_t block_size;
257 
258  DRESULT res = RES_OK;
259  switch ( ctrl )
260  {
261 
262 
263  case GET_SECTOR_COUNT : // Get number of sectors on the disk (DWORD)
264  *( DWORD* )buff = STORAGE_GetCapacity( 0, buff, &block_size );
265  res = RES_OK;
266  break;
267 
268  case GET_SECTOR_SIZE : // Get R/W sector size (WORD)
269  *( WORD* )buff = SECTOR_SIZE;
270  res = RES_OK;
271  break;
272 
273  case GET_BLOCK_SIZE : // Get erase block size in unit of sector (DWORD)
274  *( DWORD* )buff = 32;
275  res = RES_OK;
276  }
277 
278  return res;
279 }
280 
286 NODEBUG DRESULT SD_ConvertErr( SD_Error error )
287 {
288  DRESULT retVal;
289 
290  switch ( error )
291  {
292  case SD_OK:
293  retVal = RES_OK;
294  break;
295  case SD_ERROR:
296  retVal = RES_ERROR;
297  break;
298  case SD_WRITE_PROT_VIOLATION:
299  retVal = RES_WRPRT;
300  break;
301  case SD_NOT_CONFIGURED:
302  retVal = RES_NOTRDY;
303  break;
304  case SD_INVALID_PARAMETER:
305  retVal = RES_PARERR;
306  break;
307  default:
308  retVal = RES_ERROR;
309  }
310 
311  return retVal;
312 }