1
0

main.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2025 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. /* Private includes ----------------------------------------------------------*/
  22. /* USER CODE BEGIN Includes */
  23. #include "epd_test.h"
  24. /* USER CODE END Includes */
  25. /* Private typedef -----------------------------------------------------------*/
  26. /* USER CODE BEGIN PTD */
  27. /* USER CODE END PTD */
  28. /* Private define ------------------------------------------------------------*/
  29. /* USER CODE BEGIN PD */
  30. /* USER CODE END PD */
  31. /* Private macro -------------------------------------------------------------*/
  32. /* USER CODE BEGIN PM */
  33. /* USER CODE END PM */
  34. /* Private variables ---------------------------------------------------------*/
  35. SPI_HandleTypeDef hspi1;
  36. UART_HandleTypeDef huart1;
  37. /* USER CODE BEGIN PV */
  38. /* USER CODE END PV */
  39. /* Private function prototypes -----------------------------------------------*/
  40. void SystemClock_Config(void);
  41. static void MX_GPIO_Init(void);
  42. static void MX_SPI1_Init(void);
  43. static void MX_USART1_UART_Init(void);
  44. /* USER CODE BEGIN PFP */
  45. /* USER CODE END PFP */
  46. /* Private user code ---------------------------------------------------------*/
  47. /* USER CODE BEGIN 0 */
  48. /* USER CODE END 0 */
  49. /**
  50. * @brief The application entry point.
  51. * @retval int
  52. */
  53. int main(void)
  54. {
  55. /* USER CODE BEGIN 1 */
  56. /* USER CODE END 1 */
  57. /* MCU Configuration--------------------------------------------------------*/
  58. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  59. HAL_Init();
  60. /* USER CODE BEGIN Init */
  61. /* USER CODE END Init */
  62. /* Configure the system clock */
  63. SystemClock_Config();
  64. /* USER CODE BEGIN SysInit */
  65. /* USER CODE END SysInit */
  66. /* Initialize all configured peripherals */
  67. MX_GPIO_Init();
  68. MX_SPI1_Init();
  69. MX_USART1_UART_Init();
  70. /* USER CODE BEGIN 2 */
  71. EPD_test();
  72. /* USER CODE END 2 */
  73. /* Infinite loop */
  74. /* USER CODE BEGIN WHILE */
  75. while (1)
  76. {
  77. /* USER CODE END WHILE */
  78. /* USER CODE BEGIN 3 */
  79. HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin);
  80. HAL_Delay(1000);
  81. }
  82. /* USER CODE END 3 */
  83. }
  84. /**
  85. * @brief System Clock Configuration
  86. * @retval None
  87. */
  88. void SystemClock_Config(void)
  89. {
  90. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  91. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  92. /** Initializes the RCC Oscillators according to the specified parameters
  93. * in the RCC_OscInitTypeDef structure.
  94. */
  95. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  96. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  97. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  98. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  99. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  100. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  101. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  102. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  103. {
  104. Error_Handler();
  105. }
  106. /** Initializes the CPU, AHB and APB buses clocks
  107. */
  108. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  109. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  110. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  111. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  112. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  113. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  114. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  115. {
  116. Error_Handler();
  117. }
  118. }
  119. /**
  120. * @brief SPI1 Initialization Function
  121. * @param None
  122. * @retval None
  123. */
  124. static void MX_SPI1_Init(void)
  125. {
  126. /* USER CODE BEGIN SPI1_Init 0 */
  127. /* USER CODE END SPI1_Init 0 */
  128. /* USER CODE BEGIN SPI1_Init 1 */
  129. /* USER CODE END SPI1_Init 1 */
  130. /* SPI1 parameter configuration*/
  131. hspi1.Instance = SPI1;
  132. hspi1.Init.Mode = SPI_MODE_MASTER;
  133. hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  134. hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  135. hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
  136. hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  137. hspi1.Init.NSS = SPI_NSS_SOFT;
  138. hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_8;
  139. hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  140. hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  141. hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  142. hspi1.Init.CRCPolynomial = 10;
  143. if (HAL_SPI_Init(&hspi1) != HAL_OK)
  144. {
  145. Error_Handler();
  146. }
  147. /* USER CODE BEGIN SPI1_Init 2 */
  148. /* USER CODE END SPI1_Init 2 */
  149. }
  150. /**
  151. * @brief USART1 Initialization Function
  152. * @param None
  153. * @retval None
  154. */
  155. static void MX_USART1_UART_Init(void)
  156. {
  157. /* USER CODE BEGIN USART1_Init 0 */
  158. /* USER CODE END USART1_Init 0 */
  159. /* USER CODE BEGIN USART1_Init 1 */
  160. /* USER CODE END USART1_Init 1 */
  161. huart1.Instance = USART1;
  162. huart1.Init.BaudRate = 9600;
  163. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  164. huart1.Init.StopBits = UART_STOPBITS_1;
  165. huart1.Init.Parity = UART_PARITY_NONE;
  166. huart1.Init.Mode = UART_MODE_TX_RX;
  167. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  168. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  169. if (HAL_UART_Init(&huart1) != HAL_OK)
  170. {
  171. Error_Handler();
  172. }
  173. /* USER CODE BEGIN USART1_Init 2 */
  174. /* USER CODE END USART1_Init 2 */
  175. }
  176. /**
  177. * @brief GPIO Initialization Function
  178. * @param None
  179. * @retval None
  180. */
  181. static void MX_GPIO_Init(void)
  182. {
  183. GPIO_InitTypeDef GPIO_InitStruct = {0};
  184. /* USER CODE BEGIN MX_GPIO_Init_1 */
  185. /* USER CODE END MX_GPIO_Init_1 */
  186. /* GPIO Ports Clock Enable */
  187. __HAL_RCC_GPIOC_CLK_ENABLE();
  188. __HAL_RCC_GPIOD_CLK_ENABLE();
  189. __HAL_RCC_GPIOA_CLK_ENABLE();
  190. __HAL_RCC_GPIOB_CLK_ENABLE();
  191. /*Configure GPIO pin Output Level */
  192. HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
  193. /*Configure GPIO pin Output Level */
  194. HAL_GPIO_WritePin(GPIOA, RST_Pin|DC_Pin|CS_Pin, GPIO_PIN_SET);
  195. /*Configure GPIO pin : LED_Pin */
  196. GPIO_InitStruct.Pin = LED_Pin;
  197. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  198. GPIO_InitStruct.Pull = GPIO_NOPULL;
  199. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  200. HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
  201. /*Configure GPIO pins : RST_Pin DC_Pin CS_Pin */
  202. GPIO_InitStruct.Pin = RST_Pin|DC_Pin|CS_Pin;
  203. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  204. GPIO_InitStruct.Pull = GPIO_NOPULL;
  205. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  206. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  207. /*Configure GPIO pin : BUSY_Pin */
  208. GPIO_InitStruct.Pin = BUSY_Pin;
  209. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  210. GPIO_InitStruct.Pull = GPIO_NOPULL;
  211. HAL_GPIO_Init(BUSY_GPIO_Port, &GPIO_InitStruct);
  212. /* USER CODE BEGIN MX_GPIO_Init_2 */
  213. /* USER CODE END MX_GPIO_Init_2 */
  214. }
  215. /* USER CODE BEGIN 4 */
  216. /* USER CODE END 4 */
  217. /**
  218. * @brief This function is executed in case of error occurrence.
  219. * @retval None
  220. */
  221. void Error_Handler(void)
  222. {
  223. /* USER CODE BEGIN Error_Handler_Debug */
  224. /* User can add his own implementation to report the HAL error return state */
  225. __disable_irq();
  226. while (1)
  227. {
  228. }
  229. /* USER CODE END Error_Handler_Debug */
  230. }
  231. #ifdef USE_FULL_ASSERT
  232. /**
  233. * @brief Reports the name of the source file and the source line number
  234. * where the assert_param error has occurred.
  235. * @param file: pointer to the source file name
  236. * @param line: assert_param error line source number
  237. * @retval None
  238. */
  239. void assert_failed(uint8_t *file, uint32_t line)
  240. {
  241. /* USER CODE BEGIN 6 */
  242. /* User can add his own implementation to report the file name and line number,
  243. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  244. /* USER CODE END 6 */
  245. }
  246. #endif /* USE_FULL_ASSERT */