PDL for FM4  Version1.2
Peripheral Driverl Library for FM4
sdcard.c
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2013 Spansion LLC. All Rights Reserved.
3 *
4 * This software is owned and published by:
5 * Spansion LLC, 915 DeGuigne Dr. Sunnyvale, CA 94088-3453 ("Spansion").
6 *
7 * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
8 * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
9 *
10 * This software contains source code for use with Spansion
11 * components. This software is licensed by Spansion to be adapted only
12 * for use in systems utilizing Spansion components. Spansion shall not be
13 * responsible for misuse or illegal use of this software for devices not
14 * supported herein. Spansion is providing this software "AS IS" and will
15 * not be responsible for issues arising from incorrect user implementation
16 * of the software.
17 *
18 * SPANSION MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
19 * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
20 * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
21 * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
22 * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
23 * WARRANTY OF NONINFRINGEMENT.
24 * SPANSION SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
25 * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
26 * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
27 * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
28 * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
29 * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
30 * SAVINGS OR PROFITS,
31 * EVEN IF SPANSION HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
32 * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
33 * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
34 * FROM, THE SOFTWARE.
35 *
36 * This software may be replicated in part or whole for the licensed use,
37 * with the restriction that this Disclaimer and Copyright notice must be
38 * included with each copy of this software, whether used in part or whole,
39 * at all times.
40 */
41 /******************************************************************************/
51 /******************************************************************************/
52 /* Include files */
53 /******************************************************************************/
54 #include "sdcard.h"
55 #include <string.h>
56 
57 #if (defined(PDL_PERIPHERAL_SD_ACTIVE))
58 
64 
65 /******************************************************************************/
66 /* Local pre-processor symbols/macros ('#define') */
67 /******************************************************************************/
68 #define SD_DLY_1UNIT 8000
69 
70 /******************************************************************************/
71 /* Global variable definitions (declared in header file with 'extern') */
72 /******************************************************************************/
73 
74 /******************************************************************************/
75 /* Local type definitions ('typedef') */
76 /******************************************************************************/
77 
78 /******************************************************************************/
79 /* Local function prototypes ('static') */
80 /******************************************************************************/
81 
82 /******************************************************************************/
83 /* Local variable definitions ('static') */
84 /******************************************************************************/
85 static uint32_t m_u32NumSector;
86 
87 /******************************************************************************/
88 /* Function implementation - global ('extern') and local ('static') */
89 /******************************************************************************/
90 
91 /******************************************************************************/
92 /* Local Functions */
93 /******************************************************************************/
101 static void SdDly_1unit (uint32_t u32Delay)
102 {
103  volatile uint32_t u32i;
104  for(;u32Delay;--u32Delay)
105  {
106  for(u32i = SD_DLY_1UNIT;u32i;--u32i);
107  }
108 }
109 
110 /******************************************************************************/
111 /* Global Functions */
112 /******************************************************************************/
122 {
124  {
125  return TRUE;
126  }
127 
128  return FALSE;
129 }
130 
142 int32_t Sdcard_Init(stc_sdcard_info_t *pstcSdcardReg)
143 {
144  stc_csdver1_t *pstcCsdSd;
145  stc_csdver2_t *pstcCsdSdHc;
146  uint32_t u32ArgAcd41 = 0;
147  uint32_t u32Buf[4]={0};
148  boolean_t result = FALSE;
149  uint8_t u8Flag = 1;//1: Support High capacity; 0: not support
150  volatile uint8_t u8Retry = 0;
151  uint32_t i;
152  uint32_t u32Csize;
153  uint32_t u32CsizeMulti;
154 
155  if(NULL == pstcSdcardReg)
156  {
157  return E_SD_PARAMETER;
158  }
159 
160  pstcCsdSd = (stc_csdver1_t*)pstcSdcardReg->CSD;
161  pstcCsdSdHc = (stc_csdver2_t*)pstcSdcardReg->CSD;
162 
163  if(Sdcard_Detect() == FALSE) return E_SD_NO_CARD;
164 
165  result = Sdcmd_SendCmd(SD_CMD0, 0, u32Buf);
166  if(FALSE == result)
167  {
168  Sd_HostDeInit();
169  return E_SD_INIT;
170  }
171 
172  result = Sdcmd_SendCmd(SD_CMD8,ARG_CMD8,u32Buf);
173  if (FALSE == result) {
174  Sd_HostDeInit();
175  return E_SD_INIT;
176  }
177  for (i = 0; i < CMD8_WAIT; i++)
178  {}
179 
180  //<3.Get OCR
181  u8Retry = 0;
182  memset(u32Buf,0,sizeof(u32Buf));
183 
184  do
185  {
186  u8Retry++;
187  SdDly_1unit(100);
188  result =Sdcmd_SendCmd(SD_CMD55,0,u32Buf);
189 
190  if(result == TRUE)
191  {
192  u32ArgAcd41 =( (u8Flag == 1)? ARG_ACMD41_HCS_ON : ARG_ACMD41_HCS_OFF );
193  Sdcmd_SendCmd(SD_ACMD41, u32ArgAcd41, u32Buf);
194  }
195  }while((0 == (u32Buf[0]&ARG_ACMD41_BUSY))&&(u8Retry < 10));
196 
197  if(u8Retry >= 10)
198  {
199  return E_SD_INIT;
200  }
201  pstcSdcardReg->OCR = u32Buf[0];
202 
203  if(0 != (u32Buf[0] & OCR_CAPACITY_MASK )) //need to verify
204  {
205  pstcSdcardReg->stcCardType.u8Capacity = SD_CAPACITY_HIGH ;
206  }
207  else
208  {
210  }
211  if(FALSE == result)
212  {
213  return E_SD_INIT;
214  }
215 
216  SdDly_1unit(5);
217 
218  //<4.Get CID
219  memset(u32Buf,0,sizeof(u32Buf));
220  if(Sdcmd_SendCmd(SD_CMD2,0,u32Buf) == TRUE)
221  {
222  for(i=0;i<15;i++)
223  {
224  /* Response data 127:8 is stored in Response reg: 119:0 */
225  /* Make clear of LSB/MSB */
226  pstcSdcardReg->CID[i] = *((uint8_t *)(u32Buf) + (14 - i));
227  }
228  }
229  else
230  {
231  return E_SD_INIT;
232  }
233 
234  //<5.Get RCA
235  memset(u32Buf,0,sizeof(u32Buf));
236  if(Sdcmd_SendCmd(SD_CMD3,0,u32Buf) == TRUE)
237  {
238  pstcSdcardReg->RCA = u32Buf[0] >> 16;
239  }
240  else
241  {
242  return E_SD_INIT;
243  }
244 
245  //<6.Get CSD
246  memset(u32Buf,0,sizeof(u32Buf));
247  if(TRUE == Sdcmd_SendCmd(SD_CMD9,pstcSdcardReg->RCA<<16,u32Buf))
248  {
249  for(i=0;i<15;i++)
250  {
251  /* Response data 127:8 is stored in Response reg: 119:0 */
252  /* Make clear of LSB/MSB */
253  pstcSdcardReg->CSD[i] = *((uint8_t *)(u32Buf) + (14 - i));
254  }
255  }
256  else
257  {
258  return E_SD_INIT;
259  }
260 
261  /* *** High Capacity *** */
262  if(SD_CAPACITY_HIGH == pstcSdcardReg->stcCardType.u8Capacity)
263  {
264  /* number of sectors */
265  u32Csize = ((unsigned int)pstcCsdSdHc->u8C_size1 << 16) + ((unsigned int)pstcCsdSdHc->u8C_size2 << 8) + pstcCsdSdHc->u8C_size3;
266  m_u32NumSector = (u32Csize + 1) * 1024;
267  }
268  /* *** Standard Capacity ***/
269  else{
270  /* number of sectors */
271  u32Csize = ((unsigned int)pstcCsdSd->u8C_size1 << 10) + ((unsigned int)pstcCsdSd->u8C_size2 << 2) + pstcCsdSd->u8C_size3;
272  u32CsizeMulti = (pstcCsdSd->u8C_size_multi1 << 1) + pstcCsdSd->u8C_size_multi2;
273  m_u32NumSector = (u32Csize + 1) << (u32CsizeMulti + 2);
274 
275  if(pstcCsdSd->u8Read_bl_len == 0x0A ){
276  m_u32NumSector *= 2;
277  }
278  else if(pstcCsdSd->u8Read_bl_len == 0x0B ){
279  m_u32NumSector *= 4;
280  }
281  }
282  pstcSdcardReg->u32MaxSectorNum = m_u32NumSector;
283 
284  //<7.Select Card
285  if(Sdcmd_SendCmd(SD_CMD7, pstcSdcardReg->RCA<<16, u32Buf) == TRUE)
286  {
287  return E_SD_OK;
288  }
289  else
290  {
291  return E_SD_INIT;
292  }
293 }
294 
313 int32_t Sdcard_ReadSector(stc_sdcard_info_t *pstcSdcardInfo, uint32_t u32Start, uint16_t u16Count, uint8_t *pu8Buf)
314 {
315  uint32_t u32addr;
316  uint32_t u32i;
317  boolean_t result = FALSE;
318  uint32_t u32start_addr = u32Start;
319  uint32_t u32maxsectornum = pstcSdcardInfo->u32MaxSectorNum;
320  uint32_t u32buf[4] = {0};
321  uint32_t *pu32 = (uint32_t*)pu8Buf;
322 
323  if(Sdcard_Detect() == FALSE) return E_SD_NO_CARD;
324 
325  if((pstcSdcardInfo ==NULL)||(u16Count == 0)||(u32Start+u16Count>u32maxsectornum)||(u32Start>u32maxsectornum))
326  {
327  return E_SD_PARAMETER;
328  }
329 
330  if(SD_CAPACITY_STANDARD == pstcSdcardInfo->stcCardType.u8Capacity)
331  {
332  u32addr = u32start_addr<<9;
333  }
334  else
335  {
336  u32addr = u32start_addr;
337  }
338 
339  for (u32i = 0;u32i <u16Count;u32i++)
340  {
341  if(TRUE == Sdcmd_SendCmd(SD_CMD17,u32addr,u32buf))
342  {
343  result = Sdcmd_RxData(pu32);
344  }
345  else
346  {
347  return E_SD_READ;
348  }
349 
350  u32start_addr++;
351  pu32 += BLOCK_SIZE;
352  }
353 
354  return (TRUE == result? E_SD_OK : E_SD_READ);
355 }
356 
374 int32_t Sdcard_WriteSector(stc_sdcard_info_t *pstcSdcardInfo, uint32_t u32Start, uint16_t u16Count, const uint8_t *pu8Buf)
375 {
376  uint32_t u32addr;
377  uint32_t u32i;
378  boolean_t result = FALSE;
379  uint32_t u32start_addr = u32Start;
380  uint32_t u32maxsectornum = pstcSdcardInfo->u32MaxSectorNum;
381  uint32_t u32buf[4] = {0};
382  uint32_t *pu32 = (uint32_t*)pu8Buf;
383 
384  if(Sdcard_Detect() == FALSE) return E_SD_NO_CARD;
385 
386  if((pstcSdcardInfo ==NULL)||(u16Count == 0)||(u32Start+u16Count>u32maxsectornum)||(u32Start>u32maxsectornum))
387  {
388  return E_SD_PARAMETER;
389  }
390 
391  if(SD_CAPACITY_STANDARD == pstcSdcardInfo->stcCardType.u8Capacity)
392  {
393  u32addr = u32start_addr << 9;
394  }
395  else
396  {
397  u32addr = u32start_addr;
398  }
399 
400  for (u32i = 0;u32i <u16Count;u32i++)
401  {
402  if(TRUE == Sdcmd_SendCmd(SD_CMD24,u32addr,u32buf))
403  {
404  result = Sdcmd_TxData(pu32);
405  }
406  else
407  {
408  return E_SD_WRITE;
409  }
410 
411  u32start_addr++;
412  pu32 += BLOCK_SIZE;
413  }
414 
415  return (TRUE == result? E_SD_OK : E_SD_WRITE);
416 }
417 
419 
420 #endif // #if (defined(PDL_PERIPHERAL_SD_ACTIVE))
421 
422 /******************************************************************************/
423 /* EOF (not truncated) */
424 /******************************************************************************/