mci_device.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. //*----------------------------------------------------------------------------
  2. //* ATMEL Microcontroller Software Support - ROUSSET -
  3. //*----------------------------------------------------------------------------
  4. //* The software is delivered "AS IS" without warranty or condition of any
  5. //* kind, either express, implied or statutory. This includes without
  6. //* limitation any warranty or condition with respect to merchantability or
  7. //* fitness for any particular purpose, or against the infringements of
  8. //* intellectual property rights of others.
  9. //*----------------------------------------------------------------------------
  10. //* File Name : mci_device.c
  11. //* Object : TEST DataFlash Functions
  12. //* Creation : FB 26/11/2002
  13. //*
  14. //*----------------------------------------------------------------------------
  15. #include <AT91C_MCI_Device.h>
  16. #include "stdio.h"
  17. #define AT91C_MCI_TIMEOUT 1000000 /* For AT91F_MCIDeviceWaitReady */
  18. #define BUFFER_SIZE_MCI_DEVICE 512
  19. #define MASTER_CLOCK 60000000
  20. #define FALSE 0
  21. #define TRUE 1
  22. //* External Functions
  23. extern void AT91F_ASM_MCI_Handler(void);
  24. //* Global Variables
  25. AT91S_MciDeviceFeatures MCI_Device_Features;
  26. AT91S_MciDeviceDesc MCI_Device_Desc;
  27. AT91S_MciDevice MCI_Device;
  28. #undef ENABLE_WRITE
  29. #undef MMC
  30. //*----------------------------------------------------------------------------
  31. //* \fn AT91F_MCI_SendCommand
  32. //* \brief Generic function to send a command to the MMC or SDCard
  33. //*----------------------------------------------------------------------------
  34. int AT91F_MCI_SendCommand (
  35. AT91PS_MciDevice pMCI_Device,
  36. unsigned int Cmd,
  37. unsigned int Arg)
  38. {
  39. unsigned int error,status;
  40. //unsigned int tick=0;
  41. // Send the command
  42. AT91C_BASE_MCI->MCI_ARGR = Arg;
  43. AT91C_BASE_MCI->MCI_CMDR = Cmd;
  44. // wait for CMDRDY Status flag to read the response
  45. do
  46. {
  47. status = AT91C_BASE_MCI->MCI_SR;
  48. //tick++;
  49. }
  50. while( !(status & AT91C_MCI_CMDRDY) );//&& (tick<100) );
  51. // Test error ==> if crc error and response R3 ==> don't check error
  52. error = (AT91C_BASE_MCI->MCI_SR) & AT91C_MCI_SR_ERROR;
  53. if(error != 0 )
  54. {
  55. // if the command is SEND_OP_COND the CRC error flag is always present (cf : R3 response)
  56. if ( (Cmd != AT91C_SDCARD_APP_OP_COND_CMD) && (Cmd != AT91C_MMC_SEND_OP_COND_CMD) )
  57. return ((AT91C_BASE_MCI->MCI_SR) & AT91C_MCI_SR_ERROR);
  58. else
  59. {
  60. if (error != AT91C_MCI_RCRCE)
  61. return ((AT91C_BASE_MCI->MCI_SR) & AT91C_MCI_SR_ERROR);
  62. }
  63. }
  64. return AT91C_CMD_SEND_OK;
  65. }
  66. //*----------------------------------------------------------------------------
  67. //* \fn AT91F_MCI_SDCard_SendAppCommand
  68. //* \brief Specific function to send a specific command to the SDCard
  69. //*----------------------------------------------------------------------------
  70. int AT91F_MCI_SDCard_SendAppCommand (
  71. AT91PS_MciDevice pMCI_Device,
  72. unsigned int Cmd_App,
  73. unsigned int Arg )
  74. {
  75. unsigned int status;
  76. //unsigned int tick=0;
  77. // Send the CMD55 for application specific command
  78. AT91C_BASE_MCI->MCI_ARGR = (pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address << 16 );
  79. AT91C_BASE_MCI->MCI_CMDR = AT91C_APP_CMD;
  80. // wait for CMDRDY Status flag to read the response
  81. do
  82. {
  83. status = AT91C_BASE_MCI->MCI_SR;
  84. //tick++;
  85. }
  86. while( !(status & AT91C_MCI_CMDRDY) );//&& (tick<100) );
  87. // if an error occurs
  88. if (((AT91C_BASE_MCI->MCI_SR) & AT91C_MCI_SR_ERROR) != 0 )
  89. return ((AT91C_BASE_MCI->MCI_SR) & AT91C_MCI_SR_ERROR);
  90. // check if it is a specific command and then send the command
  91. if ( (Cmd_App && AT91C_SDCARD_APP_ALL_CMD) == 0)
  92. return AT91C_CMD_SEND_ERROR;
  93. return( AT91F_MCI_SendCommand(pMCI_Device,Cmd_App,Arg) );
  94. }
  95. //*----------------------------------------------------------------------------
  96. //* \fn AT91F_MCI_GetStatus
  97. //* \brief Addressed card sends its status register
  98. //*----------------------------------------------------------------------------
  99. int AT91F_MCI_GetStatus(AT91PS_MciDevice pMCI_Device,unsigned int relative_card_address)
  100. {
  101. if (AT91F_MCI_SendCommand(pMCI_Device,
  102. AT91C_SEND_STATUS_CMD,
  103. relative_card_address <<16) == AT91C_CMD_SEND_OK)
  104. return (AT91C_BASE_MCI->MCI_RSPR[0]);
  105. return AT91C_CMD_SEND_ERROR;
  106. }
  107. //*----------------------------------------------------------------------------
  108. //* \fn AT91F_MCI_Device_Handler
  109. //* \brief MCI C interrupt handler
  110. //*----------------------------------------------------------------------------
  111. void AT91F_MCI_Device_Handler(
  112. AT91PS_MciDevice pMCI_Device,
  113. unsigned int status)
  114. {
  115. // If End of Tx Buffer Empty interrupt occurred
  116. if ( status & AT91C_MCI_TXBUFE )
  117. {
  118. AT91C_BASE_MCI->MCI_IDR = AT91C_MCI_TXBUFE;
  119. AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_TXTDIS;
  120. pMCI_Device->pMCI_DeviceDesc->state = AT91C_MCI_IDLE;
  121. } // End of if AT91C_MCI_TXBUFF
  122. // If End of Rx Buffer Full interrupt occurred
  123. if ( status & AT91C_MCI_RXBUFF )
  124. {
  125. AT91C_BASE_MCI->MCI_IDR = AT91C_MCI_RXBUFF;
  126. AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_RXTDIS;
  127. pMCI_Device->pMCI_DeviceDesc->state = AT91C_MCI_IDLE;
  128. } // End of if AT91C_MCI_RXBUFF
  129. }
  130. //*----------------------------------------------------------------------------
  131. //* \fn AT91F_MCI_Handler
  132. //* \brief MCI Handler
  133. //*----------------------------------------------------------------------------
  134. void AT91F_MCI_Handler(void)
  135. {
  136. int status;
  137. status = ( AT91C_BASE_MCI->MCI_SR & AT91C_BASE_MCI->MCI_IMR );
  138. AT91F_MCI_Device_Handler(&MCI_Device,status);
  139. }
  140. //*----------------------------------------------------------------------------
  141. //* \fn AT91F_MCI_ReadBlock
  142. //* \brief Read an ENTIRE block or PARTIAL block
  143. //*----------------------------------------------------------------------------
  144. int AT91F_MCI_ReadBlock(
  145. AT91PS_MciDevice pMCI_Device,
  146. int src,
  147. unsigned int *dataBuffer,
  148. int sizeToRead )
  149. {
  150. ////////////////////////////////////////////////////////////////////////////////////////////
  151. if(pMCI_Device->pMCI_DeviceDesc->state != AT91C_MCI_IDLE)
  152. return AT91C_READ_ERROR;
  153. if( (AT91F_MCI_GetStatus(pMCI_Device,pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address) & AT91C_SR_READY_FOR_DATA) != AT91C_SR_READY_FOR_DATA)
  154. return AT91C_READ_ERROR;
  155. if ( (src + sizeToRead) > pMCI_Device->pMCI_DeviceFeatures->Memory_Capacity )
  156. return AT91C_READ_ERROR;
  157. // If source does not fit a begin of a block
  158. if ( (src % pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length) != 0 )
  159. return AT91C_READ_ERROR;
  160. // Test if the MMC supports Partial Read Block
  161. // ALWAYS SUPPORTED IN SD Memory Card
  162. if( (sizeToRead < pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length)
  163. && (pMCI_Device->pMCI_DeviceFeatures->Read_Partial == 0x00) )
  164. return AT91C_READ_ERROR;
  165. if( sizeToRead > pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length)
  166. return AT91C_READ_ERROR;
  167. ////////////////////////////////////////////////////////////////////////////////////////////
  168. // Init Mode Register
  169. AT91C_BASE_MCI->MCI_MR |= ((pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length << 16) | AT91C_MCI_PDCMODE);
  170. if (sizeToRead %4)
  171. sizeToRead = (sizeToRead /4)+1;
  172. else
  173. sizeToRead = sizeToRead/4;
  174. AT91C_BASE_PDC_MCI->PDC_PTCR = (AT91C_PDC_TXTDIS | AT91C_PDC_RXTDIS);
  175. AT91C_BASE_PDC_MCI->PDC_RPR = (unsigned int)dataBuffer;
  176. AT91C_BASE_PDC_MCI->PDC_RCR = sizeToRead;
  177. // Send the Read single block command
  178. if ( AT91F_MCI_SendCommand(pMCI_Device, AT91C_READ_SINGLE_BLOCK_CMD, src) != AT91C_CMD_SEND_OK )
  179. return AT91C_READ_ERROR;
  180. pMCI_Device->pMCI_DeviceDesc->state = AT91C_MCI_RX_SINGLE_BLOCK;
  181. // Enable AT91C_MCI_RXBUFF Interrupt
  182. AT91C_BASE_MCI->MCI_IER = AT91C_MCI_RXBUFF;
  183. // (PDC) Receiver Transfer Enable
  184. AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_RXTEN;
  185. return AT91C_READ_OK;
  186. }
  187. #ifdef ENABLE_WRITE
  188. //*----------------------------------------------------------------------------
  189. //* \fn AT91F_MCI_WriteBlock
  190. //* \brief Write an ENTIRE block but not always PARTIAL block !!!
  191. //*----------------------------------------------------------------------------
  192. int AT91F_MCI_WriteBlock(
  193. AT91PS_MciDevice pMCI_Device,
  194. int dest,
  195. unsigned int *dataBuffer,
  196. int sizeToWrite )
  197. {
  198. ////////////////////////////////////////////////////////////////////////////////////////////
  199. if( pMCI_Device->pMCI_DeviceDesc->state != AT91C_MCI_IDLE)
  200. return AT91C_WRITE_ERROR;
  201. if( (AT91F_MCI_GetStatus(pMCI_Device,pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address) & AT91C_SR_READY_FOR_DATA) != AT91C_SR_READY_FOR_DATA)
  202. return AT91C_WRITE_ERROR;
  203. if ( (dest + sizeToWrite) > pMCI_Device->pMCI_DeviceFeatures->Memory_Capacity )
  204. return AT91C_WRITE_ERROR;
  205. // If source does not fit a begin of a block
  206. if ( (dest % pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length) != 0 )
  207. return AT91C_WRITE_ERROR;
  208. // Test if the MMC supports Partial Write Block
  209. if( (sizeToWrite < pMCI_Device->pMCI_DeviceFeatures->Max_Write_DataBlock_Length)
  210. && (pMCI_Device->pMCI_DeviceFeatures->Write_Partial == 0x00) )
  211. return AT91C_WRITE_ERROR;
  212. if( sizeToWrite > pMCI_Device->pMCI_DeviceFeatures->Max_Write_DataBlock_Length )
  213. return AT91C_WRITE_ERROR;
  214. ////////////////////////////////////////////////////////////////////////////////////////////
  215. // Init Mode Register
  216. AT91C_BASE_MCI->MCI_MR |= ((pMCI_Device->pMCI_DeviceFeatures->Max_Write_DataBlock_Length << 16) | AT91C_MCI_PDCMODE);
  217. if (sizeToWrite %4)
  218. sizeToWrite = (sizeToWrite /4)+1;
  219. else
  220. sizeToWrite = sizeToWrite/4;
  221. // Init PDC for write sequence
  222. AT91C_BASE_PDC_MCI->PDC_PTCR = (AT91C_PDC_TXTDIS | AT91C_PDC_RXTDIS);
  223. AT91C_BASE_PDC_MCI->PDC_TPR = (unsigned int) dataBuffer;
  224. AT91C_BASE_PDC_MCI->PDC_TCR = sizeToWrite;
  225. // Send the write single block command
  226. if ( AT91F_MCI_SendCommand(pMCI_Device, AT91C_WRITE_BLOCK_CMD, dest) != AT91C_CMD_SEND_OK)
  227. return AT91C_WRITE_ERROR;
  228. pMCI_Device->pMCI_DeviceDesc->state = AT91C_MCI_TX_SINGLE_BLOCK;
  229. // Enable AT91C_MCI_TXBUFE Interrupt
  230. AT91C_BASE_MCI->MCI_IER = AT91C_MCI_TXBUFE;
  231. // Enables TX for PDC transfert requests
  232. AT91C_BASE_PDC_MCI->PDC_PTCR = AT91C_PDC_TXTEN;
  233. return AT91C_WRITE_OK;
  234. }
  235. #endif
  236. #ifdef MMC
  237. //*------------------------------------------------------------------------------------------------------------
  238. //* \fn AT91F_MCI_MMC_SelectCard
  239. //* \brief Toggles a card between the Stand_by and Transfer states or between Programming and Disconnect states
  240. //*------------------------------------------------------------------------------------------------------------
  241. int AT91F_MCI_MMC_SelectCard(AT91PS_MciDevice pMCI_Device, unsigned int relative_card_address)
  242. {
  243. int status;
  244. //* Check if the MMC card chosen is already the selected one
  245. status = AT91F_MCI_GetStatus(pMCI_Device,relative_card_address);
  246. if (status < 0)
  247. return AT91C_CARD_SELECTED_ERROR;
  248. if ((status & AT91C_SR_CARD_SELECTED) == AT91C_SR_CARD_SELECTED)
  249. return AT91C_CARD_SELECTED_OK;
  250. //* Search for the MMC Card to be selected, status = the Corresponding Device Number
  251. status = 0;
  252. while( (pMCI_Device->pMCI_DeviceFeatures[status].Relative_Card_Address != relative_card_address)
  253. && (status < AT91C_MAX_MCI_CARDS) )
  254. status++;
  255. if (status > AT91C_MAX_MCI_CARDS)
  256. return AT91C_CARD_SELECTED_ERROR;
  257. if (AT91F_MCI_SendCommand( pMCI_Device,
  258. AT91C_SEL_DESEL_CARD_CMD,
  259. pMCI_Device->pMCI_DeviceFeatures[status].Relative_Card_Address << 16) == AT91C_CMD_SEND_OK)
  260. return AT91C_CARD_SELECTED_OK;
  261. return AT91C_CARD_SELECTED_ERROR;
  262. }
  263. #endif
  264. //*----------------------------------------------------------------------------
  265. //* \fn AT91F_MCI_GetCSD
  266. //* \brief Asks to the specified card to send its CSD
  267. //*----------------------------------------------------------------------------
  268. int AT91F_MCI_GetCSD (AT91PS_MciDevice pMCI_Device, unsigned int relative_card_address , unsigned int * response)
  269. {
  270. if(AT91F_MCI_SendCommand(pMCI_Device,
  271. AT91C_SEND_CSD_CMD,
  272. (relative_card_address << 16)) != AT91C_CMD_SEND_OK)
  273. return AT91C_CMD_SEND_ERROR;
  274. response[0] = AT91C_BASE_MCI->MCI_RSPR[0];
  275. response[1] = AT91C_BASE_MCI->MCI_RSPR[1];
  276. response[2] = AT91C_BASE_MCI->MCI_RSPR[2];
  277. response[3] = AT91C_BASE_MCI->MCI_RSPR[3];
  278. return AT91C_CMD_SEND_OK;
  279. }
  280. //*----------------------------------------------------------------------------
  281. //* \fn AT91F_MCI_SetBlocklength
  282. //* \brief Select a block length for all following block commands (R/W)
  283. //*----------------------------------------------------------------------------
  284. int AT91F_MCI_SetBlocklength(AT91PS_MciDevice pMCI_Device,unsigned int length)
  285. {
  286. return( AT91F_MCI_SendCommand(pMCI_Device, AT91C_SET_BLOCKLEN_CMD, length) );
  287. }
  288. #ifdef MMC
  289. //*----------------------------------------------------------------------------
  290. //* \fn AT91F_MCI_MMC_GetAllOCR
  291. //* \brief Asks to all cards to send their operations conditions
  292. //*----------------------------------------------------------------------------
  293. int AT91F_MCI_MMC_GetAllOCR (AT91PS_MciDevice pMCI_Device)
  294. {
  295. unsigned int response =0x0;
  296. while(1)
  297. {
  298. response = AT91F_MCI_SendCommand(pMCI_Device,
  299. AT91C_MMC_SEND_OP_COND_CMD,
  300. AT91C_MMC_HOST_VOLTAGE_RANGE);
  301. if (response != AT91C_CMD_SEND_OK)
  302. return AT91C_INIT_ERROR;
  303. response = AT91C_BASE_MCI->MCI_RSPR[0];
  304. if ( (response & AT91C_CARD_POWER_UP_BUSY) == AT91C_CARD_POWER_UP_BUSY)
  305. return(response);
  306. }
  307. }
  308. #endif
  309. #ifdef MMC
  310. //*----------------------------------------------------------------------------
  311. //* \fn AT91F_MCI_MMC_GetAllCID
  312. //* \brief Asks to the MMC on the chosen slot to send its CID
  313. //*----------------------------------------------------------------------------
  314. int AT91F_MCI_MMC_GetAllCID (AT91PS_MciDevice pMCI_Device, unsigned int *response)
  315. {
  316. int Nb_Cards_Found=-1;
  317. while(1)
  318. {
  319. if(AT91F_MCI_SendCommand(pMCI_Device,
  320. AT91C_MMC_ALL_SEND_CID_CMD,
  321. AT91C_NO_ARGUMENT) != AT91C_CMD_SEND_OK)
  322. return Nb_Cards_Found;
  323. else
  324. {
  325. Nb_Cards_Found = 0;
  326. //* Assignation of the relative address to the MMC CARD
  327. pMCI_Device->pMCI_DeviceFeatures[Nb_Cards_Found].Relative_Card_Address = Nb_Cards_Found + AT91C_FIRST_RCA;
  328. //* Set the insert flag
  329. pMCI_Device->pMCI_DeviceFeatures[Nb_Cards_Found].Card_Inserted = AT91C_MMC_CARD_INSERTED;
  330. if (AT91F_MCI_SendCommand(pMCI_Device,
  331. AT91C_MMC_SET_RELATIVE_ADDR_CMD,
  332. (Nb_Cards_Found + AT91C_FIRST_RCA) << 16) != AT91C_CMD_SEND_OK)
  333. return AT91C_CMD_SEND_ERROR;
  334. //* If no error during assignation address ==> Increment Nb_cards_Found
  335. Nb_Cards_Found++ ;
  336. }
  337. }
  338. }
  339. #endif
  340. #ifdef MMC
  341. //*----------------------------------------------------------------------------
  342. //* \fn AT91F_MCI_MMC_Init
  343. //* \brief Return the MMC initialisation status
  344. //*----------------------------------------------------------------------------
  345. int AT91F_MCI_MMC_Init (AT91PS_MciDevice pMCI_Device)
  346. {
  347. unsigned int tab_response[4];
  348. unsigned int mult,blocknr;
  349. unsigned int i,Nb_Cards_Found=0;
  350. //* Resets all MMC Cards in Idle state
  351. AT91F_MCI_SendCommand(pMCI_Device, AT91C_MMC_GO_IDLE_STATE_CMD, AT91C_NO_ARGUMENT);
  352. if(AT91F_MCI_MMC_GetAllOCR(pMCI_Device) == AT91C_INIT_ERROR)
  353. return AT91C_INIT_ERROR;
  354. Nb_Cards_Found = AT91F_MCI_MMC_GetAllCID(pMCI_Device,tab_response);
  355. if (Nb_Cards_Found != AT91C_CMD_SEND_ERROR)
  356. {
  357. //* Set the Mode Register
  358. AT91C_BASE_MCI->MCI_MR = AT91C_MCI_MR_PDCMODE;
  359. for(i = 0; i < Nb_Cards_Found; i++)
  360. {
  361. if (AT91F_MCI_GetCSD(pMCI_Device,
  362. pMCI_Device->pMCI_DeviceFeatures[i].Relative_Card_Address,
  363. tab_response) != AT91C_CMD_SEND_OK)
  364. pMCI_Device->pMCI_DeviceFeatures[i].Relative_Card_Address = 0;
  365. else
  366. {
  367. pMCI_Device->pMCI_DeviceFeatures[i].Max_Read_DataBlock_Length = 1 << ((tab_response[1] >> AT91C_CSD_RD_B_LEN_S) & AT91C_CSD_RD_B_LEN_M );
  368. pMCI_Device->pMCI_DeviceFeatures[i].Max_Write_DataBlock_Length = 1 << ((tab_response[3] >> AT91C_CSD_WBLEN_S) & AT91C_CSD_WBLEN_M );
  369. pMCI_Device->pMCI_DeviceFeatures[i].Sector_Size = 1 + ((tab_response[2] >> AT91C_CSD_v22_SECT_SIZE_S) & AT91C_CSD_v22_SECT_SIZE_M );
  370. pMCI_Device->pMCI_DeviceFeatures[i].Read_Partial = (tab_response[1] >> AT91C_CSD_RD_B_PAR_S) & AT91C_CSD_RD_B_PAR_M;
  371. pMCI_Device->pMCI_DeviceFeatures[i].Write_Partial = (tab_response[3] >> AT91C_CSD_WBLOCK_P_S) & AT91C_CSD_WBLOCK_P_M;
  372. // None in MMC specification version 2.2
  373. pMCI_Device->pMCI_DeviceFeatures[i].Erase_Block_Enable = 0;
  374. pMCI_Device->pMCI_DeviceFeatures[i].Read_Block_Misalignment = (tab_response[1] >> AT91C_CSD_RD_B_MIS_S) & AT91C_CSD_RD_B_MIS_M;
  375. pMCI_Device->pMCI_DeviceFeatures[i].Write_Block_Misalignment = (tab_response[1] >> AT91C_CSD_WR_B_MIS_S) & AT91C_CSD_WR_B_MIS_M;
  376. //// Compute Memory Capacity
  377. // compute MULT
  378. mult = 1 << ( ((tab_response[2] >> AT91C_CSD_C_SIZE_M_S) & AT91C_CSD_C_SIZE_M_M) + 2 );
  379. // compute MSB of C_SIZE
  380. blocknr = ((tab_response[1] >> AT91C_CSD_CSIZE_H_S) & AT91C_CSD_CSIZE_H_M) << 2;
  381. // compute MULT * (LSB of C-SIZE + MSB already computed + 1) = BLOCKNR
  382. blocknr = mult * ( ( blocknr + ( (tab_response[2] >> AT91C_CSD_CSIZE_L_S) & AT91C_CSD_CSIZE_L_M) ) + 1 );
  383. pMCI_Device->pMCI_DeviceFeatures[i].Memory_Capacity = pMCI_Device->pMCI_DeviceFeatures[i].Max_Read_DataBlock_Length * blocknr;
  384. //// End of Compute Memory Capacity
  385. } // end of else
  386. } // end of for
  387. return AT91C_INIT_OK;
  388. } // end of if
  389. return AT91C_INIT_ERROR;
  390. }
  391. #endif
  392. //*----------------------------------------------------------------------------
  393. //* \fn AT91F_MCI_SDCard_GetOCR
  394. //* \brief Asks to all cards to send their operations conditions
  395. //*----------------------------------------------------------------------------
  396. int AT91F_MCI_SDCard_GetOCR (AT91PS_MciDevice pMCI_Device)
  397. {
  398. unsigned int response =0x0;
  399. // The RCA to be used for CMD55 in Idle state shall be the card's default RCA=0x0000.
  400. pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address = 0x0;
  401. while( (response & AT91C_CARD_POWER_UP_BUSY) != AT91C_CARD_POWER_UP_BUSY )
  402. {
  403. response = AT91F_MCI_SDCard_SendAppCommand(pMCI_Device,
  404. AT91C_SDCARD_APP_OP_COND_CMD,
  405. AT91C_MMC_HOST_VOLTAGE_RANGE);
  406. if (response != AT91C_CMD_SEND_OK)
  407. return AT91C_INIT_ERROR;
  408. response = AT91C_BASE_MCI->MCI_RSPR[0];
  409. }
  410. return(AT91C_BASE_MCI->MCI_RSPR[0]);
  411. }
  412. //*----------------------------------------------------------------------------
  413. //* \fn AT91F_MCI_SDCard_GetCID
  414. //* \brief Asks to the SDCard on the chosen slot to send its CID
  415. //*----------------------------------------------------------------------------
  416. int AT91F_MCI_SDCard_GetCID (AT91PS_MciDevice pMCI_Device, unsigned int *response)
  417. {
  418. if(AT91F_MCI_SendCommand(pMCI_Device,
  419. AT91C_ALL_SEND_CID_CMD,
  420. AT91C_NO_ARGUMENT) != AT91C_CMD_SEND_OK)
  421. return AT91C_CMD_SEND_ERROR;
  422. response[0] = AT91C_BASE_MCI->MCI_RSPR[0];
  423. response[1] = AT91C_BASE_MCI->MCI_RSPR[1];
  424. response[2] = AT91C_BASE_MCI->MCI_RSPR[2];
  425. response[3] = AT91C_BASE_MCI->MCI_RSPR[3];
  426. return AT91C_CMD_SEND_OK;
  427. }
  428. //*----------------------------------------------------------------------------
  429. //* \fn AT91F_MCI_SDCard_SetBusWidth
  430. //* \brief Set bus width for SDCard
  431. //*----------------------------------------------------------------------------
  432. int AT91F_MCI_SDCard_SetBusWidth(AT91PS_MciDevice pMCI_Device)
  433. {
  434. volatile int ret_value;
  435. char bus_width;
  436. do
  437. {
  438. ret_value =AT91F_MCI_GetStatus(pMCI_Device,pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address);
  439. }
  440. while((ret_value > 0) && ((ret_value & AT91C_SR_READY_FOR_DATA) == 0));
  441. // Select Card
  442. AT91F_MCI_SendCommand(pMCI_Device,
  443. AT91C_SEL_DESEL_CARD_CMD,
  444. (pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address)<<16);
  445. // Set bus width for Sdcard
  446. if(pMCI_Device->pMCI_DeviceDesc->SDCard_bus_width == AT91C_MCI_SCDBUS)
  447. bus_width = AT91C_BUS_WIDTH_4BITS;
  448. else bus_width = AT91C_BUS_WIDTH_1BIT;
  449. if (AT91F_MCI_SDCard_SendAppCommand(pMCI_Device,AT91C_SDCARD_SET_BUS_WIDTH_CMD,bus_width) != AT91C_CMD_SEND_OK)
  450. return AT91C_CMD_SEND_ERROR;
  451. return AT91C_CMD_SEND_OK;
  452. }
  453. //*----------------------------------------------------------------------------
  454. //* \fn AT91F_MCI_SDCard_Init
  455. //* \brief Return the SDCard initialisation status
  456. //*----------------------------------------------------------------------------
  457. int AT91F_MCI_SDCard_Init (AT91PS_MciDevice pMCI_Device)
  458. {
  459. unsigned int tab_response[4];
  460. unsigned int mult,blocknr;
  461. AT91F_MCI_SendCommand(pMCI_Device, AT91C_GO_IDLE_STATE_CMD, AT91C_NO_ARGUMENT);
  462. if(AT91F_MCI_SDCard_GetOCR(pMCI_Device) == AT91C_INIT_ERROR)
  463. return AT91C_INIT_ERROR;
  464. if (AT91F_MCI_SDCard_GetCID(pMCI_Device,tab_response) == AT91C_CMD_SEND_OK)
  465. {
  466. pMCI_Device->pMCI_DeviceFeatures->Card_Inserted = AT91C_SD_CARD_INSERTED;
  467. if (AT91F_MCI_SendCommand(pMCI_Device, AT91C_SET_RELATIVE_ADDR_CMD, 0) == AT91C_CMD_SEND_OK)
  468. {
  469. pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address = (AT91C_BASE_MCI->MCI_RSPR[0] >> 16);
  470. if (AT91F_MCI_GetCSD(pMCI_Device,pMCI_Device->pMCI_DeviceFeatures->Relative_Card_Address,tab_response) == AT91C_CMD_SEND_OK)
  471. {
  472. pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length = 1 << ((tab_response[1] >> AT91C_CSD_RD_B_LEN_S) & AT91C_CSD_RD_B_LEN_M );
  473. pMCI_Device->pMCI_DeviceFeatures->Max_Write_DataBlock_Length = 1 << ((tab_response[3] >> AT91C_CSD_WBLEN_S) & AT91C_CSD_WBLEN_M );
  474. pMCI_Device->pMCI_DeviceFeatures->Sector_Size = 1 + ((tab_response[2] >> AT91C_CSD_v21_SECT_SIZE_S) & AT91C_CSD_v21_SECT_SIZE_M );
  475. pMCI_Device->pMCI_DeviceFeatures->Read_Partial = (tab_response[1] >> AT91C_CSD_RD_B_PAR_S) & AT91C_CSD_RD_B_PAR_M;
  476. pMCI_Device->pMCI_DeviceFeatures->Write_Partial = (tab_response[3] >> AT91C_CSD_WBLOCK_P_S) & AT91C_CSD_WBLOCK_P_M;
  477. pMCI_Device->pMCI_DeviceFeatures->Erase_Block_Enable = (tab_response[3] >> AT91C_CSD_v21_ER_BLEN_EN_S) & AT91C_CSD_v21_ER_BLEN_EN_M;
  478. pMCI_Device->pMCI_DeviceFeatures->Read_Block_Misalignment = (tab_response[1] >> AT91C_CSD_RD_B_MIS_S) & AT91C_CSD_RD_B_MIS_M;
  479. pMCI_Device->pMCI_DeviceFeatures->Write_Block_Misalignment = (tab_response[1] >> AT91C_CSD_WR_B_MIS_S) & AT91C_CSD_WR_B_MIS_M;
  480. //// Compute Memory Capacity
  481. // compute MULT
  482. mult = 1 << ( ((tab_response[2] >> AT91C_CSD_C_SIZE_M_S) & AT91C_CSD_C_SIZE_M_M) + 2 );
  483. // compute MSB of C_SIZE
  484. blocknr = ((tab_response[1] >> AT91C_CSD_CSIZE_H_S) & AT91C_CSD_CSIZE_H_M) << 2;
  485. // compute MULT * (LSB of C-SIZE + MSB already computed + 1) = BLOCKNR
  486. blocknr = mult * ( ( blocknr + ( (tab_response[2] >> AT91C_CSD_CSIZE_L_S) & AT91C_CSD_CSIZE_L_M) ) + 1 );
  487. pMCI_Device->pMCI_DeviceFeatures->Memory_Capacity = pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length * blocknr;
  488. //// End of Compute Memory Capacity
  489. printf("SD-Card: %d Bytes\n\r", pMCI_Device->pMCI_DeviceFeatures->Memory_Capacity);
  490. if( AT91F_MCI_SDCard_SetBusWidth(pMCI_Device) == AT91C_CMD_SEND_OK )
  491. {
  492. if (AT91F_MCI_SetBlocklength(pMCI_Device,pMCI_Device->pMCI_DeviceFeatures->Max_Read_DataBlock_Length) == AT91C_CMD_SEND_OK)
  493. return AT91C_INIT_OK;
  494. }
  495. }
  496. }
  497. }
  498. return AT91C_INIT_ERROR;
  499. }
  500. //*----------------------------------------------------------------------------
  501. //* \fn AT91F_CfgDevice
  502. //* \brief This function is used to initialise MMC or SDCard Features
  503. //*----------------------------------------------------------------------------
  504. void AT91F_CfgDevice(void)
  505. {
  506. // Init Device Structure
  507. MCI_Device_Features.Relative_Card_Address = 0;
  508. MCI_Device_Features.Card_Inserted = AT91C_CARD_REMOVED;
  509. MCI_Device_Features.Max_Read_DataBlock_Length = 0;
  510. MCI_Device_Features.Max_Write_DataBlock_Length = 0;
  511. MCI_Device_Features.Read_Partial = 0;
  512. MCI_Device_Features.Write_Partial = 0;
  513. MCI_Device_Features.Erase_Block_Enable = 0;
  514. MCI_Device_Features.Sector_Size = 0;
  515. MCI_Device_Features.Memory_Capacity = 0;
  516. MCI_Device_Desc.state = AT91C_MCI_IDLE;
  517. MCI_Device_Desc.SDCard_bus_width = AT91C_MCI_SCDBUS;
  518. // Init AT91S_DataFlash Global Structure, by default AT45DB choosen !!!
  519. MCI_Device.pMCI_DeviceDesc = &MCI_Device_Desc;
  520. MCI_Device.pMCI_DeviceFeatures = &MCI_Device_Features;
  521. }
  522. //*----------------------------------------------------------------------------
  523. //* \fn AT91F_MCI_Init
  524. //* \brief Initialsise Card
  525. //*----------------------------------------------------------------------------
  526. int AT91F_MCI_Init(void)
  527. {
  528. ///////////////////////////////////////////////////////////////////////////////////////////
  529. // MCI Init : common to MMC and SDCard
  530. ///////////////////////////////////////////////////////////////////////////////////////////
  531. // Set up PIO SDC_TYPE to switch on MMC/SDCard and not DataFlash Card
  532. AT91F_PIO_CfgOutput(AT91C_BASE_PIOB,AT91C_PIO_PB7);
  533. AT91F_PIO_SetOutput(AT91C_BASE_PIOB,AT91C_PIO_PB7);
  534. // Init MCI for MMC and SDCard interface
  535. AT91F_MCI_CfgPIO();
  536. AT91F_MCI_CfgPMC();
  537. AT91F_PDC_Open(AT91C_BASE_PDC_MCI);
  538. // Disable all the interrupts
  539. AT91C_BASE_MCI->MCI_IDR = 0xFFFFFFFF;
  540. // Init MCI Device Structures
  541. AT91F_CfgDevice();
  542. // Configure MCI interrupt
  543. AT91F_AIC_ConfigureIt(AT91C_BASE_AIC,
  544. AT91C_ID_MCI,
  545. AT91C_AIC_PRIOR_HIGHEST,
  546. AT91C_AIC_SRCTYPE_INT_LEVEL_SENSITIVE,
  547. AT91F_ASM_MCI_Handler);
  548. // Enable MCI interrupt
  549. AT91F_AIC_EnableIt(AT91C_BASE_AIC,AT91C_ID_MCI);
  550. // Enable Receiver
  551. AT91F_US_EnableRx((AT91PS_USART) AT91C_BASE_DBGU);
  552. AT91F_MCI_Configure(AT91C_BASE_MCI,
  553. AT91C_MCI_DTOR_1MEGA_CYCLES,
  554. AT91C_MCI_MR_PDCMODE, // 15MHz for MCK = 60MHz (CLKDIV = 1)
  555. AT91C_MCI_SDCARD_4BITS_SLOTA);
  556. if(AT91F_MCI_SDCard_Init(&MCI_Device) != AT91C_INIT_OK)
  557. return FALSE;
  558. else
  559. return TRUE;
  560. }
  561. //*----------------------------------------------------------------------------
  562. //* \fn AT91F_MCIDeviceWaitReady
  563. //* \brief Wait for MCI Device ready
  564. //*----------------------------------------------------------------------------
  565. void AT91F_MCIDeviceWaitReady(unsigned int timeout)
  566. {
  567. volatile int status;
  568. do
  569. {
  570. status = AT91C_BASE_MCI->MCI_SR;
  571. timeout--;
  572. }
  573. while( !(status & AT91C_MCI_NOTBUSY) && (timeout>0) );
  574. }
  575. unsigned int swab32(unsigned int data)
  576. {
  577. unsigned int res = 0;
  578. res = (data & 0x000000ff) << 24 |
  579. (data & 0x0000ff00) << 8 |
  580. (data & 0x00ff0000) >> 8 |
  581. (data & 0xff000000) >> 24;
  582. return res;
  583. }
  584. //*--------------------------------------------------------------------
  585. //* \fn AT91F_MCI_ReadBlockSwab
  586. //* \brief Read Block and swap byte order
  587. //*--------------------------------------------------------------------
  588. int AT91F_MCI_ReadBlockSwab(
  589. AT91PS_MciDevice pMCI_Device,
  590. int src,
  591. unsigned int *databuffer,
  592. int sizeToRead)
  593. {
  594. int i;
  595. unsigned char *buf = (unsigned char *)databuffer;
  596. //* Read Block 1
  597. for(i=0;i<BUFFER_SIZE_MCI_DEVICE;i++)
  598. *buf++ = 0x00;
  599. AT91F_MCI_ReadBlock(&MCI_Device,src,databuffer,sizeToRead);
  600. //* Wait end of Read
  601. AT91F_MCIDeviceWaitReady(AT91C_MCI_TIMEOUT);
  602. {
  603. int index;
  604. unsigned int *uiBuffer = databuffer;
  605. for(index = 0; index < 512/4; index++)
  606. uiBuffer[index] = swab32(uiBuffer[index]);
  607. }
  608. return(1);
  609. }