main.c 9.2 KB

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