main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2022 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. #include "cmsis_os.h"
  22. #include "lwip.h"
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. /* ETH_CODE: add lwiperf, see comment in StartDefaultTask function */
  26. #include "lwip/apps/lwiperf.h"
  27. #include <stdio.h>
  28. #include "echo_server.h"
  29. #include "mqtt_client.h"
  30. /* USER CODE END Includes */
  31. /* Private typedef -----------------------------------------------------------*/
  32. /* USER CODE BEGIN PTD */
  33. /* USER CODE END PTD */
  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36. #ifndef HSEM_ID_0
  37. #define HSEM_ID_0 (0U) /* HW semaphore 0*/
  38. #endif
  39. /* USER CODE END PD */
  40. /* Private macro -------------------------------------------------------------*/
  41. /* USER CODE BEGIN PM */
  42. /* USER CODE END PM */
  43. /* Private variables ---------------------------------------------------------*/
  44. /* Definitions for defaultTask */
  45. osThreadId_t defaultTaskHandle;
  46. const osThreadAttr_t defaultTask_attributes = {
  47. .name = "defaultTask",
  48. .stack_size = 512 * 4,
  49. .priority = (osPriority_t) osPriorityNormal,
  50. };
  51. /* USER CODE BEGIN PV */
  52. extern uint32_t MilliTimer;
  53. /* USER CODE END PV */
  54. /* Private function prototypes -----------------------------------------------*/
  55. void SystemClock_Config(void);
  56. static void MPU_Config(void);
  57. static void MX_GPIO_Init(void);
  58. void StartDefaultTask(void *argument);
  59. /* USER CODE BEGIN PFP */
  60. /* USER CODE END PFP */
  61. /* Private user code ---------------------------------------------------------*/
  62. /* USER CODE BEGIN 0 */
  63. uint32_t my_counter =0;
  64. /* USER CODE END 0 */
  65. /**
  66. * @brief The application entry point.
  67. * @retval int
  68. */
  69. int main(void)
  70. {
  71. /* USER CODE BEGIN 1 */
  72. /* USER CODE END 1 */
  73. /* USER CODE BEGIN Boot_Mode_Sequence_0 */
  74. /* USER CODE END Boot_Mode_Sequence_0 */
  75. /* MPU Configuration--------------------------------------------------------*/
  76. MPU_Config();
  77. /* Enable I-Cache---------------------------------------------------------*/
  78. SCB_EnableICache();
  79. /* Enable D-Cache---------------------------------------------------------*/
  80. SCB_EnableDCache();
  81. /* USER CODE BEGIN Boot_Mode_Sequence_1 */
  82. /* USER CODE END Boot_Mode_Sequence_1 */
  83. /* MCU Configuration--------------------------------------------------------*/
  84. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  85. HAL_Init();
  86. /* USER CODE BEGIN Init */
  87. /* USER CODE END Init */
  88. /* Configure the system clock */
  89. SystemClock_Config();
  90. /* USER CODE BEGIN Boot_Mode_Sequence_2 */
  91. /* USER CODE END Boot_Mode_Sequence_2 */
  92. /* USER CODE BEGIN SysInit */
  93. ITM->TCR |= ITM_TCR_ITMENA_Msk;
  94. ITM->TER |= 1UL;
  95. // DBGMCU->CR |= DBGMCU_CR_DBG_TRACECKEN_Msk;
  96. /* USER CODE END SysInit */
  97. /* Initialize all configured peripherals */
  98. MX_GPIO_Init();
  99. /* USER CODE BEGIN 2 */
  100. /* ETH_CODE: fixed core synchronization
  101. * Release M4 core after GPIO and peripherals init
  102. * to avoid conflict.
  103. */
  104. __HAL_RCC_HSEM_CLK_ENABLE();
  105. HAL_HSEM_FastTake(HSEM_ID_0);
  106. HAL_HSEM_Release(HSEM_ID_0,0);
  107. /* USER CODE END 2 */
  108. /* Init scheduler */
  109. osKernelInitialize();
  110. /* USER CODE BEGIN RTOS_MUTEX */
  111. /* add mutexes, ... */
  112. /* USER CODE END RTOS_MUTEX */
  113. /* USER CODE BEGIN RTOS_SEMAPHORES */
  114. /* add semaphores, ... */
  115. /* USER CODE END RTOS_SEMAPHORES */
  116. /* USER CODE BEGIN RTOS_TIMERS */
  117. /* start timers, add new ones, ... */
  118. /* USER CODE END RTOS_TIMERS */
  119. /* USER CODE BEGIN RTOS_QUEUES */
  120. /* add queues, ... */
  121. /* USER CODE END RTOS_QUEUES */
  122. /* Create the thread(s) */
  123. /* creation of defaultTask */
  124. defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
  125. // echo_server_netconn_init();
  126. mqtt_cli_init();
  127. /* USER CODE BEGIN RTOS_THREADS */
  128. /* add threads, ... */
  129. /* USER CODE END RTOS_THREADS */
  130. /* USER CODE BEGIN RTOS_EVENTS */
  131. /* add events, ... */
  132. /* USER CODE END RTOS_EVENTS */
  133. /* Start scheduler */
  134. osKernelStart();
  135. /* We should never get here as control is now taken by the scheduler */
  136. /* Infinite loop */
  137. /* USER CODE BEGIN WHILE */
  138. while (1)
  139. {
  140. /* USER CODE END WHILE */
  141. /* USER CODE BEGIN 3 */
  142. }
  143. /* USER CODE END 3 */
  144. }
  145. /**
  146. * @brief System Clock Configuration
  147. * @retval None
  148. */
  149. void SystemClock_Config(void)
  150. {
  151. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  152. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  153. /** Supply configuration update enable
  154. */
  155. HAL_PWREx_ConfigSupply(PWR_DIRECT_SMPS_SUPPLY);
  156. /** Configure the main internal regulator output voltage
  157. */
  158. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  159. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  160. /** Initializes the RCC Oscillators according to the specified parameters
  161. * in the RCC_OscInitTypeDef structure.
  162. */
  163. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  164. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  165. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  166. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  167. RCC_OscInitStruct.PLL.PLLM = 5;
  168. RCC_OscInitStruct.PLL.PLLN = 160;
  169. RCC_OscInitStruct.PLL.PLLP = 2;
  170. RCC_OscInitStruct.PLL.PLLQ = 4;
  171. RCC_OscInitStruct.PLL.PLLR = 4;
  172. RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_2;
  173. RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  174. RCC_OscInitStruct.PLL.PLLFRACN = 0;
  175. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  176. {
  177. Error_Handler();
  178. }
  179. /** Initializes the CPU, AHB and APB buses clocks
  180. */
  181. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  182. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  183. |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  184. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  185. RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  186. RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  187. RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  188. RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  189. RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  190. RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
  191. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  192. {
  193. Error_Handler();
  194. }
  195. }
  196. /**
  197. * @brief GPIO Initialization Function
  198. * @param None
  199. * @retval None
  200. */
  201. static void MX_GPIO_Init(void)
  202. {
  203. /* GPIO Ports Clock Enable */
  204. __HAL_RCC_GPIOG_CLK_ENABLE();
  205. __HAL_RCC_GPIOA_CLK_ENABLE();
  206. __HAL_RCC_GPIOC_CLK_ENABLE();
  207. __HAL_RCC_GPIOE_CLK_ENABLE();
  208. __HAL_RCC_GPIOB_CLK_ENABLE();
  209. __HAL_RCC_GPIOI_CLK_ENABLE();
  210. __HAL_RCC_GPIOH_CLK_ENABLE();
  211. }
  212. /* USER CODE BEGIN 4 */
  213. /* USER CODE END 4 */
  214. /* USER CODE BEGIN Header_StartDefaultTask */
  215. /**
  216. * @brief Function implementing the defaultTask thread.
  217. * @param argument: Not used
  218. * @retval None
  219. */
  220. /* USER CODE END Header_StartDefaultTask */
  221. void StartDefaultTask(void *argument)
  222. {
  223. /* init code for LWIP */
  224. MX_LWIP_Init();
  225. /* USER CODE BEGIN 5 */
  226. /* ETH_CODE: Adding lwiperf to measure TCP/IP performance.
  227. * iperf 2.0.6 (or older?) is required for the tests. Newer iperf2 versions
  228. * might work without data check, but they send different headers.
  229. * iperf3 is not compatible at all.
  230. * Adding lwiperf.c file to the project is necessary.
  231. * The default include path should already contain
  232. * 'lwip/apps/lwiperf.h'
  233. */
  234. // LOCK_TCPIP_CORE();
  235. // lwiperf_start_tcp_server_default(NULL, NULL);
  236. //
  237. // ip4_addr_t remote_addr;
  238. // IP4_ADDR(&remote_addr, 192, 168, 1, 63);
  239. // lwiperf_start_tcp_client_default(&remote_addr, NULL, NULL);
  240. // UNLOCK_TCPIP_CORE();
  241. /* Infinite loop */
  242. for(;;)
  243. {
  244. // printf("Hello World! %d\n\r", (int16_t)my_counter);
  245. osDelay(pdMS_TO_TICKS(1000));
  246. // fflush(stdout);
  247. my_counter++;
  248. if(my_counter > 10)
  249. {
  250. my_counter = 0;
  251. }
  252. }
  253. /* USER CODE END 5 */
  254. }
  255. /* MPU Configuration */
  256. void MPU_Config(void)
  257. {
  258. MPU_Region_InitTypeDef MPU_InitStruct = {0};
  259. /* Disables the MPU */
  260. HAL_MPU_Disable();
  261. /** Initializes and configures the Region and the memory to be protected
  262. */
  263. MPU_InitStruct.Enable = MPU_REGION_ENABLE;
  264. MPU_InitStruct.Number = MPU_REGION_NUMBER0;
  265. MPU_InitStruct.BaseAddress = 0x0;
  266. MPU_InitStruct.Size = MPU_REGION_SIZE_4GB;
  267. MPU_InitStruct.SubRegionDisable = 0x87;
  268. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
  269. MPU_InitStruct.AccessPermission = MPU_REGION_NO_ACCESS;
  270. MPU_InitStruct.DisableExec = MPU_INSTRUCTION_ACCESS_DISABLE;
  271. MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
  272. MPU_InitStruct.IsCacheable = MPU_ACCESS_NOT_CACHEABLE;
  273. MPU_InitStruct.IsBufferable = MPU_ACCESS_NOT_BUFFERABLE;
  274. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  275. /** Initializes and configures the Region and the memory to be protected
  276. */
  277. MPU_InitStruct.Number = MPU_REGION_NUMBER1;
  278. MPU_InitStruct.BaseAddress = 0x30020000;
  279. MPU_InitStruct.Size = MPU_REGION_SIZE_128KB;
  280. MPU_InitStruct.SubRegionDisable = 0x0;
  281. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL1;
  282. MPU_InitStruct.AccessPermission = MPU_REGION_FULL_ACCESS;
  283. MPU_InitStruct.IsShareable = MPU_ACCESS_NOT_SHAREABLE;
  284. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  285. /** Initializes and configures the Region and the memory to be protected
  286. */
  287. MPU_InitStruct.Number = MPU_REGION_NUMBER2;
  288. MPU_InitStruct.BaseAddress = 0x30040000;
  289. MPU_InitStruct.Size = MPU_REGION_SIZE_512B;
  290. MPU_InitStruct.TypeExtField = MPU_TEX_LEVEL0;
  291. MPU_InitStruct.IsShareable = MPU_ACCESS_SHAREABLE;
  292. MPU_InitStruct.IsBufferable = MPU_ACCESS_BUFFERABLE;
  293. HAL_MPU_ConfigRegion(&MPU_InitStruct);
  294. /* Enables the MPU */
  295. HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
  296. }
  297. /**
  298. * @brief Period elapsed callback in non blocking mode
  299. * @note This function is called when TIM6 interrupt took place, inside
  300. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  301. * a global variable "uwTick" used as application time base.
  302. * @param htim : TIM handle
  303. * @retval None
  304. */
  305. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  306. {
  307. /* USER CODE BEGIN Callback 0 */
  308. /* USER CODE END Callback 0 */
  309. if (htim->Instance == TIM6) {
  310. HAL_IncTick();
  311. MilliTimer++;
  312. }
  313. /* USER CODE BEGIN Callback 1 */
  314. /* USER CODE END Callback 1 */
  315. }
  316. /**
  317. * @brief This function is executed in case of error occurrence.
  318. * @retval None
  319. */
  320. void Error_Handler(void)
  321. {
  322. /* USER CODE BEGIN Error_Handler_Debug */
  323. /* User can add his own implementation to report the HAL error return state */
  324. __disable_irq();
  325. while (1)
  326. {
  327. }
  328. /* USER CODE END Error_Handler_Debug */
  329. }
  330. #ifdef USE_FULL_ASSERT
  331. /**
  332. * @brief Reports the name of the source file and the source line number
  333. * where the assert_param error has occurred.
  334. * @param file: pointer to the source file name
  335. * @param line: assert_param error line source number
  336. * @retval None
  337. */
  338. void assert_failed(uint8_t *file, uint32_t line)
  339. {
  340. /* USER CODE BEGIN 6 */
  341. /* User can add his own implementation to report the file name and line number,
  342. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  343. /* USER CODE END 6 */
  344. }
  345. #endif /* USE_FULL_ASSERT */