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