main.c 9.6 KB

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