Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

royalkludge-rk61 device support #15

Draft
wants to merge 11 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ You can very easily end up with a bricked device if the written firmware can't j
| -------- | --- | --- | --- | -------- | ------- |
| [NuPhy Air60 v1](https://nuphy.com/products/air60) | SH68F90A / BYK916 | ✅ | ✅ | TBD | [Details](docs/keyboards/nuphy-air60.md) |
| E-YOOSO Z11 | SH68F90A / BYK901 | ✅ | ✅ | N/A | [Details](docs/keyboards/nuphy-air60.md) |
| Royal Kludge RK61 | SH68F90? / BYK901 | ✅ | ✅ | N/A | [Details](docs/keyboards/royalkludge-rk61.md) |

## Developing

Expand Down
17 changes: 17 additions & 0 deletions docs/keyboards/royalkludge-rk61.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Royal Kludge RK61

**Warning:** Be aware that there are several versions of the RK61 keyboard, each containing distinct internal components. This SMK firmware has been successfully tested on a wired 2022 model (with the date stamped on the board: 2022/08/16) and is likely to not function correctly on other variations. If you opt to install this firmware on your RK61, it is highly recommended to create a backup of your original firmware using the sinowealth-kb-tool [available here](https://github.com/carlossless/sinowealth-kb-tool) first. Keep in mind that you should know how to revert to the original firmware using sinowealth-kb-tool in case the SMK firmware does not work as intended. We disclaim any responsibility for any damage caused while trying this firmware or using the sinowealth-kb-tool.

## Keyboard Specs

- MCU: BYK916 (SH68F90?)
- Backlight: RGB LEDs
- Indicators: none
- Switches: none
- Wireless: wired only version
- Wireless: none, wired only version

## SMK Supported Features

- [x] Key Scan
- [ ] RGB Matrix
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ parts = [
['example', 'sh68f90a', ['default'], {}],
['nuphy-air60', 'sh68f90a', ['default'], { 'vendor_id': '0x05ac', 'product_id': '0x024f' }],
['eyooso-z11', 'sh68f90a', ['default'], { 'vendor_id': '0x258a', 'product_id': '0x002a' }],
['royalkludge-rk61', 'sh68f90a', ['default'], { 'vendor_id': '0x258a', 'product_id': '0x00c7' }],
]

# SMK
Expand Down
93 changes: 93 additions & 0 deletions src/keyboards/royalkludge-rk61/kbdef.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#ifndef KBDEF_H
#define KBDEF_H

#include "sh68f90a.h"

#define MATRIX_ROWS 5
#define MATRIX_COLS 14

// Row Pins
#define KB_R0 P7_1
#define KB_R1 P7_2
#define KB_R2 P7_3
#define KB_R3 P5_3
#define KB_R4 P5_4

// Column Pins
#define KB_C0 P6_0
#define KB_C1 P6_1
#define KB_C2 P6_2
#define KB_C3 P6_3
#define KB_C4 P6_4
#define KB_C5 P6_5
#define KB_C6 P6_6
#define KB_C7 P6_7
#define KB_C8 P5_0
#define KB_C9 P5_1
#define KB_C10 P5_2
#define KB_C11 P5_7
#define KB_C12 P4_0
#define KB_C13 P4_2

// ATTENTION: LED pins have not been traced.
// All code related to led handling doesn't do harm and serve as reference for future work on making the leds work.

// LED PWM Registers
#define LED_PWM_C0 PWM40
#define LED_PWM_C1 PWM41
#define LED_PWM_C2 PWM42
#define LED_PWM_C3 PWM05
#define LED_PWM_C4 PWM04
#define LED_PWM_C5 PWM03
#define LED_PWM_C6 PWM02
#define LED_PWM_C7 PWM01
#define LED_PWM_C8 PWM00
#define LED_PWM_C9 PWM15
#define LED_PWM_C10 PWM14
#define LED_PWM_C11 PWM13
#define LED_PWM_C12 PWM12
#define LED_PWM_C13 PWM11
#define LED_PWM_C14 PWM10
#define LED_PWM_C15 PWM25

// RGB Row Pins
#define RGB_R0R P0_4
#define RGB_R0G P6_1
#define RGB_R0B P0_3
#define RGB_R1R P6_7
#define RGB_R1G P6_2
#define RGB_R1B P6_6
#define RGB_R2R P0_2
#define RGB_R2G P6_3
#define RGB_R2B P5_7
#define RGB_R3R P4_5
#define RGB_R3G P6_4
#define RGB_R3B P4_6
#define RGB_R4R P4_4
#define RGB_R4G P6_5
#define RGB_R4B P4_3
#define RGB_ULR P1_1
#define RGB_ULG P1_2
#define RGB_ULB P1_3

// RGB Row Pin Bits
#define RGB_R0R_P0_4 _P0_4
#define RGB_R0G_P6_1 _P6_1
#define RGB_R0B_P0_3 _P0_3
#define RGB_R1R_P6_7 _P6_7
#define RGB_R1G_P6_2 _P6_2
#define RGB_R1B_P6_6 _P6_6
#define RGB_R2R_P0_2 _P0_2
#define RGB_R2G_P6_3 _P6_3
#define RGB_R2B_P5_7 _P5_7
#define RGB_R3R_P4_5 _P4_5
#define RGB_R3G_P6_4 _P6_4
#define RGB_R3B_P4_6 _P4_6
#define RGB_R4R_P4_4 _P4_4
#define RGB_R4G_P6_5 _P6_5
#define RGB_R4B_P4_3 _P4_3
#define RGB_ULR_P1_1 _P1_1
#define RGB_ULG_P1_2 _P1_2
#define RGB_ULB_P1_3 _P1_3

#endif
207 changes: 207 additions & 0 deletions src/keyboards/royalkludge-rk61/layouts/default/indicators.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
#include "indicators.h"
#include "kbdef.h"
#include "pwm.h"
#include <stdlib.h>

// TODO: move these defines out
#define PWM_CLK_DIV 0b010 // PWM_CLK = SYS_CLK / 4
#define PWM_SS_BIT (1 << 3)
#define PWM_MOD_BIT (1 << 4)
#define PWM_INT_ENABLE_BIT (1 << 6)
#define PWM_MODE_ENABLE_BIT (1 << 7)

void indicators_pwm_set_all_columns(uint16_t intensity);
void indicators_pwm_enable();
void indicators_pwm_disable();

void indicators_start()
{
indicators_pwm_enable();
}

void indicators_pre_update()
{
// set all rgb sinks to low (animation step will enable needed ones)
P0 &= ~(RGB_R2R_P0_2 | RGB_R0B_P0_3 | RGB_R0R_P0_4);
P1 &= ~(RGB_ULR_P1_1 | RGB_ULG_P1_2 | RGB_ULB_P1_3);
P4 &= ~(RGB_R4B_P4_3 | RGB_R4R_P4_4 | RGB_R3R_P4_5 | RGB_R3B_P4_6);
P5 &= ~(RGB_R2B_P5_7);
P6 &= ~(RGB_R0G_P6_1 | RGB_R1G_P6_2 | RGB_R2G_P6_3 | RGB_R3G_P6_4 | RGB_R4G_P6_5 | RGB_R1B_P6_6 | RGB_R1R_P6_7);

indicators_pwm_disable();
}

bool indicators_update_step(keyboard_state_t *keyboard, uint8_t current_step)
{
keyboard;
current_step;
indicators_pwm_set_all_columns(0);

/*
static uint16_t current_cycle = 0;

if (current_step == 0) {
if (current_cycle < 3072) {
current_cycle++;
} else {
current_cycle = 0;
}
}

uint16_t red_intensity = 0;
uint16_t green_intensity = 0;
uint16_t blue_intensity = 0;

if (current_cycle < 1024) {
blue_intensity = 1024 - (uint16_t)abs((int16_t)((current_cycle + 1024) % 2048) - 1024);
red_intensity = 1024 - (uint16_t)abs((int16_t)((current_cycle) % 2048) - 1024);
} else if (current_cycle < 2048) {
red_intensity = 1024 - (uint16_t)abs((int16_t)((current_cycle) % 2048) - 1024);
green_intensity = 1024 - (uint16_t)abs((int16_t)((current_cycle + 1024) % 2048) - 1024);
} else {
green_intensity = 1024 - (uint16_t)abs((int16_t)((current_cycle + 1024) % 2048) - 1024);
blue_intensity = 1024 - (uint16_t)abs((int16_t)((current_cycle) % 2048) - 1024);
}

uint16_t color_intensity;

if (keyboard->led_state & (1 << 0)) { // num_lock
red_intensity = 1024;
}

if (keyboard->led_state & (1 << 1)) { // caps_lock
green_intensity = 1024;
}

if (keyboard->led_state & (1 << 2)) { // scroll_lock
blue_intensity = 1024;
}

switch (current_step % 3) {
case 0: // red
RGB_R0R = 1;
RGB_R1R = 1;
RGB_R2R = 1;
RGB_R3R = 1;
RGB_R4R = 1;
RGB_ULR = 1;

color_intensity = red_intensity;
break;

case 1: // green
RGB_R0G = 1;
RGB_R1G = 1;
RGB_R2G = 1;
RGB_R3G = 1;
RGB_R4G = 1;
RGB_ULG = 1;

color_intensity = green_intensity;
break;

case 2: // blue
RGB_R0B = 1;
RGB_R1B = 1;
RGB_R2B = 1;
RGB_R3B = 1;
RGB_R4B = 1;
RGB_ULB = 1;

color_intensity = blue_intensity;
break;

default:
// unreachable
color_intensity = 0;
break;
}

// set pwm duty cycles to expected colors
indicators_pwm_set_all_columns(color_intensity);
*/

return false;
}

void indicators_post_update()
{
// clear pwm isr flag
PWM00CON &= ~(1 << 5);

indicators_pwm_enable();
}

void indicators_pwm_set_all_columns(uint16_t intensity)
{
uint16_t adjusted = 0x0400 - intensity;

SET_PWM_DUTY_2(LED_PWM_C0, adjusted);
SET_PWM_DUTY_2(LED_PWM_C1, adjusted);
SET_PWM_DUTY_2(LED_PWM_C2, adjusted);
SET_PWM_DUTY_2(LED_PWM_C3, adjusted);
SET_PWM_DUTY_2(LED_PWM_C4, adjusted);
SET_PWM_DUTY_2(LED_PWM_C5, adjusted);
SET_PWM_DUTY_2(LED_PWM_C6, adjusted);
SET_PWM_DUTY_2(LED_PWM_C7, adjusted);
SET_PWM_DUTY_2(LED_PWM_C8, adjusted);
SET_PWM_DUTY_2(LED_PWM_C9, adjusted);
SET_PWM_DUTY_2(LED_PWM_C10, adjusted);
SET_PWM_DUTY_2(LED_PWM_C11, adjusted);
SET_PWM_DUTY_2(LED_PWM_C12, adjusted);
SET_PWM_DUTY_2(LED_PWM_C13, adjusted);
SET_PWM_DUTY_2(LED_PWM_C14, adjusted);
SET_PWM_DUTY_2(LED_PWM_C15, adjusted);
}

void indicators_pwm_enable()
{
// TODO: try abstracting individual banks away
PWM00CON = (uint8_t)(PWM_MODE_ENABLE_BIT | PWM_INT_ENABLE_BIT | PWM_SS_BIT | PWM_CLK_DIV);
PWM01CON = PWM_SS_BIT;
PWM02CON = PWM_SS_BIT;
PWM03CON = PWM_SS_BIT;
PWM04CON = PWM_SS_BIT;
PWM05CON = PWM_SS_BIT;

PWM10CON = (uint8_t)(PWM_MODE_ENABLE_BIT | PWM_SS_BIT | PWM_CLK_DIV);
PWM11CON = PWM_SS_BIT;
PWM12CON = PWM_SS_BIT;
PWM13CON = PWM_SS_BIT;
PWM14CON = PWM_SS_BIT;
PWM15CON = PWM_SS_BIT;

PWM20CON = (uint8_t)(PWM_MODE_ENABLE_BIT | PWM_SS_BIT | PWM_CLK_DIV);
// PWM24CON = PWM_SS_BIT;
PWM25CON = PWM_SS_BIT;

PWM40CON = (uint8_t)(PWM_MODE_ENABLE_BIT | PWM_SS_BIT | PWM_CLK_DIV);
PWM41CON = PWM_SS_BIT;
PWM42CON = PWM_SS_BIT;
}

void indicators_pwm_disable()
{
// TODO: try abstracting individual banks away
PWM00CON = (uint8_t)(PWM_CLK_DIV);
PWM01CON = 0;
PWM02CON = 0;
PWM03CON = 0;
PWM04CON = 0;
PWM05CON = 0;

PWM10CON = (uint8_t)(PWM_CLK_DIV);
PWM11CON = 0;
PWM12CON = 0;
PWM13CON = 0;
PWM14CON = 0;
PWM15CON = 0;

PWM20CON = (uint8_t)(PWM_CLK_DIV);
// PWM24CON = 0;
PWM25CON = 0;

PWM40CON = (uint8_t)(PWM_CLK_DIV);
PWM41CON = 0;
PWM42CON = 0;
}
Loading