From 4c4543cab0ffdf6078027cc31dcf71d0d60750b0 Mon Sep 17 00:00:00 2001 From: 1Conan Date: Sun, 23 Oct 2022 21:05:04 +0800 Subject: [PATCH 01/19] iton bt driver --- builddefs/common_features.mk | 6 +- drivers/bluetooth/iton_bt.c | 189 +++++++++++++++++++++++++++ drivers/bluetooth/iton_bt.h | 102 +++++++++++++++ drivers/bluetooth/outputselect.c | 4 + tmk_core/protocol/chibios/chibios.c | 8 ++ tmk_core/protocol/chibios/usb_main.c | 44 +++++++ 6 files changed, 352 insertions(+), 1 deletion(-) create mode 100644 drivers/bluetooth/iton_bt.c create mode 100644 drivers/bluetooth/iton_bt.h diff --git a/builddefs/common_features.mk b/builddefs/common_features.mk index b7333cee938c..37203c500bd7 100644 --- a/builddefs/common_features.mk +++ b/builddefs/common_features.mk @@ -885,7 +885,7 @@ ifeq ($(strip $(USBPD_ENABLE)), yes) endif BLUETOOTH_ENABLE ?= no -VALID_BLUETOOTH_DRIVER_TYPES := bluefruit_le custom rn42 +VALID_BLUETOOTH_DRIVER_TYPES := bluefruit_le custom rn42 iton_bt ifeq ($(strip $(BLUETOOTH_ENABLE)), yes) ifeq ($(filter $(strip $(BLUETOOTH_DRIVER)),$(VALID_BLUETOOTH_DRIVER_TYPES)),) $(call CATASTROPHIC_ERROR,Invalid BLUETOOTH_DRIVER,BLUETOOTH_DRIVER="$(BLUETOOTH_DRIVER)" is not a valid Bluetooth driver type) @@ -908,6 +908,10 @@ ifeq ($(strip $(BLUETOOTH_ENABLE)), yes) SRC += $(DRIVER_PATH)/bluetooth/bluetooth.c SRC += $(DRIVER_PATH)/bluetooth/rn42.c endif + + ifeq ($(strip $(BLUETOOTH_DRIVER)), iton_bt) + SRC += $(DRIVER_PATH)/bluetooth/iton_bt.c + endif endif ENCODER_ENABLE ?= no diff --git a/drivers/bluetooth/iton_bt.c b/drivers/bluetooth/iton_bt.c new file mode 100644 index 000000000000..b5b99be7b805 --- /dev/null +++ b/drivers/bluetooth/iton_bt.c @@ -0,0 +1,189 @@ +// Copyright 2022 1Conan (@1Conan) +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include +#include "config.h" +#include "gpio.h" +#include "iton_bt.h" +#include "report.h" +#include "keycode_config.h" + +#ifndef ITON_BT_SPI_PORT +#define ITON_BT_SPI_PORT SPID1 +#endif + +#ifndef ITON_BT_IRQ_LINE +#define ITON_BT_IRQ_LINE A0 +#endif + +#ifndef ITON_BT_INT_LINE +#define ITON_BT_INT_LINE A1 +#endif + +#ifndef ITON_BT_MAX_PROFILE +#define ITON_BT_MAX_PROFILE 3 +#endif + +#ifndef ITON_BT_TX_LEN +#define ITON_BT_TX_LEN 16 +#endif + +#ifndef ITON_BT_RX_LEN +#define ITON_BT_RX_LEN 3 +#endif + +#ifdef KEYBOARD_SHARED_EP +#define HID_REPORT_OFFSET 1 +#else +#define HID_REPORT_OFFSET 0 +#endif + +/** + * Driver Macros + */ +#define HIGH_BITS(x) ((uint8_t)(x >> 8)) +#define LOW_BITS(x) ((uint8_t)(x & 0x00FF)) +#define WAIT_FOR_FINISH() while(readPin(ITON_BT_IRQ_LINE)) + +/** + * Function definitions + */ +void iton_bt_data_cb(SPIDriver *spip); +static void iton_bt_rx_end(void *arg); + +/** + * Driver variables + */ +uint8_t iton_bt_rx[ITON_BT_RX_LEN]; +uint8_t iton_bt_tx[ITON_BT_TX_LEN]; +uint8_t iton_bt_led_state = 0x00; +uint8_t iton_bt_send_kb_last_key = 0x00; +#if defined(PAL_USE_CALLBACKS) || defined(PAL_USE_WAIT) +static THD_WORKING_AREA(iton_bt_rx_thd_wa, 128); +thread_t *iton_bt_rx_thd; +#endif + +const SPIConfig iton_bt_spicfg = { + .slave = true, + .data_cb = iton_bt_data_cb, + // mcu specific + .ctrl0 = SPI_CTRL0_DL(8), +}; + +/** + * Callbacks + */ +#if defined(PAL_USE_CALLBACKS) || defined(PAL_USE_WAIT) +static THD_FUNCTION(iton_bt_rx_thd_func, arg) { + iton_bt_rx_thd = chThdGetSelfX(); + while (true) { + chEvtWaitAny((eventmask_t)1); + + if (iton_bt_rx[0] == led_state) { + iton_bt_led_state = iton_bt_rx[1]; + } + } +} + +static void iton_bt_rx_end(void *arg) { + if (readPin(ITON_BT_INT_LINE)) { + chSysLockFromISR(); + spiStartReceiveI(&ITON_BT_SPI_PORT, ITON_BT_RX_LEN, &iton_bt_rx[0]); + chSysUnlockFromISR(); + } else { + chSysLockFromISR(); + spiStopTransferI(&ITON_BT_SPI_PORT, NULL); + chEvtSignalI(iton_bt_rx_thd, (eventmask_t)1); + chSysUnlockFromISR(); + } +} +#endif + +void iton_bt_data_cb(SPIDriver *spip) { + writePinLow(ITON_BT_IRQ_LINE); +} + +/** + * Driver Functions + */ +void iton_bt_init(void) { + setPinOutput(ITON_BT_IRQ_LINE); + setPinInput(ITON_BT_INT_LINE); +#if defined(PAL_USE_CALLBACKS) || defined(PAL_USE_WAIT) + palSetLineCallback(ITON_BT_INT_LINE, iton_bt_rx_end, NULL); +#endif +} + +void iton_bt_start(void) { + spiStart(&ITON_BT_SPI_PORT, &iton_bt_spicfg); +#if defined(PAL_USE_CALLBACKS) || defined(PAL_USE_WAIT) + palEnableLineEvent(ITON_BT_INT_LINE, PAL_EVENT_MODE_BOTH_EDGES); + chThdCreateStatic( + iton_bt_rx_thd_wa, + sizeof(iton_bt_rx_thd_wa), + HIGHPRIO, + iton_bt_rx_thd_func, + NULL + ); +#endif +} + +void iton_bt_stop(void) { + spiStopTransfer(&ITON_BT_SPI_PORT, NULL); +#if defined(PAL_USE_CALLBACKS) || defined(PAL_USE_WAIT) + palDisableLineEvent(ITON_BT_INT_LINE); +#endif + spiStop(&ITON_BT_SPI_PORT); +} + +void iton_bt_send(uint8_t cmd, uint8_t *data, uint8_t len) { + WAIT_FOR_FINISH(); + writePinHigh(ITON_BT_IRQ_LINE); + iton_bt_tx[0] = cmd; + memcpy(&iton_bt_tx[1], data, len); + spiStartSend(&ITON_BT_SPI_PORT, len + 1, &iton_bt_tx[0]); +} + +void iton_bt_send2(uint8_t cmd, uint8_t b1, uint8_t b2) { + WAIT_FOR_FINISH(); + writePinHigh(ITON_BT_IRQ_LINE); + iton_bt_tx[0] = cmd; + iton_bt_tx[1] = b1; + iton_bt_tx[2] = b2; + spiStartSend(&ITON_BT_SPI_PORT, 3, &iton_bt_tx[0]); +} + +void iton_bt_report_hid(uint8_t *raw) { + iton_bt_send(report_hid, raw, 8); +} + +void iton_bt_report_nkro(uint8_t *raw) { + iton_bt_send(report_nkro, raw, 15); +} + +void iton_bt_report_media(uint16_t data) { + iton_bt_send2(report_media, HIGH_BITS(data), LOW_BITS(data)); +} + +void iton_bt_report_system(uint16_t data) { + iton_bt_send(report_system, (uint8_t *)&data, 1); +} + +void iton_bt_send_keyboard(report_keyboard_t *report) { + +#ifndef ITON_BT_NO_6KRO_HACK + // iton module only reads 5 of the keys in the hid report + // so we send the last key as an nkro report. + if (report->keys[5] != iton_bt_send_kb_last_key) { + uint8_t nkro_report[16] = {0}; + nkro_report[report->keys[5] >> 3] |= (1 << (report->keys[5] & 7)); + iton_bt_send_kb_last_key = report->keys[5]; + + return iton_bt_report_nkro(&nkro_report[0]); + } +#endif + + iton_bt_report_hid(&report->raw[HID_REPORT_OFFSET]); +} diff --git a/drivers/bluetooth/iton_bt.h b/drivers/bluetooth/iton_bt.h new file mode 100644 index 000000000000..79b8377e403a --- /dev/null +++ b/drivers/bluetooth/iton_bt.h @@ -0,0 +1,102 @@ +// Copyright 2022 1Conan (@1Conan) +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "report.h" + +#pragma once + +/** + * Driver Structures + */ +enum iton_bt_cmd { + // mcu to iton + report_hid = 0xA1, + report_nkro = 0xA2, + report_media = 0xA3, + report_system = 0xA4, + report_fn = 0xA5, + control = 0xA6, + set_name = 0xA7, + set_alarm_volt = 0xA8, + + // iton to mcu + led_state = 0xB1, + notification = 0xB6, +}; + +enum iton_bt_control_cmd { + control_usb = 0x58, + control_bt = 0x51, + control_pins = 0x52, +}; + +enum iton_bt_control_param { + // control_usb + mode_usb = 0x01, + + // control_bt + mode_bt = 0x62, + reset_pairing = 0x70, + enter_pairing = 0x89, + switch_profile = 0x81, // + 0-5 profiles + os_mac = 0x74, + os_win = 0x75, + connect_ack = 0x50, + disconnect_ack = 0x51, +}; + +enum iton_bt_notification_type { + notif_battery = 0x5A, + notif_bluetooth = 0x51, +}; + +enum iton_bt_notification_param { + // notif_battery + batt_voltage_low =0x06, + batt_exit_low_battery_mode = 0x0A, + batt_low_power_shutdown = 0x07, + + query_working_mode = 0xA0, + query_bt_name = 0xA1, + + // notif_bluetooth + bt_connection_success = 0x76, + bt_entered_pairing = 0x77, + bt_disconected = 0x78, + bt_enters_connection = 0x79, +}; + +/** + * Exported Variables + */ +uint8_t iton_bt_led_state; + +/** + * Driver Functions + */ +void iton_bt_init(void); +void iton_bt_start(void); +void iton_bt_stop(void); +void iton_bt_send(uint8_t cmd, uint8_t *data, uint8_t len); +void iton_bt_send2(uint8_t cmd, uint8_t b1, uint8_t b2); +void iton_bt_report_hid(uint8_t *raw); +void iton_bt_report_nkro(uint8_t *raw); +void iton_bt_report_media(uint16_t data); +void iton_bt_report_system(uint16_t data); +void iton_bt_send_keyboard(report_keyboard_t *report); + +/** + * Driver Macros + */ + +#define iton_bt_control(cmd, param) iton_bt_send2(control, cmd, param) +#define iton_bt_control_bt(param) iton_bt_control(control_bt, param) +#define iton_bt_control_usb(param) iton_bt_control(control_bt, param) + +#define iton_bt_mode_usb() iton_bt_control_usb(mode_usb) +#define iton_bt_mode_bt() iton_bt_control_bt(mode_bt) +#define iton_bt_reset_pairing() iton_bt_control_bt(reset_pairing) +#define iton_bt_enter_pairing() iton_bt_control_bt(enter_pairing) +#define iton_bt_switch_profile(x) iton_bt_control_bt(switch_profile + x) +#define iton_bt_os_win() iton_bt_control_bt(os_win) +#define iton_bt_os_mac() iton_bt_control_bt(os_mac) diff --git a/drivers/bluetooth/outputselect.c b/drivers/bluetooth/outputselect.c index b986ba274e9d..0656799dd0bc 100644 --- a/drivers/bluetooth/outputselect.c +++ b/drivers/bluetooth/outputselect.c @@ -19,6 +19,10 @@ along with this program. If not, see . # include "bluefruit_le.h" #endif +#ifdef BLUETOOTH_ITON_BT +# include "iton_bt.h" +#endif + uint8_t desired_output = OUTPUT_DEFAULT; /** \brief Set Output diff --git a/tmk_core/protocol/chibios/chibios.c b/tmk_core/protocol/chibios/chibios.c index a249af8d38cc..8992fd3ab260 100644 --- a/tmk_core/protocol/chibios/chibios.c +++ b/tmk_core/protocol/chibios/chibios.c @@ -34,6 +34,10 @@ #include "debug.h" #include "print.h" +#ifdef BLUETOOTH_ITON_BT +# include "iton_bt.h" +#endif + #ifndef EARLY_INIT_PERFORM_BOOTLOADER_JUMP // Change this to be TRUE once we've migrated keyboards to the new init system // Remember to change docs/platformdev_chibios_earlyinit.md as well. @@ -142,6 +146,10 @@ void protocol_pre_init(void) { usb_event_queue_init(); init_usb_driver(&USB_DRIVER); +#ifdef BLUETOOTH_ITON_BT + iton_bt_init(); +#endif + #ifdef MIDI_ENABLE setup_midi(); #endif diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index 4027fb438d35..790c63034ba4 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -38,6 +38,13 @@ extern keymap_config_t keymap_config; #endif +#ifdef BLUETOOTH_ENABLE +# include "outputselect.h" +# ifdef BLUETOOTH_ITON_BT +# include "iton_bt.h" +# endif +#endif + /* --------------------------------------------------------- * Global interface variables and declarations * --------------------------------------------------------- @@ -398,6 +405,13 @@ __attribute__((weak)) void restart_usb_driver(USBDriver *usbp) { /* LED status */ uint8_t keyboard_leds(void) { +#ifdef BLUETOOTH_ENABLE + if (where_to_send() == OUTPUT_BLUETOOTH) { +# ifdef BLUETOOTH_ITON_BT + return iton_bt_led_state; +# endif + } +#endif return keyboard_led_state; } @@ -493,6 +507,36 @@ void send_extra(report_extra_t *report) { #endif } +void send_system(uint16_t data) { +#ifdef BLUETOOTH_ENABLE + if (where_to_send() == OUTPUT_BLUETOOTH) { +# ifdef BLUETOOTH_ITON_BT + iton_bt_report_system(data); +# endif + return; + } +#endif + +#ifdef EXTRAKEY_ENABLE + send_extra(REPORT_ID_SYSTEM, data); +#endif +} + +void send_consumer(uint16_t data) { +#ifdef BLUETOOTH_ENABLE + if (where_to_send() == OUTPUT_BLUETOOTH) { +# ifdef BLUETOOTH_ITON_BT + iton_bt_report_media(data); +# endif + return; + } +#endif + +#ifdef EXTRAKEY_ENABLE + send_extra(REPORT_ID_CONSUMER, data); +#endif +} + void send_programmable_button(report_programmable_button_t *report) { #ifdef PROGRAMMABLE_BUTTON_ENABLE send_report(USB_ENDPOINT_IN_SHARED, report, sizeof(report_programmable_button_t)); From e994285e3c3b8bb6b1a43969dc04a5e7bd69b3c4 Mon Sep 17 00:00:00 2001 From: 1Conan Date: Sun, 23 Oct 2022 22:17:30 +0800 Subject: [PATCH 02/19] fix indentation --- drivers/bluetooth/iton_bt.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/drivers/bluetooth/iton_bt.h b/drivers/bluetooth/iton_bt.h index 79b8377e403a..c1258fa86f1c 100644 --- a/drivers/bluetooth/iton_bt.h +++ b/drivers/bluetooth/iton_bt.h @@ -52,18 +52,18 @@ enum iton_bt_notification_type { enum iton_bt_notification_param { // notif_battery - batt_voltage_low =0x06, - batt_exit_low_battery_mode = 0x0A, + batt_voltage_low = 0x06, + batt_exit_low_battery_mode = 0x0A, batt_low_power_shutdown = 0x07, - query_working_mode = 0xA0, - query_bt_name = 0xA1, + query_working_mode = 0xA0, + query_bt_name = 0xA1, // notif_bluetooth - bt_connection_success = 0x76, - bt_entered_pairing = 0x77, - bt_disconected = 0x78, - bt_enters_connection = 0x79, + bt_connection_success = 0x76, + bt_entered_pairing = 0x77, + bt_disconected = 0x78, + bt_enters_connection = 0x79, }; /** From c38691a1a35b35cf55f20afbec946861930bb244 Mon Sep 17 00:00:00 2001 From: 1Conan Date: Sun, 23 Oct 2022 22:32:35 +0800 Subject: [PATCH 03/19] remove old code --- tmk_core/protocol/chibios/usb_main.c | 30 ---------------------------- 1 file changed, 30 deletions(-) diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index 790c63034ba4..343497eca534 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -507,36 +507,6 @@ void send_extra(report_extra_t *report) { #endif } -void send_system(uint16_t data) { -#ifdef BLUETOOTH_ENABLE - if (where_to_send() == OUTPUT_BLUETOOTH) { -# ifdef BLUETOOTH_ITON_BT - iton_bt_report_system(data); -# endif - return; - } -#endif - -#ifdef EXTRAKEY_ENABLE - send_extra(REPORT_ID_SYSTEM, data); -#endif -} - -void send_consumer(uint16_t data) { -#ifdef BLUETOOTH_ENABLE - if (where_to_send() == OUTPUT_BLUETOOTH) { -# ifdef BLUETOOTH_ITON_BT - iton_bt_report_media(data); -# endif - return; - } -#endif - -#ifdef EXTRAKEY_ENABLE - send_extra(REPORT_ID_CONSUMER, data); -#endif -} - void send_programmable_button(report_programmable_button_t *report) { #ifdef PROGRAMMABLE_BUTTON_ENABLE send_report(USB_ENDPOINT_IN_SHARED, report, sizeof(report_programmable_button_t)); From 2a159fd994fac015654c3db7f910d451583db123 Mon Sep 17 00:00:00 2001 From: 1Conan Date: Sun, 23 Oct 2022 22:53:31 +0800 Subject: [PATCH 04/19] rename bt driver and fix build --- data/schemas/keyboard.jsonschema | 2 +- drivers/bluetooth/iton_bt.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/data/schemas/keyboard.jsonschema b/data/schemas/keyboard.jsonschema index acbf06599df7..340a4459c494 100644 --- a/data/schemas/keyboard.jsonschema +++ b/data/schemas/keyboard.jsonschema @@ -190,7 +190,7 @@ "properties": { "driver": { "type": "string", - "enum": ["bluefruit_le", "custom", "rn42"] + "enum": ["bluefruit_le", "custom", "rn42", "iton_bt"] } } }, diff --git a/drivers/bluetooth/iton_bt.c b/drivers/bluetooth/iton_bt.c index b5b99be7b805..b18f51727ff2 100644 --- a/drivers/bluetooth/iton_bt.c +++ b/drivers/bluetooth/iton_bt.c @@ -69,7 +69,7 @@ const SPIConfig iton_bt_spicfg = { .slave = true, .data_cb = iton_bt_data_cb, // mcu specific - .ctrl0 = SPI_CTRL0_DL(8), + .ctrl0 = (8 << 8), // Data Length = 8 }; /** From b675618e0cff7c5ff5bca5498cb6a853234a7b1f Mon Sep 17 00:00:00 2001 From: 1Conan Date: Sun, 29 Jan 2023 22:46:04 +0800 Subject: [PATCH 05/19] update to fixed driver --- drivers/bluetooth/iton_bt.c | 231 +++++++++++++++++++------------ drivers/bluetooth/iton_bt.h | 69 +++++++-- drivers/bluetooth/outputselect.c | 6 + 3 files changed, 209 insertions(+), 97 deletions(-) diff --git a/drivers/bluetooth/iton_bt.c b/drivers/bluetooth/iton_bt.c index b18f51727ff2..eaacfa136e6b 100644 --- a/drivers/bluetooth/iton_bt.c +++ b/drivers/bluetooth/iton_bt.c @@ -1,43 +1,32 @@ -// Copyright 2022 1Conan (@1Conan) +// Copyright 2023 1Conan (@1Conan) // SPDX-License-Identifier: GPL-2.0-or-later #include -#include #include -#include "config.h" #include "gpio.h" +#include "config.h" #include "iton_bt.h" -#include "report.h" -#include "keycode_config.h" #ifndef ITON_BT_SPI_PORT -#define ITON_BT_SPI_PORT SPID1 +# define ITON_BT_SPI_PORT SPID0 #endif #ifndef ITON_BT_IRQ_LINE -#define ITON_BT_IRQ_LINE A0 +# define ITON_BT_IRQ_LINE A0 #endif #ifndef ITON_BT_INT_LINE -#define ITON_BT_INT_LINE A1 -#endif - -#ifndef ITON_BT_MAX_PROFILE -#define ITON_BT_MAX_PROFILE 3 -#endif - -#ifndef ITON_BT_TX_LEN -#define ITON_BT_TX_LEN 16 -#endif - -#ifndef ITON_BT_RX_LEN -#define ITON_BT_RX_LEN 3 +# define ITON_BT_INT_LINE A1 #endif #ifdef KEYBOARD_SHARED_EP -#define HID_REPORT_OFFSET 1 +# define HID_REPORT_OFFSET 1 #else -#define HID_REPORT_OFFSET 0 +# define HID_REPORT_OFFSET 0 +#endif + +#ifndef ITON_BT_BUFFER_LEN +# define ITON_BT_BUFFER_LEN 16 #endif /** @@ -45,62 +34,142 @@ */ #define HIGH_BITS(x) ((uint8_t)(x >> 8)) #define LOW_BITS(x) ((uint8_t)(x & 0x00FF)) -#define WAIT_FOR_FINISH() while(readPin(ITON_BT_IRQ_LINE)) /** * Function definitions */ void iton_bt_data_cb(SPIDriver *spip); -static void iton_bt_rx_end(void *arg); + +/** + * Callbacks + */ +__attribute__((weak)) void iton_bt_battery_voltage_low(void) {} +__attribute__((weak)) void iton_bt_battery_exit_low_battery_mode(void) {} +__attribute__((weak)) void iton_bt_battery_low_power_shutdown(void) {} +__attribute__((weak)) void iton_bt_battery_level(uint8_t level) {} + +__attribute__((weak)) void iton_bt_connection_successful(void) {} +__attribute__((weak)) void iton_bt_entered_pairing(void) {} +__attribute__((weak)) void iton_bt_disconnected(void) {} +__attribute__((weak)) void iton_bt_enters_connection_state(void) {} /** * Driver variables */ -uint8_t iton_bt_rx[ITON_BT_RX_LEN]; -uint8_t iton_bt_tx[ITON_BT_TX_LEN]; +bool iton_bt_is_connected = false; uint8_t iton_bt_led_state = 0x00; + +static uint8_t iton_bt_buffer[ITON_BT_BUFFER_LEN]; uint8_t iton_bt_send_kb_last_key = 0x00; -#if defined(PAL_USE_CALLBACKS) || defined(PAL_USE_WAIT) -static THD_WORKING_AREA(iton_bt_rx_thd_wa, 128); -thread_t *iton_bt_rx_thd; -#endif const SPIConfig iton_bt_spicfg = { .slave = true, .data_cb = iton_bt_data_cb, - // mcu specific - .ctrl0 = (8 << 8), // Data Length = 8 + // SN32 specific + .ctrl0 = SPI_DATA_LENGTH(8), }; /** * Callbacks */ #if defined(PAL_USE_CALLBACKS) || defined(PAL_USE_WAIT) -static THD_FUNCTION(iton_bt_rx_thd_func, arg) { - iton_bt_rx_thd = chThdGetSelfX(); - while (true) { - chEvtWaitAny((eventmask_t)1); +static inline void iton_bt_rx_battery_notif(uint8_t data) { + switch (data) { + case batt_voltage_low: + iton_bt_battery_voltage_low(); + break; + case batt_exit_low_battery_mode: + iton_bt_battery_exit_low_battery_mode(); + break; + case batt_low_power_shutdown: + iton_bt_battery_low_power_shutdown(); + break; + case batt_above_70: + case batt_between_30_70: + case batt_below_30: + iton_bt_battery_level(data); + break; + case batt_wake_mcu: + #ifdef ITON_BT_ENABLE_ACK + iton_bt_send_ack(control_bt, wake_ack); + #endif + break; + case batt_unknown: + #ifdef ITON_BT_ENABLE_ACK + iton_bt_send_ack(control_bt, unknown_ack); + #endif + break; + case query_working_mode: + break; + case query_bt_name: + break; + } +} - if (iton_bt_rx[0] == led_state) { - iton_bt_led_state = iton_bt_rx[1]; - } +static inline void iton_bt_rx_bluetooth_notif(uint8_t data) { + switch (iton_bt_buffer[2]) { + case bt_connection_success: + iton_bt_is_connected = true; + + #ifdef ITON_BT_ENABLE_ACK + iton_bt_send_ack(control_bt, connect_ack); + #endif + + iton_bt_connection_successful(); + break; + case bt_entered_pairing: + iton_bt_entered_pairing(); + break; + case bt_disconected: + iton_bt_is_connected = false; + + #ifdef ITON_BT_ENABLE_ACK + iton_bt_send_ack(control_bt, disconnect_ack); + #endif + + iton_bt_disconnected(); + break; + case bt_enters_connection: + iton_bt_enters_connection_state(); + break; } } -static void iton_bt_rx_end(void *arg) { +static void iton_bt_rx_cb(void *arg) { if (readPin(ITON_BT_INT_LINE)) { chSysLockFromISR(); - spiStartReceiveI(&ITON_BT_SPI_PORT, ITON_BT_RX_LEN, &iton_bt_rx[0]); + spiStartReceiveI(&ITON_BT_SPI_PORT, ITON_BT_BUFFER_LEN, &iton_bt_buffer[0]); chSysUnlockFromISR(); } else { chSysLockFromISR(); spiStopTransferI(&ITON_BT_SPI_PORT, NULL); - chEvtSignalI(iton_bt_rx_thd, (eventmask_t)1); chSysUnlockFromISR(); + + #ifdef ITON_BT_ENABLE_ACK + // hack to make sure irq is low since acks messes with stuff + writePinLow(ITON_BT_IRQ_LINE); + #endif + + switch (iton_bt_buffer[0]) { + case led_state: + iton_bt_led_state = iton_bt_buffer[1]; + break; + case notification: + switch (iton_bt_buffer[1]) { + case notif_battery: + iton_bt_rx_battery_notif(iton_bt_buffer[2]); + break; + case notif_bluetooth: + iton_bt_rx_bluetooth_notif(iton_bt_buffer[2]); + break; + } + break; + } } } #endif + void iton_bt_data_cb(SPIDriver *spip) { writePinLow(ITON_BT_IRQ_LINE); } @@ -111,79 +180,71 @@ void iton_bt_data_cb(SPIDriver *spip) { void iton_bt_init(void) { setPinOutput(ITON_BT_IRQ_LINE); setPinInput(ITON_BT_INT_LINE); -#if defined(PAL_USE_CALLBACKS) || defined(PAL_USE_WAIT) - palSetLineCallback(ITON_BT_INT_LINE, iton_bt_rx_end, NULL); -#endif -} -void iton_bt_start(void) { - spiStart(&ITON_BT_SPI_PORT, &iton_bt_spicfg); + writePinLow(ITON_BT_IRQ_LINE); + #if defined(PAL_USE_CALLBACKS) || defined(PAL_USE_WAIT) + palSetLineCallback(ITON_BT_INT_LINE, iton_bt_rx_cb, NULL); palEnableLineEvent(ITON_BT_INT_LINE, PAL_EVENT_MODE_BOTH_EDGES); - chThdCreateStatic( - iton_bt_rx_thd_wa, - sizeof(iton_bt_rx_thd_wa), - HIGHPRIO, - iton_bt_rx_thd_func, - NULL - ); #endif -} -void iton_bt_stop(void) { - spiStopTransfer(&ITON_BT_SPI_PORT, NULL); -#if defined(PAL_USE_CALLBACKS) || defined(PAL_USE_WAIT) - palDisableLineEvent(ITON_BT_INT_LINE); -#endif - spiStop(&ITON_BT_SPI_PORT); + spiStart(&ITON_BT_SPI_PORT, &iton_bt_spicfg); } void iton_bt_send(uint8_t cmd, uint8_t *data, uint8_t len) { - WAIT_FOR_FINISH(); + while (readPin(ITON_BT_IRQ_LINE)); + writePinHigh(ITON_BT_IRQ_LINE); - iton_bt_tx[0] = cmd; - memcpy(&iton_bt_tx[1], data, len); - spiStartSend(&ITON_BT_SPI_PORT, len + 1, &iton_bt_tx[0]); + iton_bt_buffer[0] = cmd; + memcpy(&iton_bt_buffer[1], data, len); + spiStartSend(&ITON_BT_SPI_PORT, len + 1, &iton_bt_buffer[0]); } void iton_bt_send2(uint8_t cmd, uint8_t b1, uint8_t b2) { - WAIT_FOR_FINISH(); + while (readPin(ITON_BT_IRQ_LINE)); + writePinHigh(ITON_BT_IRQ_LINE); - iton_bt_tx[0] = cmd; - iton_bt_tx[1] = b1; - iton_bt_tx[2] = b2; - spiStartSend(&ITON_BT_SPI_PORT, 3, &iton_bt_tx[0]); -} + iton_bt_buffer[0] = cmd; + iton_bt_buffer[1] = b1; + iton_bt_buffer[2] = b2; -void iton_bt_report_hid(uint8_t *raw) { - iton_bt_send(report_hid, raw, 8); + spiStartSend(&ITON_BT_SPI_PORT, 3, &iton_bt_buffer[0]); } -void iton_bt_report_nkro(uint8_t *raw) { - iton_bt_send(report_nkro, raw, 15); +inline void iton_bt_send_ack(uint8_t b1, uint8_t b2) { + writePinHigh(ITON_BT_IRQ_LINE); + iton_bt_buffer[0] = control; + iton_bt_buffer[1] = b1; + iton_bt_buffer[2] = b2; + chSysLockFromISR(); + spiStartSendI(&ITON_BT_SPI_PORT, 3, &iton_bt_buffer[0]); + chSysUnlockFromISR(); } -void iton_bt_report_media(uint16_t data) { - iton_bt_send2(report_media, HIGH_BITS(data), LOW_BITS(data)); +void iton_bt_send_fn(bool pressed) { + uint8_t data = pressed ? 0xA3 : 0x00; + + iton_bt_send(report_fn, &data, 1); } -void iton_bt_report_system(uint16_t data) { +void iton_bt_send_system(uint16_t data) { iton_bt_send(report_system, (uint8_t *)&data, 1); } -void iton_bt_send_keyboard(report_keyboard_t *report) { +void iton_bt_send_consumer(uint16_t data) { + iton_bt_send2(report_consumer, HIGH_BITS(data), LOW_BITS(data)); +} -#ifndef ITON_BT_NO_6KRO_HACK +void iton_bt_send_keyboard(report_keyboard_t *report) { // iton module only reads 5 of the keys in the hid report // so we send the last key as an nkro report. if (report->keys[5] != iton_bt_send_kb_last_key) { - uint8_t nkro_report[16] = {0}; + uint8_t nkro_report[15] = {0}; nkro_report[report->keys[5] >> 3] |= (1 << (report->keys[5] & 7)); iton_bt_send_kb_last_key = report->keys[5]; - return iton_bt_report_nkro(&nkro_report[0]); + return iton_bt_send(report_nkro, &nkro_report[0], 15); } -#endif - iton_bt_report_hid(&report->raw[HID_REPORT_OFFSET]); + iton_bt_send(report_hid, &report->raw[HID_REPORT_OFFSET], 8); } diff --git a/drivers/bluetooth/iton_bt.h b/drivers/bluetooth/iton_bt.h index c1258fa86f1c..cdbf48cb3e95 100644 --- a/drivers/bluetooth/iton_bt.h +++ b/drivers/bluetooth/iton_bt.h @@ -1,4 +1,4 @@ -// Copyright 2022 1Conan (@1Conan) +// Copyright 2023 1Conan (@1Conan) // SPDX-License-Identifier: GPL-2.0-or-later #include "report.h" @@ -12,7 +12,7 @@ enum iton_bt_cmd { // mcu to iton report_hid = 0xA1, report_nkro = 0xA2, - report_media = 0xA3, + report_consumer = 0xA3, report_system = 0xA4, report_fn = 0xA5, control = 0xA6, @@ -25,12 +25,18 @@ enum iton_bt_cmd { }; enum iton_bt_control_cmd { + control_power = 0x25, control_usb = 0x58, control_bt = 0x51, control_pins = 0x52, }; enum iton_bt_control_param { + // control_power + sleep_idle_10m = 0x01, + sleep_idle_20m = 0x02, + sleep_idle_30m = 0x03, + // control_usb mode_usb = 0x01, @@ -38,11 +44,20 @@ enum iton_bt_control_param { mode_bt = 0x62, reset_pairing = 0x70, enter_pairing = 0x89, - switch_profile = 0x81, // + 0-5 profiles + switch_profile = 0x81, // + 0-5 profiles os_mac = 0x74, os_win = 0x75, + connect_ack = 0x50, disconnect_ack = 0x51, + wake_ack = 0x60, + unknown_ack = 0x52, + + query_voltage = 0x66, + query_battery_level = 0x61, + + disable_sleep = 0x65, + enable_sleep = 0x68, }; enum iton_bt_notification_type { @@ -56,9 +71,18 @@ enum iton_bt_notification_param { batt_exit_low_battery_mode = 0x0A, batt_low_power_shutdown = 0x07, + batt_above_70 = 0x04, + batt_between_30_70 = 0x02, + batt_below_30 = 0x01, + query_working_mode = 0xA0, query_bt_name = 0xA1, + // Wake from batt_low_power_shutdown + batt_wake_mcu = 0x0B, + + batt_unknown = 0x08, + // notif_bluetooth bt_connection_success = 0x76, bt_entered_pairing = 0x77, @@ -70,33 +94,54 @@ enum iton_bt_notification_param { * Exported Variables */ uint8_t iton_bt_led_state; +bool iton_bt_is_connected; + +/** + * Driver Callbacks + */ + +/** + * Driver Callbacks + */ +void iton_bt_battery_voltage_low(void); +void iton_bt_battery_exit_low_battery_mode(void); +void iton_bt_battery_low_power_shutdown(void); +void iton_bt_battery_level(uint8_t level); + +void iton_bt_connection_successful(void); +void iton_bt_entered_pairing(void); +void iton_bt_disconnected(void); +void iton_bt_enters_connection_state(void); /** * Driver Functions */ void iton_bt_init(void); -void iton_bt_start(void); -void iton_bt_stop(void); void iton_bt_send(uint8_t cmd, uint8_t *data, uint8_t len); void iton_bt_send2(uint8_t cmd, uint8_t b1, uint8_t b2); -void iton_bt_report_hid(uint8_t *raw); -void iton_bt_report_nkro(uint8_t *raw); -void iton_bt_report_media(uint16_t data); -void iton_bt_report_system(uint16_t data); +void iton_bt_send_ack(uint8_t b1, uint8_t b2); + +void iton_bt_send_fn(bool pressed); +void iton_bt_send_system(uint16_t data); +void iton_bt_send_consumer(uint16_t data); void iton_bt_send_keyboard(report_keyboard_t *report); /** * Driver Macros */ - #define iton_bt_control(cmd, param) iton_bt_send2(control, cmd, param) #define iton_bt_control_bt(param) iton_bt_control(control_bt, param) -#define iton_bt_control_usb(param) iton_bt_control(control_bt, param) +#define iton_bt_control_usb(param) iton_bt_control(control_usb, param) #define iton_bt_mode_usb() iton_bt_control_usb(mode_usb) #define iton_bt_mode_bt() iton_bt_control_bt(mode_bt) #define iton_bt_reset_pairing() iton_bt_control_bt(reset_pairing) #define iton_bt_enter_pairing() iton_bt_control_bt(enter_pairing) #define iton_bt_switch_profile(x) iton_bt_control_bt(switch_profile + x) -#define iton_bt_os_win() iton_bt_control_bt(os_win) #define iton_bt_os_mac() iton_bt_control_bt(os_mac) +#define iton_bt_os_win() iton_bt_control_bt(os_win) +#define iton_bt_query_voltage() iton_bt_control_bt(query_voltage) + +#define iton_bt_query_battery_level() iton_bt_control_bt(query_battery_level) +#define iton_bt_disable_sleep() iton_bt_control_bt(disable_sleep) +#define iton_bt_enable_sleep() iton_bt_control_bt(enable_sleep) diff --git a/drivers/bluetooth/outputselect.c b/drivers/bluetooth/outputselect.c index 0656799dd0bc..bb88f9765a42 100644 --- a/drivers/bluetooth/outputselect.c +++ b/drivers/bluetooth/outputselect.c @@ -55,6 +55,12 @@ uint8_t auto_detect_output(void) { } #endif +#ifdef BLUETOOTH_ITON_BT + if (iton_bt_is_connected) { + return OUTPUT_BLUETOOTH; + } +#endif + #ifdef BLUETOOTH_ENABLE return OUTPUT_BLUETOOTH; // should check if BT is connected here #endif From b7232353a24234a294eba8e5839f85095a124b36 Mon Sep 17 00:00:00 2001 From: 1Conan Date: Sun, 29 Jan 2023 22:46:19 +0800 Subject: [PATCH 06/19] add system and led state support to bluetooth api --- drivers/bluetooth/bluetooth.c | 14 ++++++++++++++ drivers/bluetooth/bluetooth.h | 12 ++++++++++++ tmk_core/protocol/host.c | 14 ++++++++++++++ 3 files changed, 40 insertions(+) diff --git a/drivers/bluetooth/bluetooth.c b/drivers/bluetooth/bluetooth.c index d5382401e7e5..85144983addb 100644 --- a/drivers/bluetooth/bluetooth.c +++ b/drivers/bluetooth/bluetooth.c @@ -60,3 +60,17 @@ void bluetooth_send_consumer(uint16_t usage) { rn42_send_consumer(usage); #endif } + +void bluetooth_send_system(uint16_t usage) { +#if defined(BLUETOOTH_ITON_BT) + iton_bt_send_system(usage); +#endif +} + +uint8_t bluetooth_led_state(void) { +#if defined(BLUETOOTH_ITON_BT) + return iton_bt_led_state; +#else + return 0; +#endif +} diff --git a/drivers/bluetooth/bluetooth.h b/drivers/bluetooth/bluetooth.h index 2e4d0df5381a..fd34c2bb11fb 100644 --- a/drivers/bluetooth/bluetooth.h +++ b/drivers/bluetooth/bluetooth.h @@ -50,3 +50,15 @@ void bluetooth_send_mouse(report_mouse_t *report); * \param usage The consumer usage to send. */ void bluetooth_send_consumer(uint16_t usage); + +/** + * \brief Send a system usage. + * + * \param usage The system usage to send. + */ +void bluetooth_send_system(uint16_t usage); + +/** + * \brief Get the current state of the status LEDs. + */ +uint8_t bluetooth_led_state(void); diff --git a/tmk_core/protocol/host.c b/tmk_core/protocol/host.c index 732fbdc37d4d..f93a44778bd8 100644 --- a/tmk_core/protocol/host.c +++ b/tmk_core/protocol/host.c @@ -63,6 +63,13 @@ uint8_t host_keyboard_leds(void) { #ifdef SPLIT_KEYBOARD if (!is_keyboard_master()) return split_led_state; #endif + +#ifdef BLUETOOTH_ENABLE + if (where_to_send() == OUTPUT_BLUETOOTH) { + return bluetooth_led_state(); + } +#endif + if (!driver) return 0; return (*driver->keyboard_leds)(); } @@ -133,6 +140,13 @@ void host_system_send(uint16_t usage) { if (usage == last_system_usage) return; last_system_usage = usage; +#ifdef BLUETOOTH_ENABLE + if (where_to_send() == OUTPUT_BLUETOOTH) { + bluetooth_send_system(usage); + return; + } +#endif + if (!driver) return; report_extra_t report = { From cae7453f04e0c2a8f7c7999ea373712ab5a12d72 Mon Sep 17 00:00:00 2001 From: 1Conan Date: Sun, 29 Jan 2023 22:58:09 +0800 Subject: [PATCH 07/19] fix some rebase stuff --- tmk_core/protocol/chibios/chibios.c | 8 -------- tmk_core/protocol/chibios/usb_main.c | 14 -------------- 2 files changed, 22 deletions(-) diff --git a/tmk_core/protocol/chibios/chibios.c b/tmk_core/protocol/chibios/chibios.c index 8992fd3ab260..a249af8d38cc 100644 --- a/tmk_core/protocol/chibios/chibios.c +++ b/tmk_core/protocol/chibios/chibios.c @@ -34,10 +34,6 @@ #include "debug.h" #include "print.h" -#ifdef BLUETOOTH_ITON_BT -# include "iton_bt.h" -#endif - #ifndef EARLY_INIT_PERFORM_BOOTLOADER_JUMP // Change this to be TRUE once we've migrated keyboards to the new init system // Remember to change docs/platformdev_chibios_earlyinit.md as well. @@ -146,10 +142,6 @@ void protocol_pre_init(void) { usb_event_queue_init(); init_usb_driver(&USB_DRIVER); -#ifdef BLUETOOTH_ITON_BT - iton_bt_init(); -#endif - #ifdef MIDI_ENABLE setup_midi(); #endif diff --git a/tmk_core/protocol/chibios/usb_main.c b/tmk_core/protocol/chibios/usb_main.c index 343497eca534..4027fb438d35 100644 --- a/tmk_core/protocol/chibios/usb_main.c +++ b/tmk_core/protocol/chibios/usb_main.c @@ -38,13 +38,6 @@ extern keymap_config_t keymap_config; #endif -#ifdef BLUETOOTH_ENABLE -# include "outputselect.h" -# ifdef BLUETOOTH_ITON_BT -# include "iton_bt.h" -# endif -#endif - /* --------------------------------------------------------- * Global interface variables and declarations * --------------------------------------------------------- @@ -405,13 +398,6 @@ __attribute__((weak)) void restart_usb_driver(USBDriver *usbp) { /* LED status */ uint8_t keyboard_leds(void) { -#ifdef BLUETOOTH_ENABLE - if (where_to_send() == OUTPUT_BLUETOOTH) { -# ifdef BLUETOOTH_ITON_BT - return iton_bt_led_state; -# endif - } -#endif return keyboard_led_state; } From 1d350f44ea4c1a43b83c72475ccddd1fa5a88395 Mon Sep 17 00:00:00 2001 From: 1Conan Date: Mon, 22 Jul 2024 16:42:28 +0800 Subject: [PATCH 08/19] add ctrl0 check --- drivers/bluetooth/iton_bt.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/bluetooth/iton_bt.c b/drivers/bluetooth/iton_bt.c index eaacfa136e6b..d4236f42986b 100644 --- a/drivers/bluetooth/iton_bt.c +++ b/drivers/bluetooth/iton_bt.c @@ -65,8 +65,9 @@ uint8_t iton_bt_send_kb_last_key = 0x00; const SPIConfig iton_bt_spicfg = { .slave = true, .data_cb = iton_bt_data_cb, - // SN32 specific +#if defined(SN32) .ctrl0 = SPI_DATA_LENGTH(8), +#endif }; /** From 81eac0644c9b399daa3a1599daa14404845d0216 Mon Sep 17 00:00:00 2001 From: 1Conan Date: Mon, 22 Jul 2024 16:45:04 +0800 Subject: [PATCH 09/19] fix report --- drivers/bluetooth/iton_bt.c | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/drivers/bluetooth/iton_bt.c b/drivers/bluetooth/iton_bt.c index d4236f42986b..3a6fcbb930a0 100644 --- a/drivers/bluetooth/iton_bt.c +++ b/drivers/bluetooth/iton_bt.c @@ -19,12 +19,6 @@ # define ITON_BT_INT_LINE A1 #endif -#ifdef KEYBOARD_SHARED_EP -# define HID_REPORT_OFFSET 1 -#else -# define HID_REPORT_OFFSET 0 -#endif - #ifndef ITON_BT_BUFFER_LEN # define ITON_BT_BUFFER_LEN 16 #endif @@ -247,5 +241,5 @@ void iton_bt_send_keyboard(report_keyboard_t *report) { return iton_bt_send(report_nkro, &nkro_report[0], 15); } - iton_bt_send(report_hid, &report->raw[HID_REPORT_OFFSET], 8); + iton_bt_send(report_hid, &report->mods, 8); } From 62d9856f42894d5be82fcf4eeb30b9dd05498fad Mon Sep 17 00:00:00 2001 From: 1Conan Date: Mon, 22 Jul 2024 16:49:26 +0800 Subject: [PATCH 10/19] remove led_state/send_system --- drivers/bluetooth/bluetooth.c | 14 -------------- tmk_core/protocol/host.c | 13 ------------- 2 files changed, 27 deletions(-) diff --git a/drivers/bluetooth/bluetooth.c b/drivers/bluetooth/bluetooth.c index 85144983addb..d5382401e7e5 100644 --- a/drivers/bluetooth/bluetooth.c +++ b/drivers/bluetooth/bluetooth.c @@ -60,17 +60,3 @@ void bluetooth_send_consumer(uint16_t usage) { rn42_send_consumer(usage); #endif } - -void bluetooth_send_system(uint16_t usage) { -#if defined(BLUETOOTH_ITON_BT) - iton_bt_send_system(usage); -#endif -} - -uint8_t bluetooth_led_state(void) { -#if defined(BLUETOOTH_ITON_BT) - return iton_bt_led_state; -#else - return 0; -#endif -} diff --git a/tmk_core/protocol/host.c b/tmk_core/protocol/host.c index f93a44778bd8..05101b3c42bf 100644 --- a/tmk_core/protocol/host.c +++ b/tmk_core/protocol/host.c @@ -64,12 +64,6 @@ uint8_t host_keyboard_leds(void) { if (!is_keyboard_master()) return split_led_state; #endif -#ifdef BLUETOOTH_ENABLE - if (where_to_send() == OUTPUT_BLUETOOTH) { - return bluetooth_led_state(); - } -#endif - if (!driver) return 0; return (*driver->keyboard_leds)(); } @@ -140,13 +134,6 @@ void host_system_send(uint16_t usage) { if (usage == last_system_usage) return; last_system_usage = usage; -#ifdef BLUETOOTH_ENABLE - if (where_to_send() == OUTPUT_BLUETOOTH) { - bluetooth_send_system(usage); - return; - } -#endif - if (!driver) return; report_extra_t report = { From cd624b66c8695b761a50614d287e22a3fb586b1a Mon Sep 17 00:00:00 2001 From: 1Conan Date: Mon, 22 Jul 2024 16:52:18 +0800 Subject: [PATCH 11/19] whitespace --- tmk_core/protocol/host.c | 1 - 1 file changed, 1 deletion(-) diff --git a/tmk_core/protocol/host.c b/tmk_core/protocol/host.c index 05101b3c42bf..732fbdc37d4d 100644 --- a/tmk_core/protocol/host.c +++ b/tmk_core/protocol/host.c @@ -63,7 +63,6 @@ uint8_t host_keyboard_leds(void) { #ifdef SPLIT_KEYBOARD if (!is_keyboard_master()) return split_led_state; #endif - if (!driver) return 0; return (*driver->keyboard_leds)(); } From b1696df5be79ed4ca54bf501fcba77019398f4c0 Mon Sep 17 00:00:00 2001 From: 1Conan Date: Mon, 22 Jul 2024 16:52:57 +0800 Subject: [PATCH 12/19] remove from header --- drivers/bluetooth/bluetooth.h | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/drivers/bluetooth/bluetooth.h b/drivers/bluetooth/bluetooth.h index fd34c2bb11fb..2e4d0df5381a 100644 --- a/drivers/bluetooth/bluetooth.h +++ b/drivers/bluetooth/bluetooth.h @@ -50,15 +50,3 @@ void bluetooth_send_mouse(report_mouse_t *report); * \param usage The consumer usage to send. */ void bluetooth_send_consumer(uint16_t usage); - -/** - * \brief Send a system usage. - * - * \param usage The system usage to send. - */ -void bluetooth_send_system(uint16_t usage); - -/** - * \brief Get the current state of the status LEDs. - */ -uint8_t bluetooth_led_state(void); From b773ec3cbb958a07c608cc61b427a1bbd6c1a7af Mon Sep 17 00:00:00 2001 From: 1Conan Date: Mon, 22 Jul 2024 17:00:22 +0800 Subject: [PATCH 13/19] call send functions --- drivers/bluetooth/bluetooth.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/drivers/bluetooth/bluetooth.c b/drivers/bluetooth/bluetooth.c index d5382401e7e5..1cf4d84bb22d 100644 --- a/drivers/bluetooth/bluetooth.c +++ b/drivers/bluetooth/bluetooth.c @@ -21,6 +21,8 @@ # include "bluefruit_le.h" #elif defined(BLUETOOTH_RN42) # include "rn42.h" +#elif defined(BLUETOOTH_ITON_BT) +# include "iton_bt.h" #endif void bluetooth_init(void) { @@ -28,6 +30,8 @@ void bluetooth_init(void) { bluefruit_le_init(); #elif defined(BLUETOOTH_RN42) rn42_init(); +#elif defined(BLUETOOTH_ITON_BT) + iton_bt_init(); #endif } @@ -42,6 +46,8 @@ void bluetooth_send_keyboard(report_keyboard_t *report) { bluefruit_le_send_keyboard(report); #elif defined(BLUETOOTH_RN42) rn42_send_keyboard(report); +#elif defined(BLUETOOTH_ITON_BT) + iton_bt_send_keyboard(report); #endif } @@ -58,5 +64,7 @@ void bluetooth_send_consumer(uint16_t usage) { bluefruit_le_send_consumer(usage); #elif defined(BLUETOOTH_RN42) rn42_send_consumer(usage); +#elif defined(BLUETOOTH_ITON_BT) + iton_bt_send_consumer(usage) #endif } From 31fe27669efa764cec48adb91b276bc66299ef6d Mon Sep 17 00:00:00 2001 From: 1Conan Date: Tue, 23 Jul 2024 13:50:44 +0800 Subject: [PATCH 14/19] add missing bluetooth.c --- builddefs/common_features.mk | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/builddefs/common_features.mk b/builddefs/common_features.mk index 37203c500bd7..419bd9129073 100644 --- a/builddefs/common_features.mk +++ b/builddefs/common_features.mk @@ -909,7 +909,9 @@ ifeq ($(strip $(BLUETOOTH_ENABLE)), yes) SRC += $(DRIVER_PATH)/bluetooth/rn42.c endif - ifeq ($(strip $(BLUETOOTH_DRIVER)), iton_bt) + ifeq ($(strip $(BLUETOOTH_DRIVER)), iton_bt) + SPI_DRIVER_REQUIRED = yes + SRC += $(DRIVER_PATH)/bluetooth/bluetooth.c SRC += $(DRIVER_PATH)/bluetooth/iton_bt.c endif endif From 11ea5250f1e538228f9ad2dfa2ed3af120a33c8f Mon Sep 17 00:00:00 2001 From: 1Conan Date: Tue, 23 Jul 2024 14:09:40 +0800 Subject: [PATCH 15/19] we don't actually need spi_master.c --- builddefs/common_features.mk | 1 - 1 file changed, 1 deletion(-) diff --git a/builddefs/common_features.mk b/builddefs/common_features.mk index 419bd9129073..4f85ac49f8c7 100644 --- a/builddefs/common_features.mk +++ b/builddefs/common_features.mk @@ -910,7 +910,6 @@ ifeq ($(strip $(BLUETOOTH_ENABLE)), yes) endif ifeq ($(strip $(BLUETOOTH_DRIVER)), iton_bt) - SPI_DRIVER_REQUIRED = yes SRC += $(DRIVER_PATH)/bluetooth/bluetooth.c SRC += $(DRIVER_PATH)/bluetooth/iton_bt.c endif From 3846da7a1539880ae6bf34c77144b6f00825dd0c Mon Sep 17 00:00:00 2001 From: 1Conan Date: Wed, 24 Jul 2024 04:33:40 +0800 Subject: [PATCH 16/19] semis --- drivers/bluetooth/bluetooth.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/bluetooth/bluetooth.c b/drivers/bluetooth/bluetooth.c index 1cf4d84bb22d..abb20d144881 100644 --- a/drivers/bluetooth/bluetooth.c +++ b/drivers/bluetooth/bluetooth.c @@ -65,6 +65,6 @@ void bluetooth_send_consumer(uint16_t usage) { #elif defined(BLUETOOTH_RN42) rn42_send_consumer(usage); #elif defined(BLUETOOTH_ITON_BT) - iton_bt_send_consumer(usage) + iton_bt_send_consumer(usage); #endif } From d0c3c07f04da485425664e8768ad686ece6c3133 Mon Sep 17 00:00:00 2001 From: 1Conan Date: Thu, 5 Sep 2024 03:55:50 +0800 Subject: [PATCH 17/19] MCU_SN32 --- drivers/bluetooth/iton_bt.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/bluetooth/iton_bt.c b/drivers/bluetooth/iton_bt.c index 3a6fcbb930a0..43158155f3f4 100644 --- a/drivers/bluetooth/iton_bt.c +++ b/drivers/bluetooth/iton_bt.c @@ -59,7 +59,7 @@ uint8_t iton_bt_send_kb_last_key = 0x00; const SPIConfig iton_bt_spicfg = { .slave = true, .data_cb = iton_bt_data_cb, -#if defined(SN32) +#if defined(MCU_SN32) .ctrl0 = SPI_DATA_LENGTH(8), #endif }; From e27f38f8dfd7025caa5e077b89ccca48610f1587 Mon Sep 17 00:00:00 2001 From: 1Conan Date: Thu, 5 Sep 2024 03:59:50 +0800 Subject: [PATCH 18/19] copyright formatting --- drivers/bluetooth/iton_bt.c | 2 +- drivers/bluetooth/iton_bt.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/bluetooth/iton_bt.c b/drivers/bluetooth/iton_bt.c index 43158155f3f4..c5cc5b3911d3 100644 --- a/drivers/bluetooth/iton_bt.c +++ b/drivers/bluetooth/iton_bt.c @@ -1,4 +1,4 @@ -// Copyright 2023 1Conan (@1Conan) +// Copyright 2024 1Conan (@1Conan) // SPDX-License-Identifier: GPL-2.0-or-later #include diff --git a/drivers/bluetooth/iton_bt.h b/drivers/bluetooth/iton_bt.h index cdbf48cb3e95..df40965b708c 100644 --- a/drivers/bluetooth/iton_bt.h +++ b/drivers/bluetooth/iton_bt.h @@ -1,4 +1,4 @@ -// Copyright 2023 1Conan (@1Conan) +// Copyright 2024 1Conan (@1Conan) // SPDX-License-Identifier: GPL-2.0-or-later #include "report.h" From 117639d026b405c3f2196d7a34f16537fa27dfc5 Mon Sep 17 00:00:00 2001 From: 1Conan Date: Thu, 5 Sep 2024 13:16:09 +0800 Subject: [PATCH 19/19] l linter --- drivers/bluetooth/iton_bt.c | 39 ++++++------- drivers/bluetooth/iton_bt.h | 106 ++++++++++++++++++------------------ 2 files changed, 73 insertions(+), 72 deletions(-) diff --git a/drivers/bluetooth/iton_bt.c b/drivers/bluetooth/iton_bt.c index c5cc5b3911d3..e127ba9ece71 100644 --- a/drivers/bluetooth/iton_bt.c +++ b/drivers/bluetooth/iton_bt.c @@ -26,8 +26,8 @@ /** * Driver Macros */ -#define HIGH_BITS(x) ((uint8_t)(x >> 8)) -#define LOW_BITS(x) ((uint8_t)(x & 0x00FF)) +#define HIGH_BITS(x) ((uint8_t)(x >> 8)) +#define LOW_BITS(x) ((uint8_t)(x & 0x00FF)) /** * Function definitions @@ -50,14 +50,14 @@ __attribute__((weak)) void iton_bt_enters_connection_state(void) {} /** * Driver variables */ -bool iton_bt_is_connected = false; -uint8_t iton_bt_led_state = 0x00; +bool iton_bt_is_connected = false; +uint8_t iton_bt_led_state = 0x00; static uint8_t iton_bt_buffer[ITON_BT_BUFFER_LEN]; -uint8_t iton_bt_send_kb_last_key = 0x00; +uint8_t iton_bt_send_kb_last_key = 0x00; const SPIConfig iton_bt_spicfg = { - .slave = true, + .slave = true, .data_cb = iton_bt_data_cb, #if defined(MCU_SN32) .ctrl0 = SPI_DATA_LENGTH(8), @@ -85,14 +85,14 @@ static inline void iton_bt_rx_battery_notif(uint8_t data) { iton_bt_battery_level(data); break; case batt_wake_mcu: - #ifdef ITON_BT_ENABLE_ACK +# ifdef ITON_BT_ENABLE_ACK iton_bt_send_ack(control_bt, wake_ack); - #endif +# endif break; case batt_unknown: - #ifdef ITON_BT_ENABLE_ACK +# ifdef ITON_BT_ENABLE_ACK iton_bt_send_ack(control_bt, unknown_ack); - #endif +# endif break; case query_working_mode: break; @@ -106,9 +106,9 @@ static inline void iton_bt_rx_bluetooth_notif(uint8_t data) { case bt_connection_success: iton_bt_is_connected = true; - #ifdef ITON_BT_ENABLE_ACK +# ifdef ITON_BT_ENABLE_ACK iton_bt_send_ack(control_bt, connect_ack); - #endif +# endif iton_bt_connection_successful(); break; @@ -118,9 +118,9 @@ static inline void iton_bt_rx_bluetooth_notif(uint8_t data) { case bt_disconected: iton_bt_is_connected = false; - #ifdef ITON_BT_ENABLE_ACK +# ifdef ITON_BT_ENABLE_ACK iton_bt_send_ack(control_bt, disconnect_ack); - #endif +# endif iton_bt_disconnected(); break; @@ -140,10 +140,10 @@ static void iton_bt_rx_cb(void *arg) { spiStopTransferI(&ITON_BT_SPI_PORT, NULL); chSysUnlockFromISR(); - #ifdef ITON_BT_ENABLE_ACK +# ifdef ITON_BT_ENABLE_ACK // hack to make sure irq is low since acks messes with stuff writePinLow(ITON_BT_IRQ_LINE); - #endif +# endif switch (iton_bt_buffer[0]) { case led_state: @@ -164,7 +164,6 @@ static void iton_bt_rx_cb(void *arg) { } #endif - void iton_bt_data_cb(SPIDriver *spip) { writePinLow(ITON_BT_IRQ_LINE); } @@ -187,7 +186,8 @@ void iton_bt_init(void) { } void iton_bt_send(uint8_t cmd, uint8_t *data, uint8_t len) { - while (readPin(ITON_BT_IRQ_LINE)); + while (readPin(ITON_BT_IRQ_LINE)) + ; writePinHigh(ITON_BT_IRQ_LINE); iton_bt_buffer[0] = cmd; @@ -196,7 +196,8 @@ void iton_bt_send(uint8_t cmd, uint8_t *data, uint8_t len) { } void iton_bt_send2(uint8_t cmd, uint8_t b1, uint8_t b2) { - while (readPin(ITON_BT_IRQ_LINE)); + while (readPin(ITON_BT_IRQ_LINE)) + ; writePinHigh(ITON_BT_IRQ_LINE); iton_bt_buffer[0] = cmd; diff --git a/drivers/bluetooth/iton_bt.h b/drivers/bluetooth/iton_bt.h index df40965b708c..eba8766f0ef0 100644 --- a/drivers/bluetooth/iton_bt.h +++ b/drivers/bluetooth/iton_bt.h @@ -20,44 +20,44 @@ enum iton_bt_cmd { set_alarm_volt = 0xA8, // iton to mcu - led_state = 0xB1, - notification = 0xB6, + led_state = 0xB1, + notification = 0xB6, }; enum iton_bt_control_cmd { - control_power = 0x25, - control_usb = 0x58, - control_bt = 0x51, - control_pins = 0x52, + control_power = 0x25, + control_usb = 0x58, + control_bt = 0x51, + control_pins = 0x52, }; enum iton_bt_control_param { // control_power - sleep_idle_10m = 0x01, - sleep_idle_20m = 0x02, - sleep_idle_30m = 0x03, + sleep_idle_10m = 0x01, + sleep_idle_20m = 0x02, + sleep_idle_30m = 0x03, // control_usb - mode_usb = 0x01, + mode_usb = 0x01, // control_bt - mode_bt = 0x62, - reset_pairing = 0x70, - enter_pairing = 0x89, - switch_profile = 0x81, // + 0-5 profiles - os_mac = 0x74, - os_win = 0x75, - - connect_ack = 0x50, - disconnect_ack = 0x51, - wake_ack = 0x60, - unknown_ack = 0x52, - - query_voltage = 0x66, + mode_bt = 0x62, + reset_pairing = 0x70, + enter_pairing = 0x89, + switch_profile = 0x81, // + 0-5 profiles + os_mac = 0x74, + os_win = 0x75, + + connect_ack = 0x50, + disconnect_ack = 0x51, + wake_ack = 0x60, + unknown_ack = 0x52, + + query_voltage = 0x66, query_battery_level = 0x61, - disable_sleep = 0x65, - enable_sleep = 0x68, + disable_sleep = 0x65, + enable_sleep = 0x68, }; enum iton_bt_notification_type { @@ -67,34 +67,34 @@ enum iton_bt_notification_type { enum iton_bt_notification_param { // notif_battery - batt_voltage_low = 0x06, - batt_exit_low_battery_mode = 0x0A, - batt_low_power_shutdown = 0x07, + batt_voltage_low = 0x06, + batt_exit_low_battery_mode = 0x0A, + batt_low_power_shutdown = 0x07, - batt_above_70 = 0x04, - batt_between_30_70 = 0x02, - batt_below_30 = 0x01, + batt_above_70 = 0x04, + batt_between_30_70 = 0x02, + batt_below_30 = 0x01, - query_working_mode = 0xA0, - query_bt_name = 0xA1, + query_working_mode = 0xA0, + query_bt_name = 0xA1, // Wake from batt_low_power_shutdown - batt_wake_mcu = 0x0B, + batt_wake_mcu = 0x0B, - batt_unknown = 0x08, + batt_unknown = 0x08, // notif_bluetooth - bt_connection_success = 0x76, - bt_entered_pairing = 0x77, - bt_disconected = 0x78, - bt_enters_connection = 0x79, + bt_connection_success = 0x76, + bt_entered_pairing = 0x77, + bt_disconected = 0x78, + bt_enters_connection = 0x79, }; /** * Exported Variables */ uint8_t iton_bt_led_state; -bool iton_bt_is_connected; +bool iton_bt_is_connected; /** * Driver Callbacks @@ -130,18 +130,18 @@ void iton_bt_send_keyboard(report_keyboard_t *report); * Driver Macros */ #define iton_bt_control(cmd, param) iton_bt_send2(control, cmd, param) -#define iton_bt_control_bt(param) iton_bt_control(control_bt, param) -#define iton_bt_control_usb(param) iton_bt_control(control_usb, param) - -#define iton_bt_mode_usb() iton_bt_control_usb(mode_usb) -#define iton_bt_mode_bt() iton_bt_control_bt(mode_bt) -#define iton_bt_reset_pairing() iton_bt_control_bt(reset_pairing) -#define iton_bt_enter_pairing() iton_bt_control_bt(enter_pairing) -#define iton_bt_switch_profile(x) iton_bt_control_bt(switch_profile + x) -#define iton_bt_os_mac() iton_bt_control_bt(os_mac) -#define iton_bt_os_win() iton_bt_control_bt(os_win) -#define iton_bt_query_voltage() iton_bt_control_bt(query_voltage) +#define iton_bt_control_bt(param) iton_bt_control(control_bt, param) +#define iton_bt_control_usb(param) iton_bt_control(control_usb, param) + +#define iton_bt_mode_usb() iton_bt_control_usb(mode_usb) +#define iton_bt_mode_bt() iton_bt_control_bt(mode_bt) +#define iton_bt_reset_pairing() iton_bt_control_bt(reset_pairing) +#define iton_bt_enter_pairing() iton_bt_control_bt(enter_pairing) +#define iton_bt_switch_profile(x) iton_bt_control_bt(switch_profile + x) +#define iton_bt_os_mac() iton_bt_control_bt(os_mac) +#define iton_bt_os_win() iton_bt_control_bt(os_win) +#define iton_bt_query_voltage() iton_bt_control_bt(query_voltage) #define iton_bt_query_battery_level() iton_bt_control_bt(query_battery_level) -#define iton_bt_disable_sleep() iton_bt_control_bt(disable_sleep) -#define iton_bt_enable_sleep() iton_bt_control_bt(enable_sleep) +#define iton_bt_disable_sleep() iton_bt_control_bt(disable_sleep) +#define iton_bt_enable_sleep() iton_bt_control_bt(enable_sleep)