Skip to content

Commit

Permalink
USB support, SMK name, GPLv2 license, sh68f90a.h improvements
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 39efd4c
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Dec 2 10:26:41 2023 +0100

    improved led and other reports handler

commit cd0acfa
Author: Karolis Stasaitis <[email protected]>
Date:   Mon Nov 27 23:50:10 2023 +0100

    reg control macro separation

commit 2bda1c7
Author: Karolis Stasaitis <[email protected]>
Date:   Mon Nov 27 23:18:57 2023 +0100

    gplv2 license

commit 4d30f8e
Author: Karolis Stasaitis <[email protected]>
Date:   Mon Nov 27 23:08:14 2023 +0100

    cleanup of unused code

commit 1a5b735
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Nov 25 18:01:49 2023 +0100

    _SBUF

commit ebac878
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Nov 25 17:52:22 2023 +0100

    license and more register use

commit 4069a4e
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Nov 25 12:01:49 2023 +0100

    readme adjustments

commit 7b7e96d
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Nov 25 11:45:39 2023 +0100

    format

commit f59fc4e
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Nov 25 11:44:25 2023 +0100

    more cleanup

commit 69df71d
Author: Karolis Stasaitis <[email protected]>
Date:   Fri Nov 24 00:00:55 2023 +0100

    include self-powered status

commit 581bf67
Author: Karolis Stasaitis <[email protected]>
Date:   Thu Nov 23 23:28:54 2023 +0100

    updated init code

commit 797ddab
Author: Karolis Stasaitis <[email protected]>
Date:   Thu Nov 23 23:19:27 2023 +0100

    better declarations for usb buffers

commit 003c2df
Author: Karolis Stasaitis <[email protected]>
Date:   Wed Nov 22 22:56:03 2023 +0100

    format changes

commit 46d1424
Author: Karolis Stasaitis <[email protected]>
Date:   Wed Nov 22 22:55:04 2023 +0100

    more cleanup and reorganization

commit 758f79b
Author: Karolis Stasaitis <[email protected]>
Date:   Tue Nov 21 23:49:13 2023 +0100

    a bunch of duplicate define cleanup

commit b327ec4
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 19 17:43:53 2023 +0100

    new docs

commit 2f480e9
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 19 17:14:39 2023 +0100

    fixed USB_EP1_COM_BUFFER_ADDR

commit 6e70eca
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 19 14:27:44 2023 +0100

    remove buffer_addr usage and replace endpoint type mask

commit b09ab2e
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 19 14:20:25 2023 +0100

    removed global rx_data

commit 24314a8
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 19 12:55:37 2023 +0100

    formatting

commit 5ecd811
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 19 12:55:05 2023 +0100

    back to working state

commit 718584d
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 19 10:27:13 2023 +0100

    register bit flag defines

commit 472c5a1
Author: Karolis Stasaitis <[email protected]>
Date:   Mon Nov 13 00:08:30 2023 +0100

    a style cleanup

commit 99e67b6
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 23:57:00 2023 +0100

    cleanup

commit 81ec91f
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 23:51:54 2023 +0100

    formatting changes

commit 1c4f350
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 23:51:17 2023 +0100

    more refactoring

commit 6839957
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 21:41:29 2023 +0100

    removed request table and replaced with switches and added a lint command

commit be262d0
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 18:48:08 2023 +0100

    falke adjustments to get sdcc and astyle working properly

commit dff639f
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 17:27:37 2023 +0100

    more cleanup

commit 1e8c9a5
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 17:09:35 2023 +0100

    clock init optimization

commit bc98a30
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 16:34:45 2023 +0100

    uart update

commit 9826754
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 15:32:43 2023 +0100

    formatting changes

commit 8be7590
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 14:47:10 2023 +0100

    further refactoring

commit b672443
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 13:01:49 2023 +0100

    peaces of cleanup

commit 492994a
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 12:41:42 2023 +0100

    removed unused rx buffer

commit 810eb0f
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 12:36:42 2023 +0100

    refactoring request methods

commit cd0e698
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 12:07:07 2023 +0100

    refacotred stall

commit 2230e4a
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 11:55:48 2023 +0100

    removed uneeded printfs

commit 36d6f27
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 11:10:53 2023 +0100

    fixed all setup timeouts

commit abcdd86
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 10:25:48 2023 +0100

    more refactoring, but set_report seems to be slightly broken

commit 4e39fbe
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 00:19:55 2023 +0100

    formatting update

commit 79388dd
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 12 00:00:41 2023 +0100

    formatting changes

commit 2c770e5
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Nov 11 23:46:18 2023 +0100

    remove led mode

commit 6f915f2
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Nov 11 23:44:39 2023 +0100

    better HID definitions

commit c0858c6
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Nov 11 17:41:43 2023 +0100

    more defined hid descriptions

