├── .gitignore ├── pio ├── pio_spi.h ├── pio_i2c.h ├── uart_tx.pio ├── pio_spi.c ├── uart_rx.pio ├── pio_i2c.c ├── i2c.pio └── spi.pio ├── CMakeLists.txt ├── usb_descriptors.h ├── pico_sdk_import.cmake ├── tusb_config.h ├── usb_descriptors.c ├── main.c └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | CMakeFiles/* 2 | elf2uf2/* 3 | generated/* 4 | pico-sdk/* 5 | pioasm/* 6 | _deps/* 7 | Makefile 8 | *.hex 9 | *.uf2 10 | *.elf 11 | *.dis 12 | *.bin 13 | *.map 14 | CMakeCache.txt 15 | pico-sdk 16 | spi.pio.h 17 | cmake_install.cmake 18 | -------------------------------------------------------------------------------- /pio/pio_spi.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. 3 | * 4 | * SPDX-License-Identifier: BSD-3-Clause 5 | */ 6 | #ifndef _PIO_SPI_H 7 | #define _PIO_SPI_H 8 | 9 | #include "hardware/pio.h" 10 | #include "spi.pio.h" 11 | 12 | typedef struct pio_spi_inst { 13 | PIO pio; 14 | uint sm; 15 | uint cs_pin; 16 | } pio_spi_inst_t; 17 | 18 | void pio_spi_write8_blocking(const pio_spi_inst_t *spi, const uint8_t *src, size_t len); 19 | 20 | void pio_spi_read8_blocking(const pio_spi_inst_t *spi, uint8_t *dst, size_t len); 21 | 22 | void pio_spi_write8_read8_blocking(const pio_spi_inst_t *spi, uint8_t *src, uint8_t *dst, size_t len); 23 | 24 | #endif 25 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.13) 2 | # initialize the SDK based on PICO_SDK_PATH 3 | # note: this must happen before project() 4 | include(pico_sdk_import.cmake) 5 | project(gbusb) 6 | 7 | pico_sdk_init() 8 | 9 | add_executable(gbusb) 10 | 11 | pico_generate_pio_header(gbusb ${CMAKE_CURRENT_LIST_DIR}/pio/spi.pio) 12 | 13 | target_include_directories(gbusb PRIVATE ${CMAKE_CURRENT_LIST_DIR}) 14 | 15 | target_sources(gbusb PRIVATE 16 | main.c 17 | 18 | usb_descriptors.c 19 | 20 | # PIO components 21 | pio/pio_spi.c 22 | ) 23 | 24 | target_link_libraries(gbusb PRIVATE pico_stdlib hardware_pio tinyusb_device tinyusb_board) 25 | pico_add_extra_outputs(gbusb) 26 | 27 | -------------------------------------------------------------------------------- /pio/pio_i2c.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2021 Raspberry Pi (Trading) Ltd. 3 | * 4 | * SPDX-License-Identifier: BSD-3-Clause 5 | */ 6 | #ifndef _PIO_I2C_H 7 | #define _PIO_I2C_H 8 | 9 | #include "i2c.pio.h" 10 | 11 | // ---------------------------------------------------------------------------- 12 | // Low-level functions 13 | 14 | void pio_i2c_start(PIO pio, uint sm); 15 | void pio_i2c_stop(PIO pio, uint sm); 16 | void pio_i2c_repstart(PIO pio, uint sm); 17 | 18 | bool pio_i2c_check_error(PIO pio, uint sm); 19 | void pio_i2c_resume_after_error(PIO pio, uint sm); 20 | 21 | // If I2C is ok, block and push data. Otherwise fall straight through. 22 | void pio_i2c_put_or_err(PIO pio, uint sm, uint16_t data); 23 | uint8_t pio_i2c_get(PIO pio, uint sm); 24 | 25 | // ---------------------------------------------------------------------------- 26 | // Transaction-level functions 27 | 28 | int pio_i2c_write_blocking(PIO pio, uint sm, uint8_t addr, uint8_t *txbuf, uint len); 29 | int pio_i2c_read_blocking(PIO pio, uint sm, uint8_t addr, uint8_t *rxbuf, uint len); 30 | 31 | #endif 32 | -------------------------------------------------------------------------------- /usb_descriptors.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 Ha Thach (tinyusb.org) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | */ 24 | 25 | #ifndef USB_DESCRIPTORS_H_ 26 | #define USB_DESCRIPTORS_H_ 27 | 28 | enum 29 | { 30 | VENDOR_REQUEST_WEBUSB = 1, 31 | VENDOR_REQUEST_MICROSOFT = 2 32 | }; 33 | 34 | extern uint8_t const desc_ms_os_20[]; 35 | 36 | #endif /* USB_DESCRIPTORS_H_ */ 37 | -------------------------------------------------------------------------------- /pio/uart_tx.pio: -------------------------------------------------------------------------------- 1 | ; 2 | ; Copyright (c) 2020 Raspberry Pi (Trading) Ltd. 3 | ; 4 | ; SPDX-License-Identifier: BSD-3-Clause 5 | ; 6 | 7 | .program uart_tx 8 | .side_set 1 opt 9 | 10 | ; An 8n1 UART transmit program. 11 | ; OUT pin 0 and side-set pin 0 are both mapped to UART TX pin. 12 | 13 | pull side 1 [7] ; Assert stop bit, or stall with line in idle state 14 | set x, 7 side 0 [7] ; Preload bit counter, assert start bit for 8 clocks 15 | bitloop: ; This loop will run 8 times (8n1 UART) 16 | out pins, 1 ; Shift 1 bit from OSR to the first OUT pin 17 | jmp x-- bitloop [6] ; Each loop iteration is 8 cycles. 18 | 19 | 20 | % c-sdk { 21 | #include "hardware/clocks.h" 22 | 23 | static inline void uart_tx_program_init(PIO pio, uint sm, uint offset, uint pin_tx, uint baud) { 24 | // Tell PIO to initially drive output-high on the selected pin, then map PIO 25 | // onto that pin with the IO muxes. 26 | pio_sm_set_pins_with_mask(pio, sm, 1u << pin_tx, 1u << pin_tx); 27 | pio_sm_set_pindirs_with_mask(pio, sm, 1u << pin_tx, 1u << pin_tx); 28 | pio_gpio_init(pio, pin_tx); 29 | 30 | pio_sm_config c = uart_tx_program_get_default_config(offset); 31 | 32 | // OUT shifts to right, no autopull 33 | sm_config_set_out_shift(&c, true, false, 32); 34 | 35 | // We are mapping both OUT and side-set to the same pin, because sometimes 36 | // we need to assert user data onto the pin (with OUT) and sometimes 37 | // assert constant values (start/stop bit) 38 | sm_config_set_out_pins(&c, pin_tx, 1); 39 | sm_config_set_sideset_pins(&c, pin_tx); 40 | 41 | // We only need TX, so get an 8-deep FIFO! 42 | sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX); 43 | 44 | // SM transmits 1 bit per 8 execution cycles. 45 | float div = (float)clock_get_hz(clk_sys) / (8 * baud); 46 | sm_config_set_clkdiv(&c, div); 47 | 48 | pio_sm_init(pio, sm, offset, &c); 49 | pio_sm_set_enabled(pio, sm, true); 50 | } 51 | 52 | static inline void uart_tx_program_putc(PIO pio, uint sm, char c) { 53 | pio_sm_put(pio, sm, (uint32_t)c); 54 | } 55 | 56 | static inline void uart_tx_program_puts(PIO pio, uint sm, const char *s) { 57 | while (*s) 58 | uart_tx_program_putc(pio, sm, *s++); 59 | } 60 | 61 | %} -------------------------------------------------------------------------------- /pio/pio_spi.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2020 Raspberry Pi (Trading) Ltd. 3 | * 4 | * SPDX-License-Identifier: BSD-3-Clause 5 | */ 6 | 7 | #include "pio_spi.h" 8 | 9 | // Just 8 bit functions provided here. The PIO program supports any frame size 10 | // 1...32, but the software to do the necessary FIFO shuffling is left as an 11 | // exercise for the reader :) 12 | // 13 | // Likewise we only provide MSB-first here. To do LSB-first, you need to 14 | // - Do shifts when reading from the FIFO, for general case n != 8, 16, 32 15 | // - Do a narrow read at a one halfword or 3 byte offset for n == 16, 8 16 | // in order to get the read data correctly justified. 17 | 18 | void __time_critical_func(pio_spi_write8_blocking)(const pio_spi_inst_t *spi, const uint8_t *src, size_t len) { 19 | size_t tx_remain = len, rx_remain = len; 20 | // Do 8 bit accesses on FIFO, so that write data is byte-replicated. This 21 | // gets us the left-justification for free (for MSB-first shift-out) 22 | io_rw_8 *txfifo = (io_rw_8 *) &spi->pio->txf[spi->sm]; 23 | io_rw_8 *rxfifo = (io_rw_8 *) &spi->pio->rxf[spi->sm]; 24 | while (tx_remain || rx_remain) { 25 | if (tx_remain && !pio_sm_is_tx_fifo_full(spi->pio, spi->sm)) { 26 | *txfifo = *src++; 27 | --tx_remain; 28 | } 29 | if (rx_remain && !pio_sm_is_rx_fifo_empty(spi->pio, spi->sm)) { 30 | (void) *rxfifo; 31 | --rx_remain; 32 | } 33 | } 34 | } 35 | 36 | void __time_critical_func(pio_spi_read8_blocking)(const pio_spi_inst_t *spi, uint8_t *dst, size_t len) { 37 | size_t tx_remain = len, rx_remain = len; 38 | io_rw_8 *txfifo = (io_rw_8 *) &spi->pio->txf[spi->sm]; 39 | io_rw_8 *rxfifo = (io_rw_8 *) &spi->pio->rxf[spi->sm]; 40 | while (tx_remain || rx_remain) { 41 | if (tx_remain && !pio_sm_is_tx_fifo_full(spi->pio, spi->sm)) { 42 | *txfifo = 0; 43 | --tx_remain; 44 | } 45 | if (rx_remain && !pio_sm_is_rx_fifo_empty(spi->pio, spi->sm)) { 46 | *dst++ = *rxfifo; 47 | --rx_remain; 48 | } 49 | } 50 | } 51 | 52 | void __time_critical_func(pio_spi_write8_read8_blocking)(const pio_spi_inst_t *spi, uint8_t *src, uint8_t *dst, 53 | size_t len) { 54 | size_t tx_remain = len, rx_remain = len; 55 | io_rw_8 *txfifo = (io_rw_8 *) &spi->pio->txf[spi->sm]; 56 | io_rw_8 *rxfifo = (io_rw_8 *) &spi->pio->rxf[spi->sm]; 57 | while (tx_remain || rx_remain) { 58 | if (tx_remain && !pio_sm_is_tx_fifo_full(spi->pio, spi->sm)) { 59 | *txfifo = *src++; 60 | --tx_remain; 61 | } 62 | if (rx_remain && !pio_sm_is_rx_fifo_empty(spi->pio, spi->sm)) { 63 | *dst++ = *rxfifo; 64 | --rx_remain; 65 | } 66 | } 67 | } 68 | 69 | -------------------------------------------------------------------------------- /pico_sdk_import.cmake: -------------------------------------------------------------------------------- 1 | # This is a copy of /external/pico_sdk_import.cmake 2 | 3 | # This can be dropped into an external project to help locate this SDK 4 | # It should be include()ed prior to project() 5 | 6 | if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH)) 7 | set(PICO_SDK_PATH $ENV{PICO_SDK_PATH}) 8 | message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')") 9 | endif () 10 | 11 | if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT)) 12 | set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT}) 13 | message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')") 14 | endif () 15 | 16 | if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH)) 17 | set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH}) 18 | message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')") 19 | endif () 20 | 21 | set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK") 22 | set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable") 23 | set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK") 24 | 25 | if (NOT PICO_SDK_PATH) 26 | if (PICO_SDK_FETCH_FROM_GIT) 27 | include(FetchContent) 28 | set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR}) 29 | if (PICO_SDK_FETCH_FROM_GIT_PATH) 30 | get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}") 31 | endif () 32 | FetchContent_Declare( 33 | pico_sdk 34 | GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk 35 | GIT_TAG master 36 | ) 37 | if (NOT pico_sdk) 38 | message("Downloading Raspberry Pi Pico SDK") 39 | FetchContent_Populate(pico_sdk) 40 | set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR}) 41 | endif () 42 | set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE}) 43 | else () 44 | message(FATAL_ERROR 45 | "SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git." 46 | ) 47 | endif () 48 | endif () 49 | 50 | get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}") 51 | if (NOT EXISTS ${PICO_SDK_PATH}) 52 | message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found") 53 | endif () 54 | 55 | set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake) 56 | if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE}) 57 | message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK") 58 | endif () 59 | 60 | set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE) 61 | 62 | include(${PICO_SDK_INIT_CMAKE_FILE}) 63 | -------------------------------------------------------------------------------- /pio/uart_rx.pio: -------------------------------------------------------------------------------- 1 | ; 2 | ; Copyright (c) 2020 Raspberry Pi (Trading) Ltd. 3 | ; 4 | ; SPDX-License-Identifier: BSD-3-Clause 5 | ; 6 | 7 | .program uart_rx_mini 8 | 9 | ; Minimum viable 8n1 UART receiver. Wait for the start bit, then sample 8 bits 10 | ; with the correct timing. 11 | ; IN pin 0 is mapped to the GPIO used as UART RX. 12 | ; Autopush must be enabled, with a threshold of 8. 13 | 14 | wait 0 pin 0 ; Wait for start bit 15 | set x, 7 [10] ; Preload bit counter, delay until eye of first data bit 16 | bitloop: ; Loop 8 times 17 | in pins, 1 ; Sample data 18 | jmp x-- bitloop [6] ; Each iteration is 8 cycles 19 | 20 | % c-sdk { 21 | #include "hardware/clocks.h" 22 | #include "hardware/gpio.h" 23 | 24 | static inline void uart_rx_mini_program_init(PIO pio, uint sm, uint offset, uint pin, uint baud) { 25 | pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, false); 26 | pio_gpio_init(pio, pin); 27 | gpio_pull_up(pin); 28 | 29 | pio_sm_config c = uart_rx_mini_program_get_default_config(offset); 30 | sm_config_set_in_pins(&c, pin); // for WAIT, IN 31 | // Shift to right, autopush enabled 32 | sm_config_set_in_shift(&c, true, true, 8); 33 | sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_RX); 34 | // SM transmits 1 bit per 8 execution cycles. 35 | float div = (float)clock_get_hz(clk_sys) / (8 * baud); 36 | sm_config_set_clkdiv(&c, div); 37 | 38 | pio_sm_init(pio, sm, offset, &c); 39 | pio_sm_set_enabled(pio, sm, true); 40 | } 41 | %} 42 | 43 | .program uart_rx 44 | 45 | ; Slightly more fleshed-out 8n1 UART receiver which handles framing errors and 46 | ; break conditions more gracefully. 47 | ; IN pin 0 and JMP pin are both mapped to the GPIO used as UART RX. 48 | 49 | start: 50 | wait 0 pin 0 ; Stall until start bit is asserted 51 | set x, 7 [10] ; Preload bit counter, then delay until halfway through 52 | bitloop: ; the first data bit (12 cycles incl wait, set). 53 | in pins, 1 ; Shift data bit into ISR 54 | jmp x-- bitloop [6] ; Loop 8 times, each loop iteration is 8 cycles 55 | jmp pin good_stop ; Check stop bit (should be high) 56 | 57 | irq 4 rel ; Either a framing error or a break. Set a sticky flag, 58 | wait 1 pin 0 ; and wait for line to return to idle state. 59 | jmp start ; Don't push data if we didn't see good framing. 60 | 61 | good_stop: ; No delay before returning to start; a little slack is 62 | push ; important in case the TX clock is slightly too fast. 63 | 64 | 65 | % c-sdk { 66 | static inline void uart_rx_program_init(PIO pio, uint sm, uint offset, uint pin, uint baud) { 67 | pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, false); 68 | pio_gpio_init(pio, pin); 69 | gpio_pull_up(pin); 70 | 71 | pio_sm_config c = uart_rx_program_get_default_config(offset); 72 | sm_config_set_in_pins(&c, pin); // for WAIT, IN 73 | sm_config_set_jmp_pin(&c, pin); // for JMP 74 | // Shift to right, autopull disabled 75 | sm_config_set_in_shift(&c, true, false, 32); 76 | // Deeper FIFO as we're not doing any TX 77 | sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_RX); 78 | // SM transmits 1 bit per 8 execution cycles. 79 | float div = (float)clock_get_hz(clk_sys) / (8 * baud); 80 | sm_config_set_clkdiv(&c, div); 81 | 82 | pio_sm_init(pio, sm, offset, &c); 83 | pio_sm_set_enabled(pio, sm, true); 84 | } 85 | 86 | static inline char uart_rx_program_getc(PIO pio, uint sm) { 87 | // 8-bit read from the uppermost byte of the FIFO, as data is left-justified 88 | io_rw_8 *rxfifo_shift = (io_rw_8*)&pio->rxf[sm] + 3; 89 | while (pio_sm_is_rx_fifo_empty(pio, sm)) 90 | tight_loop_contents(); 91 | return (char)*rxfifo_shift; 92 | } 93 | 94 | static inline bool uart_rx_program_data_available(PIO pio, uint sm) { 95 | if(pio_sm_is_rx_fifo_empty(pio, sm)) { 96 | return false; 97 | } 98 | return true; 99 | } 100 | 101 | %} 102 | -------------------------------------------------------------------------------- /tusb_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 Ha Thach (tinyusb.org) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * 24 | */ 25 | 26 | #ifndef _TUSB_CONFIG_H_ 27 | #define _TUSB_CONFIG_H_ 28 | 29 | #ifdef __cplusplus 30 | extern "C" { 31 | #endif 32 | 33 | //-------------------------------------------------------------------- 34 | // COMMON CONFIGURATION 35 | //-------------------------------------------------------------------- 36 | 37 | // defined by board.mk 38 | #ifndef CFG_TUSB_MCU 39 | #error CFG_TUSB_MCU must be defined 40 | #endif 41 | 42 | // RHPort number used for device can be defined by board.mk, default to port 0 43 | #ifndef BOARD_DEVICE_RHPORT_NUM 44 | #define BOARD_DEVICE_RHPORT_NUM 0 45 | #endif 46 | 47 | // RHPort max operational speed can defined by board.mk 48 | // Default to Highspeed for MCU with internal HighSpeed PHY (can be port specific), otherwise FullSpeed 49 | #ifndef BOARD_DEVICE_RHPORT_SPEED 50 | #if (CFG_TUSB_MCU == OPT_MCU_LPC18XX || CFG_TUSB_MCU == OPT_MCU_LPC43XX || CFG_TUSB_MCU == OPT_MCU_MIMXRT10XX || \ 51 | CFG_TUSB_MCU == OPT_MCU_NUC505 || CFG_TUSB_MCU == OPT_MCU_CXD56) 52 | #define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_HIGH_SPEED 53 | #else 54 | #define BOARD_DEVICE_RHPORT_SPEED OPT_MODE_FULL_SPEED 55 | #endif 56 | #endif 57 | 58 | // Device mode with rhport and speed defined by board.mk 59 | #if BOARD_DEVICE_RHPORT_NUM == 0 60 | #define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED) 61 | #elif BOARD_DEVICE_RHPORT_NUM == 1 62 | #define CFG_TUSB_RHPORT1_MODE (OPT_MODE_DEVICE | BOARD_DEVICE_RHPORT_SPEED) 63 | #else 64 | #error "Incorrect RHPort configuration" 65 | #endif 66 | 67 | #ifndef CFG_TUSB_OS 68 | #define CFG_TUSB_OS OPT_OS_NONE 69 | #endif 70 | 71 | // CFG_TUSB_DEBUG is defined by compiler in DEBUG build 72 | // #define CFG_TUSB_DEBUG 0 73 | 74 | /* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment. 75 | * Tinyusb use follows macros to declare transferring memory so that they can be put 76 | * into those specific section. 77 | * e.g 78 | * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") )) 79 | * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4))) 80 | */ 81 | #ifndef CFG_TUSB_MEM_SECTION 82 | #define CFG_TUSB_MEM_SECTION 83 | #endif 84 | 85 | #ifndef CFG_TUSB_MEM_ALIGN 86 | #define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) 87 | #endif 88 | 89 | //-------------------------------------------------------------------- 90 | // DEVICE CONFIGURATION 91 | //-------------------------------------------------------------------- 92 | 93 | #ifndef CFG_TUD_ENDPOINT0_SIZE 94 | #define CFG_TUD_ENDPOINT0_SIZE 64 95 | #endif 96 | 97 | //------------- CLASS -------------// 98 | #define CFG_TUD_CDC 1 99 | #define CFG_TUD_MSC 0 100 | #define CFG_TUD_HID 0 101 | #define CFG_TUD_MIDI 0 102 | #define CFG_TUD_VENDOR 1 103 | 104 | // CDC FIFO size of TX and RX 105 | #define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) 106 | #define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) 107 | 108 | // Vendor FIFO size of TX and RX 109 | // If not configured vendor endpoints will not be buffered 110 | #define CFG_TUD_VENDOR_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) 111 | #define CFG_TUD_VENDOR_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64) 112 | 113 | 114 | #ifdef __cplusplus 115 | } 116 | #endif 117 | 118 | #endif /* _TUSB_CONFIG_H_ */ 119 | -------------------------------------------------------------------------------- /pio/pio_i2c.c: -------------------------------------------------------------------------------- 1 | /** 2 | * Copyright (c) 2021 Raspberry Pi (Trading) Ltd. 3 | * 4 | * SPDX-License-Identifier: BSD-3-Clause 5 | */ 6 | 7 | #include "pio_i2c.h" 8 | 9 | const int PIO_I2C_ICOUNT_LSB = 10; 10 | const int PIO_I2C_FINAL_LSB = 9; 11 | const int PIO_I2C_DATA_LSB = 1; 12 | const int PIO_I2C_NAK_LSB = 0; 13 | 14 | 15 | bool pio_i2c_check_error(PIO pio, uint sm) { 16 | return !!(pio->irq & (1u << sm)); 17 | } 18 | 19 | void pio_i2c_resume_after_error(PIO pio, uint sm) { 20 | pio_sm_drain_tx_fifo(pio, sm); 21 | pio_sm_exec(pio, sm, (pio->sm[sm].execctrl & PIO_SM0_EXECCTRL_WRAP_BOTTOM_BITS) >> PIO_SM0_EXECCTRL_WRAP_BOTTOM_LSB); 22 | pio->irq = 1u << sm; 23 | } 24 | 25 | void pio_i2c_rx_enable(PIO pio, uint sm, bool en) { 26 | if (en) 27 | hw_set_bits(&pio->sm[sm].shiftctrl, PIO_SM0_SHIFTCTRL_AUTOPUSH_BITS); 28 | else 29 | hw_clear_bits(&pio->sm[sm].shiftctrl, PIO_SM0_SHIFTCTRL_AUTOPUSH_BITS); 30 | } 31 | 32 | static inline void pio_i2c_put16(PIO pio, uint sm, uint16_t data) { 33 | while (pio_sm_is_tx_fifo_full(pio, sm)) 34 | ; 35 | *(io_rw_16 *)&pio->txf[sm] = data; 36 | } 37 | 38 | 39 | // If I2C is ok, block and push data. Otherwise fall straight through. 40 | void pio_i2c_put_or_err(PIO pio, uint sm, uint16_t data) { 41 | while (pio_sm_is_tx_fifo_full(pio, sm)) 42 | if (pio_i2c_check_error(pio, sm)) 43 | return; 44 | if (pio_i2c_check_error(pio, sm)) 45 | return; 46 | *(io_rw_16 *)&pio->txf[sm] = data; 47 | } 48 | 49 | uint8_t pio_i2c_get(PIO pio, uint sm) { 50 | return (uint8_t)pio_sm_get(pio, sm); 51 | } 52 | 53 | void pio_i2c_start(PIO pio, uint sm) { 54 | pio_i2c_put_or_err(pio, sm, 1u << PIO_I2C_ICOUNT_LSB); // Escape code for 2 instruction sequence 55 | pio_i2c_put_or_err(pio, sm, set_scl_sda_program_instructions[I2C_SC1_SD0]); // We are already in idle state, just pull SDA low 56 | pio_i2c_put_or_err(pio, sm, set_scl_sda_program_instructions[I2C_SC0_SD0]); // Also pull clock low so we can present data 57 | } 58 | 59 | void pio_i2c_stop(PIO pio, uint sm) { 60 | pio_i2c_put_or_err(pio, sm, 2u << PIO_I2C_ICOUNT_LSB); 61 | pio_i2c_put_or_err(pio, sm, set_scl_sda_program_instructions[I2C_SC0_SD0]); // SDA is unknown; pull it down 62 | pio_i2c_put_or_err(pio, sm, set_scl_sda_program_instructions[I2C_SC1_SD0]); // Release clock 63 | pio_i2c_put_or_err(pio, sm, set_scl_sda_program_instructions[I2C_SC1_SD1]); // Release SDA to return to idle state 64 | }; 65 | 66 | void pio_i2c_repstart(PIO pio, uint sm) { 67 | pio_i2c_put_or_err(pio, sm, 3u << PIO_I2C_ICOUNT_LSB); 68 | pio_i2c_put_or_err(pio, sm, set_scl_sda_program_instructions[I2C_SC0_SD1]); 69 | pio_i2c_put_or_err(pio, sm, set_scl_sda_program_instructions[I2C_SC1_SD1]); 70 | pio_i2c_put_or_err(pio, sm, set_scl_sda_program_instructions[I2C_SC1_SD0]); 71 | pio_i2c_put_or_err(pio, sm, set_scl_sda_program_instructions[I2C_SC0_SD0]); 72 | } 73 | 74 | static void pio_i2c_wait_idle(PIO pio, uint sm) { 75 | // Finished when TX runs dry or SM hits an IRQ 76 | pio->fdebug = 1u << (PIO_FDEBUG_TXSTALL_LSB + sm); 77 | while (!(pio->fdebug & 1u << (PIO_FDEBUG_TXSTALL_LSB + sm) || pio_i2c_check_error(pio, sm))) 78 | tight_loop_contents(); 79 | } 80 | 81 | int pio_i2c_write_blocking(PIO pio, uint sm, uint8_t addr, uint8_t *txbuf, uint len) { 82 | int err = 0; 83 | pio_i2c_start(pio, sm); 84 | pio_i2c_rx_enable(pio, sm, false); 85 | pio_i2c_put16(pio, sm, (addr << 2) | 1u); 86 | while (len && !pio_i2c_check_error(pio, sm)) { 87 | if (!pio_sm_is_tx_fifo_full(pio, sm)) { 88 | --len; 89 | pio_i2c_put_or_err(pio, sm, (*txbuf++ << PIO_I2C_DATA_LSB) | ((len == 0) << PIO_I2C_FINAL_LSB) | 1u); 90 | } 91 | } 92 | pio_i2c_stop(pio, sm); 93 | pio_i2c_wait_idle(pio, sm); 94 | if (pio_i2c_check_error(pio, sm)) { 95 | err = -1; 96 | pio_i2c_resume_after_error(pio, sm); 97 | pio_i2c_stop(pio, sm); 98 | } 99 | return err; 100 | } 101 | 102 | int pio_i2c_read_blocking(PIO pio, uint sm, uint8_t addr, uint8_t *rxbuf, uint len) { 103 | int err = 0; 104 | pio_i2c_start(pio, sm); 105 | pio_i2c_rx_enable(pio, sm, true); 106 | while (!pio_sm_is_rx_fifo_empty(pio, sm)) 107 | (void)pio_i2c_get(pio, sm); 108 | pio_i2c_put16(pio, sm, (addr << 2) | 3u); 109 | uint32_t tx_remain = len; // Need to stuff 0xff bytes in to get clocks 110 | 111 | bool first = true; 112 | 113 | while ((tx_remain || len) && !pio_i2c_check_error(pio, sm)) { 114 | if (tx_remain && !pio_sm_is_tx_fifo_full(pio, sm)) { 115 | --tx_remain; 116 | pio_i2c_put16(pio, sm, (0xffu << 1) | (tx_remain ? 0 : (1u << PIO_I2C_FINAL_LSB) | (1u << PIO_I2C_NAK_LSB))); 117 | } 118 | if (!pio_sm_is_rx_fifo_empty(pio, sm)) { 119 | if (first) { 120 | // Ignore returned address byte 121 | (void)pio_i2c_get(pio, sm); 122 | first = false; 123 | } 124 | else { 125 | --len; 126 | *rxbuf++ = pio_i2c_get(pio, sm); 127 | } 128 | } 129 | } 130 | pio_i2c_stop(pio, sm); 131 | pio_i2c_wait_idle(pio, sm); 132 | if (pio_i2c_check_error(pio, sm)) { 133 | err = -1; 134 | pio_i2c_resume_after_error(pio, sm); 135 | pio_i2c_stop(pio, sm); 136 | } 137 | return err; 138 | } 139 | 140 | -------------------------------------------------------------------------------- /pio/i2c.pio: -------------------------------------------------------------------------------- 1 | ; 2 | ; Copyright (c) 2020 Raspberry Pi (Trading) Ltd. 3 | ; 4 | ; SPDX-License-Identifier: BSD-3-Clause 5 | ; 6 | 7 | .program i2c 8 | .side_set 1 opt pindirs 9 | 10 | ; TX Encoding: 11 | ; | 15:10 | 9 | 8:1 | 0 | 12 | ; | Instr | Final | Data | NAK | 13 | ; 14 | ; If Instr has a value n > 0, then this FIFO word has no 15 | ; data payload, and the next n + 1 words will be executed as instructions. 16 | ; Otherwise, shift out the 8 data bits, followed by the ACK bit. 17 | ; 18 | ; The Instr mechanism allows stop/start/repstart sequences to be programmed 19 | ; by the processor, and then carried out by the state machine at defined points 20 | ; in the datastream. 21 | ; 22 | ; The "Final" field should be set for the final byte in a transfer. 23 | ; This tells the state machine to ignore a NAK: if this field is not 24 | ; set, then any NAK will cause the state machine to halt and interrupt. 25 | ; 26 | ; Autopull should be enabled, with a threshold of 16. 27 | ; Autopush should be enabled, with a threshold of 8. 28 | ; The TX FIFO should be accessed with halfword writes, to ensure 29 | ; the data is immediately available in the OSR. 30 | ; 31 | ; Pin mapping: 32 | ; - Input pin 0 is SDA, 1 is SCL (if clock stretching used) 33 | ; - Jump pin is SDA 34 | ; - Side-set pin 0 is SCL 35 | ; - Set pin 0 is SDA 36 | ; - OUT pin 0 is SDA 37 | ; - SCL must be SDA + 1 (for wait mapping) 38 | ; 39 | ; The OE outputs should be inverted in the system IO controls! 40 | ; (It's possible for the inversion to be done in this program, 41 | ; but costs 2 instructions: 1 for inversion, and one to cope 42 | ; with the side effect of the MOV on TX shift counter.) 43 | 44 | do_nack: 45 | jmp y-- entry_point ; Continue if NAK was expected 46 | irq wait 0 rel ; Otherwise stop, ask for help 47 | 48 | do_byte: 49 | set x, 7 ; Loop 8 times 50 | bitloop: 51 | out pindirs, 1 [7] ; Serialise write data (all-ones if reading) 52 | nop side 1 [2] ; SCL rising edge 53 | wait 1 pin, 1 [4] ; Allow clock to be stretched 54 | in pins, 1 [7] ; Sample read data in middle of SCL pulse 55 | jmp x-- bitloop side 0 [7] ; SCL falling edge 56 | 57 | ; Handle ACK pulse 58 | out pindirs, 1 [7] ; On reads, we provide the ACK. 59 | nop side 1 [7] ; SCL rising edge 60 | wait 1 pin, 1 [7] ; Allow clock to be stretched 61 | jmp pin do_nack side 0 [2] ; Test SDA for ACK/NAK, fall through if ACK 62 | 63 | public entry_point: 64 | .wrap_target 65 | out x, 6 ; Unpack Instr count 66 | out y, 1 ; Unpack the NAK ignore bit 67 | jmp !x do_byte ; Instr == 0, this is a data record. 68 | out null, 32 ; Instr > 0, remainder of this OSR is invalid 69 | do_exec: 70 | out exec, 16 ; Execute one instruction per FIFO word 71 | jmp x-- do_exec ; Repeat n + 1 times 72 | .wrap 73 | 74 | % c-sdk { 75 | 76 | #include "hardware/clocks.h" 77 | #include "hardware/gpio.h" 78 | 79 | 80 | static inline void i2c_program_init(PIO pio, uint sm, uint offset, uint pin_sda, uint pin_scl) { 81 | assert(pin_scl == pin_sda + 1); 82 | pio_sm_config c = i2c_program_get_default_config(offset); 83 | 84 | // IO mapping 85 | sm_config_set_out_pins(&c, pin_sda, 1); 86 | sm_config_set_set_pins(&c, pin_sda, 1); 87 | sm_config_set_in_pins(&c, pin_sda); 88 | sm_config_set_sideset_pins(&c, pin_scl); 89 | sm_config_set_jmp_pin(&c, pin_sda); 90 | 91 | sm_config_set_out_shift(&c, false, true, 16); 92 | sm_config_set_in_shift(&c, false, true, 8); 93 | 94 | float div = (float)clock_get_hz(clk_sys) / (200 * 100000); 95 | sm_config_set_clkdiv(&c, div); 96 | 97 | // Try to avoid glitching the bus while connecting the IOs. Get things set 98 | // up so that pin is driven down when PIO asserts OE low, and pulled up 99 | // otherwise. 100 | gpio_pull_up(pin_scl); 101 | gpio_pull_up(pin_sda); 102 | uint32_t both_pins = (1u << pin_sda) | (1u << pin_scl); 103 | pio_sm_set_pins_with_mask(pio, sm, both_pins, both_pins); 104 | pio_sm_set_pindirs_with_mask(pio, sm, both_pins, both_pins); 105 | pio_gpio_init(pio, pin_sda); 106 | gpio_set_oeover(pin_sda, GPIO_OVERRIDE_INVERT); 107 | pio_gpio_init(pio, pin_scl); 108 | gpio_set_oeover(pin_scl, GPIO_OVERRIDE_INVERT); 109 | pio_sm_set_pins_with_mask(pio, sm, 0, both_pins); 110 | 111 | // Clear IRQ flag before starting 112 | hw_clear_bits(&pio->inte0, 1u << sm); 113 | hw_clear_bits(&pio->inte1, 1u << sm); 114 | pio->irq = 1u << sm; 115 | 116 | // Configure and start SM 117 | pio_sm_init(pio, sm, offset + i2c_offset_entry_point, &c); 118 | pio_sm_set_enabled(pio, sm, true); 119 | } 120 | 121 | %} 122 | 123 | 124 | .program set_scl_sda 125 | .side_set 1 opt 126 | 127 | ; Assemble a table of instructions which software can select from, and pass 128 | ; into the FIFO, to issue START/STOP/RSTART. This isn't intended to be run as 129 | ; a complete program. 130 | 131 | set pindirs, 0 side 0 [7] ; SCL = 0, SDA = 0 132 | set pindirs, 1 side 0 [7] ; SCL = 0, SDA = 1 133 | set pindirs, 0 side 1 [7] ; SCL = 1, SDA = 0 134 | set pindirs, 1 side 1 [7] ; SCL = 1, SDA = 1 135 | 136 | % c-sdk { 137 | // Define order of our instruction table 138 | enum { 139 | I2C_SC0_SD0 = 0, 140 | I2C_SC0_SD1, 141 | I2C_SC1_SD0, 142 | I2C_SC1_SD1 143 | }; 144 | %} 145 | -------------------------------------------------------------------------------- /pio/spi.pio: -------------------------------------------------------------------------------- 1 | ; 2 | ; Copyright (c) 2020 Raspberry Pi (Trading) Ltd. 3 | ; 4 | ; SPDX-License-Identifier: BSD-3-Clause 5 | ; 6 | 7 | ; These programs implement full-duplex SPI, with a SCK period of 4 clock 8 | ; cycles. A different program is provided for each value of CPHA, and CPOL is 9 | ; achieved using the hardware GPIO inversion available in the IO controls. 10 | ; 11 | ; Transmit-only SPI can go twice as fast -- see the ST7789 example! 12 | 13 | 14 | .program spi_cpha0 15 | .side_set 1 16 | 17 | ; Pin assignments: 18 | ; - SCK is side-set pin 0 19 | ; - MOSI is OUT pin 0 20 | ; - MISO is IN pin 0 21 | ; 22 | ; Autopush and autopull must be enabled, and the serial frame size is set by 23 | ; configuring the push/pull threshold. Shift left/right is fine, but you must 24 | ; justify the data yourself. This is done most conveniently for frame sizes of 25 | ; 8 or 16 bits by using the narrow store replication and narrow load byte 26 | ; picking behaviour of RP2040's IO fabric. 27 | 28 | ; Clock phase = 0: data is captured on the leading edge of each SCK pulse, and 29 | ; transitions on the trailing edge, or some time before the first leading edge. 30 | 31 | out pins, 1 side 0 [1] ; Stall here on empty (sideset proceeds even if 32 | in pins, 1 side 1 [1] ; instruction stalls, so we stall with SCK low) 33 | 34 | .program spi_cpha1 35 | .side_set 1 36 | 37 | ; Clock phase = 1: data transitions on the leading edge of each SCK pulse, and 38 | ; is captured on the trailing edge. 39 | 40 | out x, 1 side 0 ; Stall here on empty (keep SCK deasserted) 41 | mov pins, x side 1 [1] ; Output data, assert SCK (mov pins uses OUT mapping) 42 | in pins, 1 side 0 ; Input data, deassert SCK 43 | 44 | % c-sdk { 45 | #include "hardware/gpio.h" 46 | static inline void pio_spi_init(PIO pio, uint sm, uint prog_offs, uint n_bits, 47 | float clkdiv, bool cpha, bool cpol, uint pin_sck, uint pin_mosi, uint pin_miso) { 48 | pio_sm_config c = cpha ? spi_cpha1_program_get_default_config(prog_offs) : spi_cpha0_program_get_default_config(prog_offs); 49 | sm_config_set_out_pins(&c, pin_mosi, 1); 50 | sm_config_set_in_pins(&c, pin_miso); 51 | sm_config_set_sideset_pins(&c, pin_sck); 52 | // Only support MSB-first in this example code (shift to left, auto push/pull, threshold=nbits) 53 | sm_config_set_out_shift(&c, false, true, n_bits); 54 | sm_config_set_in_shift(&c, false, true, n_bits); 55 | sm_config_set_clkdiv(&c, clkdiv); 56 | 57 | // MOSI, SCK output are low, MISO is input 58 | pio_sm_set_pins_with_mask(pio, sm, 0, (1u << pin_sck) | (1u << pin_mosi)); 59 | pio_sm_set_pindirs_with_mask(pio, sm, (1u << pin_sck) | (1u << pin_mosi), (1u << pin_sck) | (1u << pin_mosi) | (1u << pin_miso)); 60 | pio_gpio_init(pio, pin_mosi); 61 | pio_gpio_init(pio, pin_miso); 62 | pio_gpio_init(pio, pin_sck); 63 | 64 | // The pin muxes can be configured to invert the output (among other things 65 | // and this is a cheesy way to get CPOL=1 66 | gpio_set_outover(pin_sck, cpol ? GPIO_OVERRIDE_INVERT : GPIO_OVERRIDE_NORMAL); 67 | // SPI is synchronous, so bypass input synchroniser to reduce input delay. 68 | hw_set_bits(&pio->input_sync_bypass, 1u << pin_miso); 69 | 70 | pio_sm_init(pio, sm, prog_offs, &c); 71 | pio_sm_set_enabled(pio, sm, true); 72 | } 73 | %} 74 | 75 | ; SPI with Chip Select 76 | ; ----------------------------------------------------------------------------- 77 | ; 78 | ; For your amusement, here are some SPI programs with an automatic chip select 79 | ; (asserted once data appears in TX FIFO, deasserts when FIFO bottoms out, has 80 | ; a nice front/back porch). 81 | ; 82 | ; The number of bits per FIFO entry is configured via the Y register 83 | ; and the autopush/pull threshold. From 2 to 32 bits. 84 | ; 85 | ; Pin assignments: 86 | ; - SCK is side-set bit 0 87 | ; - CSn is side-set bit 1 88 | ; - MOSI is OUT bit 0 (host-to-device) 89 | ; - MISO is IN bit 0 (device-to-host) 90 | ; 91 | ; This program only supports one chip select -- use GPIO if more are needed 92 | ; 93 | ; Provide a variation for each possibility of CPHA; for CPOL we can just 94 | ; invert SCK in the IO muxing controls (downstream from PIO) 95 | 96 | 97 | ; CPHA=0: data is captured on the leading edge of each SCK pulse (including 98 | ; the first pulse), and transitions on the trailing edge 99 | 100 | .program spi_cpha0_cs 101 | .side_set 2 102 | 103 | .wrap_target 104 | bitloop: 105 | out pins, 1 side 0x0 [1] 106 | in pins, 1 side 0x1 107 | jmp x-- bitloop side 0x1 108 | 109 | out pins, 1 side 0x0 110 | mov x, y side 0x0 ; Reload bit counter from Y 111 | in pins, 1 side 0x1 112 | jmp !osre bitloop side 0x1 ; Fall-through if TXF empties 113 | 114 | nop side 0x0 [1] ; CSn back porch 115 | public entry_point: ; Must set X,Y to n-2 before starting! 116 | pull ifempty side 0x2 [1] ; Block with CSn high (minimum 2 cycles) 117 | .wrap ; Note ifempty to avoid time-of-check race 118 | 119 | ; CPHA=1: data transitions on the leading edge of each SCK pulse, and is 120 | ; captured on the trailing edge 121 | 122 | .program spi_cpha1_cs 123 | .side_set 2 124 | 125 | .wrap_target 126 | bitloop: 127 | out pins, 1 side 0x1 [1] 128 | in pins, 1 side 0x0 129 | jmp x-- bitloop side 0x0 130 | 131 | out pins, 1 side 0x1 132 | mov x, y side 0x1 133 | in pins, 1 side 0x0 134 | jmp !osre bitloop side 0x0 135 | 136 | public entry_point: ; Must set X,Y to n-2 before starting! 137 | pull ifempty side 0x2 [1] ; Block with CSn high (minimum 2 cycles) 138 | nop side 0x0 [1]; CSn front porch 139 | .wrap 140 | 141 | % c-sdk { 142 | #include "hardware/gpio.h" 143 | static inline void pio_spi_cs_init(PIO pio, uint sm, uint prog_offs, uint n_bits, float clkdiv, bool cpha, bool cpol, 144 | uint pin_sck, uint pin_mosi, uint pin_miso) { 145 | pio_sm_config c = cpha ? spi_cpha1_cs_program_get_default_config(prog_offs) : spi_cpha0_cs_program_get_default_config(prog_offs); 146 | sm_config_set_out_pins(&c, pin_mosi, 1); 147 | sm_config_set_in_pins(&c, pin_miso); 148 | sm_config_set_sideset_pins(&c, pin_sck); 149 | sm_config_set_out_shift(&c, false, true, n_bits); 150 | sm_config_set_in_shift(&c, false, true, n_bits); 151 | sm_config_set_clkdiv(&c, clkdiv); 152 | 153 | pio_sm_set_pins_with_mask(pio, sm, (2u << pin_sck), (3u << pin_sck) | (1u << pin_mosi)); 154 | pio_sm_set_pindirs_with_mask(pio, sm, (3u << pin_sck) | (1u << pin_mosi), (3u << pin_sck) | (1u << pin_mosi) | (1u << pin_miso)); 155 | pio_gpio_init(pio, pin_mosi); 156 | pio_gpio_init(pio, pin_miso); 157 | pio_gpio_init(pio, pin_sck); 158 | pio_gpio_init(pio, pin_sck + 1); 159 | gpio_set_outover(pin_sck, cpol ? GPIO_OVERRIDE_INVERT : GPIO_OVERRIDE_NORMAL); 160 | hw_set_bits(&pio->input_sync_bypass, 1u << pin_miso); 161 | 162 | uint entry_point = prog_offs + (cpha ? spi_cpha1_cs_offset_entry_point : spi_cpha0_cs_offset_entry_point); 163 | pio_sm_init(pio, sm, entry_point, &c); 164 | pio_sm_exec(pio, sm, pio_encode_set(pio_x, n_bits - 2)); 165 | pio_sm_exec(pio, sm, pio_encode_set(pio_y, n_bits - 2)); 166 | pio_sm_set_enabled(pio, sm, true); 167 | } 168 | %} 169 | -------------------------------------------------------------------------------- /usb_descriptors.c: -------------------------------------------------------------------------------- 1 | /* 2 | * The MIT License (MIT) 3 | * 4 | * Copyright (c) 2019 Ha Thach (tinyusb.org) 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy 7 | * of this software and associated documentation files (the "Software"), to deal 8 | * in the Software without restriction, including without limitation the rights 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 | * copies of the Software, and to permit persons to whom the Software is 11 | * furnished to do so, subject to the following conditions: 12 | * 13 | * The above copyright notice and this permission notice shall be included in 14 | * all copies or substantial portions of the Software. 15 | * 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 | * THE SOFTWARE. 23 | * 24 | */ 25 | 26 | #include "tusb.h" 27 | #include "usb_descriptors.h" 28 | 29 | /* A combination of interfaces must have a unique product id, since PC will save device driver after the first plug. 30 | * Same VID/PID with different interface e.g MSC (first), then CDC (later) will possibly cause system error on PC. 31 | * 32 | * Auto ProductID layout's Bitmap: 33 | * [MSB] MIDI | HID | MSC | CDC [LSB] 34 | */ 35 | #define _PID_MAP(itf, n) ( (CFG_TUD_##itf) << (n) ) 36 | #define USB_PID (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1) | _PID_MAP(HID, 2) | \ 37 | _PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) ) 38 | 39 | //--------------------------------------------------------------------+ 40 | // Device Descriptors 41 | //--------------------------------------------------------------------+ 42 | tusb_desc_device_t const desc_device = 43 | { 44 | .bLength = sizeof(tusb_desc_device_t), 45 | .bDescriptorType = TUSB_DESC_DEVICE, 46 | .bcdUSB = 0x0210, // at least 2.1 or 3.x for BOS & webUSB 47 | 48 | // Use Interface Association Descriptor (IAD) for CDC 49 | // As required by USB Specs IAD's subclass must be common class (2) and protocol must be IAD (1) 50 | .bDeviceClass = TUSB_CLASS_MISC, 51 | .bDeviceSubClass = MISC_SUBCLASS_COMMON, 52 | .bDeviceProtocol = MISC_PROTOCOL_IAD, 53 | .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE, 54 | 55 | .idVendor = 0xCafe, 56 | .idProduct = USB_PID, 57 | .bcdDevice = 0x0100, 58 | 59 | .iManufacturer = 0x01, 60 | .iProduct = 0x02, 61 | .iSerialNumber = 0x03, 62 | 63 | .bNumConfigurations = 0x01 64 | }; 65 | 66 | // Invoked when received GET DEVICE DESCRIPTOR 67 | // Application return pointer to descriptor 68 | uint8_t const * tud_descriptor_device_cb(void) 69 | { 70 | return (uint8_t const *) &desc_device; 71 | } 72 | 73 | //--------------------------------------------------------------------+ 74 | // Configuration Descriptor 75 | //--------------------------------------------------------------------+ 76 | enum 77 | { 78 | ITF_NUM_CDC = 0, 79 | ITF_NUM_CDC_DATA, 80 | ITF_NUM_VENDOR, 81 | ITF_NUM_TOTAL 82 | }; 83 | 84 | #define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN + TUD_VENDOR_DESC_LEN) 85 | 86 | #if CFG_TUSB_MCU == OPT_MCU_LPC175X_6X || CFG_TUSB_MCU == OPT_MCU_LPC177X_8X || CFG_TUSB_MCU == OPT_MCU_LPC40XX 87 | // LPC 17xx and 40xx endpoint type (bulk/interrupt/iso) are fixed by its number 88 | // 0 control, 1 In, 2 Bulk, 3 Iso, 4 In etc ... 89 | #define EPNUM_CDC 2 90 | #define EPNUM_VENDOR 5 91 | #else 92 | #define EPNUM_CDC 2 93 | #define EPNUM_VENDOR 3 94 | #endif 95 | 96 | uint8_t const desc_configuration[] = 97 | { 98 | // Config number, interface count, string index, total length, attribute, power in mA 99 | TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, TUSB_DESC_CONFIG_ATT_REMOTE_WAKEUP, 100), 100 | 101 | // Interface number, string index, EP notification address and size, EP data address (out, in) and size. 102 | TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, 0x81, 8, EPNUM_CDC, 0x80 | EPNUM_CDC, TUD_OPT_HIGH_SPEED ? 512 : 64), 103 | 104 | // Interface number, string index, EP Out & IN address, EP size 105 | TUD_VENDOR_DESCRIPTOR(ITF_NUM_VENDOR, 5, EPNUM_VENDOR, 0x80 | EPNUM_VENDOR, TUD_OPT_HIGH_SPEED ? 512 : 64) 106 | }; 107 | 108 | // Invoked when received GET CONFIGURATION DESCRIPTOR 109 | // Application return pointer to descriptor 110 | // Descriptor contents must exist long enough for transfer to complete 111 | uint8_t const * tud_descriptor_configuration_cb(uint8_t index) 112 | { 113 | (void) index; // for multiple configurations 114 | return desc_configuration; 115 | } 116 | 117 | //--------------------------------------------------------------------+ 118 | // BOS Descriptor 119 | //--------------------------------------------------------------------+ 120 | 121 | /* Microsoft OS 2.0 registry property descriptor 122 | Per MS requirements https://msdn.microsoft.com/en-us/library/windows/hardware/hh450799(v=vs.85).aspx 123 | device should create DeviceInterfaceGUIDs. It can be done by driver and 124 | in case of real PnP solution device should expose MS "Microsoft OS 2.0 125 | registry property descriptor". Such descriptor can insert any record 126 | into Windows registry per device/configuration/interface. In our case it 127 | will insert "DeviceInterfaceGUIDs" multistring property. 128 | 129 | GUID is freshly generated and should be OK to use. 130 | 131 | https://developers.google.com/web/fundamentals/native-hardware/build-for-webusb/ 132 | (Section Microsoft OS compatibility descriptors) 133 | */ 134 | 135 | #define BOS_TOTAL_LEN (TUD_BOS_DESC_LEN + TUD_BOS_WEBUSB_DESC_LEN + TUD_BOS_MICROSOFT_OS_DESC_LEN) 136 | 137 | #define MS_OS_20_DESC_LEN 0xB2 138 | 139 | // BOS Descriptor is required for webUSB 140 | uint8_t const desc_bos[] = 141 | { 142 | // total length, number of device caps 143 | TUD_BOS_DESCRIPTOR(BOS_TOTAL_LEN, 2), 144 | 145 | // Vendor Code, iLandingPage 146 | TUD_BOS_WEBUSB_DESCRIPTOR(VENDOR_REQUEST_WEBUSB, 1), 147 | 148 | // Microsoft OS 2.0 descriptor 149 | TUD_BOS_MS_OS_20_DESCRIPTOR(MS_OS_20_DESC_LEN, VENDOR_REQUEST_MICROSOFT) 150 | }; 151 | 152 | uint8_t const * tud_descriptor_bos_cb(void) 153 | { 154 | return desc_bos; 155 | } 156 | 157 | 158 | uint8_t const desc_ms_os_20[] = 159 | { 160 | // Set header: length, type, windows version, total length 161 | U16_TO_U8S_LE(0x000A), U16_TO_U8S_LE(MS_OS_20_SET_HEADER_DESCRIPTOR), U32_TO_U8S_LE(0x06030000), U16_TO_U8S_LE(MS_OS_20_DESC_LEN), 162 | 163 | // Configuration subset header: length, type, configuration index, reserved, configuration total length 164 | U16_TO_U8S_LE(0x0008), U16_TO_U8S_LE(MS_OS_20_SUBSET_HEADER_CONFIGURATION), 0, 0, U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A), 165 | 166 | // Function Subset header: length, type, first interface, reserved, subset length 167 | U16_TO_U8S_LE(0x0008), U16_TO_U8S_LE(MS_OS_20_SUBSET_HEADER_FUNCTION), ITF_NUM_VENDOR, 0, U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A-0x08), 168 | 169 | // MS OS 2.0 Compatible ID descriptor: length, type, compatible ID, sub compatible ID 170 | U16_TO_U8S_LE(0x0014), U16_TO_U8S_LE(MS_OS_20_FEATURE_COMPATBLE_ID), 'W', 'I', 'N', 'U', 'S', 'B', 0x00, 0x00, 171 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // sub-compatible 172 | 173 | // MS OS 2.0 Registry property descriptor: length, type 174 | U16_TO_U8S_LE(MS_OS_20_DESC_LEN-0x0A-0x08-0x08-0x14), U16_TO_U8S_LE(MS_OS_20_FEATURE_REG_PROPERTY), 175 | U16_TO_U8S_LE(0x0007), U16_TO_U8S_LE(0x002A), // wPropertyDataType, wPropertyNameLength and PropertyName "DeviceInterfaceGUIDs\0" in UTF-16 176 | 'D', 0x00, 'e', 0x00, 'v', 0x00, 'i', 0x00, 'c', 0x00, 'e', 0x00, 'I', 0x00, 'n', 0x00, 't', 0x00, 'e', 0x00, 177 | 'r', 0x00, 'f', 0x00, 'a', 0x00, 'c', 0x00, 'e', 0x00, 'G', 0x00, 'U', 0x00, 'I', 0x00, 'D', 0x00, 's', 0x00, 0x00, 0x00, 178 | U16_TO_U8S_LE(0x0050), // wPropertyDataLength 179 | //bPropertyData: “{975F44D9-0D08-43FD-8B3E-127CA8AFFF9D}”. 180 | '{', 0x00, '9', 0x00, '7', 0x00, '5', 0x00, 'F', 0x00, '4', 0x00, '4', 0x00, 'D', 0x00, '9', 0x00, '-', 0x00, 181 | '0', 0x00, 'D', 0x00, '0', 0x00, '8', 0x00, '-', 0x00, '4', 0x00, '3', 0x00, 'F', 0x00, 'D', 0x00, '-', 0x00, 182 | '8', 0x00, 'B', 0x00, '3', 0x00, 'E', 0x00, '-', 0x00, '1', 0x00, '2', 0x00, '7', 0x00, 'C', 0x00, 'A', 0x00, 183 | '8', 0x00, 'A', 0x00, 'F', 0x00, 'F', 0x00, 'F', 0x00, '9', 0x00, 'D', 0x00, '}', 0x00, 0x00, 0x00, 0x00, 0x00 184 | }; 185 | 186 | TU_VERIFY_STATIC(sizeof(desc_ms_os_20) == MS_OS_20_DESC_LEN, "Incorrect size"); 187 | 188 | //--------------------------------------------------------------------+ 189 | // String Descriptors 190 | //--------------------------------------------------------------------+ 191 | 192 | // array of pointer to string descriptors 193 | char const* string_desc_arr [] = 194 | { 195 | (const char[]) { 0x09, 0x04 }, // 0: is supported language is English (0x0409) 196 | "stacksmashing", // 1: Manufacturer 197 | "USB to Game Boy Link Cable", // 2: Product 198 | "1", // 3: Serials, should use chip ID 199 | "TinyUSB CDC", // 4: CDC Interface 200 | "TinyUSB WebUSB" // 5: Vendor Interface 201 | }; 202 | 203 | static uint16_t _desc_str[32]; 204 | 205 | // Invoked when received GET STRING DESCRIPTOR request 206 | // Application return pointer to descriptor, whose contents must exist long enough for transfer to complete 207 | uint16_t const* tud_descriptor_string_cb(uint8_t index, uint16_t langid) 208 | { 209 | (void) langid; 210 | 211 | uint8_t chr_count; 212 | 213 | if ( index == 0) 214 | { 215 | memcpy(&_desc_str[1], string_desc_arr[0], 2); 216 | chr_count = 1; 217 | }else 218 | { 219 | // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors. 220 | // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors 221 | 222 | if ( !(index < sizeof(string_desc_arr)/sizeof(string_desc_arr[0])) ) return NULL; 223 | 224 | const char* str = string_desc_arr[index]; 225 | 226 | // Cap at max char 227 | chr_count = strlen(str); 228 | if ( chr_count > 31 ) chr_count = 31; 229 | 230 | // Convert ASCII string into UTF-16 231 | for(uint8_t i=0; i 27 | #include 28 | #include 29 | 30 | #include "bsp/board.h" 31 | #include "tusb.h" 32 | #include "usb_descriptors.h" 33 | #include 34 | #include 35 | #include "hardware/pio.h" 36 | #include "pio/pio_spi.h" 37 | #include "pico/time.h" 38 | 39 | #define NUM_CMP_BYTES 0x20 40 | #define NUM_CMP_BYTES_RECV (NUM_CMP_BYTES+4) 41 | 42 | #define NUM_DEFAULT_BYTES_PER_TRANSFER 1 43 | #define US_DEFAULT_PER_TRANSFER 1000 44 | 45 | #define MAX_TRANSFER_BYTES 0x40 46 | 47 | #define PIN_SCK 0 48 | #define PIN_SIN 1 49 | #define TEST_PIN 6 50 | 51 | uint PIN_SOUT = 2; 52 | uint SI_PIN = 3; 53 | 54 | bool is_test_pin_grounded() { 55 | gpio_init(TEST_PIN); 56 | gpio_set_dir(TEST_PIN, GPIO_OUT); 57 | gpio_put(TEST_PIN, 1); // Set the pin high 58 | 59 | // Read the state of the pin 60 | bool grounded = gpio_get(TEST_PIN) == 0; 61 | 62 | return grounded; 63 | } 64 | 65 | //--------------------------------------------------------------------+ 66 | // MACRO CONSTANT TYPEDEF PROTYPES 67 | //--------------------------------------------------------------------+ 68 | 69 | /* Blink pattern 70 | * - 250 ms : device not mounted 71 | * - 1000 ms : device mounted 72 | * - 2500 ms : device is suspended 73 | */ 74 | enum { 75 | BLINK_NOT_MOUNTED = 250, 76 | BLINK_MOUNTED = 1000, 77 | BLINK_SUSPENDED = 2500, 78 | 79 | BLINK_ALWAYS_ON = UINT32_MAX, 80 | BLINK_ALWAYS_OFF = 0 81 | }; 82 | 83 | static uint32_t blink_interval_ms = BLINK_NOT_MOUNTED; 84 | static uint8_t data_buf[MAX_TRANSFER_BYTES]; 85 | static uint8_t compare_bytes[NUM_CMP_BYTES] = {0xCA, 0xFE, 0xCA, 0xFE, 0xCA, 0xFE, 0xCA, 0xFE, 0xCA, 0xFE, 0xCA, 0xFE, 0xCA, 0xFE, 0xCA, 0xFE, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF}; 86 | static uint8_t buf_count; 87 | static uint8_t num_bytes_per_transfer = NUM_DEFAULT_BYTES_PER_TRANSFER; 88 | static uint32_t us_between_transfer = US_DEFAULT_PER_TRANSFER; 89 | static uint32_t total_transferred = 0; 90 | 91 | #define URL "tetris.gblink.io" 92 | 93 | const tusb_desc_webusb_url_t desc_url = 94 | { 95 | .bLength = 3 + sizeof(URL) - 1, 96 | .bDescriptorType = 3, // WEBUSB URL type 97 | .bScheme = 1, // 0: http, 1: https 98 | .url = URL 99 | }; 100 | 101 | static bool web_serial_connected = false; 102 | 103 | //------------- prototypes -------------// 104 | void handle_input_data(uint8_t* buf_in, uint32_t count); 105 | void data_transfer_task(void); 106 | void led_blinking_task(void); 107 | void cdc_task(void); 108 | void webserial_task(void); 109 | 110 | /*------------- MAIN -------------*/ 111 | 112 | pio_spi_inst_t spi = { 113 | .pio = pio1, 114 | .sm = 0 115 | }; 116 | 117 | 118 | int main(void) 119 | { 120 | // Check the state of TEST_PIN 121 | if (is_test_pin_grounded()) { 122 | // GPIO 6 (TEST_PIN) is grounded, update PIN_SOUT and SI_PIN 123 | PIN_SOUT = 3; 124 | SI_PIN = 4; 125 | } 126 | else { 127 | // GPIO 6 (TEST_PIN) is not grounded, use default values 128 | PIN_SOUT = 2; 129 | SI_PIN = 3; 130 | } 131 | 132 | //board_init(); 133 | buf_count = 0; 134 | uint cpha1_prog_offs = pio_add_program(spi.pio, &spi_cpha1_program); 135 | pio_spi_init(spi.pio, spi.sm, cpha1_prog_offs, 8, 4058.838/128, 1, 1, PIN_SCK, PIN_SOUT, PIN_SIN); 136 | 137 | tusb_init(); 138 | 139 | while (1) 140 | { 141 | tud_task(); // tinyusb device task 142 | data_transfer_task(); 143 | cdc_task(); 144 | webserial_task(); 145 | led_blinking_task(); 146 | } 147 | 148 | return 0; 149 | } 150 | 151 | int oldmain(void) 152 | { 153 | board_init(); 154 | 155 | tusb_init(); 156 | 157 | while (1) 158 | { 159 | tud_task(); // tinyusb device task 160 | cdc_task(); 161 | webserial_task(); 162 | led_blinking_task(); 163 | } 164 | 165 | return 0; 166 | } 167 | 168 | // send characters to both CDC and WebUSB 169 | void echo_all(uint8_t buf[], uint32_t count) 170 | { 171 | // echo to web serial 172 | if ( web_serial_connected ) 173 | { 174 | tud_vendor_write(buf, count); 175 | tud_vendor_flush(); 176 | } 177 | 178 | // echo to cdc 179 | if ( tud_cdc_connected() ) 180 | { 181 | for(uint32_t i=0; ibRequest) 233 | { 234 | case VENDOR_REQUEST_WEBUSB: 235 | // match vendor request in BOS descriptor 236 | // Get landing page url 237 | return tud_control_xfer(rhport, request, (void*) &desc_url, desc_url.bLength); 238 | 239 | case VENDOR_REQUEST_MICROSOFT: 240 | if ( request->wIndex == 7 ) 241 | { 242 | // Get Microsoft OS 2.0 compatible descriptor 243 | uint16_t total_len; 244 | memcpy(&total_len, desc_ms_os_20+8, 2); 245 | 246 | return tud_control_xfer(rhport, request, (void*) desc_ms_os_20, total_len); 247 | }else 248 | { 249 | return false; 250 | } 251 | case 0x22: 252 | // Webserial simulate the CDC_REQUEST_SET_CONTROL_LINE_STATE (0x22) to connect and disconnect. 253 | web_serial_connected = (request->wValue != 0); 254 | 255 | total_transferred = 0; 256 | num_bytes_per_transfer = NUM_DEFAULT_BYTES_PER_TRANSFER; 257 | us_between_transfer = US_DEFAULT_PER_TRANSFER; 258 | 259 | // Always lit LED if connected 260 | if ( web_serial_connected ) 261 | { 262 | board_led_write(true); 263 | blink_interval_ms = BLINK_ALWAYS_ON; 264 | 265 | // tud_vendor_write_str("\r\nTinyUSB WebUSB device example\r\n"); 266 | }else 267 | { 268 | blink_interval_ms = BLINK_MOUNTED; 269 | } 270 | 271 | // response with status OK 272 | return tud_control_status(rhport, request); 273 | break; 274 | 275 | default: break; 276 | } 277 | 278 | // stall unknown request 279 | return false; 280 | } 281 | 282 | // Invoked when DATA Stage of VENDOR's request is complete 283 | bool tud_vendor_control_complete_cb(uint8_t rhport, tusb_control_request_t const * request) 284 | { 285 | (void) rhport; 286 | (void) request; 287 | 288 | // nothing to do 289 | return true; 290 | } 291 | 292 | void data_transfer_task(void) { 293 | //if(buf_count) { 294 | //uint8_t buf_out[MAX_TRANSFER_BYTES]; 295 | // for(int i = 0; i < (buf_count+3) >> 2; i++) { 296 | // pio_spi_write8_blocking(&spi, data_buf+(4*i), 4); 297 | // busy_wait_us(36); 298 | // } 299 | //pio_spi_write8_read8_blocking(&spi, data_buf, buf_out, buf_count); 300 | //echo_all(buf_out, buf_count); 301 | // buf_count = 0; 302 | //} 303 | } 304 | 305 | void handle_input_data(uint8_t* buf_in, uint32_t count) { 306 | for(int i = count; i < (MAX_TRANSFER_BYTES*2); i++) 307 | buf_in[i] = 0; 308 | uint8_t processed = 0; 309 | if(count == NUM_CMP_BYTES_RECV) { 310 | uint8_t failed = 0; 311 | for(int i = 0; i < NUM_CMP_BYTES; i++) 312 | if(buf_in[i] != compare_bytes[i]) { 313 | failed = 1; 314 | break; 315 | } 316 | if(!failed) { 317 | us_between_transfer = (buf_in[NUM_CMP_BYTES]<<0) + (buf_in[NUM_CMP_BYTES+1]<<8) + (buf_in[NUM_CMP_BYTES+2]<<16); 318 | num_bytes_per_transfer = buf_in[NUM_CMP_BYTES+3]; 319 | if(num_bytes_per_transfer > MAX_TRANSFER_BYTES) 320 | num_bytes_per_transfer = MAX_TRANSFER_BYTES; 321 | processed = 1; 322 | echo_all(&processed, 1); 323 | } 324 | } 325 | if(!processed) { 326 | // pprintf("Sending: %02x", buf[0]); 327 | uint8_t total_processed = 0; 328 | uint8_t buf_out[MAX_TRANSFER_BYTES*2]; 329 | while(total_processed < count) { 330 | uint8_t transferable = num_bytes_per_transfer; 331 | //if(count-total_processed < transferable) 332 | //transferable = count-total_processed; 333 | pio_spi_write8_read8_blocking(&spi, buf_in + total_processed, buf_out + total_processed, transferable); 334 | total_transferred += transferable; 335 | total_processed += transferable; 336 | busy_wait_us(us_between_transfer); 337 | } 338 | echo_all(buf_out, total_processed); 339 | //echo_all(&availables, 1); 340 | } 341 | } 342 | 343 | void webserial_task(void) 344 | { 345 | if ( web_serial_connected ) 346 | if ( tud_vendor_available() ) { 347 | uint8_t buf_in[MAX_TRANSFER_BYTES*2]; 348 | uint32_t count = tud_vendor_read(buf_in, sizeof(buf_in)); 349 | handle_input_data(buf_in, count); 350 | } 351 | } 352 | 353 | 354 | //--------------------------------------------------------------------+ 355 | // USB CDC 356 | //--------------------------------------------------------------------+ 357 | void cdc_task(void) 358 | { 359 | if ( tud_cdc_connected() ) 360 | // connected and there are data available 361 | if ( tud_cdc_available() ) { 362 | uint8_t buf_in[MAX_TRANSFER_BYTES*2]; 363 | uint32_t count = tud_cdc_read((uint8_t*)buf_in, sizeof(buf_in)); 364 | handle_input_data(buf_in, count); 365 | } 366 | } 367 | 368 | // Invoked when cdc when line state changed e.g connected/disconnected 369 | void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) 370 | { 371 | (void) itf; 372 | 373 | // connected 374 | if ( dtr && rts ) 375 | { 376 | // print initial message when connected 377 | // tud_cdc_write_str("\r\nTinyUSB WebUSB device example\r\n"); 378 | } 379 | } 380 | 381 | // Invoked when CDC interface received data from host 382 | void tud_cdc_rx_cb(uint8_t itf) 383 | { 384 | (void) itf; 385 | } 386 | 387 | //--------------------------------------------------------------------+ 388 | // BLINKING TASK 389 | //--------------------------------------------------------------------+ 390 | void led_blinking_task(void) 391 | { 392 | static uint32_t start_ms = 0; 393 | static bool led_state = false; 394 | 395 | // Blink every interval ms 396 | if ( board_millis() - start_ms < blink_interval_ms) return; // not enough time 397 | start_ms += blink_interval_ms; 398 | 399 | board_led_write(led_state); 400 | led_state = 1 - led_state; // toggle 401 | } 402 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------