From cf3c26217b082406651d02a8d70a4e38821703f2 Mon Sep 17 00:00:00 2001 From: jiaxin96 <572673807@qq.com> Date: Sat, 19 Jun 2021 22:11:51 +0800 Subject: [PATCH] fix uart --- .../biu_nrf52_ble_lib/biu_ble_common.cpp | 2 + .../biu_nrf52_ble_lib/biu_ble_common.h | 2 + .../biu_nrf52_ble_lib/biu_nrf52.cpp | 87 +++++++++++++++---- .../yandrstudio/biu_nrf52_ble_lib/main.c | 5 ++ .../biu_nrf52_ble_lib/outputselect.h | 2 +- .../yandrstudio/biu_nrf52_ble_lib/uart.c | 2 +- 6 files changed, 80 insertions(+), 20 deletions(-) diff --git a/keyboards/yandrstudio/biu_nrf52_ble_lib/biu_ble_common.cpp b/keyboards/yandrstudio/biu_nrf52_ble_lib/biu_ble_common.cpp index d398cd11d257..31fb0f3f308e 100644 --- a/keyboards/yandrstudio/biu_nrf52_ble_lib/biu_ble_common.cpp +++ b/keyboards/yandrstudio/biu_nrf52_ble_lib/biu_ble_common.cpp @@ -2,6 +2,8 @@ #include "biu_ble_common.h" __attribute__((weak)) bool bluetooth_init(void) { return true; } +__attribute__((weak)) bool bluetooth_init_pre(void) { return true; } +__attribute__((weak)) bool bluetooth_init_pos(void) { return true; } __attribute__((weak)) void bluetooth_task(void) {} diff --git a/keyboards/yandrstudio/biu_nrf52_ble_lib/biu_ble_common.h b/keyboards/yandrstudio/biu_nrf52_ble_lib/biu_ble_common.h index df2ad810c9b2..cb5a7d4c9409 100644 --- a/keyboards/yandrstudio/biu_nrf52_ble_lib/biu_ble_common.h +++ b/keyboards/yandrstudio/biu_nrf52_ble_lib/biu_ble_common.h @@ -8,6 +8,8 @@ extern "C" { #endif bool bluetooth_init(void); +bool bluetooth_init_pre(void); +bool bluetooth_init_pos(void); void bluetooth_task(void); bool bluetooth_is_connected(void); void bluetooth_unpair_all(void); diff --git a/keyboards/yandrstudio/biu_nrf52_ble_lib/biu_nrf52.cpp b/keyboards/yandrstudio/biu_nrf52_ble_lib/biu_nrf52.cpp index 927fb2123c53..8296d7737b5a 100644 --- a/keyboards/yandrstudio/biu_nrf52_ble_lib/biu_nrf52.cpp +++ b/keyboards/yandrstudio/biu_nrf52_ble_lib/biu_nrf52.cpp @@ -65,7 +65,7 @@ #define BiuNrf52MsgTimeout 150 /* milliseconds */ #define BiuNrf52MsgShortTimeout 10 /* milliseconds */ #define BiuNrf52MsgBackOff 25 /* microseconds */ -#define BatteryUpdateInterval 60000 /* milliseconds */ +#define BatteryUpdateInterval 30000 /* milliseconds */ enum biunrf52_type { @@ -99,6 +99,11 @@ struct biunrf52_msg { } __attribute__((packed)); enum queue_type { + QTStartAdv, + QTStopAdv, + QTDelAllDev, + QTDelDev, + QTSwDev, QTBatVMsg, QTKeyReport, // 1-byte modifier + 6-byte key report #ifdef EXTRAKEY_ENABLE @@ -124,6 +129,7 @@ struct queue_item { uint16_t added; union __attribute__((packed)) { uint8_t bat_lev; + uint8_t device_id; struct __attribute__((packed)) { uint8_t modifier; uint8_t keys[6]; @@ -306,6 +312,27 @@ static bool process_queue_item(struct queue_item *item, uint16_t timeout) { #endif switch (item->queue_type) { + case QTStartAdv: + cmdbuf[1] = START_ADV_WITH_WL; + cmdbuf[2] = BleUartTail; + return send_a_pkt(cmdbuf, 3, timeout); + case QTStopAdv: + cmdbuf[1] = STOP_ADV; + cmdbuf[2] = BleUartTail; + return send_a_pkt(cmdbuf, 3, timeout); + case QTDelAllDev: + cmdbuf[1] = DEL_ALL_BOUND; + cmdbuf[2] = BleUartTail; + return send_a_pkt(cmdbuf, 3, timeout); + case QTDelDev: + cmdbuf[1] = DEL_CURR_BOUND; + cmdbuf[2] = BleUartTail; + return send_a_pkt(cmdbuf, 3, timeout); + case QTSwDev: + cmdbuf[1] = SWITCH_BOUND; + cmdbuf[2] = item->device_id; + cmdbuf[3] = BleUartTail; + return send_a_pkt(cmdbuf, 4, timeout); case QTBatVMsg: cmdbuf[1] = GET_BAT_INFO; cmdbuf[2] = item->bat_lev; @@ -505,20 +532,24 @@ void bluetooth_send_battery_level() { item.queue_type = QTBatVMsg; item.added = timer_read(); - item.bat_lev = cur_bat_lev < 5 ? 5 : cur_bat_lev; + item.bat_lev = cur_bat_lev < 10 ? 10 : cur_bat_lev; while (!send_buf.enqueue(item)) { send_buf_send_one(); } } +bool bluetooth_init_pre(void) { + // start the uart data trans + uart_init(BIUNRF52UartBaud); +} + bool bluetooth_init(void) { state.initialized = false; state.configured = false; state.is_connected = false; - // start the uart data trans - uart_init(BIUNRF52UartBaud); + bluetooth_init_pre(); // Perform a hardware reset setPinOutput(BIUNRF52ResetPin); @@ -545,14 +576,24 @@ bool bluetooth_init(void) { void connected(void) { uprintf("biu ble connected\n"); // start adv with wl - uart_putchar(0xff); + struct queue_item item; + + item.queue_type = QTStartAdv; + while (!send_buf.enqueue(item)) { + send_buf_send_one(); + } state.is_connected = true; } void disconnected(void) { uprintf("biu ble disconnected\n"); // stop adv - uart_putchar(0xff); + struct queue_item item; + + item.queue_type = QTStopAdv; + while (!send_buf.enqueue(item)) { + send_buf_send_one(); + } state.is_connected = false; } @@ -562,25 +603,35 @@ bool bluetooth_is_connected(void) { return state.is_connected; } void bluetooth_unpair_all(void) { uprintf("biu ble del all\n"); // stop adv and del all - uart_putchar(0xff); + struct queue_item item; + + item.queue_type = QTDelAllDev; + while (!send_buf.enqueue(item)) { + send_buf_send_one(); + } state.is_connected = false; } -void bluetooth_unpair_one(uint8_t device_id) { - uprintf("biu ble del %d\n", device_id); +void bluetooth_unpair(uint8_t device_id) { + uprint("biu ble del current\n"); // stop adv and del one - uart_putchar(0xff); + struct queue_item item; + + item.queue_type = QTDelDev; + while (!send_buf.enqueue(item)) { + send_buf_send_one(); + } state.is_connected = false; } -void bluetooth_pair(void) { - uprintf("biu ble pair cuurt\n"); - // start adv with wl (auto) - uart_putchar(0xff); - state.is_connected = true; -} + void bluetooth_switch_one(uint8_t device_id) { uprintf("biu ble pair %d\n", device_id); - // switch adv with wl (auto) - uart_putchar(0xff); + struct queue_item item; + + item.queue_type = QTSwDev; + item.device_id = device_id; + while (!send_buf.enqueue(item)) { + send_buf_send_one(); + } state.is_connected = true; } diff --git a/keyboards/yandrstudio/biu_nrf52_ble_lib/main.c b/keyboards/yandrstudio/biu_nrf52_ble_lib/main.c index f3373d9b95e9..3c23f4c72dbf 100644 --- a/keyboards/yandrstudio/biu_nrf52_ble_lib/main.c +++ b/keyboards/yandrstudio/biu_nrf52_ble_lib/main.c @@ -194,6 +194,8 @@ int main(void) { visualizer_init(); #endif + + host_driver_t *driver = NULL; /* Wait until the USB or serial link is active */ @@ -217,6 +219,9 @@ int main(void) { wait_ms(50); } +// #ifdef BIU_BLE5_ENABLE +// bluetooth_init_pre(); +// #endif /* Do need to wait here! * Otherwise the next print might start a transfer on console EP * before the USB is completely ready, which sometimes causes diff --git a/keyboards/yandrstudio/biu_nrf52_ble_lib/outputselect.h b/keyboards/yandrstudio/biu_nrf52_ble_lib/outputselect.h index 85c65aac1d53..2ae74e46e497 100644 --- a/keyboards/yandrstudio/biu_nrf52_ble_lib/outputselect.h +++ b/keyboards/yandrstudio/biu_nrf52_ble_lib/outputselect.h @@ -24,7 +24,7 @@ enum outputs { }; #ifndef OUTPUT_DEFAULT -# define OUTPUT_DEFAULT OUTPUT_BLUETOOTH +# define OUTPUT_DEFAULT OUTPUT_AUTO #endif diff --git a/keyboards/yandrstudio/biu_nrf52_ble_lib/uart.c b/keyboards/yandrstudio/biu_nrf52_ble_lib/uart.c index 8e1854664f30..6ce91f934349 100644 --- a/keyboards/yandrstudio/biu_nrf52_ble_lib/uart.c +++ b/keyboards/yandrstudio/biu_nrf52_ble_lib/uart.c @@ -33,7 +33,7 @@ void uart_init(uint32_t baud) { palSetLineMode(SD1_RX_PIN, PAL_MODE_STM32_ALTERNATE_OPENDRAIN); #else palSetLineMode(SD1_TX_PIN, PAL_MODE_ALTERNATE(SD1_TX_PAL_MODE) | PAL_STM32_OTYPE_PUSHPULL); - palSetLineMode(SD1_RX_PIN, PAL_MODE_ALTERNATE(SD1_RX_PAL_MODE) | PAL_MODE_INPUT_PULLUP); + palSetLineMode(SD1_RX_PIN, PAL_MODE_ALTERNATE(SD1_RX_PAL_MODE) | PAL_MODE_INPUT); #endif sdStart(&SERIAL_DRIVER, &serialConfig); }