commit 4490e9a
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Nov 11 15:22:12 2023 +0100

    more desscriptor struct usage, back to working keyboard

commit 9c02ea2
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Nov 11 12:03:19 2023 +0100

    moved device descriptor to new structure

commit af6009c
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Nov 11 11:18:53 2023 +0100

    added structures from libfx2 and replace string handling with one of them

commit f90d130
Author: Karolis Stasaitis <[email protected]>
Date:   Tue Nov 7 23:40:29 2023 +0100

    first version of working layers

commit 97d9cd5
Author: Karolis Stasaitis <[email protected]>
Date:   Tue Nov 7 22:13:54 2023 +0100

    removed qmk action handling

commit ad07688
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 5 22:07:17 2023 +0100

    broken wip attempt to bring in more qmk code

commit c63ea4c
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 5 12:59:47 2023 +0100

    successfully sending all 6 keys and modifiers + some debug utils

commit f52ef39
Author: Karolis Stasaitis <[email protected]>
Date:   Sun Nov 5 10:20:48 2023 +0100

    github actions

commit bc68f30
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Nov 4 21:53:02 2023 +0100

    more cleanup

commit a74cb79
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Nov 4 20:31:52 2023 +0100

    small format changes

commit 67d6e1d
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Nov 4 16:55:35 2023 +0100

    reworked usb_request handler into struct

commit fdd1a23
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Nov 4 16:31:20 2023 +0100

    separation into usbdef

commit 80e697a
Author: Karolis Stasaitis <[email protected]>
Date:   Sat Nov 4 15:27:04 2023 +0100

    working usb stack
  • Loading branch information
carlossless committed Dec 2, 2023
1 parent c1e131f commit d65d1be
Show file tree
Hide file tree
Showing 41 changed files with 4,756 additions and 1,745 deletions.
19 changes: 19 additions & 0 deletions .astylerc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# @file Astyle code automatic formatting settings
# @see http://astyle.sourceforge.net/astyle.html#_General_Information

--style=otbs
--indent=spaces=4
--indent-switches
--indent-preproc-block
--indent-preproc-define
--indent-col1-comments
--break-blocks
--pad-oper
--pad-header
--align-pointer=name
--align-reference=name
--convert-tabs
--max-code-length=100
--break-after-logical
--mode=c
--lineend=linux
28 changes: 28 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

