main.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  10. * All rights reserved.</center></h2>
  11. *
  12. * This software component is licensed by ST under BSD 3-Clause license,
  13. * the "License"; You may not use this file except in compliance with the
  14. * License. You may obtain a copy of the License at:
  15. * opensource.org/licenses/BSD-3-Clause
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. #include "main.h"
  25. #include "stm32wlxx_hal.h"
  26. #include "app_lorawan.h"
  27. /* USER CODE END Includes */
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */
  30. /* USER CODE END PTD */
  31. /* Private define ------------------------------------------------------------*/
  32. /* USER CODE BEGIN PD */
  33. /* USER CODE END PD */
  34. /* Private macro -------------------------------------------------------------*/
  35. /* USER CODE BEGIN PM */
  36. /* USER CODE END PM */
  37. /* Private variables ---------------------------------------------------------*/
  38. UART_HandleTypeDef hlpuart1;
  39. /* USER CODE BEGIN PV */
  40. /* USER CODE END PV */
  41. /* Private function prototypes -----------------------------------------------*/
  42. void SystemClock_Config(void);
  43. static void MX_GPIO_Init(void);
  44. static void MX_LPUART1_UART_Init(void);
  45. /* USER CODE BEGIN PFP */
  46. /* USER CODE END PFP */
  47. /* Private user code ---------------------------------------------------------*/
  48. /* USER CODE BEGIN 0 */
  49. /* USER CODE END 0 */
  50. /**
  51. * @brief The application entry point.
  52. * @retval int
  53. */
  54. int main(void)
  55. {
  56. /* USER CODE BEGIN 1 */
  57. /* USER CODE END 1 */
  58. /* MCU Configuration--------------------------------------------------------*/
  59. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  60. HAL_Init();
  61. /* USER CODE BEGIN Init */
  62. /* USER CODE END Init */
  63. /* Configure the system clock */
  64. SystemClock_Config();
  65. /* USER CODE BEGIN SysInit */
  66. /* USER CODE END SysInit */
  67. /* Initialize all configured peripherals */
  68. MX_GPIO_Init();
  69. MX_LPUART1_UART_Init();
  70. /* USER CODE BEGIN 2 */
  71. /* USER CODE END 2 */
  72. /* Infinite loop */
  73. /* USER CODE BEGIN WHILE */
  74. while (1)
  75. {
  76. MX_LoRaWAN_Process();
  77. }
  78. /* USER CODE END WHILE */
  79. /* USER CODE BEGIN 3 */
  80. /* USER CODE END 3 */
  81. }
  82. /**
  83. * @brief System Clock Configuration
  84. * @retval None
  85. */
  86. void SystemClock_Config(void)
  87. {
  88. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  89. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  90. /** Configure the main internal regulator output voltage
  91. */
  92. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  93. /** Initializes the CPU, AHB and APB busses clocks
  94. */
  95. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_MSI;
  96. RCC_OscInitStruct.MSIState = RCC_MSI_ON;
  97. RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT;
  98. RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_11;
  99. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  100. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  101. {
  102. Error_Handler();
  103. }
  104. /** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers
  105. */
  106. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3|RCC_CLOCKTYPE_HCLK
  107. |RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
  108. |RCC_CLOCKTYPE_PCLK2;
  109. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_MSI;
  110. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  111. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  112. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  113. RCC_ClkInitStruct.AHBCLK3Divider = RCC_SYSCLK_DIV1;
  114. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  115. {
  116. Error_Handler();
  117. }
  118. }
  119. /**
  120. * @brief LPUART1 Initialization Function
  121. * @param None
  122. * @retval None
  123. */
  124. static void MX_LPUART1_UART_Init(void)
  125. {
  126. /* USER CODE BEGIN LPUART1_Init 0 */
  127. /* USER CODE END LPUART1_Init 0 */
  128. /* USER CODE BEGIN LPUART1_Init 1 */
  129. /* USER CODE END LPUART1_Init 1 */
  130. hlpuart1.Instance = LPUART1;
  131. hlpuart1.Init.BaudRate = 209700;
  132. hlpuart1.Init.WordLength = UART_WORDLENGTH_8B;
  133. hlpuart1.Init.StopBits = UART_STOPBITS_1;
  134. hlpuart1.Init.Parity = UART_PARITY_NONE;
  135. hlpuart1.Init.Mode = UART_MODE_TX_RX;
  136. hlpuart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  137. hlpuart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  138. hlpuart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  139. hlpuart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  140. hlpuart1.FifoMode = UART_FIFOMODE_DISABLE;
  141. if (HAL_UART_Init(&hlpuart1) != HAL_OK)
  142. {
  143. Error_Handler();
  144. }
  145. if (HAL_UARTEx_SetTxFifoThreshold(&hlpuart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  146. {
  147. Error_Handler();
  148. }
  149. if (HAL_UARTEx_SetRxFifoThreshold(&hlpuart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  150. {
  151. Error_Handler();
  152. }
  153. if (HAL_UARTEx_DisableFifoMode(&hlpuart1) != HAL_OK)
  154. {
  155. Error_Handler();
  156. }
  157. /* USER CODE BEGIN LPUART1_Init 2 */
  158. /* USER CODE END LPUART1_Init 2 */
  159. }
  160. /**
  161. * @brief GPIO Initialization Function
  162. * @param None
  163. * @retval None
  164. */
  165. static void MX_GPIO_Init(void)
  166. {
  167. GPIO_InitTypeDef GPIO_InitStruct = {0};
  168. /* GPIO Ports Clock Enable */
  169. __HAL_RCC_GPIOB_CLK_ENABLE();
  170. __HAL_RCC_GPIOA_CLK_ENABLE();
  171. __HAL_RCC_GPIOC_CLK_ENABLE();
  172. /*Configure GPIO pin Output Level */
  173. HAL_GPIO_WritePin(GPIOB, LED1_Pin|LED2_Pin|LED3_Pin, GPIO_PIN_RESET);
  174. /*Configure GPIO pins : LED1_Pin LED2_Pin LED3_Pin */
  175. GPIO_InitStruct.Pin = LED1_Pin|LED2_Pin|LED3_Pin;
  176. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  177. GPIO_InitStruct.Pull = GPIO_NOPULL;
  178. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  179. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  180. /*Configure GPIO pins : BUT1_Pin BUT2_Pin */
  181. GPIO_InitStruct.Pin = BUT1_Pin|BUT2_Pin;
  182. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  183. GPIO_InitStruct.Pull = GPIO_NOPULL;
  184. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  185. /*Configure GPIO pin : BUT3_Pin */
  186. GPIO_InitStruct.Pin = BUT3_Pin;
  187. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  188. GPIO_InitStruct.Pull = GPIO_NOPULL;
  189. HAL_GPIO_Init(BUT3_GPIO_Port, &GPIO_InitStruct);
  190. }
  191. /* USER CODE BEGIN 4 */
  192. /* USER CODE END 4 */
  193. /**
  194. * @brief This function is executed in case of error occurrence.
  195. * @retval None
  196. */
  197. void Error_Handler(void)
  198. {
  199. /* USER CODE BEGIN Error_Handler_Debug */
  200. /* User can add his own implementation to report the HAL error return state */
  201. while (1)
  202. {
  203. }
  204. /* USER CODE END Error_Handler_Debug */
  205. }
  206. #ifdef USE_FULL_ASSERT
  207. /**
  208. * @brief Reports the name of the source file and the source line number
  209. * where the assert_param error has occurred.
  210. * @param file: pointer to the source file name
  211. * @param line: assert_param error line source number
  212. * @retval None
  213. */
  214. void assert_failed(uint8_t *file, uint32_t line)
  215. {
  216. /* USER CODE BEGIN 6 */
  217. /* User can add his own implementation to report the file name and line number,
  218. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  219. /* Infinite loop */
  220. while (1)
  221. {
  222. }
  223. /* USER CODE END 6 */
  224. }
  225. #endif /* USE_FULL_ASSERT */
  226. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/