diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-09-27 17:25:36 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2024-09-27 17:25:36 -0600 |
commit | e5e15c04bc58c34906e6d7cfcbad68d1a5617563 (patch) | |
tree | 580f5fb0fafc7e974c969fc8aae229205c836195 /cmd/sbc_harness | |
parent | 71e1a86a033c380f85dd300d788af63bfef25bab (diff) |
wip
Diffstat (limited to 'cmd/sbc_harness')
-rw-r--r-- | cmd/sbc_harness/CMakeLists.txt | 23 | ||||
-rw-r--r-- | cmd/sbc_harness/config/config.h | 54 | ||||
-rw-r--r-- | cmd/sbc_harness/config/tusb_config.h | 107 | ||||
-rw-r--r-- | cmd/sbc_harness/main.c | 48 | ||||
-rw-r--r-- | cmd/sbc_harness/usb_keyboard.c | 113 | ||||
-rw-r--r-- | cmd/sbc_harness/usb_keyboard.h | 17 |
6 files changed, 362 insertions, 0 deletions
diff --git a/cmd/sbc_harness/CMakeLists.txt b/cmd/sbc_harness/CMakeLists.txt new file mode 100644 index 0000000..34f6360 --- /dev/null +++ b/cmd/sbc_harness/CMakeLists.txt @@ -0,0 +1,23 @@ +# cmd/sbc_harness/CMakeLists.txt - Build script for main sbc_harness.uf2 firmware file +# +# Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> +# SPDX-Licence-Identifier: AGPL-3.0-or-later + +add_executable(sbc_harness + main.c + usb_keyboard.c +) +target_include_directories(sbc_harness PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/config) +target_link_libraries(sbc_harness + pico_stdlib + pico_unique_id + tinyusb_device + tinyusb_board + libusb +) +pico_enable_stdio_usb(sbc_harness 0) +pico_enable_stdio_uart(sbc_harness 1) +pico_enable_stdio_semihosting(sbc_harness 0) +pico_enable_stdio_rtt(sbc_harness 0) +pico_add_extra_outputs(sbc_harness) # create .map/.bin/.hex/.uf2 files in addition to .elf +pico_set_program_url(sbc_harness "https://git.lukeshu.com/sbc-harness") diff --git a/cmd/sbc_harness/config/config.h b/cmd/sbc_harness/config/config.h new file mode 100644 index 0000000..0f0385b --- /dev/null +++ b/cmd/sbc_harness/config/config.h @@ -0,0 +1,54 @@ +/* config.h - Compile-time configuration for sbc_harness + * + * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-Licence-Identifier: AGPL-3.0-or-later + */ +#ifndef _CONFIG_H_ +#define _CONFIG_H_ + +/*#include <assert.h>*/ + +#if defined(USE_CONFIG_NETIO_POSIX) || defined(USE_CONFIG_COROUTINE) +# define CONFIG_NETIO_NUM_CONNS 8 +#endif +#ifdef USE_CONFIG_NETIO_POSIX +# define CONFIG_NETIO_ISLINUX 1 /* can we use Linux-kernel-specific fcntls? */ +# define CONFIG_NETIO_NUM_PORTS 1 +#endif + +#ifdef USE_CONFIG_COROUTINE +# define CONFIG_COROUTINE_DEFAULT_STACK_SIZE (8*1024) +# define CONFIG_COROUTINE_MEASURE_STACK 1 /* bool */ +# define CONFIG_COROUTINE_PROTECT_STACK 1 /* bool */ +# define CONFIG_COROUTINE_DEBUG 0 /* bool */ +# define CONFIG_COROUTINE_NUM (1 /* usb_common */ +\ + 1 /* usb_keyboard */ +\ + CONFIG_NETIO_NUM_CONNS /* accept+read */ +\ + (2*CONFIG_NETIO_NUM_CONNS) /* work+write */ ) + /*static_assert((CONFIG_COROUTINE_NUM * CONFIG_COROUTINE_DEFAULT_STACK_SIZE) < (264 * 1024)); */ +#endif + +#ifdef USE_CONFIG_9P +# define CONFIG_9P_PORT 564 + /** + * This max-msg-size is sized so that a Twrite message can return + * 8KiB of data. + * + * This is the same as the default in Plan 9 4e's lib9p; it has the + * comment that "24" is "ample room for Twrite/Rread header + * (iounit)". In fact, the Twrite header is only 23 bytes + * ("size[4] Twrite[1] tag[2] fid[4] offset[8] count[4]") and the + * Rread header is even shorter at 11 bytes ("size[4] Rread[1] + * tag[2] count[4]"), so "24" appears to be the size of the Twrite + * header rounded up to a nice round number. + * + * In older versions of 9P ("9P1"), the max message size was + * defined as part of the protocol specification rather than + * negotiated. In Plan 9 1e it was (8*1024)+128, and was bumped to + * (8*1024)+160 in 2e and 3e. + */ +# define CONFIG_9P_MAX_MSG_SIZE ((8*1024)+24) +# define CONFIG_9P_MAX_ERR_SIZE 128 /* 128 is what Plan 9 4e uses */ +#endif + +#endif /* _CONFIG_H */ diff --git a/cmd/sbc_harness/config/tusb_config.h b/cmd/sbc_harness/config/tusb_config.h new file mode 100644 index 0000000..cb1ca3b --- /dev/null +++ b/cmd/sbc_harness/config/tusb_config.h @@ -0,0 +1,107 @@ +/* tusb_config.h - Compile-time configuration for the TinyUSB library + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Ha Thach (tinyusb.org) + * Copyright (c) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * + */ + +#ifndef _TUSB_CONFIG_H_ +#define _TUSB_CONFIG_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +//-------------------------------------------------------------------- +// TinyUSB Device (TUD) initialization for rp2040-based boards +//-------------------------------------------------------------------- + +// Which USB port to use for the RootHub. +// The rp2040 only has 1 port, so it's gotta be port #0. +#define BOARD_TUD_RHPORT 0 + +// RHPort max operational speed. +// Use OPT_MODE_DEFAULT_SPEED for max speed supported by MCU. +#define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED + +//-------------------------------------------------------------------- +// Configuration: common +//-------------------------------------------------------------------- + +// defined by compiler flags +#ifndef CFG_TUSB_MCU +#error CFG_TUSB_MCU must be defined +#endif + +// Conditional because it might be defined with `-D` on the command +// line if `cmake -DCMAKE_BUILD_TYPE=Debug`. +#ifndef CFG_TUSB_DEBUG +#define CFG_TUSB_DEBUG 0 +#endif + +// USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment. +// Tinyusb use follows macros to declare transferring memory so that they can be put +// into those specific section. +// e.g +// - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") )) +// - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4))) +#define CFG_TUSB_MEM_SECTION /* blank */ +#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) + +#define CFG_TUD_ENABLED 1 +#define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED + +//-------------------------------------------------------------------- +// Configuration: TinyUSB Device (TUD) +//-------------------------------------------------------------------- + +// control endpoint max packet size (only 8, 16, 32, or 64 are valid) +#define CFG_TUD_ENDPOINT0_SIZE 64 + +// Which of TinyUSB's built-in class drivers to enable. +// +// If a class takes an int, that's the maximum number of interfaces of +// that type that may be listed in the same configuration descriptor. +#define CFG_TUD_CDC 0 // int : Communications Device Class (e.g. ttyUSB) https://www.usb.org/sites/default/files/CDC1.2_WMC1.1_012011.zip +#define CFG_TUD_MSC 0 // bool: Mass Storage Class https://www.usb.org/sites/default/files/Mass_Storage_Specification_Overview_v1.4_2-19-2010.pdf +#define CFG_TUD_HID 1 // int : Human Interface Device https://www.usb.org/sites/default/files/hid1_11.pdf +#define CFG_TUD_AUDIO 0 // int : Audio https://www.usb.org/sites/default/files/audio10.pdf +#define CFG_TUD_VIDEO 0 // int : Video https://www.usb.org/sites/default/files/USB_Video_Class_1_5.zip +#define CFG_TUD_MIDI 0 // int : Musical Instrument Digital Interface https://www.usb.org/sites/default/files/USB%20MIDI%20v2_0.pdf +#define CFG_TUD_VENDOR 0 // int : ??? +#define CFG_TUD_USBTMC 0 // bool: Test & Measurement Class https://www.usb.org/sites/default/files/USBTMC_1_006a.zip +#define CFG_TUD_DFU_RUNTIME 0 // bool: Device Firmware Upgrade https://www.usb.org/sites/default/files/DFU_1.1.pdf +#define CFG_TUD_DFU 0 // bool: Device Firmware Upgrade https://www.usb.org/sites/default/files/DFU_1.1.pdf +#define CFG_TUD_ECM_RNDIS 0 // bool: net +#define CFG_TUD_NCM 0 // bool: net +#define CFG_TUD_BTH 0 // bool: Bluetooth + +// HID buffer size Should be sufficient to hold ID (if any) + Data +#define CFG_TUD_HID_EP_BUFSIZE 8 + +//-------------------------------------------------------------------- +#ifdef __cplusplus +} +#endif + +#endif /* _TUSB_CONFIG_H_ */ diff --git a/cmd/sbc_harness/main.c b/cmd/sbc_harness/main.c new file mode 100644 index 0000000..1fd9f8c --- /dev/null +++ b/cmd/sbc_harness/main.c @@ -0,0 +1,48 @@ +/* main.c - Main entry point and event loop for sbc-harness + * + * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-Licence-Identifier: AGPL-3.0-or-later + */ + +/* newlib */ +#include <string.h> /* for strlen() */ + +/* pico-sdk */ +#include "pico/stdlib.h" + +/* local */ +#include "coroutine.h" +#include "usb_common.h" +#include "usb_keyboard.h" + +COROUTINE hello_world_cr(void *_chan) { + const char *msg = "Hello world!\n"; + usb_keyboard_rpc_t *chan = _chan; + cr_begin(); + + for (size_t i = 0;; i = (i+1) % strlen(msg)) { + cr_rpc_req(chan, NULL, msg[i]); + } + + cr_end(); +} + +int main() { + /* initialization */ + stdio_uart_init(); + //gpio_init(PICO_DEFAULT_LED_PIN); + //gpio_set_dir(PICO_DEFAULT_LED_PIN, GPIO_OUT); + + usb_common_earlyinit(); + usb_keyboard_init(); + usb_common_lateinit(); + + /* set up coroutines */ + coroutine_add(usb_common_cr, NULL); + usb_keyboard_rpc_t keyboard_chan = {0}; + coroutine_add(usb_keyboard_cr, &keyboard_chan); + coroutine_add(hello_world_cr, &keyboard_chan); + + /* Event loop. */ + coroutine_main(); +} diff --git a/cmd/sbc_harness/usb_keyboard.c b/cmd/sbc_harness/usb_keyboard.c new file mode 100644 index 0000000..1989b35 --- /dev/null +++ b/cmd/sbc_harness/usb_keyboard.c @@ -0,0 +1,113 @@ +/* usb_keyboard.c - Implementation of a USB keyboard device + * + * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-Licence-Identifier: AGPL-3.0-or-later + */ + +#include "tusb.h" + +#include "tusb_helpers.h" /* for TUD_ENDPOINT_IN */ +#include "usb_common.h" +#include "usb_keyboard.h" + +/** + * A USB-HID "Report Descriptor" (see USB-HID 1.11 §6.2.2 "Report + * Descriptor") describing a keyboard. + */ +static uint8_t const hid_report_descriptor_keyboard[] = { TUD_HID_REPORT_DESC_KEYBOARD() }; + +static uint8_t kbd_ifc = 0; + +void usb_keyboard_init() { + if (kbd_ifc) + return; + usb_common_earlyinit(); + + kbd_ifc = usb_add_interface(cfgnum_std, TUD_HID_DESC_LEN, (uint8_t[]){ + /* USB-HID input-only descriptor for inclusion in the config descriptor; consisting of 3 parts: + * 1. an interface descriptor (USB 2.0 §9.6.5 "Interface"), + * 2. a class-specific (class=HID) descriptor of type HID (USB-HID 1.11 §6.2.1 "HID Descriptor"), + * 3. an endpoint descriptor for inputs (USB 2.0 §9.6.6 "Endpoint"). + * The TUD_HID_DESCRIPTOR() macro takes care of this for us. */ + TUD_HID_DESCRIPTOR( + 0, /* interface : bInterfaceNumber ; Number of this interface (0-indexed) */ + STRID_KBD_IFC, /* interface : iInterface ; Index of string descriptor describing this interface */ + HID_ITF_PROTOCOL_KEYBOARD, /* interface : bInterfaceProtocol ; see USB-HID 1.11 §4.3 "Protocols" */ + sizeof(hid_report_descriptor_keyboard), /* hid : wDescriptorLength ; Total size of report descriptor */ + TUD_ENDPOINT_IN | 1, /* endpoint : bEndpointAddress ; Direction | endpoint number (arbitrary?) */ + CFG_TUD_HID_EP_BUFSIZE, /* endpoint : wMaxPacketSize ; Maximum packet size this endpoint is capable of sending or receiving */ + 10), /* endpoint : bInterval ; poll interval (in milliseconds?) */ + }); +} + +static uint8_t ascii2keycode[128][2] = { HID_ASCII_TO_KEYCODE }; + +COROUTINE usb_keyboard_cr(void *_chan) { + usb_keyboard_rpc_t *chan = _chan; + cr_begin(); + + uint8_t report_id = 0; + uint8_t modifier = 0; + uint8_t keycodes[6] = {0}; + for (;;) { + while (!tud_hid_n_ready(kbd_ifc)) + cr_yield(); + + if (cr_rpc_have_req(chan)) { + uint32_t rune; + cr_rpc_recv_req(chan, &rune); + + modifier = ascii2keycode[rune][0] ? KEYBOARD_MODIFIER_LEFTSHIFT : 0; + keycodes[0] = ascii2keycode[rune][1]; + tud_hid_n_keyboard_report(kbd_ifc, report_id, modifier, keycodes); + + while (!tud_hid_n_ready(kbd_ifc)) + cr_yield(); + + modifier = 0; + keycodes[0] = 0; + tud_hid_n_keyboard_report(kbd_ifc, report_id, modifier, keycodes); + + cr_rpc_send_resp(chan, 1); + } else { + modifier = 0; + keycodes[0] = 0; + tud_hid_n_keyboard_report(kbd_ifc, report_id, modifier, keycodes); + } + } + + cr_end(); +} + +/** + * Return a pointer to the HID "Report Descriptor" (see USB-HID 1.11 + * §6.2.2 "Report Descriptor") for the given index. + */ +uint8_t const *tud_hid_descriptor_report_cb(uint8_t index) { + static uint8_t const *reports[] = { + hid_report_descriptor_keyboard, + }; + if (index >= TU_ARRAY_SIZE(reports)) + return NULL; + return reports[index]; +} + +uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) +{ + // TODO not Implemented + (void) instance; + (void) report_id; + (void) report_type; + (void) buffer; + (void) reqlen; + + return 0; +} + +// Invoked when received SET_REPORT control request or +// received data on OUT endpoint ( Report ID = 0, Type = 0 ) +void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) +{ + // TODO not implemented + (void) report_id; +} diff --git a/cmd/sbc_harness/usb_keyboard.h b/cmd/sbc_harness/usb_keyboard.h new file mode 100644 index 0000000..6b65360 --- /dev/null +++ b/cmd/sbc_harness/usb_keyboard.h @@ -0,0 +1,17 @@ +/* usb_keyboard.h - Implementation of a USB keyboard device + * + * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-Licence-Identifier: AGPL-3.0-or-later + */ + +#ifndef _USB_KEYBOARD_H_ +#define _USB_KEYBOARD_H_ + +#include "coroutine_rpc.h" + +typedef cr_rpc_t(uint32_t, int) usb_keyboard_rpc_t; + +void usb_keyboard_init(void); +COROUTINE usb_keyboard_cr(void *arg); + +#endif /* _USB_KEYBOARD_H_ */ |