sdio.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**
  2. ******************************************************************************
  3. * File Name : SDIO.c
  4. * Description : This file provides code for the configuration
  5. * of the SDIO instances.
  6. ******************************************************************************
  7. * This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * Copyright (c) 2018 STMicroelectronics International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /* Includes ------------------------------------------------------------------*/
  50. #include "sdio.h"
  51. #include "gpio.h"
  52. #include "dma.h"
  53. /* USER CODE BEGIN 0 */
  54. /* USER CODE END 0 */
  55. SD_HandleTypeDef hsd;
  56. DMA_HandleTypeDef hdma_sdio;
  57. /* SDIO init function */
  58. void MX_SDIO_SD_Init(void)
  59. {
  60. hsd.Instance = SDIO;
  61. hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;
  62. hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_ENABLE;
  63. hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;
  64. hsd.Init.BusWide = SDIO_BUS_WIDE_1B;
  65. hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_ENABLE;
  66. hsd.Init.ClockDiv = 1;
  67. }
  68. void HAL_SD_MspInit(SD_HandleTypeDef* sdHandle)
  69. {
  70. GPIO_InitTypeDef GPIO_InitStruct;
  71. if(sdHandle->Instance==SDIO)
  72. {
  73. /* USER CODE BEGIN SDIO_MspInit 0 */
  74. /* USER CODE END SDIO_MspInit 0 */
  75. /* SDIO clock enable */
  76. __HAL_RCC_SDIO_CLK_ENABLE();
  77. /**SDIO GPIO Configuration
  78. PC8 ------> SDIO_D0
  79. PC9 ------> SDIO_D1
  80. PC10 ------> SDIO_D2
  81. PC11 ------> SDIO_D3
  82. PC12 ------> SDIO_CK
  83. PD2 ------> SDIO_CMD
  84. */
  85. GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11;
  86. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  87. GPIO_InitStruct.Pull = GPIO_PULLUP;
  88. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  89. GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
  90. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  91. GPIO_InitStruct.Pin = GPIO_PIN_12;
  92. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  93. GPIO_InitStruct.Pull = GPIO_NOPULL;
  94. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  95. GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
  96. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  97. GPIO_InitStruct.Pin = GPIO_PIN_2;
  98. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  99. GPIO_InitStruct.Pull = GPIO_PULLUP;
  100. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  101. GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
  102. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  103. /* SDIO DMA Init */
  104. /* SDIO Init */
  105. hdma_sdio.Instance = DMA2_Stream3;
  106. hdma_sdio.Init.Channel = DMA_CHANNEL_4;
  107. hdma_sdio.Init.Direction = DMA_PERIPH_TO_MEMORY;
  108. hdma_sdio.Init.PeriphInc = DMA_PINC_DISABLE;
  109. hdma_sdio.Init.MemInc = DMA_MINC_ENABLE;
  110. hdma_sdio.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
  111. hdma_sdio.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
  112. hdma_sdio.Init.Mode = DMA_PFCTRL;
  113. hdma_sdio.Init.Priority = DMA_PRIORITY_LOW;
  114. hdma_sdio.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
  115. hdma_sdio.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
  116. hdma_sdio.Init.MemBurst = DMA_MBURST_INC4;
  117. hdma_sdio.Init.PeriphBurst = DMA_PBURST_INC4;
  118. if (HAL_DMA_Init(&hdma_sdio) != HAL_OK)
  119. {
  120. _Error_Handler(__FILE__, __LINE__);
  121. }
  122. /* Several peripheral DMA handle pointers point to the same DMA handle.
  123. Be aware that there is only one stream to perform all the requested DMAs. */
  124. /* Be sure to change transfer direction before calling
  125. HAL_SD_ReadBlocks_DMA or HAL_SD_WriteBlocks_DMA. */
  126. __HAL_LINKDMA(sdHandle,hdmarx,hdma_sdio);
  127. __HAL_LINKDMA(sdHandle,hdmatx,hdma_sdio);
  128. /* SDIO interrupt Init */
  129. HAL_NVIC_SetPriority(SDIO_IRQn, 0, 0);
  130. HAL_NVIC_EnableIRQ(SDIO_IRQn);
  131. /* USER CODE BEGIN SDIO_MspInit 1 */
  132. /* USER CODE END SDIO_MspInit 1 */
  133. }
  134. }
  135. void HAL_SD_MspDeInit(SD_HandleTypeDef* sdHandle)
  136. {
  137. if(sdHandle->Instance==SDIO)
  138. {
  139. /* USER CODE BEGIN SDIO_MspDeInit 0 */
  140. /* USER CODE END SDIO_MspDeInit 0 */
  141. /* Peripheral clock disable */
  142. __HAL_RCC_SDIO_CLK_DISABLE();
  143. /**SDIO GPIO Configuration
  144. PC8 ------> SDIO_D0
  145. PC9 ------> SDIO_D1
  146. PC10 ------> SDIO_D2
  147. PC11 ------> SDIO_D3
  148. PC12 ------> SDIO_CK
  149. PD2 ------> SDIO_CMD
  150. */
  151. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11
  152. |GPIO_PIN_12);
  153. HAL_GPIO_DeInit(GPIOD, GPIO_PIN_2);
  154. /* SDIO DMA DeInit */
  155. HAL_DMA_DeInit(sdHandle->hdmarx);
  156. HAL_DMA_DeInit(sdHandle->hdmatx);
  157. /* SDIO interrupt Deinit */
  158. HAL_NVIC_DisableIRQ(SDIO_IRQn);
  159. /* USER CODE BEGIN SDIO_MspDeInit 1 */
  160. /* USER CODE END SDIO_MspDeInit 1 */
  161. }
  162. }
  163. /* USER CODE BEGIN 1 */
  164. /* USER CODE END 1 */
  165. /**
  166. * @}
  167. */
  168. /**
  169. * @}
  170. */
  171. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/