diff --git a/keyboards/annepro2/annepro2.c b/keyboards/annepro2/annepro2.c index 836dc31f09a..2ec26834cf1 100644 --- a/keyboards/annepro2/annepro2.c +++ b/keyboards/annepro2/annepro2.c @@ -1,19 +1,22 @@ #include "ch.h" #include "hal.h" #include "annepro2.h" +#include "annepro2_ble.h" #include "qmk_ap2_led.h" -/** - * @brief Driver default configuration. - */ static const SerialConfig ledUartConfig = { .speed = 115200, }; -static uint8_t ledMcuWakeup[11] = { -0x7b, 0x10, 0x43, 0x10, 0x03, 0, 0, 0x7d, 0x02, 0x01, 0x02 +static const SerialConfig bleUartConfig = { + .speed = 115200, }; +static uint8_t ledMcuWakeup[11] = { + 0x7b, 0x10, 0x43, 0x10, 0x03, 0x00, 0x00, 0x7d, 0x02, 0x01, 0x02 +}; + + uint16_t annepro2LedMatrix[MATRIX_ROWS * MATRIX_COLS] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0, @@ -22,11 +25,20 @@ uint16_t annepro2LedMatrix[MATRIX_ROWS * MATRIX_COLS] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0, }; -void OVERRIDE matrix_init_kb(void) { - // Start uart +void OVERRIDE keyboard_pre_init_kb(void) { +} + +void OVERRIDE keyboard_post_init_kb(void) { + // Start LED UART sdStart(&SD0, &ledUartConfig); sdWrite(&SD0, ledMcuWakeup, 11); + // Start BLE UART + sdStart(&SD1, &bleUartConfig); + annepro2_ble_startup(); +} + +void OVERRIDE matrix_init_kb(void) { matrix_init_user(); } @@ -61,6 +73,45 @@ bool OVERRIDE led_update_kb(led_t status) { return led_update_user(status); } +/*! + * @returns false processing for this keycode has been completed. + */ bool OVERRIDE process_record_kb(uint16_t keycode, keyrecord_t *record) { + if (record->event.pressed) { + switch (keycode) { + case KC_AP2_BT_SCAN1: + annepro2_ble_broadcast(0); + return false; + case KC_AP2_BT_SCAN2: + annepro2_ble_broadcast(1); + return false; + case KC_AP2_BT_SCAN3: + annepro2_ble_broadcast(2); + return false; + case KC_AP2_BT_SCAN4: + annepro2_ble_broadcast(3); + return false; + case KC_AP2_BT_CONN1: + annepro2_ble_connect(0); + return false; + case KC_AP2_BT_CONN2: + annepro2_ble_connect(1); + return false; + case KC_AP2_BT_CONN3: + annepro2_ble_connect(2); + return false; + case KC_AP2_BT_CONN4: + annepro2_ble_connect(3); + return false; + case KC_AP2_USB: + annepro2_ble_disconnect(); + return false; + case KC_AP2_BT_UNPAIR: + annepro2_ble_unpair(); + return false; + default: + break; + } + } return process_record_user(keycode, record); } diff --git a/keyboards/annepro2/annepro2.h b/keyboards/annepro2/annepro2.h index b7dbe3c5a79..4f6e3821f38 100644 --- a/keyboards/annepro2/annepro2.h +++ b/keyboards/annepro2/annepro2.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018 Charlie Waters + * Copyright (c) 2018 Yaotian Feng * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -23,6 +23,20 @@ extern uint16_t annepro2LedMatrix[MATRIX_ROWS * MATRIX_COLS]; +enum AP2KeyCodes { + KC_AP2_BT_SCAN1 = SAFE_RANGE, + KC_AP2_BT_SCAN2, + KC_AP2_BT_SCAN3, + KC_AP2_BT_SCAN4, + KC_AP2_BT_CONN1, + KC_AP2_BT_CONN2, + KC_AP2_BT_CONN3, + KC_AP2_BT_CONN4, + KC_AP2_BT_UNPAIR, + KC_AP2_USB, + AP2_SAFE_RANGE, +}; + void annepro2LedDisable(void); void annepro2LedEnable(void); void annepro2LedUpdate(uint8_t row, uint8_t col); diff --git a/keyboards/annepro2/annepro2_ble.c b/keyboards/annepro2/annepro2_ble.c new file mode 100644 index 00000000000..57d38efc2f4 --- /dev/null +++ b/keyboards/annepro2/annepro2_ble.c @@ -0,0 +1,141 @@ +/* + Copyright (C) 2020 Yaotian Feng, Codetector + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#include "annepro2_ble.h" +#include "ch.h" +#include "hal.h" +#include "host.h" +#include "host_driver.h" + +/* -------------------- Static Function Prototypes -------------------------- */ +static uint8_t ap2_ble_leds(void); +static void ap2_ble_mouse(report_mouse_t *report); +static void ap2_ble_system(uint16_t data); +static void ap2_ble_consumer(uint16_t data); +static void ap2_ble_keyboard(report_keyboard_t *report); + +static void ap2_ble_swtich_ble_driver(void); + +/* -------------------- Static Local Variables ------------------------------ */ +static host_driver_t ap2_ble_driver = { + ap2_ble_leds, + ap2_ble_keyboard, + ap2_ble_mouse, + ap2_ble_system, + ap2_ble_consumer, +}; + +static uint8_t bleMcuWakeup[11] = { + 0x7b, 0x10, 0x53, 0x10, 0x03, 0x00, 0x01, 0x7d, 0x02, 0x01, 0x02 +}; +static uint8_t bleMcuStartBroadcast[11] = { + 0x7b, 0x10, 0x53, 0x10, 0x03, 0x00, 0x00, 0x7d, 0x40, 0x01, 0x00 // Broadcast ID[0-3] +}; +static uint8_t bleMcuConnect[11] = { + 0x7b, 0x10, 0x53, 0x10, 0x03, 0x00, 0x00, 0x7d, 0x40, 0x04, 0x00 // Connect ID [0-3] +}; +static uint8_t bleMcuSendReport[10] = { + 0x7b, 0x10, 0x53, 0x10, 0x0A, 0x00, 0x00, 0x7d, 0x10, 0x04, +}; +static uint8_t bleMcuUnpair[10] = { + 0x7b, 0x10, 0x53, 0x10, 0x02, 0x00, 0x00, 0x7d, 0x40, 0x05, +}; + +static host_driver_t *lastHostDriver = NULL; +#ifdef NKRO_ENABLE +static bool lastNkroStatus = false; +#endif // NKRO_ENABLE + +/* -------------------- Public Function Implementation ---------------------- */ + +void annepro2_ble_startup(void) { + sdWrite(&SD1, bleMcuWakeup, 11); +} + +void annepro2_ble_broadcast(uint8_t port) { + if (port > 3) { + port = 3; + } + sdPut(&SD1, 0x00); + sdWrite(&SD1, bleMcuStartBroadcast, 10); + sdPut(&SD1, port); +} + +void annepro2_ble_connect(uint8_t port) { + if (port > 3) { + port = 3; + } + sdPut(&SD1, 0x00); + sdWrite(&SD1, bleMcuConnect, 10); + sdPut(&SD1, port); + ap2_ble_swtich_ble_driver(); +} + +void annepro2_ble_disconnect(void) { + /* Skip if the driver is already enabled */ + if (host_get_driver() != &ap2_ble_driver) { + return; + } + + clear_keyboard(); +#ifdef NKRO_ENABLE + keymap_config.nkro = lastNkroStatus; +#endif + host_set_driver(lastHostDriver); +} + +void annepro2_ble_unpair(void) { + sdPut(&SD1, 0x0); + sdWrite(&SD1, bleMcuUnpair, 11); +} + +/* ------------------- Static Function Implementation ----------------------- */ +static void ap2_ble_swtich_ble_driver(void) { + if (host_get_driver() == &ap2_ble_driver) { + return; + } + clear_keyboard(); + lastHostDriver = host_get_driver(); +#ifdef NKRO_ENABLE + lastNkroStatus = keymap_config.nkro; + keymap_config.nkro = false; +#endif + host_set_driver(&ap2_ble_driver); +} + +static uint8_t ap2_ble_leds(void) { + return 0; // TODO: Figure out how to obtain LED status +} + +static void ap2_ble_mouse(report_mouse_t *report){ +} + +static void ap2_ble_system(uint16_t data) { +} + +static void ap2_ble_consumer(uint16_t data) { + +} + +/*! + * @brief Send keyboard HID report for Bluetooth driver + */ +static void ap2_ble_keyboard(report_keyboard_t *report) { + sdPut(&SD1, 0x0); + sdWrite(&SD1, bleMcuSendReport, 10); + sdWrite(&SD1, &report->raw[0], 8); +} + diff --git a/keyboards/annepro2/annepro2_ble.h b/keyboards/annepro2/annepro2_ble.h new file mode 100644 index 00000000000..71498890fde --- /dev/null +++ b/keyboards/annepro2/annepro2_ble.h @@ -0,0 +1,26 @@ +/* + Copyright (C) 2020 Yaotian Feng, Codetector + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +#pragma once + +#include "annepro2.h" +#include "quantum.h" + +void annepro2_ble_startup(void); +void annepro2_ble_broadcast(uint8_t port); +void annepro2_ble_connect(uint8_t port); +void annepro2_ble_disconnect(void); +void annepro2_ble_unpair(void); diff --git a/keyboards/annepro2/boards/ANNEPRO2/board.c b/keyboards/annepro2/boards/ANNEPRO2/board.c index 92d597a9e1d..2514d96f973 100644 --- a/keyboards/annepro2/boards/ANNEPRO2/board.c +++ b/keyboards/annepro2/boards/ANNEPRO2/board.c @@ -1,7 +1,5 @@ /* - ChibiOS - Copyright (C) 2006..2016 Giovanni Di Sirio - (C) 2018 Charlie Waters - (C) 2020 Yaotian Feng, Codetector + Copyright (C) 2020 Yaotian Feng, Codetector Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -62,6 +60,8 @@ #define AF_BITS(PORT, N) (\ PAFIO(PORT, N, LINE_UART_RX, AFIO_USART)| \ PAFIO(PORT, N, LINE_UART_TX, AFIO_USART)| \ + PAFIO(PORT, N, LINE_BT_UART_TX, AFIO_USART)| \ + PAFIO(PORT, N, LINE_BT_UART_RX, AFIO_USART)| \ PAFIO(PORT, N, LINE_ROW1, AFIO_GPIO) | \ PAFIO(PORT, N, LINE_ROW2, AFIO_GPIO) | \ PAFIO(PORT, N, LINE_ROW3, AFIO_GPIO) | \ diff --git a/keyboards/annepro2/boards/ANNEPRO2/board.h b/keyboards/annepro2/boards/ANNEPRO2/board.h index 405dc63d967..695f1b7e96a 100644 --- a/keyboards/annepro2/boards/ANNEPRO2/board.h +++ b/keyboards/annepro2/boards/ANNEPRO2/board.h @@ -34,8 +34,11 @@ * I/O */ -#define LINE_UART_TX PAL_LINE(IOPORTB, 0) -#define LINE_UART_RX PAL_LINE(IOPORTB, 1) +#define LINE_UART_TX PAL_LINE(IOPORTB, 0) // Master TX, LED RX +#define LINE_UART_RX PAL_LINE(IOPORTB, 1) // Master RX, LED TX + +#define LINE_BT_UART_TX PAL_LINE(IOPORTA, 4) // Master TX, BLE RX +#define LINE_BT_UART_RX PAL_LINE(IOPORTA, 5) // Master RX, BLE TX // outputs (rows are pulled low) #define LINE_ROW1 PAL_LINE(IOPORTC, 2) diff --git a/keyboards/annepro2/boards/ANNEPRO2_C18/board.h b/keyboards/annepro2/boards/ANNEPRO2_C18/board.h index 0251a3a0411..ee75f310d66 100644 --- a/keyboards/annepro2/boards/ANNEPRO2_C18/board.h +++ b/keyboards/annepro2/boards/ANNEPRO2_C18/board.h @@ -36,6 +36,9 @@ #define LINE_UART_TX PAL_LINE(IOPORTB, 0) #define LINE_UART_RX PAL_LINE(IOPORTB, 1) +#define LINE_BT_UART_TX PAL_LINE(IOPORTA, 4) // Master TX, BLE RX +#define LINE_BT_UART_RX PAL_LINE(IOPORTA, 5) // Master RX, BLE TX + // outputs (rows are pulled low) #define LINE_ROW1 PAL_LINE(IOPORTB, 5) #define LINE_ROW2 PAL_LINE(IOPORTB, 4) diff --git a/keyboards/annepro2/c15/rules.mk b/keyboards/annepro2/c15/rules.mk index c358b7e5033..0878c54cb36 100644 --- a/keyboards/annepro2/c15/rules.mk +++ b/keyboards/annepro2/c15/rules.mk @@ -1,7 +1,8 @@ # Anne Pro 2 SRC = \ matrix.c \ - hardfault_handler.c + hardfault_handler.c \ + annepro2_ble.c LAYOUTS += diff --git a/keyboards/annepro2/c18/rules.mk b/keyboards/annepro2/c18/rules.mk index 701307163d1..7df05caeb00 100644 --- a/keyboards/annepro2/c18/rules.mk +++ b/keyboards/annepro2/c18/rules.mk @@ -1,7 +1,8 @@ # Anne Pro 2 SRC = \ matrix.c \ - hardfault_handler.c + hardfault_handler.c \ + annepro2_ble.c LAYOUTS += diff --git a/keyboards/annepro2/keymaps/default/keymap.c b/keyboards/annepro2/keymaps/default/keymap.c index 8582cf556d6..e576205969d 100644 --- a/keyboards/annepro2/keymaps/default/keymap.c +++ b/keyboards/annepro2/keymaps/default/keymap.c @@ -7,7 +7,7 @@ enum anne_pro_layers { _FN2_LAYER, }; enum custom_keys { - KC_AP_LED_ON = SAFE_RANGE, + KC_AP_LED_ON = AP2_SAFE_RANGE, KC_AP_LED_OFF, }; /* diff --git a/keyboards/annepro2/mcuconf.h b/keyboards/annepro2/mcuconf.h index a503bb7c884..d57afebd4a6 100644 --- a/keyboards/annepro2/mcuconf.h +++ b/keyboards/annepro2/mcuconf.h @@ -43,22 +43,19 @@ #define HT32_AHB_PRESCALER 1 // 48 MHz -> 48 MHz #define HT32_USART_PRESCALER 1 // 48 MHz #define HT32_USB_PRESCALER 1 // 48 MHz -> 48 MHz -// SysTick uses processor clock at 72MHz +// SysTick uses processor clock at 48MHz #define HT32_ST_USE_HCLK TRUE /* * Peripheral driver settings */ -// #define HT32_GPT_USE_BFTM0 TRUE -// #define HT32_GPT_BFTM0_IRQ_PRIORITY 4 - -//#define HT32_SPI_USE_SPI1 TRUE -//#define HT32_SPI1_IRQ_PRIORITY 6 - #define HT32_SERIAL_USE_USART0 TRUE #define HT32_USART0_IRQ_PRIORITY 6 +#define HT32_SERIAL_USE_USART1 TRUE +#define HT32_USART1_IRQ_PRIORITY 7 + /* * USB driver settings */