From cbc6d8a15bf3a163d1518d94598876dfeb0a8166 Mon Sep 17 00:00:00 2001 From: Benjamin Aigner Date: Tue, 2 Aug 2022 17:09:01 +0200 Subject: [PATCH 1/7] Changed flash size to 2MB in default config --- sdkconfig.defaults | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/sdkconfig.defaults b/sdkconfig.defaults index 453fcd6..5aef1ab 100644 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -10,10 +10,10 @@ CONFIG_ESP32_XTAL_FREQ_AUTO=y CONFIG_ESP32_XTAL_FREQ=0 # ESP32-WROOM uses 4MB -CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y -CONFIG_ESPTOOLPY_FLASHSIZE="4MB" -CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y +#CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y +#CONFIG_ESPTOOLPY_FLASHSIZE="4MB" +# Arduino Nano connect Nina Module has 2MB +CONFIG_ESPTOOLPY_FLASHSIZE_2MB=y +CONFIG_ESPTOOLPY_FLASHSIZE="2MB" -# flash with 921k by default -CONFIG_ESPTOOLPY_BAUD_921600B=y -CONFIG_ESPTOOLPY_BAUD=921600 +CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y From 1bca2675c735056a47430b01f8b9ee1d63ad0aaa Mon Sep 17 00:00:00 2001 From: Benjamin Aigner Date: Tue, 2 Aug 2022 17:12:30 +0200 Subject: [PATCH 2/7] Removed CMake requirements --- main/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index e5ca208..5a61976 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -3,6 +3,6 @@ idf_component_register(SRCS "ble_hidd_demo_main.c" "hid_dev.c" "hid_device_le_prf.c" INCLUDE_DIRS "." - REQUIRES esp_hid PRIV_REQUIRES esp32 esp_wifi esp_websocket_client esp_https_server esp_eth mdns lwip fatfs openssl esp_https_ota esp_hid app_update) + REQUIRES esp_hid PRIV_REQUIRES esp32 esp_wifi esp_https_server esp_eth mdns lwip fatfs esp_https_ota esp_hid app_update) target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-const-variable) From 023bc302fc9d8cda2dbb3b8b430e9d60188d5967 Mon Sep 17 00:00:00 2001 From: Benjamin Aigner Date: Tue, 2 Aug 2022 17:13:10 +0200 Subject: [PATCH 3/7] Changed KConfig and some main code to support the Arduino Nano RP2040 connect board --- main/Kconfig.projbuild | 121 ++++++++++------------ main/ble_hidd_demo_main.c | 211 ++++++++++++++++++++++++++------------ main/config.h | 48 ++++++--- 3 files changed, 231 insertions(+), 149 deletions(-) diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index 41b5a05..70cca9d 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -1,25 +1,60 @@ -menu "esp32_mouse_keyboard - FLipMouse & FABI config" +menu "esp32_mouse_keyboard / FLipMouse & FABI config" + config USE_AS_FLIPMOUSE_FABI + bool "Use project as FLipMouse/FABI Bluetooth module" + default n + help + If this option is enabled, a FLipMouse/FABI Bluetooth module + is built. If disabled, this project acts as demo code for Bluetooth + HID code. + menu "FM/FP/FABI configuration" + visible if USE_AS_FLIPMOUSE_FABI + + choice MODULE_TYPE + prompt "Module type (FLipMouse/FABI/FLipPad esp32miniBT or Arduino Nano Connect)" + depends on USE_AS_FLIPMOUSE_FABI + help + Specify the module type. FLipMouse/FABI/FLipPad is a esp32miniBT board, this setting changes the BLE name. Nano Connect is different in pinning & UART usage. + + config MODULE_FLIPMOUSE + bool "FLipMouse - esp32miniBT module" - config MODULE_RX_PIN - int "RX pin for command/HID UART" - default 17 - range 1 39 - - config MODULE_TX_PIN - int "TX pin for command/HID UART" - default 16 - range 1 39 - - config MODULE_LED_PIN - int "LED connection indicator pin" - default 5 - range 1 39 - - config MODULE_UART_NR - int "UART interface number" - default 2 - range 1 2 + config MODULE_FABI + bool "FABI - esp32miniBT module" + + config MODULE_NANO + bool "Arduino Nano Connect RP2040 with uBlox Nina" + endchoice + + config FM_WEBGUI + bool "Enable WebGUI (UNUSED)" + default n + help + Enable the WebGUI for the FLipMouse in the ESP32. + endmenu + + menu "Pinning for UART RX/TX & LED (if not a defined module)" + visible if !USE_AS_FLIPMOUSE_FABI + config MODULE_RX_PIN + int "RX pin for command/HID UART" + default 17 + range 1 39 + + config MODULE_TX_PIN + int "TX pin for command/HID UART" + default 16 + range 1 39 + + config MODULE_LED_PIN + int "LED connection indicator pin" + default 5 + range 1 39 + + config MODULE_UART_NR + int "UART interface number" + default 2 + range 1 2 + endmenu config MODULE_USEKEYBOARD bool "Enable BLE-HID keyboard (UNUSED)" default y @@ -56,50 +91,4 @@ menu "esp32_mouse_keyboard - FLipMouse & FABI config" If enabled, the module denies pairing requests by default. It is possible to enable the pairing by sending the pairing en-/disable command via the serial interface. - - - config USE_AS_FLIPMOUSE_FABI - bool "Use project as FLipMouse/FABI Bluetooth module" - default n - help - If this option is enabled, a FLipMouse/FABI Bluetooth module - is built. If disabled, this project acts as demo code for Bluetooth - HID code. - menu "FLipMouse / FABI configuration" - visible if USE_AS_FLIPMOUSE_FABI - - choice MODULE_TYPE - prompt "Module type (FLipMouse/FABI)" - depends on USE_AS_FLIPMOUSE_FABI - help - Specify the module type (FLipMouse or FABI). - - config MODULE_FLIPMOUSE - bool "FLipMouse" - - config MODULE_FABI - bool "FABI" - endchoice - - config FM_WEBGUI - bool "Enable WebGUI (UNUSED)" - default n - help - Enable the WebGUI for the FLipMouse in the ESP32. - - config FM_OTA - depends on MODULE_FLIPMOUSE - bool "Enable programming via Teensy (UNUSED)" - default n - help - Enable the update functionality via the Teensy module. - - config FABI_OTA - depends on MODULE_FABI - bool "Enable programming via ArduinoProMicro (UNUSED)" - default n - help - Enable the update functionality via the Arduino Pro Micro module. - - endmenu endmenu diff --git a/main/ble_hidd_demo_main.c b/main/ble_hidd_demo_main.c index 88146d4..d1e7aa3 100644 --- a/main/ble_hidd_demo_main.c +++ b/main/ble_hidd_demo_main.c @@ -282,7 +282,7 @@ static void periodicHIDCallback(void* arg) } //save timestamp for next call timestampLastSent = esp_timer_get_time(); - ESP_LOGI(HID_DEMO_TAG,"Idle..."); + ESP_LOGD(HID_DEMO_TAG,"Idle..."); } } @@ -427,7 +427,7 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param xEventGroupSetBits(eventgroup_system,SYSTEM_CURRENTLY_ADVERTISING); break; case ESP_GAP_BLE_SCAN_PARAM_SET_COMPLETE_EVT: - if(esp_ble_gap_start_scanning(3600) != ESP_OK) ESP_LOGE(HID_DEMO_TAG,"Cannot start scan"); + if(esp_ble_gap_start_scanning(3600) != ESP_OK) ESP_LOGW(HID_DEMO_TAG,"Cannot start scan"); else ESP_LOGI(HID_DEMO_TAG,"Start scan"); break; case ESP_GAP_BLE_SEC_REQ_EVT: @@ -446,7 +446,7 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param ESP_LOGI(HID_DEMO_TAG, "address type = %d", param->ble_security.auth_cmpl.addr_type); ESP_LOGI(HID_DEMO_TAG, "pair status = %s",param->ble_security.auth_cmpl.success ? "success" : "fail"); if(!param->ble_security.auth_cmpl.success) { - ESP_LOGE(HID_DEMO_TAG, "fail reason = 0x%x",param->ble_security.auth_cmpl.fail_reason); + ESP_LOGW(HID_DEMO_TAG, "fail reason = 0x%x",param->ble_security.auth_cmpl.fail_reason); } else { xEventGroupClearBits(eventgroup_system,SYSTEM_CURRENTLY_ADVERTISING); } @@ -505,7 +505,7 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param case ESP_GAP_BLE_SCAN_START_COMPLETE_EVT: //scan start complete event to indicate scan start successfully or failed if (param->scan_start_cmpl.status != ESP_BT_STATUS_SUCCESS) { - ESP_LOGE(HID_DEMO_TAG, "scan start failed, error status = %x", param->scan_start_cmpl.status); + ESP_LOGW(HID_DEMO_TAG, "scan start failed, error status = %x", param->scan_start_cmpl.status); break; } ESP_LOGI(HID_DEMO_TAG, "Scan start success"); @@ -530,18 +530,50 @@ void processCommand(struct cmdBuf *cmdBuffer) // $GV get the value of the given key from NVS. Note: no spaces in ! max. key length: 15 // $SV set the value of the given key & store to NVS. Note: no spaces in ! // $CV clear all key/value pairs set with $SV - // $UG start flash update by searching for factory partition and rebooting there. + // $UG start flash update by searching for factory partition and rebooting there. Warning: not possible to boot back without flashing! + // $LGx (0,1,2): enable / disable logging system of ESP32.0 is level error, 1 is level info, 2 is level debug if(cmdBuffer->bufferLength < 2) return; //easier this way than typecast in each str* function const char *input = (const char *) cmdBuffer->buf; int len = cmdBuffer->bufferLength; - uint8_t keycode; const char *nl = "\r\n"; esp_ble_bond_dev_t * btdevlist; int counter; esp_err_t ret; - + + /**++++en-/disable logging++++*/ + if(strcmp(input,"LG0") == 0) + { + esp_log_level_set("*",ESP_LOG_ERROR); + if(cmdBuffer->sendToUART != 0) + { + uart_write_bytes(EX_UART_NUM, "LOG:0",strlen("LOG:0")); + uart_write_bytes(EX_UART_NUM,nl,sizeof(nl)); //newline + } + return; + } + if(strcmp(input,"LG1") == 0) + { + esp_log_level_set("*",ESP_LOG_INFO); + if(cmdBuffer->sendToUART != 0) + { + uart_write_bytes(EX_UART_NUM, "LOG:1",strlen("LOG:1")); + uart_write_bytes(EX_UART_NUM,nl,sizeof(nl)); //newline + } + return; + } + if(strcmp(input,"LG2") == 0) + { + esp_log_level_set("*",ESP_LOG_DEBUG); + if(cmdBuffer->sendToUART != 0) + { + uart_write_bytes(EX_UART_NUM, "LOG:2",strlen("LOG:2")); + uart_write_bytes(EX_UART_NUM,nl,sizeof(nl)); //newline + } + return; + } + /**++++ key/value storing ++++*/ if(strncmp(input,"CV ", 2) == 0) { @@ -584,7 +616,7 @@ void processCommand(struct cmdBuf *cmdBuffer) if(ret != ESP_OK) { //send back error message - ESP_LOGE(EXT_UART_TAG,"error reading value: %s",esp_err_to_name(ret)); + ESP_LOGI(EXT_UART_TAG,"error reading value: %s",esp_err_to_name(ret)); if(cmdBuffer->sendToUART != 0) { uart_write_bytes(EX_UART_NUM, "NVS:",strlen("NVS:")); @@ -615,7 +647,7 @@ void processCommand(struct cmdBuf *cmdBuffer) if(work == NULL) { - ESP_LOGE(EXT_UART_TAG,"error setting string: no value provided"); + ESP_LOGI(EXT_UART_TAG,"error setting string: no value provided"); if(cmdBuffer->sendToUART != 0) { uart_write_bytes(EX_UART_NUM, "NVS:ESP_ERR_NVS_NO_VALUE",strlen("NVS:ESP_ERR_NVS_NO_VALUE")); @@ -636,7 +668,7 @@ void processCommand(struct cmdBuf *cmdBuffer) if(ret != ESP_OK) { //send back error message - ESP_LOGE(EXT_UART_TAG,"error setting string: %s",esp_err_to_name(ret)); + ESP_LOGI(EXT_UART_TAG,"error setting string: %s",esp_err_to_name(ret)); if(cmdBuffer->sendToUART != 0) { uart_write_bytes(EX_UART_NUM, "NVS:",strlen("NVS:")); @@ -811,10 +843,10 @@ void processCommand(struct cmdBuf *cmdBuffer) if(cmdBuffer->sendToUART != 0) uart_write_bytes(EX_UART_NUM,nl,sizeof(nl)); //newline } ESP_LOGI(EXT_UART_TAG,"---------------------------------------"); - } else ESP_LOGE(EXT_UART_TAG,"error getting device list"); + } else ESP_LOGW(EXT_UART_TAG,"error getting device list"); } else ESP_LOGE(EXT_UART_TAG,"error allocating memory for device list"); } else { - ESP_LOGE(EXT_UART_TAG,"error getting bonded devices count or no devices bonded"); + ESP_LOGI(EXT_UART_TAG,"error getting bonded devices count or no devices bonded"); if(cmdBuffer->sendToUART != 0) uart_write_bytes(EX_UART_NUM, "END\r\n", 5); } return; @@ -833,13 +865,13 @@ void processCommand(struct cmdBuf *cmdBuffer) counter = esp_ble_get_bond_device_num(); if(counter == 0) { - ESP_LOGE(EXT_UART_TAG,"error deleting device, no paired devices"); + ESP_LOGI(EXT_UART_TAG,"error deleting device, no paired devices"); return; } if(index_to_remove >= counter) { - ESP_LOGE(EXT_UART_TAG,"error deleting device, number out of range"); + ESP_LOGW(EXT_UART_TAG,"error deleting device, number out of range"); return; } if(counter >= 0) @@ -863,14 +895,14 @@ void processCommand(struct cmdBuf *cmdBuffer) esp_ble_gap_update_whitelist(false,btdevlist[i].bd_addr,BLE_WL_ADDR_TYPE_RANDOM); } } - } else ESP_LOGE(EXT_UART_TAG,"error getting device list"); + } else ESP_LOGI(EXT_UART_TAG,"error getting device list"); free (btdevlist); //wait 20 ticks for everything to settle (write commits to NVS) vTaskDelay(20); //then restart to avoid re-bonding of the device(s). esp_restart(); - } else ESP_LOGE(EXT_UART_TAG,"error allocating memory for device list"); - } else ESP_LOGE(EXT_UART_TAG,"error getting bonded devices count"); + } else ESP_LOGW(EXT_UART_TAG,"error allocating memory for device list"); + } else ESP_LOGW(EXT_UART_TAG,"error getting bonded devices count"); return; } @@ -901,6 +933,12 @@ void processCommand(struct cmdBuf *cmdBuffer) uart_write_bytes(EX_UART_NUM, "OTA:start", strlen("OTA:start")); uart_write_bytes(EX_UART_NUM, nl, sizeof(nl)); ESP_LOGI(EXT_UART_TAG, "Addon board in upgrade mode"); + //LED off + #if !CONFIG_MODULE_NANO + gpio_set_level(INDICATOR_LED_PIN, 0); + #else + gpio_set_level(INDICATOR_LED_PIN, 1); + #endif esp_restart(); } else { @@ -915,7 +953,7 @@ void processCommand(struct cmdBuf *cmdBuffer) } return; } - ESP_LOGE(EXT_UART_TAG,"No command executed with: %s ; len= %d\n",input,len); + ESP_LOGW(EXT_UART_TAG,"No command executed with: %s ; len= %d\n",input,len); } void uart_parse_command (uint8_t character, struct cmdBuf * cmdBuffer) @@ -982,7 +1020,7 @@ void uart_parse_command (uint8_t character, struct cmdBuf * cmdBuffer) mouseButtons = cmdBuffer->buf[2]; //ESP_LOGI(EXT_UART_TAG,"m: %d/%d",cmdBuffer->buf[3],cmdBuffer->buf[4]); } - else ESP_LOGE(EXT_UART_TAG,"Unknown RAW HID packet"); + else ESP_LOGW(EXT_UART_TAG,"Unknown RAW HID packet"); } cmdBuffer->state=CMDSTATE_IDLE; } @@ -1011,40 +1049,49 @@ void uart_external_task(void *pvParameters) { char character; struct cmdBuf cmdBuffer; - int changePinning = 0; - - /* determine if we should switch RX/TX pins. */ - /* we enable the RX pin as GPIO with pull-down. - * if "1" is read, this is the "real" RX pin. If not, it - * should be the TX pin. */ - - gpio_config_t io_conf; - //disable interrupt - io_conf.intr_type = GPIO_INTR_DISABLE; - //set as output mode - io_conf.mode = GPIO_MODE_INPUT; - //bit mask, we want to set the RX pin as input - io_conf.pin_bit_mask = (1ULL< Date: Thu, 4 Aug 2022 09:03:28 +0200 Subject: [PATCH 4/7] Changed version string, updated README --- README.md | 10 ++++++++++ main/config.h | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d5eaead..66c85b1 100644 --- a/README.md +++ b/README.md @@ -40,6 +40,16 @@ With `idf.py -p (PORT) flash` or `make flash` you can upload this build to an ES With `idf.py -p (PORT) monitor` or `make monitor` you can see the debug output (please use this output if you open an issue) or trigger basic test commands (mouse movement or a keyboard key press) on a connected target. + +### esp32miniBT vs. Arduino Nano Connect + +This firmware is used on 2 different devices in context of our assistive devices: +* __esp32miniBT module__: This board is located in this repository and is used as addon for v2 FLipMouse, FABI and FLipPad PCBs which use either a TeensyLC or a ProMicro controller. +* __Arduino Nano RP2040 Connect__: Version 3 of FLipMouse and FLipPad (maybe FABI as well) use this Arduino board, where we have a RP2040 controller AND the ESP32 on the same board (more RAM, more flash, cheaper than 2 boards). + +__Note:__ Please select the correct type of board in `idf.py menuconfig`! + +__Note:__ If you want to use this firmware on a custom board, you can select the external UART interface settings in menuconfig; for our boards the pinning, baudrate and UART config is pre-defined. # Usage via Console or second UART diff --git a/main/config.h b/main/config.h index 9c73ba3..f24aa08 100644 --- a/main/config.h +++ b/main/config.h @@ -2,7 +2,7 @@ #ifndef _CONFIG_H_ #define _CONFIG_H_ -#define MODULE_ID "ESP32miniBT_v0.3.2" +#define MODULE_ID "ESP32miniBT_v0.3.3" #if CONFIG_USE_AS_FLIPMOUSE_FABI #define EX_SERIAL_RXPIN GPIO_NUM_17 From 0693f5ac6d7524cda416def8133b8cf235520117 Mon Sep 17 00:00:00 2001 From: beni Date: Wed, 8 Feb 2023 12:00:00 +0100 Subject: [PATCH 5/7] Added idf v5 compatibility --- CMakeLists.txt | 2 +- main/CMakeLists.txt | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 4da3a2f..f31a6d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ # The following lines of boilerplate have to be in your project's CMakeLists # in this exact order for cmake to work correctly -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.16) set(SUPPORTED_TARGETS esp32) include($ENV{IDF_PATH}/tools/cmake/project.cmake) diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 5a61976..31d5ae7 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -3,6 +3,6 @@ idf_component_register(SRCS "ble_hidd_demo_main.c" "hid_dev.c" "hid_device_le_prf.c" INCLUDE_DIRS "." - REQUIRES esp_hid PRIV_REQUIRES esp32 esp_wifi esp_https_server esp_eth mdns lwip fatfs esp_https_ota esp_hid app_update) + REQUIRES esp_hid PRIV_REQUIRES esp_wifi esp_https_server esp_eth nvs_flash lwip fatfs esp_https_ota esp_hid app_update) target_compile_options(${COMPONENT_LIB} PRIVATE -Wno-unused-const-variable) From 9324fe374b0a2b76572f1328cd0636f14301b74c Mon Sep 17 00:00:00 2001 From: beni Date: Wed, 8 Feb 2023 13:46:23 +0100 Subject: [PATCH 6/7] Added compatibility code to use this firmware on addon boards and the Arduino RP2040 Connect --- main/Kconfig.projbuild | 27 +--- main/ble_hidd_demo_main.c | 317 +++++++++++++++++++++++--------------- main/config.h | 27 +--- 3 files changed, 194 insertions(+), 177 deletions(-) diff --git a/main/Kconfig.projbuild b/main/Kconfig.projbuild index 70cca9d..34e84f0 100644 --- a/main/Kconfig.projbuild +++ b/main/Kconfig.projbuild @@ -6,32 +6,7 @@ menu "esp32_mouse_keyboard / FLipMouse & FABI config" If this option is enabled, a FLipMouse/FABI Bluetooth module is built. If disabled, this project acts as demo code for Bluetooth HID code. - menu "FM/FP/FABI configuration" - visible if USE_AS_FLIPMOUSE_FABI - - choice MODULE_TYPE - prompt "Module type (FLipMouse/FABI/FLipPad esp32miniBT or Arduino Nano Connect)" - depends on USE_AS_FLIPMOUSE_FABI - help - Specify the module type. FLipMouse/FABI/FLipPad is a esp32miniBT board, this setting changes the BLE name. Nano Connect is different in pinning & UART usage. - - config MODULE_FLIPMOUSE - bool "FLipMouse - esp32miniBT module" - - config MODULE_FABI - bool "FABI - esp32miniBT module" - - config MODULE_NANO - bool "Arduino Nano Connect RP2040 with uBlox Nina" - endchoice - - config FM_WEBGUI - bool "Enable WebGUI (UNUSED)" - default n - help - Enable the WebGUI for the FLipMouse in the ESP32. - endmenu - + menu "Pinning for UART RX/TX & LED (if not a defined module)" visible if !USE_AS_FLIPMOUSE_FABI diff --git a/main/ble_hidd_demo_main.c b/main/ble_hidd_demo_main.c index d1e7aa3..eb522af 100644 --- a/main/ble_hidd_demo_main.c +++ b/main/ble_hidd_demo_main.c @@ -14,7 +14,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, * MA 02110-1301, USA. * - * + * Copyright 2023: + * Benjamin Aigner , + * * Copyright 2020-2021: * Benjamin Aigner ,, * Junaid Khan @@ -62,6 +64,7 @@ #include "hid_dev.h" #include "config.h" #include "esp_ota_ops.h" +#include "esp_flash.h" /** * Brief: @@ -125,6 +128,32 @@ uint64_t timestampLastSent; * @see periodicHIDCallback */ uint8_t mouseButtons = 0; +/** Flag for hardware which this firmware is running on. + * If we are running this software on the Arudino RP2040 connect, + * we need to use different pins, also another UART (including pins). + * This flag is set immediately after startup in app_main and is set + * based on the flash size. All modules we used were with >=4MB flash, + * the uBlox NINA module on the Arduino RP2040 connect uses 2MB. */ +bool onArduinoRP2040 = false; + +/** 2nd UART unit number for RX/TX of commands + * Previously we used a define via KConfig to set the UART number. + * Because we need to detect on the fly if we are on an Arduino RP2040 Connect + * or on a Bluetooth addon, this value is set in a variable now. */ +int ext_uart_num = 0; + +/** 2nd UART unit RX pin */ +int ext_uart_rx = -1; + +/** 2nd UART unit TX pin */ +int ext_uart_tx = -1; + +/** pin number for indicator LED + * Was also a KConfig define for all available builds, now it is fixed + * for FABI/FM/FP builds depending on Arduino or addon board. + * For other modules or custom builds, it can be defined via idf.py menuconfig. */ +int indicator_led = -1; + /** "Keepalive" rate when in idle (no HID commands) * @note Microseconds! * @see timestampLastSent @@ -273,7 +302,7 @@ int get_int(const char * input, int index, int * value) /** Periodic sending of empty HID reports if no updates are sent via API */ static void periodicHIDCallback(void* arg) { - if(abs(esp_timer_get_time()-timestampLastSent) > HID_IDLE_UPDATE_RATE) + if((esp_timer_get_time()-timestampLastSent) > HID_IDLE_UPDATE_RATE) { //send empty report (but with last known button state) for(uint8_t i = 0; isendToUART != 0) { - uart_write_bytes(EX_UART_NUM, "LOG:0",strlen("LOG:0")); - uart_write_bytes(EX_UART_NUM,nl,sizeof(nl)); //newline + uart_write_bytes(ext_uart_num, "LOG:0",strlen("LOG:0")); + uart_write_bytes(ext_uart_num,nl,sizeof(nl)); //newline } return; } @@ -558,8 +587,8 @@ void processCommand(struct cmdBuf *cmdBuffer) esp_log_level_set("*",ESP_LOG_INFO); if(cmdBuffer->sendToUART != 0) { - uart_write_bytes(EX_UART_NUM, "LOG:1",strlen("LOG:1")); - uart_write_bytes(EX_UART_NUM,nl,sizeof(nl)); //newline + uart_write_bytes(ext_uart_num, "LOG:1",strlen("LOG:1")); + uart_write_bytes(ext_uart_num,nl,sizeof(nl)); //newline } return; } @@ -568,8 +597,8 @@ void processCommand(struct cmdBuf *cmdBuffer) esp_log_level_set("*",ESP_LOG_DEBUG); if(cmdBuffer->sendToUART != 0) { - uart_write_bytes(EX_UART_NUM, "LOG:2",strlen("LOG:2")); - uart_write_bytes(EX_UART_NUM,nl,sizeof(nl)); //newline + uart_write_bytes(ext_uart_num, "LOG:2",strlen("LOG:2")); + uart_write_bytes(ext_uart_num,nl,sizeof(nl)); //newline } return; } @@ -586,8 +615,8 @@ void processCommand(struct cmdBuf *cmdBuffer) ESP_LOGI(EXT_UART_TAG,"cleared all NVS key/value pairs"); if(cmdBuffer->sendToUART != 0) { - uart_write_bytes(EX_UART_NUM, "NVS:OK",strlen("NVS:OK")); - uart_write_bytes(EX_UART_NUM,nl,sizeof(nl)); //newline + uart_write_bytes(ext_uart_num, "NVS:OK",strlen("NVS:OK")); + uart_write_bytes(ext_uart_num,nl,sizeof(nl)); //newline } return; } @@ -619,15 +648,15 @@ void processCommand(struct cmdBuf *cmdBuffer) ESP_LOGI(EXT_UART_TAG,"error reading value: %s",esp_err_to_name(ret)); if(cmdBuffer->sendToUART != 0) { - uart_write_bytes(EX_UART_NUM, "NVS:",strlen("NVS:")); - uart_write_bytes(EX_UART_NUM, esp_err_to_name(ret), strlen(esp_err_to_name(ret))); - uart_write_bytes(EX_UART_NUM,nl,sizeof(nl)); //newline + uart_write_bytes(ext_uart_num, "NVS:",strlen("NVS:")); + uart_write_bytes(ext_uart_num, esp_err_to_name(ret), strlen(esp_err_to_name(ret))); + uart_write_bytes(ext_uart_num,nl,sizeof(nl)); //newline } } else { ESP_LOGI(EXT_UART_TAG,"loaded - %s:%s",key,nvspayload); - uart_write_bytes(EX_UART_NUM, "NVS:",strlen("NVS:")); - uart_write_bytes(EX_UART_NUM, nvspayload, strlen(nvspayload)); - uart_write_bytes(EX_UART_NUM,nl,sizeof(nl)); //newline + uart_write_bytes(ext_uart_num, "NVS:",strlen("NVS:")); + uart_write_bytes(ext_uart_num, nvspayload, strlen(nvspayload)); + uart_write_bytes(ext_uart_num,nl,sizeof(nl)); //newline } //done with the payload @@ -650,8 +679,8 @@ void processCommand(struct cmdBuf *cmdBuffer) ESP_LOGI(EXT_UART_TAG,"error setting string: no value provided"); if(cmdBuffer->sendToUART != 0) { - uart_write_bytes(EX_UART_NUM, "NVS:ESP_ERR_NVS_NO_VALUE",strlen("NVS:ESP_ERR_NVS_NO_VALUE")); - uart_write_bytes(EX_UART_NUM,nl,sizeof(nl)); //newline + uart_write_bytes(ext_uart_num, "NVS:ESP_ERR_NVS_NO_VALUE",strlen("NVS:ESP_ERR_NVS_NO_VALUE")); + uart_write_bytes(ext_uart_num,nl,sizeof(nl)); //newline } return; } @@ -671,9 +700,9 @@ void processCommand(struct cmdBuf *cmdBuffer) ESP_LOGI(EXT_UART_TAG,"error setting string: %s",esp_err_to_name(ret)); if(cmdBuffer->sendToUART != 0) { - uart_write_bytes(EX_UART_NUM, "NVS:",strlen("NVS:")); - uart_write_bytes(EX_UART_NUM, esp_err_to_name(ret), strlen(esp_err_to_name(ret))); - uart_write_bytes(EX_UART_NUM,nl,sizeof(nl)); //newline + uart_write_bytes(ext_uart_num, "NVS:",strlen("NVS:")); + uart_write_bytes(ext_uart_num, esp_err_to_name(ret), strlen(esp_err_to_name(ret))); + uart_write_bytes(ext_uart_num,nl,sizeof(nl)); //newline } } else { //send back OK & used/free entries @@ -682,11 +711,11 @@ void processCommand(struct cmdBuf *cmdBuffer) ESP_LOGI(EXT_UART_TAG,"set - %s:%s - used:%d,free:%d",key,nvspayload,nvs_stats.used_entries, nvs_stats.free_entries); if(cmdBuffer->sendToUART != 0) { - uart_write_bytes(EX_UART_NUM, "NVS:OK ", strlen("NVS:OK ")); + uart_write_bytes(ext_uart_num, "NVS:OK ", strlen("NVS:OK ")); char stats[64]; sprintf(stats,"%d/%d - used/free",nvs_stats.used_entries, nvs_stats.free_entries); - uart_write_bytes(EX_UART_NUM,stats,strnlen(stats,64)); - uart_write_bytes(EX_UART_NUM,nl,sizeof(nl)); //newline + uart_write_bytes(ext_uart_num,stats,strnlen(stats,64)); + uart_write_bytes(ext_uart_num,nl,sizeof(nl)); //newline } } return; @@ -707,13 +736,13 @@ void processCommand(struct cmdBuf *cmdBuffer) if(memcmp(active_connections[i],empty,sizeof(esp_bd_addr_t)) != 0) { //print on monitor & external uart - if(cmdBuffer->sendToUART != 0) uart_write_bytes(EX_UART_NUM, "CONNECTED:",strlen("CONNECTED:")); + if(cmdBuffer->sendToUART != 0) uart_write_bytes(ext_uart_num, "CONNECTED:",strlen("CONNECTED:")); esp_log_buffer_hex(EXT_UART_TAG, active_connections[i], sizeof(esp_bd_addr_t)); for (int t=0; tsendToUART != 0) uart_write_bytes(EX_UART_NUM, hexnum, 3); + if(cmdBuffer->sendToUART != 0) uart_write_bytes(ext_uart_num, hexnum, 3); } - if(cmdBuffer->sendToUART != 0) uart_write_bytes(EX_UART_NUM,nl,sizeof(nl)); //newline + if(cmdBuffer->sendToUART != 0) uart_write_bytes(ext_uart_num,nl,sizeof(nl)); //newline } } ESP_LOGI(EXT_UART_TAG,"---------------------------------------"); @@ -759,8 +788,8 @@ void processCommand(struct cmdBuf *cmdBuffer) { if(cmdBuffer->sendToUART != 0) { - uart_write_bytes(EX_UART_NUM, MODULE_ID, sizeof(MODULE_ID)); - uart_write_bytes(EX_UART_NUM, nl, sizeof(nl)); + uart_write_bytes(ext_uart_num, MODULE_ID, sizeof(MODULE_ID)); + uart_write_bytes(ext_uart_num, nl, sizeof(nl)); } ESP_LOGI(EXT_UART_TAG,"ID: %s",MODULE_ID); return; @@ -819,11 +848,11 @@ void processCommand(struct cmdBuf *cmdBuffer) for(uint8_t i = 0; isendToUART != 0) uart_write_bytes(EX_UART_NUM, "PAIRING:",strlen("PAIRING:")); + if(cmdBuffer->sendToUART != 0) uart_write_bytes(ext_uart_num, "PAIRING:",strlen("PAIRING:")); esp_log_buffer_hex(EXT_UART_TAG, btdevlist[i].bd_addr, sizeof(esp_bd_addr_t)); for (int t=0; tsendToUART != 0) uart_write_bytes(EX_UART_NUM, hexnum, 3); + if(cmdBuffer->sendToUART != 0) uart_write_bytes(ext_uart_num, hexnum, 3); } //print out name char btname[64]; @@ -835,19 +864,19 @@ void processCommand(struct cmdBuf *cmdBuffer) if(nvs_get_str(nvs_bt_name_h,key,btname,&name_len) == ESP_OK) { sprintf(hexnum," - "); - if(cmdBuffer->sendToUART != 0) uart_write_bytes(EX_UART_NUM, hexnum, 3); - if(cmdBuffer->sendToUART != 0) uart_write_bytes(EX_UART_NUM, btname, name_len); + if(cmdBuffer->sendToUART != 0) uart_write_bytes(ext_uart_num, hexnum, 3); + if(cmdBuffer->sendToUART != 0) uart_write_bytes(ext_uart_num, btname, name_len); ESP_LOGI(EXT_UART_TAG,"%s",btname); } else ESP_LOGW(EXT_UART_TAG,"cannot find name for addr."); - if(cmdBuffer->sendToUART != 0) uart_write_bytes(EX_UART_NUM,nl,sizeof(nl)); //newline + if(cmdBuffer->sendToUART != 0) uart_write_bytes(ext_uart_num,nl,sizeof(nl)); //newline } ESP_LOGI(EXT_UART_TAG,"---------------------------------------"); } else ESP_LOGW(EXT_UART_TAG,"error getting device list"); } else ESP_LOGE(EXT_UART_TAG,"error allocating memory for device list"); } else { ESP_LOGI(EXT_UART_TAG,"error getting bonded devices count or no devices bonded"); - if(cmdBuffer->sendToUART != 0) uart_write_bytes(EX_UART_NUM, "END\r\n", 5); + if(cmdBuffer->sendToUART != 0) uart_write_bytes(ext_uart_num, "END\r\n", 5); } return; } @@ -930,26 +959,22 @@ void processCommand(struct cmdBuf *cmdBuffer) const esp_partition_t* factory = esp_partition_get(pi); esp_partition_iterator_release(pi); if (esp_ota_set_boot_partition(factory) == ESP_OK) { - uart_write_bytes(EX_UART_NUM, "OTA:start", strlen("OTA:start")); - uart_write_bytes(EX_UART_NUM, nl, sizeof(nl)); - ESP_LOGI(EXT_UART_TAG, "Addon board in upgrade mode"); - //LED off - #if !CONFIG_MODULE_NANO - gpio_set_level(INDICATOR_LED_PIN, 0); - #else - gpio_set_level(INDICATOR_LED_PIN, 1); - #endif + uart_write_bytes(ext_uart_num, "OTA:start", strlen("OTA:start")); + uart_write_bytes(ext_uart_num, nl, sizeof(nl)); + ESP_LOGI(EXT_UART_TAG, "Addon board in upgrade mode"); + //LED off + if(onArduinoRP2040) gpio_set_level(indicator_led, 1); + else gpio_set_level(indicator_led, 0); esp_restart(); - } - else { + }else { ESP_LOGI(EXT_UART_TAG, "Booting factory partition not possible"); - uart_write_bytes(EX_UART_NUM, "OTA:not possible", strlen("OTA:not possible")); - uart_write_bytes(EX_UART_NUM, nl, sizeof(nl)); + uart_write_bytes(ext_uart_num, "OTA:not possible", strlen("OTA:not possible")); + uart_write_bytes(ext_uart_num, nl, sizeof(nl)); } } else { ESP_LOGI(EXT_UART_TAG, "Factory partition not found"); - uart_write_bytes(EX_UART_NUM, "OTA:not possible", strlen("OTA:not possible")); - uart_write_bytes(EX_UART_NUM, nl, sizeof(nl)); + uart_write_bytes(ext_uart_num, "OTA:not possible", strlen("OTA:not possible")); + uart_write_bytes(ext_uart_num, nl, sizeof(nl)); } return; } @@ -1049,72 +1074,71 @@ void uart_external_task(void *pvParameters) { char character; struct cmdBuf cmdBuffer; - #if !CONFIG_MODULE_NANO - int changePinning = 0; - - /* determine if we should switch RX/TX pins. */ - /* we enable the RX pin as GPIO with pull-down. - * if "1" is read, this is the "real" RX pin. If not, it - * should be the TX pin. */ - - gpio_config_t io_conf; - //disable interrupt - io_conf.intr_type = GPIO_INTR_DISABLE; - //set as output mode - io_conf.mode = GPIO_MODE_INPUT; - //bit mask, we want to set the RX pin as input - io_conf.pin_bit_mask = (1ULL<> 20; //now we have 2,4,8,.. + if(size_flash_chip > 2) + { + ESP_LOGW(HID_DEMO_TAG,"Not on Arduino RP2040 connect, using WROOM setup"); + } else onArduinoRP2040 = true; + esp_err_t ret; //if we have this firmware on the Arduino, //we should disable logging on startup. - #if CONFIG_MODULE_NANO - esp_log_level_set("*",ESP_LOG_ERROR); - #endif + if(onArduinoRP2040) esp_log_level_set("*",ESP_LOG_ERROR); + + //set external UART number according to setup + //and setup anything pin related. + #if CONFIG_USE_AS_FLIPMOUSE_FABI + if(onArduinoRP2040) + { + ext_uart_num = UART_NUM_0; + //GPIO26 is blue; 25 is green and 27 red + indicator_led = GPIO_NUM_26; + } else { + ext_uart_num = UART_NUM_2; + ext_uart_rx = GPIO_NUM_17; + ext_uart_tx = GPIO_NUM_16; + indicator_led = GPIO_NUM_5; + } + #else + ext_uart_num = CONFIG_MODULE_UART_NR; + ext_uart_rx = CONFIG_MODULE_RX_PIN; + ext_uart_tx = CONFIG_MODULE_TX_PIN; + indicator_led = CONFIG_MODULE_LED_PIN; + #endif // Initialize FreeRTOS elements eventgroup_system = xEventGroupCreate(); @@ -1435,9 +1493,8 @@ void app_main(void) //start active scan //if(esp_ble_gap_set_scan_params(&scan_params) != ESP_OK) ESP_LOGE("MAIN","Cannot set scan params"); - #if !CONFIG_MODULE_NANO - xTaskCreate(&uart_console_task, "console", 4096, NULL, configMAX_PRIORITIES, NULL); - #endif + //a console for HID debugging (sending simple mouse/kbd commands) is not available on Arduino RP2040 Connect. + if(!onArduinoRP2040) xTaskCreate(&uart_console_task, "console", 4096, NULL, configMAX_PRIORITIES, NULL); xTaskCreate(&uart_external_task, "external", 4096, NULL, configMAX_PRIORITIES, NULL); ///@todo maybe reduce stack size for blink task? 4k words for blinky :-)? xTaskCreate(&blink_task, "blink", 4096, NULL, configMAX_PRIORITIES, NULL); @@ -1452,4 +1509,8 @@ void app_main(void) esp_timer_create(&periodic_timer_args, &periodic_timer); //call every 100ms esp_timer_start_periodic(periodic_timer, 100000); + + //avoid unused variable warnings here: + (void)hidd_adv_resp; + (void)scan_params; } diff --git a/main/config.h b/main/config.h index f24aa08..98f98d4 100644 --- a/main/config.h +++ b/main/config.h @@ -2,31 +2,12 @@ #ifndef _CONFIG_H_ #define _CONFIG_H_ -#define MODULE_ID "ESP32miniBT_v0.3.3" +#define MODULE_ID "ESP32miniBT_v0.3.4" #if CONFIG_USE_AS_FLIPMOUSE_FABI - #define EX_SERIAL_RXPIN GPIO_NUM_17 - #define EX_SERIAL_TXPIN GPIO_NUM_16 - - #if CONFIG_MODULE_FLIPMOUSE - #define GATTS_TAG "FLipMouse" - #define EX_UART_NUM UART_NUM_2 - #define INDICATOR_LED_PIN GPIO_NUM5 - #endif - #if CONFIG_MODULE_FABI - #define GATTS_TAG "FABI" - #define EX_UART_NUM UART_NUM_2 - #define INDICATOR_LED_PIN GPIO_NUM_5 - #endif - - #if CONFIG_MODULE_NANO - //will be overwritten anyway by RP2040 FW - #define GATTS_TAG "esp32_nano" - #define EX_UART_NUM UART_NUM_0 - //GPIO26 is blue; 25 is green and 27 red - #define INDICATOR_LED_PIN GPIO_NUM_26 - #endif - + //this will be overwritten by FABI/FLipMouse/FLipPad firmware to correct + //name, this is the BT device name for the first start + #define GATTS_TAG "AsTeRICS Foundation Module" //non FM/FP/FABI boards #else #define EX_UART_NUM CONFIG_MODULE_UART_NR From d69ea9d85b0242d686734bf0d5a2bf14e0f5c975 Mon Sep 17 00:00:00 2001 From: Benjamin Aigner Date: Thu, 9 Feb 2023 16:39:56 +0100 Subject: [PATCH 7/7] Fixed UART clock error due to idf v5 upgrade; added sdkconfig defaults for detecting flash size --- main/ble_hidd_demo_main.c | 3 ++- sdkconfig.defaults | 10 +++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/main/ble_hidd_demo_main.c b/main/ble_hidd_demo_main.c index eb522af..f35c9ef 100644 --- a/main/ble_hidd_demo_main.c +++ b/main/ble_hidd_demo_main.c @@ -1117,7 +1117,8 @@ void uart_external_task(void *pvParameters) .data_bits = UART_DATA_8_BITS, .parity = UART_PARITY_DISABLE, .stop_bits = UART_STOP_BITS_1, - .flow_ctrl = UART_HW_FLOWCTRL_DISABLE + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, + .source_clk = UART_SCLK_DEFAULT }; if(onArduinoRP2040) uart_config.baud_rate = 115200; diff --git a/sdkconfig.defaults b/sdkconfig.defaults index 5aef1ab..25e5fc8 100644 --- a/sdkconfig.defaults +++ b/sdkconfig.defaults @@ -6,14 +6,14 @@ CONFIG_BTDM_CTRL_MODE_BR_EDR_ONLY=n CONFIG_BTDM_CTRL_MODE_BTDM=n # for testing with ESP32Think (Sparkfun), which has a different XTAl -CONFIG_ESP32_XTAL_FREQ_AUTO=y -CONFIG_ESP32_XTAL_FREQ=0 +CONFIG_XTAL_FREQ_AUTO=y +CONFIG_XTAL_FREQ=0 # ESP32-WROOM uses 4MB #CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y #CONFIG_ESPTOOLPY_FLASHSIZE="4MB" # Arduino Nano connect Nina Module has 2MB -CONFIG_ESPTOOLPY_FLASHSIZE_2MB=y -CONFIG_ESPTOOLPY_FLASHSIZE="2MB" +#CONFIG_ESPTOOLPY_FLASHSIZE_2MB=y +#CONFIG_ESPTOOLPY_FLASHSIZE="2MB" -CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y +CONFIG_ESPTOOLPY_HEADER_FLASHSIZE_UPDATE=y