Skip to content

Commit

Permalink
fix uart
Browse files Browse the repository at this point in the history
  • Loading branch information
jiaxin96 committed Jun 19, 2021
1 parent df7a30c commit cf3c262
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 20 deletions.
2 changes: 2 additions & 0 deletions keyboards/yandrstudio/biu_nrf52_ble_lib/biu_ble_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}

Expand Down
2 changes: 2 additions & 0 deletions keyboards/yandrstudio/biu_nrf52_ble_lib/biu_ble_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
87 changes: 69 additions & 18 deletions keyboards/yandrstudio/biu_nrf52_ble_lib/biu_nrf52.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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];
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down
5 changes: 5 additions & 0 deletions keyboards/yandrstudio/biu_nrf52_ble_lib/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ int main(void) {
visualizer_init();
#endif



host_driver_t *driver = NULL;

/* Wait until the USB or serial link is active */
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion keyboards/yandrstudio/biu_nrf52_ble_lib/outputselect.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ enum outputs {
};

#ifndef OUTPUT_DEFAULT
# define OUTPUT_DEFAULT OUTPUT_BLUETOOTH
# define OUTPUT_DEFAULT OUTPUT_AUTO
#endif


Expand Down
2 changes: 1 addition & 1 deletion keyboards/yandrstudio/biu_nrf52_ble_lib/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit cf3c262

Please sign in to comment.