main.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /**
  2. ******************************************************************************
  3. * File Name : main.c
  4. * Description : Main program body
  5. ******************************************************************************
  6. *
  7. * COPYRIGHT(c) 2015 STMicroelectronics
  8. *
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. * 1. Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright notice,
  14. * this list of conditions and the following disclaimer in the documentation
  15. * and/or other materials provided with the distribution.
  16. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. *
  31. ******************************************************************************
  32. */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f4xx_hal.h"
  35. /* USER CODE BEGIN Includes */
  36. #include <stdio.h>
  37. #include "led_blink.h"
  38. #include "app.h"
  39. /* USER CODE END Includes */
  40. /* Private variables ---------------------------------------------------------*/
  41. RTC_HandleTypeDef hrtc;
  42. TIM_HandleTypeDef htim2;
  43. TIM_HandleTypeDef htim3;
  44. UART_HandleTypeDef huart1;
  45. UART_HandleTypeDef huart2;
  46. /* USER CODE BEGIN PV */
  47. /* USER CODE END PV */
  48. /* Private function prototypes -----------------------------------------------*/
  49. void SystemClock_Config(void);
  50. static void MX_GPIO_Init(void);
  51. static void MX_RTC_Init(void);
  52. static void MX_TIM2_Init(void);
  53. static void MX_TIM3_Init(void);
  54. static void MX_USART1_UART_Init(void);
  55. static void MX_USART2_UART_Init(void);
  56. /* USER CODE BEGIN PFP */
  57. /* USER CODE END PFP */
  58. /* USER CODE BEGIN 0 */
  59. /* USER CODE END 0 */
  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. /* Configure the system clock */
  68. SystemClock_Config();
  69. /* Initialize all configured peripherals */
  70. MX_GPIO_Init();
  71. MX_RTC_Init();
  72. MX_TIM2_Init();
  73. MX_TIM3_Init();
  74. MX_USART1_UART_Init();
  75. MX_USART2_UART_Init();
  76. /* USER CODE BEGIN 2 */
  77. // Enable TIM2-Interrupts (433-Receiver interrupt)
  78. HAL_TIM_Base_Start_IT(&htim2);
  79. // Enable TIM3-Interrupts (just timing)
  80. //HAL_TIM_Base_Start_IT(&htim3);
  81. // Enable USART1 Interrupts (WiFi)
  82. USART1->CR1 |= USART_CR1_RXNEIE | USART_CR1_TXEIE;
  83. // Enable USART2 Interrupts (USB-Debug)
  84. USART2->CR1 |= USART_CR1_RXNEIE | USART_CR1_TXEIE;
  85. NVIC_SetPriority(TIM2_IRQn, 0); // Highest priority
  86. NVIC_SetPriority(RTC_WKUP_IRQn, 2);
  87. NVIC_SetPriority(USART1_IRQn,5);
  88. NVIC_SetPriority(USART2_IRQn,6);
  89. NVIC_SetPriority(SysTick_IRQn, 10); // Set systick at a very low priority
  90. setup();
  91. /* USER CODE END 2 */
  92. /* Infinite loop */
  93. /* USER CODE BEGIN WHILE */
  94. while (1)
  95. {
  96. loop();
  97. }
  98. /* USER CODE END WHILE */
  99. /* USER CODE BEGIN 3 */
  100. /* USER CODE END 3 */
  101. }
  102. /** System Clock Configuration
  103. */
  104. void SystemClock_Config(void)
  105. {
  106. RCC_OscInitTypeDef RCC_OscInitStruct;
  107. RCC_ClkInitTypeDef RCC_ClkInitStruct;
  108. RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
  109. __PWR_CLK_ENABLE();
  110. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2);
  111. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE|RCC_OSCILLATORTYPE_LSE;
  112. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  113. RCC_OscInitStruct.LSEState = RCC_LSE_ON;
  114. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  115. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  116. RCC_OscInitStruct.PLL.PLLM = 8;
  117. RCC_OscInitStruct.PLL.PLLN = 200;
  118. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  119. RCC_OscInitStruct.PLL.PLLQ = 4;
  120. HAL_RCC_OscConfig(&RCC_OscInitStruct);
  121. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1;
  122. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  123. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  124. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  125. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  126. HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3);
  127. PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_RTC;
  128. PeriphClkInitStruct.RTCClockSelection = RCC_RTCCLKSOURCE_LSE;
  129. HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
  130. HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
  131. HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
  132. }
  133. /* RTC init function */
  134. void MX_RTC_Init(void)
  135. {
  136. RTC_TimeTypeDef sTime;
  137. RTC_DateTypeDef sDate;
  138. /**Initialize RTC and set the Time and Date
  139. */
  140. hrtc.Instance = RTC;
  141. hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
  142. hrtc.Init.AsynchPrediv = 127;
  143. hrtc.Init.SynchPrediv = 255;
  144. hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
  145. hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
  146. hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
  147. HAL_RTC_Init(&hrtc);
  148. sTime.Hours = 0;
  149. sTime.Minutes = 0;
  150. sTime.Seconds = 0;
  151. sTime.SubSeconds = 0;
  152. sTime.TimeFormat = RTC_HOURFORMAT12_AM;
  153. sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
  154. sTime.StoreOperation = RTC_STOREOPERATION_RESET;
  155. HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BCD);
  156. sDate.WeekDay = RTC_WEEKDAY_MONDAY;
  157. sDate.Month = RTC_MONTH_JANUARY;
  158. sDate.Date = 1;
  159. sDate.Year = 0;
  160. HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BCD);
  161. /**Enable the WakeUp
  162. */
  163. HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, 59, RTC_WAKEUPCLOCK_CK_SPRE_16BITS);
  164. }
  165. /* TIM2 init function */
  166. void MX_TIM2_Init(void)
  167. {
  168. TIM_ClockConfigTypeDef sClockSourceConfig;
  169. TIM_MasterConfigTypeDef sMasterConfig;
  170. htim2.Instance = TIM2;
  171. htim2.Init.Prescaler = 99;
  172. htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  173. htim2.Init.Period = 9;
  174. htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  175. HAL_TIM_Base_Init(&htim2);
  176. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  177. HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig);
  178. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  179. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  180. HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig);
  181. }
  182. /* TIM3 init function */
  183. void MX_TIM3_Init(void)
  184. {
  185. TIM_ClockConfigTypeDef sClockSourceConfig;
  186. TIM_MasterConfigTypeDef sMasterConfig;
  187. htim3.Instance = TIM3;
  188. htim3.Init.Prescaler = 99;
  189. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  190. htim3.Init.Period = 0;
  191. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  192. HAL_TIM_Base_Init(&htim3);
  193. sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  194. HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);
  195. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  196. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  197. HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);
  198. }
  199. /* USART1 init function */
  200. void MX_USART1_UART_Init(void)
  201. {
  202. huart1.Instance = USART1;
  203. huart1.Init.BaudRate = 9600;
  204. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  205. huart1.Init.StopBits = UART_STOPBITS_1;
  206. huart1.Init.Parity = UART_PARITY_NONE;
  207. huart1.Init.Mode = UART_MODE_TX_RX;
  208. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  209. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  210. HAL_UART_Init(&huart1);
  211. }
  212. /* USART2 init function */
  213. void MX_USART2_UART_Init(void)
  214. {
  215. huart2.Instance = USART2;
  216. huart2.Init.BaudRate = 9600;
  217. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  218. huart2.Init.StopBits = UART_STOPBITS_1;
  219. huart2.Init.Parity = UART_PARITY_NONE;
  220. huart2.Init.Mode = UART_MODE_TX_RX;
  221. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  222. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  223. HAL_UART_Init(&huart2);
  224. }
  225. /** Configure pins as
  226. * Analog
  227. * Input
  228. * Output
  229. * EVENT_OUT
  230. * EXTI
  231. */
  232. void MX_GPIO_Init(void)
  233. {
  234. GPIO_InitTypeDef GPIO_InitStruct;
  235. /* GPIO Ports Clock Enable */
  236. __GPIOC_CLK_ENABLE();
  237. __GPIOH_CLK_ENABLE();
  238. __GPIOA_CLK_ENABLE();
  239. /*Configure GPIO pin : PA0 */
  240. GPIO_InitStruct.Pin = GPIO_PIN_0;
  241. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  242. GPIO_InitStruct.Pull = GPIO_NOPULL;
  243. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  244. /*Configure GPIO pins : PA5 PA15 */
  245. GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_15;
  246. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  247. GPIO_InitStruct.Pull = GPIO_NOPULL;
  248. GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
  249. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  250. }
  251. /* USER CODE BEGIN 4 */
  252. /* USER CODE END 4 */
  253. #ifdef USE_FULL_ASSERT
  254. /**
  255. * @brief Reports the name of the source file and the source line number
  256. * where the assert_param error has occurred.
  257. * @param file: pointer to the source file name
  258. * @param line: assert_param error line source number
  259. * @retval None
  260. */
  261. void assert_failed(uint8_t* file, uint32_t line)
  262. {
  263. /* USER CODE BEGIN 6 */
  264. /* User can add his own implementation to report the file name and line number,
  265. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  266. /* USER CODE END 6 */
  267. }
  268. #endif
  269. /**
  270. * @}
  271. */
  272. /**
  273. * @}
  274. */
  275. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/