main.c 9.1 KB

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