main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. #include "app_dummy.h"
  25. #include "one_wire.h"
  26. #include "one_wire.h"
  27. #include "memory_ec20.h"
  28. /* USER CODE END Includes */
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31. /* USER CODE END PTD */
  32. /* Private define ------------------------------------------------------------*/
  33. /* USER CODE BEGIN PD */
  34. /* USER CODE END PD */
  35. /* Private macro -------------------------------------------------------------*/
  36. /* USER CODE BEGIN PM */
  37. /* USER CODE END PM */
  38. /* Private variables ---------------------------------------------------------*/
  39. RTC_HandleTypeDef hrtc;
  40. TIM_HandleTypeDef htim21;
  41. TIM_HandleTypeDef htim22;
  42. UART_HandleTypeDef huart2;
  43. DMA_HandleTypeDef hdma_usart2_tx;
  44. /* USER CODE BEGIN PV */
  45. /* USER CODE END PV */
  46. /* Private function prototypes -----------------------------------------------*/
  47. void SystemClock_Config(void);
  48. static void MX_GPIO_Init(void);
  49. static void MX_DMA_Init(void);
  50. static void MX_USART2_UART_Init(void);
  51. static void MX_TIM22_Init(void);
  52. static void MX_TIM21_Init(void);
  53. static void MX_RTC_Init(void);
  54. /* USER CODE BEGIN PFP */
  55. /* USER CODE END PFP */
  56. /* Private user code ---------------------------------------------------------*/
  57. /* USER CODE BEGIN 0 */
  58. /* USER CODE END 0 */
  59. /**
  60. * @brief The application entry point.
  61. * @retval int
  62. */
  63. int main(void)
  64. {
  65. /* USER CODE BEGIN 1 */
  66. /* USER CODE END 1 */
  67. /* MCU Configuration--------------------------------------------------------*/
  68. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  69. HAL_Init();
  70. /* USER CODE BEGIN Init */
  71. /* USER CODE END Init */
  72. /* Configure the system clock */
  73. SystemClock_Config();
  74. /* USER CODE BEGIN SysInit */
  75. /* USER CODE END SysInit */
  76. /* Initialize all configured peripherals */
  77. MX_GPIO_Init();
  78. MX_DMA_Init();
  79. MX_USART2_UART_Init();
  80. MX_TIM22_Init();
  81. MX_TIM21_Init();
  82. MX_RTC_Init();
  83. /* USER CODE BEGIN 2 */
  84. McuPin_typeDef Led;
  85. Led.port = LD3_GPIO_Port;
  86. Led.pin = LD3_Pin;
  87. Peripheral_typeDef periph;
  88. periph.huart = &huart2;
  89. periph.uart_timer = &htim22;
  90. periph.measure_timer = &htim21;
  91. periph.adc = NULL;
  92. periph.led = &Led;
  93. periph.rtc = &hrtc;
  94. Ec20_MemoryDriver *memory = getEc20MemoryDriver();
  95. memory->init(ONE_WIRE_GPIO_Port, ONE_WIRE_Pin);
  96. nBusAppInterface_t *dummy = getDummyDriver();
  97. nbus_init(&periph, dummy);
  98. nbus_init_app(periph.adc, NULL);
  99. nbus_stack();
  100. /* USER CODE END 2 */
  101. /* Infinite loop */
  102. /* USER CODE BEGIN WHILE */
  103. while (1)
  104. {
  105. /* USER CODE END WHILE */
  106. /* USER CODE BEGIN 3 */
  107. }
  108. /* USER CODE END 3 */
  109. }
  110. /**
  111. * @brief System Clock Configuration
  112. * @retval None
  113. */
  114. void SystemClock_Config(void)
  115. {
  116. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  117. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  118. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  119. /** Configure the main internal regulator output voltage
  120. */
  121. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  122. /** Initializes the RCC Oscillators according to the specified parameters
  123. * in the RCC_OscInitTypeDef structure.
  124. */
  125. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  126. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  127. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  128. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  129. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  130. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  131. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_4;
  132. RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  133. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  134. {
  135. Error_Handler();
  136. }
  137. /** Initializes the CPU, AHB and APB buses clocks
  138. */
  139. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  140. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  141. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  142. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  143. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  144. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  145. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  146. {
  147. Error_Handler();
  148. }
  149. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_RTC;
  150. PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  151. PeriphClkInit.RTCClockSelection = RCC_RTCCLKSOURCE_LSI;
  152. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  153. {
  154. Error_Handler();
  155. }
  156. }
  157. /**
  158. * @brief RTC Initialization Function
  159. * @param None
  160. * @retval None
  161. */
  162. static void MX_RTC_Init(void)
  163. {
  164. /* USER CODE BEGIN RTC_Init 0 */
  165. /* USER CODE END RTC_Init 0 */
  166. RTC_TimeTypeDef sTime = {0};
  167. RTC_DateTypeDef sDate = {0};
  168. /* USER CODE BEGIN RTC_Init 1 */
  169. /* USER CODE END RTC_Init 1 */
  170. /** Initialize RTC Only
  171. */
  172. hrtc.Instance = RTC;
  173. hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  174. hrtc.Init.AsynchPrediv = 127;
  175. hrtc.Init.SynchPrediv = 255;
  176. hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  177. hrtc.Init.OutPutRemap = RTC_OUTPUT_REMAP_NONE;
  178. hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  179. hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  180. if (HAL_RTC_Init(&hrtc) != HAL_OK)
  181. {
  182. Error_Handler();
  183. }
  184. /* USER CODE BEGIN Check_RTC_BKUP */
  185. /* USER CODE END Check_RTC_BKUP */
  186. /** Initialize RTC and set the Time and Date
  187. */
  188. sTime.Hours = 0x0;
  189. sTime.Minutes = 0x0;
  190. sTime.Seconds = 0x0;
  191. sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  192. sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  193. if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD) != HAL_OK)
  194. {
  195. Error_Handler();
  196. }
  197. sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  198. sDate.Month = RTC_MONTH_JANUARY;
  199. sDate.Date = 0x1;
  200. sDate.Year = 0x0;
  201. if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD) != HAL_OK)
  202. {
  203. Error_Handler();
  204. }
  205. /* USER CODE BEGIN RTC_Init 2 */
  206. /* USER CODE END RTC_Init 2 */
  207. }
  208. /**
  209. * @brief TIM21 Initialization Function
  210. * @param None
  211. * @retval None
  212. */
  213. static void MX_TIM21_Init(void)
  214. {
  215. /* USER CODE BEGIN TIM21_Init 0 */
  216. /* USER CODE END TIM21_Init 0 */
  217. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  218. TIM_MasterConfigTypeDef sMasterConfig = {0};
  219. /* USER CODE BEGIN TIM21_Init 1 */
  220. /* USER CODE END TIM21_Init 1 */
  221. htim21.Instance = TIM21;
  222. htim21.Init.Prescaler = 32000;
  223. htim21.Init.CounterMode = TIM_COUNTERMODE_UP;
  224. htim21.Init.Period = 100;
  225. htim21.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  226. htim21.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  227. if (HAL_TIM_Base_Init(&htim21) != HAL_OK)
  228. {
  229. Error_Handler();
  230. }
  231. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  232. if (HAL_TIM_ConfigClockSource(&htim21, &sClockSourceConfig) != HAL_OK)
  233. {
  234. Error_Handler();
  235. }
  236. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  237. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  238. if (HAL_TIMEx_MasterConfigSynchronization(&htim21, &sMasterConfig) != HAL_OK)
  239. {
  240. Error_Handler();
  241. }
  242. /* USER CODE BEGIN TIM21_Init 2 */
  243. /* USER CODE END TIM21_Init 2 */
  244. }
  245. /**
  246. * @brief TIM22 Initialization Function
  247. * @param None
  248. * @retval None
  249. */
  250. static void MX_TIM22_Init(void)
  251. {
  252. /* USER CODE BEGIN TIM22_Init 0 */
  253. /* USER CODE END TIM22_Init 0 */
  254. TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  255. TIM_MasterConfigTypeDef sMasterConfig = {0};
  256. /* USER CODE BEGIN TIM22_Init 1 */
  257. /* USER CODE END TIM22_Init 1 */
  258. htim22.Instance = TIM22;
  259. htim22.Init.Prescaler = UART_TIMER_PRESCALER;
  260. htim22.Init.CounterMode = TIM_COUNTERMODE_UP;
  261. htim22.Init.Period = 65535;
  262. htim22.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  263. htim22.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  264. if (HAL_TIM_Base_Init(&htim22) != HAL_OK)
  265. {
  266. Error_Handler();
  267. }
  268. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  269. if (HAL_TIM_ConfigClockSource(&htim22, &sClockSourceConfig) != HAL_OK)
  270. {
  271. Error_Handler();
  272. }
  273. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  274. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  275. if (HAL_TIMEx_MasterConfigSynchronization(&htim22, &sMasterConfig) != HAL_OK)
  276. {
  277. Error_Handler();
  278. }
  279. /* USER CODE BEGIN TIM22_Init 2 */
  280. /* USER CODE END TIM22_Init 2 */
  281. }
  282. /**
  283. * @brief USART2 Initialization Function
  284. * @param None
  285. * @retval None
  286. */
  287. static void MX_USART2_UART_Init(void)
  288. {
  289. /* USER CODE BEGIN USART2_Init 0 */
  290. /* USER CODE END USART2_Init 0 */
  291. /* USER CODE BEGIN USART2_Init 1 */
  292. /* USER CODE END USART2_Init 1 */
  293. huart2.Instance = USART2;
  294. huart2.Init.BaudRate = UART_BAUDRATE;
  295. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  296. huart2.Init.StopBits = UART_STOPBITS_1;
  297. huart2.Init.Parity = UART_PARITY_NONE;
  298. huart2.Init.Mode = UART_MODE_TX_RX;
  299. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  300. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  301. huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  302. huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  303. if (HAL_UART_Init(&huart2) != HAL_OK)
  304. {
  305. Error_Handler();
  306. }
  307. /* USER CODE BEGIN USART2_Init 2 */
  308. /* USER CODE END USART2_Init 2 */
  309. }
  310. /**
  311. * Enable DMA controller clock
  312. */
  313. static void MX_DMA_Init(void)
  314. {
  315. /* DMA controller clock enable */
  316. __HAL_RCC_DMA1_CLK_ENABLE();
  317. /* DMA interrupt init */
  318. /* DMA1_Channel4_5_6_7_IRQn interrupt configuration */
  319. HAL_NVIC_SetPriority(DMA1_Channel4_5_6_7_IRQn, 0, 0);
  320. HAL_NVIC_EnableIRQ(DMA1_Channel4_5_6_7_IRQn);
  321. }
  322. /**
  323. * @brief GPIO Initialization Function
  324. * @param None
  325. * @retval None
  326. */
  327. static void MX_GPIO_Init(void)
  328. {
  329. GPIO_InitTypeDef GPIO_InitStruct = {0};
  330. /* USER CODE BEGIN MX_GPIO_Init_1 */
  331. /* USER CODE END MX_GPIO_Init_1 */
  332. /* GPIO Ports Clock Enable */
  333. __HAL_RCC_GPIOC_CLK_ENABLE();
  334. __HAL_RCC_GPIOA_CLK_ENABLE();
  335. __HAL_RCC_GPIOB_CLK_ENABLE();
  336. /*Configure GPIO pin Output Level */
  337. HAL_GPIO_WritePin(ONE_WIRE_GPIO_Port, ONE_WIRE_Pin, GPIO_PIN_RESET);
  338. /*Configure GPIO pin Output Level */
  339. HAL_GPIO_WritePin(LD3_GPIO_Port, LD3_Pin, GPIO_PIN_RESET);
  340. /*Configure GPIO pin : ONE_WIRE_Pin */
  341. GPIO_InitStruct.Pin = ONE_WIRE_Pin;
  342. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  343. GPIO_InitStruct.Pull = GPIO_NOPULL;
  344. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  345. HAL_GPIO_Init(ONE_WIRE_GPIO_Port, &GPIO_InitStruct);
  346. /*Configure GPIO pin : LD3_Pin */
  347. GPIO_InitStruct.Pin = LD3_Pin;
  348. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  349. GPIO_InitStruct.Pull = GPIO_NOPULL;
  350. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  351. HAL_GPIO_Init(LD3_GPIO_Port, &GPIO_InitStruct);
  352. /* USER CODE BEGIN MX_GPIO_Init_2 */
  353. /* USER CODE END MX_GPIO_Init_2 */
  354. }
  355. /* USER CODE BEGIN 4 */
  356. /* USER CODE END 4 */
  357. /**
  358. * @brief This function is executed in case of error occurrence.
  359. * @retval None
  360. */
  361. void Error_Handler(void)
  362. {
  363. /* USER CODE BEGIN Error_Handler_Debug */
  364. /* User can add his own implementation to report the HAL error return state */
  365. __disable_irq();
  366. while (1)
  367. {
  368. }
  369. /* USER CODE END Error_Handler_Debug */
  370. }
  371. #ifdef USE_FULL_ASSERT
  372. /**
  373. * @brief Reports the name of the source file and the source line number
  374. * where the assert_param error has occurred.
  375. * @param file: pointer to the source file name
  376. * @param line: assert_param error line source number
  377. * @retval None
  378. */
  379. void assert_failed(uint8_t *file, uint32_t line)
  380. {
  381. /* USER CODE BEGIN 6 */
  382. /* User can add his own implementation to report the file name and line number,
  383. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  384. /* USER CODE END 6 */
  385. }
  386. #endif /* USE_FULL_ASSERT */