본문 바로가기
프로그래밍/STM32

Free RTOS (Message Queue) Example

by C.D.콤파스 2023. 7. 12.

Task간 동기화및 메시지 송수신을 위한 메시지 Queue 사용예입니다.

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

 

'프로그래밍 > STM32' 카테고리의 다른 글

button(버튼) / Key(키) 이벤트 처리(핸들링)  (0) 2023.07.26
STM32 GPIO 설정 (Configuration)  (0) 2023.07.18
Free RTOS (Timer) Example  (0) 2023.07.13