From 4d913f3542b5cfd8a7bd53a8c0ab97650b63751b Mon Sep 17 00:00:00 2001 From: fbuirey Date: Tue, 16 Dec 2025 10:49:15 +0100 Subject: [PATCH] =?UTF-8?q?Ajout=20du=20support=20d'un=20bouton=20customis?= =?UTF-8?q?=C3=A9=20pour=20Pico=20RP2350?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + CMakeLists.txt | 124 ++++++++++++++++++++++++++++++------------------- mbedtls | 2 +- src/main.c | 120 ++++++++++++++++++++++++++++++++++++++++------- 4 files changed, 179 insertions(+), 68 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c795b05 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 85def50..069b723 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,66 +17,92 @@ cmake_minimum_required(VERSION 3.16) -if(ESP_PLATFORM) -set(EXTRA_COMPONENT_DIRS src) -include($ENV{IDF_PATH}/tools/cmake/project.cmake) -set(USB_ITF_CCID 1) -#set(USB_ITF_HID 1) -include(pico_keys_sdk_import.cmake) -project(pico_keys_sdk) + if(ESP_PLATFORM) + set(EXTRA_COMPONENT_DIRS src) + include($ENV{IDF_PATH}/tools/cmake/project.cmake) else() + if(NOT ENABLE_EMULATION) + set(PICO_USE_FASTEST_SUPPORTED_CLOCK 1) + include(pico_sdk_import.cmake) + endif() - if(ENABLE_EMULATION) - else() - include(pico_sdk_import.cmake) - endif() + project(picokey C CXX ASM) - project(pico_keys C CXX ASM) + set(CMAKE_C_STANDARD 11) + set(CMAKE_CXX_STANDARD 17) - set(CMAKE_C_STANDARD 11) - set(CMAKE_CXX_STANDARD 17) + if(NOT DEFINED __FOR_CI) + set(__FOR_CI 0) + endif() + if(__FOR_CI) + add_definitions(-D__FOR_CI) + endif() - if(ENABLE_EMULATION) - else() - pico_sdk_init() - endif() - - if (NOT DEFINED __FOR_CI) - set(__FOR_CI 0) - endif() - if (__FOR_CI) - add_definitions(-D__FOR_CI) - endif() + add_executable(picokey) +endif() set(USB_ITF_CCID 1) -set(USB_ITF_HID 1) +set(USB_ITF_WCID 1) +include(cmake/version.cmake) include(pico_keys_sdk_import.cmake) - -add_executable(pico_keys_sdk_exe) - -target_compile_options(pico_keys_sdk_exe PUBLIC - -Wall - -Werror +if(NOT ESP_PLATFORM) + set(SOURCES ${PICO_KEYS_SOURCES}) +endif() +set(SOURCES ${SOURCES} + ${CMAKE_CURRENT_LIST_DIR}/src/fs/files.c + ${CMAKE_CURRENT_LIST_DIR}/src/version.c ) -if(ENABLE_EMULATION) -target_compile_options(pico_keys_sdk_exe PUBLIC - -fdata-sections - -ffunction-sections - ) -if(APPLE) -target_link_options(pico_keys_sdk_exe PUBLIC - -Wl,-dead_strip - ) -else() -target_link_options(pico_keys_sdk_exe PUBLIC - -Wl,--gc-sections - ) -endif (APPLE) -else() -pico_add_extra_outputs(pico_keys_sdk_exe) +SET_VERSION(ver_major ver_minor "${CMAKE_CURRENT_LIST_DIR}/src/pico_keys_version.h" 2) -target_link_libraries(pico_keys_sdk_exe PRIVATE pico_keys_sdk pico_stdlib pico_multicore hardware_flash hardware_sync hardware_adc pico_unique_id hardware_rtc tinyusb_device tinyusb_board) +if(ESP_PLATFORM) + project(picokey) endif() +if(NOT ESP_PLATFORM) + target_sources(picokey PUBLIC ${SOURCES}) + target_include_directories(picokey PUBLIC ${INCLUDES}) + + target_compile_options(picokey PUBLIC + -Wall + ) + + if(CUST_BUTTON_PIN) + target_compile_definitions(picokey PUBLIC CUST_BUTTON_PIN=${CUST_BUTTON_PIN}) + endif() + message(STATUS "CUST_BUTTON_PIN is set to: ${CUST_BUTTON_PIN}") + + if(NOT MSVC) + target_compile_options(picokey PUBLIC + -Werror + ) + endif() + + if(ENABLE_EMULATION) + if(NOT MSVC) + target_compile_options(picokey PUBLIC + -fdata-sections + -ffunction-sections + ) + endif() + if(APPLE) + target_link_options(picokey PUBLIC + -Wl,-dead_strip + ) + elseif(MSVC) + target_compile_options(picokey PUBLIC + -WX + ) + + target_link_libraries(picokey PUBLIC wsock32 ws2_32 Bcrypt) + else() + target_link_options(picokey PUBLIC + -Wl,--gc-sections + ) + endif(APPLE) + target_link_libraries(picokey PRIVATE pthread m) + else() + pico_add_extra_outputs(${CMAKE_PROJECT_NAME}) + endif() endif() + diff --git a/mbedtls b/mbedtls index 107ea89..e185d7f 160000 --- a/mbedtls +++ b/mbedtls @@ -1 +1 @@ -Subproject commit 107ea89daaefb9867ea9121002fbbdf926780e98 +Subproject commit e185d7fd85499c8ce5ca2a54f5cf8fe7dbe3f8df diff --git a/src/main.c b/src/main.c index 74bfcf8..9d8570d 100644 --- a/src/main.c +++ b/src/main.c @@ -15,6 +15,12 @@ * along with this program. If not, see . */ +#define STRINGIFY_(x) #x +#define STRINGIFY(x) STRINGIFY_(x) + + +//#define CUST_BUTTON_PIN 2 + #include #include #include "pico_keys.h" @@ -146,12 +152,19 @@ int gettimeofday(struct timeval* tp, struct timezone* tzp) #endif #if !defined(ENABLE_EMULATION) #ifdef ESP_PLATFORM +#ifdef CUST_BUTTON_PIN +bool picok_board_button_read() { + int boot_state = gpio_get_level(CUST_BUTTON_PIN); + return boot_state == 0; +} +#else bool picok_board_button_read() { int boot_state = gpio_get_level(BOOT_PIN); return boot_state == 0; } +#endif #elif defined(PICO_PLATFORM) -bool __no_inline_not_in_flash_func(picok_get_bootsel_button)() { +bool __no_inline_not_in_flash_func(picok_get_bootsel_button)() { const uint CS_PIN_INDEX = 1; // Must disable interrupts, as interrupt handlers may be in flash, and we @@ -185,14 +198,49 @@ bool __no_inline_not_in_flash_func(picok_get_bootsel_button)() { return button_state; } + +#ifdef CUST_BUTTON_PIN +#pragma message("FBU DEBUG: Using custom button pin function") +#pragma message("FBU DEBUG: CUST_BUTTON_PIN=" STRINGIFY(CUST_BUTTON_PIN)) + +bool picok_board_button_read(void) { + // Variables statiques pour mémoriser l'état précédent et le timing + static bool last_stable_state = false; // dernier état stable validé + static bool last_raw_state = false; // dernier état lu du GPIO + static uint32_t stable_since = 0; // timestamp du dernier changement + + // Lecture brute du GPIO (pull-up active, bouton appuyé = 0) + bool cur = !gpio_get(CUST_BUTTON_PIN); + + uint32_t now = board_millis(); + + // Détecte un changement brut + if (cur != last_raw_state) { + stable_since = now; // réinitialise le timer anti-rebond + last_raw_state = cur; + } + + // Si l'état est stable depuis au moins 50 ms, on le valide + if (now - stable_since >= 50) { + last_stable_state = cur; + } + + // Retourne l'état stable (prêt pour core0_loop) + return last_stable_state; +} + +#else +#pragma message("FBU DEBUG: Using BOOTSEL button function") bool picok_board_button_read(void) { return picok_get_bootsel_button(); } +#endif #else bool picok_board_button_read(void) { return true; // always unpressed } #endif + bool button_pressed_state = false; uint32_t button_pressed_time = 0; uint8_t button_press = 0; @@ -255,37 +303,53 @@ void execute_tasks() led_blinking_task(); } +//bool button_pressed_state = false; +uint32_t button_press_start = 0; +bool long_press_reported = false; + void core0_loop() { while (1) { execute_tasks(); neug_task(); do_flash(); + #ifndef ENABLE_EMULATION - if (button_pressed_cb && board_millis() > 1000 && !is_busy()) { // wait 1 second to boot up - bool current_button_state = picok_board_button_read(); - if (current_button_state != button_pressed_state) { - if (current_button_state == false) { // unpressed - if (button_pressed_time == 0 || button_pressed_time + 1000 > board_millis()) { - button_press++; - } - button_pressed_time = board_millis(); - } - button_pressed_state = current_button_state; + if (button_pressed_cb && board_millis() > 1000 && !is_busy()) { + bool cur = picok_board_button_read(); + + // front descendant = bouton pressé + if (cur && !button_pressed_state) { + button_pressed_state = true; + button_press_start = board_millis(); + long_press_reported = false; } - if (button_pressed_time > 0 && button_press > 0 && button_pressed_time + 1000 < board_millis() && button_pressed_state == false) { - if (button_pressed_cb != NULL) { - (*button_pressed_cb)(button_press); + + // front montant = bouton relâché + if (!cur && button_pressed_state) { + button_pressed_state = false; + uint32_t duration = board_millis() - button_press_start; + + if (!long_press_reported) { + if (duration < 800) { + button_pressed_cb(1); // short press + } else { + button_pressed_cb(2); // long press + } } - button_pressed_time = button_press = 0; + } + + // long press en cours + if (button_pressed_state && !long_press_reported && + (board_millis() - button_press_start >= 800)) { + long_press_reported = true; + button_pressed_cb(2); // long press } } -#endif -#ifdef ESP_PLATFORM - vTaskDelay(pdMS_TO_TICKS(10)); #endif } } + char pico_serial_str[2 * PICO_UNIQUE_BOARD_ID_SIZE_BYTES + 1]; uint8_t pico_serial_hash[32]; pico_unique_board_id_t pico_serial; @@ -301,6 +365,7 @@ int app_main() { #endif int main(void) { #endif +#pragma message("FBU DEBUG: main start") pico_get_unique_board_id(&pico_serial); memset(pico_serial_str, 0, sizeof(pico_serial_str)); for (size_t i = 0; i < sizeof(pico_serial); i++) { @@ -361,11 +426,30 @@ int main(void) { #endif #endif +#ifdef CUST_BUTTON_PIN +#pragma message("FBU DEBUG: Custom button pin defined") + /* platform-specific GPIO setup */ +#if defined(ESP_PLATFORM) +#pragma message("FBU DEBUG: ESP Platform - init custom button pin") + gpio_pad_select_gpio(CUST_BUTTON_PIN); + gpio_set_direction(CUST_BUTTON_PIN, GPIO_MODE_INPUT); + gpio_pulldown_dis(CUST_BUTTON_PIN); +#elif defined(PICO_PLATFORM) +#pragma message("FBU DEBUG: PICO Platform - init custom button pin") + gpio_init(CUST_BUTTON_PIN); + gpio_set_dir(CUST_BUTTON_PIN, GPIO_IN); + gpio_pull_up(CUST_BUTTON_PIN); +#endif +#endif + + #ifdef ESP_PLATFORM xTaskCreatePinnedToCore(core0_loop, "core0", 4096*ITF_TOTAL*2, NULL, CONFIG_TINYUSB_TASK_PRIORITY - 1, &hcore0, ESP32_CORE0); #else core0_loop(); #endif + + return 0; }