main.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2023 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 "nbus_app.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. TIM_HandleTypeDef htim22;
  36. UART_HandleTypeDef huart2;
  37. DMA_HandleTypeDef hdma_usart2_tx;
  38. /* USER CODE BEGIN PV */
  39. /* USER CODE END PV */
  40. /* Private function prototypes -----------------------------------------------*/
  41. void SystemClock_Config(void);
  42. static void MX_GPIO_Init(void);
  43. static void MX_DMA_Init(void);
  44. static void MX_USART2_UART_Init(void);
  45. static void MX_TIM22_Init(void);
  46. /* USER CODE BEGIN PFP */
  47. /* USER CODE END PFP */
  48. /* Private user code ---------------------------------------------------------*/
  49. /* USER CODE BEGIN 0 */
  50. /* USER CODE END 0 */
  51. /**
  52. * @brief The application entry point.
  53. * @retval int
  54. */
  55. int main(void)
  56. {
  57. /* USER CODE BEGIN 1 */
  58. /* USER CODE END 1 */
  59. /* MCU Configuration--------------------------------------------------------*/
  60. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  61. HAL_Init();
  62. /* USER CODE BEGIN Init */
  63. /* USER CODE END Init */
  64. /* Configure the system clock */
  65. SystemClock_Config();
  66. /* USER CODE BEGIN SysInit */
  67. /* USER CODE END SysInit */
  68. /* Initialize all configured peripherals */
  69. MX_GPIO_Init();
  70. MX_DMA_Init();
  71. MX_USART2_UART_Init();
  72. MX_TIM22_Init();
  73. /* USER CODE BEGIN 2 */
  74. McuPin_typeDef Led;
  75. Led.port = LD3_GPIO_Port;
  76. Led.pin = LD3_Pin;
  77. Peripheral_typeDef periph;
  78. periph.huart = &huart2;
  79. periph.uart_timer = &htim22;
  80. periph.adc = NULL;
  81. periph.led = &Led;
  82. nbus_init(&periph);
  83. nbus_stack();
  84. /* USER CODE END 2 */
  85. /* Infinite loop */
  86. /* USER CODE BEGIN WHILE */
  87. while (1)
  88. {
  89. /* USER CODE END WHILE */
  90. /* USER CODE BEGIN 3 */
  91. }
  92. /* USER CODE END 3 */
  93. }
  94. /**
  95. * @brief System Clock Configuration
  96. * @retval None
  97. */
  98. void SystemClock_Config(void)
  99. {
  100. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  101. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  102. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  103. /** Configure the main internal regulator output voltage
  104. */
  105. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  106. /** Initializes the RCC Oscillators according to the specified parameters
  107. * in the RCC_OscInitTypeDef structure.
  108. */
  109. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  110. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  111. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  112. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  113. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  114. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
  115. RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  116. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  117. {
  118. Error_Handler();
  119. }
  120. /** Initializes the CPU, AHB and APB buses clocks
  121. */
  122. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  123. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  124. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  125. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  126. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  127. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  128. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  129. {
  130. Error_Handler();
  131. }
  132. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
  133. PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  134. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  135. {
  136. Error_Handler();
  137. }
  138. }
  139. /**
  140. * @brief TIM22 Initialization Function
  141. * @param None
  142. * @retval None
  143. */
  144. static void MX_TIM22_Init(void)
  145. {
  146. /* USER CODE BEGIN TIM22_Init 0 */
  147. /* USER CODE END TIM22_Init 0 */
  148. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  149. TIM_MasterConfigTypeDef sMasterConfig = {0};
  150. /* USER CODE BEGIN TIM22_Init 1 */
  151. /* USER CODE END TIM22_Init 1 */
  152. htim22.Instance = TIM22;
  153. htim22.Init.Prescaler = 63;
  154. htim22.Init.CounterMode = TIM_COUNTERMODE_UP;
  155. htim22.Init.Period = 65535;
  156. htim22.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  157. htim22.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  158. if (HAL_TIM_Base_Init(&htim22) != HAL_OK)
  159. {
  160. Error_Handler();
  161. }
  162. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  163. if (HAL_TIM_ConfigClockSource(&htim22, &sClockSourceConfig) != HAL_OK)
  164. {
  165. Error_Handler();
  166. }
  167. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  168. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  169. if (HAL_TIMEx_MasterConfigSynchronization(&htim22, &sMasterConfig) != HAL_OK)
  170. {
  171. Error_Handler();
  172. }
  173. /* USER CODE BEGIN TIM22_Init 2 */
  174. /* USER CODE END TIM22_Init 2 */
  175. }
  176. /**
  177. * @brief USART2 Initialization Function
  178. * @param None
  179. * @retval None
  180. */
  181. static void MX_USART2_UART_Init(void)
  182. {
  183. /* USER CODE BEGIN USART2_Init 0 */
  184. /* USER CODE END USART2_Init 0 */
  185. /* USER CODE BEGIN USART2_Init 1 */
  186. /* USER CODE END USART2_Init 1 */
  187. huart2.Instance = USART2;
  188. huart2.Init.BaudRate = 115200;
  189. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  190. huart2.Init.StopBits = UART_STOPBITS_1;
  191. huart2.Init.Parity = UART_PARITY_NONE;
  192. huart2.Init.Mode = UART_MODE_TX_RX;
  193. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  194. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  195. huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  196. huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  197. if (HAL_UART_Init(&huart2) != HAL_OK)
  198. {
  199. Error_Handler();
  200. }
  201. /* USER CODE BEGIN USART2_Init 2 */
  202. /* USER CODE END USART2_Init 2 */
  203. }
  204. /**
  205. * Enable DMA controller clock
  206. */
  207. static void MX_DMA_Init(void)
  208. {
  209. /* DMA controller clock enable */
  210. __HAL_RCC_DMA1_CLK_ENABLE();
  211. /* DMA interrupt init */
  212. /* DMA1_Channel4_5_6_7_IRQn interrupt configuration */
  213. HAL_NVIC_SetPriority(DMA1_Channel4_5_6_7_IRQn, 0, 0);
  214. HAL_NVIC_EnableIRQ(DMA1_Channel4_5_6_7_IRQn);
  215. }
  216. /**
  217. * @brief GPIO Initialization Function
  218. * @param None
  219. * @retval None
  220. */
  221. static void MX_GPIO_Init(void)
  222. {
  223. GPIO_InitTypeDef GPIO_InitStruct = {0};
  224. /* USER CODE BEGIN MX_GPIO_Init_1 */
  225. /* USER CODE END MX_GPIO_Init_1 */
  226. /* GPIO Ports Clock Enable */
  227. __HAL_RCC_GPIOC_CLK_ENABLE();
  228. __HAL_RCC_GPIOA_CLK_ENABLE();
  229. __HAL_RCC_GPIOB_CLK_ENABLE();
  230. /*Configure GPIO pin Output Level */
  231. HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
  232. /*Configure GPIO pin : LD3_Pin */
  233. GPIO_InitStruct.Pin = LD3_Pin;
  234. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  235. GPIO_InitStruct.Pull = GPIO_NOPULL;
  236. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  237. HAL_GPIO_Init(LD3_GPIO_Port, &GPIO_InitStruct);
  238. /* USER CODE BEGIN MX_GPIO_Init_2 */
  239. /* USER CODE END MX_GPIO_Init_2 */
  240. }
  241. /* USER CODE BEGIN 4 */
  242. /* USER CODE END 4 */
  243. /**
  244. * @brief This function is executed in case of error occurrence.
  245. * @retval None
  246. */
  247. void Error_Handler(void)
  248. {
  249. /* USER CODE BEGIN Error_Handler_Debug */
  250. /* User can add his own implementation to report the HAL error return state */
  251. __disable_irq();
  252. while (1)
  253. {
  254. }
  255. /* USER CODE END Error_Handler_Debug */
  256. }
  257. #ifdef USE_FULL_ASSERT
  258. /**
  259. * @brief Reports the name of the source file and the source line number
  260. * where the assert_param error has occurred.
  261. * @param file: pointer to the source file name
  262. * @param line: assert_param error line source number
  263. * @retval None
  264. */
  265. void assert_failed(uint8_t *file, uint32_t line)
  266. {
  267. /* USER CODE BEGIN 6 */
  268. /* User can add his own implementation to report the file name and line number,
  269. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  270. /* USER CODE END 6 */
  271. }
  272. #endif /* USE_FULL_ASSERT */