Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- config.h ---
- #ifndef CONFIG_H
- #define CONFIG_H
- /* USB Device descriptor parameter */
- #define VENDOR_ID 0xFEED
- #define PRODUCT_ID 0x6464
- #define DEVICE_VER 0x0001
- /* in python2: list(u"whatever".encode('utf-16-le')) */
- /* at most 32 characters or the ugly hack in usb_main.c borks */
- #define MANUFACTURER "TMK"
- #define USBSTR_MANUFACTURER 'T', '\x00', 'M', '\x00', 'K', '\x00', ' ', '\x00', '\xc6', '\x00'
- #define PRODUCT "ChibiOS TMK test"
- #define USBSTR_PRODUCT 'C', '\x00', 'h', '\x00', 'i', '\x00', 'b', '\x00', 'i', '\x00', 'O', '\x00', 'S', '\x00', ' ', '\x00', 'T', '\x00', 'M', '\x00', 'K', '\x00', ' ', '\x00', 't', '\x00', 'e', '\x00', 's', '\x00', 't', '\x00'
- #define DESCRIPTION "TMK keyboard firmware over ChibiOS"
- /* key matrix size */
- #define MATRIX_ROWS 6
- #define MATRIX_COLS 16
- /* define if matrix has ghost */
- //#define MATRIX_HAS_GHOST
- /* Set 0 if debouncing isn't needed */
- #define DEBOUNCE 5
- /* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
- #define LOCKING_SUPPORT_ENABLE
- /* Locking resynchronize hack */
- #define LOCKING_RESYNC_ENABLE
- /* key combination for command */
- #define IS_COMMAND() ( \
- keyboard_report->mods == (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT)) \
- )
- #endif
- --- keymap_common.h ---
- #ifndef KEYMAP_COMMON_H
- #define KEYMAP_COMMON_H
- #include <stdint.h>
- #include <stdbool.h>
- #include "keycode.h"
- #include "action.h"
- #include "action_macro.h"
- #include "action_layer.h"
- #include "report.h"
- #include "host.h"
- #include "print.h"
- #include "debug.h"
- #include "keymap.h"
- extern const uint8_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
- extern const action_t fn_actions[];
- #define KEYMAP( \
- K00, K01, K02, K03, K04, K06, K07, K08, K09, K0A, K0B, K0C, K0D, K0E, K0F, \
- K10, K11, K12, K13, K14, K15, K16, K17, K18, K19, K1A, K1B, K1D, K1E, K1F, \
- K20, K21, K22, K23, K25, K26, K27, K28, K29, K2A, K2B, K2C, K2D, K2E, K2F, \
- K30, K32, K33, K34, K35, K36, K37, K38, K39, K3A, K3B, K3D, K3E, K3F, \
- K40, K43, K44, K45, K46, K47, K48, K49, K4A, K4B, K4C, K4D, K4E, K4F, \
- K50, K52, K53, K55, K58, K5A, K5B, K5C, K5D, K5E, K5F \
- ) { \
- { KC_##K00, KC_##K01, KC_##K02, KC_##K03, KC_##K04, KC_NO, KC_##K06, KC_##K07, KC_##K08, KC_##K09, KC_##K0A, KC_##K0B, KC_##K0C, KC_##K0D, KC_##K0E, KC_##K0F }, \
- { KC_##K10, KC_##K11, KC_##K12, KC_##K13, KC_##K14, KC_##K15, KC_##K16, KC_##K17, KC_##K18, KC_##K19, KC_##K1A, KC_##K1B, KC_NO, KC_##K1D, KC_##K1E, KC_##K1F }, \
- { KC_##K20, KC_##K21, KC_##K22, KC_##K23, KC_NO, KC_##K25, KC_##K26, KC_##K27, KC_##K28, KC_##K29, KC_##K2A, KC_##K2B, KC_##K2C, KC_##K2D, KC_##K2E, KC_##K2F }, \
- { KC_##K30, KC_NO, KC_##K32, KC_##K33, KC_##K34, KC_##K35, KC_##K36, KC_##K37, KC_##K38, KC_##K39, KC_##K3A, KC_##K3B, KC_NO, KC_##K3D, KC_##K3E, KC_##K3F }, \
- { KC_##K40, KC_NO, KC_NO, KC_##K43, KC_##K44, KC_##K45, KC_##K46, KC_##K47, KC_##K48, KC_##K49, KC_##K4A, KC_##K4B, KC_##K4C, KC_##K4D, KC_##K4E, KC_##K4F }, \
- { KC_##K50, KC_NO, KC_##K52, KC_##K53, KC_NO, KC_##K55, KC_NO, KC_NO, KC_##K58, KC_NO, KC_##K5A, KC_##K5B, KC_##K5C, KC_##K5D, KC_##K5E, KC_##K5F } \
- }
- /* translates key to keycode */
- uint8_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
- {
- return keymaps[(layer)][(key.row)][(key.col)];
- }
- /* translates Fn keycode to action */
- action_t keymap_fn_to_action(uint8_t keycode)
- {
- return fn_actions[FN_INDEX(keycode)];
- }
- const action_t PROGMEM fn_actions[] = {
- [0] = ACTION_LAYER_MOMENTARY(1)
- };
- #endif
- --- keymap_kk80.c ---
- #include "keymap_common.h"
- const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
- /* 0: querty */
- KEYMAP(
- ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PAUS, MUTE, \
- GRV, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS, EQL, BSPC, _VOLUP, \
- TAB, Q, W, E, R, T, Y, U, I, O, P, LBRC, RBRC, BSLS, _VOLDOWN, \
- CAPS, A, S, D, F, G, H, J, K, L, SCLN, QUOT, ENT, PASTE, \
- LSFT, Z, X, C, V, B, N, M, COMM, DOT, SLSH, RSFT, UP, COPY, \
- FN0, LCTL, LALT, LGUI, SPC, RGUI, RALT, BTN1, LEFT, DOWN, RIGHT),
- /* 1: fn 1 */
- KEYMAP(
- MUTE, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, WAKE, SLEP, \
- TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, DELETE, F2, \
- TRNS, TRNS, MS_U, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, SELECT, F1, \
- TRNS, MS_L, MS_D, MS_R, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, \
- TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, WBAK, WFWD, WREF, TRNS, PGUP, TRNS, \
- TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, BTN2, UNDO, PGDN, AGAIN),
- };
- --- Makefile ---
- # Target file name (without extension).
- PROJECT = kk80
- # Directory common source files exist
- TMK_DIR = ../../tmk_core
- # Directory keyboard dependent files exist
- TARGET_DIR = .
- # project specific files
- SRC = matrix.c \
- led.c
- ifdef KEYMAP
- SRC := keymap_$(KEYMAP).c $(SRC)
- else
- SRC := keymap_kk80.c $(SRC)
- endif
- CONFIG_H = config.h
- ## chip/board settings
- # - the next two should match the directories in
- # <chibios>/os/hal/ports/$(MCU_FAMILY)/$(MCU_SERIES)
- # - For Teensies, FAMILY = KINETIS and SERIES is either
- # KL2x (LC) or K20x (3.0,3.1,3.2).
- MCU_FAMILY = KINETIS
- MCU_SERIES = KL2x
- # Linker script to use
- # - it should exist either in <chibios>/os/common/ports/ARMCMx/compilers/GCC/ld/
- # or <this_dir>/ld/
- # - NOTE: a custom ld script is needed for EEPROM on Teensy LC
- # - LDSCRIPT =
- # - MKL26Z64 for Teensy LC
- # - MK20DX128 for Teensy 3.0
- # - MK20DX256 for Teensy 3.1 and 3.2
- MCU_LDSCRIPT = MKL26Z64
- # Startup code to use
- # - it should exist in <chibios>/os/common/ports/ARMCMx/compilers/GCC/mk/
- # - STARTUP =
- # - kl2x for Teensy LC
- # - k20x5 for Teensy 3.0
- # - k20x7 for Teensy 3.1 and 3.2
- MCU_STARTUP = kl2x
- # Board: it should exist either in <chibios>/os/hal/boards/
- # or <this_dir>/boards
- # - BOARD =
- # - PJRC_TEENSY_LC for Teensy LC
- # - PJRC_TEENSY_3 for Teensy 3.0
- # - PJRC_TEENSY_3_1 for Teensy 3.1 or 3.2
- BOARD = PJRC_TEENSY_LC
- # Cortex version
- # Teensy LC is cortex-m0plus; Teensy 3.x are cortex-m4
- MCU = cortex-m0plus
- # ARM version, CORTEX-M0/M1 are 6, CORTEX-M3/M4/M7 are 7
- # I.e. 6 for Teensy LC; 7 for Teensy 3.x
- ARMV = 6
- # Build Options
- # comment out to disable the options.
- #
- BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration
- ## (Note that for BOOTMAGIC on Teensy LC you have to use a custom .ld script.)
- MOUSEKEY_ENABLE = yes # Mouse keys
- EXTRAKEY_ENABLE = yes # Audio control and System control
- CONSOLE_ENABLE = yes # Console for debug
- COMMAND_ENABLE = yes # Commands for debug and configuration
- # SLEEP_LED_ENABLE = yes # Breathing sleep LED during USB suspend
- # NKRO_ENABLE = yes # USB Nkey Rollover
- include $(TMK_DIR)/tool/chibios/common.mk
- include $(TMK_DIR)/tool/chibios/chibios.mk
- --- matrix.c ---
- #include "ch.h"
- #include "hal.h"
- /*
- * scan matrix
- */
- #include "print.h"
- #include "debug.h"
- #include "util.h"
- #include "matrix.h"
- #include "wait.h"
- #ifndef DEBOUNCE
- # define DEBOUNCE 5
- #endif
- static uint8_t debouncing = DEBOUNCE;
- /* matrix state(1:on, 0:off) */
- static matrix_row_t matrix[MATRIX_ROWS];
- static matrix_row_t matrix_debouncing[MATRIX_ROWS];
- static matrix_row_t read_cols(void);
- static void init_cols(void);
- static void unselect_rows(void);
- static void select_row(uint8_t row);
- inline
- uint8_t matrix_rows(void)
- {
- return MATRIX_ROWS;
- }
- inline
- uint8_t matrix_cols(void)
- {
- return MATRIX_COLS;
- }
- // #define LED_ON() do { palSetPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13) ;} while (0)
- // #define LED_OFF() do { palClearPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13); } while (0)
- // #define LED_TGL() do { palTogglePad(TEENSY_PIN13_IOPORT, TEENSY_PIN13); } while (0)
- void matrix_init(void)
- {
- // initialize row and col
- unselect_rows();
- init_cols();
- // initialize matrix state: all keys off
- for (uint8_t i=0; i < MATRIX_ROWS; i++) {
- matrix[i] = 0;
- matrix_debouncing[i] = 0;
- }
- //debug
- // debug_matrix = true;
- // LED_ON();
- // wait_ms(500);
- // LED_OFF();
- }
- uint8_t matrix_scan(void)
- {
- for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
- select_row(i);
- wait_us(10); // without this wait read unstable value.
- // for (uint8_t i=0; i < 3; i++) { // Loop for very short delay, wait_us is returning a 1ms delay
- // LED_TGL(); // toggle the onboard LED attached to pin 13
- // }
- matrix_row_t cols = read_cols();
- if (matrix_debouncing[i] != cols) {
- matrix_debouncing[i] = cols;
- if (debouncing) {
- debug("bounce!: "); debug_hex(debouncing); debug("\n");
- }
- debouncing = DEBOUNCE;
- }
- unselect_rows();
- }
- if (debouncing) {
- if (--debouncing) {
- wait_ms(1);
- } else {
- for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
- matrix[i] = matrix_debouncing[i];
- }
- }
- }
- return 1;
- }
- inline
- bool matrix_is_on(uint8_t row, uint8_t col)
- {
- return (matrix[row] & ((matrix_row_t)1<<col));
- }
- inline
- matrix_row_t matrix_get_row(uint8_t row)
- {
- return matrix[row];
- }
- void matrix_print(void)
- {
- print("\nr/c 0123456789ABCDEF\n");
- for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
- phex(row); print(": ");
- pbin_reverse16(matrix_get_row(row));
- print("\n");
- }
- }
- /* Column pin configuration
- */
- static void init_cols(void)
- {
- // internal pull-up
- palSetPadMode(TEENSY_PIN14_IOPORT, TEENSY_PIN14, PAL_MODE_INPUT_PULLUP);
- palSetPadMode(TEENSY_PIN17_IOPORT, TEENSY_PIN17, PAL_MODE_INPUT_PULLUP);
- palSetPadMode(TEENSY_PIN18_IOPORT, TEENSY_PIN18, PAL_MODE_INPUT_PULLUP);
- palSetPadMode(TEENSY_PIN16_IOPORT, TEENSY_PIN16, PAL_MODE_INPUT_PULLUP);
- palSetPadMode(TEENSY_PIN12_IOPORT, TEENSY_PIN12, PAL_MODE_INPUT_PULLUP);
- palSetPadMode(TEENSY_PIN11_IOPORT, TEENSY_PIN11, PAL_MODE_INPUT_PULLUP);
- palSetPadMode(TEENSY_PIN10_IOPORT, TEENSY_PIN10, PAL_MODE_INPUT_PULLUP);
- palSetPadMode(TEENSY_PIN7_IOPORT, TEENSY_PIN7, PAL_MODE_INPUT_PULLUP);
- palSetPadMode(TEENSY_PIN9_IOPORT, TEENSY_PIN9, PAL_MODE_INPUT_PULLUP);
- palSetPadMode(TEENSY_PIN8_IOPORT, TEENSY_PIN8, PAL_MODE_INPUT_PULLUP);
- palSetPadMode(TEENSY_PIN5_IOPORT, TEENSY_PIN5, PAL_MODE_INPUT_PULLUP);
- palSetPadMode(TEENSY_PIN6_IOPORT, TEENSY_PIN6, PAL_MODE_INPUT_PULLUP);
- palSetPadMode(TEENSY_PIN24_IOPORT, TEENSY_PIN24, PAL_MODE_INPUT_PULLUP);
- palSetPadMode(TEENSY_PIN4_IOPORT, TEENSY_PIN4, PAL_MODE_INPUT_PULLUP);
- palSetPadMode(TEENSY_PIN1_IOPORT, TEENSY_PIN1, PAL_MODE_INPUT_PULLUP);
- palSetPadMode(TEENSY_PIN0_IOPORT, TEENSY_PIN0, PAL_MODE_INPUT_PULLUP);
- }
- /* Returns status of switches(1:on, 0:off) */
- static matrix_row_t read_cols(void)
- {
- return ((palReadPad(TEENSY_PIN14_IOPORT, TEENSY_PIN14)==PAL_HIGH) ? 0 : (1<<0))
- | ((palReadPad(TEENSY_PIN17_IOPORT, TEENSY_PIN17)==PAL_HIGH) ? 0 : (1<<1))
- | ((palReadPad(TEENSY_PIN18_IOPORT, TEENSY_PIN18)==PAL_HIGH) ? 0 : (1<<2))
- | ((palReadPad(TEENSY_PIN16_IOPORT, TEENSY_PIN16)==PAL_HIGH) ? 0 : (1<<3))
- | ((palReadPad(TEENSY_PIN12_IOPORT, TEENSY_PIN12)==PAL_HIGH) ? 0 : (1<<4))
- | ((palReadPad(TEENSY_PIN11_IOPORT, TEENSY_PIN11)==PAL_HIGH) ? 0 : (1<<5))
- | ((palReadPad(TEENSY_PIN10_IOPORT, TEENSY_PIN10)==PAL_HIGH) ? 0 : (1<<6))
- | ((palReadPad(TEENSY_PIN7_IOPORT, TEENSY_PIN7)==PAL_HIGH) ? 0 : (1<<7))
- | ((palReadPad(TEENSY_PIN9_IOPORT, TEENSY_PIN9)==PAL_HIGH) ? 0 : (1<<8))
- | ((palReadPad(TEENSY_PIN8_IOPORT, TEENSY_PIN8)==PAL_HIGH) ? 0 : (1<<9))
- | ((palReadPad(TEENSY_PIN5_IOPORT, TEENSY_PIN5)==PAL_HIGH) ? 0 : (1<<10))
- | ((palReadPad(TEENSY_PIN6_IOPORT, TEENSY_PIN6)==PAL_HIGH) ? 0 : (1<<11))
- | ((palReadPad(TEENSY_PIN24_IOPORT, TEENSY_PIN24)==PAL_HIGH) ? 0 : (1<<12))
- | ((palReadPad(TEENSY_PIN4_IOPORT, TEENSY_PIN4)==PAL_HIGH) ? 0 : (1<<13))
- | ((palReadPad(TEENSY_PIN1_IOPORT, TEENSY_PIN1)==PAL_HIGH) ? 0 : (1<<14))
- | ((palReadPad(TEENSY_PIN0_IOPORT, TEENSY_PIN0)==PAL_HIGH) ? 0 : (1<<15));
- }
- /* Row pin configuration
- */
- static void unselect_rows(void)
- {
- palSetPadMode(TEENSY_PIN15_IOPORT, TEENSY_PIN15, PAL_MODE_INPUT); // hi-Z
- palSetPadMode(TEENSY_PIN2_IOPORT, TEENSY_PIN2, PAL_MODE_INPUT); // hi-Z
- palSetPadMode(TEENSY_PIN19_IOPORT, TEENSY_PIN19, PAL_MODE_INPUT); // hi-Z
- palSetPadMode(TEENSY_PIN20_IOPORT, TEENSY_PIN20, PAL_MODE_INPUT); // hi-Z
- palSetPadMode(TEENSY_PIN21_IOPORT, TEENSY_PIN21, PAL_MODE_INPUT); // hi-Z
- palSetPadMode(TEENSY_PIN22_IOPORT, TEENSY_PIN22, PAL_MODE_INPUT); // hi-Z
- }
- static void select_row(uint8_t row)
- {
- (void)row;
- // Output low to select
- switch (row) {
- case 0:
- palSetPadMode(TEENSY_PIN15_IOPORT, TEENSY_PIN15, PAL_MODE_OUTPUT_PUSHPULL);
- palClearPad(TEENSY_PIN15_IOPORT, TEENSY_PIN15);
- break;
- case 1:
- palSetPadMode(TEENSY_PIN2_IOPORT, TEENSY_PIN2, PAL_MODE_OUTPUT_PUSHPULL);
- palClearPad(TEENSY_PIN2_IOPORT, TEENSY_PIN2);
- break;
- case 2:
- palSetPadMode(TEENSY_PIN19_IOPORT, TEENSY_PIN19, PAL_MODE_OUTPUT_PUSHPULL);
- palClearPad(TEENSY_PIN19_IOPORT, TEENSY_PIN19);
- break;
- case 3:
- palSetPadMode(TEENSY_PIN20_IOPORT, TEENSY_PIN20, PAL_MODE_OUTPUT_PUSHPULL);
- palClearPad(TEENSY_PIN20_IOPORT, TEENSY_PIN20);
- break;
- case 4:
- palSetPadMode(TEENSY_PIN21_IOPORT, TEENSY_PIN21, PAL_MODE_OUTPUT_PUSHPULL);
- palClearPad(TEENSY_PIN21_IOPORT, TEENSY_PIN21);
- break;
- case 5:
- palSetPadMode(TEENSY_PIN22_IOPORT, TEENSY_PIN22, PAL_MODE_OUTPUT_PUSHPULL);
- palClearPad(TEENSY_PIN22_IOPORT, TEENSY_PIN22);
- break;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment