qmk_firmware

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

lagrange.c (1938B)


      1 /* Copyright 2020 Dimitris Papavasiliou <dpapavas@protonmail.ch>
      2  *
      3  * This program is free software: you can redistribute it and/or modify
      4  * it under the terms of the GNU General Public License as published by
      5  * the Free Software Foundation, either version 3 of the License, or
      6  * (at your option) any later version.
      7  *
      8  * This program is distributed in the hope that it will be useful,
      9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
     10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     11  * GNU General Public License for more details.
     12  *
     13  * You should have received a copy of the GNU General Public License
     14  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
     15  */
     16 
     17 #include <LUFA/Drivers/USB/USB.h>
     18 
     19 #include "lagrange.h"
     20 
     21 #ifndef SPLIT_USB_TIMEOUT_POLL
     22 #    define SPLIT_USB_TIMEOUT_POLL 10
     23 #endif
     24 
     25 /* Instead of timing out, the slave waits indefinitely for the other
     26  * side to signal that it has become master.  This avoids both sides
     27  * assuming the slave role when the USB port is powered but not
     28  * otherwise active (e.g. when the host is turned off, or suspended).
     29  * The SPI SS line is used for signaling.  On power-up it is
     30  * configured as input with pull-up enabled.  When one side assumes
     31  * the master role, it reconfigures the line for SPI, and pulls it low
     32  * to select the slave, which doubles as the signal. */
     33 
     34 bool is_keyboard_master(void) {
     35     static int8_t is_master = -1;
     36 
     37     if (is_master < 0) {
     38         while (gpio_read_pin(SPI_SS_PIN)) {
     39             if (USB_Device_IsAddressSet()) {
     40                 is_master = 1;
     41                 return is_master;
     42             }
     43             wait_ms(SPLIT_USB_TIMEOUT_POLL);
     44         }
     45 
     46         is_master = 0;
     47 
     48         USB_Disable();
     49         USB_DeviceState = DEVICE_STATE_Unattached;
     50     }
     51 
     52     return is_master;
     53 }
     54 
     55 void keyboard_pre_init_kb(void) {
     56     gpio_set_pin_input_high(SPI_SS_PIN);
     57 
     58     keyboard_pre_init_user();
     59 }