From ec3764886a7f203a6ef994a5da42a1f9ddfcf25b Mon Sep 17 00:00:00 2001 From: youhy Date: Tue, 19 Dec 2023 15:04:15 -0800 Subject: [PATCH 1/2] add eventflag example --- examples/rtos/CMakeLists.txt | 3 ++ examples/rtos/eventflag.cc | 95 ++++++++++++++++++++++++++++++++++++ 2 files changed, 98 insertions(+) create mode 100644 examples/rtos/eventflag.cc diff --git a/examples/rtos/CMakeLists.txt b/examples/rtos/CMakeLists.txt index e16531ab..4b93e664 100644 --- a/examples/rtos/CMakeLists.txt +++ b/examples/rtos/CMakeLists.txt @@ -28,3 +28,6 @@ irm_add_arm_executable(${PROJECT_NAME}_spin TARGET DJI_Board_TypeA SOURCES spinlock.cc) +irm_add_arm_executable(${PROJECT_NAME}_eventflag + TARGET DJI_Board_TypeA + SOURCES eventflag.cc) diff --git a/examples/rtos/eventflag.cc b/examples/rtos/eventflag.cc new file mode 100644 index 00000000..e95bd918 --- /dev/null +++ b/examples/rtos/eventflag.cc @@ -0,0 +1,95 @@ +/**************************************************************************** + * * + * Copyright (C) 2023 RoboMaster. * + * Illini RoboMaster @ University of Illinois at Urbana-Champaign * + * * + * This program is free software: you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation, either version 3 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program. If not, see . * + * * + ****************************************************************************/ + +#include "bsp_gpio.h" +#include "cmsis_os.h" +#include "main.h" +#include "utils.h" + +#define KEY_GPIO_GROUP GPIOB +#define KEY_GPIO_PIN GPIO_PIN_2 + +#define VX_READY_SIGNAL (1 << 1) + +/* Event flag example + * Press user key on type A board and the green led should toggle. + * + * The difference between event flag VS thread flag is: + * Event flag: thread -> event flag -> N threads + * Thread flag: thread -> specific thread + */ + +static bsp::GPIO *gpio_green; +BoolEdgeDetector key_detector(false); + +osEventFlagsId_t key_event_flag; + +static osThreadId_t LED_GREEN_TaskHandle; + +const osThreadAttr_t LED2Task_attributes = {.name = "LEDGreenTask", + .attr_bits = osThreadDetached, + .cb_mem = nullptr, + .cb_size = 0, + .stack_mem = nullptr, + .stack_size = 128 * 4, + .priority = (osPriority_t)osPriorityNormal, + .tz_module = 0, + .reserved = 0}; + +void LED_GREEN_Task(void* argument) { + UNUSED(argument); + uint32_t flags; + while (true) { + + flags = osEventFlagsWait(key_event_flag, VX_READY_SIGNAL, osFlagsWaitAny, osWaitForever); + + if (flags & VX_READY_SIGNAL) { + print("\r\nOK!\r\n"); + gpio_green->Toggle(); + osDelay(200); + } + } +} + +void RM_RTOS_Threads_Init(void) { + LED_GREEN_TaskHandle = osThreadNew(LED_GREEN_Task, nullptr, &LED2Task_attributes); +} + +void RM_RTOS_Init(void) { + print_use_uart(&huart8); + gpio_green = new bsp::GPIO(LED_GREEN_GPIO_Port, LED_GREEN_Pin); + gpio_green->Low(); + + key_event_flag = osEventFlagsNew(nullptr); +} + +void RM_RTOS_Default_Task(const void* args) { + UNUSED(args); + bsp::GPIO key(KEY_GPIO_GROUP, KEY_GPIO_PIN); + while (true) { + key_detector.input(key.Read()); + if (key_detector.posEdge()) { + + osEventFlagsSet(key_event_flag, VX_READY_SIGNAL); + + print("event flag sent!"); + } + } +} From 8cad513cb5d2884545988a401b83f53600535d1e Mon Sep 17 00:00:00 2001 From: youhy Date: Tue, 19 Dec 2023 15:30:04 -0800 Subject: [PATCH 2/2] add comments on error code --- examples/rtos/eventflag.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/examples/rtos/eventflag.cc b/examples/rtos/eventflag.cc index e95bd918..34d9a223 100644 --- a/examples/rtos/eventflag.cc +++ b/examples/rtos/eventflag.cc @@ -59,7 +59,8 @@ void LED_GREEN_Task(void* argument) { while (true) { flags = osEventFlagsWait(key_event_flag, VX_READY_SIGNAL, osFlagsWaitAny, osWaitForever); - + // Note that if osEventFlagsWait returns a error code (most commonly, timeout), flags is negative + // and only checking flags & SIGNAL is not enough if (flags & VX_READY_SIGNAL) { print("\r\nOK!\r\n"); gpio_green->Toggle();