main.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. #include<string.h>
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. /* USER CODE END Includes */
  26. /* Private typedef -----------------------------------------------------------*/
  27. /* USER CODE BEGIN PTD */
  28. /* USER CODE END PTD */
  29. /* Private define ------------------------------------------------------------*/
  30. /* USER CODE BEGIN PD */
  31. /* USER CODE END PD */
  32. /* Private macro -------------------------------------------------------------*/
  33. /* USER CODE BEGIN PM */
  34. /* USER CODE END PM */
  35. /* Private variables ---------------------------------------------------------*/
  36. UART_HandleTypeDef huart1;
  37. //osThreadId defaultTaskHandle;
  38. //osThreadId myTask02Handle;
  39. /* USER CODE BEGIN PV */
  40. osThreadId task1Handle;
  41. osThreadId task2Handle;
  42. osThreadId task3Handle;
  43. /* USER CODE END PV */
  44. /* Private function prototypes -----------------------------------------------*/
  45. void SystemClock_Config(void);
  46. static void MX_GPIO_Init(void);
  47. static void MX_USART1_UART_Init(void);
  48. //void StartDefaultTask(void const * argument);
  49. void StartTask1(const void *argument);
  50. void StartTask2(const void *argument);
  51. void StartTask3(const void *argument);
  52. /* USER CODE BEGIN PFP */
  53. /* USER CODE END PFP */
  54. /* Private user code ---------------------------------------------------------*/
  55. /* USER CODE BEGIN 0 */
  56. /* USER CODE END 0 */
  57. /**
  58. * @brief The application entry point.
  59. * @retval int
  60. */
  61. int main(void)
  62. {
  63. /* USER CODE BEGIN 1 */
  64. /* USER CODE END 1 */
  65. /* MCU Configuration--------------------------------------------------------*/
  66. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  67. HAL_Init();
  68. /* USER CODE BEGIN Init */
  69. /* USER CODE END Init */
  70. /* Configure the system clock */
  71. SystemClock_Config();
  72. /* USER CODE BEGIN SysInit */
  73. /* USER CODE END SysInit */
  74. /* Initialize all configured peripherals */
  75. MX_GPIO_Init();
  76. MX_USART1_UART_Init();
  77. /* USER CODE BEGIN 2 */
  78. /* USER CODE END 2 */
  79. /* USER CODE BEGIN RTOS_MUTEX */
  80. /* add mutexes, ... */
  81. /* USER CODE END RTOS_MUTEX */
  82. /* USER CODE BEGIN RTOS_SEMAPHORES */
  83. /* add semaphores, ... */
  84. /* USER CODE END RTOS_SEMAPHORES */
  85. /* USER CODE BEGIN RTOS_TIMERS */
  86. /* start timers, add new ones, ... */
  87. /* USER CODE END RTOS_TIMERS */
  88. /* USER CODE BEGIN RTOS_QUEUES */
  89. /* add queues, ... */
  90. /* USER CODE END RTOS_QUEUES */
  91. /* Create the thread(s) */
  92. /* definition and creation of defaultTask */
  93. //osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
  94. //defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
  95. /* definition and creation of myTask02 */
  96. //osThreadDef(myTask02, StartTask02, osPriorityIdle, 0, 128);
  97. //myTask02Handle = osThreadCreate(osThread(myTask02), NULL);
  98. /* USER CODE BEGIN RTOS_THREADS */
  99. osThreadDef(Task1, StartTask1, osPriorityHigh, 0, 128);
  100. task1Handle = osThreadCreate(osThread(Task1), NULL);
  101. osThreadDef(Task2, StartTask2, osPriorityNormal, 0, 128);
  102. task2Handle = osThreadCreate(osThread(Task2), NULL);
  103. osThreadDef(Task3, StartTask3, osPriorityLow, 0, 128);
  104. task3Handle = osThreadCreate(osThread(Task3), NULL);
  105. /* add threads, ... */
  106. /* USER CODE END RTOS_THREADS */
  107. /* Start scheduler */
  108. osKernelStart();
  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. /* USER CODE END 4 */
  195. /* USER CODE BEGIN Header_StartDefaultTask */
  196. /**
  197. * @brief Function implementing the defaultTask thread.
  198. * @param argument: Not used
  199. * @retval None
  200. */
  201. /* USER CODE END Header_StartDefaultTask */
  202. //void StartDefaultTask(void const * argument)
  203. //{
  204. /* USER CODE BEGIN 5 */
  205. /* Infinite loop */
  206. //for(;;)
  207. //{
  208. //osDelay(1);
  209. //}
  210. /* USER CODE END 5 */
  211. //}
  212. /* USER CODE BEGIN Header_StartTask02 */
  213. /**
  214. * @brief Function implementing the myTask02 thread.
  215. * @param argument: Not used
  216. * @retval None
  217. */
  218. /* USER CODE END Header_StartTask02 */
  219. void StartTask1(const void *argument) {
  220. char message[] = "Task 1: High Priority\n\r";
  221. for (;;) {
  222. HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_0);
  223. HAL_UART_Transmit(&huart1, (uint8_t*)message, strlen(message), HAL_MAX_DELAY);
  224. osDelay(500); // Delay for 500 ms
  225. }
  226. }
  227. // Task 2: Medium Priority
  228. void StartTask2(const void *argument) {
  229. char message[] = "Task 2: Medium Priority\n\r";
  230. for (;;) {
  231. HAL_UART_Transmit(&huart1, (uint8_t*)message, strlen(message), HAL_MAX_DELAY);
  232. osDelay(1000);
  233. }
  234. }
  235. // Task 3: Low Priority
  236. void StartTask3(const void *argument) {
  237. char message[] = "Task 3: Low Priority\n\r";
  238. for (;;) {
  239. HAL_UART_Transmit(&huart1, (uint8_t*)message, strlen(message), HAL_MAX_DELAY);
  240. osDelay(1500);
  241. }
  242. }
  243. /* USER CODE END StartTask02 */
  244. /**
  245. * @brief Period elapsed callback in non blocking mode
  246. * @note This function is called when TIM1 interrupt took place, inside
  247. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  248. * a global variable "uwTick" used as application time base.
  249. * @param htim : TIM handle
  250. * @retval None
  251. */
  252. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  253. {
  254. /* USER CODE BEGIN Callback 0 */
  255. /* USER CODE END Callback 0 */
  256. if (htim->Instance == TIM1) {
  257. HAL_IncTick();
  258. }
  259. /* USER CODE BEGIN Callback 1 */
  260. /* USER CODE END Callback 1 */
  261. }
  262. /**
  263. * @brief This function is executed in case of error occurrence.
  264. * @retval None
  265. */
  266. void Error_Handler(void)
  267. {
  268. /* USER CODE BEGIN Error_Handler_Debug */
  269. /* User can add his own implementation to report the HAL error return state */
  270. __disable_irq();
  271. while (1)
  272. {
  273. }
  274. /* USER CODE END Error_Handler_Debug */
  275. }
  276. #ifdef USE_FULL_ASSERT
  277. /**
  278. * @brief Reports the name of the source file and the source line number
  279. * where the assert_param error has occurred.
  280. * @param file: pointer to the source file name
  281. * @param line: assert_param error line source number
  282. * @retval None
  283. */
  284. void assert_failed(uint8_t *file, uint32_t line)
  285. {
  286. /* USER CODE BEGIN 6 */
  287. /* User can add his own implementation to report the file name and line number,
  288. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  289. /* USER CODE END 6 */
  290. }
  291. #endif /* USE_FULL_ASSERT */