drv_qspi_flash.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-07-07 thread-liu first version
  9. */
  10. #include <board.h>
  11. #include <drv_qspi.h>
  12. #include <rtdevice.h>
  13. #include <rthw.h>
  14. #include <finsh.h>
  15. #ifdef BSP_USING_QSPI_FLASH
  16. #include "spi_flash.h"
  17. #include "spi_flash_sfud.h"
  18. /**
  19. * @brief QSPI MSP Initialization
  20. * This function configures the hardware resources used in this example
  21. * @param hqspi: QSPI handle pointer
  22. * @retval None
  23. */
  24. void HAL_QSPI_MspInit(QSPI_HandleTypeDef* hqspi)
  25. {
  26. GPIO_InitTypeDef GPIO_InitStruct = {0};
  27. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  28. if(hqspi->Instance==QUADSPI)
  29. {
  30. /* USER CODE BEGIN QUADSPI_MspInit 0 */
  31. if (IS_ENGINEERING_BOOT_MODE())
  32. {
  33. PeriphClkInit.Sdmmc12ClockSelection = RCC_QSPICLKSOURCE_ACLK;
  34. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_QSPI;
  35. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  36. {
  37. Error_Handler();
  38. }
  39. }
  40. /* USER CODE END QUADSPI_MspInit 0 */
  41. /* Peripheral clock enable */
  42. __HAL_RCC_QSPI_CLK_ENABLE();
  43. __HAL_RCC_GPIOF_CLK_ENABLE();
  44. __HAL_RCC_GPIOB_CLK_ENABLE();
  45. /**QUADSPI GPIO Configuration
  46. PF6 ------> QUADSPI_BK1_IO3
  47. PF7 ------> QUADSPI_BK1_IO2
  48. PF8 ------> QUADSPI_BK1_IO0
  49. PF9 ------> QUADSPI_BK1_IO1
  50. PF10 ------> QUADSPI_CLK
  51. PB6 ------> QUADSPI_BK1_NCS
  52. */
  53. GPIO_InitStruct.Pin = GPIO_PIN_10;
  54. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  55. GPIO_InitStruct.Pull = GPIO_NOPULL;
  56. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  57. GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI;
  58. HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
  59. GPIO_InitStruct.Pin = GPIO_PIN_6;
  60. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  61. GPIO_InitStruct.Pull = GPIO_PULLUP;
  62. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  63. GPIO_InitStruct.Alternate = GPIO_AF10_QUADSPI;
  64. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  65. GPIO_InitStruct.Pin = GPIO_PIN_7 | GPIO_PIN_6;
  66. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  67. GPIO_InitStruct.Pull = GPIO_NOPULL;
  68. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  69. GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI;
  70. HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
  71. GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9;
  72. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  73. GPIO_InitStruct.Pull = GPIO_NOPULL;
  74. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  75. GPIO_InitStruct.Alternate = GPIO_AF10_QUADSPI;
  76. HAL_GPIO_Init(GPIOF, &GPIO_InitStruct);
  77. /**QUADSPI GPIO Configuration
  78. PC0 ------> QUADSPI_BK2_NCS
  79. PH3 ------> QUADSPI_BK2_IO1
  80. PG7 ------> QUADSPI_BK2_IO3
  81. PG10 ------> QUADSPI_BK2_IO2
  82. PH2 ------> QUADSPI_BK2_IO0
  83. */
  84. GPIO_InitStruct.Pin = GPIO_PIN_0;
  85. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  86. GPIO_InitStruct.Pull = GPIO_PULLUP;
  87. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  88. GPIO_InitStruct.Alternate = GPIO_AF10_QUADSPI;
  89. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  90. GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_2;
  91. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  92. GPIO_InitStruct.Pull = GPIO_NOPULL;
  93. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  94. GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI;
  95. HAL_GPIO_Init(GPIOH, &GPIO_InitStruct);
  96. GPIO_InitStruct.Pin = GPIO_PIN_7|GPIO_PIN_10;
  97. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  98. GPIO_InitStruct.Pull = GPIO_NOPULL;
  99. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
  100. GPIO_InitStruct.Alternate = GPIO_AF11_QUADSPI;
  101. HAL_GPIO_Init(GPIOG, &GPIO_InitStruct);
  102. /* USER CODE BEGIN QUADSPI_MspInit 1 */
  103. /* USER CODE END QUADSPI_MspInit 1 */
  104. }
  105. }
  106. /**
  107. * @brief QSPI MSP De-Initialization
  108. * This function freeze the hardware resources used in this example
  109. * @param hqspi: QSPI handle pointer
  110. * @retval None
  111. */
  112. void HAL_QSPI_MspDeInit(QSPI_HandleTypeDef* hqspi)
  113. {
  114. if(hqspi->Instance==QUADSPI)
  115. {
  116. /* USER CODE BEGIN QUADSPI_MspDeInit 0 */
  117. /* USER CODE END QUADSPI_MspDeInit 0 */
  118. /* Peripheral clock disable */
  119. __HAL_RCC_QSPI_CLK_DISABLE();
  120. /**QUADSPI GPIO Configuration
  121. PC0 ------> QUADSPI_BK2_NCS
  122. PF10 ------> QUADSPI_CLK
  123. PB6 ------> QUADSPI_BK1_NCS
  124. PH3 ------> QUADSPI_BK2_IO1
  125. PG7 ------> QUADSPI_BK2_IO3
  126. PG10 ------> QUADSPI_BK2_IO2
  127. PF7 ------> QUADSPI_BK1_IO2
  128. PF6 ------> QUADSPI_BK1_IO3
  129. PH2 ------> QUADSPI_BK2_IO0
  130. PF8 ------> QUADSPI_BK1_IO0
  131. PF9 ------> QUADSPI_BK1_IO1
  132. */
  133. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0);
  134. HAL_GPIO_DeInit(GPIOF, GPIO_PIN_6|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10);
  135. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6);
  136. HAL_GPIO_DeInit(GPIOH, GPIO_PIN_3|GPIO_PIN_2);
  137. HAL_GPIO_DeInit(GPIOG, GPIO_PIN_7|GPIO_PIN_10);
  138. /* USER CODE BEGIN QUADSPI_MspDeInit 1 */
  139. /* USER CODE END QUADSPI_MspDeInit 1 */
  140. }
  141. }
  142. static int rt_hw_qspi_flash_with_sfud_init(void)
  143. {
  144. stm32_qspi_bus_attach_device("qspi1", "qspi10", RT_NULL, 4, RT_NULL, RT_NULL);
  145. /* init MX25L51245G */
  146. if (RT_NULL == rt_sfud_flash_probe("MX25L51245G", "qspi10"))
  147. {
  148. return -RT_ERROR;
  149. }
  150. return RT_EOK;
  151. }
  152. INIT_DEVICE_EXPORT(rt_hw_qspi_flash_with_sfud_init);
  153. #endif /* BSP_USING_QSPI_FLASH */