main.c 9.0 KB

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