STM32F10x Standard Peripherals Library  3.5.0
/opt/STM32F10x_StdPeriph_Lib_V3.5.0/Libraries/STM32F10x_StdPeriph_Driver/src/stm32f10x_sdio.c
Go to the documentation of this file.
00001 
00022 /* Includes ------------------------------------------------------------------*/
00023 #include "stm32f10x_sdio.h"
00024 #include "stm32f10x_rcc.h"
00025 
00039 /* ------------ SDIO registers bit address in the alias region ----------- */
00040 #define SDIO_OFFSET                (SDIO_BASE - PERIPH_BASE)
00041 
00042 /* --- CLKCR Register ---*/
00043 
00044 /* Alias word address of CLKEN bit */
00045 #define CLKCR_OFFSET              (SDIO_OFFSET + 0x04)
00046 #define CLKEN_BitNumber           0x08
00047 #define CLKCR_CLKEN_BB            (PERIPH_BB_BASE + (CLKCR_OFFSET * 32) + (CLKEN_BitNumber * 4))
00048 
00049 /* --- CMD Register ---*/
00050 
00051 /* Alias word address of SDIOSUSPEND bit */
00052 #define CMD_OFFSET                (SDIO_OFFSET + 0x0C)
00053 #define SDIOSUSPEND_BitNumber     0x0B
00054 #define CMD_SDIOSUSPEND_BB        (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (SDIOSUSPEND_BitNumber * 4))
00055 
00056 /* Alias word address of ENCMDCOMPL bit */
00057 #define ENCMDCOMPL_BitNumber      0x0C
00058 #define CMD_ENCMDCOMPL_BB         (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (ENCMDCOMPL_BitNumber * 4))
00059 
00060 /* Alias word address of NIEN bit */
00061 #define NIEN_BitNumber            0x0D
00062 #define CMD_NIEN_BB               (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (NIEN_BitNumber * 4))
00063 
00064 /* Alias word address of ATACMD bit */
00065 #define ATACMD_BitNumber          0x0E
00066 #define CMD_ATACMD_BB             (PERIPH_BB_BASE + (CMD_OFFSET * 32) + (ATACMD_BitNumber * 4))
00067 
00068 /* --- DCTRL Register ---*/
00069 
00070 /* Alias word address of DMAEN bit */
00071 #define DCTRL_OFFSET              (SDIO_OFFSET + 0x2C)
00072 #define DMAEN_BitNumber           0x03
00073 #define DCTRL_DMAEN_BB            (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (DMAEN_BitNumber * 4))
00074 
00075 /* Alias word address of RWSTART bit */
00076 #define RWSTART_BitNumber         0x08
00077 #define DCTRL_RWSTART_BB          (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWSTART_BitNumber * 4))
00078 
00079 /* Alias word address of RWSTOP bit */
00080 #define RWSTOP_BitNumber          0x09
00081 #define DCTRL_RWSTOP_BB           (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWSTOP_BitNumber * 4))
00082 
00083 /* Alias word address of RWMOD bit */
00084 #define RWMOD_BitNumber           0x0A
00085 #define DCTRL_RWMOD_BB            (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (RWMOD_BitNumber * 4))
00086 
00087 /* Alias word address of SDIOEN bit */
00088 #define SDIOEN_BitNumber          0x0B
00089 #define DCTRL_SDIOEN_BB           (PERIPH_BB_BASE + (DCTRL_OFFSET * 32) + (SDIOEN_BitNumber * 4))
00090 
00091 /* ---------------------- SDIO registers bit mask ------------------------ */
00092 
00093 /* --- CLKCR Register ---*/
00094 
00095 /* CLKCR register clear mask */
00096 #define CLKCR_CLEAR_MASK         ((uint32_t)0xFFFF8100) 
00097 
00098 /* --- PWRCTRL Register ---*/
00099 
00100 /* SDIO PWRCTRL Mask */
00101 #define PWR_PWRCTRL_MASK         ((uint32_t)0xFFFFFFFC)
00102 
00103 /* --- DCTRL Register ---*/
00104 
00105 /* SDIO DCTRL Clear Mask */
00106 #define DCTRL_CLEAR_MASK         ((uint32_t)0xFFFFFF08)
00107 
00108 /* --- CMD Register ---*/
00109 
00110 /* CMD Register clear mask */
00111 #define CMD_CLEAR_MASK           ((uint32_t)0xFFFFF800)
00112 
00113 /* SDIO RESP Registers Address */
00114 #define SDIO_RESP_ADDR           ((uint32_t)(SDIO_BASE + 0x14))
00115 
00161 void SDIO_DeInit(void)
00162 {
00163   SDIO->POWER = 0x00000000;
00164   SDIO->CLKCR = 0x00000000;
00165   SDIO->ARG = 0x00000000;
00166   SDIO->CMD = 0x00000000;
00167   SDIO->DTIMER = 0x00000000;
00168   SDIO->DLEN = 0x00000000;
00169   SDIO->DCTRL = 0x00000000;
00170   SDIO->ICR = 0x00C007FF;
00171   SDIO->MASK = 0x00000000;
00172 }
00173 
00181 void SDIO_Init(SDIO_InitTypeDef* SDIO_InitStruct)
00182 {
00183   uint32_t tmpreg = 0;
00184     
00185   /* Check the parameters */
00186   assert_param(IS_SDIO_CLOCK_EDGE(SDIO_InitStruct->SDIO_ClockEdge));
00187   assert_param(IS_SDIO_CLOCK_BYPASS(SDIO_InitStruct->SDIO_ClockBypass));
00188   assert_param(IS_SDIO_CLOCK_POWER_SAVE(SDIO_InitStruct->SDIO_ClockPowerSave));
00189   assert_param(IS_SDIO_BUS_WIDE(SDIO_InitStruct->SDIO_BusWide));
00190   assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(SDIO_InitStruct->SDIO_HardwareFlowControl)); 
00191    
00192 /*---------------------------- SDIO CLKCR Configuration ------------------------*/  
00193   /* Get the SDIO CLKCR value */
00194   tmpreg = SDIO->CLKCR;
00195   
00196   /* Clear CLKDIV, PWRSAV, BYPASS, WIDBUS, NEGEDGE, HWFC_EN bits */
00197   tmpreg &= CLKCR_CLEAR_MASK;
00198   
00199   /* Set CLKDIV bits according to SDIO_ClockDiv value */
00200   /* Set PWRSAV bit according to SDIO_ClockPowerSave value */
00201   /* Set BYPASS bit according to SDIO_ClockBypass value */
00202   /* Set WIDBUS bits according to SDIO_BusWide value */
00203   /* Set NEGEDGE bits according to SDIO_ClockEdge value */
00204   /* Set HWFC_EN bits according to SDIO_HardwareFlowControl value */
00205   tmpreg |= (SDIO_InitStruct->SDIO_ClockDiv  | SDIO_InitStruct->SDIO_ClockPowerSave |
00206              SDIO_InitStruct->SDIO_ClockBypass | SDIO_InitStruct->SDIO_BusWide |
00207              SDIO_InitStruct->SDIO_ClockEdge | SDIO_InitStruct->SDIO_HardwareFlowControl); 
00208   
00209   /* Write to SDIO CLKCR */
00210   SDIO->CLKCR = tmpreg;
00211 }
00212 
00219 void SDIO_StructInit(SDIO_InitTypeDef* SDIO_InitStruct)
00220 {
00221   /* SDIO_InitStruct members default value */
00222   SDIO_InitStruct->SDIO_ClockDiv = 0x00;
00223   SDIO_InitStruct->SDIO_ClockEdge = SDIO_ClockEdge_Rising;
00224   SDIO_InitStruct->SDIO_ClockBypass = SDIO_ClockBypass_Disable;
00225   SDIO_InitStruct->SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable;
00226   SDIO_InitStruct->SDIO_BusWide = SDIO_BusWide_1b;
00227   SDIO_InitStruct->SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable;
00228 }
00229 
00235 void SDIO_ClockCmd(FunctionalState NewState)
00236 {
00237   /* Check the parameters */
00238   assert_param(IS_FUNCTIONAL_STATE(NewState));
00239   
00240   *(__IO uint32_t *) CLKCR_CLKEN_BB = (uint32_t)NewState;
00241 }
00242 
00251 void SDIO_SetPowerState(uint32_t SDIO_PowerState)
00252 {
00253   /* Check the parameters */
00254   assert_param(IS_SDIO_POWER_STATE(SDIO_PowerState));
00255   
00256   SDIO->POWER &= PWR_PWRCTRL_MASK;
00257   SDIO->POWER |= SDIO_PowerState;
00258 }
00259 
00269 uint32_t SDIO_GetPowerState(void)
00270 {
00271   return (SDIO->POWER & (~PWR_PWRCTRL_MASK));
00272 }
00273 
00307 void SDIO_ITConfig(uint32_t SDIO_IT, FunctionalState NewState)
00308 {
00309   /* Check the parameters */
00310   assert_param(IS_SDIO_IT(SDIO_IT));
00311   assert_param(IS_FUNCTIONAL_STATE(NewState));
00312   
00313   if (NewState != DISABLE)
00314   {
00315     /* Enable the SDIO interrupts */
00316     SDIO->MASK |= SDIO_IT;
00317   }
00318   else
00319   {
00320     /* Disable the SDIO interrupts */
00321     SDIO->MASK &= ~SDIO_IT;
00322   } 
00323 }
00324 
00331 void SDIO_DMACmd(FunctionalState NewState)
00332 {
00333   /* Check the parameters */
00334   assert_param(IS_FUNCTIONAL_STATE(NewState));
00335   
00336   *(__IO uint32_t *) DCTRL_DMAEN_BB = (uint32_t)NewState;
00337 }
00338 
00346 void SDIO_SendCommand(SDIO_CmdInitTypeDef *SDIO_CmdInitStruct)
00347 {
00348   uint32_t tmpreg = 0;
00349   
00350   /* Check the parameters */
00351   assert_param(IS_SDIO_CMD_INDEX(SDIO_CmdInitStruct->SDIO_CmdIndex));
00352   assert_param(IS_SDIO_RESPONSE(SDIO_CmdInitStruct->SDIO_Response));
00353   assert_param(IS_SDIO_WAIT(SDIO_CmdInitStruct->SDIO_Wait));
00354   assert_param(IS_SDIO_CPSM(SDIO_CmdInitStruct->SDIO_CPSM));
00355   
00356 /*---------------------------- SDIO ARG Configuration ------------------------*/
00357   /* Set the SDIO Argument value */
00358   SDIO->ARG = SDIO_CmdInitStruct->SDIO_Argument;
00359   
00360 /*---------------------------- SDIO CMD Configuration ------------------------*/  
00361   /* Get the SDIO CMD value */
00362   tmpreg = SDIO->CMD;
00363   /* Clear CMDINDEX, WAITRESP, WAITINT, WAITPEND, CPSMEN bits */
00364   tmpreg &= CMD_CLEAR_MASK;
00365   /* Set CMDINDEX bits according to SDIO_CmdIndex value */
00366   /* Set WAITRESP bits according to SDIO_Response value */
00367   /* Set WAITINT and WAITPEND bits according to SDIO_Wait value */
00368   /* Set CPSMEN bits according to SDIO_CPSM value */
00369   tmpreg |= (uint32_t)SDIO_CmdInitStruct->SDIO_CmdIndex | SDIO_CmdInitStruct->SDIO_Response
00370            | SDIO_CmdInitStruct->SDIO_Wait | SDIO_CmdInitStruct->SDIO_CPSM;
00371   
00372   /* Write to SDIO CMD */
00373   SDIO->CMD = tmpreg;
00374 }
00375 
00382 void SDIO_CmdStructInit(SDIO_CmdInitTypeDef* SDIO_CmdInitStruct)
00383 {
00384   /* SDIO_CmdInitStruct members default value */
00385   SDIO_CmdInitStruct->SDIO_Argument = 0x00;
00386   SDIO_CmdInitStruct->SDIO_CmdIndex = 0x00;
00387   SDIO_CmdInitStruct->SDIO_Response = SDIO_Response_No;
00388   SDIO_CmdInitStruct->SDIO_Wait = SDIO_Wait_No;
00389   SDIO_CmdInitStruct->SDIO_CPSM = SDIO_CPSM_Disable;
00390 }
00391 
00397 uint8_t SDIO_GetCommandResponse(void)
00398 {
00399   return (uint8_t)(SDIO->RESPCMD);
00400 }
00401 
00412 uint32_t SDIO_GetResponse(uint32_t SDIO_RESP)
00413 {
00414   __IO uint32_t tmp = 0;
00415 
00416   /* Check the parameters */
00417   assert_param(IS_SDIO_RESP(SDIO_RESP));
00418 
00419   tmp = SDIO_RESP_ADDR + SDIO_RESP;
00420   
00421   return (*(__IO uint32_t *) tmp); 
00422 }
00423 
00431 void SDIO_DataConfig(SDIO_DataInitTypeDef* SDIO_DataInitStruct)
00432 {
00433   uint32_t tmpreg = 0;
00434   
00435   /* Check the parameters */
00436   assert_param(IS_SDIO_DATA_LENGTH(SDIO_DataInitStruct->SDIO_DataLength));
00437   assert_param(IS_SDIO_BLOCK_SIZE(SDIO_DataInitStruct->SDIO_DataBlockSize));
00438   assert_param(IS_SDIO_TRANSFER_DIR(SDIO_DataInitStruct->SDIO_TransferDir));
00439   assert_param(IS_SDIO_TRANSFER_MODE(SDIO_DataInitStruct->SDIO_TransferMode));
00440   assert_param(IS_SDIO_DPSM(SDIO_DataInitStruct->SDIO_DPSM));
00441 
00442 /*---------------------------- SDIO DTIMER Configuration ---------------------*/
00443   /* Set the SDIO Data TimeOut value */
00444   SDIO->DTIMER = SDIO_DataInitStruct->SDIO_DataTimeOut;
00445 
00446 /*---------------------------- SDIO DLEN Configuration -----------------------*/
00447   /* Set the SDIO DataLength value */
00448   SDIO->DLEN = SDIO_DataInitStruct->SDIO_DataLength;
00449 
00450 /*---------------------------- SDIO DCTRL Configuration ----------------------*/  
00451   /* Get the SDIO DCTRL value */
00452   tmpreg = SDIO->DCTRL;
00453   /* Clear DEN, DTMODE, DTDIR and DBCKSIZE bits */
00454   tmpreg &= DCTRL_CLEAR_MASK;
00455   /* Set DEN bit according to SDIO_DPSM value */
00456   /* Set DTMODE bit according to SDIO_TransferMode value */
00457   /* Set DTDIR bit according to SDIO_TransferDir value */
00458   /* Set DBCKSIZE bits according to SDIO_DataBlockSize value */
00459   tmpreg |= (uint32_t)SDIO_DataInitStruct->SDIO_DataBlockSize | SDIO_DataInitStruct->SDIO_TransferDir
00460            | SDIO_DataInitStruct->SDIO_TransferMode | SDIO_DataInitStruct->SDIO_DPSM;
00461 
00462   /* Write to SDIO DCTRL */
00463   SDIO->DCTRL = tmpreg;
00464 }
00465 
00472 void SDIO_DataStructInit(SDIO_DataInitTypeDef* SDIO_DataInitStruct)
00473 {
00474   /* SDIO_DataInitStruct members default value */
00475   SDIO_DataInitStruct->SDIO_DataTimeOut = 0xFFFFFFFF;
00476   SDIO_DataInitStruct->SDIO_DataLength = 0x00;
00477   SDIO_DataInitStruct->SDIO_DataBlockSize = SDIO_DataBlockSize_1b;
00478   SDIO_DataInitStruct->SDIO_TransferDir = SDIO_TransferDir_ToCard;
00479   SDIO_DataInitStruct->SDIO_TransferMode = SDIO_TransferMode_Block;  
00480   SDIO_DataInitStruct->SDIO_DPSM = SDIO_DPSM_Disable;
00481 }
00482 
00488 uint32_t SDIO_GetDataCounter(void)
00489 { 
00490   return SDIO->DCOUNT;
00491 }
00492 
00498 uint32_t SDIO_ReadData(void)
00499 { 
00500   return SDIO->FIFO;
00501 }
00502 
00508 void SDIO_WriteData(uint32_t Data)
00509 { 
00510   SDIO->FIFO = Data;
00511 }
00512 
00518 uint32_t SDIO_GetFIFOCount(void)
00519 { 
00520   return SDIO->FIFOCNT;
00521 }
00522 
00529 void SDIO_StartSDIOReadWait(FunctionalState NewState)
00530 { 
00531   /* Check the parameters */
00532   assert_param(IS_FUNCTIONAL_STATE(NewState));
00533   
00534   *(__IO uint32_t *) DCTRL_RWSTART_BB = (uint32_t) NewState;
00535 }
00536 
00543 void SDIO_StopSDIOReadWait(FunctionalState NewState)
00544 { 
00545   /* Check the parameters */
00546   assert_param(IS_FUNCTIONAL_STATE(NewState));
00547   
00548   *(__IO uint32_t *) DCTRL_RWSTOP_BB = (uint32_t) NewState;
00549 }
00550 
00559 void SDIO_SetSDIOReadWaitMode(uint32_t SDIO_ReadWaitMode)
00560 {
00561   /* Check the parameters */
00562   assert_param(IS_SDIO_READWAIT_MODE(SDIO_ReadWaitMode));
00563   
00564   *(__IO uint32_t *) DCTRL_RWMOD_BB = SDIO_ReadWaitMode;
00565 }
00566 
00573 void SDIO_SetSDIOOperation(FunctionalState NewState)
00574 { 
00575   /* Check the parameters */
00576   assert_param(IS_FUNCTIONAL_STATE(NewState));
00577   
00578   *(__IO uint32_t *) DCTRL_SDIOEN_BB = (uint32_t)NewState;
00579 }
00580 
00587 void SDIO_SendSDIOSuspendCmd(FunctionalState NewState)
00588 { 
00589   /* Check the parameters */
00590   assert_param(IS_FUNCTIONAL_STATE(NewState));
00591   
00592   *(__IO uint32_t *) CMD_SDIOSUSPEND_BB = (uint32_t)NewState;
00593 }
00594 
00601 void SDIO_CommandCompletionCmd(FunctionalState NewState)
00602 { 
00603   /* Check the parameters */
00604   assert_param(IS_FUNCTIONAL_STATE(NewState));
00605   
00606   *(__IO uint32_t *) CMD_ENCMDCOMPL_BB = (uint32_t)NewState;
00607 }
00608 
00614 void SDIO_CEATAITCmd(FunctionalState NewState)
00615 { 
00616   /* Check the parameters */
00617   assert_param(IS_FUNCTIONAL_STATE(NewState));
00618   
00619   *(__IO uint32_t *) CMD_NIEN_BB = (uint32_t)((~((uint32_t)NewState)) & ((uint32_t)0x1));
00620 }
00621 
00627 void SDIO_SendCEATACmd(FunctionalState NewState)
00628 { 
00629   /* Check the parameters */
00630   assert_param(IS_FUNCTIONAL_STATE(NewState));
00631   
00632   *(__IO uint32_t *) CMD_ATACMD_BB = (uint32_t)NewState;
00633 }
00634 
00666 FlagStatus SDIO_GetFlagStatus(uint32_t SDIO_FLAG)
00667 { 
00668   FlagStatus bitstatus = RESET;
00669   
00670   /* Check the parameters */
00671   assert_param(IS_SDIO_FLAG(SDIO_FLAG));
00672   
00673   if ((SDIO->STA & SDIO_FLAG) != (uint32_t)RESET)
00674   {
00675     bitstatus = SET;
00676   }
00677   else
00678   {
00679     bitstatus = RESET;
00680   }
00681   return bitstatus;
00682 }
00683 
00704 void SDIO_ClearFlag(uint32_t SDIO_FLAG)
00705 { 
00706   /* Check the parameters */
00707   assert_param(IS_SDIO_CLEAR_FLAG(SDIO_FLAG));
00708    
00709   SDIO->ICR = SDIO_FLAG;
00710 }
00711 
00743 ITStatus SDIO_GetITStatus(uint32_t SDIO_IT)
00744 { 
00745   ITStatus bitstatus = RESET;
00746   
00747   /* Check the parameters */
00748   assert_param(IS_SDIO_GET_IT(SDIO_IT));
00749   if ((SDIO->STA & SDIO_IT) != (uint32_t)RESET)  
00750   {
00751     bitstatus = SET;
00752   }
00753   else
00754   {
00755     bitstatus = RESET;
00756   }
00757   return bitstatus;
00758 }
00759 
00779 void SDIO_ClearITPendingBit(uint32_t SDIO_IT)
00780 { 
00781   /* Check the parameters */
00782   assert_param(IS_SDIO_CLEAR_IT(SDIO_IT));
00783    
00784   SDIO->ICR = SDIO_IT;
00785 }
00786 
00799 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/