diff --git a/CMakeLists.txt b/CMakeLists.txt index ace19ee..cca9686 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,11 +1,14 @@ -cmake_minimum_required(VERSION 3.5) +cmake_minimum_required(VERSION 3.6) + +set(VERSION_MAJOR 00) +set(VERSION_MINOR 139) set(PICO_SDK_FETCH_FROM_GIT off) set(PICO_BOARD=pico) set(PICO_SDK_PATH ${CMAKE_CURRENT_LIST_DIR}/pico-sdk) include(pico_sdk_import.cmake) -set(CMAKE_C_FLAGS "-Ofast -Wall -mcpu=cortex-m0plus -mtune=cortex-m0plus -funroll-loops") +set(CMAKE_C_FLAGS "-Ofast -Wall -mcpu=cortex-m0plus -mtune=cortex-m0plus -fstack-usage") set(PICO_COPY_TO_RAM 1) @@ -36,22 +39,27 @@ target_include_directories(Pico-PIO-USB PRIVATE ${PICO_PIO_USB_DIR}) set(COMMON_SOURCES ${CMAKE_CURRENT_LIST_DIR}/src/usb_descriptors.c ${CMAKE_CURRENT_LIST_DIR}/src/defaults.c + ${CMAKE_CURRENT_LIST_DIR}/src/constants.c + ${CMAKE_CURRENT_LIST_DIR}/src/protocol.c ${CMAKE_CURRENT_LIST_DIR}/src/hid_parser.c + ${CMAKE_CURRENT_LIST_DIR}/src/hid_report.c ${CMAKE_CURRENT_LIST_DIR}/src/utils.c ${CMAKE_CURRENT_LIST_DIR}/src/handlers.c ${CMAKE_CURRENT_LIST_DIR}/src/setup.c ${CMAKE_CURRENT_LIST_DIR}/src/keyboard.c ${CMAKE_CURRENT_LIST_DIR}/src/mouse.c + ${CMAKE_CURRENT_LIST_DIR}/src/tasks.c ${CMAKE_CURRENT_LIST_DIR}/src/led.c ${CMAKE_CURRENT_LIST_DIR}/src/uart.c ${CMAKE_CURRENT_LIST_DIR}/src/usb.c ${CMAKE_CURRENT_LIST_DIR}/src/main.c + ${CMAKE_CURRENT_LIST_DIR}/src/ramdisk.c ${PICO_TINYUSB_PATH}/src/portable/raspberrypi/pio_usb/dcd_pio_usb.c ${PICO_TINYUSB_PATH}/src/portable/raspberrypi/pio_usb/hcd_pio_usb.c ) set(COMMON_INCLUDES - ${CMAKE_CURRENT_LIST_DIR}/src + ${CMAKE_CURRENT_LIST_DIR}/src/include ${PICO_PIO_USB_DIR}/src ) @@ -61,6 +69,7 @@ set(COMMON_LINK_LIBRARIES hardware_uart hardware_gpio hardware_pio + hardware_dma tinyusb_device tinyusb_host @@ -68,30 +77,45 @@ set(COMMON_LINK_LIBRARIES Pico-PIO-USB ) -# Pico A - Keyboard (board_role = 0) -# B - Mouse (board_role = 1) +set(binary deskhop) -set(binaries board_A board_B) +set(DISK_ASM "${CMAKE_CURRENT_LIST_DIR}/disk/disk.S") +set(DISK_BIN "${CMAKE_CURRENT_LIST_DIR}/disk/disk.img") -foreach(board_role RANGE 0 1) - list (GET binaries ${board_role} binary) +set_property(SOURCE ${DISK_ASM} APPEND PROPERTY COMPILE_OPTIONS "-x" "assembler-with-cpp") - add_executable(${binary}) +add_executable(${binary} ${DISK_ASM}) - target_sources(${binary} PUBLIC ${COMMON_SOURCES}) - target_compile_definitions(${binary} PRIVATE BOARD_ROLE=${board_role} PIO_USB_USE_TINYUSB=1 PIO_USB_DP_PIN_DEFAULT=14) +target_sources(${binary} PUBLIC ${COMMON_SOURCES}) +target_compile_definitions(${binary} + PRIVATE + PIO_USB_USE_TINYUSB=1 + PIO_USB_DP_PIN_DEFAULT=14 + # Uncomment to enable debug uart: + # DH_DEBUG=1 + __disk_file_path__="${DISK_BIN}" +) + +target_include_directories(${binary} PUBLIC ${COMMON_INCLUDES}) +target_link_libraries(${binary} PUBLIC ${COMMON_LINK_LIBRARIES}) - target_include_directories(${binary} PUBLIC ${COMMON_INCLUDES}) - target_link_libraries(${binary} PUBLIC ${COMMON_LINK_LIBRARIES}) +pico_enable_stdio_usb(${binary} 0) +pico_enable_stdio_uart(${binary} 0) - pico_enable_stdio_usb(${binary} 0) - pico_add_extra_outputs(${binary}) +pico_add_extra_outputs(${binary}) - target_link_options(${binary} PRIVATE - -Xlinker - --print-memory-usage - ) +add_custom_command( + TARGET ${binary} POST_BUILD + COMMAND python3 ${CMAKE_SOURCE_DIR}/tools/crc32.py ${binary}.bin ${binary}.crc ${VERSION_MAJOR}${VERSION_MINOR} + COMMAND ${CMAKE_OBJCOPY} --update-section .section_metadata=${binary}.crc ${binary}.elf + COMMAND ${CMAKE_OBJCOPY} -O binary ${binary}.elf ${binary}.bin + COMMAND ${CMAKE_BINARY_DIR}/elf2uf2/elf2uf2 ${binary}.elf ${binary}.uf2 + COMMENT "Update CRC32 section to match the actual binary" +) - pico_set_linker_script(${binary} ${CMAKE_SOURCE_DIR}/memory_map.ld) +target_link_options(${binary} PRIVATE + -Xlinker + --print-memory-usage +) -endforeach() +pico_set_linker_script(${binary} ${CMAKE_SOURCE_DIR}/memory_map.ld) diff --git a/Pico-PIO-USB/src/pio_usb.h b/Pico-PIO-USB/src/pio_usb.h index 8a970e7..5de4598 100644 --- a/Pico-PIO-USB/src/pio_usb.h +++ b/Pico-PIO-USB/src/pio_usb.h @@ -32,6 +32,8 @@ int pio_usb_set_out_data(endpoint_t *ep, const uint8_t *buffer, uint8_t len); // Misc functions int pio_usb_kbd_set_leds(usb_device_t *device, uint8_t port, uint8_t value); +extern int dh_debug_printf(const char *format, ...); + #ifdef __cplusplus } #endif diff --git a/Pico-PIO-USB/src/pio_usb_host.c b/Pico-PIO-USB/src/pio_usb_host.c index 157844a..0e96a49 100644 --- a/Pico-PIO-USB/src/pio_usb_host.c +++ b/Pico-PIO-USB/src/pio_usb_host.c @@ -414,7 +414,7 @@ bool pio_usb_host_send_setup(uint8_t root_idx, uint8_t device_address, uint8_t const setup_packet[8]) { endpoint_t *ep = _find_ep(root_idx, device_address, 0); if (!ep) { - printf("cannot find ep 0x00\r\n"); + dh_debug_printf("cannot find ep 0x00\r\n"); return false; } @@ -430,7 +430,7 @@ bool pio_usb_host_endpoint_transfer(uint8_t root_idx, uint8_t device_address, uint16_t buflen) { endpoint_t *ep = _find_ep(root_idx, device_address, ep_address); if (!ep) { - printf("no endpoint 0x%02X\r\n", ep_address); + dh_debug_printf("no endpoint 0x%02X\r\n", ep_address); return false; } @@ -449,7 +449,7 @@ bool pio_usb_host_endpoint_abort_transfer(uint8_t root_idx, uint8_t device_addre uint8_t ep_address) { endpoint_t *ep = _find_ep(root_idx, device_address, ep_address); if (!ep) { - printf("no endpoint 0x%02X\r\n", ep_address); + dh_debug_printf("no endpoint 0x%02X\r\n", ep_address); return false; } @@ -679,13 +679,13 @@ static int __no_inline_not_in_flash_func(control_out_protocol)( } if (time_us_64() - start_time >= timeout) { - printf("control out[timeout]\n"); + dh_debug_printf("control out[timeout]\n"); res = -2; } else if (pipe->operation == CONTROL_ERROR) { - printf("control out[error]\n"); + dh_debug_printf("control out[error]\n"); res = -1; } else if (pipe->operation == CONTROL_COMPLETE) { - printf("control out[complete]\n"); + dh_debug_printf("control out[complete]\n"); res = 0; } pipe->operation = CONTROL_NONE; @@ -723,13 +723,13 @@ static int __no_inline_not_in_flash_func(control_in_protocol)( } if (time_us_64() - start_time >= timeout) { - printf("control in[timeout]\n"); + dh_debug_printf("control in[timeout]\n"); res = -2; } else if (pipe->operation == CONTROL_ERROR) { - printf("control in[error]\n"); + dh_debug_printf("control in[error]\n"); res = -1; } else if (pipe->operation == CONTROL_COMPLETE) { - printf("control in[complete]\n"); + dh_debug_printf("control in[complete]\n"); res = 0; } pipe->operation = CONTROL_NONE; @@ -763,18 +763,18 @@ static int get_hub_port_status(usb_device_t *device, uint8_t port, static int initialize_hub(usb_device_t *device) { uint8_t rx_buffer[16]; int res = 0; - printf("USB Hub detected\n"); + dh_debug_printf("USB Hub detected\n"); usb_setup_packet_t get_hub_desc_request = GET_HUB_DESCRPTOR_REQUEST; control_in_protocol(device, (uint8_t *)&get_hub_desc_request, sizeof(get_hub_desc_request), rx_buffer, 8); const hub_descriptor_t *desc = (hub_descriptor_t *)rx_buffer; uint8_t port_num = desc->port_num; - printf("\tTurn on port powers\n"); + dh_debug_printf("\tTurn on port powers\n"); for (int idx = 0; idx < port_num; idx++) { res = set_hub_feature(device, idx, HUB_SET_PORT_POWER); if (res != 0) { - printf("\tFailed to turn on ports\n"); + dh_debug_printf("\tFailed to turn on ports\n"); break; } } @@ -837,7 +837,7 @@ static int enumerate_device(usb_device_t *device, uint8_t address) { uint8_t idx_product = desc->product; uint8_t idx_serial = desc->serial; - printf("Enumerating %04x:%04x, class:%d, address:%d\n", device->vid, + dh_debug_printf("Enumerating %04x:%04x, class:%d, address:%d\n", device->vid, device->pid, device->device_class, address); usb_setup_packet_t set_address_request = SET_ADDRESS_REQ_DEFAULT; @@ -862,9 +862,9 @@ static int enumerate_device(usb_device_t *device, uint8_t address) { if (idx_manufacture != 0) { res = get_string_descriptor(device, idx_manufacture, rx_buffer, str); if (res == 0) { - printf("Manufacture:%s\n", str); + dh_debug_printf("Manufacture:%s\n", str); } else { - printf("Failed to get string descriptor (Manufacture)\n"); + dh_debug_printf("Failed to get string descriptor (Manufacture)\n"); } stdio_flush(); } @@ -872,9 +872,9 @@ static int enumerate_device(usb_device_t *device, uint8_t address) { if (idx_product != 0) { res = get_string_descriptor(device, idx_product, rx_buffer, str); if (res == 0) { - printf("Product:%s\n", str); + dh_debug_printf("Product:%s\n", str); } else { - printf("Failed to get string descriptor (Product)\n"); + dh_debug_printf("Failed to get string descriptor (Product)\n"); } stdio_flush(); } @@ -882,9 +882,9 @@ static int enumerate_device(usb_device_t *device, uint8_t address) { if (idx_serial != 0) { res = get_string_descriptor(device, idx_serial, rx_buffer, str); if (res == 0) { - printf("Serial:%s\n", str); + dh_debug_printf("Serial:%s\n", str); } else { - printf("Failed to get string descriptor (Serial)\n"); + dh_debug_printf("Failed to get string descriptor (Serial)\n"); } stdio_flush(); } @@ -943,7 +943,7 @@ static int enumerate_device(usb_device_t *device, uint8_t address) { case DESC_TYPE_INTERFACE: { const interface_descriptor_t *d = (const interface_descriptor_t *)descriptor; - printf( + dh_debug_printf( "inum:%d, altsetting:%d, numep:%d, iclass:%d, isubclass:%d, " "iprotcol:%d, iface:%d\n", d->inum, d->altsetting, d->numep, d->iclass, d->isubclass, @@ -954,7 +954,7 @@ static int enumerate_device(usb_device_t *device, uint8_t address) { case DESC_TYPE_ENDPOINT: { const endpoint_descriptor_t *d = (const endpoint_descriptor_t *)descriptor; - printf("\t\t\tepaddr:0x%02x, attr:%d, size:%d, interval:%d\n", + dh_debug_printf("\t\t\tepaddr:0x%02x, attr:%d, size:%d, interval:%d\n", d->epaddr, d->attr, d->max_size[0] | (d->max_size[1] << 8), d->interval); @@ -983,13 +983,13 @@ static int enumerate_device(usb_device_t *device, uint8_t address) { ep->need_pre = !device->is_root && !device->is_fullspeed; ep->is_tx = (d->epaddr & 0x80) ? false : true; } else { - printf("No empty EP\n"); + dh_debug_printf("No empty EP\n"); } } } break; case DESC_TYPE_HID: { const hid_descriptor_t *d = (const hid_descriptor_t *)descriptor; - printf( + dh_debug_printf( "\tbcdHID:%x.%x, country:%d, desc num:%d, desc_type:%d, " "desc_size:%d\n", d->bcd_hid[1], d->bcd_hid[0], d->contry_code, d->num_desc, @@ -1011,11 +1011,11 @@ static int enumerate_device(usb_device_t *device, uint8_t address) { control_in_protocol( device, (uint8_t *)&get_hid_report_descrpitor_request, sizeof(get_hid_report_descrpitor_request), rx_buffer, desc_len); - printf("\t\tReport descriptor:"); + dh_debug_printf("\t\tReport descriptor:"); for (int i = 0; i < desc_len; i++) { - printf("%02x ", device->control_pipe.rx_buffer[i]); + dh_debug_printf("%02x ", device->control_pipe.rx_buffer[i]); } - printf("\n"); + dh_debug_printf("\n"); stdio_flush(); } break; @@ -1044,7 +1044,7 @@ static int enumerate_device(usb_device_t *device, uint8_t address) { } static void device_disconnect(usb_device_t *device) { - printf("Disconnect device %d\n", device->address); + dh_debug_printf("Disconnect device %d\n", device->address); for (int port = 0; port < PIO_USB_HUB_PORT_CNT; port++) { if (device->child_devices[port] != 0) { device_disconnect(&pio_usb_device[device->child_devices[port]]); @@ -1086,7 +1086,7 @@ static int assign_new_device_to_port(usb_device_t *hub_device, uint8_t port, boo pio_usb_device[idx].connected = true; pio_usb_device[idx].is_fullspeed = !is_ls; pio_usb_device[idx].event = EVENT_CONNECT; - printf("Assign device %d to %d-%d\n", idx, hub_device->address, port); + dh_debug_printf("Assign device %d to %d-%d\n", idx, hub_device->address, port); endpoint_descriptor_t ep0_desc = { sizeof(endpoint_descriptor_t), DESC_TYPE_ENDPOINT, 0x00, 0x00, { 0x08, 0x00 }, 0x00 @@ -1097,7 +1097,7 @@ static int assign_new_device_to_port(usb_device_t *hub_device, uint8_t port, boo return 0; } - printf("Failed to assign device\n"); + dh_debug_printf("Failed to assign device\n"); return -1; } @@ -1114,22 +1114,22 @@ static void __no_inline_not_in_flash_func(process_hub_event)( hub_port_status_t status; int res = get_hub_port_status(device, port, &status); if (res != 0) { - printf("Failed to get port%d-%d status\n", device->address, port); + dh_debug_printf("Failed to get port%d-%d status\n", device->address, port); continue; } - printf("port%d-%d status:%d %d\n", device->address, port, + dh_debug_printf("port%d-%d status:%d %d\n", device->address, port, status.port_change, status.port_status); if (status.port_change & HUB_CHANGE_PORT_CONNECTION) { if (status.port_status & HUB_STAT_PORT_CONNECTION) { - printf("new device on port %d, reset port\n", port); + dh_debug_printf("new device on port %d, reset port\n", port); if (device->child_devices[port] != 0) { - printf("device is already assigned. disconnect previous\n"); + dh_debug_printf("device is already assigned. disconnect previous\n"); device_disconnect(&pio_usb_device[device->child_devices[port]]); } if (device->root->addr0_exists) { - printf("Address 0 already exists\n"); + dh_debug_printf("Address 0 already exists\n"); continue; } @@ -1137,17 +1137,17 @@ static void __no_inline_not_in_flash_func(process_hub_event)( set_hub_feature(device, port, HUB_SET_PORT_RESET); device->root->addr0_exists = true; } else { - printf("No vacant in device pool\n"); + dh_debug_printf("No vacant in device pool\n"); } } else { - printf("device removed from port %d\n", port); + dh_debug_printf("device removed from port %d\n", port); if (device->child_devices[port] != 0) { device_disconnect(&pio_usb_device[device->child_devices[port]]); } } clear_hub_feature(device, port, HUB_CLR_PORT_CONNECTION); } else if (status.port_change & HUB_CHANGE_PORT_RESET) { - printf("reset port %d complete\n", port); + dh_debug_printf("reset port %d complete\n", port); res = clear_hub_feature(device, port, HUB_CLR_PORT_RESET); if (res == 0) { assign_new_device_to_port(device, port, @@ -1164,7 +1164,7 @@ static void __no_inline_not_in_flash_func(process_hub_event)( void __no_inline_not_in_flash_func(pio_usb_host_task)(void) { for (int root_idx = 0; root_idx < PIO_USB_ROOT_PORT_CNT; root_idx++) { if (pio_usb_root_port[root_idx].event == EVENT_CONNECT) { - printf("Root %d connected\n", root_idx); + dh_debug_printf("Root %d connected\n", root_idx); int dev_idx = device_pool_vacant(); if (dev_idx >= 0) { on_device_connect(&pio_port[0], &pio_usb_root_port[root_idx], dev_idx); @@ -1172,7 +1172,7 @@ void __no_inline_not_in_flash_func(pio_usb_host_task)(void) { } pio_usb_root_port[root_idx].event = EVENT_NONE; } else if (pio_usb_root_port[root_idx].event == EVENT_DISCONNECT) { - printf("Root %d disconnected\n", root_idx); + dh_debug_printf("Root %d disconnected\n", root_idx); pio_usb_host_close_device( root_idx, pio_usb_root_port[root_idx].root_device->address); pio_usb_root_port[root_idx].root_device->connected = false; @@ -1187,7 +1187,7 @@ void __no_inline_not_in_flash_func(pio_usb_host_task)(void) { if (device->event == EVENT_CONNECT) { device->event = EVENT_NONE; - printf("Device %d Connected\n", idx); + dh_debug_printf("Device %d Connected\n", idx); int res = enumerate_device(device, idx + 1); if (res == 0) { device->enumerated = true; @@ -1198,7 +1198,7 @@ void __no_inline_not_in_flash_func(pio_usb_host_task)(void) { } if (res != 0) { - printf("Enumeration failed(%d)\n", res); + dh_debug_printf("Enumeration failed(%d)\n", res); // retry if (device->is_root) { device->root->event = EVENT_DISCONNECT; @@ -1210,7 +1210,7 @@ void __no_inline_not_in_flash_func(pio_usb_host_task)(void) { } } else if (device->event == EVENT_DISCONNECT) { device->event = EVENT_NONE; - printf("Disconnect\n"); + dh_debug_printf("Disconnect\n"); device_disconnect(device); } else if (device->event == EVENT_HUB_PORT_CHANGE) { process_hub_event(device); diff --git a/README.md b/README.md index 969fca2..475c655 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,13 @@ It also remembers the LED state for each computer, so you can pick up exactly ho ## How to build -To avoid version mismatch and reported path issues when building, the project now bundles minimal pico sdk and tinyusb. +To avoid version mismatch and reported path issues when building, as well as to save you from having to download a large SDK, the project now bundles minimal pico sdk and tinyusb. + +On a Debian/Ubuntu systems, make sure to install these: + +``` +sudo apt install cmake gcc-arm-none-eabi libnewlib-arm-none-eabi build-essential +``` You should be able to build by running: @@ -61,19 +67,21 @@ cmake -S . -B build cmake --build build ``` -## Using pre-built images +additionally, to rebuild web UI check webconfig/ and execute ```./render.py```, you'll need jinja2 installed. -Alternatively, you can use the [pre-built images](binaries/). Take the Pico board that goes to slot A on the PCB and hold the on-board button while connecting the cable. +To rebuild the disk, check disk/ folder and run ```./create.sh```, tweak to your system if needed. You'll need **dosfstools** (to provide mkdosfs), -It should appear as a USB drive on your system. Copy the corresponding board_A.uf2 file there and repeat the same with B. +## Using a pre-built image + +Alternatively, you can use the [pre-built images](https://github.com/hrvach/deskhop/releases). Since version 0.6 there is only a single universal image. You need the .uf2 file which you simply copy to the device in one of the following ways: ## Upgrading firmware -Option 1 - Open the case, hold the button while connecting each Pico and copy the right uf2 to it. +**Option 1** - (firmware 0.6 and later) Put the device in "config mode" by simultaneously pressing **left shift + right shift + c + o**. Device your keyboard is plugged into will reboot and turn into a USB drive called "DESKHOP". All you need to do is copy the .uf2 file to it. Once image is verified, device will flash and reboot, then proceed to upgrade the other board. During this operation the led will blink. Once it's done, it will write flash and reboot, completing the operation. -Option 2 - Switch a board to BOOTSEL mode by using a special key combination (listed below). +_Note_ - This is not an actual generic USB drive, you can't use it to copy files to it. -This will make the corresponding Pico board enter the bootloader upgrade mode and act as USB flash drive. Now you can drag-and-drop the .uf2 file to it (you might need to plug in your mouse directly). +**Option 2** - Using the ROM bootloader - hold the on-board button while connecting each Pico and copy the uf2 to the flash drive that appears. Images later than 0.6 support holding the button without having to fiddle around the power supply, but the "hold button while plugging" should always work, regardless of device state. ## Misc features @@ -94,19 +102,17 @@ Supposedly built in to prevent computer from entering standby, but truth be told Potential usage example - I have a buggy USB dock that won't resume video from standby, so not allowing it to sleep can be a handy workaround. -![Image](img/screensaver.gif) - ## Hardware [The circuit](schematics/DeskHop_v1.1.pdf) is based on two Raspberry Pi Pico boards, chosen because they are cheap (4.10 € / pc), can be hand soldered and most suppliers have them in stock. -The Picos are connected using UART and separated by an Analog Devices ADuM1201 dual-channel digital isolator (~3€) or a much cheaper, faster and pin-compatible TI ISO7721DR (~1.5€). +The Picos are connected using UART and separated by an Analog Devices ADuM1201 dual-channel digital isolator (~3€) or a much cheaper, faster and pin-compatible TI ISO7721DR (~1.5€) which is the preferred choice. While they normally don't have support for dual USB, thanks to an [amazing project](https://github.com/sekigon-gonnoc/Pico-PIO-USB) where USB is implemented using the programmable IO wizardry found in RP2040, there is support for it to act both as an USB host and device. ## PCB [updated] -To keep things as simple as possible for DIY builds, the traces were kept on one side and the number of parts kept to a theoretical minimum. +To keep things as simple as possible for DIY builds, the traces were kept on one side and the number of parts kept to a minimum. ![Image](img/plocica2.png) @@ -193,11 +199,13 @@ The standard process to do that is using isopropyl alcohol and an old toothbrush ## Usage guide -### Keyboard shortcuts +### Keyboard shortcuts - (fw versions 0.6+) -_Firmware upgrade_ -- ```Right Shift + F12 + Left Shift + A``` - put board A in FW upgrade mode -- ```Right Shift + F12 + Left Shift + B``` - put board B in FW upgrade mode +_Config_ +- ```Left Shift + Right Shift + C + O``` - enter config mode +- ```Right Shift + F12 + D``` - remove flash config +- ```Right Shift + F12 + Y``` - save screen switch offset +- ```Right Shift + F12 + S``` - turn on/off screensaver option _Usage_ - ```Right CTRL + Right ALT``` - Toggle slower mouse mode @@ -205,20 +213,6 @@ _Usage_ - ```Right ALT + Right Shift + L``` - Lock both outputs at once (set output OS before, see shortcuts below) - ```Caps Lock``` - Switch between outputs -_Config_ -- ```Right Shift + F12 + D``` - remove flash config -- ```Right Shift + F12 + Y``` - save screen switch offset -- ```Right Shift + F12 + S``` - turn on/off screensaver option - -_Number of outputs_ -- ```Right Shift + Backspace + 1``` - set number of screens to 1 on current active output -- ```Right Shift + Backspace + 2``` - set number of screens to 2 on current active output - -_Set operating systems_ -- ```Right Shift + Backspace + 7``` - set os to Linux on current active output -- ```Right Shift + Backspace + 8``` - set os to Windows on current active output -- ```Right Shift + Backspace + 9``` - set os to MacOS on current active output - ### Switch cursor height calibration This step is not required, but it can be handy if your screens are not perfectly aligned or differ in size. The objective is to have the mouse pointer come out at exactly the same height. @@ -229,16 +223,46 @@ Just park your mouse on the LARGER screen at the height of the smaller/lower scr Repeat for the bottom border (if it's above the larger screen's border). This will get saved to flash and it should keep this calibration value from now on. - ### Multiple screens per output -Windows and Mac have issues with multiple screens and absolute positioning, so workarounds are needed (still experimental). Your main screens need to be in the middle, and secondary screen(s) on the edges. Move the mouse to the screen with multiple desktops and press *right shift + backspace + 2* if you have 2 desktops and *right shift + backspace + 7, 8 or 9* depending on your OS (Linux, Windows, Mac). +Windows and Mac have issues with multiple screens and absolute positioning, so workarounds are needed (still experimental). There is a better workaround under construction, but for now you have to set the operating system for each output and number of screens. + +Your main screens need to be in the middle, and secondary screen(s) on the edges. To configure the actual options, open the web configuration page for your device. ![Image](img/deskhop-scr.png) -### Other configuration +### Web configuration mode -Mouse speed can now be configured per output screen and per axis. If you have multiple displays under Linux, your X speed might be too fast, so you need to configure it in user_config.h and rebuild. In the future, this will be configurable without having to do that. +Starting with fw 0.6, an improved configuration mode is introduced. To configure your device, follow these instructions: + +1. Press Left Shift + Right Shift + C + O - your device will reboot and enter configuration mode (on the side your keyboard is plugged into). LED will keep blinking during the configuration session. +2. A new USB drive will appear named "DESKHOP" with a single file, config.htm +3. Open that file with Chromium / Chrome. Unfortunately FF is not supported right now, since they avoid implementing WebHID. +4. Click connect and allow deskhop device to pair. + +![Image](img/connect-dialog.png) + +5. Configure the options as you wish and click save to write to device. +6. Click "exit" in the menu to leave configuration mode for added safety. + +![Image](img/config-page-big.png) + +Q: Why not simply create a nice online web page like Via instead of dealing with this weird USB drive thing? +A: Loading javascript from a random online location that interacts with your input devices is a potential security risk. The configuration web page is local only and nothing is ever loaded externally. + +
+ Linux doesn't see device? Click here. + +Q: Chromium on Linux doesn't work. +A: You probably need to tweak /dev permissions or create a corresponding udev rules file and make sure your user is in the right group, like so: + +/etc/udev/rules.d/99-deskhop.rules +``` +KERNEL=="hidraw*", SUBSYSTEM=="hidraw", ATTRS{idVendor}=="1209", ATTRS{idProduct}=="c000", GROUP="plugdev", MODE="0660" +``` +

+ +Please note the config web page is not "weird" because of deliberate obfuscation - it's self-decompressing due to very limited storage. Entire source is 100% open and a part of this repo. You are encouraged to rebuild everything yourself. ### Functional verification @@ -251,14 +275,16 @@ Do this test by first plugging the keyboard on one side and then on the other. I Some features are missing on purpose, despite the fact it would make the device easier to use or simpler to configure. Here is a quick breakdown of these decisions: - There is no copy-paste or *any* information sharing between systems. This prevents information leakage. -- No webhid device management or any inbound connectivity from the output computers, with the only exception of standard keyboard LED on/off messages, hard limited to 1 byte of data. +- No webhid device management without explicit user consent. No inbound connectivity from the output computers, with the only exception of standard keyboard LED on/off messages and hard limited to 1 byte of data. - No FW upgrade triggering from the outputs. Only explicit and deliberate user action through a special keyboard shortcut may do that. - No plugged-in keyboard/mouse custom endpoints are exposed or information forwarded towards these devices. Their potential vulnerabilities are effectively firewalled from the computer. - No input history is allowed to be retained. +- No device-initiated keystrokes, for any reason. Only thing that comes out is what you type/trigger. - Outputs are physically separated and galvanically isolated with a minimal isolation voltage of 2kV. -- All packets exchanged between devices are of fixed length, no code is transferred and no raw config exchange of any kind can take place. -- There is no bluetooth or wifi, networking, Internet access, usb drives etc. +- All packets exchanged between devices are of fixed length, config options transferred are limited to a short list. Most options are read-only. Cross-device firmware upgrades can be disabled. +- There is no bluetooth or wifi, networking, Internet access, etc. - No connected computer is considered trusted under any circumstances. +- Configuration mode is automatically disabled after a period of inactivity. - Entirety of the code is open source, without any binary blobs and thoroughly commented to explain its purpose. I encourage you to never trust anyone and always make sure you know what you are running by doing a manual audit. This still doesn't guarantee anything, but I believe it makes a reasonable set of ground rules to keep you safe and protected. @@ -279,7 +305,7 @@ This still doesn't guarantee anything, but I believe it makes a reasonable set o [UPDATE] It seems you can order it in QTY of 1 (for either PCB, assembled PCB or a fully assembled device) from [Elecrow if you follow this link](https://www.elecrow.com/deskhop-fast-desktop-switching.html) -*I **don't want to take any commission** on this - the only goal is to provide an alternative for people who don't feel confident enough to assemble the boards themselves.* +[UPDATE2] - I never asked Elecrow for anything, but a couple of days ago they offered to sponsor the project with a small budget that will be used for future board prototyping. Since my goal is to create a better board with more than 2 outputs etc, I believe prototyping services might be beneficial to the project. 4. When the active screen is changed via the mouse, does the keyboard follow (and vice versa)? @@ -287,7 +313,7 @@ This still doesn't guarantee anything, but I believe it makes a reasonable set o 5. Will this work with keyboard/mouse combo dongles, like the Logitech Unifying receiver? -*Not tested yet, but the latest version might actually work (please provide feedback).* +It should work. After a recent FW update, support for combo receivers should be much better. 6. Will this work with wireless mice and keyboards that have separate wireless receivers (one for the mouse, another for the keyboard)? @@ -309,12 +335,12 @@ There are several software alternatives you can use if that works in your partic ## Shortcomings -- Windows 10 broke HID absolute coordinates behavior in KB5003637, so you can't use more than 1 screen on Windows (mouse will stay on the main screen). There is an experimental workaround. +- Windows 10 broke HID absolute coordinates behavior in KB5003637, so you can't use more than 1 screen on Windows (mouse will stay on the main screen). There is an experimental workaround with a better one on the way. - Code needs cleanup, some refactoring etc. - Not tested with a wide variety of devices, I don't know how it will work with your hardware. There is a reasonable chance things might not work out-of-the-box. - Advanced keyboards (with knobs, extra buttons or sliders) will probably face issues where this additional hardware doesn't work. - Super-modern mice with 300 buttons might see some buttons not work as expected. -- NOTE: Both computers need to be connected and provide power to the USB for this to work (as each board gets powered by the computer it plugs into). Many desktops and laptops will provide power even when shut down nowadays. If you need to run with one board fully disconnected, you should be able to use a USB hub to plug both keyboard and mouse to a single port. +- NOTE: **Both computers need to be connected and provide power to the USB for this to work** (as each board gets powered by the computer it plugs into). Many desktops and laptops will provide power even when shut down nowadays. If you need to run with one board fully disconnected, you should be able to use a USB hub to plug both keyboard and mouse to a single port. - MacOS has issues with more than one screens, latest firmware offers an experimental workaround that fixes it. ## Progress @@ -322,14 +348,10 @@ There are several software alternatives you can use if that works in your partic So, what's the deal with all the enthusiasm? I can't believe it - please allow me to thank you all! I've never expected this kind of interest in a simple personal project, so the initial features are pretty basic (just like my cooking skills) and mostly cover my own usecase. Stay tuned for firmware updates that should bring wider device compatibility, more features and less bugs. As this is a hobby project, I appreciate your understanding for being time-constrained and promise to do the best I can. Planned features: -- ~~Proper TinyUSB host integration~~ (done) -- ~~HID report protocol parsing, not just boot protocol~~ (mostly done) -- ~~Support for unified dongle receivers~~ -- ~~Support for USB hubs and single-sided operation~~ -- ~~Configurable screens (done)~~ -- ~~Permament configuration stored in flash~~ -- Better support for keyboards with knobs and mice with mickeys -- Unified firmware for both Picos +- Better workarounds for multiscreen windows and macos +- Transparent / Gaming mode +- Support for more than 2 outputs +- Improvements on the configuration UI - ... and more! Working on a *lite* version which provides basic functionality with just a single Pico W board, lowering the cost even further and enabling you to try it out even with no added hardware or PCB. @@ -341,13 +363,14 @@ Mouse polling should now work at 1000 Hz (the dips in the graph is my arm hurtin ## Sponsor / donate I'm NOT doing this for profit or any other reason except to try and help people by creating a better working environment for everyone. + I have, however, decided to accept donations for a single purpose only - to buy some keyboards/mice with media keys, buttons, nkro and other weird stuff people reported issues with in order to fix bugs, improve the state of the project and provide a better user experience overall. Having said that, if you want to support the project, you can use this link: [![](https://www.paypalobjects.com/en_US/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/donate/?hosted_button_id=4RDC7JY5FNC78) -Thank you! +Please allow me to thank everyone who helped or considering it, this already helped me expand support for a range of devices. Many thanks! ## Disclaimer diff --git a/binaries/board_A.uf2 b/binaries/board_A.uf2 deleted file mode 100644 index 234d2ca..0000000 Binary files a/binaries/board_A.uf2 and /dev/null differ diff --git a/binaries/board_B.uf2 b/binaries/board_B.uf2 deleted file mode 100644 index 3cd3995..0000000 Binary files a/binaries/board_B.uf2 and /dev/null differ diff --git a/disk/create.sh b/disk/create.sh new file mode 100755 index 0000000..25f2002 --- /dev/null +++ b/disk/create.sh @@ -0,0 +1,13 @@ + +#!/bin/bash + +dd if=/dev/zero of=fat.img bs=2M count=1 + +mkdosfs -F12 -n DESKHOP -i 0 fat.img + +sudo mount -o loop,x-mount.mkdir -t vfat fat.img /mnt/disk/ +sudo cp ../webconfig/config.htm /mnt/disk/config.htm +sudo umount /mnt/disk + +dd if=fat.img of=disk.img bs=512 count=128 +rm fat.img diff --git a/disk/disk.S b/disk/disk.S new file mode 100644 index 0000000..3016b23 --- /dev/null +++ b/disk/disk.S @@ -0,0 +1,10 @@ + .section .section_disk,"a" + .global _disk_start + +_disk_start: + .incbin __disk_file_path__ + .global _disk_end + +_disk_end: + + diff --git a/disk/disk.img b/disk/disk.img new file mode 100644 index 0000000..7fef427 Binary files /dev/null and b/disk/disk.img differ diff --git a/img/screensaver.gif b/img/screensaver.gif deleted file mode 100644 index d49cfc4..0000000 Binary files a/img/screensaver.gif and /dev/null differ diff --git a/memory_map.ld b/memory_map.ld index 51fe3bf..cceaa03 100644 --- a/memory_map.ld +++ b/memory_map.ld @@ -21,16 +21,33 @@ __stack (== StackTop) */ +/* Total image is 256 kB, consisting of: + Executable = 188 kB + FAT disk image = 64 kB + Firmware metadata = 4 kB (contains checksum) +*/ + +__FLASH_LEN = 188k; +__DISK_IMAGE_LEN = 64k; +__METADATA_LEN = 4k; +__TOTAL_IMAGE_LENGTH = 256k; + __CONFIG_STORAGE_LEN = 4k; MEMORY { - FLASH(rx) : ORIGIN = 0x10000000, LENGTH = 2048k - __CONFIG_STORAGE_LEN + FLASH(rx) : ORIGIN = 0x10000000, LENGTH = __FLASH_LEN + DISK_IMAGE(rw) : ORIGIN = 0x10000000 + __FLASH_LEN, LENGTH = __DISK_IMAGE_LEN + FW_METADATA(rw) : ORIGIN = 0x10000000 + (__TOTAL_IMAGE_LENGTH - __METADATA_LEN), LENGTH = __METADATA_LEN + FW_STAGING(rw) : ORIGIN = 0x10000000 + __TOTAL_IMAGE_LENGTH, LENGTH = __TOTAL_IMAGE_LENGTH + FLASH_CONFIG(rw) : ORIGIN = 0x10000000 + (2048k - __CONFIG_STORAGE_LEN), LENGTH = __CONFIG_STORAGE_LEN RAM(rwx) : ORIGIN = 0x20000000, LENGTH = 256k SCRATCH_X(rwx) : ORIGIN = 0x20040000, LENGTH = 4k SCRATCH_Y(rwx) : ORIGIN = 0x20041000, LENGTH = 4k } +PROVIDE(_staging_metadata = ORIGIN(FW_STAGING) + (__TOTAL_IMAGE_LENGTH - __METADATA_LEN)); +PROVIDE(_firmware_metadata = ORIGIN(FW_METADATA)); ENTRY(_entry_point) @@ -42,6 +59,7 @@ SECTIONS */ .flash_begin : { + ADDR_FW_RUNNING = .; __flash_binary_start = .; } > FLASH @@ -180,6 +198,7 @@ SECTIONS .uninitialized_data (NOLOAD): { . = ALIGN(4); + __udata_end__ = .; *(.uninitialized_data*) } > RAM @@ -217,10 +236,35 @@ SECTIONS __HeapLimit = .; } > RAM - /* Configuration flash section (4k in size, end of flash) */ - - .section_config : { - "ADDR_CONFIG" = .; + /* Store web configuration utility HTML */ + .section_disk : { + ADDR_DISK_IMAGE = .; + KEEP(*(.section_disk)) + } > DISK_IMAGE + + /* Firmware metadata section (4k in size, contains version, checksum etc.) */ + .section_metadata : { + ADDR_FW_METADATA = .; + KEEP(*(.section_metadata)); + } > FW_METADATA + + /* Firmware staging section (256k in size, near the end of flash) */ + .section_staging : { + ADDR_FW_STAGING = .; + KEEP(*(.section_staging)) + } > FW_STAGING + + /* Just padding so we can have a nice, consistently-sized .bin file */ + .fill : { + FILL(0x00); + . = ORIGIN(FW_METADATA) + LENGTH(FW_METADATA) - 1; + BYTE(0x00) + ___ROM_AT = .; + } > FW_METADATA + + /* Configuration flash section (4k in size, end of flash) */ + .section_config (NOLOAD) : { + ADDR_CONFIG = .; } > FLASH_CONFIG /* .stack*_dummy section doesn't contains any symbols. It is only @@ -245,6 +289,14 @@ SECTIONS __flash_binary_end = .; } > FLASH + .pad : { + /* This section will be filled with zeroes */ + FILL(0x00) + . = ADDR_DISK_IMAGE - __flash_binary_end - 1; + BYTE(0x00) + KEEP(*(.pad)) + } > FLASH + /* stack limit is poorly named, but historically is maximum heap ptr */ __StackLimit = ORIGIN(RAM) + LENGTH(RAM); __StackOneTop = ORIGIN(SCRATCH_X) + LENGTH(SCRATCH_X); diff --git a/pico-sdk/lib/tinyusb/hw/bsp/board.c b/pico-sdk/lib/tinyusb/hw/bsp/board.c index 23b4b66..bb339f6 100644 --- a/pico-sdk/lib/tinyusb/hw/bsp/board.c +++ b/pico-sdk/lib/tinyusb/hw/bsp/board.c @@ -108,6 +108,28 @@ TU_ATTR_USED int sys_read (int fhdl, char *buf, size_t count) { // st->st_mode = S_IFCHR; //} +// Clang use picolibc +#if defined(__clang__) +static int cl_putc(char c, FILE *f) { + (void) f; + return sys_write(0, &c, 1); +} + +static int cl_getc(FILE* f) { + (void) f; + char c; + return sys_read(0, &c, 1) > 0 ? c : -1; +} + +static FILE __stdio = FDEV_SETUP_STREAM(cl_putc, cl_getc, NULL, _FDEV_SETUP_RW); +FILE *const stdin = &__stdio; +__strong_reference(stdin, stdout); +__strong_reference(stdin, stderr); +#endif + +//--------------------------------------------------------------------+ +// Board API +//--------------------------------------------------------------------+ int board_getchar(void) { char c; return (sys_read(0, &c, 1) > 0) ? (int) c : (-1); diff --git a/pico-sdk/lib/tinyusb/hw/bsp/board_api.h b/pico-sdk/lib/tinyusb/hw/bsp/board_api.h index 404509a..eee9ed9 100644 --- a/pico-sdk/lib/tinyusb/hw/bsp/board_api.h +++ b/pico-sdk/lib/tinyusb/hw/bsp/board_api.h @@ -32,10 +32,30 @@ extern "C" { #endif #include +#include #include +#include #include "tusb.h" +#if CFG_TUSB_OS == OPT_OS_FREERTOS +#if TUP_MCU_ESPRESSIF + // ESP-IDF need "freertos/" prefix in include path. + // CFG_TUSB_OS_INC_PATH should be defined accordingly. + #include "freertos/FreeRTOS.h" + #include "freertos/semphr.h" + #include "freertos/queue.h" + #include "freertos/task.h" + #include "freertos/timers.h" +#else + #include "FreeRTOS.h" + #include "semphr.h" + #include "queue.h" + #include "task.h" + #include "timers.h" +#endif +#endif + // Define the default baudrate #ifndef CFG_BOARD_UART_BAUDRATE #define CFG_BOARD_UART_BAUDRATE 115200 ///< Default baud rate @@ -99,6 +119,7 @@ static inline uint32_t board_millis(void) { #elif CFG_TUSB_OS == OPT_OS_CUSTOM // Implement your own board_millis() in any of .c file +uint32_t board_millis(void); #else #error "board_millis() is not implemented for this OS" @@ -121,6 +142,7 @@ static inline size_t board_usb_get_serial(uint16_t desc_str1[], size_t max_chars uint8_t uid[16] TU_ATTR_ALIGNED(4); size_t uid_len; + // TODO work with make, but not working with esp32s3 cmake if ( board_get_unique_id ) { uid_len = board_get_unique_id(uid, sizeof(uid)); }else { diff --git a/pico-sdk/lib/tinyusb/hw/bsp/board_mcu.h b/pico-sdk/lib/tinyusb/hw/bsp/board_mcu.h index d40f33b..013eb1c 100644 --- a/pico-sdk/lib/tinyusb/hw/bsp/board_mcu.h +++ b/pico-sdk/lib/tinyusb/hw/bsp/board_mcu.h @@ -47,7 +47,7 @@ #elif TU_CHECK_MCU(OPT_MCU_LPC51UXX, OPT_MCU_LPC54XXX, OPT_MCU_LPC55XX, OPT_MCU_MCXN9) #include "fsl_device_registers.h" -#elif TU_CHECK_MCU(OPT_MCU_KINETIS_KL, OPT_MCU_KINETIS_K32L) +#elif TU_CHECK_MCU(OPT_MCU_KINETIS_KL, OPT_MCU_KINETIS_K32L, OPT_MCU_KINETIS_K) #include "fsl_device_registers.h" #elif CFG_TUSB_MCU == OPT_MCU_NRF5X diff --git a/pico-sdk/lib/tinyusb/hw/bsp/family_support.cmake b/pico-sdk/lib/tinyusb/hw/bsp/family_support.cmake index 539a776..6eef5b8 100644 --- a/pico-sdk/lib/tinyusb/hw/bsp/family_support.cmake +++ b/pico-sdk/lib/tinyusb/hw/bsp/family_support.cmake @@ -6,12 +6,33 @@ include(CMakePrintHelpers) set(TOP "${CMAKE_CURRENT_LIST_DIR}/../..") get_filename_component(TOP ${TOP} ABSOLUTE) -# Default to gcc +#------------------------------------------------------------- +# Toolchain +# Can be changed via -DTOOLCHAIN=gcc|iar or -DCMAKE_C_COMPILER= +#------------------------------------------------------------- +# Detect toolchain based on CMAKE_C_COMPILER +if (DEFINED CMAKE_C_COMPILER) + string(FIND ${CMAKE_C_COMPILER} "iccarm" IS_IAR) + string(FIND ${CMAKE_C_COMPILER} "clang" IS_CLANG) + string(FIND ${CMAKE_C_COMPILER} "gcc" IS_GCC) + + if (NOT IS_IAR EQUAL -1) + set(TOOLCHAIN iar) + elseif (NOT IS_CLANG EQUAL -1) + set(TOOLCHAIN clang) + elseif (NOT IS_GCC EQUAL -1) + set(TOOLCHAIN gcc) + endif () +endif () + +# default to gcc if (NOT DEFINED TOOLCHAIN) set(TOOLCHAIN gcc) endif () -# FAMILY not defined, try to detect it from BOARD +#------------------------------------------------------------- +# FAMILY and BOARD +#------------------------------------------------------------- if (NOT DEFINED FAMILY) if (NOT DEFINED BOARD) message(FATAL_ERROR "You must set a FAMILY variable for the build (e.g. rp2040, espressif). @@ -74,22 +95,35 @@ set(WARNING_FLAGS_GNU set(WARNING_FLAGS_IAR "") +#------------------------------------------------------------- +# Functions +#------------------------------------------------------------- # Filter example based on only.txt and skip.txt function(family_filter RESULT DIR) get_filename_component(DIR ${DIR} ABSOLUTE BASE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) - if (EXISTS "${DIR}/only.txt") - file(READ "${DIR}/only.txt" ONLYS) - # Replace newlines with semicolon so that it is treated as a list by CMake - string(REPLACE "\n" ";" ONLYS_LINES ${ONLYS}) + if (EXISTS "${DIR}/skip.txt") + file(STRINGS "${DIR}/skip.txt" SKIPS_LINES) + foreach(MCU IN LISTS FAMILY_MCUS) + # For each line in only.txt + foreach(_line ${SKIPS_LINES}) + # If mcu:xxx exists for this mcu then skip + if (${_line} STREQUAL "mcu:${MCU}" OR ${_line} STREQUAL "board:${BOARD}" OR ${_line} STREQUAL "family:${FAMILY}") + set(${RESULT} 0 PARENT_SCOPE) + return() + endif() + endforeach() + endforeach() + endif () - # For each mcu + if (EXISTS "${DIR}/only.txt") + file(STRINGS "${DIR}/only.txt" ONLYS_LINES) foreach(MCU IN LISTS FAMILY_MCUS) # For each line in only.txt foreach(_line ${ONLYS_LINES}) # If mcu:xxx exists for this mcu or board:xxx then include - if (${_line} STREQUAL "mcu:${MCU}" OR ${_line} STREQUAL "board:${BOARD}") + if (${_line} STREQUAL "mcu:${MCU}" OR ${_line} STREQUAL "board:${BOARD}" OR ${_line} STREQUAL "family:${FAMILY}") set(${RESULT} 1 PARENT_SCOPE) return() endif() @@ -98,29 +132,8 @@ function(family_filter RESULT DIR) # Didn't find it in only file so don't build set(${RESULT} 0 PARENT_SCOPE) - - elseif (EXISTS "${DIR}/skip.txt") - file(READ "${DIR}/skip.txt" SKIPS) - # Replace newlines with semicolon so that it is treated as a list by CMake - string(REPLACE "\n" ";" SKIPS_LINES ${SKIPS}) - - # For each mcu - foreach(MCU IN LISTS FAMILY_MCUS) - # For each line in only.txt - foreach(_line ${SKIPS_LINES}) - # If mcu:xxx exists for this mcu then skip - if (${_line} STREQUAL "mcu:${MCU}") - set(${RESULT} 0 PARENT_SCOPE) - return() - endif() - endforeach() - endforeach() - - # Didn't find in skip file so build - set(${RESULT} 1 PARENT_SCOPE) else() - - # Didn't find skip or only file so build + # only.txt not exist so build set(${RESULT} 1 PARENT_SCOPE) endif() endfunction() @@ -206,12 +219,12 @@ function(family_configure_common TARGET RTOS) if (CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 12.0) target_link_options(${TARGET} PUBLIC "LINKER:--no-warn-rwx-segments") endif () - endif() - if (CMAKE_C_COMPILER_ID STREQUAL "IAR") + elseif (CMAKE_C_COMPILER_ID STREQUAL "Clang") + target_link_options(${TARGET} PUBLIC "LINKER:-Map=$.map") + elseif (CMAKE_C_COMPILER_ID STREQUAL "IAR") target_link_options(${TARGET} PUBLIC "LINKER:--map=$.map") endif() - # ETM Trace option if (TRACE_ETM STREQUAL "1") target_compile_definitions(${TARGET} PUBLIC TRACE_ETM) @@ -226,7 +239,7 @@ function(family_configure_common TARGET RTOS) if (NOT TARGET segger_rtt) add_library(segger_rtt STATIC ${TOP}/lib/SEGGER_RTT/RTT/SEGGER_RTT.c) target_include_directories(segger_rtt PUBLIC ${TOP}/lib/SEGGER_RTT/RTT) - #target_compile_definitions(segger_rtt PUBLIC SEGGER_RTT_MODE_DEFAULT=SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL) +# target_compile_definitions(segger_rtt PUBLIC SEGGER_RTT_MODE_DEFAULT=SEGGER_RTT_MODE_BLOCK_IF_FIFO_FULL) endif() target_link_libraries(${TARGET} PUBLIC segger_rtt) endif () @@ -372,6 +385,10 @@ function(family_flash_jlink TARGET) set(JLINKEXE JLinkExe) endif () + if (NOT DEFINED JLINK_IF) + set(JLINK_IF swd) + endif () + file(GENERATE OUTPUT $/${TARGET}.jlink CONTENT "halt @@ -383,7 +400,7 @@ exit" add_custom_target(${TARGET}-jlink DEPENDS ${TARGET} - COMMAND ${JLINKEXE} -device ${JLINK_DEVICE} -if swd -JTAGConf -1,-1 -speed auto -CommandFile $/${TARGET}.jlink + COMMAND ${JLINKEXE} -device ${JLINK_DEVICE} -if ${JLINK_IF} -JTAGConf -1,-1 -speed auto -CommandFile $/${TARGET}.jlink ) endfunction() @@ -402,21 +419,36 @@ endfunction() # Add flash openocd target -function(family_flash_openocd TARGET CLI_OPTIONS) +function(family_flash_openocd TARGET) if (NOT DEFINED OPENOCD) set(OPENOCD openocd) endif () - separate_arguments(CLI_OPTIONS_LIST UNIX_COMMAND ${CLI_OPTIONS}) + if (NOT DEFINED OPENOCD_OPTION2) + set(OPENOCD_OPTION2 "") + endif () + + separate_arguments(OPTION_LIST UNIX_COMMAND ${OPENOCD_OPTION}) + separate_arguments(OPTION_LIST2 UNIX_COMMAND ${OPENOCD_OPTION2}) # note skip verify since it has issue with rp2040 add_custom_target(${TARGET}-openocd DEPENDS ${TARGET} - COMMAND ${OPENOCD} ${CLI_OPTIONS_LIST} -c "program $ reset exit" + COMMAND ${OPENOCD} ${OPTION_LIST} -c "program $ reset" ${OPTION_LIST2} -c exit VERBATIM ) endfunction() +# Add flash openocd-wch target +# compiled from https://github.com/hathach/riscv-openocd-wch or https://github.com/dragonlock2/miscboards/blob/main/wch/SDK/riscv-openocd.tar.xz +function(family_flash_openocd_wch TARGET) + if (NOT DEFINED OPENOCD) + set(OPENOCD $ENV{HOME}/app/riscv-openocd-wch/src/openocd) + endif () + + family_flash_openocd(${TARGET}) +endfunction() + # Add flash pycod target function(family_flash_pyocd TARGET) if (NOT DEFINED PYOC) @@ -430,6 +462,18 @@ function(family_flash_pyocd TARGET) endfunction() +# Add flash teensy_cli target +function(family_flash_teensy TARGET) + if (NOT DEFINED TEENSY_CLI) + set(TEENSY_CLI teensy_loader_cli) + endif () + + add_custom_target(${TARGET}-teensy + DEPENDS ${TARGET} + COMMAND ${TEENSY_CLI} --mcu=${TEENSY_MCU} -w -s $/${TARGET}.hex + ) +endfunction() + # Add flash using NXP's LinkServer (redserver) # https://www.nxp.com/design/software/development-software/mcuxpresso-software-and-tools-/linkserver-for-microcontrollers:LINKERSERVER function(family_flash_nxplink TARGET) @@ -460,6 +504,21 @@ function(family_flash_dfu_util TARGET OPTION) ) endfunction() +function(family_flash_msp430flasher TARGET) + if (NOT DEFINED MSP430Flasher) + set(MSP430FLASHER MSP430Flasher) + endif () + + # set LD_LIBRARY_PATH to find libmsp430.so (directory containing MSP430Flasher) + find_program(MSP430FLASHER_PATH MSP430Flasher) + get_filename_component(MSP430FLASHER_PARENT_DIR "${MSP430FLASHER_PATH}" DIRECTORY) + add_custom_target(${TARGET}-msp430flasher + DEPENDS ${TARGET} + COMMAND ${CMAKE_COMMAND} -E env LD_LIBRARY_PATH=${MSP430FLASHER_PARENT_DIR} + ${MSP430FLASHER} -w $/${TARGET}.hex -z [VCC] + ) +endfunction() + #---------------------------------- # Family specific #---------------------------------- diff --git a/pico-sdk/lib/tinyusb/hw/bsp/rp2040/family.cmake b/pico-sdk/lib/tinyusb/hw/bsp/rp2040/family.cmake index 222b4e7..93b4f72 100644 --- a/pico-sdk/lib/tinyusb/hw/bsp/rp2040/family.cmake +++ b/pico-sdk/lib/tinyusb/hw/bsp/rp2040/family.cmake @@ -21,6 +21,7 @@ if (NOT PICO_TINYUSB_PATH) endif() if (NOT TINYUSB_OPT_OS) + message("Setting OPT_OS_PICO") set(TINYUSB_OPT_OS OPT_OS_PICO) endif() @@ -71,6 +72,7 @@ target_sources(tinyusb_device_base INTERFACE ${TOP}/src/device/usbd_control.c ${TOP}/src/class/cdc/cdc_device.c ${TOP}/src/class/hid/hid_device.c + ${TOP}/src/class/msc/msc_device.c ) #------------------------------------ @@ -82,10 +84,7 @@ target_sources(tinyusb_host_base INTERFACE ${TOP}/src/portable/raspberrypi/rp2040/rp2040_usb.c ${TOP}/src/host/usbh.c ${TOP}/src/host/hub.c - ${TOP}/src/class/cdc/cdc_host.c ${TOP}/src/class/hid/hid_host.c - ${TOP}/src/class/msc/msc_host.c - ${TOP}/src/class/vendor/vendor_host.c ) # Sometimes have to do host specific actions in mostly common functions @@ -122,6 +121,8 @@ target_link_libraries(tinyusb_bsp INTERFACE pico_unique_id) # tinyusb_additions will hold our extra settings for examples add_library(tinyusb_additions INTERFACE) + +message("Setting PICO workarounds") target_compile_definitions(tinyusb_additions INTERFACE PICO_RP2040_USB_DEVICE_ENUMERATION_FIX=1 PICO_RP2040_USB_DEVICE_UFRAME_FIX=1 @@ -307,7 +308,6 @@ function(suppress_tinyusb_warnings) ${PICO_TINYUSB_PATH}/src/device/usbd_control.c ${PICO_TINYUSB_PATH}/src/host/usbh.c ${PICO_TINYUSB_PATH}/src/class/cdc/cdc_device.c - ${PICO_TINYUSB_PATH}/src/class/cdc/cdc_host.c ${PICO_TINYUSB_PATH}/src/class/hid/hid_device.c ${PICO_TINYUSB_PATH}/src/class/hid/hid_host.c ${PICO_TINYUSB_PATH}/src/class/audio/audio_device.c @@ -360,9 +360,6 @@ function(suppress_tinyusb_warnings) set_source_files_properties( ${PICO_TINYUSB_PATH}/src/class/cdc/cdc_device.c COMPILE_FLAGS "-Wno-unreachable-code") - set_source_files_properties( - ${PICO_TINYUSB_PATH}/src/class/cdc/cdc_host.c - COMPILE_FLAGS "-Wno-unreachable-code-fallthrough") set_source_files_properties( ${PICO_TINYUSB_PATH}/lib/fatfs/source/ff.c PROPERTIES diff --git a/pico-sdk/lib/tinyusb/src/CMakeLists.txt b/pico-sdk/lib/tinyusb/src/CMakeLists.txt index 4dcd33c..b6d6c2e 100644 --- a/pico-sdk/lib/tinyusb/src/CMakeLists.txt +++ b/pico-sdk/lib/tinyusb/src/CMakeLists.txt @@ -13,14 +13,22 @@ function(add_tinyusb TARGET) # device ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/device/usbd.c ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/device/usbd_control.c + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/audio/audio_device.c ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/cdc/cdc_device.c + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/dfu/dfu_device.c + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/dfu/dfu_rt_device.c ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/hid/hid_device.c + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/midi/midi_device.c + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/msc/msc_device.c + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/net/ecm_rndis_device.c + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/net/ncm_device.c + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/usbtmc/usbtmc_device.c + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/vendor/vendor_device.c + ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/video/video_device.c # host ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/host/usbh.c ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/host/hub.c - ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/cdc/cdc_host.c ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/hid/hid_host.c - ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/class/vendor/vendor_host.c # typec ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/typec/usbc.c ) @@ -30,7 +38,7 @@ function(add_tinyusb TARGET) ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../lib/networking ) - if (CMAKE_C_COMPILER_ID STREQUAL "GNU") + if (CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "Clang") target_compile_options(${TARGET} PRIVATE -Wall -Wextra diff --git a/pico-sdk/lib/tinyusb/src/class/cdc/cdc.h b/pico-sdk/lib/tinyusb/src/class/cdc/cdc.h index deec32a..5cbd658 100644 --- a/pico-sdk/lib/tinyusb/src/class/cdc/cdc.h +++ b/pico-sdk/lib/tinyusb/src/class/cdc/cdc.h @@ -136,8 +136,7 @@ typedef enum{ //--------------------------------------------------------------------+ /// Communication Interface Management Element Request Codes -typedef enum -{ +typedef enum { CDC_REQUEST_SEND_ENCAPSULATED_COMMAND = 0x00, ///< is used to issue a command in the format of the supported control protocol of the Communications Class interface CDC_REQUEST_GET_ENCAPSULATED_RESPONSE = 0x01, ///< is used to request a response in the format of the supported control protocol of the Communications Class interface. CDC_REQUEST_SET_COMM_FEATURE = 0x02, @@ -180,39 +179,38 @@ typedef enum CDC_REQUEST_GET_ATM_VC_STATISTICS = 0x53, CDC_REQUEST_MDLM_SEMANTIC_MODEL = 0x60, -}cdc_management_request_t; +} cdc_management_request_t; -enum { +typedef enum { CDC_CONTROL_LINE_STATE_DTR = 0x01, CDC_CONTROL_LINE_STATE_RTS = 0x02, -}; +} cdc_control_line_state_t; -enum { +typedef enum { CDC_LINE_CODING_STOP_BITS_1 = 0, // 1 bit CDC_LINE_CODING_STOP_BITS_1_5 = 1, // 1.5 bits CDC_LINE_CODING_STOP_BITS_2 = 2, // 2 bits -}; +} cdc_line_coding_stopbits_t; // TODO Backward compatible for typos. Maybe removed in the future release #define CDC_LINE_CONDING_STOP_BITS_1 CDC_LINE_CODING_STOP_BITS_1 #define CDC_LINE_CONDING_STOP_BITS_1_5 CDC_LINE_CODING_STOP_BITS_1_5 #define CDC_LINE_CONDING_STOP_BITS_2 CDC_LINE_CODING_STOP_BITS_2 -enum { +typedef enum { CDC_LINE_CODING_PARITY_NONE = 0, CDC_LINE_CODING_PARITY_ODD = 1, CDC_LINE_CODING_PARITY_EVEN = 2, CDC_LINE_CODING_PARITY_MARK = 3, CDC_LINE_CODING_PARITY_SPACE = 4, -}; +} cdc_line_coding_parity_t; //--------------------------------------------------------------------+ // Management Element Notification (Notification Endpoint) //--------------------------------------------------------------------+ /// 6.3 Notification Codes -typedef enum -{ +typedef enum { CDC_NOTIF_NETWORK_CONNECTION = 0x00, ///< This notification allows the device to notify the host about network connection status. CDC_NOTIF_RESPONSE_AVAILABLE = 0x01, ///< This notification allows the device to notify the hostthat a response is available. This response can be retrieved with a subsequent \ref CDC_REQUEST_GET_ENCAPSULATED_RESPONSE request. CDC_NOTIF_AUX_JACK_HOOK_STATE = 0x08, diff --git a/pico-sdk/lib/tinyusb/src/class/cdc/cdc_device.c b/pico-sdk/lib/tinyusb/src/class/cdc/cdc_device.c index c26264e..f36725e 100644 --- a/pico-sdk/lib/tinyusb/src/class/cdc/cdc_device.c +++ b/pico-sdk/lib/tinyusb/src/class/cdc/cdc_device.c @@ -43,10 +43,7 @@ //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ -enum -{ - BULK_PACKET_SIZE = (TUD_OPT_HIGH_SPEED ? 512 : 64) -}; +#define BULK_PACKET_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) typedef struct { @@ -176,9 +173,11 @@ uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize) uint16_t ret = tu_fifo_write_n(&p_cdc->tx_ff, buffer, (uint16_t) TU_MIN(bufsize, UINT16_MAX)); // flush if queue more than packet size - // may need to suppress -Wunreachable-code since most of the time CFG_TUD_CDC_TX_BUFSIZE < BULK_PACKET_SIZE - if ( (tu_fifo_count(&p_cdc->tx_ff) >= BULK_PACKET_SIZE) || ((CFG_TUD_CDC_TX_BUFSIZE < BULK_PACKET_SIZE) && tu_fifo_full(&p_cdc->tx_ff)) ) - { + if ( tu_fifo_count(&p_cdc->tx_ff) >= BULK_PACKET_SIZE + #if CFG_TUD_CDC_TX_BUFSIZE < BULK_PACKET_SIZE + || tu_fifo_full(&p_cdc->tx_ff) // check full if fifo size is less than packet size + #endif + ) { tud_cdc_n_write_flush(itf); } @@ -253,11 +252,39 @@ void cdcd_init(void) // In this way, the most current data is prioritized. tu_fifo_config(&p_cdc->tx_ff, p_cdc->tx_ff_buf, TU_ARRAY_SIZE(p_cdc->tx_ff_buf), 1, true); - tu_fifo_config_mutex(&p_cdc->rx_ff, NULL, osal_mutex_create(&p_cdc->rx_ff_mutex)); - tu_fifo_config_mutex(&p_cdc->tx_ff, osal_mutex_create(&p_cdc->tx_ff_mutex), NULL); + #if OSAL_MUTEX_REQUIRED + osal_mutex_t mutex_rd = osal_mutex_create(&p_cdc->rx_ff_mutex); + osal_mutex_t mutex_wr = osal_mutex_create(&p_cdc->tx_ff_mutex); + TU_ASSERT(mutex_rd != NULL && mutex_wr != NULL, ); + + tu_fifo_config_mutex(&p_cdc->rx_ff, NULL, mutex_rd); + tu_fifo_config_mutex(&p_cdc->tx_ff, mutex_wr, NULL); + #endif } } +bool cdcd_deinit(void) { + #if OSAL_MUTEX_REQUIRED + for(uint8_t i=0; irx_ff.mutex_rd; + osal_mutex_t mutex_wr = p_cdc->tx_ff.mutex_wr; + + if (mutex_rd) { + osal_mutex_delete(mutex_rd); + tu_fifo_config_mutex(&p_cdc->rx_ff, NULL, NULL); + } + + if (mutex_wr) { + osal_mutex_delete(mutex_wr); + tu_fifo_config_mutex(&p_cdc->tx_ff, NULL, NULL); + } + } + #endif + + return true; +} + void cdcd_reset(uint8_t rhport) { (void) rhport; @@ -268,7 +295,9 @@ void cdcd_reset(uint8_t rhport) tu_memclr(p_cdc, ITF_MEM_RESET_SIZE); tu_fifo_clear(&p_cdc->rx_ff); + #if !CFG_TUD_CDC_PERSISTENT_TX_BUFF tu_fifo_clear(&p_cdc->tx_ff); + #endif tu_fifo_set_overwritable(&p_cdc->tx_ff, true); } } diff --git a/pico-sdk/lib/tinyusb/src/class/cdc/cdc_device.h b/pico-sdk/lib/tinyusb/src/class/cdc/cdc_device.h index a6e07aa..db709b3 100644 --- a/pico-sdk/lib/tinyusb/src/class/cdc/cdc_device.h +++ b/pico-sdk/lib/tinyusb/src/class/cdc/cdc_device.h @@ -41,6 +41,12 @@ #define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) #endif +// By default the TX fifo buffer is cleared on connect / bus reset. +// Enable this to persist any data in the fifo instead. +#ifndef CFG_TUD_CDC_PERSISTENT_TX_BUFF + #define CFG_TUD_CDC_PERSISTENT_TX_BUFF (0) +#endif + #ifdef __cplusplus extern "C" { #endif @@ -247,6 +253,7 @@ static inline bool tud_cdc_write_clear(void) // INTERNAL USBD-CLASS DRIVER API //--------------------------------------------------------------------+ void cdcd_init (void); +bool cdcd_deinit (void); void cdcd_reset (uint8_t rhport); uint16_t cdcd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len); bool cdcd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request); diff --git a/pico-sdk/lib/tinyusb/src/class/cdc/cdc_host.c b/pico-sdk/lib/tinyusb/src/class/cdc/cdc_host.c deleted file mode 100644 index 2463671..0000000 --- a/pico-sdk/lib/tinyusb/src/class/cdc/cdc_host.c +++ /dev/null @@ -1,1174 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach (tinyusb.org) - * - * 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. - * - * This file is part of the TinyUSB stack. - */ - -#include "tusb_option.h" - -#if (CFG_TUH_ENABLED && CFG_TUH_CDC) - -#include "host/usbh.h" -#include "host/usbh_pvt.h" - -#include "cdc_host.h" - -// Level where CFG_TUSB_DEBUG must be at least for this driver is logged -#ifndef CFG_TUH_CDC_LOG_LEVEL - #define CFG_TUH_CDC_LOG_LEVEL CFG_TUH_LOG_LEVEL -#endif - -#define TU_LOG_DRV(...) TU_LOG(CFG_TUH_CDC_LOG_LEVEL, __VA_ARGS__) - -//--------------------------------------------------------------------+ -// Host CDC Interface -//--------------------------------------------------------------------+ - -typedef struct { - uint8_t daddr; - uint8_t bInterfaceNumber; - uint8_t bInterfaceSubClass; - uint8_t bInterfaceProtocol; - - uint8_t serial_drid; // Serial Driver ID - cdc_acm_capability_t acm_capability; - uint8_t ep_notif; - - uint8_t line_state; // DTR (bit0), RTS (bit1) - TU_ATTR_ALIGNED(4) cdc_line_coding_t line_coding; // Baudrate, stop bits, parity, data width - - tuh_xfer_cb_t user_control_cb; - - struct { - tu_edpt_stream_t tx; - tu_edpt_stream_t rx; - - uint8_t tx_ff_buf[CFG_TUH_CDC_TX_BUFSIZE]; - CFG_TUH_MEM_ALIGN uint8_t tx_ep_buf[CFG_TUH_CDC_TX_EPSIZE]; - - uint8_t rx_ff_buf[CFG_TUH_CDC_TX_BUFSIZE]; - CFG_TUH_MEM_ALIGN uint8_t rx_ep_buf[CFG_TUH_CDC_TX_EPSIZE]; - } stream; - -} cdch_interface_t; - -CFG_TUH_MEM_SECTION -static cdch_interface_t cdch_data[CFG_TUH_CDC]; - -//--------------------------------------------------------------------+ -// Serial Driver -//--------------------------------------------------------------------+ - -//------------- ACM prototypes -------------// -static bool acm_open(uint8_t daddr, tusb_desc_interface_t const *itf_desc, uint16_t max_len); -static void acm_process_config(tuh_xfer_t* xfer); - -static bool acm_set_line_coding(cdch_interface_t* p_cdc, cdc_line_coding_t const* line_coding, tuh_xfer_cb_t complete_cb, uintptr_t user_data); -static bool acm_set_control_line_state(cdch_interface_t* p_cdc, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data); -static bool acm_set_baudrate(cdch_interface_t* p_cdc, uint32_t baudrate, tuh_xfer_cb_t complete_cb, uintptr_t user_data); - -//------------- FTDI prototypes -------------// -#if CFG_TUH_CDC_FTDI -#include "serial/ftdi_sio.h" - -static uint16_t const ftdi_vid_pid_list[][2] = {CFG_TUH_CDC_FTDI_VID_PID_LIST }; -enum { - FTDI_PID_COUNT = sizeof(ftdi_vid_pid_list) / sizeof(ftdi_vid_pid_list[0]) -}; - -// Store last request baudrate since divisor to baudrate is not easy -static uint32_t _ftdi_requested_baud; - -static bool ftdi_open(uint8_t daddr, const tusb_desc_interface_t *itf_desc, uint16_t max_len); -static void ftdi_process_config(tuh_xfer_t* xfer); - -static bool ftdi_sio_set_modem_ctrl(cdch_interface_t* p_cdc, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data); -static bool ftdi_sio_set_baudrate(cdch_interface_t* p_cdc, uint32_t baudrate, tuh_xfer_cb_t complete_cb, uintptr_t user_data); -#endif - -//------------- CP210X prototypes -------------// -#if CFG_TUH_CDC_CP210X -#include "serial/cp210x.h" - -static uint16_t const cp210x_vid_pid_list[][2] = {CFG_TUH_CDC_CP210X_VID_PID_LIST }; -enum { - CP210X_PID_COUNT = sizeof(cp210x_vid_pid_list) / sizeof(cp210x_vid_pid_list[0]) -}; - -static bool cp210x_open(uint8_t daddr, tusb_desc_interface_t const *itf_desc, uint16_t max_len); -static void cp210x_process_config(tuh_xfer_t* xfer); - -static bool cp210x_set_modem_ctrl(cdch_interface_t* p_cdc, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data); -static bool cp210x_set_baudrate(cdch_interface_t* p_cdc, uint32_t baudrate, tuh_xfer_cb_t complete_cb, uintptr_t user_data); -#endif - -enum { - SERIAL_DRIVER_ACM = 0, - -#if CFG_TUH_CDC_FTDI - SERIAL_DRIVER_FTDI, -#endif - -#if CFG_TUH_CDC_CP210X - SERIAL_DRIVER_CP210X, -#endif -}; - -typedef struct { - void (*const process_set_config)(tuh_xfer_t* xfer); - bool (*const set_control_line_state)(cdch_interface_t* p_cdc, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data); - bool (*const set_baudrate)(cdch_interface_t* p_cdc, uint32_t baudrate, tuh_xfer_cb_t complete_cb, uintptr_t user_data); -} cdch_serial_driver_t; - -// Note driver list must be in the same order as SERIAL_DRIVER enum -static const cdch_serial_driver_t serial_drivers[] = { - { .process_set_config = acm_process_config, - .set_control_line_state = acm_set_control_line_state, - .set_baudrate = acm_set_baudrate - }, - - #if CFG_TUH_CDC_FTDI - { .process_set_config = ftdi_process_config, - .set_control_line_state = ftdi_sio_set_modem_ctrl, - .set_baudrate = ftdi_sio_set_baudrate - }, - #endif - - #if CFG_TUH_CDC_CP210X - { .process_set_config = cp210x_process_config, - .set_control_line_state = cp210x_set_modem_ctrl, - .set_baudrate = cp210x_set_baudrate - }, - #endif -}; - -enum { - SERIAL_DRIVER_COUNT = sizeof(serial_drivers) / sizeof(serial_drivers[0]) -}; - -//--------------------------------------------------------------------+ -// INTERNAL OBJECT & FUNCTION DECLARATION -//--------------------------------------------------------------------+ - -static inline cdch_interface_t* get_itf(uint8_t idx) -{ - TU_ASSERT(idx < CFG_TUH_CDC, NULL); - cdch_interface_t* p_cdc = &cdch_data[idx]; - - return (p_cdc->daddr != 0) ? p_cdc : NULL; -} - -static inline uint8_t get_idx_by_ep_addr(uint8_t daddr, uint8_t ep_addr) -{ - for(uint8_t i=0; idaddr == daddr) && - (ep_addr == p_cdc->ep_notif || ep_addr == p_cdc->stream.rx.ep_addr || ep_addr == p_cdc->stream.tx.ep_addr)) - { - return i; - } - } - - return TUSB_INDEX_INVALID_8; -} - - -static cdch_interface_t* make_new_itf(uint8_t daddr, tusb_desc_interface_t const *itf_desc) -{ - for(uint8_t i=0; idaddr = daddr; - p_cdc->bInterfaceNumber = itf_desc->bInterfaceNumber; - p_cdc->bInterfaceSubClass = itf_desc->bInterfaceSubClass; - p_cdc->bInterfaceProtocol = itf_desc->bInterfaceProtocol; - p_cdc->line_state = 0; - return p_cdc; - } - } - - return NULL; -} - -static bool open_ep_stream_pair(cdch_interface_t* p_cdc , tusb_desc_endpoint_t const *desc_ep); -static void set_config_complete(cdch_interface_t * p_cdc, uint8_t idx, uint8_t itf_num); -static void cdch_internal_control_complete(tuh_xfer_t* xfer); - -//--------------------------------------------------------------------+ -// APPLICATION API -//--------------------------------------------------------------------+ - -uint8_t tuh_cdc_itf_get_index(uint8_t daddr, uint8_t itf_num) -{ - for(uint8_t i=0; idaddr == daddr && p_cdc->bInterfaceNumber == itf_num) return i; - } - - return TUSB_INDEX_INVALID_8; -} - -bool tuh_cdc_itf_get_info(uint8_t idx, tuh_itf_info_t* info) -{ - cdch_interface_t* p_cdc = get_itf(idx); - TU_VERIFY(p_cdc && info); - - info->daddr = p_cdc->daddr; - - // re-construct descriptor - tusb_desc_interface_t* desc = &info->desc; - desc->bLength = sizeof(tusb_desc_interface_t); - desc->bDescriptorType = TUSB_DESC_INTERFACE; - - desc->bInterfaceNumber = p_cdc->bInterfaceNumber; - desc->bAlternateSetting = 0; - desc->bNumEndpoints = 2u + (p_cdc->ep_notif ? 1u : 0u); - desc->bInterfaceClass = TUSB_CLASS_CDC; - desc->bInterfaceSubClass = p_cdc->bInterfaceSubClass; - desc->bInterfaceProtocol = p_cdc->bInterfaceProtocol; - desc->iInterface = 0; // not used yet - - return true; -} - -bool tuh_cdc_mounted(uint8_t idx) -{ - cdch_interface_t* p_cdc = get_itf(idx); - return p_cdc != NULL; -} - -bool tuh_cdc_get_dtr(uint8_t idx) -{ - cdch_interface_t* p_cdc = get_itf(idx); - TU_VERIFY(p_cdc); - - return (p_cdc->line_state & CDC_CONTROL_LINE_STATE_DTR) ? true : false; -} - -bool tuh_cdc_get_rts(uint8_t idx) -{ - cdch_interface_t* p_cdc = get_itf(idx); - TU_VERIFY(p_cdc); - - return (p_cdc->line_state & CDC_CONTROL_LINE_STATE_RTS) ? true : false; -} - -bool tuh_cdc_get_local_line_coding(uint8_t idx, cdc_line_coding_t* line_coding) -{ - cdch_interface_t* p_cdc = get_itf(idx); - TU_VERIFY(p_cdc); - - *line_coding = p_cdc->line_coding; - - return true; -} - -//--------------------------------------------------------------------+ -// Write -//--------------------------------------------------------------------+ - -uint32_t tuh_cdc_write(uint8_t idx, void const* buffer, uint32_t bufsize) -{ - cdch_interface_t* p_cdc = get_itf(idx); - TU_VERIFY(p_cdc); - - return tu_edpt_stream_write(&p_cdc->stream.tx, buffer, bufsize); -} - -uint32_t tuh_cdc_write_flush(uint8_t idx) -{ - cdch_interface_t* p_cdc = get_itf(idx); - TU_VERIFY(p_cdc); - - return tu_edpt_stream_write_xfer(&p_cdc->stream.tx); -} - -bool tuh_cdc_write_clear(uint8_t idx) -{ - cdch_interface_t* p_cdc = get_itf(idx); - TU_VERIFY(p_cdc); - - return tu_edpt_stream_clear(&p_cdc->stream.tx); -} - -uint32_t tuh_cdc_write_available(uint8_t idx) -{ - cdch_interface_t* p_cdc = get_itf(idx); - TU_VERIFY(p_cdc); - - return tu_edpt_stream_write_available(&p_cdc->stream.tx); -} - -//--------------------------------------------------------------------+ -// Read -//--------------------------------------------------------------------+ - -uint32_t tuh_cdc_read (uint8_t idx, void* buffer, uint32_t bufsize) -{ - cdch_interface_t* p_cdc = get_itf(idx); - TU_VERIFY(p_cdc); - - return tu_edpt_stream_read(&p_cdc->stream.rx, buffer, bufsize); -} - -uint32_t tuh_cdc_read_available(uint8_t idx) -{ - cdch_interface_t* p_cdc = get_itf(idx); - TU_VERIFY(p_cdc); - - return tu_edpt_stream_read_available(&p_cdc->stream.rx); -} - -bool tuh_cdc_peek(uint8_t idx, uint8_t* ch) -{ - cdch_interface_t* p_cdc = get_itf(idx); - TU_VERIFY(p_cdc); - - return tu_edpt_stream_peek(&p_cdc->stream.rx, ch); -} - -bool tuh_cdc_read_clear (uint8_t idx) -{ - cdch_interface_t* p_cdc = get_itf(idx); - TU_VERIFY(p_cdc); - - bool ret = tu_edpt_stream_clear(&p_cdc->stream.rx); - tu_edpt_stream_read_xfer(&p_cdc->stream.rx); - return ret; -} - -//--------------------------------------------------------------------+ -// Control Endpoint API -//--------------------------------------------------------------------+ - -// internal control complete to update state such as line state, encoding -static void cdch_internal_control_complete(tuh_xfer_t* xfer) -{ - uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex); - uint8_t idx = tuh_cdc_itf_get_index(xfer->daddr, itf_num); - cdch_interface_t* p_cdc = get_itf(idx); - TU_ASSERT(p_cdc, ); - - if (xfer->result == XFER_RESULT_SUCCESS) - { - switch (p_cdc->serial_drid) { - case SERIAL_DRIVER_ACM: - switch (xfer->setup->bRequest) { - case CDC_REQUEST_SET_CONTROL_LINE_STATE: - p_cdc->line_state = (uint8_t) tu_le16toh(xfer->setup->wValue); - break; - - case CDC_REQUEST_SET_LINE_CODING: { - uint16_t const len = tu_min16(sizeof(cdc_line_coding_t), tu_le16toh(xfer->setup->wLength)); - memcpy(&p_cdc->line_coding, xfer->buffer, len); - } - break; - - default: break; - } - break; - - #if CFG_TUH_CDC_FTDI - case SERIAL_DRIVER_FTDI: - switch (xfer->setup->bRequest) { - case FTDI_SIO_MODEM_CTRL: - p_cdc->line_state = (uint8_t) (tu_le16toh(xfer->setup->wValue) & 0x00ff); - break; - - case FTDI_SIO_SET_BAUD_RATE: - // convert from divisor to baudrate is not supported - p_cdc->line_coding.bit_rate = _ftdi_requested_baud; - break; - - default: break; - } - break; - #endif - - #if CFG_TUH_CDC_CP210X - case SERIAL_DRIVER_CP210X: - switch(xfer->setup->bRequest) { - case CP210X_SET_MHS: - p_cdc->line_state = (uint8_t) (tu_le16toh(xfer->setup->wValue) & 0x00ff); - break; - - case CP210X_SET_BAUDRATE: { - uint32_t baudrate; - memcpy(&baudrate, xfer->buffer, sizeof(uint32_t)); - p_cdc->line_coding.bit_rate = tu_le32toh(baudrate); - } - break; - } - break; - #endif - - default: break; - } - } - - xfer->complete_cb = p_cdc->user_control_cb; - if (xfer->complete_cb) { - xfer->complete_cb(xfer); - } -} - -bool tuh_cdc_set_control_line_state(uint8_t idx, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data) { - cdch_interface_t* p_cdc = get_itf(idx); - TU_VERIFY(p_cdc && p_cdc->serial_drid < SERIAL_DRIVER_COUNT); - cdch_serial_driver_t const* driver = &serial_drivers[p_cdc->serial_drid]; - - if ( complete_cb ) { - return driver->set_control_line_state(p_cdc, line_state, complete_cb, user_data); - }else { - // blocking - xfer_result_t result = XFER_RESULT_INVALID; - bool ret = driver->set_control_line_state(p_cdc, line_state, complete_cb, (uintptr_t) &result); - - if (user_data) { - // user_data is not NULL, return result via user_data - *((xfer_result_t*) user_data) = result; - } - - TU_VERIFY(ret && result == XFER_RESULT_SUCCESS); - - p_cdc->line_state = (uint8_t) line_state; - return true; - } -} - -bool tuh_cdc_set_baudrate(uint8_t idx, uint32_t baudrate, tuh_xfer_cb_t complete_cb, uintptr_t user_data) { - cdch_interface_t* p_cdc = get_itf(idx); - TU_VERIFY(p_cdc && p_cdc->serial_drid < SERIAL_DRIVER_COUNT); - cdch_serial_driver_t const* driver = &serial_drivers[p_cdc->serial_drid]; - - if ( complete_cb ) { - return driver->set_baudrate(p_cdc, baudrate, complete_cb, user_data); - }else { - // blocking - xfer_result_t result = XFER_RESULT_INVALID; - bool ret = driver->set_baudrate(p_cdc, baudrate, complete_cb, (uintptr_t) &result); - - if (user_data) { - // user_data is not NULL, return result via user_data - *((xfer_result_t*) user_data) = result; - } - - TU_VERIFY(ret && result == XFER_RESULT_SUCCESS); - - p_cdc->line_coding.bit_rate = baudrate; - return true; - } -} - -bool tuh_cdc_set_line_coding(uint8_t idx, cdc_line_coding_t const* line_coding, tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ - cdch_interface_t* p_cdc = get_itf(idx); - // only ACM support this set line coding request - TU_VERIFY(p_cdc && p_cdc->serial_drid == SERIAL_DRIVER_ACM); - TU_VERIFY(p_cdc->acm_capability.support_line_request); - - if ( complete_cb ) { - return acm_set_line_coding(p_cdc, line_coding, complete_cb, user_data); - }else { - // blocking - xfer_result_t result = XFER_RESULT_INVALID; - bool ret = acm_set_line_coding(p_cdc, line_coding, complete_cb, (uintptr_t) &result); - - if (user_data) { - // user_data is not NULL, return result via user_data - *((xfer_result_t*) user_data) = result; - } - - TU_VERIFY(ret && result == XFER_RESULT_SUCCESS); - - p_cdc->line_coding = *line_coding; - return true; - } -} - -//--------------------------------------------------------------------+ -// CLASS-USBH API -//--------------------------------------------------------------------+ - -void cdch_init(void) -{ - tu_memclr(cdch_data, sizeof(cdch_data)); - - for(size_t i=0; istream.tx, true, true, false, - p_cdc->stream.tx_ff_buf, CFG_TUH_CDC_TX_BUFSIZE, - p_cdc->stream.tx_ep_buf, CFG_TUH_CDC_TX_EPSIZE); - - tu_edpt_stream_init(&p_cdc->stream.rx, true, false, false, - p_cdc->stream.rx_ff_buf, CFG_TUH_CDC_RX_BUFSIZE, - p_cdc->stream.rx_ep_buf, CFG_TUH_CDC_RX_EPSIZE); - } -} - -void cdch_close(uint8_t daddr) -{ - for(uint8_t idx=0; idxdaddr == daddr) - { - TU_LOG_DRV(" CDCh close addr = %u index = %u\r\n", daddr, idx); - - // Invoke application callback - if (tuh_cdc_umount_cb) tuh_cdc_umount_cb(idx); - - //tu_memclr(p_cdc, sizeof(cdch_interface_t)); - p_cdc->daddr = 0; - p_cdc->bInterfaceNumber = 0; - tu_edpt_stream_close(&p_cdc->stream.tx); - tu_edpt_stream_close(&p_cdc->stream.rx); - } - } -} - -bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) { - // TODO handle stall response, retry failed transfer ... - TU_ASSERT(event == XFER_RESULT_SUCCESS); - - uint8_t const idx = get_idx_by_ep_addr(daddr, ep_addr); - cdch_interface_t * p_cdc = get_itf(idx); - TU_ASSERT(p_cdc); - - if ( ep_addr == p_cdc->stream.tx.ep_addr ) { - // invoke tx complete callback to possibly refill tx fifo - if (tuh_cdc_tx_complete_cb) tuh_cdc_tx_complete_cb(idx); - - if ( 0 == tu_edpt_stream_write_xfer(&p_cdc->stream.tx) ) { - // If there is no data left, a ZLP should be sent if: - // - xferred_bytes is multiple of EP Packet size and not zero - tu_edpt_stream_write_zlp_if_needed(&p_cdc->stream.tx, xferred_bytes); - } - } - else if ( ep_addr == p_cdc->stream.rx.ep_addr ) { - #if CFG_TUH_CDC_FTDI - if (p_cdc->serial_drid == SERIAL_DRIVER_FTDI) { - // FTDI reserve 2 bytes for status - // FTDI status -// uint8_t status[2] = { -// p_cdc->stream.rx.ep_buf[0], -// p_cdc->stream.rx.ep_buf[1] -// }; - tu_edpt_stream_read_xfer_complete_offset(&p_cdc->stream.rx, xferred_bytes, 2); - }else - #endif - { - tu_edpt_stream_read_xfer_complete(&p_cdc->stream.rx, xferred_bytes); - } - - // invoke receive callback - if (tuh_cdc_rx_cb) tuh_cdc_rx_cb(idx); - - // prepare for next transfer if needed - tu_edpt_stream_read_xfer(&p_cdc->stream.rx); - }else if ( ep_addr == p_cdc->ep_notif ) { - // TODO handle notification endpoint - }else { - TU_ASSERT(false); - } - - return true; -} - -//--------------------------------------------------------------------+ -// Enumeration -//--------------------------------------------------------------------+ - -static bool open_ep_stream_pair(cdch_interface_t* p_cdc, tusb_desc_endpoint_t const *desc_ep) -{ - for(size_t i=0; i<2; i++) - { - TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType && - TUSB_XFER_BULK == desc_ep->bmAttributes.xfer); - - TU_ASSERT(tuh_edpt_open(p_cdc->daddr, desc_ep)); - - if ( tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN ) - { - tu_edpt_stream_open(&p_cdc->stream.rx, p_cdc->daddr, desc_ep); - }else - { - tu_edpt_stream_open(&p_cdc->stream.tx, p_cdc->daddr, desc_ep); - } - - desc_ep = (tusb_desc_endpoint_t const*) tu_desc_next(desc_ep); - } - - return true; -} - -bool cdch_open(uint8_t rhport, uint8_t daddr, tusb_desc_interface_t const *itf_desc, uint16_t max_len) -{ - (void) rhport; - - // Only support ACM subclass - // Note: Protocol 0xFF can be RNDIS device - if ( TUSB_CLASS_CDC == itf_desc->bInterfaceClass && - CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass) - { - return acm_open(daddr, itf_desc, max_len); - } - #if CFG_TUH_CDC_FTDI || CFG_TUH_CDC_CP210X - else if ( TUSB_CLASS_VENDOR_SPECIFIC == itf_desc->bInterfaceClass ) - { - uint16_t vid, pid; - TU_VERIFY(tuh_vid_pid_get(daddr, &vid, &pid)); - - #if CFG_TUH_CDC_FTDI - for (size_t i = 0; i < FTDI_PID_COUNT; i++) { - if (ftdi_vid_pid_list[i][0] == vid && ftdi_vid_pid_list[i][1] == pid) { - return ftdi_open(daddr, itf_desc, max_len); - } - } - #endif - - #if CFG_TUH_CDC_CP210X - for (size_t i = 0; i < CP210X_PID_COUNT; i++) { - if (cp210x_vid_pid_list[i][0] == vid && cp210x_vid_pid_list[i][1] == pid) { - return cp210x_open(daddr, itf_desc, max_len); - } - } - #endif - } - #endif - - return false; -} - -static void set_config_complete(cdch_interface_t * p_cdc, uint8_t idx, uint8_t itf_num) { - if (tuh_cdc_mount_cb) tuh_cdc_mount_cb(idx); - - // Prepare for incoming data - tu_edpt_stream_read_xfer(&p_cdc->stream.rx); - - // notify usbh that driver enumeration is complete - usbh_driver_set_config_complete(p_cdc->daddr, itf_num); -} - - -bool cdch_set_config(uint8_t daddr, uint8_t itf_num) -{ - tusb_control_request_t request; - request.wIndex = tu_htole16((uint16_t) itf_num); - - // fake transfer to kick-off process - tuh_xfer_t xfer; - xfer.daddr = daddr; - xfer.result = XFER_RESULT_SUCCESS; - xfer.setup = &request; - xfer.user_data = 0; // initial state - - uint8_t const idx = tuh_cdc_itf_get_index(daddr, itf_num); - cdch_interface_t * p_cdc = get_itf(idx); - TU_ASSERT(p_cdc && p_cdc->serial_drid < SERIAL_DRIVER_COUNT); - - serial_drivers[p_cdc->serial_drid].process_set_config(&xfer); - return true; -} - -//--------------------------------------------------------------------+ -// ACM -//--------------------------------------------------------------------+ - -enum { - CONFIG_ACM_SET_CONTROL_LINE_STATE = 0, - CONFIG_ACM_SET_LINE_CODING, - CONFIG_ACM_COMPLETE, -}; - -static bool acm_open(uint8_t daddr, tusb_desc_interface_t const *itf_desc, uint16_t max_len) -{ - uint8_t const * p_desc_end = ((uint8_t const*) itf_desc) + max_len; - - cdch_interface_t * p_cdc = make_new_itf(daddr, itf_desc); - TU_VERIFY(p_cdc); - - p_cdc->serial_drid = SERIAL_DRIVER_ACM; - - //------------- Control Interface -------------// - uint8_t const * p_desc = tu_desc_next(itf_desc); - - // Communication Functional Descriptors - while( (p_desc < p_desc_end) && (TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc)) ) - { - if ( CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT == cdc_functional_desc_typeof(p_desc) ) - { - // save ACM bmCapabilities - p_cdc->acm_capability = ((cdc_desc_func_acm_t const *) p_desc)->bmCapabilities; - } - - p_desc = tu_desc_next(p_desc); - } - - // Open notification endpoint of control interface if any - if (itf_desc->bNumEndpoints == 1) - { - TU_ASSERT(TUSB_DESC_ENDPOINT == tu_desc_type(p_desc)); - tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc; - - TU_ASSERT( tuh_edpt_open(daddr, desc_ep) ); - p_cdc->ep_notif = desc_ep->bEndpointAddress; - - p_desc = tu_desc_next(p_desc); - } - - //------------- Data Interface (if any) -------------// - if ( (TUSB_DESC_INTERFACE == tu_desc_type(p_desc)) && - (TUSB_CLASS_CDC_DATA == ((tusb_desc_interface_t const *) p_desc)->bInterfaceClass) ) - { - // next to endpoint descriptor - p_desc = tu_desc_next(p_desc); - - // data endpoints expected to be in pairs - TU_ASSERT(open_ep_stream_pair(p_cdc, (tusb_desc_endpoint_t const *) p_desc)); - } - - return true; -} - -static void acm_process_config(tuh_xfer_t* xfer) -{ - uintptr_t const state = xfer->user_data; - uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex); - uint8_t const idx = tuh_cdc_itf_get_index(xfer->daddr, itf_num); - cdch_interface_t * p_cdc = get_itf(idx); - TU_ASSERT(p_cdc, ); - - switch(state) - { - case CONFIG_ACM_SET_CONTROL_LINE_STATE: - #if CFG_TUH_CDC_LINE_CONTROL_ON_ENUM - if (p_cdc->acm_capability.support_line_request) - { - TU_ASSERT(acm_set_control_line_state(p_cdc, CFG_TUH_CDC_LINE_CONTROL_ON_ENUM, acm_process_config, - CONFIG_ACM_SET_LINE_CODING), ); - break; - } - #endif - TU_ATTR_FALLTHROUGH; - - case CONFIG_ACM_SET_LINE_CODING: - #ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM - if (p_cdc->acm_capability.support_line_request) - { - cdc_line_coding_t line_coding = CFG_TUH_CDC_LINE_CODING_ON_ENUM; - TU_ASSERT(acm_set_line_coding(p_cdc, &line_coding, acm_process_config, CONFIG_ACM_COMPLETE), ); - break; - } - #endif - TU_ATTR_FALLTHROUGH; - - case CONFIG_ACM_COMPLETE: - // itf_num+1 to account for data interface as well - set_config_complete(p_cdc, idx, itf_num+1); - break; - - default: break; - } -} - -static bool acm_set_control_line_state(cdch_interface_t* p_cdc, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data) { - TU_VERIFY(p_cdc->acm_capability.support_line_request); - TU_LOG_DRV("CDC ACM Set Control Line State\r\n"); - - tusb_control_request_t const request = { - .bmRequestType_bit = { - .recipient = TUSB_REQ_RCPT_INTERFACE, - .type = TUSB_REQ_TYPE_CLASS, - .direction = TUSB_DIR_OUT - }, - .bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE, - .wValue = tu_htole16(line_state), - .wIndex = tu_htole16((uint16_t) p_cdc->bInterfaceNumber), - .wLength = 0 - }; - - p_cdc->user_control_cb = complete_cb; - - tuh_xfer_t xfer = { - .daddr = p_cdc->daddr, - .ep_addr = 0, - .setup = &request, - .buffer = NULL, - .complete_cb = complete_cb ? cdch_internal_control_complete : NULL, // complete_cb is NULL for sync call - .user_data = user_data - }; - - TU_ASSERT(tuh_control_xfer(&xfer)); - return true; -} - -static bool acm_set_line_coding(cdch_interface_t* p_cdc, cdc_line_coding_t const* line_coding, tuh_xfer_cb_t complete_cb, uintptr_t user_data) { - TU_LOG_DRV("CDC ACM Set Line Conding\r\n"); - - tusb_control_request_t const request = { - .bmRequestType_bit = { - .recipient = TUSB_REQ_RCPT_INTERFACE, - .type = TUSB_REQ_TYPE_CLASS, - .direction = TUSB_DIR_OUT - }, - .bRequest = CDC_REQUEST_SET_LINE_CODING, - .wValue = 0, - .wIndex = tu_htole16(p_cdc->bInterfaceNumber), - .wLength = tu_htole16(sizeof(cdc_line_coding_t)) - }; - - // use usbh enum buf to hold line coding since user line_coding variable does not live long enough - uint8_t* enum_buf = usbh_get_enum_buf(); - memcpy(enum_buf, line_coding, sizeof(cdc_line_coding_t)); - - p_cdc->user_control_cb = complete_cb; - tuh_xfer_t xfer = { - .daddr = p_cdc->daddr, - .ep_addr = 0, - .setup = &request, - .buffer = enum_buf, - .complete_cb = complete_cb ? cdch_internal_control_complete : NULL, // complete_cb is NULL for sync call - .user_data = user_data - }; - - TU_ASSERT(tuh_control_xfer(&xfer)); - return true; -} - -static bool acm_set_baudrate(cdch_interface_t* p_cdc, uint32_t baudrate, tuh_xfer_cb_t complete_cb, uintptr_t user_data) { - TU_VERIFY(p_cdc->acm_capability.support_line_request); - cdc_line_coding_t line_coding = p_cdc->line_coding; - line_coding.bit_rate = baudrate; - return acm_set_line_coding(p_cdc, &line_coding, complete_cb, user_data); -} - -//--------------------------------------------------------------------+ -// FTDI -//--------------------------------------------------------------------+ -#if CFG_TUH_CDC_FTDI - -enum { - CONFIG_FTDI_RESET = 0, - CONFIG_FTDI_MODEM_CTRL, - CONFIG_FTDI_SET_BAUDRATE, - CONFIG_FTDI_SET_DATA, - CONFIG_FTDI_COMPLETE -}; - -static bool ftdi_open(uint8_t daddr, const tusb_desc_interface_t *itf_desc, uint16_t max_len) { - // FTDI Interface includes 1 vendor interface + 2 bulk endpoints - TU_VERIFY(itf_desc->bInterfaceSubClass == 0xff && itf_desc->bInterfaceProtocol == 0xff && itf_desc->bNumEndpoints == 2); - TU_VERIFY(sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t) <= max_len); - - cdch_interface_t * p_cdc = make_new_itf(daddr, itf_desc); - TU_VERIFY(p_cdc); - - TU_LOG_DRV("FTDI opened\r\n"); - - p_cdc->serial_drid = SERIAL_DRIVER_FTDI; - - // endpoint pair - tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next(itf_desc); - - // data endpoints expected to be in pairs - return open_ep_stream_pair(p_cdc, desc_ep); -} - -// set request without data -static bool ftdi_sio_set_request(cdch_interface_t* p_cdc, uint8_t command, uint16_t value, tuh_xfer_cb_t complete_cb, uintptr_t user_data) { - tusb_control_request_t const request = { - .bmRequestType_bit = { - .recipient = TUSB_REQ_RCPT_DEVICE, - .type = TUSB_REQ_TYPE_VENDOR, - .direction = TUSB_DIR_OUT - }, - .bRequest = command, - .wValue = tu_htole16(value), - .wIndex = 0, - .wLength = 0 - }; - - tuh_xfer_t xfer = { - .daddr = p_cdc->daddr, - .ep_addr = 0, - .setup = &request, - .buffer = NULL, - .complete_cb = complete_cb, - .user_data = user_data - }; - - return tuh_control_xfer(&xfer); -} - -static bool ftdi_sio_reset(cdch_interface_t* p_cdc, tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ - return ftdi_sio_set_request(p_cdc, FTDI_SIO_RESET, FTDI_SIO_RESET_SIO, complete_cb, user_data); -} - -static bool ftdi_sio_set_modem_ctrl(cdch_interface_t* p_cdc, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ - TU_LOG_DRV("CDC FTDI Set Control Line State\r\n"); - p_cdc->user_control_cb = complete_cb; - TU_ASSERT(ftdi_sio_set_request(p_cdc, FTDI_SIO_MODEM_CTRL, 0x0300 | line_state, - complete_cb ? cdch_internal_control_complete : NULL, user_data)); - return true; -} - -static uint32_t ftdi_232bm_baud_base_to_divisor(uint32_t baud, uint32_t base) -{ - const uint8_t divfrac[8] = { 0, 3, 2, 4, 1, 5, 6, 7 }; - uint32_t divisor; - - /* divisor shifted 3 bits to the left */ - uint32_t divisor3 = base / (2 * baud); - divisor = (divisor3 >> 3); - divisor |= (uint32_t) divfrac[divisor3 & 0x7] << 14; - - /* Deal with special cases for highest baud rates. */ - if (divisor == 1) { /* 1.0 */ - divisor = 0; - } - else if (divisor == 0x4001) { /* 1.5 */ - divisor = 1; - } - - return divisor; -} - -static uint32_t ftdi_232bm_baud_to_divisor(uint32_t baud) -{ - return ftdi_232bm_baud_base_to_divisor(baud, 48000000u); -} - -static bool ftdi_sio_set_baudrate(cdch_interface_t* p_cdc, uint32_t baudrate, tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ - uint16_t const divisor = (uint16_t) ftdi_232bm_baud_to_divisor(baudrate); - TU_LOG_DRV("CDC FTDI Set BaudRate = %lu, divisor = 0x%04x\r\n", baudrate, divisor); - - p_cdc->user_control_cb = complete_cb; - _ftdi_requested_baud = baudrate; - TU_ASSERT(ftdi_sio_set_request(p_cdc, FTDI_SIO_SET_BAUD_RATE, divisor, - complete_cb ? cdch_internal_control_complete : NULL, user_data)); - - return true; -} - -static void ftdi_process_config(tuh_xfer_t* xfer) { - uintptr_t const state = xfer->user_data; - uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex); - uint8_t const idx = tuh_cdc_itf_get_index(xfer->daddr, itf_num); - cdch_interface_t * p_cdc = get_itf(idx); - TU_ASSERT(p_cdc, ); - - switch(state) { - // Note may need to read FTDI eeprom - case CONFIG_FTDI_RESET: - TU_ASSERT(ftdi_sio_reset(p_cdc, ftdi_process_config, CONFIG_FTDI_MODEM_CTRL),); - break; - - case CONFIG_FTDI_MODEM_CTRL: - #if CFG_TUH_CDC_LINE_CONTROL_ON_ENUM - TU_ASSERT( - ftdi_sio_set_modem_ctrl(p_cdc, CFG_TUH_CDC_LINE_CONTROL_ON_ENUM, ftdi_process_config, CONFIG_FTDI_SET_BAUDRATE),); - break; - #else - TU_ATTR_FALLTHROUGH; - #endif - - case CONFIG_FTDI_SET_BAUDRATE: { - #ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM - cdc_line_coding_t line_coding = CFG_TUH_CDC_LINE_CODING_ON_ENUM; - TU_ASSERT(ftdi_sio_set_baudrate(p_cdc, line_coding.bit_rate, ftdi_process_config, CONFIG_FTDI_SET_DATA),); - break; - #else - TU_ATTR_FALLTHROUGH; - #endif - } - - case CONFIG_FTDI_SET_DATA: { - #if 0 // TODO set data format - #ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM - cdc_line_coding_t line_coding = CFG_TUH_CDC_LINE_CODING_ON_ENUM; - TU_ASSERT(ftdi_sio_set_data(p_cdc, process_ftdi_config, CONFIG_FTDI_COMPLETE),); - break; - #endif - #endif - - TU_ATTR_FALLTHROUGH; - } - - case CONFIG_FTDI_COMPLETE: - set_config_complete(p_cdc, idx, itf_num); - break; - - default: - break; - } -} - -#endif - -//--------------------------------------------------------------------+ -// CP210x -//--------------------------------------------------------------------+ - -#if CFG_TUH_CDC_CP210X - -enum { - CONFIG_CP210X_IFC_ENABLE = 0, - CONFIG_CP210X_SET_BAUDRATE, - CONFIG_CP210X_SET_LINE_CTL, - CONFIG_CP210X_SET_DTR_RTS, - CONFIG_CP210X_COMPLETE -}; - -static bool cp210x_open(uint8_t daddr, tusb_desc_interface_t const *itf_desc, uint16_t max_len) { - // CP210x Interface includes 1 vendor interface + 2 bulk endpoints - TU_VERIFY(itf_desc->bInterfaceSubClass == 0 && itf_desc->bInterfaceProtocol == 0 && itf_desc->bNumEndpoints == 2); - TU_VERIFY(sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t) <= max_len); - - cdch_interface_t * p_cdc = make_new_itf(daddr, itf_desc); - TU_VERIFY(p_cdc); - - TU_LOG_DRV("CP210x opened\r\n"); - p_cdc->serial_drid = SERIAL_DRIVER_CP210X; - - // endpoint pair - tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) tu_desc_next(itf_desc); - - // data endpoints expected to be in pairs - return open_ep_stream_pair(p_cdc, desc_ep); -} - -static bool cp210x_set_request(cdch_interface_t* p_cdc, uint8_t command, uint16_t value, uint8_t* buffer, uint16_t length, tuh_xfer_cb_t complete_cb, uintptr_t user_data) { - tusb_control_request_t const request = { - .bmRequestType_bit = { - .recipient = TUSB_REQ_RCPT_INTERFACE, - .type = TUSB_REQ_TYPE_VENDOR, - .direction = TUSB_DIR_OUT - }, - .bRequest = command, - .wValue = tu_htole16(value), - .wIndex = p_cdc->bInterfaceNumber, - .wLength = tu_htole16(length) - }; - - // use usbh enum buf since application variable does not live long enough - uint8_t* enum_buf = NULL; - - if (buffer && length > 0) { - enum_buf = usbh_get_enum_buf(); - tu_memcpy_s(enum_buf, CFG_TUH_ENUMERATION_BUFSIZE, buffer, length); - } - - tuh_xfer_t xfer = { - .daddr = p_cdc->daddr, - .ep_addr = 0, - .setup = &request, - .buffer = enum_buf, - .complete_cb = complete_cb, - .user_data = user_data - }; - - return tuh_control_xfer(&xfer); -} - -static bool cp210x_ifc_enable(cdch_interface_t* p_cdc, uint16_t enabled, tuh_xfer_cb_t complete_cb, uintptr_t user_data) { - return cp210x_set_request(p_cdc, CP210X_IFC_ENABLE, enabled, NULL, 0, complete_cb, user_data); -} - -static bool cp210x_set_baudrate(cdch_interface_t* p_cdc, uint32_t baudrate, tuh_xfer_cb_t complete_cb, uintptr_t user_data) { - TU_LOG_DRV("CDC CP210x Set BaudRate = %lu\r\n", baudrate); - uint32_t baud_le = tu_htole32(baudrate); - p_cdc->user_control_cb = complete_cb; - return cp210x_set_request(p_cdc, CP210X_SET_BAUDRATE, 0, (uint8_t *) &baud_le, 4, - complete_cb ? cdch_internal_control_complete : NULL, user_data); -} - -static bool cp210x_set_modem_ctrl(cdch_interface_t* p_cdc, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ - TU_LOG_DRV("CDC CP210x Set Control Line State\r\n"); - p_cdc->user_control_cb = complete_cb; - return cp210x_set_request(p_cdc, CP210X_SET_MHS, 0x0300 | line_state, NULL, 0, - complete_cb ? cdch_internal_control_complete : NULL, user_data); -} - -static void cp210x_process_config(tuh_xfer_t* xfer) { - uintptr_t const state = xfer->user_data; - uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex); - uint8_t const idx = tuh_cdc_itf_get_index(xfer->daddr, itf_num); - cdch_interface_t *p_cdc = get_itf(idx); - TU_ASSERT(p_cdc,); - - switch (state) { - case CONFIG_CP210X_IFC_ENABLE: - TU_ASSERT(cp210x_ifc_enable(p_cdc, 1, cp210x_process_config, CONFIG_CP210X_SET_BAUDRATE),); - break; - - case CONFIG_CP210X_SET_BAUDRATE: { - #ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM - cdc_line_coding_t line_coding = CFG_TUH_CDC_LINE_CODING_ON_ENUM; - TU_ASSERT(cp210x_set_baudrate(p_cdc, line_coding.bit_rate, cp210x_process_config, CONFIG_CP210X_SET_LINE_CTL),); - break; - #else - TU_ATTR_FALLTHROUGH; - #endif - } - - case CONFIG_CP210X_SET_LINE_CTL: { - #if defined(CFG_TUH_CDC_LINE_CODING_ON_ENUM) && 0 // skip for now - cdc_line_coding_t line_coding = CFG_TUH_CDC_LINE_CODING_ON_ENUM; - break; - #else - TU_ATTR_FALLTHROUGH; - #endif - } - - case CONFIG_CP210X_SET_DTR_RTS: - #if CFG_TUH_CDC_LINE_CONTROL_ON_ENUM - TU_ASSERT( - cp210x_set_modem_ctrl(p_cdc, CFG_TUH_CDC_LINE_CONTROL_ON_ENUM, cp210x_process_config, CONFIG_CP210X_COMPLETE),); - break; - #else - TU_ATTR_FALLTHROUGH; - #endif - - case CONFIG_CP210X_COMPLETE: - set_config_complete(p_cdc, idx, itf_num); - break; - - default: break; - } -} - -#endif - -#endif diff --git a/pico-sdk/lib/tinyusb/src/class/cdc/cdc_host.h b/pico-sdk/lib/tinyusb/src/class/cdc/cdc_host.h deleted file mode 100644 index 9e5edd9..0000000 --- a/pico-sdk/lib/tinyusb/src/class/cdc/cdc_host.h +++ /dev/null @@ -1,204 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach (tinyusb.org) - * - * 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. - * - * This file is part of the TinyUSB stack. - */ - -#ifndef _TUSB_CDC_HOST_H_ -#define _TUSB_CDC_HOST_H_ - -#include "cdc.h" - -#ifdef __cplusplus - extern "C" { -#endif - -//--------------------------------------------------------------------+ -// Class Driver Configuration -//--------------------------------------------------------------------+ - -// Set Line Control state on enumeration/mounted: DTR ( bit 0), RTS (bit 1) -#ifndef CFG_TUH_CDC_LINE_CONTROL_ON_ENUM -#define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM 0 -#endif - -// Set Line Coding on enumeration/mounted, value for cdc_line_coding_t -//#ifndef CFG_TUH_CDC_LINE_CODING_ON_ENUM -//#define CFG_TUH_CDC_LINE_CODING_ON_ENUM { 115200, CDC_LINE_CODING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 } -//#endif - -// RX FIFO size -#ifndef CFG_TUH_CDC_RX_BUFSIZE -#define CFG_TUH_CDC_RX_BUFSIZE USBH_EPSIZE_BULK_MAX -#endif - -// RX Endpoint size -#ifndef CFG_TUH_CDC_RX_EPSIZE -#define CFG_TUH_CDC_RX_EPSIZE USBH_EPSIZE_BULK_MAX -#endif - -// TX FIFO size -#ifndef CFG_TUH_CDC_TX_BUFSIZE -#define CFG_TUH_CDC_TX_BUFSIZE USBH_EPSIZE_BULK_MAX -#endif - -// TX Endpoint size -#ifndef CFG_TUH_CDC_TX_EPSIZE -#define CFG_TUH_CDC_TX_EPSIZE USBH_EPSIZE_BULK_MAX -#endif - -//--------------------------------------------------------------------+ -// Application API -//--------------------------------------------------------------------+ - -// Get Interface index from device address + interface number -// return TUSB_INDEX_INVALID_8 (0xFF) if not found -uint8_t tuh_cdc_itf_get_index(uint8_t daddr, uint8_t itf_num); - -// Get Interface information -// return true if index is correct and interface is currently mounted -bool tuh_cdc_itf_get_info(uint8_t idx, tuh_itf_info_t* info); - -// Check if a interface is mounted -bool tuh_cdc_mounted(uint8_t idx); - -// Get current DTR status -bool tuh_cdc_get_dtr(uint8_t idx); - -// Get current RTS status -bool tuh_cdc_get_rts(uint8_t idx); - -// Check if interface is connected (DTR active) -TU_ATTR_ALWAYS_INLINE static inline bool tuh_cdc_connected(uint8_t idx) -{ - return tuh_cdc_get_dtr(idx); -} - -// Get local (saved/cached) version of line coding. -// This function should return correct values if tuh_cdc_set_line_coding() / tuh_cdc_get_line_coding() -// are invoked previously or CFG_TUH_CDC_LINE_CODING_ON_ENUM is defined. -// NOTE: This function does not make any USB transfer request to device. -bool tuh_cdc_get_local_line_coding(uint8_t idx, cdc_line_coding_t* line_coding); - -//--------------------------------------------------------------------+ -// Write API -//--------------------------------------------------------------------+ - -// Get the number of bytes available for writing -uint32_t tuh_cdc_write_available(uint8_t idx); - -// Write to cdc interface -uint32_t tuh_cdc_write(uint8_t idx, void const* buffer, uint32_t bufsize); - -// Force sending data if possible, return number of forced bytes -uint32_t tuh_cdc_write_flush(uint8_t idx); - -// Clear the transmit FIFO -bool tuh_cdc_write_clear(uint8_t idx); - -//--------------------------------------------------------------------+ -// Read API -//--------------------------------------------------------------------+ - -// Get the number of bytes available for reading -uint32_t tuh_cdc_read_available(uint8_t idx); - -// Read from cdc interface -uint32_t tuh_cdc_read (uint8_t idx, void* buffer, uint32_t bufsize); - -// Get a byte from RX FIFO without removing it -bool tuh_cdc_peek(uint8_t idx, uint8_t* ch); - -// Clear the received FIFO -bool tuh_cdc_read_clear (uint8_t idx); - -//--------------------------------------------------------------------+ -// Control Endpoint (Request) API -// Each Function will make a USB control transfer request to/from device -// - If complete_cb is provided, the function will return immediately and invoke -// the callback when request is complete. -// - If complete_cb is NULL, the function will block until request is complete. -// - In this case, user_data should be pointed to xfer_result_t to hold the transfer result. -// - The function will return true if transfer is successful, false otherwise. -//--------------------------------------------------------------------+ - -// Request to Set Control Line State: DTR (bit 0), RTS (bit 1) -bool tuh_cdc_set_control_line_state(uint8_t idx, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data); - -// Request to set baudrate -bool tuh_cdc_set_baudrate(uint8_t idx, uint32_t baudrate, tuh_xfer_cb_t complete_cb, uintptr_t user_data); - -// Request to Set Line Coding (ACM only) -// Should only use if you don't work with serial devices such as FTDI/CP210x -bool tuh_cdc_set_line_coding(uint8_t idx, cdc_line_coding_t const* line_coding, tuh_xfer_cb_t complete_cb, uintptr_t user_data); - -// Request to Get Line Coding (ACM only) -// Should only use if tuh_cdc_set_line_coding() / tuh_cdc_get_line_coding() never got invoked and -// CFG_TUH_CDC_LINE_CODING_ON_ENUM is not defined -// bool tuh_cdc_get_line_coding(uint8_t idx, cdc_line_coding_t* coding); - -// Connect by set both DTR, RTS -TU_ATTR_ALWAYS_INLINE static inline -bool tuh_cdc_connect(uint8_t idx, tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ - return tuh_cdc_set_control_line_state(idx, CDC_CONTROL_LINE_STATE_DTR | CDC_CONTROL_LINE_STATE_RTS, complete_cb, user_data); -} - -// Disconnect by clear both DTR, RTS -TU_ATTR_ALWAYS_INLINE static inline -bool tuh_cdc_disconnect(uint8_t idx, tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ - return tuh_cdc_set_control_line_state(idx, 0x00, complete_cb, user_data); -} - -//--------------------------------------------------------------------+ -// CDC APPLICATION CALLBACKS -//--------------------------------------------------------------------+ - -// Invoked when a device with CDC interface is mounted -// idx is index of cdc interface in the internal pool. -TU_ATTR_WEAK extern void tuh_cdc_mount_cb(uint8_t idx); - -// Invoked when a device with CDC interface is unmounted -TU_ATTR_WEAK extern void tuh_cdc_umount_cb(uint8_t idx); - -// Invoked when received new data -TU_ATTR_WEAK extern void tuh_cdc_rx_cb(uint8_t idx); - -// Invoked when a TX is complete and therefore space becomes available in TX buffer -TU_ATTR_WEAK extern void tuh_cdc_tx_complete_cb(uint8_t idx); - -//--------------------------------------------------------------------+ -// Internal Class Driver API -//--------------------------------------------------------------------+ -void cdch_init (void); -bool cdch_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len); -bool cdch_set_config (uint8_t dev_addr, uint8_t itf_num); -bool cdch_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); -void cdch_close (uint8_t dev_addr); - -#ifdef __cplusplus - } -#endif - -#endif /* _TUSB_CDC_HOST_H_ */ diff --git a/pico-sdk/lib/tinyusb/src/class/cdc/cdc_rndis.h b/pico-sdk/lib/tinyusb/src/class/cdc/cdc_rndis.h deleted file mode 100644 index ad153e0..0000000 --- a/pico-sdk/lib/tinyusb/src/class/cdc/cdc_rndis.h +++ /dev/null @@ -1,301 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach (tinyusb.org) - * - * 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. - * - * This file is part of the TinyUSB stack. - */ - -/** \ingroup ClassDriver_CDC Communication Device Class (CDC) - * \defgroup CDC_RNDIS Remote Network Driver Interface Specification (RNDIS) - * @{ - * \defgroup CDC_RNDIS_Common Common Definitions - * @{ */ - -#ifndef _TUSB_CDC_RNDIS_H_ -#define _TUSB_CDC_RNDIS_H_ - -#include "cdc.h" - -#ifdef __cplusplus - extern "C" { -#endif - -#ifdef __CC_ARM -#pragma diag_suppress 66 // Suppress Keil warnings #66-D: enumeration value is out of "int" range -#endif - -/// RNDIS Message Types -typedef enum -{ - RNDIS_MSG_PACKET = 0x00000001UL, ///< The host and device use this to send network data to one another. - - RNDIS_MSG_INITIALIZE = 0x00000002UL, ///< Sent by the host to initialize the device. - RNDIS_MSG_INITIALIZE_CMPLT = 0x80000002UL, ///< Device response to an initialize message. - - RNDIS_MSG_HALT = 0x00000003UL, ///< Sent by the host to halt the device. This does not have a response. It is optional for the device to send this message to the host. - - RNDIS_MSG_QUERY = 0x00000004UL, ///< Sent by the host to send a query OID. - RNDIS_MSG_QUERY_CMPLT = 0x80000004UL, ///< Device response to a query OID. - - RNDIS_MSG_SET = 0x00000005UL, ///< Sent by the host to send a set OID. - RNDIS_MSG_SET_CMPLT = 0x80000005UL, ///< Device response to a set OID. - - RNDIS_MSG_RESET = 0x00000006UL, ///< Sent by the host to perform a soft reset on the device. - RNDIS_MSG_RESET_CMPLT = 0x80000006UL, ///< Device response to reset message. - - RNDIS_MSG_INDICATE_STATUS = 0x00000007UL, ///< Sent by the device to indicate its status or an error when an unrecognized message is received. - - RNDIS_MSG_KEEP_ALIVE = 0x00000008UL, ///< During idle periods, sent every few seconds by the host to check that the device is still responsive. It is optional for the device to send this message to check if the host is active. - RNDIS_MSG_KEEP_ALIVE_CMPLT = 0x80000008UL ///< The device response to a keepalivemessage. The host can respond with this message to a keepalive message from the device when the device implements the optional KeepAliveTimer. -}rndis_msg_type_t; - -/// RNDIS Message Status Values -typedef enum -{ - RNDIS_STATUS_SUCCESS = 0x00000000UL, ///< Success - RNDIS_STATUS_FAILURE = 0xC0000001UL, ///< Unspecified error - RNDIS_STATUS_INVALID_DATA = 0xC0010015UL, ///< Invalid data error - RNDIS_STATUS_NOT_SUPPORTED = 0xC00000BBUL, ///< Unsupported request error - RNDIS_STATUS_MEDIA_CONNECT = 0x4001000BUL, ///< Device is connected to a network medium. - RNDIS_STATUS_MEDIA_DISCONNECT = 0x4001000CUL ///< Device is disconnected from the medium. -}rndis_msg_status_t; - -#ifdef __CC_ARM -#pragma diag_default 66 // return Keil 66 to normal severity -#endif - -//--------------------------------------------------------------------+ -// MESSAGE STRUCTURE -//--------------------------------------------------------------------+ - -//------------- Initialize -------------// -/// \brief Initialize Message -/// \details This message MUST be sent by the host to initialize the device. -typedef struct { - uint32_t type ; ///< Message type, must be \ref RNDIS_MSG_INITIALIZE - uint32_t length ; ///< Message length in bytes, must be 0x18 - uint32_t request_id ; ///< A 32-bit integer value, generated by the host, used to match the host's sent request to the response from the device. - uint32_t major_version ; ///< The major version of the RNDIS Protocol implemented by the host. - uint32_t minor_version ; ///< The minor version of the RNDIS Protocol implemented by the host - uint32_t max_xfer_size ; ///< The maximum size, in bytes, of any single bus data transfer that the host expects to receive from the device. -}rndis_msg_initialize_t; - -/// \brief Initialize Complete Message -/// \details This message MUST be sent by the device in response to an initialize message. -typedef struct { - uint32_t type ; ///< Message Type, must be \ref RNDIS_MSG_INITIALIZE_CMPLT - uint32_t length ; ///< Message length in bytes, must be 0x30 - uint32_t request_id ; ///< A 32-bit integer value from \a request_id field of the \ref rndis_msg_initialize_t to which this message is a response. - uint32_t status ; ///< The initialization status of the device, has value from \ref rndis_msg_status_t - uint32_t major_version ; ///< the highest-numbered RNDIS Protocol version supported by the device. - uint32_t minor_version ; ///< the highest-numbered RNDIS Protocol version supported by the device. - uint32_t device_flags ; ///< MUST be set to 0x000000010. Other values are reserved for future use. - uint32_t medium ; ///< is 0x00 for RNDIS_MEDIUM_802_3 - uint32_t max_packet_per_xfer ; ///< The maximum number of concatenated \ref RNDIS_MSG_PACKET messages that the device can handle in a single bus transfer to it. This value MUST be at least 1. - uint32_t max_xfer_size ; ///< The maximum size, in bytes, of any single bus data transfer that the device expects to receive from the host. - uint32_t packet_alignment_factor ; ///< The byte alignment the device expects for each RNDIS message that is part of a multimessage transfer to it. The value is specified as an exponent of 2; for example, the host uses 2{PacketAlignmentFactor} as the alignment value. - uint32_t reserved[2] ; -} rndis_msg_initialize_cmplt_t; - -//------------- Query -------------// -/// \brief Query Message -/// \details This message MUST be sent by the host to query an OID. -typedef struct { - uint32_t type ; ///< Message Type, must be \ref RNDIS_MSG_QUERY - uint32_t length ; ///< Message length in bytes, including the header and the \a oid_buffer - uint32_t request_id ; ///< A 32-bit integer value, generated by the host, used to match the host's sent request to the response from the device. - uint32_t oid ; ///< The integer value of the host operating system-defined identifier, for the parameter of the device being queried for. - uint32_t buffer_length ; ///< The length, in bytes, of the input data required for the OID query. This MUST be set to 0 when there is no input data associated with the OID. - uint32_t buffer_offset ; ///< The offset, in bytes, from the beginning of \a request_id field where the input data for the query is located in the message. This value MUST be set to 0 when there is no input data associated with the OID. - uint32_t reserved ; - uint8_t oid_buffer[] ; ///< Flexible array contains the input data supplied by the host, required for the OID query request processing by the device, as per the host NDIS specification. -} rndis_msg_query_t, rndis_msg_set_t; - -TU_VERIFY_STATIC(sizeof(rndis_msg_query_t) == 28, "Make sure flexible array member does not affect layout"); - -/// \brief Query Complete Message -/// \details This message MUST be sent by the device in response to a query OID message. -typedef struct { - uint32_t type ; ///< Message Type, must be \ref RNDIS_MSG_QUERY_CMPLT - uint32_t length ; ///< Message length in bytes, including the header and the \a oid_buffer - uint32_t request_id ; ///< A 32-bit integer value from \a request_id field of the \ref rndis_msg_query_t to which this message is a response. - uint32_t status ; ///< The status of processing for the query request, has value from \ref rndis_msg_status_t. - uint32_t buffer_length ; ///< The length, in bytes, of the data in the response to the query. This MUST be set to 0 when there is no OIDInputBuffer. - uint32_t buffer_offset ; ///< The offset, in bytes, from the beginning of \a request_id field where the response data for the query is located in the message. This MUST be set to 0 when there is no \ref oid_buffer. - uint8_t oid_buffer[] ; ///< Flexible array member contains the response data to the OID query request as specified by the host. -} rndis_msg_query_cmplt_t; - -TU_VERIFY_STATIC(sizeof(rndis_msg_query_cmplt_t) == 24, "Make sure flexible array member does not affect layout"); - -//------------- Reset -------------// -/// \brief Reset Message -/// \details This message MUST be sent by the host to perform a soft reset on the device. -typedef struct { - uint32_t type ; ///< Message Type, must be \ref RNDIS_MSG_RESET - uint32_t length ; ///< Message length in bytes, MUST be 0x06 - uint32_t reserved ; -} rndis_msg_reset_t; - -/// \brief Reset Complete Message -/// \details This message MUST be sent by the device in response to a reset message. -typedef struct { - uint32_t type ; ///< Message Type, must be \ref RNDIS_MSG_RESET_CMPLT - uint32_t length ; ///< Message length in bytes, MUST be 0x10 - uint32_t status ; ///< The status of processing for the \ref rndis_msg_reset_t, has value from \ref rndis_msg_status_t. - uint32_t addressing_reset ; ///< This field indicates whether the addressing information, which is the multicast address list or packet filter, has been lost during the reset operation. This MUST be set to 0x00000001 if the device requires that the host to resend addressing information or MUST be set to zero otherwise. -} rndis_msg_reset_cmplt_t; - -//typedef struct { -// uint32_t type; -// uint32_t length; -// uint32_t status; -// uint32_t buffer_length; -// uint32_t buffer_offset; -// uint32_t diagnostic_status; // optional -// uint32_t diagnostic_error_offset; // optional -// uint32_t status_buffer[0]; // optional -//} rndis_msg_indicate_status_t; - -/// \brief Keep Alive Message -/// \details This message MUST be sent by the host to check that device is still responsive. It is optional for the device to send this message to check if the host is active -typedef struct { - uint32_t type ; ///< Message Type - uint32_t length ; ///< Message length in bytes, MUST be 0x10 - uint32_t request_id ; -} rndis_msg_keep_alive_t, rndis_msg_halt_t; - -/// \brief Set Complete Message -/// \brief This message MUST be sent in response to a the request message -typedef struct { - uint32_t type ; ///< Message Type - uint32_t length ; ///< Message length in bytes, MUST be 0x10 - uint32_t request_id ; ///< must be the same as requesting message - uint32_t status ; ///< The status of processing for the request message request by the device to which this message is the response. -} rndis_msg_set_cmplt_t, rndis_msg_keep_alive_cmplt_t; - -/// \brief Packet Data Message -/// \brief This message MUST be used by the host and the device to send network data to one another. -typedef struct { - uint32_t type ; ///< Message Type, must be \ref RNDIS_MSG_PACKET - uint32_t length ; ///< Message length in bytes, The total length of this RNDIS message including the header, payload, and padding. - uint32_t data_offset ; ///< Specifies the offset, in bytes, from the start of this \a data_offset field of this message to the start of the data. This MUST be an integer multiple of 4. - uint32_t data_length ; ///< Specifies the number of bytes in the payload of this message. - uint32_t out_of_band_data_offet ; ///< Specifies the offset, in bytes, of the first out-of-band data record from the start of the DataOffset field in this message. MUST be an integer multiple of 4 when out-of-band data is present or set to 0 otherwise. When there are multiple out-ofband data records, each subsequent record MUST immediately follow the previous out-of-band data record. - uint32_t out_of_band_data_length ; ///< Specifies, in bytes, the total length of the out-of-band data. - uint32_t num_out_of_band_data_elements ; ///< Specifies the number of out-of-band records in this message. - uint32_t per_packet_info_offset ; ///< Specifies the offset, in bytes, of the start of per-packet-info data record from the start of the \a data_offset field in this message. MUST be an integer multiple of 4 when per-packet-info data record is present or MUST be set to 0 otherwise. When there are multiple per-packet-info data records, each subsequent record MUST immediately follow the previous record. - uint32_t per_packet_info_length ; ///< Specifies, in bytes, the total length of per-packetinformation contained in this message. - uint32_t reserved[2] ; - uint32_t payload[0] ; ///< Network data contained in this message. - - // uint8_t padding[0] - // Additional bytes of zeros added at the end of the message to comply with - // the internal and external padding requirements. Internal padding SHOULD be as per the - // specification of the out-of-band data record and per-packet-info data record. The external - //padding size SHOULD be determined based on the PacketAlignmentFactor field specification - //in REMOTE_NDIS_INITIALIZE_CMPLT message by the device, when multiple - //REMOTE_NDIS_PACKET_MSG messages are bundled together in a single bus-native message. - //In this case, all but the very last REMOTE_NDIS_PACKET_MSG MUST respect the - //PacketAlignmentFactor field. - - // rndis_msg_packet_t [0] : (optional) more packet if multiple packet per bus transaction is supported -} rndis_msg_packet_t; - - -typedef struct { - uint32_t size ; ///< Length, in bytes, of this header and appended data and padding. This value MUST be an integer multiple of 4. - uint32_t type ; ///< MUST be as per host operating system specification. - uint32_t offset ; ///< The byte offset from the beginning of this record to the beginning of data. - uint32_t data[0] ; ///< Flexible array contains data -} rndis_msg_out_of_band_data_t, rndis_msg_per_packet_info_t; - -//--------------------------------------------------------------------+ -// NDIS Object ID -//--------------------------------------------------------------------+ - -/// NDIS Object ID -typedef enum -{ - //------------- General Required OIDs -------------// - RNDIS_OID_GEN_SUPPORTED_LIST = 0x00010101, ///< List of supported OIDs - RNDIS_OID_GEN_HARDWARE_STATUS = 0x00010102, ///< Hardware status - RNDIS_OID_GEN_MEDIA_SUPPORTED = 0x00010103, ///< Media types supported (encoded) - RNDIS_OID_GEN_MEDIA_IN_USE = 0x00010104, ///< Media types in use (encoded) - RNDIS_OID_GEN_MAXIMUM_LOOKAHEAD = 0x00010105, ///< - RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE = 0x00010106, ///< Maximum frame size in bytes - RNDIS_OID_GEN_LINK_SPEED = 0x00010107, ///< Link speed in units of 100 bps - RNDIS_OID_GEN_TRANSMIT_BUFFER_SPACE = 0x00010108, ///< Transmit buffer space - RNDIS_OID_GEN_RECEIVE_BUFFER_SPACE = 0x00010109, ///< Receive buffer space - RNDIS_OID_GEN_TRANSMIT_BLOCK_SIZE = 0x0001010A, ///< Minimum amount of storage, in bytes, that a single packet occupies in the transmit buffer space of the NIC - RNDIS_OID_GEN_RECEIVE_BLOCK_SIZE = 0x0001010B, ///< Amount of storage, in bytes, that a single packet occupies in the receive buffer space of the NIC - RNDIS_OID_GEN_VENDOR_ID = 0x0001010C, ///< Vendor NIC code - RNDIS_OID_GEN_VENDOR_DESCRIPTION = 0x0001010D, ///< Vendor network card description - RNDIS_OID_GEN_CURRENT_PACKET_FILTER = 0x0001010E, ///< Current packet filter (encoded) - RNDIS_OID_GEN_CURRENT_LOOKAHEAD = 0x0001010F, ///< Current lookahead size in bytes - RNDIS_OID_GEN_DRIVER_VERSION = 0x00010110, ///< NDIS version number used by the driver - RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE = 0x00010111, ///< Maximum total packet length in bytes - RNDIS_OID_GEN_PROTOCOL_OPTIONS = 0x00010112, ///< Optional protocol flags (encoded) - RNDIS_OID_GEN_MAC_OPTIONS = 0x00010113, ///< Optional NIC flags (encoded) - RNDIS_OID_GEN_MEDIA_CONNECT_STATUS = 0x00010114, ///< Whether the NIC is connected to the network - RNDIS_OID_GEN_MAXIMUM_SEND_PACKETS = 0x00010115, ///< The maximum number of send packets the driver can accept per call to its MiniportSendPacketsfunction - - //------------- General Optional OIDs -------------// - RNDIS_OID_GEN_VENDOR_DRIVER_VERSION = 0x00010116, ///< Vendor-assigned version number of the driver - RNDIS_OID_GEN_SUPPORTED_GUIDS = 0x00010117, ///< The custom GUIDs (Globally Unique Identifier) supported by the miniport driver - RNDIS_OID_GEN_NETWORK_LAYER_ADDRESSES = 0x00010118, ///< List of network-layer addresses associated with the binding between a transport and the driver - RNDIS_OID_GEN_TRANSPORT_HEADER_OFFSET = 0x00010119, ///< Size of packets' additional headers - RNDIS_OID_GEN_MEDIA_CAPABILITIES = 0x00010201, ///< - RNDIS_OID_GEN_PHYSICAL_MEDIUM = 0x00010202, ///< Physical media supported by the miniport driver (encoded) - - //------------- 802.3 Objects (Ethernet) -------------// - RNDIS_OID_802_3_PERMANENT_ADDRESS = 0x01010101, ///< Permanent station address - RNDIS_OID_802_3_CURRENT_ADDRESS = 0x01010102, ///< Current station address - RNDIS_OID_802_3_MULTICAST_LIST = 0x01010103, ///< Current multicast address list - RNDIS_OID_802_3_MAXIMUM_LIST_SIZE = 0x01010104, ///< Maximum size of multicast address list -} rndis_oid_type_t; - -/// RNDIS Packet Filter Bits \ref RNDIS_OID_GEN_CURRENT_PACKET_FILTER. -typedef enum -{ - RNDIS_PACKET_TYPE_DIRECTED = 0x00000001, ///< Directed packets. Directed packets contain a destination address equal to the station address of the NIC. - RNDIS_PACKET_TYPE_MULTICAST = 0x00000002, ///< Multicast address packets sent to addresses in the multicast address list. - RNDIS_PACKET_TYPE_ALL_MULTICAST = 0x00000004, ///< All multicast address packets, not just the ones enumerated in the multicast address list. - RNDIS_PACKET_TYPE_BROADCAST = 0x00000008, ///< Broadcast packets. - RNDIS_PACKET_TYPE_SOURCE_ROUTING = 0x00000010, ///< All source routing packets. If the protocol driver sets this bit, the NDIS library attempts to act as a source routing bridge. - RNDIS_PACKET_TYPE_PROMISCUOUS = 0x00000020, ///< Specifies all packets regardless of whether VLAN filtering is enabled or not and whether the VLAN identifier matches or not. - RNDIS_PACKET_TYPE_SMT = 0x00000040, ///< SMT packets that an FDDI NIC receives. - RNDIS_PACKET_TYPE_ALL_LOCAL = 0x00000080, ///< All packets sent by installed protocols and all packets indicated by the NIC that is identified by a given NdisBindingHandle. - RNDIS_PACKET_TYPE_GROUP = 0x00001000, ///< Packets sent to the current group address. - RNDIS_PACKET_TYPE_ALL_FUNCTIONAL = 0x00002000, ///< All functional address packets, not just the ones in the current functional address. - RNDIS_PACKET_TYPE_FUNCTIONAL = 0x00004000, ///< Functional address packets sent to addresses included in the current functional address. - RNDIS_PACKET_TYPE_MAC_FRAME = 0x00008000, ///< NIC driver frames that a Token Ring NIC receives. - RNDIS_PACKET_TYPE_NO_LOCAL = 0x00010000, -} rndis_packet_filter_type_t; - -#ifdef __cplusplus - } -#endif - -#endif /* _TUSB_CDC_RNDIS_H_ */ - -/** @} */ -/** @} */ diff --git a/pico-sdk/lib/tinyusb/src/class/cdc/cdc_rndis_host.c b/pico-sdk/lib/tinyusb/src/class/cdc/cdc_rndis_host.c deleted file mode 100644 index 11a5355..0000000 --- a/pico-sdk/lib/tinyusb/src/class/cdc/cdc_rndis_host.c +++ /dev/null @@ -1,289 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach (tinyusb.org) - * - * 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. - * - * This file is part of the TinyUSB stack. - */ - -#include "tusb_option.h" - -#if (CFG_TUH_ENABLED && CFG_TUH_CDC && CFG_TUH_CDC_RNDIS) - -//--------------------------------------------------------------------+ -// INCLUDE -//--------------------------------------------------------------------+ -#include "common/tusb_common.h" -#include "cdc_host.h" -#include "cdc_rndis_host.h" - -#if 0 // TODO remove subtask related macros later -// Sub Task -#define OSAL_SUBTASK_BEGIN -#define OSAL_SUBTASK_END return TUSB_ERROR_NONE; - -#define STASK_RETURN(_error) return _error; -#define STASK_INVOKE(_subtask, _status) (_status) = _subtask -#define STASK_ASSERT(_cond) TU_VERIFY(_cond, TUSB_ERROR_OSAL_TASK_FAILED) -#endif - -//--------------------------------------------------------------------+ -// MACRO CONSTANT TYPEDEF -//--------------------------------------------------------------------+ -#define RNDIS_MSG_PAYLOAD_MAX (1024*4) - -CFG_TUH_MEM_SECTION static uint8_t msg_notification[CFG_TUH_DEVICE_MAX][8]; -CFG_TUH_MEM_SECTION CFG_TUH_MEM_ALIGN static uint8_t msg_payload[RNDIS_MSG_PAYLOAD_MAX]; - -static rndish_data_t rndish_data[CFG_TUH_DEVICE_MAX]; - -// TODO Microsoft requires message length for any get command must be at least 4096 bytes - -//--------------------------------------------------------------------+ -// INTERNAL OBJECT & FUNCTION DECLARATION -//--------------------------------------------------------------------+ -static tusb_error_t rndis_body_subtask(void); -static tusb_error_t send_message_get_response_subtask( uint8_t dev_addr, cdch_data_t *p_cdc, - uint8_t * p_mess, uint32_t mess_length, - uint8_t *p_response ); - -//--------------------------------------------------------------------+ -// APPLICATION API -//--------------------------------------------------------------------+ -tusb_error_t tusbh_cdc_rndis_get_mac_addr(uint8_t dev_addr, uint8_t mac_address[6]) -{ - TU_ASSERT( tusbh_cdc_rndis_is_mounted(dev_addr), TUSB_ERROR_CDCH_DEVICE_NOT_MOUNTED); - TU_VERIFY( mac_address, TUSB_ERROR_INVALID_PARA); - - memcpy(mac_address, rndish_data[dev_addr-1].mac_address, 6); - - return TUSB_ERROR_NONE; -} - -//--------------------------------------------------------------------+ -// IMPLEMENTATION -//--------------------------------------------------------------------+ - -// To enable the TASK_ASSERT style (quick return on false condition) in a real RTOS, a task must act as a wrapper -// and is used mainly to call subtasks. Within a subtask return statement can be called freely, the task with -// forever loop cannot have any return at all. -OSAL_TASK_FUNCTION(cdch_rndis_task) (void* param;) -{ - OSAL_TASK_BEGIN - rndis_body_subtask(); - OSAL_TASK_END -} - -static tusb_error_t rndis_body_subtask(void) -{ - static uint8_t relative_addr; - - OSAL_SUBTASK_BEGIN - - for (relative_addr = 0; relative_addr < CFG_TUH_DEVICE_MAX; relative_addr++) - { - - } - - osal_task_delay(100); - - OSAL_SUBTASK_END -} - -//--------------------------------------------------------------------+ -// RNDIS-CDC Driver API -//--------------------------------------------------------------------+ -void rndish_init(void) -{ - tu_memclr(rndish_data, sizeof(rndish_data_t)*CFG_TUH_DEVICE_MAX); - - //------------- Task creation -------------// - - //------------- semaphore creation for notification pipe -------------// - for(uint8_t i=0; itype == RNDIS_MSG_INITIALIZE_CMPLT && p_init_cmpt->status == RNDIS_STATUS_SUCCESS && - p_init_cmpt->max_packet_per_xfer == 1 && p_init_cmpt->max_xfer_size <= RNDIS_MSG_PAYLOAD_MAX); - rndish_data[dev_addr-1].max_xfer_size = p_init_cmpt->max_xfer_size; - - //------------- Message Query 802.3 Permanent Address -------------// - memcpy(msg_payload, &msg_query_permanent_addr, sizeof(rndis_msg_query_t)); - tu_memclr(msg_payload + sizeof(rndis_msg_query_t), 6); // 6 bytes for MAC address - - STASK_INVOKE( - send_message_get_response_subtask( dev_addr, p_cdc, - msg_payload, sizeof(rndis_msg_query_t) + 6, - msg_payload), - error - ); - if ( TUSB_ERROR_NONE != error ) STASK_RETURN(error); - - rndis_msg_query_cmplt_t * const p_query_cmpt = (rndis_msg_query_cmplt_t *) msg_payload; - STASK_ASSERT(p_query_cmpt->type == RNDIS_MSG_QUERY_CMPLT && p_query_cmpt->status == RNDIS_STATUS_SUCCESS); - memcpy(rndish_data[dev_addr-1].mac_address, msg_payload + 8 + p_query_cmpt->buffer_offset, 6); - - //------------- Set OID_GEN_CURRENT_PACKET_FILTER to (DIRECTED | MULTICAST | BROADCAST) -------------// - memcpy(msg_payload, &msg_set_packet_filter, sizeof(rndis_msg_set_t)); - tu_memclr(msg_payload + sizeof(rndis_msg_set_t), 4); // 4 bytes for filter flags - ((rndis_msg_set_t*) msg_payload)->oid_buffer[0] = (RNDIS_PACKET_TYPE_DIRECTED | RNDIS_PACKET_TYPE_MULTICAST | RNDIS_PACKET_TYPE_BROADCAST); - - STASK_INVOKE( - send_message_get_response_subtask( dev_addr, p_cdc, - msg_payload, sizeof(rndis_msg_set_t) + 4, - msg_payload), - error - ); - if ( TUSB_ERROR_NONE != error ) STASK_RETURN(error); - - rndis_msg_set_cmplt_t * const p_set_cmpt = (rndis_msg_set_cmplt_t *) msg_payload; - STASK_ASSERT(p_set_cmpt->type == RNDIS_MSG_SET_CMPLT && p_set_cmpt->status == RNDIS_STATUS_SUCCESS); - - tusbh_cdc_rndis_mounted_cb(dev_addr); - - OSAL_SUBTASK_END -} - -void rndish_xfer_isr(cdch_data_t *p_cdc, pipe_handle_t pipe_hdl, xfer_result_t event, uint32_t xferred_bytes) -{ - if ( pipehandle_is_equal(pipe_hdl, p_cdc->pipe_notification) ) - { - osal_semaphore_post( rndish_data[pipe_hdl.dev_addr-1].sem_notification_hdl ); - } -} - -//--------------------------------------------------------------------+ -// INTERNAL & HELPER -//--------------------------------------------------------------------+ -static tusb_error_t send_message_get_response_subtask( uint8_t dev_addr, cdch_data_t *p_cdc, - uint8_t * p_mess, uint32_t mess_length, - uint8_t *p_response) -{ - tusb_error_t error; - - OSAL_SUBTASK_BEGIN - - //------------- Send RNDIS Control Message -------------// - STASK_INVOKE( - usbh_control_xfer_subtask( dev_addr, bm_request_type(TUSB_DIR_OUT, TUSB_REQ_TYPE_CLASS, TUSB_REQ_RCPT_INTERFACE), - CDC_REQUEST_SEND_ENCAPSULATED_COMMAND, 0, p_cdc->interface_number, - mess_length, p_mess), - error - ); - if ( TUSB_ERROR_NONE != error ) STASK_RETURN(error); - - //------------- waiting for Response Available notification -------------// - (void) usbh_edpt_xfer(p_cdc->pipe_notification, msg_notification[dev_addr-1], 8); - osal_semaphore_wait(rndish_data[dev_addr-1].sem_notification_hdl, OSAL_TIMEOUT_NORMAL, &error); - if ( TUSB_ERROR_NONE != error ) STASK_RETURN(error); - STASK_ASSERT(msg_notification[dev_addr-1][0] == 1); - - //------------- Get RNDIS Message Initialize Complete -------------// - STASK_INVOKE( - usbh_control_xfer_subtask( dev_addr, bm_request_type(TUSB_DIR_IN, TUSB_REQ_TYPE_CLASS, TUSB_REQ_RCPT_INTERFACE), - CDC_REQUEST_GET_ENCAPSULATED_RESPONSE, 0, p_cdc->interface_number, - RNDIS_MSG_PAYLOAD_MAX, p_response), - error - ); - if ( TUSB_ERROR_NONE != error ) STASK_RETURN(error); - - OSAL_SUBTASK_END -} - -//static tusb_error_t send_process_msg_initialize_subtask(uint8_t dev_addr, cdch_data_t *p_cdc) -//{ -// tusb_error_t error; -// -// OSAL_SUBTASK_BEGIN -// -// *((rndis_msg_initialize_t*) msg_payload) = (rndis_msg_initialize_t) -// { -// .type = RNDIS_MSG_INITIALIZE, -// .length = sizeof(rndis_msg_initialize_t), -// .request_id = 1, // TODO should use some magic number -// .major_version = 1, -// .minor_version = 0, -// .max_xfer_size = 0x4000 // TODO mimic windows -// }; -// -// -// -// OSAL_SUBTASK_END -//} -#endif diff --git a/pico-sdk/lib/tinyusb/src/class/cdc/cdc_rndis_host.h b/pico-sdk/lib/tinyusb/src/class/cdc/cdc_rndis_host.h deleted file mode 100644 index bb431ec..0000000 --- a/pico-sdk/lib/tinyusb/src/class/cdc/cdc_rndis_host.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach (tinyusb.org) - * - * 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. - * - * This file is part of the TinyUSB stack. - */ - -/** \ingroup CDC_RNDIS - * \defgroup CDC_RNSID_Host Host - * @{ */ - -#ifndef _TUSB_CDC_RNDIS_HOST_H_ -#define _TUSB_CDC_RNDIS_HOST_H_ - -#include "common/tusb_common.h" -#include "host/usbh.h" -#include "cdc_rndis.h" - -#ifdef __cplusplus - extern "C" { -#endif - -//--------------------------------------------------------------------+ -// INTERNAL RNDIS-CDC Driver API -//--------------------------------------------------------------------+ -typedef struct { - OSAL_SEM_DEF(semaphore_notification); - osal_semaphore_handle_t sem_notification_hdl; // used to wait on notification pipe - uint32_t max_xfer_size; // got from device's msg initialize complete - uint8_t mac_address[6]; -}rndish_data_t; - -void rndish_init(void); -bool rndish_open_subtask(uint8_t dev_addr, cdch_data_t *p_cdc); -void rndish_xfer_isr(cdch_data_t *p_cdc, pipe_handle_t pipe_hdl, xfer_result_t event, uint32_t xferred_bytes); -void rndish_close(uint8_t dev_addr); - -#ifdef __cplusplus - } -#endif - -#endif /* _TUSB_CDC_RNDIS_HOST_H_ */ - -/** @} */ diff --git a/pico-sdk/lib/tinyusb/src/class/hid/.swp b/pico-sdk/lib/tinyusb/src/class/hid/.swp deleted file mode 100644 index 525ad14..0000000 Binary files a/pico-sdk/lib/tinyusb/src/class/hid/.swp and /dev/null differ diff --git a/pico-sdk/lib/tinyusb/src/class/hid/hid.h b/pico-sdk/lib/tinyusb/src/class/hid/hid.h index fbd3eef..2fe9221 100644 --- a/pico-sdk/lib/tinyusb/src/class/hid/hid.h +++ b/pico-sdk/lib/tinyusb/src/class/hid/hid.h @@ -300,6 +300,19 @@ typedef struct TU_ATTR_PACKED int8_t pan; // using AC Pan } hid_mouse_report_t; + +// Absolute Mouse: same as the Standard (relative) Mouse Report but +// with int16_t instead of int8_t for X and Y coordinates. +typedef struct TU_ATTR_PACKED +{ + uint8_t buttons; /**< buttons mask for currently pressed buttons in the mouse. */ + int16_t x; /**< Current x position of the mouse. */ + int16_t y; /**< Current y position of the mouse. */ + int8_t wheel; /**< Current delta wheel movement on the mouse. */ + int8_t pan; // using AC Pan +} hid_abs_mouse_report_t; + + /// Standard Mouse Buttons Bitmap typedef enum { @@ -353,177 +366,224 @@ typedef enum //--------------------------------------------------------------------+ // HID KEYCODE //--------------------------------------------------------------------+ -#define HID_KEY_NONE 0x00 -#define HID_KEY_A 0x04 -#define HID_KEY_B 0x05 -#define HID_KEY_C 0x06 -#define HID_KEY_D 0x07 -#define HID_KEY_E 0x08 -#define HID_KEY_F 0x09 -#define HID_KEY_G 0x0A -#define HID_KEY_H 0x0B -#define HID_KEY_I 0x0C -#define HID_KEY_J 0x0D -#define HID_KEY_K 0x0E -#define HID_KEY_L 0x0F -#define HID_KEY_M 0x10 -#define HID_KEY_N 0x11 -#define HID_KEY_O 0x12 -#define HID_KEY_P 0x13 -#define HID_KEY_Q 0x14 -#define HID_KEY_R 0x15 -#define HID_KEY_S 0x16 -#define HID_KEY_T 0x17 -#define HID_KEY_U 0x18 -#define HID_KEY_V 0x19 -#define HID_KEY_W 0x1A -#define HID_KEY_X 0x1B -#define HID_KEY_Y 0x1C -#define HID_KEY_Z 0x1D -#define HID_KEY_1 0x1E -#define HID_KEY_2 0x1F -#define HID_KEY_3 0x20 -#define HID_KEY_4 0x21 -#define HID_KEY_5 0x22 -#define HID_KEY_6 0x23 -#define HID_KEY_7 0x24 -#define HID_KEY_8 0x25 -#define HID_KEY_9 0x26 -#define HID_KEY_0 0x27 -#define HID_KEY_ENTER 0x28 -#define HID_KEY_ESCAPE 0x29 -#define HID_KEY_BACKSPACE 0x2A -#define HID_KEY_TAB 0x2B -#define HID_KEY_SPACE 0x2C -#define HID_KEY_MINUS 0x2D -#define HID_KEY_EQUAL 0x2E -#define HID_KEY_BRACKET_LEFT 0x2F -#define HID_KEY_BRACKET_RIGHT 0x30 -#define HID_KEY_BACKSLASH 0x31 -#define HID_KEY_EUROPE_1 0x32 -#define HID_KEY_SEMICOLON 0x33 -#define HID_KEY_APOSTROPHE 0x34 -#define HID_KEY_GRAVE 0x35 -#define HID_KEY_COMMA 0x36 -#define HID_KEY_PERIOD 0x37 -#define HID_KEY_SLASH 0x38 -#define HID_KEY_CAPS_LOCK 0x39 -#define HID_KEY_F1 0x3A -#define HID_KEY_F2 0x3B -#define HID_KEY_F3 0x3C -#define HID_KEY_F4 0x3D -#define HID_KEY_F5 0x3E -#define HID_KEY_F6 0x3F -#define HID_KEY_F7 0x40 -#define HID_KEY_F8 0x41 -#define HID_KEY_F9 0x42 -#define HID_KEY_F10 0x43 -#define HID_KEY_F11 0x44 -#define HID_KEY_F12 0x45 -#define HID_KEY_PRINT_SCREEN 0x46 -#define HID_KEY_SCROLL_LOCK 0x47 -#define HID_KEY_PAUSE 0x48 -#define HID_KEY_INSERT 0x49 -#define HID_KEY_HOME 0x4A -#define HID_KEY_PAGE_UP 0x4B -#define HID_KEY_DELETE 0x4C -#define HID_KEY_END 0x4D -#define HID_KEY_PAGE_DOWN 0x4E -#define HID_KEY_ARROW_RIGHT 0x4F -#define HID_KEY_ARROW_LEFT 0x50 -#define HID_KEY_ARROW_DOWN 0x51 -#define HID_KEY_ARROW_UP 0x52 -#define HID_KEY_NUM_LOCK 0x53 -#define HID_KEY_KEYPAD_DIVIDE 0x54 -#define HID_KEY_KEYPAD_MULTIPLY 0x55 -#define HID_KEY_KEYPAD_SUBTRACT 0x56 -#define HID_KEY_KEYPAD_ADD 0x57 -#define HID_KEY_KEYPAD_ENTER 0x58 -#define HID_KEY_KEYPAD_1 0x59 -#define HID_KEY_KEYPAD_2 0x5A -#define HID_KEY_KEYPAD_3 0x5B -#define HID_KEY_KEYPAD_4 0x5C -#define HID_KEY_KEYPAD_5 0x5D -#define HID_KEY_KEYPAD_6 0x5E -#define HID_KEY_KEYPAD_7 0x5F -#define HID_KEY_KEYPAD_8 0x60 -#define HID_KEY_KEYPAD_9 0x61 -#define HID_KEY_KEYPAD_0 0x62 -#define HID_KEY_KEYPAD_DECIMAL 0x63 -#define HID_KEY_EUROPE_2 0x64 -#define HID_KEY_APPLICATION 0x65 -#define HID_KEY_POWER 0x66 -#define HID_KEY_KEYPAD_EQUAL 0x67 -#define HID_KEY_F13 0x68 -#define HID_KEY_F14 0x69 -#define HID_KEY_F15 0x6A -#define HID_KEY_F16 0x6B -#define HID_KEY_F17 0x6C -#define HID_KEY_F18 0x6D -#define HID_KEY_F19 0x6E -#define HID_KEY_F20 0x6F -#define HID_KEY_F21 0x70 -#define HID_KEY_F22 0x71 -#define HID_KEY_F23 0x72 -#define HID_KEY_F24 0x73 -#define HID_KEY_EXECUTE 0x74 -#define HID_KEY_HELP 0x75 -#define HID_KEY_MENU 0x76 -#define HID_KEY_SELECT 0x77 -#define HID_KEY_STOP 0x78 -#define HID_KEY_AGAIN 0x79 -#define HID_KEY_UNDO 0x7A -#define HID_KEY_CUT 0x7B -#define HID_KEY_COPY 0x7C -#define HID_KEY_PASTE 0x7D -#define HID_KEY_FIND 0x7E -#define HID_KEY_MUTE 0x7F -#define HID_KEY_VOLUME_UP 0x80 -#define HID_KEY_VOLUME_DOWN 0x81 -#define HID_KEY_LOCKING_CAPS_LOCK 0x82 -#define HID_KEY_LOCKING_NUM_LOCK 0x83 -#define HID_KEY_LOCKING_SCROLL_LOCK 0x84 -#define HID_KEY_KEYPAD_COMMA 0x85 -#define HID_KEY_KEYPAD_EQUAL_SIGN 0x86 -#define HID_KEY_KANJI1 0x87 -#define HID_KEY_KANJI2 0x88 -#define HID_KEY_KANJI3 0x89 -#define HID_KEY_KANJI4 0x8A -#define HID_KEY_KANJI5 0x8B -#define HID_KEY_KANJI6 0x8C -#define HID_KEY_KANJI7 0x8D -#define HID_KEY_KANJI8 0x8E -#define HID_KEY_KANJI9 0x8F -#define HID_KEY_LANG1 0x90 -#define HID_KEY_LANG2 0x91 -#define HID_KEY_LANG3 0x92 -#define HID_KEY_LANG4 0x93 -#define HID_KEY_LANG5 0x94 -#define HID_KEY_LANG6 0x95 -#define HID_KEY_LANG7 0x96 -#define HID_KEY_LANG8 0x97 -#define HID_KEY_LANG9 0x98 -#define HID_KEY_ALTERNATE_ERASE 0x99 -#define HID_KEY_SYSREQ_ATTENTION 0x9A -#define HID_KEY_CANCEL 0x9B -#define HID_KEY_CLEAR 0x9C -#define HID_KEY_PRIOR 0x9D -#define HID_KEY_RETURN 0x9E -#define HID_KEY_SEPARATOR 0x9F -#define HID_KEY_OUT 0xA0 -#define HID_KEY_OPER 0xA1 -#define HID_KEY_CLEAR_AGAIN 0xA2 -#define HID_KEY_CRSEL_PROPS 0xA3 -#define HID_KEY_EXSEL 0xA4 -// RESERVED 0xA5-DF -#define HID_KEY_CONTROL_LEFT 0xE0 -#define HID_KEY_SHIFT_LEFT 0xE1 -#define HID_KEY_ALT_LEFT 0xE2 -#define HID_KEY_GUI_LEFT 0xE3 -#define HID_KEY_CONTROL_RIGHT 0xE4 -#define HID_KEY_SHIFT_RIGHT 0xE5 -#define HID_KEY_ALT_RIGHT 0xE6 -#define HID_KEY_GUI_RIGHT 0xE7 +#define HID_KEY_NONE 0x00 +#define HID_KEY_A 0x04 +#define HID_KEY_B 0x05 +#define HID_KEY_C 0x06 +#define HID_KEY_D 0x07 +#define HID_KEY_E 0x08 +#define HID_KEY_F 0x09 +#define HID_KEY_G 0x0A +#define HID_KEY_H 0x0B +#define HID_KEY_I 0x0C +#define HID_KEY_J 0x0D +#define HID_KEY_K 0x0E +#define HID_KEY_L 0x0F +#define HID_KEY_M 0x10 +#define HID_KEY_N 0x11 +#define HID_KEY_O 0x12 +#define HID_KEY_P 0x13 +#define HID_KEY_Q 0x14 +#define HID_KEY_R 0x15 +#define HID_KEY_S 0x16 +#define HID_KEY_T 0x17 +#define HID_KEY_U 0x18 +#define HID_KEY_V 0x19 +#define HID_KEY_W 0x1A +#define HID_KEY_X 0x1B +#define HID_KEY_Y 0x1C +#define HID_KEY_Z 0x1D +#define HID_KEY_1 0x1E +#define HID_KEY_2 0x1F +#define HID_KEY_3 0x20 +#define HID_KEY_4 0x21 +#define HID_KEY_5 0x22 +#define HID_KEY_6 0x23 +#define HID_KEY_7 0x24 +#define HID_KEY_8 0x25 +#define HID_KEY_9 0x26 +#define HID_KEY_0 0x27 +#define HID_KEY_ENTER 0x28 +#define HID_KEY_ESCAPE 0x29 +#define HID_KEY_BACKSPACE 0x2A +#define HID_KEY_TAB 0x2B +#define HID_KEY_SPACE 0x2C +#define HID_KEY_MINUS 0x2D +#define HID_KEY_EQUAL 0x2E +#define HID_KEY_BRACKET_LEFT 0x2F +#define HID_KEY_BRACKET_RIGHT 0x30 +#define HID_KEY_BACKSLASH 0x31 +#define HID_KEY_EUROPE_1 0x32 +#define HID_KEY_SEMICOLON 0x33 +#define HID_KEY_APOSTROPHE 0x34 +#define HID_KEY_GRAVE 0x35 +#define HID_KEY_COMMA 0x36 +#define HID_KEY_PERIOD 0x37 +#define HID_KEY_SLASH 0x38 +#define HID_KEY_CAPS_LOCK 0x39 +#define HID_KEY_F1 0x3A +#define HID_KEY_F2 0x3B +#define HID_KEY_F3 0x3C +#define HID_KEY_F4 0x3D +#define HID_KEY_F5 0x3E +#define HID_KEY_F6 0x3F +#define HID_KEY_F7 0x40 +#define HID_KEY_F8 0x41 +#define HID_KEY_F9 0x42 +#define HID_KEY_F10 0x43 +#define HID_KEY_F11 0x44 +#define HID_KEY_F12 0x45 +#define HID_KEY_PRINT_SCREEN 0x46 +#define HID_KEY_SCROLL_LOCK 0x47 +#define HID_KEY_PAUSE 0x48 +#define HID_KEY_INSERT 0x49 +#define HID_KEY_HOME 0x4A +#define HID_KEY_PAGE_UP 0x4B +#define HID_KEY_DELETE 0x4C +#define HID_KEY_END 0x4D +#define HID_KEY_PAGE_DOWN 0x4E +#define HID_KEY_ARROW_RIGHT 0x4F +#define HID_KEY_ARROW_LEFT 0x50 +#define HID_KEY_ARROW_DOWN 0x51 +#define HID_KEY_ARROW_UP 0x52 +#define HID_KEY_NUM_LOCK 0x53 +#define HID_KEY_KEYPAD_DIVIDE 0x54 +#define HID_KEY_KEYPAD_MULTIPLY 0x55 +#define HID_KEY_KEYPAD_SUBTRACT 0x56 +#define HID_KEY_KEYPAD_ADD 0x57 +#define HID_KEY_KEYPAD_ENTER 0x58 +#define HID_KEY_KEYPAD_1 0x59 +#define HID_KEY_KEYPAD_2 0x5A +#define HID_KEY_KEYPAD_3 0x5B +#define HID_KEY_KEYPAD_4 0x5C +#define HID_KEY_KEYPAD_5 0x5D +#define HID_KEY_KEYPAD_6 0x5E +#define HID_KEY_KEYPAD_7 0x5F +#define HID_KEY_KEYPAD_8 0x60 +#define HID_KEY_KEYPAD_9 0x61 +#define HID_KEY_KEYPAD_0 0x62 +#define HID_KEY_KEYPAD_DECIMAL 0x63 +#define HID_KEY_EUROPE_2 0x64 +#define HID_KEY_APPLICATION 0x65 +#define HID_KEY_POWER 0x66 +#define HID_KEY_KEYPAD_EQUAL 0x67 +#define HID_KEY_F13 0x68 +#define HID_KEY_F14 0x69 +#define HID_KEY_F15 0x6A +#define HID_KEY_F16 0x6B +#define HID_KEY_F17 0x6C +#define HID_KEY_F18 0x6D +#define HID_KEY_F19 0x6E +#define HID_KEY_F20 0x6F +#define HID_KEY_F21 0x70 +#define HID_KEY_F22 0x71 +#define HID_KEY_F23 0x72 +#define HID_KEY_F24 0x73 +#define HID_KEY_EXECUTE 0x74 +#define HID_KEY_HELP 0x75 +#define HID_KEY_MENU 0x76 +#define HID_KEY_SELECT 0x77 +#define HID_KEY_STOP 0x78 +#define HID_KEY_AGAIN 0x79 +#define HID_KEY_UNDO 0x7A +#define HID_KEY_CUT 0x7B +#define HID_KEY_COPY 0x7C +#define HID_KEY_PASTE 0x7D +#define HID_KEY_FIND 0x7E +#define HID_KEY_MUTE 0x7F +#define HID_KEY_VOLUME_UP 0x80 +#define HID_KEY_VOLUME_DOWN 0x81 +#define HID_KEY_LOCKING_CAPS_LOCK 0x82 +#define HID_KEY_LOCKING_NUM_LOCK 0x83 +#define HID_KEY_LOCKING_SCROLL_LOCK 0x84 +#define HID_KEY_KEYPAD_COMMA 0x85 +#define HID_KEY_KEYPAD_EQUAL_SIGN 0x86 +#define HID_KEY_KANJI1 0x87 +#define HID_KEY_KANJI2 0x88 +#define HID_KEY_KANJI3 0x89 +#define HID_KEY_KANJI4 0x8A +#define HID_KEY_KANJI5 0x8B +#define HID_KEY_KANJI6 0x8C +#define HID_KEY_KANJI7 0x8D +#define HID_KEY_KANJI8 0x8E +#define HID_KEY_KANJI9 0x8F +#define HID_KEY_LANG1 0x90 +#define HID_KEY_LANG2 0x91 +#define HID_KEY_LANG3 0x92 +#define HID_KEY_LANG4 0x93 +#define HID_KEY_LANG5 0x94 +#define HID_KEY_LANG6 0x95 +#define HID_KEY_LANG7 0x96 +#define HID_KEY_LANG8 0x97 +#define HID_KEY_LANG9 0x98 +#define HID_KEY_ALTERNATE_ERASE 0x99 +#define HID_KEY_SYSREQ_ATTENTION 0x9A +#define HID_KEY_CANCEL 0x9B +#define HID_KEY_CLEAR 0x9C +#define HID_KEY_PRIOR 0x9D +#define HID_KEY_RETURN 0x9E +#define HID_KEY_SEPARATOR 0x9F +#define HID_KEY_OUT 0xA0 +#define HID_KEY_OPER 0xA1 +#define HID_KEY_CLEAR_AGAIN 0xA2 +#define HID_KEY_CRSEL_PROPS 0xA3 +#define HID_KEY_EXSEL 0xA4 +// RESERVED 0xA5-AF +#define HID_KEY_KEYPAD_00 0xB0 +#define HID_KEY_KEYPAD_000 0xB1 +#define HID_KEY_THOUSANDS_SEPARATOR 0xB2 +#define HID_KEY_DECIMAL_SEPARATOR 0xB3 +#define HID_KEY_CURRENCY_UNIT 0xB4 +#define HID_KEY_CURRENCY_SUBUNIT 0xB5 +#define HID_KEY_KEYPAD_LEFT_PARENTHESIS 0xB6 +#define HID_KEY_KEYPAD_RIGHT_PARENTHESIS 0xB7 +#define HID_KEY_KEYPAD_LEFT_BRACE 0xB8 +#define HID_KEY_KEYPAD_RIGHT_BRACE 0xB9 +#define HID_KEY_KEYPAD_TAB 0xBA +#define HID_KEY_KEYPAD_BACKSPACE 0xBB +#define HID_KEY_KEYPAD_A 0xBC +#define HID_KEY_KEYPAD_B 0xBD +#define HID_KEY_KEYPAD_C 0xBE +#define HID_KEY_KEYPAD_D 0xBF +#define HID_KEY_KEYPAD_E 0xC0 +#define HID_KEY_KEYPAD_F 0xC1 +#define HID_KEY_KEYPAD_XOR 0xC2 +#define HID_KEY_KEYPAD_CARET 0xC3 +#define HID_KEY_KEYPAD_PERCENT 0xC4 +#define HID_KEY_KEYPAD_LESS_THAN 0xC5 +#define HID_KEY_KEYPAD_GREATER_THAN 0xC6 +#define HID_KEY_KEYPAD_AMPERSAND 0xC7 +#define HID_KEY_KEYPAD_DOUBLE_AMPERSAND 0xC8 +#define HID_KEY_KEYPAD_VERTICAL_BAR 0xC9 +#define HID_KEY_KEYPAD_DOUBLE_VERTICAL_BAR 0xCA +#define HID_KEY_KEYPAD_COLON 0xCB +#define HID_KEY_KEYPAD_HASH 0xCC +#define HID_KEY_KEYPAD_SPACE 0xCD +#define HID_KEY_KEYPAD_AT 0xCE +#define HID_KEY_KEYPAD_EXCLAMATION 0xCF +#define HID_KEY_KEYPAD_MEMORY_STORE 0xD0 +#define HID_KEY_KEYPAD_MEMORY_RECALL 0xD1 +#define HID_KEY_KEYPAD_MEMORY_CLEAR 0xD2 +#define HID_KEY_KEYPAD_MEMORY_ADD 0xD3 +#define HID_KEY_KEYPAD_MEMORY_SUBTRACT 0xD4 +#define HID_KEY_KEYPAD_MEMORY_MULTIPLY 0xD5 +#define HID_KEY_KEYPAD_MEMORY_DIVIDE 0xD6 +#define HID_KEY_KEYPAD_PLUS_MINUS 0xD7 +#define HID_KEY_KEYPAD_CLEAR 0xD8 +#define HID_KEY_KEYPAD_CLEAR_ENTRY 0xD9 +#define HID_KEY_KEYPAD_BINARY 0xDA +#define HID_KEY_KEYPAD_OCTAL 0xDB +#define HID_KEY_KEYPAD_DECIMAL_2 0xDC +#define HID_KEY_KEYPAD_HEXADECIMAL 0xDD +// RESERVED 0xDE-DF +#define HID_KEY_CONTROL_LEFT 0xE0 +#define HID_KEY_SHIFT_LEFT 0xE1 +#define HID_KEY_ALT_LEFT 0xE2 +#define HID_KEY_GUI_LEFT 0xE3 +#define HID_KEY_CONTROL_RIGHT 0xE4 +#define HID_KEY_SHIFT_RIGHT 0xE5 +#define HID_KEY_ALT_RIGHT 0xE6 +#define HID_KEY_GUI_RIGHT 0xE7 //--------------------------------------------------------------------+ @@ -684,32 +744,33 @@ enum { /// HID Usage Table - Table 1: Usage Page Summary enum { - HID_USAGE_PAGE_DESKTOP = 0x01, - HID_USAGE_PAGE_SIMULATE = 0x02, - HID_USAGE_PAGE_VIRTUAL_REALITY = 0x03, - HID_USAGE_PAGE_SPORT = 0x04, - HID_USAGE_PAGE_GAME = 0x05, - HID_USAGE_PAGE_GENERIC_DEVICE = 0x06, - HID_USAGE_PAGE_KEYBOARD = 0x07, - HID_USAGE_PAGE_LED = 0x08, - HID_USAGE_PAGE_BUTTON = 0x09, - HID_USAGE_PAGE_ORDINAL = 0x0a, - HID_USAGE_PAGE_TELEPHONY = 0x0b, - HID_USAGE_PAGE_CONSUMER = 0x0c, - HID_USAGE_PAGE_DIGITIZER = 0x0d, - HID_USAGE_PAGE_PID = 0x0f, - HID_USAGE_PAGE_UNICODE = 0x10, - HID_USAGE_PAGE_ALPHA_DISPLAY = 0x14, - HID_USAGE_PAGE_MEDICAL = 0x40, - HID_USAGE_PAGE_MONITOR = 0x80, //0x80 - 0x83 - HID_USAGE_PAGE_POWER = 0x84, // 0x084 - 0x87 - HID_USAGE_PAGE_BARCODE_SCANNER = 0x8c, - HID_USAGE_PAGE_SCALE = 0x8d, - HID_USAGE_PAGE_MSR = 0x8e, - HID_USAGE_PAGE_CAMERA = 0x90, - HID_USAGE_PAGE_ARCADE = 0x91, - HID_USAGE_PAGE_FIDO = 0xF1D0, // FIDO alliance HID usage page - HID_USAGE_PAGE_VENDOR = 0xFF00 // 0xFF00 - 0xFFFF + HID_USAGE_PAGE_DESKTOP = 0x01, + HID_USAGE_PAGE_SIMULATE = 0x02, + HID_USAGE_PAGE_VIRTUAL_REALITY = 0x03, + HID_USAGE_PAGE_SPORT = 0x04, + HID_USAGE_PAGE_GAME = 0x05, + HID_USAGE_PAGE_GENERIC_DEVICE = 0x06, + HID_USAGE_PAGE_KEYBOARD = 0x07, + HID_USAGE_PAGE_LED = 0x08, + HID_USAGE_PAGE_BUTTON = 0x09, + HID_USAGE_PAGE_ORDINAL = 0x0a, + HID_USAGE_PAGE_TELEPHONY = 0x0b, + HID_USAGE_PAGE_CONSUMER = 0x0c, + HID_USAGE_PAGE_DIGITIZER = 0x0d, + HID_USAGE_PAGE_PID = 0x0f, + HID_USAGE_PAGE_UNICODE = 0x10, + HID_USAGE_PAGE_ALPHA_DISPLAY = 0x14, + HID_USAGE_PAGE_MEDICAL = 0x40, + HID_USAGE_PAGE_LIGHTING_AND_ILLUMINATION = 0x59, + HID_USAGE_PAGE_MONITOR = 0x80, //0x80 - 0x83 + HID_USAGE_PAGE_POWER = 0x84, // 0x084 - 0x87 + HID_USAGE_PAGE_BARCODE_SCANNER = 0x8c, + HID_USAGE_PAGE_SCALE = 0x8d, + HID_USAGE_PAGE_MSR = 0x8e, + HID_USAGE_PAGE_CAMERA = 0x90, + HID_USAGE_PAGE_ARCADE = 0x91, + HID_USAGE_PAGE_FIDO = 0xF1D0, // FIDO alliance HID usage page + HID_USAGE_PAGE_VENDOR = 0xFF00 // 0xFF00 - 0xFFFF }; /// HID Usage Table - Table 6: Generic Desktop Page @@ -788,8 +849,7 @@ enum { /// HID Usage Table: Consumer Page (0x0C) /// Only contains controls that supported by Windows (whole list is too long) -enum -{ +enum { // Generic Control HID_USAGE_CONSUMER_CONTROL = 0x0001, @@ -845,9 +905,45 @@ enum HID_USAGE_CONSUMER_AC_PAN = 0x0238, }; +/// HID Usage Table - Lighting And Illumination Page (0x59) +enum { + HID_USAGE_LIGHTING_LAMP_ARRAY = 0x01, + HID_USAGE_LIGHTING_LAMP_ARRAY_ATTRIBUTES_REPORT = 0x02, + HID_USAGE_LIGHTING_LAMP_COUNT = 0x03, + HID_USAGE_LIGHTING_BOUNDING_BOX_WIDTH_IN_MICROMETERS = 0x04, + HID_USAGE_LIGHTING_BOUNDING_BOX_HEIGHT_IN_MICROMETERS = 0x05, + HID_USAGE_LIGHTING_BOUNDING_BOX_DEPTH_IN_MICROMETERS = 0x06, + HID_USAGE_LIGHTING_LAMP_ARRAY_KIND = 0x07, + HID_USAGE_LIGHTING_MIN_UPDATE_INTERVAL_IN_MICROSECONDS = 0x08, + HID_USAGE_LIGHTING_LAMP_ATTRIBUTES_REQUEST_REPORT = 0x20, + HID_USAGE_LIGHTING_LAMP_ID = 0x21, + HID_USAGE_LIGHTING_LAMP_ATTRIBUTES_RESPONSE_REPORT = 0x22, + HID_USAGE_LIGHTING_POSITION_X_IN_MICROMETERS = 0x23, + HID_USAGE_LIGHTING_POSITION_Y_IN_MICROMETERS = 0x24, + HID_USAGE_LIGHTING_POSITION_Z_IN_MICROMETERS = 0x25, + HID_USAGE_LIGHTING_LAMP_PURPOSES = 0x26, + HID_USAGE_LIGHTING_UPDATE_LATENCY_IN_MICROSECONDS = 0x27, + HID_USAGE_LIGHTING_RED_LEVEL_COUNT = 0x28, + HID_USAGE_LIGHTING_GREEN_LEVEL_COUNT = 0x29, + HID_USAGE_LIGHTING_BLUE_LEVEL_COUNT = 0x2A, + HID_USAGE_LIGHTING_INTENSITY_LEVEL_COUNT = 0x2B, + HID_USAGE_LIGHTING_IS_PROGRAMMABLE = 0x2C, + HID_USAGE_LIGHTING_INPUT_BINDING = 0x2D, + HID_USAGE_LIGHTING_LAMP_MULTI_UPDATE_REPORT = 0x50, + HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL = 0x51, + HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL = 0x52, + HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL = 0x53, + HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL = 0x54, + HID_USAGE_LIGHTING_LAMP_UPDATE_FLAGS = 0x55, + HID_USAGE_LIGHTING_LAMP_RANGE_UPDATE_REPORT = 0x60, + HID_USAGE_LIGHTING_LAMP_ID_START = 0x61, + HID_USAGE_LIGHTING_LAMP_ID_END = 0x62, + HID_USAGE_LIGHTING_LAMP_ARRAY_CONTROL_REPORT = 0x70, + HID_USAGE_LIGHTING_AUTONOMOUS_MODE = 0x71, +}; + /// HID Usage Table: FIDO Alliance Page (0xF1D0) -enum -{ +enum { HID_USAGE_FIDO_U2FHID = 0x01, // U2FHID usage for top-level collection HID_USAGE_FIDO_DATA_IN = 0x20, // Raw IN data report HID_USAGE_FIDO_DATA_OUT = 0x21 // Raw OUT data report diff --git a/pico-sdk/lib/tinyusb/src/class/hid/hid_device.c b/pico-sdk/lib/tinyusb/src/class/hid/hid_device.c index 5637ea6..ada0158 100644 --- a/pico-sdk/lib/tinyusb/src/class/hid/hid_device.c +++ b/pico-sdk/lib/tinyusb/src/class/hid/hid_device.c @@ -39,23 +39,23 @@ //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ -typedef struct -{ +typedef struct { uint8_t itf_num; uint8_t ep_in; - uint8_t ep_out; // optional Out endpoint - uint8_t itf_protocol; // Boot mouse or keyboard + uint8_t ep_out; // optional Out endpoint + uint8_t itf_protocol; // Boot mouse or keyboard - uint8_t protocol_mode; // Boot (0) or Report protocol (1) - uint8_t idle_rate; // up to application to handle idle rate uint16_t report_desc_len; + CFG_TUSB_MEM_ALIGN uint8_t protocol_mode; // Boot (0) or Report protocol (1) + CFG_TUSB_MEM_ALIGN uint8_t idle_rate; // up to application to handle idle rate CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_HID_EP_BUFSIZE]; CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_HID_EP_BUFSIZE]; + CFG_TUSB_MEM_ALIGN uint8_t ctrl_buf[CFG_TUD_HID_EP_BUFSIZE]; // TODO save hid descriptor since host can specifically request this after enumeration // Note: HID descriptor may be not available from application after enumeration - tusb_hid_descriptor_hid_t const * hid_descriptor; + tusb_hid_descriptor_hid_t const *hid_descriptor; } hidd_interface_t; CFG_TUD_MEM_SECTION tu_static hidd_interface_t _hidd_itf[CFG_TUD_HID]; @@ -63,12 +63,12 @@ CFG_TUD_MEM_SECTION tu_static hidd_interface_t _hidd_itf[CFG_TUD_HID]; /*------------- Helpers -------------*/ static inline uint8_t get_index_by_itfnum(uint8_t itf_num) { - for (uint8_t i=0; i < CFG_TUD_HID; i++ ) - { - if ( itf_num == _hidd_itf[i].itf_num ) return i; - } + for (uint8_t i = 0; i < CFG_TUD_HID; i++) { + if (itf_num == _hidd_itf[i].itf_num) + return i; + } - return 0xFF; + return 0xFF; } //--------------------------------------------------------------------+ @@ -81,37 +81,29 @@ bool tud_hid_n_ready(uint8_t instance) return tud_ready() && (ep_in != 0) && !usbd_edpt_busy(rhport, ep_in); } -bool tud_hid_n_report(uint8_t instance, uint8_t report_id, void const* report, uint16_t len) +bool tud_hid_n_report(uint8_t instance, uint8_t report_id, void const *report, uint16_t len) { uint8_t const rhport = 0; - hidd_interface_t * p_hid = &_hidd_itf[instance]; + hidd_interface_t *p_hid = &_hidd_itf[instance]; // claim endpoint - TU_VERIFY( usbd_edpt_claim(rhport, p_hid->ep_in) ); + TU_VERIFY(usbd_edpt_claim(rhport, p_hid->ep_in)); // prepare data - if (report_id) - { + if (report_id) { p_hid->epin_buf[0] = report_id; - TU_VERIFY(0 == tu_memcpy_s(p_hid->epin_buf+1, CFG_TUD_HID_EP_BUFSIZE-1, report, len)); + TU_VERIFY(0 == tu_memcpy_s(p_hid->epin_buf + 1, CFG_TUD_HID_EP_BUFSIZE - 1, report, len)); len++; - }else - { + } else { TU_VERIFY(0 == tu_memcpy_s(p_hid->epin_buf, CFG_TUD_HID_EP_BUFSIZE, report, len)); } return usbd_edpt_xfer(rhport, p_hid->ep_in, p_hid->epin_buf, len); } -uint8_t tud_hid_n_interface_protocol(uint8_t instance) -{ - return _hidd_itf[instance].itf_protocol; -} +uint8_t tud_hid_n_interface_protocol(uint8_t instance) { return _hidd_itf[instance].itf_protocol; } -uint8_t tud_hid_n_get_protocol(uint8_t instance) -{ - return _hidd_itf[instance].protocol_mode; -} +uint8_t tud_hid_n_get_protocol(uint8_t instance) { return _hidd_itf[instance].protocol_mode; } bool tud_hid_n_keyboard_report(uint8_t instance, uint8_t report_id, uint8_t modifier, uint8_t keycode[6]) { @@ -120,44 +112,51 @@ bool tud_hid_n_keyboard_report(uint8_t instance, uint8_t report_id, uint8_t modi report.modifier = modifier; report.reserved = 0; - if ( keycode ) - { + if (keycode) { memcpy(report.keycode, keycode, sizeof(report.keycode)); - }else - { + } else { tu_memclr(report.keycode, 6); } return tud_hid_n_report(instance, report_id, &report, sizeof(report)); } -bool tud_hid_n_mouse_report(uint8_t instance, uint8_t report_id, - uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal) +bool tud_hid_n_mouse_report(uint8_t instance, uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal) { - hid_mouse_report_t report = - { + hid_mouse_report_t report = { .buttons = buttons, - .x = x, - .y = y, - .wheel = vertical, - .pan = horizontal + .x = x, + .y = y, + .wheel = vertical, + .pan = horizontal }; return tud_hid_n_report(instance, report_id, &report, sizeof(report)); } -bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id, - int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons) { - hid_gamepad_report_t report = - { - .x = x, - .y = y, - .z = z, - .rz = rz, - .rx = rx, - .ry = ry, - .hat = hat, +bool tud_hid_n_abs_mouse_report(uint8_t instance, uint8_t report_id, uint8_t buttons, int16_t x, int16_t y, int8_t vertical, int8_t horizontal) +{ + hid_abs_mouse_report_t report = { .buttons = buttons, + .x = x, + .y = y, + .wheel = vertical, + .pan = horizontal + }; + return tud_hid_n_report(instance, report_id, &report, sizeof(report)); +} + +bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons) +{ + hid_gamepad_report_t report = { + .x = x, + .y = y, + .z = z, + .rz = rz, + .rx = rx, + .ry = ry, + .hat = hat, + .buttons = buttons, }; return tud_hid_n_report(instance, report_id, &report, sizeof(report)); @@ -171,59 +170,59 @@ void hidd_init(void) hidd_reset(0); } +bool hidd_deinit(void) +{ + return true; +} + void hidd_reset(uint8_t rhport) { - (void) rhport; + (void)rhport; tu_memclr(_hidd_itf, sizeof(_hidd_itf)); } -uint16_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t max_len) - { +uint16_t hidd_open(uint8_t rhport, tusb_desc_interface_t const *desc_itf, uint16_t max_len) +{ TU_VERIFY(TUSB_CLASS_HID == desc_itf->bInterfaceClass, 0); // len = interface + hid + n*endpoints - uint16_t const drv_len = - (uint16_t) (sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + - desc_itf->bNumEndpoints * sizeof(tusb_desc_endpoint_t)); + uint16_t const drv_len = (uint16_t)(sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + desc_itf->bNumEndpoints * sizeof(tusb_desc_endpoint_t)); TU_ASSERT(max_len >= drv_len, 0); // Find available interface - hidd_interface_t * p_hid = NULL; + hidd_interface_t *p_hid = NULL; uint8_t hid_id; - for(hid_id=0; hid_idhid_descriptor = (tusb_hid_descriptor_hid_t const *) p_desc; + p_hid->hid_descriptor = (tusb_hid_descriptor_hid_t const *)p_desc; //------------- Endpoint Descriptor -------------// p_desc = tu_desc_next(p_desc); TU_ASSERT(usbd_open_edpt_pair(rhport, p_desc, desc_itf->bNumEndpoints, TUSB_XFER_INTERRUPT, &p_hid->ep_out, &p_hid->ep_in), 0); - if ( desc_itf->bInterfaceSubClass == HID_SUBCLASS_BOOT ) p_hid->itf_protocol = desc_itf->bInterfaceProtocol; + if (desc_itf->bInterfaceSubClass == HID_SUBCLASS_BOOT) + p_hid->itf_protocol = desc_itf->bInterfaceProtocol; p_hid->protocol_mode = HID_PROTOCOL_REPORT; // Per Specs: default is report mode - p_hid->itf_num = desc_itf->bInterfaceNumber; + p_hid->itf_num = desc_itf->bInterfaceNumber; // Use offsetof to avoid pointer to the odd/misaligned address - p_hid->report_desc_len = tu_unaligned_read16((uint8_t const*) p_hid->hid_descriptor + offsetof(tusb_hid_descriptor_hid_t, wReportLength)); + p_hid->report_desc_len = tu_unaligned_read16((uint8_t const *)p_hid->hid_descriptor + offsetof(tusb_hid_descriptor_hid_t, wReportLength)); // Prepare for output endpoint - if (p_hid->ep_out) - { - if ( !usbd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf)) ) - { + if (p_hid->ep_out) { + if (!usbd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf))) { TU_LOG_FAILED(); TU_BREAKPOINT(); } @@ -235,144 +234,120 @@ uint16_t hidd_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint1 // Invoked when a control transfer occurred on an interface of this class // Driver response accordingly to the request and the transfer stage (setup/data/ack) // return false to stall control endpoint (e.g unsupported request) -bool hidd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) +bool hidd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const *request) { TU_VERIFY(request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_INTERFACE); - uint8_t const hid_itf = get_index_by_itfnum((uint8_t) request->wIndex); + uint8_t const hid_itf = get_index_by_itfnum((uint8_t)request->wIndex); TU_VERIFY(hid_itf < CFG_TUD_HID); - hidd_interface_t* p_hid = &_hidd_itf[hid_itf]; + hidd_interface_t *p_hid = &_hidd_itf[hid_itf]; - if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD) - { + if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD) { //------------- STD Request -------------// - if ( stage == CONTROL_STAGE_SETUP ) - { - uint8_t const desc_type = tu_u16_high(request->wValue); - //uint8_t const desc_index = tu_u16_low (request->wValue); + if (stage == CONTROL_STAGE_SETUP) { + uint8_t const desc_type = tu_u16_high(request->wValue); + // uint8_t const desc_index = tu_u16_low (request->wValue); - if (request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_HID) - { + if (request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_HID) { TU_VERIFY(p_hid->hid_descriptor); - TU_VERIFY(tud_control_xfer(rhport, request, (void*)(uintptr_t) p_hid->hid_descriptor, p_hid->hid_descriptor->bLength)); - } - else if (request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT) - { - uint8_t const * desc_report = tud_hid_descriptor_report_cb(hid_itf); - tud_control_xfer(rhport, request, (void*)(uintptr_t) desc_report, p_hid->report_desc_len); - } - else - { + TU_VERIFY(tud_control_xfer(rhport, request, (void *)(uintptr_t)p_hid->hid_descriptor, p_hid->hid_descriptor->bLength)); + } else if (request->bRequest == TUSB_REQ_GET_DESCRIPTOR && desc_type == HID_DESC_TYPE_REPORT) { + uint8_t const *desc_report = tud_hid_descriptor_report_cb(hid_itf); + tud_control_xfer(rhport, request, (void *)(uintptr_t)desc_report, p_hid->report_desc_len); + } else { return false; // stall unsupported request } } - } - else if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS) - { + } else if (request->bmRequestType_bit.type == TUSB_REQ_TYPE_CLASS) { //------------- Class Specific Request -------------// - switch( request->bRequest ) - { - case HID_REQ_CONTROL_GET_REPORT: - if ( stage == CONTROL_STAGE_SETUP ) - { - uint8_t const report_type = tu_u16_high(request->wValue); - uint8_t const report_id = tu_u16_low(request->wValue); + switch (request->bRequest) { + case HID_REQ_CONTROL_GET_REPORT: + if (stage == CONTROL_STAGE_SETUP) { + uint8_t const report_type = tu_u16_high(request->wValue); + uint8_t const report_id = tu_u16_low(request->wValue); - uint8_t* report_buf = p_hid->epin_buf; - uint16_t req_len = tu_min16(request->wLength, CFG_TUD_HID_EP_BUFSIZE); + uint8_t *report_buf = p_hid->ctrl_buf; + uint16_t req_len = tu_min16(request->wLength, CFG_TUD_HID_EP_BUFSIZE); - uint16_t xferlen = 0; + uint16_t xferlen = 0; - // If host request a specific Report ID, add ID to as 1 byte of response - if ( (report_id != HID_REPORT_TYPE_INVALID) && (req_len > 1) ) - { - *report_buf++ = report_id; - req_len--; + // If host request a specific Report ID, add ID to as 1 byte of response + if ((report_id != HID_REPORT_TYPE_INVALID) && (req_len > 1)) { + *report_buf++ = report_id; + req_len--; - xferlen++; - } - - xferlen += tud_hid_get_report_cb(hid_itf, report_id, (hid_report_type_t) report_type, report_buf, req_len); - TU_ASSERT( xferlen > 0 ); - - tud_control_xfer(rhport, request, p_hid->epin_buf, xferlen); + xferlen++; } + + xferlen += tud_hid_get_report_cb(hid_itf, report_id, (hid_report_type_t)report_type, report_buf, req_len); + TU_ASSERT(xferlen > 0); + + tud_control_xfer(rhport, request, p_hid->ctrl_buf, xferlen); + } break; - case HID_REQ_CONTROL_SET_REPORT: - if ( stage == CONTROL_STAGE_SETUP ) - { - TU_VERIFY(request->wLength <= sizeof(p_hid->epout_buf)); - tud_control_xfer(rhport, request, p_hid->epout_buf, request->wLength); + case HID_REQ_CONTROL_SET_REPORT: + if (stage == CONTROL_STAGE_SETUP) { + TU_VERIFY(request->wLength <= sizeof(p_hid->ctrl_buf)); + tud_control_xfer(rhport, request, p_hid->ctrl_buf, request->wLength); + } else if (stage == CONTROL_STAGE_ACK) { + uint8_t const report_type = tu_u16_high(request->wValue); + uint8_t const report_id = tu_u16_low(request->wValue); + + uint8_t const *report_buf = p_hid->ctrl_buf; + uint16_t report_len = tu_min16(request->wLength, CFG_TUD_HID_EP_BUFSIZE); + + // If host request a specific Report ID, extract report ID in buffer before invoking callback + if ((report_id != HID_REPORT_TYPE_INVALID) && (report_len > 1) && (report_id == report_buf[0])) { + report_buf++; + report_len--; } - else if ( stage == CONTROL_STAGE_ACK ) - { - uint8_t const report_type = tu_u16_high(request->wValue); - uint8_t const report_id = tu_u16_low(request->wValue); - uint8_t const* report_buf = p_hid->epout_buf; - uint16_t report_len = tu_min16(request->wLength, CFG_TUD_HID_EP_BUFSIZE); - - // If host request a specific Report ID, extract report ID in buffer before invoking callback - if ( (report_id != HID_REPORT_TYPE_INVALID) && (report_len > 1) && (report_id == report_buf[0]) ) - { - report_buf++; - report_len--; - } - - tud_hid_set_report_cb(hid_itf, report_id, (hid_report_type_t) report_type, report_buf, report_len); - } + tud_hid_set_report_cb(hid_itf, report_id, (hid_report_type_t)report_type, report_buf, report_len); + } break; - case HID_REQ_CONTROL_SET_IDLE: - if ( stage == CONTROL_STAGE_SETUP ) - { - p_hid->idle_rate = tu_u16_high(request->wValue); - if ( tud_hid_set_idle_cb ) - { - // stall request if callback return false - TU_VERIFY( tud_hid_set_idle_cb( hid_itf, p_hid->idle_rate) ); - } - - tud_control_status(rhport, request); + case HID_REQ_CONTROL_SET_IDLE: + if (stage == CONTROL_STAGE_SETUP) { + p_hid->idle_rate = tu_u16_high(request->wValue); + if (tud_hid_set_idle_cb) { + // stall request if callback return false + TU_VERIFY(tud_hid_set_idle_cb(hid_itf, p_hid->idle_rate)); } + + tud_control_status(rhport, request); + } break; - case HID_REQ_CONTROL_GET_IDLE: - if ( stage == CONTROL_STAGE_SETUP ) - { - // TODO idle rate of report - tud_control_xfer(rhport, request, &p_hid->idle_rate, 1); - } + case HID_REQ_CONTROL_GET_IDLE: + if (stage == CONTROL_STAGE_SETUP) { + // TODO idle rate of report + tud_control_xfer(rhport, request, &p_hid->idle_rate, 1); + } break; - case HID_REQ_CONTROL_GET_PROTOCOL: - if ( stage == CONTROL_STAGE_SETUP ) - { - tud_control_xfer(rhport, request, &p_hid->protocol_mode, 1); - } + case HID_REQ_CONTROL_GET_PROTOCOL: + if (stage == CONTROL_STAGE_SETUP) { + tud_control_xfer(rhport, request, &p_hid->protocol_mode, 1); + } break; - case HID_REQ_CONTROL_SET_PROTOCOL: - if ( stage == CONTROL_STAGE_SETUP ) - { - tud_control_status(rhport, request); - } - else if ( stage == CONTROL_STAGE_ACK ) - { - p_hid->protocol_mode = (uint8_t) request->wValue; - if (tud_hid_set_protocol_cb) - { - tud_hid_set_protocol_cb(hid_itf, p_hid->protocol_mode); - } + case HID_REQ_CONTROL_SET_PROTOCOL: + if (stage == CONTROL_STAGE_SETUP) { + tud_control_status(rhport, request); + } else if (stage == CONTROL_STAGE_ACK) { + p_hid->protocol_mode = (uint8_t)request->wValue; + if (tud_hid_set_protocol_cb) { + tud_hid_set_protocol_cb(hid_itf, p_hid->protocol_mode); } + } break; - default: return false; // stall unsupported request + default: + return false; // stall unsupported request } - }else - { + } else { return false; // stall unsupported request } @@ -381,31 +356,43 @@ bool hidd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t bool hidd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { - (void) result; + (void)result; uint8_t instance = 0; - hidd_interface_t * p_hid = _hidd_itf; + hidd_interface_t *p_hid = _hidd_itf; // Identify which interface to use - for (instance = 0; instance < CFG_TUD_HID; instance++) - { + for (instance = 0; instance < CFG_TUD_HID; instance++) { p_hid = &_hidd_itf[instance]; - if ( (ep_addr == p_hid->ep_out) || (ep_addr == p_hid->ep_in) ) break; + if ((ep_addr == p_hid->ep_out) || (ep_addr == p_hid->ep_in)) + break; } TU_ASSERT(instance < CFG_TUD_HID); + // Check if there was a problem + if (XFER_RESULT_SUCCESS != result) { // Inform application about the issue + if (tud_hid_report_fail_cb) { + tud_hid_report_fail_cb(instance, ep_addr, (uint16_t)xferred_bytes); + } + + // Allow a new transfer to be received if issue happened on an OUT endpoint + if (ep_addr == p_hid->ep_out) { + // Prepare the OUT endpoint to be able to receive a new transfer + TU_ASSERT(usbd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf))); + } + + return true; + } + // Sent report successfully - if (ep_addr == p_hid->ep_in) - { - if (tud_hid_report_complete_cb) - { - tud_hid_report_complete_cb(instance, p_hid->epin_buf, (uint16_t) xferred_bytes); + if (ep_addr == p_hid->ep_in) { + if (tud_hid_report_complete_cb) { + tud_hid_report_complete_cb(instance, p_hid->epin_buf, (uint16_t)xferred_bytes); } } - // Received report - else if (ep_addr == p_hid->ep_out) - { - tud_hid_set_report_cb(instance, 0, HID_REPORT_TYPE_INVALID, p_hid->epout_buf, (uint16_t) xferred_bytes); + // Received report successfully + else if (ep_addr == p_hid->ep_out) { + tud_hid_set_report_cb(instance, 0, HID_REPORT_TYPE_OUTPUT, p_hid->epout_buf, (uint16_t)xferred_bytes); TU_ASSERT(usbd_edpt_xfer(rhport, p_hid->ep_out, p_hid->epout_buf, sizeof(p_hid->epout_buf))); } diff --git a/pico-sdk/lib/tinyusb/src/class/hid/hid_device.h b/pico-sdk/lib/tinyusb/src/class/hid/hid_device.h index 17b24de..fcbf161 100644 --- a/pico-sdk/lib/tinyusb/src/class/hid/hid_device.h +++ b/pico-sdk/lib/tinyusb/src/class/hid/hid_device.h @@ -72,6 +72,16 @@ bool tud_hid_n_keyboard_report(uint8_t instance, uint8_t report_id, uint8_t modi // use template layout report as defined by hid_mouse_report_t bool tud_hid_n_mouse_report(uint8_t instance, uint8_t report_id, uint8_t buttons, int8_t x, int8_t y, int8_t vertical, int8_t horizontal); +// ABSOLUTE MOUSE: convenient helper to send absolute mouse report if application +// use template layout report as defined by hid_abs_mouse_report_t +bool tud_hid_n_abs_mouse_report(uint8_t instance, uint8_t report_id, uint8_t buttons, int16_t x, int16_t y, int8_t vertical, int8_t horizontal); + + +static inline bool tud_hid_abs_mouse_report(uint8_t report_id, uint8_t buttons, int16_t x, int16_t y, int8_t vertical, int8_t horizontal) +{ + return tud_hid_n_abs_mouse_report(0, report_id, buttons, x, y, vertical, horizontal); +} + // Gamepad: convenient helper to send gamepad report if application // use template layout report TUD_HID_REPORT_DESC_GAMEPAD bool tud_hid_n_gamepad_report(uint8_t instance, uint8_t report_id, int8_t x, int8_t y, int8_t z, int8_t rz, int8_t rx, int8_t ry, uint8_t hat, uint32_t buttons); @@ -118,6 +128,8 @@ TU_ATTR_WEAK bool tud_hid_set_idle_cb(uint8_t instance, uint8_t idle_rate); // Note: For composite reports, report[0] is report ID TU_ATTR_WEAK void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint16_t len); +// Invoked when a transfer wasn't successful +TU_ATTR_WEAK void tud_hid_report_fail_cb(uint8_t instance, uint8_t ep_addr, uint16_t len); //--------------------------------------------------------------------+ // Inline Functions @@ -266,6 +278,55 @@ static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y HID_COLLECTION_END , \ HID_COLLECTION_END \ +// Absolute Mouse Report Descriptor Template +#define TUD_HID_REPORT_DESC_ABSMOUSE(...) \ + HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ + HID_USAGE ( HID_USAGE_DESKTOP_MOUSE ) ,\ + HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ + /* Report ID if any */\ + __VA_ARGS__ \ + HID_USAGE ( HID_USAGE_DESKTOP_POINTER ) ,\ + HID_COLLECTION ( HID_COLLECTION_PHYSICAL ) ,\ + HID_USAGE_PAGE ( HID_USAGE_PAGE_BUTTON ) ,\ + HID_USAGE_MIN ( 1 ) ,\ + HID_USAGE_MAX ( 5 ) ,\ + HID_LOGICAL_MIN ( 0 ) ,\ + HID_LOGICAL_MAX ( 1 ) ,\ + /* Left, Right, Middle, Backward, Forward buttons */ \ + HID_REPORT_COUNT( 5 ) ,\ + HID_REPORT_SIZE ( 1 ) ,\ + HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\ + /* 3 bit padding */ \ + HID_REPORT_COUNT( 1 ) ,\ + HID_REPORT_SIZE ( 3 ) ,\ + HID_INPUT ( HID_CONSTANT ) ,\ + HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ + /* X, Y absolute position [0, 32767] */ \ + HID_USAGE ( HID_USAGE_DESKTOP_X ) ,\ + HID_USAGE ( HID_USAGE_DESKTOP_Y ) ,\ + HID_LOGICAL_MIN ( 0x00 ) ,\ + HID_LOGICAL_MAX_N( 0x7FFF, 2 ) ,\ + HID_REPORT_SIZE ( 16 ) ,\ + HID_REPORT_COUNT ( 2 ) ,\ + HID_INPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ) ,\ + /* Vertical wheel scroll [-127, 127] */ \ + HID_USAGE ( HID_USAGE_DESKTOP_WHEEL ) ,\ + HID_LOGICAL_MIN ( 0x81 ) ,\ + HID_LOGICAL_MAX ( 0x7f ) ,\ + HID_REPORT_COUNT( 1 ) ,\ + HID_REPORT_SIZE ( 8 ) ,\ + HID_INPUT ( HID_DATA | HID_VARIABLE | HID_RELATIVE ) ,\ + HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ), \ + /* Horizontal wheel scroll [-127, 127] */ \ + HID_USAGE_N ( HID_USAGE_CONSUMER_AC_PAN, 2 ), \ + HID_LOGICAL_MIN ( 0x81 ), \ + HID_LOGICAL_MAX ( 0x7f ), \ + HID_REPORT_COUNT( 1 ), \ + HID_REPORT_SIZE ( 8 ), \ + HID_INPUT ( HID_DATA | HID_VARIABLE | HID_RELATIVE ), \ + HID_COLLECTION_END , \ + HID_COLLECTION_END \ + // Consumer Control Report Descriptor Template #define TUD_HID_REPORT_DESC_CONSUMER(...) \ HID_USAGE_PAGE ( HID_USAGE_PAGE_CONSUMER ) ,\ @@ -402,15 +463,189 @@ static inline bool tud_hid_gamepad_report(uint8_t report_id, int8_t x, int8_t y HID_OUTPUT ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\ HID_COLLECTION_END \ +// HID Lighting and Illumination Report Descriptor Template +// - 1st parameter is report id (required) +// Creates 6 report ids for lighting HID usages in the following order: +// report_id+0: HID_USAGE_LIGHTING_LAMP_ARRAY_ATTRIBUTES_REPORT +// report_id+1: HID_USAGE_LIGHTING_LAMP_ATTRIBUTES_REQUEST_REPORT +// report_id+2: HID_USAGE_LIGHTING_LAMP_ATTRIBUTES_RESPONSE_REPORT +// report_id+3: HID_USAGE_LIGHTING_LAMP_MULTI_UPDATE_REPORT +// report_id+4: HID_USAGE_LIGHTING_LAMP_RANGE_UPDATE_REPORT +// report_id+5: HID_USAGE_LIGHTING_LAMP_ARRAY_CONTROL_REPORT +#define TUD_HID_REPORT_DESC_LIGHTING(report_id) \ + HID_USAGE_PAGE ( HID_USAGE_PAGE_LIGHTING_AND_ILLUMINATION ),\ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_ARRAY ),\ + HID_COLLECTION ( HID_COLLECTION_APPLICATION ),\ + /* Lamp Array Attributes Report */ \ + HID_REPORT_ID (report_id ) \ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_ARRAY_ATTRIBUTES_REPORT ),\ + HID_COLLECTION ( HID_COLLECTION_LOGICAL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_COUNT ),\ + HID_LOGICAL_MIN ( 0 ),\ + HID_LOGICAL_MAX_N ( 65535, 3 ),\ + HID_REPORT_SIZE ( 16 ),\ + HID_REPORT_COUNT ( 1 ),\ + HID_FEATURE ( HID_CONSTANT | HID_VARIABLE | HID_ABSOLUTE ),\ + HID_USAGE ( HID_USAGE_LIGHTING_BOUNDING_BOX_WIDTH_IN_MICROMETERS ),\ + HID_USAGE ( HID_USAGE_LIGHTING_BOUNDING_BOX_HEIGHT_IN_MICROMETERS ),\ + HID_USAGE ( HID_USAGE_LIGHTING_BOUNDING_BOX_DEPTH_IN_MICROMETERS ),\ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_ARRAY_KIND ),\ + HID_USAGE ( HID_USAGE_LIGHTING_MIN_UPDATE_INTERVAL_IN_MICROSECONDS ),\ + HID_LOGICAL_MIN ( 0 ),\ + HID_LOGICAL_MAX_N ( 2147483647, 3 ),\ + HID_REPORT_SIZE ( 32 ),\ + HID_REPORT_COUNT ( 5 ),\ + HID_FEATURE ( HID_CONSTANT | HID_VARIABLE | HID_ABSOLUTE ),\ + HID_COLLECTION_END ,\ + /* Lamp Attributes Request Report */ \ + HID_REPORT_ID ( report_id + 1 ) \ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_ATTRIBUTES_REQUEST_REPORT ),\ + HID_COLLECTION ( HID_COLLECTION_LOGICAL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_ID ),\ + HID_LOGICAL_MIN ( 0 ),\ + HID_LOGICAL_MAX_N ( 65535, 3 ),\ + HID_REPORT_SIZE ( 16 ),\ + HID_REPORT_COUNT ( 1 ),\ + HID_FEATURE ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\ + HID_COLLECTION_END ,\ + /* Lamp Attributes Response Report */ \ + HID_REPORT_ID ( report_id + 2 ) \ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_ATTRIBUTES_RESPONSE_REPORT ),\ + HID_COLLECTION ( HID_COLLECTION_LOGICAL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_ID ),\ + HID_LOGICAL_MIN ( 0 ),\ + HID_LOGICAL_MAX_N ( 65535, 3 ),\ + HID_REPORT_SIZE ( 16 ),\ + HID_REPORT_COUNT ( 1 ),\ + HID_FEATURE ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\ + HID_USAGE ( HID_USAGE_LIGHTING_POSITION_X_IN_MICROMETERS ),\ + HID_USAGE ( HID_USAGE_LIGHTING_POSITION_Y_IN_MICROMETERS ),\ + HID_USAGE ( HID_USAGE_LIGHTING_POSITION_Z_IN_MICROMETERS ),\ + HID_USAGE ( HID_USAGE_LIGHTING_UPDATE_LATENCY_IN_MICROSECONDS ),\ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_PURPOSES ),\ + HID_LOGICAL_MIN ( 0 ),\ + HID_LOGICAL_MAX_N ( 2147483647, 3 ),\ + HID_REPORT_SIZE ( 32 ),\ + HID_REPORT_COUNT ( 5 ),\ + HID_FEATURE ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\ + HID_USAGE ( HID_USAGE_LIGHTING_RED_LEVEL_COUNT ),\ + HID_USAGE ( HID_USAGE_LIGHTING_GREEN_LEVEL_COUNT ),\ + HID_USAGE ( HID_USAGE_LIGHTING_BLUE_LEVEL_COUNT ),\ + HID_USAGE ( HID_USAGE_LIGHTING_INTENSITY_LEVEL_COUNT ),\ + HID_USAGE ( HID_USAGE_LIGHTING_IS_PROGRAMMABLE ),\ + HID_USAGE ( HID_USAGE_LIGHTING_INPUT_BINDING ),\ + HID_LOGICAL_MIN ( 0 ),\ + HID_LOGICAL_MAX_N ( 255, 2 ),\ + HID_REPORT_SIZE ( 8 ),\ + HID_REPORT_COUNT ( 6 ),\ + HID_FEATURE ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\ + HID_COLLECTION_END ,\ + /* Lamp Multi-Update Report */ \ + HID_REPORT_ID ( report_id + 3 ) \ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_MULTI_UPDATE_REPORT ),\ + HID_COLLECTION ( HID_COLLECTION_LOGICAL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_COUNT ),\ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_UPDATE_FLAGS ),\ + HID_LOGICAL_MIN ( 0 ),\ + HID_LOGICAL_MAX ( 8 ),\ + HID_REPORT_SIZE ( 8 ),\ + HID_REPORT_COUNT ( 2 ),\ + HID_FEATURE ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_ID ),\ + HID_LOGICAL_MIN ( 0 ),\ + HID_LOGICAL_MAX_N ( 65535, 3 ),\ + HID_REPORT_SIZE ( 16 ),\ + HID_REPORT_COUNT ( 8 ),\ + HID_FEATURE ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\ + HID_USAGE ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ + HID_LOGICAL_MIN ( 0 ),\ + HID_LOGICAL_MAX_N ( 255, 2 ),\ + HID_REPORT_SIZE ( 8 ),\ + HID_REPORT_COUNT ( 32 ),\ + HID_FEATURE ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\ + HID_COLLECTION_END ,\ + /* Lamp Range Update Report */ \ + HID_REPORT_ID ( report_id + 4 ) \ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_RANGE_UPDATE_REPORT ),\ + HID_COLLECTION ( HID_COLLECTION_LOGICAL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_UPDATE_FLAGS ),\ + HID_LOGICAL_MIN ( 0 ),\ + HID_LOGICAL_MAX ( 8 ),\ + HID_REPORT_SIZE ( 8 ),\ + HID_REPORT_COUNT ( 1 ),\ + HID_FEATURE ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_ID_START ),\ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_ID_END ),\ + HID_LOGICAL_MIN ( 0 ),\ + HID_LOGICAL_MAX_N ( 65535, 3 ),\ + HID_REPORT_SIZE ( 16 ),\ + HID_REPORT_COUNT ( 2 ),\ + HID_FEATURE ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\ + HID_USAGE ( HID_USAGE_LIGHTING_RED_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_GREEN_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_BLUE_UPDATE_CHANNEL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_INTENSITY_UPDATE_CHANNEL ),\ + HID_LOGICAL_MIN ( 0 ),\ + HID_LOGICAL_MAX_N ( 255, 2 ),\ + HID_REPORT_SIZE ( 8 ),\ + HID_REPORT_COUNT ( 4 ),\ + HID_FEATURE ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\ + HID_COLLECTION_END ,\ + /* Lamp Array Control Report */ \ + HID_REPORT_ID ( report_id + 5 ) \ + HID_USAGE ( HID_USAGE_LIGHTING_LAMP_ARRAY_CONTROL_REPORT ),\ + HID_COLLECTION ( HID_COLLECTION_LOGICAL ),\ + HID_USAGE ( HID_USAGE_LIGHTING_AUTONOMOUS_MODE ),\ + HID_LOGICAL_MIN ( 0 ),\ + HID_LOGICAL_MAX ( 1 ),\ + HID_REPORT_SIZE ( 8 ),\ + HID_REPORT_COUNT ( 1 ),\ + HID_FEATURE ( HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),\ + HID_COLLECTION_END ,\ + HID_COLLECTION_END \ + //--------------------------------------------------------------------+ // Internal Class Driver API //--------------------------------------------------------------------+ void hidd_init (void); +bool hidd_deinit (void); void hidd_reset (uint8_t rhport); uint16_t hidd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len); bool hidd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request); bool hidd_xfer_cb (uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); + #ifdef __cplusplus } #endif diff --git a/pico-sdk/lib/tinyusb/src/class/hid/hid_host.c b/pico-sdk/lib/tinyusb/src/class/hid/hid_host.c index 54e6c98..115a8a4 100644 --- a/pico-sdk/lib/tinyusb/src/class/hid/hid_host.c +++ b/pico-sdk/lib/tinyusb/src/class/hid/hid_host.c @@ -39,22 +39,22 @@ #endif #define TU_LOG_DRV(...) TU_LOG(CFG_TUH_HID_LOG_LEVEL, __VA_ARGS__) + //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ - -typedef struct -{ +typedef struct { uint8_t daddr; uint8_t itf_num; uint8_t ep_in; uint8_t ep_out; + bool mounted; // Enumeration is complete uint8_t itf_protocol; // None, Keyboard, Mouse uint8_t protocol_mode; // Boot (0) or Report protocol (1) - uint8_t report_desc_type; + uint8_t report_desc_type; uint16_t report_desc_len; uint16_t epin_size; @@ -72,78 +72,57 @@ tu_static uint8_t _hidh_default_protocol = HID_PROTOCOL_BOOT; //--------------------------------------------------------------------+ // Helper //--------------------------------------------------------------------+ - -TU_ATTR_ALWAYS_INLINE static inline -hidh_interface_t* get_hid_itf(uint8_t daddr, uint8_t idx) -{ +TU_ATTR_ALWAYS_INLINE static inline hidh_interface_t* get_hid_itf(uint8_t daddr, uint8_t idx) { TU_ASSERT(daddr > 0 && idx < CFG_TUH_HID, NULL); hidh_interface_t* p_hid = &_hidh_itf[idx]; return (p_hid->daddr == daddr) ? p_hid : NULL; } // Get instance ID by endpoint address -static uint8_t get_idx_by_epaddr(uint8_t daddr, uint8_t ep_addr) -{ - for ( uint8_t idx = 0; idx < CFG_TUH_HID; idx++ ) - { - hidh_interface_t const * p_hid = &_hidh_itf[idx]; - - if ( p_hid->daddr == daddr && - (p_hid->ep_in == ep_addr || p_hid->ep_out == ep_addr) ) - { +static uint8_t get_idx_by_epaddr(uint8_t daddr, uint8_t ep_addr) { + for (uint8_t idx = 0; idx < CFG_TUH_HID; idx++) { + hidh_interface_t const* p_hid = &_hidh_itf[idx]; + if (p_hid->daddr == daddr && + (p_hid->ep_in == ep_addr || p_hid->ep_out == ep_addr)) { return idx; } } - return TUSB_INDEX_INVALID_8; } -static hidh_interface_t* find_new_itf(void) -{ - for(uint8_t i=0; imounted; } -bool tuh_hid_itf_get_info(uint8_t daddr, uint8_t idx, tuh_itf_info_t* info) -{ +bool tuh_hid_itf_get_info(uint8_t daddr, uint8_t idx, tuh_itf_info_t* info) { hidh_interface_t* p_hid = get_hid_itf(daddr, idx); TU_VERIFY(p_hid && info); @@ -151,34 +130,30 @@ bool tuh_hid_itf_get_info(uint8_t daddr, uint8_t idx, tuh_itf_info_t* info) // re-construct descriptor tusb_desc_interface_t* desc = &info->desc; - desc->bLength = sizeof(tusb_desc_interface_t); - desc->bDescriptorType = TUSB_DESC_INTERFACE; + desc->bLength = sizeof(tusb_desc_interface_t); + desc->bDescriptorType = TUSB_DESC_INTERFACE; - desc->bInterfaceNumber = p_hid->itf_num; - desc->bAlternateSetting = 0; - desc->bNumEndpoints = (uint8_t) ((p_hid->ep_in ? 1u : 0u) + (p_hid->ep_out ? 1u : 0u)); - desc->bInterfaceClass = TUSB_CLASS_HID; + desc->bInterfaceNumber = p_hid->itf_num; + desc->bAlternateSetting = 0; + desc->bNumEndpoints = (uint8_t) ((p_hid->ep_in ? 1u : 0u) + (p_hid->ep_out ? 1u : 0u)); + desc->bInterfaceClass = TUSB_CLASS_HID; desc->bInterfaceSubClass = (p_hid->itf_protocol ? HID_SUBCLASS_BOOT : HID_SUBCLASS_NONE); desc->bInterfaceProtocol = p_hid->itf_protocol; - desc->iInterface = 0; // not used yet + desc->iInterface = 0; // not used yet return true; } -uint8_t tuh_hid_itf_get_index(uint8_t daddr, uint8_t itf_num) -{ - for ( uint8_t idx = 0; idx < CFG_TUH_HID; idx++ ) - { - hidh_interface_t const * p_hid = &_hidh_itf[idx]; - - if ( p_hid->daddr == daddr && p_hid->itf_num == itf_num) return idx; +uint8_t tuh_hid_itf_get_index(uint8_t daddr, uint8_t itf_num) { + for (uint8_t idx = 0; idx < CFG_TUH_HID; idx++) { + hidh_interface_t const* p_hid = &_hidh_itf[idx]; + if (p_hid->daddr == daddr && p_hid->itf_num == itf_num) return idx; } return TUSB_INDEX_INVALID_8; } -uint8_t tuh_hid_interface_protocol(uint8_t daddr, uint8_t idx) -{ +uint8_t tuh_hid_interface_protocol(uint8_t daddr, uint8_t idx) { hidh_interface_t* p_hid = get_hid_itf(daddr, idx); return p_hid ? p_hid->itf_protocol : 0; } @@ -186,29 +161,24 @@ uint8_t tuh_hid_interface_protocol(uint8_t daddr, uint8_t idx) //--------------------------------------------------------------------+ // Control Endpoint API //--------------------------------------------------------------------+ - -uint8_t tuh_hid_get_protocol(uint8_t daddr, uint8_t idx) -{ +uint8_t tuh_hid_get_protocol(uint8_t daddr, uint8_t idx) { hidh_interface_t* p_hid = get_hid_itf(daddr, idx); return p_hid ? p_hid->protocol_mode : 0; } -static void set_protocol_complete(tuh_xfer_t* xfer) -{ +static void set_protocol_complete(tuh_xfer_t* xfer) { uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex); - uint8_t const daddr = xfer->daddr; - uint8_t const idx = tuh_hid_itf_get_index(daddr, itf_num); + uint8_t const daddr = xfer->daddr; + uint8_t const idx = tuh_hid_itf_get_index(daddr, itf_num); hidh_interface_t* p_hid = get_hid_itf(daddr, idx); - TU_VERIFY(p_hid, ); + TU_VERIFY(p_hid,); - if (XFER_RESULT_SUCCESS == xfer->result) - { + if (XFER_RESULT_SUCCESS == xfer->result) { p_hid->protocol_mode = (uint8_t) tu_le16toh(xfer->setup->wValue); } - if (tuh_hid_set_protocol_complete_cb) - { + if (tuh_hid_set_protocol_complete_cb) { tuh_hid_set_protocol_complete_cb(daddr, idx, p_hid->protocol_mode); } } @@ -217,123 +187,153 @@ void tuh_hid_set_default_protocol(uint8_t protocol) { _hidh_default_protocol = protocol; } -static bool _hidh_set_protocol(uint8_t daddr, uint8_t itf_num, uint8_t protocol, tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ +static bool _hidh_set_protocol(uint8_t daddr, uint8_t itf_num, uint8_t protocol, + tuh_xfer_cb_t complete_cb, uintptr_t user_data) { TU_LOG_DRV("HID Set Protocol = %d\r\n", protocol); - tusb_control_request_t const request = - { - .bmRequestType_bit = - { - .recipient = TUSB_REQ_RCPT_INTERFACE, - .type = TUSB_REQ_TYPE_CLASS, - .direction = TUSB_DIR_OUT - }, - .bRequest = HID_REQ_CONTROL_SET_PROTOCOL, - .wValue = protocol, - .wIndex = itf_num, - .wLength = 0 + tusb_control_request_t const request = { + .bmRequestType_bit = { + .recipient = TUSB_REQ_RCPT_INTERFACE, + .type = TUSB_REQ_TYPE_CLASS, + .direction = TUSB_DIR_OUT + }, + .bRequest = HID_REQ_CONTROL_SET_PROTOCOL, + .wValue = protocol, + .wIndex = itf_num, + .wLength = 0 }; - tuh_xfer_t xfer = - { - .daddr = daddr, - .ep_addr = 0, - .setup = &request, - .buffer = NULL, - .complete_cb = complete_cb, - .user_data = user_data + tuh_xfer_t xfer = { + .daddr = daddr, + .ep_addr = 0, + .setup = &request, + .buffer = NULL, + .complete_cb = complete_cb, + .user_data = user_data }; return tuh_control_xfer(&xfer); } -bool tuh_hid_set_protocol(uint8_t daddr, uint8_t idx, uint8_t protocol) -{ +bool tuh_hid_set_protocol(uint8_t daddr, uint8_t idx, uint8_t protocol) { hidh_interface_t* p_hid = get_hid_itf(daddr, idx); TU_VERIFY(p_hid && p_hid->itf_protocol != HID_ITF_PROTOCOL_NONE); return _hidh_set_protocol(daddr, p_hid->itf_num, protocol, set_protocol_complete, 0); } -static void set_report_complete(tuh_xfer_t* xfer) -{ - TU_LOG_DRV("HID Set Report complete\r\n"); +static void get_report_complete(tuh_xfer_t* xfer) { + TU_LOG_DRV("HID Get Report complete\r\n"); - if (tuh_hid_set_report_complete_cb) - { + if (tuh_hid_get_report_complete_cb) { uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex); - uint8_t const idx = tuh_hid_itf_get_index(xfer->daddr, itf_num); + uint8_t const idx = tuh_hid_itf_get_index(xfer->daddr, itf_num); uint8_t const report_type = tu_u16_high(xfer->setup->wValue); - uint8_t const report_id = tu_u16_low(xfer->setup->wValue); + uint8_t const report_id = tu_u16_low(xfer->setup->wValue); + + tuh_hid_get_report_complete_cb(xfer->daddr, idx, report_id, report_type, + (xfer->result == XFER_RESULT_SUCCESS) ? xfer->setup->wLength : 0); + } +} + +bool tuh_hid_get_report(uint8_t daddr, uint8_t idx, uint8_t report_id, uint8_t report_type, void* report, uint16_t len) { + hidh_interface_t* p_hid = get_hid_itf(daddr, idx); + TU_VERIFY(p_hid); + TU_LOG_DRV("HID Get Report: id = %u, type = %u, len = %u\r\n", report_id, report_type, len); + + tusb_control_request_t const request = { + .bmRequestType_bit = { + .recipient = TUSB_REQ_RCPT_INTERFACE, + .type = TUSB_REQ_TYPE_CLASS, + .direction = TUSB_DIR_IN + }, + .bRequest = HID_REQ_CONTROL_GET_REPORT, + .wValue = tu_htole16(tu_u16(report_type, report_id)), + .wIndex = tu_htole16((uint16_t) p_hid->itf_num), + .wLength = len + }; + + tuh_xfer_t xfer = { + .daddr = daddr, + .ep_addr = 0, + .setup = &request, + .buffer = report, + .complete_cb = get_report_complete, + .user_data = 0 + }; + + return tuh_control_xfer(&xfer); +} + +static void set_report_complete(tuh_xfer_t* xfer) { + TU_LOG_DRV("HID Set Report complete\r\n"); + + if (tuh_hid_set_report_complete_cb) { + uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex); + uint8_t const idx = tuh_hid_itf_get_index(xfer->daddr, itf_num); + + uint8_t const report_type = tu_u16_high(xfer->setup->wValue); + uint8_t const report_id = tu_u16_low(xfer->setup->wValue); tuh_hid_set_report_complete_cb(xfer->daddr, idx, report_id, report_type, (xfer->result == XFER_RESULT_SUCCESS) ? xfer->setup->wLength : 0); } } -bool tuh_hid_set_report(uint8_t daddr, uint8_t idx, uint8_t report_id, uint8_t report_type, void* report, uint16_t len) -{ +bool tuh_hid_set_report(uint8_t daddr, uint8_t idx, uint8_t report_id, uint8_t report_type, void* report, uint16_t len) { hidh_interface_t* p_hid = get_hid_itf(daddr, idx); TU_VERIFY(p_hid); - TU_LOG_DRV("HID Set Report: id = %u, type = %u, len = %u\r\n", report_id, report_type, len); - tusb_control_request_t const request = - { - .bmRequestType_bit = - { - .recipient = TUSB_REQ_RCPT_INTERFACE, - .type = TUSB_REQ_TYPE_CLASS, - .direction = TUSB_DIR_OUT - }, - .bRequest = HID_REQ_CONTROL_SET_REPORT, - .wValue = tu_htole16(tu_u16(report_type, report_id)), - .wIndex = tu_htole16((uint16_t)p_hid->itf_num), - .wLength = len + tusb_control_request_t const request = { + .bmRequestType_bit = { + .recipient = TUSB_REQ_RCPT_INTERFACE, + .type = TUSB_REQ_TYPE_CLASS, + .direction = TUSB_DIR_OUT + }, + .bRequest = HID_REQ_CONTROL_SET_REPORT, + .wValue = tu_htole16(tu_u16(report_type, report_id)), + .wIndex = tu_htole16((uint16_t) p_hid->itf_num), + .wLength = len }; - tuh_xfer_t xfer = - { - .daddr = daddr, - .ep_addr = 0, - .setup = &request, - .buffer = report, - .complete_cb = set_report_complete, - .user_data = 0 + tuh_xfer_t xfer = { + .daddr = daddr, + .ep_addr = 0, + .setup = &request, + .buffer = report, + .complete_cb = set_report_complete, + .user_data = 0 }; return tuh_control_xfer(&xfer); } -static bool _hidh_set_idle(uint8_t daddr, uint8_t itf_num, uint16_t idle_rate, tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ +static bool _hidh_set_idle(uint8_t daddr, uint8_t itf_num, uint16_t idle_rate, + tuh_xfer_cb_t complete_cb, uintptr_t user_data) { // SET IDLE request, device can stall if not support this request TU_LOG_DRV("HID Set Idle \r\n"); - tusb_control_request_t const request = - { - .bmRequestType_bit = - { - .recipient = TUSB_REQ_RCPT_INTERFACE, - .type = TUSB_REQ_TYPE_CLASS, - .direction = TUSB_DIR_OUT - }, - .bRequest = HID_REQ_CONTROL_SET_IDLE, - .wValue = tu_htole16(idle_rate), - .wIndex = tu_htole16((uint16_t)itf_num), - .wLength = 0 + tusb_control_request_t const request = { + .bmRequestType_bit = { + .recipient = TUSB_REQ_RCPT_INTERFACE, + .type = TUSB_REQ_TYPE_CLASS, + .direction = TUSB_DIR_OUT + }, + .bRequest = HID_REQ_CONTROL_SET_IDLE, + .wValue = tu_htole16(idle_rate), + .wIndex = tu_htole16((uint16_t) itf_num), + .wLength = 0 }; - tuh_xfer_t xfer = - { - .daddr = daddr, - .ep_addr = 0, - .setup = &request, - .buffer = NULL, - .complete_cb = complete_cb, - .user_data = user_data + tuh_xfer_t xfer = { + .daddr = daddr, + .ep_addr = 0, + .setup = &request, + .buffer = NULL, + .complete_cb = complete_cb, + .user_data = user_data }; return tuh_control_xfer(&xfer); @@ -344,68 +344,60 @@ static bool _hidh_set_idle(uint8_t daddr, uint8_t itf_num, uint16_t idle_rate, t //--------------------------------------------------------------------+ // Check if HID interface is ready to receive report -bool tuh_hid_receive_ready(uint8_t dev_addr, uint8_t idx) -{ +bool tuh_hid_receive_ready(uint8_t dev_addr, uint8_t idx) { hidh_interface_t* p_hid = get_hid_itf(dev_addr, idx); TU_VERIFY(p_hid); - return !usbh_edpt_busy(dev_addr, p_hid->ep_in); } -bool tuh_hid_receive_report(uint8_t daddr, uint8_t idx) -{ +bool tuh_hid_receive_report(uint8_t daddr, uint8_t idx) { hidh_interface_t* p_hid = get_hid_itf(daddr, idx); TU_VERIFY(p_hid); // claim endpoint - TU_VERIFY( usbh_edpt_claim(daddr, p_hid->ep_in) ); + TU_VERIFY(usbh_edpt_claim(daddr, p_hid->ep_in)); - if ( !usbh_edpt_xfer(daddr, p_hid->ep_in, p_hid->epin_buf, p_hid->epin_size) ) - { + if (!usbh_edpt_xfer(daddr, p_hid->ep_in, p_hid->epin_buf, p_hid->epin_size)) { usbh_edpt_release(daddr, p_hid->ep_in); return false; } return true; } - -bool tuh_hid_send_ready(uint8_t dev_addr, uint8_t idx) -{ +bool tuh_hid_receive_abort(uint8_t dev_addr, uint8_t idx) { hidh_interface_t* p_hid = get_hid_itf(dev_addr, idx); TU_VERIFY(p_hid); + return tuh_edpt_abort_xfer(dev_addr, p_hid->ep_in); +} +bool tuh_hid_send_ready(uint8_t dev_addr, uint8_t idx) { + hidh_interface_t* p_hid = get_hid_itf(dev_addr, idx); + TU_VERIFY(p_hid); return !usbh_edpt_busy(dev_addr, p_hid->ep_out); } -bool tuh_hid_send_report(uint8_t daddr, uint8_t idx, uint8_t report_id, const void* report, uint16_t len) -{ +bool tuh_hid_send_report(uint8_t daddr, uint8_t idx, uint8_t report_id, const void* report, uint16_t len) { TU_LOG_DRV("HID Send Report %d\r\n", report_id); hidh_interface_t* p_hid = get_hid_itf(daddr, idx); TU_VERIFY(p_hid); - if (p_hid->ep_out == 0) - { + if (p_hid->ep_out == 0) { // This HID does not have an out endpoint (other than control) return false; - } - else if (len > CFG_TUH_HID_EPOUT_BUFSIZE || - (report_id != 0 && len > (CFG_TUH_HID_EPOUT_BUFSIZE - 1))) - { + } else if (len > CFG_TUH_HID_EPOUT_BUFSIZE || + (report_id != 0 && len > (CFG_TUH_HID_EPOUT_BUFSIZE - 1))) { // ep_out buffer is not large enough to hold contents return false; } // claim endpoint - TU_VERIFY( usbh_edpt_claim(daddr, p_hid->ep_out) ); + TU_VERIFY(usbh_edpt_claim(daddr, p_hid->ep_out)); - if (report_id == 0) - { + if (report_id == 0) { // No report ID in transmission memcpy(&p_hid->epout_buf[0], report, len); - } - else - { + } else { p_hid->epout_buf[0] = report_id; memcpy(&p_hid->epout_buf[1], report, len); ++len; // 1 more byte for report_id @@ -413,8 +405,7 @@ bool tuh_hid_send_report(uint8_t daddr, uint8_t idx, uint8_t report_id, const vo TU_LOG3_MEM(p_hid->epout_buf, len, 2); - if ( !usbh_edpt_xfer(daddr, p_hid->ep_out, p_hid->epout_buf, len) ) - { + if (!usbh_edpt_xfer(daddr, p_hid->ep_out, p_hid->epout_buf, len)) { usbh_edpt_release(daddr, p_hid->ep_out); return false; } @@ -425,13 +416,17 @@ bool tuh_hid_send_report(uint8_t daddr, uint8_t idx, uint8_t report_id, const vo //--------------------------------------------------------------------+ // USBH API //--------------------------------------------------------------------+ -void hidh_init(void) -{ +bool hidh_init(void) { + TU_LOG_DRV("sizeof(hidh_interface_t) = %u\r\n", sizeof(hidh_interface_t)); tu_memclr(_hidh_itf, sizeof(_hidh_itf)); + return true; } -bool hidh_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) -{ +bool hidh_deinit(void) { + return true; +} + +bool hidh_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { (void) result; uint8_t const dir = tu_edpt_dir(ep_addr); @@ -440,29 +435,26 @@ bool hidh_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t result, uint32_t hidh_interface_t* p_hid = get_hid_itf(daddr, idx); TU_VERIFY(p_hid); - if ( dir == TUSB_DIR_IN ) - { - // TU_LOG_DRV(" Get Report callback (%u, %u)\r\n", daddr, idx); + if (dir == TUSB_DIR_IN) { + TU_LOG_DRV(" Get Report callback (%u, %u)\r\n", daddr, idx); TU_LOG3_MEM(p_hid->epin_buf, xferred_bytes, 2); tuh_hid_report_received_cb(daddr, idx, p_hid->epin_buf, (uint16_t) xferred_bytes); - }else - { - if (tuh_hid_report_sent_cb) tuh_hid_report_sent_cb(daddr, idx, p_hid->epout_buf, (uint16_t) xferred_bytes); + } else { + if (tuh_hid_report_sent_cb) { + tuh_hid_report_sent_cb(daddr, idx, p_hid->epout_buf, (uint16_t) xferred_bytes); + } } return true; } -void hidh_close(uint8_t daddr) -{ - for(uint8_t i=0; idaddr == daddr) - { + if (p_hid->daddr == daddr) { TU_LOG_DRV(" HIDh close addr = %u index = %u\r\n", daddr, i); - if(tuh_hid_umount_cb) tuh_hid_umount_cb(daddr, i); - p_hid->daddr = 0; + if (tuh_hid_umount_cb) tuh_hid_umount_cb(daddr, i); + tu_memclr(p_hid, sizeof(hidh_interface_t)); } } } @@ -471,25 +463,22 @@ void hidh_close(uint8_t daddr) // Enumeration //--------------------------------------------------------------------+ -bool hidh_open(uint8_t rhport, uint8_t daddr, tusb_desc_interface_t const *desc_itf, uint16_t max_len) -{ +bool hidh_open(uint8_t rhport, uint8_t daddr, tusb_desc_interface_t const* desc_itf, uint16_t max_len) { (void) rhport; (void) max_len; TU_VERIFY(TUSB_CLASS_HID == desc_itf->bInterfaceClass); - TU_LOG_DRV("[%u] HID opening Interface %u\r\n", daddr, desc_itf->bInterfaceNumber); // len = interface + hid + n*endpoints uint16_t const drv_len = (uint16_t) (sizeof(tusb_desc_interface_t) + sizeof(tusb_hid_descriptor_hid_t) + desc_itf->bNumEndpoints * sizeof(tusb_desc_endpoint_t)); TU_ASSERT(max_len >= drv_len); - - uint8_t const *p_desc = (uint8_t const *) desc_itf; + uint8_t const* p_desc = (uint8_t const*) desc_itf; //------------- HID descriptor -------------// p_desc = tu_desc_next(p_desc); - tusb_hid_descriptor_hid_t const *desc_hid = (tusb_hid_descriptor_hid_t const *) p_desc; + tusb_hid_descriptor_hid_t const* desc_hid = (tusb_hid_descriptor_hid_t const*) p_desc; TU_ASSERT(HID_DESC_TYPE_HID == desc_hid->bDescriptorType); hidh_interface_t* p_hid = find_new_itf(); @@ -498,38 +487,33 @@ bool hidh_open(uint8_t rhport, uint8_t daddr, tusb_desc_interface_t const *desc_ //------------- Endpoint Descriptors -------------// p_desc = tu_desc_next(p_desc); - tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc; + tusb_desc_endpoint_t const* desc_ep = (tusb_desc_endpoint_t const*) p_desc; - for(int i = 0; i < desc_itf->bNumEndpoints; i++) - { + for (int i = 0; i < desc_itf->bNumEndpoints; i++) { TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType); - TU_ASSERT( tuh_edpt_open(daddr, desc_ep) ); + TU_ASSERT(tuh_edpt_open(daddr, desc_ep)); - if(tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) - { - p_hid->ep_in = desc_ep->bEndpointAddress; + if (tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN) { + p_hid->ep_in = desc_ep->bEndpointAddress; p_hid->epin_size = tu_edpt_packet_size(desc_ep); - } - else - { - p_hid->ep_out = desc_ep->bEndpointAddress; + } else { + p_hid->ep_out = desc_ep->bEndpointAddress; p_hid->epout_size = tu_edpt_packet_size(desc_ep); } p_desc = tu_desc_next(p_desc); - desc_ep = (tusb_desc_endpoint_t const *) p_desc; + desc_ep = (tusb_desc_endpoint_t const*) p_desc; } - p_hid->itf_num = desc_itf->bInterfaceNumber; + p_hid->itf_num = desc_itf->bInterfaceNumber; // Assume bNumDescriptors = 1 p_hid->report_desc_type = desc_hid->bReportType; - p_hid->report_desc_len = tu_unaligned_read16(&desc_hid->wReportLength); + p_hid->report_desc_len = tu_unaligned_read16(&desc_hid->wReportLength); // Per HID Specs: default is Report protocol, though we will force Boot protocol when set_config p_hid->protocol_mode = _hidh_default_protocol; - if ( HID_SUBCLASS_BOOT == desc_itf->bInterfaceSubClass ) - { + if (HID_SUBCLASS_BOOT == desc_itf->bInterfaceSubClass) { p_hid->itf_protocol = desc_itf->bInterfaceProtocol; } @@ -550,15 +534,14 @@ enum { static void config_driver_mount_complete(uint8_t daddr, uint8_t idx, uint8_t const* desc_report, uint16_t desc_len); static void process_set_config(tuh_xfer_t* xfer); -bool hidh_set_config(uint8_t daddr, uint8_t itf_num) -{ +bool hidh_set_config(uint8_t daddr, uint8_t itf_num) { tusb_control_request_t request; request.wIndex = tu_htole16((uint16_t) itf_num); tuh_xfer_t xfer; - xfer.daddr = daddr; - xfer.result = XFER_RESULT_SUCCESS; - xfer.setup = &request; + xfer.daddr = daddr; + xfer.result = XFER_RESULT_SUCCESS; + xfer.setup = &request; xfer.user_data = CONFG_SET_IDLE; // fake request to kick-off the set config process @@ -567,71 +550,68 @@ bool hidh_set_config(uint8_t daddr, uint8_t itf_num) return true; } -static void process_set_config(tuh_xfer_t* xfer) -{ +static void process_set_config(tuh_xfer_t* xfer) { // Stall is a valid response for SET_IDLE, sometime SET_PROTOCOL as well // therefore we could ignore its result - if ( !(xfer->setup->bRequest == HID_REQ_CONTROL_SET_IDLE || - xfer->setup->bRequest == HID_REQ_CONTROL_SET_PROTOCOL) ) - { - TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS, ); + if (!(xfer->setup->bRequest == HID_REQ_CONTROL_SET_IDLE || + xfer->setup->bRequest == HID_REQ_CONTROL_SET_PROTOCOL)) { + TU_ASSERT(xfer->result == XFER_RESULT_SUCCESS,); } uintptr_t const state = xfer->user_data; uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex); - uint8_t const daddr = xfer->daddr; + uint8_t const daddr = xfer->daddr; - uint8_t const idx = tuh_hid_itf_get_index(daddr, itf_num); + uint8_t const idx = tuh_hid_itf_get_index(daddr, itf_num); hidh_interface_t* p_hid = get_hid_itf(daddr, idx); - TU_VERIFY(p_hid, ); + TU_VERIFY(p_hid,); - switch(state) - { - case CONFG_SET_IDLE: - { + switch (state) { + case CONFG_SET_IDLE: { // Idle rate = 0 mean only report when there is changes const uint16_t idle_rate = 0; - const uintptr_t next_state = (p_hid->itf_protocol != HID_ITF_PROTOCOL_NONE) ? CONFIG_SET_PROTOCOL : CONFIG_GET_REPORT_DESC; + const uintptr_t next_state = (p_hid->itf_protocol != HID_ITF_PROTOCOL_NONE) + ? CONFIG_SET_PROTOCOL : CONFIG_GET_REPORT_DESC; _hidh_set_idle(daddr, itf_num, idle_rate, process_set_config, next_state); + break; } - break; case CONFIG_SET_PROTOCOL: _hidh_set_protocol(daddr, p_hid->itf_num, _hidh_default_protocol, process_set_config, CONFIG_GET_REPORT_DESC); - break; + break; case CONFIG_GET_REPORT_DESC: // Get Report Descriptor if possible // using usbh enumeration buffer since report descriptor can be very long - if( p_hid->report_desc_len > CFG_TUH_ENUMERATION_BUFSIZE ) - { + if (p_hid->report_desc_len > CFG_TUH_ENUMERATION_BUFSIZE) { TU_LOG_DRV("HID Skip Report Descriptor since it is too large %u bytes\r\n", p_hid->report_desc_len); // Driver is mounted without report descriptor config_driver_mount_complete(daddr, idx, NULL, 0); - }else - { - tuh_descriptor_get_hid_report(daddr, itf_num, p_hid->report_desc_type, 0, usbh_get_enum_buf(), p_hid->report_desc_len, process_set_config, CONFIG_COMPLETE); + } else { + tuh_descriptor_get_hid_report(daddr, itf_num, p_hid->report_desc_type, 0, + usbh_get_enum_buf(), p_hid->report_desc_len, + process_set_config, CONFIG_COMPLETE); } break; - case CONFIG_COMPLETE: - { + case CONFIG_COMPLETE: { uint8_t const* desc_report = usbh_get_enum_buf(); - uint16_t const desc_len = tu_le16toh(xfer->setup->wLength); + uint16_t const desc_len = tu_le16toh(xfer->setup->wLength); config_driver_mount_complete(daddr, idx, desc_report, desc_len); + break; } - break; - default: break; + default: + break; } } -static void config_driver_mount_complete(uint8_t daddr, uint8_t idx, uint8_t const* desc_report, uint16_t desc_len) -{ +static void config_driver_mount_complete(uint8_t daddr, uint8_t idx, uint8_t const* desc_report, uint16_t desc_len) { hidh_interface_t* p_hid = get_hid_itf(daddr, idx); - TU_VERIFY(p_hid, ); + TU_VERIFY(p_hid,); + p_hid->mounted = true; // enumeration is complete if (tuh_hid_mount_cb) tuh_hid_mount_cb(daddr, idx, desc_report, desc_len); @@ -644,21 +624,19 @@ static void config_driver_mount_complete(uint8_t daddr, uint8_t idx, uint8_t con // Report Descriptor Parser //--------------------------------------------------------------------+ -uint8_t tuh_hid_parse_report_descriptor(tuh_hid_report_info_t* report_info_arr, uint8_t arr_count, uint8_t const* desc_report, uint16_t desc_len) -{ +uint8_t tuh_hid_parse_report_descriptor(tuh_hid_report_info_t* report_info_arr, uint8_t arr_count, + uint8_t const* desc_report, uint16_t desc_len) { // Report Item 6.2.2.2 USB HID 1.11 - union TU_ATTR_PACKED - { + union TU_ATTR_PACKED { uint8_t byte; - struct TU_ATTR_PACKED - { - uint8_t size : 2; - uint8_t type : 2; - uint8_t tag : 4; + struct TU_ATTR_PACKED { + uint8_t size : 2; + uint8_t type : 2; + uint8_t tag : 4; }; } header; - tu_memclr(report_info_arr, arr_count*sizeof(tuh_hid_report_info_t)); + tu_memclr(report_info_arr, arr_count * sizeof(tuh_hid_report_info_t)); uint8_t report_num = 0; tuh_hid_report_info_t* info = report_info_arr; @@ -668,114 +646,105 @@ uint8_t tuh_hid_parse_report_descriptor(tuh_hid_report_info_t* report_info_arr, // uint8_t ri_report_size = 0; uint8_t ri_collection_depth = 0; - - while(desc_len && report_num < arr_count) - { + while (desc_len && report_num < arr_count) { header.byte = *desc_report++; desc_len--; - uint8_t const tag = header.tag; + uint8_t const tag = header.tag; uint8_t const type = header.type; uint8_t const size = header.size; uint8_t const data8 = desc_report[0]; TU_LOG(3, "tag = %d, type = %d, size = %d, data = ", tag, type, size); - for(uint32_t i=0; iusage_page, desc_report, size); - break; + if (ri_collection_depth == 0) memcpy(&info->usage_page, desc_report, size); + break; - case RI_GLOBAL_LOGICAL_MIN : break; - case RI_GLOBAL_LOGICAL_MAX : break; - case RI_GLOBAL_PHYSICAL_MIN : break; - case RI_GLOBAL_PHYSICAL_MAX : break; + case RI_GLOBAL_LOGICAL_MIN: break; + case RI_GLOBAL_LOGICAL_MAX: break; + case RI_GLOBAL_PHYSICAL_MIN: break; + case RI_GLOBAL_PHYSICAL_MAX: break; case RI_GLOBAL_REPORT_ID: info->report_id = data8; - break; + break; case RI_GLOBAL_REPORT_SIZE: // ri_report_size = data8; - break; + break; case RI_GLOBAL_REPORT_COUNT: // ri_report_count = data8; - break; + break; - case RI_GLOBAL_UNIT_EXPONENT : break; - case RI_GLOBAL_UNIT : break; - case RI_GLOBAL_PUSH : break; - case RI_GLOBAL_POP : break; + case RI_GLOBAL_UNIT_EXPONENT: break; + case RI_GLOBAL_UNIT: break; + case RI_GLOBAL_PUSH: break; + case RI_GLOBAL_POP: break; default: break; } - break; + break; case RI_TYPE_LOCAL: - switch(tag) - { + switch (tag) { case RI_LOCAL_USAGE: // only take in account the "usage" before starting REPORT ID - if ( ri_collection_depth == 0 ) info->usage = data8; - break; + if (ri_collection_depth == 0) info->usage = data8; + break; - case RI_LOCAL_USAGE_MIN : break; - case RI_LOCAL_USAGE_MAX : break; - case RI_LOCAL_DESIGNATOR_INDEX : break; - case RI_LOCAL_DESIGNATOR_MIN : break; - case RI_LOCAL_DESIGNATOR_MAX : break; - case RI_LOCAL_STRING_INDEX : break; - case RI_LOCAL_STRING_MIN : break; - case RI_LOCAL_STRING_MAX : break; - case RI_LOCAL_DELIMITER : break; + case RI_LOCAL_USAGE_MIN: break; + case RI_LOCAL_USAGE_MAX: break; + case RI_LOCAL_DESIGNATOR_INDEX: break; + case RI_LOCAL_DESIGNATOR_MIN: break; + case RI_LOCAL_DESIGNATOR_MAX: break; + case RI_LOCAL_STRING_INDEX: break; + case RI_LOCAL_STRING_MIN: break; + case RI_LOCAL_STRING_MAX: break; + case RI_LOCAL_DELIMITER: break; default: break; } - break; + break; - // error + // error default: break; } desc_report += size; - desc_len -= size; + desc_len -= size; } - for ( uint8_t i = 0; i < report_num; i++ ) - { - info = report_info_arr+i; + for (uint8_t i = 0; i < report_num; i++) { + info = report_info_arr + i; TU_LOG_DRV("%u: id = %u, usage_page = %u, usage = %u\r\n", i, info->report_id, info->usage_page, info->usage); } diff --git a/pico-sdk/lib/tinyusb/src/class/hid/hid_host.h b/pico-sdk/lib/tinyusb/src/class/hid/hid_host.h index 238b7c6..9681c70 100644 --- a/pico-sdk/lib/tinyusb/src/class/hid/hid_host.h +++ b/pico-sdk/lib/tinyusb/src/class/hid/hid_host.h @@ -30,7 +30,7 @@ #include "hid.h" #ifdef __cplusplus - extern "C" { +extern "C" { #endif //--------------------------------------------------------------------+ @@ -47,10 +47,9 @@ #endif -typedef struct -{ - uint8_t report_id; - uint8_t usage; +typedef struct { + uint8_t report_id; + uint8_t usage; uint16_t usage_page; // TODO still use the endpoint size for now @@ -86,7 +85,8 @@ bool tuh_hid_mounted(uint8_t dev_addr, uint8_t idx); // Parse report descriptor into array of report_info struct and return number of reports. // For complicated report, application should write its own parser. -uint8_t tuh_hid_parse_report_descriptor(tuh_hid_report_info_t* reports_info_arr, uint8_t arr_count, uint8_t const* desc_report, uint16_t desc_len) TU_ATTR_UNUSED; +TU_ATTR_UNUSED uint8_t tuh_hid_parse_report_descriptor(tuh_hid_report_info_t* reports_info_arr, uint8_t arr_count, + uint8_t const* desc_report, uint16_t desc_len); //--------------------------------------------------------------------+ // Control Endpoint API @@ -105,9 +105,14 @@ void tuh_hid_set_default_protocol(uint8_t protocol); // This function is only supported by Boot interface (tuh_n_hid_interface_protocol() != NONE) bool tuh_hid_set_protocol(uint8_t dev_addr, uint8_t idx, uint8_t protocol); +// Get Report using control endpoint +// report_type is either Input, Output or Feature, (value from hid_report_type_t) +bool tuh_hid_get_report(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type, void* report, uint16_t len); + // Set Report using control endpoint // report_type is either Input, Output or Feature, (value from hid_report_type_t) -bool tuh_hid_set_report(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type, void* report, uint16_t len); +bool tuh_hid_set_report(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type, + void* report, uint16_t len); //--------------------------------------------------------------------+ // Interrupt Endpoint API @@ -121,6 +126,9 @@ bool tuh_hid_receive_ready(uint8_t dev_addr, uint8_t idx); // - false if failed to queue the transfer e.g endpoint is busy bool tuh_hid_receive_report(uint8_t dev_addr, uint8_t idx); +// Abort receiving report on Interrupt Endpoint +bool tuh_hid_receive_abort(uint8_t dev_addr, uint8_t idx); + // Check if HID interface is ready to send report bool tuh_hid_send_ready(uint8_t dev_addr, uint8_t idx); @@ -149,6 +157,10 @@ void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t idx, uint8_t const* re // Invoked when sent report to device successfully via interrupt endpoint TU_ATTR_WEAK void tuh_hid_report_sent_cb(uint8_t dev_addr, uint8_t idx, uint8_t const* report, uint16_t len); +// Invoked when Get Report to device via either control endpoint +// len = 0 indicate there is error in the transfer e.g stalled response +TU_ATTR_WEAK void tuh_hid_get_report_complete_cb(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type, uint16_t len); + // Invoked when Sent Report to device via either control endpoint // len = 0 indicate there is error in the transfer e.g stalled response TU_ATTR_WEAK void tuh_hid_set_report_complete_cb(uint8_t dev_addr, uint8_t idx, uint8_t report_id, uint8_t report_type, uint16_t len); @@ -159,11 +171,12 @@ TU_ATTR_WEAK void tuh_hid_set_protocol_complete_cb(uint8_t dev_addr, uint8_t idx //--------------------------------------------------------------------+ // Internal Class Driver API //--------------------------------------------------------------------+ -void hidh_init (void); -bool hidh_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len); -bool hidh_set_config (uint8_t dev_addr, uint8_t itf_num); -bool hidh_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); -void hidh_close (uint8_t dev_addr); +bool hidh_init(void); +bool hidh_deinit(void); +bool hidh_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const* desc_itf, uint16_t max_len); +bool hidh_set_config(uint8_t dev_addr, uint8_t itf_num); +bool hidh_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); +void hidh_close(uint8_t dev_addr); #ifdef __cplusplus } diff --git a/pico-sdk/lib/tinyusb/src/class/msc/msc_device.c b/pico-sdk/lib/tinyusb/src/class/msc/msc_device.c index 2589dcd..447560b 100644 --- a/pico-sdk/lib/tinyusb/src/class/msc/msc_device.c +++ b/pico-sdk/lib/tinyusb/src/class/msc/msc_device.c @@ -203,7 +203,7 @@ uint8_t rdwr10_validate_cmd(msc_cbw_t const* cbw) //--------------------------------------------------------------------+ // Debug //--------------------------------------------------------------------+ -#if CFG_TUSB_DEBUG >= 2 +#if CFG_TUSB_DEBUG >= CFG_TUD_MSC_LOG_LEVEL TU_ATTR_UNUSED tu_static tu_lookup_entry_t const _msc_scsi_cmd_lookup[] = { @@ -251,11 +251,15 @@ static inline void set_sense_medium_not_present(uint8_t lun) //--------------------------------------------------------------------+ // USBD Driver API //--------------------------------------------------------------------+ -void mscd_init(void) -{ +void mscd_init(void) { tu_memclr(&_mscd_itf, sizeof(mscd_interface_t)); } +bool mscd_deinit(void) { + // nothing to do + return true; +} + void mscd_reset(uint8_t rhport) { (void) rhport; @@ -685,6 +689,24 @@ static int32_t proc_builtin_scsi(uint8_t lun, uint8_t const scsi_cmd[16], uint8_ } break; + case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL: + resplen = 0; + + if (tud_msc_prevent_allow_medium_removal_cb) + { + scsi_prevent_allow_medium_removal_t const * prevent_allow = (scsi_prevent_allow_medium_removal_t const *) scsi_cmd; + if ( !tud_msc_prevent_allow_medium_removal_cb(lun, prevent_allow->prohibit_removal, prevent_allow->control) ) + { + // Failed status response + resplen = - 1; + + // set default sense if not set by callback + if ( p_msc->sense_key == 0 ) set_sense_medium_not_present(lun); + } + } + break; + + case SCSI_CMD_READ_CAPACITY_10: { uint32_t block_count; diff --git a/pico-sdk/lib/tinyusb/src/class/msc/msc_device.h b/pico-sdk/lib/tinyusb/src/class/msc/msc_device.h index 72f95be..29acd28 100644 --- a/pico-sdk/lib/tinyusb/src/class/msc/msc_device.h +++ b/pico-sdk/lib/tinyusb/src/class/msc/msc_device.h @@ -131,6 +131,9 @@ TU_ATTR_WEAK uint8_t tud_msc_get_maxlun_cb(void); // - Start = 1 : active mode, if load_eject = 1 : load disk storage TU_ATTR_WEAK bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject); +//Invoked when we receive the Prevent / Allow Medium Removal command +TU_ATTR_WEAK bool tud_msc_prevent_allow_medium_removal_cb(uint8_t lun, uint8_t prohibit_removal, uint8_t control); + // Invoked when received REQUEST_SENSE TU_ATTR_WEAK int32_t tud_msc_request_sense_cb(uint8_t lun, void* buffer, uint16_t bufsize); @@ -150,6 +153,7 @@ TU_ATTR_WEAK bool tud_msc_is_writable_cb(uint8_t lun); // Internal Class Driver API //--------------------------------------------------------------------+ void mscd_init (void); +bool mscd_deinit (void); void mscd_reset (uint8_t rhport); uint16_t mscd_open (uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len); bool mscd_control_xfer_cb (uint8_t rhport, uint8_t stage, tusb_control_request_t const * p_request); diff --git a/pico-sdk/lib/tinyusb/src/class/msc/msc_host.c b/pico-sdk/lib/tinyusb/src/class/msc/msc_host.c deleted file mode 100644 index 39f2d9f..0000000 --- a/pico-sdk/lib/tinyusb/src/class/msc/msc_host.c +++ /dev/null @@ -1,497 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach (tinyusb.org) - * - * 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. - * - * This file is part of the TinyUSB stack. - */ - -#include "tusb_option.h" - -#if CFG_TUH_ENABLED && CFG_TUH_MSC - -#include "host/usbh.h" -#include "host/usbh_pvt.h" - -#include "msc_host.h" - -// Level where CFG_TUSB_DEBUG must be at least for this driver is logged -#ifndef CFG_TUH_MSC_LOG_LEVEL - #define CFG_TUH_MSC_LOG_LEVEL CFG_TUH_LOG_LEVEL -#endif - -#define TU_LOG_DRV(...) TU_LOG(CFG_TUH_MSC_LOG_LEVEL, __VA_ARGS__) - -//--------------------------------------------------------------------+ -// MACRO CONSTANT TYPEDEF -//--------------------------------------------------------------------+ -enum { - MSC_STAGE_IDLE = 0, - MSC_STAGE_CMD, - MSC_STAGE_DATA, - MSC_STAGE_STATUS, -}; - -typedef struct { - uint8_t itf_num; - uint8_t ep_in; - uint8_t ep_out; - - uint8_t max_lun; - - volatile bool configured; // Receive SET_CONFIGURE - volatile bool mounted; // Enumeration is complete - - struct { - uint32_t block_size; - uint32_t block_count; - } capacity[CFG_TUH_MSC_MAXLUN]; - - //------------- SCSI -------------// - uint8_t stage; - void* buffer; - tuh_msc_complete_cb_t complete_cb; - uintptr_t complete_arg; - - CFG_TUH_MEM_ALIGN msc_cbw_t cbw; - CFG_TUH_MEM_ALIGN msc_csw_t csw; -} msch_interface_t; - -CFG_TUH_MEM_SECTION static msch_interface_t _msch_itf[CFG_TUH_DEVICE_MAX]; - -// buffer used to read scsi information when mounted -// largest response data currently is inquiry TODO Inquiry is not part of enum anymore -CFG_TUH_MEM_SECTION CFG_TUH_MEM_ALIGN -static uint8_t _msch_buffer[sizeof(scsi_inquiry_resp_t)]; - -// FIXME potential nul reference -TU_ATTR_ALWAYS_INLINE -static inline msch_interface_t* get_itf(uint8_t dev_addr) { - return &_msch_itf[dev_addr - 1]; -} - -//--------------------------------------------------------------------+ -// PUBLIC API -//--------------------------------------------------------------------+ -uint8_t tuh_msc_get_maxlun(uint8_t dev_addr) { - msch_interface_t* p_msc = get_itf(dev_addr); - return p_msc->max_lun; -} - -uint32_t tuh_msc_get_block_count(uint8_t dev_addr, uint8_t lun) { - msch_interface_t* p_msc = get_itf(dev_addr); - return p_msc->capacity[lun].block_count; -} - -uint32_t tuh_msc_get_block_size(uint8_t dev_addr, uint8_t lun) { - msch_interface_t* p_msc = get_itf(dev_addr); - return p_msc->capacity[lun].block_size; -} - -bool tuh_msc_mounted(uint8_t dev_addr) { - msch_interface_t* p_msc = get_itf(dev_addr); - return p_msc->mounted; -} - -bool tuh_msc_ready(uint8_t dev_addr) { - msch_interface_t* p_msc = get_itf(dev_addr); - return p_msc->mounted && !usbh_edpt_busy(dev_addr, p_msc->ep_in) && !usbh_edpt_busy(dev_addr, p_msc->ep_out); -} - -//--------------------------------------------------------------------+ -// PUBLIC API: SCSI COMMAND -//--------------------------------------------------------------------+ -static inline void cbw_init(msc_cbw_t* cbw, uint8_t lun) { - tu_memclr(cbw, sizeof(msc_cbw_t)); - cbw->signature = MSC_CBW_SIGNATURE; - cbw->tag = 0x54555342; // TUSB - cbw->lun = lun; -} - -bool tuh_msc_scsi_command(uint8_t daddr, msc_cbw_t const* cbw, void* data, - tuh_msc_complete_cb_t complete_cb, uintptr_t arg) { - msch_interface_t* p_msc = get_itf(daddr); - TU_VERIFY(p_msc->configured); - - // claim endpoint - TU_VERIFY(usbh_edpt_claim(daddr, p_msc->ep_out)); - - p_msc->cbw = *cbw; - p_msc->stage = MSC_STAGE_CMD; - p_msc->buffer = data; - p_msc->complete_cb = complete_cb; - p_msc->complete_arg = arg; - - if (!usbh_edpt_xfer(daddr, p_msc->ep_out, (uint8_t*) &p_msc->cbw, sizeof(msc_cbw_t))) { - usbh_edpt_release(daddr, p_msc->ep_out); - return false; - } - - return true; -} - -bool tuh_msc_read_capacity(uint8_t dev_addr, uint8_t lun, scsi_read_capacity10_resp_t* response, - tuh_msc_complete_cb_t complete_cb, uintptr_t arg) { - msch_interface_t* p_msc = get_itf(dev_addr); - TU_VERIFY(p_msc->configured); - - msc_cbw_t cbw; - cbw_init(&cbw, lun); - - cbw.total_bytes = sizeof(scsi_read_capacity10_resp_t); - cbw.dir = TUSB_DIR_IN_MASK; - cbw.cmd_len = sizeof(scsi_read_capacity10_t); - cbw.command[0] = SCSI_CMD_READ_CAPACITY_10; - - return tuh_msc_scsi_command(dev_addr, &cbw, response, complete_cb, arg); -} - -bool tuh_msc_inquiry(uint8_t dev_addr, uint8_t lun, scsi_inquiry_resp_t* response, - tuh_msc_complete_cb_t complete_cb, uintptr_t arg) { - msch_interface_t* p_msc = get_itf(dev_addr); - TU_VERIFY(p_msc->mounted); - - msc_cbw_t cbw; - cbw_init(&cbw, lun); - - cbw.total_bytes = sizeof(scsi_inquiry_resp_t); - cbw.dir = TUSB_DIR_IN_MASK; - cbw.cmd_len = sizeof(scsi_inquiry_t); - - scsi_inquiry_t const cmd_inquiry = { - .cmd_code = SCSI_CMD_INQUIRY, - .alloc_length = sizeof(scsi_inquiry_resp_t) - }; - memcpy(cbw.command, &cmd_inquiry, cbw.cmd_len); - - return tuh_msc_scsi_command(dev_addr, &cbw, response, complete_cb, arg); -} - -bool tuh_msc_test_unit_ready(uint8_t dev_addr, uint8_t lun, tuh_msc_complete_cb_t complete_cb, uintptr_t arg) { - msch_interface_t* p_msc = get_itf(dev_addr); - TU_VERIFY(p_msc->configured); - - msc_cbw_t cbw; - cbw_init(&cbw, lun); - - cbw.total_bytes = 0; - cbw.dir = TUSB_DIR_OUT; - cbw.cmd_len = sizeof(scsi_test_unit_ready_t); - cbw.command[0] = SCSI_CMD_TEST_UNIT_READY; - cbw.command[1] = lun; // according to wiki TODO need verification - - return tuh_msc_scsi_command(dev_addr, &cbw, NULL, complete_cb, arg); -} - -bool tuh_msc_request_sense(uint8_t dev_addr, uint8_t lun, void* response, - tuh_msc_complete_cb_t complete_cb, uintptr_t arg) { - msc_cbw_t cbw; - cbw_init(&cbw, lun); - - cbw.total_bytes = 18; // TODO sense response - cbw.dir = TUSB_DIR_IN_MASK; - cbw.cmd_len = sizeof(scsi_request_sense_t); - - scsi_request_sense_t const cmd_request_sense = { - .cmd_code = SCSI_CMD_REQUEST_SENSE, - .alloc_length = 18 - }; - memcpy(cbw.command, &cmd_request_sense, cbw.cmd_len); - - return tuh_msc_scsi_command(dev_addr, &cbw, response, complete_cb, arg); -} - -bool tuh_msc_read10(uint8_t dev_addr, uint8_t lun, void* buffer, uint32_t lba, uint16_t block_count, - tuh_msc_complete_cb_t complete_cb, uintptr_t arg) { - msch_interface_t* p_msc = get_itf(dev_addr); - TU_VERIFY(p_msc->mounted); - - msc_cbw_t cbw; - cbw_init(&cbw, lun); - - cbw.total_bytes = block_count * p_msc->capacity[lun].block_size; - cbw.dir = TUSB_DIR_IN_MASK; - cbw.cmd_len = sizeof(scsi_read10_t); - - scsi_read10_t const cmd_read10 = { - .cmd_code = SCSI_CMD_READ_10, - .lba = tu_htonl(lba), - .block_count = tu_htons(block_count) - }; - memcpy(cbw.command, &cmd_read10, cbw.cmd_len); - - return tuh_msc_scsi_command(dev_addr, &cbw, buffer, complete_cb, arg); -} - -bool tuh_msc_write10(uint8_t dev_addr, uint8_t lun, void const* buffer, uint32_t lba, uint16_t block_count, - tuh_msc_complete_cb_t complete_cb, uintptr_t arg) { - msch_interface_t* p_msc = get_itf(dev_addr); - TU_VERIFY(p_msc->mounted); - - msc_cbw_t cbw; - cbw_init(&cbw, lun); - - cbw.total_bytes = block_count * p_msc->capacity[lun].block_size; - cbw.dir = TUSB_DIR_OUT; - cbw.cmd_len = sizeof(scsi_write10_t); - - scsi_write10_t const cmd_write10 = { - .cmd_code = SCSI_CMD_WRITE_10, - .lba = tu_htonl(lba), - .block_count = tu_htons(block_count) - }; - memcpy(cbw.command, &cmd_write10, cbw.cmd_len); - - return tuh_msc_scsi_command(dev_addr, &cbw, (void*) (uintptr_t) buffer, complete_cb, arg); -} - -#if 0 -// MSC interface Reset (not used now) -bool tuh_msc_reset(uint8_t dev_addr) { - tusb_control_request_t const new_request = { - .bmRequestType_bit = { - .recipient = TUSB_REQ_RCPT_INTERFACE, - .type = TUSB_REQ_TYPE_CLASS, - .direction = TUSB_DIR_OUT - }, - .bRequest = MSC_REQ_RESET, - .wValue = 0, - .wIndex = p_msc->itf_num, - .wLength = 0 - }; - TU_ASSERT( usbh_control_xfer( dev_addr, &new_request, NULL ) ); -} -#endif - -//--------------------------------------------------------------------+ -// CLASS-USBH API -//--------------------------------------------------------------------+ -void msch_init(void) { - tu_memclr(_msch_itf, sizeof(_msch_itf)); -} - -void msch_close(uint8_t dev_addr) { - TU_VERIFY(dev_addr <= CFG_TUH_DEVICE_MAX,); - msch_interface_t* p_msc = get_itf(dev_addr); - TU_VERIFY(p_msc->configured,); - - TU_LOG_DRV(" MSCh close addr = %d\r\n", dev_addr); - - // invoke Application Callback - if (p_msc->mounted) { - if (tuh_msc_umount_cb) tuh_msc_umount_cb(dev_addr); - } - - tu_memclr(p_msc, sizeof(msch_interface_t)); -} - -bool msch_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes) { - msch_interface_t* p_msc = get_itf(dev_addr); - msc_cbw_t const * cbw = &p_msc->cbw; - msc_csw_t * csw = &p_msc->csw; - - switch (p_msc->stage) { - case MSC_STAGE_CMD: - // Must be Command Block - TU_ASSERT(ep_addr == p_msc->ep_out && event == XFER_RESULT_SUCCESS && xferred_bytes == sizeof(msc_cbw_t)); - - if (cbw->total_bytes && p_msc->buffer) { - // Data stage if any - p_msc->stage = MSC_STAGE_DATA; - uint8_t const ep_data = (cbw->dir & TUSB_DIR_IN_MASK) ? p_msc->ep_in : p_msc->ep_out; - TU_ASSERT(usbh_edpt_xfer(dev_addr, ep_data, p_msc->buffer, (uint16_t) cbw->total_bytes)); - } else { - // Status stage - p_msc->stage = MSC_STAGE_STATUS; - TU_ASSERT(usbh_edpt_xfer(dev_addr, p_msc->ep_in, (uint8_t*) &p_msc->csw, (uint16_t) sizeof(msc_csw_t))); - } - break; - - case MSC_STAGE_DATA: - // Status stage - p_msc->stage = MSC_STAGE_STATUS; - TU_ASSERT(usbh_edpt_xfer(dev_addr, p_msc->ep_in, (uint8_t*) &p_msc->csw, (uint16_t) sizeof(msc_csw_t))); - break; - - case MSC_STAGE_STATUS: - // SCSI op is complete - p_msc->stage = MSC_STAGE_IDLE; - - if (p_msc->complete_cb) { - tuh_msc_complete_data_t const cb_data = { - .cbw = cbw, - .csw = csw, - .scsi_data = p_msc->buffer, - .user_arg = p_msc->complete_arg - }; - p_msc->complete_cb(dev_addr, &cb_data); - } - break; - - // unknown state - default: - break; - } - - return true; -} - -//--------------------------------------------------------------------+ -// MSC Enumeration -//--------------------------------------------------------------------+ -static void config_get_maxlun_complete(tuh_xfer_t* xfer); -static bool config_test_unit_ready_complete(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data); -static bool config_request_sense_complete(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data); -static bool config_read_capacity_complete(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data); - -bool msch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const* desc_itf, uint16_t max_len) { - (void) rhport; - TU_VERIFY (MSC_SUBCLASS_SCSI == desc_itf->bInterfaceSubClass && - MSC_PROTOCOL_BOT == desc_itf->bInterfaceProtocol); - - // msc driver length is fixed - uint16_t const drv_len = (uint16_t) (sizeof(tusb_desc_interface_t) + - desc_itf->bNumEndpoints * sizeof(tusb_desc_endpoint_t)); - TU_ASSERT(drv_len <= max_len); - - msch_interface_t* p_msc = get_itf(dev_addr); - tusb_desc_endpoint_t const* ep_desc = (tusb_desc_endpoint_t const*) tu_desc_next(desc_itf); - - for (uint32_t i = 0; i < 2; i++) { - TU_ASSERT(TUSB_DESC_ENDPOINT == ep_desc->bDescriptorType && TUSB_XFER_BULK == ep_desc->bmAttributes.xfer); - TU_ASSERT(tuh_edpt_open(dev_addr, ep_desc)); - - if (TUSB_DIR_IN == tu_edpt_dir(ep_desc->bEndpointAddress)) { - p_msc->ep_in = ep_desc->bEndpointAddress; - } else { - p_msc->ep_out = ep_desc->bEndpointAddress; - } - - ep_desc = (tusb_desc_endpoint_t const*) tu_desc_next(ep_desc); - } - - p_msc->itf_num = desc_itf->bInterfaceNumber; - - return true; -} - -bool msch_set_config(uint8_t dev_addr, uint8_t itf_num) { - msch_interface_t* p_msc = get_itf(dev_addr); - TU_ASSERT(p_msc->itf_num == itf_num); - - p_msc->configured = true; - - //------------- Get Max Lun -------------// - TU_LOG_DRV("MSC Get Max Lun\r\n"); - tusb_control_request_t const request = { - .bmRequestType_bit = { - .recipient = TUSB_REQ_RCPT_INTERFACE, - .type = TUSB_REQ_TYPE_CLASS, - .direction = TUSB_DIR_IN - }, - .bRequest = MSC_REQ_GET_MAX_LUN, - .wValue = 0, - .wIndex = itf_num, - .wLength = 1 - }; - - tuh_xfer_t xfer = { - .daddr = dev_addr, - .ep_addr = 0, - .setup = &request, - .buffer = _msch_buffer, - .complete_cb = config_get_maxlun_complete, - .user_data = 0 - }; - TU_ASSERT(tuh_control_xfer(&xfer)); - - return true; -} - -static void config_get_maxlun_complete(tuh_xfer_t* xfer) { - uint8_t const daddr = xfer->daddr; - msch_interface_t* p_msc = get_itf(daddr); - - // STALL means zero - p_msc->max_lun = (XFER_RESULT_SUCCESS == xfer->result) ? _msch_buffer[0] : 0; - p_msc->max_lun++; // MAX LUN is minus 1 by specs - - TU_LOG_DRV(" Max LUN = %u\r\n", p_msc->max_lun); - - // TODO multiple LUN support - TU_LOG_DRV("SCSI Test Unit Ready\r\n"); - uint8_t const lun = 0; - tuh_msc_test_unit_ready(daddr, lun, config_test_unit_ready_complete, 0); -} - -static bool config_test_unit_ready_complete(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data) { - msc_cbw_t const* cbw = cb_data->cbw; - msc_csw_t const* csw = cb_data->csw; - - if (csw->status == 0) { - // Unit is ready, read its capacity - TU_LOG_DRV("SCSI Read Capacity\r\n"); - tuh_msc_read_capacity(dev_addr, cbw->lun, (scsi_read_capacity10_resp_t*) ((void*) _msch_buffer), - config_read_capacity_complete, 0); - } else { - // Note: During enumeration, some device fails Test Unit Ready and require a few retries - // with Request Sense to start working !! - // TODO limit number of retries - TU_LOG_DRV("SCSI Request Sense\r\n"); - TU_ASSERT(tuh_msc_request_sense(dev_addr, cbw->lun, _msch_buffer, config_request_sense_complete, 0)); - } - - return true; -} - -static bool config_request_sense_complete(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data) { - msc_cbw_t const* cbw = cb_data->cbw; - msc_csw_t const* csw = cb_data->csw; - - TU_ASSERT(csw->status == 0); - TU_ASSERT(tuh_msc_test_unit_ready(dev_addr, cbw->lun, config_test_unit_ready_complete, 0)); - return true; -} - -static bool config_read_capacity_complete(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data) { - msc_cbw_t const* cbw = cb_data->cbw; - msc_csw_t const* csw = cb_data->csw; - - TU_ASSERT(csw->status == 0); - - msch_interface_t* p_msc = get_itf(dev_addr); - - // Capacity response field: Block size and Last LBA are both Big-Endian - scsi_read_capacity10_resp_t* resp = (scsi_read_capacity10_resp_t*) ((void*) _msch_buffer); - p_msc->capacity[cbw->lun].block_count = tu_ntohl(resp->last_lba) + 1; - p_msc->capacity[cbw->lun].block_size = tu_ntohl(resp->block_size); - - // Mark enumeration is complete - p_msc->mounted = true; - if (tuh_msc_mount_cb) tuh_msc_mount_cb(dev_addr); - - // notify usbh that driver enumeration is complete - usbh_driver_set_config_complete(dev_addr, p_msc->itf_num); - - return true; -} - -#endif diff --git a/pico-sdk/lib/tinyusb/src/class/msc/msc_host.h b/pico-sdk/lib/tinyusb/src/class/msc/msc_host.h deleted file mode 100644 index 9ca1b47..0000000 --- a/pico-sdk/lib/tinyusb/src/class/msc/msc_host.h +++ /dev/null @@ -1,126 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach (tinyusb.org) - * - * 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. - * - * This file is part of the TinyUSB stack. - */ - -#ifndef TUSB_MSC_HOST_H_ -#define TUSB_MSC_HOST_H_ - -#include "msc.h" - -#ifdef __cplusplus - extern "C" { -#endif - -//--------------------------------------------------------------------+ -// Class Driver Configuration -//--------------------------------------------------------------------+ - -#ifndef CFG_TUH_MSC_MAXLUN -#define CFG_TUH_MSC_MAXLUN 4 -#endif - -typedef struct { - msc_cbw_t const* cbw; // SCSI command - msc_csw_t const* csw; // SCSI status - void* scsi_data; // SCSI Data - uintptr_t user_arg; // user argument -}tuh_msc_complete_data_t; - -typedef bool (*tuh_msc_complete_cb_t)(uint8_t dev_addr, tuh_msc_complete_data_t const* cb_data); - -//--------------------------------------------------------------------+ -// Application API -//--------------------------------------------------------------------+ - -// Check if device supports MassStorage interface. -// This function true after tuh_msc_mounted_cb() and false after tuh_msc_unmounted_cb() -bool tuh_msc_mounted(uint8_t dev_addr); - -// Check if the interface is currently ready or busy transferring data -bool tuh_msc_ready(uint8_t dev_addr); - -// Get Max Lun -uint8_t tuh_msc_get_maxlun(uint8_t dev_addr); - -// Get number of block -uint32_t tuh_msc_get_block_count(uint8_t dev_addr, uint8_t lun); - -// Get block size in bytes -uint32_t tuh_msc_get_block_size(uint8_t dev_addr, uint8_t lun); - -// Perform a full SCSI command (cbw, data, csw) in non-blocking manner. -// Complete callback is invoked when SCSI op is complete. -// return true if success, false if there is already pending operation. -bool tuh_msc_scsi_command(uint8_t daddr, msc_cbw_t const* cbw, void* data, tuh_msc_complete_cb_t complete_cb, uintptr_t arg); - -// Perform SCSI Inquiry command -// Complete callback is invoked when SCSI op is complete. -bool tuh_msc_inquiry(uint8_t dev_addr, uint8_t lun, scsi_inquiry_resp_t* response, tuh_msc_complete_cb_t complete_cb, uintptr_t arg); - -// Perform SCSI Test Unit Ready command -// Complete callback is invoked when SCSI op is complete. -bool tuh_msc_test_unit_ready(uint8_t dev_addr, uint8_t lun, tuh_msc_complete_cb_t complete_cb, uintptr_t arg); - -// Perform SCSI Request Sense 10 command -// Complete callback is invoked when SCSI op is complete. -bool tuh_msc_request_sense(uint8_t dev_addr, uint8_t lun, void *response, tuh_msc_complete_cb_t complete_cb, uintptr_t arg); - -// Perform SCSI Read 10 command. Read n blocks starting from LBA to buffer -// Complete callback is invoked when SCSI op is complete. -bool tuh_msc_read10(uint8_t dev_addr, uint8_t lun, void * buffer, uint32_t lba, uint16_t block_count, tuh_msc_complete_cb_t complete_cb, uintptr_t arg); - -// Perform SCSI Write 10 command. Write n blocks starting from LBA to device -// Complete callback is invoked when SCSI op is complete. -bool tuh_msc_write10(uint8_t dev_addr, uint8_t lun, void const * buffer, uint32_t lba, uint16_t block_count, tuh_msc_complete_cb_t complete_cb, uintptr_t arg); - -// Perform SCSI Read Capacity 10 command -// Complete callback is invoked when SCSI op is complete. -// Note: during enumeration, host stack already carried out this request. Application can retrieve capacity by -// simply call tuh_msc_get_block_count() and tuh_msc_get_block_size() -bool tuh_msc_read_capacity(uint8_t dev_addr, uint8_t lun, scsi_read_capacity10_resp_t* response, tuh_msc_complete_cb_t complete_cb, uintptr_t arg); - -//------------- Application Callback -------------// - -// Invoked when a device with MassStorage interface is mounted -TU_ATTR_WEAK void tuh_msc_mount_cb(uint8_t dev_addr); - -// Invoked when a device with MassStorage interface is unmounted -TU_ATTR_WEAK void tuh_msc_umount_cb(uint8_t dev_addr); - -//--------------------------------------------------------------------+ -// Internal Class Driver API -//--------------------------------------------------------------------+ - -void msch_init (void); -bool msch_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *desc_itf, uint16_t max_len); -bool msch_set_config (uint8_t dev_addr, uint8_t itf_num); -void msch_close (uint8_t dev_addr); -bool msch_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); - -#ifdef __cplusplus - } -#endif - -#endif diff --git a/pico-sdk/lib/tinyusb/src/class/vendor/vendor_device.c b/pico-sdk/lib/tinyusb/src/class/vendor/vendor_device.c deleted file mode 100644 index 389a296..0000000 --- a/pico-sdk/lib/tinyusb/src/class/vendor/vendor_device.c +++ /dev/null @@ -1,287 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach (tinyusb.org) - * - * 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. - * - * This file is part of the TinyUSB stack. - */ - -#include "tusb_option.h" - -#if (CFG_TUD_ENABLED && CFG_TUD_VENDOR) - -#include "device/usbd.h" -#include "device/usbd_pvt.h" - -#include "vendor_device.h" - -//--------------------------------------------------------------------+ -// MACRO CONSTANT TYPEDEF -//--------------------------------------------------------------------+ -typedef struct -{ - uint8_t itf_num; - uint8_t ep_in; - uint8_t ep_out; - - /*------------- From this point, data is not cleared by bus reset -------------*/ - tu_fifo_t rx_ff; - tu_fifo_t tx_ff; - - uint8_t rx_ff_buf[CFG_TUD_VENDOR_RX_BUFSIZE]; - uint8_t tx_ff_buf[CFG_TUD_VENDOR_TX_BUFSIZE]; - -#if CFG_FIFO_MUTEX - osal_mutex_def_t rx_ff_mutex; - osal_mutex_def_t tx_ff_mutex; -#endif - - // Endpoint Transfer buffer - CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_VENDOR_EPSIZE]; - CFG_TUSB_MEM_ALIGN uint8_t epin_buf[CFG_TUD_VENDOR_EPSIZE]; -} vendord_interface_t; - -CFG_TUD_MEM_SECTION tu_static vendord_interface_t _vendord_itf[CFG_TUD_VENDOR]; - -#define ITF_MEM_RESET_SIZE offsetof(vendord_interface_t, rx_ff) - - -bool tud_vendor_n_mounted (uint8_t itf) -{ - return _vendord_itf[itf].ep_in && _vendord_itf[itf].ep_out; -} - -uint32_t tud_vendor_n_available (uint8_t itf) -{ - return tu_fifo_count(&_vendord_itf[itf].rx_ff); -} - -bool tud_vendor_n_peek(uint8_t itf, uint8_t* u8) -{ - return tu_fifo_peek(&_vendord_itf[itf].rx_ff, u8); -} - -//--------------------------------------------------------------------+ -// Read API -//--------------------------------------------------------------------+ -static void _prep_out_transaction (vendord_interface_t* p_itf) -{ - uint8_t const rhport = 0; - - // claim endpoint - TU_VERIFY(usbd_edpt_claim(rhport, p_itf->ep_out), ); - - // Prepare for incoming data but only allow what we can store in the ring buffer. - uint16_t max_read = tu_fifo_remaining(&p_itf->rx_ff); - if ( max_read >= CFG_TUD_VENDOR_EPSIZE ) - { - usbd_edpt_xfer(rhport, p_itf->ep_out, p_itf->epout_buf, CFG_TUD_VENDOR_EPSIZE); - } - else - { - // Release endpoint since we don't make any transfer - usbd_edpt_release(rhport, p_itf->ep_out); - } -} - -uint32_t tud_vendor_n_read (uint8_t itf, void* buffer, uint32_t bufsize) -{ - vendord_interface_t* p_itf = &_vendord_itf[itf]; - uint32_t num_read = tu_fifo_read_n(&p_itf->rx_ff, buffer, (uint16_t) bufsize); - _prep_out_transaction(p_itf); - return num_read; -} - -void tud_vendor_n_read_flush (uint8_t itf) -{ - vendord_interface_t* p_itf = &_vendord_itf[itf]; - tu_fifo_clear(&p_itf->rx_ff); - _prep_out_transaction(p_itf); -} - -//--------------------------------------------------------------------+ -// Write API -//--------------------------------------------------------------------+ -uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize) -{ - vendord_interface_t* p_itf = &_vendord_itf[itf]; - uint16_t ret = tu_fifo_write_n(&p_itf->tx_ff, buffer, (uint16_t) bufsize); - - // flush if queue more than packet size - if (tu_fifo_count(&p_itf->tx_ff) >= CFG_TUD_VENDOR_EPSIZE) { - tud_vendor_n_write_flush(itf); - } - return ret; -} - -uint32_t tud_vendor_n_write_flush (uint8_t itf) -{ - vendord_interface_t* p_itf = &_vendord_itf[itf]; - - // Skip if usb is not ready yet - TU_VERIFY( tud_ready(), 0 ); - - // No data to send - if ( !tu_fifo_count(&p_itf->tx_ff) ) return 0; - - uint8_t const rhport = 0; - - // Claim the endpoint - TU_VERIFY( usbd_edpt_claim(rhport, p_itf->ep_in), 0 ); - - // Pull data from FIFO - uint16_t const count = tu_fifo_read_n(&p_itf->tx_ff, p_itf->epin_buf, sizeof(p_itf->epin_buf)); - - if ( count ) - { - TU_ASSERT( usbd_edpt_xfer(rhport, p_itf->ep_in, p_itf->epin_buf, count), 0 ); - return count; - }else - { - // Release endpoint since we don't make any transfer - // Note: data is dropped if terminal is not connected - usbd_edpt_release(rhport, p_itf->ep_in); - return 0; - } -} - -uint32_t tud_vendor_n_write_available (uint8_t itf) -{ - return tu_fifo_remaining(&_vendord_itf[itf].tx_ff); -} - -//--------------------------------------------------------------------+ -// USBD Driver API -//--------------------------------------------------------------------+ -void vendord_init(void) -{ - tu_memclr(_vendord_itf, sizeof(_vendord_itf)); - - for(uint8_t i=0; irx_ff, p_itf->rx_ff_buf, CFG_TUD_VENDOR_RX_BUFSIZE, 1, false); - tu_fifo_config(&p_itf->tx_ff, p_itf->tx_ff_buf, CFG_TUD_VENDOR_TX_BUFSIZE, 1, false); - -#if CFG_FIFO_MUTEX - tu_fifo_config_mutex(&p_itf->rx_ff, NULL, osal_mutex_create(&p_itf->rx_ff_mutex)); - tu_fifo_config_mutex(&p_itf->tx_ff, osal_mutex_create(&p_itf->tx_ff_mutex), NULL); -#endif - } -} - -void vendord_reset(uint8_t rhport) -{ - (void) rhport; - - for(uint8_t i=0; irx_ff); - tu_fifo_clear(&p_itf->tx_ff); - } -} - -uint16_t vendord_open(uint8_t rhport, tusb_desc_interface_t const * desc_itf, uint16_t max_len) -{ - TU_VERIFY(TUSB_CLASS_VENDOR_SPECIFIC == desc_itf->bInterfaceClass, 0); - - uint8_t const * p_desc = tu_desc_next(desc_itf); - uint8_t const * desc_end = p_desc + max_len; - - // Find available interface - vendord_interface_t* p_vendor = NULL; - for(uint8_t i=0; iitf_num = desc_itf->bInterfaceNumber; - if (desc_itf->bNumEndpoints) - { - // skip non-endpoint descriptors - while ( (TUSB_DESC_ENDPOINT != tu_desc_type(p_desc)) && (p_desc < desc_end) ) - { - p_desc = tu_desc_next(p_desc); - } - - // Open endpoint pair with usbd helper - TU_ASSERT(usbd_open_edpt_pair(rhport, p_desc, desc_itf->bNumEndpoints, TUSB_XFER_BULK, &p_vendor->ep_out, &p_vendor->ep_in), 0); - - p_desc += desc_itf->bNumEndpoints*sizeof(tusb_desc_endpoint_t); - - // Prepare for incoming data - if ( p_vendor->ep_out ) - { - _prep_out_transaction(p_vendor); - } - - if ( p_vendor->ep_in ) tud_vendor_n_write_flush((uint8_t)(p_vendor - _vendord_itf)); - } - - return (uint16_t) ((uintptr_t) p_desc - (uintptr_t) desc_itf); -} - -bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) -{ - (void) rhport; - (void) result; - - uint8_t itf = 0; - vendord_interface_t* p_itf = _vendord_itf; - - for ( ; ; itf++, p_itf++) - { - if (itf >= TU_ARRAY_SIZE(_vendord_itf)) return false; - - if ( ( ep_addr == p_itf->ep_out ) || ( ep_addr == p_itf->ep_in ) ) break; - } - - if ( ep_addr == p_itf->ep_out ) - { - // Receive new data - tu_fifo_write_n(&p_itf->rx_ff, p_itf->epout_buf, (uint16_t) xferred_bytes); - - // Invoked callback if any - if (tud_vendor_rx_cb) tud_vendor_rx_cb(itf); - - _prep_out_transaction(p_itf); - } - else if ( ep_addr == p_itf->ep_in ) - { - if (tud_vendor_tx_cb) tud_vendor_tx_cb(itf, (uint16_t) xferred_bytes); - // Send complete, try to send more if possible - tud_vendor_n_write_flush(itf); - } - - return true; -} - -#endif diff --git a/pico-sdk/lib/tinyusb/src/class/vendor/vendor_device.h b/pico-sdk/lib/tinyusb/src/class/vendor/vendor_device.h deleted file mode 100644 index d239406..0000000 --- a/pico-sdk/lib/tinyusb/src/class/vendor/vendor_device.h +++ /dev/null @@ -1,150 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach (tinyusb.org) - * - * 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. - * - * This file is part of the TinyUSB stack. - */ - -#ifndef _TUSB_VENDOR_DEVICE_H_ -#define _TUSB_VENDOR_DEVICE_H_ - -#include "common/tusb_common.h" - -#ifndef CFG_TUD_VENDOR_EPSIZE -#define CFG_TUD_VENDOR_EPSIZE 64 -#endif - -#ifdef __cplusplus - extern "C" { -#endif - -//--------------------------------------------------------------------+ -// Application API (Multiple Interfaces) -//--------------------------------------------------------------------+ -bool tud_vendor_n_mounted (uint8_t itf); - -uint32_t tud_vendor_n_available (uint8_t itf); -uint32_t tud_vendor_n_read (uint8_t itf, void* buffer, uint32_t bufsize); -bool tud_vendor_n_peek (uint8_t itf, uint8_t* ui8); -void tud_vendor_n_read_flush (uint8_t itf); - -uint32_t tud_vendor_n_write (uint8_t itf, void const* buffer, uint32_t bufsize); -uint32_t tud_vendor_n_write_flush (uint8_t itf); -uint32_t tud_vendor_n_write_available (uint8_t itf); - -static inline uint32_t tud_vendor_n_write_str (uint8_t itf, char const* str); - -// backward compatible -#define tud_vendor_n_flush(itf) tud_vendor_n_write_flush(itf) - -//--------------------------------------------------------------------+ -// Application API (Single Port) -//--------------------------------------------------------------------+ -static inline bool tud_vendor_mounted (void); -static inline uint32_t tud_vendor_available (void); -static inline uint32_t tud_vendor_read (void* buffer, uint32_t bufsize); -static inline bool tud_vendor_peek (uint8_t* ui8); -static inline void tud_vendor_read_flush (void); -static inline uint32_t tud_vendor_write (void const* buffer, uint32_t bufsize); -static inline uint32_t tud_vendor_write_str (char const* str); -static inline uint32_t tud_vendor_write_available (void); -static inline uint32_t tud_vendor_write_flush (void); - -// backward compatible -#define tud_vendor_flush() tud_vendor_write_flush() - -//--------------------------------------------------------------------+ -// Application Callback API (weak is optional) -//--------------------------------------------------------------------+ - -// Invoked when received new data -TU_ATTR_WEAK void tud_vendor_rx_cb(uint8_t itf); -// Invoked when last rx transfer finished -TU_ATTR_WEAK void tud_vendor_tx_cb(uint8_t itf, uint32_t sent_bytes); - -//--------------------------------------------------------------------+ -// Inline Functions -//--------------------------------------------------------------------+ - -static inline uint32_t tud_vendor_n_write_str (uint8_t itf, char const* str) -{ - return tud_vendor_n_write(itf, str, strlen(str)); -} - -static inline bool tud_vendor_mounted (void) -{ - return tud_vendor_n_mounted(0); -} - -static inline uint32_t tud_vendor_available (void) -{ - return tud_vendor_n_available(0); -} - -static inline uint32_t tud_vendor_read (void* buffer, uint32_t bufsize) -{ - return tud_vendor_n_read(0, buffer, bufsize); -} - -static inline bool tud_vendor_peek (uint8_t* ui8) -{ - return tud_vendor_n_peek(0, ui8); -} - -static inline void tud_vendor_read_flush(void) -{ - tud_vendor_n_read_flush(0); -} - -static inline uint32_t tud_vendor_write (void const* buffer, uint32_t bufsize) -{ - return tud_vendor_n_write(0, buffer, bufsize); -} - -static inline uint32_t tud_vendor_write_flush (void) -{ - return tud_vendor_n_write_flush(0); -} - -static inline uint32_t tud_vendor_write_str (char const* str) -{ - return tud_vendor_n_write_str(0, str); -} - -static inline uint32_t tud_vendor_write_available (void) -{ - return tud_vendor_n_write_available(0); -} - -//--------------------------------------------------------------------+ -// Internal Class Driver API -//--------------------------------------------------------------------+ -void vendord_init(void); -void vendord_reset(uint8_t rhport); -uint16_t vendord_open(uint8_t rhport, tusb_desc_interface_t const * itf_desc, uint16_t max_len); -bool vendord_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); - -#ifdef __cplusplus - } -#endif - -#endif /* _TUSB_VENDOR_DEVICE_H_ */ diff --git a/pico-sdk/lib/tinyusb/src/class/vendor/vendor_host.c b/pico-sdk/lib/tinyusb/src/class/vendor/vendor_host.c deleted file mode 100644 index e66c500..0000000 --- a/pico-sdk/lib/tinyusb/src/class/vendor/vendor_host.c +++ /dev/null @@ -1,146 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach (tinyusb.org) - * - * 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. - * - * This file is part of the TinyUSB stack. - */ - -#include "tusb_option.h" - -#if (CFG_TUH_ENABLED && CFG_TUH_VENDOR) - -//--------------------------------------------------------------------+ -// INCLUDE -//--------------------------------------------------------------------+ -#include "host/usbh.h" -#include "vendor_host.h" - -//--------------------------------------------------------------------+ -// MACRO CONSTANT TYPEDEF -//--------------------------------------------------------------------+ - -//--------------------------------------------------------------------+ -// INTERNAL OBJECT & FUNCTION DECLARATION -//--------------------------------------------------------------------+ -custom_interface_info_t custom_interface[CFG_TUH_DEVICE_MAX]; - -static tusb_error_t cush_validate_paras(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id, void * p_buffer, uint16_t length) -{ - if ( !tusbh_custom_is_mounted(dev_addr, vendor_id, product_id) ) - { - return TUSB_ERROR_DEVICE_NOT_READY; - } - - TU_ASSERT( p_buffer != NULL && length != 0, TUSB_ERROR_INVALID_PARA); - - return TUSB_ERROR_NONE; -} -//--------------------------------------------------------------------+ -// APPLICATION API (need to check parameters) -//--------------------------------------------------------------------+ -tusb_error_t tusbh_custom_read(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id, void * p_buffer, uint16_t length) -{ - TU_ASSERT_ERR( cush_validate_paras(dev_addr, vendor_id, product_id, p_buffer, length) ); - - if ( !hcd_pipe_is_idle(custom_interface[dev_addr-1].pipe_in) ) - { - return TUSB_ERROR_INTERFACE_IS_BUSY; - } - - (void) usbh_edpt_xfer( custom_interface[dev_addr-1].pipe_in, p_buffer, length); - - return TUSB_ERROR_NONE; -} - -tusb_error_t tusbh_custom_write(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id, void const * p_data, uint16_t length) -{ - TU_ASSERT_ERR( cush_validate_paras(dev_addr, vendor_id, product_id, p_data, length) ); - - if ( !hcd_pipe_is_idle(custom_interface[dev_addr-1].pipe_out) ) - { - return TUSB_ERROR_INTERFACE_IS_BUSY; - } - - (void) usbh_edpt_xfer( custom_interface[dev_addr-1].pipe_out, p_data, length); - - return TUSB_ERROR_NONE; -} - -//--------------------------------------------------------------------+ -// USBH-CLASS API -//--------------------------------------------------------------------+ -void cush_init(void) -{ - tu_memclr(&custom_interface, sizeof(custom_interface_info_t) * CFG_TUH_DEVICE_MAX); -} - -tusb_error_t cush_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length) -{ - // FIXME quick hack to test lpc1k custom class with 2 bulk endpoints - uint8_t const *p_desc = (uint8_t const *) p_interface_desc; - p_desc = tu_desc_next(p_desc); - - //------------- Bulk Endpoints Descriptor -------------// - for(uint32_t i=0; i<2; i++) - { - tusb_desc_endpoint_t const *p_endpoint = (tusb_desc_endpoint_t const *) p_desc; - TU_ASSERT(TUSB_DESC_ENDPOINT == p_endpoint->bDescriptorType, TUSB_ERROR_INVALID_PARA); - - pipe_handle_t * p_pipe_hdl = ( p_endpoint->bEndpointAddress & TUSB_DIR_IN_MASK ) ? - &custom_interface[dev_addr-1].pipe_in : &custom_interface[dev_addr-1].pipe_out; - *p_pipe_hdl = usbh_edpt_open(dev_addr, p_endpoint, TUSB_CLASS_VENDOR_SPECIFIC); - TU_ASSERT ( pipehandle_is_valid(*p_pipe_hdl), TUSB_ERROR_HCD_OPEN_PIPE_FAILED ); - - p_desc = tu_desc_next(p_desc); - } - - (*p_length) = sizeof(tusb_desc_interface_t) + 2*sizeof(tusb_desc_endpoint_t); - return TUSB_ERROR_NONE; -} - -void cush_isr(pipe_handle_t pipe_hdl, xfer_result_t event) -{ - -} - -void cush_close(uint8_t dev_addr) -{ - tusb_error_t err1, err2; - custom_interface_info_t * p_interface = &custom_interface[dev_addr-1]; - - // TODO re-consider to check pipe valid before calling pipe_close - if( pipehandle_is_valid( p_interface->pipe_in ) ) - { - err1 = hcd_pipe_close( p_interface->pipe_in ); - } - - if ( pipehandle_is_valid( p_interface->pipe_out ) ) - { - err2 = hcd_pipe_close( p_interface->pipe_out ); - } - - tu_memclr(p_interface, sizeof(custom_interface_info_t)); - - TU_ASSERT(err1 == TUSB_ERROR_NONE && err2 == TUSB_ERROR_NONE, (void) 0 ); -} - -#endif diff --git a/pico-sdk/lib/tinyusb/src/class/vendor/vendor_host.h b/pico-sdk/lib/tinyusb/src/class/vendor/vendor_host.h deleted file mode 100644 index acfebe7..0000000 --- a/pico-sdk/lib/tinyusb/src/class/vendor/vendor_host.h +++ /dev/null @@ -1,67 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach (tinyusb.org) - * - * 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. - * - * This file is part of the TinyUSB stack. - */ - -#ifndef _TUSB_VENDOR_HOST_H_ -#define _TUSB_VENDOR_HOST_H_ - -#include "common/tusb_common.h" - -#ifdef __cplusplus - extern "C" { -#endif - -typedef struct { - pipe_handle_t pipe_in; - pipe_handle_t pipe_out; -}custom_interface_info_t; - -//--------------------------------------------------------------------+ -// USBH-CLASS DRIVER API -//--------------------------------------------------------------------+ -static inline bool tusbh_custom_is_mounted(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id) -{ - (void) vendor_id; // TODO check this later - (void) product_id; -// return (tusbh_device_get_mounted_class_flag(dev_addr) & TU_BIT(TUSB_CLASS_MAPPED_INDEX_END-1) ) != 0; - return false; -} - -bool tusbh_custom_read(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id, void * p_buffer, uint16_t length); -bool tusbh_custom_write(uint8_t dev_addr, uint16_t vendor_id, uint16_t product_id, void const * p_data, uint16_t length); - -//--------------------------------------------------------------------+ -// Internal Class Driver API -//--------------------------------------------------------------------+ -void cush_init(void); -bool cush_open_subtask(uint8_t dev_addr, tusb_desc_interface_t const *p_interface_desc, uint16_t *p_length); -void cush_isr(pipe_handle_t pipe_hdl, xfer_result_t event); -void cush_close(uint8_t dev_addr); - -#ifdef __cplusplus - } -#endif - -#endif /* _TUSB_VENDOR_HOST_H_ */ diff --git a/pico-sdk/lib/tinyusb/src/common/tusb_common.h b/pico-sdk/lib/tinyusb/src/common/tusb_common.h index caeb5f2..0d4082c 100644 --- a/pico-sdk/lib/tinyusb/src/common/tusb_common.h +++ b/pico-sdk/lib/tinyusb/src/common/tusb_common.h @@ -37,6 +37,7 @@ #define TU_ARRAY_SIZE(_arr) ( sizeof(_arr) / sizeof(_arr[0]) ) #define TU_MIN(_x, _y) ( ( (_x) < (_y) ) ? (_x) : (_y) ) #define TU_MAX(_x, _y) ( ( (_x) > (_y) ) ? (_x) : (_y) ) +#define TU_DIV_CEIL(n, d) (((n) + (d) - 1) / (d)) #define TU_U16(_high, _low) ((uint16_t) (((_high) << 8) | (_low))) #define TU_U16_HIGH(_u16) ((uint8_t) (((_u16) >> 8) & 0x00ff)) @@ -64,6 +65,7 @@ // Standard Headers #include #include +#include #include #include #include diff --git a/pico-sdk/lib/tinyusb/src/common/tusb_compiler.h b/pico-sdk/lib/tinyusb/src/common/tusb_compiler.h index 6f07bdd..0d5570b 100644 --- a/pico-sdk/lib/tinyusb/src/common/tusb_compiler.h +++ b/pico-sdk/lib/tinyusb/src/common/tusb_compiler.h @@ -56,7 +56,7 @@ #elif defined (__STDC_VERSION__) && __STDC_VERSION__ >= 201112L #define TU_VERIFY_STATIC _Static_assert #elif defined(__CCRX__) - #define TU_VERIFY_STATIC(const_expr, _mess) typedef char TU_XSTRCAT(Line, __LINE__)[(const_expr) ? 1 : 0]; + #define TU_VERIFY_STATIC(const_expr, _mess) typedef char TU_XSTRCAT(_verify_static_, _TU_COUNTER_)[(const_expr) ? 1 : 0]; #else #define TU_VERIFY_STATIC(const_expr, _mess) enum { TU_XSTRCAT(_verify_static_, _TU_COUNTER_) = 1/(!!(const_expr)) } #endif diff --git a/pico-sdk/lib/tinyusb/src/common/tusb_debug.h b/pico-sdk/lib/tinyusb/src/common/tusb_debug.h index 0f4dc93..2e9f1d9 100644 --- a/pico-sdk/lib/tinyusb/src/common/tusb_debug.h +++ b/pico-sdk/lib/tinyusb/src/common/tusb_debug.h @@ -60,6 +60,7 @@ void tu_print_mem(void const *buf, uint32_t count, uint8_t indent); static inline void tu_print_buf(uint8_t const* buf, uint32_t bufsize) { for(uint32_t i=0; i= 2 diff --git a/pico-sdk/lib/tinyusb/src/common/tusb_fifo.c b/pico-sdk/lib/tinyusb/src/common/tusb_fifo.c index d6c3db4..8a0fd44 100644 --- a/pico-sdk/lib/tinyusb/src/common/tusb_fifo.c +++ b/pico-sdk/lib/tinyusb/src/common/tusb_fifo.c @@ -62,7 +62,9 @@ TU_ATTR_ALWAYS_INLINE static inline void _ff_unlock(osal_mutex_t mutex) typedef enum { TU_FIFO_COPY_INC, ///< Copy from/to an increasing source/destination address - default mode +#ifdef TUP_MEM_CONST_ADDR TU_FIFO_COPY_CST_FULL_WORDS, ///< Copy from/to a constant source/destination address - required for e.g. STM32 to write into USB hardware FIFO +#endif } tu_fifo_copy_mode_t; bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable) @@ -92,6 +94,7 @@ bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_si // Pull & Push //--------------------------------------------------------------------+ +#ifdef TUP_MEM_CONST_ADDR // Intended to be used to read from hardware USB FIFO in e.g. STM32 where all data is read from a constant address // Code adapted from dcd_synopsys.c // TODO generalize with configurable 1 byte or 4 byte each read @@ -140,6 +143,7 @@ static void _ff_pull_const_addr(void * app_buf, const uint8_t * ff_buf, uint16_t *reg_tx = tmp32; } } +#endif // send one item to fifo WITHOUT updating write pointer static inline void _ff_push(tu_fifo_t* f, void const * app_buf, uint16_t rel) @@ -179,7 +183,7 @@ static void _ff_push_n(tu_fifo_t* f, void const * app_buf, uint16_t n, uint16_t memcpy(f->buffer, ((uint8_t const*) app_buf) + lin_bytes, wrap_bytes); } break; - +#ifdef TUP_MEM_CONST_ADDR case TU_FIFO_COPY_CST_FULL_WORDS: // Intended for hardware buffers from which it can be read word by word only if(n <= lin_count) @@ -224,6 +228,8 @@ static void _ff_push_n(tu_fifo_t* f, void const * app_buf, uint16_t n, uint16_t if (wrap_bytes > 0) _ff_push_const_addr(ff_buf, app_buf, wrap_bytes); } break; +#endif + default: break; } } @@ -264,7 +270,7 @@ static void _ff_pull_n(tu_fifo_t* f, void* app_buf, uint16_t n, uint16_t rd_ptr, memcpy((uint8_t*) app_buf + lin_bytes, f->buffer, wrap_bytes); } break; - +#ifdef TUP_MEM_CONST_ADDR case TU_FIFO_COPY_CST_FULL_WORDS: if ( n <= lin_count ) { @@ -309,6 +315,7 @@ static void _ff_pull_n(tu_fifo_t* f, void* app_buf, uint16_t n, uint16_t rd_ptr, // Read data wrapped part if (wrap_bytes > 0) _ff_pull_const_addr(app_buf, ff_buf, wrap_bytes); } +#endif break; default: break; @@ -726,10 +733,29 @@ uint16_t tu_fifo_read_n(tu_fifo_t* f, void * buffer, uint16_t n) return _tu_fifo_read_n(f, buffer, n, TU_FIFO_COPY_INC); } +#ifdef TUP_MEM_CONST_ADDR +/******************************************************************************/ +/*! + @brief This function will read n elements from the array index specified by + the read pointer and increment the read index. + This function checks for an overflow and corrects read pointer if required. + The dest address will not be incremented which is useful for writing to registers. + + @param[in] f + Pointer to the FIFO buffer to manipulate + @param[in] buffer + The pointer to data location + @param[in] n + Number of element that buffer can afford + + @returns number of items read from the FIFO + */ +/******************************************************************************/ uint16_t tu_fifo_read_n_const_addr_full_words(tu_fifo_t* f, void * buffer, uint16_t n) { return _tu_fifo_read_n(f, buffer, n, TU_FIFO_COPY_CST_FULL_WORDS); } +#endif /******************************************************************************/ /*! @@ -838,6 +864,7 @@ uint16_t tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t n) return _tu_fifo_write_n(f, data, n, TU_FIFO_COPY_INC); } +#ifdef TUP_MEM_CONST_ADDR /******************************************************************************/ /*! @brief This function will write n elements into the array index specified by @@ -857,6 +884,7 @@ uint16_t tu_fifo_write_n_const_addr_full_words(tu_fifo_t* f, const void * data, { return _tu_fifo_write_n(f, data, n, TU_FIFO_COPY_CST_FULL_WORDS); } +#endif /******************************************************************************/ /*! diff --git a/pico-sdk/lib/tinyusb/src/common/tusb_fifo.h b/pico-sdk/lib/tinyusb/src/common/tusb_fifo.h index 2f60ec2..6c0efb5 100644 --- a/pico-sdk/lib/tinyusb/src/common/tusb_fifo.h +++ b/pico-sdk/lib/tinyusb/src/common/tusb_fifo.h @@ -102,10 +102,8 @@ extern "C" { * | * ------------------------- * | R | 1 | 2 | W | 4 | 5 | - */ -typedef struct -{ +typedef struct { uint8_t* buffer ; // buffer pointer uint16_t depth ; // max items @@ -124,16 +122,14 @@ typedef struct } tu_fifo_t; -typedef struct -{ +typedef struct { uint16_t len_lin ; ///< linear length in item size uint16_t len_wrap ; ///< wrapped length in item size void * ptr_lin ; ///< linear part start pointer void * ptr_wrap ; ///< wrapped part start pointer } tu_fifo_buffer_info_t; -#define TU_FIFO_INIT(_buffer, _depth, _type, _overwritable) \ -{ \ +#define TU_FIFO_INIT(_buffer, _depth, _type, _overwritable){\ .buffer = _buffer, \ .depth = _depth, \ .item_size = sizeof(_type), \ @@ -144,32 +140,31 @@ typedef struct uint8_t _name##_buf[_depth*sizeof(_type)]; \ tu_fifo_t _name = TU_FIFO_INIT(_name##_buf, _depth, _type, _overwritable) - bool tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable); bool tu_fifo_clear(tu_fifo_t *f); bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable); #if OSAL_MUTEX_REQUIRED TU_ATTR_ALWAYS_INLINE static inline -void tu_fifo_config_mutex(tu_fifo_t *f, osal_mutex_t wr_mutex, osal_mutex_t rd_mutex) -{ +void tu_fifo_config_mutex(tu_fifo_t *f, osal_mutex_t wr_mutex, osal_mutex_t rd_mutex) { f->mutex_wr = wr_mutex; f->mutex_rd = rd_mutex; } - #else - #define tu_fifo_config_mutex(_f, _wr_mutex, _rd_mutex) - #endif bool tu_fifo_write (tu_fifo_t* f, void const * p_data); uint16_t tu_fifo_write_n (tu_fifo_t* f, void const * p_data, uint16_t n); +#ifdef TUP_MEM_CONST_ADDR uint16_t tu_fifo_write_n_const_addr_full_words (tu_fifo_t* f, const void * data, uint16_t n); +#endif bool tu_fifo_read (tu_fifo_t* f, void * p_buffer); uint16_t tu_fifo_read_n (tu_fifo_t* f, void * p_buffer, uint16_t n); +#ifdef TUP_MEM_CONST_ADDR uint16_t tu_fifo_read_n_const_addr_full_words (tu_fifo_t* f, void * buffer, uint16_t n); +#endif bool tu_fifo_peek (tu_fifo_t* f, void * p_buffer); uint16_t tu_fifo_peek_n (tu_fifo_t* f, void * p_buffer, uint16_t n); @@ -182,8 +177,7 @@ bool tu_fifo_overflowed (tu_fifo_t* f); void tu_fifo_correct_read_pointer (tu_fifo_t* f); TU_ATTR_ALWAYS_INLINE static inline -uint16_t tu_fifo_depth(tu_fifo_t* f) -{ +uint16_t tu_fifo_depth(tu_fifo_t* f) { return f->depth; } @@ -198,7 +192,6 @@ void tu_fifo_advance_read_pointer (tu_fifo_t *f, uint16_t n); void tu_fifo_get_read_info (tu_fifo_t *f, tu_fifo_buffer_info_t *info); void tu_fifo_get_write_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info); - #ifdef __cplusplus } #endif diff --git a/pico-sdk/lib/tinyusb/src/common/tusb_mcu.h b/pico-sdk/lib/tinyusb/src/common/tusb_mcu.h index e589c4c..abfed28 100644 --- a/pico-sdk/lib/tinyusb/src/common/tusb_mcu.h +++ b/pico-sdk/lib/tinyusb/src/common/tusb_mcu.h @@ -100,6 +100,13 @@ #define TUP_DCD_ENDPOINT_MAX 8 #define TUP_RHPORT_HIGHSPEED 1 +#elif TU_CHECK_MCU(OPT_MCU_MCXA15) + // USB0 is chipidea FS + #define TUP_USBIP_CHIPIDEA_FS + #define TUP_USBIP_CHIPIDEA_FS_MCX + + #define TUP_DCD_ENDPOINT_MAX 16 + #elif TU_CHECK_MCU(OPT_MCU_MIMXRT1XXX) #define TUP_USBIP_CHIPIDEA_HS #define TUP_USBIP_EHCI @@ -107,7 +114,7 @@ #define TUP_DCD_ENDPOINT_MAX 8 #define TUP_RHPORT_HIGHSPEED 1 -#elif TU_CHECK_MCU(OPT_MCU_KINETIS_KL, OPT_MCU_KINETIS_K32L) +#elif TU_CHECK_MCU(OPT_MCU_KINETIS_KL, OPT_MCU_KINETIS_K32L, OPT_MCU_KINETIS_K) #define TUP_USBIP_CHIPIDEA_FS #define TUP_USBIP_CHIPIDEA_FS_KINETIS #define TUP_DCD_ENDPOINT_MAX 16 @@ -188,6 +195,7 @@ #elif TU_CHECK_MCU(OPT_MCU_STM32F4) #define TUP_USBIP_DWC2 #define TUP_USBIP_DWC2_STM32 + #define TUP_USBIP_DWC2_TEST_MODE // For most mcu, FS has 4, HS has 6. TODO 446/469/479 HS has 9 #define TUP_DCD_ENDPOINT_MAX 6 @@ -202,6 +210,7 @@ // MCU with on-chip HS Phy #if defined(STM32F723xx) || defined(STM32F730xx) || defined(STM32F733xx) #define TUP_RHPORT_HIGHSPEED 1 // Port0: FS, Port1: HS + #define TUP_USBIP_DWC2_TEST_MODE #endif #elif TU_CHECK_MCU(OPT_MCU_STM32H7) @@ -270,6 +279,7 @@ defined(STM32U5F7xx) || defined(STM32U5F9xx) || defined(STM32U5G7xx) || defined(STM32U5G9xx) #define TUP_DCD_ENDPOINT_MAX 9 #define TUP_RHPORT_HIGHSPEED 1 + #define TUP_USBIP_DWC2_TEST_MODE #else #define TUP_DCD_ENDPOINT_MAX 6 #endif @@ -322,6 +332,9 @@ #define TUP_USBIP_DWC2 #define TUP_DCD_ENDPOINT_MAX 6 +#elif TU_CHECK_MCU(OPT_MCU_ESP32, OPT_MCU_ESP32C2, OPT_MCU_ESP32C3, OPT_MCU_ESP32C6, OPT_MCU_ESP32H2) && (CFG_TUD_ENABLED || !(defined(CFG_TUH_MAX3421) && CFG_TUH_MAX3421)) + #error "MCUs are only supported with CFG_TUH_MAX3421 enabled" + //--------------------------------------------------------------------+ // Dialog //--------------------------------------------------------------------+ @@ -391,12 +404,24 @@ //------------- WCH -------------// #elif TU_CHECK_MCU(OPT_MCU_CH32V307) - #define TUP_DCD_ENDPOINT_MAX 16 - #define TUP_RHPORT_HIGHSPEED 1 + // v307 support both FS and HS + #define TUP_USBIP_WCH_USBHS + #define TUP_USBIP_WCH_USBFS + + #define TUP_RHPORT_HIGHSPEED 1 // default to highspeed + #define TUP_DCD_ENDPOINT_MAX (CFG_TUD_MAX_SPEED == OPT_MODE_HIGH_SPEED ? 16 : 8) #elif TU_CHECK_MCU(OPT_MCU_CH32F20X) - #define TUP_DCD_ENDPOINT_MAX 16 - #define TUP_RHPORT_HIGHSPEED 1 + #define TUP_USBIP_WCH_USBHS + #define TUP_USBIP_WCH_USBFS + + #define TUP_RHPORT_HIGHSPEED 1 // default to highspeed + #define TUP_DCD_ENDPOINT_MAX (CFG_TUD_MAX_SPEED == OPT_MODE_HIGH_SPEED ? 16 : 8) + +#elif TU_CHECK_MCU(OPT_MCU_CH32V20X) + #define TUP_USBIP_WCH_USBFS + #define TUP_DCD_ENDPOINT_MAX 8 + #endif @@ -419,7 +444,7 @@ #define TUP_MCU_MULTIPLE_CORE 0 #endif -#ifndef TUP_DCD_ENDPOINT_MAX +#if !defined(TUP_DCD_ENDPOINT_MAX) && defined(CFG_TUD_ENABLED) && CFG_TUD_ENABLED #warning "TUP_DCD_ENDPOINT_MAX is not defined for this MCU, default to 8" #define TUP_DCD_ENDPOINT_MAX 8 #endif @@ -434,4 +459,12 @@ #define TU_ATTR_FAST_FUNC #endif +#if defined(TUP_USBIP_DWC2) || defined(TUP_USBIP_FSDEV) + #define TUP_DCD_EDPT_ISO_ALLOC +#endif + +#if defined(TUP_USBIP_DWC2) + #define TUP_MEM_CONST_ADDR +#endif + #endif diff --git a/pico-sdk/lib/tinyusb/src/common/tusb_private.h b/pico-sdk/lib/tinyusb/src/common/tusb_private.h index db1ba97..373a502 100644 --- a/pico-sdk/lib/tinyusb/src/common/tusb_private.h +++ b/pico-sdk/lib/tinyusb/src/common/tusb_private.h @@ -60,7 +60,7 @@ typedef struct { tu_fifo_t ff; // mutex: read if ep rx, write if e tx - OSAL_MUTEX_DEF(ff_mutex); + OSAL_MUTEX_DEF(ff_mutexdef); }tu_edpt_stream_t; @@ -87,15 +87,17 @@ bool tu_edpt_release(tu_edpt_state_t* ep_state, osal_mutex_t mutex); // Endpoint Stream //--------------------------------------------------------------------+ -// Init an stream, should only be called once +// Init an endpoint stream bool tu_edpt_stream_init(tu_edpt_stream_t* s, bool is_host, bool is_tx, bool overwritable, void* ff_buf, uint16_t ff_bufsize, uint8_t* ep_buf, uint16_t ep_bufsize); +// Deinit an endpoint stream +bool tu_edpt_stream_deinit(tu_edpt_stream_t* s); + // Open an stream for an endpoint // hwid is either device address (host mode) or rhport (device mode) TU_ATTR_ALWAYS_INLINE static inline -void tu_edpt_stream_open(tu_edpt_stream_t* s, uint8_t hwid, tusb_desc_endpoint_t const *desc_ep) -{ +void tu_edpt_stream_open(tu_edpt_stream_t* s, uint8_t hwid, tusb_desc_endpoint_t const *desc_ep) { tu_fifo_clear(&s->ff); s->hwid = hwid; s->ep_addr = desc_ep->bEndpointAddress; @@ -103,16 +105,14 @@ void tu_edpt_stream_open(tu_edpt_stream_t* s, uint8_t hwid, tusb_desc_endpoint_t } TU_ATTR_ALWAYS_INLINE static inline -void tu_edpt_stream_close(tu_edpt_stream_t* s) -{ +void tu_edpt_stream_close(tu_edpt_stream_t* s) { s->hwid = 0; s->ep_addr = 0; } // Clear fifo TU_ATTR_ALWAYS_INLINE static inline -bool tu_edpt_stream_clear(tu_edpt_stream_t* s) -{ +bool tu_edpt_stream_clear(tu_edpt_stream_t* s) { return tu_fifo_clear(&s->ff); } @@ -131,8 +131,7 @@ bool tu_edpt_stream_write_zlp_if_needed(tu_edpt_stream_t* s, uint32_t last_xferr // Get the number of bytes available for writing TU_ATTR_ALWAYS_INLINE static inline -uint32_t tu_edpt_stream_write_available(tu_edpt_stream_t* s) -{ +uint32_t tu_edpt_stream_write_available(tu_edpt_stream_t* s) { return (uint32_t) tu_fifo_remaining(&s->ff); } diff --git a/pico-sdk/lib/tinyusb/src/common/tusb_types.h b/pico-sdk/lib/tinyusb/src/common/tusb_types.h index fab6809..b571f9b 100644 --- a/pico-sdk/lib/tinyusb/src/common/tusb_types.h +++ b/pico-sdk/lib/tinyusb/src/common/tusb_types.h @@ -24,12 +24,8 @@ * This file is part of the TinyUSB stack. */ -/** \ingroup group_usb_definitions - * \defgroup USBDef_Type USB Types - * @{ */ - -#ifndef _TUSB_TYPES_H_ -#define _TUSB_TYPES_H_ +#ifndef TUSB_TYPES_H_ +#define TUSB_TYPES_H_ #include #include @@ -44,43 +40,38 @@ *------------------------------------------------------------------*/ /// defined base on EHCI specs value for Endpoint Speed -typedef enum -{ +typedef enum { TUSB_SPEED_FULL = 0, TUSB_SPEED_LOW = 1, TUSB_SPEED_HIGH = 2, TUSB_SPEED_INVALID = 0xff, -}tusb_speed_t; +} tusb_speed_t; /// defined base on USB Specs Endpoint's bmAttributes -typedef enum -{ +typedef enum { TUSB_XFER_CONTROL = 0 , TUSB_XFER_ISOCHRONOUS , TUSB_XFER_BULK , TUSB_XFER_INTERRUPT -}tusb_xfer_type_t; +} tusb_xfer_type_t; -typedef enum -{ +typedef enum { TUSB_DIR_OUT = 0, TUSB_DIR_IN = 1, TUSB_DIR_IN_MASK = 0x80 -}tusb_dir_t; +} tusb_dir_t; -enum -{ +enum { TUSB_EPSIZE_BULK_FS = 64, - TUSB_EPSIZE_BULK_HS= 512, + TUSB_EPSIZE_BULK_HS = 512, TUSB_EPSIZE_ISO_FS_MAX = 1023, TUSB_EPSIZE_ISO_HS_MAX = 1024, }; -/// Isochronous End Point Attributes -typedef enum -{ +/// Isochronous Endpoint Attributes +typedef enum { TUSB_ISO_EP_ATT_NO_SYNC = 0x00, TUSB_ISO_EP_ATT_ASYNCHRONOUS = 0x04, TUSB_ISO_EP_ATT_ADAPTIVE = 0x08, @@ -88,11 +79,10 @@ typedef enum TUSB_ISO_EP_ATT_DATA = 0x00, ///< Data End Point TUSB_ISO_EP_ATT_EXPLICIT_FB = 0x10, ///< Feedback End Point TUSB_ISO_EP_ATT_IMPLICIT_FB = 0x20, ///< Data endpoint that also serves as an implicit feedback -}tusb_iso_ep_attribute_t; +} tusb_iso_ep_attribute_t; /// USB Descriptor Types -typedef enum -{ +typedef enum { TUSB_DESC_DEVICE = 0x01, TUSB_DESC_CONFIGURATION = 0x02, TUSB_DESC_STRING = 0x03, @@ -119,10 +109,9 @@ typedef enum TUSB_DESC_SUPERSPEED_ENDPOINT_COMPANION = 0x30, TUSB_DESC_SUPERSPEED_ISO_ENDPOINT_COMPANION = 0x31 -}tusb_desc_type_t; +} tusb_desc_type_t; -typedef enum -{ +typedef enum { TUSB_REQ_GET_STATUS = 0 , TUSB_REQ_CLEAR_FEATURE = 1 , TUSB_REQ_RESERVED = 2 , @@ -136,25 +125,22 @@ typedef enum TUSB_REQ_GET_INTERFACE = 10 , TUSB_REQ_SET_INTERFACE = 11 , TUSB_REQ_SYNCH_FRAME = 12 -}tusb_request_code_t; +} tusb_request_code_t; -typedef enum -{ +typedef enum { TUSB_REQ_FEATURE_EDPT_HALT = 0, TUSB_REQ_FEATURE_REMOTE_WAKEUP = 1, TUSB_REQ_FEATURE_TEST_MODE = 2 -}tusb_request_feature_selector_t; +} tusb_request_feature_selector_t; -typedef enum -{ +typedef enum { TUSB_REQ_TYPE_STANDARD = 0, TUSB_REQ_TYPE_CLASS, TUSB_REQ_TYPE_VENDOR, TUSB_REQ_TYPE_INVALID } tusb_request_type_t; -typedef enum -{ +typedef enum { TUSB_REQ_RCPT_DEVICE =0, TUSB_REQ_RCPT_INTERFACE, TUSB_REQ_RCPT_ENDPOINT, @@ -162,8 +148,7 @@ typedef enum } tusb_request_recipient_t; // https://www.usb.org/defined-class-codes -typedef enum -{ +typedef enum { TUSB_CLASS_UNSPECIFIED = 0 , TUSB_CLASS_AUDIO = 1 , TUSB_CLASS_CDC = 2 , @@ -187,26 +172,23 @@ typedef enum TUSB_CLASS_MISC = 0xEF , TUSB_CLASS_APPLICATION_SPECIFIC = 0xFE , TUSB_CLASS_VENDOR_SPECIFIC = 0xFF -}tusb_class_code_t; +} tusb_class_code_t; typedef enum { MISC_SUBCLASS_COMMON = 2 }misc_subclass_type_t; -typedef enum -{ +typedef enum { MISC_PROTOCOL_IAD = 1 -}misc_protocol_type_t; +} misc_protocol_type_t; -typedef enum -{ +typedef enum { APP_SUBCLASS_USBTMC = 0x03, APP_SUBCLASS_DFU_RUNTIME = 0x01 } app_subclass_type_t; -typedef enum -{ +typedef enum { DEVICE_CAPABILITY_WIRELESS_USB = 0x01, DEVICE_CAPABILITY_USB20_EXTENSION = 0x02, DEVICE_CAPABILITY_SUPERSPEED_USB = 0x03, @@ -223,11 +205,11 @@ typedef enum DEVICE_CAPABILITY_AUTHENTICATION = 0x0E, DEVICE_CAPABILITY_BILLBOARD_EX = 0x0F, DEVICE_CAPABILITY_CONFIGURATION_SUMMARY = 0x10 -}device_capability_type_t; +} device_capability_type_t; enum { - TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP = TU_BIT(5), - TUSB_DESC_CONFIG_ATT_SELF_POWERED = TU_BIT(6), + TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP = 1u << 5, + TUSB_DESC_CONFIG_ATT_SELF_POWERED = 1u << 6, }; #define TUSB_DESC_CONFIG_POWER_MA(x) ((x)/2) @@ -235,28 +217,25 @@ enum { //--------------------------------------------------------------------+ // //--------------------------------------------------------------------+ -typedef enum -{ +typedef enum { XFER_RESULT_SUCCESS = 0, XFER_RESULT_FAILED, XFER_RESULT_STALLED, XFER_RESULT_TIMEOUT, XFER_RESULT_INVALID -}xfer_result_t; +} xfer_result_t; -enum // TODO remove -{ +// TODO remove +enum { DESC_OFFSET_LEN = 0, DESC_OFFSET_TYPE = 1 }; -enum -{ +enum { INTERFACE_INVALID_NUMBER = 0xff }; -typedef enum -{ +typedef enum { MS_OS_20_SET_HEADER_DESCRIPTOR = 0x00, MS_OS_20_SUBSET_HEADER_CONFIGURATION = 0x01, MS_OS_20_SUBSET_HEADER_FUNCTION = 0x02, @@ -268,16 +247,14 @@ typedef enum MS_OS_20_FEATURE_VENDOR_REVISION = 0x08 } microsoft_os_20_type_t; -enum -{ +enum { CONTROL_STAGE_IDLE, CONTROL_STAGE_SETUP, CONTROL_STAGE_DATA, CONTROL_STAGE_ACK }; -enum -{ +enum { TUSB_INDEX_INVALID_8 = 0xFFu }; @@ -290,15 +267,14 @@ TU_ATTR_PACKED_BEGIN TU_ATTR_BIT_FIELD_ORDER_BEGIN /// USB Device Descriptor -typedef struct TU_ATTR_PACKED -{ +typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes. uint8_t bDescriptorType ; ///< DEVICE Descriptor Type. - uint16_t bcdUSB ; ///< BUSB Specification Release Number in Binary-Coded Decimal (i.e., 2.10 is 210H). This field identifies the release of the USB Specification with which the device and its descriptors are compliant. + uint16_t bcdUSB ; ///< BUSB Specification Release Number in Binary-Coded Decimal (i.e., 2.10 is 210H). - uint8_t bDeviceClass ; ///< Class code (assigned by the USB-IF). \li If this field is reset to zero, each interface within a configuration specifies its own class information and the various interfaces operate independently. \li If this field is set to a value between 1 and FEH, the device supports different class specifications on different interfaces and the interfaces may not operate independently. This value identifies the class definition used for the aggregate interfaces. \li If this field is set to FFH, the device class is vendor-specific. - uint8_t bDeviceSubClass ; ///< Subclass code (assigned by the USB-IF). These codes are qualified by the value of the bDeviceClass field. \li If the bDeviceClass field is reset to zero, this field must also be reset to zero. \li If the bDeviceClass field is not set to FFH, all values are reserved for assignment by the USB-IF. - uint8_t bDeviceProtocol ; ///< Protocol code (assigned by the USB-IF). These codes are qualified by the value of the bDeviceClass and the bDeviceSubClass fields. If a device supports class-specific protocols on a device basis as opposed to an interface basis, this code identifies the protocols that the device uses as defined by the specification of the device class. \li If this field is reset to zero, the device does not use class-specific protocols on a device basis. However, it may use classspecific protocols on an interface basis. \li If this field is set to FFH, the device uses a vendor-specific protocol on a device basis. + uint8_t bDeviceClass ; ///< Class code (assigned by the USB-IF). + uint8_t bDeviceSubClass ; ///< Subclass code (assigned by the USB-IF). + uint8_t bDeviceProtocol ; ///< Protocol code (assigned by the USB-IF). uint8_t bMaxPacketSize0 ; ///< Maximum packet size for endpoint zero (only 8, 16, 32, or 64 are valid). For HS devices is fixed to 64. uint16_t idVendor ; ///< Vendor ID (assigned by the USB-IF). @@ -314,8 +290,7 @@ typedef struct TU_ATTR_PACKED TU_VERIFY_STATIC( sizeof(tusb_desc_device_t) == 18, "size is not correct"); // USB Binary Device Object Store (BOS) Descriptor -typedef struct TU_ATTR_PACKED -{ +typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes uint8_t bDescriptorType ; ///< CONFIGURATION Descriptor Type uint16_t wTotalLength ; ///< Total length of data returned for this descriptor @@ -325,8 +300,7 @@ typedef struct TU_ATTR_PACKED TU_VERIFY_STATIC( sizeof(tusb_desc_bos_t) == 5, "size is not correct"); /// USB Configuration Descriptor -typedef struct TU_ATTR_PACKED -{ +typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes uint8_t bDescriptorType ; ///< CONFIGURATION Descriptor Type uint16_t wTotalLength ; ///< Total length of data returned for this configuration. Includes the combined length of all descriptors (configuration, interface, endpoint, and class- or vendor-specific) returned for this configuration. @@ -341,8 +315,7 @@ typedef struct TU_ATTR_PACKED TU_VERIFY_STATIC( sizeof(tusb_desc_configuration_t) == 9, "size is not correct"); /// USB Interface Descriptor -typedef struct TU_ATTR_PACKED -{ +typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes uint8_t bDescriptorType ; ///< INTERFACE Descriptor Type @@ -358,8 +331,7 @@ typedef struct TU_ATTR_PACKED TU_VERIFY_STATIC( sizeof(tusb_desc_interface_t) == 9, "size is not correct"); /// USB Endpoint Descriptor -typedef struct TU_ATTR_PACKED -{ +typedef struct TU_ATTR_PACKED { uint8_t bLength ; // Size of this descriptor in bytes uint8_t bDescriptorType ; // ENDPOINT Descriptor Type @@ -379,8 +351,7 @@ typedef struct TU_ATTR_PACKED TU_VERIFY_STATIC( sizeof(tusb_desc_endpoint_t) == 7, "size is not correct"); /// USB Other Speed Configuration Descriptor -typedef struct TU_ATTR_PACKED -{ +typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of descriptor uint8_t bDescriptorType ; ///< Other_speed_Configuration Type uint16_t wTotalLength ; ///< Total length of data returned @@ -393,8 +364,7 @@ typedef struct TU_ATTR_PACKED } tusb_desc_other_speed_t; /// USB Device Qualifier Descriptor -typedef struct TU_ATTR_PACKED -{ +typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of descriptor uint8_t bDescriptorType ; ///< Device Qualifier Type uint16_t bcdUSB ; ///< USB specification version number (e.g., 0200H for V2.00) @@ -411,8 +381,7 @@ typedef struct TU_ATTR_PACKED TU_VERIFY_STATIC( sizeof(tusb_desc_device_qualifier_t) == 10, "size is not correct"); /// USB Interface Association Descriptor (IAD ECN) -typedef struct TU_ATTR_PACKED -{ +typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of descriptor uint8_t bDescriptorType ; ///< Other_speed_Configuration Type @@ -426,17 +395,17 @@ typedef struct TU_ATTR_PACKED uint8_t iFunction ; ///< Index of the string descriptor describing the interface association. } tusb_desc_interface_assoc_t; +TU_VERIFY_STATIC( sizeof(tusb_desc_interface_assoc_t) == 8, "size is not correct"); + // USB String Descriptor -typedef struct TU_ATTR_PACKED -{ +typedef struct TU_ATTR_PACKED { uint8_t bLength ; ///< Size of this descriptor in bytes uint8_t bDescriptorType ; ///< Descriptor Type uint16_t unicode_string[]; } tusb_desc_string_t; // USB Binary Device Object Store (BOS) -typedef struct TU_ATTR_PACKED -{ +typedef struct TU_ATTR_PACKED { uint8_t bLength; uint8_t bDescriptorType ; uint8_t bDevCapabilityType; @@ -445,9 +414,8 @@ typedef struct TU_ATTR_PACKED uint8_t CapabilityData[]; } tusb_desc_bos_platform_t; -// USB WebuSB URL Descriptor -typedef struct TU_ATTR_PACKED -{ +// USB WebUSB URL Descriptor +typedef struct TU_ATTR_PACKED { uint8_t bLength; uint8_t bDescriptorType; uint8_t bScheme; @@ -455,8 +423,7 @@ typedef struct TU_ATTR_PACKED } tusb_desc_webusb_url_t; // DFU Functional Descriptor -typedef struct TU_ATTR_PACKED -{ +typedef struct TU_ATTR_PACKED { uint8_t bLength; uint8_t bDescriptorType; @@ -481,7 +448,7 @@ typedef struct TU_ATTR_PACKED // //--------------------------------------------------------------------+ -typedef struct TU_ATTR_PACKED{ +typedef struct TU_ATTR_PACKED { union { struct TU_ATTR_PACKED { uint8_t recipient : 5; ///< Recipient type tusb_request_recipient_t. @@ -500,7 +467,6 @@ typedef struct TU_ATTR_PACKED{ TU_VERIFY_STATIC( sizeof(tusb_control_request_t) == 8, "size is not correct"); - TU_ATTR_PACKED_END // End of all packed definitions TU_ATTR_BIT_FIELD_ORDER_END @@ -509,36 +475,25 @@ TU_ATTR_BIT_FIELD_ORDER_END //--------------------------------------------------------------------+ // Get direction from Endpoint address -TU_ATTR_ALWAYS_INLINE static inline tusb_dir_t tu_edpt_dir(uint8_t addr) -{ +TU_ATTR_ALWAYS_INLINE static inline tusb_dir_t tu_edpt_dir(uint8_t addr) { return (addr & TUSB_DIR_IN_MASK) ? TUSB_DIR_IN : TUSB_DIR_OUT; } // Get Endpoint number from address -TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_edpt_number(uint8_t addr) -{ +TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_edpt_number(uint8_t addr) { return (uint8_t)(addr & (~TUSB_DIR_IN_MASK)); } -TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_edpt_addr(uint8_t num, uint8_t dir) -{ +TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_edpt_addr(uint8_t num, uint8_t dir) { return (uint8_t)(num | (dir ? TUSB_DIR_IN_MASK : 0)); } -TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_edpt_packet_size(tusb_desc_endpoint_t const* desc_ep) -{ - return tu_le16toh(desc_ep->wMaxPacketSize) & TU_GENMASK(10, 0); +TU_ATTR_ALWAYS_INLINE static inline uint16_t tu_edpt_packet_size(tusb_desc_endpoint_t const* desc_ep) { + return tu_le16toh(desc_ep->wMaxPacketSize) & 0x7FF; } #if CFG_TUSB_DEBUG -TU_ATTR_ALWAYS_INLINE static inline const char *tu_edpt_dir_str(tusb_dir_t dir) -{ - tu_static const char *str[] = {"out", "in"}; - return str[dir]; -} - -TU_ATTR_ALWAYS_INLINE static inline const char *tu_edpt_type_str(tusb_xfer_type_t t) -{ +TU_ATTR_ALWAYS_INLINE static inline const char *tu_edpt_type_str(tusb_xfer_type_t t) { tu_static const char *str[] = {"control", "isochronous", "bulk", "interrupt"}; return str[t]; } @@ -549,21 +504,18 @@ TU_ATTR_ALWAYS_INLINE static inline const char *tu_edpt_type_str(tusb_xfer_type_ //--------------------------------------------------------------------+ // return next descriptor -TU_ATTR_ALWAYS_INLINE static inline uint8_t const * tu_desc_next(void const* desc) -{ +TU_ATTR_ALWAYS_INLINE static inline uint8_t const * tu_desc_next(void const* desc) { uint8_t const* desc8 = (uint8_t const*) desc; return desc8 + desc8[DESC_OFFSET_LEN]; } // get descriptor type -TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_type(void const* desc) -{ +TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_type(void const* desc) { return ((uint8_t const*) desc)[DESC_OFFSET_TYPE]; } // get descriptor length -TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_len(void const* desc) -{ +TU_ATTR_ALWAYS_INLINE static inline uint8_t tu_desc_len(void const* desc) { return ((uint8_t const*) desc)[DESC_OFFSET_LEN]; } @@ -580,6 +532,4 @@ uint8_t const * tu_desc_find3(uint8_t const* desc, uint8_t const* end, uint8_t b } #endif -#endif /* _TUSB_TYPES_H_ */ - -/** @} */ +#endif // TUSB_TYPES_H_ diff --git a/pico-sdk/lib/tinyusb/src/common/tusb_verify.h b/pico-sdk/lib/tinyusb/src/common/tusb_verify.h index 8aa66b4..dde0550 100644 --- a/pico-sdk/lib/tinyusb/src/common/tusb_verify.h +++ b/pico-sdk/lib/tinyusb/src/common/tusb_verify.h @@ -76,14 +76,14 @@ #endif // Halt CPU (breakpoint) when hitting error, only apply for Cortex M3, M4, M7, M33. M55 -#if defined(__ARM_ARCH_7M__) || defined (__ARM_ARCH_7EM__) || defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8_1M_MAIN__) - #define TU_BREAKPOINT() do \ - { \ +#if defined(__ARM_ARCH_7M__) || defined (__ARM_ARCH_7EM__) || defined(__ARM_ARCH_8M_MAIN__) || defined(__ARM_ARCH_8_1M_MAIN__) || \ + defined(__ARM7M__) || defined (__ARM7EM__) || defined(__ARM8M_MAINLINE__) || defined(__ARM8EM_MAINLINE__) + #define TU_BREAKPOINT() do { \ volatile uint32_t* ARM_CM_DHCSR = ((volatile uint32_t*) 0xE000EDF0UL); /* Cortex M CoreDebug->DHCSR */ \ if ( (*ARM_CM_DHCSR) & 1UL ) __asm("BKPT #0\n"); /* Only halt mcu if debugger is attached */ \ } while(0) -#elif defined(__riscv) +#elif defined(__riscv) && !TUP_MCU_ESPRESSIF #define TU_BREAKPOINT() do { __asm("ebreak\n"); } while(0) #elif defined(_mips) diff --git a/pico-sdk/lib/tinyusb/src/device/dcd.h b/pico-sdk/lib/tinyusb/src/device/dcd.h index 69c26bc..9447d6d 100644 --- a/pico-sdk/lib/tinyusb/src/device/dcd.h +++ b/pico-sdk/lib/tinyusb/src/device/dcd.h @@ -97,6 +97,14 @@ typedef struct TU_ATTR_ALIGNED(4) { }; } dcd_event_t; +typedef enum { + TEST_J = 1, + TEST_K, + TEST_SE0_NAK, + TEST_PACKET, + TEST_FORCE_ENABLE, +} test_mode_t; + //TU_VERIFY_STATIC(sizeof(dcd_event_t) <= 12, "size is not correct"); //--------------------------------------------------------------------+ @@ -122,6 +130,9 @@ void dcd_dcache_clean_invalidate(void const* addr, uint32_t data_size) TU_ATTR_W // Initialize controller to device mode void dcd_init(uint8_t rhport); +// Deinitialize controller, unset device mode. +bool dcd_deinit(uint8_t rhport); + // Interrupt Handler void dcd_int_handler(uint8_t rhport); @@ -146,6 +157,13 @@ void dcd_disconnect(uint8_t rhport) TU_ATTR_WEAK; // Enable/Disable Start-of-frame interrupt. Default is disabled void dcd_sof_enable(uint8_t rhport, bool en); +#if CFG_TUD_TEST_MODE +// Check if the test mode is supported, returns true is test mode selector is supported +bool dcd_check_test_mode_support(test_mode_t test_selector) TU_ATTR_WEAK; + +// Put device into a test mode (needs power cycle to quit) +void dcd_enter_test_mode(uint8_t rhport, test_mode_t test_selector) TU_ATTR_WEAK; +#endif //--------------------------------------------------------------------+ // Endpoint API //--------------------------------------------------------------------+ diff --git a/pico-sdk/lib/tinyusb/src/device/usbd.c b/pico-sdk/lib/tinyusb/src/device/usbd.c index 5c94ebc..83100ef 100644 --- a/pico-sdk/lib/tinyusb/src/device/usbd.c +++ b/pico-sdk/lib/tinyusb/src/device/usbd.c @@ -39,18 +39,27 @@ // USBD Configuration //--------------------------------------------------------------------+ #ifndef CFG_TUD_TASK_QUEUE_SZ - #define CFG_TUD_TASK_QUEUE_SZ 16 + #define CFG_TUD_TASK_QUEUE_SZ 32 #endif //--------------------------------------------------------------------+ -// Callback weak stubs (called if application does not provide) +// Weak stubs: invoked if no strong implementation is available //--------------------------------------------------------------------+ +TU_ATTR_WEAK bool dcd_deinit(uint8_t rhport) { + (void) rhport; + return false; +} + TU_ATTR_WEAK void tud_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr) { (void)rhport; (void)eventid; (void)in_isr; } +TU_ATTR_WEAK void tud_sof_cb(uint32_t frame_count) { + (void)frame_count; +} + //--------------------------------------------------------------------+ // Device Data //--------------------------------------------------------------------+ @@ -68,9 +77,10 @@ typedef struct { uint8_t remote_wakeup_support : 1; // configuration descriptor's attribute uint8_t self_powered : 1; // configuration descriptor's attribute }; - volatile uint8_t cfg_num; // current active configuration (0x00 is not configured) uint8_t speed; + volatile uint8_t setup_count; + volatile uint8_t sof_consumer; uint8_t itf2drv[CFG_TUD_INTERFACE_MAX]; // map interface number to driver (0xff is invalid) uint8_t ep2drv[CFG_TUD_ENDPPOINT_MAX][2]; // map endpoint to driver ( 0xff is invalid ), can use only 4-bit each @@ -85,17 +95,18 @@ tu_static usbd_device_t _usbd_dev; // Class Driver //--------------------------------------------------------------------+ #if CFG_TUSB_DEBUG >= CFG_TUD_LOG_LEVEL - #define DRIVER_NAME(_name) .name = _name, + #define DRIVER_NAME(_name) _name #else - #define DRIVER_NAME(_name) + #define DRIVER_NAME(_name) NULL #endif // Built-in class drivers tu_static usbd_class_driver_t const _usbd_driver[] = { #if CFG_TUD_CDC { - DRIVER_NAME("CDC") + .name = DRIVER_NAME("CDC"), .init = cdcd_init, + .deinit = cdcd_deinit, .reset = cdcd_reset, .open = cdcd_open, .control_xfer_cb = cdcd_control_xfer_cb, @@ -106,8 +117,9 @@ tu_static usbd_class_driver_t const _usbd_driver[] = { #if CFG_TUD_MSC { - DRIVER_NAME("MSC") + .name = DRIVER_NAME("MSC"), .init = mscd_init, + .deinit = NULL, .reset = mscd_reset, .open = mscd_open, .control_xfer_cb = mscd_control_xfer_cb, @@ -118,121 +130,131 @@ tu_static usbd_class_driver_t const _usbd_driver[] = { #if CFG_TUD_HID { - DRIVER_NAME("HID") - .init = hidd_init, - .reset = hidd_reset, - .open = hidd_open, - .control_xfer_cb = hidd_control_xfer_cb, - .xfer_cb = hidd_xfer_cb, - .sof = NULL + .name = DRIVER_NAME("HID"), + .init = hidd_init, + .deinit = hidd_deinit, + .reset = hidd_reset, + .open = hidd_open, + .control_xfer_cb = hidd_control_xfer_cb, + .xfer_cb = hidd_xfer_cb, + .sof = NULL }, #endif #if CFG_TUD_AUDIO { - DRIVER_NAME("AUDIO") - .init = audiod_init, - .reset = audiod_reset, - .open = audiod_open, - .control_xfer_cb = audiod_control_xfer_cb, - .xfer_cb = audiod_xfer_cb, - .sof = audiod_sof_isr + .name = DRIVER_NAME("AUDIO"), + .init = audiod_init, + .deinit = audiod_deinit, + .reset = audiod_reset, + .open = audiod_open, + .control_xfer_cb = audiod_control_xfer_cb, + .xfer_cb = audiod_xfer_cb, + .sof = audiod_sof_isr }, #endif #if CFG_TUD_VIDEO { - DRIVER_NAME("VIDEO") - .init = videod_init, - .reset = videod_reset, - .open = videod_open, - .control_xfer_cb = videod_control_xfer_cb, - .xfer_cb = videod_xfer_cb, - .sof = NULL + .name = DRIVER_NAME("VIDEO"), + .init = videod_init, + .deinit = videod_deinit, + .reset = videod_reset, + .open = videod_open, + .control_xfer_cb = videod_control_xfer_cb, + .xfer_cb = videod_xfer_cb, + .sof = NULL }, #endif #if CFG_TUD_MIDI { - DRIVER_NAME("MIDI") - .init = midid_init, - .open = midid_open, - .reset = midid_reset, - .control_xfer_cb = midid_control_xfer_cb, - .xfer_cb = midid_xfer_cb, - .sof = NULL + .name = DRIVER_NAME("MIDI"), + .init = midid_init, + .deinit = midid_deinit, + .open = midid_open, + .reset = midid_reset, + .control_xfer_cb = midid_control_xfer_cb, + .xfer_cb = midid_xfer_cb, + .sof = NULL }, #endif #if CFG_TUD_VENDOR { - DRIVER_NAME("VENDOR") - .init = vendord_init, - .reset = vendord_reset, - .open = vendord_open, - .control_xfer_cb = tud_vendor_control_xfer_cb, - .xfer_cb = vendord_xfer_cb, - .sof = NULL + .name = DRIVER_NAME("VENDOR"), + .init = vendord_init, + .deinit = vendord_deinit, + .reset = vendord_reset, + .open = vendord_open, + .control_xfer_cb = tud_vendor_control_xfer_cb, + .xfer_cb = vendord_xfer_cb, + .sof = NULL }, #endif #if CFG_TUD_USBTMC { - DRIVER_NAME("TMC") - .init = usbtmcd_init_cb, - .reset = usbtmcd_reset_cb, - .open = usbtmcd_open_cb, - .control_xfer_cb = usbtmcd_control_xfer_cb, - .xfer_cb = usbtmcd_xfer_cb, - .sof = NULL + .name = DRIVER_NAME("TMC"), + .init = usbtmcd_init_cb, + .deinit = usbtmcd_deinit, + .reset = usbtmcd_reset_cb, + .open = usbtmcd_open_cb, + .control_xfer_cb = usbtmcd_control_xfer_cb, + .xfer_cb = usbtmcd_xfer_cb, + .sof = NULL }, #endif #if CFG_TUD_DFU_RUNTIME { - DRIVER_NAME("DFU-RUNTIME") - .init = dfu_rtd_init, - .reset = dfu_rtd_reset, - .open = dfu_rtd_open, - .control_xfer_cb = dfu_rtd_control_xfer_cb, - .xfer_cb = NULL, - .sof = NULL + .name = DRIVER_NAME("DFU-RUNTIME"), + .init = dfu_rtd_init, + .deinit = dfu_rtd_deinit, + .reset = dfu_rtd_reset, + .open = dfu_rtd_open, + .control_xfer_cb = dfu_rtd_control_xfer_cb, + .xfer_cb = NULL, + .sof = NULL }, #endif #if CFG_TUD_DFU { - DRIVER_NAME("DFU") - .init = dfu_moded_init, - .reset = dfu_moded_reset, - .open = dfu_moded_open, - .control_xfer_cb = dfu_moded_control_xfer_cb, - .xfer_cb = NULL, - .sof = NULL + .name = DRIVER_NAME("DFU"), + .init = dfu_moded_init, + .deinit = dfu_moded_deinit, + .reset = dfu_moded_reset, + .open = dfu_moded_open, + .control_xfer_cb = dfu_moded_control_xfer_cb, + .xfer_cb = NULL, + .sof = NULL }, #endif #if CFG_TUD_ECM_RNDIS || CFG_TUD_NCM { - DRIVER_NAME("NET") - .init = netd_init, - .reset = netd_reset, - .open = netd_open, - .control_xfer_cb = netd_control_xfer_cb, - .xfer_cb = netd_xfer_cb, - .sof = NULL, + .name = DRIVER_NAME("NET"), + .init = netd_init, + .deinit = netd_deinit, + .reset = netd_reset, + .open = netd_open, + .control_xfer_cb = netd_control_xfer_cb, + .xfer_cb = netd_xfer_cb, + .sof = NULL, }, #endif #if CFG_TUD_BTH { - DRIVER_NAME("BTH") - .init = btd_init, - .reset = btd_reset, - .open = btd_open, - .control_xfer_cb = btd_control_xfer_cb, - .xfer_cb = btd_xfer_cb, - .sof = NULL + .name = DRIVER_NAME("BTH"), + .init = btd_init, + .deinit = btd_deinit, + .reset = btd_reset, + .open = btd_open, + .control_xfer_cb = btd_control_xfer_cb, + .xfer_cb = btd_xfer_cb, + .sof = NULL }, #endif }; @@ -258,6 +280,7 @@ TU_ATTR_ALWAYS_INLINE static inline usbd_class_driver_t const * get_driver(uint8 return driver; } + //--------------------------------------------------------------------+ // DCD Event //--------------------------------------------------------------------+ @@ -279,9 +302,9 @@ tu_static osal_queue_t _usbd_q; #endif TU_ATTR_ALWAYS_INLINE static inline bool queue_event(dcd_event_t const * event, bool in_isr) { - bool ret = osal_queue_send(_usbd_q, event, in_isr); + TU_ASSERT(osal_queue_send(_usbd_q, event, in_isr)); tud_event_hook_cb(event->rhport, event->event_id, in_isr); - return ret; + return true; } //--------------------------------------------------------------------+ @@ -290,7 +313,9 @@ TU_ATTR_ALWAYS_INLINE static inline bool queue_event(dcd_event_t const * event, static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request); static bool process_set_config(uint8_t rhport, uint8_t cfg_num); static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const * p_request); - +#if CFG_TUD_TEST_MODE +static bool process_test_mode_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request); +#endif // from usbd_control.c void usbd_control_reset(void); void usbd_control_set_request(tusb_control_request_t const *request); @@ -319,7 +344,7 @@ void usbd_driver_print_control_complete_name(usbd_control_xfer_cb_t callback) { for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) { usbd_class_driver_t const* driver = get_driver(i); if (driver && driver->control_xfer_cb == callback) { - TU_LOG_USBD(" %s control complete\r\n", driver->name); + TU_LOG_USBD("%s control complete\r\n", driver->name); return; } } @@ -365,6 +390,12 @@ bool tud_connect(void) { return true; } +bool tud_sof_cb_enable(bool en) +{ + usbd_sof_enable(_usbd_rhport, SOF_CONSUMER_USER, en); + return true; +} + //--------------------------------------------------------------------+ // USBD Task //--------------------------------------------------------------------+ @@ -372,13 +403,13 @@ bool tud_inited(void) { return _usbd_rhport != RHPORT_INVALID; } -bool tud_init (uint8_t rhport) -{ +bool tud_init(uint8_t rhport) { // skip if already initialized - if ( tud_inited() ) return true; + if (tud_inited()) return true; TU_LOG_USBD("USBD init on controller %u\r\n", rhport); TU_LOG_INT(CFG_TUD_LOG_LEVEL, sizeof(usbd_device_t)); + TU_LOG_INT(CFG_TUD_LOG_LEVEL, sizeof(dcd_event_t)); TU_LOG_INT(CFG_TUD_LOG_LEVEL, sizeof(tu_fifo_t)); TU_LOG_INT(CFG_TUD_LOG_LEVEL, sizeof(tu_edpt_stream_t)); @@ -395,16 +426,14 @@ bool tud_init (uint8_t rhport) TU_ASSERT(_usbd_q); // Get application driver if available - if ( usbd_app_driver_get_cb ) - { + if (usbd_app_driver_get_cb) { _app_driver = usbd_app_driver_get_cb(&_app_driver_count); } // Init class drivers - for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) - { - usbd_class_driver_t const * driver = get_driver(i); - TU_ASSERT(driver); + for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) { + usbd_class_driver_t const* driver = get_driver(i); + TU_ASSERT(driver && driver->init); TU_LOG_USBD("%s init\r\n", driver->name); driver->init(); } @@ -418,31 +447,61 @@ bool tud_init (uint8_t rhport) return true; } -static void configuration_reset(uint8_t rhport) -{ - for ( uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++ ) - { - usbd_class_driver_t const * driver = get_driver(i); - TU_ASSERT(driver, ); +bool tud_deinit(uint8_t rhport) { + // skip if not initialized + if (!tud_inited()) return true; + + TU_LOG_USBD("USBD deinit on controller %u\r\n", rhport); + + // Deinit device controller driver + dcd_int_disable(rhport); + dcd_disconnect(rhport); + dcd_deinit(rhport); + + // Deinit class drivers + for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) { + usbd_class_driver_t const* driver = get_driver(i); + if(driver && driver->deinit) { + TU_LOG_USBD("%s deinit\r\n", driver->name); + driver->deinit(); + } + } + + // Deinit device queue & task + osal_queue_delete(_usbd_q); + _usbd_q = NULL; + +#if OSAL_MUTEX_REQUIRED + // TODO make sure there is no task waiting on this mutex + osal_mutex_delete(_usbd_mutex); + _usbd_mutex = NULL; +#endif + + _usbd_rhport = RHPORT_INVALID; + + return true; +} + +static void configuration_reset(uint8_t rhport) { + for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) { + usbd_class_driver_t const* driver = get_driver(i); + TU_ASSERT(driver,); driver->reset(rhport); } tu_varclr(&_usbd_dev); memset(_usbd_dev.itf2drv, DRVID_INVALID, sizeof(_usbd_dev.itf2drv)); // invalid mapping - memset(_usbd_dev.ep2drv , DRVID_INVALID, sizeof(_usbd_dev.ep2drv )); // invalid mapping + memset(_usbd_dev.ep2drv, DRVID_INVALID, sizeof(_usbd_dev.ep2drv)); // invalid mapping } -static void usbd_reset(uint8_t rhport) -{ +static void usbd_reset(uint8_t rhport) { configuration_reset(rhport); usbd_control_reset(); } -bool tud_task_event_ready(void) -{ +bool tud_task_event_ready(void) { // Skip if stack is not initialized - if ( !tud_inited() ) return false; - + if (!tud_inited()) return false; return !osal_queue_empty(_usbd_q); } @@ -450,57 +509,52 @@ bool tud_task_event_ready(void) * This top level thread manages all device controller event and delegates events to class-specific drivers. * This should be called periodically within the mainloop or rtos thread. * - @code - int main(void) - { + int main(void) { application_init(); tusb_init(); - while(1) // the mainloop - { + while(1) { // the mainloop application_code(); tud_task(); // tinyusb device task } } - @endcode */ -void tud_task_ext(uint32_t timeout_ms, bool in_isr) -{ +void tud_task_ext(uint32_t timeout_ms, bool in_isr) { (void) in_isr; // not implemented yet // Skip if stack is not initialized - if ( !tud_inited() ) return; + if (!tud_inited()) return; // Loop until there is no more events in the queue - while (1) - { + while (1) { dcd_event_t event; - if ( !osal_queue_receive(_usbd_q, &event, timeout_ms) ) return; + if (!osal_queue_receive(_usbd_q, &event, timeout_ms)) return; #if CFG_TUSB_DEBUG >= CFG_TUD_LOG_LEVEL if (event.event_id == DCD_EVENT_SETUP_RECEIVED) TU_LOG_USBD("\r\n"); // extra line for setup TU_LOG_USBD("USBD %s ", event.event_id < DCD_EVENT_COUNT ? _usbd_event_str[event.event_id] : "CORRUPTED"); #endif - switch ( event.event_id ) - { + switch (event.event_id) { case DCD_EVENT_BUS_RESET: TU_LOG_USBD(": %s Speed\r\n", tu_str_speed[event.bus_reset.speed]); usbd_reset(event.rhport); _usbd_dev.speed = event.bus_reset.speed; - break; + break; case DCD_EVENT_UNPLUGGED: TU_LOG_USBD("\r\n"); usbd_reset(event.rhport); - - // invoke callback if (tud_umount_cb) tud_umount_cb(); - break; + break; case DCD_EVENT_SETUP_RECEIVED: + _usbd_dev.setup_count--; TU_LOG_BUF(CFG_TUD_LOG_LEVEL, &event.setup_received, 8); - TU_LOG_USBD("\r\n"); + if (_usbd_dev.setup_count) { + TU_LOG_USBD(" Skipped since there is other SETUP in queue\r\n"); + break; + } // Mark as connected after receiving 1st setup packet. // But it is easier to set it every time instead of wasting time to check then set @@ -509,87 +563,82 @@ void tud_task_ext(uint32_t timeout_ms, bool in_isr) // mark both in & out control as free _usbd_dev.ep_status[0][TUSB_DIR_OUT].busy = 0; _usbd_dev.ep_status[0][TUSB_DIR_OUT].claimed = 0; - _usbd_dev.ep_status[0][TUSB_DIR_IN ].busy = 0; - _usbd_dev.ep_status[0][TUSB_DIR_IN ].claimed = 0; + _usbd_dev.ep_status[0][TUSB_DIR_IN].busy = 0; + _usbd_dev.ep_status[0][TUSB_DIR_IN].claimed = 0; // Process control request - if ( !process_control_request(event.rhport, &event.setup_received) ) - { + if (!process_control_request(event.rhport, &event.setup_received)) { TU_LOG_USBD(" Stall EP0\r\n"); // Failed -> stall both control endpoint IN and OUT dcd_edpt_stall(event.rhport, 0); dcd_edpt_stall(event.rhport, 0 | TUSB_DIR_IN_MASK); } - break; + break; - case DCD_EVENT_XFER_COMPLETE: - { + case DCD_EVENT_XFER_COMPLETE: { // Invoke the class callback associated with the endpoint address uint8_t const ep_addr = event.xfer_complete.ep_addr; - uint8_t const epnum = tu_edpt_number(ep_addr); - uint8_t const ep_dir = tu_edpt_dir(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const ep_dir = tu_edpt_dir(ep_addr); TU_LOG_USBD("on EP %02X with %u bytes\r\n", ep_addr, (unsigned int) event.xfer_complete.len); _usbd_dev.ep_status[epnum][ep_dir].busy = 0; _usbd_dev.ep_status[epnum][ep_dir].claimed = 0; - if ( 0 == epnum ) - { - usbd_control_xfer_cb(event.rhport, ep_addr, (xfer_result_t) event.xfer_complete.result, event.xfer_complete - .len); - } - else - { - usbd_class_driver_t const * driver = get_driver( _usbd_dev.ep2drv[epnum][ep_dir] ); - TU_ASSERT(driver, ); + if (0 == epnum) { + usbd_control_xfer_cb(event.rhport, ep_addr, (xfer_result_t) event.xfer_complete.result, + event.xfer_complete.len); + } else { + usbd_class_driver_t const* driver = get_driver(_usbd_dev.ep2drv[epnum][ep_dir]); + TU_ASSERT(driver,); TU_LOG_USBD(" %s xfer callback\r\n", driver->name); driver->xfer_cb(event.rhport, ep_addr, (xfer_result_t) event.xfer_complete.result, event.xfer_complete.len); } + break; } - break; case DCD_EVENT_SUSPEND: // NOTE: When plugging/unplugging device, the D+/D- state are unstable and // can accidentally meet the SUSPEND condition ( Bus Idle for 3ms ), which result in a series of event // e.g suspend -> resume -> unplug/plug. Skip suspend/resume if not connected - if ( _usbd_dev.connected ) - { + if (_usbd_dev.connected) { TU_LOG_USBD(": Remote Wakeup = %u\r\n", _usbd_dev.remote_wakeup_en); if (tud_suspend_cb) tud_suspend_cb(_usbd_dev.remote_wakeup_en); - }else - { + } else { TU_LOG_USBD(" Skipped\r\n"); } - break; + break; case DCD_EVENT_RESUME: - if ( _usbd_dev.connected ) - { + if (_usbd_dev.connected) { TU_LOG_USBD("\r\n"); if (tud_resume_cb) tud_resume_cb(); - }else - { + } else { TU_LOG_USBD(" Skipped\r\n"); } - break; + break; case USBD_EVENT_FUNC_CALL: TU_LOG_USBD("\r\n"); - if ( event.func_call.func ) event.func_call.func(event.func_call.param); - break; + if (event.func_call.func) event.func_call.func(event.func_call.param); + break; case DCD_EVENT_SOF: + if (tu_bit_test(_usbd_dev.sof_consumer, SOF_CONSUMER_USER)) { + TU_LOG_USBD("\r\n"); + tud_sof_cb(event.sof.frame_count); + } + break; + default: TU_BREAKPOINT(); - break; + break; } -#if CFG_TUSB_OS != OPT_OS_NONE && CFG_TUSB_OS != OPT_OS_PICO // return if there is no more events, for application to run other background if (osal_queue_empty(_usbd_q)) return; -#endif } } @@ -598,24 +647,20 @@ void tud_task_ext(uint32_t timeout_ms, bool in_isr) //--------------------------------------------------------------------+ // Helper to invoke class driver control request handler -static bool invoke_class_control(uint8_t rhport, usbd_class_driver_t const * driver, tusb_control_request_t const * request) -{ +static bool invoke_class_control(uint8_t rhport, usbd_class_driver_t const * driver, tusb_control_request_t const * request) { usbd_control_set_complete_callback(driver->control_xfer_cb); TU_LOG_USBD(" %s control request\r\n", driver->name); return driver->control_xfer_cb(rhport, CONTROL_STAGE_SETUP, request); } // This handles the actual request and its response. -// return false will cause its caller to stall control endpoint -static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request) -{ +// Returns false if unable to complete the request, causing caller to stall control endpoints. +static bool process_control_request(uint8_t rhport, tusb_control_request_t const * p_request) { usbd_control_set_complete_callback(NULL); - TU_ASSERT(p_request->bmRequestType_bit.type < TUSB_REQ_TYPE_INVALID); // Vendor request - if ( p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_VENDOR ) - { + if ( p_request->bmRequestType_bit.type == TUSB_REQ_TYPE_VENDOR ) { TU_VERIFY(tud_vendor_control_xfer_cb); usbd_control_set_complete_callback(tud_vendor_control_xfer_cb); @@ -623,19 +668,16 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const } #if CFG_TUSB_DEBUG >= CFG_TUD_LOG_LEVEL - if (TUSB_REQ_TYPE_STANDARD == p_request->bmRequestType_bit.type && p_request->bRequest <= TUSB_REQ_SYNCH_FRAME) - { + if (TUSB_REQ_TYPE_STANDARD == p_request->bmRequestType_bit.type && p_request->bRequest <= TUSB_REQ_SYNCH_FRAME) { TU_LOG_USBD(" %s", tu_str_std_request[p_request->bRequest]); if (TUSB_REQ_GET_DESCRIPTOR != p_request->bRequest) TU_LOG_USBD("\r\n"); } #endif - switch ( p_request->bmRequestType_bit.recipient ) - { + switch ( p_request->bmRequestType_bit.recipient ) { //------------- Device Requests e.g in enumeration -------------// case TUSB_REQ_RCPT_DEVICE: - if ( TUSB_REQ_TYPE_CLASS == p_request->bmRequestType_bit.type ) - { + if ( TUSB_REQ_TYPE_CLASS == p_request->bmRequestType_bit.type ) { uint8_t const itf = tu_u16_low(p_request->wIndex); TU_VERIFY(itf < TU_ARRAY_SIZE(_usbd_dev.itf2drv)); @@ -646,15 +688,13 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const return invoke_class_control(rhport, driver, p_request); } - if ( TUSB_REQ_TYPE_STANDARD != p_request->bmRequestType_bit.type ) - { + if ( TUSB_REQ_TYPE_STANDARD != p_request->bmRequestType_bit.type ) { // Non standard request is not supported TU_BREAKPOINT(); return false; } - switch ( p_request->bRequest ) - { + switch ( p_request->bRequest ) { case TUSB_REQ_SET_ADDRESS: // Depending on mcu, status phase could be sent either before or after changing device address, // or even require stack to not response with status at all @@ -665,25 +705,24 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const _usbd_dev.addressed = 1; break; - case TUSB_REQ_GET_CONFIGURATION: - { + case TUSB_REQ_GET_CONFIGURATION: { uint8_t cfg_num = _usbd_dev.cfg_num; tud_control_xfer(rhport, p_request, &cfg_num, 1); } break; - case TUSB_REQ_SET_CONFIGURATION: - { + case TUSB_REQ_SET_CONFIGURATION: { uint8_t const cfg_num = (uint8_t) p_request->wValue; // Only process if new configure is different - if (_usbd_dev.cfg_num != cfg_num) - { - if ( _usbd_dev.cfg_num ) - { + if (_usbd_dev.cfg_num != cfg_num) { + if ( _usbd_dev.cfg_num ) { // already configured: need to clear all endpoints and driver first TU_LOG_USBD(" Clear current Configuration (%u) before switching\r\n", _usbd_dev.cfg_num); + // disable SOF + dcd_sof_enable(rhport, false); + // close all non-control endpoints, cancel all pending transfers if any dcd_edpt_close_all(rhport); @@ -695,15 +734,11 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const } // Handle the new configuration and execute the corresponding callback - if ( cfg_num ) - { + if ( cfg_num ) { // switch to new configuration if not zero TU_ASSERT( process_set_config(rhport, cfg_num) ); - if ( tud_mount_cb ) tud_mount_cb(); - } - else - { + } else { if ( tud_umount_cb ) tud_umount_cb(); } } @@ -718,14 +753,47 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const break; case TUSB_REQ_SET_FEATURE: - // Only support remote wakeup for device feature - TU_VERIFY(TUSB_REQ_FEATURE_REMOTE_WAKEUP == p_request->wValue); + // Handle the feature selector + switch(p_request->wValue) + { + // Support for remote wakeup + case TUSB_REQ_FEATURE_REMOTE_WAKEUP: + TU_LOG_USBD(" Enable Remote Wakeup\r\n"); - TU_LOG_USBD(" Enable Remote Wakeup\r\n"); + // Host may enable remote wake up before suspending especially HID device + _usbd_dev.remote_wakeup_en = true; + tud_control_status(rhport, p_request); + break; - // Host may enable remote wake up before suspending especially HID device - _usbd_dev.remote_wakeup_en = true; - tud_control_status(rhport, p_request); +#if CFG_TUD_TEST_MODE + // Support for TEST_MODE + case TUSB_REQ_FEATURE_TEST_MODE: { + // Only handle the test mode if supported and valid + TU_VERIFY(dcd_enter_test_mode && dcd_check_test_mode_support && 0 == tu_u16_low(p_request->wIndex)); + + uint8_t selector = tu_u16_high(p_request->wIndex); + + // Stall request if the selected test mode isn't supported + if (!dcd_check_test_mode_support((test_mode_t)selector)) + { + TU_LOG_USBD(" Unsupported Test Mode (test selector index: %d)\r\n", selector); + + return false; + } + + // Acknowledge request + tud_control_status(rhport, p_request); + + TU_LOG_USBD(" Enter Test Mode (test selector index: %d)\r\n", selector); + + usbd_control_set_complete_callback(process_test_mode_cb); + break; + } +#endif /* CFG_TUD_TEST_MODE */ + + // Stall unsupported feature selector + default: return false; + } break; case TUSB_REQ_CLEAR_FEATURE: @@ -739,15 +807,14 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const tud_control_status(rhport, p_request); break; - case TUSB_REQ_GET_STATUS: - { + case TUSB_REQ_GET_STATUS: { // Device status bit mask // - Bit 0: Self Powered // - Bit 1: Remote Wakeup enabled uint16_t status = (uint16_t) ((_usbd_dev.self_powered ? 1u : 0u) | (_usbd_dev.remote_wakeup_en ? 2u : 0u)); tud_control_xfer(rhport, p_request, &status, 2); + break; } - break; // Unknown/Unsupported request default: TU_BREAKPOINT(); return false; @@ -755,8 +822,7 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const break; //------------- Class/Interface Specific Request -------------// - case TUSB_REQ_RCPT_INTERFACE: - { + case TUSB_REQ_RCPT_INTERFACE: { uint8_t const itf = tu_u16_low(p_request->wIndex); TU_VERIFY(itf < TU_ARRAY_SIZE(_usbd_dev.itf2drv)); @@ -765,25 +831,21 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const // all requests to Interface (STD or Class) is forwarded to class driver. // notable requests are: GET HID REPORT DESCRIPTOR, SET_INTERFACE, GET_INTERFACE - if ( !invoke_class_control(rhport, driver, p_request) ) - { + if ( !invoke_class_control(rhport, driver, p_request) ) { // For GET_INTERFACE and SET_INTERFACE, it is mandatory to respond even if the class // driver doesn't use alternate settings or implement this TU_VERIFY(TUSB_REQ_TYPE_STANDARD == p_request->bmRequestType_bit.type); - switch(p_request->bRequest) - { + switch(p_request->bRequest) { case TUSB_REQ_GET_INTERFACE: case TUSB_REQ_SET_INTERFACE: // Clear complete callback if driver set since it can also stall the request. usbd_control_set_complete_callback(NULL); - if (TUSB_REQ_GET_INTERFACE == p_request->bRequest) - { + if (TUSB_REQ_GET_INTERFACE == p_request->bRequest) { uint8_t alternate = 0; tud_control_xfer(rhport, p_request, &alternate, 1); - }else - { + }else { tud_control_status(rhport, p_request); } break; @@ -791,54 +853,42 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const default: return false; } } + break; } - break; //------------- Endpoint Request -------------// - case TUSB_REQ_RCPT_ENDPOINT: - { + case TUSB_REQ_RCPT_ENDPOINT: { uint8_t const ep_addr = tu_u16_low(p_request->wIndex); uint8_t const ep_num = tu_edpt_number(ep_addr); uint8_t const ep_dir = tu_edpt_dir(ep_addr); TU_ASSERT(ep_num < TU_ARRAY_SIZE(_usbd_dev.ep2drv) ); - usbd_class_driver_t const * driver = get_driver(_usbd_dev.ep2drv[ep_num][ep_dir]); - if ( TUSB_REQ_TYPE_STANDARD != p_request->bmRequestType_bit.type ) - { + if ( TUSB_REQ_TYPE_STANDARD != p_request->bmRequestType_bit.type ) { // Forward class request to its driver TU_VERIFY(driver); return invoke_class_control(rhport, driver, p_request); - } - else - { + } else { // Handle STD request to endpoint - switch ( p_request->bRequest ) - { - case TUSB_REQ_GET_STATUS: - { + switch ( p_request->bRequest ) { + case TUSB_REQ_GET_STATUS: { uint16_t status = usbd_edpt_stalled(rhport, ep_addr) ? 0x0001 : 0x0000; tud_control_xfer(rhport, p_request, &status, 2); } break; case TUSB_REQ_CLEAR_FEATURE: - case TUSB_REQ_SET_FEATURE: - { - if ( TUSB_REQ_FEATURE_EDPT_HALT == p_request->wValue ) - { - if ( TUSB_REQ_CLEAR_FEATURE == p_request->bRequest ) - { + case TUSB_REQ_SET_FEATURE: { + if ( TUSB_REQ_FEATURE_EDPT_HALT == p_request->wValue ) { + if ( TUSB_REQ_CLEAR_FEATURE == p_request->bRequest ) { usbd_edpt_clear_stall(rhport, ep_addr); - }else - { + }else { usbd_edpt_stall(rhport, ep_addr); } } - if (driver) - { + if (driver) { // Some classes such as USBTMC needs to clear/re-init its buffer when receiving CLEAR_FEATURE request // We will also forward std request targeted endpoint to class drivers as well @@ -854,14 +904,18 @@ static bool process_control_request(uint8_t rhport, tusb_control_request_t const break; // Unknown/Unsupported request - default: TU_BREAKPOINT(); return false; + default: + TU_BREAKPOINT(); + return false; } } } break; // Unknown recipient - default: TU_BREAKPOINT(); return false; + default: + TU_BREAKPOINT(); + return false; } return true; @@ -1067,6 +1121,20 @@ static bool process_get_descriptor(uint8_t rhport, tusb_control_request_t const } } +#if CFG_TUD_TEST_MODE +static bool process_test_mode_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request) +{ + // At this point it should already be ensured that dcd_enter_test_mode() is defined + + // Only enter the test mode after the request for it has completed + TU_VERIFY(CONTROL_STAGE_ACK == stage); + + dcd_enter_test_mode(rhport, (test_mode_t)tu_u16_high(request->wIndex)); + + return true; +} +#endif /* CFG_TUD_TEST_MODE */ + //--------------------------------------------------------------------+ // DCD Event Handler //--------------------------------------------------------------------+ @@ -1101,6 +1169,14 @@ TU_ATTR_FAST_FUNC void dcd_event_handler(dcd_event_t const* event, bool in_isr) break; case DCD_EVENT_SOF: + // SOF driver handler in ISR context + for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) { + usbd_class_driver_t const* driver = get_driver(i); + if (driver && driver->sof) { + driver->sof(event->rhport, event->sof.frame_count); + } + } + // Some MCUs after running dcd_remote_wakeup() does not have way to detect the end of remote wakeup // which last 1-15 ms. DCD can use SOF as a clear indicator that bus is back to operational if (_usbd_dev.suspended) { @@ -1110,15 +1186,15 @@ TU_ATTR_FAST_FUNC void dcd_event_handler(dcd_event_t const* event, bool in_isr) queue_event(&event_resume, in_isr); } - // SOF driver handler in ISR context - for (uint8_t i = 0; i < TOTAL_DRIVER_COUNT; i++) { - usbd_class_driver_t const* driver = get_driver(i); - if (driver && driver->sof) { - driver->sof(event->rhport, event->sof.frame_count); - } + if (tu_bit_test(_usbd_dev.sof_consumer, SOF_CONSUMER_USER)) { + dcd_event_t const event_sof = {.rhport = event->rhport, .event_id = DCD_EVENT_SOF}; + queue_event(&event_sof, in_isr); } + break; - // skip osal queue for SOF in usbd task + case DCD_EVENT_SETUP_RECEIVED: + _usbd_dev.setup_count++; + send = true; break; default: @@ -1186,8 +1262,7 @@ void usbd_defer_func(osal_task_func_t func, void* param, bool in_isr) { // USBD Endpoint API //--------------------------------------------------------------------+ -bool usbd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * desc_ep) -{ +bool usbd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const* desc_ep) { rhport = _usbd_rhport; TU_ASSERT(tu_edpt_number(desc_ep->bEndpointAddress) < CFG_TUD_ENDPPOINT_MAX); @@ -1196,42 +1271,44 @@ bool usbd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * desc_ep) return dcd_edpt_open(rhport, desc_ep); } -bool usbd_edpt_claim(uint8_t rhport, uint8_t ep_addr) -{ +bool usbd_edpt_claim(uint8_t rhport, uint8_t ep_addr) { (void) rhport; // TODO add this check later, also make sure we don't starve an out endpoint while suspending // TU_VERIFY(tud_ready()); - uint8_t const epnum = tu_edpt_number(ep_addr); - uint8_t const dir = tu_edpt_dir(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); tu_edpt_state_t* ep_state = &_usbd_dev.ep_status[epnum][dir]; return tu_edpt_claim(ep_state, _usbd_mutex); } -bool usbd_edpt_release(uint8_t rhport, uint8_t ep_addr) -{ +bool usbd_edpt_release(uint8_t rhport, uint8_t ep_addr) { (void) rhport; - uint8_t const epnum = tu_edpt_number(ep_addr); - uint8_t const dir = tu_edpt_dir(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); tu_edpt_state_t* ep_state = &_usbd_dev.ep_status[epnum][dir]; return tu_edpt_release(ep_state, _usbd_mutex); } -bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) -{ +bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes) { rhport = _usbd_rhport; uint8_t const epnum = tu_edpt_number(ep_addr); - uint8_t const dir = tu_edpt_dir(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); // TODO skip ready() check for now since enumeration also use this API // TU_VERIFY(tud_ready()); TU_LOG_USBD(" Queue EP %02X with %u bytes ...\r\n", ep_addr, total_bytes); +#if CFG_TUD_LOG_LEVEL >= 3 + if(dir == TUSB_DIR_IN) { + TU_LOG_MEM(CFG_TUD_LOG_LEVEL, buffer, total_bytes, 2); + } +#endif // Attempt to transfer on a busy endpoint, sound like an race condition ! TU_ASSERT(_usbd_dev.ep_status[epnum][dir].busy == 0); @@ -1240,11 +1317,9 @@ bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t // could return and USBD task can preempt and clear the busy _usbd_dev.ep_status[epnum][dir].busy = 1; - if ( dcd_edpt_xfer(rhport, ep_addr, buffer, total_bytes) ) - { + if (dcd_edpt_xfer(rhport, ep_addr, buffer, total_bytes)) { return true; - }else - { + } else { // DCD error, mark endpoint as ready to allow next transfer _usbd_dev.ep_status[epnum][dir].busy = 0; _usbd_dev.ep_status[epnum][dir].claimed = 0; @@ -1258,12 +1333,11 @@ bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t // bytes should be written and second to keep the return value free to give back a boolean // success message. If total_bytes is too big, the FIFO will copy only what is available // into the USB buffer! -bool usbd_edpt_xfer_fifo(uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16_t total_bytes) -{ +bool usbd_edpt_xfer_fifo(uint8_t rhport, uint8_t ep_addr, tu_fifo_t* ff, uint16_t total_bytes) { rhport = _usbd_rhport; uint8_t const epnum = tu_edpt_number(ep_addr); - uint8_t const dir = tu_edpt_dir(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); TU_LOG_USBD(" Queue ISO EP %02X with %u bytes ... ", ep_addr, total_bytes); @@ -1274,12 +1348,10 @@ bool usbd_edpt_xfer_fifo(uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16 // and usbd task can preempt and clear the busy _usbd_dev.ep_status[epnum][dir].busy = 1; - if (dcd_edpt_xfer_fifo(rhport, ep_addr, ff, total_bytes)) - { + if (dcd_edpt_xfer_fifo(rhport, ep_addr, ff, total_bytes)) { TU_LOG_USBD("OK\r\n"); return true; - }else - { + } else { // DCD error, mark endpoint as ready to allow next transfer _usbd_dev.ep_status[epnum][dir].busy = 0; _usbd_dev.ep_status[epnum][dir].claimed = 0; @@ -1289,75 +1361,62 @@ bool usbd_edpt_xfer_fifo(uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16 } } -bool usbd_edpt_busy(uint8_t rhport, uint8_t ep_addr) -{ +bool usbd_edpt_busy(uint8_t rhport, uint8_t ep_addr) { (void) rhport; uint8_t const epnum = tu_edpt_number(ep_addr); - uint8_t const dir = tu_edpt_dir(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); return _usbd_dev.ep_status[epnum][dir].busy; } -void usbd_edpt_stall(uint8_t rhport, uint8_t ep_addr) -{ +void usbd_edpt_stall(uint8_t rhport, uint8_t ep_addr) { rhport = _usbd_rhport; uint8_t const epnum = tu_edpt_number(ep_addr); - uint8_t const dir = tu_edpt_dir(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); // only stalled if currently cleared - if ( !_usbd_dev.ep_status[epnum][dir].stalled ) - { - TU_LOG_USBD(" Stall EP %02X\r\n", ep_addr); - dcd_edpt_stall(rhport, ep_addr); - _usbd_dev.ep_status[epnum][dir].stalled = 1; - _usbd_dev.ep_status[epnum][dir].busy = 1; - } + TU_LOG_USBD(" Stall EP %02X\r\n", ep_addr); + dcd_edpt_stall(rhport, ep_addr); + _usbd_dev.ep_status[epnum][dir].stalled = 1; + _usbd_dev.ep_status[epnum][dir].busy = 1; } -void usbd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) -{ +void usbd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) { rhport = _usbd_rhport; uint8_t const epnum = tu_edpt_number(ep_addr); - uint8_t const dir = tu_edpt_dir(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); // only clear if currently stalled - if ( _usbd_dev.ep_status[epnum][dir].stalled ) - { - TU_LOG_USBD(" Clear Stall EP %02X\r\n", ep_addr); - dcd_edpt_clear_stall(rhport, ep_addr); - _usbd_dev.ep_status[epnum][dir].stalled = 0; - _usbd_dev.ep_status[epnum][dir].busy = 0; - } + TU_LOG_USBD(" Clear Stall EP %02X\r\n", ep_addr); + dcd_edpt_clear_stall(rhport, ep_addr); + _usbd_dev.ep_status[epnum][dir].stalled = 0; + _usbd_dev.ep_status[epnum][dir].busy = 0; } -bool usbd_edpt_stalled(uint8_t rhport, uint8_t ep_addr) -{ +bool usbd_edpt_stalled(uint8_t rhport, uint8_t ep_addr) { (void) rhport; uint8_t const epnum = tu_edpt_number(ep_addr); - uint8_t const dir = tu_edpt_dir(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); return _usbd_dev.ep_status[epnum][dir].stalled; } /** * usbd_edpt_close will disable an endpoint. - * * In progress transfers on this EP may be delivered after this call. - * */ -void usbd_edpt_close(uint8_t rhport, uint8_t ep_addr) -{ +void usbd_edpt_close(uint8_t rhport, uint8_t ep_addr) { rhport = _usbd_rhport; TU_ASSERT(dcd_edpt_close, /**/); TU_LOG_USBD(" CLOSING Endpoint: 0x%02X\r\n", ep_addr); uint8_t const epnum = tu_edpt_number(ep_addr); - uint8_t const dir = tu_edpt_dir(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); dcd_edpt_close(rhport, ep_addr); _usbd_dev.ep_status[epnum][dir].stalled = 0; @@ -1367,17 +1426,24 @@ void usbd_edpt_close(uint8_t rhport, uint8_t ep_addr) return; } -void usbd_sof_enable(uint8_t rhport, bool en) -{ +void usbd_sof_enable(uint8_t rhport, sof_consumer_t consumer, bool en) { rhport = _usbd_rhport; - // TODO: Check needed if all drivers including the user sof_cb does not need an active SOF ISR any more. - // Only if all drivers switched off SOF calls the SOF interrupt may be disabled - dcd_sof_enable(rhport, en); + uint8_t consumer_old = _usbd_dev.sof_consumer; + // Keep track how many class instances need the SOF interrupt + if (en) { + _usbd_dev.sof_consumer |= (uint8_t)(1 << consumer); + } else { + _usbd_dev.sof_consumer &= (uint8_t)(~(1 << consumer)); + } + + // Test logically unequal + if(!_usbd_dev.sof_consumer != !consumer_old) { + dcd_sof_enable(rhport, _usbd_dev.sof_consumer); + } } -bool usbd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet_size) -{ +bool usbd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packet_size) { rhport = _usbd_rhport; TU_ASSERT(dcd_edpt_iso_alloc); @@ -1386,12 +1452,11 @@ bool usbd_edpt_iso_alloc(uint8_t rhport, uint8_t ep_addr, uint16_t largest_packe return dcd_edpt_iso_alloc(rhport, ep_addr, largest_packet_size); } -bool usbd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const * desc_ep) -{ +bool usbd_edpt_iso_activate(uint8_t rhport, tusb_desc_endpoint_t const* desc_ep) { rhport = _usbd_rhport; uint8_t const epnum = tu_edpt_number(desc_ep->bEndpointAddress); - uint8_t const dir = tu_edpt_dir(desc_ep->bEndpointAddress); + uint8_t const dir = tu_edpt_dir(desc_ep->bEndpointAddress); TU_ASSERT(dcd_edpt_iso_activate); TU_ASSERT(epnum < CFG_TUD_ENDPPOINT_MAX); diff --git a/pico-sdk/lib/tinyusb/src/device/usbd.h b/pico-sdk/lib/tinyusb/src/device/usbd.h index 3ab6c81..cba94fd 100644 --- a/pico-sdk/lib/tinyusb/src/device/usbd.h +++ b/pico-sdk/lib/tinyusb/src/device/usbd.h @@ -37,9 +37,12 @@ extern "C" { // Application API //--------------------------------------------------------------------+ -// Init device stack +// Init device stack on roothub port bool tud_init (uint8_t rhport); +// Deinit device stack on roothub port +bool tud_deinit(uint8_t rhport); + // Check if device stack is already initialized bool tud_inited(void); @@ -94,6 +97,9 @@ bool tud_disconnect(void); // Return false on unsupported MCUs bool tud_connect(void); +// Enable or disable the Start Of Frame callback support +bool tud_sof_cb_enable(bool en); + // Carry out Data and Status stage of control transfer // - If len = 0, it is equivalent to sending status only // - If len > wLength : it will be truncated @@ -149,6 +155,9 @@ TU_ATTR_WEAK void tud_resume_cb(void); // Invoked when there is a new usb event, which need to be processed by tud_task()/tud_task_ext() void tud_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr); +// Invoked when a new (micro) frame started +void tud_sof_cb(uint32_t frame_count); + // Invoked when received control request with VENDOR TYPE TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request); @@ -218,8 +227,8 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb 5, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_HEADER, U16_TO_U8S_LE(0x0120),\ /* CDC Call */\ 5, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_CALL_MANAGEMENT, 0, (uint8_t)((_itfnum) + 1),\ - /* CDC ACM: support line request */\ - 4, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT, 2,\ + /* CDC ACM: support line request + send break */\ + 4, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT, 6,\ /* CDC Union */\ 5, TUSB_DESC_CS_INTERFACE, CDC_FUNC_DESC_UNION, _itfnum, (uint8_t)((_itfnum) + 1),\ /* Endpoint Notification */\ @@ -393,6 +402,11 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb // For more channels, add definitions here +/* Standard AC Interrupt Endpoint Descriptor(4.8.2.1) */ +#define TUD_AUDIO_DESC_STD_AC_INT_EP_LEN 7 +#define TUD_AUDIO_DESC_STD_AC_INT_EP(_ep, _interval) \ + TUD_AUDIO_DESC_STD_AC_INT_EP_LEN, TUSB_DESC_ENDPOINT, _ep, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(6), _interval + /* Standard AS Interface Descriptor(4.9.1) */ #define TUD_AUDIO_DESC_STD_AS_INT_LEN 9 #define TUD_AUDIO_DESC_STD_AS_INT(_itfnum, _altset, _nEPs, _stridx) \ @@ -421,7 +435,7 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb /* Standard AS Isochronous Feedback Endpoint Descriptor(4.10.2.1) */ #define TUD_AUDIO_DESC_STD_AS_ISO_FB_EP_LEN 7 #define TUD_AUDIO_DESC_STD_AS_ISO_FB_EP(_ep, _interval) \ - TUD_AUDIO_DESC_STD_AS_ISO_FB_EP_LEN, TUSB_DESC_ENDPOINT, _ep, (uint8_t) (TUSB_XFER_ISOCHRONOUS | TUSB_ISO_EP_ATT_NO_SYNC | TUSB_ISO_EP_ATT_EXPLICIT_FB), U16_TO_U8S_LE(4), _interval + TUD_AUDIO_DESC_STD_AS_ISO_FB_EP_LEN, TUSB_DESC_ENDPOINT, _ep, (uint8_t) ((uint8_t)TUSB_XFER_ISOCHRONOUS | (uint8_t)TUSB_ISO_EP_ATT_NO_SYNC | (uint8_t)TUSB_ISO_EP_ATT_EXPLICIT_FB), U16_TO_U8S_LE(4), _interval // AUDIO simple descriptor (UAC2) for 1 microphone input // - 1 Input Terminal, 1 Feature Unit (Mute and Volume Control), 1 Output Terminal, 1 Clock Source @@ -468,7 +482,7 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb /* Type I Format Type Descriptor(2.3.1.6 - Audio Formats) */\ TUD_AUDIO_DESC_TYPE_I_FORMAT(_nBytesPerSample, _nBitsUsedPerSample),\ /* Standard AS Isochronous Audio Data Endpoint Descriptor(4.10.1.1) */\ - TUD_AUDIO_DESC_STD_AS_ISO_EP(/*_ep*/ _epin, /*_attr*/ (uint8_t) (TUSB_XFER_ISOCHRONOUS | TUSB_ISO_EP_ATT_ASYNCHRONOUS | TUSB_ISO_EP_ATT_DATA), /*_maxEPsize*/ _epsize, /*_interval*/ 0x01),\ + TUD_AUDIO_DESC_STD_AS_ISO_EP(/*_ep*/ _epin, /*_attr*/ (uint8_t) ((uint8_t)TUSB_XFER_ISOCHRONOUS | (uint8_t)TUSB_ISO_EP_ATT_ASYNCHRONOUS | (uint8_t)TUSB_ISO_EP_ATT_DATA), /*_maxEPsize*/ _epsize, /*_interval*/ 0x01),\ /* Class-Specific AS Isochronous Audio Data Endpoint Descriptor(4.10.1.2) */\ TUD_AUDIO_DESC_CS_AS_ISO_EP(/*_attr*/ AUDIO_CS_AS_ISO_DATA_EP_ATT_NON_MAX_PACKETS_OK, /*_ctrl*/ AUDIO_CTRL_NONE, /*_lockdelayunit*/ AUDIO_CS_AS_ISO_DATA_EP_LOCK_DELAY_UNIT_UNDEFINED, /*_lockdelay*/ 0x0000) @@ -517,7 +531,7 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb /* Type I Format Type Descriptor(2.3.1.6 - Audio Formats) */\ TUD_AUDIO_DESC_TYPE_I_FORMAT(_nBytesPerSample, _nBitsUsedPerSample),\ /* Standard AS Isochronous Audio Data Endpoint Descriptor(4.10.1.1) */\ - TUD_AUDIO_DESC_STD_AS_ISO_EP(/*_ep*/ _epin, /*_attr*/ (uint8_t) (TUSB_XFER_ISOCHRONOUS | TUSB_ISO_EP_ATT_ASYNCHRONOUS | TUSB_ISO_EP_ATT_DATA), /*_maxEPsize*/ _epsize, /*_interval*/ 0x01),\ + TUD_AUDIO_DESC_STD_AS_ISO_EP(/*_ep*/ _epin, /*_attr*/ (uint8_t) ((uint8_t)TUSB_XFER_ISOCHRONOUS | (uint8_t)TUSB_ISO_EP_ATT_ASYNCHRONOUS | (uint8_t)TUSB_ISO_EP_ATT_DATA), /*_maxEPsize*/ _epsize, /*_interval*/ 0x01),\ /* Class-Specific AS Isochronous Audio Data Endpoint Descriptor(4.10.1.2) */\ TUD_AUDIO_DESC_CS_AS_ISO_EP(/*_attr*/ AUDIO_CS_AS_ISO_DATA_EP_ATT_NON_MAX_PACKETS_OK, /*_ctrl*/ AUDIO_CTRL_NONE, /*_lockdelayunit*/ AUDIO_CS_AS_ISO_DATA_EP_LOCK_DELAY_UNIT_UNDEFINED, /*_lockdelay*/ 0x0000) @@ -565,7 +579,7 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb /* Type I Format Type Descriptor(2.3.1.6 - Audio Formats) */\ TUD_AUDIO_DESC_TYPE_I_FORMAT(_nBytesPerSample, _nBitsUsedPerSample),\ /* Standard AS Isochronous Audio Data Endpoint Descriptor(4.10.1.1) */\ - TUD_AUDIO_DESC_STD_AS_ISO_EP(/*_ep*/ _epout, /*_attr*/ (uint8_t) (TUSB_XFER_ISOCHRONOUS | TUSB_ISO_EP_ATT_ASYNCHRONOUS | TUSB_ISO_EP_ATT_DATA), /*_maxEPsize*/ _epsize, /*_interval*/ 0x01),\ + TUD_AUDIO_DESC_STD_AS_ISO_EP(/*_ep*/ _epout, /*_attr*/ (uint8_t) ((uint8_t)TUSB_XFER_ISOCHRONOUS | (uint8_t)TUSB_ISO_EP_ATT_ASYNCHRONOUS | (uint8_t)TUSB_ISO_EP_ATT_DATA), /*_maxEPsize*/ _epsize, /*_interval*/ 0x01),\ /* Class-Specific AS Isochronous Audio Data Endpoint Descriptor(4.10.1.2) */\ TUD_AUDIO_DESC_CS_AS_ISO_EP(/*_attr*/ AUDIO_CS_AS_ISO_DATA_EP_ATT_NON_MAX_PACKETS_OK, /*_ctrl*/ AUDIO_CTRL_NONE, /*_lockdelayunit*/ AUDIO_CS_AS_ISO_DATA_EP_LOCK_DELAY_UNIT_UNDEFINED, /*_lockdelay*/ 0x0000),\ /* Standard AS Isochronous Feedback Endpoint Descriptor(4.10.2.1) */\ diff --git a/pico-sdk/lib/tinyusb/src/device/usbd_pvt.h b/pico-sdk/lib/tinyusb/src/device/usbd_pvt.h index 9039bc9..335d46c 100644 --- a/pico-sdk/lib/tinyusb/src/device/usbd_pvt.h +++ b/pico-sdk/lib/tinyusb/src/device/usbd_pvt.h @@ -23,8 +23,8 @@ * * This file is part of the TinyUSB stack. */ -#ifndef _TUSB_USBD_PVT_H_ -#define _TUSB_USBD_PVT_H_ +#ifndef TUSB_USBD_PVT_H_ +#define TUSB_USBD_PVT_H_ #include "osal/osal.h" #include "common/tusb_fifo.h" @@ -35,16 +35,23 @@ #define TU_LOG_USBD(...) TU_LOG(CFG_TUD_LOG_LEVEL, __VA_ARGS__) +//--------------------------------------------------------------------+ +// MACRO CONSTANT TYPEDEF PROTYPES +//--------------------------------------------------------------------+ + +typedef enum { + SOF_CONSUMER_USER = 0, + SOF_CONSUMER_AUDIO, +} sof_consumer_t; + //--------------------------------------------------------------------+ // Class Driver API //--------------------------------------------------------------------+ typedef struct { - #if CFG_TUSB_DEBUG >= CFG_TUD_LOG_LEVEL char const* name; - #endif - void (* init ) (void); + bool (* deinit ) (void); void (* reset ) (uint8_t rhport); uint16_t (* open ) (uint8_t rhport, tusb_desc_interface_t const * desc_intf, uint16_t max_len); bool (* control_xfer_cb ) (uint8_t rhport, uint8_t stage, tusb_control_request_t const * request); @@ -110,7 +117,7 @@ bool usbd_edpt_ready(uint8_t rhport, uint8_t ep_addr) { } // Enable SOF interrupt -void usbd_sof_enable(uint8_t rhport, bool en); +void usbd_sof_enable(uint8_t rhport, sof_consumer_t consumer, bool en); /*------------------------------------------------------------------*/ /* Helper diff --git a/pico-sdk/lib/tinyusb/src/host/hcd.h b/pico-sdk/lib/tinyusb/src/host/hcd.h index 2bde289..5547c7c 100644 --- a/pico-sdk/lib/tinyusb/src/host/hcd.h +++ b/pico-sdk/lib/tinyusb/src/host/hcd.h @@ -125,11 +125,14 @@ bool hcd_dcache_clean_invalidate(void const* addr, uint32_t data_size) TU_ATTR_W //--------------------------------------------------------------------+ // optional hcd configuration, called by tuh_configure() -bool hcd_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param) TU_ATTR_WEAK; +bool hcd_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param); // Initialize controller to host mode bool hcd_init(uint8_t rhport); +// De-initialize controller +bool hcd_deinit(uint8_t rhport); + // Interrupt Handler void hcd_int_handler(uint8_t rhport, bool in_isr); diff --git a/pico-sdk/lib/tinyusb/src/host/hub.c b/pico-sdk/lib/tinyusb/src/host/hub.c index 3bac186..e970144 100644 --- a/pico-sdk/lib/tinyusb/src/host/hub.c +++ b/pico-sdk/lib/tinyusb/src/host/hub.c @@ -182,9 +182,13 @@ bool hub_port_get_status(uint8_t hub_addr, uint8_t hub_port, void* resp, //--------------------------------------------------------------------+ // CLASS-USBH API (don't require to verify parameters) //--------------------------------------------------------------------+ -void hub_init(void) -{ +bool hub_init(void) { tu_memclr(hub_data, sizeof(hub_data)); + return true; +} + +bool hub_deinit(void) { + return true; } bool hub_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len) diff --git a/pico-sdk/lib/tinyusb/src/host/hub.h b/pico-sdk/lib/tinyusb/src/host/hub.h index 390740e..385efe6 100644 --- a/pico-sdk/lib/tinyusb/src/host/hub.h +++ b/pico-sdk/lib/tinyusb/src/host/hub.h @@ -187,16 +187,14 @@ bool hub_port_get_status (uint8_t hub_addr, uint8_t hub_port, void* resp, bool hub_edpt_status_xfer(uint8_t dev_addr); // Reset a port -static inline bool hub_port_reset(uint8_t hub_addr, uint8_t hub_port, - tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ +TU_ATTR_ALWAYS_INLINE static inline +bool hub_port_reset(uint8_t hub_addr, uint8_t hub_port, tuh_xfer_cb_t complete_cb, uintptr_t user_data) { return hub_port_set_feature(hub_addr, hub_port, HUB_FEATURE_PORT_RESET, complete_cb, user_data); } // Clear Reset Change -static inline bool hub_port_clear_reset_change(uint8_t hub_addr, uint8_t hub_port, - tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ +TU_ATTR_ALWAYS_INLINE static inline +bool hub_port_clear_reset_change(uint8_t hub_addr, uint8_t hub_port, tuh_xfer_cb_t complete_cb, uintptr_t user_data) { return hub_port_clear_feature(hub_addr, hub_port, HUB_FEATURE_PORT_RESET_CHANGE, complete_cb, user_data); } @@ -204,7 +202,8 @@ static inline bool hub_port_clear_reset_change(uint8_t hub_addr, uint8_t hub_por //--------------------------------------------------------------------+ // Internal Class Driver API //--------------------------------------------------------------------+ -void hub_init (void); +bool hub_init (void); +bool hub_deinit (void); bool hub_open (uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len); bool hub_set_config (uint8_t dev_addr, uint8_t itf_num); bool hub_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes); diff --git a/pico-sdk/lib/tinyusb/src/host/usbh.c b/pico-sdk/lib/tinyusb/src/host/usbh.c index 3da296f..53b8527 100644 --- a/pico-sdk/lib/tinyusb/src/host/usbh.c +++ b/pico-sdk/lib/tinyusb/src/host/usbh.c @@ -37,7 +37,7 @@ // USBH Configuration //--------------------------------------------------------------------+ #ifndef CFG_TUH_TASK_QUEUE_SZ - #define CFG_TUH_TASK_QUEUE_SZ 16 + #define CFG_TUH_TASK_QUEUE_SZ 32 #endif #ifndef CFG_TUH_INTERFACE_MAX @@ -45,8 +45,20 @@ #endif //--------------------------------------------------------------------+ -// Callback weak stubs (called if application does not provide) +// Weak stubs: invoked if no strong implementation is available //--------------------------------------------------------------------+ +TU_ATTR_WEAK bool hcd_deinit(uint8_t rhport) { + (void) rhport; + return false; +} + +TU_ATTR_WEAK bool hcd_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param) { + (void) rhport; + (void) cfg_id; + (void) cfg_param; + return false; +} + TU_ATTR_WEAK void tuh_event_hook_cb(uint8_t rhport, uint32_t eventid, bool in_isr) { (void) rhport; (void) eventid; @@ -119,16 +131,17 @@ typedef struct { // MACRO CONSTANT TYPEDEF //--------------------------------------------------------------------+ #if CFG_TUSB_DEBUG >= CFG_TUH_LOG_LEVEL - #define DRIVER_NAME(_name) .name = _name, + #define DRIVER_NAME(_name) _name #else - #define DRIVER_NAME(_name) + #define DRIVER_NAME(_name) NULL #endif static usbh_class_driver_t const usbh_class_drivers[] = { #if CFG_TUH_CDC { - DRIVER_NAME("CDC") + .name = DRIVER_NAME("CDC"), .init = cdch_init, + .deinit = cdch_deinit, .open = cdch_open, .set_config = cdch_set_config, .xfer_cb = cdch_xfer_cb, @@ -138,8 +151,9 @@ static usbh_class_driver_t const usbh_class_drivers[] = { #if CFG_TUH_MSC { - DRIVER_NAME("MSC") + .name = DRIVER_NAME("MSC"), .init = msch_init, + .deinit = msch_deinit, .open = msch_open, .set_config = msch_set_config, .xfer_cb = msch_xfer_cb, @@ -149,8 +163,9 @@ static usbh_class_driver_t const usbh_class_drivers[] = { #if CFG_TUH_HID { - DRIVER_NAME("HID") + .name = DRIVER_NAME("HID"), .init = hidh_init, + .deinit = hidh_deinit, .open = hidh_open, .set_config = hidh_set_config, .xfer_cb = hidh_xfer_cb, @@ -160,8 +175,9 @@ static usbh_class_driver_t const usbh_class_drivers[] = { #if CFG_TUH_HUB { - DRIVER_NAME("HUB") + .name = DRIVER_NAME("HUB"), .init = hub_init, + .deinit = hub_deinit, .open = hub_open, .set_config = hub_set_config, .xfer_cb = hub_xfer_cb, @@ -171,9 +187,11 @@ static usbh_class_driver_t const usbh_class_drivers[] = { #if CFG_TUH_VENDOR { - DRIVER_NAME("VENDOR") + .name = DRIVER_NAME("VENDOR"), .init = cush_init, - .open = cush_open_subtask, + .deinit = cush_deinit, + .open = cush_open, + .set_config = cush_set_config, .xfer_cb = cush_isr, .close = cush_close } @@ -270,9 +288,9 @@ TU_ATTR_WEAK void osal_task_delay(uint32_t msec) { #endif TU_ATTR_ALWAYS_INLINE static inline bool queue_event(hcd_event_t const * event, bool in_isr) { - bool ret = osal_queue_send(_usbh_q, event, in_isr); + TU_ASSERT(osal_queue_send(_usbh_q, event, in_isr)); tuh_event_hook_cb(event->rhport, event->event_id, in_isr); - return ret; + return true; } //--------------------------------------------------------------------+ @@ -321,11 +339,7 @@ bool tuh_rhport_reset_bus(uint8_t rhport, bool active) { //--------------------------------------------------------------------+ bool tuh_configure(uint8_t rhport, uint32_t cfg_id, const void *cfg_param) { - if ( hcd_configure ) { - return hcd_configure(rhport, cfg_id, cfg_param); - } else { - return false; - } + return hcd_configure(rhport, cfg_id, cfg_param); } static void clear_device(usbh_device_t* dev) { @@ -338,55 +352,94 @@ bool tuh_inited(void) { return _usbh_controller != TUSB_INDEX_INVALID_8; } -bool tuh_init(uint8_t controller_id) { +bool tuh_init(uint8_t rhport) { // skip if already initialized - if ( tuh_inited() ) return true; + if (tuh_rhport_is_active(rhport)) return true; - TU_LOG_USBH("USBH init on controller %u\r\n", controller_id); - TU_LOG_INT(CFG_TUH_LOG_LEVEL, sizeof(usbh_device_t)); - TU_LOG_INT(CFG_TUH_LOG_LEVEL, sizeof(hcd_event_t)); - TU_LOG_INT(CFG_TUH_LOG_LEVEL, sizeof(_ctrl_xfer)); - TU_LOG_INT(CFG_TUH_LOG_LEVEL, sizeof(tuh_xfer_t)); - TU_LOG_INT(CFG_TUH_LOG_LEVEL, sizeof(tu_fifo_t)); - TU_LOG_INT(CFG_TUH_LOG_LEVEL, sizeof(tu_edpt_stream_t)); + TU_LOG_USBH("USBH init on controller %u\r\n", rhport); - // Event queue - _usbh_q = osal_queue_create( &_usbh_qdef ); - TU_ASSERT(_usbh_q != NULL); + // Init host stack if not already + if (!tuh_inited()) { + TU_LOG_INT_USBH(sizeof(usbh_device_t)); + TU_LOG_INT_USBH(sizeof(hcd_event_t)); + TU_LOG_INT_USBH(sizeof(_ctrl_xfer)); + TU_LOG_INT_USBH(sizeof(tuh_xfer_t)); + TU_LOG_INT_USBH(sizeof(tu_fifo_t)); + TU_LOG_INT_USBH(sizeof(tu_edpt_stream_t)); + + // Event queue + _usbh_q = osal_queue_create(&_usbh_qdef); + TU_ASSERT(_usbh_q != NULL); #if OSAL_MUTEX_REQUIRED - // Init mutex - _usbh_mutex = osal_mutex_create(&_usbh_mutexdef); - TU_ASSERT(_usbh_mutex); + // Init mutex + _usbh_mutex = osal_mutex_create(&_usbh_mutexdef); + TU_ASSERT(_usbh_mutex); #endif - // Get application driver if available - if ( usbh_app_driver_get_cb ) { - _app_driver = usbh_app_driver_get_cb(&_app_driver_count); - } + // Get application driver if available + if (usbh_app_driver_get_cb) { + _app_driver = usbh_app_driver_get_cb(&_app_driver_count); + } - // Device - tu_memclr(&_dev0, sizeof(_dev0)); - tu_memclr(_usbh_devices, sizeof(_usbh_devices)); - tu_memclr(&_ctrl_xfer, sizeof(_ctrl_xfer)); + // Device + tu_memclr(&_dev0, sizeof(_dev0)); + tu_memclr(_usbh_devices, sizeof(_usbh_devices)); + tu_memclr(&_ctrl_xfer, sizeof(_ctrl_xfer)); - for(uint8_t i=0; iname); - driver->init(); + // Class drivers + for (uint8_t drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++) { + usbh_class_driver_t const* driver = get_driver(drv_id); + if (driver) { + TU_LOG_USBH("%s init\r\n", driver->name); + driver->init(); + } } } - _usbh_controller = controller_id;; + // Init host controller + _usbh_controller = rhport;; + TU_ASSERT(hcd_init(rhport)); + hcd_int_enable(rhport); - TU_ASSERT(hcd_init(controller_id)); - hcd_int_enable(controller_id); + return true; +} + +bool tuh_deinit(uint8_t rhport) { + if (!tuh_rhport_is_active(rhport)) return true; + + // deinit host controller + hcd_int_disable(rhport); + hcd_deinit(rhport); + _usbh_controller = TUSB_INDEX_INVALID_8; + + // "unplug" all devices on this rhport (hub_addr = 0, hub_port = 0) + process_removing_device(rhport, 0, 0); + + // deinit host stack if no controller is active + if (!tuh_inited()) { + // Class drivers + for (uint8_t drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++) { + usbh_class_driver_t const* driver = get_driver(drv_id); + if (driver && driver->deinit) { + TU_LOG_USBH("%s deinit\r\n", driver->name); + driver->deinit(); + } + } + + osal_queue_delete(_usbh_q); + _usbh_q = NULL; + + #if OSAL_MUTEX_REQUIRED + // TODO make sure there is no task waiting on this mutex + osal_mutex_delete(_usbh_mutex); + _usbh_mutex = NULL; + #endif + } return true; } @@ -420,20 +473,18 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) { (void) in_isr; // not implemented yet // Skip if stack is not initialized - if ( !tuh_inited() ) return; + if (!tuh_inited()) return; // Loop until there is no more events in the queue - while (1) - { + while (1) { hcd_event_t event; - if ( !osal_queue_receive(_usbh_q, &event, timeout_ms) ) return; + if (!osal_queue_receive(_usbh_q, &event, timeout_ms)) return; - switch (event.event_id) - { + switch (event.event_id) { case HCD_EVENT_DEVICE_ATTACH: // due to the shared _usbh_ctrl_buf, we must complete enumerating one device before enumerating another one. // TODO better to have an separated queue for newly attached devices - if ( _dev0.enumerating ) { + if (_dev0.enumerating) { TU_LOG_USBH("[%u:] USBH Defer Attach until current enumeration complete\r\n", event.rhport); bool is_empty = osal_queue_empty(_usbh_q); @@ -443,12 +494,12 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) { // Exit if this is the only event in the queue, otherwise we may loop forever return; } - }else { + } else { TU_LOG_USBH("[%u:] USBH DEVICE ATTACH\r\n", event.rhport); _dev0.enumerating = 1; enum_new_device(&event); } - break; + break; case HCD_EVENT_DEVICE_REMOVE: TU_LOG_USBH("[%u:%u:%u] USBH DEVICE REMOVED\r\n", event.rhport, event.connection.hub_addr, event.connection.hub_port); @@ -456,37 +507,34 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) { #if CFG_TUH_HUB // TODO remove - if ( event.connection.hub_addr != 0 && event.connection.hub_port != 0) { + if (event.connection.hub_addr != 0 && event.connection.hub_port != 0) { // done with hub, waiting for next data on status pipe - (void) hub_edpt_status_xfer( event.connection.hub_addr ); + (void) hub_edpt_status_xfer(event.connection.hub_addr); } #endif - break; + break; - case HCD_EVENT_XFER_COMPLETE: - { + case HCD_EVENT_XFER_COMPLETE: { uint8_t const ep_addr = event.xfer_complete.ep_addr; - uint8_t const epnum = tu_edpt_number(ep_addr); - uint8_t const ep_dir = tu_edpt_dir(ep_addr); + uint8_t const epnum = tu_edpt_number(ep_addr); + uint8_t const ep_dir = (uint8_t) tu_edpt_dir(ep_addr); - // TU_LOG_USBH("on EP %02X with %u bytes: %s\r\n", ep_addr, (unsigned int) event.xfer_complete.len, - // tu_str_xfer_result[event.xfer_complete.result]); + TU_LOG_USBH("on EP %02X with %u bytes: %s\r\n", ep_addr, (unsigned int) event.xfer_complete.len, tu_str_xfer_result[event.xfer_complete.result]); if (event.dev_addr == 0) { // device 0 only has control endpoint - TU_ASSERT(epnum == 0, ); + TU_ASSERT(epnum == 0,); usbh_control_xfer_cb(event.dev_addr, ep_addr, (xfer_result_t) event.xfer_complete.result, event.xfer_complete.len); } else { usbh_device_t* dev = get_device(event.dev_addr); - TU_VERIFY(dev && dev->connected, ); + TU_VERIFY(dev && dev->connected,); - dev->ep_status[epnum][ep_dir].busy = 0; + dev->ep_status[epnum][ep_dir].busy = 0; dev->ep_status[epnum][ep_dir].claimed = 0; - if ( 0 == epnum ) { - usbh_control_xfer_cb(event.dev_addr, ep_addr, (xfer_result_t) event.xfer_complete.result, - event.xfer_complete.len); - }else { + if (0 == epnum) { + usbh_control_xfer_cb(event.dev_addr, ep_addr, (xfer_result_t) event.xfer_complete.result, event.xfer_complete.len); + } else { // Prefer application callback over built-in one if available. This occurs when tuh_edpt_xfer() is used // with enabled driver e.g HID endpoint #if CFG_TUH_API_EDPT_XFER @@ -503,41 +551,36 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr) { .complete_cb = complete_cb, .user_data = dev->ep_callback[epnum][ep_dir].user_data }; - complete_cb(&xfer); }else #endif { uint8_t drv_id = dev->ep2drv[epnum][ep_dir]; - usbh_class_driver_t const * driver = get_driver(drv_id); - if ( driver ) - { - // TU_LOG_USBH("%s xfer callback\r\n", driver->name); + usbh_class_driver_t const* driver = get_driver(drv_id); + if (driver) { + TU_LOG_USBH("%s xfer callback\r\n", driver->name); driver->xfer_cb(event.dev_addr, ep_addr, (xfer_result_t) event.xfer_complete.result, event.xfer_complete.len); - } - else - { + } else { // no driver/callback responsible for this transfer TU_ASSERT(false,); } } } } + break; } - break; case USBH_EVENT_FUNC_CALL: - if ( event.func_call.func ) event.func_call.func(event.func_call.param); - break; + if (event.func_call.func) event.func_call.func(event.func_call.param); + break; - default: break; + default: + break; } -#if CFG_TUSB_OS != OPT_OS_NONE && CFG_TUSB_OS != OPT_OS_PICO // return if there is no more events, for application to run other background if (osal_queue_empty(_usbh_q)) return; -#endif } } @@ -588,8 +631,7 @@ bool tuh_control_xfer (tuh_xfer_t* xfer) { TU_LOG_USBH("[%u:%u] %s: ", rhport, daddr, (xfer->setup->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD && xfer->setup->bRequest <= TUSB_REQ_SYNCH_FRAME) ? tu_str_std_request[xfer->setup->bRequest] : "Class Request"); - TU_LOG_BUF(CFG_TUH_LOG_LEVEL, xfer->setup, 8); - TU_LOG_USBH("\r\n"); + TU_LOG_BUF_USBH(xfer->setup, 8); if (xfer->complete_cb) { TU_ASSERT( hcd_setup_send(rhport, daddr, (uint8_t const*) &_ctrl_xfer.request) ); @@ -630,7 +672,7 @@ TU_ATTR_ALWAYS_INLINE static inline void _set_control_xfer_stage(uint8_t stage) (void) osal_mutex_unlock(_usbh_mutex); } -static void _xfer_complete(uint8_t daddr, xfer_result_t result) { +static void _control_xfer_complete(uint8_t daddr, xfer_result_t result) { TU_LOG_USBH("\r\n"); // duplicate xfer since user can execute control transfer within callback @@ -653,46 +695,56 @@ static void _xfer_complete(uint8_t daddr, xfer_result_t result) { } } -static bool usbh_control_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { +static bool usbh_control_xfer_cb (uint8_t daddr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes) { (void) ep_addr; - const uint8_t rhport = usbh_get_rhport(dev_addr); + const uint8_t rhport = usbh_get_rhport(daddr); tusb_control_request_t const * request = &_ctrl_xfer.request; if (XFER_RESULT_SUCCESS != result) { - TU_LOG1("[%u:%u] Control %s, xferred_bytes = %lu\r\n", rhport, dev_addr, result == XFER_RESULT_STALLED ? "STALLED" : "FAILED", xferred_bytes); - TU_LOG1_BUF(request, 8); - TU_LOG1("\r\n"); + TU_LOG_USBH("[%u:%u] Control %s, xferred_bytes = %" PRIu32 "\r\n", rhport, daddr, result == XFER_RESULT_STALLED ? "STALLED" : "FAILED", xferred_bytes); + TU_LOG_BUF_USBH(request, 8); // terminate transfer if any stage failed - _xfer_complete(dev_addr, result); + _control_xfer_complete(daddr, result); }else { switch(_ctrl_xfer.stage) { case CONTROL_STAGE_SETUP: if (request->wLength) { // DATA stage: initial data toggle is always 1 _set_control_xfer_stage(CONTROL_STAGE_DATA); - TU_ASSERT( hcd_edpt_xfer(rhport, dev_addr, tu_edpt_addr(0, request->bmRequestType_bit.direction), _ctrl_xfer.buffer, request->wLength) ); + TU_ASSERT( hcd_edpt_xfer(rhport, daddr, tu_edpt_addr(0, request->bmRequestType_bit.direction), _ctrl_xfer.buffer, request->wLength) ); return true; } TU_ATTR_FALLTHROUGH; case CONTROL_STAGE_DATA: if (request->wLength) { - TU_LOG_USBH("[%u:%u] Control data:\r\n", rhport, dev_addr); - TU_LOG_MEM(CFG_TUH_LOG_LEVEL, _ctrl_xfer.buffer, xferred_bytes, 2); + TU_LOG_USBH("[%u:%u] Control data:\r\n", rhport, daddr); + TU_LOG_MEM_USBH(_ctrl_xfer.buffer, xferred_bytes, 2); } _ctrl_xfer.actual_len = (uint16_t) xferred_bytes; // ACK stage: toggle is always 1 _set_control_xfer_stage(CONTROL_STAGE_ACK); - TU_ASSERT( hcd_edpt_xfer(rhport, dev_addr, tu_edpt_addr(0, 1-request->bmRequestType_bit.direction), NULL, 0) ); - break; + TU_ASSERT( hcd_edpt_xfer(rhport, daddr, tu_edpt_addr(0, 1 - request->bmRequestType_bit.direction), NULL, 0) ); + break; - case CONTROL_STAGE_ACK: - _xfer_complete(dev_addr, result); - break; + case CONTROL_STAGE_ACK: { + // Abort all pending transfers if SET_CONFIGURATION request + // NOTE: should we force closing all non-control endpoints in the future? + if (request->bRequest == TUSB_REQ_SET_CONFIGURATION && request->bmRequestType == 0x00) { + for(uint8_t epnum=1; epnumep_addr; TU_VERIFY(daddr && ep_addr); - TU_VERIFY(usbh_edpt_claim(daddr, ep_addr)); if (!usbh_edpt_xfer_with_callback(daddr, ep_addr, xfer->buffer, (uint16_t) xfer->buflen, @@ -743,7 +794,7 @@ bool tuh_edpt_abort_xfer(uint8_t daddr, uint8_t ep_addr) { TU_VERIFY(hcd_edpt_abort_xfer(dev->rhport, daddr, ep_addr)); // mark as ready and release endpoint if transfer is aborted dev->ep_status[epnum][dir].busy = false; - usbh_edpt_release(daddr, ep_addr); + tu_edpt_release(&dev->ep_status[epnum][dir], _usbh_mutex); } return true; @@ -785,30 +836,28 @@ void usbh_defer_func(osal_task_func_t func, void *param, bool in_isr) { //--------------------------------------------------------------------+ // Claim an endpoint for transfer -bool usbh_edpt_claim(uint8_t dev_addr, uint8_t ep_addr) -{ +bool usbh_edpt_claim(uint8_t dev_addr, uint8_t ep_addr) { // Note: addr0 only use tuh_control_xfer usbh_device_t* dev = get_device(dev_addr); TU_ASSERT(dev && dev->connected); uint8_t const epnum = tu_edpt_number(ep_addr); - uint8_t const dir = tu_edpt_dir(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); TU_VERIFY(tu_edpt_claim(&dev->ep_status[epnum][dir], _usbh_mutex)); - // TU_LOG_USBH("[%u] Claimed EP 0x%02x\r\n", dev_addr, ep_addr); + TU_LOG_USBH("[%u] Claimed EP 0x%02x\r\n", dev_addr, ep_addr); return true; } // Release an claimed endpoint due to failed transfer attempt -bool usbh_edpt_release(uint8_t dev_addr, uint8_t ep_addr) -{ +bool usbh_edpt_release(uint8_t dev_addr, uint8_t ep_addr) { // Note: addr0 only use tuh_control_xfer usbh_device_t* dev = get_device(dev_addr); TU_VERIFY(dev && dev->connected); uint8_t const epnum = tu_edpt_number(ep_addr); - uint8_t const dir = tu_edpt_dir(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); TU_VERIFY(tu_edpt_release(&dev->ep_status[epnum][dir], _usbh_mutex)); TU_LOG_USBH("[%u] Released EP 0x%02x\r\n", dev_addr, ep_addr); @@ -818,9 +867,8 @@ bool usbh_edpt_release(uint8_t dev_addr, uint8_t ep_addr) // Submit an transfer // TODO call usbh_edpt_release if failed -bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes, - tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ +bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes, + tuh_xfer_cb_t complete_cb, uintptr_t user_data) { (void) complete_cb; (void) user_data; @@ -828,10 +876,10 @@ bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t * b TU_VERIFY(dev); uint8_t const epnum = tu_edpt_number(ep_addr); - uint8_t const dir = tu_edpt_dir(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); tu_edpt_state_t* ep_state = &dev->ep_status[epnum][dir]; - // TU_LOG_USBH(" Queue EP %02X with %u bytes ... \r\n", ep_addr, total_bytes); + TU_LOG_USBH(" Queue EP %02X with %u bytes ... \r\n", ep_addr, total_bytes); // Attempt to transfer on a busy endpoint, sound like an race condition ! TU_ASSERT(ep_state->busy == 0); @@ -845,14 +893,12 @@ bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t * b dev->ep_callback[epnum][dir].user_data = user_data; #endif - if ( hcd_edpt_xfer(dev->rhport, dev_addr, ep_addr, buffer, total_bytes) ) - { - // TU_LOG_USBH("OK\r\n"); + if (hcd_edpt_xfer(dev->rhport, dev_addr, ep_addr, buffer, total_bytes)) { + TU_LOG_USBH("OK\r\n"); return true; - }else - { + } else { // HCD error, mark endpoint as ready to allow next transfer - ep_state->busy = 0; + ep_state->busy = 0; ep_state->claimed = 0; TU_LOG1("Failed\r\n"); // TU_BREAKPOINT(); @@ -860,12 +906,9 @@ bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t * b } } -static bool usbh_edpt_control_open(uint8_t dev_addr, uint8_t max_packet_size) -{ +static bool usbh_edpt_control_open(uint8_t dev_addr, uint8_t max_packet_size) { TU_LOG_USBH("[%u:%u] Open EP0 with Size = %u\r\n", usbh_get_rhport(dev_addr), dev_addr, max_packet_size); - - tusb_desc_endpoint_t ep0_desc = - { + tusb_desc_endpoint_t ep0_desc = { .bLength = sizeof(tusb_desc_endpoint_t), .bDescriptorType = TUSB_DESC_ENDPOINT, .bEndpointAddress = 0, @@ -877,10 +920,8 @@ static bool usbh_edpt_control_open(uint8_t dev_addr, uint8_t max_packet_size) return hcd_edpt_open(usbh_get_rhport(dev_addr), dev_addr, &ep0_desc); } -bool tuh_edpt_open(uint8_t dev_addr, tusb_desc_endpoint_t const * desc_ep) -{ - TU_ASSERT( tu_edpt_validate(desc_ep, tuh_speed_get(dev_addr)) ); - +bool tuh_edpt_open(uint8_t dev_addr, tusb_desc_endpoint_t const* desc_ep) { + TU_ASSERT(tu_edpt_validate(desc_ep, tuh_speed_get(dev_addr))); return hcd_edpt_open(usbh_get_rhport(dev_addr), dev_addr, desc_ep); } @@ -889,7 +930,7 @@ bool usbh_edpt_busy(uint8_t dev_addr, uint8_t ep_addr) { TU_VERIFY(dev); uint8_t const epnum = tu_edpt_number(ep_addr); - uint8_t const dir = tu_edpt_dir(ep_addr); + uint8_t const dir = tu_edpt_dir(ep_addr); return dev->ep_status[epnum][dir].busy; } @@ -898,22 +939,18 @@ bool usbh_edpt_busy(uint8_t dev_addr, uint8_t ep_addr) { // HCD Event Handler //--------------------------------------------------------------------+ -void hcd_devtree_get_info(uint8_t dev_addr, hcd_devtree_info_t* devtree_info) -{ +void hcd_devtree_get_info(uint8_t dev_addr, hcd_devtree_info_t* devtree_info) { usbh_device_t const* dev = get_device(dev_addr); - - if (dev) - { - devtree_info->rhport = dev->rhport; + if (dev) { + devtree_info->rhport = dev->rhport; devtree_info->hub_addr = dev->hub_addr; devtree_info->hub_port = dev->hub_port; - devtree_info->speed = dev->speed; - }else - { - devtree_info->rhport = _dev0.rhport; + devtree_info->speed = dev->speed; + } else { + devtree_info->rhport = _dev0.rhport; devtree_info->hub_addr = _dev0.hub_addr; devtree_info->hub_port = _dev0.hub_port; - devtree_info->speed = _dev0.speed; + devtree_info->speed = _dev0.speed; } } @@ -943,12 +980,9 @@ TU_ATTR_FAST_FUNC void hcd_event_handler(hcd_event_t const* event, bool in_isr) // generic helper to get a descriptor // if blocking, user_data is pointed to xfer_result static bool _get_descriptor(uint8_t daddr, uint8_t type, uint8_t index, uint16_t language_id, void* buffer, uint16_t len, - tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ - tusb_control_request_t const request = - { - .bmRequestType_bit = - { + tuh_xfer_cb_t complete_cb, uintptr_t user_data) { + tusb_control_request_t const request = { + .bmRequestType_bit = { .recipient = TUSB_REQ_RCPT_DEVICE, .type = TUSB_REQ_TYPE_STANDARD, .direction = TUSB_DIR_IN @@ -958,9 +992,7 @@ static bool _get_descriptor(uint8_t daddr, uint8_t type, uint8_t index, uint16_t .wIndex = tu_htole16(language_id), .wLength = tu_htole16(len) }; - - tuh_xfer_t xfer = - { + tuh_xfer_t xfer = { .daddr = daddr, .ep_addr = 0, .setup = &request, @@ -973,29 +1005,25 @@ static bool _get_descriptor(uint8_t daddr, uint8_t type, uint8_t index, uint16_t } bool tuh_descriptor_get(uint8_t daddr, uint8_t type, uint8_t index, void* buffer, uint16_t len, - tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ + tuh_xfer_cb_t complete_cb, uintptr_t user_data) { return _get_descriptor(daddr, type, index, 0x0000, buffer, len, complete_cb, user_data); } bool tuh_descriptor_get_device(uint8_t daddr, void* buffer, uint16_t len, - tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ + tuh_xfer_cb_t complete_cb, uintptr_t user_data) { len = tu_min16(len, sizeof(tusb_desc_device_t)); return tuh_descriptor_get(daddr, TUSB_DESC_DEVICE, 0, buffer, len, complete_cb, user_data); } bool tuh_descriptor_get_configuration(uint8_t daddr, uint8_t index, void* buffer, uint16_t len, - tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ + tuh_xfer_cb_t complete_cb, uintptr_t user_data) { return tuh_descriptor_get(daddr, TUSB_DESC_CONFIGURATION, index, buffer, len, complete_cb, user_data); } //------------- String Descriptor -------------// bool tuh_descriptor_get_string(uint8_t daddr, uint8_t index, uint16_t language_id, void* buffer, uint16_t len, - tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ + tuh_xfer_cb_t complete_cb, uintptr_t user_data) { return _get_descriptor(daddr, TUSB_DESC_STRING, index, language_id, buffer, len, complete_cb, user_data); } @@ -1010,8 +1038,7 @@ bool tuh_descriptor_get_manufacturer_string(uint8_t daddr, uint16_t language_id, // Get product string descriptor bool tuh_descriptor_get_product_string(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len, - tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ + tuh_xfer_cb_t complete_cb, uintptr_t user_data) { usbh_device_t const* dev = get_device(daddr); TU_VERIFY(dev && dev->i_product); return tuh_descriptor_get_string(daddr, dev->i_product, language_id, buffer, len, complete_cb, user_data); @@ -1019,8 +1046,7 @@ bool tuh_descriptor_get_product_string(uint8_t daddr, uint16_t language_id, void // Get serial string descriptor bool tuh_descriptor_get_serial_string(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len, - tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ + tuh_xfer_cb_t complete_cb, uintptr_t user_data) { usbh_device_t const* dev = get_device(daddr); TU_VERIFY(dev && dev->i_serial); return tuh_descriptor_get_string(daddr, dev->i_serial, language_id, buffer, len, complete_cb, user_data); @@ -1029,95 +1055,78 @@ bool tuh_descriptor_get_serial_string(uint8_t daddr, uint16_t language_id, void* // Get HID report descriptor // if blocking, user_data is pointed to xfer_result bool tuh_descriptor_get_hid_report(uint8_t daddr, uint8_t itf_num, uint8_t desc_type, uint8_t index, void* buffer, uint16_t len, - tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ + tuh_xfer_cb_t complete_cb, uintptr_t user_data) { TU_LOG_USBH("HID Get Report Descriptor\r\n"); - tusb_control_request_t const request = - { - .bmRequestType_bit = - { - .recipient = TUSB_REQ_RCPT_INTERFACE, - .type = TUSB_REQ_TYPE_STANDARD, - .direction = TUSB_DIR_IN - }, - .bRequest = TUSB_REQ_GET_DESCRIPTOR, - .wValue = tu_htole16(TU_U16(desc_type, index)), - .wIndex = tu_htole16((uint16_t) itf_num), - .wLength = len + tusb_control_request_t const request = { + .bmRequestType_bit = { + .recipient = TUSB_REQ_RCPT_INTERFACE, + .type = TUSB_REQ_TYPE_STANDARD, + .direction = TUSB_DIR_IN + }, + .bRequest = TUSB_REQ_GET_DESCRIPTOR, + .wValue = tu_htole16(TU_U16(desc_type, index)), + .wIndex = tu_htole16((uint16_t) itf_num), + .wLength = len }; - - tuh_xfer_t xfer = - { - .daddr = daddr, - .ep_addr = 0, - .setup = &request, - .buffer = buffer, - .complete_cb = complete_cb, - .user_data = user_data + tuh_xfer_t xfer = { + .daddr = daddr, + .ep_addr = 0, + .setup = &request, + .buffer = buffer, + .complete_cb = complete_cb, + .user_data = user_data }; return tuh_control_xfer(&xfer); } bool tuh_configuration_set(uint8_t daddr, uint8_t config_num, - tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ + tuh_xfer_cb_t complete_cb, uintptr_t user_data) { TU_LOG_USBH("Set Configuration = %d\r\n", config_num); - - tusb_control_request_t const request = - { - .bmRequestType_bit = - { - .recipient = TUSB_REQ_RCPT_DEVICE, - .type = TUSB_REQ_TYPE_STANDARD, - .direction = TUSB_DIR_OUT - }, - .bRequest = TUSB_REQ_SET_CONFIGURATION, - .wValue = tu_htole16(config_num), - .wIndex = 0, - .wLength = 0 + tusb_control_request_t const request = { + .bmRequestType_bit = { + .recipient = TUSB_REQ_RCPT_DEVICE, + .type = TUSB_REQ_TYPE_STANDARD, + .direction = TUSB_DIR_OUT + }, + .bRequest = TUSB_REQ_SET_CONFIGURATION, + .wValue = tu_htole16(config_num), + .wIndex = 0, + .wLength = 0 }; - - tuh_xfer_t xfer = - { - .daddr = daddr, - .ep_addr = 0, - .setup = &request, - .buffer = NULL, - .complete_cb = complete_cb, - .user_data = user_data + tuh_xfer_t xfer = { + .daddr = daddr, + .ep_addr = 0, + .setup = &request, + .buffer = NULL, + .complete_cb = complete_cb, + .user_data = user_data }; return tuh_control_xfer(&xfer); } bool tuh_interface_set(uint8_t daddr, uint8_t itf_num, uint8_t itf_alt, - tuh_xfer_cb_t complete_cb, uintptr_t user_data) -{ + tuh_xfer_cb_t complete_cb, uintptr_t user_data) { TU_LOG_USBH("Set Interface %u Alternate %u\r\n", itf_num, itf_alt); - - tusb_control_request_t const request = - { - .bmRequestType_bit = - { - .recipient = TUSB_REQ_RCPT_DEVICE, - .type = TUSB_REQ_TYPE_STANDARD, - .direction = TUSB_DIR_OUT - }, - .bRequest = TUSB_REQ_SET_INTERFACE, - .wValue = tu_htole16(itf_alt), - .wIndex = tu_htole16(itf_num), - .wLength = 0 + tusb_control_request_t const request = { + .bmRequestType_bit = { + .recipient = TUSB_REQ_RCPT_DEVICE, + .type = TUSB_REQ_TYPE_STANDARD, + .direction = TUSB_DIR_OUT + }, + .bRequest = TUSB_REQ_SET_INTERFACE, + .wValue = tu_htole16(itf_alt), + .wIndex = tu_htole16(itf_num), + .wLength = 0 }; - - tuh_xfer_t xfer = - { - .daddr = daddr, - .ep_addr = 0, - .setup = &request, - .buffer = NULL, - .complete_cb = complete_cb, - .user_data = user_data + tuh_xfer_t xfer = { + .daddr = daddr, + .ep_addr = 0, + .setup = &request, + .buffer = NULL, + .complete_cb = complete_cb, + .user_data = user_data }; return tuh_control_xfer(&xfer); @@ -1132,43 +1141,42 @@ bool tuh_interface_set(uint8_t daddr, uint8_t itf_num, uint8_t itf_alt, TU_VERIFY(_async_func(__VA_ARGS__, NULL, (uintptr_t) &result), XFER_RESULT_TIMEOUT); \ return (uint8_t) result -uint8_t tuh_descriptor_get_sync(uint8_t daddr, uint8_t type, uint8_t index, void* buffer, uint16_t len) -{ +uint8_t tuh_descriptor_get_sync(uint8_t daddr, uint8_t type, uint8_t index, + void* buffer, uint16_t len) { _CONTROL_SYNC_API(tuh_descriptor_get, daddr, type, index, buffer, len); } -uint8_t tuh_descriptor_get_device_sync(uint8_t daddr, void* buffer, uint16_t len) -{ +uint8_t tuh_descriptor_get_device_sync(uint8_t daddr, void* buffer, uint16_t len) { _CONTROL_SYNC_API(tuh_descriptor_get_device, daddr, buffer, len); } -uint8_t tuh_descriptor_get_configuration_sync(uint8_t daddr, uint8_t index, void* buffer, uint16_t len) -{ +uint8_t tuh_descriptor_get_configuration_sync(uint8_t daddr, uint8_t index, + void* buffer, uint16_t len) { _CONTROL_SYNC_API(tuh_descriptor_get_configuration, daddr, index, buffer, len); } -uint8_t tuh_descriptor_get_hid_report_sync(uint8_t daddr, uint8_t itf_num, uint8_t desc_type, uint8_t index, void* buffer, uint16_t len) -{ +uint8_t tuh_descriptor_get_hid_report_sync(uint8_t daddr, uint8_t itf_num, uint8_t desc_type, uint8_t index, + void* buffer, uint16_t len) { _CONTROL_SYNC_API(tuh_descriptor_get_hid_report, daddr, itf_num, desc_type, index, buffer, len); } -uint8_t tuh_descriptor_get_string_sync(uint8_t daddr, uint8_t index, uint16_t language_id, void* buffer, uint16_t len) -{ +uint8_t tuh_descriptor_get_string_sync(uint8_t daddr, uint8_t index, uint16_t language_id, + void* buffer, uint16_t len) { _CONTROL_SYNC_API(tuh_descriptor_get_string, daddr, index, language_id, buffer, len); } -uint8_t tuh_descriptor_get_manufacturer_string_sync(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len) -{ +uint8_t tuh_descriptor_get_manufacturer_string_sync(uint8_t daddr, uint16_t language_id, + void* buffer, uint16_t len) { _CONTROL_SYNC_API(tuh_descriptor_get_manufacturer_string, daddr, language_id, buffer, len); } -uint8_t tuh_descriptor_get_product_string_sync(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len) -{ +uint8_t tuh_descriptor_get_product_string_sync(uint8_t daddr, uint16_t language_id, + void* buffer, uint16_t len) { _CONTROL_SYNC_API(tuh_descriptor_get_product_string, daddr, language_id, buffer, len); } -uint8_t tuh_descriptor_get_serial_string_sync(uint8_t daddr, uint16_t language_id, void* buffer, uint16_t len) -{ +uint8_t tuh_descriptor_get_serial_string_sync(uint8_t daddr, uint16_t language_id, + void* buffer, uint16_t len) { _CONTROL_SYNC_API(tuh_descriptor_get_serial_string, daddr, language_id, buffer, len); } @@ -1176,9 +1184,7 @@ uint8_t tuh_descriptor_get_serial_string_sync(uint8_t daddr, uint16_t language_i // Detaching //--------------------------------------------------------------------+ -TU_ATTR_ALWAYS_INLINE -static inline bool is_hub_addr(uint8_t daddr) -{ +TU_ATTR_ALWAYS_INLINE static inline bool is_hub_addr(uint8_t daddr) { return (CFG_TUH_HUB > 0) && (daddr > CFG_TUH_DEVICE_MAX); } @@ -1203,62 +1209,63 @@ static inline bool is_hub_addr(uint8_t daddr) //} // a device unplugged from rhport:hub_addr:hub_port -static void process_removing_device(uint8_t rhport, uint8_t hub_addr, uint8_t hub_port) -{ +static void process_removing_device(uint8_t rhport, uint8_t hub_addr, uint8_t hub_port) { //------------- find the all devices (star-network) under port that is unplugged -------------// // TODO mark as disconnected in ISR, also handle dev0 + uint32_t removing_hubs = 0; + do { + for (uint8_t dev_id = 0; dev_id < TOTAL_DEVICES; dev_id++) { + usbh_device_t* dev = &_usbh_devices[dev_id]; + uint8_t const daddr = dev_id + 1; -#if 0 - // index as hub addr, value is hub port (0xFF for invalid) - uint8_t removing_hubs[CFG_TUH_HUB]; - memset(removing_hubs, TUSB_INDEX_INVALID_8, sizeof(removing_hubs)); + // hub_addr = 0 means roothub, hub_port = 0 means all devices of downstream hub + if (dev->rhport == rhport && dev->connected && + (hub_addr == 0 || dev->hub_addr == hub_addr) && + (hub_port == 0 || dev->hub_port == hub_port)) { + TU_LOG_USBH("[%u:%u:%u] unplugged address = %u\r\n", rhport, hub_addr, hub_port, daddr); - removing_hubs[hub_addr-CFG_TUH_DEVICE_MAX] = hub_port; + if (is_hub_addr(daddr)) { + TU_LOG_USBH(" is a HUB device %u\r\n", daddr); + removing_hubs |= TU_BIT(dev_id - CFG_TUH_DEVICE_MAX); + } else { + // Invoke callback before closing driver (maybe call it later ?) + if (tuh_umount_cb) tuh_umount_cb(daddr); + } - // consecutive non-removing hub - uint8_t nop_count = 0; -#endif + // Close class driver + for (uint8_t drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++) { + usbh_class_driver_t const* driver = get_driver(drv_id); + if (driver) driver->close(daddr); + } - for (uint8_t dev_id = 0; dev_id < TOTAL_DEVICES; dev_id++) - { - usbh_device_t *dev = &_usbh_devices[dev_id]; - uint8_t const daddr = dev_id + 1; + hcd_device_close(rhport, daddr); + clear_device(dev); - // hub_addr = 0 means roothub, hub_port = 0 means all devices of downstream hub - if (dev->rhport == rhport && dev->connected && - (hub_addr == 0 || dev->hub_addr == hub_addr) && - (hub_port == 0 || dev->hub_port == hub_port)) { - TU_LOG_USBH("Device unplugged address = %u\r\n", daddr); - - if (is_hub_addr(daddr)) { - TU_LOG_USBH(" is a HUB device %u\r\n", daddr); - - // Submit removed event If the device itself is a hub (un-rolled recursive) - // TODO a better to unroll recursrive is using array of removing_hubs and mark it here - hcd_event_t event; - event.rhport = rhport; - event.event_id = HCD_EVENT_DEVICE_REMOVE; - event.connection.hub_addr = daddr; - event.connection.hub_port = 0; - - hcd_event_handler(&event, false); - } else { - // Invoke callback before closing driver (maybe call it later ?) - if (tuh_umount_cb) tuh_umount_cb(daddr); + // abort on-going control xfer on this device if any + if (_ctrl_xfer.daddr == daddr) _set_control_xfer_stage(CONTROL_STAGE_IDLE); } - - // Close class driver - for (uint8_t drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++) { - usbh_class_driver_t const * driver = get_driver(drv_id); - if ( driver ) driver->close(daddr); - } - - hcd_device_close(rhport, daddr); - clear_device(dev); - // abort on-going control xfer if any - if (_ctrl_xfer.daddr == daddr) _set_control_xfer_stage(CONTROL_STAGE_IDLE); } - } + + // if removing a hub, we need to remove its downstream devices + #if CFG_TUH_HUB + if (removing_hubs == 0) break; + + // find a marked hub to process + for (uint8_t h_id = 0; h_id < CFG_TUH_HUB; h_id++) { + if (tu_bit_test(removing_hubs, h_id)) { + removing_hubs &= ~TU_BIT(h_id); + + // update hub_addr and hub_port for next loop + hub_addr = h_id + 1 + CFG_TUH_DEVICE_MAX; + hub_port = 0; + break; + } + } + #else + (void) removing_hubs; + break; + #endif + } while(1); } //--------------------------------------------------------------------+ @@ -1327,227 +1334,221 @@ static void process_enumeration(tuh_xfer_t* xfer) { uint8_t const daddr = xfer->daddr; uintptr_t const state = xfer->user_data; - switch(state) - { -#if CFG_TUH_HUB + switch (state) { + #if CFG_TUH_HUB //case ENUM_HUB_GET_STATUS_1: break; - case ENUM_HUB_CLEAR_RESET_1: - { + case ENUM_HUB_CLEAR_RESET_1: { hub_port_status_response_t port_status; memcpy(&port_status, _usbh_ctrl_buf, sizeof(hub_port_status_response_t)); - if ( !port_status.status.connection ) - { + if (!port_status.status.connection) { // device unplugged while delaying, nothing else to do enum_full_complete(); return; } _dev0.speed = (port_status.status.high_speed) ? TUSB_SPEED_HIGH : - (port_status.status.low_speed ) ? TUSB_SPEED_LOW : TUSB_SPEED_FULL; + (port_status.status.low_speed) ? TUSB_SPEED_LOW : TUSB_SPEED_FULL; // Acknowledge Port Reset Change - if (port_status.change.reset) - { - hub_port_clear_reset_change(_dev0.hub_addr, _dev0.hub_port, process_enumeration, ENUM_ADDR0_DEVICE_DESC); + if (port_status.change.reset) { + hub_port_clear_reset_change(_dev0.hub_addr, _dev0.hub_port, + process_enumeration, ENUM_ADDR0_DEVICE_DESC); } + break; } - break; case ENUM_HUB_GET_STATUS_2: osal_task_delay(ENUM_RESET_DELAY); - TU_ASSERT( hub_port_get_status(_dev0.hub_addr, _dev0.hub_port, _usbh_ctrl_buf, process_enumeration, ENUM_HUB_CLEAR_RESET_2), ); - break; + TU_ASSERT(hub_port_get_status(_dev0.hub_addr, _dev0.hub_port, _usbh_ctrl_buf, + process_enumeration, ENUM_HUB_CLEAR_RESET_2),); + break; - case ENUM_HUB_CLEAR_RESET_2: - { + case ENUM_HUB_CLEAR_RESET_2: { hub_port_status_response_t port_status; memcpy(&port_status, _usbh_ctrl_buf, sizeof(hub_port_status_response_t)); // Acknowledge Port Reset Change if Reset Successful - if (port_status.change.reset) - { - TU_ASSERT( hub_port_clear_reset_change(_dev0.hub_addr, _dev0.hub_port, process_enumeration, ENUM_SET_ADDR), ); + if (port_status.change.reset) { + TU_ASSERT(hub_port_clear_reset_change(_dev0.hub_addr, _dev0.hub_port, + process_enumeration, ENUM_SET_ADDR),); } + break; } - break; -#endif + #endif - case ENUM_ADDR0_DEVICE_DESC: - { + case ENUM_ADDR0_DEVICE_DESC: { // TODO probably doesn't need to open/close each enumeration uint8_t const addr0 = 0; - TU_ASSERT( usbh_edpt_control_open(addr0, 8), ); + TU_ASSERT(usbh_edpt_control_open(addr0, 8),); // Get first 8 bytes of device descriptor for Control Endpoint size TU_LOG_USBH("Get 8 byte of Device Descriptor\r\n"); - TU_ASSERT(tuh_descriptor_get_device(addr0, _usbh_ctrl_buf, 8, process_enumeration, ENUM_SET_ADDR), ); + TU_ASSERT(tuh_descriptor_get_device(addr0, _usbh_ctrl_buf, 8, + process_enumeration, ENUM_SET_ADDR),); + break; } - break; #if 0 - case ENUM_RESET_2: - // TODO not used by now, but may be needed for some devices !? - // Reset device again before Set Address - TU_LOG_USBH("Port reset2 \r\n"); - if (_dev0.hub_addr == 0) - { - // connected directly to roothub - hcd_port_reset( _dev0.rhport ); - osal_task_delay(RESET_DELAY); // TODO may not work for no-OS on MCU that require reset_end() since - // sof of controller may not running while resetting - hcd_port_reset_end(_dev0.rhport); - // TODO: fall through to SET ADDRESS, refactor later - } - #if CFG_TUH_HUB - else - { - // after RESET_DELAY the hub_port_reset() already complete - TU_ASSERT( hub_port_reset(_dev0.hub_addr, _dev0.hub_port, process_enumeration, ENUM_HUB_GET_STATUS_2), ); - break; - } - #endif - TU_ATTR_FALLTHROUGH; + case ENUM_RESET_2: + // TODO not used by now, but may be needed for some devices !? + // Reset device again before Set Address + TU_LOG_USBH("Port reset2 \r\n"); + if (_dev0.hub_addr == 0) { + // connected directly to roothub + hcd_port_reset( _dev0.rhport ); + osal_task_delay(RESET_DELAY); // TODO may not work for no-OS on MCU that require reset_end() since + // sof of controller may not running while resetting + hcd_port_reset_end(_dev0.rhport); + // TODO: fall through to SET ADDRESS, refactor later + } +#if CFG_TUH_HUB + else { + // after RESET_DELAY the hub_port_reset() already complete + TU_ASSERT( hub_port_reset(_dev0.hub_addr, _dev0.hub_port, + process_enumeration, ENUM_HUB_GET_STATUS_2), ); + break; + } +#endif + TU_ATTR_FALLTHROUGH; #endif case ENUM_SET_ADDR: enum_request_set_addr(); - break; + break; - case ENUM_GET_DEVICE_DESC: - { + case ENUM_GET_DEVICE_DESC: { uint8_t const new_addr = (uint8_t) tu_le16toh(xfer->setup->wValue); usbh_device_t* new_dev = get_device(new_addr); - TU_ASSERT(new_dev, ); + TU_ASSERT(new_dev,); new_dev->addressed = 1; // Close device 0 hcd_device_close(_dev0.rhport, 0); // open control pipe for new address - TU_ASSERT( usbh_edpt_control_open(new_addr, new_dev->ep0_size), ); + TU_ASSERT(usbh_edpt_control_open(new_addr, new_dev->ep0_size),); // Get full device descriptor TU_LOG_USBH("Get Device Descriptor\r\n"); - TU_ASSERT(tuh_descriptor_get_device(new_addr, _usbh_ctrl_buf, sizeof(tusb_desc_device_t), process_enumeration, ENUM_GET_9BYTE_CONFIG_DESC), ); + TU_ASSERT(tuh_descriptor_get_device(new_addr, _usbh_ctrl_buf, sizeof(tusb_desc_device_t), + process_enumeration, ENUM_GET_9BYTE_CONFIG_DESC),); + break; } - break; - case ENUM_GET_9BYTE_CONFIG_DESC: - { - tusb_desc_device_t const * desc_device = (tusb_desc_device_t const*) _usbh_ctrl_buf; + case ENUM_GET_9BYTE_CONFIG_DESC: { + tusb_desc_device_t const* desc_device = (tusb_desc_device_t const*) _usbh_ctrl_buf; usbh_device_t* dev = get_device(daddr); - TU_ASSERT(dev, ); + TU_ASSERT(dev,); - dev->vid = desc_device->idVendor; - dev->pid = desc_device->idProduct; + dev->vid = desc_device->idVendor; + dev->pid = desc_device->idProduct; dev->i_manufacturer = desc_device->iManufacturer; - dev->i_product = desc_device->iProduct; - dev->i_serial = desc_device->iSerialNumber; + dev->i_product = desc_device->iProduct; + dev->i_serial = desc_device->iSerialNumber; - // if (tuh_attach_cb) tuh_attach_cb((tusb_desc_device_t*) _usbh_ctrl_buf); + // if (tuh_attach_cb) tuh_attach_cb((tusb_desc_device_t*) _usbh_ctrl_buf); // Get 9-byte for total length uint8_t const config_idx = CONFIG_NUM - 1; TU_LOG_USBH("Get Configuration[0] Descriptor (9 bytes)\r\n"); - TU_ASSERT( tuh_descriptor_get_configuration(daddr, config_idx, _usbh_ctrl_buf, 9, process_enumeration, ENUM_GET_FULL_CONFIG_DESC), ); + TU_ASSERT(tuh_descriptor_get_configuration(daddr, config_idx, _usbh_ctrl_buf, 9, + process_enumeration, ENUM_GET_FULL_CONFIG_DESC),); + break; } - break; - case ENUM_GET_FULL_CONFIG_DESC: - { - uint8_t const * desc_config = _usbh_ctrl_buf; + case ENUM_GET_FULL_CONFIG_DESC: { + uint8_t const* desc_config = _usbh_ctrl_buf; // Use offsetof to avoid pointer to the odd/misaligned address - uint16_t const total_len = tu_le16toh( tu_unaligned_read16(desc_config + offsetof(tusb_desc_configuration_t, wTotalLength)) ); + uint16_t const total_len = tu_le16toh( + tu_unaligned_read16(desc_config + offsetof(tusb_desc_configuration_t, wTotalLength))); // TODO not enough buffer to hold configuration descriptor - TU_ASSERT(total_len <= CFG_TUH_ENUMERATION_BUFSIZE, ); + TU_ASSERT(total_len <= CFG_TUH_ENUMERATION_BUFSIZE,); // Get full configuration descriptor uint8_t const config_idx = CONFIG_NUM - 1; TU_LOG_USBH("Get Configuration[0] Descriptor\r\n"); - TU_ASSERT( tuh_descriptor_get_configuration(daddr, config_idx, _usbh_ctrl_buf, total_len, process_enumeration, ENUM_SET_CONFIG), ); + TU_ASSERT(tuh_descriptor_get_configuration(daddr, config_idx, _usbh_ctrl_buf, total_len, + process_enumeration, ENUM_SET_CONFIG),); + break; } - break; case ENUM_SET_CONFIG: - // Parse configuration & set up drivers - // Driver open aren't allowed to make any usb transfer yet - TU_ASSERT( _parse_configuration_descriptor(daddr, (tusb_desc_configuration_t*) _usbh_ctrl_buf), ); + TU_ASSERT(tuh_configuration_set(daddr, CONFIG_NUM, process_enumeration, ENUM_CONFIG_DRIVER),); + break; - TU_ASSERT( tuh_configuration_set(daddr, CONFIG_NUM, process_enumeration, ENUM_CONFIG_DRIVER), ); - break; - - case ENUM_CONFIG_DRIVER: - { + case ENUM_CONFIG_DRIVER: { TU_LOG_USBH("Device configured\r\n"); usbh_device_t* dev = get_device(daddr); - TU_ASSERT(dev, ); + TU_ASSERT(dev,); dev->configured = 1; + // Parse configuration & set up drivers + // driver_open() must not make any usb transfer + TU_ASSERT(_parse_configuration_descriptor(daddr, (tusb_desc_configuration_t*) _usbh_ctrl_buf),); + // Start the Set Configuration process for interfaces (itf = TUSB_INDEX_INVALID_8) // Since driver can perform control transfer within its set_config, this is done asynchronously. // The process continue with next interface when class driver complete its sequence with usbh_driver_set_config_complete() // TODO use separated API instead of using TUSB_INDEX_INVALID_8 usbh_driver_set_config_complete(daddr, TUSB_INDEX_INVALID_8); + break; } - break; default: // stop enumeration if unknown state enum_full_complete(); - break; + break; } } -static bool enum_new_device(hcd_event_t* event) -{ - _dev0.rhport = event->rhport; +static bool enum_new_device(hcd_event_t* event) { + _dev0.rhport = event->rhport; _dev0.hub_addr = event->connection.hub_addr; _dev0.hub_port = event->connection.hub_port; - if (_dev0.hub_addr == 0) - { + if (_dev0.hub_addr == 0) { // connected/disconnected directly with roothub hcd_port_reset(_dev0.rhport); osal_task_delay(ENUM_RESET_DELAY); // TODO may not work for no-OS on MCU that require reset_end() since - // sof of controller may not running while resetting - hcd_port_reset_end( _dev0.rhport); + // sof of controller may not running while resetting + hcd_port_reset_end(_dev0.rhport); // wait until device connection is stable TODO non blocking osal_task_delay(ENUM_CONTACT_DEBOUNCING_DELAY); // device unplugged while delaying - if ( !hcd_port_connect_status(_dev0.rhport) ) { + if (!hcd_port_connect_status(_dev0.rhport)) { enum_full_complete(); return true; } - _dev0.speed = hcd_port_speed_get(_dev0.rhport ); + _dev0.speed = hcd_port_speed_get(_dev0.rhport); TU_LOG_USBH("%s Speed\r\n", tu_str_speed[_dev0.speed]); // fake transfer to kick-off the enumeration process tuh_xfer_t xfer; - xfer.daddr = 0; - xfer.result = XFER_RESULT_SUCCESS; + xfer.daddr = 0; + xfer.result = XFER_RESULT_SUCCESS; xfer.user_data = ENUM_ADDR0_DEVICE_DESC; process_enumeration(&xfer); } #if CFG_TUH_HUB - else - { + else { // connected/disconnected via external hub // wait until device connection is stable TODO non blocking osal_task_delay(ENUM_CONTACT_DEBOUNCING_DELAY); // ENUM_HUB_GET_STATUS //TU_ASSERT( hub_port_get_status(_dev0.hub_addr, _dev0.hub_port, _usbh_ctrl_buf, enum_hub_get_status0_complete, 0) ); - TU_ASSERT( hub_port_get_status(_dev0.hub_addr, _dev0.hub_port, _usbh_ctrl_buf, process_enumeration, ENUM_HUB_CLEAR_RESET_1) ); + TU_ASSERT(hub_port_get_status(_dev0.hub_addr, _dev0.hub_port, _usbh_ctrl_buf, + process_enumeration, ENUM_HUB_CLEAR_RESET_1)); } #endif // hub @@ -1573,58 +1574,48 @@ static uint8_t get_new_address(bool is_hub) { return 0; // invalid address } -static bool enum_request_set_addr(void) -{ - tusb_desc_device_t const * desc_device = (tusb_desc_device_t const*) _usbh_ctrl_buf; +static bool enum_request_set_addr(void) { + tusb_desc_device_t const* desc_device = (tusb_desc_device_t const*) _usbh_ctrl_buf; // Get new address uint8_t const new_addr = get_new_address(desc_device->bDeviceClass == TUSB_CLASS_HUB); TU_ASSERT(new_addr != 0); - TU_LOG_USBH("Set Address = %d\r\n", new_addr); usbh_device_t* new_dev = get_device(new_addr); - - new_dev->rhport = _dev0.rhport; - new_dev->hub_addr = _dev0.hub_addr; - new_dev->hub_port = _dev0.hub_port; - new_dev->speed = _dev0.speed; + new_dev->rhport = _dev0.rhport; + new_dev->hub_addr = _dev0.hub_addr; + new_dev->hub_port = _dev0.hub_port; + new_dev->speed = _dev0.speed; new_dev->connected = 1; - new_dev->ep0_size = desc_device->bMaxPacketSize0; + new_dev->ep0_size = desc_device->bMaxPacketSize0; - tusb_control_request_t const request = - { - .bmRequestType_bit = - { - .recipient = TUSB_REQ_RCPT_DEVICE, - .type = TUSB_REQ_TYPE_STANDARD, - .direction = TUSB_DIR_OUT - }, - .bRequest = TUSB_REQ_SET_ADDRESS, - .wValue = tu_htole16(new_addr), - .wIndex = 0, - .wLength = 0 + tusb_control_request_t const request = { + .bmRequestType_bit = { + .recipient = TUSB_REQ_RCPT_DEVICE, + .type = TUSB_REQ_TYPE_STANDARD, + .direction = TUSB_DIR_OUT + }, + .bRequest = TUSB_REQ_SET_ADDRESS, + .wValue = tu_htole16(new_addr), + .wIndex = 0, + .wLength = 0 + }; + tuh_xfer_t xfer = { + .daddr = 0, // dev0 + .ep_addr = 0, + .setup = &request, + .buffer = NULL, + .complete_cb = process_enumeration, + .user_data = ENUM_GET_DEVICE_DESC }; - tuh_xfer_t xfer = - { - .daddr = 0, // dev0 - .ep_addr = 0, - .setup = &request, - .buffer = NULL, - .complete_cb = process_enumeration, - .user_data = ENUM_GET_DEVICE_DESC - }; - - TU_ASSERT( tuh_control_xfer(&xfer) ); - + TU_ASSERT(tuh_control_xfer(&xfer)); return true; } -static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configuration_t const* desc_cfg) -{ +static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configuration_t const* desc_cfg) { usbh_device_t* dev = get_device(dev_addr); - uint16_t const total_len = tu_le16toh(desc_cfg->wTotalLength); uint8_t const* desc_end = ((uint8_t const*) desc_cfg) + total_len; uint8_t const* p_desc = tu_desc_next(desc_cfg); @@ -1632,13 +1623,11 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur TU_LOG_USBH("Parsing Configuration descriptor (wTotalLength = %u)\r\n", total_len); // parse each interfaces - while( p_desc < desc_end ) - { + while( p_desc < desc_end ) { uint8_t assoc_itf_count = 1; // Class will always starts with Interface Association (if any) and then Interface descriptor - if ( TUSB_DESC_INTERFACE_ASSOCIATION == tu_desc_type(p_desc) ) - { + if ( TUSB_DESC_INTERFACE_ASSOCIATION == tu_desc_type(p_desc) ) { tusb_desc_interface_assoc_t const * desc_iad = (tusb_desc_interface_assoc_t const *) p_desc; assoc_itf_count = desc_iad->bInterfaceCount; @@ -1658,8 +1647,7 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur if (1 == assoc_itf_count && TUSB_CLASS_AUDIO == desc_itf->bInterfaceClass && AUDIO_SUBCLASS_CONTROL == desc_itf->bInterfaceSubClass && - AUDIO_FUNC_PROTOCOL_CODE_UNDEF == desc_itf->bInterfaceProtocol) - { + AUDIO_FUNC_PROTOCOL_CODE_UNDEF == desc_itf->bInterfaceProtocol) { assoc_itf_count = 2; } #endif @@ -1669,8 +1657,7 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur // manually force associated count = 2 if (1 == assoc_itf_count && TUSB_CLASS_CDC == desc_itf->bInterfaceClass && - CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == desc_itf->bInterfaceSubClass) - { + CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == desc_itf->bInterfaceSubClass) { assoc_itf_count = 2; } #endif @@ -1679,18 +1666,14 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur TU_ASSERT(drv_len >= sizeof(tusb_desc_interface_t)); // Find driver for this interface - for (uint8_t drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++) - { + for (uint8_t drv_id = 0; drv_id < TOTAL_DRIVER_COUNT; drv_id++) { usbh_class_driver_t const * driver = get_driver(drv_id); - - if (driver && driver->open(dev->rhport, dev_addr, desc_itf, drv_len) ) - { + if (driver && driver->open(dev->rhport, dev_addr, desc_itf, drv_len) ) { // open successfully TU_LOG_USBH(" %s opened\r\n", driver->name); // bind (associated) interfaces to found driver - for(uint8_t i=0; ibInterfaceNumber+i; // Interface number must not be used already @@ -1704,8 +1687,7 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur break; // exit driver find loop } - if ( drv_id == TOTAL_DRIVER_COUNT - 1 ) - { + if ( drv_id == TOTAL_DRIVER_COUNT - 1 ) { TU_LOG_USBH("[%u:%u] Interface %u: class = %u subclass = %u protocol = %u is not supported\r\n", dev->rhport, dev_addr, desc_itf->bInterfaceNumber, desc_itf->bInterfaceClass, desc_itf->bInterfaceSubClass, desc_itf->bInterfaceProtocol); } diff --git a/pico-sdk/lib/tinyusb/src/host/usbh.h b/pico-sdk/lib/tinyusb/src/host/usbh.h index 9ff1185..3596841 100644 --- a/pico-sdk/lib/tinyusb/src/host/usbh.h +++ b/pico-sdk/lib/tinyusb/src/host/usbh.h @@ -73,11 +73,25 @@ typedef struct { tusb_desc_interface_t desc; } tuh_itf_info_t; -// ConfigID for tuh_config() +// ConfigID for tuh_configure() enum { - TUH_CFGID_RPI_PIO_USB_CONFIGURATION = OPT_MCU_RP2040 << 8 // cfg_param: pio_usb_configuration_t + TUH_CFGID_INVALID = 0, + TUH_CFGID_RPI_PIO_USB_CONFIGURATION = 100, // cfg_param: pio_usb_configuration_t + TUH_CFGID_MAX3421 = 200, }; +typedef struct { + uint8_t max_nak; // max NAK per endpoint per frame + uint8_t cpuctl; // R16: CPU Control Register + uint8_t pinctl; // R17: Pin Control Register. FDUPSPI bit is ignored +} tuh_configure_max3421_t; + +typedef union { + // For TUH_CFGID_RPI_PIO_USB_CONFIGURATION use pio_usb_configuration_t + + tuh_configure_max3421_t max3421; +} tuh_configure_param_t; + //--------------------------------------------------------------------+ // APPLICATION CALLBACK //--------------------------------------------------------------------+ @@ -109,7 +123,11 @@ bool tuh_configure(uint8_t rhport, uint32_t cfg_id, const void* cfg_param); // Init host stack bool tuh_init(uint8_t rhport); +// Deinit host stack on rhport +bool tuh_deinit(uint8_t rhport); + // Check if host stack is already initialized with any roothub ports +// To check if an rhport is initialized, use tuh_rhport_is_active() bool tuh_inited(void); // Task function should be called in main/rtos loop, extended version of tuh_task() diff --git a/pico-sdk/lib/tinyusb/src/host/usbh_pvt.h b/pico-sdk/lib/tinyusb/src/host/usbh_pvt.h index 4a97a1c..95de915 100644 --- a/pico-sdk/lib/tinyusb/src/host/usbh_pvt.h +++ b/pico-sdk/lib/tinyusb/src/host/usbh_pvt.h @@ -35,7 +35,11 @@ extern "C" { #endif -#define TU_LOG_USBH(...) TU_LOG(CFG_TUH_LOG_LEVEL, __VA_ARGS__) +#define TU_LOG_USBH(...) TU_LOG(CFG_TUH_LOG_LEVEL, __VA_ARGS__) +#define TU_LOG_MEM_USBH(...) TU_LOG_MEM(CFG_TUH_LOG_LEVEL, __VA_ARGS__) +#define TU_LOG_BUF_USBH(...) TU_LOG_BUF(CFG_TUH_LOG_LEVEL, __VA_ARGS__) +#define TU_LOG_INT_USBH(...) TU_LOG_INT(CFG_TUH_LOG_LEVEL, __VA_ARGS__) +#define TU_LOG_HEX_USBH(...) TU_LOG_HEX(CFG_TUH_LOG_LEVEL, __VA_ARGS__) enum { USBH_EPSIZE_BULK_MAX = (TUH_OPT_HIGH_SPEED ? TUSB_EPSIZE_BULK_HS : TUSB_EPSIZE_BULK_FS) @@ -46,11 +50,9 @@ enum { //--------------------------------------------------------------------+ typedef struct { - #if CFG_TUSB_DEBUG >= CFG_TUH_LOG_LEVEL char const* name; - #endif - - void (* const init )(void); + bool (* const init )(void); + bool (* const deinit )(void); bool (* const open )(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const * itf_desc, uint16_t max_len); bool (* const set_config )(uint8_t dev_addr, uint8_t itf_num); bool (* const xfer_cb )(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t result, uint32_t xferred_bytes); diff --git a/pico-sdk/lib/tinyusb/src/osal/osal.h b/pico-sdk/lib/tinyusb/src/osal/osal.h index f092e8f..8f45ea5 100644 --- a/pico-sdk/lib/tinyusb/src/osal/osal.h +++ b/pico-sdk/lib/tinyusb/src/osal/osal.h @@ -74,15 +74,18 @@ typedef void (*osal_task_func_t)( void * ); // Should be implemented as static inline function in osal_port.h header /* osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef); + bool osal_semaphore_delete(osal_semaphore_t semd_hdl); bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr); bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec); void osal_semaphore_reset(osal_semaphore_t sem_hdl); // TODO removed osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef); + bool osal_mutex_delete(osal_mutex_t mutex_hdl) bool osal_mutex_lock (osal_mutex_t sem_hdl, uint32_t msec); bool osal_mutex_unlock(osal_mutex_t mutex_hdl); osal_queue_t osal_queue_create(osal_queue_def_t* qdef); + bool osal_queue_delete(osal_queue_t qhdl); bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec); bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr); bool osal_queue_empty(osal_queue_t qhdl); diff --git a/pico-sdk/lib/tinyusb/src/osal/osal_freertos.h b/pico-sdk/lib/tinyusb/src/osal/osal_freertos.h deleted file mode 100644 index 501e0bd..0000000 --- a/pico-sdk/lib/tinyusb/src/osal/osal_freertos.h +++ /dev/null @@ -1,214 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach (tinyusb.org) - * - * 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. - * - * This file is part of the TinyUSB stack. - */ - -#ifndef _TUSB_OSAL_FREERTOS_H_ -#define _TUSB_OSAL_FREERTOS_H_ - -// FreeRTOS Headers -#include TU_INCLUDE_PATH(CFG_TUSB_OS_INC_PATH,FreeRTOS.h) -#include TU_INCLUDE_PATH(CFG_TUSB_OS_INC_PATH,semphr.h) -#include TU_INCLUDE_PATH(CFG_TUSB_OS_INC_PATH,queue.h) -#include TU_INCLUDE_PATH(CFG_TUSB_OS_INC_PATH,task.h) - -#ifdef __cplusplus -extern "C" { -#endif - -//--------------------------------------------------------------------+ -// MACRO CONSTANT TYPEDEF PROTYPES -//--------------------------------------------------------------------+ - -#if configSUPPORT_STATIC_ALLOCATION - typedef StaticSemaphore_t osal_semaphore_def_t; - typedef StaticSemaphore_t osal_mutex_def_t; -#else - // not used therefore defined to smallest possible type to save space - typedef uint8_t osal_semaphore_def_t; - typedef uint8_t osal_mutex_def_t; -#endif - -typedef SemaphoreHandle_t osal_semaphore_t; -typedef SemaphoreHandle_t osal_mutex_t; -typedef QueueHandle_t osal_queue_t; - -typedef struct -{ - uint16_t depth; - uint16_t item_sz; - void* buf; - -#if defined(configQUEUE_REGISTRY_SIZE) && (configQUEUE_REGISTRY_SIZE>0) - char const* name; -#endif - -#if configSUPPORT_STATIC_ALLOCATION - StaticQueue_t sq; -#endif -} osal_queue_def_t; - -#if defined(configQUEUE_REGISTRY_SIZE) && (configQUEUE_REGISTRY_SIZE>0) - #define _OSAL_Q_NAME(_name) .name = #_name -#else - #define _OSAL_Q_NAME(_name) -#endif - -// _int_set is not used with an RTOS -#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \ - static _type _name##_##buf[_depth];\ - osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf, _OSAL_Q_NAME(_name) }; - -//--------------------------------------------------------------------+ -// TASK API -//--------------------------------------------------------------------+ - -TU_ATTR_ALWAYS_INLINE static inline uint32_t _osal_ms2tick(uint32_t msec) { - if ( msec == OSAL_TIMEOUT_WAIT_FOREVER ) return portMAX_DELAY; - if ( msec == 0 ) return 0; - - uint32_t ticks = pdMS_TO_TICKS(msec); - - // configTICK_RATE_HZ is less than 1000 and 1 tick > 1 ms - // we still need to delay at least 1 tick - if ( ticks == 0 ) ticks = 1; - - return ticks; -} - -TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) { - vTaskDelay(pdMS_TO_TICKS(msec)); -} - -//--------------------------------------------------------------------+ -// Semaphore API -//--------------------------------------------------------------------+ - -TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t *semdef) { -#if configSUPPORT_STATIC_ALLOCATION - return xSemaphoreCreateBinaryStatic(semdef); -#else - (void) semdef; - return xSemaphoreCreateBinary(); -#endif -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { - if ( !in_isr ) { - return xSemaphoreGive(sem_hdl) != 0; - } else { - BaseType_t xHigherPriorityTaskWoken = pdFALSE; - BaseType_t res = xSemaphoreGiveFromISR(sem_hdl, &xHigherPriorityTaskWoken); - -#if CFG_TUSB_MCU == OPT_MCU_ESP32S2 || CFG_TUSB_MCU == OPT_MCU_ESP32S3 - // not needed after https://github.com/espressif/esp-idf/commit/c5fd79547ac9b7bae06fa660e9f814d18d3390b7 - if ( xHigherPriorityTaskWoken ) portYIELD_FROM_ISR(); -#else - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); -#endif - - return res != 0; - } -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) { - return xSemaphoreTake(sem_hdl, _osal_ms2tick(msec)); -} - -TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) { - xQueueReset(sem_hdl); -} - -//--------------------------------------------------------------------+ -// MUTEX API (priority inheritance) -//--------------------------------------------------------------------+ - -TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t *mdef) { -#if configSUPPORT_STATIC_ALLOCATION - return xSemaphoreCreateMutexStatic(mdef); -#else - (void) mdef; - return xSemaphoreCreateMutex(); -#endif -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) { - return osal_semaphore_wait(mutex_hdl, msec); -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) { - return xSemaphoreGive(mutex_hdl); -} - -//--------------------------------------------------------------------+ -// QUEUE API -//--------------------------------------------------------------------+ - -TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) { - osal_queue_t q; - -#if configSUPPORT_STATIC_ALLOCATION - q = xQueueCreateStatic(qdef->depth, qdef->item_sz, (uint8_t*) qdef->buf, &qdef->sq); -#else - q = xQueueCreate(qdef->depth, qdef->item_sz); -#endif - -#if defined(configQUEUE_REGISTRY_SIZE) && (configQUEUE_REGISTRY_SIZE>0) - vQueueAddToRegistry(q, qdef->name); -#endif - - return q; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) { - return xQueueReceive(qhdl, data, _osal_ms2tick(msec)); -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const *data, bool in_isr) { - if ( !in_isr ) { - return xQueueSendToBack(qhdl, data, OSAL_TIMEOUT_WAIT_FOREVER) != 0; - } else { - BaseType_t xHigherPriorityTaskWoken = pdFALSE; - BaseType_t res = xQueueSendToBackFromISR(qhdl, data, &xHigherPriorityTaskWoken); - -#if CFG_TUSB_MCU == OPT_MCU_ESP32S2 || CFG_TUSB_MCU == OPT_MCU_ESP32S3 - // not needed after https://github.com/espressif/esp-idf/commit/c5fd79547ac9b7bae06fa660e9f814d18d3390b7 (IDF v5) - if ( xHigherPriorityTaskWoken ) portYIELD_FROM_ISR(); -#else - portYIELD_FROM_ISR(xHigherPriorityTaskWoken); -#endif - - return res != 0; - } -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) { - return uxQueueMessagesWaiting(qhdl) == 0; -} - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/pico-sdk/lib/tinyusb/src/osal/osal_mynewt.h b/pico-sdk/lib/tinyusb/src/osal/osal_mynewt.h deleted file mode 100644 index b8ea208..0000000 --- a/pico-sdk/lib/tinyusb/src/osal/osal_mynewt.h +++ /dev/null @@ -1,176 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2019 Ha Thach (tinyusb.org) - * - * 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. - * - * This file is part of the TinyUSB stack. - */ - -#ifndef OSAL_MYNEWT_H_ -#define OSAL_MYNEWT_H_ - -#include "os/os.h" - -#ifdef __cplusplus - extern "C" { -#endif - -//--------------------------------------------------------------------+ -// TASK API -//--------------------------------------------------------------------+ -TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) -{ - os_time_delay( os_time_ms_to_ticks32(msec) ); -} - -//--------------------------------------------------------------------+ -// Semaphore API -//--------------------------------------------------------------------+ -typedef struct os_sem osal_semaphore_def_t; -typedef struct os_sem* osal_semaphore_t; - -TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) -{ - return (os_sem_init(semdef, 0) == OS_OK) ? (osal_semaphore_t) semdef : NULL; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) -{ - (void) in_isr; - return os_sem_release(sem_hdl) == OS_OK; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) -{ - uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? OS_TIMEOUT_NEVER : os_time_ms_to_ticks32(msec); - return os_sem_pend(sem_hdl, ticks) == OS_OK; -} - -static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) -{ - // TODO implement later -} - -//--------------------------------------------------------------------+ -// MUTEX API (priority inheritance) -//--------------------------------------------------------------------+ -typedef struct os_mutex osal_mutex_def_t; -typedef struct os_mutex* osal_mutex_t; - -TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) -{ - return (os_mutex_init(mdef) == OS_OK) ? (osal_mutex_t) mdef : NULL; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) -{ - uint32_t const ticks = (msec == OSAL_TIMEOUT_WAIT_FOREVER) ? OS_TIMEOUT_NEVER : os_time_ms_to_ticks32(msec); - return os_mutex_pend(mutex_hdl, ticks) == OS_OK; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) -{ - return os_mutex_release(mutex_hdl) == OS_OK; -} - -//--------------------------------------------------------------------+ -// QUEUE API -//--------------------------------------------------------------------+ - -// role device/host is used by OS NONE for mutex (disable usb isr) only -#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \ - static _type _name##_##buf[_depth];\ - static struct os_event _name##_##evbuf[_depth];\ - osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf, .evbuf = _name##_##evbuf};\ - -typedef struct -{ - uint16_t depth; - uint16_t item_sz; - void* buf; - void* evbuf; - - struct os_mempool mpool; - struct os_mempool epool; - - struct os_eventq evq; -}osal_queue_def_t; - -typedef osal_queue_def_t* osal_queue_t; - -TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) -{ - if ( OS_OK != os_mempool_init(&qdef->mpool, qdef->depth, qdef->item_sz, qdef->buf, "usbd queue") ) return NULL; - if ( OS_OK != os_mempool_init(&qdef->epool, qdef->depth, sizeof(struct os_event), qdef->evbuf, "usbd evqueue") ) return NULL; - - os_eventq_init(&qdef->evq); - return (osal_queue_t) qdef; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) -{ - (void) msec; // os_eventq_get() does not take timeout, always behave as msec = WAIT_FOREVER - - struct os_event* ev; - ev = os_eventq_get(&qhdl->evq); - - memcpy(data, ev->ev_arg, qhdl->item_sz); // copy message - os_memblock_put(&qhdl->mpool, ev->ev_arg); // put back mem block - os_memblock_put(&qhdl->epool, ev); // put back ev block - - return true; -} - -static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) -{ - (void) in_isr; - - // get a block from mem pool for data - void* ptr = os_memblock_get(&qhdl->mpool); - if (!ptr) return false; - memcpy(ptr, data, qhdl->item_sz); - - // get a block from event pool to put into queue - struct os_event* ev = (struct os_event*) os_memblock_get(&qhdl->epool); - if (!ev) - { - os_memblock_put(&qhdl->mpool, ptr); - return false; - } - tu_memclr(ev, sizeof(struct os_event)); - ev->ev_arg = ptr; - - os_eventq_put(&qhdl->evq, ev); - - return true; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) -{ - return STAILQ_EMPTY(&qhdl->evq.evq_list); -} - - -#ifdef __cplusplus - } -#endif - -#endif /* OSAL_MYNEWT_H_ */ diff --git a/pico-sdk/lib/tinyusb/src/osal/osal_none.h b/pico-sdk/lib/tinyusb/src/osal/osal_none.h index a07d398..c93f7a8 100644 --- a/pico-sdk/lib/tinyusb/src/osal/osal_none.h +++ b/pico-sdk/lib/tinyusb/src/osal/osal_none.h @@ -54,6 +54,12 @@ TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_ return semdef; } +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_delete(osal_semaphore_t semd_hdl) { + (void) semd_hdl; + return true; // nothing to do +} + + TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { (void) in_isr; sem_hdl->count++; @@ -90,6 +96,11 @@ TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_de return mdef; } +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_delete(osal_mutex_t mutex_hdl) { + (void) mutex_hdl; + return true; // nothing to do +} + TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) { return osal_semaphore_wait(mutex_hdl, msec); } @@ -143,6 +154,11 @@ TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_de return (osal_queue_t) qdef; } +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_delete(osal_queue_t qhdl) { + (void) qhdl; + return true; // nothing to do +} + TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) { (void) msec; // not used, always behave as msec = 0 @@ -164,7 +180,6 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void _osal_q_unlock(qhdl); } - TU_ASSERT(success); return success; } diff --git a/pico-sdk/lib/tinyusb/src/osal/osal_pico.h b/pico-sdk/lib/tinyusb/src/osal/osal_pico.h index e6efa09..315de09 100644 --- a/pico-sdk/lib/tinyusb/src/osal/osal_pico.h +++ b/pico-sdk/lib/tinyusb/src/osal/osal_pico.h @@ -24,8 +24,8 @@ * This file is part of the TinyUSB stack. */ -#ifndef _TUSB_OSAL_PICO_H_ -#define _TUSB_OSAL_PICO_H_ +#ifndef TUSB_OSAL_PICO_H_ +#define TUSB_OSAL_PICO_H_ #include "pico/time.h" #include "pico/sem.h" @@ -33,42 +33,42 @@ #include "pico/critical_section.h" #ifdef __cplusplus - extern "C" { +extern "C" { #endif //--------------------------------------------------------------------+ // TASK API //--------------------------------------------------------------------+ -TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) -{ +TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) { sleep_ms(msec); } //--------------------------------------------------------------------+ // Binary Semaphore API //--------------------------------------------------------------------+ -typedef struct semaphore osal_semaphore_def_t, *osal_semaphore_t; +typedef struct semaphore osal_semaphore_def_t, * osal_semaphore_t; -TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) -{ +TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef) { sem_init(semdef, 0, 255); return semdef; } -TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) -{ +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_delete(osal_semaphore_t semd_hdl) { + (void) semd_hdl; + return true; // nothing to do +} + +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { (void) in_isr; sem_release(sem_hdl); return true; } -TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) -{ +TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) { return sem_acquire_timeout_ms(sem_hdl, msec); } -TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) -{ +TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t sem_hdl) { sem_reset(sem_hdl, 0); } @@ -76,21 +76,23 @@ TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t s // MUTEX API // Within tinyusb, mutex is never used in ISR context //--------------------------------------------------------------------+ -typedef struct mutex osal_mutex_def_t, *osal_mutex_t; +typedef struct mutex osal_mutex_def_t, * osal_mutex_t; -TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) -{ +TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) { mutex_init(mdef); return mdef; } -TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) -{ +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_delete(osal_mutex_t mutex_hdl) { + (void) mutex_hdl; + return true; // nothing to do +} + +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) { return mutex_enter_timeout_ms(mutex_hdl, msec); } -TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) -{ +TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) { mutex_exit(mutex_hdl); return true; } @@ -100,75 +102,53 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hd //--------------------------------------------------------------------+ #include "common/tusb_fifo.h" -typedef struct -{ - tu_fifo_t ff; - struct critical_section critsec; // osal_queue may be used in IRQs, so need critical section +typedef struct { + tu_fifo_t ff; + struct critical_section critsec; // osal_queue may be used in IRQs, so need critical section } osal_queue_def_t; typedef osal_queue_def_t* osal_queue_t; // role device/host is used by OS NONE for mutex (disable usb isr) only -#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \ +#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \ uint8_t _name##_buf[_depth*sizeof(_type)]; \ osal_queue_def_t _name = { \ .ff = TU_FIFO_INIT(_name##_buf, _depth, _type, false) \ } -// lock queue by disable USB interrupt -TU_ATTR_ALWAYS_INLINE static inline void _osal_q_lock(osal_queue_t qhdl) -{ - critical_section_enter_blocking(&qhdl->critsec); -} - -// unlock queue -TU_ATTR_ALWAYS_INLINE static inline void _osal_q_unlock(osal_queue_t qhdl) -{ - critical_section_exit(&qhdl->critsec); -} - -TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) -{ +TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) { critical_section_init(&qdef->critsec); tu_fifo_clear(&qdef->ff); return (osal_queue_t) qdef; } -TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) -{ +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_delete(osal_queue_t qhdl) { + osal_queue_def_t* qdef = (osal_queue_def_t*) qhdl; + critical_section_deinit(&qdef->critsec); + return true; +} + +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) { (void) msec; // not used, always behave as msec = 0 - // TODO: revisit... docs say that mutexes are never used from IRQ context, - // however osal_queue_recieve may be. therefore my assumption is that - // the fifo mutex is not populated for queues used from an IRQ context - //assert(!qhdl->ff.mutex); - - _osal_q_lock(qhdl); + critical_section_enter_blocking(&qhdl->critsec); bool success = tu_fifo_read(&qhdl->ff, data); - _osal_q_unlock(qhdl); + critical_section_exit(&qhdl->critsec); return success; } -TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) -{ - // TODO: revisit... docs say that mutexes are never used from IRQ context, - // however osal_queue_recieve may be. therefore my assumption is that - // the fifo mutex is not populated for queues used from an IRQ context - //assert(!qhdl->ff.mutex); +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const* data, bool in_isr) { (void) in_isr; - _osal_q_lock(qhdl); + critical_section_enter_blocking(&qhdl->critsec); bool success = tu_fifo_write(&qhdl->ff, data); - _osal_q_unlock(qhdl); - - TU_ASSERT(success); + critical_section_exit(&qhdl->critsec); return success; } -TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) -{ +TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) { // TODO: revisit; whether this is true or not currently, tu_fifo_empty is a single // volatile read. @@ -178,7 +158,7 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) } #ifdef __cplusplus - } +} #endif -#endif /* _TUSB_OSAL_PICO_H_ */ +#endif diff --git a/pico-sdk/lib/tinyusb/src/osal/osal_rtthread.h b/pico-sdk/lib/tinyusb/src/osal/osal_rtthread.h deleted file mode 100644 index 18eb9c6..0000000 --- a/pico-sdk/lib/tinyusb/src/osal/osal_rtthread.h +++ /dev/null @@ -1,132 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2020 tfx2001 (2479727366@qq.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. - * - * This file is part of the TinyUSB stack. - */ - -#ifndef _TUSB_OSAL_RTTHREAD_H_ -#define _TUSB_OSAL_RTTHREAD_H_ - -// RT-Thread Headers -#include "rtthread.h" - -#ifdef __cplusplus -extern "C" { -#endif - -//--------------------------------------------------------------------+ -// TASK API -//--------------------------------------------------------------------+ -TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) { - rt_thread_mdelay(msec); -} - -//--------------------------------------------------------------------+ -// Semaphore API -//--------------------------------------------------------------------+ -typedef struct rt_semaphore osal_semaphore_def_t; -typedef rt_sem_t osal_semaphore_t; - -TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t -osal_semaphore_create(osal_semaphore_def_t *semdef) { - rt_sem_init(semdef, "tusb", 0, RT_IPC_FLAG_PRIO); - return semdef; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { - (void) in_isr; - return rt_sem_release(sem_hdl) == RT_EOK; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec) { - return rt_sem_take(sem_hdl, rt_tick_from_millisecond((rt_int32_t) msec)) == RT_EOK; -} - -TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) { - rt_sem_control(sem_hdl, RT_IPC_CMD_RESET, 0); -} - -//--------------------------------------------------------------------+ -// MUTEX API (priority inheritance) -//--------------------------------------------------------------------+ -typedef struct rt_mutex osal_mutex_def_t; -typedef rt_mutex_t osal_mutex_t; - -TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t *mdef) { - rt_mutex_init(mdef, "tusb", RT_IPC_FLAG_PRIO); - return mdef; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec) { - return rt_mutex_take(mutex_hdl, rt_tick_from_millisecond((rt_int32_t) msec)) == RT_EOK; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) { - return rt_mutex_release(mutex_hdl) == RT_EOK; -} - -//--------------------------------------------------------------------+ -// QUEUE API -//--------------------------------------------------------------------+ - -// role device/host is used by OS NONE for mutex (disable usb isr) only -#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \ - static _type _name##_##buf[_depth]; \ - osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf }; - -typedef struct { - uint16_t depth; - uint16_t item_sz; - void *buf; - - struct rt_messagequeue sq; -} osal_queue_def_t; - -typedef rt_mq_t osal_queue_t; - -TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t *qdef) { - rt_mq_init(&(qdef->sq), "tusb", qdef->buf, qdef->item_sz, - qdef->item_sz * qdef->depth, RT_IPC_FLAG_PRIO); - return &(qdef->sq); -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void *data, uint32_t msec) { - - rt_tick_t tick = rt_tick_from_millisecond((rt_int32_t) msec); - return rt_mq_recv(qhdl, data, qhdl->msg_size, tick) == RT_EOK; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const *data, bool in_isr) { - (void) in_isr; - return rt_mq_send(qhdl, (void *)data, qhdl->msg_size) == RT_EOK; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) { - return (qhdl->entry) == 0; -} - -#ifdef __cplusplus -} -#endif - -#endif /* _TUSB_OSAL_RTTHREAD_H_ */ diff --git a/pico-sdk/lib/tinyusb/src/osal/osal_rtx4.h b/pico-sdk/lib/tinyusb/src/osal/osal_rtx4.h deleted file mode 100644 index e443135..0000000 --- a/pico-sdk/lib/tinyusb/src/osal/osal_rtx4.h +++ /dev/null @@ -1,170 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2021 Tian Yunhao (t123yh) - * Copyright (c) 2019 Ha Thach (tinyusb.org) - * - * 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. - * - * This file is part of the TinyUSB stack. - */ - -#ifndef _TUSB_OSAL_RTX4_H_ -#define _TUSB_OSAL_RTX4_H_ - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -//--------------------------------------------------------------------+ -// TASK API -//--------------------------------------------------------------------+ -TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec) -{ - uint16_t hi = msec >> 16; - uint16_t lo = msec; - while (hi--) { - os_dly_wait(0xFFFE); - } - os_dly_wait(lo); -} - -TU_ATTR_ALWAYS_INLINE static inline uint16_t msec2wait(uint32_t msec) { - if (msec == OSAL_TIMEOUT_WAIT_FOREVER) - return 0xFFFF; - else if (msec >= 0xFFFE) - return 0xFFFE; - else - return msec; -} - -//--------------------------------------------------------------------+ -// Semaphore API -//--------------------------------------------------------------------+ -typedef OS_SEM osal_semaphore_def_t; -typedef OS_ID osal_semaphore_t; - -TU_ATTR_ALWAYS_INLINE static inline OS_ID osal_semaphore_create(osal_semaphore_def_t* semdef) { - os_sem_init(semdef, 0); - return semdef; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr) { - if ( !in_isr ) { - os_sem_send(sem_hdl); - } else { - isr_sem_send(sem_hdl); - } - return true; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec) { - return os_sem_wait(sem_hdl, msec2wait(msec)) != OS_R_TMO; -} - -TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) { - // TODO: implement -} - -//--------------------------------------------------------------------+ -// MUTEX API (priority inheritance) -//--------------------------------------------------------------------+ -typedef OS_MUT osal_mutex_def_t; -typedef OS_ID osal_mutex_t; - -TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef) -{ - os_mut_init(mdef); - return mdef; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec) -{ - return os_mut_wait(mutex_hdl, msec2wait(msec)) != OS_R_TMO; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hdl) -{ - return os_mut_release(mutex_hdl) == OS_R_OK; -} - -//--------------------------------------------------------------------+ -// QUEUE API -//--------------------------------------------------------------------+ - -// role device/host is used by OS NONE for mutex (disable usb isr) only -#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \ - os_mbx_declare(_name##__mbox, _depth); \ - _declare_box(_name##__pool, sizeof(_type), _depth); \ - osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .pool = _name##__pool, .mbox = _name##__mbox }; - - -typedef struct -{ - uint16_t depth; - uint16_t item_sz; - U32* pool; - U32* mbox; -}osal_queue_def_t; - -typedef osal_queue_def_t* osal_queue_t; - -TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef) -{ - os_mbx_init(qdef->mbox, (qdef->depth + 4) * 4); - _init_box(qdef->pool, ((qdef->item_sz+3)/4)*(qdef->depth) + 3, qdef->item_sz); - return qdef; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec) -{ - void* buf; - os_mbx_wait(qhdl->mbox, &buf, msec2wait(msec)); - memcpy(data, buf, qhdl->item_sz); - _free_box(qhdl->pool, buf); - return true; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void const * data, bool in_isr) -{ - void* buf = _alloc_box(qhdl->pool); - memcpy(buf, data, qhdl->item_sz); - if ( !in_isr ) - { - os_mbx_send(qhdl->mbox, buf, 0xFFFF); - } - else - { - isr_mbx_send(qhdl->mbox, buf); - } - return true; -} - -TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_empty(osal_queue_t qhdl) -{ - return os_mbx_check(qhdl->mbox) == qhdl->depth; -} - -#ifdef __cplusplus - } -#endif - -#endif diff --git a/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040/dcd_rp2040.c b/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040/dcd_rp2040.c index e8cee73..bc0deee 100644 --- a/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040/dcd_rp2040.c +++ b/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040/dcd_rp2040.c @@ -48,7 +48,7 @@ *------------------------------------------------------------------*/ // Init these in dcd_init -static uint8_t *next_buffer_ptr; +static uint8_t* next_buffer_ptr; // USB_MAX_ENDPOINTS Endpoints, direction TUSB_DIR_OUT for out and TUSB_DIR_IN for in. static struct hw_endpoint hw_endpoints[USB_MAX_ENDPOINTS][2]; @@ -56,79 +56,70 @@ static struct hw_endpoint hw_endpoints[USB_MAX_ENDPOINTS][2]; // SOF may be used by remote wakeup as RESUME, this indicate whether SOF is actually used by usbd static bool _sof_enable = false; -TU_ATTR_ALWAYS_INLINE static inline struct hw_endpoint *hw_endpoint_get_by_num(uint8_t num, tusb_dir_t dir) -{ +TU_ATTR_ALWAYS_INLINE static inline struct hw_endpoint* hw_endpoint_get_by_num(uint8_t num, tusb_dir_t dir) { return &hw_endpoints[num][dir]; } -static struct hw_endpoint *hw_endpoint_get_by_addr(uint8_t ep_addr) -{ +TU_ATTR_ALWAYS_INLINE static inline struct hw_endpoint* hw_endpoint_get_by_addr(uint8_t ep_addr) { uint8_t num = tu_edpt_number(ep_addr); tusb_dir_t dir = tu_edpt_dir(ep_addr); return hw_endpoint_get_by_num(num, dir); } -static void _hw_endpoint_alloc(struct hw_endpoint *ep, uint8_t transfer_type) -{ +static void _hw_endpoint_alloc(struct hw_endpoint* ep, uint8_t transfer_type) { // size must be multiple of 64 uint size = tu_div_ceil(ep->wMaxPacketSize, 64) * 64u; // double buffered Bulk endpoint - if ( transfer_type == TUSB_XFER_BULK ) - { + if (transfer_type == TUSB_XFER_BULK) { size *= 2u; } ep->hw_data_buf = next_buffer_ptr; next_buffer_ptr += size; - assert(((uintptr_t )next_buffer_ptr & 0b111111u) == 0); + assert(((uintptr_t) next_buffer_ptr & 0b111111u) == 0); uint dpram_offset = hw_data_offset(ep->hw_data_buf); hard_assert(hw_data_offset(next_buffer_ptr) <= USB_DPRAM_MAX); pico_info(" Allocated %d bytes at offset 0x%x (0x%p)\r\n", size, dpram_offset, ep->hw_data_buf); // Fill in endpoint control register with buffer offset - uint32_t const reg = EP_CTRL_ENABLE_BITS | ((uint)transfer_type << EP_CTRL_BUFFER_TYPE_LSB) | dpram_offset; + uint32_t const reg = EP_CTRL_ENABLE_BITS | ((uint) transfer_type << EP_CTRL_BUFFER_TYPE_LSB) | dpram_offset; *ep->endpoint_control = reg; } -static void _hw_endpoint_close(struct hw_endpoint *ep) -{ - // Clear hardware registers and then zero the struct - // Clears endpoint enable - *ep->endpoint_control = 0; - // Clears buffer available, etc - *ep->buffer_control = 0; - // Clear any endpoint state - memset(ep, 0, sizeof(struct hw_endpoint)); +static void _hw_endpoint_close(struct hw_endpoint* ep) { + // Clear hardware registers and then zero the struct + // Clears endpoint enable + *ep->endpoint_control = 0; + // Clears buffer available, etc + *ep->buffer_control = 0; + // Clear any endpoint state + memset(ep, 0, sizeof(struct hw_endpoint)); - // Reclaim buffer space if all endpoints are closed - bool reclaim_buffers = true; - for ( uint8_t i = 1; i < USB_MAX_ENDPOINTS; i++ ) - { - if (hw_endpoint_get_by_num(i, TUSB_DIR_OUT)->hw_data_buf != NULL || hw_endpoint_get_by_num(i, TUSB_DIR_IN)->hw_data_buf != NULL) - { - reclaim_buffers = false; - break; - } - } - if (reclaim_buffers) - { - next_buffer_ptr = &usb_dpram->epx_data[0]; + // Reclaim buffer space if all endpoints are closed + bool reclaim_buffers = true; + for (uint8_t i = 1; i < USB_MAX_ENDPOINTS; i++) { + if (hw_endpoint_get_by_num(i, TUSB_DIR_OUT)->hw_data_buf != NULL || + hw_endpoint_get_by_num(i, TUSB_DIR_IN)->hw_data_buf != NULL) { + reclaim_buffers = false; + break; } + } + if (reclaim_buffers) { + next_buffer_ptr = &usb_dpram->epx_data[0]; + } } -static void hw_endpoint_close(uint8_t ep_addr) -{ - struct hw_endpoint *ep = hw_endpoint_get_by_addr(ep_addr); - _hw_endpoint_close(ep); +static void hw_endpoint_close(uint8_t ep_addr) { + struct hw_endpoint* ep = hw_endpoint_get_by_addr(ep_addr); + _hw_endpoint_close(ep); } -static void hw_endpoint_init(uint8_t ep_addr, uint16_t wMaxPacketSize, uint8_t transfer_type) -{ - struct hw_endpoint *ep = hw_endpoint_get_by_addr(ep_addr); +static void hw_endpoint_init(uint8_t ep_addr, uint16_t wMaxPacketSize, uint8_t transfer_type) { + struct hw_endpoint* ep = hw_endpoint_get_by_addr(ep_addr); const uint8_t num = tu_edpt_number(ep_addr); const tusb_dir_t dir = tu_edpt_dir(ep_addr); @@ -143,35 +134,26 @@ static void hw_endpoint_init(uint8_t ep_addr, uint16_t wMaxPacketSize, uint8_t t ep->transfer_type = transfer_type; // Every endpoint has a buffer control register in dpram - if ( dir == TUSB_DIR_IN ) - { + if (dir == TUSB_DIR_IN) { ep->buffer_control = &usb_dpram->ep_buf_ctrl[num].in; - } - else - { + } else { ep->buffer_control = &usb_dpram->ep_buf_ctrl[num].out; } // Clear existing buffer control state *ep->buffer_control = 0; - if ( num == 0 ) - { + if (num == 0) { // EP0 has no endpoint control register because the buffer offsets are fixed ep->endpoint_control = NULL; // Buffer offset is fixed (also double buffered) ep->hw_data_buf = (uint8_t*) &usb_dpram->ep0_buf_a[0]; - } - else - { + } else { // Set the endpoint control register (starts at EP1, hence num-1) - if ( dir == TUSB_DIR_IN ) - { + if (dir == TUSB_DIR_IN) { ep->endpoint_control = &usb_dpram->ep_ctrl[num - 1].in; - } - else - { + } else { ep->endpoint_control = &usb_dpram->ep_ctrl[num - 1].out; } @@ -180,76 +162,82 @@ static void hw_endpoint_init(uint8_t ep_addr, uint16_t wMaxPacketSize, uint8_t t } } -static void hw_endpoint_xfer(uint8_t ep_addr, uint8_t *buffer, uint16_t total_bytes) -{ - struct hw_endpoint *ep = hw_endpoint_get_by_addr(ep_addr); - hw_endpoint_xfer_start(ep, buffer, total_bytes); +static void hw_endpoint_xfer(uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes) { + struct hw_endpoint* ep = hw_endpoint_get_by_addr(ep_addr); + hw_endpoint_xfer_start(ep, buffer, total_bytes); } -static void __tusb_irq_path_func(hw_handle_buff_status)(void) -{ - uint32_t remaining_buffers = usb_hw->buf_status; - pico_trace("buf_status = 0x%08lx\r\n", remaining_buffers); - uint bit = 1u; - for (uint8_t i = 0; remaining_buffers && i < USB_MAX_ENDPOINTS * 2; i++) - { - if (remaining_buffers & bit) - { - // clear this in advance - usb_hw_clear->buf_status = bit; +static void __tusb_irq_path_func(hw_handle_buff_status)(void) { + uint32_t remaining_buffers = usb_hw->buf_status; + pico_trace("buf_status = 0x%08lx\r\n", remaining_buffers); + uint bit = 1u; + for (uint8_t i = 0; remaining_buffers && i < USB_MAX_ENDPOINTS * 2; i++) { + if (remaining_buffers & bit) { + // clear this in advance + usb_hw_clear->buf_status = bit; - // IN transfer for even i, OUT transfer for odd i - struct hw_endpoint *ep = hw_endpoint_get_by_num(i >> 1u, (i & 1u) ? TUSB_DIR_OUT : TUSB_DIR_IN); + // IN transfer for even i, OUT transfer for odd i + struct hw_endpoint* ep = hw_endpoint_get_by_num(i >> 1u, (i & 1u) ? TUSB_DIR_OUT : TUSB_DIR_IN); - // Continue xfer - bool done = hw_endpoint_xfer_continue(ep); - if (done) - { - // Notify - dcd_event_xfer_complete(0, ep->ep_addr, ep->xferred_len, XFER_RESULT_SUCCESS, true); - hw_endpoint_reset_transfer(ep); - } - remaining_buffers &= ~bit; - } - bit <<= 1u; + // Continue xfer + bool done = hw_endpoint_xfer_continue(ep); + if (done) { + // Notify + dcd_event_xfer_complete(0, ep->ep_addr, ep->xferred_len, XFER_RESULT_SUCCESS, true); + hw_endpoint_reset_transfer(ep); + } + remaining_buffers &= ~bit; } + bit <<= 1u; + } } -TU_ATTR_ALWAYS_INLINE static inline void reset_ep0_pid(void) -{ - // If we have finished this transfer on EP0 set pid back to 1 for next - // setup transfer. Also clear a stall in case - uint8_t addrs[] = {0x0, 0x80}; - for (uint i = 0 ; i < TU_ARRAY_SIZE(addrs); i++) - { - struct hw_endpoint *ep = hw_endpoint_get_by_addr(addrs[i]); - ep->next_pid = 1u; +TU_ATTR_ALWAYS_INLINE static inline void reset_ep0(void) { + // If we have finished this transfer on EP0 set pid back to 1 for next + // setup transfer. Also clear a stall in case + for (uint8_t dir = 0; dir < 2; dir++) { + struct hw_endpoint* ep = hw_endpoint_get_by_num(0, dir); + if (ep->active) { + // Abort any pending transfer from a prior control transfer per USB specs + // Due to Errata RP2040-E2: ABORT flag is only applicable for B2 and later (unusable for B0, B1). + // Which means we are not guaranteed to safely abort pending transfer on B0 and B1. + uint32_t const abort_mask = (dir ? USB_EP_ABORT_EP0_IN_BITS : USB_EP_ABORT_EP0_OUT_BITS); + if (rp2040_chip_version() >= 2) { + usb_hw_set->abort = abort_mask; + while ((usb_hw->abort_done & abort_mask) != abort_mask) {} + } + + _hw_endpoint_buffer_control_set_value32(ep, USB_BUF_CTRL_DATA1_PID | USB_BUF_CTRL_SEL); + hw_endpoint_reset_transfer(ep); + + if (rp2040_chip_version() >= 2) { + usb_hw_clear->abort_done = abort_mask; + usb_hw_clear->abort = abort_mask; + } } + ep->next_pid = 1u; + } } -static void __tusb_irq_path_func(reset_non_control_endpoints)(void) -{ +static void __tusb_irq_path_func(reset_non_control_endpoints)(void) { // Disable all non-control - for ( uint8_t i = 0; i < USB_MAX_ENDPOINTS-1; i++ ) - { + for (uint8_t i = 0; i < USB_MAX_ENDPOINTS - 1; i++) { usb_dpram->ep_ctrl[i].in = 0; usb_dpram->ep_ctrl[i].out = 0; } // clear non-control hw endpoints - tu_memclr(hw_endpoints[1], sizeof(hw_endpoints) - 2*sizeof(hw_endpoint_t)); + tu_memclr(hw_endpoints[1], sizeof(hw_endpoints) - 2 * sizeof(hw_endpoint_t)); // reclaim buffer space next_buffer_ptr = &usb_dpram->epx_data[0]; } -static void __tusb_irq_path_func(dcd_rp2040_irq)(void) -{ +static void __tusb_irq_path_func(dcd_rp2040_irq)(void) { uint32_t const status = usb_hw->ints; uint32_t handled = 0; - if ( status & USB_INTF_DEV_SOF_BITS ) - { + if (status & USB_INTF_DEV_SOF_BITS) { bool keep_sof_alive = false; handled |= USB_INTF_DEV_SOF_BITS; @@ -258,20 +246,17 @@ static void __tusb_irq_path_func(dcd_rp2040_irq)(void) // Errata 15 workaround for Device Bulk-In endpoint e15_last_sof = time_us_32(); - for ( uint8_t i = 0; i < USB_MAX_ENDPOINTS; i++ ) - { - struct hw_endpoint * ep = hw_endpoint_get_by_num(i, TUSB_DIR_IN); + for (uint8_t i = 0; i < USB_MAX_ENDPOINTS; i++) { + struct hw_endpoint* ep = hw_endpoint_get_by_num(i, TUSB_DIR_IN); // Active Bulk IN endpoint requires SOF - if ( (ep->transfer_type == TUSB_XFER_BULK) && ep->active ) - { + if ((ep->transfer_type == TUSB_XFER_BULK) && ep->active) { keep_sof_alive = true; hw_endpoint_lock_update(ep, 1); // Deferred enable? - if ( ep->pending ) - { + if (ep->pending) { ep->pending = 0; hw_endpoint_start_next_buffer(ep); } @@ -282,26 +267,24 @@ static void __tusb_irq_path_func(dcd_rp2040_irq)(void) #endif // disable SOF interrupt if it is used for RESUME in remote wakeup - if ( !keep_sof_alive && !_sof_enable ) usb_hw_clear->inte = USB_INTS_DEV_SOF_BITS; + if (!keep_sof_alive && !_sof_enable) usb_hw_clear->inte = USB_INTS_DEV_SOF_BITS; dcd_event_sof(0, usb_hw->sof_rd & USB_SOF_RD_BITS, true); } // xfer events are handled before setup req. So if a transfer completes immediately // before closing the EP, the events will be delivered in same order. - if ( status & USB_INTS_BUFF_STATUS_BITS ) - { + if (status & USB_INTS_BUFF_STATUS_BITS) { handled |= USB_INTS_BUFF_STATUS_BITS; hw_handle_buff_status(); } - if ( status & USB_INTS_SETUP_REQ_BITS ) - { + if (status & USB_INTS_SETUP_REQ_BITS) { handled |= USB_INTS_SETUP_REQ_BITS; - uint8_t const * setup = remove_volatile_cast(uint8_t const*, &usb_dpram->setup_packet); + uint8_t const* setup = remove_volatile_cast(uint8_t const*, &usb_dpram->setup_packet); // reset pid to both 1 (data and ack) - reset_ep0_pid(); + reset_ep0(); // Pass setup packet to tiny usb dcd_event_setup_received(0, setup, true); @@ -329,8 +312,7 @@ static void __tusb_irq_path_func(dcd_rp2040_irq)(void) #endif // SE0 for 2.5 us or more (will last at least 10ms) - if ( status & USB_INTS_BUS_RESET_BITS ) - { + if (status & USB_INTS_BUS_RESET_BITS) { pico_trace("BUS RESET\r\n"); handled |= USB_INTS_BUS_RESET_BITS; @@ -342,7 +324,7 @@ static void __tusb_irq_path_func(dcd_rp2040_irq)(void) #if TUD_OPT_RP2040_USB_DEVICE_ENUMERATION_FIX // Only run enumeration workaround if pull up is enabled - if ( usb_hw->sie_ctrl & USB_SIE_CTRL_PULLUP_EN_BITS ) rp2040_usb_device_enumeration_fix(); + if (usb_hw->sie_ctrl & USB_SIE_CTRL_PULLUP_EN_BITS) rp2040_usb_device_enumeration_fix(); #endif } @@ -354,22 +336,19 @@ static void __tusb_irq_path_func(dcd_rp2040_irq)(void) * because without VBUS detection, it is impossible to tell the difference between * being disconnected and suspended. */ - if ( status & USB_INTS_DEV_SUSPEND_BITS ) - { + if (status & USB_INTS_DEV_SUSPEND_BITS) { handled |= USB_INTS_DEV_SUSPEND_BITS; dcd_event_bus_signal(0, DCD_EVENT_SUSPEND, true); usb_hw_clear->sie_status = USB_SIE_STATUS_SUSPENDED_BITS; } - if ( status & USB_INTS_DEV_RESUME_FROM_HOST_BITS ) - { + if (status & USB_INTS_DEV_RESUME_FROM_HOST_BITS) { handled |= USB_INTS_DEV_RESUME_FROM_HOST_BITS; dcd_event_bus_signal(0, DCD_EVENT_RESUME, true); usb_hw_clear->sie_status = USB_SIE_STATUS_RESUME_BITS; } - if ( status ^ handled ) - { + if (status ^ handled) { panic("Unhandled IRQ 0x%x\n", (uint) (status ^ handled)); } } @@ -390,10 +369,11 @@ static void __tusb_irq_path_func(dcd_rp2040_irq)(void) #define PICO_SHARED_IRQ_HANDLER_HIGHEST_ORDER_PRIORITY 0xff #endif -void dcd_init (uint8_t rhport) -{ +void dcd_init(uint8_t rhport) { assert(rhport == 0); + TU_LOG(2, "Chip Version B%u\r\n", rp2040_chip_version()); + // Reset hardware to default state rp2040_usb_init(); @@ -405,7 +385,7 @@ void dcd_init (uint8_t rhport) irq_add_shared_handler(USBCTRL_IRQ, dcd_rp2040_irq, PICO_SHARED_IRQ_HANDLER_HIGHEST_ORDER_PRIORITY); // Init control endpoints - tu_memclr(hw_endpoints[0], 2*sizeof(hw_endpoint_t)); + tu_memclr(hw_endpoints[0], 2 * sizeof(hw_endpoint_t)); hw_endpoint_init(0x0, 64, TUSB_XFER_CONTROL); hw_endpoint_init(0x80, 64, TUSB_XFER_CONTROL); @@ -420,27 +400,37 @@ void dcd_init (uint8_t rhport) // for the global interrupt enable... // Note: Force VBUS detect cause disconnection not detectable usb_hw->sie_ctrl = USB_SIE_CTRL_EP0_INT_1BUF_BITS; - usb_hw->inte = USB_INTS_BUFF_STATUS_BITS | USB_INTS_BUS_RESET_BITS | USB_INTS_SETUP_REQ_BITS | - USB_INTS_DEV_SUSPEND_BITS | USB_INTS_DEV_RESUME_FROM_HOST_BITS | - (FORCE_VBUS_DETECT ? 0 : USB_INTS_DEV_CONN_DIS_BITS); + usb_hw->inte = USB_INTS_BUFF_STATUS_BITS | USB_INTS_BUS_RESET_BITS | USB_INTS_SETUP_REQ_BITS | + USB_INTS_DEV_SUSPEND_BITS | USB_INTS_DEV_RESUME_FROM_HOST_BITS | + (FORCE_VBUS_DETECT ? 0 : USB_INTS_DEV_CONN_DIS_BITS); dcd_connect(rhport); } -void dcd_int_enable(__unused uint8_t rhport) -{ - assert(rhport == 0); - irq_set_enabled(USBCTRL_IRQ, true); +bool dcd_deinit(uint8_t rhport) { + (void) rhport; + + reset_non_control_endpoints(); + irq_remove_handler(USBCTRL_IRQ, dcd_rp2040_irq); + + // reset usb hardware into initial state + reset_block(RESETS_RESET_USBCTRL_BITS); + unreset_block_wait(RESETS_RESET_USBCTRL_BITS); + + return true; } -void dcd_int_disable(__unused uint8_t rhport) -{ - assert(rhport == 0); - irq_set_enabled(USBCTRL_IRQ, false); +void dcd_int_enable(__unused uint8_t rhport) { + assert(rhport == 0); + irq_set_enabled(USBCTRL_IRQ, true); } -void dcd_set_address (__unused uint8_t rhport, __unused uint8_t dev_addr) -{ +void dcd_int_disable(__unused uint8_t rhport) { + assert(rhport == 0); + irq_set_enabled(USBCTRL_IRQ, false); +} + +void dcd_set_address(__unused uint8_t rhport, __unused uint8_t dev_addr) { assert(rhport == 0); // Can't set device address in hardware until status xfer has complete @@ -448,8 +438,7 @@ void dcd_set_address (__unused uint8_t rhport, __unused uint8_t dev_addr) hw_endpoint_xfer(0x80, NULL, 0); } -void dcd_remote_wakeup(__unused uint8_t rhport) -{ +void dcd_remote_wakeup(__unused uint8_t rhport) { pico_info("dcd_remote_wakeup %d\n", rhport); assert(rhport == 0); @@ -460,100 +449,88 @@ void dcd_remote_wakeup(__unused uint8_t rhport) } // disconnect by disabling internal pull-up resistor on D+/D- -void dcd_disconnect(__unused uint8_t rhport) -{ +void dcd_disconnect(__unused uint8_t rhport) { (void) rhport; usb_hw_clear->sie_ctrl = USB_SIE_CTRL_PULLUP_EN_BITS; } // connect by enabling internal pull-up resistor on D+/D- -void dcd_connect(__unused uint8_t rhport) -{ +void dcd_connect(__unused uint8_t rhport) { (void) rhport; usb_hw_set->sie_ctrl = USB_SIE_CTRL_PULLUP_EN_BITS; } -void dcd_sof_enable(uint8_t rhport, bool en) -{ +void dcd_sof_enable(uint8_t rhport, bool en) { (void) rhport; _sof_enable = en; - if (en) - { + if (en) { usb_hw_set->inte = USB_INTS_DEV_SOF_BITS; - }else - { + } +#if !TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX + else { // Don't clear immediately if the SOF workaround is in use. // The SOF handler will conditionally disable the interrupt. -#if !TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX usb_hw_clear->inte = USB_INTS_DEV_SOF_BITS; -#endif } +#endif } /*------------------------------------------------------------------*/ /* DCD Endpoint port *------------------------------------------------------------------*/ -void dcd_edpt0_status_complete(uint8_t rhport, tusb_control_request_t const * request) -{ +void dcd_edpt0_status_complete(uint8_t rhport, tusb_control_request_t const* request) { (void) rhport; - if ( request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_DEVICE && - request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD && - request->bRequest == TUSB_REQ_SET_ADDRESS ) - { + if (request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_DEVICE && + request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD && + request->bRequest == TUSB_REQ_SET_ADDRESS) { usb_hw->dev_addr_ctrl = (uint8_t) request->wValue; } } -bool dcd_edpt_open (__unused uint8_t rhport, tusb_desc_endpoint_t const * desc_edpt) -{ - assert(rhport == 0); - hw_endpoint_init(desc_edpt->bEndpointAddress, tu_edpt_packet_size(desc_edpt), desc_edpt->bmAttributes.xfer); - return true; +bool dcd_edpt_open(__unused uint8_t rhport, tusb_desc_endpoint_t const* desc_edpt) { + assert(rhport == 0); + hw_endpoint_init(desc_edpt->bEndpointAddress, tu_edpt_packet_size(desc_edpt), desc_edpt->bmAttributes.xfer); + return true; } -void dcd_edpt_close_all (uint8_t rhport) -{ +void dcd_edpt_close_all(uint8_t rhport) { (void) rhport; // may need to use EP Abort reset_non_control_endpoints(); } -bool dcd_edpt_xfer(__unused uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes) -{ - assert(rhport == 0); - hw_endpoint_xfer(ep_addr, buffer, total_bytes); - return true; +bool dcd_edpt_xfer(__unused uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes) { + assert(rhport == 0); + hw_endpoint_xfer(ep_addr, buffer, total_bytes); + return true; } -void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr) -{ +void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr) { (void) rhport; - if ( tu_edpt_number(ep_addr) == 0 ) - { + if (tu_edpt_number(ep_addr) == 0) { // A stall on EP0 has to be armed so it can be cleared on the next setup packet - usb_hw_set->ep_stall_arm = (tu_edpt_dir(ep_addr) == TUSB_DIR_IN) ? USB_EP_STALL_ARM_EP0_IN_BITS : USB_EP_STALL_ARM_EP0_OUT_BITS; + usb_hw_set->ep_stall_arm = (tu_edpt_dir(ep_addr) == TUSB_DIR_IN) ? USB_EP_STALL_ARM_EP0_IN_BITS + : USB_EP_STALL_ARM_EP0_OUT_BITS; } - struct hw_endpoint *ep = hw_endpoint_get_by_addr(ep_addr); + struct hw_endpoint* ep = hw_endpoint_get_by_addr(ep_addr); // stall and clear current pending buffer // may need to use EP_ABORT _hw_endpoint_buffer_control_set_value32(ep, USB_BUF_CTRL_STALL); } -void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) -{ +void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) { (void) rhport; - if (tu_edpt_number(ep_addr)) - { - struct hw_endpoint *ep = hw_endpoint_get_by_addr(ep_addr); + if (tu_edpt_number(ep_addr)) { + struct hw_endpoint* ep = hw_endpoint_get_by_addr(ep_addr); // clear stall also reset toggle to DATA0, ready for next transfer ep->next_pid = 0; @@ -561,16 +538,13 @@ void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) } } -void dcd_edpt_close (uint8_t rhport, uint8_t ep_addr) -{ - (void) rhport; - - pico_trace("dcd_edpt_close %02x\r\n", ep_addr); - hw_endpoint_close(ep_addr); +void dcd_edpt_close(uint8_t rhport, uint8_t ep_addr) { + (void) rhport; + pico_trace("dcd_edpt_close %02x\r\n", ep_addr); + hw_endpoint_close(ep_addr); } -void __tusb_irq_path_func(dcd_int_handler)(uint8_t rhport) -{ +void __tusb_irq_path_func(dcd_int_handler)(uint8_t rhport) { (void) rhport; dcd_rp2040_irq(); } diff --git a/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040/hcd_rp2040.c b/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040/hcd_rp2040.c index 08aef93..222dbbb 100644 --- a/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040/hcd_rp2040.c +++ b/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040/hcd_rp2040.c @@ -113,7 +113,7 @@ static void __tusb_irq_path_func(_handle_buff_status_bit)(uint bit, struct hw_en static void __tusb_irq_path_func(hw_handle_buff_status)(void) { uint32_t remaining_buffers = usb_hw->buf_status; - pico_trace("buf_status 0x%08x\n", remaining_buffers); + pico_trace("buf_status 0x%08lx\n", remaining_buffers); // Check EPX first uint bit = 0b1; @@ -325,10 +325,8 @@ static void _hw_endpoint_init(struct hw_endpoint *ep, uint8_t dev_addr, uint8_t ep->wMaxPacketSize = wMaxPacketSize; ep->transfer_type = transfer_type; - pico_trace("hw_endpoint_init dev %d ep %d %s xfer %d\n", ep->dev_addr, tu_edpt_number(ep->ep_addr), - ep_dir_string[tu_edpt_dir(ep->ep_addr)], ep->transfer_type); - pico_trace("dev %d ep %d %s setup buffer @ 0x%p\n", ep->dev_addr, tu_edpt_number(ep->ep_addr), - ep_dir_string[tu_edpt_dir(ep->ep_addr)], ep->hw_data_buf); + pico_trace("hw_endpoint_init dev %d ep %02X xfer %d\n", ep->dev_addr, ep->ep_addr, ep->transfer_type); + pico_trace("dev %d ep %02X setup buffer @ 0x%p\n", ep->dev_addr, ep->ep_addr, ep->hw_data_buf); uint dpram_offset = hw_data_offset(ep->hw_data_buf); // Bits 0-5 should be 0 assert(!(dpram_offset & 0b111111)); @@ -343,7 +341,7 @@ static void _hw_endpoint_init(struct hw_endpoint *ep, uint8_t dev_addr, uint8_t ep_reg |= (uint32_t) ((bmInterval - 1) << EP_CTRL_HOST_INTERRUPT_INTERVAL_LSB); } *ep->endpoint_control = ep_reg; - pico_trace("endpoint control (0x%p) <- 0x%x\n", ep->endpoint_control, ep_reg); + pico_trace("endpoint control (0x%p) <- 0x%lx\n", ep->endpoint_control, ep_reg); ep->configured = true; if ( ep != &epx ) @@ -411,6 +409,16 @@ bool hcd_init(uint8_t rhport) return true; } +bool hcd_deinit(uint8_t rhport) { + (void) rhport; + + irq_remove_handler(USBCTRL_IRQ, hcd_rp2040_irq); + reset_block(RESETS_RESET_USBCTRL_BITS); + unreset_block_wait(RESETS_RESET_USBCTRL_BITS); + + return true; +} + void hcd_port_reset(uint8_t rhport) { (void) rhport; diff --git a/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040/rp2040_usb.c b/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040/rp2040_usb.c index a512dc3..1ca711c 100644 --- a/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040/rp2040_usb.c +++ b/pico-sdk/lib/tinyusb/src/portable/raspberrypi/rp2040/rp2040_usb.c @@ -35,26 +35,18 @@ //--------------------------------------------------------------------+ // MACRO CONSTANT TYPEDEF PROTOTYPE //--------------------------------------------------------------------+ - -// Direction strings for debug -const char *ep_dir_string[] = { - "out", - "in", -}; - -static void _hw_endpoint_xfer_sync(struct hw_endpoint *ep); +static void _hw_endpoint_xfer_sync(struct hw_endpoint* ep); #if TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX - static bool e15_is_bulkin_ep(struct hw_endpoint *ep); - static bool e15_is_critical_frame_period(struct hw_endpoint *ep); + static bool e15_is_bulkin_ep(struct hw_endpoint* ep); + static bool e15_is_critical_frame_period(struct hw_endpoint* ep); #else #define e15_is_bulkin_ep(x) (false) #define e15_is_critical_frame_period(x) (false) #endif // if usb hardware is in host mode -TU_ATTR_ALWAYS_INLINE static inline bool is_host_mode(void) -{ +TU_ATTR_ALWAYS_INLINE static inline bool is_host_mode(void) { return (usb_hw->main_ctrl & USB_MAIN_CTRL_HOST_NDEVICE_BITS) ? true : false; } @@ -62,8 +54,7 @@ TU_ATTR_ALWAYS_INLINE static inline bool is_host_mode(void) // Implementation //--------------------------------------------------------------------+ -void rp2040_usb_init(void) -{ +void rp2040_usb_init(void) { // Reset usb controller reset_block(RESETS_RESET_USBCTRL_BITS); unreset_block_wait(RESETS_RESET_USBCTRL_BITS); @@ -88,46 +79,33 @@ void rp2040_usb_init(void) TU_LOG2_INT(sizeof(hw_endpoint_t)); } -void __tusb_irq_path_func(hw_endpoint_reset_transfer)(struct hw_endpoint *ep) -{ +void __tusb_irq_path_func(hw_endpoint_reset_transfer)(struct hw_endpoint* ep) { ep->active = false; ep->remaining_len = 0; ep->xferred_len = 0; ep->user_buf = 0; } -void __tusb_irq_path_func(_hw_endpoint_buffer_control_update32)(struct hw_endpoint *ep, uint32_t and_mask, uint32_t or_mask) -{ +void __tusb_irq_path_func(_hw_endpoint_buffer_control_update32)(struct hw_endpoint* ep, uint32_t and_mask, + uint32_t or_mask) { uint32_t value = 0; - if ( and_mask ) - { + if (and_mask) { value = *ep->buffer_control & and_mask; } - if ( or_mask ) - { + if (or_mask) { value |= or_mask; - if ( or_mask & USB_BUF_CTRL_AVAIL ) - { - if ( *ep->buffer_control & USB_BUF_CTRL_AVAIL ) - { - panic("ep %d %s was already available", tu_edpt_number(ep->ep_addr), ep_dir_string[tu_edpt_dir(ep->ep_addr)]); + if (or_mask & USB_BUF_CTRL_AVAIL) { + if (*ep->buffer_control & USB_BUF_CTRL_AVAIL) { + panic("ep %02X was already available", ep->ep_addr); } *ep->buffer_control = value & ~USB_BUF_CTRL_AVAIL; - // 12 cycle delay.. (should be good for 48*12Mhz = 576Mhz) + // 4.1.2.5.1 Con-current access: 12 cycles (should be good for 48*12Mhz = 576Mhz) after write to buffer control // Don't need delay in host mode as host is in charge -#if !CFG_TUH_ENABLED - __asm volatile ( - "b 1f\n" - "1: b 1f\n" - "1: b 1f\n" - "1: b 1f\n" - "1: b 1f\n" - "1: b 1f\n" - "1:\n" - : : : "memory"); -#endif + if ( !is_host_mode()) { + busy_wait_at_least_cycles(12); + } } } @@ -135,10 +113,9 @@ void __tusb_irq_path_func(_hw_endpoint_buffer_control_update32)(struct hw_endpoi } // prepare buffer, return buffer control -static uint32_t __tusb_irq_path_func(prepare_ep_buffer)(struct hw_endpoint *ep, uint8_t buf_id) -{ +static uint32_t __tusb_irq_path_func(prepare_ep_buffer)(struct hw_endpoint* ep, uint8_t buf_id) { uint16_t const buflen = tu_min16(ep->remaining_len, ep->wMaxPacketSize); - ep->remaining_len = (uint16_t)(ep->remaining_len - buflen); + ep->remaining_len = (uint16_t) (ep->remaining_len - buflen); uint32_t buf_ctrl = buflen | USB_BUF_CTRL_AVAIL; @@ -146,10 +123,9 @@ static uint32_t __tusb_irq_path_func(prepare_ep_buffer)(struct hw_endpoint *ep, buf_ctrl |= ep->next_pid ? USB_BUF_CTRL_DATA1_PID : USB_BUF_CTRL_DATA0_PID; ep->next_pid ^= 1u; - if ( !ep->rx ) - { + if (!ep->rx) { // Copy data from user buffer to hw buffer - memcpy(ep->hw_data_buf + buf_id*64, ep->user_buf, buflen); + memcpy(ep->hw_data_buf + buf_id * 64, ep->user_buf, buflen); ep->user_buf += buflen; // Mark as full @@ -159,8 +135,7 @@ static uint32_t __tusb_irq_path_func(prepare_ep_buffer)(struct hw_endpoint *ep, // Is this the last buffer? Only really matters for host mode. Will trigger // the trans complete irq but also stop it polling. We only really care about // trans complete for setup packets being sent - if (ep->remaining_len == 0) - { + if (ep->remaining_len == 0) { buf_ctrl |= USB_BUF_CTRL_LAST; } @@ -170,8 +145,7 @@ static uint32_t __tusb_irq_path_func(prepare_ep_buffer)(struct hw_endpoint *ep, } // Prepare buffer control register value -void __tusb_irq_path_func(hw_endpoint_start_next_buffer)(struct hw_endpoint *ep) -{ +void __tusb_irq_path_func(hw_endpoint_start_next_buffer)(struct hw_endpoint* ep) { uint32_t ep_ctrl = *ep->endpoint_control; // always compute and start with buffer 0 @@ -186,8 +160,7 @@ void __tusb_irq_path_func(hw_endpoint_start_next_buffer)(struct hw_endpoint *ep) bool const force_single = (!is_host && !tu_edpt_dir(ep->ep_addr)) || (is_host && tu_edpt_number(ep->ep_addr) != 0); - if(ep->remaining_len && !force_single) - { + if (ep->remaining_len && !force_single) { // Use buffer 1 (double buffered) if there is still data // TODO: Isochronous for buffer1 bit-field is different than CBI (control bulk, interrupt) @@ -196,8 +169,7 @@ void __tusb_irq_path_func(hw_endpoint_start_next_buffer)(struct hw_endpoint *ep) // Set endpoint control double buffered bit if needed ep_ctrl &= ~EP_CTRL_INTERRUPT_PER_BUFFER; ep_ctrl |= EP_CTRL_DOUBLE_BUFFERED_BITS | EP_CTRL_INTERRUPT_PER_DOUBLE_BUFFER; - }else - { + } else { // Single buffered since 1 is enough ep_ctrl &= ~(EP_CTRL_DOUBLE_BUFFERED_BITS | EP_CTRL_INTERRUPT_PER_DOUBLE_BUFFER); ep_ctrl |= EP_CTRL_INTERRUPT_PER_BUFFER; @@ -212,35 +184,28 @@ void __tusb_irq_path_func(hw_endpoint_start_next_buffer)(struct hw_endpoint *ep) _hw_endpoint_buffer_control_set_value32(ep, buf_ctrl); } -void hw_endpoint_xfer_start(struct hw_endpoint *ep, uint8_t *buffer, uint16_t total_len) -{ +void hw_endpoint_xfer_start(struct hw_endpoint* ep, uint8_t* buffer, uint16_t total_len) { hw_endpoint_lock_update(ep, 1); - if ( ep->active ) - { + if (ep->active) { // TODO: Is this acceptable for interrupt packets? - TU_LOG(1, "WARN: starting new transfer on already active ep %d %s\r\n", tu_edpt_number(ep->ep_addr), - ep_dir_string[tu_edpt_dir(ep->ep_addr)]); - + TU_LOG(1, "WARN: starting new transfer on already active ep %02X\r\n", ep->ep_addr); hw_endpoint_reset_transfer(ep); } // Fill in info now that we're kicking off the hw ep->remaining_len = total_len; - ep->xferred_len = 0; - ep->active = true; - ep->user_buf = buffer; + ep->xferred_len = 0; + ep->active = true; + ep->user_buf = buffer; - if ( e15_is_bulkin_ep(ep) ) - { + if (e15_is_bulkin_ep(ep)) { usb_hw_set->inte = USB_INTS_DEV_SOF_BITS; } - if ( e15_is_critical_frame_period(ep) ) - { + if (e15_is_critical_frame_period(ep)) { ep->pending = 1; - } else - { + } else { hw_endpoint_start_next_buffer(ep); } @@ -248,34 +213,30 @@ void hw_endpoint_xfer_start(struct hw_endpoint *ep, uint8_t *buffer, uint16_t to } // sync endpoint buffer and return transferred bytes -static uint16_t __tusb_irq_path_func(sync_ep_buffer)(struct hw_endpoint *ep, uint8_t buf_id) -{ +static uint16_t __tusb_irq_path_func(sync_ep_buffer)(struct hw_endpoint* ep, uint8_t buf_id) { uint32_t buf_ctrl = _hw_endpoint_buffer_control_get_value32(ep); - if (buf_id) buf_ctrl = buf_ctrl >> 16; + if (buf_id) buf_ctrl = buf_ctrl >> 16; uint16_t xferred_bytes = buf_ctrl & USB_BUF_CTRL_LEN_MASK; - if ( !ep->rx ) - { + if (!ep->rx) { // We are continuing a transfer here. If we are TX, we have successfully // sent some data can increase the length we have sent assert(!(buf_ctrl & USB_BUF_CTRL_FULL)); - ep->xferred_len = (uint16_t)(ep->xferred_len + xferred_bytes); - }else - { + ep->xferred_len = (uint16_t) (ep->xferred_len + xferred_bytes); + } else { // If we have received some data, so can increase the length // we have received AFTER we have copied it to the user buffer at the appropriate offset assert(buf_ctrl & USB_BUF_CTRL_FULL); - memcpy(ep->user_buf, ep->hw_data_buf + buf_id*64, xferred_bytes); - ep->xferred_len = (uint16_t)(ep->xferred_len + xferred_bytes); + memcpy(ep->user_buf, ep->hw_data_buf + buf_id * 64, xferred_bytes); + ep->xferred_len = (uint16_t) (ep->xferred_len + xferred_bytes); ep->user_buf += xferred_bytes; } // Short packet - if (xferred_bytes < ep->wMaxPacketSize) - { + if (xferred_bytes < ep->wMaxPacketSize) { pico_trace(" Short packet on buffer %d with %u bytes\r\n", buf_id, xferred_bytes); // Reduce total length as this is last packet ep->remaining_len = 0; @@ -284,8 +245,7 @@ static uint16_t __tusb_irq_path_func(sync_ep_buffer)(struct hw_endpoint *ep, uin return xferred_bytes; } -static void __tusb_irq_path_func(_hw_endpoint_xfer_sync) (struct hw_endpoint *ep) -{ +static void __tusb_irq_path_func(_hw_endpoint_xfer_sync)(struct hw_endpoint* ep) { // Update hw endpoint struct with info from hardware // after a buff status interrupt @@ -296,14 +256,11 @@ static void __tusb_irq_path_func(_hw_endpoint_xfer_sync) (struct hw_endpoint *ep uint16_t buf0_bytes = sync_ep_buffer(ep, 0); // sync buffer 1 if double buffered - if ( (*ep->endpoint_control) & EP_CTRL_DOUBLE_BUFFERED_BITS ) - { - if (buf0_bytes == ep->wMaxPacketSize) - { + if ((*ep->endpoint_control) & EP_CTRL_DOUBLE_BUFFERED_BITS) { + if (buf0_bytes == ep->wMaxPacketSize) { // sync buffer 1 if not short packet sync_ep_buffer(ep, 1); - }else - { + } else { // short packet on buffer 0 // TODO couldn't figure out how to handle this case which happen with net_lwip_webserver example // At this time (currently trigger per 2 buffer), the buffer1 is probably filled with data from @@ -335,14 +292,12 @@ static void __tusb_irq_path_func(_hw_endpoint_xfer_sync) (struct hw_endpoint *ep } // Returns true if transfer is complete -bool __tusb_irq_path_func(hw_endpoint_xfer_continue)(struct hw_endpoint *ep) -{ +bool __tusb_irq_path_func(hw_endpoint_xfer_continue)(struct hw_endpoint* ep) { hw_endpoint_lock_update(ep, 1); // Part way through a transfer - if (!ep->active) - { - panic("Can't continue xfer on inactive ep %d %s", tu_edpt_number(ep->ep_addr), ep_dir_string[tu_edpt_dir(ep->ep_addr)]); + if (!ep->active) { + panic("Can't continue xfer on inactive ep %02X", ep->ep_addr); } // Update EP struct from hardware state @@ -350,21 +305,15 @@ bool __tusb_irq_path_func(hw_endpoint_xfer_continue)(struct hw_endpoint *ep) // Now we have synced our state with the hardware. Is there more data to transfer? // If we are done then notify tinyusb - if (ep->remaining_len == 0) - { - pico_trace("Completed transfer of %d bytes on ep %d %s\r\n", - ep->xferred_len, tu_edpt_number(ep->ep_addr), ep_dir_string[tu_edpt_dir(ep->ep_addr)]); + if (ep->remaining_len == 0) { + pico_trace("Completed transfer of %d bytes on ep %02X\r\n", ep->xferred_len, ep->ep_addr); // Notify caller we are done so it can notify the tinyusb stack hw_endpoint_lock_update(ep, -1); return true; - } - else - { - if ( e15_is_critical_frame_period(ep) ) - { + } else { + if (e15_is_critical_frame_period(ep)) { ep->pending = 1; - } else - { + } else { hw_endpoint_start_next_buffer(ep); } } @@ -399,16 +348,14 @@ bool __tusb_irq_path_func(hw_endpoint_xfer_continue)(struct hw_endpoint *ep) volatile uint32_t e15_last_sof = 0; // check if Errata 15 is needed for this endpoint i.e device bulk-in -static bool __tusb_irq_path_func(e15_is_bulkin_ep) (struct hw_endpoint *ep) -{ +static bool __tusb_irq_path_func(e15_is_bulkin_ep)(struct hw_endpoint* ep) { return (!is_host_mode() && tu_edpt_dir(ep->ep_addr) == TUSB_DIR_IN && ep->transfer_type == TUSB_XFER_BULK); } // check if we need to apply Errata 15 workaround : i.e // Endpoint is BULK IN and is currently in critical frame period i.e 20% of last usb frame -static bool __tusb_irq_path_func(e15_is_critical_frame_period) (struct hw_endpoint *ep) -{ +static bool __tusb_irq_path_func(e15_is_critical_frame_period)(struct hw_endpoint* ep) { TU_VERIFY(e15_is_bulkin_ep(ep)); /* Avoid the last 200us (uframe 6.5-7) of a frame, up to the EOF2 point. @@ -419,11 +366,10 @@ static bool __tusb_irq_path_func(e15_is_critical_frame_period) (struct hw_endpoi if (delta < 800 || delta > 998) { return false; } - TU_LOG(3, "Avoiding sof %lu now %lu last %lu\r\n", (usb_hw->sof_rd + 1) & USB_SOF_RD_BITS, time_us_32(), e15_last_sof); + TU_LOG(3, "Avoiding sof %lu now %lu last %lu\r\n", (usb_hw->sof_rd + 1) & USB_SOF_RD_BITS, time_us_32(), + e15_last_sof); return true; } -#endif - - +#endif // TUD_OPT_RP2040_USB_DEVICE_UFRAME_FIX #endif diff --git a/pico-sdk/lib/tinyusb/src/tinyusb.mk b/pico-sdk/lib/tinyusb/src/tinyusb.mk index 84974ad..3d7c644 100644 --- a/pico-sdk/lib/tinyusb/src/tinyusb.mk +++ b/pico-sdk/lib/tinyusb/src/tinyusb.mk @@ -4,11 +4,15 @@ TINYUSB_SRC_C += \ src/common/tusb_fifo.c \ src/device/usbd.c \ src/device/usbd_control.c \ + src/class/msc/msc_device.c \ src/typec/usbc.c \ + src/class/audio/audio_device.c \ src/class/cdc/cdc_device.c \ + src/class/dfu/dfu_device.c \ + src/class/dfu/dfu_rt_device.c \ src/class/hid/hid_device.c \ + src/class/usbtmc/usbtmc_device.c \ src/host/usbh.c \ src/host/hub.c \ - src/class/cdc/cdc_host.c \ src/class/hid/hid_host.c \ src/typec/usbc.c \ diff --git a/pico-sdk/lib/tinyusb/src/tusb.c b/pico-sdk/lib/tinyusb/src/tusb.c index 7943ea5..0092267 100644 --- a/pico-sdk/lib/tinyusb/src/tusb.c +++ b/pico-sdk/lib/tinyusb/src/tusb.c @@ -43,32 +43,30 @@ // Public API //--------------------------------------------------------------------+ -bool tusb_init(void) -{ -#if CFG_TUD_ENABLED && defined(TUD_OPT_RHPORT) +bool tusb_init(void) { + #if CFG_TUD_ENABLED && defined(TUD_OPT_RHPORT) // init device stack CFG_TUSB_RHPORTx_MODE must be defined TU_ASSERT ( tud_init(TUD_OPT_RHPORT) ); -#endif + #endif -#if CFG_TUH_ENABLED && defined(TUH_OPT_RHPORT) + #if CFG_TUH_ENABLED && defined(TUH_OPT_RHPORT) // init host stack CFG_TUSB_RHPORTx_MODE must be defined TU_ASSERT( tuh_init(TUH_OPT_RHPORT) ); -#endif + #endif return true; } -bool tusb_inited(void) -{ +bool tusb_inited(void) { bool ret = false; -#if CFG_TUD_ENABLED + #if CFG_TUD_ENABLED ret = ret || tud_inited(); -#endif + #endif -#if CFG_TUH_ENABLED + #if CFG_TUH_ENABLED ret = ret || tuh_inited(); -#endif + #endif return ret; } @@ -77,43 +75,35 @@ bool tusb_inited(void) // Descriptor helper //--------------------------------------------------------------------+ -uint8_t const * tu_desc_find(uint8_t const* desc, uint8_t const* end, uint8_t byte1) -{ - while(desc+1 < end) - { - if ( desc[1] == byte1 ) return desc; +uint8_t const* tu_desc_find(uint8_t const* desc, uint8_t const* end, uint8_t byte1) { + while (desc + 1 < end) { + if (desc[1] == byte1) return desc; desc += desc[DESC_OFFSET_LEN]; } return NULL; } -uint8_t const * tu_desc_find2(uint8_t const* desc, uint8_t const* end, uint8_t byte1, uint8_t byte2) -{ - while(desc+2 < end) - { - if ( desc[1] == byte1 && desc[2] == byte2) return desc; +uint8_t const* tu_desc_find2(uint8_t const* desc, uint8_t const* end, uint8_t byte1, uint8_t byte2) { + while (desc + 2 < end) { + if (desc[1] == byte1 && desc[2] == byte2) return desc; desc += desc[DESC_OFFSET_LEN]; } return NULL; } -uint8_t const * tu_desc_find3(uint8_t const* desc, uint8_t const* end, uint8_t byte1, uint8_t byte2, uint8_t byte3) -{ - while(desc+3 < end) - { +uint8_t const* tu_desc_find3(uint8_t const* desc, uint8_t const* end, uint8_t byte1, uint8_t byte2, uint8_t byte3) { + while (desc + 3 < end) { if (desc[1] == byte1 && desc[2] == byte2 && desc[3] == byte3) return desc; desc += desc[DESC_OFFSET_LEN]; } return NULL; } - //--------------------------------------------------------------------+ // Endpoint Helper for both Host and Device stack //--------------------------------------------------------------------+ -bool tu_edpt_claim(tu_edpt_state_t* ep_state, osal_mutex_t mutex) -{ +bool tu_edpt_claim(tu_edpt_state_t* ep_state, osal_mutex_t mutex) { (void) mutex; // pre-check to help reducing mutex lock @@ -122,111 +112,93 @@ bool tu_edpt_claim(tu_edpt_state_t* ep_state, osal_mutex_t mutex) // can only claim the endpoint if it is not busy and not claimed yet. bool const available = (ep_state->busy == 0) && (ep_state->claimed == 0); - if (available) - { + if (available) { ep_state->claimed = 1; } (void) osal_mutex_unlock(mutex); - return available; } -bool tu_edpt_release(tu_edpt_state_t* ep_state, osal_mutex_t mutex) -{ +bool tu_edpt_release(tu_edpt_state_t* ep_state, osal_mutex_t mutex) { (void) mutex; - (void) osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER); // can only release the endpoint if it is claimed and not busy bool const ret = (ep_state->claimed == 1) && (ep_state->busy == 0); - if (ret) - { + if (ret) { ep_state->claimed = 0; } (void) osal_mutex_unlock(mutex); - return ret; } -bool tu_edpt_validate(tusb_desc_endpoint_t const * desc_ep, tusb_speed_t speed) -{ +bool tu_edpt_validate(tusb_desc_endpoint_t const* desc_ep, tusb_speed_t speed) { uint16_t const max_packet_size = tu_edpt_packet_size(desc_ep); TU_LOG2(" Open EP %02X with Size = %u\r\n", desc_ep->bEndpointAddress, max_packet_size); - switch (desc_ep->bmAttributes.xfer) - { - case TUSB_XFER_ISOCHRONOUS: - { + switch (desc_ep->bmAttributes.xfer) { + case TUSB_XFER_ISOCHRONOUS: { uint16_t const spec_size = (speed == TUSB_SPEED_HIGH ? 1024 : 1023); TU_ASSERT(max_packet_size <= spec_size); + break; } - break; case TUSB_XFER_BULK: - if (speed == TUSB_SPEED_HIGH) - { + if (speed == TUSB_SPEED_HIGH) { // Bulk highspeed must be EXACTLY 512 TU_ASSERT(max_packet_size == 512); - }else - { + } else { // TODO Bulk fullspeed can only be 8, 16, 32, 64 TU_ASSERT(max_packet_size <= 64); } - break; + break; - case TUSB_XFER_INTERRUPT: - { + case TUSB_XFER_INTERRUPT: { uint16_t const spec_size = (speed == TUSB_SPEED_HIGH ? 1024 : 64); TU_ASSERT(max_packet_size <= spec_size); + break; } - break; - default: return false; + default: + return false; } return true; } -void tu_edpt_bind_driver(uint8_t ep2drv[][2], tusb_desc_interface_t const* desc_itf, uint16_t desc_len, uint8_t driver_id) -{ +void tu_edpt_bind_driver(uint8_t ep2drv[][2], tusb_desc_interface_t const* desc_itf, uint16_t desc_len, + uint8_t driver_id) { uint8_t const* p_desc = (uint8_t const*) desc_itf; uint8_t const* desc_end = p_desc + desc_len; - while( p_desc < desc_end ) - { - if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) ) - { + while (p_desc < desc_end) { + if (TUSB_DESC_ENDPOINT == tu_desc_type(p_desc)) { uint8_t const ep_addr = ((tusb_desc_endpoint_t const*) p_desc)->bEndpointAddress; - TU_LOG(2, " Bind EP %02x to driver id %u\r\n", ep_addr, driver_id); ep2drv[tu_edpt_number(ep_addr)][tu_edpt_dir(ep_addr)] = driver_id; } - p_desc = tu_desc_next(p_desc); } } -uint16_t tu_desc_get_interface_total_len(tusb_desc_interface_t const* desc_itf, uint8_t itf_count, uint16_t max_len) -{ +uint16_t tu_desc_get_interface_total_len(tusb_desc_interface_t const* desc_itf, uint8_t itf_count, uint16_t max_len) { uint8_t const* p_desc = (uint8_t const*) desc_itf; uint16_t len = 0; - while (itf_count--) - { + while (itf_count--) { // Next on interface desc len += tu_desc_len(desc_itf); p_desc = tu_desc_next(p_desc); - while (len < max_len) - { + while (len < max_len) { // return on IAD regardless of itf count - if ( tu_desc_type(p_desc) == TUSB_DESC_INTERFACE_ASSOCIATION ) return len; - - if ( (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE) && - ((tusb_desc_interface_t const*) p_desc)->bAlternateSetting == 0 ) - { + if (tu_desc_type(p_desc) == TUSB_DESC_INTERFACE_ASSOCIATION) { + return len; + } + if ((tu_desc_type(p_desc) == TUSB_DESC_INTERFACE) && + ((tusb_desc_interface_t const*) p_desc)->bAlternateSetting == 0) { break; } @@ -243,9 +215,8 @@ uint16_t tu_desc_get_interface_total_len(tusb_desc_interface_t const* desc_itf, //--------------------------------------------------------------------+ bool tu_edpt_stream_init(tu_edpt_stream_t* s, bool is_host, bool is_tx, bool overwritable, - void* ff_buf, uint16_t ff_bufsize, uint8_t* ep_buf, uint16_t ep_bufsize) -{ - osal_mutex_t new_mutex = osal_mutex_create(&s->ff_mutex); + void* ff_buf, uint16_t ff_bufsize, uint8_t* ep_buf, uint16_t ep_bufsize) { + osal_mutex_t new_mutex = osal_mutex_create(&s->ff_mutexdef); (void) new_mutex; (void) is_tx; @@ -259,92 +230,82 @@ bool tu_edpt_stream_init(tu_edpt_stream_t* s, bool is_host, bool is_tx, bool ove return true; } +bool tu_edpt_stream_deinit(tu_edpt_stream_t* s) { + (void) s; + #if OSAL_MUTEX_REQUIRED + if (s->ff.mutex_wr) osal_mutex_delete(s->ff.mutex_wr); + if (s->ff.mutex_rd) osal_mutex_delete(s->ff.mutex_rd); + #endif + return true; +} + TU_ATTR_ALWAYS_INLINE static inline -bool stream_claim(tu_edpt_stream_t* s) -{ - if (s->is_host) - { +bool stream_claim(tu_edpt_stream_t* s) { + if (s->is_host) { #if CFG_TUH_ENABLED return usbh_edpt_claim(s->daddr, s->ep_addr); #endif - }else - { + } else { #if CFG_TUD_ENABLED return usbd_edpt_claim(s->rhport, s->ep_addr); #endif } - return false; } TU_ATTR_ALWAYS_INLINE static inline -bool stream_xfer(tu_edpt_stream_t* s, uint16_t count) -{ - if (s->is_host) - { +bool stream_xfer(tu_edpt_stream_t* s, uint16_t count) { + if (s->is_host) { #if CFG_TUH_ENABLED return usbh_edpt_xfer(s->daddr, s->ep_addr, count ? s->ep_buf : NULL, count); #endif - }else - { + } else { #if CFG_TUD_ENABLED return usbd_edpt_xfer(s->rhport, s->ep_addr, count ? s->ep_buf : NULL, count); #endif } - return false; } TU_ATTR_ALWAYS_INLINE static inline -bool stream_release(tu_edpt_stream_t* s) -{ - if (s->is_host) - { +bool stream_release(tu_edpt_stream_t* s) { + if (s->is_host) { #if CFG_TUH_ENABLED return usbh_edpt_release(s->daddr, s->ep_addr); #endif - }else - { + } else { #if CFG_TUD_ENABLED return usbd_edpt_release(s->rhport, s->ep_addr); #endif } - return false; } //--------------------------------------------------------------------+ // Stream Write //--------------------------------------------------------------------+ - -bool tu_edpt_stream_write_zlp_if_needed(tu_edpt_stream_t* s, uint32_t last_xferred_bytes) -{ +bool tu_edpt_stream_write_zlp_if_needed(tu_edpt_stream_t* s, uint32_t last_xferred_bytes) { // ZLP condition: no pending data, last transferred bytes is multiple of packet size - TU_VERIFY( !tu_fifo_count(&s->ff) && last_xferred_bytes && (0 == (last_xferred_bytes & (s->ep_packetsize-1))) ); - - TU_VERIFY( stream_claim(s) ); - TU_ASSERT( stream_xfer(s, 0) ); - + TU_VERIFY(!tu_fifo_count(&s->ff) && last_xferred_bytes && (0 == (last_xferred_bytes & (s->ep_packetsize - 1)))); + TU_VERIFY(stream_claim(s)); + TU_ASSERT(stream_xfer(s, 0)); return true; } -uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s) -{ +uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s) { // skip if no data - TU_VERIFY( tu_fifo_count(&s->ff), 0 ); + TU_VERIFY(tu_fifo_count(&s->ff), 0); // Claim the endpoint - TU_VERIFY( stream_claim(s), 0 ); + TU_VERIFY(stream_claim(s), 0); // Pull data from FIFO -> EP buf uint16_t const count = tu_fifo_read_n(&s->ff, s->ep_buf, s->ep_bufsize); - if ( count ) - { - TU_ASSERT( stream_xfer(s, count), 0 ); + if (count) { + TU_ASSERT(stream_xfer(s, count), 0); return count; - }else - { + } else { // Release endpoint since we don't make any transfer // Note: data is dropped if terminal is not connected stream_release(s); @@ -352,16 +313,13 @@ uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s) } } -uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const *buffer, uint32_t bufsize) -{ +uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const* buffer, uint32_t bufsize) { TU_VERIFY(bufsize); // TODO support ZLP - uint16_t ret = tu_fifo_write_n(&s->ff, buffer, (uint16_t) bufsize); // flush if fifo has more than packet size or // in rare case: fifo depth is configured too small (which never reach packet size) - if ( (tu_fifo_count(&s->ff) >= s->ep_packetsize) || (tu_fifo_depth(&s->ff) < s->ep_packetsize) ) - { + if ((tu_fifo_count(&s->ff) >= s->ep_packetsize) || (tu_fifo_depth(&s->ff) < s->ep_packetsize)) { tu_edpt_stream_write_xfer(s); } @@ -371,9 +329,7 @@ uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const *buffer, uint32_t //--------------------------------------------------------------------+ // Stream Read //--------------------------------------------------------------------+ - -uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s) -{ +uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s) { uint16_t available = tu_fifo_remaining(&s->ff); // Prepare for incoming data but only allow what we can store in the ring buffer. @@ -388,25 +344,21 @@ uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s) // get available again since fifo can be changed before endpoint is claimed available = tu_fifo_remaining(&s->ff); - if ( available >= s->ep_packetsize ) - { + if (available >= s->ep_packetsize) { // multiple of packet size limit by ep bufsize - uint16_t count = (uint16_t) (available & ~(s->ep_packetsize -1)); + uint16_t count = (uint16_t) (available & ~(s->ep_packetsize - 1)); count = tu_min16(count, s->ep_bufsize); - TU_ASSERT( stream_xfer(s, count), 0 ); - + TU_ASSERT(stream_xfer(s, count), 0); return count; - }else - { + } else { // Release endpoint since we don't make any transfer stream_release(s); return 0; } } -uint32_t tu_edpt_stream_read(tu_edpt_stream_t* s, void* buffer, uint32_t bufsize) -{ +uint32_t tu_edpt_stream_read(tu_edpt_stream_t* s, void* buffer, uint32_t bufsize) { uint32_t num_read = tu_fifo_read_n(&s->ff, buffer, (uint16_t) bufsize); tu_edpt_stream_read_xfer(s); return num_read; @@ -420,42 +372,35 @@ uint32_t tu_edpt_stream_read(tu_edpt_stream_t* s, void* buffer, uint32_t bufsize #include #if CFG_TUSB_DEBUG >= CFG_TUH_LOG_LEVEL || CFG_TUSB_DEBUG >= CFG_TUD_LOG_LEVEL - -char const* const tu_str_speed[] = { "Full", "Low", "High" }; -char const* const tu_str_std_request[] = -{ - "Get Status" , - "Clear Feature" , - "Reserved" , - "Set Feature" , - "Reserved" , - "Set Address" , - "Get Descriptor" , - "Set Descriptor" , - "Get Configuration" , - "Set Configuration" , - "Get Interface" , - "Set Interface" , - "Synch Frame" +char const* const tu_str_speed[] = {"Full", "Low", "High"}; +char const* const tu_str_std_request[] = { + "Get Status", + "Clear Feature", + "Reserved", + "Set Feature", + "Reserved", + "Set Address", + "Get Descriptor", + "Set Descriptor", + "Get Configuration", + "Set Configuration", + "Get Interface", + "Set Interface", + "Synch Frame" }; char const* const tu_str_xfer_result[] = { "OK", "FAILED", "STALLED", "TIMEOUT" }; - #endif -static void dump_str_line(uint8_t const* buf, uint16_t count) -{ +static void dump_str_line(uint8_t const* buf, uint16_t count) { tu_printf(" |"); - // each line is 16 bytes - for(uint16_t i=0; i= 900 && CFG_TUSB_MCU < 1000) // check if Espressif MCU // Dialog #define OPT_MCU_DA1469X 1000 ///< Dialog Semiconductor DA1469x @@ -125,6 +137,7 @@ #define OPT_MCU_KINETIS_KL 1200 ///< NXP KL series #define OPT_MCU_KINETIS_K32L 1201 ///< NXP K32L series #define OPT_MCU_KINETIS_K32 1201 ///< Alias to K32L +#define OPT_MCU_KINETIS_K 1202 ///< NXP K series #define OPT_MCU_MKL25ZXX 1200 ///< Alias to KL (obsolete) #define OPT_MCU_K32L2BXX 1201 ///< Alias to K32 (obsolete) @@ -138,7 +151,6 @@ #define OPT_MCU_RX72N 1402 ///< Renesas RX72N #define OPT_MCU_RAXXX 1403 ///< Renesas RAxxx families - // Mind Motion #define OPT_MCU_MM32F327X 1500 ///< Mind Motion MM32F327 @@ -171,10 +183,12 @@ // WCH #define OPT_MCU_CH32V307 2200 ///< WCH CH32V307 #define OPT_MCU_CH32F20X 2210 ///< WCH CH32F20x +#define OPT_MCU_CH32V20X 2220 ///< WCH CH32V20X // NXP LPC MCX #define OPT_MCU_MCXN9 2300 ///< NXP MCX N9 Series +#define OPT_MCU_MCXA15 2301 ///< NXP MCX A15 Series // Check if configured MCU is one of listed // Apply _TU_CHECK_MCU with || as separator to list of input @@ -353,6 +367,11 @@ #define CFG_TUD_INTERFACE_MAX 16 #endif +// USB 2.0 compliance test mode support +#ifndef CFG_TUD_TEST_MODE + #define CFG_TUD_TEST_MODE 0 +#endif + //------------- Device Class Driver -------------// #ifndef CFG_TUD_BTH #define CFG_TUD_BTH 0 @@ -472,6 +491,23 @@ {0x10C4, 0xEA60}, {0x10C4, 0xEA70} #endif +#ifndef CFG_TUH_CDC_CH34X + // CH34X is not part of CDC class, only to re-use CDC driver API + #define CFG_TUH_CDC_CH34X 0 +#endif + +#ifndef CFG_TUH_CDC_CH34X_VID_PID_LIST + // List of product IDs that can use the CH34X CDC driver + #define CFG_TUH_CDC_CH34X_VID_PID_LIST \ + { 0x1a86, 0x5523 }, /* ch341 chip */ \ + { 0x1a86, 0x7522 }, /* ch340k chip */ \ + { 0x1a86, 0x7523 }, /* ch340 chip */ \ + { 0x1a86, 0xe523 }, /* ch330 chip */ \ + { 0x4348, 0x5523 }, /* ch340 custom chip */ \ + { 0x2184, 0x0057 }, /* overtaken from Linux Kernel driver /drivers/usb/serial/ch341.c */ \ + { 0x9986, 0x7523 } /* overtaken from Linux Kernel driver /drivers/usb/serial/ch341.c */ +#endif + #ifndef CFG_TUH_HID #define CFG_TUH_HID 0 #endif diff --git a/pico-sdk/src/rp2_common/CMakeLists.txt b/pico-sdk/src/rp2_common/CMakeLists.txt index 82f56f8..15b3435 100644 --- a/pico-sdk/src/rp2_common/CMakeLists.txt +++ b/pico-sdk/src/rp2_common/CMakeLists.txt @@ -61,14 +61,7 @@ if (NOT PICO_BARE_METAL) pico_add_subdirectory(pico_stdio_usb) pico_add_subdirectory(pico_i2c_slave) - # networking libraries - note dependency order is important pico_add_subdirectory(pico_async_context) - pico_add_subdirectory(pico_btstack) - pico_add_subdirectory(pico_cyw43_driver) - pico_add_subdirectory(pico_lwip) - pico_add_subdirectory(pico_cyw43_arch) - pico_add_subdirectory(pico_mbedtls) - pico_add_subdirectory(pico_stdlib) pico_add_subdirectory(pico_cxx_options) @@ -85,4 +78,4 @@ set(CMAKE_EXECUTABLE_SUFFIX "${CMAKE_EXECUTABLE_SUFFIX}" PARENT_SCOPE) pico_add_doxygen(${CMAKE_CURRENT_LIST_DIR}) pico_add_doxygen_exclude(${CMAKE_CURRENT_LIST_DIR}/cmsis) -pico_promote_common_scope_vars() \ No newline at end of file +pico_promote_common_scope_vars() diff --git a/pico-sdk/src/rp2_common/pico_btstack/CMakeLists.txt b/pico-sdk/src/rp2_common/pico_btstack/CMakeLists.txt deleted file mode 100644 index 6ead74d..0000000 --- a/pico-sdk/src/rp2_common/pico_btstack/CMakeLists.txt +++ /dev/null @@ -1,357 +0,0 @@ -if (DEFINED ENV{PICO_BTSTACK_PATH} AND (NOT PICO_BTSTACK_PATH)) - set(PICO_BTSTACK_PATH $ENV{PICO_BTSTACK_PATH}) - message("Using PICO_BTSTACK_PATH from environment ('${PICO_BTSTACK_PATH}')") -endif () - -set(BTSTACK_TEST_PATH "src/bluetooth.h") -if (NOT PICO_BTSTACK_PATH) - set(PICO_BTSTACK_PATH ${PROJECT_SOURCE_DIR}/lib/btstack) - if (PICO_CYW43_SUPPORTED AND NOT EXISTS ${PICO_BTSTACK_PATH}/${BTSTACK_TEST_PATH}) - message(WARNING "btstack submodule has not been initialized; Pico W BLE support will be unavailable. - hint: try 'git submodule update --init' from your SDK directory (${PICO_SDK_PATH}).") - endif() -elseif (NOT EXISTS ${PICO_BTSTACK_PATH}/${BTSTACK_TEST_PATH}) - message(WARNING "PICO_BTSTACK_PATH specified but content not present.") -endif() - -if (EXISTS ${PICO_BTSTACK_PATH}/${BTSTACK_TEST_PATH}) - message("BTstack available at ${PICO_BTSTACK_PATH}") - - pico_register_common_scope_var(PICO_BTSTACK_PATH) - - pico_add_library(pico_btstack_base NOFLAG) - target_include_directories(pico_btstack_base_headers INTERFACE - ${PICO_BTSTACK_PATH}/src - ${PICO_BTSTACK_PATH}/platform/embedded - ) - - target_sources(pico_btstack_base INTERFACE - ${PICO_BTSTACK_PATH}/3rd-party/micro-ecc/uECC.c - ${PICO_BTSTACK_PATH}/3rd-party/rijndael/rijndael.c - ${PICO_BTSTACK_PATH}/3rd-party/segger-rtt/SEGGER_RTT.c - ${PICO_BTSTACK_PATH}/3rd-party/segger-rtt/SEGGER_RTT_printf.c - ${PICO_BTSTACK_PATH}/platform/embedded/btstack_tlv_flash_bank.c - ${PICO_BTSTACK_PATH}/platform/embedded/hci_dump_embedded_stdout.c - ${PICO_BTSTACK_PATH}/platform/embedded/hci_dump_segger_rtt_stdout.c - ${PICO_BTSTACK_PATH}/src/ad_parser.c - ${PICO_BTSTACK_PATH}/src/btstack_audio.c - ${PICO_BTSTACK_PATH}/src/btstack_base64_decoder.c - ${PICO_BTSTACK_PATH}/src/btstack_crypto.c - ${PICO_BTSTACK_PATH}/src/btstack_hid_parser.c - ${PICO_BTSTACK_PATH}/src/btstack_linked_list.c - ${PICO_BTSTACK_PATH}/src/btstack_memory.c - ${PICO_BTSTACK_PATH}/src/btstack_memory_pool.c - ${PICO_BTSTACK_PATH}/src/btstack_resample.c - ${PICO_BTSTACK_PATH}/src/btstack_ring_buffer.c - ${PICO_BTSTACK_PATH}/src/btstack_run_loop.c - ${PICO_BTSTACK_PATH}/src/btstack_run_loop_base.c - ${PICO_BTSTACK_PATH}/src/btstack_slip.c - ${PICO_BTSTACK_PATH}/src/btstack_tlv.c - ${PICO_BTSTACK_PATH}/src/btstack_tlv_none.c - ${PICO_BTSTACK_PATH}/src/btstack_util.c - ${PICO_BTSTACK_PATH}/src/hci.c - ${PICO_BTSTACK_PATH}/src/hci_cmd.c - ${PICO_BTSTACK_PATH}/src/hci_dump.c - ${PICO_BTSTACK_PATH}/src/hci_event.c - ${PICO_BTSTACK_PATH}/src/l2cap.c - ${PICO_BTSTACK_PATH}/src/l2cap_signaling.c - ${PICO_BTSTACK_PATH}/src/mesh/gatt-service/mesh_provisioning_service_server.c - ${PICO_BTSTACK_PATH}/src/mesh/gatt-service/mesh_proxy_service_server.c - ${PICO_BTSTACK_PATH}/3rd-party/md5/md5.c - ${PICO_BTSTACK_PATH}/3rd-party/yxml/yxml.c - ${CMAKE_CURRENT_LIST_DIR}/btstack_stdin_pico.c - ) - target_include_directories(pico_btstack_base_headers INTERFACE - ${PICO_BTSTACK_PATH}/ - ${PICO_BTSTACK_PATH}/3rd-party/md5 - ${PICO_BTSTACK_PATH}/3rd-party/yxml - ${PICO_BTSTACK_PATH}/3rd-party/rijndael - ${PICO_BTSTACK_PATH}/3rd-party/micro-ecc - ${PICO_BTSTACK_PATH}/3rd-party/segger-rtt - ) - - pico_add_library(pico_btstack_ble) - target_sources(pico_btstack_ble INTERFACE - ${PICO_BTSTACK_PATH}/src/ble/att_db.c - ${PICO_BTSTACK_PATH}/src/ble/att_db_util.c - ${PICO_BTSTACK_PATH}/src/ble/att_dispatch.c - ${PICO_BTSTACK_PATH}/src/ble/att_server.c - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/battery_service_server.c - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/battery_service_client.c - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/cycling_power_service_server.c - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/cycling_speed_and_cadence_service_server.c - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/device_information_service_server.c - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/device_information_service_client.c - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/heart_rate_service_server.c - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/hids_client.c - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/hids_device.c - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/nordic_spp_service_server.c - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/ublox_spp_service_server.c - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/ancs_client.c - ${PICO_BTSTACK_PATH}/src/ble/gatt_client.c - ${PICO_BTSTACK_PATH}/src/ble/le_device_db_memory.c - ${PICO_BTSTACK_PATH}/src/ble/le_device_db_tlv.c - ${PICO_BTSTACK_PATH}/src/ble/sm.c - ) - pico_mirrored_target_link_libraries(pico_btstack_ble INTERFACE - pico_btstack_base - ) - target_compile_definitions(pico_btstack_ble_headers INTERFACE - ENABLE_BLE=1 - ) - - pico_add_library(pico_btstack_classic) - target_sources(pico_btstack_classic INTERFACE - ${PICO_BTSTACK_PATH}/src/classic/a2dp.c - ${PICO_BTSTACK_PATH}/src/classic/a2dp_sink.c - ${PICO_BTSTACK_PATH}/src/classic/a2dp_source.c - ${PICO_BTSTACK_PATH}/src/classic/avdtp.c - ${PICO_BTSTACK_PATH}/src/classic/avdtp_acceptor.c - ${PICO_BTSTACK_PATH}/src/classic/avdtp_initiator.c - ${PICO_BTSTACK_PATH}/src/classic/avdtp_sink.c - ${PICO_BTSTACK_PATH}/src/classic/avdtp_source.c - ${PICO_BTSTACK_PATH}/src/classic/avdtp_util.c - ${PICO_BTSTACK_PATH}/src/classic/avrcp.c - ${PICO_BTSTACK_PATH}/src/classic/avrcp_browsing.c - ${PICO_BTSTACK_PATH}/src/classic/avrcp_browsing_controller.c - ${PICO_BTSTACK_PATH}/src/classic/avrcp_browsing_target.c - ${PICO_BTSTACK_PATH}/src/classic/avrcp_controller.c - ${PICO_BTSTACK_PATH}/src/classic/avrcp_cover_art_client.c - ${PICO_BTSTACK_PATH}/src/classic/avrcp_media_item_iterator.c - ${PICO_BTSTACK_PATH}/src/classic/avrcp_target.c - ${PICO_BTSTACK_PATH}/src/classic/btstack_cvsd_plc.c - ${PICO_BTSTACK_PATH}/src/classic/btstack_link_key_db_tlv.c - ${PICO_BTSTACK_PATH}/src/classic/btstack_sbc_plc.c - ${PICO_BTSTACK_PATH}/src/classic/device_id_server.c - ${PICO_BTSTACK_PATH}/src/classic/gatt_sdp.c - ${PICO_BTSTACK_PATH}/src/classic/goep_client.c - ${PICO_BTSTACK_PATH}/src/classic/goep_server.c - ${PICO_BTSTACK_PATH}/src/classic/hfp.c - ${PICO_BTSTACK_PATH}/src/classic/hfp_ag.c - ${PICO_BTSTACK_PATH}/src/classic/hfp_gsm_model.c - ${PICO_BTSTACK_PATH}/src/classic/hfp_hf.c - ${PICO_BTSTACK_PATH}/src/classic/hfp_msbc.c - ${PICO_BTSTACK_PATH}/src/classic/hid_device.c - ${PICO_BTSTACK_PATH}/src/classic/hid_host.c - ${PICO_BTSTACK_PATH}/src/classic/hsp_ag.c - ${PICO_BTSTACK_PATH}/src/classic/hsp_hs.c - ${PICO_BTSTACK_PATH}/src/classic/obex_iterator.c - ${PICO_BTSTACK_PATH}/src/classic/obex_message_builder.c - ${PICO_BTSTACK_PATH}/src/classic/obex_parser.c - ${PICO_BTSTACK_PATH}/src/classic/pan.c - ${PICO_BTSTACK_PATH}/src/classic/pbap_client.c - ${PICO_BTSTACK_PATH}/src/classic/rfcomm.c - ${PICO_BTSTACK_PATH}/src/classic/sdp_client.c - ${PICO_BTSTACK_PATH}/src/classic/sdp_client_rfcomm.c - ${PICO_BTSTACK_PATH}/src/classic/sdp_server.c - ${PICO_BTSTACK_PATH}/src/classic/sdp_util.c - ${PICO_BTSTACK_PATH}/src/classic/spp_server.c - ) - pico_mirrored_target_link_libraries(pico_btstack_classic INTERFACE - pico_btstack_base - ) - target_compile_definitions(pico_btstack_classic_headers INTERFACE - ENABLE_CLASSIC=1 - ) - - pico_add_library(pico_btstack_flash_bank) - target_sources(pico_btstack_flash_bank INTERFACE - ${CMAKE_CURRENT_LIST_DIR}/btstack_flash_bank.c - ) - target_include_directories(pico_btstack_flash_bank_headers INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) - pico_mirrored_target_link_libraries(pico_btstack_flash_bank INTERFACE pico_btstack_base pico_flash) - - pico_add_library(pico_btstack_run_loop_async_context NOFLAG) - target_sources(pico_btstack_run_loop_async_context INTERFACE - ${CMAKE_CURRENT_LIST_DIR}/btstack_run_loop_async_context.c - ) - target_include_directories(pico_btstack_run_loop_async_context_headers INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) - pico_mirrored_target_link_libraries(pico_btstack_run_loop_async_context INTERFACE pico_btstack_base pico_async_context_base) - - pico_add_library(pico_btstack_sbc_encoder NOFLAG) - target_sources(pico_btstack_sbc_encoder INTERFACE - # SBC codec for A2DP and HFP demos - ${PICO_BTSTACK_PATH}/src/classic/btstack_sbc_encoder_bluedroid.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/encoder/srce/sbc_analysis.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/encoder/srce/sbc_dct.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/encoder/srce/sbc_dct_coeffs.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/encoder/srce/sbc_enc_bit_alloc_mono.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/encoder/srce/sbc_enc_bit_alloc_ste.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/encoder/srce/sbc_enc_coeffs.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/encoder/srce/sbc_encoder.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/encoder/srce/sbc_packing.c - ) - target_include_directories(pico_btstack_sbc_encoder_headers INTERFACE - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/encoder/include - ) - - pico_add_library(pico_btstack_sbc_decoder NOFLAG) - target_sources(pico_btstack_sbc_decoder INTERFACE - ${PICO_BTSTACK_PATH}/src/classic/btstack_sbc_decoder_bluedroid.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/decoder/srce/alloc.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/decoder/srce/bitalloc.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/decoder/srce/bitalloc-sbc.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/decoder/srce/bitstream-decode.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/decoder/srce/decoder-oina.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/decoder/srce/decoder-private.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/decoder/srce/decoder-sbc.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/decoder/srce/dequant.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/decoder/srce/framing.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/decoder/srce/framing-sbc.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/decoder/srce/oi_codec_version.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/decoder/srce/synthesis-sbc.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/decoder/srce/synthesis-dct8.c - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/decoder/srce/synthesis-8-generated.c - ) - target_include_directories(pico_btstack_sbc_decoder_headers INTERFACE - ${PICO_BTSTACK_PATH}/3rd-party/bluedroid/decoder/include - ) - - pico_add_library(pico_btstack_bnep_lwip NOFLAG) - target_sources(pico_btstack_bnep_lwip INTERFACE - ${PICO_BTSTACK_PATH}/src/classic/bnep.c - ${PICO_BTSTACK_PATH}/platform/lwip/bnep_lwip.c - ) - target_include_directories(pico_btstack_bnep_lwip_headers INTERFACE - ${PICO_BTSTACK_PATH}/platform/lwip - ) - - pico_add_library(pico_btstack_bnep_lwip_sys_freertos NOFLAG) - target_include_directories(pico_btstack_bnep_lwip_sys_freertos INTERFACE - ${PICO_BTSTACK_PATH}/platform/freertos - ) - pico_mirrored_target_link_libraries(pico_btstack_bnep_lwip_sys_freertos INTERFACE - pico_btstack_bnep_lwip - ) - target_compile_definitions(pico_btstack_bnep_lwip_sys_freertos INTERFACE - LWIP_PROVIDE_ERRNO=1 - PICO_LWIP_CUSTOM_LOCK_TCPIP_CORE=1 - ) - - pico_promote_common_scope_vars() - - # Make a GATT header file from a BTstack GATT file - # Pass the target library name library type and path to the GATT input file - function(pico_btstack_make_gatt_header TARGET_LIB TARGET_TYPE GATT_FILE) - find_package (Python3 REQUIRED COMPONENTS Interpreter) - get_filename_component(GATT_NAME "${GATT_FILE}" NAME_WE) - get_filename_component(GATT_PATH "${GATT_FILE}" PATH) - set(GATT_BINARY_DIR "${CMAKE_CURRENT_BINARY_DIR}/generated") - set(GATT_HEADER "${GATT_BINARY_DIR}/${GATT_NAME}.h") - set(TARGET_GATT "${TARGET_LIB}_gatt_header") - - add_custom_target(${TARGET_GATT} DEPENDS ${GATT_HEADER}) - add_custom_command( - OUTPUT ${GATT_HEADER} - DEPENDS ${GATT_FILE} - WORKING_DIRECTORY ${GATT_PATH} - COMMAND ${CMAKE_COMMAND} -E make_directory ${GATT_BINARY_DIR} && - ${Python3_EXECUTABLE} ${PICO_SDK_PATH}/lib/btstack/tool/compile_gatt.py ${GATT_FILE} ${GATT_HEADER} - VERBATIM) - add_dependencies(${TARGET_LIB} - ${TARGET_GATT} - ) - target_include_directories(${TARGET_LIB} ${TARGET_TYPE} - ${GATT_BINARY_DIR} - ) - endfunction() - - function(suppress_btstack_warnings) - set_source_files_properties( - ${PICO_BTSTACK_PATH}/src/ble/att_server.c - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/device_information_service_server.c - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/hids_client.c - ${PICO_BTSTACK_PATH}/src/btstack_util.c - ${PICO_BTSTACK_PATH}/src/btstack_crypto.c - ${PICO_BTSTACK_PATH}/src/classic/a2dp.c - ${PICO_BTSTACK_PATH}/src/classic/a2dp_sink.c - ${PICO_BTSTACK_PATH}/src/classic/a2dp_source.c - ${PICO_BTSTACK_PATH}/src/classic/avdtp.c - ${PICO_BTSTACK_PATH}/src/classic/avdtp_source.c - ${PICO_BTSTACK_PATH}/src/classic/avrcp.c - ${PICO_BTSTACK_PATH}/src/classic/avrcp_controller.c - ${PICO_BTSTACK_PATH}/src/classic/btstack_sbc_decoder_bluedroid.c - ${PICO_BTSTACK_PATH}/src/classic/avrcp_target.c - ${PICO_BTSTACK_PATH}/src/classic/hid_device.c - ${PICO_BTSTACK_PATH}/src/classic/hsp_ag.c - ${PICO_BTSTACK_PATH}/src/classic/hsp_hs.c - ${PICO_BTSTACK_PATH}/src/classic/pan.c - ${PICO_BTSTACK_PATH}/src/classic/pbap_client.c - ${PICO_BTSTACK_PATH}/src/classic/rfcomm.c - ${PICO_BTSTACK_PATH}/src/classic/sdp_client_rfcomm.c - ${PICO_BTSTACK_PATH}/src/classic/sdp_server.c - ${PICO_BTSTACK_PATH}/src/classic/spp_server.c - PROPERTIES - COMPILE_OPTIONS "-Wno-cast-qual" - ) - set_source_files_properties( - ${PICO_BTSTACK_PATH}/src/ble/sm.c - ${PICO_BTSTACK_PATH}/src/l2cap.c - PROPERTIES - COMPILE_OPTIONS "-Wno-cast-qual;-Wno-unused-parameter" - ) - set_source_files_properties( - ${PICO_BTSTACK_PATH}/src/btstack_hid_parser.c - PROPERTIES - COMPILE_OPTIONS "-Wno-maybe-uninitialized" - ) - set_source_files_properties( - ${PICO_BTSTACK_PATH}/src/btstack_tlv_none.c - ${PICO_BTSTACK_PATH}/src/classic/avdtp_util.c - PROPERTIES - COMPILE_OPTIONS "-Wno-unused-parameter" - ) - set_source_files_properties( - ${PICO_BTSTACK_PATH}/platform/embedded/hci_dump_embedded_stdout.c - PROPERTIES - COMPILE_OPTIONS "-Wno-suggest-attribute=format" - ) - set_source_files_properties( - ${PICO_BTSTACK_PATH}/src/hci.c - ${PICO_BTSTACK_PATH}/src/classic/rfcomm.c - PROPERTIES - COMPILE_OPTIONS "-Wno-cast-qual;-Wno-format" - ) - set_source_files_properties( - ${PICO_BTSTACK_PATH}/platform/embedded/hal_flash_bank_memory.c - PROPERTIES - COMPILE_OPTIONS "-Wno-sign-compare;-Wno-format" - ) - set_source_files_properties( - ${PICO_BTSTACK_PATH}/platform/embedded/btstack_tlv_flash_bank.c - PROPERTIES - COMPILE_OPTIONS "-Wno-unused-parameter;-Wno-sign-compare" - ) - set_source_files_properties( - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/hids_client.c - PROPERTIES - COMPILE_OPTIONS "-Wno-cast-qual;-Wno-null-dereference" - ) - set_source_files_properties( - ${PICO_BTSTACK_PATH}/src/classic/hfp.c - PROPERTIES - COMPILE_OPTIONS "-Wno-cast-qual;-Wno-null-dereference;-Wno-unused-parameter" - ) - set_source_files_properties( - ${PICO_BTSTACK_PATH}/src/classic/goep_server.c - PROPERTIES - COMPILE_OPTIONS "-Wno-unused-parameter;-Wno-null-dereference" - ) - set_source_files_properties( - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/battery_service_client.c - ${PICO_BTSTACK_PATH}/src/ble/gatt-service/device_information_service_client.c - PROPERTIES - COMPILE_OPTIONS "-Wno-null-dereference" - ) - set_source_files_properties( - ${PICO_BTSTACK_PATH}/src/classic/hfp_hf.c - PROPERTIES - COMPILE_OPTIONS "-Wno-type-limits;-Wno-stringop-overflow" - ) - set_source_files_properties( - ${PICO_BTSTACK_PATH}/src/btstack_crypto.c - PROPERTIES - COMPILE_OPTIONS "-Wno-cast-qual;-Wno-sign-compare" - ) - endfunction() -endif() diff --git a/pico-sdk/src/rp2_common/pico_btstack/LICENSE.RP b/pico-sdk/src/rp2_common/pico_btstack/LICENSE.RP deleted file mode 100644 index 79e0080..0000000 --- a/pico-sdk/src/rp2_common/pico_btstack/LICENSE.RP +++ /dev/null @@ -1,30 +0,0 @@ -“BlueKitchen” shall refer to BlueKitchen GmbH. -“Raspberry Pi” shall refer to Raspberry Pi Ltd. -“Product” shall refer to Raspberry Pi hardware products Raspberry Pi Pico W or Raspberry Pi Pico WH. -“Customer” means any purchaser of a Product. -“Customer Products” means products manufactured or distributed by Customers which use or are derived from Products. - -Raspberry Pi grants to the Customer a non-exclusive, non-transferable, non-sublicensable, irrevocable, perpetual -and worldwide licence to use, copy, store, develop, modify, and transmit BTstack in order to use BTstack with or -integrate BTstack into Products or Customer Products, and distribute BTstack as part of these Products or -Customer Products or their related documentation or SDKs. - -All use of BTstack by the Customer is limited to Products or Customer Products, and the Customer represents and -warrants that all such use shall be in compliance with the terms of this licence and all applicable laws and -regulations, including but not limited to, copyright and other intellectual property laws and privacy regulations. - -BlueKitchen retains all rights, title and interest in, to and associated with BTstack and associated websites. -Customer shall not take any action inconsistent with BlueKitchen’s ownership of BTstack, any associated services, -websites and related content. - -There are no implied licences under the terms set forth in this licence, and any rights not expressly granted -hereunder are reserved by BlueKitchen. - -BTSTACK IS PROVIDED BY RASPBERRY PI "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, -THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED TO THE FULLEST EXTENT -PERMISSIBLE UNDER APPLICABLE LAW. IN NO EVENT SHALL RASPBERRY PI OR BLUEKITCHEN BE LIABLE FOR ANY DIRECT, INDIRECT, -INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE -GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY -OUT OF THE USE OF BTSTACK, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - diff --git a/pico-sdk/src/rp2_common/pico_btstack/btstack_flash_bank.c b/pico-sdk/src/rp2_common/pico_btstack/btstack_flash_bank.c deleted file mode 100644 index 9ed9c02..0000000 --- a/pico-sdk/src/rp2_common/pico_btstack/btstack_flash_bank.c +++ /dev/null @@ -1,177 +0,0 @@ -/* - * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include "pico/btstack_flash_bank.h" -#include "pico/flash.h" -#include "hardware/sync.h" -#include - -// Check sizes -static_assert(PICO_FLASH_BANK_TOTAL_SIZE % (FLASH_SECTOR_SIZE * 2) == 0, "PICO_FLASH_BANK_TOTAL_SIZE invalid"); -static_assert(PICO_FLASH_BANK_TOTAL_SIZE <= PICO_FLASH_SIZE_BYTES, "PICO_FLASH_BANK_TOTAL_SIZE too big"); - -// Size of one bank -#define PICO_FLASH_BANK_SIZE (PICO_FLASH_BANK_TOTAL_SIZE / 2) - -#if 0 -#define DEBUG_PRINT(format,args...) printf(format, ## args) -#else -#define DEBUG_PRINT(...) -#endif - -static uint32_t pico_flash_bank_get_size(void * context) { - (void)(context); - return PICO_FLASH_BANK_SIZE; -} - -static uint32_t pico_flash_bank_get_alignment(void * context) { - (void)(context); - return 1; -} - -typedef struct { - bool op_is_erase; - uintptr_t p0; - uintptr_t p1; -} mutation_operation_t; - -static void pico_flash_bank_perform_flash_mutation_operation(void *param) { - const mutation_operation_t *mop = (const mutation_operation_t *)param; - if (mop->op_is_erase) { - flash_range_erase(mop->p0, PICO_FLASH_BANK_SIZE); - } else { - flash_range_program(mop->p0, (const uint8_t *)mop->p1, FLASH_PAGE_SIZE); - } -} - -#ifndef pico_flash_bank_get_storage_offset_func -static inline uint32_t pico_flash_bank_get_fixed_storage_offset(void) { - static_assert(PICO_FLASH_BANK_STORAGE_OFFSET + PICO_FLASH_BANK_TOTAL_SIZE <= PICO_FLASH_SIZE_BYTES, "PICO_FLASH_BANK_TOTAL_SIZE too big"); -#ifndef NDEBUG - // Check we're not overlapping the binary in flash - extern char __flash_binary_end; - assert(((uintptr_t)&__flash_binary_end - XIP_BASE <= PICO_FLASH_BANK_STORAGE_OFFSET)); -#endif - return PICO_FLASH_BANK_STORAGE_OFFSET; -} -#define pico_flash_bank_get_storage_offset_func pico_flash_bank_get_fixed_storage_offset -#else -extern uint32_t pico_flash_bank_get_storage_offset_func(void); -#endif - -static void pico_flash_bank_erase(void * context, int bank) { - (void)(context); - DEBUG_PRINT("erase: bank %d\n", bank); - mutation_operation_t mop = { - .op_is_erase = true, - .p0 = pico_flash_bank_get_storage_offset_func() + (PICO_FLASH_BANK_SIZE * bank), - }; - // todo choice of timeout and check return code... currently we have no way to return an error - // to the caller anyway. flash_safe_execute asserts by default on problem other than timeout, - // so that's fine for now, and UINT32_MAX is a timeout of 49 days which seems long enough - flash_safe_execute(pico_flash_bank_perform_flash_mutation_operation, &mop, UINT32_MAX); -} - -static void pico_flash_bank_read(void *context, int bank, uint32_t offset, uint8_t *buffer, uint32_t size) { - (void)(context); - DEBUG_PRINT("read: bank %d offset %u size %u\n", bank, offset, size); - - assert(bank <= 1); - if (bank > 1) return; - - assert(offset < PICO_FLASH_BANK_SIZE); - if (offset >= PICO_FLASH_BANK_SIZE) return; - - assert((offset + size) <= PICO_FLASH_BANK_SIZE); - if ((offset + size) > PICO_FLASH_BANK_SIZE) return; - - // Flash is xip - memcpy(buffer, (void *)(XIP_BASE + pico_flash_bank_get_storage_offset_func() + (PICO_FLASH_BANK_SIZE * bank) + offset), size); -} - -static void pico_flash_bank_write(void * context, int bank, uint32_t offset, const uint8_t *data, uint32_t size) { - (void)(context); - DEBUG_PRINT("write: bank %d offset %u size %u\n", bank, offset, size); - - assert(bank <= 1); - if (bank > 1) return; - - assert(offset < PICO_FLASH_BANK_SIZE); - if (offset >= PICO_FLASH_BANK_SIZE) return; - - assert((offset + size) <= PICO_FLASH_BANK_SIZE); - if ((offset + size) > PICO_FLASH_BANK_SIZE) return; - - if (size == 0) return; - - // calc bank start position - const uint32_t bank_start_pos = pico_flash_bank_get_storage_offset_func() + (PICO_FLASH_BANK_SIZE * bank); - - // Calculate first and last page in the bank - const uint32_t first_page = offset / FLASH_PAGE_SIZE; - const uint32_t last_page = (offset + size + FLASH_PAGE_SIZE - 1) / FLASH_PAGE_SIZE; - - // Now we only care about the offset in the first page - offset %= FLASH_PAGE_SIZE; - - // Amount of data we've copied - uint32_t data_pos = 0; - uint32_t size_left = size; - - // Write all the pages required - for(uint32_t page = first_page; page < last_page; page++) { - uint8_t page_data[FLASH_PAGE_SIZE]; - - assert(data_pos < size && size_left <= size); - - // Copy data we're not going to overwrite in the first page - if (page == first_page && offset > 0) { - memcpy(page_data, - (void *)(XIP_BASE + bank_start_pos + (page * FLASH_PAGE_SIZE)), - offset); - } - - // Copy the data we're not going to overwrite in the last page - if (page == last_page - 1 && (offset + size_left) < FLASH_PAGE_SIZE) { - memcpy(page_data + offset + size_left, - (void *)(XIP_BASE + bank_start_pos + (page * FLASH_PAGE_SIZE) + offset + size_left), - FLASH_PAGE_SIZE - offset - size_left); - } - - // Now copy the new data into the page - const uint32_t size_to_copy = MIN(size_left, FLASH_PAGE_SIZE - offset); - memcpy(page_data + offset, data + data_pos, size_to_copy); - - data_pos += size_to_copy; - size_left -= size_to_copy; - - // zero offset for the following pages - offset = 0; - - // Now program the entire page - mutation_operation_t mop = { - .op_is_erase = false, - .p0 = bank_start_pos + (page * FLASH_PAGE_SIZE), - .p1 = (uintptr_t)page_data - }; - // todo choice of timeout and check return code... currently we have no way to return an error - // to the caller anyway. flash_safe_execute asserts by default on problem other than timeout, - // so that's fine for now, and UINT32_MAX is a timeout of 49 days which seems long enough - flash_safe_execute(pico_flash_bank_perform_flash_mutation_operation, &mop, UINT32_MAX); - } -} - -static const hal_flash_bank_t pico_flash_bank_instance_obj = { - /* uint32_t (*get_size)(..) */ &pico_flash_bank_get_size, - /* uint32_t (*get_alignment)(..); */ &pico_flash_bank_get_alignment, - /* void (*erase)(..); */ &pico_flash_bank_erase, - /* void (*read)(..); */ &pico_flash_bank_read, - /* void (*write)(..); */ &pico_flash_bank_write, -}; - -const hal_flash_bank_t *pico_flash_bank_instance(void) { - return &pico_flash_bank_instance_obj; -} diff --git a/pico-sdk/src/rp2_common/pico_btstack/btstack_run_loop_async_context.c b/pico-sdk/src/rp2_common/pico_btstack/btstack_run_loop_async_context.c deleted file mode 100644 index 9847be9..0000000 --- a/pico-sdk/src/rp2_common/pico_btstack/btstack_run_loop_async_context.c +++ /dev/null @@ -1,155 +0,0 @@ -/* - * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include "pico/btstack_run_loop_async_context.h" -#include "hardware/sync.h" - -static async_context_t *btstack_async_context; -static async_at_time_worker_t btstack_timeout_worker; -static async_when_pending_worker_t btstack_processing_worker; -static void btstack_timeout_reached(async_context_t *context, async_at_time_worker_t *worker); -static void btstack_work_pending(async_context_t *context, async_when_pending_worker_t *worker); -static volatile bool run_loop_exit; - -static void btstack_run_loop_async_context_init(void) { - btstack_run_loop_base_init(); - btstack_timeout_worker.do_work = btstack_timeout_reached; - btstack_processing_worker.do_work = btstack_work_pending; - async_context_add_when_pending_worker(btstack_async_context, &btstack_processing_worker); -} - -static void btstack_run_loop_async_context_add_data_source(btstack_data_source_t * data_source) { - async_context_acquire_lock_blocking(btstack_async_context); - btstack_run_loop_base_add_data_source(data_source); - async_context_release_lock(btstack_async_context); -} - -static bool btstack_run_loop_async_context_remove_data_source(btstack_data_source_t * data_source) { - async_context_acquire_lock_blocking(btstack_async_context); - bool rc = btstack_run_loop_base_remove_data_source(data_source); - async_context_release_lock(btstack_async_context); - return rc; -} - -static void btstack_run_loop_async_context_enable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks) { - async_context_acquire_lock_blocking(btstack_async_context); - btstack_run_loop_base_enable_data_source_callbacks(data_source, callbacks); - async_context_release_lock(btstack_async_context); -} - -static void btstack_run_loop_async_context_disable_data_source_callbacks(btstack_data_source_t * data_source, uint16_t callbacks) { - async_context_acquire_lock_blocking(btstack_async_context); - btstack_run_loop_base_disable_data_source_callbacks(data_source, callbacks); - async_context_release_lock(btstack_async_context); -} - -static void btstack_run_loop_async_context_set_timer(btstack_timer_source_t *ts, uint32_t timeout_in_ms){ - async_context_acquire_lock_blocking(btstack_async_context); - ts->timeout = to_ms_since_boot(get_absolute_time()) + timeout_in_ms + 1; - async_context_set_work_pending(btstack_async_context, &btstack_processing_worker); - async_context_release_lock(btstack_async_context); -} - -static void btstack_run_loop_async_context_add_timer(btstack_timer_source_t *timer) { - async_context_acquire_lock_blocking(btstack_async_context); - btstack_run_loop_base_add_timer(timer); - async_context_set_work_pending(btstack_async_context, &btstack_processing_worker); - async_context_release_lock(btstack_async_context); -} - -static bool btstack_run_loop_async_context_remove_timer(btstack_timer_source_t *timer) { - async_context_acquire_lock_blocking(btstack_async_context); - bool rc = btstack_run_loop_base_remove_timer(timer); - async_context_release_lock(btstack_async_context); - return rc; -} - -static void btstack_run_loop_async_context_dump_timer(void){ - async_context_acquire_lock_blocking(btstack_async_context); - btstack_run_loop_base_dump_timer(); - async_context_release_lock(btstack_async_context); -} - -static uint32_t btstack_run_loop_async_context_get_time_ms(void) -{ - return to_ms_since_boot(get_absolute_time()); -} - -static void btstack_run_loop_async_context_execute(void) -{ - run_loop_exit = false; - while (!run_loop_exit) { - async_context_poll(btstack_async_context); - async_context_wait_for_work_until(btstack_async_context, at_the_end_of_time); - } -} - -static void btstack_run_loop_async_context_trigger_exit(void) -{ - run_loop_exit = true; -} - -static void btstack_run_loop_async_context_execute_on_main_thread(btstack_context_callback_registration_t *callback_registration) -{ - async_context_acquire_lock_blocking(btstack_async_context); - btstack_run_loop_base_add_callback(callback_registration); - async_context_set_work_pending(btstack_async_context, &btstack_processing_worker); - async_context_release_lock(btstack_async_context); -} - -static void btstack_run_loop_async_context_poll_data_sources_from_irq(void) -{ - async_context_set_work_pending(btstack_async_context, &btstack_processing_worker); -} - -static const btstack_run_loop_t btstack_run_loop_async_context = { - &btstack_run_loop_async_context_init, - &btstack_run_loop_async_context_add_data_source, - &btstack_run_loop_async_context_remove_data_source, - &btstack_run_loop_async_context_enable_data_source_callbacks, - &btstack_run_loop_async_context_disable_data_source_callbacks, - &btstack_run_loop_async_context_set_timer, - &btstack_run_loop_async_context_add_timer, - &btstack_run_loop_async_context_remove_timer, - &btstack_run_loop_async_context_execute, - &btstack_run_loop_async_context_dump_timer, - &btstack_run_loop_async_context_get_time_ms, - &btstack_run_loop_async_context_poll_data_sources_from_irq, - &btstack_run_loop_async_context_execute_on_main_thread, - &btstack_run_loop_async_context_trigger_exit, -}; - -const btstack_run_loop_t *btstack_run_loop_async_context_get_instance(async_context_t *async_context) -{ - assert(!btstack_async_context || btstack_async_context == async_context); - btstack_async_context = async_context; - return &btstack_run_loop_async_context; -} - -static void btstack_timeout_reached(__unused async_context_t *context, __unused async_at_time_worker_t *worker) { - // simply wakeup worker - async_context_set_work_pending(btstack_async_context, &btstack_processing_worker); -} - -static void btstack_work_pending(__unused async_context_t *context, __unused async_when_pending_worker_t *worker) { - // poll data sources - btstack_run_loop_base_poll_data_sources(); - - // execute callbacks - btstack_run_loop_base_execute_callbacks(); - - uint32_t now = to_ms_since_boot(get_absolute_time()); - - // process timers - btstack_run_loop_base_process_timers(now); - now = to_ms_since_boot(get_absolute_time()); - int ms = btstack_run_loop_base_get_time_until_timeout(now); - if (ms == -1) { - async_context_remove_at_time_worker(btstack_async_context, &btstack_timeout_worker); - } else { - async_context_add_at_time_worker_in_ms(btstack_async_context, &btstack_timeout_worker, ms); - } -} diff --git a/pico-sdk/src/rp2_common/pico_btstack/btstack_stdin_pico.c b/pico-sdk/src/rp2_common/pico_btstack/btstack_stdin_pico.c deleted file mode 100644 index 1afa4cc..0000000 --- a/pico-sdk/src/rp2_common/pico_btstack/btstack_stdin_pico.c +++ /dev/null @@ -1,60 +0,0 @@ -/* - * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include "btstack_config.h" - -#ifdef HAVE_BTSTACK_STDIN - -#include "btstack_stdin.h" -#include "btstack_run_loop.h" -#include "pico/stdio.h" - -static btstack_data_source_t stdin_data_source; -static void (*stdin_handler)(char c); - -// Data source callback, return any character received -static void btstack_stdin_process(__unused struct btstack_data_source *ds, __unused btstack_data_source_callback_type_t callback_type){ - if (stdin_handler) { - while(true) { - int c = getchar_timeout_us(0); - if (c == PICO_ERROR_TIMEOUT) return; - (*stdin_handler)(c); - } - } -} - -void on_chars_available_callback(__unused void *param) { - btstack_run_loop_poll_data_sources_from_irq(); -} - -// Test code calls this if HAVE_BTSTACK_STDIN is defined and it wants key presses -void btstack_stdin_setup(void (*handler)(char c)) { - if (stdin_handler) { - return; - } - - // set handler - stdin_handler = handler; - - // set up polling data_source - btstack_run_loop_set_data_source_handler(&stdin_data_source, &btstack_stdin_process); - btstack_run_loop_enable_data_source_callbacks(&stdin_data_source, DATA_SOURCE_CALLBACK_POLL); - btstack_run_loop_add_data_source(&stdin_data_source); - - stdio_set_chars_available_callback(on_chars_available_callback, NULL); -} - -// Deinit everything -void btstack_stdin_reset(void){ - if (!stdin_handler) { - return; - } - stdio_set_chars_available_callback(NULL, NULL); - stdin_handler = NULL; - btstack_run_loop_remove_data_source(&stdin_data_source); -} - -#endif \ No newline at end of file diff --git a/pico-sdk/src/rp2_common/pico_btstack/doc.h b/pico-sdk/src/rp2_common/pico_btstack/doc.h deleted file mode 100644 index 0adf219..0000000 --- a/pico-sdk/src/rp2_common/pico_btstack/doc.h +++ /dev/null @@ -1,27 +0,0 @@ -/** - * \defgroup pico_btstack pico_btstack - * \brief Integration/wrapper libraries for BTstack - * the documentation for which is here. - * - * A supplemental license for BTstack (in addition to the stock BTstack licensing terms) is provided here. - * - * The \c \b pico_btstack_ble library adds the support needed for Bluetooth Low Energy (BLE). The \c \b pico_btstack_classic library adds the support needed for Bluetooth Classic. - * You can link to either library individually, or to both libraries thus enabling dual-mode support provided by BTstack. - * - * To use BTstack you need to provide a \c btstack_config.h file in your source tree and add its location to your include path. - * The BTstack configuration macros \c ENABLE_CLASSIC and \c ENABLE_BLE are defined for you when you link the \c pico_btstack_classic and \c pico_btstack_ble libraries respectively, so you should not define them yourself. - * - * For more details, see How to configure BTstack and the relevant pico-examples. - * - * The follow libraries are provided for you to link. - * * \c \b pico_btstack_ble - Adds Bluetooth Low Energy (LE) support. - * * \c \b pico_btstack_classic - Adds Bluetooth Classic support. - * * \c \b pico_btstack_sbc_encoder - Adds Bluetooth Sub Band Coding (SBC) encoder support. - * * \c \b pico_btstack_sbc_decoder - Adds Bluetooth Sub Band Coding (SBC) decoder support. - * * \c \b pico_btstack_bnep_lwip - Adds Bluetooth Network Encapsulation Protocol (BNEP) support using LwIP. - * * \c \b pico_btstack_bnep_lwip_sys_freertos - Adds Bluetooth Network Encapsulation Protocol (BNEP) support using LwIP with FreeRTOS for NO_SYS=0. - * - * \note The CMake function pico_btstack_make_gatt_header can be used to run the BTstack compile_gatt tool to make a GATT header file from a BTstack GATT file. - * - * \sa pico_btstack_cyw43 in pico_cyw43_driver, which adds the cyw43 driver support needed for BTstack including BTstack run loop support. - */ diff --git a/pico-sdk/src/rp2_common/pico_btstack/include/pico/btstack_flash_bank.h b/pico-sdk/src/rp2_common/pico_btstack/include/pico/btstack_flash_bank.h deleted file mode 100644 index d275221..0000000 --- a/pico-sdk/src/rp2_common/pico_btstack/include/pico/btstack_flash_bank.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef _PICO_BTSTACK_FLASH_BANK_H -#define _PICO_BTSTACK_FLASH_BANK_H - -#include "pico.h" -#include "hal_flash_bank.h" - -#ifdef __cplusplus -extern "C" { -#endif - -// PICO_CONFIG: PICO_FLASH_BANK_TOTAL_SIZE, Total size of the Bluetooth flash storage. Must be an even multiple of FLASH_SECTOR_SIZE, type=int, default=FLASH_SECTOR_SIZE * 2, group=pico_btstack -#ifndef PICO_FLASH_BANK_TOTAL_SIZE -#define PICO_FLASH_BANK_TOTAL_SIZE (FLASH_SECTOR_SIZE * 2u) -#endif - -// PICO_CONFIG: PICO_FLASH_BANK_STORAGE_OFFSET, Offset in flash of the Bluetooth flash storage, type=int, default=PICO_FLASH_SIZE_BYTES - PICO_FLASH_BANK_TOTAL_SIZE, group=pico_btstack -#ifndef PICO_FLASH_BANK_STORAGE_OFFSET -#define PICO_FLASH_BANK_STORAGE_OFFSET (PICO_FLASH_SIZE_BYTES - PICO_FLASH_BANK_TOTAL_SIZE) -#endif - -/** - * \brief Return the singleton BTstack HAL flash instance, used for non-volatile storage - * \ingroup pico_btstack - * - * \note By default two sectors at the end of flash are used (see \c PICO_FLASH_BANK_STORAGE_OFFSET and \c PICO_FLASH_BANK_TOTAL_SIZE) - */ -const hal_flash_bank_t *pico_flash_bank_instance(void); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/pico-sdk/src/rp2_common/pico_btstack/include/pico/btstack_run_loop_async_context.h b/pico-sdk/src/rp2_common/pico_btstack/include/pico/btstack_run_loop_async_context.h deleted file mode 100644 index d1b09b7..0000000 --- a/pico-sdk/src/rp2_common/pico_btstack/include/pico/btstack_run_loop_async_context.h +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef _PICO_BTSTACK_RUN_LOOP_ASYNC_CONTEXT_H -#define _PICO_BTSTACK_RUN_LOOP_ASYNC_CONTEXT_H - -#include "btstack_run_loop.h" -#include "pico/async_context.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** - * \brief Initialize and return the singleton BTstack run loop instance that integrates with the async_context API - * \ingroup pico_btstack - * - * \param context the async_context instance that provides the abstraction for handling asynchronous work. - * \return the BTstack run loop instance - */ -const btstack_run_loop_t *btstack_run_loop_async_context_get_instance(async_context_t *context); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/pico-sdk/src/rp2_common/pico_cyw43_arch/CMakeLists.txt b/pico-sdk/src/rp2_common/pico_cyw43_arch/CMakeLists.txt deleted file mode 100644 index 9ba7f59..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_arch/CMakeLists.txt +++ /dev/null @@ -1,89 +0,0 @@ -if (PICO_CYW43_SUPPORTED) # set by BOARD=pico-w - if (TARGET cyw43_driver_picow) - pico_add_library(pico_cyw43_arch) - target_sources(pico_cyw43_arch INTERFACE - ${CMAKE_CURRENT_LIST_DIR}/cyw43_arch.c - ${CMAKE_CURRENT_LIST_DIR}/cyw43_arch_poll.c - ${CMAKE_CURRENT_LIST_DIR}/cyw43_arch_threadsafe_background.c - ${CMAKE_CURRENT_LIST_DIR}/cyw43_arch_freertos.c - ) - - target_include_directories(pico_cyw43_arch_headers INTERFACE - ${CMAKE_CURRENT_LIST_DIR}/include) - - pico_mirrored_target_link_libraries(pico_cyw43_arch INTERFACE - pico_unique_id - cyw43_driver_picow # driver for pico w - pico_cyw43_driver # integration with async_context - ) - - if (NOT TARGET pico_lwip) - message(WARNING "lwIP is not available; Full Pico W wireless support will be unavailable") - else() - message("Pico W Wi-Fi build support available.") - pico_add_library(pico_cyw43_arch_poll NOFLAG) - target_compile_definitions(pico_cyw43_arch_poll_headers INTERFACE - PICO_CYW43_ARCH_POLL=1 - ) - pico_mirrored_target_link_libraries(pico_cyw43_arch_poll INTERFACE - pico_cyw43_arch - pico_async_context_poll) - - pico_add_library(pico_cyw43_arch_lwip_poll NOFLAG) - pico_mirrored_target_link_libraries(pico_cyw43_arch_lwip_poll INTERFACE - pico_lwip_nosys - pico_cyw43_arch_poll) - target_compile_definitions(pico_cyw43_arch_lwip_poll_headers INTERFACE - CYW43_LWIP=1 - ) - - pico_add_library(pico_cyw43_arch_threadsafe_background NOFLAG) - pico_mirrored_target_link_libraries(pico_cyw43_arch_threadsafe_background INTERFACE - pico_cyw43_arch - pico_async_context_threadsafe_background) - target_compile_definitions(pico_cyw43_arch_threadsafe_background_headers INTERFACE - PICO_CYW43_ARCH_THREADSAFE_BACKGROUND=1 - ) - - pico_add_library(pico_cyw43_arch_lwip_threadsafe_background NOFLAG) - pico_mirrored_target_link_libraries(pico_cyw43_arch_lwip_threadsafe_background INTERFACE - pico_lwip_nosys - pico_cyw43_arch_threadsafe_background) - target_compile_definitions(pico_cyw43_arch_lwip_threadsafe_background_headers INTERFACE - CYW43_LWIP=1 - ) - - pico_add_library(pico_cyw43_arch_sys_freertos NOFLAG) - pico_mirrored_target_link_libraries(pico_cyw43_arch_sys_freertos INTERFACE - pico_cyw43_arch - pico_async_context_freertos) - target_compile_definitions(pico_cyw43_arch_sys_freertos_headers INTERFACE - PICO_CYW43_ARCH_FREERTOS=1 - ) - - pico_add_library(pico_cyw43_arch_lwip_sys_freertos NOFLAG) - pico_mirrored_target_link_libraries(pico_cyw43_arch_lwip_sys_freertos INTERFACE - pico_lwip_freertos - pico_cyw43_arch_sys_freertos) - target_compile_definitions(pico_cyw43_arch_lwip_sys_freertos_headers INTERFACE - CYW43_LWIP=1 - LWIP_PROVIDE_ERRNO=1 - # now the default - #PICO_LWIP_CUSTOM_LOCK_TCPIP_CORE=1 # we want to override the lwip locking mechanism to use our mutex - ) - endif() - - pico_add_library(pico_cyw43_arch_none NOFLAG) - pico_mirrored_target_link_libraries(pico_cyw43_arch_none INTERFACE - pico_cyw43_arch - pico_async_context_threadsafe_background) - target_compile_definitions(pico_cyw43_arch_none_headers INTERFACE - CYW43_LWIP=0 - PICO_CYW43_ARCH_THREADSAFE_BACKGROUND=1 # none still uses threadsafe_background to make gpio use easy - ) - endif() -endif() - -if (PICO_CYW43_DRIVER_PATH AND EXISTS "${PICO_CYW43_DRIVER_PATH}") - pico_add_doxygen(${PICO_CYW43_DRIVER_PATH}/src) -endif() \ No newline at end of file diff --git a/pico-sdk/src/rp2_common/pico_cyw43_arch/cyw43_arch.c b/pico-sdk/src/rp2_common/pico_cyw43_arch/cyw43_arch.c deleted file mode 100644 index bdfab8c..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_arch/cyw43_arch.c +++ /dev/null @@ -1,188 +0,0 @@ -/* - * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include -#include -#include - -#include "pico/unique_id.h" -#include "cyw43.h" -#include "pico/cyw43_arch.h" -#include "cyw43_ll.h" -#include "cyw43_stats.h" - -#if PICO_CYW43_ARCH_DEBUG_ENABLED -#define CYW43_ARCH_DEBUG(...) printf(__VA_ARGS__) -#else -#define CYW43_ARCH_DEBUG(...) ((void)0) -#endif - -static uint32_t country_code = PICO_CYW43_ARCH_DEFAULT_COUNTRY_CODE; - -static async_context_t *async_context; - -void cyw43_arch_set_async_context(async_context_t *context) { - async_context = context; -} - -void cyw43_arch_enable_sta_mode(void) { - assert(cyw43_is_initialized(&cyw43_state)); - cyw43_wifi_set_up(&cyw43_state, CYW43_ITF_STA, true, cyw43_arch_get_country_code()); -} - -void cyw43_arch_disable_sta_mode(void) { - assert(cyw43_is_initialized(&cyw43_state)); - if (cyw43_state.itf_state & (1 << CYW43_ITF_STA)) { - cyw43_cb_tcpip_deinit(&cyw43_state, CYW43_ITF_STA); - cyw43_state.itf_state &= ~(1 << CYW43_ITF_STA); - } - if (cyw43_state.wifi_join_state) { - cyw43_wifi_leave(&cyw43_state, CYW43_ITF_STA); - } -} - -void cyw43_arch_enable_ap_mode(const char *ssid, const char *password, uint32_t auth) { - assert(cyw43_is_initialized(&cyw43_state)); - cyw43_wifi_ap_set_ssid(&cyw43_state, strlen(ssid), (const uint8_t *) ssid); - if (password) { - cyw43_wifi_ap_set_password(&cyw43_state, strlen(password), (const uint8_t *) password); - cyw43_wifi_ap_set_auth(&cyw43_state, auth); - } else { - cyw43_wifi_ap_set_auth(&cyw43_state, CYW43_AUTH_OPEN); - } - cyw43_wifi_set_up(&cyw43_state, CYW43_ITF_AP, true, cyw43_arch_get_country_code()); -} - -void cyw43_arch_disable_ap_mode(void) { - assert(cyw43_is_initialized(&cyw43_state)); - cyw43_wifi_set_up(&cyw43_state, CYW43_ITF_AP, false, cyw43_arch_get_country_code()); - cyw43_state.itf_state &= ~(1 << CYW43_ITF_AP); -} - -#if PICO_CYW43_ARCH_DEBUG_ENABLED -// Return a string for the wireless state -static const char* cyw43_tcpip_link_status_name(int status) -{ - switch (status) { - case CYW43_LINK_DOWN: - return "link down"; - case CYW43_LINK_JOIN: - return "joining"; - case CYW43_LINK_NOIP: - return "no ip"; - case CYW43_LINK_UP: - return "link up"; - case CYW43_LINK_FAIL: - return "link fail"; - case CYW43_LINK_NONET: - return "network fail"; - case CYW43_LINK_BADAUTH: - return "bad auth"; - } - return "unknown"; -} -#endif - - -int cyw43_arch_wifi_connect_bssid_async(const char *ssid, const uint8_t *bssid, const char *pw, uint32_t auth) { - if (!pw) auth = CYW43_AUTH_OPEN; - // Connect to wireless - return cyw43_wifi_join(&cyw43_state, strlen(ssid), (const uint8_t *)ssid, pw ? strlen(pw) : 0, (const uint8_t *)pw, auth, bssid, CYW43_CHANNEL_NONE); -} - -int cyw43_arch_wifi_connect_async(const char *ssid, const char *pw, uint32_t auth) { - return cyw43_arch_wifi_connect_bssid_async(ssid, NULL, pw, auth); -} - -static int cyw43_arch_wifi_connect_bssid_until(const char *ssid, const uint8_t *bssid, const char *pw, uint32_t auth, absolute_time_t until) { - int err = cyw43_arch_wifi_connect_bssid_async(ssid, bssid, pw, auth); - if (err) return err; - - int status = CYW43_LINK_UP + 1; - while(status >= 0 && status != CYW43_LINK_UP) { - int new_status = cyw43_tcpip_link_status(&cyw43_state, CYW43_ITF_STA); - // If there was no network, keep trying - if (new_status == CYW43_LINK_NONET) { - new_status = CYW43_LINK_JOIN; - err = cyw43_arch_wifi_connect_bssid_async(ssid, bssid, pw, auth); - if (err) return err; - } - if (new_status != status) { - status = new_status; - CYW43_ARCH_DEBUG("connect status: %s\n", cyw43_tcpip_link_status_name(status)); - } - if (time_reached(until)) { - return PICO_ERROR_TIMEOUT; - } - // Do polling - cyw43_arch_poll(); - cyw43_arch_wait_for_work_until(until); - } - // Turn status into a pico_error_codes, CYW43_LINK_NONET shouldn't happen as we fail with PICO_ERROR_TIMEOUT instead - assert(status == CYW43_LINK_UP || status == CYW43_LINK_BADAUTH || status == CYW43_LINK_FAIL); - if (status == CYW43_LINK_UP) { - return PICO_OK; // success - } else if (status == CYW43_LINK_BADAUTH) { - return PICO_ERROR_BADAUTH; - } else { - return PICO_ERROR_CONNECT_FAILED; - } -} - -// Connect to wireless, return with success when an IP address has been assigned -static int cyw43_arch_wifi_connect_until(const char *ssid, const char *pw, uint32_t auth, absolute_time_t until) { - return cyw43_arch_wifi_connect_bssid_until(ssid, NULL, pw, auth, until); -} - -int cyw43_arch_wifi_connect_blocking(const char *ssid, const char *pw, uint32_t auth) { - return cyw43_arch_wifi_connect_until(ssid, pw, auth, at_the_end_of_time); -} - -int cyw43_arch_wifi_connect_bssid_blocking(const char *ssid, const uint8_t *bssid, const char *pw, uint32_t auth) { - return cyw43_arch_wifi_connect_bssid_until(ssid, bssid, pw, auth, at_the_end_of_time); -} - -int cyw43_arch_wifi_connect_timeout_ms(const char *ssid, const char *pw, uint32_t auth, uint32_t timeout_ms) { - return cyw43_arch_wifi_connect_until(ssid, pw, auth, make_timeout_time_ms(timeout_ms)); -} - -int cyw43_arch_wifi_connect_bssid_timeout_ms(const char *ssid, const uint8_t *bssid, const char *pw, uint32_t auth, uint32_t timeout_ms) { - return cyw43_arch_wifi_connect_bssid_until(ssid, bssid, pw, auth, make_timeout_time_ms(timeout_ms)); -} - -uint32_t cyw43_arch_get_country_code(void) { - return country_code; -} - -int cyw43_arch_init_with_country(uint32_t country) { - country_code = country; - return cyw43_arch_init(); -} - -void cyw43_arch_gpio_put(uint wl_gpio, bool value) { - invalid_params_if(CYW43_ARCH, wl_gpio >= CYW43_WL_GPIO_COUNT); - cyw43_gpio_set(&cyw43_state, (int)wl_gpio, value); -} - -bool cyw43_arch_gpio_get(uint wl_gpio) { - invalid_params_if(CYW43_ARCH, wl_gpio >= CYW43_WL_GPIO_COUNT); - bool value = false; - cyw43_gpio_get(&cyw43_state, (int)wl_gpio, &value); - return value; -} - -async_context_t *cyw43_arch_async_context(void) { - return async_context; -} - -void cyw43_arch_poll(void) -{ - async_context_poll(async_context); -} - -void cyw43_arch_wait_for_work_until(absolute_time_t until) { - async_context_wait_for_work_until(async_context, until); -} diff --git a/pico-sdk/src/rp2_common/pico_cyw43_arch/cyw43_arch_freertos.c b/pico-sdk/src/rp2_common/pico_cyw43_arch/cyw43_arch_freertos.c deleted file mode 100644 index 93f73ad..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_arch/cyw43_arch_freertos.c +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#if PICO_CYW43_ARCH_FREERTOS - -#include "pico/cyw43_arch.h" -#include "pico/cyw43_driver.h" -#include "pico/async_context_freertos.h" - -#if CYW43_LWIP -#include "pico/lwip_freertos.h" -#include -#endif - -#if CYW43_ENABLE_BLUETOOTH -#include "pico/btstack_cyw43.h" -#endif - -#if NO_SYS -#error example_cyw43_arch_freetos_sys requires NO_SYS=0 -#endif - -static async_context_freertos_t cyw43_async_context_freertos; - -async_context_t *cyw43_arch_init_default_async_context(void) { - async_context_freertos_config_t config = async_context_freertos_default_config(); -#ifdef CYW43_TASK_PRIORITY - config.task_priority = CYW43_TASK_PRIORITY; -#endif -#ifdef CYW43_TASK_STACK_SIZE - config.task_stack_size = CYW43_TASK_STACK_SIZE; -#endif - if (async_context_freertos_init(&cyw43_async_context_freertos, &config)) - return &cyw43_async_context_freertos.core; - return NULL; -} - -int cyw43_arch_init(void) { - async_context_t *context = cyw43_arch_async_context(); - if (!context) { - context = cyw43_arch_init_default_async_context(); - if (!context) return PICO_ERROR_GENERIC; - cyw43_arch_set_async_context(context); - } - bool ok = cyw43_driver_init(context); -#if CYW43_LWIP - ok &= lwip_freertos_init(context); -#endif -#if CYW43_ENABLE_BLUETOOTH - ok &= btstack_cyw43_init(context); -#endif - if (!ok) { - cyw43_arch_deinit(); - return PICO_ERROR_GENERIC; - } else { - return 0; - } -} - -void cyw43_arch_deinit(void) { - async_context_t *context = cyw43_arch_async_context(); -#if CYW43_ENABLE_BLUETOOTH - btstack_cyw43_deinit(context); -#endif - // there is a bit of a circular dependency here between lwIP and cyw43_driver. We - // shut down cyw43_driver first as it has IRQs calling back into lwIP. Also lwIP itself - // does not actually get shut down. - // todo add a "pause" method to async_context if we need to provide some atomicity (we - // don't want to take the lock as these methods may invoke execute_sync() - cyw43_driver_deinit(context); -#if CYW43_LWIP - lwip_freertos_deinit(context); -#endif - // if it is our context, then we de-init it. - if (context == &cyw43_async_context_freertos.core) { - async_context_deinit(context); - cyw43_arch_set_async_context(NULL); - } -} - -#endif \ No newline at end of file diff --git a/pico-sdk/src/rp2_common/pico_cyw43_arch/cyw43_arch_poll.c b/pico-sdk/src/rp2_common/pico_cyw43_arch/cyw43_arch_poll.c deleted file mode 100644 index e885985..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_arch/cyw43_arch_poll.c +++ /dev/null @@ -1,76 +0,0 @@ -/* - * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#if PICO_CYW43_ARCH_POLL - -#include "pico/cyw43_arch.h" -#include "pico/cyw43_driver.h" - -#include "pico/async_context_poll.h" -#if CYW43_LWIP -#include "pico/lwip_nosys.h" -#endif - -#if CYW43_ENABLE_BLUETOOTH -#include "pico/btstack_cyw43.h" -#endif - -#if CYW43_LWIP && !NO_SYS -#error PICO_CYW43_ARCH_POLL requires lwIP NO_SYS=1 -#endif - -static async_context_poll_t cyw43_async_context_poll; - -async_context_t *cyw43_arch_init_default_async_context(void) { - if (async_context_poll_init_with_defaults(&cyw43_async_context_poll)) - return &cyw43_async_context_poll.core; - return NULL; -} - -int cyw43_arch_init(void) { - async_context_t *context = cyw43_arch_async_context(); - if (!context) { - context = cyw43_arch_init_default_async_context(); - if (!context) return PICO_ERROR_GENERIC; - cyw43_arch_set_async_context(context); - } - bool ok = cyw43_driver_init(context); -#if CYW43_LWIP - ok &= lwip_nosys_init(context); -#endif -#if CYW43_ENABLE_BLUETOOTH - ok &= btstack_cyw43_init(context); -#endif - if (!ok) { - cyw43_arch_deinit(); - return PICO_ERROR_GENERIC; - } else { - return 0; - } -} - -void cyw43_arch_deinit(void) { - async_context_t *context = cyw43_arch_async_context(); -#if CYW43_ENABLE_BLUETOOTH - btstack_cyw43_deinit(context); -#endif - // there is a bit of a circular dependency here between lwIP and cyw43_driver. We - // shut down cyw43_driver first as it has IRQs calling back into lwIP. Also lwIP itself - // does not actually get shut down. - // todo add a "pause" method to async_context if we need to provide some atomicity (we - // don't want to take the lock as these methods may invoke execute_sync() - cyw43_driver_deinit(context); -#if CYW43_LWIP - lwip_nosys_deinit(context); -#endif - // if it is our context, then we de-init it. - if (context == &cyw43_async_context_poll.core) { - async_context_deinit(context); - cyw43_arch_set_async_context(NULL); - } -} - -#endif diff --git a/pico-sdk/src/rp2_common/pico_cyw43_arch/cyw43_arch_threadsafe_background.c b/pico-sdk/src/rp2_common/pico_cyw43_arch/cyw43_arch_threadsafe_background.c deleted file mode 100644 index 397da8a..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_arch/cyw43_arch_threadsafe_background.c +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#if PICO_CYW43_ARCH_THREADSAFE_BACKGROUND - -#include "pico/cyw43_arch.h" -#include "pico/cyw43_driver.h" -#include "pico/async_context_threadsafe_background.h" - -#if CYW43_LWIP -#include "pico/lwip_nosys.h" -#endif - -#if CYW43_ENABLE_BLUETOOTH -#include "pico/btstack_cyw43.h" -#endif - -#if CYW43_LWIP && !NO_SYS -#error PICO_CYW43_ARCH_THREADSAFE_BACKGROUND requires lwIP NO_SYS=1 -#endif -#if CYW43_LWIP && MEM_LIBC_MALLOC -// would attempt to use malloc from IRQ context -#error MEM_LIBC_MALLOC is incompatible with PICO_CYW43_ARCH_THREADSAFE_BACKGROUND -#endif - -static async_context_threadsafe_background_t cyw43_async_context_threadsafe_background; - -async_context_t *cyw43_arch_init_default_async_context(void) { - async_context_threadsafe_background_config_t config = async_context_threadsafe_background_default_config(); - if (async_context_threadsafe_background_init(&cyw43_async_context_threadsafe_background, &config)) - return &cyw43_async_context_threadsafe_background.core; - return NULL; -} - -int cyw43_arch_init(void) { - async_context_t *context = cyw43_arch_async_context(); - if (!context) { - context = cyw43_arch_init_default_async_context(); - if (!context) return PICO_ERROR_GENERIC; - cyw43_arch_set_async_context(context); - } - bool ok = cyw43_driver_init(context); -#if CYW43_LWIP - ok &= lwip_nosys_init(context); -#endif -#if CYW43_ENABLE_BLUETOOTH - ok &= btstack_cyw43_init(context); -#endif - if (!ok) { - cyw43_arch_deinit(); - return PICO_ERROR_GENERIC; - } else { - return 0; - } -} - -void cyw43_arch_deinit(void) { - async_context_t *context = cyw43_arch_async_context(); -#if CYW43_ENABLE_BLUETOOTH - btstack_cyw43_deinit(context); -#endif - // there is a bit of a circular dependency here between lwIP and cyw43_driver. We - // shut down cyw43_driver first as it has IRQs calling back into lwIP. Also lwIP itself - // does not actually get shut down. - // todo add a "pause" method to async_context if we need to provide some atomicity (we - // don't want to take the lock as these methods may invoke execute_sync() - cyw43_driver_deinit(context); -#if CYW43_LWIP - lwip_nosys_deinit(context); -#endif - // if it is our context, then we de-init it. - if (context == &cyw43_async_context_threadsafe_background.core) { - async_context_deinit(context); - cyw43_arch_set_async_context(NULL); - } -} - -#endif diff --git a/pico-sdk/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch.h b/pico-sdk/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch.h deleted file mode 100644 index bae2112..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch.h +++ /dev/null @@ -1,504 +0,0 @@ -/* - * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef _PICO_CYW43_ARCH_H -#define _PICO_CYW43_ARCH_H - -#include "pico.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#include "cyw43.h" -#include "cyw43_country.h" -#include "pico/async_context.h" - -#ifdef PICO_CYW43_ARCH_HEADER -#include __XSTRING(PICO_CYW43_ARCH_HEADER) -#else -#if PICO_CYW43_ARCH_POLL -#include "pico/cyw43_arch/arch_poll.h" -#elif PICO_CYW43_ARCH_THREADSAFE_BACKGROUND -#include "pico/cyw43_arch/arch_threadsafe_background.h" -#elif PICO_CYW43_ARCH_FREERTOS -#include "pico/cyw43_arch/arch_freertos.h" -#else -#error must specify support pico_cyw43_arch architecture type or set PICO_CYW43_ARCH_HEADER -#endif -#endif - -/** - * \defgroup cyw43_driver cyw43_driver - * \ingroup pico_cyw43_arch - * \brief Driver used for Pico W wireless -*/ - -/** - * \defgroup cyw43_ll cyw43_ll - * \ingroup cyw43_driver - * \brief Low Level CYW43 driver interface -*/ - -/** \file pico/cyw43_arch.h - * \defgroup pico_cyw43_arch pico_cyw43_arch - * - * Architecture for integrating the CYW43 driver (for the wireless on Pico W) and lwIP (for TCP/IP stack) into the SDK. It is also necessary for accessing the on-board LED on Pico W - * - * Both the low level \c cyw43_driver and the lwIP stack require periodic servicing, and have limitations - * on whether they can be called from multiple cores/threads. - * - * \c pico_cyw43_arch attempts to abstract these complications into several behavioral groups: - * - * * \em 'poll' - This not multi-core/IRQ safe, and requires the user to call \ref cyw43_arch_poll periodically from their main loop - * * \em 'thread_safe_background' - This is multi-core/thread/task safe, and maintenance of the driver and TCP/IP stack is handled automatically in the background - * * \em 'freertos' - This is multi-core/thread/task safe, and uses a separate FreeRTOS task to handle lwIP and and driver work. - * - * As of right now, lwIP is the only supported TCP/IP stack, however the use of \c pico_cyw43_arch is intended to be independent of - * the particular TCP/IP stack used (and possibly Bluetooth stack used) in the future. For this reason, the integration of lwIP - * is handled in the base (\c pico_cyw43_arch) library based on the #define \ref CYW43_LWIP used by the \c cyw43_driver. - * - * \note As of version 1.5.0 of the Raspberry Pi Pico SDK, the \c pico_cyw43_arch library no longer directly implements - * the distinct behavioral abstractions. This is now handled by the more general \ref pico_async_context library. The - * user facing behavior of pico_cyw43_arch has not changed as a result of this implementation detail, however pico_cyw43_arch - * is now just a thin wrapper which creates an appropriate async_context and makes a simple call to add lwIP or cyw43_driver support - * as appropriate. You are free to perform this context creation and adding of lwIP, cyw43_driver or indeed any other additional - * future protocol/driver support to your async_context, however for now pico_cyw43_arch does still provide a few cyw43_ specific (i.e. Pico W) - * APIs for connection management, locking and GPIO interaction. - * - * \note The connection management APIs at least may be moved - * to a more generic library in a future release. The locking methods are now backed by their \ref pico_async_context equivalents, and - * those methods may be used interchangeably (see \ref cyw43_arch_lwip_begin, \ref cyw43_arch_lwip_end and \ref cyw43_arch_lwip_check for more details). - * - * \note For examples of creating of your own async_context and addition of \c cyw43_driver and \c lwIP support, please - * refer to the specific source files \c cyw43_arch_poll.c, \c cyw43_arch_threadsafe_background.c and \c cyw43_arch_freertos.c. - * - * Whilst you can use the \c pico_cyw43_arch library directly and specify \ref CYW43_LWIP (and other defines) yourself, several - * other libraries are made available to the build which aggregate the defines and other dependencies for you: - * - * * \b pico_cyw43_arch_lwip_poll - For using the RAW lwIP API (in `NO_SYS=1` mode) without any background processing or multi-core/thread safety. - * - * The user must call \ref cyw43_arch_poll periodically from their main loop. - * - * This wrapper library: - * - Sets \c CYW43_LWIP=1 to enable lwIP support in \c pico_cyw43_arch and \c cyw43_driver. - * - Sets \c PICO_CYW43_ARCH_POLL=1 to select the polling behavior. - * - Adds the \c pico_lwip as a dependency to pull in lwIP. - * - * * \b pico_cyw43_arch_lwip_threadsafe_background - For using the RAW lwIP API (in `NO_SYS=1` mode) with multi-core/thread safety, and automatic servicing of the \c cyw43_driver and - * lwIP in background. - * - * Calls into the \c cyw43_driver high level API (cyw43.h) may be made from either core or from lwIP callbacks, however calls into lwIP (which - * is not thread-safe) other than those made from lwIP callbacks, must be bracketed with \ref cyw43_arch_lwip_begin and \ref cyw43_arch_lwip_end. It is fine to bracket - * calls made from within lwIP callbacks too; you just don't have to. - * - * \note lwIP callbacks happen in a (low priority) IRQ context (similar to an alarm callback), so care should be taken when interacting - * with other code. - * - * This wrapper library: - * - Sets \c CYW43_LWIP=1 to enable lwIP support in \c pico_cyw43_arch and \c cyw43_driver - * - Sets \c PICO_CYW43_ARCH_THREADSAFE_BACKGROUND=1 to select the thread-safe/non-polling behavior. - * - Adds the pico_lwip as a dependency to pull in lwIP. - * - * - * This library \em can also be used under the RP2040 port of FreeRTOS with lwIP in `NO_SYS=1` mode (allowing you to call \c cyw43_driver APIs - * from any task, and to call lwIP from lwIP callbacks, or from any task if you bracket the calls with \ref cyw43_arch_lwip_begin and \ref cyw43_arch_lwip_end. Again, you should be - * careful about what you do in lwIP callbacks, as you cannot call most FreeRTOS APIs from within an IRQ context. Unless you have good reason, you should probably - * use the full FreeRTOS integration (with `NO_SYS=0`) provided by \c pico_cyw43_arch_lwip_sys_freertos. - * - * * \b pico_cyw43_arch_lwip_sys_freertos - For using the full lwIP API including blocking sockets in OS (`NO_SYS=0`) mode, along with with multi-core/task/thread safety, and automatic servicing of the \c cyw43_driver and - * the lwIP stack. - * - * This wrapper library: - * - Sets \c CYW43_LWIP=1 to enable lwIP support in \c pico_cyw43_arch and \c cyw43_driver. - * - Sets \c PICO_CYW43_ARCH_FREERTOS=1 to select the NO_SYS=0 lwip/FreeRTOS integration - * - Sets \c LWIP_PROVIDE_ERRNO=1 to provide error numbers needed for compilation without an OS - * - Adds the \c pico_lwip as a dependency to pull in lwIP. - * - Adds the lwIP/FreeRTOS code from lwip-contrib (in the contrib directory of lwIP) - * - * Calls into the \c cyw43_driver high level API (cyw43.h) may be made from any task or from lwIP callbacks, but not from IRQs. Calls into the lwIP RAW API (which is not thread safe) - * must be bracketed with \ref cyw43_arch_lwip_begin and \ref cyw43_arch_lwip_end. It is fine to bracket calls made from within lwIP callbacks too; you just don't have to. - * - * \note this wrapper library requires you to link FreeRTOS functionality with your application yourself. - * - * * \b pico_cyw43_arch_none - If you do not need the TCP/IP stack but wish to use the on-board LED. - * - * This wrapper library: - * - Sets \c CYW43_LWIP=0 to disable lwIP support in \c pico_cyw43_arch and \c cyw43_driver - */ - -// PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_CYW43_ARCH, Enable/disable assertions in the pico_cyw43_arch module, type=bool, default=0, group=pico_cyw43_arch -#ifndef PARAM_ASSERTIONS_ENABLED_CYW43_ARCH -#define PARAM_ASSERTIONS_ENABLED_CYW43_ARCH 0 -#endif - -// PICO_CONFIG: PICO_CYW43_ARCH_DEBUG_ENABLED, Enable/disable some debugging output in the pico_cyw43_arch module, type=bool, default=1 in debug builds, group=pico_cyw43_arch -#ifndef PICO_CYW43_ARCH_DEBUG_ENABLED -#ifndef NDEBUG -#define PICO_CYW43_ARCH_DEBUG_ENABLED 1 -#else -#define PICO_CYW43_ARCH_DEBUG_ENABLED 0 -#endif -#endif - -// PICO_CONFIG: PICO_CYW43_ARCH_DEFAULT_COUNTRY_CODE, Default country code for the cyw43 wireless driver, default=CYW43_COUNTRY_WORLDWIDE, group=pico_cyw43_arch -#ifndef PICO_CYW43_ARCH_DEFAULT_COUNTRY_CODE -#define PICO_CYW43_ARCH_DEFAULT_COUNTRY_CODE CYW43_COUNTRY_WORLDWIDE -#endif - -/*! - * \brief Initialize the CYW43 architecture - * \ingroup pico_cyw43_arch - * - * This method initializes the `cyw43_driver` code and initializes the lwIP stack (if it - * was enabled at build time). This method must be called prior to using any other \c pico_cyw43_arch, - * \c cyw43_driver or lwIP functions. - * - * \note this method initializes wireless with a country code of \c PICO_CYW43_ARCH_DEFAULT_COUNTRY_CODE - * which defaults to \c CYW43_COUNTRY_WORLDWIDE. Worldwide settings may not give the best performance; consider - * setting PICO_CYW43_ARCH_DEFAULT_COUNTRY_CODE to a different value or calling \ref cyw43_arch_init_with_country - * - * By default this method initializes the cyw43_arch code's own async_context by calling - * \ref cyw43_arch_init_default_async_context, however the user can specify use of their own async_context - * by calling \ref cyw43_arch_set_async_context() before calling this method - * - * \return 0 if the initialization is successful, an error code otherwise \see pico_error_codes - */ -int cyw43_arch_init(void); - -/*! - * \brief Initialize the CYW43 architecture for use in a specific country - * \ingroup pico_cyw43_arch - * - * This method initializes the `cyw43_driver` code and initializes the lwIP stack (if it - * was enabled at build time). This method must be called prior to using any other \c pico_cyw43_arch, - * \c cyw43_driver or lwIP functions. - * - * By default this method initializes the cyw43_arch code's own async_context by calling - * \ref cyw43_arch_init_default_async_context, however the user can specify use of their own async_context - * by calling \ref cyw43_arch_set_async_context() before calling this method - * - * \param country the country code to use (see \ref CYW43_COUNTRY_) - * \return 0 if the initialization is successful, an error code otherwise \see pico_error_codes - */ -int cyw43_arch_init_with_country(uint32_t country); - -/*! - * \brief De-initialize the CYW43 architecture - * \ingroup pico_cyw43_arch - * - * This method de-initializes the `cyw43_driver` code and de-initializes the lwIP stack (if it - * was enabled at build time). Note this method should always be called from the same core (or RTOS - * task, depending on the environment) as \ref cyw43_arch_init. - * - * Additionally if the cyw43_arch is using its own async_context instance, then that instance is de-initialized. - */ -void cyw43_arch_deinit(void); - -/*! - * \brief Return the current async_context currently in use by the cyw43_arch code - * \ingroup pico_cyw43_arch - * - * \return the async_context. - */ -async_context_t *cyw43_arch_async_context(void); - -/*! - * \brief Set the async_context to be used by the cyw43_arch_init - * \ingroup pico_cyw43_arch - * - * \note This method must be called before calling cyw43_arch_init or cyw43_arch_init_with_country - * if you wish to use a custom async_context instance. - * - * \param context the async_context to be used - */ -void cyw43_arch_set_async_context(async_context_t *context); - -/*! - * \brief Initialize the default async_context for the current cyw43_arch type - * \ingroup pico_cyw43_arch - * - * This method initializes and returns a pointer to the static async_context associated - * with cyw43_arch. This method is called by \ref cyw43_arch_init automatically - * if a different async_context has not been set by \ref cyw43_arch_set_async_context - * - * \return the context or NULL if initialization failed. - */ -async_context_t *cyw43_arch_init_default_async_context(void); - -/*! - * \brief Perform any processing required by the \c cyw43_driver or the TCP/IP stack - * \ingroup pico_cyw43_arch - * - * This method must be called periodically from the main loop when using a - * \em polling style \c pico_cyw43_arch (e.g. \c pico_cyw43_arch_lwip_poll ). It - * may be called in other styles, but it is unnecessary to do so. - */ -void cyw43_arch_poll(void); - -/*! - * \brief Sleep until there is cyw43_driver work to be done - * \ingroup pico_cyw43_arch - * - * This method may be called by code that is waiting for an event to - * come from the cyw43_driver, and has no work to do, but would like - * to sleep without blocking any background work associated with the cyw43_driver. - * - * \param until the time to wait until if there is no work to do. - */ -void cyw43_arch_wait_for_work_until(absolute_time_t until); - -/*! - * \fn cyw43_arch_lwip_begin - * \brief Acquire any locks required to call into lwIP - * \ingroup pico_cyw43_arch - * - * The lwIP API is not thread safe. You should surround calls into the lwIP API - * with calls to this method and \ref cyw43_arch_lwip_end. Note these calls are not - * necessary (but harmless) when you are calling back into the lwIP API from an lwIP callback. - * If you are using single-core polling only (pico_cyw43_arch_poll) then these calls are no-ops - * anyway it is good practice to call them anyway where they are necessary. - * - * \note as of SDK release 1.5.0, this is now equivalent to calling \ref async_context_acquire_lock_blocking - * on the async_context associated with cyw43_arch and lwIP. - * - * \sa cyw43_arch_lwip_end - * \sa cyw43_arch_lwip_protect - * \sa async_context_acquire_lock_blocking - * \sa cyw43_arch_async_context - */ -static inline void cyw43_arch_lwip_begin(void) { - cyw43_thread_enter(); -} - -/*! - * \fn void cyw43_arch_lwip_end(void) - * \brief Release any locks required for calling into lwIP - * \ingroup pico_cyw43_arch - * - * The lwIP API is not thread safe. You should surround calls into the lwIP API - * with calls to \ref cyw43_arch_lwip_begin and this method. Note these calls are not - * necessary (but harmless) when you are calling back into the lwIP API from an lwIP callback. - * If you are using single-core polling only (pico_cyw43_arch_poll) then these calls are no-ops - * anyway it is good practice to call them anyway where they are necessary. - * - * \note as of SDK release 1.5.0, this is now equivalent to calling \ref async_context_release_lock - * on the async_context associated with cyw43_arch and lwIP. - * - * \sa cyw43_arch_lwip_begin - * \sa cyw43_arch_lwip_protect - * \sa async_context_release_lock - * \sa cyw43_arch_async_context - */ -static inline void cyw43_arch_lwip_end(void) { - cyw43_thread_exit(); -} - -/*! - * \fn int cyw43_arch_lwip_protect(int (*func)(void *param), void *param) - * \brief sad Release any locks required for calling into lwIP - * \ingroup pico_cyw43_arch - * - * The lwIP API is not thread safe. You can use this method to wrap a function - * with any locking required to call into the lwIP API. If you are using - * single-core polling only (pico_cyw43_arch_poll) then there are no - * locks to required, but it is still good practice to use this function. - * - * \param func the function ta call with any required locks held - * \param param parameter to pass to \c func - * \return the return value from \c func - * \sa cyw43_arch_lwip_begin - * \sa cyw43_arch_lwip_end - */ -static inline int cyw43_arch_lwip_protect(int (*func)(void *param), void *param) { - cyw43_arch_lwip_begin(); - int rc = func(param); - cyw43_arch_lwip_end(); - return rc; -} - -/*! - * \fn void cyw43_arch_lwip_check(void) - * \brief Checks the caller has any locks required for calling into lwIP - * \ingroup pico_cyw43_arch - * - * The lwIP API is not thread safe. You should surround calls into the lwIP API - * with calls to \ref cyw43_arch_lwip_begin and this method. Note these calls are not - * necessary (but harmless) when you are calling back into the lwIP API from an lwIP callback. - * - * This method will assert in debug mode, if the above conditions are not met (i.e. it is not safe to - * call into the lwIP API) - * - * \note as of SDK release 1.5.0, this is now equivalent to calling \ref async_context_lock_check - * on the async_context associated with cyw43_arch and lwIP. - * - * \sa cyw43_arch_lwip_begin - * \sa cyw43_arch_lwip_protect - * \sa async_context_lock_check - * \sa cyw43_arch_async_context - */ - -/*! - * \brief Return the country code used to initialize cyw43_arch - * \ingroup pico_cyw43_arch - * - * \return the country code (see \ref CYW43_COUNTRY_) - */ -uint32_t cyw43_arch_get_country_code(void); - -/*! - * \brief Enables Wi-Fi STA (Station) mode. - * \ingroup pico_cyw43_arch - * - * This enables the Wi-Fi in \em Station mode such that connections can be made to other Wi-Fi Access Points - */ -void cyw43_arch_enable_sta_mode(void); - -/*! - * \brief Disables Wi-Fi STA (Station) mode. - * \ingroup pico_cyw43_arch - * - * This disables the Wi-Fi in \em Station mode, disconnecting any active connection. - * You should subsequently check the status by calling \ref cyw43_wifi_link_status. - */ -void cyw43_arch_disable_sta_mode(void); - -/*! - * \brief Enables Wi-Fi AP (Access point) mode. - * \ingroup pico_cyw43_arch - * - * This enables the Wi-Fi in \em Access \em Point mode such that connections can be made to the device by other Wi-Fi clients - * \param ssid the name for the access point - * \param password the password to use or NULL for no password. - * \param auth the authorization type to use when the password is enabled. Values are \ref CYW43_AUTH_WPA_TKIP_PSK, - * \ref CYW43_AUTH_WPA2_AES_PSK, or \ref CYW43_AUTH_WPA2_MIXED_PSK (see \ref CYW43_AUTH_) - */ -void cyw43_arch_enable_ap_mode(const char *ssid, const char *password, uint32_t auth); - -/*! - * \brief Disables Wi-Fi AP (Access point) mode. - * \ingroup pico_cyw43_arch - * - * This Disbles the Wi-Fi in \em Access \em Point mode. - */ -void cyw43_arch_disable_ap_mode(void); - -/*! - * \brief Attempt to connect to a wireless access point, blocking until the network is joined or a failure is detected. - * \ingroup pico_cyw43_arch - * - * \param ssid the network name to connect to - * \param pw the network password or NULL if there is no password required - * \param auth the authorization type to use when the password is enabled. Values are \ref CYW43_AUTH_WPA_TKIP_PSK, - * \ref CYW43_AUTH_WPA2_AES_PSK, or \ref CYW43_AUTH_WPA2_MIXED_PSK (see \ref CYW43_AUTH_) - * - * \return 0 if the initialization is successful, an error code otherwise \see pico_error_codes - */ -int cyw43_arch_wifi_connect_blocking(const char *ssid, const char *pw, uint32_t auth); - -/*! - * \brief Attempt to connect to a wireless access point specified by SSID and BSSID, blocking until the network is joined or a failure is detected. - * \ingroup pico_cyw43_arch - * - * \param ssid the network name to connect to - * \param bssid the network BSSID to connect to or NULL if ignored - * \param pw the network password or NULL if there is no password required - * \param auth the authorization type to use when the password is enabled. Values are \ref CYW43_AUTH_WPA_TKIP_PSK, - * \ref CYW43_AUTH_WPA2_AES_PSK, or \ref CYW43_AUTH_WPA2_MIXED_PSK (see \ref CYW43_AUTH_) - * - * \return 0 if the initialization is successful, an error code otherwise \see pico_error_codes - */ -int cyw43_arch_wifi_connect_bssid_blocking(const char *ssid, const uint8_t *bssid, const char *pw, uint32_t auth); - -/*! - * \brief Attempt to connect to a wireless access point, blocking until the network is joined, a failure is detected or a timeout occurs - * \ingroup pico_cyw43_arch - * - * \param ssid the network name to connect to - * \param pw the network password or NULL if there is no password required - * \param auth the authorization type to use when the password is enabled. Values are \ref CYW43_AUTH_WPA_TKIP_PSK, - * \ref CYW43_AUTH_WPA2_AES_PSK, or \ref CYW43_AUTH_WPA2_MIXED_PSK (see \ref CYW43_AUTH_) - * \param timeout how long to wait in milliseconds for a connection to succeed before giving up - * - * \return 0 if the initialization is successful, an error code otherwise \see pico_error_codes - */ -int cyw43_arch_wifi_connect_timeout_ms(const char *ssid, const char *pw, uint32_t auth, uint32_t timeout); - -/*! - * \brief Attempt to connect to a wireless access point specified by SSID and BSSID, blocking until the network is joined, a failure is detected or a timeout occurs - * \ingroup pico_cyw43_arch - * - * \param ssid the network name to connect to - * \param bssid the network BSSID to connect to or NULL if ignored - * \param pw the network password or NULL if there is no password required - * \param auth the authorization type to use when the password is enabled. Values are \ref CYW43_AUTH_WPA_TKIP_PSK, - * \ref CYW43_AUTH_WPA2_AES_PSK, or \ref CYW43_AUTH_WPA2_MIXED_PSK (see \ref CYW43_AUTH_) - * \param timeout how long to wait in milliseconds for a connection to succeed before giving up - * - * \return 0 if the initialization is successful, an error code otherwise \see pico_error_codes - */ -int cyw43_arch_wifi_connect_bssid_timeout_ms(const char *ssid, const uint8_t *bssid, const char *pw, uint32_t auth, uint32_t timeout); - -/*! - * \brief Start attempting to connect to a wireless access point - * \ingroup pico_cyw43_arch - * - * This method tells the CYW43 driver to start connecting to an access point. You should subsequently check the - * status by calling \ref cyw43_wifi_link_status. - * - * \param ssid the network name to connect to - * \param pw the network password or NULL if there is no password required - * \param auth the authorization type to use when the password is enabled. Values are \ref CYW43_AUTH_WPA_TKIP_PSK, - * \ref CYW43_AUTH_WPA2_AES_PSK, or \ref CYW43_AUTH_WPA2_MIXED_PSK (see \ref CYW43_AUTH_) - * - * \return 0 if the scan was started successfully, an error code otherwise \see pico_error_codes - */ -int cyw43_arch_wifi_connect_async(const char *ssid, const char *pw, uint32_t auth); - -/*! - * \brief Start attempting to connect to a wireless access point specified by SSID and BSSID - * \ingroup pico_cyw43_arch - * - * This method tells the CYW43 driver to start connecting to an access point. You should subsequently check the - * status by calling \ref cyw43_wifi_link_status. - * - * \param ssid the network name to connect to - * \param bssid the network BSSID to connect to or NULL if ignored - * \param pw the network password or NULL if there is no password required - * \param auth the authorization type to use when the password is enabled. Values are \ref CYW43_AUTH_WPA_TKIP_PSK, - * \ref CYW43_AUTH_WPA2_AES_PSK, or \ref CYW43_AUTH_WPA2_MIXED_PSK (see \ref CYW43_AUTH_) - * - * \return 0 if the scan was started successfully, an error code otherwise \see pico_error_codes - */ -int cyw43_arch_wifi_connect_bssid_async(const char *ssid, const uint8_t *bssid, const char *pw, uint32_t auth); - -/*! - * \brief Set a GPIO pin on the wireless chip to a given value - * \ingroup pico_cyw43_arch - * \note this method does not check for errors setting the GPIO. You can use the lower level \ref cyw43_gpio_set instead if you wish - * to check for errors. - * - * \param wl_gpio the GPIO number on the wireless chip - * \param value true to set the GPIO, false to clear it. - */ -void cyw43_arch_gpio_put(uint wl_gpio, bool value); - -/*! - * \brief Read the value of a GPIO pin on the wireless chip - * \ingroup pico_cyw43_arch - * \note this method does not check for errors setting the GPIO. You can use the lower level \ref cyw43_gpio_get instead if you wish - * to check for errors. - * - * \param wl_gpio the GPIO number on the wireless chip - * \return true if the GPIO is high, false otherwise - */ -bool cyw43_arch_gpio_get(uint wl_gpio); - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/pico-sdk/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_freertos.h b/pico-sdk/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_freertos.h deleted file mode 100644 index 1ba23ef..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_freertos.h +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef _PICO_CYW43_ARCH_ARCH_FREERTOS_H -#define _PICO_CYW43_ARCH_ARCH_FREERTOS_H - -// PICO_CONFIG: CYW43_TASK_STACK_SIZE, Stack size for the CYW43 FreeRTOS task in 4-byte words, type=int, default=1024, group=pico_cyw43_arch -#ifndef CYW43_TASK_STACK_SIZE -#define CYW43_TASK_STACK_SIZE 1024 -#endif - -// PICO_CONFIG: CYW43_TASK_PRIORITY, Priority for the CYW43 FreeRTOS task, type=int, default=tskIDLE_PRIORITY + 4, group=pico_cyw43_arch -#ifndef CYW43_TASK_PRIORITY -#define CYW43_TASK_PRIORITY (tskIDLE_PRIORITY + 4) -#endif - -#endif diff --git a/pico-sdk/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_poll.h b/pico-sdk/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_poll.h deleted file mode 100644 index a221917..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_poll.h +++ /dev/null @@ -1,12 +0,0 @@ -/* - * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef _PICO_CYW43_ARCH_ARCH_POLL_H -#define _PICO_CYW43_ARCH_ARCH_POLL_H - -// now obsolete; kept for backwards compatibility - -#endif diff --git a/pico-sdk/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_threadsafe_background.h b/pico-sdk/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_threadsafe_background.h deleted file mode 100644 index 005c15a..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_arch/include/pico/cyw43_arch/arch_threadsafe_background.h +++ /dev/null @@ -1,12 +0,0 @@ -/* - * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef _PICO_CYW43_ARCH_ARCH_THREADSAFE_BACKGROUND_H -#define _PICO_CYW43_ARCH_ARCH_THREADSAFE_BACKGROUND_H - -// now obsolete; kept for backwards compatibility - -#endif \ No newline at end of file diff --git a/pico-sdk/src/rp2_common/pico_cyw43_driver/CMakeLists.txt b/pico-sdk/src/rp2_common/pico_cyw43_driver/CMakeLists.txt deleted file mode 100644 index 4cdf95b..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_driver/CMakeLists.txt +++ /dev/null @@ -1,93 +0,0 @@ -if (DEFINED ENV{PICO_CYW43_DRIVER_PATH} AND (NOT PICO_CYW43_DRIVER_PATH)) - set(PICO_CYW43_DRIVER_PATH $ENV{PICO_CYW43_DRIVER_PATH}) - message("Using PICO_CYW43_DRIVER_PATH from environment ('${PICO_CYW43_DRIVER_PATH}')") -endif() - -set(CYW43_DRIVER_TEST_FILE "src/cyw43.h") - -if (NOT PICO_CYW43_DRIVER_PATH) - set(PICO_CYW43_DRIVER_PATH ${PICO_SDK_PATH}/lib/cyw43-driver) - if (PICO_CYW43_SUPPORTED AND NOT EXISTS ${PICO_CYW43_DRIVER_PATH}/${CYW43_DRIVER_TEST_FILE}) - message(WARNING "cyw43-driver submodule has not been initialized; Pico W wireless support will be unavailable -hint: try 'git submodule update --init' from your SDK directory (${PICO_SDK_PATH}).") - endif() -elseif (NOT EXISTS ${PICO_CYW43_DRIVER_PATH}/${CYW43_DRIVER_TEST_FILE}) - message(WARNING "PICO_CYW43_DRIVER_PATH specified but content not present.") -endif() - -if (EXISTS ${PICO_CYW43_DRIVER_PATH}/${CYW43_DRIVER_TEST_FILE}) - message("cyw43-driver available at ${PICO_CYW43_DRIVER_PATH}") - - add_subdirectory(cybt_shared_bus) - - pico_register_common_scope_var(PICO_CYW43_DRIVER_PATH) - - # base driver without our bus - pico_add_library(cyw43_driver NOFLAG) - target_sources(cyw43_driver INTERFACE - ${PICO_CYW43_DRIVER_PATH}/src/cyw43_ll.c - ${PICO_CYW43_DRIVER_PATH}/src/cyw43_stats.c - ${PICO_CYW43_DRIVER_PATH}/src/cyw43_lwip.c - ${PICO_CYW43_DRIVER_PATH}/src/cyw43_ctrl.c - ) - target_include_directories(cyw43_driver_headers INTERFACE - ${PICO_CYW43_DRIVER_PATH}/src - ${PICO_CYW43_DRIVER_PATH}/firmware - ) - - # pico_cyw43_driver adds async_context integration to cyw43_driver - pico_add_library(pico_cyw43_driver NOFLAG) - target_sources(pico_cyw43_driver INTERFACE - cyw43_driver.c) - target_include_directories(pico_cyw43_driver_headers INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) - pico_mirrored_target_link_libraries(pico_cyw43_driver INTERFACE cyw43_driver) - - # cyw43_driver_picow is cyw43_driver plus Pico W specific bus implementation - pico_add_library(cyw43_driver_picow NOFLAG) - target_sources(cyw43_driver_picow INTERFACE - ${CMAKE_CURRENT_LIST_DIR}/cyw43_bus_pio_spi.c - ) - pico_generate_pio_header(cyw43_driver_picow ${CMAKE_CURRENT_LIST_DIR}/cyw43_bus_pio_spi.pio) - pico_mirrored_target_link_libraries(cyw43_driver_picow INTERFACE - cyw43_driver - cybt_shared_bus - hardware_pio - hardware_dma - hardware_exception - ) - - # Note: This is used by MP, so check for issues when making changes - # e.g. Don't add new depenedences - pico_add_library(pico_btstack_hci_transport_cyw43 NOFLAG) - target_sources(pico_btstack_hci_transport_cyw43 INTERFACE - ${CMAKE_CURRENT_LIST_DIR}/btstack_hci_transport_cyw43.c - ${CMAKE_CURRENT_LIST_DIR}/btstack_chipset_cyw43.c - ) - target_include_directories(pico_btstack_hci_transport_cyw43_headers INTERFACE - ${CMAKE_CURRENT_LIST_DIR}/include - ) - target_compile_definitions(pico_btstack_hci_transport_cyw43_headers INTERFACE - CYW43_ENABLE_BLUETOOTH=1 - ) - - if (TARGET pico_btstack_base) - message("Pico W Bluetooth build support available.") - - pico_add_library(pico_btstack_cyw43) - target_sources(pico_btstack_cyw43 INTERFACE - ${CMAKE_CURRENT_LIST_DIR}/btstack_cyw43.c - ) - target_include_directories(pico_btstack_cyw43_headers INTERFACE - ${CMAKE_CURRENT_LIST_DIR}/include - ) - pico_mirrored_target_link_libraries(pico_btstack_cyw43 INTERFACE - pico_btstack_base - pico_btstack_flash_bank - pico_btstack_run_loop_async_context - pico_cyw43_arch - pico_btstack_hci_transport_cyw43 - ) - endif() - - pico_promote_common_scope_vars() -endif() diff --git a/pico-sdk/src/rp2_common/pico_cyw43_driver/btstack_chipset_cyw43.c b/pico-sdk/src/rp2_common/pico_cyw43_driver/btstack_chipset_cyw43.c deleted file mode 100644 index 2c481ed..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_driver/btstack_chipset_cyw43.c +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include "pico/btstack_chipset_cyw43.h" - -static void chipset_set_bd_addr_command(bd_addr_t addr, uint8_t *hci_cmd_buffer) { - hci_cmd_buffer[0] = 0x01; - hci_cmd_buffer[1] = 0xfc; - hci_cmd_buffer[2] = 0x06; - reverse_bd_addr(addr, &hci_cmd_buffer[3]); -} - -static const btstack_chipset_t btstack_chipset_cyw43 = { - .name = "CYW43", - .init = NULL, - .next_command = NULL, - .set_baudrate_command = NULL, - .set_bd_addr_command = chipset_set_bd_addr_command, -}; - -const btstack_chipset_t * btstack_chipset_cyw43_instance(void) { - return &btstack_chipset_cyw43; -} diff --git a/pico-sdk/src/rp2_common/pico_cyw43_driver/btstack_cyw43.c b/pico-sdk/src/rp2_common/pico_cyw43_driver/btstack_cyw43.c deleted file mode 100644 index 75061f1..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_driver/btstack_cyw43.c +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include "ble/le_device_db_tlv.h" -#include "classic/btstack_link_key_db_tlv.h" -#include "btstack_tlv.h" -#include "btstack_tlv_flash_bank.h" -#include "btstack_memory.h" -#include "hci.h" - -#if WANT_HCI_DUMP -#include "hci_dump.h" -#ifdef ENABLE_SEGGER_RTT -#include "hci_dump_segger_rtt_stdout.h" -#else -#include "hci_dump_embedded_stdout.h" -#endif -#endif - -#include "pico/btstack_hci_transport_cyw43.h" -#include "pico/btstack_run_loop_async_context.h" -#include "pico/btstack_flash_bank.h" -#include "pico/btstack_cyw43.h" - -static void setup_tlv(void) { - static btstack_tlv_flash_bank_t btstack_tlv_flash_bank_context; - const hal_flash_bank_t *hal_flash_bank_impl = pico_flash_bank_instance(); - - const btstack_tlv_t *btstack_tlv_impl = btstack_tlv_flash_bank_init_instance( - &btstack_tlv_flash_bank_context, - hal_flash_bank_impl, - NULL); - - // setup global TLV - btstack_tlv_set_instance(btstack_tlv_impl, &btstack_tlv_flash_bank_context); -#ifdef ENABLE_CLASSIC - const btstack_link_key_db_t *btstack_link_key_db = btstack_link_key_db_tlv_get_instance(btstack_tlv_impl, &btstack_tlv_flash_bank_context); - hci_set_link_key_db(btstack_link_key_db); -#endif -#ifdef ENABLE_BLE - // configure LE Device DB for TLV - le_device_db_tlv_configure(btstack_tlv_impl, &btstack_tlv_flash_bank_context); -#endif -} - -bool btstack_cyw43_init(async_context_t *context) { - // Initialise bluetooth - btstack_memory_init(); - btstack_run_loop_init(btstack_run_loop_async_context_get_instance(context)); - -#if WANT_HCI_DUMP -#ifdef ENABLE_SEGGER_RTT - hci_dump_init(hci_dump_segger_rtt_stdout_get_instance()); -#else - hci_dump_init(hci_dump_embedded_stdout_get_instance()); -#endif -#endif - - hci_init(hci_transport_cyw43_instance(), NULL); - - // setup TLV storage - setup_tlv(); - return true; -} - -void btstack_cyw43_deinit(__unused async_context_t *context) { - hci_power_control(HCI_POWER_OFF); - hci_close(); - btstack_run_loop_deinit(); - btstack_memory_deinit(); -} \ No newline at end of file diff --git a/pico-sdk/src/rp2_common/pico_cyw43_driver/btstack_hci_transport_cyw43.c b/pico-sdk/src/rp2_common/pico_cyw43_driver/btstack_hci_transport_cyw43.c deleted file mode 100644 index c0210b6..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_driver/btstack_hci_transport_cyw43.c +++ /dev/null @@ -1,159 +0,0 @@ -/* - * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include "pico.h" -#include "cyw43.h" -#include "hci_transport.h" -#include "hci.h" -#include "pico/btstack_hci_transport_cyw43.h" -#include "pico/btstack_chipset_cyw43.h" - -// assert outgoing pre-buffer for cyw43 header is available -#if !defined(HCI_OUTGOING_PRE_BUFFER_SIZE) || (HCI_OUTGOING_PRE_BUFFER_SIZE < 4) -#error HCI_OUTGOING_PRE_BUFFER_SIZE not defined or smaller than 4. Please update btstack_config.h -#endif - -// assert outgoing packet fragments are word aligned -#if !defined(HCI_ACL_CHUNK_SIZE_ALIGNMENT) || ((HCI_ACL_CHUNK_SIZE_ALIGNMENT & 3) != 0) -#error HCI_ACL_CHUNK_SIZE_ALIGNMENT not defined or not a multiply of 4. Please update btstack_config.h -#endif - -#define BT_DEBUG_ENABLED 0 -#if BT_DEBUG_ENABLED -#define BT_DEBUG(...) CYW43_PRINTF(__VA_ARGS__) -#else -#define BT_DEBUG(...) (void)0 -#endif - -// Callback when we have data -static void (*hci_transport_cyw43_packet_handler)(uint8_t packet_type, uint8_t *packet, uint16_t size) = NULL; - -// Incoming packet buffer - cyw43 packet header (incl packet type) + incoming pre buffer + max(acl header + acl payload, event header + event data) -__attribute__((aligned(4))) -static uint8_t hci_packet_with_pre_buffer[4 + HCI_INCOMING_PRE_BUFFER_SIZE + HCI_INCOMING_PACKET_BUFFER_SIZE ]; - -static btstack_data_source_t transport_data_source; -static bool hci_transport_ready; - -// Forward declaration -static void hci_transport_cyw43_process(void); - -static void hci_transport_data_source_process(btstack_data_source_t *ds, btstack_data_source_callback_type_t callback_type) { - assert(callback_type == DATA_SOURCE_CALLBACK_POLL); - assert(ds == &transport_data_source); - (void)callback_type; - (void)ds; - hci_transport_cyw43_process(); -} - -static void hci_transport_cyw43_init(const void *transport_config) { - UNUSED(transport_config); -} - -static int hci_transport_cyw43_open(void) { - int err = cyw43_bluetooth_hci_init(); - if (err != 0) { - CYW43_PRINTF("Failed to open cyw43 hci controller: %d\n", err); - return err; - } - - // OTP should be set in which case BT gets an address of wifi mac + 1 - // If OTP is not set for some reason BT gets set to 43:43:A2:12:1F:AC. - // So for safety, set the bluetooth device address here. - bd_addr_t addr; - cyw43_hal_get_mac(0, (uint8_t*)&addr); - addr[BD_ADDR_LEN - 1]++; - hci_set_chipset(btstack_chipset_cyw43_instance()); - hci_set_bd_addr(addr); - - btstack_run_loop_set_data_source_handler(&transport_data_source, &hci_transport_data_source_process); - btstack_run_loop_enable_data_source_callbacks(&transport_data_source, DATA_SOURCE_CALLBACK_POLL); - btstack_run_loop_add_data_source(&transport_data_source); - hci_transport_ready = true; - - return 0; -} - -static int hci_transport_cyw43_close(void) { - btstack_run_loop_disable_data_source_callbacks(&transport_data_source, DATA_SOURCE_CALLBACK_POLL); - btstack_run_loop_remove_data_source(&transport_data_source); - hci_transport_ready = false; - - return 0; -} - -static void hci_transport_cyw43_register_packet_handler(void (*handler)(uint8_t packet_type, uint8_t *packet, uint16_t size)) { - hci_transport_cyw43_packet_handler = handler; -} - -static int hci_transport_cyw43_can_send_now(uint8_t packet_type) { - UNUSED(packet_type); - return true; -} - -static int hci_transport_cyw43_send_packet(uint8_t packet_type, uint8_t *packet, int size) { - // store packet type before actual data and increase size - // This relies on HCI_OUTGOING_PRE_BUFFER_SIZE being set - uint8_t *buffer = &packet[-4]; - uint32_t buffer_size = size + 4; - buffer[3] = packet_type; - - CYW43_THREAD_ENTER - int err = cyw43_bluetooth_hci_write(buffer, buffer_size); - - if (err != 0) { - CYW43_PRINTF("Failed to send cyw43 hci packet: %d\n", err); - assert(false); - } else { - BT_DEBUG("bt sent %lu\n", buffer_size); - static uint8_t packet_sent_event[] = { HCI_EVENT_TRANSPORT_PACKET_SENT, 0}; - hci_transport_cyw43_packet_handler(HCI_EVENT_PACKET, &packet_sent_event[0], sizeof(packet_sent_event)); - } - CYW43_THREAD_EXIT - return err; -} - -// configure and return hci transport singleton -static const hci_transport_t hci_transport_cyw43 = { - /* const char * name; */ "CYW43", - /* void (*init) (const void *transport_config); */ &hci_transport_cyw43_init, - /* int (*open)(void); */ &hci_transport_cyw43_open, - /* int (*close)(void); */ &hci_transport_cyw43_close, - /* void (*register_packet_handler)(void (*handler)(...); */ &hci_transport_cyw43_register_packet_handler, - /* int (*can_send_packet_now)(uint8_t packet_type); */ &hci_transport_cyw43_can_send_now, - /* int (*send_packet)(...); */ &hci_transport_cyw43_send_packet, - /* int (*set_baudrate)(uint32_t baudrate); */ NULL, - /* void (*reset_link)(void); */ NULL, - /* void (*set_sco_config)(uint16_t voice_setting, int num_connections); */ NULL, -}; - -const hci_transport_t *hci_transport_cyw43_instance(void) { - return &hci_transport_cyw43; -} - -// Called to perform bt work from a data source -static void hci_transport_cyw43_process(void) { - CYW43_THREAD_LOCK_CHECK - uint32_t len = 0; - bool has_work; - do { - int err = cyw43_bluetooth_hci_read(hci_packet_with_pre_buffer, sizeof(hci_packet_with_pre_buffer), &len); - BT_DEBUG("bt in len=%lu err=%d\n", len, err); - if (err == 0 && len > 0) { - hci_transport_cyw43_packet_handler(hci_packet_with_pre_buffer[3], hci_packet_with_pre_buffer + 4, len - 4); - has_work = true; - } else { - has_work = false; - } - } while (has_work); -} - -// This is called from cyw43_poll_func. -void cyw43_bluetooth_hci_process(void) { - if (hci_transport_ready) { - btstack_run_loop_poll_data_sources_from_irq(); - } -} diff --git a/pico-sdk/src/rp2_common/pico_cyw43_driver/cybt_shared_bus/CMakeLists.txt b/pico-sdk/src/rp2_common/pico_cyw43_driver/cybt_shared_bus/CMakeLists.txt deleted file mode 100644 index fad69cd..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_driver/cybt_shared_bus/CMakeLists.txt +++ /dev/null @@ -1,23 +0,0 @@ -# cyw43 shared bus read and write -pico_add_library(cybt_shared_bus NOFLAG) - -target_sources(cybt_shared_bus INTERFACE - ${CMAKE_CURRENT_LIST_DIR}/cybt_shared_bus.c - ${CMAKE_CURRENT_LIST_DIR}/cybt_shared_bus_driver.c -) -target_include_directories(cybt_shared_bus_headers INTERFACE - ${CMAKE_CURRENT_LIST_DIR} -) - -# The BT firmware is supplied as a source file containing a static array with ascii hex data -# Set this to true to use this for testing -set(CYW43_USE_HEX_BTFW 0) -if (CYW43_USE_HEX_BTFW) - message("Warning: CYW43_USE_HEX_BTFW is true") - target_sources(cybt_shared_bus INTERFACE - ${PICO_CYW43_DRIVER_PATH}/firmware/cybt_firmware_43439.c - ) - target_compile_definitions(cybt_shared_bus INTERFACE - CYW43_USE_HEX_BTFW=1 - ) -endif() \ No newline at end of file diff --git a/pico-sdk/src/rp2_common/pico_cyw43_driver/cybt_shared_bus/cybt_shared_bus.c b/pico-sdk/src/rp2_common/pico_cyw43_driver/cybt_shared_bus/cybt_shared_bus.c deleted file mode 100644 index b464b3f..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_driver/cybt_shared_bus/cybt_shared_bus.c +++ /dev/null @@ -1,431 +0,0 @@ -/* - * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include -#include -#include -#include - -#include "cyw43_btbus.h" -#include "cyw43_ll.h" -#include "cyw43_config.h" -#include "cybt_shared_bus_driver.h" - -#include "cyw43_btfw_43439.h" - -#if CYW43_USE_HEX_BTFW -extern const char brcm_patch_version[]; -extern const uint8_t brcm_patchram_buf[]; -extern const int brcm_patch_ram_length; -#endif - -#define BTSDIO_FW_READY_POLLING_INTERVAL_MS (1) -#define BTSDIO_BT_AWAKE_POLLING_INTERVAL_MS (1) - -#define BTSDIO_FW_READY_POLLING_RETRY_COUNT (300) -#define BTSDIO_FW_AWAKE_POLLING_RETRY_COUNT (300) - -#define BTSDIO_FWBUF_OPER_DELAY_US (250) -#define BTFW_WAIT_TIME_MS (150) - -#define CYBT_DEBUG 0 -#define CYBT_VDEBUG 0 - -#if CYBT_DEBUG -#define cybt_debug(format,args...) printf("%d.%d: " format, (int)cyw43_hal_ticks_ms() / 1000, (int)cyw43_hal_ticks_ms() % 1000, ## args) -#else -#define cybt_debug(format, ...) ((void)0) -#endif -#define cybt_printf(format, args...) printf("%d.%d: " format, (int)cyw43_hal_ticks_ms() / 1000, (int)cyw43_hal_ticks_ms() % 1000, ## args) - -#define ROUNDUP(x, a) ((((x) + ((a) - 1)) / (a)) * (a)) -#define ROUNDDN(x, a) ((x) & ~((a) - 1)) -#define ISALIGNED(a, x) (((uint32_t)(a) & ((x) - 1)) == 0) - -#define CIRC_BUF_CNT(in, out) (((in) - (out)) & ((BTSDIO_FWBUF_SIZE)-1)) -#define CIRC_BUF_SPACE(in, out) CIRC_BUF_CNT((out), ((in) + 4)) - -typedef enum { - HCI_PACKET_TYPE_IGNORE = 0x00, - HCI_PACKET_TYPE_COMMAND = 0x01, - HCI_PACKET_TYPE_ACL = 0x02, - HCI_PACKET_TYPE_SCO = 0x03, - HCI_PACKET_TYPE_EVENT = 0x04, - HCI_PACKET_TYPE_DIAG = 0x07, - HCI_PACKET_TYPE_LOOPBACK = 0xFF -} hci_packet_type_t; - -static cybt_result_t cybt_fw_download_prepare(uint8_t **p_write_buf, uint8_t **p_hex_buf) { - *p_write_buf = NULL; - *p_hex_buf = NULL; - - *p_write_buf = cyw43_malloc(BTFW_DOWNLOAD_BLK_SIZE + BTFW_SD_ALIGN); - if (NULL == *p_write_buf) { - return CYBT_ERR_OUT_OF_MEMORY; - } - - *p_hex_buf = cyw43_malloc(BTFW_MAX_STR_LEN); - if (NULL == *p_hex_buf) { - cyw43_free(*p_write_buf); - return CYBT_ERR_OUT_OF_MEMORY; - } - - return CYBT_SUCCESS; -} - -static cybt_result_t cybt_fw_download_finish(uint8_t *p_write_buf, uint8_t *p_hex_buf) { - if (p_write_buf) { - cyw43_free(p_write_buf); - } - - if (p_hex_buf) { - cyw43_free(p_hex_buf); - } - - return CYBT_SUCCESS; -} - -static cybt_result_t cybt_wait_bt_ready(uint32_t max_polling_times) { - cyw43_delay_ms(BTFW_WAIT_TIME_MS); - do { - if (cybt_ready()) { - return CYBT_SUCCESS; - } - cyw43_delay_ms(BTSDIO_FW_READY_POLLING_INTERVAL_MS); - } while (max_polling_times--); - return CYBT_ERR_TIMEOUT; -} - -static cybt_result_t cybt_wait_bt_awake(uint32_t max_polling_times) { - do { - if (cybt_awake()) { - return CYBT_SUCCESS; - } - cyw43_delay_ms(BTSDIO_BT_AWAKE_POLLING_INTERVAL_MS); - } while (max_polling_times--); - return CYBT_ERR_TIMEOUT; -} - -int cyw43_btbus_init(cyw43_ll_t *self) { - cybt_result_t ret; - - uint8_t *p_write_buf = NULL; - uint8_t *p_hex_buf = NULL; - - cybt_sharedbus_driver_init(self); - - ret = cybt_fw_download_prepare(&p_write_buf, &p_hex_buf); - if (CYBT_SUCCESS != ret) { - cybt_printf("Could not allocate memory\n"); - return ret; - } - - cybt_debug("cybt_fw_download\n"); - const uint8_t *fw_data_buf; - uint32_t fw_data_len; -#if CYW43_USE_HEX_BTFW - cybt_printf("CYW43_USE_HEX_BTFW is true\n"); -#ifndef NDEBUG - cybt_printf("BT FW download, version = %s\n", brcm_patch_version); -#endif - fw_data_len = brcm_patch_ram_length; - fw_data_buf = brcm_patchram_buf; -#else - fw_data_len = cyw43_btfw_43439_len; - fw_data_buf = cyw43_btfw_43439; -#endif - ret = cybt_fw_download(fw_data_buf, - fw_data_len, - p_write_buf, - p_hex_buf - ); - - cybt_debug("cybt_fw_download_finish\n"); - cybt_fw_download_finish(p_write_buf, p_hex_buf); - - if (CYBT_SUCCESS != ret) { - cybt_printf("hci_open(): FW download failed (0x%x)\n", ret); - return CYBT_ERR_HCI_INIT_FAILED; - } - - cybt_debug("// cybt_wait_bt_ready\n"); - ret = cybt_wait_bt_ready(BTSDIO_FW_READY_POLLING_RETRY_COUNT); - assert(ret == CYBT_SUCCESS); - if (CYBT_SUCCESS == ret) { - cybt_debug("hci_open(): FW download successfully\n"); - } else { - cybt_printf("hci_open(): Failed to download FW\n"); - return CYBT_ERR_HCI_INIT_FAILED; - } - - ret = cybt_init_buffer(); - assert(ret == 0); - if (ret != 0) { - return ret; - } - ret = cybt_wait_bt_awake(BTSDIO_FW_AWAKE_POLLING_RETRY_COUNT); - assert(ret == 0); - if (ret != 0) { - return ret; - } - - cybt_set_host_ready(); - cybt_toggle_bt_intr(); - - return CYBT_SUCCESS; -} - -#if CYBT_VDEBUG -static void dump_bytes(const uint8_t *bptr, uint32_t len) { - unsigned int i = 0; - - for (i = 0; i < len; i++) { - if ((i & 0x07) == 0) { - printf("\n "); - } - printf("0x%02x", bptr[i]); - if (i != (len-1)) { - printf(", "); - } else { - } - } - printf("\n"); -} -#endif - -static cybt_result_t cybt_hci_write_buf(const uint8_t *p_data, uint32_t length) { - cybt_result_t ret_result = CYBT_SUCCESS; - cybt_fw_membuf_index_t fw_membuf_info = {0}; - - assert(ISALIGNED(p_data, 4)); - if (!ISALIGNED(p_data, 4)) { - cybt_printf("cybt_hci_write_hdr: buffer not aligned\n"); - return CYBT_ERR_BADARG; - } - - // total length including header - length = ROUNDUP(length, 4); - cybt_get_bt_buf_index(&fw_membuf_info); - uint32_t buf_space = CIRC_BUF_SPACE(fw_membuf_info.host2bt_in_val, fw_membuf_info.host2bt_out_val); - assert(length <= buf_space); // queue full? - if (length > buf_space) { - return CYBT_ERR_QUEUE_FULL; - } - - if (fw_membuf_info.host2bt_in_val + length <= BTSDIO_FWBUF_SIZE) { - // Don't need to wrap circular buf - cybt_debug("cybt_hci_write_hdr: 1-round write, len = %" PRId32 "\n", length); - cybt_mem_write_idx(H2B_BUF_ADDR_IDX, fw_membuf_info.host2bt_in_val, p_data, length); - fw_membuf_info.host2bt_in_val += length; - } else { - // Need to wrap circular buf - uint32_t first_write_len = BTSDIO_FWBUF_SIZE - fw_membuf_info.host2bt_in_val; - if (first_write_len >= 4) { - cybt_mem_write_idx(H2B_BUF_ADDR_IDX, fw_membuf_info.host2bt_in_val, p_data, first_write_len); - fw_membuf_info.host2bt_in_val += first_write_len; - } else { - first_write_len = 0; - } - uint32_t second_write_len = length - first_write_len; - cybt_debug("cybt_hci_write_hdr: 2-round write, 1st_len = %" PRId32 ", 2nd_len = %" PRId32 "\n", first_write_len, - second_write_len); - if (second_write_len > 0) { - cybt_mem_write_idx(H2B_BUF_ADDR_IDX, 0, p_data + first_write_len, second_write_len); - fw_membuf_info.host2bt_in_val += second_write_len; - } - } - - // Update circular buf pointer - const uint32_t new_h2b_in_val = fw_membuf_info.host2bt_in_val & (BTSDIO_FWBUF_SIZE - 1); - cybt_reg_write_idx(H2B_BUF_IN_ADDR_IDX, new_h2b_in_val); - - cybt_toggle_bt_intr(); - return ret_result; -} - -static cybt_result_t cybt_hci_read(uint8_t *p_data, uint32_t *p_length) { - cybt_result_t ret_result = CYBT_SUCCESS; - uint32_t fw_b2h_buf_count; - uint32_t new_b2h_out_val; - cybt_fw_membuf_index_t fw_membuf_info = {0}; - static uint32_t available = 0; - - assert(ISALIGNED(p_data, 4)); - if (!ISALIGNED(p_data, 4)) { - assert(false); - cybt_printf("cybt_hci_read: buffer not aligned\n"); - return CYBT_ERR_BADARG; - } - - uint32_t read_len = ROUNDUP(*p_length, 4); - - cybt_get_bt_buf_index(&fw_membuf_info); - fw_b2h_buf_count = CIRC_BUF_CNT(fw_membuf_info.bt2host_in_val, - fw_membuf_info.bt2host_out_val); - cybt_debug("cybt_hci_read: bt2host_in_val=%lu bt2host_out_val=%lu fw_b2h_buf_count=%ld\n", - fw_membuf_info.bt2host_in_val, fw_membuf_info.bt2host_out_val, fw_b2h_buf_count); - if (fw_b2h_buf_count < available) { - cybt_printf("error: cybt_hci_read buffer overflow fw_b2h_buf_count=%ld available=%lu\n", fw_b2h_buf_count, - available); - cybt_printf("error: cybt_hci_read bt2host_in_val=%lu bt2host_out_val=%lu\n", fw_membuf_info.bt2host_in_val, - fw_membuf_info.bt2host_out_val); - panic("cyw43 buffer overflow"); - } - - // No space in buffer - if (fw_b2h_buf_count == 0) { - *p_length = 0; - } else { - if (read_len > fw_b2h_buf_count) { - read_len = fw_b2h_buf_count; - } - - if (fw_membuf_info.bt2host_out_val + read_len <= BTSDIO_FWBUF_SIZE) { - // Don't need to wrap the circular buf - cybt_debug("cybt_hci_read: 1-round read, len = %" PRId32 "\n", read_len); - cybt_mem_read_idx(B2H_BUF_ADDR_IDX, fw_membuf_info.bt2host_out_val, p_data, read_len); - fw_membuf_info.bt2host_out_val += read_len; - } else { - // Need to wrap the circular buf - uint32_t first_read_len = BTSDIO_FWBUF_SIZE - fw_membuf_info.bt2host_out_val; - if (first_read_len >= 4) { - cybt_mem_read_idx(B2H_BUF_ADDR_IDX, fw_membuf_info.bt2host_out_val, p_data, first_read_len); - fw_membuf_info.bt2host_out_val += first_read_len; - } else { - first_read_len = 0; - } - uint32_t second_read_len = read_len - first_read_len; - cybt_debug("cybt_hci_read: 2-round read, 1st_len = %" PRId32 ", 2nd_len = %" PRId32 "\n", first_read_len, - second_read_len); - if (second_read_len > 0) { - cybt_mem_read_idx(B2H_BUF_ADDR_IDX, 0, p_data + first_read_len, second_read_len); - fw_membuf_info.bt2host_out_val += second_read_len; - } - } - available = fw_b2h_buf_count - read_len; // remember amount available to check for buffer overflow - - // Update pointer - new_b2h_out_val = fw_membuf_info.bt2host_out_val & (BTSDIO_FWBUF_SIZE - 1); - cybt_debug("cybt_hci_read new b2h_out = %" PRId32 "\n", new_b2h_out_val); - cybt_reg_write_idx(B2H_BUF_OUT_ADDR_IDX, new_b2h_out_val); - - // in case the real length is less than the requested one - *p_length = read_len; - } - cybt_toggle_bt_intr(); - return ret_result; -} - -static void cybt_bus_request(void) { - CYW43_THREAD_ENTER - // todo: Handle failure - cybt_result_t err = cybt_set_bt_awake(true); - assert(err == 0); - err = cybt_wait_bt_awake(BTSDIO_FW_AWAKE_POLLING_RETRY_COUNT); - assert(err == 0); - (void) err; -} - -static void cybt_bus_release(void) { - // mutex if using wifi - CYW43_THREAD_EXIT -} - -// Send the buffer which includes space for a 4 byte header at the start -// The last byte of the header should already be set to the packet type -int cyw43_btbus_write(uint8_t *buf, uint32_t size) { - uint16_t cmd_len = 0; - - // The size of the buffer should include a 4 byte header at the start - cmd_len = size - 4; //in BTSDIO, cmd_len does not include header length - - // Create payload starting with required headers - // Format: Cmd Len B0, Cmd Len B1, Cmd Len B2, HCI pckt type, Data - buf[0] = (uint8_t) (cmd_len & 0xFF); - buf[1] = (uint8_t) ((cmd_len & 0xFF00) >> 8); - buf[2] = 0; - - cybt_bus_request(); - - cybt_debug("cyw43_btbus_write: %d\n", cmd_len); -#if CYBT_VDEBUG - dump_bytes(buf, size); // dump header and data -#endif - - cybt_hci_write_buf(buf, size); - cybt_bus_release(); - - return 0; -} - -static bool cybt_hci_read_packet(uint8_t *buf, uint32_t max_buf_size, uint32_t *size) { - uint32_t total_read_len = 0; - uint32_t read_len = 0; - cybt_result_t bt_result; - - // Read the header into the first 4 bytes of the buffer - read_len = 4; //3 bytes BTSDIO packet length + 1 bytes PTI - bt_result = cybt_hci_read(buf, &read_len); - - if (bt_result != CYBT_SUCCESS) { - *size = 0; - cybt_printf("cybt_hci_read_packet: error %d", bt_result); - return true; - } - - if (read_len == 0) { - // No data is read from SPI - *size = 0; - cybt_debug("cybt_hci_read_packet: no data\n"); - return true; - } - - uint32_t hci_read_len = ((buf[2] << 16) & 0xFFFF00) | ((buf[1] << 8) & 0xFF00) | (buf[0] & 0xFF); - if (hci_read_len > max_buf_size - 4) { - *size = 0; - cybt_printf("cybt_hci_read_packet: too much data len %" PRId32"\n", hci_read_len); - assert(false); - return false; - } - total_read_len = hci_read_len; - - // Read the packet data after the header - cybt_debug("cybt_hci_read_packet: packet type 0x%" PRIx8 " len %" PRId32 "\n", buf[3], hci_read_len); - bt_result = cybt_hci_read(buf + 4, &total_read_len); - if (bt_result != CYBT_SUCCESS) { - *size = 0; - cybt_printf("cybt_hci_read_packet: read failed\n"); - assert(false); - return false; - } - - // Might read more because of alignment - if (total_read_len >= hci_read_len) { - assert(total_read_len == ROUNDUP(hci_read_len, 4)); // check if we're losing data? - *size = hci_read_len + 4; - } else { - assert(total_read_len > 0); - *size = total_read_len + 4; - cybt_printf("cybt_hci_read_packet: failed to read all data %lu < %lu\n", total_read_len, hci_read_len); - //assert(false); - return true; - } - - cybt_debug("cybt_hci_read_packet: %ld\n", *size); -#if CYBT_VDEBUG - dump_bytes(buf, *size); -#endif - - return true; -} - -// Reads the hci packet prepended with 4 byte header. The last header byte is the packet type -int cyw43_btbus_read(uint8_t *buf, uint32_t max_buf_size, uint32_t *size) { - cybt_bus_request(); - bool result = cybt_hci_read_packet(buf, max_buf_size, size); - cybt_bus_release(); - return result ? 0 : -1; -} diff --git a/pico-sdk/src/rp2_common/pico_cyw43_driver/cybt_shared_bus/cybt_shared_bus_driver.c b/pico-sdk/src/rp2_common/pico_cyw43_driver/cybt_shared_bus/cybt_shared_bus_driver.c deleted file mode 100644 index 04f7147..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_driver/cybt_shared_bus/cybt_shared_bus_driver.c +++ /dev/null @@ -1,721 +0,0 @@ -/* - * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include -#include -#include -#include - -#include "cyw43_ll.h" -#include "cybt_shared_bus_driver.h" - -// Bluetooth register corruption occurs if both wifi and bluetooth are fully utilised. -#define CYBT_CORRUPTION_TEST 1 -#if CYBT_CORRUPTION_TEST -static cybt_fw_membuf_index_t last_buf_index; -static uint32_t last_host_ctrl_reg; -static uint32_t last_bt_ctrl_reg; - -#include - -#endif - -#ifndef NDEBUG -#define cybt_printf(format, args ...) printf(format,##args) -#else -#define cybt_printf(...) -#endif - -#ifndef CYBT_DEBUG -#define CYBT_DEBUG 0 -#endif - -#if CYBT_DEBUG -#include -#define cybt_debug(format, args ...) printf(format,##args) -#else -#define cybt_debug(format, ...) ((void)0) -#endif - - -/****************************************************************************** - * Constants - ******************************************************************************/ -#define BTFW_MEM_OFFSET (0x19000000) - -/* BIT0 => WLAN Power UP and BIT1=> WLAN Wake */ -#define BT2WLAN_PWRUP_WAKE (0x03) -#define BT2WLAN_PWRUP_ADDR (0x640894)/* This address is specific to 43012B0 */ - -#define BTSDIO_OFFSET_HOST2BT_IN (0x00002000) -#define BTSDIO_OFFSET_HOST2BT_OUT (0x00002004) -#define BTSDIO_OFFSET_BT2HOST_IN (0x00002008) -#define BTSDIO_OFFSET_BT2HOST_OUT (0x0000200C) - -#define H2B_BUF_ADDR (buf_info.host2bt_buf_addr) -#define H2B_BUF_IN_ADDR (buf_info.host2bt_in_addr) -#define H2B_BUF_OUT_ADDR (buf_info.host2bt_out_addr) -#define B2H_BUF_ADDR (buf_info.bt2host_buf_addr) -#define B2H_BUF_IN_ADDR (buf_info.bt2host_in_addr) -#define B2H_BUF_OUT_ADDR (buf_info.bt2host_out_addr) - -static uint32_t wlan_ram_base_addr; -volatile uint32_t host_ctrl_cache_reg = 0; -#define WLAN_RAM_BASE_ADDR (wlan_ram_base_addr) - -// In wifi host driver these are all constants -#define BT_CTRL_REG_ADDR ((uint32_t)0x18000c7c) -#define HOST_CTRL_REG_ADDR ((uint32_t)0x18000d6c) -#define WLAN_RAM_BASE_REG_ADDR ((uint32_t)0x18000d68) - -typedef struct { - uint32_t host2bt_buf_addr; - uint32_t host2bt_in_addr; - uint32_t host2bt_out_addr; - uint32_t bt2host_buf_addr; - uint32_t bt2host_in_addr; - uint32_t bt2host_out_addr; -} cybt_fw_membuf_info_t; - -cybt_fw_membuf_info_t buf_info; - -#define BTFW_ADDR_MODE_UNKNOWN (0) -#define BTFW_ADDR_MODE_EXTENDED (1) -#define BTFW_ADDR_MODE_SEGMENT (2) -#define BTFW_ADDR_MODE_LINEAR32 (3) - -#define BTFW_HEX_LINE_TYPE_DATA (0) -#define BTFW_HEX_LINE_TYPE_END_OF_DATA (1) -#define BTFW_HEX_LINE_TYPE_EXTENDED_SEGMENT_ADDRESS (2) -#define BTFW_HEX_LINE_TYPE_EXTENDED_ADDRESS (4) -#define BTFW_HEX_LINE_TYPE_ABSOLUTE_32BIT_ADDRESS (5) - -#define BTSDIO_REG_DATA_VALID_BITMASK (1 << 1) -#define BTSDIO_REG_WAKE_BT_BITMASK (1 << 17) -#define BTSDIO_REG_SW_RDY_BITMASK (1 << 24) - -#define BTSDIO_REG_BT_AWAKE_BITMASK (1 << 8) -#define BTSDIO_REG_FW_RDY_BITMASK (1 << 24) - -#define BTSDIO_OFFSET_HOST_WRITE_BUF (0) -#define BTSDIO_OFFSET_HOST_READ_BUF BTSDIO_FWBUF_SIZE - -#define BTSDIO_FWBUF_OPER_DELAY_US (250) - -#define ROUNDUP(x, a) ((((x) + ((a) - 1)) / (a)) * (a)) -#define ROUNDDN(x, a) ((x) & ~((a) - 1)) -#define ISALIGNED(a, x) (((uint32_t)(a) & ((x) - 1)) == 0) - -typedef struct cybt_fw_cb { - const uint8_t *p_fw_mem_start; - uint32_t fw_len; - const uint8_t *p_next_line_start; -} cybt_fw_cb_t; - -typedef struct hex_file_data { - int addr_mode; - uint16_t hi_addr; - uint32_t dest_addr; - uint8_t *p_ds; -} hex_file_data_t; - -static cyw43_ll_t *cyw43_ll = NULL; - -static cybt_result_t cybt_reg_write(uint32_t reg_addr, uint32_t value); -static cybt_result_t cybt_reg_read(uint32_t reg_addr, uint32_t *p_value); -static cybt_result_t cybt_mem_write(uint32_t mem_addr, const uint8_t *p_data, uint32_t data_len); -static cybt_result_t cybt_mem_read(uint32_t mem_addr, uint8_t *p_data, uint32_t data_len); - -#if CYW43_USE_HEX_BTFW -const char *strnchr(const char *str, uint32_t len, int character) { - const char *end = str + len; - char c = (char)character; - do { - if (*str == c) { - return str; - } - } while (++str <= end); - return NULL; -} - -static uint32_t cybt_fw_hex_read_line(cybt_fw_cb_t *p_btfw_cb, - const char **p_line_start, - int len - ) { - uint32_t str_len = 0; - const char *p_str_end = NULL; - - if (NULL == p_btfw_cb || NULL == p_line_start) { - return str_len; - } - - *p_line_start = (const char *)p_btfw_cb->p_next_line_start; - p_str_end = strnchr(*p_line_start, len, '\n'); - if (p_str_end == NULL) { - return str_len; - } - - str_len = (uint32_t)(p_str_end - *p_line_start); - - /* Advance file pointer past the string length */ - p_btfw_cb->p_next_line_start += str_len + 1; - - return str_len; -} - -static inline uint8_t nibble_for_char(char c) { - if ((c >= '0') && (c <= '9')) return c - '0'; - if ((c >= 'A') && (c <= 'F')) return c - 'A' + 10; - return -1; -} - -static inline uint8_t read_hex_byte(const char *str) { - return nibble_for_char(*str) << 4 | nibble_for_char(*(str + 1)); -} - -static uint32_t read_hex(const char *str, int nchars) { - uint32_t result = 0; - assert(nchars > 0 && nchars <= 8 && nchars % 2 == 0); - for(int pos = 0; pos < nchars; pos += 2) { - result <<= 8; - result |= read_hex_byte(str + pos); - } - return result; -} - -static uint32_t cybt_fw_get_data(cybt_fw_cb_t *p_btfw_cb, - hex_file_data_t *hfd - ) { - uint32_t line_len; - uint16_t num_bytes, addr, data_pos, type, idx, octet; - uint32_t abs_base_addr32 = 0; - uint32_t data_len = 0; - const char *p_line_start = NULL; - - if (NULL == p_btfw_cb || NULL == hfd->p_ds) { - return data_len; - } - - while (data_len == 0) { - line_len = cybt_fw_hex_read_line(p_btfw_cb, &p_line_start, BTFW_MAX_STR_LEN); - if (line_len == 0) { - break; - } else if (line_len > 9) { - - num_bytes = (uint16_t)read_hex(p_line_start + 1, 2); - assert(num_bytes * 2 + 8 + 2 + 1 == line_len); - - int addr32 = read_hex(p_line_start + 3, 4); - assert(addr32 <= 0xffff); - addr = (uint16_t)addr32; - type = (uint16_t)read_hex(p_line_start + 7, 2); - assert(type <= 0xff); - - data_pos = 9; - - for (idx = 0; idx < num_bytes; idx++) - { - octet = (uint16_t)read_hex(p_line_start + data_pos, 2); - hfd->p_ds[idx] = (uint8_t)(octet & 0x00FF); - data_pos += 2; - } - - if (type == BTFW_HEX_LINE_TYPE_EXTENDED_ADDRESS) { - hfd->hi_addr = (hfd->p_ds[0] << 8) | hfd->p_ds[1]; - hfd->addr_mode = BTFW_ADDR_MODE_EXTENDED; - } else if (type == BTFW_HEX_LINE_TYPE_EXTENDED_SEGMENT_ADDRESS) { - hfd->hi_addr = (hfd->p_ds[0] << 8) | hfd->p_ds[1]; - hfd->addr_mode = BTFW_ADDR_MODE_SEGMENT; - } else if (type == BTFW_HEX_LINE_TYPE_ABSOLUTE_32BIT_ADDRESS) { - abs_base_addr32 = (hfd->p_ds[0] << 24) | (hfd->p_ds[1] << 16) | - (hfd->p_ds[2] << 8) | hfd->p_ds[3]; - hfd->addr_mode = BTFW_ADDR_MODE_LINEAR32; - } else if (type == BTFW_HEX_LINE_TYPE_DATA) { - hfd->dest_addr = addr; - - if (hfd->addr_mode == BTFW_ADDR_MODE_EXTENDED) { - hfd->dest_addr += (hfd->hi_addr << 16); - } else if (hfd->addr_mode == BTFW_ADDR_MODE_SEGMENT) { - hfd->dest_addr += (hfd->hi_addr << 4); - } else if (hfd->addr_mode == BTFW_ADDR_MODE_LINEAR32) { - hfd->dest_addr += abs_base_addr32; - } - - data_len = num_bytes; - } - } - } - - return data_len; -} -#else - -static uint32_t cybt_fw_get_data(cybt_fw_cb_t *p_btfw_cb, hex_file_data_t *hfd) { - uint32_t abs_base_addr32 = 0; - while (true) { - // 4 byte header - uint8_t num_bytes = *(p_btfw_cb->p_next_line_start)++; - uint16_t addr = *(p_btfw_cb->p_next_line_start)++ << 8; - addr |= *(p_btfw_cb->p_next_line_start)++; - uint8_t type = *(p_btfw_cb->p_next_line_start)++; - - // No data? - if (num_bytes == 0) break; - - // Copy the data - memcpy(hfd->p_ds, p_btfw_cb->p_next_line_start, num_bytes); - p_btfw_cb->p_next_line_start += num_bytes; - - // Adjust address based on type - if (type == BTFW_HEX_LINE_TYPE_EXTENDED_ADDRESS) { - hfd->hi_addr = (hfd->p_ds[0] << 8) | hfd->p_ds[1]; - hfd->addr_mode = BTFW_ADDR_MODE_EXTENDED; - } else if (type == BTFW_HEX_LINE_TYPE_EXTENDED_SEGMENT_ADDRESS) { - hfd->hi_addr = (hfd->p_ds[0] << 8) | hfd->p_ds[1]; - hfd->addr_mode = BTFW_ADDR_MODE_SEGMENT; - } else if (type == BTFW_HEX_LINE_TYPE_ABSOLUTE_32BIT_ADDRESS) { - abs_base_addr32 = (hfd->p_ds[0] << 24) | (hfd->p_ds[1] << 16) | - (hfd->p_ds[2] << 8) | hfd->p_ds[3]; - hfd->addr_mode = BTFW_ADDR_MODE_LINEAR32; - } else if (type == BTFW_HEX_LINE_TYPE_DATA) { - hfd->dest_addr = addr; - if (hfd->addr_mode == BTFW_ADDR_MODE_EXTENDED) { - hfd->dest_addr += (hfd->hi_addr << 16); - } else if (hfd->addr_mode == BTFW_ADDR_MODE_SEGMENT) { - hfd->dest_addr += (hfd->hi_addr << 4); - } else if (hfd->addr_mode == BTFW_ADDR_MODE_LINEAR32) { - hfd->dest_addr += abs_base_addr32; - } - return num_bytes; - } - } - return 0; -} - -#endif - -cybt_result_t cybt_fw_download(const uint8_t *p_bt_firmware, - uint32_t bt_firmware_len, - uint8_t *p_write_buf, - uint8_t *p_hex_buf) { - cybt_fw_cb_t btfw_cb; - hex_file_data_t hfd = {BTFW_ADDR_MODE_EXTENDED, 0, 0, NULL}; - uint8_t *p_mem_ptr; - uint32_t data_len; - - if (cyw43_ll == NULL) { - return CYBT_ERR_BADARG; - } - - if (NULL == p_bt_firmware || 0 == bt_firmware_len || NULL == p_write_buf || NULL == p_hex_buf) { - return CYBT_ERR_BADARG; - } - - // BT firmware starts with length of version string including a null terminator -#if !CYW43_USE_HEX_BTFW - uint8_t version_len = *p_bt_firmware; - assert(*(p_bt_firmware + version_len) == 0); -#ifndef NDEBUG - cybt_printf("BT FW download, version = %s\n", p_bt_firmware + 1); -#endif - p_bt_firmware += version_len + 1; // skip over version - p_bt_firmware += 1; // skip over record count -#endif - - p_mem_ptr = p_write_buf; - if ((uint32_t) (uintptr_t) p_mem_ptr % BTFW_SD_ALIGN) { - p_mem_ptr += (BTFW_SD_ALIGN - ((uint32_t) (uintptr_t) p_mem_ptr % BTFW_SD_ALIGN)); - } - - hfd.p_ds = p_hex_buf; - - btfw_cb.p_fw_mem_start = p_bt_firmware; - btfw_cb.fw_len = bt_firmware_len; - btfw_cb.p_next_line_start = p_bt_firmware; - - cybt_reg_write(BTFW_MEM_OFFSET + BT2WLAN_PWRUP_ADDR, BT2WLAN_PWRUP_WAKE); - - while ((data_len = cybt_fw_get_data(&btfw_cb, &hfd)) > 0) { - uint32_t fwmem_start_addr, fwmem_start_data, fwmem_end_addr, fwmem_end_data; - uint32_t write_data_len, idx, pad; - - fwmem_start_addr = BTFW_MEM_OFFSET + hfd.dest_addr; - write_data_len = 0; - - /** - * Make sure the start address is 4 byte aligned to avoid alignment issues - * with SD host controllers - */ - if (!ISALIGNED(fwmem_start_addr, 4)) { - pad = fwmem_start_addr % 4; - fwmem_start_addr = ROUNDDN(fwmem_start_addr, 4); - - cybt_mem_read(fwmem_start_addr, (uint8_t *) &fwmem_start_data, sizeof(uint32_t)); - - for (idx = 0; idx < pad; idx++, write_data_len++) { - p_mem_ptr[write_data_len] = (uint8_t) ((uint8_t *) &fwmem_start_data)[idx]; - } - } - memcpy(&(p_mem_ptr[write_data_len]), hfd.p_ds, data_len); - write_data_len += data_len; - - /** - * Make sure the length is multiple of 4bytes to avoid alignment issues - * with SD host controllers - */ - fwmem_end_addr = fwmem_start_addr + write_data_len; - if (!ISALIGNED(fwmem_end_addr, 4)) { - cybt_mem_read(ROUNDDN(fwmem_end_addr, 4), (uint8_t *) &fwmem_end_data, sizeof(uint32_t)); - for (idx = (fwmem_end_addr % 4); idx < 4; idx++, write_data_len++) { - p_mem_ptr[write_data_len] = (uint8_t) ((uint8_t *) &fwmem_end_data)[idx]; - } - } - - /* - * write ram - */ - if (((fwmem_start_addr & 0xFFF) + write_data_len) <= 0x1000) { - cybt_mem_write(fwmem_start_addr, p_mem_ptr, write_data_len); - } else { - uint32_t first_write_len = 0x1000 - (fwmem_start_addr & 0xFFF); - cybt_mem_write(fwmem_start_addr, p_mem_ptr, first_write_len); - cybt_mem_write(fwmem_start_addr + first_write_len, - p_mem_ptr + first_write_len, - write_data_len - first_write_len); - } - } - - return CYBT_SUCCESS; -} - -cybt_result_t cybt_set_host_ready(void) { - uint32_t reg_val; - - cybt_reg_read(HOST_CTRL_REG_ADDR, ®_val); - reg_val |= BTSDIO_REG_SW_RDY_BITMASK; - cybt_reg_write(HOST_CTRL_REG_ADDR, reg_val); -#if CYBT_CORRUPTION_TEST - last_host_ctrl_reg = reg_val; -#endif - return CYBT_SUCCESS; -} - -cybt_result_t cybt_toggle_bt_intr(void) { - uint32_t reg_val, new_val; - - cybt_reg_read(HOST_CTRL_REG_ADDR, ®_val); -#if CYBT_CORRUPTION_TEST - if ((reg_val & ~(BTSDIO_REG_SW_RDY_BITMASK | BTSDIO_REG_WAKE_BT_BITMASK | BTSDIO_REG_DATA_VALID_BITMASK)) != 0) { - cybt_printf("cybt_toggle_bt_intr read HOST_CTRL_REG_ADDR as 0x%08lx\n", reg_val); - cybt_debug_dump(); - panic("cyw43 btsdio register corruption"); - } - assert((reg_val & ~(BTSDIO_REG_SW_RDY_BITMASK | BTSDIO_REG_WAKE_BT_BITMASK | BTSDIO_REG_DATA_VALID_BITMASK)) == 0); -#endif - new_val = reg_val ^ BTSDIO_REG_DATA_VALID_BITMASK; - cybt_reg_write(HOST_CTRL_REG_ADDR, new_val); -#if CYBT_CORRUPTION_TEST - last_host_ctrl_reg = new_val; -#endif - return CYBT_SUCCESS; -} - -cybt_result_t cybt_set_bt_intr(int value) { - uint32_t reg_val, new_val; - - cybt_reg_read(HOST_CTRL_REG_ADDR, ®_val); - if (value) { - new_val = reg_val | BTSDIO_REG_DATA_VALID_BITMASK; - } else { - new_val = reg_val & ~BTSDIO_REG_DATA_VALID_BITMASK; - } - cybt_reg_write(HOST_CTRL_REG_ADDR, new_val); -#if CYBT_CORRUPTION_TEST - last_host_ctrl_reg = new_val; -#endif - return CYBT_SUCCESS; -} - -int cybt_ready(void) { - uint32_t reg_val; - cybt_reg_read(BT_CTRL_REG_ADDR, ®_val); -#if CYBT_CORRUPTION_TEST - if (reg_val & BTSDIO_REG_FW_RDY_BITMASK) { - last_bt_ctrl_reg = reg_val; - } -#endif - return (reg_val & BTSDIO_REG_FW_RDY_BITMASK) ? 1 : 0; -} - -int cybt_awake(void) { - uint32_t reg_val; - cybt_reg_read(BT_CTRL_REG_ADDR, ®_val); -#if CYBT_CORRUPTION_TEST - if (reg_val & BTSDIO_REG_BT_AWAKE_BITMASK) { - last_bt_ctrl_reg = reg_val; - } -#endif - return (reg_val & BTSDIO_REG_BT_AWAKE_BITMASK) ? 1 : 0; -} - -cybt_result_t cybt_set_bt_awake(int value) { - uint32_t reg_val_before; - cybt_reg_read(HOST_CTRL_REG_ADDR, ®_val_before); - - uint32_t reg_val_after = reg_val_before; - if (value) - reg_val_after |= BTSDIO_REG_WAKE_BT_BITMASK; - else - reg_val_after &= ~BTSDIO_REG_WAKE_BT_BITMASK; - - if (reg_val_before != reg_val_after) { - cybt_reg_write(HOST_CTRL_REG_ADDR, reg_val_after); -#if CYBT_CORRUPTION_TEST - last_host_ctrl_reg = reg_val_after; -#endif - } - return 0; -} - -void cybt_debug_dump(void) { -#if CYBT_CORRUPTION_TEST - uint32_t reg_val = 0; - cybt_fw_membuf_index_t buf_index; - - cybt_printf("WLAN_RAM_BASE_ADDR: 0x%08lx\n", WLAN_RAM_BASE_ADDR); - cybt_printf("H2B_BUF_ADDR: 0x%08lx\n", H2B_BUF_ADDR); - cybt_printf("B2H_BUF_ADDR: 0x%08lx\n", B2H_BUF_ADDR); - - cybt_reg_read(H2B_BUF_IN_ADDR, &buf_index.host2bt_in_val); - cybt_printf("H2B_BUF_IN_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", H2B_BUF_IN_ADDR, buf_index.host2bt_in_val, - last_buf_index.host2bt_in_val); - - cybt_reg_read(H2B_BUF_OUT_ADDR, &buf_index.host2bt_out_val); - cybt_printf("H2B_BUF_OUT_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", H2B_BUF_OUT_ADDR, buf_index.host2bt_out_val, - last_buf_index.host2bt_out_val); - - cybt_reg_read(B2H_BUF_IN_ADDR, &buf_index.bt2host_in_val); - cybt_printf("B2H_BUF_IN_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", B2H_BUF_IN_ADDR, buf_index.bt2host_in_val, - last_buf_index.bt2host_in_val); - - cybt_reg_read(B2H_BUF_OUT_ADDR, &buf_index.bt2host_out_val); - cybt_printf("B2H_BUF_OUT_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", B2H_BUF_OUT_ADDR, buf_index.bt2host_out_val, - last_buf_index.bt2host_out_val); - - cybt_reg_read(HOST_CTRL_REG_ADDR, ®_val); - cybt_printf("HOST_CTRL_REG_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", HOST_CTRL_REG_ADDR, reg_val, - last_host_ctrl_reg); - - cybt_reg_read(BT_CTRL_REG_ADDR, ®_val); - cybt_printf("BT_CTRL_REG_ADDR: 0x%08lx = 0x%08lx (last 0x%08lx)\n", BT_CTRL_REG_ADDR, reg_val, last_bt_ctrl_reg); -#endif -} - -cybt_result_t cybt_get_bt_buf_index(cybt_fw_membuf_index_t *p_buf_index) { - uint32_t buf[4]; - - cybt_mem_read(H2B_BUF_IN_ADDR, (uint8_t *) buf, sizeof(buf)); - - p_buf_index->host2bt_in_val = buf[0]; - p_buf_index->host2bt_out_val = buf[1]; - p_buf_index->bt2host_in_val = buf[2]; - p_buf_index->bt2host_out_val = buf[3]; - - cybt_debug("cybt_get_bt_buf_index: h2b_in = 0x%08lx, h2b_out = 0x%08lx, b2h_in = 0x%08lx, b2h_out = 0x%08lx\n", - p_buf_index->host2bt_in_val, - p_buf_index->host2bt_out_val, - p_buf_index->bt2host_in_val, - p_buf_index->bt2host_out_val); - -#if CYBT_CORRUPTION_TEST - if (p_buf_index->host2bt_in_val >= BTSDIO_FWBUF_SIZE || p_buf_index->host2bt_out_val >= BTSDIO_FWBUF_SIZE || - p_buf_index->bt2host_in_val >= BTSDIO_FWBUF_SIZE || p_buf_index->bt2host_out_val >= BTSDIO_FWBUF_SIZE) { - cybt_printf("cybt_get_bt_buf_index invalid buffer value\n"); - cybt_debug_dump(); - } else { - memcpy((uint8_t *) &last_buf_index, (uint8_t *) p_buf_index, sizeof(cybt_fw_membuf_index_t)); - } -#endif - - assert(p_buf_index->host2bt_in_val < BTSDIO_FWBUF_SIZE); - assert(p_buf_index->host2bt_out_val < BTSDIO_FWBUF_SIZE); - assert(p_buf_index->bt2host_in_val < BTSDIO_FWBUF_SIZE); - assert(p_buf_index->bt2host_out_val < BTSDIO_FWBUF_SIZE); - - return CYBT_SUCCESS; -} - -static cybt_result_t cybt_reg_write(uint32_t reg_addr, uint32_t value) { - cybt_debug("cybt_reg_write 0x%08lx 0x%08lx\n", reg_addr, value); - cyw43_ll_write_backplane_reg(cyw43_ll, reg_addr, value); - if (reg_addr == HOST_CTRL_REG_ADDR) { - host_ctrl_cache_reg = value; - } - return CYBT_SUCCESS; -} - -static cybt_result_t cybt_reg_read(uint32_t reg_addr, uint32_t *p_value) { - if (reg_addr == HOST_CTRL_REG_ADDR) { - *p_value = host_ctrl_cache_reg; - return CYBT_SUCCESS; - } - *p_value = cyw43_ll_read_backplane_reg(cyw43_ll, reg_addr); - cybt_debug("cybt_reg_read 0x%08lx == 0x%08lx\n", reg_addr, *p_value); - return CYBT_SUCCESS; -} - -#if CYBT_DEBUG -static void dump_bytes(const uint8_t *bptr, uint32_t len) { - unsigned int i = 0; - - for (i = 0; i < len; i++) { - if ((i & 0x07) == 0) { - cybt_debug("\n "); - } - cybt_debug("0x%02x", bptr[i]); - if (i != (len - 1)) { - cybt_debug(", "); - } - } - cybt_debug("\n"); -} -#define DUMP_BYTES dump_bytes -#else -#define DUMP_BYTES(...) -#endif - -static cybt_result_t cybt_mem_write(uint32_t mem_addr, const uint8_t *p_data, uint32_t data_len) { - cybt_debug("cybt_mem_write addr 0x%08lx len %ld\n", mem_addr, data_len); - do { - uint32_t transfer_size = (data_len > CYW43_BUS_MAX_BLOCK_SIZE) ? CYW43_BUS_MAX_BLOCK_SIZE : data_len; - if ((mem_addr & 0xFFF) + transfer_size > 0x1000) { - transfer_size = 0x1000 - (mem_addr & 0xFFF); - } - cyw43_ll_write_backplane_mem(cyw43_ll, mem_addr, transfer_size, p_data); - cybt_debug(" write_mem addr 0x%08lx len %ld\n", mem_addr, transfer_size); - DUMP_BYTES(p_data, transfer_size); - data_len -= transfer_size; - p_data += transfer_size; - mem_addr += transfer_size; - } while (data_len > 0); - return CYBT_SUCCESS; -} - -static cybt_result_t cybt_mem_read(uint32_t mem_addr, uint8_t *p_data, uint32_t data_len) { - assert(data_len >= 4); - cybt_debug("cybt_mem_read addr 0x%08lx len %ld\n", mem_addr, data_len); - do { - uint32_t transfer_size = (data_len > CYW43_BUS_MAX_BLOCK_SIZE) ? CYW43_BUS_MAX_BLOCK_SIZE : data_len; - if ((mem_addr & 0xFFF) + transfer_size > 0x1000) { - transfer_size = 0x1000 - (mem_addr & 0xFFF); - } - cyw43_ll_read_backplane_mem(cyw43_ll, mem_addr, transfer_size, p_data); - cybt_debug(" read_mem addr 0x%08lx len %ld\n", transfer_size, mem_addr); - DUMP_BYTES(p_data, transfer_size); - data_len -= transfer_size; - p_data += transfer_size; - mem_addr += transfer_size; - } while (data_len > 0); - return CYBT_SUCCESS; -} - -static uint32_t cybt_get_addr(cybt_addr_idx_t addr_idx) { - uint32_t addr = 0; - - switch (addr_idx) { - case H2B_BUF_ADDR_IDX: - addr = H2B_BUF_ADDR; - break; - case H2B_BUF_IN_ADDR_IDX: - addr = H2B_BUF_IN_ADDR; - break; - case H2B_BUF_OUT_ADDR_IDX: - addr = H2B_BUF_OUT_ADDR; - break; - case B2H_BUF_ADDR_IDX: - addr = B2H_BUF_ADDR; - break; - case B2H_BUF_IN_ADDR_IDX: - addr = B2H_BUF_IN_ADDR; - break; - case B2H_BUF_OUT_ADDR_IDX: - addr = B2H_BUF_OUT_ADDR; - break; - default: - assert(0); - break; - } - return addr; -} - -cybt_result_t cybt_reg_write_idx(cybt_addr_idx_t reg_idx, uint32_t value) { - assert(reg_idx == H2B_BUF_IN_ADDR_IDX || reg_idx == B2H_BUF_OUT_ADDR_IDX); - assert(value < BTSDIO_FWBUF_SIZE); // writing out of bounds register value? - if ((reg_idx != H2B_BUF_IN_ADDR_IDX && reg_idx != B2H_BUF_OUT_ADDR_IDX) || value >= BTSDIO_FWBUF_SIZE) { - assert(0); - return CYBT_ERR_BADARG; - } - uint32_t reg_addr = cybt_get_addr(reg_idx); - return cybt_reg_write(reg_addr, value); -} - -cybt_result_t cybt_mem_write_idx(cybt_addr_idx_t mem_idx, uint32_t offset, const uint8_t *p_data, uint32_t data_len) { - assert(mem_idx == H2B_BUF_ADDR_IDX); // caller should only be writing to here? - assert(offset + data_len <= BTSDIO_FWBUF_SIZE); // writing out of bounds? - if (mem_idx != H2B_BUF_ADDR_IDX || (offset + data_len) > BTSDIO_FWBUF_SIZE) { - assert(0); - return CYBT_ERR_BADARG; - } - if (!ISALIGNED(p_data, 4)) { - return CYBT_ERR_BADARG; - } - uint32_t mem_addr = cybt_get_addr(mem_idx) + offset; - return cybt_mem_write(mem_addr, p_data, data_len); -} - -cybt_result_t cybt_mem_read_idx(cybt_addr_idx_t mem_idx, uint32_t offset, uint8_t *p_data, uint32_t data_len) { - assert(mem_idx == B2H_BUF_ADDR_IDX); // caller should only be reading from here? - assert(offset + data_len <= BTSDIO_FWBUF_SIZE); // reading out of bounds? - if (mem_idx != B2H_BUF_ADDR_IDX || (offset + data_len) > BTSDIO_FWBUF_SIZE) { - assert(0); - return CYBT_ERR_BADARG; - } - uint32_t mem_addr = cybt_get_addr(mem_idx) + offset; - return cybt_mem_read(mem_addr, p_data, data_len); -} - -cybt_result_t cybt_init_buffer(void) { - int result; - result = cybt_reg_read(WLAN_RAM_BASE_REG_ADDR, &WLAN_RAM_BASE_ADDR); - if (CYBT_SUCCESS != result) { - return result; - } - - cybt_debug("hci_open(): btfw ram base = 0x%" PRIx32 "\n", WLAN_RAM_BASE_ADDR); - - // Fill in reg info - // Data buffers - H2B_BUF_ADDR = WLAN_RAM_BASE_ADDR + BTSDIO_OFFSET_HOST_WRITE_BUF; - B2H_BUF_ADDR = WLAN_RAM_BASE_ADDR + BTSDIO_OFFSET_HOST_READ_BUF; - - // circular buffer indexes - H2B_BUF_IN_ADDR = WLAN_RAM_BASE_ADDR + BTSDIO_OFFSET_HOST2BT_IN; - H2B_BUF_OUT_ADDR = WLAN_RAM_BASE_ADDR + BTSDIO_OFFSET_HOST2BT_OUT; - B2H_BUF_IN_ADDR = WLAN_RAM_BASE_ADDR + BTSDIO_OFFSET_BT2HOST_IN; - B2H_BUF_OUT_ADDR = WLAN_RAM_BASE_ADDR + BTSDIO_OFFSET_BT2HOST_OUT; - - uint32_t reg_val = 0; - cybt_reg_write(H2B_BUF_IN_ADDR, reg_val); - cybt_reg_write(H2B_BUF_OUT_ADDR, reg_val); - cybt_reg_write(B2H_BUF_IN_ADDR, reg_val); - cybt_reg_write(B2H_BUF_OUT_ADDR, reg_val); - - return CYBT_SUCCESS; -} - -void cybt_sharedbus_driver_init(cyw43_ll_t *driver) { - cyw43_ll = driver; -} diff --git a/pico-sdk/src/rp2_common/pico_cyw43_driver/cybt_shared_bus/cybt_shared_bus_driver.h b/pico-sdk/src/rp2_common/pico_cyw43_driver/cybt_shared_bus/cybt_shared_bus_driver.h deleted file mode 100644 index c5e6e99..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_driver/cybt_shared_bus/cybt_shared_bus_driver.h +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef CYBT_SHARE_BUS_DRIVER_H -#define CYBT_SHARE_BUS_DRIVER_H - -#define BTSDIO_FWBUF_SIZE (0x1000) -#define BTFW_MAX_STR_LEN (600) -#define BTFW_SD_ALIGN (32) -#define BTFW_DOWNLOAD_BLK_SIZE (((BTFW_MAX_STR_LEN) / 2) + 8) - -typedef enum { - CYBT_SUCCESS = 0, - CYBT_ERR_BADARG = 0xB1, - CYBT_ERR_OUT_OF_MEMORY, - CYBT_ERR_TIMEOUT, - CYBT_ERR_HCI_INIT_FAILED, - CYBT_ERR_HCI_UNSUPPORTED_IF, - CYBT_ERR_HCI_UNSUPPORTED_BAUDRATE, - CYBT_ERR_HCI_NOT_INITIALIZE, - CYBT_ERR_HCI_WRITE_FAILED, - CYBT_ERR_HCI_READ_FAILED, - CYBT_ERR_HCI_GET_TX_MUTEX_FAILED, - CYBT_ERR_HCI_GET_RX_MUTEX_FAILED, - CYBT_ERR_HCI_SET_BAUDRATE_FAILED, - CYBT_ERR_HCI_SET_FLOW_CTRL_FAILED, - CYBT_ERR_INIT_MEMPOOL_FAILED, - CYBT_ERR_INIT_QUEUE_FAILED, - CYBT_ERR_CREATE_TASK_FAILED, - CYBT_ERR_SEND_QUEUE_FAILED, - CYBT_ERR_MEMPOOL_NOT_INITIALIZE, - CYBT_ERR_QUEUE_ALMOST_FULL, - CYBT_ERR_QUEUE_FULL, - CYBT_ERR_GPIO_POWER_INIT_FAILED, - CYBT_ERR_GPIO_DEV_WAKE_INIT_FAILED, - CYBT_ERR_GPIO_HOST_WAKE_INIT_FAILED, - CYBT_ERR_GENERIC -} cybt_result_t; - -typedef enum { - H2B_BUF_ADDR_IDX = 0x10, - H2B_BUF_IN_ADDR_IDX, - H2B_BUF_OUT_ADDR_IDX, - B2H_BUF_ADDR_IDX, - B2H_BUF_IN_ADDR_IDX, - B2H_BUF_OUT_ADDR_IDX, -} cybt_addr_idx_t; - -typedef struct { - uint32_t host2bt_in_val; - uint32_t host2bt_out_val; - uint32_t bt2host_in_val; - uint32_t bt2host_out_val; -} cybt_fw_membuf_index_t; - -struct _cyw43_ll_t; -void cybt_sharedbus_driver_init(struct _cyw43_ll_t *driver); - -cybt_result_t cybt_init_buffer(void); - -cybt_result_t cybt_reg_write_idx(cybt_addr_idx_t reg_idx, uint32_t value); -cybt_result_t cybt_mem_write_idx(cybt_addr_idx_t mem_idx, uint32_t offset, const uint8_t *p_data, uint32_t data_len); -cybt_result_t cybt_mem_read_idx(cybt_addr_idx_t mem_idx, uint32_t offset, uint8_t *p_data, uint32_t data_len); - -cybt_result_t cybt_fw_download(const uint8_t *p_bt_firmware, uint32_t bt_firmware_len, uint8_t *p_write_buf, uint8_t *p_hex_buf); - -int cybt_ready(void); -int cybt_awake(void); - -cybt_result_t cybt_set_bt_awake(int value); -cybt_result_t cybt_set_host_ready(void); -cybt_result_t cybt_set_bt_intr(int value); -cybt_result_t cybt_toggle_bt_intr(void); -cybt_result_t cybt_get_bt_buf_index(cybt_fw_membuf_index_t *p_buf_index); - -void cybt_debug_dump(void); - -#endif diff --git a/pico-sdk/src/rp2_common/pico_cyw43_driver/cyw43_bus_pio_spi.c b/pico-sdk/src/rp2_common/pico_cyw43_driver/cyw43_bus_pio_spi.c deleted file mode 100644 index 5afe85e..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_driver/cyw43_bus_pio_spi.c +++ /dev/null @@ -1,548 +0,0 @@ -/* - * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include -#include -#include - -#include "hardware/gpio.h" -#include "hardware/pio.h" -#include "hardware/clocks.h" -#include "hardware/sync.h" -#include "hardware/dma.h" -#include "cyw43_bus_pio_spi.pio.h" -#include "cyw43.h" -#include "cyw43_internal.h" -#include "cyw43_spi.h" -#include "cyw43_debug_pins.h" - -#if CYW43_SPI_PIO -#define WL_REG_ON 23 -#define DATA_OUT_PIN 24u -#define DATA_IN_PIN 24u -#define IRQ_PIN 24u -// #define MONITOR_PIN 3u -#define CLOCK_PIN 29u -#define CS_PIN 25u -#define IRQ_SAMPLE_DELAY_NS 100 - -#define SPI_PROGRAM_NAME spi_gap01_sample0 -#define SPI_PROGRAM_FUNC __CONCAT(SPI_PROGRAM_NAME, _program) -#define SPI_PROGRAM_GET_DEFAULT_CONFIG_FUNC __CONCAT(SPI_PROGRAM_NAME, _program_get_default_config) -#define SPI_OFFSET_END __CONCAT(SPI_PROGRAM_NAME, _offset_end) -#define SPI_OFFSET_LP1_END __CONCAT(SPI_PROGRAM_NAME, _offset_lp1_end) - -#define CLOCK_DIV 2 -#define CLOCK_DIV_MINOR 0 -#define PADS_DRIVE_STRENGTH PADS_BANK0_GPIO0_DRIVE_VALUE_12MA - -#if !CYW43_USE_SPI -#error CYW43_USE_SPI should be true -#endif - -#ifndef NDEBUG -//#define ENABLE_SPI_DUMPING 1 -#endif - -// Set to 1 to enable -#if ENABLE_SPI_DUMPING //NDEBUG -#if 0 -#define DUMP_SPI_TRANSACTIONS(A) A -#else -static bool enable_spi_packet_dumping; // set to true to dump -#define DUMP_SPI_TRANSACTIONS(A) if (enable_spi_packet_dumping) {A} -#endif - -static uint32_t counter = 0; -#else -#define DUMP_SPI_TRANSACTIONS(A) -#endif - -//#define SWAP32(A) ((((A) & 0xff000000U) >> 8) | (((A) & 0xff0000U) << 8) | (((A) & 0xff00U) >> 8) | (((A) & 0xffU) << 8)) -__force_inline static uint32_t __swap16x2(uint32_t a) { - pico_default_asm ("rev16 %0, %0" : "+l" (a) : : ); - return a; -} -#define SWAP32(a) __swap16x2(a) - -#ifndef CYW43_SPI_PIO_PREFERRED_PIO -#define CYW43_SPI_PIO_PREFERRED_PIO 1 -#endif -static_assert(CYW43_SPI_PIO_PREFERRED_PIO >=0 && CYW43_SPI_PIO_PREFERRED_PIO < NUM_PIOS, ""); - -typedef struct { - pio_hw_t *pio; - uint8_t pio_func_sel; - int8_t pio_offset; - int8_t pio_sm; - int8_t dma_out; - int8_t dma_in; -} bus_data_t; - -static bus_data_t bus_data_instance; - -int cyw43_spi_init(cyw43_int_t *self) { - // Only does something if CYW43_LOGIC_DEBUG=1 - logic_debug_init(); - - static_assert(NUM_PIOS == 2, ""); - - pio_hw_t *pios[2] = {pio0, pio1}; - uint pio_index = CYW43_SPI_PIO_PREFERRED_PIO; - // Check we can add the program - if (!pio_can_add_program(pios[pio_index], &SPI_PROGRAM_FUNC)) { - pio_index ^= 1; - if (!pio_can_add_program(pios[pio_index], &SPI_PROGRAM_FUNC)) { - return CYW43_FAIL_FAST_CHECK(-CYW43_EIO); - } - } - assert(!self->bus_data); - self->bus_data = &bus_data_instance; - bus_data_t *bus_data = (bus_data_t *)self->bus_data; - bus_data->pio = pios[pio_index]; - bus_data->dma_in = -1; - bus_data->dma_out = -1; - - static_assert(GPIO_FUNC_PIO1 == GPIO_FUNC_PIO0 + 1, ""); - bus_data->pio_func_sel = GPIO_FUNC_PIO0 + pio_index; - bus_data->pio_sm = (int8_t)pio_claim_unused_sm(bus_data->pio, false); - if (bus_data->pio_sm < 0) { - cyw43_spi_deinit(self); - return CYW43_FAIL_FAST_CHECK(-CYW43_EIO); - } - - bus_data->pio_offset = pio_add_program(bus_data->pio, &SPI_PROGRAM_FUNC); - pio_sm_config config = SPI_PROGRAM_GET_DEFAULT_CONFIG_FUNC(bus_data->pio_offset); - - sm_config_set_clkdiv_int_frac(&config, CLOCK_DIV, CLOCK_DIV_MINOR); - hw_write_masked(&padsbank0_hw->io[CLOCK_PIN], - (uint)PADS_DRIVE_STRENGTH << PADS_BANK0_GPIO0_DRIVE_LSB, - PADS_BANK0_GPIO0_DRIVE_BITS - ); - hw_write_masked(&padsbank0_hw->io[CLOCK_PIN], - (uint)1 << PADS_BANK0_GPIO0_SLEWFAST_LSB, - PADS_BANK0_GPIO0_SLEWFAST_BITS - ); - - sm_config_set_out_pins(&config, DATA_OUT_PIN, 1); - sm_config_set_in_pins(&config, DATA_IN_PIN); - sm_config_set_set_pins(&config, DATA_OUT_PIN, 1); - sm_config_set_sideset(&config, 1, false, false); - sm_config_set_sideset_pins(&config, CLOCK_PIN); - sm_config_set_in_shift(&config, false, true, 32); - sm_config_set_out_shift(&config, false, true, 32); - hw_set_bits(&bus_data->pio->input_sync_bypass, 1u << DATA_IN_PIN); - pio_sm_set_config(bus_data->pio, bus_data->pio_sm, &config); - pio_sm_set_consecutive_pindirs(bus_data->pio, bus_data->pio_sm, CLOCK_PIN, 1, true); - gpio_set_function(DATA_OUT_PIN, bus_data->pio_func_sel); - - // Set data pin to pull down and schmitt - gpio_set_pulls(DATA_IN_PIN, false, true); - gpio_set_input_hysteresis_enabled(DATA_IN_PIN, true); - - pio_sm_exec(bus_data->pio, bus_data->pio_sm, pio_encode_set(pio_pins, 1)); - - bus_data->dma_out = (int8_t) dma_claim_unused_channel(false); - bus_data->dma_in = (int8_t) dma_claim_unused_channel(false); - if (bus_data->dma_out < 0 || bus_data->dma_in < 0) { - cyw43_spi_deinit(self); - return CYW43_FAIL_FAST_CHECK(-CYW43_EIO); - } - return 0; -} - -void cyw43_spi_deinit(cyw43_int_t *self) { - if (self->bus_data) { - bus_data_t *bus_data = (bus_data_t *)self->bus_data; - if (bus_data->pio_sm >= 0) { - if (bus_data->pio_offset != -1) - pio_remove_program(bus_data->pio, &SPI_PROGRAM_FUNC, bus_data->pio_offset); - pio_sm_unclaim(bus_data->pio, bus_data->pio_sm); - } - if (bus_data->dma_out >= 0) { - dma_channel_cleanup(bus_data->dma_out); - dma_channel_unclaim(bus_data->dma_out); - bus_data->dma_out = -1; - } - if (bus_data->dma_in >= 0) { - dma_channel_cleanup(bus_data->dma_in); - dma_channel_unclaim(bus_data->dma_in); - bus_data->dma_in = -1; - } - self->bus_data = NULL; - } -} - -static void cs_set(bool value) { - gpio_put(CS_PIN, value); -} - -static __noinline void ns_delay(uint32_t ns) { - // cycles = ns * clk_sys_hz / 1,000,000,000 - uint32_t cycles = ns * (clock_get_hz(clk_sys) >> 16u) / (1000000000u >> 16u); - busy_wait_at_least_cycles(cycles); -} - -static void start_spi_comms(cyw43_int_t *self) { - bus_data_t *bus_data = (bus_data_t *)self->bus_data; - gpio_set_function(DATA_OUT_PIN, bus_data->pio_func_sel); - gpio_set_function(CLOCK_PIN, bus_data->pio_func_sel); - gpio_pull_down(CLOCK_PIN); - // Pull CS low - cs_set(false); -} - -// we need to atomically de-assert CS and enable IRQ -static void stop_spi_comms(void) { - // from this point a positive edge will cause an IRQ to be pending - cs_set(true); - - // we need to wait a bit in case the irq line is incorrectly high - ns_delay(IRQ_SAMPLE_DELAY_NS); -} - -#if ENABLE_SPI_DUMPING -static void dump_bytes(const uint8_t *bptr, uint32_t len) { - unsigned int i = 0; - - for (i = 0; i < len;) { - if ((i & 0x0f) == 0) { - printf("\n"); - } else if ((i & 0x07) == 0) { - printf(" "); - } - printf("%02x ", bptr[i++]); - } - printf("\n"); -} -#endif - -int cyw43_spi_transfer(cyw43_int_t *self, const uint8_t *tx, size_t tx_length, uint8_t *rx, - size_t rx_length) { - - if ((tx == NULL) && (rx == NULL)) { - return CYW43_FAIL_FAST_CHECK(-CYW43_EINVAL); - } - - bus_data_t *bus_data = (bus_data_t *)self->bus_data; - start_spi_comms(self); - if (rx != NULL) { - if (tx == NULL) { - tx = rx; - assert(tx_length && tx_length < rx_length); - } - DUMP_SPI_TRANSACTIONS( - printf("[%lu] bus TX/RX %u bytes rx %u:", counter++, tx_length, rx_length); - dump_bytes(tx, tx_length); - ) - assert(!(tx_length & 3)); - assert(!(((uintptr_t)tx) & 3)); - assert(!(((uintptr_t)rx) & 3)); - assert(!(rx_length & 3)); - - pio_sm_set_enabled(bus_data->pio, bus_data->pio_sm, false); - pio_sm_set_wrap(bus_data->pio, bus_data->pio_sm, bus_data->pio_offset, bus_data->pio_offset + SPI_OFFSET_END - 1); - pio_sm_clear_fifos(bus_data->pio, bus_data->pio_sm); - pio_sm_set_pindirs_with_mask(bus_data->pio, bus_data->pio_sm, 1u << DATA_OUT_PIN, 1u << DATA_OUT_PIN); - pio_sm_restart(bus_data->pio, bus_data->pio_sm); - pio_sm_clkdiv_restart(bus_data->pio, bus_data->pio_sm); - pio_sm_put(bus_data->pio, bus_data->pio_sm, tx_length * 8 - 1); - pio_sm_exec(bus_data->pio, bus_data->pio_sm, pio_encode_out(pio_x, 32)); - pio_sm_put(bus_data->pio, bus_data->pio_sm, (rx_length - tx_length) * 8 - 1); - pio_sm_exec(bus_data->pio, bus_data->pio_sm, pio_encode_out(pio_y, 32)); - pio_sm_exec(bus_data->pio, bus_data->pio_sm, pio_encode_jmp(bus_data->pio_offset)); - dma_channel_abort(bus_data->dma_out); - dma_channel_abort(bus_data->dma_in); - - dma_channel_config out_config = dma_channel_get_default_config(bus_data->dma_out); - channel_config_set_bswap(&out_config, true); - channel_config_set_dreq(&out_config, pio_get_dreq(bus_data->pio, bus_data->pio_sm, true)); - - dma_channel_configure(bus_data->dma_out, &out_config, &bus_data->pio->txf[bus_data->pio_sm], tx, tx_length / 4, true); - - dma_channel_config in_config = dma_channel_get_default_config(bus_data->dma_in); - channel_config_set_bswap(&in_config, true); - channel_config_set_dreq(&in_config, pio_get_dreq(bus_data->pio, bus_data->pio_sm, false)); - channel_config_set_write_increment(&in_config, true); - channel_config_set_read_increment(&in_config, false); - dma_channel_configure(bus_data->dma_in, &in_config, rx + tx_length, &bus_data->pio->rxf[bus_data->pio_sm], rx_length / 4 - tx_length / 4, true); - - pio_sm_set_enabled(bus_data->pio, bus_data->pio_sm, true); - __compiler_memory_barrier(); - - dma_channel_wait_for_finish_blocking(bus_data->dma_out); - dma_channel_wait_for_finish_blocking(bus_data->dma_in); - - __compiler_memory_barrier(); - memset(rx, 0, tx_length); // make sure we don't have garbage in what would have been returned data if using real SPI - } else if (tx != NULL) { - DUMP_SPI_TRANSACTIONS( - printf("[%lu] bus TX only %u bytes:", counter++, tx_length); - dump_bytes(tx, tx_length); - ) - assert(!(((uintptr_t)tx) & 3)); - assert(!(tx_length & 3)); - pio_sm_set_enabled(bus_data->pio, bus_data->pio_sm, false); - pio_sm_set_wrap(bus_data->pio, bus_data->pio_sm, bus_data->pio_offset, bus_data->pio_offset + SPI_OFFSET_LP1_END - 1); - pio_sm_clear_fifos(bus_data->pio, bus_data->pio_sm); - pio_sm_set_pindirs_with_mask(bus_data->pio, bus_data->pio_sm, 1u << DATA_OUT_PIN, 1u << DATA_OUT_PIN); - pio_sm_restart(bus_data->pio, bus_data->pio_sm); - pio_sm_clkdiv_restart(bus_data->pio, bus_data->pio_sm); - pio_sm_put(bus_data->pio, bus_data->pio_sm, tx_length * 8 - 1); - pio_sm_exec(bus_data->pio, bus_data->pio_sm, pio_encode_out(pio_x, 32)); - pio_sm_put(bus_data->pio, bus_data->pio_sm, 0); - pio_sm_exec(bus_data->pio, bus_data->pio_sm, pio_encode_out(pio_y, 32)); - pio_sm_exec(bus_data->pio, bus_data->pio_sm, pio_encode_jmp(bus_data->pio_offset)); - dma_channel_abort(bus_data->dma_out); - - dma_channel_config out_config = dma_channel_get_default_config(bus_data->dma_out); - channel_config_set_bswap(&out_config, true); - channel_config_set_dreq(&out_config, pio_get_dreq(bus_data->pio, bus_data->pio_sm, true)); - - dma_channel_configure(bus_data->dma_out, &out_config, &bus_data->pio->txf[bus_data->pio_sm], tx, tx_length / 4, true); - - uint32_t fdebug_tx_stall = 1u << (PIO_FDEBUG_TXSTALL_LSB + bus_data->pio_sm); - bus_data->pio->fdebug = fdebug_tx_stall; - pio_sm_set_enabled(bus_data->pio, bus_data->pio_sm, true); - while (!(bus_data->pio->fdebug & fdebug_tx_stall)) { - tight_loop_contents(); // todo timeout - } - __compiler_memory_barrier(); - pio_sm_set_enabled(bus_data->pio, bus_data->pio_sm, false); - pio_sm_set_consecutive_pindirs(bus_data->pio, bus_data->pio_sm, DATA_IN_PIN, 1, false); - } else if (rx != NULL) { /* currently do one at a time */ - DUMP_SPI_TRANSACTIONS( - printf("[%lu] bus TX %u bytes:", counter++, rx_length); - dump_bytes(rx, rx_length); - ) - panic_unsupported(); - } - pio_sm_exec(bus_data->pio, bus_data->pio_sm, pio_encode_mov(pio_pins, pio_null)); // for next time we turn output on - - stop_spi_comms(); - DUMP_SPI_TRANSACTIONS( - printf("RXed:"); - dump_bytes(rx, rx_length); - printf("\n"); - ) - - return 0; -} - -// Initialise our gpios -void cyw43_spi_gpio_setup(void) { - // Setup WL_REG_ON (23) - gpio_init(WL_REG_ON); - gpio_set_dir(WL_REG_ON, GPIO_OUT); - gpio_pull_up(WL_REG_ON); - - // Setup DO, DI and IRQ (24) - gpio_init(DATA_OUT_PIN); - gpio_set_dir(DATA_OUT_PIN, GPIO_OUT); - gpio_put(DATA_OUT_PIN, false); - - // Setup CS (25) - gpio_init(CS_PIN); - gpio_set_dir(CS_PIN, GPIO_OUT); - gpio_put(CS_PIN, true); -} - -// Reset wifi chip -void cyw43_spi_reset(void) { - gpio_put(WL_REG_ON, false); // off - sleep_ms(20); - gpio_put(WL_REG_ON, true); // on - sleep_ms(250); - - // Setup IRQ (24) - also used for DO, DI - gpio_init(IRQ_PIN); - gpio_set_dir(IRQ_PIN, GPIO_IN); -} - -static inline uint32_t make_cmd(bool write, bool inc, uint32_t fn, uint32_t addr, uint32_t sz) { - return write << 31 | inc << 30 | fn << 28 | (addr & 0x1ffff) << 11 | sz; -} - -#if CYW43_VERBOSE_DEBUG -static const char *func_name(int fn) { - switch (fn) - { - case BUS_FUNCTION: - return "BUS_FUNCTION"; - case BACKPLANE_FUNCTION: - return "BACKPLANE_FUNCTION"; - case WLAN_FUNCTION: - return "WLAN_FUNCTION"; - default: - return "UNKNOWN"; - } -} -#endif - -uint32_t read_reg_u32_swap(cyw43_int_t *self, uint32_t fn, uint32_t reg) { - uint32_t buf[2] = {0}; - assert(fn != BACKPLANE_FUNCTION); - buf[0] = SWAP32(make_cmd(false, true, fn, reg, 4)); - int ret = cyw43_spi_transfer(self, NULL, 4, (uint8_t *)buf, 8); - if (ret != 0) { - return ret; - } - return SWAP32(buf[1]); -} - -static inline uint32_t _cyw43_read_reg(cyw43_int_t *self, uint32_t fn, uint32_t reg, uint size) { - // Padding plus max read size of 32 bits + another 4? - static_assert(CYW43_BACKPLANE_READ_PAD_LEN_BYTES % 4 == 0, ""); - int index = (CYW43_BACKPLANE_READ_PAD_LEN_BYTES / 4) + 1 + 1; - uint32_t buf32[index]; - uint8_t *buf = (uint8_t *)buf32; - const uint32_t padding = (fn == BACKPLANE_FUNCTION) ? CYW43_BACKPLANE_READ_PAD_LEN_BYTES : 0; // Add response delay - buf32[0] = make_cmd(false, true, fn, reg, size); - - if (fn == BACKPLANE_FUNCTION) { - logic_debug_set(pin_BACKPLANE_READ, 1); - } - int ret = cyw43_spi_transfer(self, NULL, 4, buf, 8 + padding); - if (fn == BACKPLANE_FUNCTION) { - logic_debug_set(pin_BACKPLANE_READ, 0); - } - - if (ret != 0) { - return ret; - } - uint32_t result = buf32[padding > 0 ? index - 1 : 1]; - CYW43_VDEBUG("cyw43_read_reg_u%d %s 0x%lx=0x%lx\n", size * 8, func_name(fn), reg, result); - return result; -} - -uint32_t cyw43_read_reg_u32(cyw43_int_t *self, uint32_t fn, uint32_t reg) { - return _cyw43_read_reg(self, fn, reg, 4); -} - -int cyw43_read_reg_u16(cyw43_int_t *self, uint32_t fn, uint32_t reg) { - return _cyw43_read_reg(self, fn, reg, 2); -} - -int cyw43_read_reg_u8(cyw43_int_t *self, uint32_t fn, uint32_t reg) { - return _cyw43_read_reg(self, fn, reg, 1); -} - -// This is only used to switch the word order on boot -int write_reg_u32_swap(cyw43_int_t *self, uint32_t fn, uint32_t reg, uint32_t val) { - uint32_t buf[2]; - // Boots up in little endian so command needs swapping too - buf[0] = SWAP32(make_cmd(true, true, fn, reg, 4)); - buf[1] = SWAP32(val); - int ret = cyw43_spi_transfer(self, (uint8_t *)buf, 8, NULL, 0); - CYW43_VDEBUG("write_reg_u32_swap %s 0x%lx=0x%lx\n", func_name(fn), reg, val); - return ret; -} - -static inline int _cyw43_write_reg(cyw43_int_t *self, uint32_t fn, uint32_t reg, uint32_t val, uint size) { - uint32_t buf[2]; - buf[0] = make_cmd(true, true, fn, reg, size); - buf[1] = val; - if (fn == BACKPLANE_FUNCTION) { - // In case of f1 overflow - self->last_size = 8; - self->last_header[0] = buf[0]; - self->last_header[1] = buf[1]; - self->last_backplane_window = self->cur_backplane_window; - } - - if (fn == BACKPLANE_FUNCTION) { - logic_debug_set(pin_BACKPLANE_WRITE, 1); - } - - int ret = cyw43_spi_transfer(self, (uint8_t *)buf, 8, NULL, 0); - - if (fn == BACKPLANE_FUNCTION) { - logic_debug_set(pin_BACKPLANE_WRITE, 0); - } - - CYW43_VDEBUG("cyw43_write_reg_u%d %s 0x%lx=0x%lx\n", size * 8, func_name(fn), reg, val); - return ret; -} - -int cyw43_write_reg_u32(cyw43_int_t *self, uint32_t fn, uint32_t reg, uint32_t val) { - return _cyw43_write_reg(self, fn, reg, val, 4); -} - -int cyw43_write_reg_u16(cyw43_int_t *self, uint32_t fn, uint32_t reg, uint16_t val) { - return _cyw43_write_reg(self, fn, reg, val, 2); -} - -int cyw43_write_reg_u8(cyw43_int_t *self, uint32_t fn, uint32_t reg, uint32_t val) { - return _cyw43_write_reg(self, fn, reg, val, 1); -} - -#if CYW43_BUS_MAX_BLOCK_SIZE > 0x7f8 -#error Block size is wrong for SPI -#endif - -int cyw43_read_bytes(cyw43_int_t *self, uint32_t fn, uint32_t addr, size_t len, uint8_t *buf) { - assert(fn != BACKPLANE_FUNCTION || (len <= CYW43_BUS_MAX_BLOCK_SIZE)); - const uint32_t padding = (fn == BACKPLANE_FUNCTION) ? CYW43_BACKPLANE_READ_PAD_LEN_BYTES : 0; // Add response delay - size_t aligned_len = (len + 3) & ~3; - assert(aligned_len > 0 && aligned_len <= 0x7f8); - assert(buf == self->spid_buf || buf < self->spid_buf || buf >= (self->spid_buf + sizeof(self->spid_buf))); - self->spi_header[padding > 0 ? 0 : (CYW43_BACKPLANE_READ_PAD_LEN_BYTES / 4)] = make_cmd(false, true, fn, addr, len); - if (fn == WLAN_FUNCTION) { - logic_debug_set(pin_WIFI_RX, 1); - } - int ret = cyw43_spi_transfer(self, NULL, 4, (uint8_t *)&self->spi_header[padding > 0 ? 0 : (CYW43_BACKPLANE_READ_PAD_LEN_BYTES / 4)], aligned_len + 4 + padding); - if (fn == WLAN_FUNCTION) { - logic_debug_set(pin_WIFI_RX, 0); - } - if (ret != 0) { - printf("cyw43_read_bytes error %d", ret); - return ret; - } - if (buf != self->spid_buf) { // avoid a copy in the usual case just to add the header - memcpy(buf, self->spid_buf, len); - } - return 0; -} - -// See whd_bus_spi_transfer_bytes -// Note, uses spid_buf if src isn't using it already -// Apart from firmware download this appears to only be used for wlan functions? -int cyw43_write_bytes(cyw43_int_t *self, uint32_t fn, uint32_t addr, size_t len, const uint8_t *src) { - assert(fn != BACKPLANE_FUNCTION || (len <= CYW43_BUS_MAX_BLOCK_SIZE)); - const size_t aligned_len = (len + 3) & ~3u; - assert(aligned_len > 0 && aligned_len <= 0x7f8); - if (fn == WLAN_FUNCTION) { - // Wait for FIFO to be ready to accept data - int f2_ready_attempts = 1000; - while (f2_ready_attempts-- > 0) { - uint32_t bus_status = cyw43_read_reg_u32(self, BUS_FUNCTION, SPI_STATUS_REGISTER); - if (bus_status & STATUS_F2_RX_READY) { - logic_debug_set(pin_F2_RX_READY_WAIT, 0); - break; - } else { - logic_debug_set(pin_F2_RX_READY_WAIT, 1); - } - } - if (f2_ready_attempts <= 0) { - printf("F2 not ready\n"); - return CYW43_FAIL_FAST_CHECK(-CYW43_EIO); - } - } - if (src == self->spid_buf) { // avoid a copy in the usual case just to add the header - self->spi_header[(CYW43_BACKPLANE_READ_PAD_LEN_BYTES / 4)] = make_cmd(true, true, fn, addr, len); - logic_debug_set(pin_WIFI_TX, 1); - int res = cyw43_spi_transfer(self, (uint8_t *)&self->spi_header[(CYW43_BACKPLANE_READ_PAD_LEN_BYTES / 4)], aligned_len + 4, NULL, 0); - logic_debug_set(pin_WIFI_TX, 0); - return res; - } else { - // todo: would be nice to get rid of this. Only used for firmware download? - assert(src < self->spid_buf || src >= (self->spid_buf + sizeof(self->spid_buf))); - self->spi_header[(CYW43_BACKPLANE_READ_PAD_LEN_BYTES / 4)] = make_cmd(true, true, fn, addr, len); - memcpy(self->spid_buf, src, len); - return cyw43_spi_transfer(self, (uint8_t *)&self->spi_header[(CYW43_BACKPLANE_READ_PAD_LEN_BYTES / 4)], aligned_len + 4, NULL, 0); - } -} -#endif diff --git a/pico-sdk/src/rp2_common/pico_cyw43_driver/cyw43_bus_pio_spi.pio b/pico-sdk/src/rp2_common/pico_cyw43_driver/cyw43_bus_pio_spi.pio deleted file mode 100644 index ea0d195..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_driver/cyw43_bus_pio_spi.pio +++ /dev/null @@ -1,61 +0,0 @@ -; -; Copyright (c) 2020 Raspberry Pi (Trading) Ltd. -; -; SPDX-License-Identifier: BSD-3-Clause -; - -.program spi_gap0_sample1 -.side_set 1 - -; always transmit multiple of 32 bytes -lp: out pins, 1 side 0 - jmp x-- lp side 1 -public lp1_end: - set pindirs, 0 side 0 -lp2: - in pins, 1 side 1 - jmp y-- lp2 side 0 -public end: - -.program spi_gap01_sample0 -.side_set 1 - -; always transmit multiple of 32 bytes -lp: out pins, 1 side 0 - jmp x-- lp side 1 -public lp1_end: - set pindirs, 0 side 0 - nop side 1 -lp2: - in pins, 1 side 0 - jmp y-- lp2 side 1 -public end: - -.program spi_gap010_sample1 -.side_set 1 - -; always transmit multiple of 32 bytes -lp: out pins, 1 side 0 - jmp x-- lp side 1 -public lp1_end: - set pindirs, 0 side 0 - nop side 1 - nop side 0 -lp2: - in pins, 1 side 1 - jmp y-- lp2 side 0 -public end: - -.program spi_gap0_sample1_regular -.side_set 1 - -; always transmit multiple of 32 bytes -lp: out pins, 1 side 0 - jmp x-- lp side 1 -public lp1_end: - set pindirs, 0 side 0 -lp2: - in pins, 1 side 1 - jmp y-- lp2 side 0 -public end: - diff --git a/pico-sdk/src/rp2_common/pico_cyw43_driver/cyw43_driver.c b/pico-sdk/src/rp2_common/pico_cyw43_driver/cyw43_driver.c deleted file mode 100644 index 6a3d01c..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_driver/cyw43_driver.c +++ /dev/null @@ -1,195 +0,0 @@ -/* - * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include "hardware/gpio.h" -#include "hardware/irq.h" -#include "pico/unique_id.h" -#include "cyw43.h" -#include "pico/cyw43_driver.h" - -#ifndef CYW43_GPIO_IRQ_HANDLER_PRIORITY -#define CYW43_GPIO_IRQ_HANDLER_PRIORITY 0x40 -#endif - -#ifndef CYW43_SLEEP_CHECK_MS -#define CYW43_SLEEP_CHECK_MS 50 -#endif - -static async_context_t *cyw43_async_context; - -static void cyw43_sleep_timeout_reached(async_context_t *context, async_at_time_worker_t *worker); -static void cyw43_do_poll(async_context_t *context, async_when_pending_worker_t *worker); - -static async_at_time_worker_t sleep_timeout_worker = { - .do_work = cyw43_sleep_timeout_reached -}; - -static async_when_pending_worker_t cyw43_poll_worker = { - .do_work = cyw43_do_poll -}; - -static void cyw43_set_irq_enabled(bool enabled) { - gpio_set_irq_enabled(CYW43_PIN_WL_HOST_WAKE, GPIO_IRQ_LEVEL_HIGH, enabled); -} - -// GPIO interrupt handler to tell us there's cyw43 has work to do -static void cyw43_gpio_irq_handler(void) -{ - uint32_t events = gpio_get_irq_event_mask(CYW43_PIN_WL_HOST_WAKE); - if (events & GPIO_IRQ_LEVEL_HIGH) { - // As we use a high level interrupt, it will go off forever until it's serviced - // So disable the interrupt until this is done. It's re-enabled again by CYW43_POST_POLL_HOOK - // which is called at the end of cyw43_poll_func - cyw43_set_irq_enabled(false); - async_context_set_work_pending(cyw43_async_context, &cyw43_poll_worker); - } -} - -uint32_t cyw43_irq_init(__unused void *param) { -#ifndef NDEBUG - assert(get_core_num() == async_context_core_num(cyw43_async_context)); -#endif - gpio_add_raw_irq_handler_with_order_priority(CYW43_PIN_WL_HOST_WAKE, cyw43_gpio_irq_handler, CYW43_GPIO_IRQ_HANDLER_PRIORITY); - cyw43_set_irq_enabled(true); - irq_set_enabled(IO_IRQ_BANK0, true); - return 0; -} - -uint32_t cyw43_irq_deinit(__unused void *param) { -#ifndef NDEBUG - assert(get_core_num() == async_context_core_num(cyw43_async_context)); -#endif - gpio_remove_raw_irq_handler(CYW43_PIN_WL_HOST_WAKE, cyw43_gpio_irq_handler); - cyw43_set_irq_enabled(false); - return 0; -} - -void cyw43_post_poll_hook(void) { -#ifndef NDEBUG - assert(get_core_num() == async_context_core_num(cyw43_async_context)); -#endif - cyw43_set_irq_enabled(true); -} - -void cyw43_schedule_internal_poll_dispatch(__unused void (*func)(void)) { - assert(func == cyw43_poll); - async_context_set_work_pending(cyw43_async_context, &cyw43_poll_worker); -} - -static void cyw43_do_poll(async_context_t *context, __unused async_when_pending_worker_t *worker) { -#ifndef NDEBUG - assert(get_core_num() == async_context_core_num(cyw43_async_context)); -#endif - if (cyw43_poll) { - if (cyw43_sleep > 0) { - cyw43_sleep--; - } - cyw43_poll(); - if (cyw43_sleep) { - async_context_add_at_time_worker_in_ms(context, &sleep_timeout_worker, CYW43_SLEEP_CHECK_MS); - } else { - async_context_remove_at_time_worker(context, &sleep_timeout_worker); - } - } -} - -static void cyw43_sleep_timeout_reached(async_context_t *context, __unused async_at_time_worker_t *worker) { - assert(context == cyw43_async_context); - assert(worker == &sleep_timeout_worker); - async_context_set_work_pending(context, &cyw43_poll_worker); -} - -bool cyw43_driver_init(async_context_t *context) { - cyw43_init(&cyw43_state); - cyw43_async_context = context; - // we need the IRQ to be on the same core as the context, because we need to be able to enable/disable the IRQ - // from there later - async_context_execute_sync(context, cyw43_irq_init, NULL); - async_context_add_when_pending_worker(context, &cyw43_poll_worker); - return true; -} - -void cyw43_driver_deinit(async_context_t *context) { - assert(context == cyw43_async_context); - async_context_remove_at_time_worker(context, &sleep_timeout_worker); - async_context_remove_when_pending_worker(context, &cyw43_poll_worker); - // the IRQ IS on the same core as the context, so must be de-initialized there - async_context_execute_sync(context, cyw43_irq_deinit, NULL); - cyw43_deinit(&cyw43_state); - cyw43_async_context = NULL; -} - -// todo maybe add an #ifdef in cyw43_driver -uint32_t storage_read_blocks(__unused uint8_t *dest, __unused uint32_t block_num, __unused uint32_t num_blocks) { - // shouldn't be used - panic_unsupported(); -} - -// Generate a mac address if one is not set in otp -void __attribute__((weak)) cyw43_hal_generate_laa_mac(__unused int idx, uint8_t buf[6]) { - CYW43_DEBUG("Warning. No mac in otp. Generating mac from board id\n"); - pico_unique_board_id_t board_id; - pico_get_unique_board_id(&board_id); - memcpy(buf, &board_id.id[2], 6); - buf[0] &= (uint8_t)~0x1; // unicast - buf[0] |= 0x2; // locally administered -} - -// Return mac address -void cyw43_hal_get_mac(__unused int idx, uint8_t buf[6]) { - // The mac should come from cyw43 otp. - // This is loaded into the state after the driver is initialised - // cyw43_hal_generate_laa_mac is called by the driver to generate a mac if otp is not set - memcpy(buf, cyw43_state.mac, 6); -} - -// Prevent background processing in pensv and access by the other core -// These methods are called in pensv context and on either core -// They can be called recursively -void cyw43_thread_enter(void) { - async_context_acquire_lock_blocking(cyw43_async_context); -} - -void cyw43_thread_exit(void) { - async_context_release_lock(cyw43_async_context); -} - -#ifndef NDEBUG -void cyw43_thread_lock_check(void) { - async_context_lock_check(cyw43_async_context); -} -#endif - -void cyw43_await_background_or_timeout_us(uint32_t timeout_us) { - async_context_wait_for_work_until(cyw43_async_context, make_timeout_time_us(timeout_us)); -} - -void cyw43_delay_ms(uint32_t ms) { - async_context_wait_until(cyw43_async_context, make_timeout_time_ms(ms)); -} - -void cyw43_delay_us(uint32_t us) { - async_context_wait_until(cyw43_async_context, make_timeout_time_us(us)); -} - -#if !CYW43_LWIP -static void no_lwip_fail() { - panic("cyw43 has no ethernet interface"); -} -void __attribute__((weak)) cyw43_cb_tcpip_init(cyw43_t *self, int itf) { -} -void __attribute__((weak)) cyw43_cb_tcpip_deinit(cyw43_t *self, int itf) { -} -void __attribute__((weak)) cyw43_cb_tcpip_set_link_up(cyw43_t *self, int itf) { - no_lwip_fail(); -} -void __attribute__((weak)) cyw43_cb_tcpip_set_link_down(cyw43_t *self, int itf) { - no_lwip_fail(); -} -void __attribute__((weak)) cyw43_cb_process_ethernet(void *cb_data, int itf, size_t len, const uint8_t *buf) { - no_lwip_fail(); -} -#endif diff --git a/pico-sdk/src/rp2_common/pico_cyw43_driver/include/cyw43_configport.h b/pico-sdk/src/rp2_common/pico_cyw43_driver/include/cyw43_configport.h deleted file mode 100644 index f012d24..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_driver/include/cyw43_configport.h +++ /dev/null @@ -1,173 +0,0 @@ -/* - * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -// This header is included by cyw43_driver to setup its environment - -#ifndef _CYW43_CONFIGPORT_H -#define _CYW43_CONFIGPORT_H - -#include "pico.h" -#include "hardware/gpio.h" -#include "pico/time.h" - -#ifdef __cplusplus -extern "C" { -#endif - -#ifndef CYW43_HOST_NAME -#define CYW43_HOST_NAME "PicoW" -#endif - -#ifndef CYW43_GPIO -#define CYW43_GPIO 1 -#endif - -#ifndef CYW43_LOGIC_DEBUG -#define CYW43_LOGIC_DEBUG 0 -#endif - -#ifndef CYW43_USE_OTP_MAC -#define CYW43_USE_OTP_MAC 1 -#endif - -#ifndef CYW43_NO_NETUTILS -#define CYW43_NO_NETUTILS 1 -#endif - -#ifndef CYW43_IOCTL_TIMEOUT_US -#define CYW43_IOCTL_TIMEOUT_US 1000000 -#endif - -#ifndef CYW43_USE_STATS -#define CYW43_USE_STATS 0 -#endif - -// todo should this be user settable? -#ifndef CYW43_HAL_MAC_WLAN0 -#define CYW43_HAL_MAC_WLAN0 0 -#endif - -#ifndef STATIC -#define STATIC static -#endif - -#ifndef CYW43_USE_SPI -#define CYW43_USE_SPI 1 -#endif - -#ifndef CYW43_SPI_PIO -#define CYW43_SPI_PIO 1 -#endif - -#ifndef CYW43_CHIPSET_FIRMWARE_INCLUDE_FILE -#if CYW43_ENABLE_BLUETOOTH -#define CYW43_CHIPSET_FIRMWARE_INCLUDE_FILE "wb43439A0_7_95_49_00_combined.h" -#else -#define CYW43_CHIPSET_FIRMWARE_INCLUDE_FILE "w43439A0_7_95_49_00_combined.h" -#endif -#endif - -#ifndef CYW43_WIFI_NVRAM_INCLUDE_FILE -#define CYW43_WIFI_NVRAM_INCLUDE_FILE "wifi_nvram_43439.h" -#endif - -// Note, these are negated, because cyw43_driver negates them before returning! -#define CYW43_EPERM (-PICO_ERROR_NOT_PERMITTED) // Operation not permitted -#define CYW43_EIO (-PICO_ERROR_IO) // I/O error -#define CYW43_EINVAL (-PICO_ERROR_INVALID_ARG) // Invalid argument -#define CYW43_ETIMEDOUT (-PICO_ERROR_TIMEOUT) // Connection timed out - -#define CYW43_NUM_GPIOS CYW43_WL_GPIO_COUNT - -#define cyw43_hal_pin_obj_t uint - -// get the number of elements in a fixed-size array -#define CYW43_ARRAY_SIZE(a) count_of(a) - -static inline uint32_t cyw43_hal_ticks_us(void) { - return time_us_32(); -} - -static inline uint32_t cyw43_hal_ticks_ms(void) { - return to_ms_since_boot(get_absolute_time()); -} - -static inline int cyw43_hal_pin_read(cyw43_hal_pin_obj_t pin) { - return gpio_get(pin); -} - -static inline void cyw43_hal_pin_low(cyw43_hal_pin_obj_t pin) { - gpio_clr_mask(1 << pin); -} - -static inline void cyw43_hal_pin_high(cyw43_hal_pin_obj_t pin) { - gpio_set_mask(1 << pin); -} - -#define CYW43_HAL_PIN_MODE_INPUT (GPIO_IN) -#define CYW43_HAL_PIN_MODE_OUTPUT (GPIO_OUT) - -#define CYW43_HAL_PIN_PULL_NONE (0) -#define CYW43_HAL_PIN_PULL_UP (1) -#define CYW43_HAL_PIN_PULL_DOWN (2) - -static inline void cyw43_hal_pin_config(cyw43_hal_pin_obj_t pin, uint32_t mode, uint32_t pull, __unused uint32_t alt) { - assert((mode == CYW43_HAL_PIN_MODE_INPUT || mode == CYW43_HAL_PIN_MODE_OUTPUT) && alt == 0); - gpio_set_dir(pin, mode); - gpio_set_pulls(pin, pull == CYW43_HAL_PIN_PULL_UP, pull == CYW43_HAL_PIN_PULL_DOWN); -} - -void cyw43_hal_get_mac(int idx, uint8_t buf[6]); - -void cyw43_hal_generate_laa_mac(int idx, uint8_t buf[6]); - - -void cyw43_thread_enter(void); - -void cyw43_thread_exit(void); - -#define CYW43_THREAD_ENTER cyw43_thread_enter(); -#define CYW43_THREAD_EXIT cyw43_thread_exit(); -#ifndef NDEBUG - -void cyw43_thread_lock_check(void); - -#define cyw43_arch_lwip_check() cyw43_thread_lock_check() -#define CYW43_THREAD_LOCK_CHECK cyw43_arch_lwip_check(); -#else -#define cyw43_arch_lwip_check() ((void)0) -#define CYW43_THREAD_LOCK_CHECK -#endif - -void cyw43_await_background_or_timeout_us(uint32_t timeout_us); -// todo not 100% sure about the timeouts here; MP uses __WFI which will always wakeup periodically -#define CYW43_SDPCM_SEND_COMMON_WAIT cyw43_await_background_or_timeout_us(1000); -#define CYW43_DO_IOCTL_WAIT cyw43_await_background_or_timeout_us(1000); - -void cyw43_delay_ms(uint32_t ms); - -void cyw43_delay_us(uint32_t us); - -void cyw43_schedule_internal_poll_dispatch(void (*func)(void)); - -void cyw43_post_poll_hook(void); - -#define CYW43_POST_POLL_HOOK cyw43_post_poll_hook(); - -// Allow malloc and free to be changed -#ifndef cyw43_malloc -#define cyw43_malloc malloc -#endif -#ifndef cyw43_free -#define cyw43_free free -#endif - -#ifdef __cplusplus -} -#endif - - -#endif \ No newline at end of file diff --git a/pico-sdk/src/rp2_common/pico_cyw43_driver/include/pico/btstack_chipset_cyw43.h b/pico-sdk/src/rp2_common/pico_cyw43_driver/include/pico/btstack_chipset_cyw43.h deleted file mode 100644 index 037b68d..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_driver/include/pico/btstack_chipset_cyw43.h +++ /dev/null @@ -1,18 +0,0 @@ -/* - * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef _PICO_BTSTACK_CHIPSET_CYW43_H -#define _PICO_BTSTACK_CHIPSET_CYW43_H - -#include "btstack_chipset.h" - -/** - * \brief Return the singleton BTstack chipset CY43 API instance - * \ingroup pico_btstack - */ -const btstack_chipset_t * btstack_chipset_cyw43_instance(void); - -#endif diff --git a/pico-sdk/src/rp2_common/pico_cyw43_driver/include/pico/btstack_cyw43.h b/pico-sdk/src/rp2_common/pico_cyw43_driver/include/pico/btstack_cyw43.h deleted file mode 100644 index 366ea21..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_driver/include/pico/btstack_cyw43.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef _PICO_BTSTACK_CYW43_H -#define _PICO_BTSTACK_CYW43_H - -#include "pico/async_context.h" -#ifdef __cplusplus -extern "C" { -#endif - -/** \file pico/btstack_cyw43.h - * \defgroup pico_btstack_cyw43 pico_btstack_cyw43 - * \ingroup pico_cyw43_driver - * - * \brief Low-level Bluetooth HCI support. - * - * This library provides utility functions to initialise and de-initialise BTstack for CYW43, -*/ - -/* - * \brief Perform initialisation of BTstack/CYW43 integration - * \ingroup pico_btstack_cyw43 - * - * \param context the async_context instance that provides the abstraction for handling asynchronous work. - * \return true on success or false an error - */ -bool btstack_cyw43_init(async_context_t *context); - -/* - * \brief De-initialise BTstack/CYW43 integration - * \ingroup pico_btstack_cyw43 - * - * \param context the async_context the btstack_cyw43 support was added to via \ref btstack_cyw43_init - */ -void btstack_cyw43_deinit(async_context_t *context); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/pico-sdk/src/rp2_common/pico_cyw43_driver/include/pico/btstack_hci_transport_cyw43.h b/pico-sdk/src/rp2_common/pico_cyw43_driver/include/pico/btstack_hci_transport_cyw43.h deleted file mode 100644 index e025858..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_driver/include/pico/btstack_hci_transport_cyw43.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (c) 2023 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef _PICO_BTSTACK_HCI_TRANSPORT_CYW43_H -#define _PICO_BTSTACK_HCI_TRANSPORT_CYW43_H - -#ifdef __cplusplus -extern "C" { -#endif - -/** \file pico/btstack_hci_transport_cyw43.h -* \ingroup pico_cyw43_driver -* \brief Adds low level Bluetooth HCI support -*/ - -/** - * \brief Get the Bluetooth HCI transport instance for cyw43 - * \ingroup pico_cyw43_driver - * - * \return An instantiation of the hci_transport_t interface for the cyw43 chipset - */ -const hci_transport_t *hci_transport_cyw43_instance(void); - -#ifdef __cplusplus -} -#endif - -#endif // HCI_TRANSPORT_CYW43_H diff --git a/pico-sdk/src/rp2_common/pico_cyw43_driver/include/pico/cyw43_driver.h b/pico-sdk/src/rp2_common/pico_cyw43_driver/include/pico/cyw43_driver.h deleted file mode 100644 index bfef3f7..0000000 --- a/pico-sdk/src/rp2_common/pico_cyw43_driver/include/pico/cyw43_driver.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef _PICO_CYW43_DRIVER_H -#define _PICO_CYW43_DRIVER_H - -/** \file pico/cyw43_driver.h - * \defgroup pico_cyw43_driver pico_cyw43_driver - * - * A wrapper around the lower level cyw43_driver, that integrates it with \ref pico_async_context - * for handling background work. - */ - -#include "pico.h" -#include "pico/async_context.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/*! \brief Initializes the lower level cyw43_driver and integrates it with the provided async_context - * \ingroup pico_cyw43_driver - * - * If the initialization succeeds, \ref lwip_nosys_deinit() can be called to shutdown lwIP support - * - * \param context the async_context instance that provides the abstraction for handling asynchronous work. - * \return true if the initialization succeeded -*/ -bool cyw43_driver_init(async_context_t *context); - -/*! \brief De-initialize the lowever level cyw43_driver and unhooks it from the async_context - * \ingroup pico_cyw43_driver - * - * \param context the async_context the cyw43_driver support was added to via \ref cyw43_driver_init -*/ -void cyw43_driver_deinit(async_context_t *context); - -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file diff --git a/pico-sdk/src/rp2_common/pico_lwip/CMakeLists.txt b/pico-sdk/src/rp2_common/pico_lwip/CMakeLists.txt deleted file mode 100644 index 59d33c2..0000000 --- a/pico-sdk/src/rp2_common/pico_lwip/CMakeLists.txt +++ /dev/null @@ -1,306 +0,0 @@ -if (DEFINED ENV{PICO_LWIP_PATH} AND (NOT PICO_LWIP_PATH)) - set(PICO_LWIP_PATH $ENV{PICO_LWIP_PATH}) - message("Using PICO_LWIP_PATH from environment ('${PICO_LWIP_PATH}')") -endif () - -set(LWIP_TEST_PATH "src/Filelists.cmake") -if (NOT PICO_LWIP_PATH) - set(PICO_LWIP_PATH ${PROJECT_SOURCE_DIR}/lib/lwip) - if (PICO_CYW43_SUPPORTED AND NOT EXISTS ${PICO_LWIP_PATH}/${LWIP_TEST_PATH}) - message(WARNING "LWIP submodule has not been initialized; Pico W wireless support will be unavailable -#hint: try 'git submodule update --init' from your SDK directory (${PICO_SDK_PATH}).") - endif() -elseif (NOT EXISTS ${PICO_LWIP_PATH}/${LWIP_TEST_PATH}) - message(WARNING "PICO_LWIP_PATH specified but content not present.") -endif() - -if (EXISTS ${PICO_LWIP_PATH}/${LWIP_TEST_PATH}) - message("lwIP available at ${PICO_LWIP_PATH}") - - # argh... wanted to use this, but they dump stuff into the source tree, which breaks parallel builds - #set(LWIP_DIR ${PICO_LWIP_PATH}) - #include(${PICO_LWIP_PATH}/src/Filelists.cmake) - - pico_register_common_scope_var(PICO_LWIP_PATH) - - # The minimum set of files needed for lwIP. - pico_add_library(pico_lwip_core NOFLAG) - target_sources(pico_lwip_core INTERFACE - ${PICO_LWIP_PATH}/src/core/init.c - ${PICO_LWIP_PATH}/src/core/def.c - ${PICO_LWIP_PATH}/src/core/dns.c - ${PICO_LWIP_PATH}/src/core/inet_chksum.c - ${PICO_LWIP_PATH}/src/core/ip.c - ${PICO_LWIP_PATH}/src/core/mem.c - ${PICO_LWIP_PATH}/src/core/memp.c - ${PICO_LWIP_PATH}/src/core/netif.c - ${PICO_LWIP_PATH}/src/core/pbuf.c - ${PICO_LWIP_PATH}/src/core/raw.c - ${PICO_LWIP_PATH}/src/core/stats.c - ${PICO_LWIP_PATH}/src/core/sys.c - ${PICO_LWIP_PATH}/src/core/altcp.c - ${PICO_LWIP_PATH}/src/core/altcp_alloc.c - ${PICO_LWIP_PATH}/src/core/altcp_tcp.c - ${PICO_LWIP_PATH}/src/core/tcp.c - ${PICO_LWIP_PATH}/src/core/tcp_in.c - ${PICO_LWIP_PATH}/src/core/tcp_out.c - ${PICO_LWIP_PATH}/src/core/timeouts.c - ${PICO_LWIP_PATH}/src/core/udp.c - ) - target_include_directories(pico_lwip_core_headers INTERFACE - ${PICO_LWIP_PATH}/src/include) - - pico_add_library(pico_lwip_core4 NOFLAG) - target_sources(pico_lwip_core4 INTERFACE - ${PICO_LWIP_PATH}/src/core/ipv4/autoip.c - ${PICO_LWIP_PATH}/src/core/ipv4/dhcp.c - ${PICO_LWIP_PATH}/src/core/ipv4/etharp.c - ${PICO_LWIP_PATH}/src/core/ipv4/icmp.c - ${PICO_LWIP_PATH}/src/core/ipv4/igmp.c - ${PICO_LWIP_PATH}/src/core/ipv4/ip4_frag.c - ${PICO_LWIP_PATH}/src/core/ipv4/ip4.c - ${PICO_LWIP_PATH}/src/core/ipv4/ip4_addr.c - ) - - # Doesn't exists in version earlier than 2.1.3 - if (EXISTS ${PICO_LWIP_PATH}/src/core/ipv4/acd.c) - target_sources(pico_lwip_core4 INTERFACE - ${PICO_LWIP_PATH}/src/core/ipv4/acd.c - ) - endif() - - pico_add_library(pico_lwip_core6 NOFLAG) - target_sources(pico_lwip_core6 INTERFACE - ${PICO_LWIP_PATH}/src/core/ipv6/dhcp6.c - ${PICO_LWIP_PATH}/src/core/ipv6/ethip6.c - ${PICO_LWIP_PATH}/src/core/ipv6/icmp6.c - ${PICO_LWIP_PATH}/src/core/ipv6/inet6.c - ${PICO_LWIP_PATH}/src/core/ipv6/ip6.c - ${PICO_LWIP_PATH}/src/core/ipv6/ip6_addr.c - ${PICO_LWIP_PATH}/src/core/ipv6/ip6_frag.c - ${PICO_LWIP_PATH}/src/core/ipv6/mld6.c - ${PICO_LWIP_PATH}/src/core/ipv6/nd6.c - ) - - # APIFILES: The files which implement the sequential and socket APIs. - pico_add_library(pico_lwip_api NOFLAG) - target_sources(pico_lwip_api INTERFACE - ${PICO_LWIP_PATH}/src/api/api_lib.c - ${PICO_LWIP_PATH}/src/api/api_msg.c - ${PICO_LWIP_PATH}/src/api/err.c - ${PICO_LWIP_PATH}/src/api/if_api.c - ${PICO_LWIP_PATH}/src/api/netbuf.c - ${PICO_LWIP_PATH}/src/api/netdb.c - ${PICO_LWIP_PATH}/src/api/netifapi.c - ${PICO_LWIP_PATH}/src/api/sockets.c - ${PICO_LWIP_PATH}/src/api/tcpip.c - ) - - # Files implementing various generic network interface functions - pico_add_library(pico_lwip_netif NOFLAG) - target_sources(pico_lwip_netif INTERFACE - ${PICO_LWIP_PATH}/src/netif/ethernet.c - ${PICO_LWIP_PATH}/src/netif/bridgeif.c - ${PICO_LWIP_PATH}/src/netif/bridgeif_fdb.c - ${PICO_LWIP_PATH}/src/netif/slipif.c - ) - - # 6LoWPAN - pico_add_library(pico_lwip_sixlowpan NOFLAG) - target_sources(pico_lwip_sixlowpan INTERFACE - ${PICO_LWIP_PATH}/src/netif/lowpan6_common.c - ${PICO_LWIP_PATH}/src/netif/lowpan6.c - ${PICO_LWIP_PATH}/src/netif/lowpan6_ble.c - ${PICO_LWIP_PATH}/src/netif/zepif.c - ) - - # PPP - pico_add_library(pico_lwip_ppp NOFLAG) - target_sources(pico_lwip_ppp INTERFACE - ${PICO_LWIP_PATH}/src/netif/ppp/auth.c - ${PICO_LWIP_PATH}/src/netif/ppp/ccp.c - ${PICO_LWIP_PATH}/src/netif/ppp/chap-md5.c - ${PICO_LWIP_PATH}/src/netif/ppp/chap_ms.c - ${PICO_LWIP_PATH}/src/netif/ppp/chap-new.c - ${PICO_LWIP_PATH}/src/netif/ppp/demand.c - ${PICO_LWIP_PATH}/src/netif/ppp/eap.c - ${PICO_LWIP_PATH}/src/netif/ppp/ecp.c - ${PICO_LWIP_PATH}/src/netif/ppp/eui64.c - ${PICO_LWIP_PATH}/src/netif/ppp/fsm.c - ${PICO_LWIP_PATH}/src/netif/ppp/ipcp.c - ${PICO_LWIP_PATH}/src/netif/ppp/ipv6cp.c - ${PICO_LWIP_PATH}/src/netif/ppp/lcp.c - ${PICO_LWIP_PATH}/src/netif/ppp/magic.c - ${PICO_LWIP_PATH}/src/netif/ppp/mppe.c - ${PICO_LWIP_PATH}/src/netif/ppp/multilink.c - ${PICO_LWIP_PATH}/src/netif/ppp/ppp.c - ${PICO_LWIP_PATH}/src/netif/ppp/pppapi.c - ${PICO_LWIP_PATH}/src/netif/ppp/pppcrypt.c - ${PICO_LWIP_PATH}/src/netif/ppp/pppoe.c - ${PICO_LWIP_PATH}/src/netif/ppp/pppol2tp.c - ${PICO_LWIP_PATH}/src/netif/ppp/pppos.c - ${PICO_LWIP_PATH}/src/netif/ppp/upap.c - ${PICO_LWIP_PATH}/src/netif/ppp/utils.c - ${PICO_LWIP_PATH}/src/netif/ppp/vj.c - ${PICO_LWIP_PATH}/src/netif/ppp/polarssl/arc4.c - ${PICO_LWIP_PATH}/src/netif/ppp/polarssl/des.c - ${PICO_LWIP_PATH}/src/netif/ppp/polarssl/md4.c - ${PICO_LWIP_PATH}/src/netif/ppp/polarssl/md5.c - ${PICO_LWIP_PATH}/src/netif/ppp/polarssl/sha1.c - ) - - # SNMPv3 agent - pico_add_library(pico_lwip_snmp NOFLAG) - target_sources(pico_lwip_snmp INTERFACE - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_asn1.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_core.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_mib2.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_mib2_icmp.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_mib2_interfaces.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_mib2_ip.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_mib2_snmp.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_mib2_system.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_mib2_tcp.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_mib2_udp.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_snmpv2_framework.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_snmpv2_usm.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_msg.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmpv3.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_netconn.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_pbuf_stream.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_raw.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_scalar.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_table.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_threadsync.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmp_traps.c - ) - - # HTTP server + client - pico_add_library(pico_lwip_http NOFLAG) - target_sources(pico_lwip_http INTERFACE - ${PICO_LWIP_PATH}/src/apps/http/altcp_proxyconnect.c - ${PICO_LWIP_PATH}/src/apps/http/fs.c - ${PICO_LWIP_PATH}/src/apps/http/http_client.c - ${PICO_LWIP_PATH}/src/apps/http/httpd.c - ) - - # MAKEFSDATA HTTP server host utility - pico_add_library(pico_lwip_makefsdata NOFLAG) - target_sources(pico_lwip_makefsdata INTERFACE - ${PICO_LWIP_PATH}/src/apps/http/makefsdata/makefsdata.c - ) - - # iperf - pico_add_library(pico_lwip_iperf NOFLAG) - target_sources(pico_lwip_iperf INTERFACE - ${PICO_LWIP_PATH}/src/apps/lwiperf/lwiperf.c - ) - - # SMTP client - pico_add_library(pico_lwip_smtp NOFLAG) - target_sources(pico_lwip_smtp INTERFACE - ${PICO_LWIP_PATH}/src/apps/smtp/smtp.c - ) - - # SNTP client - pico_add_library(pico_lwip_sntp NOFLAG) - target_sources(pico_lwip_sntp INTERFACE - ${PICO_LWIP_PATH}/src/apps/sntp/sntp.c - ) - - # MDNS responder - pico_add_library(pico_lwip_mdns NOFLAG) - target_sources(pico_lwip_mdns INTERFACE - ${PICO_LWIP_PATH}/src/apps/mdns/mdns.c - ) - - # Old versions of lwip had everything in mdns.c - if (EXISTS ${PICO_LWIP_PATH}/src/apps/mdns/mdns_out.c) - target_sources(pico_lwip_mdns INTERFACE - ${PICO_LWIP_PATH}/src/apps/mdns/mdns_out.c - ${PICO_LWIP_PATH}/src/apps/mdns/mdns_domain.c - ) - endif() - - # NetBIOS name server - pico_add_library(pico_lwip_netbios NOFLAG) - target_sources(pico_lwip_netbios INTERFACE - ${PICO_LWIP_PATH}/src/apps/netbiosns/netbiosns.c - ) - - # TFTP server files - pico_add_library(pico_lwip_tftp NOFLAG) - target_sources(pico_lwip_tftp INTERFACE - ${PICO_LWIP_PATH}/src/apps/tftp/tftp.c - ) - - # Mbed TLS files - pico_add_library(pico_lwip_mbedtls NOFLAG) - target_sources(pico_lwip_mbedtls INTERFACE - ${PICO_LWIP_PATH}/src/apps/altcp_tls/altcp_tls_mbedtls.c - ${PICO_LWIP_PATH}/src/apps/altcp_tls/altcp_tls_mbedtls_mem.c - ${PICO_LWIP_PATH}/src/apps/snmp/snmpv3_mbedtls.c - ) - - # MQTT client files - pico_add_library(pico_lwip_mqtt NOFLAG) - target_sources(pico_lwip_mqtt INTERFACE - ${PICO_LWIP_PATH}/src/apps/mqtt/mqtt.c - ) - - # All LWIP files without apps - pico_add_library(pico_lwip NOFLAG) - pico_mirrored_target_link_libraries(pico_lwip INTERFACE - pico_lwip_core - pico_lwip_core4 - pico_lwip_core6 - pico_lwip_api - pico_lwip_netif - pico_lwip_sixlowpan - pico_lwip_ppp - ) - - # our arch/cc.h - pico_add_library(pico_lwip_arch NOFLAG) - target_include_directories(pico_lwip_arch_headers INTERFACE - ${CMAKE_CURRENT_LIST_DIR}/include) - pico_mirrored_target_link_libraries(pico_lwip_arch INTERFACE pico_rand) - - # our nosys impl - pico_add_library(pico_lwip_nosys NOFLAG) - target_sources(pico_lwip_nosys INTERFACE - ${CMAKE_CURRENT_LIST_DIR}/lwip_nosys.c - ) - pico_mirrored_target_link_libraries(pico_lwip_nosys INTERFACE - pico_async_context_base - pico_lwip_arch - pico_lwip) - - if (NOT PICO_LWIP_CONTRIB_PATH) - set(PICO_LWIP_CONTRIB_PATH ${PICO_LWIP_PATH}/contrib) - endif() - pico_register_common_scope_var(PICO_LWIP_CONTRIB_PATH) - - # Make lwip_contrib_freertos library, with the FreeRTOS/lwIP code from lwip-contrib - pico_add_library(pico_lwip_contrib_freertos NOFLAG) - target_sources(pico_lwip_contrib_freertos INTERFACE - ${PICO_LWIP_CONTRIB_PATH}/ports/freertos/sys_arch.c - ) - target_include_directories(pico_lwip_contrib_freertos_headers INTERFACE - ${PICO_LWIP_CONTRIB_PATH}/ports/freertos/include - ) - pico_mirrored_target_link_libraries(pico_lwip_contrib_freertos INTERFACE - pico_lwip_arch) - - pico_add_library(pico_lwip_freertos NOFLAG) - target_sources(pico_lwip_freertos INTERFACE - ${CMAKE_CURRENT_LIST_DIR}/lwip_freertos.c - ) - pico_mirrored_target_link_libraries(pico_lwip_freertos INTERFACE - pico_async_context_base - pico_lwip - pico_lwip_contrib_freertos - pico_rand) - - pico_promote_common_scope_vars() -endif() diff --git a/pico-sdk/src/rp2_common/pico_lwip/doc.h b/pico-sdk/src/rp2_common/pico_lwip/doc.h deleted file mode 100644 index 0825fab..0000000 --- a/pico-sdk/src/rp2_common/pico_lwip/doc.h +++ /dev/null @@ -1,46 +0,0 @@ -/** - * \defgroup pico_lwip pico_lwip - * \brief Integration/wrapper libraries for lwIP - * the documentation for which is here. - * - * The main \c \b pico_lwip library itself aggregates the lwIP RAW API: \c \b pico_lwip_core, \c \b pico_lwip_core4, \c \b pico_lwip_core6, \c \b pico_lwip_api, \c \b pico_lwip_netif, \c \b pico_lwip_sixlowpan and \c \b pico_lwip_ppp. - * - * If you wish to run in NO_SYS=1 mode, then you can link \c \b pico_lwip along with \ref pico_lwip_nosys. - * - * If you wish to run in NO_SYS=0 mode, then you can link \c \b pico_lwip with (for instance) \ref pico_lwip_freertos, - * and also link in pico_lwip_api for the additional blocking/thread-safe APIs. - * - * Additionally you must link in \ref pico_lwip_arch unless you provide your own compiler bindings for lwIP. - * - * Additional individual pieces of lwIP functionality are available à la cart, by linking any of the libraries below. - * - * The following libraries are provided that contain exactly the equivalent lwIP functionality groups: - * - * * \c \b pico_lwip_core - - * * \c \b pico_lwip_core4 - - * * \c \b pico_lwip_core6 - - * * \c \b pico_lwip_netif - - * * \c \b pico_lwip_sixlowpan - - * * \c \b pico_lwip_ppp - - * * \c \b pico_lwip_api - - * - * The following libraries are provided that contain exactly the equivalent lwIP application support: - * - * * \c \b pico_lwip_snmp - - * * \c \b pico_lwip_http - - * * \c \b pico_lwip_makefsdata - - * * \c \b pico_lwip_iperf - - * * \c \b pico_lwip_smtp - - * * \c \b pico_lwip_sntp - - * * \c \b pico_lwip_mdns - - * * \c \b pico_lwip_netbios - - * * \c \b pico_lwip_tftp - - * * \c \b pico_lwip_mbedtls - - * * \c \b pico_lwip_mqtt - - * - */ - -/** \defgroup pico_lwip_arch pico_lwip_arch - * \ingroup pico_lwip - * \brief lwIP compiler adapters. This is not included by default in \c \b pico_lwip in case you wish to implement your own. - */ diff --git a/pico-sdk/src/rp2_common/pico_lwip/include/arch/cc.h b/pico-sdk/src/rp2_common/pico_lwip/include/arch/cc.h deleted file mode 100644 index 7ac155e..0000000 --- a/pico-sdk/src/rp2_common/pico_lwip/include/arch/cc.h +++ /dev/null @@ -1,93 +0,0 @@ -/* - * Copyright (c) 2001-2003 Swedish Institute of Computer Science. - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without modification, - * are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF - * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT - * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT - * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY - * OF SUCH DAMAGE. - * - * This file is part of the lwIP TCP/IP stack. - * - * Author: Adam Dunkels - * - */ -#ifndef __CC_H__ -#define __CC_H__ - -#include - -#ifndef PICO_LWIP_CUSTOM_LOCK_TCPIP_CORE -#define PICO_LWIP_CUSTOM_LOCK_TCPIP_CORE 1 -#endif - -#if NO_SYS -// todo really we should just not allow SYS_LIGHTWEIGHT_PROT for nosys mode (it doesn't do anything anyway) -typedef int sys_prot_t; -#elif PICO_LWIP_CUSTOM_LOCK_TCPIP_CORE -void pico_lwip_custom_lock_tcpip_core(void); -void pico_lwip_custom_unlock_tcpip_core(void); -#define LOCK_TCPIP_CORE() pico_lwip_custom_lock_tcpip_core() -#define UNLOCK_TCPIP_CORE() pico_lwip_custom_unlock_tcpip_core() -#endif - -/* define compiler specific symbols */ -#if defined (__ICCARM__) - -#define PACK_STRUCT_BEGIN -#define PACK_STRUCT_STRUCT -#define PACK_STRUCT_END -#define PACK_STRUCT_FIELD(x) x -#define PACK_STRUCT_USE_INCLUDES - -#elif defined (__CC_ARM) - -#define PACK_STRUCT_BEGIN __packed -#define PACK_STRUCT_STRUCT -#define PACK_STRUCT_END -#define PACK_STRUCT_FIELD(x) x - -#elif defined (__GNUC__) - -#define PACK_STRUCT_BEGIN -#define PACK_STRUCT_STRUCT __attribute__ ((__packed__)) -#define PACK_STRUCT_END -#define PACK_STRUCT_FIELD(x) x - -#elif defined (__TASKING__) - -#define PACK_STRUCT_BEGIN -#define PACK_STRUCT_STRUCT -#define PACK_STRUCT_END -#define PACK_STRUCT_FIELD(x) x - -#endif - -#ifndef LWIP_PLATFORM_ASSERT -#include "pico.h" -#define LWIP_PLATFORM_ASSERT(x) panic(x) -#endif - -#ifndef LWIP_RAND -#include "pico/rand.h" -// Use the pico_rand library which goes to reasonable lengths to try to provide good entropy -#define LWIP_RAND() get_rand_32() -#endif -#endif /* __CC_H__ */ diff --git a/pico-sdk/src/rp2_common/pico_lwip/include/pico/lwip_freertos.h b/pico-sdk/src/rp2_common/pico_lwip/include/pico/lwip_freertos.h deleted file mode 100644 index b1b9ab5..0000000 --- a/pico-sdk/src/rp2_common/pico_lwip/include/pico/lwip_freertos.h +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef _PICO_LWIP_FREERTOS_H -#define _PICO_LWIP_FREERTOS_H - -#include "pico.h" -#include "pico/async_context.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** \file pico/lwip_freertos.h -* \defgroup pico_lwip_freertos pico_lwip_freertos -* \ingroup pico_lwip -* \brief Glue library for integration lwIP in \c NO_SYS=0 mode with the SDK. Simple \c init and \c deinit -* are all that is required to hook up lwIP (with full blocking API support) via an \ref async_context instance. -*/ - -/*! \brief Initializes lwIP (NO_SYS=0 mode) support support for FreeRTOS using the provided async_context - * \ingroup pico_lwip_freertos - * - * If the initialization succeeds, \ref lwip_freertos_deinit() can be called to shutdown lwIP support - * - * \param context the async_context instance that provides the abstraction for handling asynchronous work. Note in general - * this would be an \ref async_context_freertos instance, though it doesn't have to be. - * - * \return true if the initialization succeeded -*/ -bool lwip_freertos_init(async_context_t *context); - -/*! \brief De-initialize lwIP (NO_SYS=0 mode) support for FreeRTOS - * \ingroup pico_lwip_freertos - * - * Note that since lwIP may only be initialized once, and doesn't itself provide a shutdown mechanism, lwIP - * itself may still consume resources. - * - * It is however safe to call \ref lwip_freertos_init again later. - * - * \param context the async_context the lwip_freertos support was added to via \ref lwip_freertos_init -*/ -void lwip_freertos_deinit(async_context_t *context); - -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file diff --git a/pico-sdk/src/rp2_common/pico_lwip/include/pico/lwip_nosys.h b/pico-sdk/src/rp2_common/pico_lwip/include/pico/lwip_nosys.h deleted file mode 100644 index cdde9ab..0000000 --- a/pico-sdk/src/rp2_common/pico_lwip/include/pico/lwip_nosys.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#ifndef _PICO_LWIP_NOSYS_H -#define _PICO_LWIP_NOSYS_H - -#include "pico.h" -#include "pico/async_context.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/** \file pico/lwip_nosys.h -* \defgroup pico_lwip_nosys pico_lwip_nosys -* \ingroup pico_lwip -* \brief Glue library for integration lwIP in \c NO_SYS=1 mode with the SDK. Simple \c init and \c deinit -* are all that is required to hook up lwIP via an \ref async_context instance. -*/ - -/*! \brief Initializes lwIP (NO_SYS=1 mode) support support using the provided async_context -* \ingroup pico_lwip_nosys -* -* If the initialization succeeds, \ref lwip_nosys_deinit() can be called to shutdown lwIP support -* -* \param context the async_context instance that provides the abstraction for handling asynchronous work. -* \return true if the initialization succeeded -*/ -bool lwip_nosys_init(async_context_t *context); - -/*! \brief De-initialize lwIP (NO_SYS=1 mode) support - * \ingroup pico_lwip_nosys - * - * Note that since lwIP may only be initialized once, and doesn't itself provide a shutdown mechanism, lwIP - * itself may still consume resources - * - * It is however safe to call \ref lwip_nosys_init again later. - * - * \param context the async_context the lwip_nosys support was added to via \ref lwip_nosys_init -*/ -void lwip_nosys_deinit(async_context_t *context); - -#ifdef __cplusplus -} -#endif -#endif \ No newline at end of file diff --git a/pico-sdk/src/rp2_common/pico_lwip/lwip_freertos.c b/pico-sdk/src/rp2_common/pico_lwip/lwip_freertos.c deleted file mode 100644 index 8f178d1..0000000 --- a/pico-sdk/src/rp2_common/pico_lwip/lwip_freertos.c +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -// todo graham #ifdef for LWIP inclusion? - -#include "pico/async_context.h" -#include "pico/time.h" -#include "lwip/tcpip.h" -#include "lwip/timeouts.h" - -#include "FreeRTOS.h" -#include "semphr.h" - -#if NO_SYS -#error lwip_freertos_async_context_bindings requires NO_SYS=0 -#endif - -static async_context_t * volatile lwip_context; -// lwIP tcpip_task cannot be shutdown, so we block it when we are de-initialized. -static SemaphoreHandle_t tcpip_task_blocker; - -static void tcpip_init_done(void *param) { - xSemaphoreGive((SemaphoreHandle_t)param); -} - -bool lwip_freertos_init(async_context_t *context) { - assert(!lwip_context); - lwip_context = context; - static bool done_lwip_init; - if (!done_lwip_init) { - done_lwip_init = true; - SemaphoreHandle_t init_sem = xSemaphoreCreateBinary(); - tcpip_task_blocker = xSemaphoreCreateBinary(); - tcpip_init(tcpip_init_done, init_sem); - xSemaphoreTake(init_sem, portMAX_DELAY); - vSemaphoreDelete(init_sem); - } else { - xSemaphoreGive(tcpip_task_blocker); - } - return true; -} - -static uint32_t clear_lwip_context(__unused void *param) { - lwip_context = NULL; - return 0; -} - -void lwip_freertos_deinit(__unused async_context_t *context) { - // clear the lwip context under lock as lwIP may still be running in tcpip_task - async_context_execute_sync(context, clear_lwip_context, NULL); -} - -void pico_lwip_custom_lock_tcpip_core(void) { - while (!lwip_context) { - xSemaphoreTake(tcpip_task_blocker, portMAX_DELAY); - } - async_context_acquire_lock_blocking(lwip_context); -} - -void pico_lwip_custom_unlock_tcpip_core(void) { - async_context_release_lock(lwip_context); -} diff --git a/pico-sdk/src/rp2_common/pico_lwip/lwip_nosys.c b/pico-sdk/src/rp2_common/pico_lwip/lwip_nosys.c deleted file mode 100644 index 856affa..0000000 --- a/pico-sdk/src/rp2_common/pico_lwip/lwip_nosys.c +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Copyright (c) 2022 Raspberry Pi (Trading) Ltd. - * - * SPDX-License-Identifier: BSD-3-Clause - */ - -#include "pico/async_context.h" - -#include -#include "lwip/timeouts.h" - -static void update_next_timeout(async_context_t *context, async_when_pending_worker_t *worker); -static void lwip_timeout_reached(async_context_t *context, async_at_time_worker_t *worker); - -static async_when_pending_worker_t always_pending_update_timeout_worker = { - .do_work = update_next_timeout -}; - -static async_at_time_worker_t lwip_timeout_worker = { - .do_work = lwip_timeout_reached, -}; - -static void lwip_timeout_reached(__unused async_context_t *context, __unused async_at_time_worker_t *worker) { - assert(worker == &lwip_timeout_worker); - sys_check_timeouts(); -} - -static void update_next_timeout(async_context_t *context, async_when_pending_worker_t *worker) { - assert(worker == &always_pending_update_timeout_worker); - // we want to run on every execution of the helper to re-reflect any changes - // to the underlying lwIP timers which may have happened in the interim - // (note that worker will be called on every outermost exit of the async_context - // lock, and lwIP timers should not be modified whilst not holding the lock. - worker->work_pending = true; - uint32_t sleep_ms = sys_timeouts_sleeptime(); - if (sleep_ms == SYS_TIMEOUTS_SLEEPTIME_INFINITE) { - lwip_timeout_worker.next_time = at_the_end_of_time; - } else { - lwip_timeout_worker.next_time = make_timeout_time_ms(sleep_ms); - } - async_context_add_at_time_worker(context, &lwip_timeout_worker); -} - -bool lwip_nosys_init(async_context_t *context) { - static bool done_lwip_init; - if (!done_lwip_init) { - lwip_init(); - done_lwip_init = true; - } - // we want the worker to be called on every async helper run (starting with the next) - always_pending_update_timeout_worker.work_pending = true; - async_context_add_when_pending_worker(context, &always_pending_update_timeout_worker); - return true; -} - -void lwip_nosys_deinit(async_context_t *context) { - async_context_remove_at_time_worker(context, &lwip_timeout_worker); - async_context_remove_when_pending_worker(context, &always_pending_update_timeout_worker); -} - -#if NO_SYS -/* lwip has provision for using a mutex, when applicable */ -sys_prot_t sys_arch_protect(void) { - return 0; -} - -void sys_arch_unprotect(__unused sys_prot_t pval) { -} - -/* lwip needs a millisecond time source, and the TinyUSB board support code has one available */ -uint32_t sys_now(void) { - return to_ms_since_boot(get_absolute_time()); -} -#endif \ No newline at end of file diff --git a/pico-sdk/src/rp2_common/pico_mbedtls/CMakeLists.txt b/pico-sdk/src/rp2_common/pico_mbedtls/CMakeLists.txt deleted file mode 100644 index 303a03a..0000000 --- a/pico-sdk/src/rp2_common/pico_mbedtls/CMakeLists.txt +++ /dev/null @@ -1,173 +0,0 @@ -if (DEFINED ENV{PICO_MBEDTLS_PATH} AND (NOT PICO_MBEDTLS_PATH)) - set(PICO_MBEDTLS_PATH $ENV{PICO_MBEDTLS_PATH}) - message("Using PICO_MBEDTLS_PATH from environment ('${PICO_MBEDTLS_PATH}')") -endif() - -set(MBEDTLS_TEST_PATH "library/aes.c") -if (NOT PICO_MBEDTLS_PATH) - set(PICO_MBEDTLS_PATH ${PROJECT_SOURCE_DIR}/lib/mbedtls) -elseif (NOT EXISTS ${PICO_MBEDTLS_PATH}/${MBEDTLS_TEST_PATH}) - message(WARNING "PICO_MBEDTLS_PATH specified but content not present.") -endif() - -if (EXISTS ${PICO_MBEDTLS_PATH}/${MBEDTLS_TEST_PATH}) - message("mbedtls available at ${PICO_MBEDTLS_PATH}") - - pico_register_common_scope_var(PICO_MBEDTLS_PATH) - - set(src_crypto - aes.c - aesni.c - arc4.c - aria.c - asn1parse.c - asn1write.c - base64.c - bignum.c - blowfish.c - camellia.c - ccm.c - chacha20.c - chachapoly.c - cipher.c - cipher_wrap.c - constant_time.c - cmac.c - ctr_drbg.c - des.c - dhm.c - ecdh.c - ecdsa.c - ecjpake.c - ecp.c - ecp_curves.c - entropy.c - entropy_poll.c - error.c - gcm.c - havege.c - hkdf.c - hmac_drbg.c - md.c - md2.c - md4.c - md5.c - memory_buffer_alloc.c - mps_reader.c - mps_trace.c - nist_kw.c - oid.c - padlock.c - pem.c - pk.c - pk_wrap.c - pkcs12.c - pkcs5.c - pkparse.c - pkwrite.c - platform.c - platform_util.c - poly1305.c - psa_crypto.c - psa_crypto_aead.c - psa_crypto_cipher.c - psa_crypto_client.c - psa_crypto_driver_wrappers.c - psa_crypto_ecp.c - psa_crypto_hash.c - psa_crypto_mac.c - psa_crypto_rsa.c - psa_crypto_se.c - psa_crypto_slot_management.c - psa_crypto_storage.c - psa_its_file.c - ripemd160.c - rsa.c - rsa_internal.c - sha1.c - sha256.c - sha512.c - threading.c - timing.c - version.c - version_features.c - xtea.c - ) - list(TRANSFORM src_crypto PREPEND ${PICO_MBEDTLS_PATH}/library/) - pico_add_library(pico_mbedtls_crypto NOFLAG) - target_sources(pico_mbedtls_crypto INTERFACE ${src_crypto}) - - set(src_x509 - certs.c - pkcs11.c - x509.c - x509_create.c - x509_crl.c - x509_crt.c - x509_csr.c - x509write_crt.c - x509write_csr.c - ) - list(TRANSFORM src_x509 PREPEND ${PICO_MBEDTLS_PATH}/library/) - pico_add_library(pico_mbedtls_x509 NOFLAG) - target_sources(pico_mbedtls_x509 INTERFACE ${src_x509}) - - set(src_tls - debug.c - net_sockets.c - ssl_cache.c - ssl_ciphersuites.c - ssl_cli.c - ssl_cookie.c - ssl_msg.c - ssl_srv.c - ssl_ticket.c - ssl_tls.c - ssl_tls13_keys.c - ) - list(TRANSFORM src_tls PREPEND ${PICO_MBEDTLS_PATH}/library/) - pico_add_library(pico_mbedtls_tls NOFLAG) - target_sources(pico_mbedtls_tls INTERFACE ${src_tls}) - - pico_add_library(pico_mbedtls NOFLAG) - pico_mirrored_target_link_libraries(pico_mbedtls INTERFACE pico_mbedtls_crypto pico_mbedtls_x509 pico_mbedtls_tls pico_rand) - if (DEFINED PICO_MBEDTLS_CONFIG_FILE) - target_compile_definitions(pico_mbedtls_headers INTERFACE MBEDTLS_CONFIG_FILE="${PICO_MBEDTLS_CONFIG_FILE}") - else() - target_compile_definitions(pico_mbedtls_headers INTERFACE MBEDTLS_CONFIG_FILE="mbedtls_config.h") - endif() - target_sources(pico_mbedtls INTERFACE ${CMAKE_CURRENT_LIST_DIR}/pico_mbedtls.c) - target_include_directories(pico_mbedtls_headers INTERFACE ${PICO_MBEDTLS_PATH}/include/ ${PICO_MBEDTLS_PATH}/library/) - - function(suppress_mbedtls_warnings) - set_source_files_properties( - ${PICO_MBEDTLS_PATH}/library/ecdsa.c - ${PICO_MBEDTLS_PATH}/library/ecp.c - ${PICO_MBEDTLS_PATH}/library/ecp_curves.c - ${PICO_MBEDTLS_PATH}/library/pk_wrap.c - ${PICO_MBEDTLS_PATH}/library/pkparse.c - ${PICO_MBEDTLS_PATH}/library/ssl_cli.c - PROPERTIES - COMPILE_OPTIONS "-Wno-cast-qual" - ) - set_source_files_properties( - ${PICO_MBEDTLS_PATH}/library/psa_crypto_client.c - ${PICO_MBEDTLS_PATH}/library/psa_crypto_driver_wrappers.c - PROPERTIES - COMPILE_OPTIONS "-Wno-redundant-decls" - ) - set_source_files_properties( - ${PICO_MBEDTLS_PATH}/library/x509_crt.c - PROPERTIES - COMPILE_OPTIONS "-Wno-cast-qual;-Wno-null-dereference" - ) - set_source_files_properties( - ${PICO_MBEDTLS_PATH}/library/ssl_srv.c - ${PICO_MBEDTLS_PATH}/library/ssl_tls.c - PROPERTIES - COMPILE_OPTIONS "-Wno-null-dereference" - ) - endfunction() - - pico_promote_common_scope_vars() -endif() diff --git a/pico-sdk/src/rp2_common/pico_mbedtls/pico_mbedtls.c b/pico-sdk/src/rp2_common/pico_mbedtls/pico_mbedtls.c deleted file mode 100644 index 5878956..0000000 --- a/pico-sdk/src/rp2_common/pico_mbedtls/pico_mbedtls.c +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include "pico/platform.h" -#include "pico/rand.h" - -/* Function to feed mbedtls entropy. */ -int mbedtls_hardware_poll(void *data __unused, unsigned char *output, size_t len, size_t *olen) { - *olen = 0; - while(*olen < len) { - uint64_t rand_data = get_rand_64(); - size_t to_copy = MIN(len, sizeof(rand_data)); - memcpy(output + *olen, &rand_data, to_copy); - *olen += to_copy; - } - return 0; -} diff --git a/pico-sdk/tools/pioasm/CMakeLists.txt b/pico-sdk/tools/pioasm/CMakeLists.txt index 322408a..6fe3817 100644 --- a/pico-sdk/tools/pioasm/CMakeLists.txt +++ b/pico-sdk/tools/pioasm/CMakeLists.txt @@ -1,4 +1,4 @@ -cmake_minimum_required(VERSION 3.4) +cmake_minimum_required(VERSION 3.6...3.27) project(pioasm CXX) set(CMAKE_CXX_STANDARD 11) diff --git a/src/constants.c b/src/constants.c new file mode 100644 index 0000000..ee08596 --- /dev/null +++ b/src/constants.c @@ -0,0 +1,38 @@ +#include "main.h" + +/* CRC32 Lookup Table, Polynomial = 0xEDB88320 */ +const uint32_t crc32_lookup_table[] = { + 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, + 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, + 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, + 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, 0xfa0f3d63, 0x8d080df5, + 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, + 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, + 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599, 0xb8bda50f, + 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, + 0x76dc4190, 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, + 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01, + 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, + 0x65b0d9c6, 0x12b7e950, 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, + 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, + 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, + 0x5005713c, 0x270241aa, 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, + 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, + 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, + 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, + 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, 0x196c3671, 0x6e6b06e7, + 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, + 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, + 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef, 0x4669be79, + 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, + 0xc5ba3bbe, 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, + 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713, + 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, + 0x86d3d2d4, 0xf1d4e242, 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, + 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, + 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, + 0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, + 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf, + 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d +}; + diff --git a/src/defaults.c b/src/defaults.c index b3e6418..a99d11b 100644 --- a/src/defaults.c +++ b/src/defaults.c @@ -18,7 +18,7 @@ const config_t default_config = { .os = OUTPUT_A_OS, .pos = RIGHT, .screensaver = { - .enabled = SCREENSAVER_A_ENABLED, + .mode = SCREENSAVER_A_MODE, .only_if_inactive = SCREENSAVER_A_ONLY_IF_INACTIVE, .idle_time_us = SCREENSAVER_A_IDLE_TIME_SEC * 1000000, .max_time_us = SCREENSAVER_A_MAX_TIME_SEC * 1000000, @@ -38,10 +38,17 @@ const config_t default_config = { .os = OUTPUT_B_OS, .pos = LEFT, .screensaver = { - .enabled = SCREENSAVER_B_ENABLED, + .mode = SCREENSAVER_B_MODE, .only_if_inactive = SCREENSAVER_B_ONLY_IF_INACTIVE, .idle_time_us = SCREENSAVER_B_IDLE_TIME_SEC * 1000000, .max_time_us = SCREENSAVER_B_MAX_TIME_SEC * 1000000, } }, + .enforce_ports = ENFORCE_PORTS, + .force_kbd_boot_protocol = ENFORCE_KEYBOARD_BOOT_PROTOCOL, + .force_mouse_boot_mode = false, + .enable_acceleration = ENABLE_ACCELERATION, + .hotkey_toggle = HID_KEY_F24, + .kbd_led_as_indicator = KBD_LED_AS_INDICATOR, + .jump_treshold = 0, }; \ No newline at end of file diff --git a/src/handlers.c b/src/handlers.c index 3566688..c89a7ab 100644 --- a/src/handlers.c +++ b/src/handlers.c @@ -31,12 +31,12 @@ void output_toggle_hotkey_handler(device_t *state, hid_keyboard_report_t *report switch_output(state, state->active_output); }; -void get_border_position(device_t *state, border_size_t *border) { +void _get_border_position(device_t *state, border_size_t *border) { /* To avoid having 2 different keys, if we're above half, it's the top coord */ - if (state->mouse_y > (MAX_SCREEN_COORD / 2)) - border->bottom = state->mouse_y; + if (state->pointer_y > (MAX_SCREEN_COORD / 2)) + border->bottom = state->pointer_y; else - border->top = state->mouse_y; + border->top = state->pointer_y; } @@ -44,11 +44,11 @@ void get_border_position(device_t *state, border_size_t *border) { void screen_border_hotkey_handler(device_t *state, hid_keyboard_report_t *report) { border_size_t *border = &state->config.output[state->active_output].border; if (CURRENT_BOARD_IS_ACTIVE_OUTPUT) { - get_border_position(state, border); + _get_border_position(state, border); save_config(state); } - send_packet((uint8_t *)border, SYNC_BORDERS_MSG, sizeof(border_size_t)); + queue_packet((uint8_t *)border, SYNC_BORDERS_MSG, sizeof(border_size_t)); }; /* This key combo puts board A in firmware upgrade mode */ @@ -67,37 +67,6 @@ void switchlock_hotkey_handler(device_t *state, hid_keyboard_report_t *report) { send_value(state->switch_lock, SWITCH_LOCK_MSG); } -/* This key combo configures multiple output parameters */ -void output_config_hotkey_handler(device_t *state, hid_keyboard_report_t *report) { - output_t *current = &state->config.output[state->active_output]; - - /* Pressing 1 or 2 with this hotkey sets the screen count */ - if(key_in_report(HID_KEY_1, report)) - current->screen_count = 1; - else if (key_in_report(HID_KEY_2, report)) - current->screen_count = 2; - - /* Pressing 7, 8 or 9 with this hotkey sets the OS to LINUX, WIN or MAC */ - else if (key_in_report(HID_KEY_7, report)) - current->os = LINUX; - else if (key_in_report(HID_KEY_8, report)) - current->os = WINDOWS; - else if (key_in_report(HID_KEY_9, report)) - current->os = MACOS; - - /* If nothing matches, don't send or save anything but bail out. */ - else - return; - - /* Save config and acknowledge */ - save_config(state); - blink_led(state); - - /* 4 bits are more than enough to transfer this */ - uint8_t value = current->screen_count | (current->os << 4); - send_value(value, OUTPUT_CONFIG_MSG); -} - /* This key combo locks both outputs simultaneously */ void screenlock_hotkey_handler(device_t *state, hid_keyboard_report_t *report) { hid_keyboard_report_t lock_report = {0}, release_keys = {0}; @@ -110,19 +79,19 @@ void screenlock_hotkey_handler(device_t *state, hid_keyboard_report_t *report) { lock_report.keycode[0] = HID_KEY_L; break; case MACOS: - lock_report.modifier = KEYBOARD_MODIFIER_LEFTCTRL | KEYBOARD_MODIFIER_LEFTGUI; + lock_report.modifier = KEYBOARD_MODIFIER_LEFTCTRL | KEYBOARD_MODIFIER_LEFTALT; lock_report.keycode[0] = HID_KEY_Q; break; default: break; } - if (BOARD_ROLE == out) { + if (global_state.active_output == out) { queue_kbd_report(&lock_report, state); release_all_keys(state); } else { - send_packet((uint8_t *)&lock_report, KEYBOARD_REPORT_MSG, KBD_REPORT_LENGTH); - send_packet((uint8_t *)&release_keys, KEYBOARD_REPORT_MSG, KBD_REPORT_LENGTH); + queue_packet((uint8_t *)&lock_report, KEYBOARD_REPORT_MSG, KBD_REPORT_LENGTH); + queue_packet((uint8_t *)&release_keys, KEYBOARD_REPORT_MSG, KBD_REPORT_LENGTH); } } } @@ -134,17 +103,26 @@ void wipe_config_hotkey_handler(device_t *state, hid_keyboard_report_t *report) send_value(ENABLE, WIPE_CONFIG_MSG); } -void screensaver_hotkey_handler(device_t *state, hid_keyboard_report_t *report) { - state->config.output[BOARD_ROLE].screensaver.enabled ^= 1; - send_value(state->config.output[BOARD_ROLE].screensaver.enabled, SCREENSAVER_MSG); -} - /* When pressed, toggles the current mouse zoom mode state */ void mouse_zoom_hotkey_handler(device_t *state, hid_keyboard_report_t *report) { state->mouse_zoom ^= 1; send_value(state->mouse_zoom, MOUSE_ZOOM_MSG); }; + +/* Put the device into a special configuration mode */ +void config_enable_hotkey_handler(device_t *state, hid_keyboard_report_t *report) { + /* If config mode is already active, skip this and reboot to return to normal mode */ + if (!state->config_mode_active) { + watchdog_hw->scratch[5] = MAGIC_WORD_1; + watchdog_hw->scratch[6] = MAGIC_WORD_2; + } + + release_all_keys(state); + state->reboot_requested = true; +}; + + /**==================================================== * * ========== UART Message Handling Routines ======== * * ==================================================== */ @@ -160,9 +138,9 @@ void handle_mouse_abs_uart_msg(uart_packet_t *packet, device_t *state) { mouse_report_t *mouse_report = (mouse_report_t *)packet->data; queue_mouse_report(mouse_report, state); - state->mouse_x = mouse_report->x; - state->mouse_y = mouse_report->y; - state->mouse_buttons = mouse_report->buttons; + state->pointer_x = mouse_report->x; + state->pointer_y = mouse_report->y; + state->mouse_buttons = mouse_report->buttons; state->last_activity[BOARD_ROLE] = time_us_64(); } @@ -202,8 +180,8 @@ void handle_sync_borders_msg(uart_packet_t *packet, device_t *state) { border_size_t *border = &state->config.output[state->active_output].border; if (CURRENT_BOARD_IS_ACTIVE_OUTPUT) { - get_border_position(state, border); - send_packet((uint8_t *)border, SYNC_BORDERS_MSG, sizeof(border_size_t)); + _get_border_position(state, border); + queue_packet((uint8_t *)border, SYNC_BORDERS_MSG, sizeof(border_size_t)); } else memcpy(border, packet->data, sizeof(border_size_t)); @@ -221,21 +199,120 @@ void handle_wipe_config_msg(uart_packet_t *packet, device_t *state) { load_config(state); } -void handle_screensaver_msg(uart_packet_t *packet, device_t *state) { - state->config.output[BOARD_ROLE].screensaver.enabled = packet->data[0]; -} - -void handle_output_config_msg(uart_packet_t *packet, device_t *state) { - state->config.output[state->active_output].os = packet->data[0] >> 4; - state->config.output[state->active_output].screen_count = packet->data[0] & 0x0F; - save_config(state); -} - -/* Process consumer control keyboard message. Send immediately, w/o queing */ +/* Process consumer control message, TODO: use queue instead of sending directly */ void handle_consumer_control_msg(uart_packet_t *packet, device_t *state) { tud_hid_n_report(0, REPORT_ID_CONSUMER, &packet->data[0], CONSUMER_CONTROL_LENGTH); } +/* Process request to store config to flash */ +void handle_save_config_msg(uart_packet_t *packet, device_t *state) { + save_config(state); +} + +/* Process request to reboot the board */ +void handle_reboot_msg(uart_packet_t *packet, device_t *state) { + reboot(); +} + +/* Decapsulate and send to the other box */ +void handle_proxy_msg(uart_packet_t *packet, device_t *state) { + queue_packet(&packet->data[1], (enum packet_type_e)packet->data[0], PACKET_DATA_LENGTH - 1); +} + +/* Process api communication messages */ +void handle_api_msgs(uart_packet_t *packet, device_t *state) { + uint8_t value_idx = packet->data[0]; + const field_map_t *map = get_field_map_entry(value_idx); + + /* If we don't have a valid map entry, return immediately */ + if (map == NULL) + return; + + /* Create a pointer to the offset into the structure we need to access */ + uint8_t *ptr = (((uint8_t *)&global_state) + map->offset); + + if (packet->type == SET_VAL_MSG) { + /* Not allowing writes to objects defined as read-only */ + if (map->readonly) + return; + + memcpy(ptr, &packet->data[1], map->len); + } + else if (packet->type == GET_VAL_MSG) { + uart_packet_t response = {.type=GET_VAL_MSG, .data={0}}; + memcpy(response.data, ptr, map->len); + queue_try_add(&state->cfg_queue_out, &response); + } + + /* With each GET/SET message, we reset the configuration mode timeout */ + reset_config_timer(state); +} + + +/* Process request packet and create a response */ +void handle_request_byte_msg(uart_packet_t *packet, device_t *state) { + uint32_t address = packet->data32[0]; + + if (address > STAGING_IMAGE_SIZE) + return; + + /* Add requested data to bytes 4-7 in the packet and return it with a different type */ + uint32_t data = *(uint32_t *)&ADDR_FW_RUNNING[address]; + packet->data32[1] = data; + + queue_packet(packet->data, RESPONSE_BYTE_MSG, PACKET_DATA_LENGTH); +} + +/* Process response message following a request we sent to read a byte */ +/* state->page_offset and state->page_number are kept locally and compared to returned values */ +void handle_response_byte_msg(uart_packet_t *packet, device_t *state) { + uint16_t offset = packet->data[0]; + uint32_t address = packet->data32[0]; + + if (address != state->fw.address) { + state->fw.upgrade_in_progress = false; + state->fw.address = 0; + return; + } + else { + /* Provide visual feedback of the ongoing copy by toggling LED for every sector */ + if((address & 0xfff) == 0x000) + toggle_led(); + } + + /* Update checksum as we receive each byte */ + if (address < STAGING_IMAGE_SIZE - FLASH_SECTOR_SIZE) + for (int i=0; i<4; i++) + state->fw.checksum = crc32_iter(state->fw.checksum, packet->data[4 + i]); + + memcpy(state->page_buffer + offset, &packet->data32[1], sizeof(uint32_t)); + + /* Neeeeeeext byte, please! */ + state->fw.address += sizeof(uint32_t); + state->fw.byte_done = true; +} + +/* Process a request to read a firmware package from flash */ +void handle_heartbeat_msg(uart_packet_t *packet, device_t *state) { + uint16_t other_running_version = packet->data16[0]; + + if (state->fw.upgrade_in_progress) + return; + + /* If the other board isn't running a newer version, we are done */ + if (other_running_version <= state->_running_fw.version) + return; + + /* It is? Ok, kick off the firmware upgrade */ + state->fw = (fw_upgrade_state_t) { + .upgrade_in_progress = true, + .byte_done = true, + .address = 0, + .checksum = 0xffffffff, + }; +} + + /**==================================================== * * ============== Output Switch Routines ============ * * ==================================================== */ diff --git a/src/hid_parser.c b/src/hid_parser.c index 123e3b4..78de004 100644 --- a/src/hid_parser.c +++ b/src/hid_parser.c @@ -20,9 +20,10 @@ #include "main.h" -#define IS_BLOCK_END (collection.start == collection.end) +#define IS_BLOCK_END (parser->collection.start == parser->collection.end) enum { SIZE_0_BIT = 0, SIZE_8_BIT = 1, SIZE_16_BIT = 2, SIZE_32_BIT = 3 }; +const uint8_t SIZE_LOOKUP[4] = {0, 1, 2, 4}; /* Size is 0, 1, 2, or 3, describing cases of no data, 8-bit, 16-bit, or 32-bit data. */ @@ -39,132 +40,108 @@ uint32_t get_descriptor_value(uint8_t const *report, int size) { } } -/* We store all globals as unsigned to avoid countless switch/cases. -In case of e.g. min/max, we need to treat some data as signed retroactively. */ -int32_t to_signed(globals_t *data) { - switch (data->hdr.size) { - case SIZE_8_BIT: - return (int8_t)data->val; - case SIZE_16_BIT: - return (int16_t)data->val; - default: - return data->val; - } -} - -/* Given a value struct with size and offset in bits, - find and return a value from the HID report */ - -int32_t get_report_value(uint8_t *report, report_val_t *val) { - /* Calculate the bit offset within the byte */ - uint8_t offset_in_bits = val->offset % 8; - - /* Calculate the remaining bits in the first byte */ - uint8_t remaining_bits = 8 - offset_in_bits; - - /* Calculate the byte offset in the array */ - uint8_t byte_offset = val->offset >> 3; - - /* Create a mask for the specified number of bits */ - uint32_t mask = (1u << val->size) - 1; - - /* Initialize the result value with the bits from the first byte */ - int32_t result = report[byte_offset] >> offset_in_bits; - - /* Move to the next byte and continue fetching bits until the desired length is reached */ - while (val->size > remaining_bits) { - result |= report[++byte_offset] << remaining_bits; - remaining_bits += 8; - } - - /* Apply the mask to retain only the desired number of bits */ - result = result & mask; - - /* Special case if result is negative. - Check if the most significant bit of 'val' is set */ - if (result & ((mask >> 1) + 1)) { - /* If it is set, sign-extend 'val' by filling the higher bits with 1s */ - result |= (0xFFFFFFFFU << val->size); - } - - return result; -} - void update_usage(parser_state_t *parser, int i) { /* If we don't have as many usages as elements, the usage for the previous element applies */ - if (i && i >= parser->usage_count) { - *(parser->p_usage + i) = *(parser->p_usage + parser->usage_count - 1); - } + if (i > 0 && i >= parser->usage_count && i < HID_MAX_USAGES) + *(parser->p_usage + i) = *(parser->p_usage + i - 1); } -void find_and_store_element(parser_state_t *parser, int map_len, int i) { - usage_map_t *map = &parser->map[0]; +void store_element(parser_state_t *parser, report_val_t *val, int i, uint32_t data, uint16_t size, hid_interface_t *iface) { + *val = (report_val_t){ + .offset = parser->offset_in_bits, + .offset_idx = parser->offset_in_bits >> 3, + .size = size, - for (int j = 0; j < map_len; j++, map++) { - /* Filter based on usage criteria */ - if (map->report_usage == parser->global_usage - && map->usage_page == parser->globals[RI_GLOBAL_USAGE_PAGE].val - && map->usage == *(parser->p_usage + i)) { + .usage_max = parser->locals[RI_LOCAL_USAGE_MAX].val, + .usage_min = parser->locals[RI_LOCAL_USAGE_MIN].val, - /* Buttons are the ones that appear multiple times, aggregate for now */ - if (map->element->size) { - map->element->size++; - continue; - } + .item_type = (data & 0x01) ? CONSTANT : DATA, + .data_type = (data & 0x02) ? VARIABLE : ARRAY, - /* Store the found element's attributes */ - map->element->offset = parser->offset_in_bits; - map->element->size = parser->globals[RI_GLOBAL_REPORT_SIZE].val; - map->element->min = to_signed(&parser->globals[RI_GLOBAL_LOGICAL_MIN]); - map->element->max = to_signed(&parser->globals[RI_GLOBAL_LOGICAL_MAX]); - } - } + .usage = *(parser->p_usage + i), + .usage_page = parser->globals[RI_GLOBAL_USAGE_PAGE].val, + .global_usage = parser->global_usage, + .report_id = parser->report_id + }; + + iface->uses_report_id |= (parser->report_id != 0); } -void handle_global_item(parser_state_t *parser, header_t *header, uint32_t data, mouse_t *mouse) { +void handle_global_item(parser_state_t *parser, item_t *item) { + if (item->hdr.tag == RI_GLOBAL_REPORT_ID) + parser->report_id = item->val; + + parser->globals[item->hdr.tag] = *item; +} + +void handle_local_item(parser_state_t *parser, item_t *item) { /* There are just 16 possible tags, store any one that comes along to an array instead of doing switch and 16 cases */ - parser->globals[header->tag].val = data; - parser->globals[header->tag].hdr = *header; + parser->locals[item->hdr.tag] = *item; - if (header->tag == RI_GLOBAL_REPORT_ID) { - /* Important to track, if report IDs are used reports are preceded/offset by a 1-byte ID value */ - if (parser->global_usage == HID_USAGE_DESKTOP_MOUSE) - mouse->report_id = data; + if (item->hdr.tag == RI_LOCAL_USAGE) { + if(IS_BLOCK_END) + parser->global_usage = item->val; - mouse->uses_report_id = true; + else if (parser->usage_count < HID_MAX_USAGES - 1) + *(parser->p_usage + parser->usage_count++) = item->val; } } -void handle_local_item(parser_state_t *parser, header_t *header, uint32_t data) { - if (header->tag == RI_LOCAL_USAGE) { - /* If we are not within a collection, the usage tag applies to the entire section */ - if (parser->collection.start == parser->collection.end) { - parser->global_usage = data; - } else { - *(parser->p_usage + parser->usage_count++) = data; - } +void handle_main_input(parser_state_t *parser, item_t *item, hid_interface_t *iface) { + uint32_t size = parser->globals[RI_GLOBAL_REPORT_SIZE].val; + uint32_t count = parser->globals[RI_GLOBAL_REPORT_COUNT].val; + report_val_t val = {0}; + + /* Swap count and size for 1-bit variables, it makes sense to process e.g. NKRO with + size = 1 and count = 240 in one go instead of doing 240 iterations + Don't do this if there are usages in the queue, though. + */ + if (size == 1 && parser->usage_count <= 1) { + size = count; + count = 1; } + + for (int i = 0; i < count; i++) { + update_usage(parser, i); + store_element(parser, &val, i, item->val, size, iface); + + /* Use the parsed data to populate internal device structures */ + extract_data(iface, &val); + + /* Iterate times and increase offset by amount, moving by x bits */ + parser->offset_in_bits += size; + } + + /* Advance the usage array pointer by global report count and reset the count variable */ + parser->p_usage += parser->usage_count; + + /* Carry the last usage to the new location */ + *parser->p_usage = *(parser->p_usage - parser->usage_count); } -void handle_main_item(parser_state_t *parser, header_t *header, int map_len) { - /* Update Collection */ - parser->collection.start += (header->tag == RI_MAIN_COLLECTION); - parser->collection.end += (header->tag == RI_MAIN_COLLECTION_END); +void handle_main_item(parser_state_t *parser, item_t *item, hid_interface_t *iface) { + if (IS_BLOCK_END) + parser->offset_in_bits = 0; - if (header->tag == RI_MAIN_INPUT) { - for (int i = 0; i < parser->globals[RI_GLOBAL_REPORT_COUNT].val; i++) { - update_usage(parser, i); - find_and_store_element(parser, map_len, i); + switch (item->hdr.tag) { + case RI_MAIN_COLLECTION: + parser->collection.start++; + break; - /* Iterate times and increase offset by amount, moving by x bits */ - parser->offset_in_bits += parser->globals[RI_GLOBAL_REPORT_SIZE].val; - } + case RI_MAIN_COLLECTION_END: + parser->collection.end++; + break; - /* Advance the usage array pointer by global report count and reset the count variable */ - parser->p_usage += parser->globals[RI_GLOBAL_REPORT_COUNT].val; - parser->usage_count = 0; + case RI_MAIN_INPUT: + handle_main_input(parser, item, iface); + break; } + + parser->usage_count = 0; + + /* Local items do not carry over to the next Main item (HID spec v1.11, section 6.2.2.8) */ + memset(parser->locals, 0, sizeof(parser->locals)); } @@ -172,53 +149,37 @@ void handle_main_item(parser_state_t *parser, header_t *header, int map_len) { * hopefully work well enough to find the basic values we care about to move the mouse around. * Your descriptor for a mouse with 2 wheels and 264 buttons might not parse correctly. **/ -uint8_t parse_report_descriptor(mouse_t *mouse, uint8_t arr_count, uint8_t const *report, uint16_t desc_len) { - usage_map_t usage_map[] = { - {.report_usage = HID_USAGE_DESKTOP_MOUSE, - .usage_page = HID_USAGE_PAGE_BUTTON, - .usage = HID_USAGE_DESKTOP_POINTER, - .element = &mouse->buttons}, +parser_state_t parser_state = {0}; // Avoid placing it on the stack, it's large - {.report_usage = HID_USAGE_DESKTOP_MOUSE, - .usage_page = HID_USAGE_PAGE_DESKTOP, - .usage = HID_USAGE_DESKTOP_X, - .element = &mouse->move_x}, +void parse_report_descriptor(hid_interface_t *iface, + uint8_t const *report, + int desc_len + ) { + item_t item = {0}; - {.report_usage = HID_USAGE_DESKTOP_MOUSE, - .usage_page = HID_USAGE_PAGE_DESKTOP, - .usage = HID_USAGE_DESKTOP_Y, - .element = &mouse->move_y}, - - {.report_usage = HID_USAGE_DESKTOP_MOUSE, - .usage_page = HID_USAGE_PAGE_DESKTOP, - .usage = HID_USAGE_DESKTOP_WHEEL, - .element = &mouse->wheel}, - }; - - parser_state_t parser = {0}; - parser.p_usage = parser.usages; - parser.map = usage_map; + /* Wipe parser_state clean */ + memset(&parser_state, 0, sizeof(parser_state_t)); + parser_state.p_usage = parser_state.usages; while (desc_len > 0) { - header_t header = *(header_t *)report++; - uint32_t data = get_descriptor_value(report, header.size); + item.hdr = *(header_t *)report++; + item.val = get_descriptor_value(report, item.hdr.size); - switch (header.type) { + switch (item.hdr.type) { case RI_TYPE_MAIN: - handle_main_item(&parser, &header, ARRAY_SIZE(usage_map)); + handle_main_item(&parser_state, &item, iface); break; case RI_TYPE_GLOBAL: - handle_global_item(&parser, &header, data, mouse); + handle_global_item(&parser_state, &item); break; case RI_TYPE_LOCAL: - handle_local_item(&parser, &header, data); + handle_local_item(&parser_state, &item); break; } /* Move to the next position and decrement size by header length + data length */ - report += header.size; - desc_len -= header.size + 1; - } - return 0; + report += SIZE_LOOKUP[item.hdr.size]; + desc_len -= (SIZE_LOOKUP[item.hdr.size] + 1); + } } diff --git a/src/hid_report.c b/src/hid_report.c new file mode 100644 index 0000000..1c6d340 --- /dev/null +++ b/src/hid_report.c @@ -0,0 +1,299 @@ +/* + * This file is part of DeskHop (https://github.com/hrvach/deskhop). + * Copyright (c) 2024 Hrvoje Cavrak + * + * 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, version 3. + * + * 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 . + */ + +#include "hid_report.h" +#include "main.h" + +/* Given a value struct with size and offset in bits, find and return a value from the HID report */ +int32_t get_report_value(uint8_t *report, report_val_t *val) { + /* Calculate the bit offset within the byte */ + uint16_t offset_in_bits = val->offset % 8; + + /* Calculate the remaining bits in the first byte */ + uint16_t remaining_bits = 8 - offset_in_bits; + + /* Calculate the byte offset in the array */ + uint16_t byte_offset = val->offset >> 3; + + /* Create a mask for the specified number of bits */ + uint32_t mask = (1u << val->size) - 1; + + /* Initialize the result value with the bits from the first byte */ + int32_t result = report[byte_offset] >> offset_in_bits; + + /* Move to the next byte and continue fetching bits until the desired length is reached */ + while (val->size > remaining_bits) { + result |= report[++byte_offset] << remaining_bits; + remaining_bits += 8; + } + + /* Apply the mask to retain only the desired number of bits */ + result = result & mask; + + /* Special case if our result is negative. + Check if the most significant bit of 'val' is set */ + if (result & ((mask >> 1) + 1)) { + /* If it is set, sign-extend 'val' by filling the higher bits with 1s */ + result |= (0xFFFFFFFFU << val->size); + } + + return result; +} + +/* After processing the descriptor, assign the values so we can later use them to interpret reports */ +void handle_consumer_control_values(report_val_t *src, report_val_t *dst, hid_interface_t *iface) { + if (src->offset > MAX_CC_BUTTONS) { + return; + } + + if (src->data_type == VARIABLE) { + iface->keyboard.cc_array[src->offset] = src->usage; + iface->consumer.is_variable = true; + } + + iface->consumer.is_array |= (src->data_type == ARRAY); +} + +/* After processing the descriptor, assign the values so we can later use them to interpret reports */ +void handle_system_control_values(report_val_t *src, report_val_t *dst, hid_interface_t *iface) { + if (src->offset > MAX_SYS_BUTTONS) { + return; + } + + if (src->data_type == VARIABLE) { + iface->keyboard.sys_array[src->offset] = src->usage; + iface->system.is_variable = true; + } + + iface->system.is_array |= (src->data_type == ARRAY); +} + +/* After processing the descriptor, assign the values so we can later use them to interpret reports */ +void handle_keyboard_descriptor_values(report_val_t *src, report_val_t *dst, hid_interface_t *iface) { + const int LEFT_CTRL = 0xE0; + + /* Constants are normally used for padding, so skip'em */ + if (src->item_type == CONSTANT) + return; + + /* Detect and handle modifier keys. <= if modifier is less + constant padding? */ + if (src->size <= MODIFIER_BIT_LENGTH && src->data_type == VARIABLE) { + /* To make sure this really is the modifier key, we expect e.g. left control to be + within the usage interval */ + if (LEFT_CTRL >= src->usage_min && LEFT_CTRL <= src->usage_max) + iface->keyboard.modifier = *src; + } + + /* If we have an array member, that's most likely a key (0x00 - 0xFF, 1 byte) */ + if (src->offset_idx < MAX_KEYS) { + iface->keyboard.key_array[src->offset_idx] = (src->data_type == ARRAY); + } + + /* Handle NKRO, normally size = 1, count = 240 or so, but they are swapped. */ + if (src->size > 32 && src->data_type == VARIABLE) { + iface->keyboard.is_nkro = true; + iface->keyboard.nkro = *src; + } + + /* We found a keyboard on this interface. */ + iface->keyboard.is_found = true; +} + +void handle_buttons(report_val_t *src, report_val_t *dst, hid_interface_t *iface) { + /* Constant is normally used for padding with mouse buttons, aggregate to simplify things */ + if (src->item_type == CONSTANT) { + iface->mouse.buttons.size += src->size; + return; + } + + iface->mouse.buttons = *src; + + /* We found a mouse on this interface. */ + iface->mouse.is_found = true; +} + +void _store(report_val_t *src, report_val_t *dst, hid_interface_t *iface) { + if (src->item_type != CONSTANT) + *dst = *src; +} + + +void extract_data(hid_interface_t *iface, report_val_t *val) { + const usage_map_t map[] = { + {.usage_page = HID_USAGE_PAGE_BUTTON, + .global_usage = HID_USAGE_DESKTOP_MOUSE, + .usage = HID_USAGE_DESKTOP_POINTER, + .handler = handle_buttons, + .receiver = process_mouse_report, + .dst = &iface->mouse.buttons, + .id = &iface->mouse.report_id}, + + {.usage_page = HID_USAGE_PAGE_DESKTOP, + .global_usage = HID_USAGE_DESKTOP_MOUSE, + .usage = HID_USAGE_DESKTOP_X, + .handler = _store, + .receiver = process_mouse_report, + .dst = &iface->mouse.move_x, + .id = &iface->mouse.report_id}, + + {.usage_page = HID_USAGE_PAGE_DESKTOP, + .global_usage = HID_USAGE_DESKTOP_MOUSE, + .usage = HID_USAGE_DESKTOP_Y, + .handler = _store, + .receiver = process_mouse_report, + .dst = &iface->mouse.move_y, + .id = &iface->mouse.report_id}, + + {.usage_page = HID_USAGE_PAGE_DESKTOP, + .global_usage = HID_USAGE_DESKTOP_MOUSE, + .usage = HID_USAGE_DESKTOP_WHEEL, + .handler = _store, + .receiver = process_mouse_report, + .dst = &iface->mouse.wheel, + .id = &iface->mouse.report_id}, + + {.usage_page = HID_USAGE_PAGE_KEYBOARD, + .global_usage = HID_USAGE_DESKTOP_KEYBOARD, + .handler = handle_keyboard_descriptor_values, + .receiver = process_keyboard_report, + .id = &iface->keyboard.report_id}, + + {.usage_page = HID_USAGE_PAGE_CONSUMER, + .global_usage = HID_USAGE_CONSUMER_CONTROL, + .handler = handle_consumer_control_values, + .receiver = process_consumer_report, + .dst = &iface->consumer.val, + .id = &iface->consumer.report_id}, + + {.usage_page = HID_USAGE_PAGE_DESKTOP, + .global_usage = HID_USAGE_DESKTOP_SYSTEM_CONTROL, + .handler = _store, + .receiver = process_system_report, + .dst = &iface->system.val, + .id = &iface->system.report_id}, + }; + + /* We extracted all we could find in the descriptor to report_values, now go through them and + match them up with the values in the table above, then store those values for later reference */ + + for (const usage_map_t *hay = map; hay != &map[ARRAY_SIZE(map)]; hay++) { + /* ---> If any condition is not defined, we consider it as matched <--- */ + bool global_usages_match = (val->global_usage == hay->global_usage) || (hay->global_usage == 0); + bool usages_match = (val->usage == hay->usage) || (hay->usage == 0); + bool usage_pages_match = (val->usage_page == hay->usage_page) || (hay->usage_page == 0); + + if (global_usages_match && usages_match && usage_pages_match) { + hay->handler(val, hay->dst, iface); + *hay->id = val->report_id; + + if (val->report_id < MAX_REPORTS) + iface->report_handler[val->report_id] = hay->receiver; + } + } +} + +int32_t extract_bit_variable(uint32_t min_val, uint32_t max_val, uint8_t *raw_report, int len, uint8_t *dst) { + int key_count = 0; + + for (int i = min_val, j = 0; i <= max_val && key_count < len; i++, j++) { + int byte_index = j >> 3; + int bit_index = j & 0b111; + + if (raw_report[byte_index] & (1 << bit_index)) { + dst[key_count++] = i; + } + } + + return key_count; +} + +int32_t _extract_kbd_boot(uint8_t *raw_report, int len, hid_keyboard_report_t *report) { + uint8_t *src = raw_report; + + /* In case keyboard still uses report ID in this, just pick the last 8 bytes */ + if (len == KBD_REPORT_LENGTH + 1) + src++; + + memcpy(report, src, KBD_REPORT_LENGTH); + return KBD_REPORT_LENGTH; +} + +int32_t _extract_kbd_other(uint8_t *raw_report, int len, hid_interface_t *iface, hid_keyboard_report_t *report) { + uint8_t *src = raw_report; + keyboard_t *kb = &iface->keyboard; + + if (iface->uses_report_id) + src++; + + report->modifier = src[kb->modifier.offset_idx]; + for (int i=0, j=0; i < MAX_KEYS && j < KEYS_IN_USB_REPORT; i++) { + if(kb->key_array[i]) + report->keycode[j++] = src[i]; + } + + return KBD_REPORT_LENGTH; +} + +int32_t _extract_kbd_nkro(uint8_t *raw_report, int len, hid_interface_t *iface, hid_keyboard_report_t *report) { + uint8_t *ptr = raw_report; + keyboard_t *kb = &iface->keyboard; + + /* Skip report ID */ + if (iface->uses_report_id) + ptr++; + + /* We expect array of bits mapping 1:1 from usage_min to usage_max, otherwise panic */ + if ((kb->nkro.usage_max - kb->nkro.usage_min + 1) != kb->nkro.size) + return -1; + + /* We expect modifier to be 8 bits long, otherwise we'll fallback to boot mode */ + if (kb->modifier.size == MODIFIER_BIT_LENGTH) { + report->modifier = ptr[kb->modifier.offset_idx]; + } else + return -1; + + /* Move the pointer to the nkro offset's byte index */ + ptr = &ptr[kb->nkro.offset_idx]; + + return extract_bit_variable( + kb->nkro.usage_min, kb->nkro.usage_max, ptr, KEYS_IN_USB_REPORT, report->keycode); +} + +int32_t extract_kbd_data( + uint8_t *raw_report, int len, uint8_t itf, hid_interface_t *iface, hid_keyboard_report_t *report) { + int report_id = raw_report[0]; + + /* Clear the report to start fresh */ + memset(report, 0, KBD_REPORT_LENGTH); + + /* If we're in boot protocol mode, then it's easy to decide. */ + if (iface->protocol == HID_PROTOCOL_BOOT) + return _extract_kbd_boot(raw_report, len, report); + + /* NKRO is a special case */ + if (report_id > 0 + && report_id == iface->keyboard.nkro.report_id + && iface->keyboard.is_nkro) + return _extract_kbd_nkro(raw_report, len, iface, report); + + /* If we're getting 8 bytes of report, it's safe to assume standard modifier + reserved + keys */ + if (len == KBD_REPORT_LENGTH || len == KBD_REPORT_LENGTH + 1) + return _extract_kbd_boot(raw_report, len, report); + + /* This is something completely different, look at the report */ + return _extract_kbd_other(raw_report, len, iface, report); +} \ No newline at end of file diff --git a/src/hid_parser.h b/src/include/hid_parser.h similarity index 51% rename from src/hid_parser.h rename to src/include/hid_parser.h index 45324e9..0926ee5 100644 --- a/src/hid_parser.h +++ b/src/include/hid_parser.h @@ -20,8 +20,16 @@ #pragma once #include "main.h" +#include "tusb.h" -#define MAX_REPORTS 32 +#define MAX_REPORTS 24 +#define MAX_DEVICES 3 +#define MAX_INTERFACES 6 +#define HID_MAX_USAGES 128 +#define HID_DEFAULT_NUM_COLLECTIONS 16 +#define MAX_CC_BUTTONS 16 +#define MAX_SYS_BUTTONS 8 +#define MAX_KEYS 32 /* Counts how many collection starts and ends we've seen, when they equalize (and not zero), we are at the end of a block */ @@ -42,7 +50,20 @@ typedef struct TU_ATTR_PACKED { typedef struct { header_t hdr; uint32_t val; -} globals_t; +} item_t; + +typedef enum { + DATA = 0, + CONSTANT, + ARRAY, + VARIABLE, + ABSOLUTE_DATA, + RELATIVE_DATA, + NO_WRAP, + WRAP, + LINEAR, + NONLINEAR, +} data_type_e; // Extended precision mouse movement information typedef struct { @@ -54,11 +75,21 @@ typedef struct { } mouse_values_t; /* Describes where can we find a value in a HID report */ -typedef struct { - uint16_t offset; // In bits - uint8_t size; // In bits - int32_t min; - int32_t max; +typedef struct TU_ATTR_PACKED { + uint16_t offset; // In bits + uint16_t offset_idx; // In bytes + uint16_t size; // In bits + + int32_t usage_min; + int32_t usage_max; + + uint8_t item_type; + uint8_t data_type; + + uint8_t report_id; + uint16_t global_usage; + uint16_t usage_page; + uint16_t usage; } report_val_t; /* Defines information about HID report format for the mouse. */ @@ -69,38 +100,65 @@ typedef struct { report_val_t wheel; uint8_t report_id; - uint8_t protocol; + bool is_found; bool uses_report_id; } mouse_t; +typedef struct hid_interface_t hid_interface_t; +typedef void (*process_report_f)(uint8_t *, int, uint8_t, hid_interface_t *); + /* Defines information about HID report format for the keyboard. */ typedef struct { - uint8_t keyboard_report_id; - uint8_t consumer_report_id; - uint8_t system_report_id; - uint8_t protocol; + report_val_t modifier; + report_val_t nkro; + uint16_t cc_array[MAX_CC_BUTTONS]; + uint16_t sys_array[MAX_SYS_BUTTONS]; + bool key_array[MAX_KEYS]; + + uint8_t report_id; + uint8_t key_array_idx; + + bool uses_report_id; + bool is_found; + bool is_nkro; } keyboard_t; -/* For each element type we're interested in there is an entry -in an array of these, defining its usage and in case matched, where to -store the data. */ typedef struct { - uint8_t report_usage; - uint8_t usage_page; - uint8_t usage; - report_val_t *element; -} usage_map_t; + report_val_t val; + uint8_t report_id; + bool is_variable; + bool is_array; +} report_t; + +struct hid_interface_t { + keyboard_t keyboard; + mouse_t mouse; + report_t consumer; + report_t system; + process_report_f report_handler[MAX_REPORTS]; + uint8_t protocol; + bool uses_report_id; +}; typedef struct { - uint8_t usage_count; - uint8_t global_usage; + report_val_t *map; + int map_index; /* Index of the current element we've found */ + int report_id; /* Report ID of the current section we're parsing */ + + uint32_t usage_count; uint32_t offset_in_bits; - uint8_t usages[256]; - uint8_t *p_usage; + uint16_t usages[HID_MAX_USAGES]; + uint16_t *p_usage; + uint16_t global_usage; collection_t collection; - usage_map_t *map; - globals_t globals[16]; /* as tag is 4 bits, there can be 16 different tags in global header type */ + /* as tag is 4 bits, there can be 16 different tags in global header type */ + item_t globals[16]; + + /* as tag is 4 bits, there can be 16 different tags in local header type */ + item_t locals[16]; } parser_state_t; + +/////////////// diff --git a/src/include/hid_report.h b/src/include/hid_report.h new file mode 100644 index 0000000..5e9297d --- /dev/null +++ b/src/include/hid_report.h @@ -0,0 +1,37 @@ +/* + * This file is part of DeskHop (https://github.com/hrvach/deskhop). + * Copyright (c) 2024 Hrvoje Cavrak + * + * Based on the TinyUSB HID parser routine and the amazing USB2N64 + * adapter (https://github.com/pdaxrom/usb2n64-adapter) + * + * 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, version 3. + * + * 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 . + */ +#pragma once + +#include "main.h" + +typedef void (*value_handler_f)(report_val_t *, report_val_t *, hid_interface_t *); + +/* For each element type we're interested in there is an entry +in an array of these, defining its usage and in case matched, where to +store the data. */ +typedef struct { + int global_usage; + int usage_page; + int usage; + uint8_t *id; + report_val_t *dst; + value_handler_f handler; + process_report_f receiver; +} usage_map_t; diff --git a/src/main.h b/src/include/main.h similarity index 51% rename from src/main.h rename to src/include/main.h index dbd9c30..4d553ac 100644 --- a/src/main.h +++ b/src/include/main.h @@ -20,7 +20,6 @@ #include #include #include -#include #include #include @@ -29,6 +28,10 @@ #include "tusb.h" #include "usb_descriptors.h" #include "user_config.h" +#include "protocol.h" +#include +#include +#include #include #include #include @@ -38,9 +41,6 @@ #include /********* Misc definitions for better readability **********/ -#define PICO_A 0 -#define PICO_B 1 - #define OUTPUT_A 0 #define OUTPUT_B 1 @@ -50,26 +50,32 @@ #define ABSOLUTE 0 #define RELATIVE 1 -#define MAX_REPORT_ITEMS 16 #define MOUSE_BOOT_REPORT_LEN 4 - -#define NUM_SCREENS 2 // Will be more in the future #define MOUSE_ZOOM_SCALING_FACTOR 2 +#define NUM_SCREENS 2 // Will be more in the future +#define CONFIG_MODE_TIMEOUT 300000000 // 5 minutes into the future #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) -#define CURRENT_BOARD_IS_ACTIVE_OUTPUT (global_state.active_output == BOARD_ROLE) +#define CURRENT_BOARD_IS_ACTIVE_OUTPUT (global_state.active_output == global_state.board_role) + +#define _TOP() 0 +#define _MS(x) (x * 1000) +#define _SEC(x) (x * 1000000) +#define _HZ(x) ((uint64_t)((1000000) / (x))) /********* Pinout definitions **********/ #define PIO_USB_DP_PIN 14 // D+ is pin 14, D- is pin 15 #define GPIO_LED_PIN 25 // LED is connected to pin 25 on a PICO -#if BOARD_ROLE == PICO_B -#define SERIAL_TX_PIN 16 -#define SERIAL_RX_PIN 17 -#elif BOARD_ROLE == PICO_A -#define SERIAL_TX_PIN 12 -#define SERIAL_RX_PIN 13 -#endif +#define BOARD_A_RX 13 +#define BOARD_A_TX 12 +#define BOARD_B_RX 17 +#define BOARD_B_TX 16 + +#define SERIAL_TX_PIN (global_state.board_role == OUTPUT_A ? BOARD_A_TX : BOARD_B_TX) +#define SERIAL_RX_PIN (global_state.board_role == OUTPUT_A ? BOARD_A_RX : BOARD_B_RX) + +#define BOARD_ROLE (global_state.board_role) /********* Serial port definitions **********/ #define SERIAL_UART uart0 @@ -79,11 +85,21 @@ #define SERIAL_STOP_BITS 1 #define SERIAL_PARITY UART_PARITY_NONE +/********* DMA definitions **********/ +#define DMA_RX_BUFFER_SIZE 1024 +#define DMA_TX_BUFFER_SIZE 32 + +extern uint8_t uart_rxbuf[DMA_RX_BUFFER_SIZE] __attribute__((aligned(DMA_RX_BUFFER_SIZE))); +extern uint8_t uart_txbuf[DMA_TX_BUFFER_SIZE] __attribute__((aligned(DMA_TX_BUFFER_SIZE))); + /********* Watchdog definitions **********/ -#define WATCHDOG_TIMEOUT 1000 // In milliseconds => needs to be reset every second +#define WATCHDOG_TIMEOUT 500 // In milliseconds => needs to be reset at least every 200ms #define WATCHDOG_PAUSE_ON_DEBUG 1 // When using a debugger, disable watchdog #define CORE1_HANG_TIMEOUT_US WATCHDOG_TIMEOUT * 1000 // Convert to microseconds +#define MAGIC_WORD_1 0xdeadf00f // When these are set, we'll boot to configuration mode +#define MAGIC_WORD_2 0x00c0ffee + /********* Protocol definitions ********* * * - every packet starts with 0xAA 0x55 for easy re-sync @@ -104,12 +120,17 @@ enum packet_type_e { SWITCH_LOCK_MSG = 7, SYNC_BORDERS_MSG = 8, FLASH_LED_MSG = 9, - SCREENSAVER_MSG = 10, - WIPE_CONFIG_MSG = 11, - SWAP_OUTPUTS_MSG = 12, - HEARTBEAT_MSG = 13, - OUTPUT_CONFIG_MSG = 14, - CONSUMER_CONTROL_MSG = 15, + WIPE_CONFIG_MSG = 10, + HEARTBEAT_MSG = 12, + CONSUMER_CONTROL_MSG = 14, + SYSTEM_CONTROL_MSG = 15, + SAVE_CONFIG_MSG = 18, + REBOOT_MSG = 19, + GET_VAL_MSG = 20, + SET_VAL_MSG = 21, + PROXY_PACKET_MSG = 23, + REQUEST_BYTE_MSG = 24, + RESPONSE_BYTE_MSG = 25, }; /* @@ -123,9 +144,13 @@ enum packet_type_e { /* Data structure defining packets of information transferred */ typedef struct { uint8_t type; // Enum field describing the type of packet - uint8_t data[8]; // Data goes here (type + payload + checksum) + union { + uint8_t data[8]; // Data goes here (type + payload + checksum) + uint16_t data16[4]; // We can treat it as 4 16-byte chunks + uint32_t data32[2]; // We can treat it as 2 32-byte chunks + }; uint8_t checksum; // Checksum, a simple XOR-based one -} uart_packet_t; +} __attribute__((packed)) uart_packet_t; /********* Packet parameters **********/ @@ -140,27 +165,33 @@ typedef struct { #define PACKET_LENGTH (TYPE_LENGTH + PACKET_DATA_LENGTH + CHECKSUM_LENGTH) #define RAW_PACKET_LENGTH (START_LENGTH + PACKET_LENGTH) +#define UART_QUEUE_LENGTH 256 +#define CFG_QUEUE_LENGTH 128 #define KBD_QUEUE_LENGTH 128 -#define MOUSE_QUEUE_LENGTH 2048 +#define MOUSE_QUEUE_LENGTH 512 +#define KEYARRAY_BIT_OFFSET 16 #define KEYS_IN_USB_REPORT 6 #define KBD_REPORT_LENGTH 8 #define MOUSE_REPORT_LENGTH 7 #define CONSUMER_CONTROL_LENGTH 4 +#define SYSTEM_CONTROL_LENGTH 1 +#define MODIFIER_BIT_LENGTH 8 /********* Screen **********/ #define MIN_SCREEN_COORD 0 #define MAX_SCREEN_COORD 32767 -#define SCREEN_MIDPOINT 16384 +#define SCREEN_MIDPOINT 16384 /********* Configuration storage definitions **********/ -#define CURRENT_CONFIG_VERSION 4 +#define CURRENT_CONFIG_VERSION 8 enum os_type_e { LINUX = 1, MACOS = 2, WINDOWS = 3, + ANDROID = 4, OTHER = 255, }; @@ -170,11 +201,17 @@ enum screen_pos_e { MIDDLE = 3, }; -enum itf_num_e { - ITF_NUM_HID = 0, - ITF_NUM_HID_REL_M = 1, +enum screensaver_mode_e { + DISABLED = 0, + PONG = 1, + JITTER = 2, }; +#define ITF_NUM_HID 0 +#define ITF_NUM_HID_REL_M 1 +#define ITF_NUM_HID_VENDOR 1 +#define ITF_NUM_MSC 2 + typedef struct { int top; // When jumping from a smaller to a bigger screen, go to THIS top height int bottom; // When jumping from a smaller to a bigger screen, go to THIS bottom @@ -183,7 +220,7 @@ typedef struct { /* Define screensaver parameters */ typedef struct { - uint8_t enabled; + uint8_t mode; uint8_t only_if_inactive; uint64_t idle_time_us; uint64_t max_time_us; @@ -191,14 +228,15 @@ typedef struct { /* Define output parameters */ typedef struct { - int number; // Number of this output (e.g. OUTPUT_A = 0 etc) - int screen_count; // How many monitors per output (e.g. Output A is Windows with 3 monitors) - int screen_index; // Current active screen - int speed_x; // Mouse speed per output, in direction X - int speed_y; // Mouse speed per output, in direction Y + uint32_t number; // Number of this output (e.g. OUTPUT_A = 0 etc) + uint32_t screen_count; // How many monitors per output (e.g. Output A is Windows with 3 monitors) + uint32_t screen_index; // Current active screen + int32_t speed_x; // Mouse speed per output, in direction X + int32_t speed_y; // Mouse speed per output, in direction Y border_size_t border; // Screen border size/offset to keep cursor at same height when switching - enum os_type_e os; // Operating system on this output - enum screen_pos_e pos; // Screen position on this output + uint8_t os; // Operating system on this output + uint8_t pos; // Screen position on this output + uint8_t mouse_park_pos; // Where the mouse goes after switch screensaver_t screensaver; // Screensaver parameters for this output } output_t; @@ -206,17 +244,80 @@ typedef struct { typedef struct { uint32_t magic_header; uint32_t version; + uint8_t force_mouse_boot_mode; - output_t output[NUM_SCREENS]; - uint8_t screensaver_enabled; + uint8_t force_kbd_boot_protocol; + + uint8_t kbd_led_as_indicator; + uint8_t hotkey_toggle; + uint8_t enable_acceleration; + + uint8_t enforce_ports; + uint16_t jump_treshold; + + output_t output[NUM_SCREENS]; + uint32_t _reserved; + // Keep checksum at the end of the struct uint32_t checksum; } config_t; extern const config_t default_config; -extern config_t ADDR_CONFIG[]; -#define ADDR_CONFIG_BASE_ADDR (ADDR_CONFIG) +/********* Flash data section **********/ +typedef struct { + uint8_t cmd; // Byte 0 = command + uint16_t page_number; // Bytes 1-2 = page number + union { + uint8_t offset; // Byte 3 = offset + uint8_t checksum; // In write packets, it's checksum + }; + uint8_t data[4]; // Bytes 4-7 = data +} fw_packet_t; + +extern const config_t ADDR_CONFIG[]; +extern const uint8_t ADDR_FW_METADATA[]; +extern const uint8_t ADDR_FW_RUNNING[]; +extern const uint8_t ADDR_FW_STAGING[]; +extern const uint8_t ADDR_DISK_IMAGE[]; + +/* Ring buffer wraps around after reaching 4095 */ +#define NEXT_RING_IDX(x) ((x + 1) & 0x3FF) + +typedef struct { + uint16_t magic; + uint16_t version; + uint32_t checksum; +} firmware_metadata_t; + +extern firmware_metadata_t _firmware_metadata; + +#define FIRMWARE_METADATA_MAGIC 0xf00d +#define RUNNING_FIRMWARE_SLOT 0 +#define STAGING_FIRMWARE_SLOT 1 + +#define STAGING_PAGES_CNT 1024 +#define STAGING_IMAGE_SIZE STAGING_PAGES_CNT * FLASH_PAGE_SIZE + +extern const uint32_t crc32_lookup_table[]; + + +typedef struct { + uint32_t magicStart0; + uint32_t magicStart1; + uint32_t flags; + uint32_t targetAddr; + uint32_t payloadSize; + uint32_t blockNo; + uint32_t numBlocks; + uint32_t fileSize; + uint8_t data[476]; + uint32_t magicEnd; +} uf2_t; + +#define UF2_MAGIC_START0 0x0A324655 +#define UF2_MAGIC_START1 0x9E5D5157 +#define UF2_MAGIC_END 0x0AB16F30 // -------------------------------------------------------+ @@ -228,12 +329,12 @@ typedef struct { // Maps message type -> message handler function } uart_handler_t; typedef struct { - uint8_t modifier; // Which modifier is pressed - uint8_t keys[6]; // Which keys need to be pressed - uint8_t key_count; // How many keys are pressed - action_handler_t action_handler; // What to execute when the key combination is detected - bool pass_to_os; // True if we are to pass the key to the OS too - bool acknowledge; // True if we are to notify the user about registering keypress + uint8_t modifier; // Which modifier is pressed + uint8_t keys[KEYS_IN_USB_REPORT]; // Which keys need to be pressed + uint8_t key_count; // How many keys are pressed + action_handler_t action_handler; // What to execute when the key combination is detected + bool pass_to_os; // True if we are to pass the key to the OS too + bool acknowledge; // True if we are to notify the user about registering keypress } hotkey_combo_t; typedef struct TU_ATTR_PACKED { @@ -246,43 +347,74 @@ typedef struct TU_ATTR_PACKED { typedef enum { IDLE, READING_PACKET, PROCESSING_PACKET } receiver_state_t; +typedef struct { + uint32_t address; // Address we're sending to the other box + uint32_t checksum; + uint16_t version; + bool byte_done; // Has the byte been successfully transferred + bool upgrade_in_progress; // True if firmware transfer from the other box is in progress +} fw_upgrade_state_t; + typedef struct { uint8_t kbd_dev_addr; // Address of the keyboard device uint8_t kbd_instance; // Keyboard instance (d'uh - isn't this a useless comment) uint8_t keyboard_leds[NUM_SCREENS]; // State of keyboard LEDs (index 0 = A, index 1 = B) uint64_t last_activity[NUM_SCREENS]; // Timestamp of the last input activity (-||-) - receiver_state_t receiver_state; // Storing the state for the simple receiver state machine uint64_t core1_last_loop_pass; // Timestamp of last core1 loop execution uint8_t active_output; // Currently selected output (0 = A, 1 = B) + uint8_t board_role; // Which board are we running on? (0 = A, 1 = B, etc.) - int16_t mouse_x; // Store and update the location of our mouse pointer - int16_t mouse_y; + int16_t pointer_x; // Store and update the location of our mouse pointer + int16_t pointer_y; int16_t mouse_buttons; // Store and update the state of mouse buttons - config_t config; // Device configuration, loaded from flash or defaults used - mouse_t mouse_dev; // Mouse device specifics, e.g. stores locations for keys in report - keyboard_t kbd_dev; // Keyboard device specifics, like report IDs - queue_t kbd_queue; // Queue that stores keyboard reports - queue_t mouse_queue; // Queue that stores mouse reports + config_t config; // Device configuration, loaded from flash or defaults used + queue_t cfg_queue_out; // Queue that stores outgoing vendor config messages + queue_t kbd_queue; // Queue that stores keyboard reports + queue_t mouse_queue; // Queue that stores mouse reports + queue_t uart_tx_queue; // Queue that stores outgoing packets + + hid_interface_t iface[MAX_DEVICES][MAX_INTERFACES]; // Store info about HID interfaces + uart_packet_t in_packet; + + /* DMA */ + uint32_t dma_ptr; // Stores info about DMA ring buffer last checked position + uint32_t dma_rx_channel; // DMA RX channel we're using to receive + uint32_t dma_control_channel; // DMA channel that controls the RX transfer channel + uint32_t dma_tx_channel; // DMA TX channel we're using to send + + /* Firmware */ + fw_upgrade_state_t fw; // State of the firmware upgrader + firmware_metadata_t _running_fw; // RAM copy of running fw metadata + bool reboot_requested; // If set, stop updating watchdog + uint64_t config_mode_timer; // Counts how long are we to remain in config mode + + uint8_t page_buffer[FLASH_PAGE_SIZE]; // For firmware-over-serial upgrades /* Connection status flags */ bool tud_connected; // True when TinyUSB device successfully connects bool keyboard_connected; // True when our keyboard is connected locally - bool mouse_connected; // True when a mouse is connected locally /* Feature flags */ bool mouse_zoom; // True when "mouse zoom" is enabled bool switch_lock; // True when device is prevented from switching bool onboard_led_state; // True when LED is ON bool relative_mouse; // True when relative mouse mode is used - + bool config_mode_active; // True when config mode is active + /* Onboard LED blinky (provide feedback when e.g. mouse connected) */ int32_t blinks_left; // How many blink transitions are left int32_t last_led_change; // Timestamp of the last time led state transitioned - } device_t; +typedef struct { + void (*exec)(device_t *state); + uint64_t frequency; + uint64_t next_run; + bool *enabled; +} task_t; + /********* Setup **********/ void initial_setup(device_t *); void serial_init(void); @@ -290,50 +422,80 @@ void core1_main(void); /********* Keyboard **********/ bool check_specific_hotkey(hotkey_combo_t, const hid_keyboard_report_t *); -void process_keyboard_report(uint8_t *, int, device_t *); -void process_consumer_report(uint8_t *, int, device_t *); +void process_keyboard_report(uint8_t *, int, uint8_t, hid_interface_t *); +void process_consumer_report(uint8_t *, int, uint8_t, hid_interface_t *); +void process_system_report(uint8_t *, int, uint8_t, hid_interface_t *); void release_all_keys(device_t *); void queue_kbd_report(hid_keyboard_report_t *, device_t *); -void process_kbd_queue_task(device_t *); void send_key(hid_keyboard_report_t *, device_t *); -bool key_in_report(uint8_t, const hid_keyboard_report_t *); void send_consumer_control(uint8_t *, device_t *); +bool key_in_report(uint8_t, const hid_keyboard_report_t *); +int32_t extract_bit_variable(uint32_t, uint32_t, uint8_t *, int, uint8_t *); +int32_t extract_kbd_data(uint8_t *, int, uint8_t, hid_interface_t *, hid_keyboard_report_t *); /********* Mouse **********/ bool tud_mouse_report(uint8_t mode, uint8_t buttons, int16_t x, int16_t y, int8_t wheel); -void process_mouse_report(uint8_t *, int, device_t *); -uint8_t -parse_report_descriptor(mouse_t *mouse, uint8_t arr_count, uint8_t const *desc_report, uint16_t desc_len); +void process_mouse_report(uint8_t *, int, uint8_t, hid_interface_t *); +void parse_report_descriptor(hid_interface_t *, uint8_t const *, int); +void extract_data(hid_interface_t *, report_val_t *); int32_t get_report_value(uint8_t *report, report_val_t *val); -void process_mouse_queue_task(device_t *); void queue_mouse_report(mouse_report_t *, device_t *); void output_mouse_report(mouse_report_t *, device_t *); /********* UART **********/ -void receive_char(uart_packet_t *, device_t *); -void send_packet(const uint8_t *, enum packet_type_e, int); +void process_packet(uart_packet_t *, device_t *); +void queue_packet(const uint8_t *, enum packet_type_e, int); +void write_raw_packet(uint8_t *, uart_packet_t *); void send_value(const uint8_t, enum packet_type_e); +bool get_packet_from_buffer(device_t *); /********* LEDs **********/ void restore_leds(device_t *); void blink_led(device_t *); -void led_blinking_task(device_t *); +uint8_t toggle_led(void); /********* Checksum **********/ uint8_t calc_checksum(const uint8_t *, int); bool verify_checksum(const uart_packet_t *); +uint32_t crc32(const uint8_t *, size_t); +uint32_t crc32_iter(uint32_t, const uint8_t); -/********* Watchdog **********/ -void kick_watchdog(device_t *); +/********* Firmware **********/ +void write_flash_page(uint32_t, uint8_t *); +uint32_t calculate_firmware_crc32(void); +bool is_bootsel_pressed(void); +void request_byte(device_t *, uint32_t); +uint32_t get_ptr_delta(uint32_t, device_t *); +bool is_start_of_packet(device_t *); +void fetch_packet(device_t *); +void reboot(void); + +/********* Tasks **********/ +void process_uart_tx_task(device_t *); +void process_mouse_queue_task(device_t *); +void process_cfg_queue_task(device_t *); +void process_kbd_queue_task(device_t *); +void usb_device_task(device_t *); +void kick_watchdog_task(device_t *); +void usb_host_task(device_t *); +void packet_receiver_task(device_t *); +void screensaver_task(device_t *); +void firmware_upgrade_task(device_t *); +void heartbeat_output_task(device_t *); +void led_blinking_task(device_t *); + +void task_scheduler(device_t *, task_t *); /********* Configuration **********/ void load_config(device_t *); void save_config(device_t *); void wipe_config(void); +void reset_config_timer(device_t *); -/********* Misc **********/ -void screensaver_task(device_t *); +extern const field_map_t api_field_map[]; +const field_map_t* get_field_map_entry(uint32_t); +bool validate_packet(uart_packet_t *); /********* Handlers **********/ void output_toggle_hotkey_handler(device_t *, hid_keyboard_report_t *); @@ -346,12 +508,11 @@ void switchlock_hotkey_handler(device_t *, hid_keyboard_report_t *); void screenlock_hotkey_handler(device_t *, hid_keyboard_report_t *); void output_config_hotkey_handler(device_t *, hid_keyboard_report_t *); void wipe_config_hotkey_handler(device_t *, hid_keyboard_report_t *); -void screensaver_hotkey_handler(device_t *, hid_keyboard_report_t *); +void config_enable_hotkey_handler(device_t *, hid_keyboard_report_t *); void handle_keyboard_uart_msg(uart_packet_t *, device_t *); void handle_mouse_abs_uart_msg(uart_packet_t *, device_t *); void handle_output_select_msg(uart_packet_t *, device_t *); -void handle_output_config_msg(uart_packet_t *, device_t *); void handle_mouse_zoom_msg(uart_packet_t *, device_t *); void handle_set_report_msg(uart_packet_t *, device_t *); void handle_switch_lock_msg(uart_packet_t *, device_t *); @@ -359,8 +520,16 @@ void handle_sync_borders_msg(uart_packet_t *, device_t *); void handle_flash_led_msg(uart_packet_t *, device_t *); void handle_fw_upgrade_msg(uart_packet_t *, device_t *); void handle_wipe_config_msg(uart_packet_t *, device_t *); -void handle_screensaver_msg(uart_packet_t *, device_t *); void handle_consumer_control_msg(uart_packet_t *, device_t *); +void handle_read_config_msg(uart_packet_t *, device_t *); +void handle_save_config_msg(uart_packet_t *, device_t *); +void handle_reboot_msg(uart_packet_t *, device_t *); +void handle_write_fw_msg(uart_packet_t *, device_t *); +void handle_request_byte_msg(uart_packet_t *, device_t *); +void handle_response_byte_msg(uart_packet_t *, device_t *); +void handle_heartbeat_msg(uart_packet_t *, device_t *); +void handle_proxy_msg(uart_packet_t *, device_t *); +void handle_api_msgs(uart_packet_t *, device_t *); void switch_output(device_t *, uint8_t); diff --git a/src/include/protocol.h b/src/include/protocol.h new file mode 100644 index 0000000..85852a5 --- /dev/null +++ b/src/include/protocol.h @@ -0,0 +1,23 @@ +#pragma once + +#include "main.h" + +typedef enum { + UINT8 = 0, + UINT16 = 1, + UINT32 = 2, + UINT64 = 3, + INT8 = 4, + INT16 = 5, + INT32 = 6, + INT64 = 7, + BOOL = 8 +} type_e; + +typedef struct { + uint32_t idx; + bool readonly; + type_e type; + uint32_t len; + size_t offset; +} field_map_t; diff --git a/src/tusb_config.h b/src/include/tusb_config.h similarity index 97% rename from src/tusb_config.h rename to src/include/tusb_config.h index 5be6c90..3d1d5f1 100644 --- a/src/tusb_config.h +++ b/src/include/tusb_config.h @@ -26,10 +26,6 @@ #ifndef _TUSB_CONFIG_H_ #define _TUSB_CONFIG_H_ -#ifdef __cplusplus -extern "C" { -#endif - //-------------------------------------------------------------------- // COMMON CONFIGURATION //-------------------------------------------------------------------- @@ -90,7 +86,6 @@ extern "C" { //------------- DEBUG -------------// #ifdef DH_DEBUG #define CFG_TUD_CDC 1 -#define CFG_TUD_LOG_LEVEL 3 #define CFG_TUSB_DEBUG_PRINTF dh_debug_printf extern int dh_debug_printf(const char *__restrict __format, ...); @@ -105,12 +100,14 @@ extern int dh_debug_printf(const char *__restrict __format, ...); //------------- CLASS -------------// #define CFG_TUD_HID 2 -#define CFG_TUD_MSC 0 #define CFG_TUD_MIDI 0 #define CFG_TUD_VENDOR 0 +#define CFG_TUD_MSC 1 // HID buffer size Should be sufficient to hold ID (if any) + Data #define CFG_TUD_HID_EP_BUFSIZE 32 +#define CFG_TUD_MSC_EP_BUFSIZE 512 + //-------------------------------------------------------------------- // HOST CONFIGURATION @@ -127,8 +124,5 @@ extern int dh_debug_printf(const char *__restrict __format, ...); #define CFG_TUH_HID_EPIN_BUFSIZE 64 #define CFG_TUH_HID_EPOUT_BUFSIZE 64 -#ifdef __cplusplus -} -#endif #endif /* _TUSB_CONFIG_H_ */ diff --git a/src/usb_descriptors.h b/src/include/usb_descriptors.h similarity index 65% rename from src/usb_descriptors.h rename to src/include/usb_descriptors.h index 1571e7f..a295851 100644 --- a/src/usb_descriptors.h +++ b/src/include/usb_descriptors.h @@ -25,20 +25,37 @@ #ifndef USB_DESCRIPTORS_H_ #define USB_DESCRIPTORS_H_ -enum -{ - REPORT_ID_KEYBOARD = 1, - REPORT_ID_MOUSE, - REPORT_ID_COUNT, - REPORT_ID_CONSUMER -}; +// Interface 0 +#define REPORT_ID_KEYBOARD 1 +#define REPORT_ID_MOUSE 2 +#define REPORT_ID_CONSUMER 3 +#define REPORT_ID_SYSTEM 4 -enum -{ - REPORT_ID_RELMOUSE = 1, -}; +// Interface 1 +#define REPORT_ID_RELMOUSE 5 -#define TUD_HID_REPORT_DESC_ABSMOUSE(...) \ +// Interface 2 +#define REPORT_ID_VENDOR 6 + + +#define DEVICE_DESCRIPTOR(vid, pid) \ +{.bLength = sizeof(tusb_desc_device_t),\ + .bDescriptorType = TUSB_DESC_DEVICE,\ + .bcdUSB = 0x0200,\ + .bDeviceClass = 0x00,\ + .bDeviceSubClass = 0x00,\ + .bDeviceProtocol = 0x00,\ + .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,\ + .idVendor = vid,\ + .idProduct = pid,\ + .bcdDevice = 0x0100,\ + .iManufacturer = 0x01,\ + .iProduct = 0x02,\ + .iSerialNumber = 0x03,\ + .bNumConfigurations = 0x01}\ + + +#define TUD_HID_REPORT_DESC_ABS_MOUSE(...) \ HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ HID_USAGE ( HID_USAGE_DESKTOP_MOUSE ) ,\ HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ @@ -94,13 +111,44 @@ HID_COLLECTION_END \ HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ /* Report ID if any */\ __VA_ARGS__ \ - HID_LOGICAL_MIN ( 0x01 ) ,\ + HID_LOGICAL_MIN ( 0x00 ) ,\ HID_LOGICAL_MAX_N( 0x0FFF, 2 ) ,\ - HID_USAGE_MIN ( 0x01 ) ,\ + HID_USAGE_MIN ( 0x00 ) ,\ HID_USAGE_MAX_N ( 0x0FFF, 2 ) ,\ HID_REPORT_SIZE ( 16 ) ,\ HID_REPORT_COUNT ( 2 ) ,\ HID_INPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ) ,\ HID_COLLECTION_END \ +// System Control Report Descriptor Template +#define TUD_HID_REPORT_DESC_SYSTEM_CTRL(...) \ + HID_USAGE_PAGE ( HID_USAGE_PAGE_DESKTOP ) ,\ + HID_USAGE ( HID_USAGE_DESKTOP_SYSTEM_CONTROL ) ,\ + HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ + /* Report ID if any */\ + __VA_ARGS__ \ + HID_LOGICAL_MIN ( 0x00 ) ,\ + HID_LOGICAL_MAX ( 0xff ) ,\ + HID_REPORT_COUNT( 1 ) ,\ + HID_REPORT_SIZE ( 8 ) ,\ + HID_INPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ) ,\ + HID_COLLECTION_END \ + +// Vendor Config Descriptor Template +#define TUD_HID_REPORT_DESC_VENDOR_CTRL(...) \ + HID_USAGE_PAGE_N ( HID_USAGE_PAGE_VENDOR, 2 ) ,\ + HID_USAGE ( 0x10 ) ,\ + HID_COLLECTION ( HID_COLLECTION_APPLICATION ) ,\ + /* Report ID if any */\ + __VA_ARGS__ \ + HID_LOGICAL_MIN ( 0x80 ) ,\ + HID_LOGICAL_MAX ( 0x7f ) ,\ + HID_REPORT_COUNT( 12 ) ,\ + HID_REPORT_SIZE ( 8 ) ,\ + HID_USAGE ( 0x10 ) ,\ + HID_INPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ) ,\ + HID_USAGE ( 0x10 ) ,\ + HID_OUTPUT ( HID_DATA | HID_ARRAY | HID_ABSOLUTE ) ,\ + HID_COLLECTION_END \ + #endif /* USB_DESCRIPTORS_H_ */ diff --git a/src/user_config.h b/src/include/user_config.h similarity index 90% rename from src/user_config.h rename to src/include/user_config.h index d7ab84a..19bf9cd 100644 --- a/src/user_config.h +++ b/src/include/user_config.h @@ -32,7 +32,7 @@ * * */ -#define HOTKEY_TOGGLE HID_KEY_CAPS_LOCK +#define HOTKEY_TOGGLE HID_KEY_F24 /**================================================== * * ============== Mouse Speed Factor ============== * @@ -66,20 +66,6 @@ /* Mouse acceleration */ #define ENABLE_ACCELERATION 1 - -/**================================================== * - * =========== Mouse General Settings ============= * - * ================================================== * - * - * MOUSE_PARKING_POSITION: [0, 1, 2 ] 0 means park mouse on TOP - * 1 means park mouse on BOTTOM - * 2 means park mouse on PREVIOUS position - * - * */ - -#define MOUSE_PARKING_POSITION 0 - - /**================================================== * * ============== Screensaver Config ============== * * ================================================== * @@ -123,8 +109,8 @@ * **/ -#define SCREENSAVER_A_ENABLED 0 -#define SCREENSAVER_B_ENABLED 0 +#define SCREENSAVER_A_MODE DISABLED +#define SCREENSAVER_B_MODE DISABLED /**================================================== * * @@ -170,9 +156,10 @@ * * */ -#define OUTPUT_A_OS LINUX +#define OUTPUT_A_OS MACOS #define OUTPUT_B_OS LINUX + /**================================================== * * ================= Enforce Ports ================= * * ================================================== @@ -185,4 +172,21 @@ * * */ -#define ENFORCE_PORTS 0 \ No newline at end of file +#define ENFORCE_PORTS 0 + + +/**================================================== * + * ============= Enforce Boot Protocol ============= * + * ================================================== + * + * If enabled, fixes some device incompatibilities by + * enforcing the boot protocol (which is simpler to parse + * and with less variation) + * + * ENFORCE_KEYBOARD_BOOT_PROTOCOL: [0, 1] - 1 means keyboard will forcefully use + * the boot protocol + * - 0 means no such thing is enforced + * + * */ + +#define ENFORCE_KEYBOARD_BOOT_PROTOCOL 0 diff --git a/src/keyboard.c b/src/keyboard.c index 032abfa..ac66cb4 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -51,13 +51,6 @@ hotkey_combo_t hotkeys[] = { .acknowledge = true, .action_handler = &screenlock_hotkey_handler}, - /* Erase stored config */ - {.modifier = KEYBOARD_MODIFIER_RIGHTSHIFT, - .keys = {HID_KEY_BACKSPACE}, - .key_count = 1, - .pass_to_os = true, - .action_handler = &output_config_hotkey_handler}, - /* Erase stored config */ {.modifier = KEYBOARD_MODIFIER_RIGHTSHIFT, .keys = {HID_KEY_F12, HID_KEY_D}, @@ -65,13 +58,6 @@ hotkey_combo_t hotkeys[] = { .acknowledge = true, .action_handler = &wipe_config_hotkey_handler}, - /* Toggle screensaver function */ - {.modifier = KEYBOARD_MODIFIER_RIGHTSHIFT, - .keys = {HID_KEY_F12, HID_KEY_S}, - .key_count = 2, - .acknowledge = true, - .action_handler = &screensaver_hotkey_handler}, - /* Record switch y coordinate */ {.modifier = KEYBOARD_MODIFIER_RIGHTSHIFT, .keys = {HID_KEY_F12, HID_KEY_Y}, @@ -79,17 +65,24 @@ hotkey_combo_t hotkeys[] = { .acknowledge = true, .action_handler = &screen_border_hotkey_handler}, + /* Switch to configuration mode */ + {.modifier = KEYBOARD_MODIFIER_RIGHTSHIFT | KEYBOARD_MODIFIER_LEFTSHIFT, + .keys = {HID_KEY_C, HID_KEY_O}, + .key_count = 2, + .acknowledge = true, + .action_handler = &config_enable_hotkey_handler}, + /* Hold down left shift + right shift + F12 + A ==> firmware upgrade mode for board A (kbd) */ {.modifier = KEYBOARD_MODIFIER_RIGHTSHIFT | KEYBOARD_MODIFIER_LEFTSHIFT, - .keys = {HID_KEY_F12, HID_KEY_A}, - .key_count = 2, + .keys = {HID_KEY_A}, + .key_count = 1, .acknowledge = true, .action_handler = &fw_upgrade_hotkey_handler_A}, /* Hold down left shift + right shift + F12 + B ==> firmware upgrade mode for board B (mouse) */ {.modifier = KEYBOARD_MODIFIER_RIGHTSHIFT | KEYBOARD_MODIFIER_LEFTSHIFT, - .keys = {HID_KEY_F12, HID_KEY_B}, - .key_count = 2, + .keys = {HID_KEY_B}, + .key_count = 1, .acknowledge = true, .action_handler = &fw_upgrade_hotkey_handler_B}}; @@ -150,13 +143,16 @@ void process_kbd_queue_task(device_t *state) { if (!queue_try_peek(&state->kbd_queue, &report)) return; - bool succeeded = false; - + /* If we are suspended, let's wake the host up */ if (tud_suspended()) - succeeded = tud_remote_wakeup(); - else - /* ... try sending it to the host, if it's successful */ - succeeded = tud_hid_keyboard_report(REPORT_ID_KEYBOARD, report.modifier, report.keycode); + tud_remote_wakeup(); + + /* If it's not ok to send yet, we'll try on the next pass */ + if (!tud_hid_n_ready(ITF_NUM_HID)) + return; + + /* ... try sending it to the host, if it's successful */ + bool succeeded = tud_hid_keyboard_report(REPORT_ID_KEYBOARD, report.modifier, report.keycode); /* ... then we can remove it from the queue. Race conditions shouldn't happen [tm] */ if (succeeded) @@ -166,7 +162,7 @@ void process_kbd_queue_task(device_t *state) { void queue_kbd_report(hid_keyboard_report_t *report, device_t *state) { /* It wouldn't be fun to queue up a bunch of messages and then dump them all on host */ if (!state->tud_connected) - return; + return; queue_try_add(&state->kbd_queue, report); } @@ -182,7 +178,7 @@ void send_key(hid_keyboard_report_t *report, device_t *state) { queue_kbd_report(report, state); state->last_activity[BOARD_ROLE] = time_us_64(); } else { - send_packet((uint8_t *)report, KEYBOARD_REPORT_MSG, KBD_REPORT_LENGTH); + queue_packet((uint8_t *)report, KEYBOARD_REPORT_MSG, KBD_REPORT_LENGTH); } } @@ -192,7 +188,17 @@ void send_consumer_control(uint8_t *raw_report, device_t *state) { tud_hid_n_report(0, REPORT_ID_CONSUMER, raw_report, CONSUMER_CONTROL_LENGTH); state->last_activity[BOARD_ROLE] = time_us_64(); } else { - send_packet((uint8_t *)raw_report, CONSUMER_CONTROL_MSG, CONSUMER_CONTROL_LENGTH); + queue_packet((uint8_t *)raw_report, CONSUMER_CONTROL_MSG, CONSUMER_CONTROL_LENGTH); + } +} + +/* Decide if consumer control reports go local or to the other board */ +void send_system_control(uint8_t *raw_report, device_t *state) { + if (CURRENT_BOARD_IS_ACTIVE_OUTPUT) { + tud_hid_n_report(0, REPORT_ID_SYSTEM, raw_report, SYSTEM_CONTROL_LENGTH); + state->last_activity[BOARD_ROLE] = time_us_64(); + } else { + queue_packet((uint8_t *)raw_report, SYSTEM_CONTROL_MSG, SYSTEM_CONTROL_LENGTH); } } @@ -200,21 +206,22 @@ void send_consumer_control(uint8_t *raw_report, device_t *state) { * Parse and interpret the keys pressed on the keyboard * ==================================================== */ -void process_keyboard_report(uint8_t *raw_report, int length, device_t *state) { - hid_keyboard_report_t *keyboard_report = (hid_keyboard_report_t *)raw_report; - hotkey_combo_t *hotkey = NULL; +void process_keyboard_report(uint8_t *raw_report, int length, uint8_t itf, hid_interface_t *iface) { + hid_keyboard_report_t new_report = {0}; + device_t *state = &global_state; + hotkey_combo_t *hotkey = NULL; if (length < KBD_REPORT_LENGTH) return; - /* If the report is longer by 1 byte, we can assume the first byte is the report ID - and that the keyboard didn't switch to boot mode properly. Use this workaround - until full HID report parsing is implemented */ - if (length == KBD_REPORT_LENGTH + 1) - keyboard_report = (hid_keyboard_report_t *)(raw_report + 1); + /* No more keys accepted if we're about to reboot */ + if (global_state.reboot_requested) + return; + + extract_kbd_data(raw_report, length, itf, iface, &new_report); /* Check if any hotkey was pressed */ - hotkey = check_all_hotkeys(keyboard_report, state); + hotkey = check_all_hotkeys(&new_report, state); /* ... and take appropriate action */ if (hotkey != NULL) { @@ -223,7 +230,7 @@ void process_keyboard_report(uint8_t *raw_report, int length, device_t *state) { blink_led(state); /* Execute the corresponding handler */ - hotkey->action_handler(state, keyboard_report); + hotkey->action_handler(state, &new_report); /* And pass the key to the output PC if configured to do so. */ if (!hotkey->pass_to_os) @@ -231,22 +238,35 @@ void process_keyboard_report(uint8_t *raw_report, int length, device_t *state) { } /* This method will decide if the key gets queued locally or sent through UART */ - send_key(keyboard_report, state); + send_key(&new_report, state); } -void process_consumer_report(uint8_t *raw_report, int length, device_t *state) { +void process_consumer_report(uint8_t *raw_report, int length, uint8_t itf, hid_interface_t *iface) { uint8_t new_report[CONSUMER_CONTROL_LENGTH] = {0}; + uint16_t *report_ptr = (uint16_t *)new_report; - /* We expect length not to be zero or bail out */ - if (!length) - return; + /* If consumer control is variable, read the values from cc_array and send as array. */ + if (iface->consumer.is_variable) { + for (int i = 0; i < MAX_CC_BUTTONS && i < 8 * (length - 1); i++) { + int bit_idx = i % 8; + int byte_idx = i >> 3; - /* Consumer control report ID rewrite and forward */ - if (raw_report[0] && raw_report[0] == global_state.kbd_dev.consumer_report_id) { - for (int i = 0; i < length - 1 || i < CONSUMER_CONTROL_LENGTH; i++) { - new_report[i] = raw_report[i + 1]; + if ((raw_report[byte_idx + 1] >> bit_idx) & 1) { + report_ptr[0] = iface->keyboard.cc_array[i]; + } } - - send_consumer_control(new_report, &global_state); } + else { + for (int i = 0; i < length - 1 && i < CONSUMER_CONTROL_LENGTH; i++) + new_report[i] = raw_report[i + 1]; + } + + send_consumer_control(new_report, &global_state); +} + +void process_system_report(uint8_t *raw_report, int length, uint8_t itf, hid_interface_t *iface) { + uint16_t new_report = raw_report[1]; + uint8_t *report_ptr = (uint8_t *)&new_report; + + send_system_control(report_ptr, &global_state); } \ No newline at end of file diff --git a/src/led.c b/src/led.c index 4f890b4..af04ae3 100644 --- a/src/led.c +++ b/src/led.c @@ -47,6 +47,13 @@ void restore_leds(device_t *state) { } } +uint8_t toggle_led(void) { + uint8_t new_led_state = gpio_get(GPIO_LED_PIN) ^ 1; + gpio_put(GPIO_LED_PIN, new_led_state); + + return new_led_state; +} + void blink_led(device_t *state) { /* Since LEDs might be ON previously, we go OFF, ON, OFF, ON, OFF */ state->blinks_left = 5; @@ -56,7 +63,7 @@ void blink_led(device_t *state) { void led_blinking_task(device_t *state) { const int blink_interval_us = 80000; /* 80 ms off, 80 ms on */ static uint8_t leds; - + /* If there is no more blinking to be done, exit immediately */ if (state->blinks_left == 0) return; @@ -66,8 +73,7 @@ void led_blinking_task(device_t *state) { return; /* Toggle the LED state */ - uint8_t new_led_state = gpio_get(GPIO_LED_PIN) ^ 1; - gpio_put(GPIO_LED_PIN, new_led_state); + uint8_t new_led_state = toggle_led(); /* Also keyboard leds (if it's connected locally) since on-board leds are not visible */ leds = new_led_state * 0x07; /* Numlock, capslock, scrollock */ diff --git a/src/main.c b/src/main.c index 06cc77e..d8ba3c4 100644 --- a/src/main.c +++ b/src/main.c @@ -17,15 +17,29 @@ #include "main.h" -/********* Global Variable **********/ -device_t global_state = {0}; -device_t *device = &global_state; +/********* Global Variables **********/ +device_t global_state = {0}; +device_t *device = &global_state; -/**================================================== * +firmware_metadata_t _firmware_metadata __attribute__((section(".section_metadata"))) = { + .version = 0x0001, +}; + +/* ================================================== * * ============== Main Program Loops ============== * * ================================================== */ int main(void) { + static task_t tasks_core0[] = { + [0] = {.exec = &usb_device_task, .frequency = _TOP()}, // .-> USB device task, needs to run as often as possible + [1] = {.exec = &kick_watchdog_task, .frequency = _HZ(30)}, // | Verify core1 is still running and if so, reset watchdog timer + [2] = {.exec = &process_kbd_queue_task, .frequency = _HZ(2000)}, // | Check if there were any keypresses and send them + [3] = {.exec = &process_mouse_queue_task, .frequency = _HZ(2000)}, // | Check if there were any mouse movements and send them + [4] = {.exec = &process_cfg_queue_task, .frequency = _HZ(1000)}, // | Check if there are any packets to send over vendor link + [5] = {.exec = &process_uart_tx_task, .frequency = _TOP()}, // | Check if there are any packets to send over UART + }; // `----- then go back and repeat forever + const int NUM_TASKS = ARRAY_SIZE(tasks_core0); + // Wait for the board to settle sleep_ms(10); @@ -36,40 +50,28 @@ int main(void) { switch_output(device, OUTPUT_A); while (true) { - // USB device task, needs to run as often as possible - tud_task(); - - // Verify core1 is still running and if so, reset watchdog timer - kick_watchdog(device); - - // Check if there were any keypresses and send them - process_kbd_queue_task(device); - - // Check if there were any mouse movements and send them - process_mouse_queue_task(device); - } + for (int i = 0; i < NUM_TASKS; i++) + task_scheduler(device, &tasks_core0[i]); + } } void core1_main() { - uart_packet_t in_packet = {0}; + static task_t tasks_core1[] = { + [0] = {.exec = &usb_host_task, .frequency = _TOP()}, // .-> USB host task, needs to run as often as possible + [1] = {.exec = &packet_receiver_task, .frequency = _TOP()}, // | Receive data over serial from the other board + [2] = {.exec = &led_blinking_task, .frequency = _HZ(30)}, // | Check if LED needs blinking + [3] = {.exec = &screensaver_task, .frequency = _HZ(120)}, // | Handle "screensaver" movements + [4] = {.exec = &firmware_upgrade_task, .frequency = _HZ(4000)}, // | Send firmware to the other board if needed + [5] = {.exec = &heartbeat_output_task, .frequency = _HZ(1)}, // | Output periodic heartbeats + }; // `----- then go back and repeat forever + const int NUM_TASKS = ARRAY_SIZE(tasks_core1); while (true) { // Update the timestamp, so core0 can figure out if we're dead device->core1_last_loop_pass = time_us_64(); - // USB host task, needs to run as often as possible - if (tuh_inited()) - tuh_task(); - - // Receives data over serial from the other board - receive_char(&in_packet, device); - - // Check if LED needs blinking - led_blinking_task(device); - - // Mouse screensaver task - screensaver_task(device); + for (int i = 0; i < NUM_TASKS; i++) + task_scheduler(device, &tasks_core1[i]); } } - /* ======= End of Main Program Loops ======= */ diff --git a/src/mouse.c b/src/mouse.c index 0a35175..5a4a2d6 100644 --- a/src/mouse.c +++ b/src/mouse.c @@ -48,7 +48,7 @@ int32_t accelerate(int32_t offset) { {70, 4.0}, // ------------------------------------------- }; // 10 20 30 40 50 60 70 - if (!ENABLE_ACCELERATION) + if (!global_state.config.enable_acceleration) return offset; for (int i = 0; i < 7; i++) { @@ -73,8 +73,8 @@ void update_mouse_position(device_t *state, mouse_values_t *values) { int offset_y = accelerate(values->move_y) * (current->speed_y >> reduce_speed); /* Update movement */ - state->mouse_x = move_and_keep_on_screen(state->mouse_x, offset_x); - state->mouse_y = move_and_keep_on_screen(state->mouse_y, offset_y); + state->pointer_x = move_and_keep_on_screen(state->pointer_x, offset_x); + state->pointer_y = move_and_keep_on_screen(state->pointer_y, offset_y); /* Update buttons state */ state->mouse_buttons = values->buttons; @@ -86,8 +86,8 @@ void output_mouse_report(mouse_report_t *report, device_t *state) { queue_mouse_report(report, state); state->last_activity[BOARD_ROLE] = time_us_64(); } else { - send_packet((uint8_t *)report, MOUSE_REPORT_MSG, MOUSE_REPORT_LENGTH); - } + queue_packet((uint8_t *)report, MOUSE_REPORT_MSG, MOUSE_REPORT_LENGTH); + } } /* Calculate and return Y coordinate when moving from screen out_from to screen out_to */ @@ -100,38 +100,41 @@ int16_t scale_y_coordinate(int screen_from, int screen_to, device_t *state) { /* If sizes match, there is nothing to do */ if (size_from == size_to) - return state->mouse_y; + return state->pointer_y; /* Moving from smaller ==> bigger screen y_a = top + (((bottom - top) * y_b) / HEIGHT) */ if (size_from > size_to) { - return to->border.top + ((size_to * state->mouse_y) / MAX_SCREEN_COORD); + return to->border.top + ((size_to * state->pointer_y) / MAX_SCREEN_COORD); } /* Moving from bigger ==> smaller screen y_b = ((y_a - top) * HEIGHT) / (bottom - top) */ - if (state->mouse_y < from->border.top) + if (state->pointer_y < from->border.top) return MIN_SCREEN_COORD; - if (state->mouse_y > from->border.bottom) + if (state->pointer_y > from->border.bottom) return MAX_SCREEN_COORD; - return ((state->mouse_y - from->border.top) * MAX_SCREEN_COORD) / size_from; + return ((state->pointer_y - from->border.top) * MAX_SCREEN_COORD) / size_from; } void switch_screen( device_t *state, output_t *output, int new_x, int output_from, int output_to, int direction) { - unsigned mouse_y = (MOUSE_PARKING_POSITION == 0) ? MIN_SCREEN_COORD : /*TOP*/ - (MOUSE_PARKING_POSITION == 1) ? MAX_SCREEN_COORD : /*BOTTOM*/ - state->mouse_y; /*PREVIOUS*/ - mouse_report_t hidden_pointer = {.y = mouse_y, .x = MAX_SCREEN_COORD}; + uint8_t *mouse_park_pos = &state->config.output[state->active_output].mouse_park_pos; + + int16_t mouse_y = (*mouse_park_pos == 0) ? MIN_SCREEN_COORD : /* Top */ + (*mouse_park_pos == 1) ? MAX_SCREEN_COORD : /* Bottom */ + state->pointer_y; /* Previous */ + + mouse_report_t hidden_pointer = {.y = mouse_y, .x = MAX_SCREEN_COORD}; output_mouse_report(&hidden_pointer, state); switch_output(state, output_to); - state->mouse_x = (direction == LEFT) ? MAX_SCREEN_COORD : MIN_SCREEN_COORD; - state->mouse_y = scale_y_coordinate(output->number, 1 - output->number, state); + state->pointer_x = (direction == LEFT) ? MAX_SCREEN_COORD : MIN_SCREEN_COORD; + state->pointer_y = scale_y_coordinate(output->number, 1 - output->number, state); } void switch_desktop(device_t *state, output_t *output, int new_index, int direction) { @@ -142,9 +145,9 @@ void switch_desktop(device_t *state, output_t *output, int new_index, int direct switch (output->os) { case MACOS: - /* Once isn't reliable enough, but repeating it does the trick */ - for (int move_cnt=0; move_cnt<5; move_cnt++) - output_mouse_report(&move_relative_one, state); + /* Once doesn't seem reliable enough, do it twice */ + output_mouse_report(&move_relative_one, state); + output_mouse_report(&move_relative_one, state); break; case WINDOWS: @@ -153,13 +156,14 @@ void switch_desktop(device_t *state, output_t *output, int new_index, int direct break; case LINUX: + case ANDROID: case OTHER: /* Linux should treat all desktops as a single virtual screen, so you should leave screen_count at 1 and it should just work */ break; } - state->mouse_x = (direction == RIGHT) ? MIN_SCREEN_COORD : MAX_SCREEN_COORD; + state->pointer_x = (direction == RIGHT) ? MIN_SCREEN_COORD : MAX_SCREEN_COORD; output->screen_index = new_index; } @@ -172,11 +176,11 @@ void switch_desktop(device_t *state, output_t *output, int new_index, int direct )___( )___( | )___( )___( )___( */ void check_screen_switch(const mouse_values_t *values, device_t *state) { - int new_x = state->mouse_x + values->move_x; + int new_x = state->pointer_x + values->move_x; output_t *output = &state->config.output[state->active_output]; - bool jump_left = new_x < MIN_SCREEN_COORD - JUMP_THRESHOLD; - bool jump_right = new_x > MAX_SCREEN_COORD + JUMP_THRESHOLD; + bool jump_left = new_x < MIN_SCREEN_COORD - state->config.jump_treshold; + bool jump_right = new_x > MAX_SCREEN_COORD + state->config.jump_treshold; int direction = jump_left ? LEFT : RIGHT; @@ -203,9 +207,9 @@ void check_screen_switch(const mouse_values_t *values, device_t *state) { switch_desktop(state, output, output->screen_index + 1, direction); } -void extract_report_values(uint8_t *raw_report, device_t *state, mouse_values_t *values) { +void extract_report_values(uint8_t *raw_report, device_t *state, mouse_values_t *values, hid_interface_t *iface) { /* Interpret values depending on the current protocol used. */ - if (state->mouse_dev.protocol == HID_PROTOCOL_BOOT) { + if (iface->protocol == HID_PROTOCOL_BOOT) { hid_mouse_report_t *mouse_report = (hid_mouse_report_t *)raw_report; values->move_x = mouse_report->x; @@ -216,20 +220,20 @@ void extract_report_values(uint8_t *raw_report, device_t *state, mouse_values_t } /* If HID Report ID is used, the report is prefixed by the report ID so we have to move by 1 byte */ - if (state->mouse_dev.uses_report_id) + if (iface->mouse.report_id) raw_report++; - values->move_x = get_report_value(raw_report, &state->mouse_dev.move_x); - values->move_y = get_report_value(raw_report, &state->mouse_dev.move_y); - values->wheel = get_report_value(raw_report, &state->mouse_dev.wheel); - values->buttons = get_report_value(raw_report, &state->mouse_dev.buttons); + values->move_x = get_report_value(raw_report, &iface->mouse.move_x); + values->move_y = get_report_value(raw_report, &iface->mouse.move_y); + values->wheel = get_report_value(raw_report, &iface->mouse.wheel); + values->buttons = get_report_value(raw_report, &iface->mouse.buttons); } mouse_report_t create_mouse_report(device_t *state, mouse_values_t *values) { mouse_report_t mouse_report = { .buttons = values->buttons, - .x = state->mouse_x, - .y = state->mouse_y, + .x = state->pointer_x, + .y = state->pointer_y, .wheel = values->wheel, .mode = ABSOLUTE, }; @@ -242,14 +246,16 @@ mouse_report_t create_mouse_report(device_t *state, mouse_values_t *values) { mouse_report.buttons = values->buttons; mouse_report.wheel = values->wheel; } + return mouse_report; } -void process_mouse_report(uint8_t *raw_report, int len, device_t *state) { +void process_mouse_report(uint8_t *raw_report, int len, uint8_t itf, hid_interface_t *iface) { mouse_values_t values = {0}; + device_t *state = &global_state; /* Interpret the mouse HID report, extract and save values we need. */ - extract_report_values(raw_report, state, &values); + extract_report_values(raw_report, state, &values, iface); /* Calculate and update mouse pointer movement. */ update_mouse_position(state, &values); @@ -261,7 +267,7 @@ void process_mouse_report(uint8_t *raw_report, int len, device_t *state) { output_mouse_report(&report, state); /* We use the mouse to switch outputs, the logic is in check_screen_switch() */ - check_screen_switch(&values, state); + check_screen_switch(&values, state); } /* ==================================================== * @@ -283,6 +289,10 @@ void process_mouse_queue_task(device_t *state) { if (tud_suspended()) tud_remote_wakeup(); + /* If it's not ready, we'll try on the next pass */ + if (!tud_hid_n_ready(ITF_NUM_HID)) + return; + /* ... try sending it to the host, if it's successful */ bool succeeded = tud_mouse_report(report.mode, report.buttons, report.x, report.y, report.wheel); diff --git a/src/protocol.c b/src/protocol.c new file mode 100644 index 0000000..d28d8f0 --- /dev/null +++ b/src/protocol.c @@ -0,0 +1,69 @@ +#include "main.h" + +const field_map_t api_field_map[] = { +/* Index, Rdonly, Type, Len, Offset in struct */ + { 0, true, UINT8, 1, offsetof(device_t, active_output) }, + { 1, true, INT16, 2, offsetof(device_t, pointer_x) }, + { 2, true, INT16, 2, offsetof(device_t, pointer_y) }, + { 3, true, INT16, 2, offsetof(device_t, mouse_buttons) }, + + /* Output A */ + { 10, false, UINT32, 4, offsetof(device_t, config.output[0].number) }, + { 11, false, UINT32, 4, offsetof(device_t, config.output[0].screen_count) }, + { 12, false, INT32, 4, offsetof(device_t, config.output[0].speed_x) }, + { 13, false, INT32, 4, offsetof(device_t, config.output[0].speed_y) }, + { 14, false, INT32, 4, offsetof(device_t, config.output[0].border.top) }, + { 15, false, INT32, 4, offsetof(device_t, config.output[0].border.bottom) }, + { 16, false, UINT8, 1, offsetof(device_t, config.output[0].os) }, + { 17, false, UINT8, 1, offsetof(device_t, config.output[0].pos) }, + { 18, false, UINT8, 1, offsetof(device_t, config.output[0].mouse_park_pos) }, + { 19, false, UINT8, 1, offsetof(device_t, config.output[0].screensaver.mode) }, + { 20, false, UINT8, 1, offsetof(device_t, config.output[0].screensaver.only_if_inactive) }, + + /* Until we increase the payload size from 8 bytes, clamp to avoid exceeding the field size */ + { 21, false, UINT64, 7, offsetof(device_t, config.output[0].screensaver.idle_time_us) }, + { 22, false, UINT64, 7, offsetof(device_t, config.output[0].screensaver.max_time_us) }, + + /* Output B */ + { 40, false, UINT32, 4, offsetof(device_t, config.output[1].number) }, + { 41, false, UINT32, 4, offsetof(device_t, config.output[1].screen_count) }, + { 42, false, INT32, 4, offsetof(device_t, config.output[1].speed_x) }, + { 43, false, INT32, 4, offsetof(device_t, config.output[1].speed_y) }, + { 44, false, INT32, 4, offsetof(device_t, config.output[1].border.top) }, + { 45, false, INT32, 4, offsetof(device_t, config.output[1].border.bottom) }, + { 46, false, UINT8, 1, offsetof(device_t, config.output[1].os) }, + { 47, false, UINT8, 1, offsetof(device_t, config.output[1].pos) }, + { 48, false, UINT8, 1, offsetof(device_t, config.output[1].mouse_park_pos) }, + { 49, false, UINT8, 1, offsetof(device_t, config.output[1].screensaver.mode) }, + { 50, false, UINT8, 1, offsetof(device_t, config.output[1].screensaver.only_if_inactive) }, + { 51, false, UINT64, 7, offsetof(device_t, config.output[1].screensaver.idle_time_us) }, + { 52, false, UINT64, 7, offsetof(device_t, config.output[1].screensaver.max_time_us) }, + + /* Common config */ + { 70, false, UINT32, 4, offsetof(device_t, config.version) }, + { 71, false, UINT8, 1, offsetof(device_t, config.force_mouse_boot_mode) }, + { 72, false, UINT8, 1, offsetof(device_t, config.force_kbd_boot_protocol) }, + { 73, false, UINT8, 1, offsetof(device_t, config.kbd_led_as_indicator) }, + { 74, false, UINT8, 1, offsetof(device_t, config.hotkey_toggle) }, + { 75, false, UINT8, 1, offsetof(device_t, config.enable_acceleration) }, + { 76, false, UINT8, 1, offsetof(device_t, config.enforce_ports) }, + { 77, false, UINT16, 2, offsetof(device_t, config.jump_treshold) }, + + /* Firmware */ + { 78, true, UINT16, 2, offsetof(device_t, _running_fw.version) }, + { 79, true, UINT32, 4, offsetof(device_t, _running_fw.checksum) }, + + { 80, true, UINT8, 1, offsetof(device_t, keyboard_connected) }, + { 81, true, UINT8, 1, offsetof(device_t, switch_lock) }, + { 82, true, UINT8, 1, offsetof(device_t, relative_mouse) }, +}; + +const field_map_t* get_field_map_entry(uint32_t index) { + for (unsigned int i = 0; i < ARRAY_SIZE(api_field_map); i++) { + if (api_field_map[i].idx == index) { + return &api_field_map[i]; + } + } + + return NULL; +} \ No newline at end of file diff --git a/src/ramdisk.c b/src/ramdisk.c new file mode 100644 index 0000000..d31d2e1 --- /dev/null +++ b/src/ramdisk.c @@ -0,0 +1,119 @@ +/* + * This file is part of DeskHop (https://github.com/hrvach/deskhop). + * Copyright (c) 2024 Hrvoje Cavrak + * + * Based on the TinyUSB example by Ha Thach. + * + * 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, version 3. + * + * 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 . + */ + +#include "main.h" + +#define NUMBER_OF_BLOCKS 4096 +#define ACTUAL_NUMBER_OF_BLOCKS 128 +#define BLOCK_SIZE 512 + +void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) { + strcpy((char *)vendor_id, "DeskHop"); + strcpy((char *)product_id, "Config Mode"); + strcpy((char *)product_rev, "1.0"); +} + +bool tud_msc_test_unit_ready_cb(uint8_t lun) { + return true; +} + +void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size) { + *block_count = NUMBER_OF_BLOCKS; + *block_size = BLOCK_SIZE; +} + +bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) { + return true; +} + +/* Return the requested data, or -1 if out-of-bounds */ +int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize) { + const uint8_t *addr = &ADDR_DISK_IMAGE[lba * BLOCK_SIZE + offset]; + + if (lba >= NUMBER_OF_BLOCKS) + return -1; + + /* We lie about the image size - actually it's 64 kB, not 512 kB, so if we're out of bounds, return zeros */ + else if (lba >= ACTUAL_NUMBER_OF_BLOCKS) + memset(buffer, 0x00, bufsize); + + else + memcpy(buffer, addr, bufsize); + + return (int32_t)bufsize; +} + +/* We're writable, so return true */ +bool tud_msc_is_writable_cb(uint8_t lun) { + return true; +} + +/* Simple firmware write routine, we get 512-byte uf2 blocks with 256 byte payload */ +int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize) { + uf2_t *uf2 = (uf2_t *)&buffer[0]; + bool is_final_block = uf2->blockNo == (STAGING_IMAGE_SIZE / FLASH_PAGE_SIZE) - 1; + uint32_t flash_addr = (uint32_t)ADDR_FW_RUNNING + uf2->blockNo * FLASH_PAGE_SIZE - XIP_BASE; + + if (lba >= NUMBER_OF_BLOCKS) + return -1; + + /* If we're not detecting UF2 magic constants, we have nothing to do... */ + if (uf2->magicStart0 != UF2_MAGIC_START0 || uf2->magicStart1 != UF2_MAGIC_START1 || uf2->magicEnd != UF2_MAGIC_END) + return (int32_t)bufsize; + + if (uf2->blockNo == 0) { + global_state.fw.checksum = 0xffffffff; + + /* Make sure nobody else touches the flash during this operation, otherwise we get empty pages */ + global_state.fw.upgrade_in_progress = true; + } + + /* Update checksum continuously as blocks are being received */ + const uint32_t last_block_with_checksum = (STAGING_IMAGE_SIZE - FLASH_SECTOR_SIZE) / FLASH_PAGE_SIZE; + for (int i=0; iblockNo < last_block_with_checksum; i++) + global_state.fw.checksum = crc32_iter(global_state.fw.checksum, buffer[32 + i]); + + write_flash_page(flash_addr, &buffer[32]); + + if (is_final_block) { + global_state.fw.checksum = ~global_state.fw.checksum; + + /* If checksums don't match, overwrite first sector and rely on ROM bootloader for recovery */ + if (global_state.fw.checksum != calculate_firmware_crc32()) { + flash_range_erase((uint32_t)ADDR_FW_RUNNING - XIP_BASE, FLASH_SECTOR_SIZE); + reset_usb_boot(1 << PICO_DEFAULT_LED_PIN, 0); + } + else { + global_state.reboot_requested = true; + } + } + + /* Provide some visual indication that fw is being uploaded */ + toggle_led(); + watchdog_update(); + + return (int32_t)bufsize; +} + +/* This is a super-dumb, rudimentary disk, any other scsi command is simply rejected */ +int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, uint16_t bufsize) { + tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); + return -1; +} + \ No newline at end of file diff --git a/src/setup.c b/src/setup.c index 333a44a..f3c8cfd 100644 --- a/src/setup.c +++ b/src/setup.c @@ -50,14 +50,13 @@ void serial_init() { * PIO USB configuration, D+ pin 14, D- pin 15 * ================================================== */ -void pio_usb_host_config(void) { +void pio_usb_host_config(device_t *state) { /* tuh_configure() must be called before tuh_init() */ static pio_usb_configuration_t config = PIO_USB_DEFAULT_CONFIG; config.pin_dp = PIO_USB_DP_PIN_DEFAULT; - /* Make HID protocol the default for port B as a fix for devices enumerating - themselves as both keyboards and mice, but having just a single common mode */ - if(BOARD_ROLE == OUTPUT_B) + /* Board B is always report mode, board A is default-boot if configured */ + if (state->board_role == OUTPUT_B || ENFORCE_KEYBOARD_BOOT_PROTOCOL == 0) tuh_hid_set_default_protocol(HID_PROTOCOL_REPORT); tuh_configure(BOARD_TUH_RHPORT, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &config); @@ -66,9 +65,153 @@ void pio_usb_host_config(void) { tuh_init(1); } +/* ================================================== * + * Board Autoprobe Routine + * ================================================== */ + +/* Probing algorithm logic: + - RX pin is driven by the digital isolator IC + - IF we are board A, it will be connected to pin 13 + and it will drive it either high or low at any given time + - Before uart setup, enable it as an input + - Go through a probing sequence of 8 values and pull either up or down + to that value + - Read out the value on the RX pin + - If the entire sequence of values match, we are definitely floating + so IC is not connected on BOARD_A_RX, and we're BOARD B +*/ +int board_autoprobe(void) { + const bool probing_sequence[] = {true, false, false, true, true, false, true, false}; + const int seq_len = ARRAY_SIZE(probing_sequence); + + /* Set the pin as INPUT and initialize it */ + gpio_init(BOARD_A_RX); + gpio_set_dir(BOARD_A_RX, GPIO_IN); + + for (int i=0; iscratch[5] == MAGIC_WORD_1 && + watchdog_hw->scratch[6] == MAGIC_WORD_2); + + /* Remove, so next reboot it's no longer active */ + if (is_active) + watchdog_hw->scratch[5] = 0; + + reset_config_timer(state); + + return is_active; +} + + +/* ================================================== * + * Configure DMA for reliable UART transfers + * ================================================== */ +const uint8_t* uart_buffer_pointers[1] = {uart_rxbuf}; +uint8_t uart_rxbuf[DMA_RX_BUFFER_SIZE] __attribute__((aligned(DMA_RX_BUFFER_SIZE))) ; +uint8_t uart_txbuf[DMA_TX_BUFFER_SIZE] __attribute__((aligned(DMA_TX_BUFFER_SIZE))) ; + +static void configure_tx_dma(device_t *state) { + state->dma_tx_channel = dma_claim_unused_channel(true); + + dma_channel_config tx_config = dma_channel_get_default_config(state->dma_tx_channel); + channel_config_set_transfer_data_size(&tx_config, DMA_SIZE_8); + + /* Writing uart (always write the same address, but source addr changes as we read) */ + channel_config_set_read_increment(&tx_config, true); + channel_config_set_write_increment(&tx_config, false); + + // channel_config_set_ring(&tx_config, false, 4); + channel_config_set_dreq(&tx_config, DREQ_UART0_TX); + + /* Configure, but don't start immediately. We'll do this each time the outgoing + packet is ready and we copy it to the buffer */ + dma_channel_configure( + state->dma_tx_channel, + &tx_config, + &uart0_hw->dr, + uart_txbuf, + 0, + false + ); +} + +static void configure_rx_dma(device_t *state) { + /* Find an empty channel, store it for later reference */ + state->dma_rx_channel = dma_claim_unused_channel(true); + state->dma_control_channel = dma_claim_unused_channel(true); + + dma_channel_config config = dma_channel_get_default_config(state->dma_rx_channel); + dma_channel_config control_config = dma_channel_get_default_config(state->dma_control_channel); + + channel_config_set_transfer_data_size(&config, DMA_SIZE_8); + channel_config_set_transfer_data_size(&control_config, DMA_SIZE_32); + + // The read address is the address of the UART data register which is constant + channel_config_set_read_increment(&config, false); + channel_config_set_read_increment(&control_config, false); + + // Read into a ringbuffer with 1024 (2^10) elements + channel_config_set_write_increment(&config, true); + channel_config_set_write_increment(&control_config, false); + + channel_config_set_ring(&config, true, 10); + + // The UART signals when data is avaliable + channel_config_set_dreq(&config, DREQ_UART0_RX); + + channel_config_set_chain_to(&config, state->dma_control_channel); + + dma_channel_configure( + state->dma_rx_channel, + &config, + uart_rxbuf, + &uart0_hw->dr, + DMA_RX_BUFFER_SIZE, + false); + + dma_channel_configure( + state->dma_control_channel, + &control_config, + &dma_hw->ch[state->dma_rx_channel].al2_write_addr_trig, + uart_buffer_pointers, + 1, + false); + + dma_channel_start(state->dma_control_channel); +} + + /* ================================================== * * Perform initial board/usb setup * ================================================== */ +int board; void initial_setup(device_t *state) { /* PIO USB requires a clock multiple of 12 MHz, setting to 120 MHz */ @@ -81,6 +224,12 @@ void initial_setup(device_t *state) { gpio_init(GPIO_LED_PIN); gpio_set_dir(GPIO_LED_PIN, GPIO_OUT); + /* Check if we should boot in configuration mode or not */ + state->config_mode_active = is_config_mode_active(state); + + /* Detect which board we're running on */ + state->board_role = board_autoprobe(); + /* Initialize and configure UART */ serial_init(); @@ -88,6 +237,12 @@ void initial_setup(device_t *state) { queue_init(&state->kbd_queue, sizeof(hid_keyboard_report_t), KBD_QUEUE_LENGTH); queue_init(&state->mouse_queue, sizeof(mouse_report_t), MOUSE_QUEUE_LENGTH); + /* Initialize vendor config protocol queue */ + queue_init(&state->cfg_queue_out, sizeof(uart_packet_t), CFG_QUEUE_LENGTH); + + /* Initialize UART queue */ + queue_init(&state->uart_tx_queue, sizeof(uart_packet_t), UART_QUEUE_LENGTH); + /* Setup RP2040 Core 1 */ multicore_reset_core1(); multicore_launch_core1(core1_main); @@ -96,11 +251,18 @@ void initial_setup(device_t *state) { tud_init(BOARD_TUD_RHPORT); /* Initialize and configure TinyUSB Host */ - pio_usb_host_config(); + pio_usb_host_config(state); + + /* Initialize and configure DMA */ + configure_tx_dma(state); + configure_rx_dma(state); + + /* Load the current firmware info */ + state->_running_fw = _firmware_metadata; /* Update the core1 initial pass timestamp before enabling the watchdog */ state->core1_last_loop_pass = time_us_64(); - + /* Setup the watchdog so we reboot and recover from a crash */ watchdog_enable(WATCHDOG_TIMEOUT, WATCHDOG_PAUSE_ON_DEBUG); } diff --git a/src/tasks.c b/src/tasks.c new file mode 100644 index 0000000..5026858 --- /dev/null +++ b/src/tasks.c @@ -0,0 +1,224 @@ +/* + * This file is part of DeskHop (https://github.com/hrvach/deskhop). + * Copyright (c) 2024 Hrvoje Cavrak + * + * 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, version 3. + * + * 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 . + */ + +#include "main.h" + +void task_scheduler(device_t *state, task_t *task) { + uint64_t current_time = time_us_64(); + + if (current_time < task->next_run) + return; + + task->next_run = current_time + task->frequency; + task->exec(state); +} + +/**================================================== * + * ============== Watchdog Functions ============== * + * ================================================== */ + +void kick_watchdog_task(device_t *state) { + /* Read the timer AFTER duplicating the core1 timestamp, + so it doesn't get updated in the meantime. */ + uint64_t core1_last_loop_pass = state->core1_last_loop_pass; + uint64_t current_time = time_us_64(); + + /* If a reboot is requested, we'll stop updating watchdog */ + if (state->reboot_requested) + return; + + /* If core1 stops updating the timestamp, we'll stop kicking the watchog and reboot */ + if (current_time - core1_last_loop_pass < CORE1_HANG_TIMEOUT_US) + watchdog_update(); +} + +/**================================================== * + * =============== USB Device / Host ============== * + * ================================================== */ + +void usb_device_task(device_t *state) { + tud_task(); +} + +void usb_host_task(device_t *state) { + if (tuh_inited()) + tuh_task(); +} + +/* Have something fun and entertaining when idle. */ +void screensaver_task(device_t *state) { + const int mouse_move_delay = 5000; + screensaver_t *screensaver = &state->config.output[BOARD_ROLE].screensaver; + uint64_t inactivity_period = time_us_64() - state->last_activity[BOARD_ROLE]; + + static mouse_report_t report = {0}; + static int last_pointer_move = 0; + static int dx = 20, dy = 25, jitter = 1; + + /* If we're not enabled, nothing to do here. */ + if (screensaver->mode == DISABLED) + return; + + /* System is still not idle for long enough to activate or we've been running for too long */ + if (inactivity_period < screensaver->idle_time_us) + return; + + /* We exceeded the maximum permitted screensaver runtime */ + if (screensaver->max_time_us + && inactivity_period > (screensaver->max_time_us + screensaver->idle_time_us)) + return; + + /* If we're the selected output and we can only run on inactive output, nothing to do here. */ + if (screensaver->only_if_inactive && CURRENT_BOARD_IS_ACTIVE_OUTPUT) + return; + + /* We're active! Now check if it's time to move the cursor yet. */ + if ((time_us_32()) - last_pointer_move < mouse_move_delay) + return; + + switch (screensaver->mode) { + case PONG: + /* Check if we are bouncing off the walls and reverse direction in that case. */ + if (report.x + dx < MIN_SCREEN_COORD || report.x + dx > MAX_SCREEN_COORD) + dx = -dx; + + if (report.y + dy < MIN_SCREEN_COORD || report.y + dy > MAX_SCREEN_COORD) + dy = -dy; + + report.x += dx; + report.y += dy; + + break; + + case JITTER: + report.x = state->pointer_x + jitter; + report.y = state->pointer_y + jitter; + jitter = -jitter; + break; + } + + /* Move mouse pointer */ + queue_mouse_report(&report, state); + + /* Update timer of the last pointer move */ + last_pointer_move = time_us_32(); +} + +/* Periodically emit heartbeat packets */ +void heartbeat_output_task(device_t *state) { + /* If firmware upgrade is in progress, don't touch flash_cs */ + if (state->fw.upgrade_in_progress) + return; + + if (state->config_mode_active) { + /* Leave config mode if timeout expired and user didn't click exit */ + if (time_us_64() > state->config_mode_timer) + reboot(); + + /* Keep notifying the user we're still in config mode */ + blink_led(state); + } + + /* Holding the button invokes bootsel firmware upgrade */ + if (is_bootsel_pressed()) + reset_usb_boot(1 << PICO_DEFAULT_LED_PIN, 0); + + uart_packet_t packet = { + .type = HEARTBEAT_MSG, + .data16 = { + [0] = state->_running_fw.version, + [2] = state->active_output, + }, + }; + + queue_try_add(&global_state.uart_tx_queue, &packet); +} + +/* Process outgoing config report messages. */ +void process_cfg_queue_task(device_t *state) { + uint8_t raw_packet[RAW_PACKET_LENGTH] = {[0] = START1, [1] = START2, [11] = 0}; + uart_packet_t packet; + + if (!queue_try_peek(&state->cfg_queue_out, &packet)) + return; + + if (!tud_hid_n_ready(ITF_NUM_HID_VENDOR)) + return; + + write_raw_packet(raw_packet, &packet); + + /* ... try sending it to the host, if it's successful */ + bool succeeded = tud_hid_n_report(ITF_NUM_HID_VENDOR, REPORT_ID_VENDOR, raw_packet, RAW_PACKET_LENGTH); + + /* ... then we can remove it from the queue. Race conditions shouldn't happen [tm] */ + if (succeeded) + queue_try_remove(&state->cfg_queue_out, &packet); +} + +/* Task that handles copying firmware from the other device to ours */ +void firmware_upgrade_task(device_t *state) { + if (!state->fw.upgrade_in_progress || !state->fw.byte_done) + return; + + if (queue_is_full(&state->uart_tx_queue)) + return; + + /* End condition, when reached the process is completed. */ + if (state->fw.address > STAGING_IMAGE_SIZE) { + state->fw.upgrade_in_progress = 0; + state->fw.checksum = ~state->fw.checksum; + + /* Checksum mismatch, we wipe the stage 2 bootloader and rely on ROM recovery */ + if(calculate_firmware_crc32() != state->fw.checksum) { + flash_range_erase((uint32_t)ADDR_FW_RUNNING - XIP_BASE, FLASH_SECTOR_SIZE); + reset_usb_boot(1 << PICO_DEFAULT_LED_PIN, 0); + } + + else { + state->_running_fw = _firmware_metadata; + global_state.reboot_requested = true; + } + } + + /* If we're on the last element of the current page, page is done - write it. */ + if (TU_U32_BYTE0(state->fw.address) == 0x00) { + + uint32_t page_start_addr = (state->fw.address - 1) & 0xFFFFFF00; + write_flash_page((uint32_t)ADDR_FW_RUNNING + page_start_addr - XIP_BASE, state->page_buffer); + } + + request_byte(state, state->fw.address); +} + +void packet_receiver_task(device_t *state) { + uint32_t current_pointer + = (uint32_t)DMA_RX_BUFFER_SIZE - dma_channel_hw_addr(state->dma_rx_channel)->transfer_count; + uint32_t delta = get_ptr_delta(current_pointer, state); + + /* If we don't have enough characters for a packet, skip loop and return immediately */ + while (delta >= RAW_PACKET_LENGTH) { + if (is_start_of_packet(state)) { + fetch_packet(state); + process_packet(&state->in_packet, state); + return; + } + + /* No packet found, advance to next position and decrement delta */ + state->dma_ptr = NEXT_RING_IDX(state->dma_ptr); + delta--; + } +} \ No newline at end of file diff --git a/src/uart.c b/src/uart.c index 3f7b7ba..c99b953 100644 --- a/src/uart.c +++ b/src/uart.c @@ -21,23 +21,43 @@ * =============== Sending Packets ================ * * ================================================== */ -void send_packet(const uint8_t *data, enum packet_type_e packet_type, int length) { - uint8_t raw_packet[RAW_PACKET_LENGTH] = {[0] = START1, - [1] = START2, - [2] = packet_type, - /* [3-10] is data, defaults to 0 */ - [11] = calc_checksum(data, length)}; - - if (length > 0) - memcpy(&raw_packet[START_LENGTH + TYPE_LENGTH], data, length); - - /* Packets are short, fixed length, high speed and there is no flow control to block this */ - uart_write_blocking(SERIAL_UART, raw_packet, RAW_PACKET_LENGTH); +/* Takes a packet as uart_packet_t struct, adds preamble, checksum and encodes it to a raw array. */ +void write_raw_packet(uint8_t *dst, uart_packet_t *packet) { + uint8_t pkt[RAW_PACKET_LENGTH] = {[0] = START1, + [1] = START2, + [2] = packet->type, + /* [3-10] is data, defaults to 0 */ + [11] = calc_checksum(packet->data, PACKET_DATA_LENGTH)}; + + memcpy(&pkt[START_LENGTH + TYPE_LENGTH], packet->data, PACKET_DATA_LENGTH); + memcpy(dst, &pkt, RAW_PACKET_LENGTH); } +/* Schedule packet for sending to the other box */ +void queue_packet(const uint8_t *data, enum packet_type_e packet_type, int length) { + uart_packet_t packet = {.type = packet_type}; + memcpy(packet.data, data, length); + + queue_try_add(&global_state.uart_tx_queue, &packet); +} + +/* Sends just one byte of a certain packet type to the other box. */ void send_value(const uint8_t value, enum packet_type_e packet_type) { - const uint8_t data = value; - send_packet(&data, packet_type, sizeof(uint8_t)); + queue_packet(&value, packet_type, sizeof(uint8_t)); +} + +/* Process outgoing config report messages. */ +void process_uart_tx_task(device_t *state) { + uart_packet_t packet = {0}; + + if (dma_channel_is_busy(state->dma_tx_channel)) + return; + + if (!queue_try_remove(&state->uart_tx_queue, &packet)) + return; + + write_raw_packet(uart_txbuf, &packet); + dma_channel_transfer_from_buffer_now(state->dma_tx_channel, uart_txbuf, RAW_PACKET_LENGTH); } /**================================================== * @@ -45,19 +65,33 @@ void send_value(const uint8_t value, enum packet_type_e packet_type) { * ================================================== */ const uart_handler_t uart_handler[] = { + /* Core functions */ {.type = KEYBOARD_REPORT_MSG, .handler = handle_keyboard_uart_msg}, {.type = MOUSE_REPORT_MSG, .handler = handle_mouse_abs_uart_msg}, {.type = OUTPUT_SELECT_MSG, .handler = handle_output_select_msg}, - {.type = FIRMWARE_UPGRADE_MSG, .handler = handle_fw_upgrade_msg}, + + /* Box control */ {.type = MOUSE_ZOOM_MSG, .handler = handle_mouse_zoom_msg}, {.type = KBD_SET_REPORT_MSG, .handler = handle_set_report_msg}, {.type = SWITCH_LOCK_MSG, .handler = handle_switch_lock_msg}, {.type = SYNC_BORDERS_MSG, .handler = handle_sync_borders_msg}, {.type = FLASH_LED_MSG, .handler = handle_flash_led_msg}, - {.type = SCREENSAVER_MSG, .handler = handle_screensaver_msg}, + {.type = CONSUMER_CONTROL_MSG, .handler = handle_consumer_control_msg}, + + /* Config */ {.type = WIPE_CONFIG_MSG, .handler = handle_wipe_config_msg}, - {.type = OUTPUT_CONFIG_MSG, .handler = handle_output_config_msg}, - {.type = CONSUMER_CONTROL_MSG, .handler = handle_consumer_control_msg}, + {.type = SAVE_CONFIG_MSG, .handler = handle_save_config_msg}, + {.type = REBOOT_MSG, .handler = handle_reboot_msg}, + {.type = GET_VAL_MSG, .handler = handle_api_msgs}, + {.type = SET_VAL_MSG, .handler = handle_api_msgs}, + + /* Firmware */ + {.type = REQUEST_BYTE_MSG, .handler = handle_request_byte_msg}, + {.type = RESPONSE_BYTE_MSG, .handler = handle_response_byte_msg}, + {.type = FIRMWARE_UPGRADE_MSG, .handler = handle_fw_upgrade_msg}, + + {.type = HEARTBEAT_MSG, .handler = handle_heartbeat_msg}, + {.type = PROXY_PACKET_MSG, .handler = handle_proxy_msg}, }; void process_packet(uart_packet_t *packet, device_t *state) { @@ -71,62 +105,3 @@ void process_packet(uart_packet_t *packet, device_t *state) { } } } - -/**================================================== * - * ============== Receiving Packets =============== * - * ================================================== */ - -/* We are in IDLE state until we detect the packet start (0xAA 0x55) */ -void handle_idle_state(uint8_t *raw_packet, device_t *state) { - if (!uart_is_readable(SERIAL_UART)) { - return; - } - - raw_packet[0] = raw_packet[1]; /* Remember the previous byte received */ - raw_packet[1] = uart_getc(SERIAL_UART); /* Try to match packet start */ - - /* If we found 0xAA 0x55, we're in sync and can move on to read/process the packet */ - if (raw_packet[0] == START1 && raw_packet[1] == START2) { - state->receiver_state = READING_PACKET; - } -} - -/* Read a character off the line until we reach fixed packet length */ -void handle_reading_state(uint8_t *raw_packet, device_t *state, int *count) { - while (uart_is_readable(SERIAL_UART) && *count < PACKET_LENGTH) { - /* Read and store the incoming byte */ - raw_packet[(*count)++] = uart_getc(SERIAL_UART); - } - - /* Check if a complete packet is received */ - if (*count >= PACKET_LENGTH) { - state->receiver_state = PROCESSING_PACKET; - } -} - -/* Process that packet, restart counters and state machine to have it back to IDLE */ -void handle_processing_state(uart_packet_t *packet, device_t *state, int *count) { - process_packet(packet, state); - state->receiver_state = IDLE; - *count = 0; -} - -/* Very simple state machine to receive and process packets over serial */ -void receive_char(uart_packet_t *packet, device_t *state) { - uint8_t *raw_packet = (uint8_t *)packet; - static int count = 0; - - switch (state->receiver_state) { - case IDLE: - handle_idle_state(raw_packet, state); - break; - - case READING_PACKET: - handle_reading_state(raw_packet, state, &count); - break; - - case PROCESSING_PACKET: - handle_processing_state(packet, state, &count); - break; - } -} diff --git a/src/usb.c b/src/usb.c index 83b91a1..4dd567e 100644 --- a/src/usb.c +++ b/src/usb.c @@ -28,7 +28,7 @@ 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 request_len) { + uint16_t request_len) { return 0; } @@ -44,13 +44,35 @@ void tud_hid_set_report_cb(uint8_t instance, hid_report_type_t report_type, uint8_t const *buffer, uint16_t bufsize) { + + /* We received a report on the config report ID */ + if (instance == ITF_NUM_HID_VENDOR && report_id == REPORT_ID_VENDOR) { + /* Security - only if config mode is enabled are we allowed to do anything. While the report_id + isn't even advertised when not in config mode, security must always be explicit and never assume */ + if (!global_state.config_mode_active) + return; + + /* We insist on a fixed size packet. No overflows. */ + if (bufsize != RAW_PACKET_LENGTH) + return; + + uart_packet_t *packet = (uart_packet_t *) (buffer + START_LENGTH); + + /* Only a certain packet types are accepted */ + if (!validate_packet(packet)) + return; + + process_packet(packet, &global_state); + } + + /* Only other set report we care about is LED state change, and that's exactly 1 byte long */ if (report_id != REPORT_ID_KEYBOARD || bufsize != 1 || report_type != HID_REPORT_TYPE_OUTPUT) return; uint8_t leds = buffer[0]; /* If we are using caps lock LED to indicate the chosen output, that has priority */ - if (KBD_LED_AS_INDICATOR) { + if (global_state.config.kbd_led_as_indicator) { leds = leds & 0xFD; /* 1111 1101 (Clear Caps Lock bit) */ if (global_state.active_output) @@ -82,65 +104,71 @@ void tud_umount_cb(void) { * ================================================== */ void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) { - uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance); + uint8_t itf_protocol = tuh_hid_interface_protocol(dev_addr, instance); + + if (dev_addr >= MAX_DEVICES || instance > MAX_INTERFACES) + return; + + hid_interface_t *iface = &global_state.iface[dev_addr-1][instance]; switch (itf_protocol) { case HID_ITF_PROTOCOL_KEYBOARD: global_state.keyboard_connected = false; - memset(&global_state.kbd_dev, 0, sizeof(global_state.kbd_dev)); break; case HID_ITF_PROTOCOL_MOUSE: - global_state.mouse_connected = false; - - /* Clear this so reconnecting a mouse doesn't try to continue in HID REPORT protocol */ - memset(&global_state.mouse_dev, 0, sizeof(global_state.mouse_dev)); break; } + + /* Also clear the interface structure, otherwise plugging something else later + might be a fun (and confusing) experience */ + memset(iface, 0, sizeof(hid_interface_t)); } -void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const *desc_report, uint16_t desc_len) { - uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance); - tuh_hid_report_info_t report_info[MAX_REPORT_ITEMS] = {0}; - tuh_hid_report_info_t *info; - uint8_t num_parsed; +void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const *desc_report, uint16_t desc_len) { + uint8_t itf_protocol = tuh_hid_interface_protocol(dev_addr, instance); + + if (dev_addr >= MAX_DEVICES || instance > MAX_INTERFACES) + return; + + /* Get interface information */ + hid_interface_t *iface = &global_state.iface[dev_addr-1][instance]; + + iface->protocol = tuh_hid_get_protocol(dev_addr, instance); + + /* Safeguard against memory corruption in case the number of instances exceeds our maximum */ + if (instance >= MAX_INTERFACES) + return; + + /* Parse the report descriptor into our internal structure. */ + parse_report_descriptor(iface, desc_report, desc_len); switch (itf_protocol) { case HID_ITF_PROTOCOL_KEYBOARD: - if (ENFORCE_PORTS && BOARD_ROLE == PICO_B) + if (global_state.config.enforce_ports && BOARD_ROLE == OUTPUT_B) return; - + + if (global_state.config.force_kbd_boot_protocol) + tuh_hid_set_protocol(dev_addr, instance, HID_PROTOCOL_BOOT); + /* Keeping this is required for setting leds from device set_report callback */ global_state.kbd_dev_addr = dev_addr; global_state.kbd_instance = instance; - global_state.keyboard_connected = true; + global_state.keyboard_connected = true; break; case HID_ITF_PROTOCOL_MOUSE: - if (ENFORCE_PORTS && BOARD_ROLE == PICO_A) + if (global_state.config.enforce_ports && BOARD_ROLE == OUTPUT_A) return; - /* Switch to using protocol report instead of boot report, it's more complicated but + /* Switch to using report protocol instead of boot, it's more complicated but at least we get all the information we need (looking at you, mouse wheel) */ if (tuh_hid_get_protocol(dev_addr, instance) == HID_PROTOCOL_BOOT) { tuh_hid_set_protocol(dev_addr, instance, HID_PROTOCOL_REPORT); } - - global_state.mouse_dev.protocol = tuh_hid_get_protocol(dev_addr, instance); - parse_report_descriptor(&global_state.mouse_dev, MAX_REPORTS, desc_report, desc_len); - - global_state.mouse_connected = true; break; - - case HID_ITF_PROTOCOL_NONE: - num_parsed = tuh_hid_parse_report_descriptor(&report_info[0], MAX_REPORT_ITEMS, desc_report, desc_len); - - for (int report_num = 0; report_num < num_parsed; report_num++) { - info = &report_info[report_num]; - - if (info->usage == HID_USAGE_CONSUMER_CONTROL && info->usage_page == HID_USAGE_PAGE_CONSUMER) - global_state.kbd_dev.consumer_report_id = info->report_id; - } + + case HID_ITF_PROTOCOL_NONE: break; } /* Flash local led to indicate a device was connected */ @@ -149,7 +177,7 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const *desc_re /* Also signal the other board to flash LED, to enable easy verification if serial works */ send_value(ENABLE, FLASH_LED_MSG); - /* Kick off the report querying */ + /* Kick off the report querying */ tuh_hid_receive_report(dev_addr, instance); } @@ -157,26 +185,41 @@ void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const *desc_re void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const *report, uint16_t len) { uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance); - switch (itf_protocol) { - case HID_ITF_PROTOCOL_KEYBOARD: - process_keyboard_report((uint8_t *)report, len, &global_state); - break; + if (dev_addr >= MAX_DEVICES || instance > MAX_INTERFACES) + return; - case HID_ITF_PROTOCOL_MOUSE: - process_mouse_report((uint8_t *)report, len, &global_state); - break; + hid_interface_t *iface = &global_state.iface[dev_addr-1][instance]; - case HID_ITF_PROTOCOL_NONE: - process_consumer_report((uint8_t *)report, len, &global_state); - break; + /* Safeguard against memory corruption in case the number of instances exceeds our maximum */ + if (instance >= MAX_INTERFACES) + return; + + if (iface->uses_report_id) { + uint8_t report_id = report[0]; + + if (report_id < MAX_REPORTS) { + process_report_f receiver = iface->report_handler[report_id]; + + if (receiver != NULL) + receiver((uint8_t *)report, len, itf_protocol, iface); + } + } + else if (itf_protocol == HID_ITF_PROTOCOL_KEYBOARD) { + process_keyboard_report((uint8_t *)report, len, itf_protocol, iface); + } + else if (itf_protocol == HID_ITF_PROTOCOL_MOUSE) { + process_mouse_report((uint8_t *)report, len, itf_protocol, iface); } /* Continue requesting reports */ tuh_hid_receive_report(dev_addr, instance); } -/* Set protocol in a callback. If we were called, command succeeded. We're only - doing this for the mouse for now, so we can only be called about the mouse */ -void tuh_hid_set_protocol_complete_cb(uint8_t dev_addr, uint8_t idx, uint8_t protocol) { - global_state.mouse_dev.protocol = protocol; +/* Set protocol in a callback. This is tied to an interface, not a specific report ID */ +void tuh_hid_set_protocol_complete_cb(uint8_t dev_addr, uint8_t idx, uint8_t protocol) { + if (dev_addr >= MAX_DEVICES || idx > MAX_INTERFACES) + return; + + hid_interface_t *iface = &global_state.iface[dev_addr-1][idx]; + iface->protocol = protocol; } diff --git a/src/usb_descriptors.c b/src/usb_descriptors.c index 7baac05..70c20f2 100644 --- a/src/usb_descriptors.c +++ b/src/usb_descriptors.c @@ -30,29 +30,20 @@ //--------------------------------------------------------------------+ // Device Descriptors //--------------------------------------------------------------------+ -tusb_desc_device_t const desc_device = {.bLength = sizeof(tusb_desc_device_t), - .bDescriptorType = TUSB_DESC_DEVICE, - .bcdUSB = 0x0200, - .bDeviceClass = 0x00, - .bDeviceSubClass = 0x00, - .bDeviceProtocol = 0x00, - .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, // https://github.com/raspberrypi/usb-pid - .idVendor = 0x2E8A, - .idProduct = 0x107C, - .bcdDevice = 0x0100, - - .iManufacturer = 0x01, - .iProduct = 0x02, - .iSerialNumber = 0x03, - - .bNumConfigurations = 0x01}; +tusb_desc_device_t const desc_device = DEVICE_DESCRIPTOR(0x2e8a, 0x107c); + + // https://pid.codes/1209/C000/ +tusb_desc_device_t const desc_device_config = DEVICE_DESCRIPTOR(0x1209, 0xc000); // Invoked when received GET DEVICE DESCRIPTOR // Application return pointer to descriptor uint8_t const *tud_descriptor_device_cb(void) { - return (uint8_t const *)&desc_device; + if (global_state.config_mode_active) + return (uint8_t const *)&desc_device_config; + else + return (uint8_t const *)&desc_device; } //--------------------------------------------------------------------+ @@ -62,36 +53,42 @@ uint8_t const *tud_descriptor_device_cb(void) { // Relative mouse is used to overcome limitations of multiple desktops on MacOS and Windows uint8_t const desc_hid_report[] = {TUD_HID_REPORT_DESC_KEYBOARD(HID_REPORT_ID(REPORT_ID_KEYBOARD)), - TUD_HID_REPORT_DESC_ABSMOUSE(HID_REPORT_ID(REPORT_ID_MOUSE)), - TUD_HID_REPORT_DESC_CONSUMER_CTRL(HID_REPORT_ID(REPORT_ID_CONSUMER)) + TUD_HID_REPORT_DESC_ABS_MOUSE(HID_REPORT_ID(REPORT_ID_MOUSE)), + TUD_HID_REPORT_DESC_CONSUMER_CTRL(HID_REPORT_ID(REPORT_ID_CONSUMER)), + TUD_HID_REPORT_DESC_SYSTEM_CONTROL(HID_REPORT_ID(REPORT_ID_SYSTEM)) }; uint8_t const desc_hid_report_relmouse[] = {TUD_HID_REPORT_DESC_MOUSE(HID_REPORT_ID(REPORT_ID_RELMOUSE))}; +uint8_t const desc_hid_report_vendor[] = {TUD_HID_REPORT_DESC_VENDOR_CTRL(HID_REPORT_ID(REPORT_ID_VENDOR))}; + + // Invoked when received GET HID REPORT DESCRIPTOR // Application return pointer to descriptor // Descriptor contents must exist long enough for transfer to complete uint8_t const *tud_hid_descriptor_report_cb(uint8_t instance) { - if (instance == ITF_NUM_HID_REL_M) { - return desc_hid_report_relmouse; - } + if (global_state.config_mode_active) + if (instance == ITF_NUM_HID_VENDOR) + return desc_hid_report_vendor; - /* Default */ - return desc_hid_report; + switch(instance) { + case ITF_NUM_HID: + return desc_hid_report; + case ITF_NUM_HID_REL_M: + return desc_hid_report_relmouse; + default: + return desc_hid_report; + } } -bool tud_mouse_report(uint8_t mode, - uint8_t buttons, - int16_t x, - int16_t y, - int8_t wheel) { +bool tud_mouse_report(uint8_t mode, uint8_t buttons, int16_t x, int16_t y, int8_t wheel) { if (mode == ABSOLUTE) { mouse_report_t report = {.buttons = buttons, .x = x, .y = y, .wheel = wheel}; return tud_hid_n_report(ITF_NUM_HID, REPORT_ID_MOUSE, &report, sizeof(report)); - } - else { - hid_mouse_report_t report = {.buttons = buttons, .x = x - 16384, .y = y - 16384, .wheel = wheel, .pan = 0}; + } else { + hid_mouse_report_t report + = {.buttons = buttons, .x = x - SCREEN_MIDPOINT, .y = y - SCREEN_MIDPOINT, .wheel = wheel, .pan = 0}; return tud_hid_n_report(ITF_NUM_HID_REL_M, REPORT_ID_RELMOUSE, &report, sizeof(report)); } } @@ -107,9 +104,11 @@ char const *string_desc_arr[] = { "Hrvoje Cavrak", // 1: Manufacturer "DeskHop Switch", // 2: Product "0", // 3: Serials, should use chip ID - "MouseHelper", // 4: Relative mouse to work around OS issues + "DeskHop Helper", // 4: Mouse Helper Interface + "DeskHop Config", // 5: Vendor Interface + "DeskHop Disk", // 6: Disk Interface #ifdef DH_DEBUG - "Debug Interface", // 5: Debug Interface + "DeskHop Debug", // 7: Debug Interface #endif }; @@ -120,6 +119,8 @@ enum { STRID_PRODUCT, STRID_SERIAL, STRID_MOUSE, + STRID_VENDOR, + STRID_DISK, STRID_DEBUG, }; @@ -166,21 +167,31 @@ uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) { // Configuration Descriptor //--------------------------------------------------------------------+ -#define EPNUM_HID 0x81 -#define EPNUM_HID_REL_M 0x82 +#define EPNUM_HID 0x81 +#define EPNUM_HID_REL_M 0x82 +#define EPNUM_HID_VENDOR 0x83 + +#define EPNUM_MSC_OUT 0x04 +#define EPNUM_MSC_IN 0x84 #ifndef DH_DEBUG -enum { ITF_NUM_TOTAL = 2 }; -#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN + TUD_HID_DESC_LEN) +#define ITF_NUM_TOTAL 2 +#define ITF_NUM_TOTAL_CONFIG 3 +#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + 2 * TUD_HID_DESC_LEN) +#define CONFIG_TOTAL_LEN_CFG (TUD_CONFIG_DESC_LEN + 2 * TUD_HID_DESC_LEN + TUD_MSC_DESC_LEN) #else +#define ITF_NUM_CDC 3 +#define ITF_NUM_TOTAL 3 +#define ITF_NUM_TOTAL_CONFIG 4 -enum { ITF_NUM_CDC = 2, ITF_NUM_TOTAL = 3 }; -#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_HID_DESC_LEN + TUD_HID_DESC_LEN + TUD_CDC_DESC_LEN) -#define EPNUM_CDC_NOTIF 0x83 -#define EPNUM_CDC_OUT 0x04 -#define EPNUM_CDC_IN 0x84 +#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + 2 * TUD_HID_DESC_LEN + TUD_CDC_DESC_LEN) +#define CONFIG_TOTAL_LEN_CFG (TUD_CONFIG_DESC_LEN + 2 * TUD_HID_DESC_LEN + TUD_MSC_DESC_LEN + TUD_CDC_DESC_LEN) + +#define EPNUM_CDC_NOTIF 0x85 +#define EPNUM_CDC_OUT 0x06 +#define EPNUM_CDC_IN 0x86 #endif @@ -205,16 +216,51 @@ uint8_t const desc_configuration[] = { EPNUM_HID_REL_M, CFG_TUD_HID_EP_BUFSIZE, 1), - #ifdef DH_DEBUG // Interface number, string index, EP notification address and size, EP data address (out, in) and size. TUD_CDC_DESCRIPTOR( ITF_NUM_CDC, STRID_DEBUG, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, CFG_TUD_CDC_EP_BUFSIZE), #endif +}; +uint8_t const desc_configuration_config[] = { + // Config number, interface count, string index, total length, attribute, power in mA + TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL_CONFIG, 0, CONFIG_TOTAL_LEN_CFG, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 500), + + // Interface number, string index, protocol, report descriptor len, EP In address, size & polling interval + TUD_HID_DESCRIPTOR(ITF_NUM_HID, + STRID_PRODUCT, + HID_ITF_PROTOCOL_NONE, + sizeof(desc_hid_report), + EPNUM_HID, + CFG_TUD_HID_EP_BUFSIZE, + 1), + + TUD_HID_DESCRIPTOR(ITF_NUM_HID_VENDOR, + STRID_VENDOR, + HID_ITF_PROTOCOL_NONE, + sizeof(desc_hid_report_vendor), + EPNUM_HID_VENDOR, + CFG_TUD_HID_EP_BUFSIZE, + 1), + + TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, + STRID_DISK, + EPNUM_MSC_OUT, + EPNUM_MSC_IN, + 64), +#ifdef DH_DEBUG + // Interface number, string index, EP notification address and size, EP data address (out, in) and size. + TUD_CDC_DESCRIPTOR( + ITF_NUM_CDC, STRID_DEBUG, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, CFG_TUD_CDC_EP_BUFSIZE), +#endif }; uint8_t const *tud_descriptor_configuration_cb(uint8_t index) { (void)index; // for multiple configurations - return desc_configuration; + + if (global_state.config_mode_active) + return desc_configuration_config; + else + return desc_configuration; } diff --git a/src/utils.c b/src/utils.c index 45df43c..51e1782 100644 --- a/src/utils.c +++ b/src/utils.c @@ -36,20 +36,23 @@ bool verify_checksum(const uart_packet_t *packet) { return checksum == packet->checksum; } -/**================================================== * - * ============== Watchdog Functions ============== * - * ================================================== */ +uint32_t crc32_iter(uint32_t crc, const uint8_t byte) { + return crc32_lookup_table[(byte ^ crc) & 0xff] ^ (crc >> 8); +} -void kick_watchdog(device_t *state) { - /* Read the timer AFTER duplicating the core1 timestamp, - so it doesn't get updated in the meantime. */ +/* TODO - use DMA sniffer's built-in CRC32 */ +uint32_t calc_crc32(const uint8_t *s, size_t n) { + uint32_t crc = 0xffffffff; - uint64_t core1_last_loop_pass = state->core1_last_loop_pass; - uint64_t current_time = time_us_64(); + for(size_t i=0; i < n; i++) { + crc = crc32_iter(crc, s[i]); + } - /* If core1 stops updating the timestamp, we'll stop kicking the watchog and reboot */ - if (current_time - core1_last_loop_pass < CORE1_HANG_TIMEOUT_US) - watchdog_update(); + return ~crc; +} + +uint32_t calculate_firmware_crc32(void) { + return calc_crc32(ADDR_FW_RUNNING, STAGING_IMAGE_SIZE - FLASH_SECTOR_SIZE); } /* ================================================== * @@ -58,19 +61,31 @@ void kick_watchdog(device_t *state) { void wipe_config(void) { uint32_t ints = save_and_disable_interrupts(); - flash_range_erase(PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE, FLASH_SECTOR_SIZE); + flash_range_erase((uint32_t)ADDR_CONFIG - XIP_BASE, FLASH_SECTOR_SIZE); restore_interrupts(ints); } +void write_flash_page(uint32_t target_addr, uint8_t *buffer) { + /* Start of sector == first 256-byte page in a 4096 byte block */ + bool is_sector_start = (target_addr & 0xf00) == 0; + + uint32_t ints = save_and_disable_interrupts(); + if (is_sector_start) + flash_range_erase(target_addr, FLASH_SECTOR_SIZE); + + flash_range_program(target_addr, buffer, FLASH_PAGE_SIZE); + restore_interrupts(ints); +} + void load_config(device_t *state) { - const config_t *config = ADDR_CONFIG_BASE_ADDR; + const config_t *config = ADDR_CONFIG; config_t *running_config = &state->config; /* Load the flash config first, including the checksum */ - memcpy(running_config, config, sizeof(config_t)); - + memcpy(running_config, config, sizeof(config_t)); + /* Calculate and update checksum, size without checksum */ - uint8_t checksum = calc_checksum((uint8_t *)running_config, sizeof(config_t) - sizeof(uint32_t)); + uint8_t checksum = calc_crc32((uint8_t *)running_config, sizeof(config_t) - sizeof(uint32_t)); /* We expect a certain byte to start the config header */ bool magic_header_fail = (running_config->magic_header != 0xB00B1E5); @@ -87,75 +102,118 @@ void load_config(device_t *state) { } void save_config(device_t *state) { - uint8_t buf[FLASH_PAGE_SIZE]; uint8_t *raw_config = (uint8_t *)&state->config; /* Calculate and update checksum, size without checksum */ - uint8_t checksum = calc_checksum(raw_config, sizeof(config_t) - sizeof(uint32_t)); + uint8_t checksum = calc_crc32(raw_config, sizeof(config_t) - sizeof(uint32_t)); state->config.checksum = checksum; - /* Copy the config to buffer and wipe the old one */ - memcpy(buf, raw_config, sizeof(config_t)); - wipe_config(); + /* Copy the config to buffer and pad the rest with zeros */ + memcpy(state->page_buffer, raw_config, sizeof(config_t)); + memset(state->page_buffer + sizeof(config_t), 0, FLASH_PAGE_SIZE - sizeof(config_t)); - /* Disable interrupts, then write the flash page and re-enable */ - uint32_t ints = save_and_disable_interrupts(); - flash_range_program(PICO_FLASH_SIZE_BYTES - FLASH_SECTOR_SIZE, buf, FLASH_PAGE_SIZE); - restore_interrupts(ints); + /* Write the new config to flash */ + write_flash_page((uint32_t)ADDR_CONFIG - XIP_BASE, state->page_buffer); } -/* Have something fun and entertaining when idle. */ -void screensaver_task(device_t *state) { - const int mouse_move_delay = 5000; - screensaver_t *screensaver = &state->config.output[BOARD_ROLE].screensaver; - - static mouse_report_t report = {.x = 0, .y = 0}; - static int last_pointer_move = 0; - - uint64_t current_time = time_us_64(); - uint64_t inactivity_period = current_time - state->last_activity[BOARD_ROLE]; - - /* "Randomly" chosen initial values */ - static int dx = 20; - static int dy = 25; - - /* If we're not enabled, nothing to do here. */ - if (!screensaver->enabled) - return; - - /* System is still not idle for long enough to activate or we've been running for too long */ - if (inactivity_period < screensaver->idle_time_us) - return; - - /* We exceeded the maximum permitted screensaver runtime */ - if (screensaver->max_time_us - && inactivity_period > (screensaver->max_time_us + screensaver->idle_time_us)) - return; - - /* If we're not the selected output and that is required, nothing to do here. */ - if (screensaver->only_if_inactive && CURRENT_BOARD_IS_ACTIVE_OUTPUT) - return; - - /* We're active! Now check if it's time to move the cursor yet. */ - if ((time_us_32()) - last_pointer_move < mouse_move_delay) - return; - - /* Check if we are bouncing off the walls and reverse direction in that case. */ - if (report.x + dx < MIN_SCREEN_COORD || report.x + dx > MAX_SCREEN_COORD) - dx = -dx; - - if (report.y + dy < MIN_SCREEN_COORD || report.y + dy > MAX_SCREEN_COORD) - dy = -dy; - - report.x += dx; - report.y += dy; - - /* Move mouse pointer */ - queue_mouse_report(&report, state); - - /* Update timer of the last pointer move */ - last_pointer_move = time_us_32(); +void reset_config_timer(device_t *state) { + /* Once this is reached, we leave the config mode */ + state->config_mode_timer = time_us_64() + CONFIG_MODE_TIMEOUT; } + +void _configure_flash_cs(enum gpio_override gpo, uint pin_index) { + hw_write_masked(&ioqspi_hw->io[pin_index].ctrl, + gpo << IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_LSB, + IO_QSPI_GPIO_QSPI_SS_CTRL_OEOVER_BITS); +} + +bool is_bootsel_pressed(void) { + const uint CS_PIN_INDEX = 1; + uint32_t flags = save_and_disable_interrupts(); + + /* Set chip select to high impedance */ + _configure_flash_cs(GPIO_OVERRIDE_LOW, CS_PIN_INDEX); + sleep_us(20); + + /* Button pressed pulls pin DOWN, so invert */ + bool button_pressed = !(sio_hw->gpio_hi_in & (1u << CS_PIN_INDEX)); + + /* Restore chip select state */ + _configure_flash_cs(GPIO_OVERRIDE_NORMAL, CS_PIN_INDEX); + restore_interrupts(flags); + + return button_pressed; +} + +void request_byte(device_t *state, uint32_t address) { + uart_packet_t packet = { + .data32[0] = address, + .type = REQUEST_BYTE_MSG, + }; + state->fw.byte_done = false; + + queue_try_add(&global_state.uart_tx_queue, &packet); +} + +void reboot(void) { + *((volatile uint32_t*)(PPB_BASE + 0x0ED0C)) = 0x5FA0004; +} + +bool is_start_of_packet(device_t *state) { + return (uart_rxbuf[state->dma_ptr] == START1 && uart_rxbuf[NEXT_RING_IDX(state->dma_ptr)] == START2); +} + +uint32_t get_ptr_delta(uint32_t current_pointer, device_t *state) { + uint32_t delta; + + if (current_pointer >= state->dma_ptr) + delta = current_pointer - state->dma_ptr; + else + delta = DMA_RX_BUFFER_SIZE - state->dma_ptr + current_pointer; + + /* Clamp to 12 bits since it can never be bigger */ + delta = delta & 0x3FF; + + return delta; +} + +void fetch_packet(device_t *state) { + uint8_t *dst = (uint8_t *)&state->in_packet; + + for (int i = 0; i < RAW_PACKET_LENGTH; i++) { + /* Skip the header preamble */ + if (i >= START_LENGTH) + dst[i - START_LENGTH] = uart_rxbuf[state->dma_ptr]; + + state->dma_ptr = NEXT_RING_IDX(state->dma_ptr); + } +} + +/* Validating any input is mandatory. Only packets of these type are allowed + to be sent to the device over configuration endpoint. */ +bool validate_packet(uart_packet_t *packet) { + const enum packet_type_e ALLOWED_PACKETS[] = { + FLASH_LED_MSG, + GET_VAL_MSG, + SET_VAL_MSG, + WIPE_CONFIG_MSG, + SAVE_CONFIG_MSG, + REBOOT_MSG, + PROXY_PACKET_MSG, + }; + uint8_t packet_type = packet->type; + + /* Proxied packets are encapsulated in the data field, but same rules apply */ + if (packet->type == PROXY_PACKET_MSG) + packet_type = packet->data[0]; + + for (int i = 0; i < ARRAY_SIZE(ALLOWED_PACKETS); i++) { + if (ALLOWED_PACKETS[i] == packet_type) + return true; + } + return false; +} + /* ================================================== * * Debug functions @@ -175,5 +233,10 @@ int dh_debug_printf(const char *format, ...) { va_end(args); return string_len; } +#else + +int dh_debug_printf(const char *format, ...) { + return 0; +} #endif diff --git a/tools/crc32.py b/tools/crc32.py new file mode 100644 index 0000000..2929f9d --- /dev/null +++ b/tools/crc32.py @@ -0,0 +1,20 @@ +import sys +import struct +import binascii + +FLASH_SECTOR_SIZE = 4096 +MAGIC_VALUE = 0xf00d + +elf_filename = sys.argv[1] +output_filename = sys.argv[2] +version = sys.argv[3] + +with open(elf_filename, 'r+b') as f: + data = f.read() + + data = data[:-FLASH_SECTOR_SIZE] + crc32_value = binascii.crc32(data) & 0xFFFFFFFF + +with open(output_filename, 'wb') as f: + f.write(struct.pack(' + + + + + + + + DeskHop Config + + + + +
+ +
+ + + +
+ +
+

DeskHop Config

+
+
+ +
+ + +
+
+ + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +

Output A

+ + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+
+ + + + + + + + + +
+ +
+ + + + + + + + + + + + + + +
+
+ + + + + + + + + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+
+ + + + + + + +

Output B

+ + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+
+ + + + + + + + + +
+ +
+ + + + + + + + + + + + + + +
+
+ + + + + + + + + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ +
+
+
+
+
+ +
+ + +

Common Config

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + +
+ + + + + + + + + + + + + + +
+
+ + + + + + + + + +
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + +
+ + + + + + + + + + + + + + +
+ + + + + + +
+ + + + + + +
+ +
+

Device Status

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+
+
+
+ + + + + \ No newline at end of file diff --git a/webconfig/config.htm b/webconfig/config.htm new file mode 100644 index 0000000..3b762b1 --- /dev/null +++ b/webconfig/config.htm @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/webconfig/form.py b/webconfig/form.py new file mode 100755 index 0000000..dc655ac --- /dev/null +++ b/webconfig/form.py @@ -0,0 +1,82 @@ +#!/usr/bin/python3 + +from dataclasses import dataclass, field + +@dataclass +class FormField: + offset: int + name: str + default: int | None = None + values: dict[int, str] = field(default_factory=dict) + data_type: str = "int32" + elem: str | None = None + +SHORTCUTS = { + 0x73: "None", + 0x2A: "Backspace", + 0x39: "Caps Lock", + 0x2B: "Tab", + 0x46: "Print Screen", + 0x47: "Scroll Lock", + 0x53: "Num Lock", + } + +STATUS_ = [ + FormField(78, "Running FW version", None, {}, "uint16", elem="hex_info"), + FormField(79, "Running FW checksum", None, {}, "uint32", elem="hex_info"), +] + +CONFIG_ = [ + FormField(1001, "Mouse", elem="label"), + FormField(71, "Force Mouse Boot Mode", None, {}, "uint8", "checkbox"), + FormField(75, "Enable Acceleration", None, {}, "uint8", "checkbox"), + FormField(77, "Jump Treshold", 0, {"min": 0, "max": 1024}, "uint16", "range"), + + FormField(1002, "Keyboard", elem="label"), + FormField(72, "Force KBD Boot Protocol", None, {}, "uint8", "checkbox"), + FormField(73, "KBD LED as Indicator", None, {}, "uint8", "checkbox"), + + FormField(76, "Enforce Ports", None, {}, "uint8", "checkbox"), +] + +OUTPUT_ = [ + FormField(1, "Screen Count", 1, {1: "1", 2: "2", 3: "3"}, "uint32"), + FormField(2, "Speed X", 16, {"min": 1, "max": 100}, "int32", "range"), + FormField(3, "Speed Y", 16, {"min": 1, "max": 100}, "int32", "range"), + FormField(4, "Border Top", None, {}, "int32"), + FormField(5, "Border Bottom", None, {}, "int32"), + FormField(6, "Operating System", 1, {1: "Linux", 2: "MacOS", 3: "Windows", 4: "Android", 255: "Other"}, "uint8"), + FormField(7, "Screen Position", 1, {1: "Left", 2: "Right"}, "uint8"), + FormField(8, "Cursor Park Position", 0, {0: "Top", 1: "Bottom", 3: "Previous"}, "uint8"), + FormField(1003, "Screensaver", elem="label"), + FormField(9, "Mode", 0, {0: "Disabled", 1: "Pong", 2: "Jitter"}, "uint8"), + FormField(10, "Only If Inactive", None, {}, "uint8", "checkbox"), + FormField(11, "Idle Time (μs)", None, {}, "uint64"), + FormField(12, "Max Time (μs)", None, {}, "uint64"), +] + +def generate_output(base, data): + output = [ + { + "name": field.name, + "key": base + field.offset, + "default": field.default, + "values": field.values, + "type": field.data_type, + "elem": field.elem, + } + for field in data + ] + return output + +def output_A(base=10): + return generate_output(base, data=OUTPUT_) + +def output_B(base=40): + return generate_output(base, data=OUTPUT_) + +def output_status(): + return generate_output(0, data=STATUS_) + +def output_config(): + return generate_output(0, data=CONFIG_) diff --git a/webconfig/render.py b/webconfig/render.py new file mode 100755 index 0000000..aea2cb5 --- /dev/null +++ b/webconfig/render.py @@ -0,0 +1,61 @@ +#!/usr/bin/python3 + +# Takes a HTML file, outputs a minified and compressed version that self-decompresses when loaded. +# This way, the device config page can be fitted in a small 64 kB "flash" partition and distributed +# with the main binary. + +from jinja2 import Environment, FileSystemLoader +from form import * +import base64 +import zlib + +# Input and output +TEMPLATE_PATH = "templates/" +INPUT_FILENAME = "main.html" +PACKER_FILENAME = "packer.j2" +OUTPUT_FILENAME = "config.htm" +OUTPUT_UNPACKED = "config-unpacked.htm" + +def render(filename, *args, **kwargs): + env = Environment(loader=FileSystemLoader(TEMPLATE_PATH)) + template = env.get_template(filename) + return template.render(*args, **kwargs) + + +def write_file(payload, filename=OUTPUT_FILENAME): + with open(filename, 'w', encoding='utf-8') as file: + file.write(payload) + + +def encode_file(payload): + # Compress using raw DEFLATE + compressed_data = zlib.compress(payload.encode('utf-8'))[2:-4] + + # Encode to base64 + base64_compressed_data = base64.b64encode(compressed_data).decode('utf-8') + + return base64_compressed_data + + +if __name__ == "__main__": + # Read main template contents + webpage = render( + INPUT_FILENAME, + screen_A=output_A(), + screen_B=output_B(), + status=output_status(), + config=output_config(), + ) + + # Compress file and encode to base64 + encoded_data = {'payload': encode_file(webpage)} + + # Tiny Inflate JS decoder (https://github.com/foliojs/tiny-inflate) + # Decompress the data and replace existing HTML with the decoded version + self_extracting_webpage = render(PACKER_FILENAME, encoded_data) + + # Write data to output filename + write_file(self_extracting_webpage) + + # Write unpacked webpage + write_file(webpage, OUTPUT_UNPACKED) \ No newline at end of file diff --git a/webconfig/requirements.txt b/webconfig/requirements.txt new file mode 100644 index 0000000..f5617e6 --- /dev/null +++ b/webconfig/requirements.txt @@ -0,0 +1,2 @@ +Jinja2==3.1.4 +MarkupSafe==2.1.5 diff --git a/webconfig/templates/form.html b/webconfig/templates/form.html new file mode 100644 index 0000000..bf8cd31 --- /dev/null +++ b/webconfig/templates/form.html @@ -0,0 +1,58 @@ +{# templates/form.html #} + +{% set key = item.key %} + +{% macro input(item, type='text', class='api', name='name') %} + {{ item.name }}{{ add }} +{% endmacro %} + +{% block form_render %} + {% if item.get("elem") == "hex_info" %} + {{ label(item, add=':') }} + {{ input(item, class='content api') }} data-hex readonly /> + + {% elif item.get("elem") == "label" %} + {{ label(item, class='') }} + + {% elif item.get("elem") == "range" %} +
+
+ {{ label(item, add='=') }} + + {{ input(item, class='input-inline', type='number', name='aInput') }} + readonly oninput="this.form.aRange{{ key }}.value=this.value" /> + + {{ input(item, class='range api', type='range', name='aRange') }} + min="{{ item.values.min }}" max="{{ item.values.max }}" oninput="this.form.aInput{{key}}.value=this.value" /> +
+ +
+ + {% elif item.get("elem") == "checkbox" %} +
+ {{ label(item) }} + {{ input(item, type="checkbox") }} /> + +
+ + {% elif item["values"] %} + {{ label(item, class='') }} +
+ + {% else %} + {{ label(item, class='') }} + {{ input(item) }} /> + + {% endif %} +{% endblock %} \ No newline at end of file diff --git a/webconfig/templates/main.html b/webconfig/templates/main.html new file mode 100644 index 0000000..dda7e9b --- /dev/null +++ b/webconfig/templates/main.html @@ -0,0 +1,142 @@ +{# templates/main.j2 #} + + + + + + + + + + DeskHop Config + + + + +
+ +
+ + + +
+ +
+

DeskHop Config

+
+
+ +
+ + +
+
+ + + + + + + + + + + + + + + + +
+ +
+ + + + + + + +

Output A

+ + {% for item in screen_A %} + {% include "form.html" with context %} + {% endfor %} + +
+
+ + + + + + + +

Output B

+ + {% for item in screen_B %} + {% include "form.html" with context %} + {% endfor %} + +
+ +
+ +
+
+
+
+
+ +
+ + +

Common Config

+ + {% for item in config %} + {% include "form.html" with context %} + {% endfor %} + + +
+ +
+

Device Status

+ + {% for item in status %} + {% include "form.html" with context %} + {% endfor %} + +
+ +
+
+
+
+ + + + + diff --git a/webconfig/templates/packer.j2 b/webconfig/templates/packer.j2 new file mode 100644 index 0000000..d5651bb --- /dev/null +++ b/webconfig/templates/packer.j2 @@ -0,0 +1 @@ +{% raw %}{% endraw %} diff --git a/webconfig/templates/script.js b/webconfig/templates/script.js new file mode 100644 index 0000000..48da171 --- /dev/null +++ b/webconfig/templates/script.js @@ -0,0 +1,218 @@ +const mgmtReportId = 6; +var device; + +const packetType = { + keyboardReportMsg: 1, mouseReportMsg: 2, outputSelectMsg: 3, firmwareUpgradeMsg: 4, switchLockMsg: 7, + syncBordersMsg: 8, flashLedMsg: 9, wipeConfigMsg: 10, readConfigMsg: 16, writeConfigMsg: 17, saveConfigMsg: 18, + rebootMsg: 19, getValMsg: 20, setValMsg: 21, proxyPacketMsg: 23 +}; + +function calcChecksum(report) { + let checksum = 0; + for (let i = 3; i < 11; i++) + checksum ^= report[i]; + + return checksum; +} + +async function sendReport(type, payload = [], sendBoth = false) { + if (!device || !device.opened) + return; + + /* First send this one, if the first one gets e.g. rebooted */ + if (sendBoth) { + var reportProxy = makeReport(type, payload, true); + await device.sendReport(mgmtReportId, reportProxy); + } + + var report = makeReport(type, payload, false); + await device.sendReport(mgmtReportId, report); +} + +function makeReport(type, payload, proxy=false) { + var dataOffset = proxy ? 4 : 3; + report = new Uint8Array([0xaa, 0x55, type, ...new Array(9).fill(0)]); + + if (proxy) + report = new Uint8Array([0xaa, 0x55, packetType.proxyPacketMsg, type, ...new Array(7).fill(0), type]); + + if (payload) { + report.set([...payload], dataOffset); + report[report.length - 1] = calcChecksum(report); + } + return report; +} + +function packValue(element, key, dataType, buffer) { + const dataOffset = 1; + var buffer = new ArrayBuffer(8); + var view = new DataView(buffer); + + const methods = { + "uint32": view.setUint32, + "uint64": view.setUint32, /* Yes, I know. :-| */ + "int32": view.setInt32, + "uint16": view.setUint16, + "uint8": view.setUint8, + "int16": view.setInt16, + "int8": view.setInt8 + }; + + if (dataType in methods) { + const method = methods[dataType]; + if (element.type === 'checkbox') + view.setUint8(dataOffset, element.checked ? 1 : 0, true); + else + method.call(view, dataOffset, element.value, true); + } + + view.setUint8(0, key); + return new Uint8Array(buffer); +} + +window.addEventListener('load', function () { + if (!("hid" in navigator)) { + document.getElementById('warning').style.display = 'block'; + } + + this.document.getElementById('menu-buttons').addEventListener('click', function (event) { + window[event.target.dataset.handler](); + }) +}); + +document.getElementById('submitButton').addEventListener('click', async () => { await saveHandler(); }); + +async function connectHandler() { + if (device && device.opened) + return; + + var devices = await navigator.hid.requestDevice({ + filters: [{ vendorId: 0x1209, productId: 0xc000, usagePage: 0xff00, usage: 0x10 }] + }); + + device = devices[0]; + device.open().then(async () => { + document.querySelectorAll('.online').forEach(element => { element.style.opacity = 1.0; }); + await readHandler(); + }); +} + +async function blinkHandler() { + await sendReport(packetType.flashLedMsg, []); +} + +async function blinkBothHandler() { + await sendReport(packetType.flashLedMsg, [], true); +} + +function getValue(element) { + if (element.type === 'checkbox') + return element.checked ? 1 : 0; + else + return element.value; +} + +function setValue(element, value) { + element.setAttribute('fetched-value', value); + + if (element.type === 'checkbox') + element.checked = value; + else + element.value = value; + element.dispatchEvent(new Event('input', { bubbles: true })); +} + + +function updateElement(element, event, dataType) { + var dataOffset = 3; + + const methods = { + "uint32": event.data.getUint32, + "uint64": event.data.getUint32, /* Yes, I know. :-| */ + "int32": event.data.getInt32, + "uint16": event.data.getUint16, + "uint8": event.data.getUint8, + "int16": event.data.getInt16, + "int8": event.data.getInt8 + }; + + if (dataType in methods) { + var value = methods[dataType].call(event.data, dataOffset, true); + setValue(element, value); + + if (element.hasAttribute('data-hex')) + setValue(element, parseInt(value).toString(16)); + } +} + +async function readHandler() { + if (!device || !device.opened) + await connectHandler(); + + const elements = document.querySelectorAll('.api'); + + for (const element of elements) { + var key = element.getAttribute('data-key'); + var dataType = element.getAttribute('data-type'); + + await sendReport(packetType.getValMsg, [key]); + + let incomingReport = await new Promise((resolve, reject) => { + const handleInputReport = (event) => { + updateElement(element, event, dataType); + + device.removeEventListener('inputreport', handleInputReport); + resolve(); + } + device.addEventListener('inputreport', handleInputReport); + }); + } +} + +async function rebootHandler() { + await sendReport(packetType.rebootMsg); +} + +async function enterBootloaderHandler() { + await sendReport(packetType.firmwareUpgradeMsg, true, true); +} + +async function valueChangedHandler(element) { + var key = element.getAttribute('data-key'); + var dataType = element.getAttribute('data-type'); + + var origValue = element.getAttribute('fetched-value'); + var newValue = getValue(element); + + if (origValue != newValue) { + uintBuffer = packValue(element, key, dataType); + + /* Send to both devices */ + await sendReport(packetType.setValMsg, uintBuffer, true); + + /* Set this as the current value */ + element.setAttribute('fetched-value', newValue); + } +} + +async function saveHandler() { + const elements = document.querySelectorAll('.api'); + + if (!device || !device.opened) + return; + + for (const element of elements) { + var origValue = element.getAttribute('fetched-value') + + if (element.hasAttribute('readonly')) + continue; + + if (origValue != getValue(element)) + await valueChangedHandler(element); + } + await sendReport(packetType.saveConfigMsg, [], true); +} + +async function wipeConfigHandler() { + await sendReport(packetType.wipeConfigMsg, [], true); +} \ No newline at end of file diff --git a/webconfig/templates/style.css b/webconfig/templates/style.css new file mode 100644 index 0000000..913fe21 --- /dev/null +++ b/webconfig/templates/style.css @@ -0,0 +1,665 @@ +/*! + * Milligram v1.4.1 + * https://milligram.io + * + * Copyright (c) 2020 CJ Patoilo + * Licensed under the MIT license + */ + +*, +*:after, +*:before { + box-sizing: inherit; +} + +:root { + --highlight-color: #384955; + --font-color: #384955; + --highlight-color2: #5e9f41; +} + +html { + box-sizing: border-box; + font-size: 62.5%; +} + +body { + color: var(--font-color); + font-family: 'Roboto', 'Helvetica Neue', 'Helvetica', 'Arial', sans-serif; + font-size: 1.6em; + font-weight: 300; + letter-spacing: .01em; + line-height: 1.6; +} + +blockquote { + border-left: 0.3rem solid #384955; + margin-left: 0; + margin-right: 0; + padding: 1rem 1.5rem; +} + +blockquote *:last-child { + margin-bottom: 0; +} + +.button, +button, +input[type='button'], +input[type='reset'], +input[type='submit'] { + background-color: var(--highlight-color); + border: 0.1rem solid var(--highlight-color); + border-radius: .4rem; + color: #fff; + cursor: pointer; + display: inline-block; + font-size: 1.1rem; + font-weight: 700; + height: 3.8rem; + letter-spacing: .1rem; + line-height: 3.8rem; + padding: 0.0rem 2.0rem; + text-align: center; + text-decoration: none; + text-transform: uppercase; + white-space: nowrap; +} + +.button:focus, .button:hover, +button:focus, +button:hover, +input[type='button']:focus, +input[type='button']:hover, +input[type='reset']:focus, +input[type='reset']:hover, +input[type='submit']:focus, +input[type='submit']:hover { + background-color: var(--highlight-color2); + border-color: var(--highlight-color2); + color: #fff; + outline: 0; +} + +.button[disabled], +button[disabled], +input[type='button'][disabled], +input[type='reset'][disabled], +input[type='submit'][disabled] { + cursor: default; + opacity: .5; +} + +.button[disabled]:focus, .button[disabled]:hover, +button[disabled]:focus, +button[disabled]:hover, +input[type='button'][disabled]:focus, +input[type='button'][disabled]:hover, +input[type='reset'][disabled]:focus, +input[type='reset'][disabled]:hover, +input[type='submit'][disabled]:focus, +input[type='submit'][disabled]:hover { + background-color: var(--highlight-color); + border-color: var(--highlight-color); +} + +.button.button-outline, +button.button-outline, +input[type='button'].button-outline, +input[type='reset'].button-outline, +input[type='submit'].button-outline { + background-color: transparent; + color: var(--highlight-color); +} + +.button.button-outline:focus, .button.button-outline:hover, +button.button-outline:focus, +button.button-outline:hover, +input[type='button'].button-outline:focus, +input[type='button'].button-outline:hover, +input[type='reset'].button-outline:focus, +input[type='reset'].button-outline:hover, +input[type='submit'].button-outline:focus, +input[type='submit'].button-outline:hover { + background-color: transparent; + border-color: var(--highlight-color2); + color: var(--highlight-color2); +} + +.button.button-outline[disabled]:focus, .button.button-outline[disabled]:hover, +button.button-outline[disabled]:focus, +button.button-outline[disabled]:hover, +input[type='button'].button-outline[disabled]:focus, +input[type='button'].button-outline[disabled]:hover, +input[type='reset'].button-outline[disabled]:focus, +input[type='reset'].button-outline[disabled]:hover, +input[type='submit'].button-outline[disabled]:focus, +input[type='submit'].button-outline[disabled]:hover { + border-color: inherit; + color: var(--highlight-color); +} + +.button.button-clear, +button.button-clear, +input[type='button'].button-clear, +input[type='reset'].button-clear, +input[type='submit'].button-clear { + background-color: transparent; + border-color: transparent; + color: var(--highlight-color); +} + +.button.button-clear:focus, .button.button-clear:hover, +button.button-clear:focus, +button.button-clear:hover, +input[type='button'].button-clear:focus, +input[type='button'].button-clear:hover, +input[type='reset'].button-clear:focus, +input[type='reset'].button-clear:hover, +input[type='submit'].button-clear:focus, +input[type='submit'].button-clear:hover { + background-color: transparent; + border-color: transparent; + color: var(--highlight-color2); +} + +.button.button-clear[disabled]:focus, .button.button-clear[disabled]:hover, +button.button-clear[disabled]:focus, +button.button-clear[disabled]:hover, +input[type='button'].button-clear[disabled]:focus, +input[type='button'].button-clear[disabled]:hover, +input[type='reset'].button-clear[disabled]:focus, +input[type='reset'].button-clear[disabled]:hover, +input[type='submit'].button-clear[disabled]:focus, +input[type='submit'].button-clear[disabled]:hover { + color: var(--highlight-color); +} + +.button.button-shifted { + margin-left: 10%; +} + +code { + background: #f4f5f6; + border-radius: .4rem; + font-size: 86%; + margin: 0 .2rem; + padding: .2rem .5rem; + white-space: nowrap; +} + +pre { + background: #f4f5f6; + border-left: 0.3rem solid var(--highlight-color); + overflow-y: hidden; +} + +pre > code { + border-radius: 0; + display: block; + padding: 1rem 1.5rem; + white-space: pre; +} + +hr { + border: 0; + border-top: 0.1rem dotted lightblue; + margin: 1.0rem 3.0rem; +} + +input[type='color'], +input[type='date'], +input[type='datetime'], +input[type='datetime-local'], +input[type='email'], +input[type='month'], +input[type='password'], +input[type='search'], +input[type='number'], +input[type='tel'], +input[type='text'], +input[type='url'], +input[type='week'], +input:not([type]), +textarea, +select { + -webkit-appearance: none; + background-color: transparent; + border: 0.1rem solid #d1d1d1; + border-radius: .4rem; + box-shadow: none; + box-sizing: inherit; + height: 3.8rem; + padding: .6rem 1.0rem .7rem; + width: 100%; +} + +input[type='color']:focus, +input[type='date']:focus, +input[type='datetime']:focus, +input[type='datetime-local']:focus, +input[type='email']:focus, +input[type='month']:focus, +input[type='number']:focus, +input[type='password']:focus, +input[type='search']:focus, +input[type='tel']:focus, +input[type='text']:focus, +input[type='url']:focus, +input[type='week']:focus, +input:not([type]):focus, +textarea:focus, +select:focus { + border-color: var(--highlight-color); + outline: 0; +} + +select { + background: url('data:image/svg+xml;utf8,') center right no-repeat; + padding-right: 3.0rem; +} + +select:focus { + background-image: url('data:image/svg+xml;utf8,'); +} + +select[multiple] { + background: none; + height: auto; +} + +textarea { + min-height: 6.5rem; +} + +label, +legend { + display: block; + font-size: 1.6rem; + font-weight: 700; + margin-bottom: .5rem; +} + +fieldset { + border-width: 0; + padding: 0; +} + +input[type='checkbox'], +input[type='radio'] { + display: inline; +} + +.label-inline { + display: inline-block; + font-weight: normal; + margin-left: .5rem; +} + +.label-inline-bold { + display: inline-block; + font-size: 1.6rem; + font-weight: 700; +} + +input[type='range'].input-inline, +input[type='number'].input-inline { + display: inline-block; + font-weight: normal; + border: none; + width: 20%; +} + +.range { + width: 100%; +} + +.container { + margin: 0 auto; + max-width: 112.0rem; + padding: 0 2.0rem; + position: relative; + width: 100%; +} + +.row { + display: flex; + flex-direction: column; + padding: 0; + width: 100%; +} + +.row.row-no-padding { + padding: 0; +} + +.row.row-no-padding > .column { + padding: 0; +} + +.row.row-wrap { + flex-wrap: wrap; +} + +.row.row-top { + align-items: flex-start; +} + +.row.row-bottom { + align-items: flex-end; +} + +.row.row-center { + align-items: center; +} + +.row.row-stretch { + align-items: stretch; +} + +.row.row-baseline { + align-items: baseline; +} + +.row .column { + display: block; + flex: 1 1 auto; + margin-left: 0; + max-width: 100%; + width: 100%; +} + +.row .column.column-offset-10 { + margin-left: 10%; +} + +.row .column.column-offset-20 { + margin-left: 20%; +} + +.row .column.column-offset-25 { + margin-left: 25%; +} + +.row .column.column-offset-33, .row .column.column-offset-34 { + margin-left: 33.3333%; +} + +.row .column.column-offset-40 { + margin-left: 40%; +} + +.row .column.column-offset-50 { + margin-left: 50%; +} + +.row .column.column-offset-60 { + margin-left: 60%; +} + +.row .column.column-offset-66, .row .column.column-offset-67 { + margin-left: 66.6666%; +} + +.row .column.column-offset-75 { + margin-left: 75%; +} + +.row .column.column-offset-80 { + margin-left: 80%; +} + +.row .column.column-offset-90 { + margin-left: 90%; +} + +.row .column.column-10 { + flex: 0 0 10%; + max-width: 10%; +} + +.row .column.column-20 { + flex: 0 0 20%; + max-width: 20%; +} + +.row .column.column-25 { + flex: 0 0 25%; + max-width: 25%; +} + +.row .column.column-33, .row .column.column-34 { + flex: 0 0 33.3333%; + max-width: 33.3333%; +} + +.row .column.column-40 { + flex: 0 0 40%; + max-width: 40%; +} + +.row .column.column-50 { + flex: 0 0 50%; + max-width: 50%; +} + +.row .column.column-60 { + flex: 0 0 60%; + max-width: 60%; +} + +.row .column.column-66, .row .column.column-67 { + flex: 0 0 66.6666%; + max-width: 66.6666%; +} + +.row .column.column-75 { + flex: 0 0 75%; + max-width: 75%; +} + +.row .column.column-80 { + flex: 0 0 80%; + max-width: 80%; +} + +.row .column.column-90 { + flex: 0 0 90%; + max-width: 90%; +} + +.row .column .column-top { + align-self: flex-start; +} + +.row .column .column-bottom { + align-self: flex-end; +} + +.row .column .column-center { + align-self: center; +} + +@media (min-width: 40rem) { + .row { + flex-direction: row; + + width: calc(100% + 2.0rem); + } + .row .column { + margin-bottom: inherit; + padding: 0 1.0rem; + } +} + +a { + color: var(--highlight-color); + text-decoration: none; +} + +a:focus, a:hover { + color: var(--highlight-color2); +} + +dl, +ol, +ul { + list-style: none; + margin-top: 0; + padding-left: 0; +} + +dl dl, +dl ol, +dl ul, +ol dl, +ol ol, +ol ul, +ul dl, +ul ol, +ul ul { + font-size: 90%; + margin: 1.5rem 0 1.5rem 3.0rem; +} + +ol { + list-style: decimal inside; +} + +ul { + list-style: circle inside; +} + +.button, +button, +dd, +dt, +li { + margin-bottom: 1.0rem; +} + +fieldset, +input, +select, +textarea { + margin-bottom: 1.5rem; +} + +blockquote, +dl, +figure, +form, +ol, +p, +pre, +table, +ul { + margin-bottom: 2.5rem; +} + +table { + border-spacing: 0; + display: block; + overflow-x: auto; + text-align: left; + width: 100%; +} + +td, +th { + border-bottom: 0.1rem solid #e1e1e1; + padding: 1.2rem 1.5rem; +} + +td:first-child, +th:first-child { + padding-left: 0; +} + +td:last-child, +th:last-child { + padding-right: 0; +} + +@media (min-width: 40rem) { + table { + display: table; + overflow-x: initial; + } +} + +b, +strong { + font-weight: bold; +} + +p { + margin-top: 0; +} + +h1, +h2, +h3, +h4, +h5, +h6 { + font-weight: 300; + letter-spacing: -.1rem; + margin-bottom: 2.0rem; + margin-top: 0; +} + +h1 { + font-size: 4.6rem; + line-height: 1.2; +} + +h2 { + font-size: 3.6rem; + line-height: 1.25; +} + +h3 { + font-size: 2.8rem; + line-height: 1.3; +} + +h4 { + font-size: 2.2rem; + letter-spacing: -.08rem; + line-height: 1.35; +} + +h5 { + font-size: 1.8rem; + letter-spacing: -.05rem; + line-height: 1.5; +} + +h6 { + font-size: 1.6rem; + letter-spacing: 0; + line-height: 1.4; +} + +img { + max-width: 100%; +} + +.clearfix:after { + clear: both; + content: ' '; + display: table; +} + +.float-left { + float: left; +} + +.float-right { + float: right; +} + +.online { + opacity: 0.5; +} \ No newline at end of file