EndOfNight: Keyboard Protocol Porting WIP
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1,3 @@
|
|||||||
.vscode
|
.vscode
|
||||||
build
|
build
|
||||||
|
src/build
|
||||||
|
|||||||
@@ -6,8 +6,8 @@ set(CMAKE_CXX_STANDARD 17)
|
|||||||
set(PICO_SDK_PATH "${CMAKE_CURRENT_LIST_DIR}/../libs/pico-sdk")
|
set(PICO_SDK_PATH "${CMAKE_CURRENT_LIST_DIR}/../libs/pico-sdk")
|
||||||
include("${PICO_SDK_PATH}/external/pico_sdk_import.cmake")
|
include("${PICO_SDK_PATH}/external/pico_sdk_import.cmake")
|
||||||
|
|
||||||
set(PICO_PIO_USB_DIR "${CMAKE_CURRENT_LIST_DIR}/../libs/pico-pio-hirsute")
|
# set(PICO_PIO_USB_DIR "${CMAKE_CURRENT_LIST_DIR}/../libs/pico-pio-hirsute")
|
||||||
add_subdirectory(${PICO_PIO_USB_DIR} pico_pio_usb)
|
# add_subdirectory(${PICO_PIO_USB_DIR} pico_pio_usb)
|
||||||
|
|
||||||
set(target_name hirsute)
|
set(target_name hirsute)
|
||||||
|
|
||||||
@@ -19,22 +19,23 @@ add_executable(${target_name}
|
|||||||
|
|
||||||
target_sources(${target_name} PRIVATE
|
target_sources(${target_name} PRIVATE
|
||||||
hirsute.c
|
hirsute.c
|
||||||
${PICO_SDK_PATH}/lib/tinyusb/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c
|
# ${PICO_SDK_PATH}/lib/tinyusb/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c
|
||||||
${PICO_SDK_PATH}/lib/tinyusb/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c
|
# ${PICO_SDK_PATH}/lib/tinyusb/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c
|
||||||
)
|
)
|
||||||
|
|
||||||
pico_sdk_init()
|
pico_sdk_init()
|
||||||
|
|
||||||
pico_enable_stdio_usb(${target_name} 0)
|
pico_enable_stdio_usb(${target_name} 1)
|
||||||
pico_enable_stdio_uart(${target_name} 1)
|
pico_enable_stdio_uart(${target_name} 0)
|
||||||
|
|
||||||
target_link_options(${target_name} PRIVATE -Xlinker --print-memory-usage)
|
target_link_options(${target_name} PRIVATE -Xlinker --print-memory-usage)
|
||||||
target_compile_options(${target_name} PRIVATE -Wall -Wextra)
|
target_compile_options(${target_name} PRIVATE -Wall -Wextra)
|
||||||
|
|
||||||
target_compile_definitions(${target_name} PRIVATE PIO_USB_USE_TINYUSB)
|
# target_compile_definitions(${target_name} PRIVATE PIO_USB_USE_TINYUSB)
|
||||||
target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_LIST_DIR})
|
# target_include_directories(${target_name} PRIVATE ${CMAKE_CURRENT_LIST_DIR})
|
||||||
|
|
||||||
target_link_libraries(${target_name} PRIVATE pico_stdlib pico_multicore pico_pio_usb tinyusb_host tinyusb_board)
|
# target_link_libraries(${target_name} PRIVATE pico_stdlib pico_multicore pico_pio_usb tinyusb_host tinyusb_board)
|
||||||
|
target_link_libraries(${target_name} PRIVATE pico_stdlib pico_multicore)
|
||||||
|
|
||||||
# Generate hex file
|
# Generate hex file
|
||||||
pico_add_extra_outputs(${target_name})
|
pico_add_extra_outputs(${target_name})
|
||||||
|
|||||||
18
src/config.h
18
src/config.h
@@ -1,6 +1,22 @@
|
|||||||
#ifndef CONFIG_H
|
#ifndef CONFIG_H
|
||||||
#define CONFIG_H
|
#define CONFIG_H
|
||||||
|
|
||||||
#define BAUD_RATE 115200
|
#define EN_USB 0 // Enable USB Host
|
||||||
|
|
||||||
|
#define USB_DP 16 // USB D+ pin on GP16
|
||||||
|
|
||||||
|
#define MAC_MOUSE_X2
|
||||||
|
#define MAC_MOUSE_X1
|
||||||
|
#define MAC_MOUSE_Y2
|
||||||
|
#define MAC_MOUSE_Y1
|
||||||
|
#define MAC_MOUSE_SW
|
||||||
|
#define MAC_MOUSE_5V
|
||||||
|
|
||||||
|
#define MAC_KB_CLOCK 3
|
||||||
|
#define MAC_KB_DATA 2
|
||||||
|
#define MAC_KB_5V
|
||||||
|
|
||||||
|
#define NULL_TRANSITION 0x7b
|
||||||
|
#define CAPS_LOCK 0x73
|
||||||
|
|
||||||
#endif // CONFIG_H
|
#endif // CONFIG_H
|
||||||
|
|||||||
117
src/hirsute.c
117
src/hirsute.c
@@ -16,44 +16,137 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
#include "pico/stdlib.h"
|
#include "pico/stdlib.h"
|
||||||
|
#include "pico/util/queue.h"
|
||||||
#include "pico/multicore.h"
|
#include "pico/multicore.h"
|
||||||
#include "pico/bootrom.h"
|
#include "pico/bootrom.h"
|
||||||
|
#if EN_USB
|
||||||
#include "pio_usb.h"
|
#include "pio_usb.h"
|
||||||
#include "tusb.h"
|
#include "tusb.h"
|
||||||
#include "bsp/board.h"
|
#include "bsp/board.h"
|
||||||
|
#endif
|
||||||
#include "hardware/uart.h"
|
#include "hardware/uart.h"
|
||||||
#include "hardware/clocks.h"
|
#include "hardware/clocks.h"
|
||||||
|
|
||||||
#include "config.h"
|
void setPullup(uint PIN);
|
||||||
|
void holdForEnable();
|
||||||
|
uint8_t readCommand();
|
||||||
|
void sendByte(uint8_t payload);
|
||||||
|
void sendNextKey();
|
||||||
|
|
||||||
|
queue_t key_queue;
|
||||||
|
|
||||||
|
#if EN_USB
|
||||||
void core_one() {
|
void core_one() {
|
||||||
sleep_ms(10);
|
sleep_ms(10);
|
||||||
pio_usb_configuration_t pio_config = PIO_USB_DEFAULT_CONFIG;
|
pio_usb_configuration_t pio_config = PIO_USB_DEFAULT_CONFIG;
|
||||||
pio_config.pin_dp = 16; // USB D+ pin on GP16
|
pio_config.pin_dp = USB_DP;
|
||||||
tuh_configure(1, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_config);
|
tuh_configure(1, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_config);
|
||||||
tuh_init(1);
|
tuh_init(1);
|
||||||
while (true) {
|
while (true) {
|
||||||
tuh_task();
|
tuh_task();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
board_init();
|
#if EN_USB
|
||||||
|
board_init();
|
||||||
|
#endif
|
||||||
|
stdio_init_all();
|
||||||
set_sys_clock_khz(120000, true);
|
set_sys_clock_khz(120000, true);
|
||||||
uart_init(UART_ID, BAUD_RATE);
|
|
||||||
gpio_set_function(UART_TX_PIN, GPIO_FUNC_UART);
|
|
||||||
gpio_set_function(UART_RX_PIN, GPIO_FUNC_UART);
|
|
||||||
sleep_ms(10);
|
sleep_ms(10);
|
||||||
|
queue_init(&key_queue, sizeof(uint8_t), 1024);
|
||||||
multicore_reset_core1();
|
multicore_reset_core1();
|
||||||
multicore_launch_core1(core_one);
|
#if EN_USB
|
||||||
board_led_write(true);
|
multicore_launch_core1(core_one);
|
||||||
|
#endif
|
||||||
|
// Init Pins for Keyboard
|
||||||
|
gpio_init(MAC_KB_CLOCK);
|
||||||
|
gpio_set_dir(MAC_KB_CLOCK, GPIO_OUT);
|
||||||
|
gpio_init(MAC_KB_DATA);
|
||||||
|
setPullup(MAC_KB_DATA);
|
||||||
|
while (true){
|
||||||
|
switch(readCommand()) {
|
||||||
|
case 0x10:
|
||||||
|
// Key Requested
|
||||||
|
|
||||||
while (true) {
|
break;
|
||||||
stdio_flush();
|
case 0x14:
|
||||||
sleep_us(10);
|
// Send Key
|
||||||
|
break;
|
||||||
|
case 0x16:
|
||||||
|
// Requests Model Number
|
||||||
|
sendByte(0x0b);
|
||||||
|
break;
|
||||||
|
case 0x36:
|
||||||
|
// Test?
|
||||||
|
sendByte(0x7d);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void setPullup(uint PIN){
|
||||||
|
gpio_set_dir(PIN, GPIO_IN);
|
||||||
|
gpio_pull_up(PIN);
|
||||||
|
}
|
||||||
|
|
||||||
|
void holdForEnable() {
|
||||||
|
printf("Waiting for keyboard enable...\n");
|
||||||
|
while (gpio_get(MAC_KB_DATA)) {
|
||||||
|
printf(".");
|
||||||
|
sleep_ms(10);
|
||||||
|
}
|
||||||
|
printf("\nKeyboard enabled.\n");
|
||||||
|
sleep_us(180);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t readCommand() {
|
||||||
|
setPullup(MAC_KB_DATA);
|
||||||
|
sleep_us(20);
|
||||||
|
while (gpio_get(MAC_KB_DATA));
|
||||||
|
sleep_us(400);
|
||||||
|
//readByte
|
||||||
|
uint8_t command = 0;
|
||||||
|
for (uint8_t i = 0; i < 8; i++) {
|
||||||
|
gpio_put(MAC_KB_CLOCK, 0);
|
||||||
|
sleep_us(180);
|
||||||
|
gpio_put(MAC_KB_CLOCK, 1);
|
||||||
|
sleep_us(80);
|
||||||
|
command = (command << 1) | gpio_get(MAC_KB_DATA);
|
||||||
|
sleep_us(140);
|
||||||
|
}
|
||||||
|
while (!gpio_get(MAC_KB_DATA));
|
||||||
|
gpio_set_dir(MAC_KB_DATA, GPIO_OUT);
|
||||||
|
sleep_us(20);
|
||||||
|
return command;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendByte(uint8_t payload) {
|
||||||
|
for (uint8_t i = 0; i < 8; i++) {
|
||||||
|
gpio_put(MAC_KB_DATA, (payload & i));
|
||||||
|
sleep_us(40);
|
||||||
|
gpio_put(MAC_KB_CLOCK, 0);
|
||||||
|
sleep_us(120);
|
||||||
|
gpio_put(MAC_KB_CLOCK, 1);
|
||||||
|
sleep_us(170);
|
||||||
|
}
|
||||||
|
gpio_put(MAC_KB_DATA, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendNextKey() {
|
||||||
|
// Send the next key from the cache (To be filled by a USB HID device)
|
||||||
|
uint64_t startTime = time_us_64();
|
||||||
|
while (time_us_64() - startTime < 250000) { // 250ms timeout
|
||||||
|
uint8_t key;
|
||||||
|
if (queue_try_remove(&key_queue, &key)) {
|
||||||
|
sendByte(key); // TODO: Replace with mac-plus-ps2's sendKey()
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
sleep_us(2);
|
||||||
|
}
|
||||||
|
sendByte(NULL_TRANSITION); // Send null transition if no key is available
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user