pico hid: refactoring

This commit is contained in:
Maxim Devaev 2023-08-24 12:56:13 +03:00
parent cbea3ce12f
commit e78d3e03ec
8 changed files with 118 additions and 36 deletions

View File

@ -9,8 +9,9 @@ target_sources(${target_name} PRIVATE
ph_usb_mouse.c
ph_ps2.c
ph_cmds.c
ph_spi.c
ph_uart.c
ph_com.c
ph_com_spi.c
ph_com_uart.c
ph_debug.c
# TODO: PS2: ${PS2_PATH}/foo.c
)

View File

@ -29,22 +29,12 @@
#include "ph_outputs.h"
#include "ph_usb.h"
#include "ph_ps2.h"
#include "ph_spi.h"
#include "ph_uart.h"
#include "ph_com.h"
#include "ph_proto.h"
#include "ph_cmds.h"
#include "ph_debug.h"
#define _COMM_PIN 22
static bool _comm_use_spi = true;
#define _COMM(x_func, ...) { \
if (_comm_use_spi) { ph_spi_##x_func(__VA_ARGS__); } \
else { ph_uart_##x_func(__VA_ARGS__); } \
}
static bool _reset_required = false;
@ -104,7 +94,7 @@ static void _send_response(u8 code) {
ph_split16(ph_crc16(resp, 6), &resp[6], &resp[7]);
_COMM(write, resp);
ph_com_write(resp);
if (_reset_required) {
watchdog_reboot(0, 0, 100); // Даем немного времени чтобы отправить ответ, а потом ребутимся
@ -126,19 +116,13 @@ int main(void) {
ph_outputs_init();
ph_usb_init();
ph_ps2_init();
gpio_init(_COMM_PIN);
gpio_set_dir(_COMM_PIN, GPIO_IN);
gpio_pull_up(_COMM_PIN);
sleep_ms(10); // Нужен небольшой слип для активации pull-up
_comm_use_spi = gpio_get(_COMM_PIN);
_COMM(init, _data_handler, _timeout_handler);
ph_com_init(_data_handler, _timeout_handler);
while (true) {
ph_usb_task();
ph_ps2_task();
if (!_reset_required) {
_COMM(task);
ph_com_task();
//ph_debug_act_pulse(100);
}
}

67
hid/pico/src/ph_com.c Normal file
View File

@ -0,0 +1,67 @@
/*****************************************************************************
# #
# KVMD - The main PiKVM daemon. #
# #
# Copyright (C) 2018-2023 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 "ph_com.h"
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "ph_types.h"
#include "ph_com_spi.h"
#include "ph_com_uart.h"
#define _USE_SPI_PIN 22
static bool _use_spi = true;
void ph_com_init(void (*data_cb)(const u8 *), void (*timeout_cb)(void)) {
gpio_init(_USE_SPI_PIN);
gpio_set_dir(_USE_SPI_PIN, GPIO_IN);
gpio_pull_up(_USE_SPI_PIN);
sleep_ms(10); // Нужен небольшой слип для активации pull-up
_use_spi = gpio_get(_USE_SPI_PIN);
if (_use_spi) {
ph_com_spi_init(data_cb, timeout_cb);
} else {
ph_com_uart_init(data_cb, timeout_cb);
}
}
void ph_com_task(void) {
if (_use_spi) {
ph_com_spi_task();
} else {
ph_com_uart_task();
}
}
void ph_com_write(const u8 *data) {
if (_use_spi) {
ph_com_spi_write(data);
} else {
ph_com_uart_write(data);
}
}

View File

@ -25,6 +25,6 @@
#include "ph_types.h"
void ph_spi_init(void (*data_cb)(const u8 *), void (*timeout_cb)(void));
void ph_spi_task(void);
void ph_spi_write(const u8 *data);
void ph_com_init(void (*data_cb)(const u8 *), void (*timeout_cb)(void));
void ph_com_task(void);
void ph_com_write(const u8 *data);

View File

@ -20,7 +20,7 @@
*****************************************************************************/
#include "ph_spi.h"
#include "ph_com_spi.h"
#include "hardware/gpio.h"
#include "hardware/irq.h"
@ -51,7 +51,7 @@ static void (*_data_cb)(const u8 *) = NULL;
static void _xfer_isr(void);
void ph_spi_init(void (*data_cb)(const u8 *), void (*timeout_cb)(void)) {
void ph_com_spi_init(void (*data_cb)(const u8 *), void (*timeout_cb)(void)) {
_data_cb = data_cb;
(void)timeout_cb;
@ -70,13 +70,13 @@ void ph_spi_init(void (*data_cb)(const u8 *), void (*timeout_cb)(void)) {
irq_set_enabled(_IRQ, true);
}
void ph_spi_task(void) {
void ph_com_spi_task(void) {
if (!_out_buf[0] && _in_index == 8) {
_data_cb((const u8 *)_in_buf);
}
}
void ph_spi_write(const u8 *data) {
void ph_com_spi_write(const u8 *data) {
// Меджик в нулевом байте разрешает начать ответ
for (s8 i = 7; i >= 0; --i) {
_out_buf[i] = data[i];

View File

@ -25,6 +25,6 @@
#include "ph_types.h"
void ph_uart_init(void (*data_cb)(const u8 *), void (*timeout_cb)(void));
void ph_uart_task(void);
void ph_uart_write(const u8 *data);
void ph_com_spi_init(void (*data_cb)(const u8 *), void (*timeout_cb)(void));
void ph_com_spi_task(void);
void ph_com_spi_write(const u8 *data);

View File

@ -20,7 +20,7 @@
*****************************************************************************/
#include "ph_uart.h"
#include "ph_com_uart.h"
#include "pico/stdlib.h"
#include "hardware/gpio.h"
@ -44,7 +44,7 @@ static void (*_data_cb)(const u8 *) = NULL;
static void (*_timeout_cb)(void) = NULL;
void ph_uart_init(void (*data_cb)(const u8 *), void (*timeout_cb)(void)) {
void ph_com_uart_init(void (*data_cb)(const u8 *), void (*timeout_cb)(void)) {
_data_cb = data_cb;
_timeout_cb = timeout_cb;
uart_init(_BUS, _SPEED);
@ -52,7 +52,7 @@ void ph_uart_init(void (*data_cb)(const u8 *), void (*timeout_cb)(void)) {
gpio_set_function(_TX_PIN, GPIO_FUNC_UART);
}
void ph_uart_task(void) {
void ph_com_uart_task(void) {
if (uart_is_readable(_BUS)) {
_buf[_index] = (u8)uart_getc(_BUS);
if (_index == 7) {
@ -70,6 +70,6 @@ void ph_uart_task(void) {
}
}
void ph_uart_write(const u8 *data) {
void ph_com_uart_write(const u8 *data) {
uart_write_blocking(_BUS, data, 8);
}

View File

@ -0,0 +1,30 @@
/*****************************************************************************
# #
# KVMD - The main PiKVM daemon. #
# #
# Copyright (C) 2018-2023 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 "ph_types.h"
void ph_com_uart_init(void (*data_cb)(const u8 *), void (*timeout_cb)(void));
void ph_com_uart_task(void);
void ph_com_uart_write(const u8 *data);