Guest User

kk80 - tmk keyboard project

a guest
May 2nd, 2017
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.16 KB | None | 0 0
  1. --- config.h ---
  2.  
  3. #ifndef CONFIG_H
  4. #define CONFIG_H
  5.  
  6. /* USB Device descriptor parameter */
  7. #define VENDOR_ID 0xFEED
  8. #define PRODUCT_ID 0x6464
  9. #define DEVICE_VER 0x0001
  10. /* in python2: list(u"whatever".encode('utf-16-le')) */
  11. /* at most 32 characters or the ugly hack in usb_main.c borks */
  12. #define MANUFACTURER "TMK"
  13. #define USBSTR_MANUFACTURER 'T', '\x00', 'M', '\x00', 'K', '\x00', ' ', '\x00', '\xc6', '\x00'
  14. #define PRODUCT "ChibiOS TMK test"
  15. #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'
  16. #define DESCRIPTION "TMK keyboard firmware over ChibiOS"
  17.  
  18. /* key matrix size */
  19. #define MATRIX_ROWS 6
  20. #define MATRIX_COLS 16
  21.  
  22. /* define if matrix has ghost */
  23. //#define MATRIX_HAS_GHOST
  24.  
  25. /* Set 0 if debouncing isn't needed */
  26. #define DEBOUNCE 5
  27.  
  28. /* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */
  29. #define LOCKING_SUPPORT_ENABLE
  30. /* Locking resynchronize hack */
  31. #define LOCKING_RESYNC_ENABLE
  32.  
  33. /* key combination for command */
  34. #define IS_COMMAND() ( \
  35. keyboard_report->mods == (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT)) \
  36. )
  37.  
  38. #endif
  39.  
  40.  
  41.  
  42. --- keymap_common.h ---
  43.  
  44. #ifndef KEYMAP_COMMON_H
  45. #define KEYMAP_COMMON_H
  46.  
  47. #include <stdint.h>
  48. #include <stdbool.h>
  49. #include "keycode.h"
  50. #include "action.h"
  51. #include "action_macro.h"
  52. #include "action_layer.h"
  53. #include "report.h"
  54. #include "host.h"
  55. #include "print.h"
  56. #include "debug.h"
  57. #include "keymap.h"
  58.  
  59.  
  60. extern const uint8_t keymaps[][MATRIX_ROWS][MATRIX_COLS];
  61. extern const action_t fn_actions[];
  62.  
  63. #define KEYMAP( \
  64. K00, K01, K02, K03, K04, K06, K07, K08, K09, K0A, K0B, K0C, K0D, K0E, K0F, \
  65. K10, K11, K12, K13, K14, K15, K16, K17, K18, K19, K1A, K1B, K1D, K1E, K1F, \
  66. K20, K21, K22, K23, K25, K26, K27, K28, K29, K2A, K2B, K2C, K2D, K2E, K2F, \
  67. K30, K32, K33, K34, K35, K36, K37, K38, K39, K3A, K3B, K3D, K3E, K3F, \
  68. K40, K43, K44, K45, K46, K47, K48, K49, K4A, K4B, K4C, K4D, K4E, K4F, \
  69. K50, K52, K53, K55, K58, K5A, K5B, K5C, K5D, K5E, K5F \
  70. ) { \
  71. { 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 }, \
  72. { 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 }, \
  73. { 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 }, \
  74. { 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 }, \
  75. { 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 }, \
  76. { 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 } \
  77. }
  78.  
  79. /* translates key to keycode */
  80. uint8_t keymap_key_to_keycode(uint8_t layer, keypos_t key)
  81. {
  82. return keymaps[(layer)][(key.row)][(key.col)];
  83. }
  84.  
  85. /* translates Fn keycode to action */
  86. action_t keymap_fn_to_action(uint8_t keycode)
  87. {
  88. return fn_actions[FN_INDEX(keycode)];
  89. }
  90.  
  91. const action_t PROGMEM fn_actions[] = {
  92. [0] = ACTION_LAYER_MOMENTARY(1)
  93. };
  94.  
  95. #endif
  96.  
  97.  
  98.  
  99. --- keymap_kk80.c ---
  100.  
  101. #include "keymap_common.h"
  102.  
  103. const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
  104. /* 0: querty */
  105. KEYMAP(
  106. ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PAUS, MUTE, \
  107. GRV, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS, EQL, BSPC, _VOLUP, \
  108. TAB, Q, W, E, R, T, Y, U, I, O, P, LBRC, RBRC, BSLS, _VOLDOWN, \
  109. CAPS, A, S, D, F, G, H, J, K, L, SCLN, QUOT, ENT, PASTE, \
  110. LSFT, Z, X, C, V, B, N, M, COMM, DOT, SLSH, RSFT, UP, COPY, \
  111. FN0, LCTL, LALT, LGUI, SPC, RGUI, RALT, BTN1, LEFT, DOWN, RIGHT),
  112.  
  113. /* 1: fn 1 */
  114. KEYMAP(
  115. MUTE, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, WAKE, SLEP, \
  116. TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, DELETE, F2, \
  117. TRNS, TRNS, MS_U, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, SELECT, F1, \
  118. TRNS, MS_L, MS_D, MS_R, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, \
  119. TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, WBAK, WFWD, WREF, TRNS, PGUP, TRNS, \
  120. TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, TRNS, BTN2, UNDO, PGDN, AGAIN),
  121. };
  122.  
  123.  
  124.  
  125. --- Makefile ---
  126.  
  127. # Target file name (without extension).
  128. PROJECT = kk80
  129.  
  130. # Directory common source files exist
  131. TMK_DIR = ../../tmk_core
  132.  
  133. # Directory keyboard dependent files exist
  134. TARGET_DIR = .
  135.  
  136. # project specific files
  137. SRC = matrix.c \
  138. led.c
  139.  
  140. ifdef KEYMAP
  141. SRC := keymap_$(KEYMAP).c $(SRC)
  142. else
  143. SRC := keymap_kk80.c $(SRC)
  144. endif
  145.  
  146. CONFIG_H = config.h
  147.  
  148. ## chip/board settings
  149. # - the next two should match the directories in
  150. # <chibios>/os/hal/ports/$(MCU_FAMILY)/$(MCU_SERIES)
  151. # - For Teensies, FAMILY = KINETIS and SERIES is either
  152. # KL2x (LC) or K20x (3.0,3.1,3.2).
  153. MCU_FAMILY = KINETIS
  154. MCU_SERIES = KL2x
  155.  
  156. # Linker script to use
  157. # - it should exist either in <chibios>/os/common/ports/ARMCMx/compilers/GCC/ld/
  158. # or <this_dir>/ld/
  159. # - NOTE: a custom ld script is needed for EEPROM on Teensy LC
  160. # - LDSCRIPT =
  161. # - MKL26Z64 for Teensy LC
  162. # - MK20DX128 for Teensy 3.0
  163. # - MK20DX256 for Teensy 3.1 and 3.2
  164. MCU_LDSCRIPT = MKL26Z64
  165.  
  166. # Startup code to use
  167. # - it should exist in <chibios>/os/common/ports/ARMCMx/compilers/GCC/mk/
  168. # - STARTUP =
  169. # - kl2x for Teensy LC
  170. # - k20x5 for Teensy 3.0
  171. # - k20x7 for Teensy 3.1 and 3.2
  172. MCU_STARTUP = kl2x
  173.  
  174. # Board: it should exist either in <chibios>/os/hal/boards/
  175. # or <this_dir>/boards
  176. # - BOARD =
  177. # - PJRC_TEENSY_LC for Teensy LC
  178. # - PJRC_TEENSY_3 for Teensy 3.0
  179. # - PJRC_TEENSY_3_1 for Teensy 3.1 or 3.2
  180. BOARD = PJRC_TEENSY_LC
  181.  
  182. # Cortex version
  183. # Teensy LC is cortex-m0plus; Teensy 3.x are cortex-m4
  184. MCU = cortex-m0plus
  185.  
  186. # ARM version, CORTEX-M0/M1 are 6, CORTEX-M3/M4/M7 are 7
  187. # I.e. 6 for Teensy LC; 7 for Teensy 3.x
  188. ARMV = 6
  189.  
  190. # Build Options
  191. # comment out to disable the options.
  192. #
  193. BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration
  194. ## (Note that for BOOTMAGIC on Teensy LC you have to use a custom .ld script.)
  195. MOUSEKEY_ENABLE = yes # Mouse keys
  196. EXTRAKEY_ENABLE = yes # Audio control and System control
  197. CONSOLE_ENABLE = yes # Console for debug
  198. COMMAND_ENABLE = yes # Commands for debug and configuration
  199. # SLEEP_LED_ENABLE = yes # Breathing sleep LED during USB suspend
  200. # NKRO_ENABLE = yes # USB Nkey Rollover
  201.  
  202. include $(TMK_DIR)/tool/chibios/common.mk
  203. include $(TMK_DIR)/tool/chibios/chibios.mk
  204.  
  205.  
  206.  
  207.  
  208.  
  209. --- matrix.c ---
  210.  
  211. #include "ch.h"
  212. #include "hal.h"
  213.  
  214. /*
  215. * scan matrix
  216. */
  217. #include "print.h"
  218. #include "debug.h"
  219. #include "util.h"
  220. #include "matrix.h"
  221. #include "wait.h"
  222.  
  223. #ifndef DEBOUNCE
  224. # define DEBOUNCE 5
  225. #endif
  226. static uint8_t debouncing = DEBOUNCE;
  227.  
  228. /* matrix state(1:on, 0:off) */
  229. static matrix_row_t matrix[MATRIX_ROWS];
  230. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  231.  
  232. static matrix_row_t read_cols(void);
  233. static void init_cols(void);
  234. static void unselect_rows(void);
  235. static void select_row(uint8_t row);
  236.  
  237.  
  238. inline
  239. uint8_t matrix_rows(void)
  240. {
  241. return MATRIX_ROWS;
  242. }
  243.  
  244. inline
  245. uint8_t matrix_cols(void)
  246. {
  247. return MATRIX_COLS;
  248. }
  249.  
  250. // #define LED_ON() do { palSetPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13) ;} while (0)
  251. // #define LED_OFF() do { palClearPad(TEENSY_PIN13_IOPORT, TEENSY_PIN13); } while (0)
  252. // #define LED_TGL() do { palTogglePad(TEENSY_PIN13_IOPORT, TEENSY_PIN13); } while (0)
  253.  
  254. void matrix_init(void)
  255. {
  256. // initialize row and col
  257. unselect_rows();
  258. init_cols();
  259.  
  260. // initialize matrix state: all keys off
  261. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  262. matrix[i] = 0;
  263. matrix_debouncing[i] = 0;
  264. }
  265.  
  266. //debug
  267. // debug_matrix = true;
  268. // LED_ON();
  269. // wait_ms(500);
  270. // LED_OFF();
  271. }
  272.  
  273. uint8_t matrix_scan(void)
  274. {
  275. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  276. select_row(i);
  277. wait_us(10); // without this wait read unstable value.
  278. // for (uint8_t i=0; i < 3; i++) { // Loop for very short delay, wait_us is returning a 1ms delay
  279. // LED_TGL(); // toggle the onboard LED attached to pin 13
  280. // }
  281. matrix_row_t cols = read_cols();
  282. if (matrix_debouncing[i] != cols) {
  283. matrix_debouncing[i] = cols;
  284. if (debouncing) {
  285. debug("bounce!: "); debug_hex(debouncing); debug("\n");
  286. }
  287. debouncing = DEBOUNCE;
  288. }
  289. unselect_rows();
  290. }
  291.  
  292. if (debouncing) {
  293. if (--debouncing) {
  294. wait_ms(1);
  295. } else {
  296. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  297. matrix[i] = matrix_debouncing[i];
  298. }
  299. }
  300. }
  301.  
  302. return 1;
  303. }
  304.  
  305. inline
  306. bool matrix_is_on(uint8_t row, uint8_t col)
  307. {
  308. return (matrix[row] & ((matrix_row_t)1<<col));
  309. }
  310.  
  311. inline
  312. matrix_row_t matrix_get_row(uint8_t row)
  313. {
  314. return matrix[row];
  315. }
  316.  
  317. void matrix_print(void)
  318. {
  319. print("\nr/c 0123456789ABCDEF\n");
  320. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  321. phex(row); print(": ");
  322. pbin_reverse16(matrix_get_row(row));
  323. print("\n");
  324. }
  325. }
  326.  
  327. /* Column pin configuration
  328. */
  329. static void init_cols(void)
  330. {
  331. // internal pull-up
  332. palSetPadMode(TEENSY_PIN14_IOPORT, TEENSY_PIN14, PAL_MODE_INPUT_PULLUP);
  333. palSetPadMode(TEENSY_PIN17_IOPORT, TEENSY_PIN17, PAL_MODE_INPUT_PULLUP);
  334. palSetPadMode(TEENSY_PIN18_IOPORT, TEENSY_PIN18, PAL_MODE_INPUT_PULLUP);
  335. palSetPadMode(TEENSY_PIN16_IOPORT, TEENSY_PIN16, PAL_MODE_INPUT_PULLUP);
  336. palSetPadMode(TEENSY_PIN12_IOPORT, TEENSY_PIN12, PAL_MODE_INPUT_PULLUP);
  337. palSetPadMode(TEENSY_PIN11_IOPORT, TEENSY_PIN11, PAL_MODE_INPUT_PULLUP);
  338. palSetPadMode(TEENSY_PIN10_IOPORT, TEENSY_PIN10, PAL_MODE_INPUT_PULLUP);
  339. palSetPadMode(TEENSY_PIN7_IOPORT, TEENSY_PIN7, PAL_MODE_INPUT_PULLUP);
  340. palSetPadMode(TEENSY_PIN9_IOPORT, TEENSY_PIN9, PAL_MODE_INPUT_PULLUP);
  341. palSetPadMode(TEENSY_PIN8_IOPORT, TEENSY_PIN8, PAL_MODE_INPUT_PULLUP);
  342. palSetPadMode(TEENSY_PIN5_IOPORT, TEENSY_PIN5, PAL_MODE_INPUT_PULLUP);
  343. palSetPadMode(TEENSY_PIN6_IOPORT, TEENSY_PIN6, PAL_MODE_INPUT_PULLUP);
  344. palSetPadMode(TEENSY_PIN24_IOPORT, TEENSY_PIN24, PAL_MODE_INPUT_PULLUP);
  345. palSetPadMode(TEENSY_PIN4_IOPORT, TEENSY_PIN4, PAL_MODE_INPUT_PULLUP);
  346. palSetPadMode(TEENSY_PIN1_IOPORT, TEENSY_PIN1, PAL_MODE_INPUT_PULLUP);
  347. palSetPadMode(TEENSY_PIN0_IOPORT, TEENSY_PIN0, PAL_MODE_INPUT_PULLUP);
  348. }
  349. /* Returns status of switches(1:on, 0:off) */
  350. static matrix_row_t read_cols(void)
  351. {
  352. return ((palReadPad(TEENSY_PIN14_IOPORT, TEENSY_PIN14)==PAL_HIGH) ? 0 : (1<<0))
  353. | ((palReadPad(TEENSY_PIN17_IOPORT, TEENSY_PIN17)==PAL_HIGH) ? 0 : (1<<1))
  354. | ((palReadPad(TEENSY_PIN18_IOPORT, TEENSY_PIN18)==PAL_HIGH) ? 0 : (1<<2))
  355. | ((palReadPad(TEENSY_PIN16_IOPORT, TEENSY_PIN16)==PAL_HIGH) ? 0 : (1<<3))
  356. | ((palReadPad(TEENSY_PIN12_IOPORT, TEENSY_PIN12)==PAL_HIGH) ? 0 : (1<<4))
  357. | ((palReadPad(TEENSY_PIN11_IOPORT, TEENSY_PIN11)==PAL_HIGH) ? 0 : (1<<5))
  358. | ((palReadPad(TEENSY_PIN10_IOPORT, TEENSY_PIN10)==PAL_HIGH) ? 0 : (1<<6))
  359. | ((palReadPad(TEENSY_PIN7_IOPORT, TEENSY_PIN7)==PAL_HIGH) ? 0 : (1<<7))
  360. | ((palReadPad(TEENSY_PIN9_IOPORT, TEENSY_PIN9)==PAL_HIGH) ? 0 : (1<<8))
  361. | ((palReadPad(TEENSY_PIN8_IOPORT, TEENSY_PIN8)==PAL_HIGH) ? 0 : (1<<9))
  362. | ((palReadPad(TEENSY_PIN5_IOPORT, TEENSY_PIN5)==PAL_HIGH) ? 0 : (1<<10))
  363. | ((palReadPad(TEENSY_PIN6_IOPORT, TEENSY_PIN6)==PAL_HIGH) ? 0 : (1<<11))
  364. | ((palReadPad(TEENSY_PIN24_IOPORT, TEENSY_PIN24)==PAL_HIGH) ? 0 : (1<<12))
  365. | ((palReadPad(TEENSY_PIN4_IOPORT, TEENSY_PIN4)==PAL_HIGH) ? 0 : (1<<13))
  366. | ((palReadPad(TEENSY_PIN1_IOPORT, TEENSY_PIN1)==PAL_HIGH) ? 0 : (1<<14))
  367. | ((palReadPad(TEENSY_PIN0_IOPORT, TEENSY_PIN0)==PAL_HIGH) ? 0 : (1<<15));
  368. }
  369.  
  370. /* Row pin configuration
  371. */
  372. static void unselect_rows(void)
  373. {
  374. palSetPadMode(TEENSY_PIN15_IOPORT, TEENSY_PIN15, PAL_MODE_INPUT); // hi-Z
  375. palSetPadMode(TEENSY_PIN2_IOPORT, TEENSY_PIN2, PAL_MODE_INPUT); // hi-Z
  376. palSetPadMode(TEENSY_PIN19_IOPORT, TEENSY_PIN19, PAL_MODE_INPUT); // hi-Z
  377. palSetPadMode(TEENSY_PIN20_IOPORT, TEENSY_PIN20, PAL_MODE_INPUT); // hi-Z
  378. palSetPadMode(TEENSY_PIN21_IOPORT, TEENSY_PIN21, PAL_MODE_INPUT); // hi-Z
  379. palSetPadMode(TEENSY_PIN22_IOPORT, TEENSY_PIN22, PAL_MODE_INPUT); // hi-Z
  380. }
  381.  
  382. static void select_row(uint8_t row)
  383. {
  384. (void)row;
  385. // Output low to select
  386. switch (row) {
  387. case 0:
  388. palSetPadMode(TEENSY_PIN15_IOPORT, TEENSY_PIN15, PAL_MODE_OUTPUT_PUSHPULL);
  389. palClearPad(TEENSY_PIN15_IOPORT, TEENSY_PIN15);
  390. break;
  391. case 1:
  392. palSetPadMode(TEENSY_PIN2_IOPORT, TEENSY_PIN2, PAL_MODE_OUTPUT_PUSHPULL);
  393. palClearPad(TEENSY_PIN2_IOPORT, TEENSY_PIN2);
  394. break;
  395. case 2:
  396. palSetPadMode(TEENSY_PIN19_IOPORT, TEENSY_PIN19, PAL_MODE_OUTPUT_PUSHPULL);
  397. palClearPad(TEENSY_PIN19_IOPORT, TEENSY_PIN19);
  398. break;
  399. case 3:
  400. palSetPadMode(TEENSY_PIN20_IOPORT, TEENSY_PIN20, PAL_MODE_OUTPUT_PUSHPULL);
  401. palClearPad(TEENSY_PIN20_IOPORT, TEENSY_PIN20);
  402. break;
  403. case 4:
  404. palSetPadMode(TEENSY_PIN21_IOPORT, TEENSY_PIN21, PAL_MODE_OUTPUT_PUSHPULL);
  405. palClearPad(TEENSY_PIN21_IOPORT, TEENSY_PIN21);
  406. break;
  407. case 5:
  408. palSetPadMode(TEENSY_PIN22_IOPORT, TEENSY_PIN22, PAL_MODE_OUTPUT_PUSHPULL);
  409. palClearPad(TEENSY_PIN22_IOPORT, TEENSY_PIN22);
  410. break;
  411. }
  412. }
Advertisement
Add Comment
Please, Sign In to add comment