[STM32] FreeRTOS event group learning

Event Group

Before a task is executed, multiple conditions need to be judged. When all the conditions are met or one of the multiple conditions is met, it will be executed.

Experiment: Create two tasks, one event group, and print when the buttons 1, 2, and 3 are pressed once.

Implementation: When FreeRTOS uses the V1 version in CubeMX, Events cannot be set, and must be added manually, or use the V2 version.

The V1 version is used here, and the code implementation is modified on the basis of [STM32] FreeRTOS mutex learning

Step 1: Create an event group

/* USER CODE BEGIN FunctionPrototypes */

//创建事件组

EventGroupHandle_t myEventGroup=NULL;

/* USER CODE END FunctionPrototypes */

The second step, initialization

void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */
  //事件组初始化
  myEventGroup=xEventGroupCreate( );
  xEventGroupClearBits(myEventGroup,0X000);
  /* USER CODE END Init */
 
  /* Create the thread(s) */
  /* definition and creation of Task1 */
  osThreadDef(Task1, StartDefaultTask, osPriorityNormal, 0, 128);
  Task1Handle = osThreadCreate(osThread(Task1), NULL);

  /* definition and creation of Task2 */
  osThreadDef(Task2, StartTask02, osPriorityNormal, 0, 128);
  Task2Handle = osThreadCreate(osThread(Task2), NULL);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

}

The third step is to realize the button plus setting in task 1

void StartDefaultTask(void const * argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {

		if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_2)==0)
		{
			osDelay(20);
			if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_2)==0)
			{
				printf("KEY1\r\n");
				xEventGroupSetBits(myEventGroup,0X001);
				osDelay(2000);
			}
		}
		if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_3)==0)
		{
			osDelay(20);
			if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_3)==0)
			{
				printf("KEY2\r\n");
				xEventGroupSetBits(myEventGroup,0X010);
				osDelay(2000);
			}
		}
		if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_4)==0)
		{
			osDelay(20);
			if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_4)==0)
			{
				printf("KEY3\r\n");
				xEventGroupSetBits(myEventGroup,0X100);
				osDelay(2000);
			}
		}
  }
  /* USER CODE END StartDefaultTask */
}

The fourth step, in task 2, task group inspection and printing are realized

void StartTask02(void const * argument)
{
  /* USER CODE BEGIN StartTask02 */
  /* Infinite loop */
	uint32_t Event=0;
  for(;;)
  {
    Event=xEventGroupWaitBits(myEventGroup,0X111,pdTRUE,pdTRUE,portMAX_DELAY);
		if(Event==0X111)
		{
			printf("OK\r\n");
		}
  }
  /* USER CODE END StartTask02 */
}

Phenomenon: It is observed that KEY1, KEY2, and KEY3 must all be pressed to trigger the event group to print OK.

If one of KEY1, KEY2, and KEY3 is pressed to trigger the event group to print OK, the following modifications can be made:

void StartTask02(void const * argument)
{
  /* USER CODE BEGIN StartTask02 */
  /* Infinite loop */
	uint32_t Event=0;
  for(;;)
  {
    Event=xEventGroupWaitBits(myEventGroup,0X111,pdTRUE,pdFALSE,portMAX_DELAY);
		if(Event==0X111)
		{
			printf("OK\r\n");
		}
  }
  /* USER CODE END StartTask02 */
}

Guess you like

Origin blog.csdn.net/weixin_45015121/article/details/132345962