본문 바로가기

FreeRTOS/FreeRTOS 기본 학습

09 FreeRTOS Tutorial: Queues part1

 

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

 

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

	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 );

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

	int waiting = uxQueueMessagesWaiting( myQueue);
	int available_spaces = uxQueueSpacesAvailable(myQueue);

	//printf("data waiting to be read : %d\r\n", uxQueueMessagesWaiting( myQueue));
	//printf("available spaces: %d\r\n", uxQueueSpacesAvailable(myQueue));



		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)){

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