quadspi.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file quadspi.c
  5. * @brief This file provides code for the configuration
  6. * of the QUADSPI instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2024 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "quadspi.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. QSPI_HandleTypeDef hqspi;
  25. /* QUADSPI init function */
  26. void MX_QUADSPI_Init(void)
  27. {
  28. /* USER CODE BEGIN QUADSPI_Init 0 */
  29. /* USER CODE END QUADSPI_Init 0 */
  30. /* USER CODE BEGIN QUADSPI_Init 1 */
  31. /* USER CODE END QUADSPI_Init 1 */
  32. hqspi.Instance = QUADSPI;
  33. hqspi.Init.ClockPrescaler = 255;
  34. hqspi.Init.FifoThreshold = 1;
  35. hqspi.Init.SampleShifting = QSPI_SAMPLE_SHIFTING_NONE;
  36. hqspi.Init.FlashSize = 1;
  37. hqspi.Init.ChipSelectHighTime = QSPI_CS_HIGH_TIME_1_CYCLE;
  38. hqspi.Init.ClockMode = QSPI_CLOCK_MODE_0;
  39. hqspi.Init.FlashID = QSPI_FLASH_ID_1;
  40. hqspi.Init.DualFlash = QSPI_DUALFLASH_DISABLE;
  41. if (HAL_QSPI_Init(&hqspi) != HAL_OK)
  42. {
  43. Error_Handler();
  44. }
  45. /* USER CODE BEGIN QUADSPI_Init 2 */
  46. /* USER CODE END QUADSPI_Init 2 */
  47. }
  48. void HAL_QSPI_MspInit(QSPI_HandleTypeDef* qspiHandle)
  49. {
  50. GPIO_InitTypeDef GPIO_InitStruct = {0};
  51. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};
  52. if(qspiHandle->Instance==QUADSPI)
  53. {
  54. /* USER CODE BEGIN QUADSPI_MspInit 0 */
  55. /* USER CODE END QUADSPI_MspInit 0 */
  56. /** Initializes the peripherals clock
  57. */
  58. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_QSPI;
  59. PeriphClkInitStruct.QspiClockSelection = RCC_QSPICLKSOURCE_D1HCLK;
  60. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  61. {
  62. Error_Handler();
  63. }
  64. /* QUADSPI clock enable */
  65. __HAL_RCC_QSPI_CLK_ENABLE();
  66. __HAL_RCC_GPIOE_CLK_ENABLE();
  67. __HAL_RCC_GPIOA_CLK_ENABLE();
  68. __HAL_RCC_GPIOB_CLK_ENABLE();
  69. __HAL_RCC_GPIOD_CLK_ENABLE();
  70. __HAL_RCC_GPIOC_CLK_ENABLE();
  71. /**QUADSPI GPIO Configuration
  72. PE2 ------> QUADSPI_BK1_IO2
  73. PA1 ------> QUADSPI_BK1_IO3
  74. PB2 ------> QUADSPI_CLK
  75. PD11 ------> QUADSPI_BK1_IO0
  76. PC10 ------> QUADSPI_BK1_IO1
  77. PB6 ------> QUADSPI_BK1_NCS
  78. */
  79. GPIO_InitStruct.Pin = GPIO_PIN_2;
  80. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  81. GPIO_InitStruct.Pull = GPIO_NOPULL;
  82. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  83. GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI;
  84. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  85. GPIO_InitStruct.Pin = GPIO_PIN_1;
  86. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  87. GPIO_InitStruct.Pull = GPIO_NOPULL;
  88. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  89. GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI;
  90. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  91. GPIO_InitStruct.Pin = GPIO_PIN_2;
  92. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  93. GPIO_InitStruct.Pull = GPIO_NOPULL;
  94. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  95. GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI;
  96. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  97. GPIO_InitStruct.Pin = GPIO_PIN_11;
  98. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  99. GPIO_InitStruct.Pull = GPIO_NOPULL;
  100. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  101. GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI;
  102. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  103. GPIO_InitStruct.Pin = GPIO_PIN_10;
  104. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  105. GPIO_InitStruct.Pull = GPIO_NOPULL;
  106. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  107. GPIO_InitStruct.Alternate = GPIO_AF9_QUADSPI;
  108. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  109. GPIO_InitStruct.Pin = GPIO_PIN_6;
  110. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  111. GPIO_InitStruct.Pull = GPIO_NOPULL;
  112. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  113. GPIO_InitStruct.Alternate = GPIO_AF10_QUADSPI;
  114. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  115. /* USER CODE BEGIN QUADSPI_MspInit 1 */
  116. /* USER CODE END QUADSPI_MspInit 1 */
  117. }
  118. }
  119. void HAL_QSPI_MspDeInit(QSPI_HandleTypeDef* qspiHandle)
  120. {
  121. if(qspiHandle->Instance==QUADSPI)
  122. {
  123. /* USER CODE BEGIN QUADSPI_MspDeInit 0 */
  124. /* USER CODE END QUADSPI_MspDeInit 0 */
  125. /* Peripheral clock disable */
  126. __HAL_RCC_QSPI_CLK_DISABLE();
  127. /**QUADSPI GPIO Configuration
  128. PE2 ------> QUADSPI_BK1_IO2
  129. PA1 ------> QUADSPI_BK1_IO3
  130. PB2 ------> QUADSPI_CLK
  131. PD11 ------> QUADSPI_BK1_IO0
  132. PC10 ------> QUADSPI_BK1_IO1
  133. PB6 ------> QUADSPI_BK1_NCS
  134. */
  135. HAL_GPIO_DeInit(GPIOE, GPIO_PIN_2);
  136. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1);
  137. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_2|GPIO_PIN_6);
  138. HAL_GPIO_DeInit(GPIOD, GPIO_PIN_11);
  139. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10);
  140. /* USER CODE BEGIN QUADSPI_MspDeInit 1 */
  141. /* USER CODE END QUADSPI_MspDeInit 1 */
  142. }
  143. }
  144. /* USER CODE BEGIN 1 */
  145. /* USER CODE END 1 */