name: "Build"
on: [pull_request, push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v22
with:
github_access_token: ${{ secrets.GITHUB_TOKEN }}
- name: Build release
run: nix develop --command make all
- name: Archive code coverage results
uses: actions/upload-artifact@v3
with:
name: firmware.hex
path: bin/main.hex

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: cachix/install-nix-action@v22
with:
github_access_token: ${{ secrets.GITHUB_TOKEN }}
- name: Build release
run: nix develop --command bash -c "! astyle --project -n --dry-run src/*.{h,c} | grep '^Formatted'"
339 changes: 339 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

19 changes: 16 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,27 @@ FAMILY = mcs51
PROC = mcs51

FREQ_SYS ?= 24000000
XRAM_SIZE ?= 0x0400
XRAM_SIZE ?= 0x1000
XRAM_LOC ?= 0x0000
CODE_SIZE ?= 0xf000 # 61440 bytes (leaving the remaining 4096 for bootloader)

SMK_VERSION ?= alpha

# Ease backup & restore process by keeping same vid & pid as nuphy-air60
USB_VID ?= 0x05ac
USB_PID ?= 0x024f

CFLAGS := -V -mmcs51 --model-small \
--xram-size $(XRAM_SIZE) --xram-loc $(XRAM_LOC) \
--code-size $(CODE_SIZE) \
-I$(ROOT_DIR)../include -DFREQ_SYS=$(FREQ_SYS) -DWATCHDOG_ENABLE=1
--std-c2x \
-I$(ROOT_DIR)../include \
-DDEBUG=1 \
-DFREQ_SYS=$(FREQ_SYS) \
-DWATCHDOG_ENABLE=1 \
-DUSB_VID=$(USB_VID) \
-DUSB_PID=$(USB_PID) \
-DSMK_VERSION=$(SMK_VERSION)
LFLAGS := $(CFLAGS)

AFLAGS= -plosgff
Expand All @@ -35,7 +48,7 @@ clean:
rm -rf $(BINDIR) $(OBJDIR)

flash: $(BINDIR)/main.hex
$(FLASHER) $<
$(FLASHER) $(BINDIR)/main.hex

$(OBJDIR)/%.rel: $(SRCDIR)/%.c
@mkdir -p $(@D)
Expand Down
78 changes: 14 additions & 64 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,73 +1,23 @@
# SH68F90A / BYK916 / NuPhy Air60 Experiments
# SMK - Small (device) Mechanical Keyboard Firmware

A repository to test various features of the Sinowealth SH68F90A 8051-based microcontroller (also labeled as BYK916) on the [NuPhy Air60](https://nuphy.com/products/air60) keyboard.
This is a keyboard firmware similar to [QMK](https://github.com/qmk/qmk_firmware), but targeting 8051-based devices like the Sinowealth SH68F90A (labeled as BYK916). It's aimed to be at least partially compatible with QMK configurations.

## Features
The S (Small) in SMK comes from this firmware using [SDCC](https://sdcc.sourceforge.net/) to build itself.

### MCU
## ⚠️ WARNING ⚠️

- [x] 3.3V LDO
- [x] CPU Clock (use 24Mhz as SYSCLK)
- [x] GPIO
- [x] UART
- [x] Transmit
- [ ] Receive
- [x] PWM
- [ ] SPI
- [ ] USB
This firmware is still highly experimental, so be cautious when trying to use it or extend it.

### Keyboard
You can very easily end up with a bricked device if the written firmware can't jump back into ISP mode, so before testing or modifying it, it's best to have a full dump of your stock firmware and a tool (like the Sinolink) that can write it back.

- [x] ISP (jump)
- [x] Key Scan
- [x] RGB Matrix
- [ ] USB HID
- [ ] Wireless
## Supported Devices

## Code Options
| Keyboard | MCU | ISP | USB | Wireless | Details |
| -------- | --- | --- | --- | -------- | ------- |
| [NuPhy Air60 v1](https://nuphy.com/products/air60) | SH68F90A / BYK916 ||| TBD | [Details](docs/nuphy-air60.md) |

All code uses the following code options for SH68F90A
## Acknowledgements

```
Code Option String: A4E063C00F000088
Code Option Number: 0x8800000fc063e0a4
Byte 0 - A4
OP_OSCDRIVE 2 - 4MHz crystal or 8~12MHz crystal with external capacitance(C1=C2)<20pF
OP_RST 1 - P0.2 used as IO pin
OP_WMT 0 - longest warm up time
OP_SCMEN 1 - Disable SCM
OP_OSCRFB 0 - 150K
Byte 1 - E0
OP_LVREN 1 - Enable LVR function
OP_LVRLEVEL 3 - 2.1V LVR level4
OP_SCM 0 - SCM is invalid in warm up period
OP_OSC2SEL 0 - OSC2 select 12M RC
OP_IOV1 0 - P7.1/P7.2/P7.3/P7.4 input/output level is VUSB(5V)
OP_IOV0 0 - P5.5/P5.6 input/output level is VUSB(5V)
Byte 2 - 63
OP_SCMSEL 3 - 8MHz
OP_OSC 3 - Oscillator1 is internal 128KHz RC, oscillator2 is internal 24MHz RC
Byte 3 - C0
OP_ISP 1 - Disable ISP function
OP_ISPPIN 1 - Enter ISP mode only when P6.3 and P6.4 are connected to GND, simultaneously
OP_WDT 0 - Enable WDT function
OP_WDTPD 0 - Disable WDT function in Power-Down mode
Byte 4 - 0F
OP_SINK1 3 - Port6[5:0] sink ability largest mode(380mA)
OP_SINK0 3 - P4.7/Port7[7:5] sink ability largest mode(50mA)
Byte 5 - 00
OP_BOPTP 0 - (1+21%)tr min
OP_BOPTN 0 - (1+21%)tf min
Byte 6 - 00
Unused
Byte 7 - 88
OP_ISPSIZE 8 - 0Bytes
```
* [libfx2](https://github.com/whitequark/libfx2)
* [LUFA](https://github.com/abcminiuser/lufa)
* [QMK](https://github.com/qmk/qmk_firmware)
76 changes: 76 additions & 0 deletions docs/nuphy-air60.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# NuPhy Air60 Support

## MCU

- [x] 3.3V LDO
- [x] CPU Clock (use 24Mhz as SYSCLK)
- [x] GPIO
- [x] UART
- [x] Transmit
- [ ] Receive
- [x] PWM
- [x] USB
- [ ] SPI

## Keyboard

- [x] ISP (jump)
- [x] Key Scan
- [x] RGB Matrix
- [ ] Debouncing
- [x] USB HID
- [x] USB Keyboard
- [ ] Status LEDs
- [ ] Extra Keys (System / Consumer)
- [ ] NKRO
- [~] QMK Compatibility
- [~] Layouts
- [ ] Wireless

## Code Options

This firmware requires the following (stock) code options for SH68F90A

```
Code Option String: A4E063C00F000088
Code Option Number: 0x8800000fc063e0a4
Byte 0 - A4
OP_OSCDRIVE 2 - 4MHz crystal or 8~12MHz crystal with external capacitance(C1=C2)<20pF
OP_RST 1 - P0.2 used as IO pin
OP_WMT 0 - longest warm up time
OP_SCMEN 1 - Disable SCM
OP_OSCRFB 0 - 150K
Byte 1 - E0
OP_LVREN 1 - Enable LVR function
OP_LVRLEVEL 3 - 2.1V LVR level4
OP_SCM 0 - SCM is invalid in warm up period
OP_OSC2SEL 0 - OSC2 select 12M RC
OP_IOV1 0 - P7.1/P7.2/P7.3/P7.4 input/output level is VUSB(5V)
OP_IOV0 0 - P5.5/P5.6 input/output level is VUSB(5V)
Byte 2 - 63
OP_SCMSEL 3 - 8MHz
OP_OSC 3 - Oscillator1 is internal 128KHz RC, oscillator2 is internal 24MHz RC
Byte 3 - C0
OP_ISP 1 - Disable ISP function
OP_ISPPIN 1 - Enter ISP mode only when P6.3 and P6.4 are connected to GND, simultaneously
OP_WDT 0 - Enable WDT function
OP_WDTPD 0 - Disable WDT function in Power-Down mode
Byte 4 - 0F
OP_SINK1 3 - Port6[5:0] sink ability largest mode(380mA)
OP_SINK0 3 - P4.7/Port7[7:5] sink ability largest mode(50mA)
Byte 5 - 00
OP_BOPTP 0 - (1+21%)tr min
OP_BOPTN 0 - (1+21%)tf min
Byte 6 - 00
Unused
Byte 7 - 88
OP_ISPSIZE 8 - 0Bytes
```
19 changes: 10 additions & 9 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 21 additions & 16 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.05";
nixpkgs.url = "github:carlossless/nixpkgs/fixups"; # latest patches to get astyle 4.3.10 & sdcc 4.3.0 working
utils.url = "github:numtide/flake-utils";
sinowealth-kb-tool.url = "github:carlossless/sinowealth-kb-tool";
sinowealth-kb-tool.url = "github:carlossless/sinowealth-kb-tool/noodle";
};

outputs = { self, nixpkgs, utils, sinowealth-kb-tool }:
utils.lib.eachDefaultSystem (
system: let pkgs = nixpkgs.legacyPackages.${system}; in
{
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
sdcc
gnumake
binutils
astyle
sinowealth-kb-tool.packages."${system}".default
uhubctl
];
};
}
);
system:
let
pkgs = import nixpkgs {
inherit system;
};
in
{
devShells.default = pkgs.mkShell {
buildInputs = with pkgs; [
sdcc
gnumake
binutils
astyle
sinowealth-kb-tool.packages."${system}".default
uhubctl
];
};
}
);
}
30 changes: 13 additions & 17 deletions src/clock.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
#include "clock.h"
#include "sh68f90a.h"
#include "delay.h"
#include "watchdog.h"

#define _SYSCLK_DIV 0 // no SYSCLK division (CLKS)
#define _HRCCLK_EN (1 << 3) // enable HRCCLK (HFON)
#define _CLK_SRC (1 << 2) // use OSCSCLK as SYSCLK (FS)

// PLL also necessary for USB peripheral
#define _PLL_EN (1 << 1) // enables PLL, necessary for HRCCLK (PLLON)
#define _OSCS_SRC (1 << 0) // PLL divided by two acts as OSCSCLK (PLLFS)

#define CLKCON_INIT (_SYSCLK_DIV|_HRCCLK_EN|_CLK_SRC)
#define PLLCON_INIT (_PLL_EN|_OSCS_SRC)

/** \brief sets up HRCCLK and uses it as SYSCLK
*
*/
void clock_init()
{
CLKCON = (CLKCON_INIT & _HRCCLK_EN); // init HRCCLK
delay_us(350); // unsure about span and if necessary
PLLCON = (PLLCON_INIT & _PLL_EN); // init PLL
delay_us(533); // unsure about span and if necessary
PLLCON = PLLCON_INIT; // switch OSCSCLK
CLKCON = CLKCON_INIT; // use HRCCLK as SYSCLK
CLKCON = _HFON; // enable HRCCLK
PLLCON = _PLLON; // init PLL

while (!(PLLCON & _PLLSTA)) { // wait for PLL to lock phase
CLR_WDT();
}

PLLCON |= _PLLFS; // switch OSCSCLK
CLKCON |= _FS; // use HRCCLK as SYSCLK
}
4 changes: 2 additions & 2 deletions src/clock.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifndef _CLOCK_H_
#define _CLOCK_H_
#ifndef CLOCK_H
#define CLOCK_H

void clock_init();

Expand Down
9 changes: 9 additions & 0 deletions src/debug.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef DEBUG_H
#define DEBUG_H

#include <stdio.h>

#define dprintf(...) \
do { if (DEBUG) printf(__VA_ARGS__); } while (0)

#endif
Loading

0 comments on commit d65d1be

Please sign in to comment.