Skip to content

Commit

Permalink
NKRO (usb and rf) (#24)
Browse files Browse the repository at this point in the history
* nkro support (untested)

* adjusted nkro report size, it now works

* nkro through rf

* nkro as a feature

* fix extra keys

* update readme
  • Loading branch information
carlossless authored Jan 1, 2025
1 parent 12cbd53 commit 0813220
Show file tree
Hide file tree
Showing 20 changed files with 743 additions and 75 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ on:

jobs:
build:
strategy:
matrix:
nkro: [enabled, disabled]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -21,13 +24,13 @@ jobs:
name: smk
authToken: ${{ secrets.CACHIX_AUTH_TOKEN }}
- name: Setup
run: nix develop --command meson setup build
run: nix develop --command meson setup build -Dnkro=${{ matrix.nkro }}
- name: Build
run: nix develop --command meson compile -C build
- name: Archive code coverage results
uses: actions/upload-artifact@v4
with:
name: firmware
name: smk-${{ matrix.nkro == 'enabled' && 'nkro' || '6kro' }}
path: build/*.hex

lint:
Expand Down
355 changes: 354 additions & 1 deletion docs/keyboards/nuphy-air60.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/smk.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- [x] USB Keyboard
- [x] Status LEDs
- [x] Extra Keys (System / Consumer)
- [ ] NKRO
- [x] NKRO
- [~] QMK Compatibility
- [~] Layouts
- [~] Wireless
Expand Down
6 changes: 5 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ project(
'smk',
'c',
version: 'alpha',
meson_version: '>=0.53.0'
meson_version: '>=1.1.0'
)

fs = import('fs')
Expand Down Expand Up @@ -113,6 +113,10 @@ cc_base_args = [
'-DSMK_VERSION=@0@'.format(meson.project_version()),
]

if get_option('nkro').enabled()
cc_base_args += '-DNKRO_ENABLE=1'
endif

if get_option('buildtype') == 'debug'
cc_base_args += '-DDEBUG=1'
endif
Expand Down
1 change: 1 addition & 0 deletions meson.options
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
option('nkro', type : 'feature', value : 'enabled', description: 'NKRO support')
5 changes: 3 additions & 2 deletions src/kb/kb.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
#include <stdbool.h>

void kb_init();
void kb_send_report(report_keyboard_t *report);
void kb_send_extra(report_extra_t *report);
void kb_send_report(__xdata report_keyboard_t *report);
void kb_send_nkro(__xdata report_nkro_t *report);
void kb_send_extra(__xdata report_extra_t *report);

bool kb_process_record(uint16_t keycode, bool key_pressed);
void kb_update_switches();
Expand Down
9 changes: 7 additions & 2 deletions src/keyboards/example/kb.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#include "report.h"
#include "usb.h"

void kb_send_report(report_keyboard_t *report)
void kb_send_report(__xdata report_keyboard_t *report)
{
usb_send_report(report);
}

void kb_send_extra(report_extra_t *report)
void kb_send_nkro(__xdata report_nkro_t *report)
{
usb_send_nkro(report);
}

void kb_send_extra(__xdata report_extra_t *report)
{
usb_send_extra(report);
}
9 changes: 7 additions & 2 deletions src/keyboards/eyooso-z11/kb.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
#include "report.h"
#include "usb.h"

void kb_send_report(report_keyboard_t *report)
void kb_send_report(__xdata report_keyboard_t *report)
{
usb_send_report(report);
}

void kb_send_extra(report_extra_t *report)
void kb_send_nkro(__xdata report_nkro_t *report)
{
usb_send_nkro(report);
}

void kb_send_extra(__xdata report_extra_t *report)
{
usb_send_extra(report);
}
18 changes: 16 additions & 2 deletions src/keyboards/nuphy-air60/kb.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ bool kb_process_record(uint16_t keycode, bool key_pressed)
}
}

void kb_send_report(report_keyboard_t *report)
void kb_send_report(__xdata report_keyboard_t *report)
{
switch (user_keyboard_state.conn_mode) {
case KEYBOARD_CONN_MODE_USB:
Expand All @@ -90,7 +90,21 @@ void kb_send_report(report_keyboard_t *report)
}
}

void kb_send_extra(report_extra_t *report)
void kb_send_nkro(__xdata report_nkro_t *report)
{
switch (user_keyboard_state.conn_mode) {
case KEYBOARD_CONN_MODE_USB:
usb_send_nkro(report);
break;
#ifdef RF_ENABLED
case KEYBOARD_CONN_MODE_RF:
rf_send_nkro(report);
break;
#endif
}
}

void kb_send_extra(__xdata report_extra_t *report)
{
switch (user_keyboard_state.conn_mode) {
case KEYBOARD_CONN_MODE_USB:
Expand Down
2 changes: 0 additions & 2 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ void main()
{
init();

delay_ms(1000); // perhaps clocks are unstable at this point? influences UART

dprintf("SMK v" TOSTRING(SMK_VERSION) "\r\n");
dprintf("DEVICE vId:" TOSTRING(USB_VID) " pId:" TOSTRING(USB_PID) "\n\r");

Expand Down
133 changes: 105 additions & 28 deletions src/platform/bk3632/rf_controller.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ void rf_send_blank_report();
bool rf_get_status(uint8_t status_bytes[2]);
void rf_cmd_01(uint8_t mode, uint8_t pairing);
void rf_cmd_02(uint8_t *buffer);
void rf_cmd_02_nkro(__xdata uint8_t mods, __xdata uint8_t *nkro_buffer);
void rf_cmd_03(uint8_t param);
void rf_cmd_04();
void rf_cmd_05(uint16_t consumer, uint16_t system);
Expand Down Expand Up @@ -70,46 +71,78 @@ void rf_init()
rf_send_blank_report();
}

void rf_send_report(report_keyboard_t *report)
{
__xdata uint8_t buffer[6];
__xdata uint8_t kro6buffer[6];

buffer[0] = report->raw[0];
buffer[1] = report->raw[2];
buffer[2] = report->raw[3];
buffer[3] = report->raw[4];
buffer[4] = report->raw[5];
buffer[5] = report->raw[6];
void rf_send_report(__xdata report_keyboard_t *report)
{
kro6buffer[0] = report->raw[0];
kro6buffer[1] = report->raw[2];
kro6buffer[2] = report->raw[3];
kro6buffer[3] = report->raw[4];
kro6buffer[4] = report->raw[5];
kro6buffer[5] = report->raw[6];

bool blank = true;
for (int i = 0; i < 6; i++) {
if (buffer[i] != 0) {
for (__xdata int i = 0; i < 6; i++) {
if (kro6buffer[i] != 0) {
blank = false;
}
}

if (blank) {
// blanking sequence
kro6buffer[1] = 0x00;
rf_cmd_02(kro6buffer);
kro6buffer[1] = 0x01;
rf_cmd_02(kro6buffer);
kro6buffer[1] = 0x00;
rf_cmd_02(kro6buffer);
kro6buffer[1] = 0x01;
rf_cmd_02(kro6buffer);
kro6buffer[1] = 0x00;
rf_cmd_02(kro6buffer);
kro6buffer[1] = 0x01;
rf_cmd_02(kro6buffer);
kro6buffer[1] = 0x00;
rf_cmd_02(kro6buffer);
} else {
rf_cmd_02(kro6buffer);
}
}

void rf_send_nkro(__xdata report_nkro_t *report)
{
__xdata bool blank = true;
for (__xdata int i = 1; i < NKRO_REPORT_SIZE - 1; i++) {
if (report->raw[i] != 0) {
blank = false;
}
}

dprintf("is blank: %d\r\n", blank); // FIXME: a delay is necessary here

if (blank) {
// blanking sequence
buffer[1] = 0x00;
rf_cmd_02(buffer);
buffer[1] = 0x01;
rf_cmd_02(buffer);
buffer[1] = 0x00;
rf_cmd_02(buffer);
buffer[1] = 0x01;
rf_cmd_02(buffer);
buffer[1] = 0x00;
rf_cmd_02(buffer);
buffer[1] = 0x01;
rf_cmd_02(buffer);
buffer[1] = 0x00;
rf_cmd_02(buffer);
kro6buffer[1] = 0x00;
rf_cmd_02(kro6buffer);
kro6buffer[1] = 0x01;
rf_cmd_02(kro6buffer);
kro6buffer[1] = 0x00;
rf_cmd_02(kro6buffer);
kro6buffer[1] = 0x01;
rf_cmd_02(kro6buffer);
kro6buffer[1] = 0x00;
rf_cmd_02(kro6buffer);
kro6buffer[1] = 0x01;
rf_cmd_02(kro6buffer);
kro6buffer[1] = 0x00;
rf_cmd_02(kro6buffer);
} else {
rf_cmd_02(buffer);
rf_cmd_02_nkro(report->mods, report->bits);
}
}

void rf_send_extra(report_extra_t *report)
void rf_send_extra(__xdata report_extra_t *report)
{
switch (report->report_id) {
case REPORT_ID_SYSTEM:
Expand Down Expand Up @@ -215,7 +248,6 @@ void rf_cmd_02(uint8_t *buffer)
rf_tx_buf[6] = buffer[3];
rf_tx_buf[7] = buffer[4];
rf_tx_buf[8] = buffer[5];
// FIXME: last keyboard report key is lost
rf_tx_buf[9] = 0x00; // 0x00 or 0x01

for (int i = 10; i < 31; i++) { // FIXME: NKRO bytes are blanked out until they are implemented
Expand All @@ -227,6 +259,51 @@ void rf_cmd_02(uint8_t *buffer)
bb_spi_xfer(rf_tx_buf, len);
}

void rf_cmd_02_nkro(__xdata uint8_t mods, __xdata uint8_t *nkro_buffer)
{
const uint8_t len = 32;

rf_tx_buf[0] = MAGIC_BYTE;
rf_tx_buf[1] = len - 3;
rf_tx_buf[2] = CMD_REPORT;
rf_tx_buf[3] = mods;

rf_tx_buf[4] = 0x00;
rf_tx_buf[5] = 0x00;
rf_tx_buf[6] = 0x00;
rf_tx_buf[7] = 0x00;
rf_tx_buf[8] = 0x00;

rf_tx_buf[9] = 0x00; // 0x00 or 0x01

rf_tx_buf[10] = nkro_buffer[0];
rf_tx_buf[11] = nkro_buffer[1];
rf_tx_buf[12] = nkro_buffer[2];
rf_tx_buf[13] = nkro_buffer[3];
rf_tx_buf[14] = nkro_buffer[4];
rf_tx_buf[15] = nkro_buffer[5];
rf_tx_buf[16] = nkro_buffer[6];
rf_tx_buf[17] = nkro_buffer[7];
rf_tx_buf[18] = nkro_buffer[8];
rf_tx_buf[19] = nkro_buffer[9];
rf_tx_buf[20] = nkro_buffer[10];
rf_tx_buf[21] = nkro_buffer[11];
rf_tx_buf[22] = nkro_buffer[12];
rf_tx_buf[23] = nkro_buffer[13];
rf_tx_buf[24] = nkro_buffer[14];
rf_tx_buf[25] = nkro_buffer[15];
rf_tx_buf[26] = nkro_buffer[16];
rf_tx_buf[27] = nkro_buffer[17];
rf_tx_buf[28] = nkro_buffer[18];
rf_tx_buf[29] = nkro_buffer[19];

rf_tx_buf[30] = 0x00;

rf_tx_buf[31] = checksum(rf_tx_buf, len - 1);

bb_spi_xfer(rf_tx_buf, len);
}

void rf_cmd_03(uint8_t param) // ?? or 0x02
{
const uint8_t len = 6;
Expand Down
5 changes: 3 additions & 2 deletions src/platform/bk3632/rf_controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "keyboard.h"

void rf_init();
void rf_send_report(report_keyboard_t *report);
void rf_send_extra(report_extra_t *report);
void rf_send_report(__xdata report_keyboard_t *report);
void rf_send_nkro(__xdata report_nkro_t *report);
void rf_send_extra(__xdata report_extra_t *report);
void rf_update_keyboard_state(keyboard_state_t *keyboard);
Loading

0 comments on commit 0813220

Please sign in to comment.