diff options
author | Devaev Maxim <[email protected]> | 2020-11-20 05:06:53 +0300 |
---|---|---|
committer | Devaev Maxim <[email protected]> | 2020-11-20 05:06:53 +0300 |
commit | c49cc1b46b41ecd219b967e9a4b0324d0cb05f13 (patch) | |
tree | dcaf89a48d2b1c0733b9ac366cf63c49e4ba730c /hid | |
parent | 649a57e842928f5e1da100ea62545445ee51eb88 (diff) |
refactoring
Diffstat (limited to 'hid')
-rw-r--r-- | hid/src/main.cpp | 92 | ||||
-rw-r--r-- | hid/src/spi.cpp | 83 | ||||
-rw-r--r-- | hid/src/spi.h | 31 |
3 files changed, 131 insertions, 75 deletions
diff --git a/hid/src/main.cpp b/hid/src/main.cpp index 6c2669d7..71d243de 100644 --- a/hid/src/main.cpp +++ b/hid/src/main.cpp @@ -20,31 +20,30 @@ *****************************************************************************/ +// #define CMD_SERIAL Serial1 +// #define CMD_SERIAL_SPEED 115200 +// #define CMD_SERIAL_TIMEOUT 100000 +// -- OR -- +// #define CMD_SPI + #if !(defined(CMD_SERIAL) || defined(CMD_SPI)) # error CMD phy is not defined #endif #include <Arduino.h> -#ifdef CMD_SPI -# include <SPI.h> -#endif #ifdef HID_DYNAMIC # include <avr/eeprom.h> #endif #include "proto.h" +#ifdef CMD_SPI +# include "spi.h" +#endif #include "usb/hid.h" #include "ps2/hid.h" -// #define CMD_SERIAL Serial1 -// #define CMD_SERIAL_SPEED 115200 -// #define CMD_SERIAL_TIMEOUT 100000 -// -- OR -- -// #define CMD_SPI - - // ----------------------------------------------------------------------------- static UsbKeyboard *_usb_kbd = NULL; static UsbMouseAbsolute *_usb_mouse_abs = NULL; @@ -247,55 +246,6 @@ static uint8_t _handleRequest(const uint8_t *data) { // 8 bytes // ----------------------------------------------------------------------------- -#ifdef CMD_SPI -static volatile uint8_t _spi_in[8] = {0}; -static volatile uint8_t _spi_in_index = 0; - -static volatile uint8_t _spi_out[8] = {0}; -static volatile uint8_t _spi_out_index = 0; - -static bool _spiReady() { - return (!_spi_out[0] && _spi_in_index == 8); -} - -static void _spiWrite(const uint8_t *data) { - // Меджик в нулевом байте разрешает начать ответ - for (int index = 7; index >= 0; --index) { - _spi_out[index] = data[index]; - } -} - -ISR(SPI_STC_vect) { - uint8_t in = SPDR; - if (_spi_out[0] && _spi_out_index < 8) { - SPDR = _spi_out[_spi_out_index]; - if (!(SPSR & (1 << WCOL))) { - ++_spi_out_index; - if (_spi_out_index == 8) { - _spi_out_index = 0; - _spi_in_index = 0; - _spi_out[0] = 0; - } - } - } else { - static bool receiving = false; - if (!receiving && in == PROTO::MAGIC) { - receiving = true; - } - if (receiving && _spi_in_index < 8) { - _spi_in[_spi_in_index] = in; - ++_spi_in_index; - } - if (_spi_in_index == 8) { - receiving = false; - } - SPDR = 0; - } -} -#endif - - -// ----------------------------------------------------------------------------- static void _sendResponse(uint8_t code) { static uint8_t prev_code = PROTO::RESP::NONE; if (code == 0) { @@ -344,7 +294,7 @@ static void _sendResponse(uint8_t code) { # ifdef CMD_SERIAL CMD_SERIAL.write(data, 8); # elif defined(CMD_SPI) - _spiWrite(data); + spiWrite(data); # endif } @@ -358,6 +308,9 @@ int main() { unsigned long last = micros(); uint8_t buffer[8]; uint8_t index = 0; +# elif defined(CMD_SPI) + spiBegin(); +# endif while (true) { # ifdef HID_WITH_PS2 @@ -365,6 +318,7 @@ int main() { _ps2_kbd->periodic(); } # endif +# ifdef CMD_SERIAL if (CMD_SERIAL.available() > 0) { buffer[index] = (uint8_t)CMD_SERIAL.read(); if (index == 7) { @@ -384,23 +338,11 @@ int main() { index = 0; } } - } - -# elif defined(CMD_SPI) - pinMode(MISO, OUTPUT); - SPCR = (1 << SPE) | (1 << SPIE); // Slave, SPI En, IRQ En - - while (true) { -# ifdef HID_WITH_PS2 - if (_ps2_kbd) { - _ps2_kbd->periodic(); +# elif defined(CMD_SPI) + if (spiReady()) { + _sendResponse(_handleRequest(spiGet())); } # endif - if (_spiReady()) { - _sendResponse(_handleRequest((const uint8_t *)_spi_in)); - } } - -# endif return 0; } diff --git a/hid/src/spi.cpp b/hid/src/spi.cpp new file mode 100644 index 00000000..fe634a14 --- /dev/null +++ b/hid/src/spi.cpp @@ -0,0 +1,83 @@ +/***************************************************************************** +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +*****************************************************************************/ + + +#include "spi.h" + +#include <Arduino.h> +#include <SPI.h> + + +static volatile uint8_t _spi_in[8] = {0}; +static volatile uint8_t _spi_in_index = 0; + +static volatile uint8_t _spi_out[8] = {0}; +static volatile uint8_t _spi_out_index = 0; + + +void spiBegin() { + pinMode(MISO, OUTPUT); + SPCR = (1 << SPE) | (1 << SPIE); // Slave, SPI En, IRQ En +} + +bool spiReady() { + return (!_spi_out[0] && _spi_in_index == 8); +} + +const uint8_t *spiGet() { + return (const uint8_t *)_spi_in; +} + +void spiWrite(const uint8_t *data) { + // Меджик в нулевом байте разрешает начать ответ + for (int index = 7; index >= 0; --index) { + _spi_out[index] = data[index]; + } +} + + +ISR(SPI_STC_vect) { + uint8_t in = SPDR; + if (_spi_out[0] && _spi_out_index < 8) { + SPDR = _spi_out[_spi_out_index]; + if (!(SPSR & (1 << WCOL))) { + ++_spi_out_index; + if (_spi_out_index == 8) { + _spi_out_index = 0; + _spi_in_index = 0; + _spi_out[0] = 0; + } + } + } else { + static bool receiving = false; + if (!receiving && in != 0) { + receiving = true; + } + if (receiving && _spi_in_index < 8) { + _spi_in[_spi_in_index] = in; + ++_spi_in_index; + } + if (_spi_in_index == 8) { + receiving = false; + } + SPDR = 0; + } +} diff --git a/hid/src/spi.h b/hid/src/spi.h new file mode 100644 index 00000000..10a97e81 --- /dev/null +++ b/hid/src/spi.h @@ -0,0 +1,31 @@ +/***************************************************************************** +# # +# KVMD - The main Pi-KVM daemon. # +# # +# Copyright (C) 2018 Maxim Devaev <[email protected]> # +# # +# This program is free software: you can redistribute it and/or modify # +# it under the terms of the GNU General Public License as published by # +# the Free Software Foundation, either version 3 of the License, or # +# (at your option) any later version. # +# # +# This program is distributed in the hope that it will be useful, # +# but WITHOUT ANY WARRANTY; without even the implied warranty of # +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # +# GNU General Public License for more details. # +# # +# You should have received a copy of the GNU General Public License # +# along with this program. If not, see <https://www.gnu.org/licenses/>. # +# # +*****************************************************************************/ + + +#pragma once + +#include <Arduino.h> + + +void spiBegin(); +bool spiReady(); +const uint8_t *spiGet(); +void spiWrite(const uint8_t *data); |