본문 바로가기

FreeRTOS/FreeRTOS 기본 학습

07 FreeRTOS Tutorial: Task Utilities part1

 

https://www.youtube.com/watch?v=cUO_Hn6536s

 

TaskHandle_t myTask1Handle = NULL;
TaskHandle_t myTask2Handle = NULL;


void myTask1(void *pvParameters)
{

	TickType_t xDelay = pdMS_TO_TICKS(1000);
	TickType_t myLastUnblock;
	myLastUnblock = xTaskGetTickCount();
	//char* text = "Task1\r\n";
	char* text = pcTaskGetName(myTask1Handle);

	int count = 0;

		for(;;)
		{

			HAL_UART_Transmit(&huart2, (uint8_t *)text,strlen(text) , 10);
			vTaskDelayUntil( &myLastUnblock, xDelay );


			count ++;

			if(count ==3){

				TaskHandle_t tskHand;
				tskHand = xTaskGetCurrentTaskHandle();

				//vTaskDelete(tskHand);
				vTaskDelete( myTask2Handle );


			}

		}
}

void myTask2(void *pvParameters)
{

	TickType_t xDelay = pdMS_TO_TICKS(1000);
	TickType_t myLastUnblock;
	myLastUnblock = xTaskGetTickCount();

	char* text = pcTaskGetName(myTask2Handle);


		for(;;)
		{

			HAL_UART_Transmit(&huart2, (uint8_t *)text,strlen(text) , 10);
			vTaskDelayUntil( &myLastUnblock, xDelay );

		}
}



int main(void)
{

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_I2C1_Init();
  MX_I2S3_Init();
  MX_SPI1_Init();
  MX_USART2_UART_Init();
  /* USER CODE BEGIN 2 */


  int pass = 8;






  xTaskCreate(myTask1,					//Task Pointer
  		  	  (const char* const)"task11",	//Task Name
  			  configMINIMAL_STACK_SIZE + 500,	//Stack Depth
  			  (void *)0,						//Parameters to pass to task
  			  1,								//Task Priority
  			  &myTask2Handle);           //Pass Handle to created task

  xTaskCreate(myTask2,					//Task Pointer
		  	  (const char* const)"task22",	//Task Name
			  configMINIMAL_STACK_SIZE + 500,	//Stack Depth
			  (void *)0,						//Parameters to pass to task
			  1,								//Task Priority
			  &myTask2Handle);           //Pass Handle to created task




  /* USER CODE END 2 */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* definition and creation of defaultTask */
  osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
  defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  vTaskStartScheduler();
  /* USER CODE END RTOS_THREADS */

  /* Start scheduler */
  osKernelStart();
  
  /* We should never get here as control is now taken by the scheduler */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {


    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */