Skip to content

Commit

Permalink
intial untested rf implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
carlossless committed Jul 20, 2024
1 parent d2c9961 commit 9076a23
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 1 deletion.
20 changes: 19 additions & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ default_options = {
parts = [
# keyboard, platform, layouts, options
['example', 'sh68f90a', ['default'], {}],
['nuphy-air60', 'sh68f90a', ['default'], { 'vendor_id': '0x05ac', 'product_id': '0x024f' }],
['nuphy-air60', 'sh68f90a', ['default'], { 'vendor_id': '0x05ac', 'product_id': '0x024f', 'wireless': 'bk3632' }],
['eyooso-z11', 'sh68f90a', ['default'], { 'vendor_id': '0x258a', 'product_id': '0x002a' }],
]

Expand Down Expand Up @@ -70,6 +70,16 @@ src_platform_sh68f90a = [
'src/platform/sh68f90a/usb.c',
]

inc_platform_bk3632 = [
'src/platform/bk3632',
'src/platform', # for bb_spi
]

src_platform_bk3632 = [
'src/platform/bb_spi.c',
'src/platform/bk3632/rf_controller.c',
]

possible_src_keyboard = [
'user_matrix.c',
'user_init.c',
Expand Down Expand Up @@ -142,6 +152,14 @@ foreach part : parts
error('unsupported platform: @0@'.format(platform))
endif

wireless = options.get('wireless', '')
if wireless == 'bk3632'
src_main += src_platform_bk3632
inc_dirs += inc_platform_bk3632
elif wireless != ''
error('unsupported platform: @0@'.format(platform))
endif

kb_inc_dir = 'src/keyboards/@0@'.format(keyboard)
if fs.is_dir(kb_inc_dir)
inc_dirs += kb_inc_dir
Expand Down
5 changes: 5 additions & 0 deletions src/keyboards/nuphy-air60/kbdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,9 @@
#define CONN_MODE_SWITCH_P5_5 _P5_5 // 1 - USB, 0 - RF
#define OS_MODE_SWITCH_P5_6 _P5_6 // 1 - MAC, 0 - WIN

#define RF_BB_SPI_CS P7_4
#define RF_BB_SPI_SCK P4_7
#define RF_BB_SPI_MISO P0_6
#define RF_BB_SPI_MOSI P0_7

#endif
3 changes: 3 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "keyboard.h"
#include "user_init.h"
#include "indicators.h"
#include "rf_controller.h"

#include "pwm.h" // TODO: interrupt is defined here and need to be imported in main, centralise interupt definitions

Expand Down Expand Up @@ -52,6 +53,8 @@ void main()
dprintf("SMK v" TOSTRING(SMK_VERSION) "\r\n");
dprintf("DEVICE vId:" TOSTRING(USB_VID) " pId:" TOSTRING(USB_PID) "\n\r");

rf_init();

// enable pwm and interrupt (driving matrix scan)
indicators_start();

Expand Down
37 changes: 37 additions & 0 deletions src/platform/bb_spi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "bb_spi.h"
#include "kbdef.h"

uint8_t bb_spi_xfer_byte(uint8_t data);

void bb_spi_xfer(uint8_t *data, int len)
{
RF_BB_SPI_CS = 0;
for (int i = 0; i < len; i++) {
data[i] = bb_spi_xfer_byte(data[i]);
}
RF_BB_SPI_CS = 1;
}

uint8_t bb_spi_xfer_byte(uint8_t data)
{
uint8_t recv = 0;

for (uint8_t i = 0; i < 8; i++) {
RF_BB_SPI_SCK = 0;

if (data & 0x01) {
RF_BB_SPI_MOSI = 1;
} else {
RF_BB_SPI_MOSI = 0;
}

recv |= RF_BB_SPI_MISO ? 1 : 0;

RF_BB_SPI_SCK = 1;

data = data >> 1;
recv = recv << 1;
}

return recv;
}
8 changes: 8 additions & 0 deletions src/platform/bb_spi.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#ifndef BB_SPI_H
#define BB_SPI_H

#include <stdint.h>

void bb_spi_xfer(uint8_t *data, int len);

#endif
237 changes: 237 additions & 0 deletions src/platform/bk3632/rf_controller.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,237 @@
#include "rf_controller.h"
#include <stdint.h>
#include "bb_spi.h" // FIXME: should be conditional?

#define MAGIC_BYTE 0xaa
#define CMD_REPORT 0x02

__xdata uint8_t rf_tx_buf[32];

void rf_send_blank_report();
// void rf_cmd_generic(uint8_t cmd, uint8_t *buf, uint8_t len);
void rf_cmd_01(uint8_t param1, uint8_t param2);
void rf_cmd_03(uint8_t param);
void rf_cmd_04();
void rf_cmd_06(uint8_t param);
void rf_cmd_08(uint8_t param1, uint8_t param2);
void rf_cmd_0a();
void rf_cmd_0c();
void rf_fetch_4();
uint8_t checksum(uint8_t *data, int len);

void rf_init()
{
rf_cmd_0c();
rf_keep_alive();
rf_cmd_08(0x00, 0x35);
rf_cmd_04();
rf_cmd_08(0x01, 0x33);
rf_cmd_04();
rf_cmd_01(0x00, 0x00);
rf_cmd_01(0x00, 0x00);
rf_send_blank_report();
}

void rf_send_report(report_keyboard_t *report)
{
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] = report->raw[0];
rf_tx_buf[4] = report->raw[2];
rf_tx_buf[5] = report->raw[3];
rf_tx_buf[6] = report->raw[4];
rf_tx_buf[7] = report->raw[5];
rf_tx_buf[8] = report->raw[6];
// 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 NKRO is implemented
rf_tx_buf[i] = 0x00;
}

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

bb_spi_xfer(rf_tx_buf, len);
}

void rf_send_blank_report()
{
const uint8_t len = 32;

rf_tx_buf[0] = MAGIC_BYTE;
rf_tx_buf[1] = len - 3;
rf_tx_buf[2] = CMD_REPORT;
for (int i = 3; i < 31; i++) {
rf_tx_buf[i] = 0x00;
}

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

bb_spi_xfer(rf_tx_buf, len);
}

void rf_keep_alive()
{
rf_cmd_0a();
rf_fetch_4();
}

void rf_fetch_4()
{
rf_tx_buf[0] = 0xff;
rf_tx_buf[1] = 0xff;
rf_tx_buf[2] = 0xff;
rf_tx_buf[3] = 0xff;

bb_spi_xfer(rf_tx_buf, 4);
}

void rf_cmd_01(uint8_t param1, uint8_t param2) // ??, 0x00 or 0x01
{
const uint8_t len = 6;

rf_tx_buf[0] = MAGIC_BYTE;
rf_tx_buf[1] = len - 3;
rf_tx_buf[2] = 0x01;
rf_tx_buf[3] = param1;
rf_tx_buf[4] = param2;
rf_tx_buf[len - 1] = checksum(rf_tx_buf, len - 1);

bb_spi_xfer(rf_tx_buf, 6);
}

void rf_cmd_03(uint8_t param) // ?? or 0x02
{
const uint8_t len = 6;

rf_tx_buf[0] = MAGIC_BYTE;
rf_tx_buf[1] = len - 3;
rf_tx_buf[2] = 0x03;
rf_tx_buf[3] = param;
rf_tx_buf[4] = 0x00;
rf_tx_buf[len - 1] = checksum(rf_tx_buf, len - 1);

bb_spi_xfer(rf_tx_buf, 6);
}

void rf_cmd_04()
{
const uint8_t len = 4;

rf_tx_buf[0] = MAGIC_BYTE;
rf_tx_buf[1] = len - 3;
rf_tx_buf[2] = 0x04;
rf_tx_buf[3] = checksum(rf_tx_buf, 3);

bb_spi_xfer(rf_tx_buf, 4);
}

void rf_cmd_06(uint8_t param) // 0x00 or 0x01
{
const uint8_t len = 6;

rf_tx_buf[0] = MAGIC_BYTE;
rf_tx_buf[1] = len - 3;
rf_tx_buf[2] = 0x06;
rf_tx_buf[3] = param;
rf_tx_buf[4] = 0x00;
rf_tx_buf[len - 1] = checksum(rf_tx_buf, len - 1);

bb_spi_xfer(rf_tx_buf, len);
}

