main.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2024 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. /* Private includes ----------------------------------------------------------*/
  23. /* USER CODE BEGIN Includes */
  24. #include"FreeRTOS.h"
  25. #include"task.h"
  26. #include"semphr.h"
  27. #include"timers.h"
  28. #include<string.h>
  29. #include"queue.h"
  30. #include<stdlib.h>
  31. #include<stdio.h>
  32. /* USER CODE END Includes */
  33. /* Private typedef -----------------------------------------------------------*/
  34. /* USER CODE BEGIN PTD */
  35. /* USER CODE END PTD */
  36. /* Private define ------------------------------------------------------------*/
  37. /* USER CODE BEGIN PD */
  38. /* USER CODE END PD */
  39. /* Private macro -------------------------------------------------------------*/
  40. /* USER CODE BEGIN PM */
  41. /* USER CODE END PM */
  42. /* Private variables ---------------------------------------------------------*/
  43. UART_HandleTypeDef huart1;
  44. osThreadId defaultTaskHandle;
  45. /* USER CODE BEGIN PV */
  46. SemaphoreHandle_t countermutex;
  47. TaskHandle_t Hpt_handler;
  48. TaskHandle_t Npt_handler;
  49. int counter =0;
  50. //TaskHandler_t Lpt_handler;
  51. /* USER CODE END PV */
  52. /* Private function prototypes -----------------------------------------------*/
  53. void SystemClock_Config(void);
  54. static void MX_GPIO_Init(void);
  55. static void MX_USART1_UART_Init(void);
  56. /* USER CODE BEGIN PFP */
  57. void vtask1(void*);
  58. void vtask2(void*);
  59. /* USER CODE END PFP */
  60. /* Private user code ---------------------------------------------------------*/
  61. /* USER CODE BEGIN 0 */
  62. /* USER CODE END 0 */
  63. /**
  64. * @brief The application entry point.
  65. * @retval int
  66. */
  67. int main(void)
  68. {
  69. /* USER CODE BEGIN 1 */
  70. /* USER CODE END 1 */
  71. /* MCU Configuration--------------------------------------------------------*/
  72. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  73. HAL_Init();
  74. /* USER CODE BEGIN Init */
  75. /* USER CODE END Init */
  76. /* Configure the system clock */
  77. SystemClock_Config();
  78. /* USER CODE BEGIN SysInit */
  79. /* USER CODE END SysInit */
  80. /* Initialize all configured peripherals */
  81. MX_GPIO_Init();
  82. MX_USART1_UART_Init();
  83. /* USER CODE BEGIN 2 */
  84. char p[]="enter into main function\n\r";
  85. HAL_UART_Transmit(&huart1, (uint8_t*)p, strlen(p), HAL_MAX_DELAY);
  86. /* USER CODE END 2 */
  87. /* USER CODE BEGIN RTOS_MUTEX */
  88. /* add mutexes, ... */
  89. countermutex = xSemaphoreCreateMutex();
  90. xSemaphoreGive(countermutex);
  91. /* USER CODE END RTOS_MUTEX */
  92. /* USER CODE BEGIN RTOS_SEMAPHORES */
  93. /* add semaphores, ... */
  94. /* USER CODE END RTOS_SEMAPHORES */
  95. /* USER CODE BEGIN RTOS_TIMERS */
  96. /* start timers, add new ones, ... */
  97. /* USER CODE END RTOS_TIMERS */
  98. /* USER CODE BEGIN RTOS_QUEUES */
  99. /* add queues, ... */
  100. /* USER CODE END RTOS_QUEUES */
  101. /* Create the thread(s) */
  102. /* definition and creation of defaultTask */
  103. /* USER CODE BEGIN RTOS_THREADS */
  104. xTaskCreate(vtask1,"Task1",128,NULL,1,&Hpt_handler);
  105. xTaskCreate(vtask2,"Task2",128,NULL,1,&Npt_handler);
  106. /* add threads, ... */
  107. /* USER CODE END RTOS_THREADS*/
  108. /* Start scheduler */
  109. vTaskStartScheduler();
  110. //osKernelStart();
  111. /* We should never get here as control is now taken by the scheduler */
  112. /* Infinite loop */
  113. /* USER CODE BEGIN WHILE */
  114. // while (1)
  115. //{
  116. /* USER CODE END WHILE */
  117. /* USER CODE BEGIN 3 */
  118. //}
  119. /* USER CODE END 3 */
  120. }
  121. /**
  122. * @brief System Clock Configuration
  123. * @retval None
  124. */
  125. void SystemClock_Config(void)
  126. {
  127. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  128. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  129. /** Configure the main internal regulator output voltage
  130. */
  131. __HAL_RCC_PWR_CLK_ENABLE();
  132. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
  133. /** Initializes the RCC Oscillators according to the specified parameters
  134. * in the RCC_OscInitTypeDef structure.
  135. */
  136. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  137. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  138. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  139. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  140. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  141. {
  142. Error_Handler();
  143. }
  144. /** Initializes the CPU, AHB and APB buses clocks
  145. */
  146. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  147. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  148. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  149. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  150. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  151. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  152. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  153. {
  154. Error_Handler();
  155. }
  156. }
  157. /**
  158. * @brief USART1 Initialization Function
  159. * @param None
  160. * @retval None
  161. */
  162. static void MX_USART1_UART_Init(void)
  163. {
  164. /* USER CODE BEGIN USART1_Init 0 */
  165. /* USER CODE END USART1_Init 0 */
  166. /* USER CODE BEGIN USART1_Init 1 */
  167. /* USER CODE END USART1_Init 1 */
  168. huart1.Instance = USART1;
  169. huart1.Init.BaudRate = 115200;
  170. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  171. huart1.Init.StopBits = UART_STOPBITS_1;
  172. huart1.Init.Parity = UART_PARITY_NONE;
  173. huart1.Init.Mode = UART_MODE_TX_RX;
  174. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  175. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  176. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  177. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  178. if (HAL_UART_Init(&huart1) != HAL_OK)
  179. {
  180. Error_Handler();
  181. }
  182. /* USER CODE BEGIN USART1_Init 2 */
  183. /* USER CODE END USART1_Init 2 */
  184. }
  185. /**
  186. * @brief GPIO Initialization Function
  187. * @param None
  188. * @retval None
  189. */
  190. static void MX_GPIO_Init(void)
  191. {
  192. /* GPIO Ports Clock Enable */
  193. __HAL_RCC_GPIOA_CLK_ENABLE();
  194. }
  195. /* USER CODE BEGIN 4 */
  196. /* USER CODE END 4 */
  197. /* USER CODE BEGIN Header_StartDefaultTask */
  198. void vtask1(void* parameters)
  199. {
  200. int i;
  201. while(1)
  202. {
  203. if(xSemaphoreTake(countermutex,portMAX_DELAY)==pdTRUE)
  204. {
  205. for(i=0;i<5;i++)
  206. {
  207. counter++;
  208. char message[20];
  209. snprintf(message,sizeof(message),"vtask1 count:%d\n\r",counter);
  210. HAL_UART_Transmit(&huart1, (uint8_t*)message, strlen(message), HAL_MAX_DELAY);
  211. }
  212. };
  213. //xSemaphoreGive(countermutex);
  214. vTaskDelay(2000);
  215. xSemaphoreGive(countermutex);
  216. vTaskDelay(2000);
  217. }
  218. }
  219. void vtask2(void* parameters)
  220. {
  221. int i;
  222. while(1)
  223. {
  224. if(xSemaphoreTake(countermutex,portMAX_DELAY)==pdTRUE)
  225. {
  226. for(i=0;i<5;i++)
  227. {
  228. counter++;
  229. char message[20];
  230. snprintf(message,sizeof(message),"vtask2 count:%d\n\r",counter);
  231. HAL_UART_Transmit(&huart1, (uint8_t*)message, strlen(message), HAL_MAX_DELAY);
  232. }
  233. };
  234. xSemaphoreGive(countermutex);
  235. vTaskDelay(2000);
  236. }
  237. }
  238. /* USER CODE END Header_StartDefaultTask */
  239. /**
  240. * @brief Period elapsed callback in non blocking mode
  241. * @note This function is called when TIM1 interrupt took place, inside
  242. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  243. * a global variable "uwTick" used as application time base.
  244. * @param htim : TIM handle
  245. * @retval None
  246. */
  247. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  248. {
  249. /* USER CODE BEGIN Callback 0 */
  250. /* USER CODE END Callback 0 */
  251. if (htim->Instance == TIM1) {
  252. HAL_IncTick();
  253. }
  254. /* USER CODE BEGIN Callback 1 */
  255. /* USER CODE END Callback 1 */
  256. }
  257. /**
  258. * @brief This function is executed in case of error occurrence.
  259. * @retval None
  260. */
  261. void Error_Handler(void)
  262. {
  263. /* USER CODE BEGIN Error_Handler_Debug */
  264. /* User can add his own implementation to report the HAL error return state */
  265. __disable_irq();
  266. while (1)
  267. {
  268. }
  269. /* USER CODE END Error_Handler_Debug */
  270. }
  271. #ifdef USE_FULL_ASSERT
  272. /**
  273. * @brief Reports the name of the source file and the source line number
  274. * where the assert_param error has occurred.
  275. * @param file: pointer to the source file name
  276. * @param line: assert_param error line source number
  277. * @retval None
  278. */
  279. void assert_failed(uint8_t *file, uint32_t line)
  280. {
  281. /* USER CODE BEGIN 6 */
  282. /* User can add his own implementation to report the file name and line number,
  283. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  284. /* USER CODE END 6 */
  285. }
  286. #endif /* USE_FULL_ASSERT */