]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil/ST_Library/stm32f7xx_ll_sdmmc.c
Final V8.2.1 release ready for tagging:
[freertos] / FreeRTOS / Demo / CORTEX_M7_STM32F7_STM32756G-EVAL_IAR_Keil / ST_Library / stm32f7xx_ll_sdmmc.c
1 /**\r
2   ******************************************************************************\r
3   * @file    stm32f7xx_ll_sdmmc.c\r
4   * @author  MCD Application Team\r
5   * @version V1.0.0RC1\r
6   * @date    24-March-2015\r
7   * @brief   SDMMC Low Layer HAL module driver.\r
8   *    \r
9   *          This file provides firmware functions to manage the following \r
10   *          functionalities of the SDMMC peripheral:\r
11   *           + Initialization/de-initialization functions\r
12   *           + I/O operation functions\r
13   *           + Peripheral Control functions \r
14   *           + Peripheral State functions\r
15   *         \r
16   @verbatim\r
17   ==============================================================================\r
18                        ##### SDMMC peripheral features #####\r
19   ==============================================================================        \r
20     [..] The SD/SDMMC MMC card host interface (SDMMC) provides an interface between the APB2\r
21          peripheral bus and MultiMedia cards (MMCs), SD memory cards, SDMMC cards and CE-ATA\r
22          devices.\r
23     \r
24     [..] The SDMMC features include the following:\r
25          (+) Full compliance with MultiMedia Card System Specification Version 4.2. Card support\r
26              for three different databus modes: 1-bit (default), 4-bit and 8-bit\r
27          (+) Full compatibility with previous versions of MultiMedia Cards (forward compatibility)\r
28          (+) Full compliance with SD Memory Card Specifications Version 2.0\r
29          (+) Full compliance with SD I/O Card Specification Version 2.0: card support for two\r
30              different data bus modes: 1-bit (default) and 4-bit\r
31          (+) Full support of the CE-ATA features (full compliance with CE-ATA digital protocol\r
32              Rev1.1)\r
33          (+) Data transfer up to 48 MHz for the 8 bit mode\r
34          (+) Data and command output enable signals to control external bidirectional drivers.\r
35                  \r
36    \r
37                            ##### How to use this driver #####\r
38   ==============================================================================\r
39     [..]\r
40       This driver is a considered as a driver of service for external devices drivers \r
41       that interfaces with the SDMMC peripheral.\r
42       According to the device used (SD card/ MMC card / SDMMC card ...), a set of APIs \r
43       is used in the device's driver to perform SDMMC operations and functionalities.\r
44    \r
45       This driver is almost transparent for the final user, it is only used to implement other\r
46       functionalities of the external device.\r
47    \r
48     [..]\r
49       (+) The SDMMC clock (SDMMCCLK = 48 MHz) is coming from a specific output of PLL \r
50           (PLL48CLK). Before start working with SDMMC peripheral make sure that the\r
51           PLL is well configured.\r
52           The SDMMC peripheral uses two clock signals:\r
53           (++) SDMMC adapter clock (SDMMCCLK = 48 MHz)\r
54           (++) APB2 bus clock (PCLK2)\r
55        \r
56           -@@- PCLK2 and SDMMC_CK clock frequencies must respect the following condition:\r
57                Frequency(PCLK2) >= (3 / 8 x Frequency(SDMMC_CK))\r
58   \r
59       (+) Enable/Disable peripheral clock using RCC peripheral macros related to SDMMC\r
60           peripheral.\r
61 \r
62       (+) Enable the Power ON State using the SDMMC_PowerState_ON(SDMMCx) \r
63           function and disable it using the function SDMMC_PowerState_OFF(SDMMCx).\r
64                 \r
65       (+) Enable/Disable the clock using the __SDMMC_ENABLE()/__SDMMC_DISABLE() macros.\r
66   \r
67       (+) Enable/Disable the peripheral interrupts using the macros __SDMMC_ENABLE_IT(hSDMMC, IT) \r
68           and __SDMMC_DISABLE_IT(hSDMMC, IT) if you need to use interrupt mode. \r
69   \r
70       (+) When using the DMA mode \r
71           (++) Configure the DMA in the MSP layer of the external device\r
72           (++) Active the needed channel Request \r
73           (++) Enable the DMA using __SDMMC_DMA_ENABLE() macro or Disable it using the macro\r
74                __SDMMC_DMA_DISABLE().\r
75   \r
76       (+) To control the CPSM (Command Path State Machine) and send \r
77           commands to the card use the SDMMC_SendCommand(SDMMCx), \r
78           SDMMC_GetCommandResponse() and SDMMC_GetResponse() functions. First, user has\r
79           to fill the command structure (pointer to SDMMC_CmdInitTypeDef) according \r
80           to the selected command to be sent.\r
81           The parameters that should be filled are:\r
82            (++) Command Argument\r
83            (++) Command Index\r
84            (++) Command Response type\r
85            (++) Command Wait\r
86            (++) CPSM Status (Enable or Disable).\r
87   \r
88           -@@- To check if the command is well received, read the SDMMC_CMDRESP\r
89               register using the SDMMC_GetCommandResponse().\r
90               The SDMMC responses registers (SDMMC_RESP1 to SDMMC_RESP2), use the\r
91               SDMMC_GetResponse() function.\r
92   \r
93       (+) To control the DPSM (Data Path State Machine) and send/receive \r
94            data to/from the card use the SDMMC_DataConfig(), SDMMC_GetDataCounter(), \r
95           SDMMC_ReadFIFO(), DIO_WriteFIFO() and SDMMC_GetFIFOCount() functions.\r
96   \r
97     *** Read Operations ***\r
98     =======================\r
99     [..]\r
100       (#) First, user has to fill the data structure (pointer to\r
101           SDMMC_DataInitTypeDef) according to the selected data type to be received.\r
102           The parameters that should be filled are:\r
103            (++) Data TimeOut\r
104            (++) Data Length\r
105            (++) Data Block size\r
106            (++) Data Transfer direction: should be from card (To SDMMC)\r
107            (++) Data Transfer mode\r
108            (++) DPSM Status (Enable or Disable)\r
109                                      \r
110       (#) Configure the SDMMC resources to receive the data from the card\r
111           according to selected transfer mode (Refer to Step 8, 9 and 10).\r
112   \r
113       (#) Send the selected Read command (refer to step 11).\r
114                     \r
115       (#) Use the SDMMC flags/interrupts to check the transfer status.\r
116   \r
117     *** Write Operations ***\r
118     ========================\r
119     [..]\r
120      (#) First, user has to fill the data structure (pointer to\r
121          SDMMC_DataInitTypeDef) according to the selected data type to be received.\r
122          The parameters that should be filled are:\r
123           (++) Data TimeOut\r
124           (++) Data Length\r
125           (++) Data Block size\r
126           (++) Data Transfer direction:  should be to card (To CARD)\r
127           (++) Data Transfer mode\r
128           (++) DPSM Status (Enable or Disable)\r
129   \r
130      (#) Configure the SDMMC resources to send the data to the card according to \r
131          selected transfer mode.\r
132                      \r
133      (#) Send the selected Write command.\r
134                     \r
135      (#) Use the SDMMC flags/interrupts to check the transfer status.\r
136   \r
137   @endverbatim\r
138   ******************************************************************************\r
139   * @attention\r
140   *\r
141   * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>\r
142   *\r
143   * Redistribution and use in source and binary forms, with or without modification,\r
144   * are permitted provided that the following conditions are met:\r
145   *   1. Redistributions of source code must retain the above copyright notice,\r
146   *      this list of conditions and the following disclaimer.\r
147   *   2. Redistributions in binary form must reproduce the above copyright notice,\r
148   *      this list of conditions and the following disclaimer in the documentation\r
149   *      and/or other materials provided with the distribution.\r
150   *   3. Neither the name of STMicroelectronics nor the names of its contributors\r
151   *      may be used to endorse or promote products derived from this software\r
152   *      without specific prior written permission.\r
153   *\r
154   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"\r
155   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE\r
156   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE\r
157   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE\r
158   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL\r
159   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
160   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER\r
161   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,\r
162   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\r
163   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
164   *\r
165   ******************************************************************************\r
166   */ \r
167 \r
168 /* Includes ------------------------------------------------------------------*/\r
169 #include "stm32f7xx_hal.h"\r
170 \r
171 /** @addtogroup STM32F7xx_HAL_Driver\r
172   * @{\r
173   */\r
174 \r
175 /** @defgroup SDMMC_LL SDMMC Low Layer\r
176   * @brief Low layer module for SD\r
177   * @{\r
178   */\r
179 \r
180 #if defined (HAL_SD_MODULE_ENABLED) || defined(HAL_MMC_MODULE_ENABLED)\r
181 \r
182 /* Private typedef -----------------------------------------------------------*/\r
183 /* Private define ------------------------------------------------------------*/\r
184 /* Private macro -------------------------------------------------------------*/\r
185 /* Private variables ---------------------------------------------------------*/\r
186 /* Private function prototypes -----------------------------------------------*/\r
187 /* Exported functions --------------------------------------------------------*/\r
188 \r
189 /** @defgroup SDMMC_LL_Exported_Functions SDMMC Low Layer Exported Functions\r
190   * @{\r
191   */\r
192 \r
193 /** @defgroup HAL_SDMMC_LL_Group1 Initialization de-initialization functions \r
194  *  @brief    Initialization and Configuration functions \r
195  *\r
196 @verbatim    \r
197  ===============================================================================\r
198               ##### Initialization/de-initialization functions #####\r
199  ===============================================================================\r
200     [..]  This section provides functions allowing to:\r
201  \r
202 @endverbatim\r
203   * @{\r
204   */\r
205 \r
206 /**\r
207   * @brief  Initializes the SDMMC according to the specified\r
208   *         parameters in the SDMMC_InitTypeDef and create the associated handle.\r
209   * @param  SDMMCx: Pointer to SDMMC register base\r
210   * @param  Init: SDMMC initialization structure   \r
211   * @retval HAL status\r
212   */\r
213 HAL_StatusTypeDef SDMMC_Init(SDMMC_TypeDef *SDMMCx, SDMMC_InitTypeDef Init)\r
214 {\r
215   uint32_t tmpreg = 0; \r
216 \r
217   /* Check the parameters */\r
218   assert_param(IS_SDMMC_ALL_INSTANCE(SDMMCx));\r
219   assert_param(IS_SDMMC_CLOCK_EDGE(Init.ClockEdge)); \r
220   assert_param(IS_SDMMC_CLOCK_BYPASS(Init.ClockBypass));\r
221   assert_param(IS_SDMMC_CLOCK_POWER_SAVE(Init.ClockPowerSave));\r
222   assert_param(IS_SDMMC_BUS_WIDE(Init.BusWide));\r
223   assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(Init.HardwareFlowControl));\r
224   assert_param(IS_SDMMC_CLKDIV(Init.ClockDiv));\r
225   \r
226   /* Set SDMMC configuration parameters */\r
227   tmpreg |= (Init.ClockEdge           |\\r
228              Init.ClockBypass         |\\r
229              Init.ClockPowerSave      |\\r
230              Init.BusWide             |\\r
231              Init.HardwareFlowControl |\\r
232              Init.ClockDiv\r
233              ); \r
234   \r
235   /* Write to SDMMC CLKCR */\r
236   MODIFY_REG(SDMMCx->CLKCR, CLKCR_CLEAR_MASK, tmpreg);  \r
237 \r
238   return HAL_OK;\r
239 }\r
240 \r
241 \r
242 \r
243 /**\r
244   * @}\r
245   */\r
246 \r
247 /** @defgroup HAL_SDMMC_LL_Group2 IO operation functions \r
248  *  @brief   Data transfers functions \r
249  *\r
250 @verbatim   \r
251  ===============================================================================\r
252                       ##### I/O operation functions #####\r
253  ===============================================================================  \r
254     [..]\r
255     This subsection provides a set of functions allowing to manage the SDMMC data \r
256     transfers.\r
257 \r
258 @endverbatim\r
259   * @{\r
260   */\r
261 \r
262 /**\r
263   * @brief  Read data (word) from Rx FIFO in blocking mode (polling) \r
264   * @param  SDMMCx: Pointer to SDMMC register base\r
265   * @retval HAL status\r
266   */\r
267 uint32_t SDMMC_ReadFIFO(SDMMC_TypeDef *SDMMCx)\r
268 {\r
269   /* Read data from Rx FIFO */ \r
270   return (SDMMCx->FIFO);\r
271 }\r
272 \r
273 /**\r
274   * @brief  Write data (word) to Tx FIFO in blocking mode (polling) \r
275   * @param  SDMMCx: Pointer to SDMMC register base\r
276   * @param  pWriteData: pointer to data to write\r
277   * @retval HAL status\r
278   */\r
279 HAL_StatusTypeDef SDMMC_WriteFIFO(SDMMC_TypeDef *SDMMCx, uint32_t *pWriteData)\r
280\r
281   /* Write data to FIFO */ \r
282   SDMMCx->FIFO = *pWriteData;\r
283 \r
284   return HAL_OK;\r
285 }\r
286 \r
287 /**\r
288   * @}\r
289   */\r
290 \r
291 /** @defgroup HAL_SDMMC_LL_Group3 Peripheral Control functions \r
292  *  @brief   management functions \r
293  *\r
294 @verbatim   \r
295  ===============================================================================\r
296                       ##### Peripheral Control functions #####\r
297  ===============================================================================  \r
298     [..]\r
299     This subsection provides a set of functions allowing to control the SDMMC data \r
300     transfers.\r
301 \r
302 @endverbatim\r
303   * @{\r
304   */\r
305 \r
306 /**\r
307   * @brief  Set SDMMC Power state to ON. \r
308   * @param  SDMMCx: Pointer to SDMMC register base\r
309   * @retval HAL status\r
310   */\r
311 HAL_StatusTypeDef SDMMC_PowerState_ON(SDMMC_TypeDef *SDMMCx)\r
312 {  \r
313   /* Set power state to ON */ \r
314   SDMMCx->POWER = SDMMC_POWER_PWRCTRL;\r
315   \r
316   return HAL_OK; \r
317 }\r
318 \r
319 /**\r
320   * @brief  Set SDMMC Power state to OFF. \r
321   * @param  SDMMCx: Pointer to SDMMC register base\r
322   * @retval HAL status\r
323   */\r
324 HAL_StatusTypeDef SDMMC_PowerState_OFF(SDMMC_TypeDef *SDMMCx)\r
325 {\r
326   /* Set power state to OFF */\r
327   SDMMCx->POWER = (uint32_t)0x00000000;\r
328   \r
329   return HAL_OK;\r
330 }\r
331 \r
332 /**\r
333   * @brief  Get SDMMC Power state. \r
334   * @param  SDMMCx: Pointer to SDMMC register base\r
335   * @retval Power status of the controller. The returned value can be one of the \r
336   *         following values:\r
337   *            - 0x00: Power OFF\r
338   *            - 0x02: Power UP\r
339   *            - 0x03: Power ON \r
340   */\r
341 uint32_t SDMMC_GetPowerState(SDMMC_TypeDef *SDMMCx)  \r
342 {\r
343   return (SDMMCx->POWER & SDMMC_POWER_PWRCTRL);\r
344 }\r
345 \r
346 /**\r
347   * @brief  Configure the SDMMC command path according to the specified parameters in\r
348   *         SDMMC_CmdInitTypeDef structure and send the command \r
349   * @param  SDMMCx: Pointer to SDMMC register base\r
350   * @param  Command: pointer to a SDMMC_CmdInitTypeDef structure that contains \r
351   *         the configuration information for the SDMMC command\r
352   * @retval HAL status\r
353   */\r
354 HAL_StatusTypeDef SDMMC_SendCommand(SDMMC_TypeDef *SDMMCx, SDMMC_CmdInitTypeDef *Command)\r
355 {\r
356   uint32_t tmpreg = 0;\r
357   \r
358   /* Check the parameters */\r
359   assert_param(IS_SDMMC_CMD_INDEX(Command->CmdIndex));\r
360   assert_param(IS_SDMMC_RESPONSE(Command->Response));\r
361   assert_param(IS_SDMMC_WAIT(Command->WaitForInterrupt));\r
362   assert_param(IS_SDMMC_CPSM(Command->CPSM));\r
363 \r
364   /* Set the SDMMC Argument value */\r
365   SDMMCx->ARG = Command->Argument;\r
366 \r
367   /* Set SDMMC command parameters */\r
368   tmpreg |= (uint32_t)(Command->CmdIndex         |\\r
369                        Command->Response         |\\r
370                        Command->WaitForInterrupt |\\r
371                        Command->CPSM);\r
372   \r
373   /* Write to SDMMC CMD register */\r
374   MODIFY_REG(SDMMCx->CMD, CMD_CLEAR_MASK, tmpreg); \r
375   \r
376   return HAL_OK;  \r
377 }\r
378 \r
379 /**\r
380   * @brief  Return the command index of last command for which response received\r
381   * @param  SDMMCx: Pointer to SDMMC register base\r
382   * @retval Command index of the last command response received\r
383   */\r
384 uint8_t SDMMC_GetCommandResponse(SDMMC_TypeDef *SDMMCx)\r
385 {\r
386   return (uint8_t)(SDMMCx->RESPCMD);\r
387 }\r
388 \r
389 \r
390 /**\r
391   * @brief  Return the response received from the card for the last command\r
392   * @param  SDMMCx: Pointer to SDMMC register base    \r
393   * @param  Response: Specifies the SDMMC response register. \r
394   *          This parameter can be one of the following values:\r
395   *            @arg SDMMC_RESP1: Response Register 1\r
396   *            @arg SDMMC_RESP2: Response Register 2\r
397   *            @arg SDMMC_RESP3: Response Register 3\r
398   *            @arg SDMMC_RESP4: Response Register 4  \r
399   * @retval The Corresponding response register value\r
400   */\r
401 uint32_t SDMMC_GetResponse(SDMMC_TypeDef *SDMMCx, uint32_t Response)\r
402 {\r
403   __IO uint32_t tmp = 0;\r
404 \r
405   /* Check the parameters */\r
406   assert_param(IS_SDMMC_RESP(Response));\r
407   \r
408   /* Get the response */\r
409   tmp = (uint32_t)&(SDMMCx->RESP1) + Response;\r
410   \r
411   return (*(__IO uint32_t *) tmp);\r
412 }  \r
413 \r
414 /**\r
415   * @brief  Configure the SDMMC data path according to the specified \r
416   *         parameters in the SDMMC_DataInitTypeDef.\r
417   * @param  SDMMCx: Pointer to SDMMC register base  \r
418   * @param  Data : pointer to a SDMMC_DataInitTypeDef structure \r
419   *         that contains the configuration information for the SDMMC data.\r
420   * @retval HAL status\r
421   */\r
422 HAL_StatusTypeDef SDMMC_DataConfig(SDMMC_TypeDef *SDMMCx, SDMMC_DataInitTypeDef* Data)\r
423 {\r
424   uint32_t tmpreg = 0;\r
425   \r
426   /* Check the parameters */\r
427   assert_param(IS_SDMMC_DATA_LENGTH(Data->DataLength));\r
428   assert_param(IS_SDMMC_BLOCK_SIZE(Data->DataBlockSize));\r
429   assert_param(IS_SDMMC_TRANSFER_DIR(Data->TransferDir));\r
430   assert_param(IS_SDMMC_TRANSFER_MODE(Data->TransferMode));\r
431   assert_param(IS_SDMMC_DPSM(Data->DPSM));\r
432 \r
433   /* Set the SDMMC Data TimeOut value */\r
434   SDMMCx->DTIMER = Data->DataTimeOut;\r
435 \r
436   /* Set the SDMMC DataLength value */\r
437   SDMMCx->DLEN = Data->DataLength;\r
438 \r
439   /* Set the SDMMC data configuration parameters */\r
440   tmpreg |= (uint32_t)(Data->DataBlockSize |\\r
441                        Data->TransferDir   |\\r
442                        Data->TransferMode  |\\r
443                        Data->DPSM);\r
444   \r
445   /* Write to SDMMC DCTRL */\r
446   MODIFY_REG(SDMMCx->DCTRL, DCTRL_CLEAR_MASK, tmpreg);\r
447 \r
448   return HAL_OK;\r
449 \r
450 }\r
451 \r
452 /**\r
453   * @brief  Returns number of remaining data bytes to be transferred.\r
454   * @param  SDMMCx: Pointer to SDMMC register base\r
455   * @retval Number of remaining data bytes to be transferred\r
456   */\r
457 uint32_t SDMMC_GetDataCounter(SDMMC_TypeDef *SDMMCx)\r
458 {\r
459   return (SDMMCx->DCOUNT);\r
460 }\r
461 \r
462 /**\r
463   * @brief  Get the FIFO data\r
464   * @param  SDMMCx: Pointer to SDMMC register base \r
465   * @retval Data received\r
466   */\r
467 uint32_t SDMMC_GetFIFOCount(SDMMC_TypeDef *SDMMCx)\r
468 {\r
469   return (SDMMCx->FIFO);\r
470 }\r
471 \r
472 \r
473 /**\r
474   * @brief  Sets one of the two options of inserting read wait interval.\r
475   * @param  SDMMCx: Pointer to SDMMC register base   \r
476   * @param  SDMMC_ReadWaitMode: SDMMC Read Wait operation mode.\r
477   *          This parameter can be:\r
478   *            @arg SDMMC_READ_WAIT_MODE_CLK: Read Wait control by stopping SDMMCCLK\r
479   *            @arg SDMMC_READ_WAIT_MODE_DATA2: Read Wait control using SDMMC_DATA2\r
480   * @retval None\r
481   */\r
482 HAL_StatusTypeDef SDMMC_SetSDMMCReadWaitMode(SDMMC_TypeDef *SDMMCx, uint32_t SDMMC_ReadWaitMode)\r
483 {\r
484   /* Check the parameters */\r
485   assert_param(IS_SDMMC_READWAIT_MODE(SDMMC_ReadWaitMode));\r
486   \r
487   /* Set SDMMC read wait mode */\r
488   SDMMCx->DCTRL |= SDMMC_ReadWaitMode;\r
489   \r
490   return HAL_OK;  \r
491 }\r
492 \r
493 /**\r
494   * @}\r
495   */\r
496 \r
497 /**\r
498   * @}\r
499   */\r
500 \r
501 #endif /* (HAL_SD_MODULE_ENABLED) || (HAL_MMC_MODULE_ENABLED) */\r
502 /**\r
503   * @}\r
504   */\r
505 \r
506 /**\r
507   * @}\r
508   */\r
509 \r
510 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/\r