Advertisement
Guest User

SD Init Function

a guest
Apr 9th, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.76 KB | None | 0 0
  1. /**
  2. * \brief Initialize and config the SD Card
  3. *
  4. * \param none
  5. *
  6. * \return Init Result
  7. */
  8.  
  9. Int16 sdInit()
  10. {
  11. Uint16 actCard;
  12. Uint32 sectCount;
  13. Uint16 rca;
  14.  
  15. sectCount = 0;
  16.  
  17. /* Initialize the CSL MMCSD module */
  18. mmcStatus = MMC_init();
  19. if(mmcStatus != CSL_SOK)
  20. {
  21. printf(" MMC_init Failed\n");
  22. return(mmcStatus);
  23. }
  24.  
  25. /* Open the MMCSD module in POLLED mode */
  26. // mmcsdHandle = MMC_open(&pMmcsdContObj, CSL_MMCSD1_INST, CSL_MMCSD_OPMODE_POLLED, &mmcStatus);
  27. mmcsdHandle = MMC_open(&pMmcsdContObj, CSL_MMCSD0_INST, CSL_MMCSD_OPMODE_POLLED, &mmcStatus);
  28.  
  29. if(mmcStatus != CSL_SOK)
  30. {
  31. printf(" MMC_open Failed\n");
  32. return(mmcStatus);
  33. }
  34. else
  35. {
  36. printf(" MMC_open Successful\n");
  37. }
  38.  
  39. /* Send CMD0 to the card */
  40. mmcStatus = MMC_sendGoIdle(mmcsdHandle);
  41. if(mmcStatus != CSL_SOK)
  42. {
  43. printf(" MMC_sendGoIdle Failed\n");
  44. return(mmcStatus);
  45. }
  46.  
  47. /* Check for the card */
  48. mmcStatus = MMC_selectCard(mmcsdHandle, &mmcCardObj);
  49. if((mmcStatus == CSL_ESYS_BADHANDLE) ||
  50. (mmcStatus == CSL_ESYS_INVPARAMS))
  51. {
  52. printf(" MMC_selectCard Failed\n");
  53. return(mmcStatus);
  54. }
  55.  
  56. /* Verify whether the SD card is detected or not */
  57. if(mmcCardObj.cardType == CSL_SD_CARD)
  58. {
  59. printf(" SD card Detected!\n");
  60.  
  61. /* Check if the card is high capacity card */
  62. if(mmcsdHandle->cardObj->sdHcDetected == TRUE)
  63. {
  64. printf(" SD card is High Capacity Card\n");
  65. printf(" Memory Access will use Block Addressing\n\n");
  66.  
  67. /* For the SDHC card Block addressing will be used.
  68. Sector address will be same as sector number */
  69. cardAddr = sectCount;
  70. }
  71. else
  72. {
  73. printf(" SD card is Standard Capacity Card\n");
  74. printf(" Memory Access will use Byte Addressing\n\n");
  75.  
  76. /* For the SD card Byte addressing will be used.
  77. Sector address will be product of sector number
  78. and sector size */
  79. cardAddr = (sectCount)*(CSL_MMCSD_BLOCK_LENGTH);
  80. }
  81. }
  82. else
  83. {
  84. /* Check if No card is inserted */
  85. if(mmcCardObj.cardType == CSL_CARD_NONE)
  86. {
  87. printf("No Card Detected!\n");
  88. }
  89. else
  90. {
  91. printf("SD card is not Detected!\n");
  92. }
  93.  
  94. printf("Please Insert SD card!!\n");
  95. return(CSL_ESYS_FAIL);
  96. }
  97.  
  98. /* Set the init clock */
  99. mmcStatus = MMC_sendOpCond(mmcsdHandle, 70);
  100. if(mmcStatus != CSL_SOK)
  101. {
  102. printf(" MMC_sendOpCond Failed\n");
  103. return(mmcStatus);
  104. }
  105.  
  106. /* Send the card identification Data */
  107. mmcStatus = SD_sendAllCID(mmcsdHandle, &sdCardIdObj);
  108. if(mmcStatus != CSL_SOK)
  109. {
  110. printf(" SD_sendAllCID Failed\n");
  111. return(mmcStatus);
  112. }
  113.  
  114. /* Set the Relative Card Address */
  115. mmcStatus = SD_sendRca(mmcsdHandle, &mmcCardObj, &rca);
  116. if(mmcStatus != CSL_SOK)
  117. {
  118. printf(" SD_sendRca Failed\n");
  119. return(mmcStatus);
  120. }
  121.  
  122. /* Read the SD Card Specific Data */
  123. mmcStatus = SD_getCardCsd(mmcsdHandle, &sdCardCsdObj);
  124. if(mmcStatus != CSL_SOK)
  125. {
  126. printf(" SD_getCardCsd Failed\n");
  127. return(mmcStatus);
  128. }
  129.  
  130. /* Set bus width - Optional */
  131. mmcStatus = SD_setBusWidth(mmcsdHandle, 1);
  132. if(mmcStatus != CSL_SOK)
  133. {
  134. printf(" SD_setBusWidth Failed\n");
  135. return(mmcStatus);
  136. }
  137.  
  138. /* Disable SD card pull-up resistors - Optional */
  139. mmcStatus = SD_configurePullup(mmcsdHandle, 0);
  140. if(mmcStatus != CSL_SOK)
  141. {
  142. printf(" SD_configurePullup Failed\n");
  143. return(mmcStatus);
  144. }
  145.  
  146. /* Set the card type in internal data structures */
  147. mmcStatus = MMC_setCardType(&mmcCardObj, mmcCardObj.cardType);
  148. if(mmcStatus != CSL_SOK)
  149. {
  150. printf(" MMC_setCardType Failed\n");
  151. return(mmcStatus);
  152. }
  153.  
  154. /* Set the card pointer in internal data structures */
  155. mmcStatus = MMC_setCardPtr(mmcsdHandle, &mmcCardObj);
  156. if(mmcStatus != CSL_SOK)
  157. {
  158. printf(" MMC_setCardPtr Failed\n");
  159. return(mmcStatus);
  160. }
  161.  
  162. /* Get the number of cards */
  163. mmcStatus = MMC_getNumberOfCards(mmcsdHandle, &actCard);
  164. if(mmcStatus != CSL_SOK)
  165. {
  166. printf(" MMC_getNumberOfCards Failed\n");
  167. return(mmcStatus);
  168. }
  169.  
  170. /* Set clock for read-write access */
  171. mmcStatus = MMC_sendOpCond(mmcsdHandle, 2);
  172. if(mmcStatus != CSL_SOK)
  173. {
  174. printf(" MMC_sendOpCond Failed\n");
  175. return(mmcStatus);
  176. }
  177.  
  178. /* Set Endian mode for read and write operations */
  179. mmcStatus = MMC_setEndianMode(mmcsdHandle, CSL_MMCSD_ENDIAN_LITTLE,
  180. CSL_MMCSD_ENDIAN_LITTLE);
  181. if(mmcStatus != CSL_SOK)
  182. {
  183. printf(" MMC_setEndianMode Failed\n");
  184. return(mmcStatus);
  185. }
  186.  
  187. /* Set block length for the memory card
  188. * For high capacity cards setting the block length will have
  189. * no effect
  190. */
  191. mmcStatus = MMC_setBlockLength(mmcsdHandle, CSL_MMCSD_BLOCK_LENGTH);
  192. if(mmcStatus != CSL_SOK)
  193. {
  194. printf(" MMC_setBlockLength Failed\n");
  195. return(mmcStatus);
  196. }
  197.  
  198. return 0;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement