From 2e372fc078afaee704230c31e2b19683904a47a7 Mon Sep 17 00:00:00 2001 From: Jan200101 Date: Wed, 7 Aug 2024 20:10:22 +0200 Subject: enable roll switch, tweak parameters, support BLE, use LED --- src/CMakeLists.txt | 1 + src/btstack_config.h | 17 +++++++++-------- src/gamepad.c | 34 ++++++++++++++++++++++++---------- src/main.c | 1 - 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c9797b6..f79aa13 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,6 +22,7 @@ target_link_libraries(wirecutter PUBLIC pico_cyw43_arch_none pico_btstack_cyw43 pico_btstack_classic + pico_btstack_ble tinyusb_host tinyusb_board xinput_host diff --git a/src/btstack_config.h b/src/btstack_config.h index e38468b..8f6b5bf 100644 --- a/src/btstack_config.h +++ b/src/btstack_config.h @@ -3,8 +3,9 @@ // BTstack features that can be enabled #ifdef ENABLE_BLE -#warning no BLE -#undef ENABLE_BLE +#define ENABLE_LE_PERIPHERAL +#define ENABLE_LE_CENTRAL +#define ENABLE_L2CAP_LE_CREDIT_BASED_FLOW_CONTROL_MODE #endif #define ENABLE_LOG_DEBUG #define ENABLE_LOG_INFO @@ -15,22 +16,22 @@ #define ENABLE_LOG_BTSTACK_EVENTS // BTstack configuration. buffers, sizes, ... -#define HCI_OUTGOING_PRE_BUFFER_SIZE 16 -#define HCI_ACL_PAYLOAD_SIZE (1691 + 4) +#define HCI_OUTGOING_PRE_BUFFER_SIZE 4 +#define HCI_ACL_PAYLOAD_SIZE (158 + 4) #define HCI_ACL_CHUNK_SIZE_ALIGNMENT 4 #define MAX_NR_AVDTP_CONNECTIONS 1 #define MAX_NR_AVDTP_STREAM_ENDPOINTS 1 #define MAX_NR_AVRCP_CONNECTIONS 2 #define MAX_NR_BNEP_CHANNELS 1 #define MAX_NR_BNEP_SERVICES 1 -#define MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES 2 +#define MAX_NR_BTSTACK_LINK_KEY_DB_MEMORY_ENTRIES 3 #define MAX_NR_GATT_CLIENTS 1 -#define MAX_NR_HCI_CONNECTIONS 1 +#define MAX_NR_HCI_CONNECTIONS 3 #define MAX_NR_HID_HOST_CONNECTIONS 1 #define MAX_NR_HIDS_CLIENTS 1 #define MAX_NR_HFP_CONNECTIONS 1 -#define MAX_NR_L2CAP_CHANNELS 4 -#define MAX_NR_L2CAP_SERVICES 3 +#define MAX_NR_L2CAP_CHANNELS 4 +#define MAX_NR_L2CAP_SERVICES 3 #define MAX_NR_RFCOMM_CHANNELS 1 #define MAX_NR_RFCOMM_MULTIPLEXERS 1 #define MAX_NR_RFCOMM_SERVICES 1 diff --git a/src/gamepad.c b/src/gamepad.c index 5e4a426..3e8c018 100644 --- a/src/gamepad.c +++ b/src/gamepad.c @@ -3,6 +3,7 @@ #include #include #include +#include "pico/cyw43_arch.h" #include "btstack.h" #include "gamepad.h" #include "hardware/gpio.h" @@ -18,9 +19,6 @@ static uint8_t device_id_sdp_service_record_handle = 0; static btstack_packet_callback_registration_t hci_event_callback_registration; uint16_t hid_cid; -const uint16_t host_max_latency = 1600; -const uint16_t host_min_timeout = 3200; - const uint8_t hid_descriptor_gamepad[] = { 0x05, 0x01, // USAGE_PAGE (Generic Desktop) 0x09, 0x05, // USAGE (Joystick - 0x04; Gamepad - 0x05; Multi-axis Controller - 0x08) @@ -154,15 +152,20 @@ static void packet_handler(uint8_t packet_type, uint16_t channel, uint8_t * pack printf("HID Connected\n"); hid_device_request_can_send_now_event(hid_cid); gap_discoverable_control(0); + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); break; case HID_SUBEVENT_CONNECTION_CLOSED: printf("HID Disconnected\n"); hid_cid = 0; break; + case HID_SUBEVENT_GET_PROTOCOL_RESPONSE: + break; case HID_SUBEVENT_CAN_SEND_NOW: if(hid_cid!=0){ //Solves crash when disconnecting gamepad on android - send_report_joystick(); - hid_device_request_can_send_now_event(hid_cid); + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); + send_report_joystick(); + hid_device_request_can_send_now_event(hid_cid); + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); } break; default: @@ -183,9 +186,11 @@ static void sync_irq_handler(void) if (gpio_get_irq_event_mask(BUTTON_PIN) & GPIO_IRQ_EDGE_FALL) { gpio_acknowledge_irq(BUTTON_PIN, GPIO_IRQ_EDGE_FALL); + hid_device_disconnect(hid_cid); gap_discoverable_control(1); printf("Set discoverable\n"); + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); } } @@ -224,15 +229,24 @@ int btstack_main(int argc, const char * argv[]){ (void)argc; (void)argv; + cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 0); gap_discoverable_control(0); gap_set_class_of_device(0x2508); gap_set_local_name(hid_device_name); + gap_set_default_link_policy_settings(LM_LINK_POLICY_ENABLE_ROLE_SWITCH | LM_LINK_POLICY_ENABLE_SNIFF_MODE); + gap_set_allow_role_switch(true); #ifdef ENABLE_BLE gap_set_connection_parameters(0x0060, 0x0030, 0x06, 0x06, 0, 0x0048, 2, 0x0030); #endif // L2CAP l2cap_init(); + +#ifdef ENABLE_BLE + // Initialize LE Security Manager. Needed for cross-transport key derivation + sm_init(); +#endif + // SDP Server sdp_init(); memset(hid_service_buffer, 0, sizeof(hid_service_buffer)); @@ -244,13 +258,13 @@ int btstack_main(int argc, const char * argv[]){ hid_sdp_record_t hid_params = { 0x2508, - 0, - 0, + 33, + 1, // virtual cable 1, // remote wake - 0, - 1, // normally connectable + 1, // reconnect initially + 0, // normally connectable 0, // boot device - host_max_latency, host_min_timeout, + 0xFFFF, 0xFFFF, 3200, hid_descriptor_gamepad, sizeof(hid_descriptor_gamepad), diff --git a/src/main.c b/src/main.c index be8e598..a8b0473 100644 --- a/src/main.c +++ b/src/main.c @@ -15,7 +15,6 @@ int main() printf("failed to initialise cyw43_arch\n"); return -1; } - cyw43_arch_gpio_put(CYW43_WL_GPIO_LED_PIN, 1); board_init(); tuh_init(BOARD_TUH_RHPORT); -- cgit v1.2.3