refactoring

This commit is contained in:
Devaev Maxim 2020-11-20 05:06:53 +03:00
parent 649a57e842
commit c49cc1b46b
3 changed files with 131 additions and 75 deletions

View File

@ -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;
@ -246,55 +245,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;
@ -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;
}

83
hid/src/spi.cpp Normal file
View File

@ -0,0 +1,83 @@
/*****************************************************************************
# #
# KVMD - The main Pi-KVM daemon. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# 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;
}
}

31
hid/src/spi.h Normal file
View File

@ -0,0 +1,31 @@
/*****************************************************************************
# #
# KVMD - The main Pi-KVM daemon. #
# #
# Copyright (C) 2018 Maxim Devaev <mdevaev@gmail.com> #
# #
# 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);