qmk_firmware

QMK firmware for my keyboards (Corne, Sweep Ferris) and trackball (Ploopy Adept)
Log | Files | Refs | Submodules | LICENSE

k320.c (2882B)


      1 /* Copyright 2021 kuenhlee, Don Kjer, Tyler Tidman
      2  * Copyright 2021 Simon Arlott
      3  *
      4  * This program is free software: you can redistribute it and/or modify
      5  * it under the terms of the GNU General Public License as published by
      6  * the Free Software Foundation, either version 2 of the License, or
      7  * (at your option) any later version.
      8  *
      9  * This program is distributed in the hope that it will be useful,
     10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     12  * GNU General Public License for more details.
     13  *
     14  * You should have received a copy of the GNU General Public License
     15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
     16  */
     17 
     18 #include "k320.h"
     19 #include <ch.h>
     20 #include <hal.h>
     21 
     22 /* Private Functions */
     23 void off_all_leds(void) {
     24 #ifdef LED_NUM_LOCK_PIN
     25     gpio_write_pin_high(LED_NUM_LOCK_PIN);
     26 #endif
     27     gpio_write_pin_high(LED_CAPS_LOCK_PIN);
     28     gpio_write_pin_high(LED_SCROLL_LOCK_PIN);
     29     gpio_write_pin_high(LED_WIN_LOCK_PIN);
     30     gpio_write_pin_high(LED_MR_LOCK_PIN);
     31 }
     32 
     33 void on_all_leds(void) {
     34 #ifdef LED_NUM_LOCK_PIN
     35     gpio_write_pin_low(LED_NUM_LOCK_PIN);
     36 #endif
     37     gpio_write_pin_low(LED_CAPS_LOCK_PIN);
     38     gpio_write_pin_low(LED_SCROLL_LOCK_PIN);
     39     gpio_write_pin_low(LED_WIN_LOCK_PIN);
     40     gpio_write_pin_low(LED_MR_LOCK_PIN);
     41 }
     42 
     43 /* WinLock and MR LEDs are non-standard. Need to override led init */
     44 void led_init_ports(void) {
     45 #ifdef LED_NUM_LOCK_PIN
     46     gpio_set_pin_output(LED_NUM_LOCK_PIN);
     47 #endif
     48     gpio_set_pin_output(LED_CAPS_LOCK_PIN);
     49     gpio_set_pin_output(LED_SCROLL_LOCK_PIN);
     50     gpio_set_pin_output(LED_WIN_LOCK_PIN);
     51     gpio_set_pin_output(LED_MR_LOCK_PIN);
     52     off_all_leds();
     53 }
     54 
     55 #ifndef WINLOCK_DISABLED
     56 bool process_record_kb(uint16_t keycode, keyrecord_t *record) {
     57     switch (keycode) {
     58         case GU_TOGG:
     59             if (record->event.pressed) {
     60                 // Toggle LED on key press
     61                 gpio_toggle_pin(LED_WIN_LOCK_PIN);
     62             }
     63             break;
     64     }
     65     return process_record_user(keycode, record);
     66 }
     67 #endif /* WINLOCK_DISABLED */
     68 
     69 #ifndef HW_RESET_PIN_DISABLED
     70 static void hardware_reset_cb(void *arg) {
     71     chSysLockFromISR();
     72     bootloader_jump();
     73     chSysUnlockFromISR();
     74 }
     75 #endif
     76 
     77 void keyboard_pre_init_kb(void) {
     78     gpio_set_pin_input_high(HARDWARE_RESET_PIN);
     79 
     80 #ifndef HW_RESET_PIN_DISABLED
     81     /* Jump to bootloader when the hardware reset button is pressed */
     82     palEnablePadEvent(PAL_PORT(HARDWARE_RESET_PIN), PAL_PAD(HARDWARE_RESET_PIN), PAL_EVENT_MODE_FALLING_EDGE);
     83     palSetPadCallback(PAL_PORT(HARDWARE_RESET_PIN), PAL_PAD(HARDWARE_RESET_PIN), hardware_reset_cb, NULL);
     84 
     85     /* The interrupt is edge-triggered so check that it's not already pressed */
     86     if (!gpio_read_pin(HARDWARE_RESET_PIN)) {
     87         bootloader_jump();
     88     }
     89 #endif
     90 
     91     keyboard_pre_init_user();
     92 }