diff --git a/keyboards/framework/laptop16/ansi/config.h b/keyboards/framework/laptop16/ansi/config.h new file mode 100644 index 00000000000..3ae475ae775 --- /dev/null +++ b/keyboards/framework/laptop16/ansi/config.h @@ -0,0 +1,7 @@ +// Copyright 2022-2023 Framework Computer +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#define MATRIX_COLS 16 +#define MATRIX_ROWS 8 diff --git a/keyboards/framework/laptop16/ansi/keyboard.json b/keyboards/framework/laptop16/ansi/keyboard.json new file mode 100644 index 00000000000..4a03422a0e9 --- /dev/null +++ b/keyboards/framework/laptop16/ansi/keyboard.json @@ -0,0 +1,113 @@ +{ + + "keyboard_name": "Laptop 16 Keyboard Module - ANSI", + "backlight": { + "breathing": true, + "driver": "pwm", + "pin": "GP25" + }, + "features": { + "audio": false, + "bootmagic": false, + "command": false, + "console": true, + "extrakey": true, + "mousekey": false, + "backlight": true + }, + + + "indicators": { + "caps_lock": "GP24" + }, + "usb": { + "device_version": "0.2.2", + "force_nkro": true, + "pid": "0x0012", + "vid": "0x32AC" + }, + "layouts": { + "LAYOUT": { + "layout": [ + {"label": "Esc", "matrix": [7, 5], "x": 0, "y": 0, "w": 1.25, "h": 0.5}, + {"label": "F1", "matrix": [3, 5], "x": 1.5, "y": 0, "h": 0.5}, + {"label": "F2", "matrix": [2, 5], "x": 2.75, "y": 0, "h": 0.5}, + {"label": "F3", "matrix": [6, 4], "x": 4, "y": 0, "h": 0.5}, + {"label": "F4", "matrix": [3, 4], "x": 5.25, "y": 0, "h": 0.5}, + {"label": "F5", "matrix": [4, 10], "x": 6.5, "y": 0, "h": 0.5}, + {"label": "F6", "matrix": [3, 10], "x": 7.75, "y": 0, "h": 0.5}, + {"label": "F7", "matrix": [2, 10], "x": 9, "y": 0, "h": 0.5}, + {"label": "F8", "matrix": [1, 15], "x": 10.25, "y": 0, "h": 0.5}, + {"label": "F9", "matrix": [3, 11], "x": 11.5, "y": 0, "h": 0.5}, + {"label": "F10", "matrix": [4, 8], "x": 12.75, "y": 0, "h": 0.5}, + {"label": "F11", "matrix": [6, 8], "x": 14, "y": 0, "h": 0.5}, + {"label": "F12", "matrix": [3, 13], "x": 15.25, "y": 0, "h": 0.5}, + {"label": "Delete", "matrix": [0, 1], "x": 16.5, "y": 0, "w": 1.75, "h": 0.5}, + {"label": "~", "matrix": [4, 2], "x": 0, "y": 0.75}, + {"label": "!", "matrix": [5, 2], "x": 1.25, "y": 0.75}, + {"label": "@", "matrix": [5, 5], "x": 2.5, "y": 0.75}, + {"label": "#", "matrix": [5, 4], "x": 3.75, "y": 0.75}, + {"label": "$", "matrix": [5, 6], "x": 5, "y": 0.75}, + {"label": "%", "matrix": [4, 6], "x": 6.25, "y": 0.75}, + {"label": "^", "matrix": [4, 7], "x": 7.5, "y": 0.75}, + {"label": "&", "matrix": [5, 7], "x": 8.75, "y": 0.75}, + {"label": "*", "matrix": [5, 10], "x": 10, "y": 0.75}, + {"label": "(", "matrix": [5, 8], "x": 11.25, "y": 0.75}, + {"label": ")", "matrix": [4, 13], "x": 12.5, "y": 0.75}, + {"label": "_", "matrix": [2, 13], "x": 13.75, "y": 0.75}, + {"label": "+", "matrix": [4, 14], "x": 15, "y": 0.75}, + {"label": "Backspace", "matrix": [5, 14], "x": 16.25, "y": 0.75, "w": 2}, + {"label": "Tab", "matrix": [3, 2], "x": 0, "y": 2, "w": 1.5}, + {"label": "Q", "matrix": [0, 2], "x": 1.75, "y": 2}, + {"label": "W", "matrix": [6, 5], "x": 3, "y": 2}, + {"label": "E", "matrix": [2, 4], "x": 4.25, "y": 2}, + {"label": "R", "matrix": [6, 6], "x": 5.5, "y": 2}, + {"label": "T", "matrix": [3, 6], "x": 6.75, "y": 2}, + {"label": "Y", "matrix": [3, 7], "x": 8, "y": 2}, + {"label": "U", "matrix": [6, 7], "x": 9.25, "y": 2}, + {"label": "I", "matrix": [6, 10], "x": 10.5, "y": 2}, + {"label": "O", "matrix": [3, 8], "x": 11.75, "y": 2}, + {"label": "P", "matrix": [5, 13], "x": 13, "y": 2}, + {"label": "{", "matrix": [6, 13], "x": 14.25, "y": 2}, + {"label": "}", "matrix": [6, 14], "x": 15.5, "y": 2}, + {"label": "|", "matrix": [2, 8], "x": 16.75, "y": 2, "w": 1.5}, + {"label": "Caps Lock", "matrix": [4, 4], "x": 0, "y": 3.25, "w": 1.75}, + {"label": "A", "matrix": [7, 2], "x": 2, "y": 3.25}, + {"label": "S", "matrix": [4, 5], "x": 3.25, "y": 3.25}, + {"label": "D", "matrix": [7, 14], "x": 4.5, "y": 3.25}, + {"label": "F", "matrix": [7, 6], "x": 5.75, "y": 3.25}, + {"label": "G", "matrix": [2, 6], "x": 7, "y": 3.25}, + {"label": "H", "matrix": [2, 7], "x": 8.25, "y": 3.25}, + {"label": "J", "matrix": [7, 7], "x": 9.5, "y": 3.25}, + {"label": "K", "matrix": [7, 10], "x": 10.75, "y": 3.25}, + {"label": "L", "matrix": [7, 8], "x": 12, "y": 3.25}, + {"label": ":", "matrix": [7, 13], "x": 13.25, "y": 3.25}, + {"label": "\"", "matrix": [0, 14], "x": 14.5, "y": 3.25}, + {"label": "Enter", "matrix": [1, 14], "x": 15.75, "y": 3.25, "w": 2.5}, + {"label": "Shift", "matrix": [1, 9], "x": 0, "y": 4.5, "w": 2.5}, + {"label": "Z", "matrix": [1, 5], "x": 2.75, "y": 4.5}, + {"label": "X", "matrix": [0, 5], "x": 4, "y": 4.5}, + {"label": "C", "matrix": [0, 0], "x": 5.25, "y": 4.5}, + {"label": "V", "matrix": [0, 6], "x": 6.5, "y": 4.5}, + {"label": "B", "matrix": [1, 6], "x": 7.75, "y": 4.5}, + {"label": "N", "matrix": [1, 7], "x": 9, "y": 4.5}, + {"label": "M", "matrix": [0, 7], "x": 10.25, "y": 4.5}, + {"label": "<", "matrix": [0, 10], "x": 11.5, "y": 4.5}, + {"label": ">", "matrix": [0, 8], "x": 12.75, "y": 4.5}, + {"label": "?", "matrix": [0, 13], "x": 14, "y": 4.5}, + {"label": "Shift", "matrix": [0, 9], "x": 15.25, "y": 4.5, "w": 3}, + {"label": "Ctrl", "matrix": [1, 12], "x": 0, "y": 5.75, "w": 1.25}, + {"label": "Fn", "matrix": [2, 2], "x": 1.5, "y": 5.75}, + {"label": "Win", "matrix": [3, 1], "x": 2.75, "y": 5.75}, + {"label": "Alt", "matrix": [1, 3], "x": 4, "y": 5.75}, + {"label": "Space", "matrix": [1, 4], "x": 5.25, "y": 5.75, "w": 6}, + {"label": "Alt", "matrix": [0, 3], "x": 11.5, "y": 5.75}, + {"label": "Ctrl", "matrix": [0, 12], "x": 12.75, "y": 5.75}, + {"label": "\u2190", "matrix": [6, 11], "x": 14, "y": 5.75, "w": 1.25}, + {"label": "\u2191", "matrix": [1, 13], "x": 15.5, "y": 5.75, "w": 1.25, "h": 0.5}, + {"label": "\u2192", "matrix": [1, 8], "x": 17, "y": 5.75, "w": 1.25}, + {"label": "\u2193", "matrix": [2, 15], "x": 15.5, "y": 6.25, "w": 1.25, "h": 0.5} + ] + } + } +} diff --git a/keyboards/framework/laptop16/ansi/keymaps/default/keymap.c b/keyboards/framework/laptop16/ansi/keymaps/default/keymap.c new file mode 100644 index 00000000000..b7fa55107d3 --- /dev/null +++ b/keyboards/framework/laptop16/ansi/keymaps/default/keymap.c @@ -0,0 +1,108 @@ +// Copyright 2022-2023 Framework Computer +// SPDX-License-Identifier: GPL-2.0-or-later + +#include QMK_KEYBOARD_H +#include "framework.h" + +// clang-format off +enum _layers { + _BASE, + _FN, + _FN_LOCK, + _FM +}; + +const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { + /* + * ┌─────┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬────┐ + * 14 keys │Esc │F1 │F2 │F3 │F4 │F5 │F6 │F7 │F8 │F9 │F10│F11│F12│ Del│ + * ├───┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────┤ + * 14 keys │ ` │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 │ 7 │ 8 │ 9 │ 0 │ - │ = │Backsp│ + * ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬────┤ + * 14 keys │ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │ [ │ ] │ \ │ + * ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴────┤ + * 13 keys │ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │ ; │ ' │ Enter │ + * ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴───────┤ + * 12 keys │ Shift │ Z │ X │ C │ V │ B │ N │ M │ , │ . │ / │ Shift │ + * ├────┬───┼───┼───┼───┴───┴───┴───┴───┼───┼───┼───┴┬───┬────┤ + * │ │ │ │ │ │ │ │ │↑ │ │ + * 11 keys │Ctrl│FN │GUI│Alt│ │Alt│Ctl│ ← ├───┤ → │ + * │ │ │ │ │ │ │ │ │ ↓│ │ + * └────┴───┴───┴───┴───────────────────┴───┴───┴────┴───┴────┘ + * 78 total + */ + [_BASE] = LAYOUT( + KC_ESC, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_DEL, + KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, KC_BSPC, + KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS, + KC_CAPS, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, + KC_LSFT, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, KC_RSFT, + KC_LCTL, MO(_FN), KC_LGUI, KC_LALT, KC_SPC, KC_RALT, KC_RCTL, KC_LEFT, KC_UP, KC_DOWN, KC_RGHT + ), + /* + * Function layer + * ┌─────┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬────┐ + * 14 keys │FN lk│Mut│vDn│vUp│Prv│Ply│Nxt│bDn│bUp│Scn│Air│Prt│App│Ins │ + * ├───┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────┤ + * 14 keys │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ + * ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬────┤ + * 14 keys │ │ │RGB│Nxt│Hue│Sat│Spd│Brt│ │ │Pau│ │ │ │ + * ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴────┤ + * 13 keys │ │ │SRq│Prv│Hue│Sat│Spd│Brt│ScL│ │ │ │ │ + * ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴───────┤ + * 12 keys │ │ │ │ │ │ │Brk│ │ │ │ │ │ + * ├────┬───┼───┼───┼───┴───┴───┴───┴───┼───┼───┼───┴┬───┬────┤ + * │ │ │ │ │ │ │ │ │PgU│ │ + * 11 keys │ │ │ │ │ Toggle Backlight │ │ │Home├───┤End │ + * │ │ │ │ │ │ │ │ │PgD│ │ + * └────┴───┴───┴───┴───────────────────┴───┴───┴────┴───┴────┘ + * 78 total + */ + [_FN] = LAYOUT( + FN_LOCK, KC_MUTE, KC_VOLD, KC_VOLU, KC_MPRV, KC_MPLY, KC_MNXT, KC_BRID, KC_BRIU, G(KC_P), XXXXXXX, KC_PSCR, KC_MSEL, KC_INS, + _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, + _______, _______, RGB_TOG, RGB_MOD, RGB_HUI, RGB_SAI, RGB_SPI, RGB_VAI, _______, _______, KC_PAUS, _______, _______, _______, + _______, _______, KC_SYRQ, RGB_RMOD,RGB_HUD, RGB_SAD, RGB_SPD, RGB_VAD, KC_SCRL, _______, _______, _______, _______, + _______, _______, _______, BL_BRTG, _______, KC_BRK, _______, _______, _______, _______, _______, _______, + _______, _______, _______, _______, BL_STEP, _______, _______, KC_HOME, KC_PGUP, KC_PGDN, KC_END + ), + // Function lock layer + // Everything on F-row locked to function layer, except ESC and DEL + [_FN_LOCK] = LAYOUT( + _______, KC_MUTE, KC_VOLD, KC_VOLU, KC_MPRV, KC_MPLY, KC_MNXT, KC_BRID, KC_BRIU, G(KC_P), XXXXXXX, KC_PSCR, KC_MSEL, _______, + _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, + _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, + _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, + _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, + _______, MO(_FM), _______, _______, _______, _______, _______, _______, _______, _______, _______ + ), + [_FM] = LAYOUT( + FN_LOCK, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_INS, + _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, + _______, _______, RGB_TOG, RGB_MOD, RGB_HUI, RGB_SAI, RGB_SPI, RGB_VAI, _______, _______, KC_PAUS, _______, _______, _______, + _______, _______, KC_SYRQ, RGB_RMOD,RGB_HUD, RGB_SAD, RGB_SPD, RGB_VAD, KC_SCRL, _______, _______, _______, _______, + _______, _______, _______, BL_BRTG, _______, KC_BRK, _______, _______, _______, _______, _______, _______, + _______, _______, _______, _______, BL_STEP, _______, _______, KC_HOME, KC_PGUP, KC_PGDN, KC_END + ), +}; +// clang-format on + +// Make sure to keep FN Lock even after reset +bool process_record_user(uint16_t keycode, keyrecord_t *record) { + switch (keycode) { + case FN_LOCK: + if (record->event.pressed) { + if (layer_state_is(_FN)) { + set_single_persistent_default_layer(_FN_LOCK); + } + if (layer_state_is(_FM)) { + set_single_persistent_default_layer(_BASE); + } + } + return false; + break; + default: + break; + } + return true; +} diff --git a/keyboards/framework/laptop16/ansi/rules.mk b/keyboards/framework/laptop16/ansi/rules.mk new file mode 100644 index 00000000000..add363f4e2a --- /dev/null +++ b/keyboards/framework/laptop16/ansi/rules.mk @@ -0,0 +1,4 @@ +# Custom matrix scanning code via ADC +CUSTOM_MATRIX = lite +SRC += matrix.c analog.c + diff --git a/keyboards/framework/laptop16/config.h b/keyboards/framework/laptop16/config.h new file mode 100644 index 00000000000..6271a135213 --- /dev/null +++ b/keyboards/framework/laptop16/config.h @@ -0,0 +1,30 @@ +// Copyright 2022-2023 Framework Computer +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +// Allow (emulated) EEPROM reset using VIA +#define VIA_EEPROM_ALLOW_RESET + +// Increase to 16K for more storage space +// With 64K backing size, this spread the wear level over 4 blocks +#define WEAR_LEVELING_LOGICAL_SIZE (4 * 4096) +// Increase to 64K backing size to allow more wear leveling +#define WEAR_LEVELING_BACKING_SIZE (16 * 4096) +// We use 1MB flash +#define PICO_FLASH_SIZE_BYTES (1 * 1024 * 1024) +#define WEAR_LEVELING_RP2040_FLASH_SIZE PICO_FLASH_SIZE_BYTES +// Keep the last 4K sector free for the serial number +#define WEAR_LEVELING_RP2040_FLASH_BASE ((PICO_FLASH_SIZE_BYTES) - (WEAR_LEVELING_BACKING_SIZE + 4096)) + +// Prints every second how many matrix scans were done (Frequency in Hz) +// Only during debugging, since it prevents the host from going to sleep +// #define DEBUG_MATRIX_SCAN_RATE + +// PWM single one backlight configuration +// GPIO25 maps to PWM channel 4B of the RP2040 +// On the Raspberry Pi Pico this is the green LED on the board, good for prototyping +#define BACKLIGHT_PWM_DRIVER PWMD4 +#define BACKLIGHT_PWM_CHANNEL RP2040_PWM_CHANNEL_B +// Change PWM frequency to 24kHz, the default of 2048Hz causes loud noise +#define BACKLIGHT_PWM_PERIOD BACKLIGHT_PWM_COUNTER_FREQUENCY / 24000 diff --git a/keyboards/framework/laptop16/framework.c b/keyboards/framework/laptop16/framework.c new file mode 100644 index 00000000000..2f0fbec81d9 --- /dev/null +++ b/keyboards/framework/laptop16/framework.c @@ -0,0 +1,33 @@ +// Copyright 2022-2023 Framework Computer +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "quantum.h" +#include "framework.h" + +// void keyboard_post_init_kb(void) { +// keyboard_post_init_user(); + +// // Enable debug output +// debug_enable = true; +// debug_matrix = true; +// debug_keyboard = true; +// } + +/** + * Hook into early keyboard initialization + */ +void keyboard_pre_init_kb(void) { + keyboard_pre_init_user(); + + // Mark boot as done. + // Before this, when holding down both alt keys QSPI_SS is pulled low to put + // the RP2040 in bootloader mode during reset. + setPinOutput(BOOT_DONE_GPIO); + writePinLow(BOOT_DONE_GPIO); + + // TODO: Do we ever need to disable it to save power? + setPinOutput(MUX_ENABLE_GPIO); + writePinHigh(MUX_ENABLE_GPIO); + + setPinInput(SLEEP_GPIO); +} diff --git a/keyboards/framework/laptop16/framework.h b/keyboards/framework/laptop16/framework.h new file mode 100644 index 00000000000..af57611a4c4 --- /dev/null +++ b/keyboards/framework/laptop16/framework.h @@ -0,0 +1,15 @@ +// Copyright 2022-2023 Framework Computer +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include "quantum.h" + +enum framework_keycodes { + // Function lock button + FN_LOCK = QK_KB, +}; + +#define SLEEP_GPIO GP0 +#define MUX_ENABLE_GPIO GP4 +#define BOOT_DONE_GPIO GP5 diff --git a/keyboards/framework/laptop16/halconf.h b/keyboards/framework/laptop16/halconf.h new file mode 100644 index 00000000000..c96c5bfb3cd --- /dev/null +++ b/keyboards/framework/laptop16/halconf.h @@ -0,0 +1,11 @@ +// Copyright 2022-2023 Framework Computer +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +// For single-zone backlight +#define HAL_USE_PWM TRUE + +#define HAL_USE_ADC TRUE + +#include_next diff --git a/keyboards/framework/laptop16/info.json b/keyboards/framework/laptop16/info.json new file mode 100644 index 00000000000..5e6e1ad62ac --- /dev/null +++ b/keyboards/framework/laptop16/info.json @@ -0,0 +1,12 @@ +{ + "manufacturer": "Framework", + "maintainer": "JohnAZoidberg", + "bootloader": "rp2040", + "processor": "RP2040", + "url": "https://frame.work/de/en/products/laptop16-diy-amd-7040", + "host": { + "default": { + "nkro": true + } + } + } diff --git a/keyboards/framework/laptop16/matrix.c b/keyboards/framework/laptop16/matrix.c new file mode 100644 index 00000000000..2bb20a4e448 --- /dev/null +++ b/keyboards/framework/laptop16/matrix.c @@ -0,0 +1,313 @@ +// Copyright 2022-2023 Framework Computer +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include +#include "debug.h" +#include "analog.h" +#include "print.h" +#include "quantum.h" +#include "hal_adc.h" +#include "chprintf.h" + +#include "matrix.h" +#include "framework.h" + +#define adc10ksample_t int + +// Mux GPIOs +#define MUX_A GP1 +#define MUX_B GP2 +#define MUX_C GP3 +#define MUX_ENABLE GP4 + +// Rows to ADC input +#define KSI0 2 +#define KSI1 0 +#define KSI2 1 +#define KSI3 3 + +// Columns to GPIOs +#define KSO0 GP8 +#define KSO1 GP9 +#define KSO2 GP10 +#define KSO3 GP11 +#define KSO4 GP12 +#define KSO5 GP13 +#define KSO6 GP14 +#define KSO7 GP15 +#define KSO8 GP21 +#define KSO9 GP20 +#define KSO10 GP19 +#define KSO11 GP18 +#define KSO12 GP17 +#define KSO13 GP16 +#define KSO14 GP23 +#define KSO15 GP22 + +#define ADC_CH2_PIN GP28 + +// Voltage threshold - anything below that counts as pressed +// 29000 = 2.9V * 10000 +const adc10ksample_t ADC_THRESHOLD = (adc10ksample_t)29000; + +bool have_slept = false; + +adc10ksample_t to_voltage(adcsample_t sample) { + int voltage = sample * 33000; + return voltage / 1023; +} + +/** + * Tell RP2040 ADC controller to initialize a specific GPIO for ADC input + */ +void adc_gpio_init(int gpio) { + assert(gpio >= GP26 && gpio <= GP28); +// Enable pull-up on GPIO input so that we always have high input +// Even on the rows that don't have the external pull-up. +// Otherwise they would be floating. +#define PAL_MODE_ADC_PULLUP (PAL_MODE_INPUT_ANALOG | PAL_RP_PAD_PUE) + palSetLineMode(gpio, PAL_MODE_ADC_PULLUP); +} + +/** + * Tell the mux to select a specific column + * + * Splits the positive integer (<=7) into its three component bits. + */ +static void mux_select_row(int row) { + assert(col >= 0 && col <= 7); + + // Not in order - need to remap them + // X0 - KSI1 + // X1 - KSI2 + // X2 - KSI0 + // X3 - KSI3 + // Only for keyboard, not for num-/grid-pad + // X4 - KSI4 + // X5 - KSI5 + // X6 - KSI6 + // X7 - KSI7 + int index = 0; + switch (row) { + case 0: + index = 2; + break; + case 1: + index = 0; + break; + case 2: + index = 1; + break; + default: + index = row; + break; + } + + int bits[] = {(index & 0x1) > 0, (index & 0x2) > 0, (index & 0x4) > 0}; + writePin(MUX_A, bits[0]); + writePin(MUX_B, bits[1]); + writePin(MUX_C, bits[2]); +} + +/** + * Based on the ADC value, update the matrix for this column + * */ +static bool interpret_adc_row(matrix_row_t cur_matrix[], adc10ksample_t voltage, int col, int row) { + bool changed = false; + + // By default the voltage is high (3.3V) + // When a key is pressed it causes the voltage to go down. + // But because every key is connected in a matrix, pressing multiple keys + // changes the voltage at every key again. So we can't check for a specific + // voltage but need to have a threshold. + bool key_state = false; + if (voltage < ADC_THRESHOLD) { + key_state = true; + } + + matrix_row_t new_row = cur_matrix[row]; + if (key_state) { + new_row |= (1 << col); + } else { + new_row &= ~(1 << col); + } + changed = cur_matrix[row] != new_row; + cur_matrix[row] = new_row; + + return changed; +} + +/** + * Drive the GPIO for a column low or high. + */ +void drive_col(int col, bool high) { + assert(col >= 0 && col <= MATRIX_COLS); + int gpio = 0; + switch (col) { + case 0: + gpio = GP8; + break; + case 1: + gpio = GP9; + break; + case 2: + gpio = GP10; + break; + case 3: + gpio = GP11; + break; + case 4: + gpio = GP12; + break; + case 5: + gpio = GP13; + break; + case 6: + gpio = GP14; + break; + case 7: + gpio = GP15; + break; + case 8: + gpio = GP21; + break; + case 9: + gpio = GP20; + break; + case 10: + gpio = GP19; + break; + case 11: + gpio = GP18; + break; + case 12: + gpio = GP17; + break; + case 13: + gpio = GP16; + break; + case 14: + gpio = GP23; + break; + case 15: + gpio = GP22; + break; + default: + // Not supposed to happen + assert(false); + return; + } + + if (high) { + // TODO: Could set up the pins with `setPinOutputOpenDrain` instead + writePinHigh(gpio); + } else { + writePinLow(gpio); + } +} + +/** + * Read a value from the ADC and print some debugging details + */ +static adc10ksample_t read_adc(void) { + // Can't use analogReadPin because it gets rid of the internal pullup on + // this pin, that we configure in matrix_init_custom + uint16_t val = adc_read(pinToMux(ADC_CH2_PIN)); + return to_voltage(val); +} + +/** + * Handle the host going to sleep or the keyboard being idle + * If the host is asleep the keyboard should reduce the scan rate and turn backlight off. + * + * If the host is awake but the keyboard is idle it should enter a low-power state + */ +bool handle_idle(void) { + bool asleep = !readPin(SLEEP_GPIO); + static uint8_t prev_asleep = -1; + if (prev_asleep != asleep) { + prev_asleep = asleep; + } + return false; +} + +/** + * Overriding behavior of matrix_scan from quantum/matrix.c + */ +bool matrix_scan_custom(matrix_row_t current_matrix[]) { + bool changed = false; + + if (handle_idle()) { + return false; + } + + // Drive all high to deselect them + for (int col = 0; col < MATRIX_COLS; col++) { + drive_col(col, true); + } + + // Go through every matrix column (KSO) and drive them low individually + // Then go through every matrix row (KSI), select it with the mux and check their ADC value + for (int col = 0; col < MATRIX_COLS; col++) { + // Drive column low so we can measure the resistors on each row in this column + drive_col(col, false); + for (int row = 0; row < MATRIX_ROWS; row++) { + // Read ADC for this row + mux_select_row(row); + + // Interpret ADC value as rows + changed |= interpret_adc_row(current_matrix, read_adc(), col, row); + } + + // Drive column high again + drive_col(col, true); + } + + return changed; +} + +/** + * Enable the ADC MUX + * + * TODO: Do we need a de-init? Probably not. + */ +static void adc_mux_init(void) { + setPinOutput(MUX_ENABLE); + writePinLow(MUX_ENABLE); + + setPinOutput(MUX_A); + setPinOutput(MUX_B); + setPinOutput(MUX_C); +} + +/** + * Overriding behavior of matrix_init from quantum/matrix.c + */ +void matrix_init_custom(void) { + adc_mux_init(); + adc_gpio_init(ADC_CH2_PIN); + + // KS0 - KSO7 for Keyboard and Numpad + setPinOutput(KSO0); + setPinOutput(KSO1); + setPinOutput(KSO2); + setPinOutput(KSO3); + setPinOutput(KSO4); + setPinOutput(KSO5); + setPinOutput(KSO6); + setPinOutput(KSO7); + // KS08 - KS015 for Keyboard only + setPinOutput(KSO8); + setPinOutput(KSO9); + setPinOutput(KSO10); + setPinOutput(KSO11); + setPinOutput(KSO12); + setPinOutput(KSO13); + setPinOutput(KSO14); + setPinOutput(KSO15); + + // Set unused pins to input to avoid interfering. They're hooked up to rows 5 and 6 + setPinInput(GP6); + setPinInput(GP7); +} diff --git a/keyboards/framework/laptop16/mcuconf.h b/keyboards/framework/laptop16/mcuconf.h new file mode 100644 index 00000000000..8c2ac091542 --- /dev/null +++ b/keyboards/framework/laptop16/mcuconf.h @@ -0,0 +1,23 @@ +// Copyright 2022-2023 Framework Computer +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include_next + + +// For RGB backlight +#undef RP_ADC_USE_ADC1 +#define RP_ADC_USE_ADC1 TRUE + +// For single-zone backlight +#undef RP_PWM_USE_PWM4 +#define RP_PWM_USE_PWM4 TRUE + +/* + * IRQ system settings. + + * TODO: Default is 3, should it be 2? + */ +#undef RP_IRQ_ADC1_PRIORITY +#define RP_IRQ_ADC1_PRIORITY 2 diff --git a/keyboards/framework/laptop16/readme.md b/keyboards/framework/laptop16/readme.md new file mode 100644 index 00000000000..fa64d8ad2fd --- /dev/null +++ b/keyboards/framework/laptop16/readme.md @@ -0,0 +1,37 @@ +# Framework Laptop 16 Keyboard + +Keyboard input modules for the Framework Laptop 16. + +* Keyboard Maintainer: [Daniel Schaefer](https://github.com/JohnAZoidberg) +* Hardware Supported: Framework Laptop 16 Keyboard, soon: Numpad and Macropad +* Hardware Availability: Available at https://frame.work/marketplace + + +## Vendor specific +On framework 16, KC_MSEL launches the framework website + +## Variants + +| Name | Size | Backlight | Keys | +| -------- | -------- | ---------- | ---- | +| ansi | Keyboard | White | 78 | + +Make example for this keyboard (after setting up your build environment): + + qmk compile -kb framework/laptop16/ansi -km default + +Flashing example for the ANSI keyboard: + + qmk compile -kb framework/laptop16/ansi -km default + +See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_tools) and the [make instructions](https://docs.qmk.fm/#/getting_started_make_guide) for more information. Brand new to QMK? Start with our [Complete Newbs Guide](https://docs.qmk.fm/#/newbs). + +## Bootloader + +Enter the bootloader in 3 ways: + +* **On Framework Laptop 16 Keyboard**: Hold down left ALT and right ALT while installing the module +* **On Framework Laptop 16 Numpad**: Hold down keys for 2 and 6 while installing the module +* **On Raspberry Pi Pico**: Hold down bootsel button when plugging in +* **Bootmagic reset**: Hold down the key at (0,0) in the matrix and plug in the keyboard. On the ANSI board, this is the C key. +* **Keycode in layout**: Press the key mapped to `QK_BOOT` if it is available