From 4d64e6760295482a67a71979e88c53b0f8aa5e92 Mon Sep 17 00:00:00 2001 From: demik Date: Sat, 21 Aug 2021 10:52:54 +0200 Subject: [PATCH] move quadrature tasks from notification to queue --- main/adb.c | 8 +++++--- main/blue.c | 47 +++++++++++++++++++++++++++++++++-------------- main/led.h | 13 ++++++++++--- main/quad.c | 42 ++++++++++++++++++++++-------------------- main/quad.h | 2 +- sdkconfig | 10 +++++----- 6 files changed, 76 insertions(+), 46 deletions(-) diff --git a/main/adb.c b/main/adb.c index cf16a60..65f5aae 100644 --- a/main/adb.c +++ b/main/adb.c @@ -23,6 +23,7 @@ #include "driver/gpio.h" #include "sdkconfig.h" #include "freertos/FreeRTOS.h" +#include "freertos/queue.h" #include "freertos/task.h" #include "esp_log.h" #include "esp_system.h" @@ -43,7 +44,8 @@ rmt_config_t adb_rmt_rx = RMT_DEFAULT_CONFIG_RX(GPIO_ADB, RMT_RX_CHANNEL); extern TaskHandle_t t_adb2hid; extern TaskHandle_t t_green, t_blue, t_yellow, t_red; -extern TaskHandle_t t_click, t_qx, t_qy; +extern TaskHandle_t t_click; +extern QueueHandle_t q_qx, q_qy; /* static defines */ static void adb_handle_button(bool action); @@ -234,7 +236,7 @@ void adb_task_host(void *pvParameters) { } if (move) - xTaskNotify(t_qx, move, eSetValueWithOverwrite); + xQueueSendToBack(q_qx, &move, 0); move = (data & ADB_CMP_MY) >> 8; if (move & 0x40) { @@ -244,7 +246,7 @@ void adb_task_host(void *pvParameters) { } if (move) - xTaskNotify(t_qy, move, eSetValueWithOverwrite); + xQueueSendToBack(q_qy, &move, 0); } else { last++; diff --git a/main/blue.c b/main/blue.c index 4dd14ce..24de856 100644 --- a/main/blue.c +++ b/main/blue.c @@ -54,10 +54,13 @@ static const char *TAG = "blue"; /* globals */ TaskHandle_t t_adb2hid; -extern TaskHandle_t t_click, t_qx, t_qy; +extern TaskHandle_t t_click; static esp_hidd_dev_t *hid_dev = NULL; +extern QueueHandle_t q_qx, q_qy; /* private functions */ +static void blue_d_connect(void); +static void blue_d_disconnect(esp_hidd_event_data_t *dev); static void blue_d_init(void); static void blue_d_start(void); void blue_h_callback(void *handler_args, esp_event_base_t base, int32_t id, void *event_data); @@ -83,8 +86,7 @@ static void blue_d_callback(void *handler_args, esp_event_base_t base, int32_t i break; } case ESP_HIDD_CONNECT_EVENT: { - ESP_LOGI(TAG, "CONNECT"); - xTaskNotify(t_blue, LED_ON, eSetValueWithOverwrite); + blue_d_connect(); break; } case ESP_HIDD_PROTOCOL_MODE_EVENT: { @@ -106,9 +108,7 @@ static void blue_d_callback(void *handler_args, esp_event_base_t base, int32_t i break; } case ESP_HIDD_DISCONNECT_EVENT: { - ESP_LOGI(TAG, "DISCONNECT: %s", esp_hid_disconnect_reason_str(esp_hidd_dev_transport_get(param->disconnect.dev), param->disconnect.reason)); - esp_hid_ble_gap_adv_start(); - xTaskNotify(t_blue, LED_SLOW, eSetValueWithOverwrite); + blue_d_disconnect(param); break; } case ESP_HIDD_STOP_EVENT: { @@ -121,6 +121,18 @@ static void blue_d_callback(void *handler_args, esp_event_base_t base, int32_t i return; } +static void blue_d_connect() { + ESP_LOGI(TAG, "Host connected"); + xTaskNotify(t_blue, LED_ON, eSetValueWithOverwrite); +} + +static void blue_d_disconnect(esp_hidd_event_data_t *dev) { + ESP_LOGI(TAG, "Host disconnected, reason: %s", + esp_hid_disconnect_reason_str(esp_hidd_dev_transport_get(dev->disconnect.dev), dev->disconnect.reason)); + esp_hid_ble_gap_adv_start(); + xTaskNotify(t_blue, LED_SLOW, eSetValueWithOverwrite); +} + static void blue_d_init() { esp_err_t ret; @@ -393,17 +405,24 @@ void blue_h_input(esp_hidh_dev_t *dev, uint8_t *data, uint16_t length) { x = data[1]; y = data[2]; + /* reduce bluetooth movement speed as well before notifying */ if (x != 0) { - if (x == 1 || x == -1) - xTaskNotify(t_qx, x, eSetValueWithOverwrite); - else - xTaskNotify(t_qx, x / 2, eSetValueWithOverwrite); + if (x == 1 || x == -1) { + xQueueSendToBack(q_qx, &x, 0); + } + else { + x /= 2; + xQueueSendToBack(q_qx, &x, 0); + } } if (y != 0) { - if (y == 1 || y == -1) - xTaskNotify(t_qy, y, eSetValueWithOverwrite); - else - xTaskNotify(t_qy, y / 2, eSetValueWithOverwrite); + if (y == 1 || y == -1) { + xQueueSendToBack(q_qy, &y, 0); + } + else { + y /= 2; + xQueueSendToBack(q_qy, &y, 0); + } } } diff --git a/main/led.h b/main/led.h index ec55cb8..be69b2f 100644 --- a/main/led.h +++ b/main/led.h @@ -23,9 +23,16 @@ #define LED_H /* prototypes */ -void led_dispatch(void *pvParameters); -void led_init(void); -void led_task(void *pvParameters); +void led_dispatch(void *pvParameters); +const char *led_gpio_name(uint8_t id); +void led_init(void); +void led_task(void *pvParameters); + +/* structures */ +typedef struct led_names_s { + const uint8_t num; + const char *name; +} led_names_t; /* defines */ #define LED_OFF (1 << 0) diff --git a/main/quad.c b/main/quad.c index afe560b..48b24c7 100644 --- a/main/quad.c +++ b/main/quad.c @@ -23,6 +23,7 @@ #include "driver/gpio.h" #include "sdkconfig.h" #include "freertos/FreeRTOS.h" +#include "freertos/queue.h" #include "freertos/task.h" #include "esp_system.h" #include "esp_err.h" @@ -37,6 +38,8 @@ TaskHandle_t t_click, t_qx, t_qy; esp_timer_handle_t quad_qx, quad_qy; +QueueHandle_t q_qx, q_qy; + /* static functions */ static void IRAM_ATTR quad_timer(void* arg); @@ -63,15 +66,18 @@ void quad_init(void) { gpio_set_level(GPIO_QY2, q2[0]); /* create timers for quadrature phases */ - args.callback = &quad_timer; + args.callback = quad_timer; args.arg = t_qx; args.name = "quad_qx"; ESP_ERROR_CHECK(esp_timer_create(&args, &quad_qx)); - args.callback = &quad_timer; + args.callback = quad_timer; args.arg = t_qy; args.name = "quad_qy"; ESP_ERROR_CHECK(esp_timer_create(&args, &quad_qy)); + q_qx = xQueueCreate(4, sizeof(int8_t)); + q_qy = xQueueCreate(4, sizeof(int8_t)); + ESP_LOGI("quad", "Quadrature tasks started on core %d", xPortGetCoreID()); } @@ -97,16 +103,14 @@ void quad_click(void *pvParameters) { } void IRAM_ATTR quad_move_x(void *pvParameters) { - unsigned int value = 0; - int move = 0; + int8_t move = 0; uint8_t i = 0; (void)pvParameters; while (true) { - xTaskNotifyWait(0, 0, &value, portMAX_DELAY); - assert(value != 0); - move = (signed)value; + xQueueReceive(q_qx, &move, portMAX_DELAY); + assert(move != 0); if (move > 0) { while (move--) { @@ -114,7 +118,7 @@ void IRAM_ATTR quad_move_x(void *pvParameters) { gpio_set_level(GPIO_QX1, q1[i % 4]); gpio_set_level(GPIO_QX2, q2[i % 4]); ESP_ERROR_CHECK(esp_timer_start_once(quad_qx, QUAD_INTERVAL)); - vTaskSuspend(NULL); + ulTaskNotifyTake(pdTRUE, portMAX_DELAY); } } else { @@ -123,23 +127,21 @@ void IRAM_ATTR quad_move_x(void *pvParameters) { gpio_set_level(GPIO_QX1, q1[i % 4]); gpio_set_level(GPIO_QX2, q2[i % 4]); ESP_ERROR_CHECK(esp_timer_start_once(quad_qx, QUAD_INTERVAL)); - vTaskSuspend(NULL); + ulTaskNotifyTake(pdTRUE, portMAX_DELAY); } } } } void IRAM_ATTR quad_move_y(void *pvParameters) { - unsigned int value = 0; - int move = 0; + int8_t move = 0; uint8_t i = 0; (void)pvParameters; while (true) { - xTaskNotifyWait(0, 0, &value, portMAX_DELAY); - assert(value != 0); - move = (signed)value; + xQueueReceive(q_qy, &move, portMAX_DELAY); + assert(move != 0); if (move > 0) { while (move--) { @@ -147,7 +149,7 @@ void IRAM_ATTR quad_move_y(void *pvParameters) { gpio_set_level(GPIO_QY1, q1[i % 4]); gpio_set_level(GPIO_QY2, q2[i % 4]); ESP_ERROR_CHECK(esp_timer_start_once(quad_qy, QUAD_INTERVAL)); - vTaskSuspend(NULL); + ulTaskNotifyTake(pdTRUE, portMAX_DELAY); } } else { @@ -156,18 +158,18 @@ void IRAM_ATTR quad_move_y(void *pvParameters) { gpio_set_level(GPIO_QY1, q1[i % 4]); gpio_set_level(GPIO_QY2, q2[i % 4]); ESP_ERROR_CHECK(esp_timer_start_once(quad_qy, QUAD_INTERVAL)); - vTaskSuspend(NULL); + ulTaskNotifyTake(pdTRUE, portMAX_DELAY); } } } } -/* simple ISR function. Resume task that called the oneshot timer */ +/* simple ISR function. Resume the task that called the oneshot timer */ static void IRAM_ATTR quad_timer(void* arg) { - BaseType_t xYieldRequired = pdFALSE; - xYieldRequired = xTaskResumeFromISR(arg); + BaseType_t pxHigherPriorityTaskWoken = pdFALSE; + vTaskNotifyGiveFromISR(arg, &pxHigherPriorityTaskWoken); /* switch context of needed */ - if( xYieldRequired == pdTRUE ) + if( pxHigherPriorityTaskWoken == pdTRUE ) portYIELD_FROM_ISR(); } diff --git a/main/quad.h b/main/quad.h index d7cd188..5fbe8f5 100644 --- a/main/quad.h +++ b/main/quad.h @@ -29,7 +29,7 @@ void quad_move_x(void *pvParameters); void quad_move_y(void *pvParameters); /* defines */ -#define QUAD_INTERVAL 400 +#define QUAD_INTERVAL 200 #endif diff --git a/sdkconfig b/sdkconfig index bd4b679..e3a272b 100644 --- a/sdkconfig +++ b/sdkconfig @@ -533,9 +533,9 @@ CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_0=y # CONFIG_ESP32_BROWNOUT_DET_LVL_SEL_7 is not set CONFIG_ESP32_BROWNOUT_DET_LVL=0 # CONFIG_ESP32_REDUCE_PHY_TX_POWER is not set -CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1=y +# CONFIG_ESP32_TIME_SYSCALL_USE_RTC_FRC1 is not set # CONFIG_ESP32_TIME_SYSCALL_USE_RTC is not set -# CONFIG_ESP32_TIME_SYSCALL_USE_FRC1 is not set +CONFIG_ESP32_TIME_SYSCALL_USE_FRC1=y # CONFIG_ESP32_TIME_SYSCALL_USE_NONE is not set CONFIG_ESP32_RTC_CLK_SRC_INT_RC=y # CONFIG_ESP32_RTC_CLK_SRC_EXT_CRYS is not set @@ -655,7 +655,7 @@ CONFIG_HTTPD_PURGE_BUF_LEN=32 CONFIG_ESP_NETIF_IP_LOST_TIMER_INTERVAL=120 CONFIG_ESP_NETIF_TCPIP_LWIP=y # CONFIG_ESP_NETIF_LOOPBACK is not set -CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER=y +# CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER is not set # end of ESP NETIF Adapter # @@ -670,7 +670,7 @@ CONFIG_ESP_SYSTEM_PANIC_PRINT_REBOOT=y # # High resolution timer (esp_timer) # -# CONFIG_ESP_TIMER_PROFILING is not set +CONFIG_ESP_TIMER_PROFILING=y CONFIG_ESP_TIMER_TASK_STACK_SIZE=3584 # CONFIG_ESP_TIMER_IMPL_FRC2 is not set CONFIG_ESP_TIMER_IMPL_TG0_LAC=y @@ -788,7 +788,7 @@ CONFIG_FREERTOS_ASSERT_ON_UNTESTED_FUNCTION=y # CONFIG_FREERTOS_CHECK_STACKOVERFLOW_PTRVAL is not set CONFIG_FREERTOS_CHECK_STACKOVERFLOW_CANARY=y # CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK is not set -CONFIG_FREERTOS_INTERRUPT_BACKTRACE=y +# CONFIG_FREERTOS_INTERRUPT_BACKTRACE is not set CONFIG_FREERTOS_THREAD_LOCAL_STORAGE_POINTERS=1 CONFIG_FREERTOS_ASSERT_FAIL_ABORT=y # CONFIG_FREERTOS_ASSERT_FAIL_PRINT_CONTINUE is not set