updated keyboard SPI packet protocol

This commit is contained in:
dekunukem
2021-12-12 21:50:41 +00:00
parent bba708d310
commit 28cfbe79e6
13 changed files with 1950 additions and 1625 deletions

View File

File diff suppressed because one or more lines are too long

View File

Binary file not shown.

View File

@@ -29,7 +29,7 @@ Project File Date: 12/12/2021
Build target 'ibmpc'
compiling main.c...
linking...
Program Size: Code=10556 RO-data=252 RW-data=96 ZI-data=2136
Program Size: Code=12252 RO-data=372 RW-data=96 ZI-data=2136
FromELF: creating hex file...
"ibmpc\ibmpc.axf" - 0 Error(s), 0 Warning(s).

View File

File diff suppressed because it is too large Load Diff

View File

File diff suppressed because it is too large Load Diff

View File

File diff suppressed because it is too large Load Diff

View File

@@ -155,7 +155,7 @@ F (..\Inc\helpers.h)(0x61B52C42)()
-I.\RTE\_ibmpc
-IC:\Users\allen\AppData\Local\Arm\Packs\ARM\CMSIS\5.6.0\CMSIS\Core\Include
-IC:\Users\allen\AppData\Local\Arm\Packs\ARM\CMSIS\5.6.0\CMSIS\Core\Include
-IC:\Users\allen\AppData\Local\Arm\Packs\Keil\STM32F0xx_DFP\2.0.0\Drivers\CMSIS\Device\ST\STM32F0xx\Include
@@ -263,7 +263,7 @@ I (../Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal_tim_ex.h)(0x5DFA0893)
-IC:\Users\allen\AppData\Local\Arm\Packs\Keil\STM32F0xx_DFP\2.0.0\Drivers\CMSIS\Device\ST\STM32F0xx\Include
-D__UVISION_VERSION="529" -D_RTE_ -DSTM32F072xB -DUSE_HAL_DRIVER -DSTM32F072xB
-D__UVISION_VERSION="529" -D_RTE_ -DSTM32F072xB -DUSE_HAL_DRIVER -DSTM32F072xB
-o ibmpc\stm32f0xx_it.o --omf_browse ibmpc\stm32f0xx_it.crf --depend ibmpc\stm32f0xx_it.d)
I (../Drivers/STM32F0xx_HAL_Driver/Inc/stm32f0xx_hal.h)(0x5DFA0893)

View File

Binary file not shown.

View File

Binary file not shown.

View File

Binary file not shown.

View File

Binary file not shown.

View File

@@ -125,7 +125,7 @@ void HAL_SPI_TxRxCpltCallback(SPI_HandleTypeDef *hspi)
}
if(backup_spi1_recv_buf[SPI_BUF_INDEX_MSG_TYPE] == SPI_MOSI_MSG_KB_EVENT)
ps2kb_buf_add(&my_ps2kb_buf, backup_spi1_recv_buf[6], backup_spi1_recv_buf[8]);
ps2kb_buf_add(&my_ps2kb_buf, backup_spi1_recv_buf[4], backup_spi1_recv_buf[6]);
if(backup_spi1_recv_buf[SPI_BUF_INDEX_MSG_TYPE] == SPI_MOSI_MSG_MOUSE_EVENT)
{
@@ -318,11 +318,11 @@ int main(void)
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
// ps2mouse_update();
ps2mouse_update();
// serial_mouse_update();
// ps2kb_update();
printf("%d\n", mcp4451_write_wiper(3, 20));
HAL_Delay(500);
ps2kb_update();
// printf("%d\n", mcp4451_write_wiper(3, 20));
// HAL_Delay(500);
}
/* USER CODE END 3 */

View File

