본문 바로가기

FreeRTOS/FreeRTOS 기본 학습

10 FreeRTOS Tutorial: Queues part2

https://www.youtube.com/watch?v=Z-XD3Q7Hqps&list=PLEfMFrwVdbPYzMgeaLiFRb4ogjV8m3lt6&index=10

 

TaskHandle_t myTask1Handle = NULL;
TaskHandle_t myTask2Handle = NULL;

QueueHandle_t myQueue;

void myTask1(void *pvParameters)
{

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


	char myTxBuff[30];

	myQueue = xQueueCreate( 5 , sizeof(myTxBuff) ); //size of queue (number of block), size of each block
	//Queue Size를 1로  바꾸면 message 2는 담기지 않음

	sprintf(myTxBuff, "message 1");
	xQueueSend(myQueue, (void*) myTxBuff, (TickType_t) 0 );
	//xQueueSendToFront(myQueue, (void*) myTxBuff, (TickType_t) 0 );

	sprintf(myTxBuff, "message 2");
	xQueueSend(myQueue, (void*) myTxBuff, (TickType_t) 0 );
	//xQueueOverwrtie(myQueue, (void*) myTxBuff); size 초과해도 덮어쓴다.

	sprintf(myTxBuff, "message 3");
	xQueueSend(myQueue, (void*) myTxBuff, (TickType_t) 0 );





		for(;;){


			//HAL_UART_Transmit(&huart2, (uint8_t *)text,strlen(text) , 10);

			//vTaskDelayUntil( &myLastUnblock, xDelay );




		}
}

void myTask2(void *pvParameters)
{

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

	char* text = "Hello\r\n";
	char myRxBuff[30];

		for(;;)
		{
			if(myQueue != 0)
			{

				if(xQueueReceive( myQueue, (void*) myRxBuff, (TickType_t) 5)){
				xQueuePeek(( myQueue, (void*) myRxBuff, (TickType_t) 5)) //1개만 보고 Queue에서 삭제하지 않음 그 다음으로 이동하지 않고 그 자리에 머무른다.
					HAL_UART_Transmit(&huart2, (uint8_t *)myRxBuff,strlen(myRxBuff) , 10);
					HAL_UART_Transmit(&huart2, (uint8_t *)"\r\n",strlen("\r\n") , 10);
					 //printf("Hi\r\n");
					vTaskDelayUntil( &myLastUnblock, xDelay );
				}
			}
		}
}



/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */
  

  /* 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 +1000,	//Stack Depth
  			  (void *)0,						//Parameters to pass to task
  			  1,								//Task Priority
  			  &myTask1Handle);           //Pass Handle to created task

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



  /* 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 */
}