diff options
author | Maxim Devaev <[email protected]> | 2023-08-24 12:56:13 +0300 |
---|---|---|
committer | Maxim Devaev <[email protected]> | 2023-08-24 12:56:13 +0300 |
commit | e78d3e03ec91e67e3b3b7c6397d1231a186a0b55 (patch) | |
tree | 396b35a72c15c048d44652d5264b57fb6e1d20e9 /hid | |
parent | cbea3ce12fd7e26c750b7bb82236848621a4f17a (diff) |
pico hid: refactoring
Diffstat (limited to 'hid')
-rw-r--r-- | hid/pico/src/CMakeLists.txt | 5 | ||||
-rw-r--r-- | hid/pico/src/main.c | 24 | ||||
-rw-r--r-- | hid/pico/src/ph_com.c | 67 | ||||
-rw-r--r-- | hid/pico/src/ph_com.h (renamed from hid/pico/src/ph_spi.h) | 6 | ||||
-rw-r--r-- | hid/pico/src/ph_com_spi.c (renamed from hid/pico/src/ph_spi.c) | 8 | ||||
-rw-r--r-- | hid/pico/src/ph_com_spi.h (renamed from hid/pico/src/ph_uart.h) | 6 | ||||
-rw-r--r-- | hid/pico/src/ph_com_uart.c (renamed from hid/pico/src/ph_uart.c) | 8 | ||||
-rw-r--r-- | hid/pico/src/ph_com_uart.h | 30 |
8 files changed, 118 insertions, 36 deletions
diff --git a/hid/pico/src/CMakeLists.txt b/hid/pico/src/CMakeLists.txt index 62cf440a..5353a0c8 100644 --- a/hid/pico/src/CMakeLists.txt +++ b/hid/pico/src/CMakeLists.txt @@ -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 ) diff --git a/hid/pico/src/main.c b/hid/pico/src/main.c index 7f58e876..a1c9531f 100644 --- a/hid/pico/src/main.c +++ b/hid/pico/src/main.c @@ -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); } } diff --git a/hid/pico/src/ph_com.c b/hid/pico/src/ph_com.c new file mode 100644 index 00000000..697edac9 --- /dev/null +++ b/hid/pico/src/ph_com.c @@ -0,0 +1,67 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2023 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 "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); + } +} diff --git a/hid/pico/src/ph_spi.h b/hid/pico/src/ph_com.h index 8ae6b792..9019d7f7 100644 --- a/hid/pico/src/ph_spi.h +++ b/hid/pico/src/ph_com.h @@ -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); diff --git a/hid/pico/src/ph_spi.c b/hid/pico/src/ph_com_spi.c index 0a016f80..5da80a6f 100644 --- a/hid/pico/src/ph_spi.c +++ b/hid/pico/src/ph_com_spi.c @@ -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]; diff --git a/hid/pico/src/ph_uart.h b/hid/pico/src/ph_com_spi.h index e807f8b0..61f7c932 100644 --- a/hid/pico/src/ph_uart.h +++ b/hid/pico/src/ph_com_spi.h @@ -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); diff --git a/hid/pico/src/ph_uart.c b/hid/pico/src/ph_com_uart.c index 705b0a13..593f5640 100644 --- a/hid/pico/src/ph_uart.c +++ b/hid/pico/src/ph_com_uart.c @@ -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); } diff --git a/hid/pico/src/ph_com_uart.h b/hid/pico/src/ph_com_uart.h new file mode 100644 index 00000000..11e372da --- /dev/null +++ b/hid/pico/src/ph_com_uart.h @@ -0,0 +1,30 @@ +/***************************************************************************** +# # +# KVMD - The main PiKVM daemon. # +# # +# Copyright (C) 2018-2023 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 "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); |