@@ -72,8 +72,9 @@ SPI_MISO_MSG_KB_LED_REQ = 1
SPI_MOSI_MAGIC = 0xde
SPI_MISO_MAGIC = 0xcd
keyboard_spi_msg_header = [SPI_MOSI_MAGIC, 0, SPI_MOSI_MSG_KEYBOARD_EVENT, 0]
mouse_spi_msg_template = [SPI_MOSI_MAGIC, 0, SPI_MOSI_MSG_MOUSE_EVENT, 0] + [0]*28
keyboard_spi_msg_header = [SPI_MOSI_MAGIC, 0, SPI_MOSI_MSG_KEYBOARD_EVENT] + [0]*29
mouse_spi_msg_template = [SPI_MOSI_MAGIC, 0, SPI_MOSI_MSG_MOUSE_EVENT] + [0]*29
gamepad_spi_msg_header = [SPI_MOSI_MAGIC, 0, SPI_MOSI_MSG_GAMEPAD_EVENT] + [0]*29
def make_spi_msg_ack():
return [SPI_MOSI_MAGIC, 0, SPI_MOSI_MSG_REQ_ACK] + [0]*29
@@ -83,6 +84,14 @@ def get_01(value):
return 1
return 0
def make_keyboard_spi_packet(input_data, kbd_id):
result = list(keyboard_spi_msg_header)
result[3] = kbd_id
result[4] = input_data[2]
result[5] = input_data[3]
result[6] = input_data[4]
return result
def change_kb_led(ps2kb_led_byte):
led_file_list = os.listdir(led_device_path)
capslock_list = [os.path.join(led_device_path, x) for x in led_file_list if 'capslock' in x]
@@ -118,9 +127,7 @@ def raw_input_event_worker():
continue
data = list(data[8:])
if data[0] == EV_KEY:
to_transfer = keyboard_spi_msg_header + data + [0]*20
to_transfer[3] = keyboard_opened_device_dict[key][1]
pcard_spi.xfer(to_transfer)
pcard_spi.xfer(make_keyboard_spi_packet(data, keyboard_opened_device_dict[key][1]))
for key in list(mouse_opened_device_dict):
try:
@@ -138,7 +145,6 @@ def raw_input_event_worker():
4 - 8 button status
"""
data = list(data[8:])
# print(data)
if data[0] == EV_REL:
if data[2] == REL_X:
mouse_spi_packet_dict["x"] = data[4:6]
@@ -149,6 +155,7 @@ def raw_input_event_worker():
if data[0] == EV_KEY:
key_code = data[3] * 256 + data[2]
if 0x110 <= key_code <= 0x117:
print(data)
mouse_button_state_list[data[2]-16] = data[4]
to_transfer = list(mouse_spi_msg_template)
to_transfer[10:13] = data[2:5]
@@ -160,9 +167,7 @@ def raw_input_event_worker():
so need to handle keyboard presses here too for compatibility
"""
if 0x1 <= key_code <= 127:
to_transfer = keyboard_spi_msg_header + data + [0]*20
to_transfer[3] = mouse_opened_device_dict[key][1]
pcard_spi.xfer(to_transfer)
pcard_spi.xfer(make_keyboard_spi_packet(data, mouse_opened_device_dict[key][1]))
if data[0] == EV_SYN and data[2] == SYN_REPORT and len(mouse_spi_packet_dict) > 0:
to_transfer = list(mouse_spi_msg_template)
@@ -177,6 +182,23 @@ def raw_input_event_worker():
mouse_spi_packet_dict.clear()
pcard_spi.xfer(to_transfer)
for key in list(gamepad_opened_device_dict):
try:
data = gamepad_opened_device_dict[key][0].read(16)
except OSError:
gamepad_opened_device_dict[key][0].close()
del gamepad_opened_device_dict[key]
print("gamepad disappeared:", key)
continue
if data is None:
continue
data = list(data[8:])
# print(data)
# if data[0] == EV_KEY:
# to_transfer = keyboard_spi_msg_header + data + [0]*20
# to_transfer[3] = gamepad_opened_device_dict[key][1]
# pcard_spi.xfer(to_transfer)
if GPIO.event_detected(SLAVE_REQ_PIN):
slave_result = None
for x in range(2):
@@ -235,6 +257,22 @@ def usb_device_scan_worker():
print("mouse open exception:", e)
continue
for item in gamepad_list:
if item not in gamepad_opened_device_dict:
try:
this_file = open(item, "rb")
os.set_blocking(this_file.fileno(), False)
device_index = 0
try:
device_index = sum([int(x) for x in item.split(':')[1].split('.')][:2])
except:
pass
gamepad_opened_device_dict[item] = (this_file, device_index)
print("opened gamepad", gamepad_opened_device_dict[item][1], ':' , item)
except Exception as e:
print("gamepad open exception:", e)
continue
raw_input_event_parser_thread = threading.Thread(target=raw_input_event_worker, daemon=True)
usb_device_scan_thread = threading.Thread(target=usb_device_scan_worker, daemon=True)