main.c 9.0 KB

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