main.c 13 KB

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