summaryrefslogtreecommitdiffstats
path: root/keyboards/keychron/bluetooth/ckbt51.h
blob: f0bc08a98fd804324bb2125943a1440ade8ba645 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* Copyright 2021 @ Keychron (https://www.keychron.com)
 *
 * 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
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#pragma once

#include "stdint.h"

#ifdef BT_DRIVER_UART_BANK
#    define BT_DRIVER_UART_TX_BANK BT_DRIVER_UART_BANK
#    define BT_DRIVER_UART_RX_BANK BT_DRIVER_UART_BANK
#endif

#ifndef BT_DRIVER_UART_TX_BANK
#    define BT_DRIVER_UART_TX_BANK GPIOA
#endif

#ifndef BT_DRIVER_UART_RX_BANK
#    define BT_DRIVER_UART_RX_BANK GPIOA
#endif

#ifndef BT_DRIVER_UART_TX
#    define BT_DRIVER_UART_TX 2
#endif

#ifndef BT_DRIVER_UART_RX
#    define BT_DRIVER_UART_RX 3
#endif

#ifndef BT_DRIVER
#    define BT_DRIVER SD2
#endif

#ifdef USE_GPIOV1
#    ifndef BT_DRIVER_UART_TX_PAL_MODE
#        define BT_DRIVER_UART_TX_PAL_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL
#    endif
#    ifndef BT_DRIVER_UART_RX_PAL_MODE
#        define BT_DRIVER_UART_RX_PAL_MODE PAL_MODE_STM32_ALTERNATE_PUSHPULL
#    endif
#else
// The default PAL alternate modes are used to signal that the pins are used for I2C
#    ifndef BT_DRIVER_UART_TX_PAL_MODE
#        define BT_DRIVER_UART_TX_PAL_MODE 7
#    endif
#    ifndef BT_DRIVER_UART_RX_PAL_MODE
#        define BT_DRIVER_UART_RX_PAL_MODE 7
#    endif
#endif

// Error checking
#if !STM32_SERIAL_USE_USART1 && !STM32_SERIAL_USE_USART2 && !STM32_SERIAL_USE_USART3 && !STM32_SERIAL_USE_UART4 && !STM32_SERIAL_USE_UART5 && !STM32_SERIAL_USE_USART6 && !STM32_SERIAL_USE_UART7 && !STM32_SERIAL_USE_UART8 && !STM32_SERIAL_USE_LPUART1
#    error "BT driver activated but no USART/UART peripheral assigned"
#endif

#define PACKECT_HEADER_LEN 5
#define BDA_LEN 6
#define PACKET_MAX_LEN 64

enum {
    PAIRING_MODE_DEFAULT = 0x00,
    PAIRING_MODE_JUST_WORK,
    PAIRING_MODE_PASSKEY_ENTRY,
    PAIRING_MODE_LESC_OR_SSP,
    PAIRING_MODE_INVALID
};

enum {
    BT_MODE_DEFAUL,
    BT_MODE_CLASSIC,
    BT_MODE_LE,     // Note: CKBT51 doesn't support BLE
    BT_MODE_INVALID,
};

typedef struct {
    uint8_t     hostIndex;
    uint16_t    timeout;     /* Pairing timeout, valid value range from 30 to 3600 seconds, 0 for default */
    uint8_t     pairingMode; /* 0: default, 1: Just Works, 2: Passkey Entry */
    uint8_t     BRorLE;      /* Only available for dual mode module. Keep 0 for single mode module */
    uint8_t     txPower;     /* Only available for BLE module */
    const char* leName;      /* Only available for BLE module */
} pairing_param_t;

typedef struct {
    uint8_t  type;
    uint16_t full_votage;
    uint16_t empty_voltage;
    uint16_t shutdown_voltage;
} battery_param_t;

typedef struct {
    uint8_t  model_name[11];
    uint8_t  mode;
    uint8_t  bluetooth_version;
    uint8_t  firmware_version[11];
    uint8_t  hardware_version[11];
    uint16_t cmd_set_verson;
} __attribute__((packed)) module_info_t;

typedef struct {
    uint8_t  event_mode; /* Must be 0x02 */
    uint16_t connected_idle_timeout;
    uint16_t pairing_timeout;   /* Range: 30 ~ 3600 second, 0 for default */
    uint8_t  pairing_mode;      /* 0: default, 1: Just Works, 2: Passkey Entry */
    uint16_t reconnect_timeout; /* 0: default, 0xFF: Unlimited time, 2 ~ 254 seconds */
    uint8_t  report_rate;       /* 90 or 133 */
    uint8_t  rsvd1;
    uint8_t  rsvd2;
    uint8_t  vendor_id_source; /* 0: From Bluetooth SIG, 1: From USB-IF */
    uint16_t verndor_id;       /* No effect, the vendor ID is 0x3434 */
    uint16_t product_id;
    /* Below parametes is only available for BLE module  */
    uint16_t le_connection_interval_min;
    uint16_t le_connection_interval_max;
    uint16_t le_connection_interval_timeout;
} __attribute__((packed)) module_param_t;

void ckbt51_init(bool wakeup_from_low_power_mode);
void ckbt51_send_cmd(uint8_t* payload, uint8_t len, bool ack_enable, bool retry);

void ckbt51_send_keyboard(uint8_t* report);
void ckbt51_send_nkro(uint8_t* report);
void ckbt51_send_consumer(uint16_t report);
void ckbt51_send_system(uint16_t report);
void ckbt51_send_mouse(uint8_t* report);

void ckbt51_become_discoverable(uint8_t host_idx, void* param);
void ckbt51_connect(uint8_t hostIndex, uint16_t timeout);
void ckbt51_disconnect(void);
void ckbt51_switch_host(uint8_t hostIndex);
void ckbt51_read_state_reg(uint8_t reg, uint8_t len);

void ckbt51_get_info(module_info_t* info);
void ckbt51_set_param(module_param_t* param);
void ckbt51_get_param(module_param_t* param);
void ckbt51_set_local_name(const char* name);
void ckbt51_get_local_name(void);

void ckbt51_factory_reset(void);
void ckbt51_int_pin_test(bool enable);
void ckbt51_dfu_rx(uint8_t* data, uint8_t length);
void ckbt51_radio_test(uint8_t channel);

void ckbt51_task(void);