void rf_cmd_08(uint8_t param1, uint8_t param2) // 0x00 0x01, 0x35 0x33
{
const uint8_t len = 32;

rf_tx_buf[0] = MAGIC_BYTE;
rf_tx_buf[1] = len - 3;
rf_tx_buf[2] = 0x08;
rf_tx_buf[3] = param1;
rf_tx_buf[4] = 0x0b;
rf_tx_buf[5] = 0x41;
rf_tx_buf[6] = 0x69;
rf_tx_buf[7] = 0x72;
rf_tx_buf[8] = 0x36;
rf_tx_buf[9] = 0x30;
rf_tx_buf[10] = 0x20;
rf_tx_buf[11] = 0x42;
rf_tx_buf[12] = 0x54;
rf_tx_buf[13] = param2;
rf_tx_buf[14] = 0x2e;
rf_tx_buf[15] = 0x30;
rf_tx_buf[16] = 0x00;
rf_tx_buf[17] = 0x00;
rf_tx_buf[18] = 0x00;
rf_tx_buf[19] = 0x00;
rf_tx_buf[20] = 0x00;
rf_tx_buf[21] = 0x00;
rf_tx_buf[22] = 0x00;
rf_tx_buf[23] = 0x00;
rf_tx_buf[24] = 0x00;
rf_tx_buf[25] = 0x00;
rf_tx_buf[26] = 0x00;
rf_tx_buf[27] = 0x00;
rf_tx_buf[28] = 0x00;
rf_tx_buf[29] = 0x00;
rf_tx_buf[30] = 0x00;
rf_tx_buf[len - 1] = checksum(rf_tx_buf, len - 1);

bb_spi_xfer(rf_tx_buf, len);
}

void rf_cmd_0a()
{
const uint8_t len = 6;

rf_tx_buf[0] = MAGIC_BYTE;
rf_tx_buf[1] = len - 1;
rf_tx_buf[2] = 0x0a;
rf_tx_buf[3] = 0x00;
rf_tx_buf[4] = 0x00;
rf_tx_buf[len - 1] = checksum(rf_tx_buf, len - 1);

bb_spi_xfer(rf_tx_buf, len);
}

void rf_cmd_0c()
{
const uint8_t len = 6;

rf_tx_buf[0] = MAGIC_BYTE;
rf_tx_buf[1] = len - 1;
rf_tx_buf[2] = 0x0c;
rf_tx_buf[3] = 0x00;
rf_tx_buf[4] = 0x00;
rf_tx_buf[len - 1] = checksum(rf_tx_buf, len - 1);

bb_spi_xfer(rf_tx_buf, len);
}

// void rf_cmd_generic(uint8_t cmd, uint8_t *buf, uint8_t len)
// {
// uint8_t total_len = len + 3;
// rf_tx_buf[0] = MAGIC_BYTE;
// rf_tx_buf[1] = len;
// int i;
// for (i = 0; i < len; i++) {
// rf_tx_buf[i + 2] = buf[i];
// }
// rf_tx_buf[i + 2] = checksum(rf_tx_buf, total_len);

// bb_spi_xfer(rf_tx_buf, total_len);
// }

uint8_t checksum(uint8_t *data, int len)
{
uint8_t checksum = 0;

for (int i = 0; i < len; i++) {
checksum += data[i];
}

return 0x55 - checksum;
}
10 changes: 10 additions & 0 deletions src/platform/bk3632/rf_controller.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef RF_CONTROLLER_H
#define RF_CONTROLLER_H

#include "report.h"

void rf_init();
void rf_send_report(report_keyboard_t *report);
void rf_keep_alive();

#endif
2 changes: 2 additions & 0 deletions src/smk/host.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#include "host.h"
#include "debug.h"
#include "usb.h"
#include "rf_controller.h"

/* send report */
void host_keyboard_send(report_keyboard_t *report)
{
usb_send_report(report);
rf_send_report(report);
}
Loading

0 comments on commit 9076a23

Please sign in to comment.