diff --git a/.codespell/exclude-file.txt b/.codespell/exclude-file.txt
new file mode 100644
index 000000000..af8265cc7
--- /dev/null
+++ b/.codespell/exclude-file.txt
@@ -0,0 +1 @@
+ return USB0.INTSTS1.BIT.ATTCH ? true : false;
diff --git a/.codespell/ignore-words.txt b/.codespell/ignore-words.txt
new file mode 100644
index 000000000..e37d97a9f
--- /dev/null
+++ b/.codespell/ignore-words.txt
@@ -0,0 +1,8 @@
+synopsys
+sie
+tre
+hsi
+fro
+dout
+mot
+te
diff --git a/.codespellrc b/.codespellrc
new file mode 100644
index 000000000..8c32ed706
--- /dev/null
+++ b/.codespellrc
@@ -0,0 +1,10 @@
+# See: https://github.com/codespell-project/codespell#using-a-config-file
+[codespell]
+# In the event of a false positive, add the problematic word, in all lowercase, to 'ignore-words.txt' (one word per line).
+# Or copy & paste the whole problematic line to 'exclude-file.txt'
+ignore-words = .codespell/ignore-words.txt
+exclude-file = .codespell/exclude-file.txt
+check-filenames =
+check-hidden =
+count =
+skip = .cproject,./.git,./hw/mcu,./lib,./examples/*/*/_build,./examples/*/*/ses,./examples/*/*/ozone,./hw/mcu,./test/unit-test/vendor,./tests_obsolete,./tools/uf2
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.yml
index 2958d3b12..8c39b95e1 100644
--- a/.github/ISSUE_TEMPLATE/bug_report.yml
+++ b/.github/ISSUE_TEMPLATE/bug_report.yml
@@ -64,7 +64,7 @@ body:
placeholder: |
Attach your debug log txt file here, where the issue occurred, best with comments to explain the actual events.
- Note1: Please DO NOT paste your lengthy log contents here since it hurts the readibility.
+ Note1: Please DO NOT paste your lengthy log contents here since it hurts the readability.
Note2: To enable logging, add `LOG=3` to to the make command if building with stock examples or set `CFG_TUSB_DEBUG=3` in your tusb_config.h.
More information can be found at [example's readme](https://github.com/hathach/tinyusb/blob/master/docs/getting_started.md)
validations:
diff --git a/.github/workflows/build_aarch64.yml b/.github/workflows/build_aarch64.yml
index af26f33f2..c552a9574 100644
--- a/.github/workflows/build_aarch64.yml
+++ b/.github/workflows/build_aarch64.yml
@@ -1,11 +1,23 @@
name: Build AArch64
on:
- pull_request:
push:
- release:
- types:
- - created
+ paths:
+ - 'src/**'
+ - 'examples/**'
+ - 'lib/**'
+ - 'hw/**'
+ pull_request:
+ branches: [ master ]
+ paths:
+ - 'src/**'
+ - 'examples/**'
+ - 'lib/**'
+ - 'hw/**'
+
+concurrency:
+ group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
+ cancel-in-progress: true
jobs:
# ---------------------------------------
diff --git a/.github/workflows/build_arm.yml b/.github/workflows/build_arm.yml
index 369048fc6..c16fc0d45 100644
--- a/.github/workflows/build_arm.yml
+++ b/.github/workflows/build_arm.yml
@@ -1,34 +1,25 @@
name: Build ARM
on:
- pull_request:
push:
- release:
- types:
- - created
+ paths:
+ - 'src/**'
+ - 'examples/**'
+ - 'lib/**'
+ - 'hw/**'
+ pull_request:
+ branches: [ master ]
+ paths:
+ - 'src/**'
+ - 'examples/**'
+ - 'lib/**'
+ - 'hw/**'
+
+concurrency:
+ group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
+ cancel-in-progress: true
jobs:
- # ---------------------------------------
- # Unit testing with Ceedling
- # ---------------------------------------
- unit-test:
- runs-on: ubuntu-latest
- steps:
- - name: Setup Ruby
- uses: ruby/setup-ruby@v1
- with:
- ruby-version: '2.7'
-
- - name: Checkout TinyUSB
- uses: actions/checkout@v3
-
- - name: Unit Tests
- run: |
- # Install Ceedling
- gem install ceedling
- cd test
- ceedling test:all
-
# ---------------------------------------
# Build ARM family
# ---------------------------------------
diff --git a/.github/workflows/build_esp.yml b/.github/workflows/build_esp.yml
index 51d91de49..2e2859c95 100644
--- a/.github/workflows/build_esp.yml
+++ b/.github/workflows/build_esp.yml
@@ -1,11 +1,23 @@
name: Build ESP
on:
- pull_request:
push:
- release:
- types:
- - created
+ paths:
+ - 'src/**'
+ - 'examples/**'
+ - 'lib/**'
+ - 'hw/**'
+ pull_request:
+ branches: [ master ]
+ paths:
+ - 'src/**'
+ - 'examples/**'
+ - 'lib/**'
+ - 'hw/**'
+
+concurrency:
+ group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
+ cancel-in-progress: true
jobs:
build-esp:
diff --git a/.github/workflows/build_msp430.yml b/.github/workflows/build_msp430.yml
index 1ef25a6a0..7e1c5b128 100644
--- a/.github/workflows/build_msp430.yml
+++ b/.github/workflows/build_msp430.yml
@@ -1,11 +1,23 @@
name: Build MSP430
on:
- pull_request:
push:
- release:
- types:
- - created
+ paths:
+ - 'src/**'
+ - 'examples/**'
+ - 'lib/**'
+ - 'hw/**'
+ pull_request:
+ branches: [ master ]
+ paths:
+ - 'src/**'
+ - 'examples/**'
+ - 'lib/**'
+ - 'hw/**'
+
+concurrency:
+ group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
+ cancel-in-progress: true
jobs:
build-msp430:
diff --git a/.github/workflows/build_renesas.yml b/.github/workflows/build_renesas.yml
index 2e272c832..d9254dea8 100644
--- a/.github/workflows/build_renesas.yml
+++ b/.github/workflows/build_renesas.yml
@@ -1,11 +1,23 @@
name: Build Renesas
on:
- pull_request:
push:
- release:
- types:
- - created
+ paths:
+ - 'src/**'
+ - 'examples/**'
+ - 'lib/**'
+ - 'hw/**'
+ pull_request:
+ branches: [ master ]
+ paths:
+ - 'src/**'
+ - 'examples/**'
+ - 'lib/**'
+ - 'hw/**'
+
+concurrency:
+ group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
+ cancel-in-progress: true
jobs:
build-rx:
diff --git a/.github/workflows/build_riscv.yml b/.github/workflows/build_riscv.yml
index 61c1c60c5..66f4fd153 100644
--- a/.github/workflows/build_riscv.yml
+++ b/.github/workflows/build_riscv.yml
@@ -1,11 +1,23 @@
name: Build RISC-V
on:
- pull_request:
push:
- release:
- types:
- - created
+ paths:
+ - 'src/**'
+ - 'examples/**'
+ - 'lib/**'
+ - 'hw/**'
+ pull_request:
+ branches: [ master ]
+ paths:
+ - 'src/**'
+ - 'examples/**'
+ - 'lib/**'
+ - 'hw/**'
+
+concurrency:
+ group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
+ cancel-in-progress: true
jobs:
build-riscv:
@@ -15,6 +27,7 @@ jobs:
matrix:
family:
# Alphabetical order
+ - 'ch32v307'
- 'fomu'
- 'gd32vf103'
steps:
diff --git a/.github/workflows/cifuzz.yml b/.github/workflows/cifuzz.yml
new file mode 100644
index 000000000..7314fd9e6
--- /dev/null
+++ b/.github/workflows/cifuzz.yml
@@ -0,0 +1,33 @@
+name: CIFuzz
+on:
+ pull_request:
+ branches:
+ - master
+ paths:
+ - '**.c'
+ - '**.cc'
+ - '**.cpp'
+ - '**.cxx'
+ - '**.h'
+jobs:
+ Fuzzing:
+ runs-on: ubuntu-latest
+ steps:
+ - name: Build Fuzzers
+ id: build
+ uses: google/oss-fuzz/infra/cifuzz/actions/build_fuzzers@master
+ with:
+ oss-fuzz-project-name: 'tinyusb'
+ language: c++
+ - name: Run Fuzzers
+ uses: google/oss-fuzz/infra/cifuzz/actions/run_fuzzers@master
+ with:
+ oss-fuzz-project-name: 'tinyusb'
+ language: c++
+ fuzz-seconds: 600
+ - name: Upload Crash
+ uses: actions/upload-artifact@v3
+ if: failure() && steps.build.outcome == 'success'
+ with:
+ name: artifacts
+ path: ./out/artifacts
diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml
new file mode 100644
index 000000000..d2150d13f
--- /dev/null
+++ b/.github/workflows/pre-commit.yml
@@ -0,0 +1,48 @@
+name: pre-commit
+
+on:
+ push:
+ pull_request:
+ branches: [ master ]
+
+concurrency:
+ group: ${{ github.workflow }}-${{ github.head_ref || github.run_id }}
+ cancel-in-progress: true
+
+jobs:
+ pre-commit:
+ runs-on: ubuntu-latest
+ steps:
+ - name: Setup Python
+ uses: actions/setup-python@v4
+ with:
+ python-version: '3.x'
+
+ - name: Setup Ruby
+ uses: ruby/setup-ruby@v1
+ with:
+ ruby-version: '3.0'
+
+ - name: Checkout TinyUSB
+ uses: actions/checkout@v3
+
+ - name: Run codespell
+ uses: codespell-project/actions-codespell@master
+
+ - name: Run Unit Tests
+ run: |
+ # Install Ceedling
+ gem install ceedling
+ cd test/unit-test
+ ceedling test:all
+
+ - name: Build Fuzzer
+ run: |
+ export CC=clang
+ export CXX=clang++
+ fuzz_harness=$(ls -d test/fuzz/device/*/)
+ for h in $fuzz_harness
+ do
+ make -C $h get-deps
+ make -C $h all
+ done
diff --git a/.github/workflows/test_hardware.yml b/.github/workflows/test_hardware.yml
index 3ef1fdc56..601a97e99 100644
--- a/.github/workflows/test_hardware.yml
+++ b/.github/workflows/test_hardware.yml
@@ -1,10 +1,19 @@
name: Hardware Test
+
on:
- pull_request:
push:
- release:
- types:
- - created
+ paths:
+ - 'src/**'
+ - 'examples/**'
+ - 'lib/**'
+ - 'hw/**'
+ pull_request:
+ branches: [ master ]
+ paths:
+ - 'src/**'
+ - 'examples/**'
+ - 'lib/**'
+ - 'hw/**'
# Hardware in the loop (HIL)
# Current self-hosted instance is running on an EPYC 7232 server hosted by HiFiPhile user
diff --git a/.gitmodules b/.gitmodules
index dcac0d9ab..14f47746b 100644
--- a/.gitmodules
+++ b/.gitmodules
@@ -146,6 +146,9 @@
[submodule "hw/mcu/allwinner"]
path = hw/mcu/allwinner
url = https://github.com/hathach/allwinner_driver.git
+[submodule "hw/mcu/wch/ch32v307"]
+ path = hw/mcu/wch/ch32v307
+ url = https://github.com/openwch/ch32v307.git
[submodule "hw/mcu/raspberry_pi/Pico-PIO-USB"]
path = hw/mcu/raspberry_pi/Pico-PIO-USB
url = https://github.com/sekigon-gonnoc/Pico-PIO-USB.git
diff --git a/README.rst b/README.rst
index 05963a655..8504b92c8 100644
--- a/README.rst
+++ b/README.rst
@@ -54,6 +54,7 @@ The stack supports the following MCUs:
- **ST:** STM32 series: F0, F1, F2, F3, F4, F7, H7, G4, L0, L1, L4, L4+, WB
- **TI:** MSP430, MSP432E4, TM4C123
- **ValentyUSB:** eptri
+- **WCH:** CH32V307
Here is the list of `Supported Devices`_ that can be used with provided examples.
diff --git a/docs/contributing/index.rst b/docs/contributing/index.rst
index c572894ad..7ff79cb32 100644
--- a/docs/contributing/index.rst
+++ b/docs/contributing/index.rst
@@ -6,7 +6,7 @@ Contributing can be highly rewarding, but it can also be frustrating at times.
It takes time to review patches, and as this is an open source project, that
sometimes can take a while. The reviewing process depends on the availability
of the maintainers, who may not be always available. Please try to be
-understanding throught the process.
+understanding through the process.
There a few guidelines you need to keep in mind when contributing. Please have
a look at them as that will make the contribution process easier for all
diff --git a/docs/contributing/porting.rst b/docs/contributing/porting.rst
index 7e9e462f0..710af51c3 100644
--- a/docs/contributing/porting.rst
+++ b/docs/contributing/porting.rst
@@ -195,7 +195,7 @@ Others (like the nRF52) may need each USB packet queued individually. To make th
some state for yourself and queue up an intermediate USB packet from the interrupt handler.
Once the transaction is going, the interrupt handler will notify TinyUSB of transfer completion.
-During transmission, the IN data buffer is guarenteed to remain unchanged in memory until the ``dcd_xfer_complete`` function is called.
+During transmission, the IN data buffer is guaranteed to remain unchanged in memory until the ``dcd_xfer_complete`` function is called.
The dcd_edpt_xfer function must never add zero-length-packets (ZLP) on its own to a transfer. If a ZLP is required,
then it must be explicitly sent by the stack calling dcd_edpt_xfer(), by calling dcd_edpt_xfer() a second time with len=0.
@@ -238,4 +238,4 @@ Use `WireShark `_ or `a Beagle `__
+
+WCH
+---
+
+- `CH32V307V-R1-1v0 `
diff --git a/examples/device/cdc_msc/src/main.c b/examples/device/cdc_msc/src/main.c
index c3666763b..c83299e59 100644
--- a/examples/device/cdc_msc/src/main.c
+++ b/examples/device/cdc_msc/src/main.c
@@ -113,7 +113,7 @@ void cdc_task(void)
// connected and there are data available
if ( tud_cdc_available() )
{
- // read datas
+ // read data
char buf[64];
uint32_t count = tud_cdc_read(buf, sizeof(buf));
(void) count;
diff --git a/examples/device/cdc_msc_freertos/CMakeLists.txt b/examples/device/cdc_msc_freertos/CMakeLists.txt
index 639dde99a..cbd75efd6 100644
--- a/examples/device/cdc_msc_freertos/CMakeLists.txt
+++ b/examples/device/cdc_msc_freertos/CMakeLists.txt
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.5)
# TOP is absolute path to root directory of TinyUSB git repo
-# needed for esp32sx build. TOOD could be removed later on
+# needed for esp32sx build. TODO could be removed later on
set(TOP "../../..")
get_filename_component(TOP "${TOP}" REALPATH)
diff --git a/examples/device/cdc_msc_freertos/Makefile b/examples/device/cdc_msc_freertos/Makefile
index 3352dd37d..ff4b41108 100644
--- a/examples/device/cdc_msc_freertos/Makefile
+++ b/examples/device/cdc_msc_freertos/Makefile
@@ -29,8 +29,15 @@ SRC_C += \
$(FREERTOS_SRC)/timers.c \
$(subst ../../../,,$(wildcard ../../../$(FREERTOS_SRC)/portable/GCC/$(FREERTOS_PORT)/*.c))
-# Suppress FreeRTOS warnings
-CFLAGS += -Wno-error=cast-qual -Wno-error=redundant-decls
+# include heap manage if configSUPPORT_DYNAMIC_ALLOCATION = 1
+# SRC_C += $(FREERTOS_SRC)/portable/MemMang/heap_1.c
+# CFLAGS += -Wno-error=sign-compare
+
+# Suppress FreeRTOSConfig.h warnings
+CFLAGS += -Wno-error=redundant-decls
+
+# Suppress FreeRTOS source warnings
+CFLAGS += -Wno-error=cast-qual
# FreeRTOS (lto + Os) linker issue
LDFLAGS += -Wl,--undefined=vTaskSwitchContext
diff --git a/examples/device/cdc_msc_freertos/skip.txt b/examples/device/cdc_msc_freertos/skip.txt
index 1ee51a9d4..49b8ee57b 100644
--- a/examples/device/cdc_msc_freertos/skip.txt
+++ b/examples/device/cdc_msc_freertos/skip.txt
@@ -1,3 +1,4 @@
+mcu:CH32V307
mcu:CXD56
mcu:F1C100S
mcu:GD32VF103
@@ -8,4 +9,4 @@ mcu:SAMD11
mcu:SAMX7X
mcu:VALENTYUSB_EPTRI
family:broadcom_32bit
-family:broadcom_64bit
\ No newline at end of file
+family:broadcom_64bit
diff --git a/examples/device/cdc_msc_freertos/src/FreeRTOSConfig/FreeRTOSConfig.h b/examples/device/cdc_msc_freertos/src/FreeRTOSConfig/FreeRTOSConfig.h
index 6a3630dbc..968c59749 100644
--- a/examples/device/cdc_msc_freertos/src/FreeRTOSConfig/FreeRTOSConfig.h
+++ b/examples/device/cdc_msc_freertos/src/FreeRTOSConfig/FreeRTOSConfig.h
@@ -69,7 +69,7 @@
#define configTICK_RATE_HZ ( 1000 )
#define configMAX_PRIORITIES ( 5 )
#define configMINIMAL_STACK_SIZE ( 128 )
-#define configTOTAL_HEAP_SIZE ( 0*1024 ) // dynamic is not used
+#define configTOTAL_HEAP_SIZE ( configSUPPORT_DYNAMIC_ALLOCATION*4*1024 )
#define configMAX_TASK_NAME_LEN 16
#define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 1
diff --git a/examples/device/cdc_msc_freertos/src/main.c b/examples/device/cdc_msc_freertos/src/main.c
index 755220c13..cfb14aa2b 100644
--- a/examples/device/cdc_msc_freertos/src/main.c
+++ b/examples/device/cdc_msc_freertos/src/main.c
@@ -51,6 +51,8 @@
#define USBD_STACK_SIZE (3*configMINIMAL_STACK_SIZE/2) * (CFG_TUSB_DEBUG ? 2 : 1)
#endif
+#define CDC_STACK_SZIE configMINIMAL_STACK_SIZE
+
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF PROTYPES
//--------------------------------------------------------------------+
@@ -66,19 +68,18 @@ enum {
BLINK_SUSPENDED = 2500,
};
-// static timer
+// static timer & task
+#if configSUPPORT_STATIC_ALLOCATION
StaticTimer_t blinky_tmdef;
-TimerHandle_t blinky_tm;
-// static task
StackType_t usb_device_stack[USBD_STACK_SIZE];
StaticTask_t usb_device_taskdef;
-// static task for cdc
-#define CDC_STACK_SZIE configMINIMAL_STACK_SIZE
StackType_t cdc_stack[CDC_STACK_SZIE];
StaticTask_t cdc_taskdef;
+#endif
+TimerHandle_t blinky_tm;
void led_blinky_cb(TimerHandle_t xTimer);
void usb_device_task(void* param);
@@ -92,15 +93,22 @@ int main(void)
{
board_init();
+#if configSUPPORT_STATIC_ALLOCATION
// soft timer for blinky
blinky_tm = xTimerCreateStatic(NULL, pdMS_TO_TICKS(BLINK_NOT_MOUNTED), true, NULL, led_blinky_cb, &blinky_tmdef);
- xTimerStart(blinky_tm, 0);
// Create a task for tinyusb device stack
- (void) xTaskCreateStatic( usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);
+ xTaskCreateStatic(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);
// Create CDC task
- (void) xTaskCreateStatic( cdc_task, "cdc", CDC_STACK_SZIE, NULL, configMAX_PRIORITIES-2, cdc_stack, &cdc_taskdef);
+ xTaskCreateStatic(cdc_task, "cdc", CDC_STACK_SZIE, NULL, configMAX_PRIORITIES-2, cdc_stack, &cdc_taskdef);
+#else
+ blinky_tm = xTimerCreate(NULL, pdMS_TO_TICKS(BLINK_NOT_MOUNTED), true, NULL, led_blinky_cb);
+ xTaskCreate( usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, NULL);
+ xTaskCreate( cdc_task, "cdc", CDC_STACK_SZIE, NULL, configMAX_PRIORITIES-2, NULL);
+#endif
+
+ xTimerStart(blinky_tm, 0);
// skip starting scheduler (and return) for ESP32-S2 or ESP32-S3
#if !TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
diff --git a/examples/device/dfu_runtime/src/main.c b/examples/device/dfu_runtime/src/main.c
index 55b380353..bd9a91c27 100644
--- a/examples/device/dfu_runtime/src/main.c
+++ b/examples/device/dfu_runtime/src/main.c
@@ -31,7 +31,7 @@
*
* $ dfu-util -e
*
- * This will send DETTACH command to put device into bootloader. Since this example
+ * This will send DETACH command to put device into bootloader. Since this example
* is minimal, it doesn't actually go into DFU mode but rather change the LED blinking
* pattern to fast rate as indicator.
*/
diff --git a/examples/device/hid_boot_interface/src/main.c b/examples/device/hid_boot_interface/src/main.c
index 71afa3f46..ef072019c 100644
--- a/examples/device/hid_boot_interface/src/main.c
+++ b/examples/device/hid_boot_interface/src/main.c
@@ -157,11 +157,11 @@ void hid_task(void)
{
uint8_t const report_id = 0;
uint8_t const button_mask = 0;
- uint8_t const veritical = 0;
+ uint8_t const vertical = 0;
uint8_t const horizontal = 0;
int8_t const delta = 5;
- tud_hid_n_mouse_report(ITF_NUM_MOUSE, report_id, button_mask, delta, delta, veritical, horizontal);
+ tud_hid_n_mouse_report(ITF_NUM_MOUSE, report_id, button_mask, delta, delta, vertical, horizontal);
}
}
}
@@ -175,7 +175,7 @@ void tud_hid_set_protocol_cb(uint8_t instance, uint8_t protocol)
(void) protocol;
// nothing to do since we use the same compatible boot report for both Boot and Report mode.
- // TOOD set a indicator for user
+ // TODO set a indicator for user
}
// Invoked when sent REPORT successfully to host
diff --git a/examples/device/hid_composite_freertos/Makefile b/examples/device/hid_composite_freertos/Makefile
index 6c8c43ddd..9c66b896d 100644
--- a/examples/device/hid_composite_freertos/Makefile
+++ b/examples/device/hid_composite_freertos/Makefile
@@ -28,8 +28,15 @@ SRC_C += \
$(FREERTOS_SRC)/timers.c \
$(subst ../../../,,$(wildcard ../../../$(FREERTOS_SRC)/portable/GCC/$(FREERTOS_PORT)/*.c))
-# Suppress FreeRTOS warnings
-CFLAGS += -Wno-error=cast-qual -Wno-error=redundant-decls
+# include heap manage if configSUPPORT_DYNAMIC_ALLOCATION = 1
+# SRC_C += $(FREERTOS_SRC)/portable/MemMang/heap_1.c
+# CFLAGS += -Wno-error=sign-compare
+
+# Suppress FreeRTOSConfig.h warnings
+CFLAGS += -Wno-error=redundant-decls
+
+# Suppress FreeRTOS source warnings
+CFLAGS += -Wno-error=cast-qual
# FreeRTOS (lto + Os) linker issue
LDFLAGS += -Wl,--undefined=vTaskSwitchContext
diff --git a/examples/device/hid_composite_freertos/skip.txt b/examples/device/hid_composite_freertos/skip.txt
index 1ee51a9d4..49b8ee57b 100644
--- a/examples/device/hid_composite_freertos/skip.txt
+++ b/examples/device/hid_composite_freertos/skip.txt
@@ -1,3 +1,4 @@
+mcu:CH32V307
mcu:CXD56
mcu:F1C100S
mcu:GD32VF103
@@ -8,4 +9,4 @@ mcu:SAMD11
mcu:SAMX7X
mcu:VALENTYUSB_EPTRI
family:broadcom_32bit
-family:broadcom_64bit
\ No newline at end of file
+family:broadcom_64bit
diff --git a/examples/device/hid_composite_freertos/src/FreeRTOSConfig/FreeRTOSConfig.h b/examples/device/hid_composite_freertos/src/FreeRTOSConfig/FreeRTOSConfig.h
index bfdf1e926..968c59749 100644
--- a/examples/device/hid_composite_freertos/src/FreeRTOSConfig/FreeRTOSConfig.h
+++ b/examples/device/hid_composite_freertos/src/FreeRTOSConfig/FreeRTOSConfig.h
@@ -46,13 +46,14 @@
#include "bsp/board_mcu.h"
#if CFG_TUSB_MCU == OPT_MCU_ESP32S2 || CFG_TUSB_MCU == OPT_MCU_ESP32S3
-#error "ESP32-Sx should use IDF's FreeRTOSConfig.h"
+ #error "ESP32-Sx should use IDF's FreeRTOSConfig.h"
#endif
+// TODO fix later
#if CFG_TUSB_MCU == OPT_MCU_MM32F327X
- // TODO fix/remove later
extern u32 SystemCoreClock;
#else
+ // FIXME cause redundant-decls warnings
extern uint32_t SystemCoreClock;
#endif
@@ -68,7 +69,7 @@
#define configTICK_RATE_HZ ( 1000 )
#define configMAX_PRIORITIES ( 5 )
#define configMINIMAL_STACK_SIZE ( 128 )
-#define configTOTAL_HEAP_SIZE ( 0*1024 ) // dynamic is not used
+#define configTOTAL_HEAP_SIZE ( configSUPPORT_DYNAMIC_ALLOCATION*4*1024 )
#define configMAX_TASK_NAME_LEN 16
#define configUSE_16_BIT_TICKS 0
#define configIDLE_SHOULD_YIELD 1
diff --git a/examples/device/hid_composite_freertos/src/main.c b/examples/device/hid_composite_freertos/src/main.c
index b67c10937..fb0d69258 100644
--- a/examples/device/hid_composite_freertos/src/main.c
+++ b/examples/device/hid_composite_freertos/src/main.c
@@ -53,6 +53,8 @@
#define USBD_STACK_SIZE (3*configMINIMAL_STACK_SIZE/2) * (CFG_TUSB_DEBUG ? 2 : 1)
#endif
+#define HID_STACK_SZIE configMINIMAL_STACK_SIZE
+
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF PROTYPES
//--------------------------------------------------------------------+
@@ -68,19 +70,18 @@ enum {
BLINK_SUSPENDED = 2500,
};
-// static timer
+// static timer & task
+#if configSUPPORT_STATIC_ALLOCATION
StaticTimer_t blinky_tmdef;
-TimerHandle_t blinky_tm;
-// static task
StackType_t usb_device_stack[USBD_STACK_SIZE];
StaticTask_t usb_device_taskdef;
-// static task for hid
-#define HID_STACK_SZIE configMINIMAL_STACK_SIZE
StackType_t hid_stack[HID_STACK_SZIE];
StaticTask_t hid_taskdef;
+#endif
+TimerHandle_t blinky_tm;
void led_blinky_cb(TimerHandle_t xTimer);
void usb_device_task(void* param);
@@ -94,15 +95,22 @@ int main(void)
{
board_init();
+#if configSUPPORT_STATIC_ALLOCATION
// soft timer for blinky
blinky_tm = xTimerCreateStatic(NULL, pdMS_TO_TICKS(BLINK_NOT_MOUNTED), true, NULL, led_blinky_cb, &blinky_tmdef);
- xTimerStart(blinky_tm, 0);
// Create a task for tinyusb device stack
- (void) xTaskCreateStatic( usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);
+ xTaskCreateStatic(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, usb_device_stack, &usb_device_taskdef);
// Create HID task
- (void) xTaskCreateStatic( hid_task, "hid", HID_STACK_SZIE, NULL, configMAX_PRIORITIES-2, hid_stack, &hid_taskdef);
+ xTaskCreateStatic(hid_task, "hid", HID_STACK_SZIE, NULL, configMAX_PRIORITIES-2, hid_stack, &hid_taskdef);
+#else
+ blinky_tm = xTimerCreate(NULL, pdMS_TO_TICKS(BLINK_NOT_MOUNTED), true, NULL, led_blinky_cb);
+ xTaskCreate(usb_device_task, "usbd", USBD_STACK_SIZE, NULL, configMAX_PRIORITIES-1, NULL);
+ xTaskCreate(hid_task, "hid", HID_STACK_SZIE, NULL, configMAX_PRIORITIES-2, NULL);
+#endif
+
+ xTimerStart(blinky_tm, 0);
// skip starting scheduler (and return) for ESP32-S2 or ESP32-S3
#if !TU_CHECK_MCU(OPT_MCU_ESP32S2, OPT_MCU_ESP32S3)
diff --git a/examples/device/hid_generic_inout/hid_test.py b/examples/device/hid_generic_inout/hid_test.py
index 21fd3f421..5bdba9db0 100644
--- a/examples/device/hid_generic_inout/hid_test.py
+++ b/examples/device/hid_generic_inout/hid_test.py
@@ -13,8 +13,8 @@ for vid in USB_VID:
if dev:
while True:
# Get input from console and encode to UTF8 for array of chars.
- # hid generic inout is single report therefore by HIDAPI requirement
- # it must be preceeded with 0x00 as dummy reportID
+ # hid generic in/out is single report therefore by HIDAPI requirement
+ # it must be preceded, with 0x00 as dummy reportID
str_out = b'\x00'
str_out += input("Send text to HID Device : ").encode('utf-8')
dev.write(str_out)
diff --git a/examples/host/cdc_msc_hid/CMakeLists.txt b/examples/host/cdc_msc_hid/CMakeLists.txt
index d84457bc9..7af6b738e 100644
--- a/examples/host/cdc_msc_hid/CMakeLists.txt
+++ b/examples/host/cdc_msc_hid/CMakeLists.txt
@@ -14,6 +14,7 @@ add_executable(${PROJECT})
# Example source
target_sources(${PROJECT} PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/cdc_app.c
${CMAKE_CURRENT_SOURCE_DIR}/src/hid_app.c
${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
${CMAKE_CURRENT_SOURCE_DIR}/src/msc_app.c
diff --git a/examples/host/cdc_msc_hid/Makefile b/examples/host/cdc_msc_hid/Makefile
index ad28076a0..9adccfa3a 100644
--- a/examples/host/cdc_msc_hid/Makefile
+++ b/examples/host/cdc_msc_hid/Makefile
@@ -7,7 +7,8 @@ INC += \
# Example source
EXAMPLE_SOURCE = \
- src/hid_app.c \
+ src/cdc_app.c \
+ src/hid_app.c \
src/main.c \
src/msc_app.c \
diff --git a/examples/host/cdc_msc_hid/src/cdc_app.c b/examples/host/cdc_msc_hid/src/cdc_app.c
new file mode 100644
index 000000000..b1b137e0e
--- /dev/null
+++ b/examples/host/cdc_msc_hid/src/cdc_app.c
@@ -0,0 +1,113 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022, Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+
+#include "tusb.h"
+#include "bsp/board.h"
+
+//--------------------------------------------------------------------+
+// MACRO TYPEDEF CONSTANT ENUM DECLARATION
+//--------------------------------------------------------------------+
+
+
+//------------- IMPLEMENTATION -------------//
+
+size_t get_console_inputs(uint8_t* buf, size_t bufsize)
+{
+ size_t count = 0;
+ while (count < bufsize)
+ {
+ int ch = board_getchar();
+ if ( ch <= 0 ) break;
+
+ buf[count] = (uint8_t) ch;
+ count++;
+ }
+
+ return count;
+}
+
+void cdc_app_task(void)
+{
+ uint8_t buf[64+1]; // +1 for extra null character
+ uint32_t const bufsize = sizeof(buf)-1;
+
+ uint32_t count = get_console_inputs(buf, bufsize);
+ buf[count] = 0;
+
+ // loop over all mounted interfaces
+ for(uint8_t idx=0; idx cdc interfaces
+ if (count)
+ {
+ tuh_cdc_write(idx, buf, count);
+ tuh_cdc_write_flush(idx);
+ }
+ }
+ }
+}
+
+// Invoked when received new data
+void tuh_cdc_rx_cb(uint8_t idx)
+{
+ uint8_t buf[64+1]; // +1 for extra null character
+ uint32_t const bufsize = sizeof(buf)-1;
+
+ // forward cdc interfaces -> console
+ uint32_t count = tuh_cdc_read(idx, buf, bufsize);
+ buf[count] = 0;
+
+ printf((char*) buf);
+}
+
+void tuh_cdc_mount_cb(uint8_t idx)
+{
+ tuh_cdc_itf_info_t itf_info = { 0 };
+ tuh_cdc_itf_get_info(idx, &itf_info);
+
+ printf("CDC Interface is mounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
+
+#ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
+ // CFG_TUH_CDC_LINE_CODING_ON_ENUM must be defined for line coding is set by tinyusb in enumeration
+ // otherwise you need to call tuh_cdc_set_line_coding() first
+ cdc_line_coding_t line_coding = { 0 };
+ if ( tuh_cdc_get_local_line_coding(idx, &line_coding) )
+ {
+ printf(" Baudrate: %lu, Stop Bits : %u\r\n", line_coding.bit_rate, line_coding.stop_bits);
+ printf(" Parity : %u, Data Width: %u\r\n", line_coding.parity , line_coding.data_bits);
+ }
+#endif
+}
+
+void tuh_cdc_umount_cb(uint8_t idx)
+{
+ tuh_cdc_itf_info_t itf_info = { 0 };
+ tuh_cdc_itf_get_info(idx, &itf_info);
+
+ printf("CDC Interface is unmounted: address = %u, itf_num = %u\r\n", itf_info.daddr, itf_info.bInterfaceNumber);
+}
diff --git a/examples/host/cdc_msc_hid/src/hid_app.c b/examples/host/cdc_msc_hid/src/hid_app.c
index 11437c2b4..ed53c502d 100644
--- a/examples/host/cdc_msc_hid/src/hid_app.c
+++ b/examples/host/cdc_msc_hid/src/hid_app.c
@@ -247,7 +247,7 @@ static void process_generic_report(uint8_t dev_addr, uint8_t instance, uint8_t c
// Composite report, 1st byte is report ID, data starts from 2nd byte
uint8_t const rpt_id = report[0];
- // Find report id in the arrray
+ // Find report id in the array
for(uint8_t i=0; i
+
+
+#define UART_RINGBUFFER_SIZE_TX 64
+#define UART_RINGBUFFER_MASK_TX (UART_RINGBUFFER_SIZE_TX-1)
+
+static char tx_buf[UART_RINGBUFFER_SIZE_TX];
+static unsigned int tx_produce;
+static volatile unsigned int tx_consume;
+
+void USART1_IRQHandler(void) __attribute__((naked));
+void USART1_IRQHandler(void) {
+ __asm volatile ("call USART1_IRQHandler_impl; mret");
+}
+
+__attribute__((used)) void USART1_IRQHandler_impl(void)
+{
+ if(USART_GetITStatus(USART1, USART_IT_TC) != RESET)
+ {
+ USART_ClearITPendingBit(USART1, USART_IT_TC);
+
+ if(tx_consume != tx_produce) {
+ USART_SendData(USART1, tx_buf[tx_consume]);
+ tx_consume = (tx_consume + 1) & UART_RINGBUFFER_MASK_TX;
+ }
+ }
+
+}
+
+void uart_write(char c)
+{
+ unsigned int tx_produce_next = (tx_produce + 1) & UART_RINGBUFFER_MASK_TX;
+
+ NVIC_DisableIRQ(USART1_IRQn);
+ if((tx_consume != tx_produce) || (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)) {
+ tx_buf[tx_produce] = c;
+ tx_produce = tx_produce_next;
+ } else {
+ USART_SendData(USART1, c);
+ }
+ NVIC_EnableIRQ(USART1_IRQn);
+}
+
+
+void uart_sync(void)
+{
+ while(tx_consume != tx_produce);
+}
+
+
+void usart_printf_init(uint32_t baudrate)
+{
+ GPIO_InitTypeDef GPIO_InitStructure;
+ USART_InitTypeDef USART_InitStructure;
+
+ tx_produce = 0;
+ tx_consume = 0;
+
+ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
+
+ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
+ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
+ GPIO_Init(GPIOA, &GPIO_InitStructure);
+
+ USART_InitStructure.USART_BaudRate = baudrate;
+ USART_InitStructure.USART_WordLength = USART_WordLength_8b;
+ USART_InitStructure.USART_StopBits = USART_StopBits_1;
+ USART_InitStructure.USART_Parity = USART_Parity_No;
+ USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
+ USART_InitStructure.USART_Mode = USART_Mode_Tx;
+
+ USART_Init(USART1, &USART_InitStructure);
+ USART_ITConfig(USART1, USART_IT_TC, ENABLE);
+ USART_Cmd(USART1, ENABLE);
+
+ NVIC_InitTypeDef NVIC_InitStructure = { 0 };
+ NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
+ NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
+ NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
+ NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
+ NVIC_Init(&NVIC_InitStructure);
+}
diff --git a/hw/bsp/ch32v307/boards/ch32v307v-r1-1v0/debug_uart.h b/hw/bsp/ch32v307/boards/ch32v307v-r1-1v0/debug_uart.h
new file mode 100644
index 000000000..a7e070585
--- /dev/null
+++ b/hw/bsp/ch32v307/boards/ch32v307v-r1-1v0/debug_uart.h
@@ -0,0 +1,31 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Greg Davill
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+
+#include
+
+void uart_write(char c);
+void uart_sync(void);
+void usart_printf_init(uint32_t baudrate);
diff --git a/hw/bsp/ch32v307/ch32v307.ld b/hw/bsp/ch32v307/ch32v307.ld
new file mode 100644
index 000000000..af7c06615
--- /dev/null
+++ b/hw/bsp/ch32v307/ch32v307.ld
@@ -0,0 +1,170 @@
+ENTRY( _start )
+
+__stack_size = 4096;
+
+PROVIDE( _stack_size = __stack_size );
+
+
+MEMORY
+{
+ FLASH (rx) : ORIGIN = 0x00000000, LENGTH = 288K
+ RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 32K
+}
+
+
+SECTIONS
+{
+
+ .init :
+ {
+ _sinit = .;
+ . = ALIGN(4);
+ KEEP(*(SORT_NONE(.init)))
+ . = ALIGN(4);
+ _einit = .;
+ } >FLASH AT>FLASH
+
+ .vector :
+ {
+ *(.vector);
+ . = ALIGN(64);
+ } >FLASH AT>FLASH
+
+ .text :
+ {
+ . = ALIGN(4);
+ *(.text)
+ *(.text.*)
+ *(.rodata)
+ *(.rodata*)
+ *(.glue_7)
+ *(.glue_7t)
+ *(.gnu.linkonce.t.*)
+ . = ALIGN(4);
+ } >FLASH AT>FLASH
+
+ .fini :
+ {
+ KEEP(*(SORT_NONE(.fini)))
+ . = ALIGN(4);
+ } >FLASH AT>FLASH
+
+ PROVIDE( _etext = . );
+ PROVIDE( _eitcm = . );
+
+ .preinit_array :
+ {
+ PROVIDE_HIDDEN (__preinit_array_start = .);
+ KEEP (*(.preinit_array))
+ PROVIDE_HIDDEN (__preinit_array_end = .);
+ } >FLASH AT>FLASH
+
+ .init_array :
+ {
+ PROVIDE_HIDDEN (__init_array_start = .);
+ KEEP (*(SORT_BY_INIT_PRIORITY(.init_array.*) SORT_BY_INIT_PRIORITY(.ctors.*)))
+ KEEP (*(.init_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .ctors))
+ PROVIDE_HIDDEN (__init_array_end = .);
+ } >FLASH AT>FLASH
+
+ .fini_array :
+ {
+ PROVIDE_HIDDEN (__fini_array_start = .);
+ KEEP (*(SORT_BY_INIT_PRIORITY(.fini_array.*) SORT_BY_INIT_PRIORITY(.dtors.*)))
+ KEEP (*(.fini_array EXCLUDE_FILE (*crtbegin.o *crtbegin?.o *crtend.o *crtend?.o ) .dtors))
+ PROVIDE_HIDDEN (__fini_array_end = .);
+ } >FLASH AT>FLASH
+
+ .ctors :
+ {
+ /* gcc uses crtbegin.o to find the start of
+ the constructors, so we make sure it is
+ first. Because this is a wildcard, it
+ doesn't matter if the user does not
+ actually link against crtbegin.o; the
+ linker won't look for a file to match a
+ wildcard. The wildcard also means that it
+ doesn't matter which directory crtbegin.o
+ is in. */
+ KEEP (*crtbegin.o(.ctors))
+ KEEP (*crtbegin?.o(.ctors))
+ /* We don't want to include the .ctor section from
+ the crtend.o file until after the sorted ctors.
+ The .ctor section from the crtend file contains the
+ end of ctors marker and it must be last */
+ KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .ctors))
+ KEEP (*(SORT(.ctors.*)))
+ KEEP (*(.ctors))
+ } >FLASH AT>FLASH
+
+ .dtors :
+ {
+ KEEP (*crtbegin.o(.dtors))
+ KEEP (*crtbegin?.o(.dtors))
+ KEEP (*(EXCLUDE_FILE (*crtend.o *crtend?.o ) .dtors))
+ KEEP (*(SORT(.dtors.*)))
+ KEEP (*(.dtors))
+ } >FLASH AT>FLASH
+
+ .dalign :
+ {
+ . = ALIGN(4);
+ PROVIDE(_data_vma = .);
+ } >RAM AT>FLASH
+
+ .dlalign :
+ {
+ . = ALIGN(4);
+ PROVIDE(_data_lma = .);
+ } >FLASH AT>FLASH
+
+ .data :
+ {
+ *(.gnu.linkonce.r.*)
+ *(.data .data.*)
+ *(.gnu.linkonce.d.*)
+ . = ALIGN(8);
+ PROVIDE( __global_pointer$ = . + 0x800 );
+ *(.sdata .sdata.*)
+ *(.sdata2.*)
+ *(.gnu.linkonce.s.*)
+ . = ALIGN(8);
+ *(.srodata.cst16)
+ *(.srodata.cst8)
+ *(.srodata.cst4)
+ *(.srodata.cst2)
+ *(.srodata .srodata.*)
+ . = ALIGN(4);
+ PROVIDE( _edata = .);
+ } >RAM AT>FLASH
+
+ .bss :
+ {
+ . = ALIGN(4);
+ PROVIDE( _sbss = .);
+ *(.sbss*)
+ *(.gnu.linkonce.sb.*)
+ *(.bss*)
+ *(.gnu.linkonce.b.*)
+ *(COMMON*)
+ . = ALIGN(4);
+ PROVIDE( _ebss = .);
+ } >RAM AT>FLASH
+
+ PROVIDE( _end = _ebss);
+ PROVIDE( end = . );
+
+ .stack ORIGIN(RAM) + LENGTH(RAM) - __stack_size :
+ {
+ PROVIDE( _heap_end = . );
+ . = ALIGN(4);
+ PROVIDE(_susrstack = . );
+ . = . + __stack_size;
+ PROVIDE( _eusrstack = .);
+ __freertos_irq_stack_top = .;
+ } >RAM
+
+}
+
+
+
diff --git a/hw/bsp/ch32v307/ch32v30x_conf.h b/hw/bsp/ch32v307/ch32v30x_conf.h
new file mode 100644
index 000000000..399feaf68
--- /dev/null
+++ b/hw/bsp/ch32v307/ch32v30x_conf.h
@@ -0,0 +1,43 @@
+/********************************** (C) COPYRIGHT *******************************
+* File Name : ch32v30x_conf.h
+* Author : WCH
+* Version : V1.0.0
+* Date : 2021/06/06
+* Description : Library configuration file.
+* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
+* SPDX-License-Identifier: Apache-2.0
+*******************************************************************************/
+#ifndef __CH32V30x_CONF_H
+#define __CH32V30x_CONF_H
+
+#include "ch32v30x_adc.h"
+#include "ch32v30x_bkp.h"
+#include "ch32v30x_can.h"
+#include "ch32v30x_crc.h"
+#include "ch32v30x_dac.h"
+#include "ch32v30x_dbgmcu.h"
+#include "ch32v30x_dma.h"
+#include "ch32v30x_exti.h"
+#include "ch32v30x_flash.h"
+#include "ch32v30x_fsmc.h"
+#include "ch32v30x_gpio.h"
+#include "ch32v30x_i2c.h"
+#include "ch32v30x_iwdg.h"
+#include "ch32v30x_pwr.h"
+#include "ch32v30x_rcc.h"
+#include "ch32v30x_rtc.h"
+#include "ch32v30x_sdio.h"
+#include "ch32v30x_spi.h"
+#include "ch32v30x_tim.h"
+#include "ch32v30x_usart.h"
+#include "ch32v30x_wwdg.h"
+#include "ch32v30x_it.h"
+#include "ch32v30x_misc.h"
+
+
+#endif /* __CH32V30x_CONF_H */
+
+
+
+
+
diff --git a/hw/bsp/ch32v307/ch32v30x_it.c b/hw/bsp/ch32v307/ch32v30x_it.c
new file mode 100644
index 000000000..d348dd989
--- /dev/null
+++ b/hw/bsp/ch32v307/ch32v30x_it.c
@@ -0,0 +1,49 @@
+/********************************** (C) COPYRIGHT *******************************
+* File Name : ch32v30x_it.c
+* Author : WCH
+* Version : V1.0.0
+* Date : 2021/06/06
+* Description : Main Interrupt Service Routines.
+* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
+* SPDX-License-Identifier: Apache-2.0
+*******************************************************************************/
+#include "ch32v30x_it.h"
+
+void NMI_Handler(void) __attribute__((naked));
+void HardFault_Handler(void) __attribute__((naked));
+
+/*********************************************************************
+ * @fn NMI_Handler
+ *
+ * @brief This function handles NMI exception.
+ *
+ * @return none
+ */
+void NMI_Handle(void){
+ __asm volatile ("call NMI_Handler_impl; mret");
+}
+
+__attribute__((used)) void NMI_Handler_impl(void)
+{
+
+}
+
+/*********************************************************************
+ * @fn HardFault_Handler
+ *
+ * @brief This function handles Hard Fault exception.
+ *
+ * @return none
+ */
+void HardFault_Handler(void){
+ __asm volatile ("call HardFault_Handler_impl; mret");
+}
+
+__attribute__((used)) void HardFault_Handler_impl(void)
+{
+ while (1)
+ {
+ }
+}
+
+
diff --git a/hw/bsp/ch32v307/ch32v30x_it.h b/hw/bsp/ch32v307/ch32v30x_it.h
new file mode 100644
index 000000000..42f285edf
--- /dev/null
+++ b/hw/bsp/ch32v307/ch32v30x_it.h
@@ -0,0 +1,18 @@
+/********************************** (C) COPYRIGHT *******************************
+* File Name : ch32v30x_it.h
+* Author : WCH
+* Version : V1.0.0
+* Date : 2021/06/06
+* Description : This file contains the headers of the interrupt handlers.
+* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
+* SPDX-License-Identifier: Apache-2.0
+*******************************************************************************/
+#ifndef __CH32V30x_IT_H
+#define __CH32V30x_IT_H
+
+// #include "debug.h"
+
+
+#endif /* __CH32V30x_IT_H */
+
+
diff --git a/hw/bsp/ch32v307/core_riscv.h b/hw/bsp/ch32v307/core_riscv.h
new file mode 100644
index 000000000..2e94ec683
--- /dev/null
+++ b/hw/bsp/ch32v307/core_riscv.h
@@ -0,0 +1,384 @@
+/********************************** (C) COPYRIGHT *******************************
+* File Name : core_riscv.h
+* Author : WCH
+* Version : V1.0.0
+* Date : 2021/06/06
+* Description : RISC-V Core Peripheral Access Layer Header File for CH32V30x
+* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
+* SPDX-License-Identifier: Apache-2.0
+*******************************************************************************/
+#ifndef __CORE_RISCV_H__
+#define __CORE_RISCV_H__
+
+/* IO definitions */
+#ifdef __cplusplus
+ #define __I volatile /* defines 'read only' permissions */
+#else
+ #define __I volatile const /* defines 'read only' permissions */
+#endif
+#define __O volatile /* defines 'write only' permissions */
+#define __IO volatile /* defines 'read / write' permissions */
+
+/* Standard Peripheral Library old types (maintained for legacy purpose) */
+typedef __I uint64_t vuc64; /* Read Only */
+typedef __I uint32_t vuc32; /* Read Only */
+typedef __I uint16_t vuc16; /* Read Only */
+typedef __I uint8_t vuc8; /* Read Only */
+
+typedef const uint64_t uc64; /* Read Only */
+typedef const uint32_t uc32; /* Read Only */
+typedef const uint16_t uc16; /* Read Only */
+typedef const uint8_t uc8; /* Read Only */
+
+typedef __I int64_t vsc64; /* Read Only */
+typedef __I int32_t vsc32; /* Read Only */
+typedef __I int16_t vsc16; /* Read Only */
+typedef __I int8_t vsc8; /* Read Only */
+
+typedef const int64_t sc64; /* Read Only */
+typedef const int32_t sc32; /* Read Only */
+typedef const int16_t sc16; /* Read Only */
+typedef const int8_t sc8; /* Read Only */
+
+typedef __IO uint64_t vu64;
+typedef __IO uint32_t vu32;
+typedef __IO uint16_t vu16;
+typedef __IO uint8_t vu8;
+
+typedef uint64_t u64;
+typedef uint32_t u32;
+typedef uint16_t u16;
+typedef uint8_t u8;
+
+typedef __IO int64_t vs64;
+typedef __IO int32_t vs32;
+typedef __IO int16_t vs16;
+typedef __IO int8_t vs8;
+
+typedef int64_t s64;
+typedef int32_t s32;
+typedef int16_t s16;
+typedef int8_t s8;
+
+typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus;
+
+typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
+
+typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;
+
+#define RV_STATIC_INLINE static inline
+
+/* memory mapped structure for Program Fast Interrupt Controller (PFIC) */
+typedef struct{
+ __I uint32_t ISR[8];
+ __I uint32_t IPR[8];
+ __IO uint32_t ITHRESDR;
+ __IO uint32_t RESERVED;
+ __IO uint32_t CFGR;
+ __I uint32_t GISR;
+ uint8_t VTFIDR[4];
+ uint8_t RESERVED0[12];
+ __IO uint32_t VTFADDR[4];
+ uint8_t RESERVED1[0x90];
+ __O uint32_t IENR[8];
+ uint8_t RESERVED2[0x60];
+ __O uint32_t IRER[8];
+ uint8_t RESERVED3[0x60];
+ __O uint32_t IPSR[8];
+ uint8_t RESERVED4[0x60];
+ __O uint32_t IPRR[8];
+ uint8_t RESERVED5[0x60];
+ __IO uint32_t IACTR[8];
+ uint8_t RESERVED6[0xE0];
+ __IO uint8_t IPRIOR[256];
+ uint8_t RESERVED7[0x810];
+ __IO uint32_t SCTLR;
+}PFIC_Type;
+
+/* memory mapped structure for SysTick */
+typedef struct
+{
+ __IO u32 CTLR;
+ __IO u32 SR;
+ __IO u64 CNT;
+ __IO u64 CMP;
+}SysTick_Type;
+
+
+#define PFIC ((PFIC_Type *) 0xE000E000 )
+#define NVIC PFIC
+#define NVIC_KEY1 ((uint32_t)0xFA050000)
+#define NVIC_KEY2 ((uint32_t)0xBCAF0000)
+#define NVIC_KEY3 ((uint32_t)0xBEEF0000)
+
+#define SysTick ((SysTick_Type *) 0xE000F000)
+
+
+/*********************************************************************
+ * @fn __enable_irq
+ *
+ * @brief Enable Global Interrupt
+ *
+ * @return none
+ */
+RV_STATIC_INLINE void __enable_irq(void)
+{
+ __asm volatile ("csrw 0x800, %0" : : "r" (0x6088) );
+}
+
+/*********************************************************************
+ * @fn __disable_irq
+ *
+ * @brief Disable Global Interrupt
+ *
+ * @return none
+ */
+RV_STATIC_INLINE void __disable_irq(void)
+{
+ __asm volatile ("csrw 0x800, %0" : : "r" (0x6000) );
+}
+
+/*********************************************************************
+ * @fn __NOP
+ *
+ * @brief nop
+ *
+ * @return none
+ */
+RV_STATIC_INLINE void __NOP(void)
+{
+ __asm volatile ("nop");
+}
+
+/*********************************************************************
+ * @fn NVIC_EnableIRQ
+ *
+ * @brief Enable Interrupt
+ *
+ * @param IRQn: Interrupt Numbers
+ *
+ * @return none
+ */
+RV_STATIC_INLINE void NVIC_EnableIRQ(IRQn_Type IRQn)
+{
+ NVIC->IENR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F));
+}
+
+/*********************************************************************
+ * @fn NVIC_DisableIRQ
+ *
+ * @brief Disable Interrupt
+ *
+ * @param IRQn: Interrupt Numbers
+ *
+ * @return none
+ */
+RV_STATIC_INLINE void NVIC_DisableIRQ(IRQn_Type IRQn)
+{
+ NVIC->IRER[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F));
+}
+
+/*********************************************************************
+ * @fn NVIC_GetStatusIRQ
+ *
+ * @brief Get Interrupt Enable State
+ *
+ * @param IRQn: Interrupt Numbers
+ *
+ * @return 1 - Interrupt Enable
+ * 0 - Interrupt Disable
+ */
+RV_STATIC_INLINE uint32_t NVIC_GetStatusIRQ(IRQn_Type IRQn)
+{
+ return((uint32_t) ((NVIC->ISR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0));
+}
+
+/*********************************************************************
+ * @fn NVIC_GetPendingIRQ
+ *
+ * @brief Get Interrupt Pending State
+ *
+ * @param IRQn: Interrupt Numbers
+ *
+ * @return 1 - Interrupt Pending Enable
+ * 0 - Interrupt Pending Disable
+ */
+RV_STATIC_INLINE uint32_t NVIC_GetPendingIRQ(IRQn_Type IRQn)
+{
+ return((uint32_t) ((NVIC->IPR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0));
+}
+
+/*********************************************************************
+ * @fn NVIC_SetPendingIRQ
+ *
+ * @brief Set Interrupt Pending
+ *
+ * @param IRQn: Interrupt Numbers
+ *
+ * @return None
+ */
+RV_STATIC_INLINE void NVIC_SetPendingIRQ(IRQn_Type IRQn)
+{
+ NVIC->IPSR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F));
+}
+
+/*********************************************************************
+ * @fn NVIC_ClearPendingIRQ
+ *
+ * @brief Clear Interrupt Pending
+ *
+ * @param IRQn: Interrupt Numbers
+ *
+ * @return None
+ */
+RV_STATIC_INLINE void NVIC_ClearPendingIRQ(IRQn_Type IRQn)
+{
+ NVIC->IPRR[((uint32_t)(IRQn) >> 5)] = (1 << ((uint32_t)(IRQn) & 0x1F));
+}
+
+/*********************************************************************
+ * @fn NVIC_GetActive
+ *
+ * @brief Get Interrupt Active State
+ *
+ * @param IRQn: Interrupt Numbers
+ *
+ * @return 1 - Interrupt Active
+ * 0 - Interrupt No Active
+ */
+RV_STATIC_INLINE uint32_t NVIC_GetActive(IRQn_Type IRQn)
+{
+ return((uint32_t)((NVIC->IACTR[(uint32_t)(IRQn) >> 5] & (1 << ((uint32_t)(IRQn) & 0x1F)))?1:0));
+}
+
+/*********************************************************************
+ * @fn NVIC_SetPriority
+ *
+ * @brief Set Interrupt Priority
+ *
+ * @param IRQn - Interrupt Numbers
+ * priority -
+ * bit7 - pre-emption priority
+ * bit6~bit4 - subpriority
+ * @return None
+ */
+RV_STATIC_INLINE void NVIC_SetPriority(IRQn_Type IRQn, uint8_t priority)
+{
+ NVIC->IPRIOR[(uint32_t)(IRQn)] = priority;
+}
+
+/*********************************************************************
+ * @fn __WFI
+ *
+ * @brief Wait for Interrupt
+ *
+ * @return None
+ */
+__attribute__( ( always_inline ) ) RV_STATIC_INLINE void __WFI(void)
+{
+ NVIC->SCTLR &= ~(1<<3); // wfi
+ asm volatile ("wfi");
+}
+
+/*********************************************************************
+ * @fn __WFE
+ *
+ * @brief Wait for Events
+ *
+ * @return None
+ */
+__attribute__( ( always_inline ) ) RV_STATIC_INLINE void __WFE(void)
+{
+ uint32_t t;
+
+ t = NVIC->SCTLR;
+ NVIC->SCTLR |= (1<<3)|(1<<5); // (wfi->wfe)+(__sev)
+ NVIC->SCTLR = (NVIC->SCTLR & ~(1<<5)) | ( t & (1<<5));
+ asm volatile ("wfi");
+ asm volatile ("wfi");
+}
+
+/*********************************************************************
+ * @fn SetVTFIRQ
+ *
+ * @brief Set VTF Interrupt
+ *
+ * @param add - VTF interrupt service function base address.
+ * IRQn -Interrupt Numbers
+ * num - VTF Interrupt Numbers
+ * NewState - DISABLE or ENABLE
+ * @return None
+ */
+RV_STATIC_INLINE void SetVTFIRQ(uint32_t addr, IRQn_Type IRQn, uint8_t num, FunctionalState NewState){
+ if(num > 3) return ;
+
+ if (NewState != DISABLE)
+ {
+ NVIC->VTFIDR[num] = IRQn;
+ NVIC->VTFADDR[num] = ((addr&0xFFFFFFFE)|0x1);
+ }
+ else{
+ NVIC->VTFIDR[num] = IRQn;
+ NVIC->VTFADDR[num] = ((addr&0xFFFFFFFE)&(~0x1));
+ }
+}
+
+/*********************************************************************
+ * @fn NVIC_SystemReset
+ *
+ * @brief Initiate a system reset request
+ *
+ * @return None
+ */
+RV_STATIC_INLINE void NVIC_SystemReset(void)
+{
+ NVIC->CFGR = NVIC_KEY3|(1<<7);
+}
+
+
+/* Core_Exported_Functions */
+extern uint32_t __get_FFLAGS(void);
+extern void __set_FFLAGS(uint32_t value);
+extern uint32_t __get_FRM(void);
+extern void __set_FRM(uint32_t value);
+extern uint32_t __get_FCSR(void);
+extern void __set_FCSR(uint32_t value);
+extern uint32_t __get_MSTATUS(void);
+extern void __set_MSTATUS(uint32_t value);
+extern uint32_t __get_MISA(void);
+extern void __set_MISA(uint32_t value);
+extern uint32_t __get_MIE(void);
+extern void __set_MIE(uint32_t value);
+extern uint32_t __get_MTVEC(void);
+extern void __set_MTVEC(uint32_t value);
+extern uint32_t __get_MSCRATCH(void);
+extern void __set_MSCRATCH(uint32_t value);
+extern uint32_t __get_MEPC(void);
+extern void __set_MEPC(uint32_t value);
+extern uint32_t __get_MCAUSE(void);
+extern void __set_MCAUSE(uint32_t value);
+extern uint32_t __get_MTVAL(void);
+extern void __set_MTVAL(uint32_t value);
+extern uint32_t __get_MIP(void);
+extern void __set_MIP(uint32_t value);
+extern uint32_t __get_MCYCLE(void);
+extern void __set_MCYCLE(uint32_t value);
+extern uint32_t __get_MCYCLEH(void);
+extern void __set_MCYCLEH(uint32_t value);
+extern uint32_t __get_MINSTRET(void);
+extern void __set_MINSTRET(uint32_t value);
+extern uint32_t __get_MINSTRETH(void);
+extern void __set_MINSTRETH(uint32_t value);
+extern uint32_t __get_MVENDORID(void);
+extern uint32_t __get_MARCHID(void);
+extern uint32_t __get_MIMPID(void);
+extern uint32_t __get_MHARTID(void);
+extern uint32_t __get_SP(void);
+
+
+#endif
+
+
+
+
+
diff --git a/hw/bsp/ch32v307/family.c b/hw/bsp/ch32v307/family.c
new file mode 100644
index 000000000..d5602c7b4
--- /dev/null
+++ b/hw/bsp/ch32v307/family.c
@@ -0,0 +1,178 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Greg Davill
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+
+#include "stdio.h"
+#include "debug_uart.h"
+#include "ch32v30x.h"
+
+#include "bsp/board.h"
+#include "board.h"
+
+//--------------------------------------------------------------------+
+// Forward USB interrupt events to TinyUSB IRQ Handler
+//--------------------------------------------------------------------+
+
+void USBHS_IRQHandler (void) __attribute__((naked));
+void USBHS_IRQHandler (void)
+{
+ __asm volatile ("call USBHS_IRQHandler_impl; mret");
+}
+
+__attribute__ ((used)) void USBHS_IRQHandler_impl (void)
+{
+ tud_int_handler(0);
+}
+
+//--------------------------------------------------------------------+
+// MACRO TYPEDEF CONSTANT ENUM
+//--------------------------------------------------------------------+
+
+uint32_t SysTick_Config(uint32_t ticks)
+{
+ NVIC_EnableIRQ(SysTicK_IRQn);
+ SysTick->CTLR=0;
+ SysTick->SR=0;
+ SysTick->CNT=0;
+ SysTick->CMP=ticks-1;
+ SysTick->CTLR=0xF;
+ return 0;
+}
+
+void board_init(void) {
+
+ /* Disable interrupts during init */
+ __disable_irq();
+
+#if CFG_TUSB_OS == OPT_OS_NONE
+ SysTick_Config(SystemCoreClock / 1000);
+#endif
+
+ usart_printf_init(115200);
+
+ RCC_USBCLK48MConfig(RCC_USBCLK48MCLKSource_USBPHY);
+ RCC_USBHSPLLCLKConfig(RCC_HSBHSPLLCLKSource_HSE);
+ RCC_USBHSConfig(RCC_USBPLL_Div2);
+ RCC_USBHSPLLCKREFCLKConfig(RCC_USBHSPLLCKREFCLK_4M);
+ RCC_USBHSPHYPLLALIVEcmd(ENABLE);
+ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_USBHS, ENABLE);
+
+ GPIO_InitTypeDef GPIO_InitStructure = {0};
+
+ // LED
+ LED_CLOCK_EN();
+ GPIO_InitStructure.GPIO_Pin = LED_PIN;
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
+ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
+ GPIO_Init(LED_PORT, &GPIO_InitStructure);
+
+ // Button
+ BUTTON_CLOCK_EN();
+ GPIO_InitStructure.GPIO_Pin = BUTTON_PIN;
+ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
+ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
+ GPIO_Init(BUTTON_PORT, &GPIO_InitStructure);
+
+ /* Enable interrupts globally */
+ __enable_irq();
+
+ board_delay(2);
+}
+
+#if CFG_TUSB_OS == OPT_OS_NONE
+
+volatile uint32_t system_ticks = 0;
+
+/* Small workaround to support HW stack save/restore */
+void SysTick_Handler (void) __attribute__((naked));
+void SysTick_Handler (void)
+{
+ __asm volatile ("call SysTick_Handler_impl; mret");
+}
+
+__attribute__((used)) void SysTick_Handler_impl (void)
+{
+ SysTick->SR = 0;
+ system_ticks++;
+}
+
+uint32_t board_millis (void)
+{
+ return system_ticks;
+}
+
+#endif
+
+//--------------------------------------------------------------------+
+// Board porting API
+//--------------------------------------------------------------------+
+
+void board_led_write (bool state)
+{
+ GPIO_WriteBit(LED_PORT, LED_PIN, state);
+}
+
+uint32_t board_button_read (void)
+{
+ return BUTTON_STATE_ACTIVE == GPIO_ReadInputDataBit(BUTTON_PORT, BUTTON_PIN);
+}
+
+int board_uart_read (uint8_t *buf, int len)
+{
+ (void) buf;
+ (void) len;
+ return 0;
+}
+
+int board_uart_write (void const *buf, int len)
+{
+ int txsize = len;
+ while ( txsize-- )
+ {
+ uart_write(*(uint8_t const*) buf);
+ buf++;
+ }
+ return len;
+}
+
+
+
+#ifdef USE_FULL_ASSERT
+/**
+ * @brief Reports the name of the source file and the source line number
+ * where the assert_param error has occurred.
+ * @param file: pointer to the source file name
+ * @param line: assert_param error line source number
+ * @retval None
+ */
+void assert_failed(char* file, uint32_t line) {
+ /* USER CODE BEGIN 6 */
+ /* User can add his own implementation to report the file name and line
+ number,
+ tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line)
+ */
+ /* USER CODE END 6 */
+}
+#endif /* USE_FULL_ASSERT */
diff --git a/hw/bsp/ch32v307/family.mk b/hw/bsp/ch32v307/family.mk
new file mode 100644
index 000000000..15f6724a8
--- /dev/null
+++ b/hw/bsp/ch32v307/family.mk
@@ -0,0 +1,64 @@
+# https://www.embecosm.com/resources/tool-chain-downloads/#riscv-stable
+#CROSS_COMPILE ?= riscv32-unknown-elf-
+
+# Toolchain from https://github.com/xpack-dev-tools/riscv-none-embed-gcc-xpack
+CROSS_COMPILE ?= riscv-none-embed-
+
+# Submodules
+CH32V307_SDK = hw/mcu/wch/ch32v307
+DEPS_SUBMODULES += $(CH32V307_SDK)
+
+# WCH-SDK paths
+CH32V307_SDK_SRC = $(CH32V307_SDK)/EVT/EXAM/SRC
+
+include $(TOP)/$(BOARD_PATH)/board.mk
+
+CFLAGS += \
+ -flto \
+ -march=rv32imac \
+ -mabi=ilp32 \
+ -msmall-data-limit=8 \
+ -mno-save-restore -Os \
+ -fmessage-length=0 \
+ -fsigned-char \
+ -ffunction-sections \
+ -fdata-sections \
+ -nostdlib -nostartfiles \
+ -DCFG_TUSB_MCU=OPT_MCU_CH32V307 \
+ -Xlinker --gc-sections \
+ -DBOARD_TUD_MAX_SPEED=OPT_MODE_HIGH_SPEED
+
+SRC_C += \
+ src/portable/wch/ch32v307/dcd_usbhs.c \
+ $(CH32V307_SDK_SRC)/Core/core_riscv.c \
+ $(CH32V307_SDK_SRC)/Peripheral/src/ch32v30x_gpio.c \
+ $(CH32V307_SDK_SRC)/Peripheral/src/ch32v30x_misc.c \
+ $(CH32V307_SDK_SRC)/Peripheral/src/ch32v30x_rcc.c \
+ $(CH32V307_SDK_SRC)/Peripheral/src/ch32v30x_usart.c
+
+SRC_S += \
+ $(CH32V307_SDK_SRC)/Startup/startup_ch32v30x_D8C.S
+
+INC += \
+ $(TOP)/$(BOARD_PATH) \
+ $(TOP)/$(CH32V307_SDK_SRC)/Peripheral/inc
+
+# For freeRTOS port source
+FREERTOS_PORT = RISC-V
+
+# wch-link is not supported yet in official openOCD yet. We need to either use
+# 1. download openocd as part of mounriver studio http://www.mounriver.com/download or
+# 2. compiled from modified source https://github.com/kprasadvnsi/riscv-openocd-wch
+#
+# Note: For Linux, somehow openocd in mounriver studio does not seem to have wch-link enable,
+# therefore we need to compile it from source as follows:
+# git clone https://github.com/kprasadvnsi/riscv-openocd-wch
+# cd riscv-openocd-wch
+# ./bootstrap
+# ./configure CFLAGS="-Wno-error" --enable-wlink
+# make
+# openocd binaries will be generated in riscv-openocd-wch/src
+
+# flash target ROM bootloader
+flash: $(BUILD)/$(PROJECT).elf
+ openocd -f $(TOP)/$(FAMILY_PATH)/wch-riscv.cfg -c init -c halt -c "program $<" -c wlink_reset_resume -c exit
diff --git a/hw/bsp/ch32v307/system_ch32v30x.c b/hw/bsp/ch32v307/system_ch32v30x.c
new file mode 100644
index 000000000..12b18d7b8
--- /dev/null
+++ b/hw/bsp/ch32v307/system_ch32v30x.c
@@ -0,0 +1,776 @@
+/********************************** (C) COPYRIGHT *******************************
+* File Name : system_ch32v30x.c
+* Author : WCH
+* Version : V1.0.0
+* Date : 2021/06/06
+* Description : CH32V30x Device Peripheral Access Layer System Source File.
+* For HSE = 8Mhz
+* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
+* SPDX-License-Identifier: Apache-2.0
+*********************************************************************************/
+#include "ch32v30x.h"
+
+/*
+* Uncomment the line corresponding to the desired System clock (SYSCLK) frequency (after
+* reset the HSI is used as SYSCLK source).
+* If none of the define below is enabled, the HSI is used as System clock source.
+*/
+// #define SYSCLK_FREQ_HSE HSE_VALUE
+/* #define SYSCLK_FREQ_24MHz 24000000 */
+//#define SYSCLK_FREQ_48MHz 48000000
+/* #define SYSCLK_FREQ_56MHz 56000000 */
+//#define SYSCLK_FREQ_72MHz 72000000
+//#define SYSCLK_FREQ_96MHz 96000000
+//#define SYSCLK_FREQ_120MHz 120000000
+#define SYSCLK_FREQ_144MHz 144000000
+
+/* Clock Definitions */
+#ifdef SYSCLK_FREQ_HSE
+ uint32_t SystemCoreClock = SYSCLK_FREQ_HSE; /* System Clock Frequency (Core Clock) */
+#elif defined SYSCLK_FREQ_24MHz
+ uint32_t SystemCoreClock = SYSCLK_FREQ_24MHz; /* System Clock Frequency (Core Clock) */
+#elif defined SYSCLK_FREQ_48MHz
+ uint32_t SystemCoreClock = SYSCLK_FREQ_48MHz; /* System Clock Frequency (Core Clock) */
+#elif defined SYSCLK_FREQ_56MHz
+ uint32_t SystemCoreClock = SYSCLK_FREQ_56MHz; /* System Clock Frequency (Core Clock) */
+#elif defined SYSCLK_FREQ_72MHz
+ uint32_t SystemCoreClock = SYSCLK_FREQ_72MHz; /* System Clock Frequency (Core Clock) */
+
+#elif defined SYSCLK_FREQ_96MHz
+ uint32_t SystemCoreClock = SYSCLK_FREQ_96MHz; /* System Clock Frequency (Core Clock) */
+#elif defined SYSCLK_FREQ_120MHz
+ uint32_t SystemCoreClock = SYSCLK_FREQ_120MHz; /* System Clock Frequency (Core Clock) */
+#elif defined SYSCLK_FREQ_144MHz
+ uint32_t SystemCoreClock = SYSCLK_FREQ_144MHz; /* System Clock Frequency (Core Clock) */
+
+#else /* HSI Selected as System Clock source */
+ uint32_t SystemCoreClock = HSI_VALUE; /* System Clock Frequency (Core Clock) */
+#endif
+
+__I uint8_t AHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9};
+
+
+/* system_private_function_proto_types */
+static void SetSysClock(void);
+
+#ifdef SYSCLK_FREQ_HSE
+ static void SetSysClockToHSE(void);
+#elif defined SYSCLK_FREQ_24MHz
+ static void SetSysClockTo24(void);
+#elif defined SYSCLK_FREQ_48MHz
+ static void SetSysClockTo48(void);
+#elif defined SYSCLK_FREQ_56MHz
+ static void SetSysClockTo56(void);
+#elif defined SYSCLK_FREQ_72MHz
+ static void SetSysClockTo72(void);
+
+#elif defined SYSCLK_FREQ_96MHz
+ static void SetSysClockTo96(void);
+#elif defined SYSCLK_FREQ_120MHz
+ static void SetSysClockTo120(void);
+#elif defined SYSCLK_FREQ_144MHz
+ static void SetSysClockTo144(void);
+
+#endif
+
+
+/*********************************************************************
+ * @fn SystemInit
+ *
+ * @brief Setup the microcontroller system Initialize the Embedded Flash Interface,
+ * the PLL and update the SystemCoreClock variable.
+ *
+ * @return none
+ */
+void SystemInit (void)
+{
+ RCC->CTLR |= (uint32_t)0x00000001;
+
+#ifdef CH32V30x_D8C
+ RCC->CFGR0 &= (uint32_t)0xF8FF0000;
+#else
+ RCC->CFGR0 &= (uint32_t)0xF0FF0000;
+#endif
+
+ RCC->CTLR &= (uint32_t)0xFEF6FFFF;
+ RCC->CTLR &= (uint32_t)0xFFFBFFFF;
+ RCC->CFGR0 &= (uint32_t)0xFF80FFFF;
+
+#ifdef CH32V30x_D8C
+ RCC->CTLR &= (uint32_t)0xEBFFFFFF;
+ RCC->INTR = 0x00FF0000;
+ RCC->CFGR2 = 0x00000000;
+#else
+ RCC->INTR = 0x009F0000;
+#endif
+ SetSysClock();
+}
+
+/*********************************************************************
+ * @fn SystemCoreClockUpdate
+ *
+ * @brief Update SystemCoreClock variable according to Clock Register Values.
+ *
+ * @return none
+ */
+void SystemCoreClockUpdate (void)
+{
+ uint32_t tmp = 0, pllmull = 0, pllsource = 0, Pll_6_5 = 0;
+
+ tmp = RCC->CFGR0 & RCC_SWS;
+
+ switch (tmp)
+ {
+ case 0x00:
+ SystemCoreClock = HSI_VALUE;
+ break;
+ case 0x04:
+ SystemCoreClock = HSE_VALUE;
+ break;
+ case 0x08:
+ pllmull = RCC->CFGR0 & RCC_PLLMULL;
+ pllsource = RCC->CFGR0 & RCC_PLLSRC;
+ pllmull = ( pllmull >> 18) + 2;
+
+#ifdef CH32V30x_D8
+ if(pllmull == 17) pllmull = 18;
+#else
+ if(pllmull == 2) pllmull = 18;
+ if(pllmull == 15){
+ pllmull = 13; /* *6.5 */
+ Pll_6_5 = 1;
+ }
+ if(pllmull == 16) pllmull = 15;
+ if(pllmull == 17) pllmull = 16;
+#endif
+
+ if (pllsource == 0x00)
+ {
+ SystemCoreClock = (HSI_VALUE >> 1) * pllmull;
+ }
+ else
+ {
+ if ((RCC->CFGR0 & RCC_PLLXTPRE) != (uint32_t)RESET)
+ {
+ SystemCoreClock = (HSE_VALUE >> 1) * pllmull;
+ }
+ else
+ {
+ SystemCoreClock = HSE_VALUE * pllmull;
+ }
+ }
+
+ if(Pll_6_5 == 1) SystemCoreClock = (SystemCoreClock / 2);
+
+ break;
+ default:
+ SystemCoreClock = HSI_VALUE;
+ break;
+ }
+
+ tmp = AHBPrescTable[((RCC->CFGR0 & RCC_HPRE) >> 4)];
+ SystemCoreClock >>= tmp;
+}
+
+/*********************************************************************
+ * @fn SetSysClock
+ *
+ * @brief Configures the System clock frequency, HCLK, PCLK2 and PCLK1 prescalers.
+ *
+ * @return none
+ */
+static void SetSysClock(void)
+{
+#ifdef SYSCLK_FREQ_HSE
+ SetSysClockToHSE();
+#elif defined SYSCLK_FREQ_24MHz
+ SetSysClockTo24();
+#elif defined SYSCLK_FREQ_48MHz
+ SetSysClockTo48();
+#elif defined SYSCLK_FREQ_56MHz
+ SetSysClockTo56();
+#elif defined SYSCLK_FREQ_72MHz
+ SetSysClockTo72();
+#elif defined SYSCLK_FREQ_96MHz
+ SetSysClockTo96();
+#elif defined SYSCLK_FREQ_120MHz
+ SetSysClockTo120();
+#elif defined SYSCLK_FREQ_144MHz
+ SetSysClockTo144();
+
+#endif
+
+ /* If none of the define above is enabled, the HSI is used as System clock
+ * source (default after reset)
+ */
+}
+
+
+#ifdef SYSCLK_FREQ_HSE
+
+/*********************************************************************
+ * @fn SetSysClockToHSE
+ *
+ * @brief Sets HSE as System clock source and configure HCLK, PCLK2 and PCLK1 prescalers.
+ *
+ * @return none
+ */
+static void SetSysClockToHSE(void)
+{
+ __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
+
+ RCC->CTLR |= ((uint32_t)RCC_HSEON);
+
+ /* Wait till HSE is ready and if Time out is reached exit */
+ do
+ {
+ HSEStatus = RCC->CTLR & RCC_HSERDY;
+ StartUpCounter++;
+ } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
+
+ if ((RCC->CTLR & RCC_HSERDY) != RESET)
+ {
+ HSEStatus = (uint32_t)0x01;
+ }
+ else
+ {
+ HSEStatus = (uint32_t)0x00;
+ }
+
+ if (HSEStatus == (uint32_t)0x01)
+ {
+ /* HCLK = SYSCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
+ /* PCLK2 = HCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
+ /* PCLK1 = HCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV1;
+
+ /* Select HSE as system clock source */
+ RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
+ RCC->CFGR0 |= (uint32_t)RCC_SW_HSE;
+
+ /* Wait till HSE is used as system clock source */
+ while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x04)
+ {
+ }
+ }
+ else
+ {
+ /* If HSE fails to start-up, the application will have wrong clock
+ * configuration. User can add here some code to deal with this error
+ */
+ }
+}
+
+#elif defined SYSCLK_FREQ_24MHz
+
+/*********************************************************************
+ * @fn SetSysClockTo24
+ *
+ * @brief Sets System clock frequency to 24MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
+ *
+ * @return none
+ */
+static void SetSysClockTo24(void)
+{
+ __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
+
+ RCC->CTLR |= ((uint32_t)RCC_HSEON);
+
+ /* Wait till HSE is ready and if Time out is reached exit */
+ do
+ {
+ HSEStatus = RCC->CTLR & RCC_HSERDY;
+ StartUpCounter++;
+ } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
+
+ if ((RCC->CTLR & RCC_HSERDY) != RESET)
+ {
+ HSEStatus = (uint32_t)0x01;
+ }
+ else
+ {
+ HSEStatus = (uint32_t)0x00;
+ }
+ if (HSEStatus == (uint32_t)0x01)
+ {
+ /* HCLK = SYSCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
+ /* PCLK2 = HCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
+ /* PCLK1 = HCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV1;
+
+ RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
+
+#ifdef CH32V30x_D8
+ RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL3);
+#else
+ RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL3_EXTEN);
+#endif
+ /* Enable PLL */
+ RCC->CTLR |= RCC_PLLON;
+
+ /* Wait till PLL is ready */
+ while((RCC->CTLR & RCC_PLLRDY) == 0)
+ {
+ }
+ /* Select PLL as system clock source */
+ RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
+ RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
+ /* Wait till PLL is used as system clock source */
+ while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
+ {
+ }
+ }
+ else
+ {
+ /* If HSE fails to start-up, the application will have wrong clock
+ * configuration. User can add here some code to deal with this error
+ */
+ }
+}
+
+#elif defined SYSCLK_FREQ_48MHz
+
+/*********************************************************************
+ * @fn SetSysClockTo48
+ *
+ * @brief Sets System clock frequency to 48MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
+ *
+ * @return none
+ */
+static void SetSysClockTo48(void)
+{
+ __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
+
+
+ RCC->CTLR |= ((uint32_t)RCC_HSEON);
+ /* Wait till HSE is ready and if Time out is reached exit */
+ do
+ {
+ HSEStatus = RCC->CTLR & RCC_HSERDY;
+ StartUpCounter++;
+ } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
+
+ if ((RCC->CTLR & RCC_HSERDY) != RESET)
+ {
+ HSEStatus = (uint32_t)0x01;
+ }
+ else
+ {
+ HSEStatus = (uint32_t)0x00;
+ }
+
+ if (HSEStatus == (uint32_t)0x01)
+ {
+ /* HCLK = SYSCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
+ /* PCLK2 = HCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
+ /* PCLK1 = HCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
+
+ /* PLL configuration: PLLCLK = HSE * 6 = 48 MHz */
+ RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
+
+#ifdef CH32V30x_D8
+ RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL6);
+#else
+ RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL6_EXTEN);
+#endif
+
+ /* Enable PLL */
+ RCC->CTLR |= RCC_PLLON;
+ /* Wait till PLL is ready */
+ while((RCC->CTLR & RCC_PLLRDY) == 0)
+ {
+ }
+ /* Select PLL as system clock source */
+ RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
+ RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
+ /* Wait till PLL is used as system clock source */
+ while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
+ {
+ }
+ }
+ else
+ {
+ /*
+ * If HSE fails to start-up, the application will have wrong clock
+ * configuration. User can add here some code to deal with this error
+ */
+ }
+}
+
+#elif defined SYSCLK_FREQ_56MHz
+
+/*********************************************************************
+ * @fn SetSysClockTo56
+ *
+ * @brief Sets System clock frequency to 56MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
+ *
+ * @return none
+ */
+static void SetSysClockTo56(void)
+{
+ __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
+
+ RCC->CTLR |= ((uint32_t)RCC_HSEON);
+
+ /* Wait till HSE is ready and if Time out is reached exit */
+ do
+ {
+ HSEStatus = RCC->CTLR & RCC_HSERDY;
+ StartUpCounter++;
+ } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
+
+ if ((RCC->CTLR & RCC_HSERDY) != RESET)
+ {
+ HSEStatus = (uint32_t)0x01;
+ }
+ else
+ {
+ HSEStatus = (uint32_t)0x00;
+ }
+
+ if (HSEStatus == (uint32_t)0x01)
+ {
+ /* HCLK = SYSCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
+ /* PCLK2 = HCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
+ /* PCLK1 = HCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
+
+ /* PLL configuration: PLLCLK = HSE * 7 = 56 MHz */
+ RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE | RCC_PLLMULL));
+
+#ifdef CH32V30x_D8
+ RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL7);
+#else
+ RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL7_EXTEN);
+#endif
+
+ /* Enable PLL */
+ RCC->CTLR |= RCC_PLLON;
+ /* Wait till PLL is ready */
+ while((RCC->CTLR & RCC_PLLRDY) == 0)
+ {
+ }
+
+ /* Select PLL as system clock source */
+ RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
+ RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
+ /* Wait till PLL is used as system clock source */
+ while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
+ {
+ }
+ }
+ else
+ {
+ /*
+ * If HSE fails to start-up, the application will have wrong clock
+ * configuration. User can add here some code to deal with this error
+ */
+ }
+}
+
+#elif defined SYSCLK_FREQ_72MHz
+
+/*********************************************************************
+ * @fn SetSysClockTo72
+ *
+ * @brief Sets System clock frequency to 72MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
+ *
+ * @return none
+ */
+static void SetSysClockTo72(void)
+{
+ __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
+
+ RCC->CTLR |= ((uint32_t)RCC_HSEON);
+
+ /* Wait till HSE is ready and if Time out is reached exit */
+ do
+ {
+ HSEStatus = RCC->CTLR & RCC_HSERDY;
+ StartUpCounter++;
+ } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
+
+ if ((RCC->CTLR & RCC_HSERDY) != RESET)
+ {
+ HSEStatus = (uint32_t)0x01;
+ }
+ else
+ {
+ HSEStatus = (uint32_t)0x00;
+ }
+
+ if (HSEStatus == (uint32_t)0x01)
+ {
+ /* HCLK = SYSCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
+ /* PCLK2 = HCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
+ /* PCLK1 = HCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
+
+ /* PLL configuration: PLLCLK = HSE * 9 = 72 MHz */
+ RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
+ RCC_PLLMULL));
+
+#ifdef CH32V30x_D8
+ RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL9);
+#else
+ RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL9_EXTEN);
+#endif
+
+ /* Enable PLL */
+ RCC->CTLR |= RCC_PLLON;
+ /* Wait till PLL is ready */
+ while((RCC->CTLR & RCC_PLLRDY) == 0)
+ {
+ }
+ /* Select PLL as system clock source */
+ RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
+ RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
+ /* Wait till PLL is used as system clock source */
+ while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
+ {
+ }
+ }
+ else
+ {
+ /*
+ * If HSE fails to start-up, the application will have wrong clock
+ * configuration. User can add here some code to deal with this error
+ */
+ }
+}
+
+
+#elif defined SYSCLK_FREQ_96MHz
+
+/*********************************************************************
+ * @fn SetSysClockTo96
+ *
+ * @brief Sets System clock frequency to 96MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
+ *
+ * @return none
+ */
+static void SetSysClockTo96(void)
+{
+ __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
+
+ RCC->CTLR |= ((uint32_t)RCC_HSEON);
+
+ /* Wait till HSE is ready and if Time out is reached exit */
+ do
+ {
+ HSEStatus = RCC->CTLR & RCC_HSERDY;
+ StartUpCounter++;
+ } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
+
+ if ((RCC->CTLR & RCC_HSERDY) != RESET)
+ {
+ HSEStatus = (uint32_t)0x01;
+ }
+ else
+ {
+ HSEStatus = (uint32_t)0x00;
+ }
+
+ if (HSEStatus == (uint32_t)0x01)
+ {
+ /* HCLK = SYSCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
+ /* PCLK2 = HCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
+ /* PCLK1 = HCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
+
+ /* PLL configuration: PLLCLK = HSE * 12 = 96 MHz */
+ RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
+ RCC_PLLMULL));
+
+#ifdef CH32V30x_D8
+ RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL12);
+#else
+ RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL12_EXTEN);
+#endif
+
+ /* Enable PLL */
+ RCC->CTLR |= RCC_PLLON;
+ /* Wait till PLL is ready */
+ while((RCC->CTLR & RCC_PLLRDY) == 0)
+ {
+ }
+ /* Select PLL as system clock source */
+ RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
+ RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
+ /* Wait till PLL is used as system clock source */
+ while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
+ {
+ }
+ }
+ else
+ {
+ /*
+ * If HSE fails to start-up, the application will have wrong clock
+ * configuration. User can add here some code to deal with this error
+ */
+ }
+}
+
+
+#elif defined SYSCLK_FREQ_120MHz
+
+/*********************************************************************
+ * @fn SetSysClockTo120
+ *
+ * @brief Sets System clock frequency to 120MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
+ *
+ * @return none
+ */
+static void SetSysClockTo120(void)
+{
+ __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
+
+ RCC->CTLR |= ((uint32_t)RCC_HSEON);
+
+ /* Wait till HSE is ready and if Time out is reached exit */
+ do
+ {
+ HSEStatus = RCC->CTLR & RCC_HSERDY;
+ StartUpCounter++;
+ } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
+
+ if ((RCC->CTLR & RCC_HSERDY) != RESET)
+ {
+ HSEStatus = (uint32_t)0x01;
+ }
+ else
+ {
+ HSEStatus = (uint32_t)0x00;
+ }
+
+ if (HSEStatus == (uint32_t)0x01)
+ {
+ /* HCLK = SYSCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
+ /* PCLK2 = HCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
+ /* PCLK1 = HCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
+
+ /* PLL configuration: PLLCLK = HSE * 15 = 120 MHz */
+ RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
+ RCC_PLLMULL));
+
+#ifdef CH32V30x_D8
+ RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL15);
+#else
+ RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL15_EXTEN);
+#endif
+
+ /* Enable PLL */
+ RCC->CTLR |= RCC_PLLON;
+ /* Wait till PLL is ready */
+ while((RCC->CTLR & RCC_PLLRDY) == 0)
+ {
+ }
+ /* Select PLL as system clock source */
+ RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
+ RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
+ /* Wait till PLL is used as system clock source */
+ while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
+ {
+ }
+ }
+ else
+ {
+ /*
+ * If HSE fails to start-up, the application will have wrong clock
+ * configuration. User can add here some code to deal with this error
+ */
+ }
+}
+
+
+#elif defined SYSCLK_FREQ_144MHz
+
+/*********************************************************************
+ * @fn SetSysClockTo144
+ *
+ * @brief Sets System clock frequency to 144MHz and configure HCLK, PCLK2 and PCLK1 prescalers.
+ *
+ * @return none
+ */
+static void SetSysClockTo144(void)
+{
+ __IO uint32_t StartUpCounter = 0, HSEStatus = 0;
+
+ RCC->CTLR |= ((uint32_t)RCC_HSEON);
+
+ /* Wait till HSE is ready and if Time out is reached exit */
+ do
+ {
+ HSEStatus = RCC->CTLR & RCC_HSERDY;
+ StartUpCounter++;
+ } while((HSEStatus == 0) && (StartUpCounter != HSE_STARTUP_TIMEOUT));
+
+ if ((RCC->CTLR & RCC_HSERDY) != RESET)
+ {
+ HSEStatus = (uint32_t)0x01;
+ }
+ else
+ {
+ HSEStatus = (uint32_t)0x00;
+ }
+
+ if (HSEStatus == (uint32_t)0x01)
+ {
+ /* HCLK = SYSCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_HPRE_DIV1;
+ /* PCLK2 = HCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_PPRE2_DIV1;
+ /* PCLK1 = HCLK */
+ RCC->CFGR0 |= (uint32_t)RCC_PPRE1_DIV2;
+
+ /* PLL configuration: PLLCLK = HSE * 18 = 144 MHz */
+ RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_PLLSRC | RCC_PLLXTPRE |
+ RCC_PLLMULL));
+
+#ifdef CH32V30x_D8
+ RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL18);
+#else
+ RCC->CFGR0 |= (uint32_t)(RCC_PLLSRC_HSE | RCC_PLLXTPRE_HSE | RCC_PLLMULL18_EXTEN);
+#endif
+
+ /* Enable PLL */
+ RCC->CTLR |= RCC_PLLON;
+ /* Wait till PLL is ready */
+ while((RCC->CTLR & RCC_PLLRDY) == 0)
+ {
+ }
+ /* Select PLL as system clock source */
+ RCC->CFGR0 &= (uint32_t)((uint32_t)~(RCC_SW));
+ RCC->CFGR0 |= (uint32_t)RCC_SW_PLL;
+ /* Wait till PLL is used as system clock source */
+ while ((RCC->CFGR0 & (uint32_t)RCC_SWS) != (uint32_t)0x08)
+ {
+ }
+ }
+ else
+ {
+ /*
+ * If HSE fails to start-up, the application will have wrong clock
+ * configuration. User can add here some code to deal with this error
+ */
+ }
+}
+
+
+#endif
diff --git a/hw/bsp/ch32v307/system_ch32v30x.h b/hw/bsp/ch32v307/system_ch32v30x.h
new file mode 100644
index 000000000..0e0ef4e51
--- /dev/null
+++ b/hw/bsp/ch32v307/system_ch32v30x.h
@@ -0,0 +1,30 @@
+/********************************** (C) COPYRIGHT *******************************
+* File Name : system_ch32v30x.h
+* Author : WCH
+* Version : V1.0.0
+* Date : 2021/06/06
+* Description : CH32V30x Device Peripheral Access Layer System Header File.
+* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
+* SPDX-License-Identifier: Apache-2.0
+*******************************************************************************/
+#ifndef __SYSTEM_CH32V30x_H
+#define __SYSTEM_CH32V30x_H
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+extern uint32_t SystemCoreClock; /* System Clock Frequency (Core Clock) */
+
+/* System_Exported_Functions */
+extern void SystemInit(void);
+extern void SystemCoreClockUpdate(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /*__CH32V30x_SYSTEM_H */
+
+
+
diff --git a/hw/bsp/ch32v307/wch-riscv.cfg b/hw/bsp/ch32v307/wch-riscv.cfg
new file mode 100644
index 000000000..0d24d16ca
--- /dev/null
+++ b/hw/bsp/ch32v307/wch-riscv.cfg
@@ -0,0 +1,15 @@
+#interface wlink
+adapter driver wlink
+wlink_set
+set _CHIPNAME riscv
+jtag newtap $_CHIPNAME cpu -irlen 5 -expected-id 0x00001
+
+set _TARGETNAME $_CHIPNAME.cpu
+
+target create $_TARGETNAME.0 riscv -chain-position $_TARGETNAME
+$_TARGETNAME.0 configure -work-area-phys 0x80000000 -work-area-size 10000 -work-area-backup 1
+set _FLASHNAME $_CHIPNAME.flash
+
+flash bank $_FLASHNAME wch_riscv 0x00000000 0 0 0 $_TARGETNAME.0
+
+echo "Ready for Remote Connections"
diff --git a/hw/bsp/esp32s2/components/led_strip/src/led_strip_rmt_ws2812.c b/hw/bsp/esp32s2/components/led_strip/src/led_strip_rmt_ws2812.c
index 025d3c590..fd1746cad 100644
--- a/hw/bsp/esp32s2/components/led_strip/src/led_strip_rmt_ws2812.c
+++ b/hw/bsp/esp32s2/components/led_strip/src/led_strip_rmt_ws2812.c
@@ -50,7 +50,7 @@ typedef struct {
} ws2812_t;
/**
- * @brief Conver RGB data to RMT format.
+ * @brief Convert RGB data to RMT format.
*
* @note For WS2812, R,G,B each contains 256 different choices (i.e. uint8_t)
*
diff --git a/hw/bsp/esp32s3/components/led_strip/src/led_strip_rmt_ws2812.c b/hw/bsp/esp32s3/components/led_strip/src/led_strip_rmt_ws2812.c
index 025d3c590..fd1746cad 100644
--- a/hw/bsp/esp32s3/components/led_strip/src/led_strip_rmt_ws2812.c
+++ b/hw/bsp/esp32s3/components/led_strip/src/led_strip_rmt_ws2812.c
@@ -50,7 +50,7 @@ typedef struct {
} ws2812_t;
/**
- * @brief Conver RGB data to RMT format.
+ * @brief Convert RGB data to RMT format.
*
* @note For WS2812, R,G,B each contains 256 different choices (i.e. uint8_t)
*
diff --git a/hw/bsp/family_support.cmake b/hw/bsp/family_support.cmake
index c5311b63f..601cd54f5 100644
--- a/hw/bsp/family_support.cmake
+++ b/hw/bsp/family_support.cmake
@@ -123,12 +123,12 @@ if (NOT TARGET _family_support_marker)
# configure an executable target to link to tinyusb in device mode, and add the board implementation
function(family_configure_device_example TARGET)
- # default implentation is empty, the function should be redefined in the FAMILY/family.cmake
+ # default implementation is empty, the function should be redefined in the FAMILY/family.cmake
endfunction()
# configure an executable target to link to tinyusb in host mode, and add the board implementation
function(family_configure_host_example TARGET)
- # default implentation is empty, the function should be redefined in the FAMILY/family.cmake
+ # default implementation is empty, the function should be redefined in the FAMILY/family.cmake
endfunction()
include(${CMAKE_CURRENT_LIST_DIR}/${FAMILY}/family.cmake)
diff --git a/hw/bsp/gd32vf103/family.c b/hw/bsp/gd32vf103/family.c
index c20732302..60a326d27 100644
--- a/hw/bsp/gd32vf103/family.c
+++ b/hw/bsp/gd32vf103/family.c
@@ -112,7 +112,7 @@ void board_init(void) {
otg_core_regs->GCCFG &= ~GCCFG_VBUSIG;
#endif
- /* Enable interrupts globaly */
+ /* Enable interrupts globally */
__enable_irq();
}
@@ -120,7 +120,7 @@ void gd32vf103_reset(void) {
/* The MTIMER unit of the GD32VF103 doesn't have the MSFRST
* register to generate a software reset request.
* BUT instead two undocumented registers in the debug peripheral
- * that allow issueing a software reset.
+ * that allow issuing a software reset.
* https://github.com/esmil/gd32vf103inator/blob/master/include/gd32vf103/dbg.h
*/
DBG_KEY = DBG_KEY_UNLOCK;
diff --git a/hw/bsp/gd32vf103/system_gd32vf103.c b/hw/bsp/gd32vf103/system_gd32vf103.c
index 29518a54a..c2001a980 100644
--- a/hw/bsp/gd32vf103/system_gd32vf103.c
+++ b/hw/bsp/gd32vf103/system_gd32vf103.c
@@ -574,7 +574,7 @@ void ECLIC_Init(void)
* \param [in] IRQn NMI interrupt handler address
* \param [in] shv \ref ECLIC_NON_VECTOR_INTERRUPT means non-vector mode, and \ref ECLIC_VECTOR_INTERRUPT is vector mode
* \param [in] trig_mode see \ref ECLIC_TRIGGER_Type
- * \param [in] lvl interupt level
+ * \param [in] lvl interrupt level
* \param [in] priority interrupt priority
* \param [in] handler interrupt handler, if NULL, handler will not be installed
* \return -1 means invalid input parameter. 0 means successful.
diff --git a/hw/bsp/imxrt/boards/mimxrt1010_evk/evkmimxrt1010_flexspi_nor_config.h b/hw/bsp/imxrt/boards/mimxrt1010_evk/evkmimxrt1010_flexspi_nor_config.h
index 4be2760be..bb5a64448 100644
--- a/hw/bsp/imxrt/boards/mimxrt1010_evk/evkmimxrt1010_flexspi_nor_config.h
+++ b/hw/bsp/imxrt/boards/mimxrt1010_evk/evkmimxrt1010_flexspi_nor_config.h
@@ -18,7 +18,7 @@
#define FSL_XIP_BOARD_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
/*@}*/
-/* FLEXSPI memory config block related defintions */
+/* FLEXSPI memory config block related definitions */
#define FLEXSPI_CFG_BLK_TAG (0x42464346UL) // ascii "FCFB" Big Endian
#define FLEXSPI_CFG_BLK_VERSION (0x56010400UL) // V1.4.0
#define FLEXSPI_CFG_BLK_SIZE (512)
@@ -26,7 +26,7 @@
/* FLEXSPI Feature related definitions */
#define FLEXSPI_FEATURE_HAS_PARALLEL_MODE 1
-/* Lookup table related defintions */
+/* Lookup table related definitions */
#define CMD_INDEX_READ 0
#define CMD_INDEX_READSTATUS 1
#define CMD_INDEX_WRITEENABLE 2
@@ -123,7 +123,7 @@ enum
kFlexSpiDeviceType_SerialNAND = 2, //!< Flash devices are Serial NAND
kFlexSpiDeviceType_SerialRAM = 3, //!< Flash devices are Serial RAM/HyperFLASH
kFlexSpiDeviceType_MCP_NOR_NAND = 0x12, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial NAND
- kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash deivce is MCP device, A1 is Serial NOR, A2 is Serial RAMs
+ kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial RAMs
};
//!@brief Flash Pad Definitions
@@ -184,7 +184,7 @@ typedef struct _FlexSPIConfig
//! details
uint8_t deviceType; //!< [0x044-0x044] Device Type: See Flash Type Definition for more details
uint8_t sflashPadType; //!< [0x045-0x045] Serial Flash Pad Type: 1 - Single, 2 - Dual, 4 - Quad, 8 - Octal
- uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequencey, device specific definitions, See System Boot
+ uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequency, device specific definitions, See System Boot
//! Chapter for more details
uint8_t lutCustomSeqEnable; //!< [0x047-0x047] LUT customization Enable, it is required if the program/erase cannot
//! be done using 1 LUT sequence, currently, only applicable to HyperFLASH
@@ -252,7 +252,7 @@ typedef struct _flexspi_nor_config
uint8_t serialNorType; //!< Serial NOR Flash type: 0/1/2/3
uint8_t needExitNoCmdMode; //!< Need to exit NoCmd mode before other IP command
uint8_t halfClkForNonReadCmd; //!< Half the Serial Clock for non-read command: true/false
- uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP commmand execution
+ uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP command execution
uint32_t blockSize; //!< Block size
uint32_t reserve2[11]; //!< Reserved for future use
} flexspi_nor_config_t;
diff --git a/hw/bsp/imxrt/boards/mimxrt1015_evk/evkmimxrt1015_flexspi_nor_config.h b/hw/bsp/imxrt/boards/mimxrt1015_evk/evkmimxrt1015_flexspi_nor_config.h
index 94af5a115..f1415aeec 100644
--- a/hw/bsp/imxrt/boards/mimxrt1015_evk/evkmimxrt1015_flexspi_nor_config.h
+++ b/hw/bsp/imxrt/boards/mimxrt1015_evk/evkmimxrt1015_flexspi_nor_config.h
@@ -18,7 +18,7 @@
#define FSL_XIP_BOARD_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
/*@}*/
-/* FLEXSPI memory config block related defintions */
+/* FLEXSPI memory config block related definitions */
#define FLEXSPI_CFG_BLK_TAG (0x42464346UL) // ascii "FCFB" Big Endian
#define FLEXSPI_CFG_BLK_VERSION (0x56010400UL) // V1.4.0
#define FLEXSPI_CFG_BLK_SIZE (512)
@@ -26,7 +26,7 @@
/* FLEXSPI Feature related definitions */
#define FLEXSPI_FEATURE_HAS_PARALLEL_MODE 1
-/* Lookup table related defintions */
+/* Lookup table related definitions */
#define CMD_INDEX_READ 0
#define CMD_INDEX_READSTATUS 1
#define CMD_INDEX_WRITEENABLE 2
@@ -124,7 +124,7 @@ enum
kFlexSpiDeviceType_SerialNAND = 2, //!< Flash devices are Serial NAND
kFlexSpiDeviceType_SerialRAM = 3, //!< Flash devices are Serial RAM/HyperFLASH
kFlexSpiDeviceType_MCP_NOR_NAND = 0x12, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial NAND
- kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash deivce is MCP device, A1 is Serial NOR, A2 is Serial RAMs
+ kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial RAMs
};
//!@brief Flash Pad Definitions
@@ -185,7 +185,7 @@ typedef struct _FlexSPIConfig
//! details
uint8_t deviceType; //!< [0x044-0x044] Device Type: See Flash Type Definition for more details
uint8_t sflashPadType; //!< [0x045-0x045] Serial Flash Pad Type: 1 - Single, 2 - Dual, 4 - Quad, 8 - Octal
- uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequencey, device specific definitions, See System Boot
+ uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequency, device specific definitions, See System Boot
//! Chapter for more details
uint8_t lutCustomSeqEnable; //!< [0x047-0x047] LUT customization Enable, it is required if the program/erase cannot
//! be done using 1 LUT sequence, currently, only applicable to HyperFLASH
@@ -253,7 +253,7 @@ typedef struct _flexspi_nor_config
uint8_t serialNorType; //!< Serial NOR Flash type: 0/1/2/3
uint8_t needExitNoCmdMode; //!< Need to exit NoCmd mode before other IP command
uint8_t halfClkForNonReadCmd; //!< Half the Serial Clock for non-read command: true/false
- uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP commmand execution
+ uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP command execution
uint32_t blockSize; //!< Block size
uint32_t reserve2[11]; //!< Reserved for future use
} flexspi_nor_config_t;
diff --git a/hw/bsp/imxrt/boards/mimxrt1020_evk/evkmimxrt1020_flexspi_nor_config.h b/hw/bsp/imxrt/boards/mimxrt1020_evk/evkmimxrt1020_flexspi_nor_config.h
index f5e1aca5c..ad79d5002 100644
--- a/hw/bsp/imxrt/boards/mimxrt1020_evk/evkmimxrt1020_flexspi_nor_config.h
+++ b/hw/bsp/imxrt/boards/mimxrt1020_evk/evkmimxrt1020_flexspi_nor_config.h
@@ -18,7 +18,7 @@
#define FSL_XIP_BOARD_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
/*@}*/
-/* FLEXSPI memory config block related defintions */
+/* FLEXSPI memory config block related definitions */
#define FLEXSPI_CFG_BLK_TAG (0x42464346UL) // ascii "FCFB" Big Endian
#define FLEXSPI_CFG_BLK_VERSION (0x56010400UL) // V1.4.0
#define FLEXSPI_CFG_BLK_SIZE (512)
@@ -26,7 +26,7 @@
/* FLEXSPI Feature related definitions */
#define FLEXSPI_FEATURE_HAS_PARALLEL_MODE 1
-/* Lookup table related defintions */
+/* Lookup table related definitions */
#define CMD_INDEX_READ 0
#define CMD_INDEX_READSTATUS 1
#define CMD_INDEX_WRITEENABLE 2
@@ -124,7 +124,7 @@ enum
kFlexSpiDeviceType_SerialNAND = 2, //!< Flash devices are Serial NAND
kFlexSpiDeviceType_SerialRAM = 3, //!< Flash devices are Serial RAM/HyperFLASH
kFlexSpiDeviceType_MCP_NOR_NAND = 0x12, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial NAND
- kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash deivce is MCP device, A1 is Serial NOR, A2 is Serial RAMs
+ kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial RAMs
};
//!@brief Flash Pad Definitions
@@ -185,7 +185,7 @@ typedef struct _FlexSPIConfig
//! details
uint8_t deviceType; //!< [0x044-0x044] Device Type: See Flash Type Definition for more details
uint8_t sflashPadType; //!< [0x045-0x045] Serial Flash Pad Type: 1 - Single, 2 - Dual, 4 - Quad, 8 - Octal
- uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequencey, device specific definitions, See System Boot
+ uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequency, device specific definitions, See System Boot
//! Chapter for more details
uint8_t lutCustomSeqEnable; //!< [0x047-0x047] LUT customization Enable, it is required if the program/erase cannot
//! be done using 1 LUT sequence, currently, only applicable to HyperFLASH
@@ -253,7 +253,7 @@ typedef struct _flexspi_nor_config
uint8_t serialNorType; //!< Serial NOR Flash type: 0/1/2/3
uint8_t needExitNoCmdMode; //!< Need to exit NoCmd mode before other IP command
uint8_t halfClkForNonReadCmd; //!< Half the Serial Clock for non-read command: true/false
- uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP commmand execution
+ uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP command execution
uint32_t blockSize; //!< Block size
uint32_t reserve2[11]; //!< Reserved for future use
} flexspi_nor_config_t;
diff --git a/hw/bsp/imxrt/boards/mimxrt1024_evk/board.h b/hw/bsp/imxrt/boards/mimxrt1024_evk/board.h
new file mode 100644
index 000000000..152c9ab18
--- /dev/null
+++ b/hw/bsp/imxrt/boards/mimxrt1024_evk/board.h
@@ -0,0 +1,52 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019, Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+
+
+#ifndef BOARD_H_
+#define BOARD_H_
+
+// required since iMX RT10xx SDK include this file for board size
+// RT1020-EVK #define BOARD_FLASH_SIZE (0x800000U)
+#define BOARD_FLASH_SIZE (0x400000U) // builtin flash of RT1024
+
+// LED - DRN updated for RT1024EVK
+#define LED_PINMUX IOMUXC_GPIO_AD_B1_08_GPIO1_IO24
+#define LED_PORT GPIO1
+#define LED_PIN 24
+#define LED_STATE_ON 1
+
+// SW8 button - DRN verified
+#define BUTTON_PINMUX IOMUXC_SNVS_WAKEUP_GPIO5_IO00
+#define BUTTON_PORT GPIO5
+#define BUTTON_PIN 0
+#define BUTTON_STATE_ACTIVE 0
+
+// UART - DRN verified
+#define UART_PORT LPUART1
+#define UART_RX_PINMUX IOMUXC_GPIO_AD_B0_07_LPUART1_RX
+#define UART_TX_PINMUX IOMUXC_GPIO_AD_B0_06_LPUART1_TX
+
+#endif /* BOARD_H_ */
diff --git a/hw/bsp/imxrt/boards/mimxrt1024_evk/board.mk b/hw/bsp/imxrt/boards/mimxrt1024_evk/board.mk
new file mode 100644
index 000000000..92209992d
--- /dev/null
+++ b/hw/bsp/imxrt/boards/mimxrt1024_evk/board.mk
@@ -0,0 +1,14 @@
+CFLAGS += -DCPU_MIMXRT1024DAG5A
+MCU_VARIANT = MIMXRT1024
+
+# warnings caused by mcu driver
+CFLAGS += -Wno-error=array-bounds
+
+# For flash-jlink target
+JLINK_DEVICE = MIMXRT1024DAG5A
+
+# For flash-pyocd target
+PYOCD_TARGET = mimxrt1024
+
+# flash using pyocd
+flash: flash-pyocd
diff --git a/hw/bsp/imxrt/boards/mimxrt1024_evk/evkmimxrt1024_flexspi_nor_config.c b/hw/bsp/imxrt/boards/mimxrt1024_evk/evkmimxrt1024_flexspi_nor_config.c
new file mode 100644
index 000000000..43dc1e823
--- /dev/null
+++ b/hw/bsp/imxrt/boards/mimxrt1024_evk/evkmimxrt1024_flexspi_nor_config.c
@@ -0,0 +1,48 @@
+/*
+ * Copyright 2020 NXP
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#include "evkmimxrt1024_flexspi_nor_config.h"
+
+/* Component ID definition, used by tools. */
+#ifndef FSL_COMPONENT_ID
+#define FSL_COMPONENT_ID "platform.drivers.xip_board"
+#endif
+
+/*******************************************************************************
+ * Code
+ ******************************************************************************/
+#if defined(XIP_BOOT_HEADER_ENABLE) && (XIP_BOOT_HEADER_ENABLE == 1)
+#if defined(__CC_ARM) || defined(__ARMCC_VERSION) || defined(__GNUC__)
+__attribute__((section(".boot_hdr.conf"), used))
+#elif defined(__ICCARM__)
+#pragma location = ".boot_hdr.conf"
+#endif
+
+const flexspi_nor_config_t qspiflash_config = {
+ .memConfig =
+ {
+ .tag = FLEXSPI_CFG_BLK_TAG,
+ .version = FLEXSPI_CFG_BLK_VERSION,
+ .readSampleClkSrc = kFlexSPIReadSampleClk_LoopbackInternally,
+ .csHoldTime = 3u,
+ .csSetupTime = 3u,
+ .sflashPadType = kSerialFlash_4Pads,
+ .serialClkFreq = kFlexSpiSerialClk_60MHz,
+ .sflashA1Size = 4u * 1024u * 1024u,
+ .lookupTable =
+ {
+ // Read LUTs
+ FLEXSPI_LUT_SEQ(CMD_SDR, FLEXSPI_1PAD, 0xEB, RADDR_SDR, FLEXSPI_4PAD, 0x18),
+ FLEXSPI_LUT_SEQ(DUMMY_SDR, FLEXSPI_4PAD, 0x06, READ_SDR, FLEXSPI_4PAD, 0x04),
+ },
+ },
+ .pageSize = 256u,
+ .sectorSize = 4u * 1024u,
+ .blockSize = 64u * 1024u,
+ .isUniformBlockSize = false,
+};
+#endif /* XIP_BOOT_HEADER_ENABLE */
diff --git a/hw/bsp/imxrt/boards/mimxrt1024_evk/evkmimxrt1024_flexspi_nor_config.h b/hw/bsp/imxrt/boards/mimxrt1024_evk/evkmimxrt1024_flexspi_nor_config.h
new file mode 100644
index 000000000..5231dc034
--- /dev/null
+++ b/hw/bsp/imxrt/boards/mimxrt1024_evk/evkmimxrt1024_flexspi_nor_config.h
@@ -0,0 +1,266 @@
+/*
+ * Copyright 2020 NXP
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier: BSD-3-Clause
+ */
+
+#ifndef __EVKMIMXRT1024_FLEXSPI_NOR_CONFIG__
+#define __EVKMIMXRT1024_FLEXSPI_NOR_CONFIG__
+
+#include
+#include
+#include "fsl_common.h"
+
+/*! @name Driver version */
+/*@{*/
+/*! @brief XIP_BOARD driver version 2.0.1. */
+#define FSL_XIP_BOARD_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
+/*@}*/
+
+/* FLEXSPI memory config block related definitions */
+#define FLEXSPI_CFG_BLK_TAG (0x42464346UL) // ascii "FCFB" Big Endian
+#define FLEXSPI_CFG_BLK_VERSION (0x56010400UL) // V1.4.0
+#define FLEXSPI_CFG_BLK_SIZE (512)
+
+/* FLEXSPI Feature related definitions */
+#define FLEXSPI_FEATURE_HAS_PARALLEL_MODE 1
+
+/* Lookup table related definitions */
+#define CMD_INDEX_READ 0
+#define CMD_INDEX_READSTATUS 1
+#define CMD_INDEX_WRITEENABLE 2
+#define CMD_INDEX_WRITE 4
+
+#define CMD_LUT_SEQ_IDX_READ 0
+#define CMD_LUT_SEQ_IDX_READSTATUS 1
+#define CMD_LUT_SEQ_IDX_WRITEENABLE 3
+#define CMD_LUT_SEQ_IDX_WRITE 9
+
+#define CMD_SDR 0x01
+#define CMD_DDR 0x21
+#define RADDR_SDR 0x02
+#define RADDR_DDR 0x22
+#define CADDR_SDR 0x03
+#define CADDR_DDR 0x23
+#define MODE1_SDR 0x04
+#define MODE1_DDR 0x24
+#define MODE2_SDR 0x05
+#define MODE2_DDR 0x25
+#define MODE4_SDR 0x06
+#define MODE4_DDR 0x26
+#define MODE8_SDR 0x07
+#define MODE8_DDR 0x27
+#define WRITE_SDR 0x08
+#define WRITE_DDR 0x28
+#define READ_SDR 0x09
+#define READ_DDR 0x29
+#define LEARN_SDR 0x0A
+#define LEARN_DDR 0x2A
+#define DATSZ_SDR 0x0B
+#define DATSZ_DDR 0x2B
+#define DUMMY_SDR 0x0C
+#define DUMMY_DDR 0x2C
+#define DUMMY_RWDS_SDR 0x0D
+#define DUMMY_RWDS_DDR 0x2D
+#define JMP_ON_CS 0x1F
+#define STOP 0
+
+#define FLEXSPI_1PAD 0
+#define FLEXSPI_2PAD 1
+#define FLEXSPI_4PAD 2
+#define FLEXSPI_8PAD 3
+
+#define FLEXSPI_LUT_SEQ(cmd0, pad0, op0, cmd1, pad1, op1) \
+ (FLEXSPI_LUT_OPERAND0(op0) | FLEXSPI_LUT_NUM_PADS0(pad0) | FLEXSPI_LUT_OPCODE0(cmd0) | FLEXSPI_LUT_OPERAND1(op1) | \
+ FLEXSPI_LUT_NUM_PADS1(pad1) | FLEXSPI_LUT_OPCODE1(cmd1))
+
+//!@brief Definitions for FlexSPI Serial Clock Frequency
+typedef enum _FlexSpiSerialClockFreq
+{
+ kFlexSpiSerialClk_30MHz = 1,
+ kFlexSpiSerialClk_50MHz = 2,
+ kFlexSpiSerialClk_60MHz = 3,
+ kFlexSpiSerialClk_75MHz = 4,
+ kFlexSpiSerialClk_80MHz = 5,
+ kFlexSpiSerialClk_100MHz = 6,
+ kFlexSpiSerialClk_133MHz = 7,
+} flexspi_serial_clk_freq_t;
+
+//!@brief FlexSPI clock configuration type
+enum
+{
+ kFlexSpiClk_SDR, //!< Clock configure for SDR mode
+ kFlexSpiClk_DDR, //!< Clock configurat for DDR mode
+};
+
+//!@brief FlexSPI Read Sample Clock Source definition
+typedef enum _FlashReadSampleClkSource
+{
+ kFlexSPIReadSampleClk_LoopbackInternally = 0,
+ kFlexSPIReadSampleClk_LoopbackFromDqsPad = 1,
+ kFlexSPIReadSampleClk_LoopbackFromSckPad = 2,
+ kFlexSPIReadSampleClk_ExternalInputFromDqsPad = 3,
+} flexspi_read_sample_clk_t;
+
+//!@brief Misc feature bit definitions
+enum
+{
+ kFlexSpiMiscOffset_DiffClkEnable = 0, //!< Bit for Differential clock enable
+ kFlexSpiMiscOffset_Ck2Enable = 1, //!< Bit for CK2 enable
+ kFlexSpiMiscOffset_ParallelEnable = 2, //!< Bit for Parallel mode enable
+ kFlexSpiMiscOffset_WordAddressableEnable = 3, //!< Bit for Word Addressable enable
+ kFlexSpiMiscOffset_SafeConfigFreqEnable = 4, //!< Bit for Safe Configuration Frequency enable
+ kFlexSpiMiscOffset_PadSettingOverrideEnable = 5, //!< Bit for Pad setting override enable
+ kFlexSpiMiscOffset_DdrModeEnable = 6, //!< Bit for DDR clock confiuration indication.
+};
+
+//!@brief Flash Type Definition
+enum
+{
+ kFlexSpiDeviceType_SerialNOR = 1, //!< Flash devices are Serial NOR
+ kFlexSpiDeviceType_SerialNAND = 2, //!< Flash devices are Serial NAND
+ kFlexSpiDeviceType_SerialRAM = 3, //!< Flash devices are Serial RAM/HyperFLASH
+ kFlexSpiDeviceType_MCP_NOR_NAND = 0x12, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial NAND
+ kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial RAMs
+};
+
+//!@brief Flash Pad Definitions
+enum
+{
+ kSerialFlash_1Pad = 1,
+ kSerialFlash_2Pads = 2,
+ kSerialFlash_4Pads = 4,
+ kSerialFlash_8Pads = 8,
+};
+
+//!@brief FlexSPI LUT Sequence structure
+typedef struct _lut_sequence
+{
+ uint8_t seqNum; //!< Sequence Number, valid number: 1-16
+ uint8_t seqId; //!< Sequence Index, valid number: 0-15
+ uint16_t reserved;
+} flexspi_lut_seq_t;
+
+//!@brief Flash Configuration Command Type
+enum
+{
+ kDeviceConfigCmdType_Generic, //!< Generic command, for example: configure dummy cycles, drive strength, etc
+ kDeviceConfigCmdType_QuadEnable, //!< Quad Enable command
+ kDeviceConfigCmdType_Spi2Xpi, //!< Switch from SPI to DPI/QPI/OPI mode
+ kDeviceConfigCmdType_Xpi2Spi, //!< Switch from DPI/QPI/OPI to SPI mode
+ kDeviceConfigCmdType_Spi2NoCmd, //!< Switch to 0-4-4/0-8-8 mode
+ kDeviceConfigCmdType_Reset, //!< Reset device command
+};
+
+//!@brief FlexSPI Memory Configuration Block
+typedef struct _FlexSPIConfig
+{
+ uint32_t tag; //!< [0x000-0x003] Tag, fixed value 0x42464346UL
+ uint32_t version; //!< [0x004-0x007] Version,[31:24] -'V', [23:16] - Major, [15:8] - Minor, [7:0] - bugfix
+ uint32_t reserved0; //!< [0x008-0x00b] Reserved for future use
+ uint8_t readSampleClkSrc; //!< [0x00c-0x00c] Read Sample Clock Source, valid value: 0/1/3
+ uint8_t csHoldTime; //!< [0x00d-0x00d] CS hold time, default value: 3
+ uint8_t csSetupTime; //!< [0x00e-0x00e] CS setup time, default value: 3
+ uint8_t columnAddressWidth; //!< [0x00f-0x00f] Column Address with, for HyperBus protocol, it is fixed to 3, For
+ //! Serial NAND, need to refer to datasheet
+ uint8_t deviceModeCfgEnable; //!< [0x010-0x010] Device Mode Configure enable flag, 1 - Enable, 0 - Disable
+ uint8_t deviceModeType; //!< [0x011-0x011] Specify the configuration command type:Quad Enable, DPI/QPI/OPI switch,
+ //! Generic configuration, etc.
+ uint16_t waitTimeCfgCommands; //!< [0x012-0x013] Wait time for all configuration commands, unit: 100us, Used for
+ //! DPI/QPI/OPI switch or reset command
+ flexspi_lut_seq_t deviceModeSeq; //!< [0x014-0x017] Device mode sequence info, [7:0] - LUT sequence id, [15:8] - LUt
+ //! sequence number, [31:16] Reserved
+ uint32_t deviceModeArg; //!< [0x018-0x01b] Argument/Parameter for device configuration
+ uint8_t configCmdEnable; //!< [0x01c-0x01c] Configure command Enable Flag, 1 - Enable, 0 - Disable
+ uint8_t configModeType[3]; //!< [0x01d-0x01f] Configure Mode Type, similar as deviceModeTpe
+ flexspi_lut_seq_t
+ configCmdSeqs[3]; //!< [0x020-0x02b] Sequence info for Device Configuration command, similar as deviceModeSeq
+ uint32_t reserved1; //!< [0x02c-0x02f] Reserved for future use
+ uint32_t configCmdArgs[3]; //!< [0x030-0x03b] Arguments/Parameters for device Configuration commands
+ uint32_t reserved2; //!< [0x03c-0x03f] Reserved for future use
+ uint32_t controllerMiscOption; //!< [0x040-0x043] Controller Misc Options, see Misc feature bit definitions for more
+ //! details
+ uint8_t deviceType; //!< [0x044-0x044] Device Type: See Flash Type Definition for more details
+ uint8_t sflashPadType; //!< [0x045-0x045] Serial Flash Pad Type: 1 - Single, 2 - Dual, 4 - Quad, 8 - Octal
+ uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequency, device specific definitions, See System Boot
+ //! Chapter for more details
+ uint8_t lutCustomSeqEnable; //!< [0x047-0x047] LUT customization Enable, it is required if the program/erase cannot
+ //! be done using 1 LUT sequence, currently, only applicable to HyperFLASH
+ uint32_t reserved3[2]; //!< [0x048-0x04f] Reserved for future use
+ uint32_t sflashA1Size; //!< [0x050-0x053] Size of Flash connected to A1
+ uint32_t sflashA2Size; //!< [0x054-0x057] Size of Flash connected to A2
+ uint32_t sflashB1Size; //!< [0x058-0x05b] Size of Flash connected to B1
+ uint32_t sflashB2Size; //!< [0x05c-0x05f] Size of Flash connected to B2
+ uint32_t csPadSettingOverride; //!< [0x060-0x063] CS pad setting override value
+ uint32_t sclkPadSettingOverride; //!< [0x064-0x067] SCK pad setting override value
+ uint32_t dataPadSettingOverride; //!< [0x068-0x06b] data pad setting override value
+ uint32_t dqsPadSettingOverride; //!< [0x06c-0x06f] DQS pad setting override value
+ uint32_t timeoutInMs; //!< [0x070-0x073] Timeout threshold for read status command
+ uint32_t commandInterval; //!< [0x074-0x077] CS deselect interval between two commands
+ uint16_t dataValidTime[2]; //!< [0x078-0x07b] CLK edge to data valid time for PORT A and PORT B, in terms of 0.1ns
+ uint16_t busyOffset; //!< [0x07c-0x07d] Busy offset, valid value: 0-31
+ uint16_t busyBitPolarity; //!< [0x07e-0x07f] Busy flag polarity, 0 - busy flag is 1 when flash device is busy, 1 -
+ //! busy flag is 0 when flash device is busy
+ uint32_t lookupTable[64]; //!< [0x080-0x17f] Lookup table holds Flash command sequences
+ flexspi_lut_seq_t lutCustomSeq[12]; //!< [0x180-0x1af] Customizable LUT Sequences
+ uint32_t reserved4[4]; //!< [0x1b0-0x1bf] Reserved for future use
+} flexspi_mem_config_t;
+
+/* */
+#define NOR_CMD_INDEX_READ CMD_INDEX_READ //!< 0
+#define NOR_CMD_INDEX_READSTATUS CMD_INDEX_READSTATUS //!< 1
+#define NOR_CMD_INDEX_WRITEENABLE CMD_INDEX_WRITEENABLE //!< 2
+#define NOR_CMD_INDEX_ERASESECTOR 3 //!< 3
+#define NOR_CMD_INDEX_PAGEPROGRAM CMD_INDEX_WRITE //!< 4
+#define NOR_CMD_INDEX_CHIPERASE 5 //!< 5
+#define NOR_CMD_INDEX_DUMMY 6 //!< 6
+#define NOR_CMD_INDEX_ERASEBLOCK 7 //!< 7
+
+#define NOR_CMD_LUT_SEQ_IDX_READ CMD_LUT_SEQ_IDX_READ //!< 0 READ LUT sequence id in lookupTable stored in config block
+#define NOR_CMD_LUT_SEQ_IDX_READSTATUS \
+ CMD_LUT_SEQ_IDX_READSTATUS //!< 1 Read Status LUT sequence id in lookupTable stored in config block
+#define NOR_CMD_LUT_SEQ_IDX_READSTATUS_XPI \
+ 2 //!< 2 Read status DPI/QPI/OPI sequence id in lookupTable stored in config block
+#define NOR_CMD_LUT_SEQ_IDX_WRITEENABLE \
+ CMD_LUT_SEQ_IDX_WRITEENABLE //!< 3 Write Enable sequence id in lookupTable stored in config block
+#define NOR_CMD_LUT_SEQ_IDX_WRITEENABLE_XPI \
+ 4 //!< 4 Write Enable DPI/QPI/OPI sequence id in lookupTable stored in config block
+#define NOR_CMD_LUT_SEQ_IDX_ERASESECTOR 5 //!< 5 Erase Sector sequence id in lookupTable stored in config block
+#define NOR_CMD_LUT_SEQ_IDX_ERASEBLOCK 8 //!< 8 Erase Block sequence id in lookupTable stored in config block
+#define NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM \
+ CMD_LUT_SEQ_IDX_WRITE //!< 9 Program sequence id in lookupTable stored in config block
+#define NOR_CMD_LUT_SEQ_IDX_CHIPERASE 11 //!< 11 Chip Erase sequence in lookupTable id stored in config block
+#define NOR_CMD_LUT_SEQ_IDX_READ_SFDP 13 //!< 13 Read SFDP sequence in lookupTable id stored in config block
+#define NOR_CMD_LUT_SEQ_IDX_RESTORE_NOCMD \
+ 14 //!< 14 Restore 0-4-4/0-8-8 mode sequence id in lookupTable stored in config block
+#define NOR_CMD_LUT_SEQ_IDX_EXIT_NOCMD \
+ 15 //!< 15 Exit 0-4-4/0-8-8 mode sequence id in lookupTable stored in config blobk
+
+/*
+ * Serial NOR configuration block
+ */
+typedef struct _flexspi_nor_config
+{
+ flexspi_mem_config_t memConfig; //!< Common memory configuration info via FlexSPI
+ uint32_t pageSize; //!< Page size of Serial NOR
+ uint32_t sectorSize; //!< Sector size of Serial NOR
+ uint8_t ipcmdSerialClkFreq; //!< Clock frequency for IP command
+ uint8_t isUniformBlockSize; //!< Sector/Block size is the same
+ uint8_t reserved0[2]; //!< Reserved for future use
+ uint8_t serialNorType; //!< Serial NOR Flash type: 0/1/2/3
+ uint8_t needExitNoCmdMode; //!< Need to exit NoCmd mode before other IP command
+ uint8_t halfClkForNonReadCmd; //!< Half the Serial Clock for non-read command: true/false
+ uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP command execution
+ uint32_t blockSize; //!< Block size
+ uint32_t reserve2[11]; //!< Reserved for future use
+} flexspi_nor_config_t;
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* __EVKMIMXRT1024_FLEXSPI_NOR_CONFIG__ */
diff --git a/hw/bsp/imxrt/boards/mimxrt1050_evkb/evkbimxrt1050_flexspi_nor_config.h b/hw/bsp/imxrt/boards/mimxrt1050_evkb/evkbimxrt1050_flexspi_nor_config.h
index fe40e7ed7..7a14360e4 100644
--- a/hw/bsp/imxrt/boards/mimxrt1050_evkb/evkbimxrt1050_flexspi_nor_config.h
+++ b/hw/bsp/imxrt/boards/mimxrt1050_evkb/evkbimxrt1050_flexspi_nor_config.h
@@ -19,7 +19,7 @@
#define FSL_XIP_BOARD_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
/*@}*/
-/* FLEXSPI memory config block related defintions */
+/* FLEXSPI memory config block related definitions */
#define FLEXSPI_CFG_BLK_TAG (0x42464346UL) // ascii "FCFB" Big Endian
#define FLEXSPI_CFG_BLK_VERSION (0x56010400UL) // V1.4.0
#define FLEXSPI_CFG_BLK_SIZE (512)
@@ -27,7 +27,7 @@
/* FLEXSPI Feature related definitions */
#define FLEXSPI_FEATURE_HAS_PARALLEL_MODE 1
-/* Lookup table related defintions */
+/* Lookup table related definitions */
#define CMD_INDEX_READ 0
#define CMD_INDEX_READSTATUS 1
#define CMD_INDEX_WRITEENABLE 2
@@ -125,7 +125,7 @@ enum
kFlexSpiDeviceType_SerialNAND = 2, //!< Flash devices are Serial NAND
kFlexSpiDeviceType_SerialRAM = 3, //!< Flash devices are Serial RAM/HyperFLASH
kFlexSpiDeviceType_MCP_NOR_NAND = 0x12, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial NAND
- kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash deivce is MCP device, A1 is Serial NOR, A2 is Serial RAMs
+ kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial RAMs
};
//!@brief Flash Pad Definitions
@@ -186,7 +186,7 @@ typedef struct _FlexSPIConfig
//! details
uint8_t deviceType; //!< [0x044-0x044] Device Type: See Flash Type Definition for more details
uint8_t sflashPadType; //!< [0x045-0x045] Serial Flash Pad Type: 1 - Single, 2 - Dual, 4 - Quad, 8 - Octal
- uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequencey, device specific definitions, See System Boot
+ uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequency, device specific definitions, See System Boot
//! Chapter for more details
uint8_t lutCustomSeqEnable; //!< [0x047-0x047] LUT customization Enable, it is required if the program/erase cannot
//! be done using 1 LUT sequence, currently, only applicable to HyperFLASH
@@ -254,7 +254,7 @@ typedef struct _flexspi_nor_config
uint8_t serialNorType; //!< Serial NOR Flash type: 0/1/2/3
uint8_t needExitNoCmdMode; //!< Need to exit NoCmd mode before other IP command
uint8_t halfClkForNonReadCmd; //!< Half the Serial Clock for non-read command: true/false
- uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP commmand execution
+ uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP command execution
uint32_t blockSize; //!< Block size
uint32_t reserve2[11]; //!< Reserved for future use
} flexspi_nor_config_t;
diff --git a/hw/bsp/imxrt/boards/mimxrt1060_evk/evkmimxrt1060_flexspi_nor_config.h b/hw/bsp/imxrt/boards/mimxrt1060_evk/evkmimxrt1060_flexspi_nor_config.h
index 28d7db57d..b4ef4ad4f 100644
--- a/hw/bsp/imxrt/boards/mimxrt1060_evk/evkmimxrt1060_flexspi_nor_config.h
+++ b/hw/bsp/imxrt/boards/mimxrt1060_evk/evkmimxrt1060_flexspi_nor_config.h
@@ -18,7 +18,7 @@
#define FSL_XIP_BOARD_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
/*@}*/
-/* FLEXSPI memory config block related defintions */
+/* FLEXSPI memory config block related definitions */
#define FLEXSPI_CFG_BLK_TAG (0x42464346UL) // ascii "FCFB" Big Endian
#define FLEXSPI_CFG_BLK_VERSION (0x56010400UL) // V1.4.0
#define FLEXSPI_CFG_BLK_SIZE (512)
@@ -26,7 +26,7 @@
/* FLEXSPI Feature related definitions */
#define FLEXSPI_FEATURE_HAS_PARALLEL_MODE 1
-/* Lookup table related defintions */
+/* Lookup table related definitions */
#define CMD_INDEX_READ 0
#define CMD_INDEX_READSTATUS 1
#define CMD_INDEX_WRITEENABLE 2
@@ -124,7 +124,7 @@ enum
kFlexSpiDeviceType_SerialNAND = 2, //!< Flash devices are Serial NAND
kFlexSpiDeviceType_SerialRAM = 3, //!< Flash devices are Serial RAM/HyperFLASH
kFlexSpiDeviceType_MCP_NOR_NAND = 0x12, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial NAND
- kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash deivce is MCP device, A1 is Serial NOR, A2 is Serial RAMs
+ kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial RAMs
};
//!@brief Flash Pad Definitions
@@ -185,7 +185,7 @@ typedef struct _FlexSPIConfig
//! details
uint8_t deviceType; //!< [0x044-0x044] Device Type: See Flash Type Definition for more details
uint8_t sflashPadType; //!< [0x045-0x045] Serial Flash Pad Type: 1 - Single, 2 - Dual, 4 - Quad, 8 - Octal
- uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequencey, device specific definitions, See System Boot
+ uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequency, device specific definitions, See System Boot
//! Chapter for more details
uint8_t lutCustomSeqEnable; //!< [0x047-0x047] LUT customization Enable, it is required if the program/erase cannot
//! be done using 1 LUT sequence, currently, only applicable to HyperFLASH
@@ -253,7 +253,7 @@ typedef struct _flexspi_nor_config
uint8_t serialNorType; //!< Serial NOR Flash type: 0/1/2/3
uint8_t needExitNoCmdMode; //!< Need to exit NoCmd mode before other IP command
uint8_t halfClkForNonReadCmd; //!< Half the Serial Clock for non-read command: true/false
- uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP commmand execution
+ uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP command execution
uint32_t blockSize; //!< Block size
uint32_t reserve2[11]; //!< Reserved for future use
} flexspi_nor_config_t;
diff --git a/hw/bsp/imxrt/boards/mimxrt1064_evk/evkmimxrt1064_flexspi_nor_config.h b/hw/bsp/imxrt/boards/mimxrt1064_evk/evkmimxrt1064_flexspi_nor_config.h
index efdfe583f..995e88cb9 100644
--- a/hw/bsp/imxrt/boards/mimxrt1064_evk/evkmimxrt1064_flexspi_nor_config.h
+++ b/hw/bsp/imxrt/boards/mimxrt1064_evk/evkmimxrt1064_flexspi_nor_config.h
@@ -18,7 +18,7 @@
#define FSL_XIP_BOARD_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
/*@}*/
-/* FLEXSPI memory config block related defintions */
+/* FLEXSPI memory config block related definitions */
#define FLEXSPI_CFG_BLK_TAG (0x42464346UL) // ascii "FCFB" Big Endian
#define FLEXSPI_CFG_BLK_VERSION (0x56010400UL) // V1.4.0
#define FLEXSPI_CFG_BLK_SIZE (512)
@@ -26,7 +26,7 @@
/* FLEXSPI Feature related definitions */
#define FLEXSPI_FEATURE_HAS_PARALLEL_MODE 1
-/* Lookup table related defintions */
+/* Lookup table related definitions */
#define CMD_INDEX_READ 0
#define CMD_INDEX_READSTATUS 1
#define CMD_INDEX_WRITEENABLE 2
@@ -124,7 +124,7 @@ enum
kFlexSpiDeviceType_SerialNAND = 2, //!< Flash devices are Serial NAND
kFlexSpiDeviceType_SerialRAM = 3, //!< Flash devices are Serial RAM/HyperFLASH
kFlexSpiDeviceType_MCP_NOR_NAND = 0x12, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial NAND
- kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash deivce is MCP device, A1 is Serial NOR, A2 is Serial RAMs
+ kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial RAMs
};
//!@brief Flash Pad Definitions
@@ -185,7 +185,7 @@ typedef struct _FlexSPIConfig
//! details
uint8_t deviceType; //!< [0x044-0x044] Device Type: See Flash Type Definition for more details
uint8_t sflashPadType; //!< [0x045-0x045] Serial Flash Pad Type: 1 - Single, 2 - Dual, 4 - Quad, 8 - Octal
- uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequencey, device specific definitions, See System Boot
+ uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequency, device specific definitions, See System Boot
//! Chapter for more details
uint8_t lutCustomSeqEnable; //!< [0x047-0x047] LUT customization Enable, it is required if the program/erase cannot
//! be done using 1 LUT sequence, currently, only applicable to HyperFLASH
@@ -253,7 +253,7 @@ typedef struct _flexspi_nor_config
uint8_t serialNorType; //!< Serial NOR Flash type: 0/1/2/3
uint8_t needExitNoCmdMode; //!< Need to exit NoCmd mode before other IP command
uint8_t halfClkForNonReadCmd; //!< Half the Serial Clock for non-read command: true/false
- uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP commmand execution
+ uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP command execution
uint32_t blockSize; //!< Block size
uint32_t reserve2[11]; //!< Reserved for future use
} flexspi_nor_config_t;
diff --git a/hw/bsp/imxrt/boards/teensy_40/teensy40_flexspi_nor_config.h b/hw/bsp/imxrt/boards/teensy_40/teensy40_flexspi_nor_config.h
index 56068ec6a..d24d4f10a 100644
--- a/hw/bsp/imxrt/boards/teensy_40/teensy40_flexspi_nor_config.h
+++ b/hw/bsp/imxrt/boards/teensy_40/teensy40_flexspi_nor_config.h
@@ -18,7 +18,7 @@
#define FSL_XIP_BOARD_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
/*@}*/
-/* FLEXSPI memory config block related defintions */
+/* FLEXSPI memory config block related definitions */
#define FLEXSPI_CFG_BLK_TAG (0x42464346UL) // ascii "FCFB" Big Endian
#define FLEXSPI_CFG_BLK_VERSION (0x56010400UL) // V1.4.0
#define FLEXSPI_CFG_BLK_SIZE (512)
@@ -26,7 +26,7 @@
/* FLEXSPI Feature related definitions */
#define FLEXSPI_FEATURE_HAS_PARALLEL_MODE 1
-/* Lookup table related defintions */
+/* Lookup table related definitions */
#define CMD_INDEX_READ 0
#define CMD_INDEX_READSTATUS 1
#define CMD_INDEX_WRITEENABLE 2
@@ -124,7 +124,7 @@ enum
kFlexSpiDeviceType_SerialNAND = 2, //!< Flash devices are Serial NAND
kFlexSpiDeviceType_SerialRAM = 3, //!< Flash devices are Serial RAM/HyperFLASH
kFlexSpiDeviceType_MCP_NOR_NAND = 0x12, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial NAND
- kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash deivce is MCP device, A1 is Serial NOR, A2 is Serial RAMs
+ kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial RAMs
};
//!@brief Flash Pad Definitions
@@ -185,7 +185,7 @@ typedef struct _FlexSPIConfig
//! details
uint8_t deviceType; //!< [0x044-0x044] Device Type: See Flash Type Definition for more details
uint8_t sflashPadType; //!< [0x045-0x045] Serial Flash Pad Type: 1 - Single, 2 - Dual, 4 - Quad, 8 - Octal
- uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequencey, device specific definitions, See System Boot
+ uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequency, device specific definitions, See System Boot
//! Chapter for more details
uint8_t lutCustomSeqEnable; //!< [0x047-0x047] LUT customization Enable, it is required if the program/erase cannot
//! be done using 1 LUT sequence, currently, only applicable to HyperFLASH
@@ -253,7 +253,7 @@ typedef struct _flexspi_nor_config
uint8_t serialNorType; //!< Serial NOR Flash type: 0/1/2/3
uint8_t needExitNoCmdMode; //!< Need to exit NoCmd mode before other IP command
uint8_t halfClkForNonReadCmd; //!< Half the Serial Clock for non-read command: true/false
- uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP commmand execution
+ uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP command execution
uint32_t blockSize; //!< Block size
uint32_t reserve2[11]; //!< Reserved for future use
} flexspi_nor_config_t;
diff --git a/hw/bsp/imxrt/boards/teensy_41/teensy41_flexspi_nor_config.h b/hw/bsp/imxrt/boards/teensy_41/teensy41_flexspi_nor_config.h
index 56068ec6a..d24d4f10a 100644
--- a/hw/bsp/imxrt/boards/teensy_41/teensy41_flexspi_nor_config.h
+++ b/hw/bsp/imxrt/boards/teensy_41/teensy41_flexspi_nor_config.h
@@ -18,7 +18,7 @@
#define FSL_XIP_BOARD_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
/*@}*/
-/* FLEXSPI memory config block related defintions */
+/* FLEXSPI memory config block related definitions */
#define FLEXSPI_CFG_BLK_TAG (0x42464346UL) // ascii "FCFB" Big Endian
#define FLEXSPI_CFG_BLK_VERSION (0x56010400UL) // V1.4.0
#define FLEXSPI_CFG_BLK_SIZE (512)
@@ -26,7 +26,7 @@
/* FLEXSPI Feature related definitions */
#define FLEXSPI_FEATURE_HAS_PARALLEL_MODE 1
-/* Lookup table related defintions */
+/* Lookup table related definitions */
#define CMD_INDEX_READ 0
#define CMD_INDEX_READSTATUS 1
#define CMD_INDEX_WRITEENABLE 2
@@ -124,7 +124,7 @@ enum
kFlexSpiDeviceType_SerialNAND = 2, //!< Flash devices are Serial NAND
kFlexSpiDeviceType_SerialRAM = 3, //!< Flash devices are Serial RAM/HyperFLASH
kFlexSpiDeviceType_MCP_NOR_NAND = 0x12, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial NAND
- kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash deivce is MCP device, A1 is Serial NOR, A2 is Serial RAMs
+ kFlexSpiDeviceType_MCP_NOR_RAM = 0x13, //!< Flash device is MCP device, A1 is Serial NOR, A2 is Serial RAMs
};
//!@brief Flash Pad Definitions
@@ -185,7 +185,7 @@ typedef struct _FlexSPIConfig
//! details
uint8_t deviceType; //!< [0x044-0x044] Device Type: See Flash Type Definition for more details
uint8_t sflashPadType; //!< [0x045-0x045] Serial Flash Pad Type: 1 - Single, 2 - Dual, 4 - Quad, 8 - Octal
- uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequencey, device specific definitions, See System Boot
+ uint8_t serialClkFreq; //!< [0x046-0x046] Serial Flash Frequency, device specific definitions, See System Boot
//! Chapter for more details
uint8_t lutCustomSeqEnable; //!< [0x047-0x047] LUT customization Enable, it is required if the program/erase cannot
//! be done using 1 LUT sequence, currently, only applicable to HyperFLASH
@@ -253,7 +253,7 @@ typedef struct _flexspi_nor_config
uint8_t serialNorType; //!< Serial NOR Flash type: 0/1/2/3
uint8_t needExitNoCmdMode; //!< Need to exit NoCmd mode before other IP command
uint8_t halfClkForNonReadCmd; //!< Half the Serial Clock for non-read command: true/false
- uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP commmand execution
+ uint8_t needRestoreNoCmdMode; //!< Need to Restore NoCmd mode after IP command execution
uint32_t blockSize; //!< Block size
uint32_t reserve2[11]; //!< Reserved for future use
} flexspi_nor_config_t;
diff --git a/hw/bsp/imxrt/family.mk b/hw/bsp/imxrt/family.mk
index 846ecd255..0642e52e3 100644
--- a/hw/bsp/imxrt/family.mk
+++ b/hw/bsp/imxrt/family.mk
@@ -30,7 +30,7 @@ MCU_DIR = $(SDK_DIR)/devices/$(MCU_VARIANT)
# All source paths should be relative to the top level.
LD_FILE = $(MCU_DIR)/gcc/$(MCU_VARIANT)xxxxx_flexspi_nor.ld
-# TODO for net_lwip_webserver exmaple, but may not needed !!
+# TODO for net_lwip_webserver example, but may not needed !!
LDFLAGS += \
-Wl,--defsym,__stack_size__=0x800 \
diff --git a/hw/bsp/lpc54/family.c b/hw/bsp/lpc54/family.c
index 9bb341e73..621788948 100644
--- a/hw/bsp/lpc54/family.c
+++ b/hw/bsp/lpc54/family.c
@@ -170,7 +170,7 @@ void board_init(void)
CLOCK_SetClkDiv(kCLOCK_DivUsb0Clk, 1, false);
CLOCK_AttachClk(kFRO_HF_to_USB0_CLK);
- /*According to reference mannual, device mode setting has to be set by access usb host register */
+ /*According to reference manual, device mode setting has to be set by access usb host register */
CLOCK_EnableClock(kCLOCK_Usbhsl0); /* enable usb0 host clock */
USBFSH->PORTMODE |= USBFSH_PORTMODE_DEV_ENABLE_MASK;
CLOCK_DisableClock(kCLOCK_Usbhsl0); /* disable usb0 host clock */
@@ -182,7 +182,7 @@ void board_init(void)
// Port1 is High Speed
POWER_DisablePD(kPDRUNCFG_PD_USB1_PHY);
- /*According to reference mannual, device mode setting has to be set by access usb host register */
+ /*According to reference manual, device mode setting has to be set by access usb host register */
CLOCK_EnableClock(kCLOCK_Usbh1); /* enable usb1 host clock */
USBHSH->PORTMODE |= USBHSH_PORTMODE_DEV_ENABLE_MASK;
CLOCK_DisableClock(kCLOCK_Usbh1); /* enable usb1 host clock */
diff --git a/hw/bsp/lpc55/family.c b/hw/bsp/lpc55/family.c
index e7aed5759..5b6c56dd3 100644
--- a/hw/bsp/lpc55/family.c
+++ b/hw/bsp/lpc55/family.c
@@ -192,7 +192,7 @@ void board_init(void)
CLOCK_SetClkDiv(kCLOCK_DivUsb0Clk, 1, false);
CLOCK_AttachClk(kFRO_HF_to_USB0_CLK);
- /*According to reference mannual, device mode setting has to be set by access usb host register */
+ /*According to reference manual, device mode setting has to be set by access usb host register */
CLOCK_EnableClock(kCLOCK_Usbhsl0); // enable usb0 host clock
USBFSH->PORTMODE |= USBFSH_PORTMODE_DEV_ENABLE_MASK;
CLOCK_DisableClock(kCLOCK_Usbhsl0); // disable usb0 host clock
@@ -213,7 +213,7 @@ void board_init(void)
RESET_PeripheralReset(kUSB1_RST_SHIFT_RSTn);
RESET_PeripheralReset(kUSB1RAM_RST_SHIFT_RSTn);
- /* According to reference mannual, device mode setting has to be set by access usb host register */
+ /* According to reference manual, device mode setting has to be set by access usb host register */
CLOCK_EnableClock(kCLOCK_Usbh1); // enable usb0 host clock
USBHSH->PORTMODE = USBHSH_PORTMODE_SW_PDCOM_MASK; // Put PHY powerdown under software control
diff --git a/hw/bsp/mm32/boards/mm32f327x_mb39/flash.ld b/hw/bsp/mm32/boards/mm32f327x_mb39/flash.ld
index d96d6e43d..0b45ee7bd 100644
--- a/hw/bsp/mm32/boards/mm32f327x_mb39/flash.ld
+++ b/hw/bsp/mm32/boards/mm32f327x_mb39/flash.ld
@@ -126,7 +126,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/rp2040/family.c b/hw/bsp/rp2040/family.c
index 26008168d..f7ee56990 100644
--- a/hw/bsp/rp2040/family.c
+++ b/hw/bsp/rp2040/family.c
@@ -226,6 +226,6 @@ int board_getchar(void)
//--------------------------------------------------------------------+
// USB Interrupt Handler
-// rp2040 implementation will install approriate handler when initializing
+// rp2040 implementation will install appropriate handler when initializing
// tinyusb. There is no need to forward IRQ from application
//--------------------------------------------------------------------+
diff --git a/hw/bsp/sltb009a/sltb009a.c b/hw/bsp/sltb009a/sltb009a.c
index b929adb13..b5eb5ed5d 100644
--- a/hw/bsp/sltb009a/sltb009a.c
+++ b/hw/bsp/sltb009a/sltb009a.c
@@ -397,7 +397,7 @@ void cmu_init(void)
CMU->HFCLKSEL = CMU_HFCLKSEL_HF_HFXO;
while((CMU->HFCLKSTATUS & _CMU_HFCLKSTATUS_SELECTED_MASK) != CMU_HFCLKSTATUS_SELECTED_HFXO);
- // Calibrate HFRCO for 72MHz and enable tunning by PLL
+ // Calibrate HFRCO for 72MHz and enable tuning by PLL
cmu_hfrco_calib((DEVINFO->HFRCOCAL16) | CMU_HFRCOCTRL_FINETUNINGEN);
// Setup the PLL
diff --git a/hw/bsp/stm32f0/boards/stm32f070rbnucleo/stm32F070rbtx_flash.ld b/hw/bsp/stm32f0/boards/stm32f070rbnucleo/stm32F070rbtx_flash.ld
index 59e18f375..4a6491510 100644
--- a/hw/bsp/stm32f0/boards/stm32f070rbnucleo/stm32F070rbtx_flash.ld
+++ b/hw/bsp/stm32f0/boards/stm32f070rbnucleo/stm32F070rbtx_flash.ld
@@ -165,7 +165,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f0/boards/stm32f072disco/STM32F072RBTx_FLASH.ld b/hw/bsp/stm32f0/boards/stm32f072disco/STM32F072RBTx_FLASH.ld
index 8d31f6a00..f0879e929 100644
--- a/hw/bsp/stm32f0/boards/stm32f072disco/STM32F072RBTx_FLASH.ld
+++ b/hw/bsp/stm32f0/boards/stm32f072disco/STM32F072RBTx_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f1/boards/stm32f103_bluepill/STM32F103X8_FLASH.ld b/hw/bsp/stm32f1/boards/stm32f103_bluepill/STM32F103X8_FLASH.ld
index ca1804905..c434ca038 100644
--- a/hw/bsp/stm32f1/boards/stm32f103_bluepill/STM32F103X8_FLASH.ld
+++ b/hw/bsp/stm32f1/boards/stm32f103_bluepill/STM32F103X8_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f1/boards/stm32f103_mini_2/STM32F103XC_FLASH.ld b/hw/bsp/stm32f1/boards/stm32f103_mini_2/STM32F103XC_FLASH.ld
index da40d1eb2..da637d1b0 100644
--- a/hw/bsp/stm32f1/boards/stm32f103_mini_2/STM32F103XC_FLASH.ld
+++ b/hw/bsp/stm32f1/boards/stm32f103_mini_2/STM32F103XC_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f207nucleo/STM32F207ZGTx_FLASH.ld b/hw/bsp/stm32f207nucleo/STM32F207ZGTx_FLASH.ld
index 29e387f28..5010435ae 100644
--- a/hw/bsp/stm32f207nucleo/STM32F207ZGTx_FLASH.ld
+++ b/hw/bsp/stm32f207nucleo/STM32F207ZGTx_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f303disco/STM32F303VCTx_FLASH.ld b/hw/bsp/stm32f303disco/STM32F303VCTx_FLASH.ld
index ca9046d29..7f46c71b2 100644
--- a/hw/bsp/stm32f303disco/STM32F303VCTx_FLASH.ld
+++ b/hw/bsp/stm32f303disco/STM32F303VCTx_FLASH.ld
@@ -150,7 +150,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f4/boards/feather_stm32f405/STM32F405RGTx_FLASH.ld b/hw/bsp/stm32f4/boards/feather_stm32f405/STM32F405RGTx_FLASH.ld
index 57ef61e26..9eb53bcc0 100644
--- a/hw/bsp/stm32f4/boards/feather_stm32f405/STM32F405RGTx_FLASH.ld
+++ b/hw/bsp/stm32f4/boards/feather_stm32f405/STM32F405RGTx_FLASH.ld
@@ -150,7 +150,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f4/boards/pyboardv11/STM32F405RGTx_FLASH.ld b/hw/bsp/stm32f4/boards/pyboardv11/STM32F405RGTx_FLASH.ld
index 57ef61e26..9eb53bcc0 100644
--- a/hw/bsp/stm32f4/boards/pyboardv11/STM32F405RGTx_FLASH.ld
+++ b/hw/bsp/stm32f4/boards/pyboardv11/STM32F405RGTx_FLASH.ld
@@ -150,7 +150,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f4/boards/stm32f401blackpill/STM32F401VCTx_FLASH.ld b/hw/bsp/stm32f4/boards/stm32f401blackpill/STM32F401VCTx_FLASH.ld
index f51f1ab41..2bc5f6c14 100644
--- a/hw/bsp/stm32f4/boards/stm32f401blackpill/STM32F401VCTx_FLASH.ld
+++ b/hw/bsp/stm32f4/boards/stm32f401blackpill/STM32F401VCTx_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f4/boards/stm32f401blackpill/board.h b/hw/bsp/stm32f4/boards/stm32f401blackpill/board.h
index 0f820512c..e1fef7277 100644
--- a/hw/bsp/stm32f4/boards/stm32f401blackpill/board.h
+++ b/hw/bsp/stm32f4/boards/stm32f401blackpill/board.h
@@ -93,7 +93,7 @@ static inline void board_clock_init(void)
static inline void board_vbus_sense_init(void)
{
- // Blackpill doens't use VBUS sense (B device) explicitly disable it
+ // Blackpill doesn't use VBUS sense (B device) explicitly disable it
USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS;
USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN;
USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSASEN;
diff --git a/hw/bsp/stm32f4/boards/stm32f407disco/STM32F407VGTx_FLASH.ld b/hw/bsp/stm32f4/boards/stm32f407disco/STM32F407VGTx_FLASH.ld
index c41602720..aac6577ea 100644
--- a/hw/bsp/stm32f4/boards/stm32f407disco/STM32F407VGTx_FLASH.ld
+++ b/hw/bsp/stm32f4/boards/stm32f407disco/STM32F407VGTx_FLASH.ld
@@ -150,7 +150,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f4/boards/stm32f411blackpill/STM32F411CEUx_FLASH.ld b/hw/bsp/stm32f4/boards/stm32f411blackpill/STM32F411CEUx_FLASH.ld
index efea1e067..56dcea605 100644
--- a/hw/bsp/stm32f4/boards/stm32f411blackpill/STM32F411CEUx_FLASH.ld
+++ b/hw/bsp/stm32f4/boards/stm32f411blackpill/STM32F411CEUx_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f4/boards/stm32f411blackpill/board.h b/hw/bsp/stm32f4/boards/stm32f411blackpill/board.h
index 0f820512c..e1fef7277 100644
--- a/hw/bsp/stm32f4/boards/stm32f411blackpill/board.h
+++ b/hw/bsp/stm32f4/boards/stm32f411blackpill/board.h
@@ -93,7 +93,7 @@ static inline void board_clock_init(void)
static inline void board_vbus_sense_init(void)
{
- // Blackpill doens't use VBUS sense (B device) explicitly disable it
+ // Blackpill doesn't use VBUS sense (B device) explicitly disable it
USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS;
USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN;
USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSASEN;
diff --git a/hw/bsp/stm32f4/boards/stm32f411disco/STM32F411VETx_FLASH.ld b/hw/bsp/stm32f4/boards/stm32f411disco/STM32F411VETx_FLASH.ld
index 3a0ce526f..4477229ea 100644
--- a/hw/bsp/stm32f4/boards/stm32f411disco/STM32F411VETx_FLASH.ld
+++ b/hw/bsp/stm32f4/boards/stm32f411disco/STM32F411VETx_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f4/boards/stm32f412disco/STM32F412ZGTx_FLASH.ld b/hw/bsp/stm32f4/boards/stm32f412disco/STM32F412ZGTx_FLASH.ld
index b00b5dbe0..2372cc1d9 100644
--- a/hw/bsp/stm32f4/boards/stm32f412disco/STM32F412ZGTx_FLASH.ld
+++ b/hw/bsp/stm32f4/boards/stm32f412disco/STM32F412ZGTx_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f4/boards/stm32f412nucleo/STM32F412ZGTx_FLASH.ld b/hw/bsp/stm32f4/boards/stm32f412nucleo/STM32F412ZGTx_FLASH.ld
index b00b5dbe0..2372cc1d9 100644
--- a/hw/bsp/stm32f4/boards/stm32f412nucleo/STM32F412ZGTx_FLASH.ld
+++ b/hw/bsp/stm32f4/boards/stm32f412nucleo/STM32F412ZGTx_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f7/boards/stlinkv3mini/STM32F723xE_FLASH.ld b/hw/bsp/stm32f7/boards/stlinkv3mini/STM32F723xE_FLASH.ld
index 8645ce5c5..f4be85703 100644
--- a/hw/bsp/stm32f7/boards/stlinkv3mini/STM32F723xE_FLASH.ld
+++ b/hw/bsp/stm32f7/boards/stlinkv3mini/STM32F723xE_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f7/boards/stm32f723disco/STM32F723xE_FLASH.ld b/hw/bsp/stm32f7/boards/stm32f723disco/STM32F723xE_FLASH.ld
index 8645ce5c5..f4be85703 100644
--- a/hw/bsp/stm32f7/boards/stm32f723disco/STM32F723xE_FLASH.ld
+++ b/hw/bsp/stm32f7/boards/stm32f723disco/STM32F723xE_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f7/boards/stm32f746disco/STM32F746ZGTx_FLASH.ld b/hw/bsp/stm32f7/boards/stm32f746disco/STM32F746ZGTx_FLASH.ld
index 045ec76f9..eeb0e29f3 100644
--- a/hw/bsp/stm32f7/boards/stm32f746disco/STM32F746ZGTx_FLASH.ld
+++ b/hw/bsp/stm32f7/boards/stm32f746disco/STM32F746ZGTx_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f7/boards/stm32f746nucleo/STM32F746ZGTx_FLASH.ld b/hw/bsp/stm32f7/boards/stm32f746nucleo/STM32F746ZGTx_FLASH.ld
index b434a01b7..e1e60bc78 100644
--- a/hw/bsp/stm32f7/boards/stm32f746nucleo/STM32F746ZGTx_FLASH.ld
+++ b/hw/bsp/stm32f7/boards/stm32f746nucleo/STM32F746ZGTx_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f7/boards/stm32f767nucleo/STM32F767ZITx_FLASH.ld b/hw/bsp/stm32f7/boards/stm32f767nucleo/STM32F767ZITx_FLASH.ld
index 0b6d5a494..3785f9cbf 100644
--- a/hw/bsp/stm32f7/boards/stm32f767nucleo/STM32F767ZITx_FLASH.ld
+++ b/hw/bsp/stm32f7/boards/stm32f767nucleo/STM32F767ZITx_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32f7/boards/stm32f769disco/STM32F769ZITx_FLASH.ld b/hw/bsp/stm32f7/boards/stm32f769disco/STM32F769ZITx_FLASH.ld
index 378ed80dd..520a75539 100644
--- a/hw/bsp/stm32f7/boards/stm32f769disco/STM32F769ZITx_FLASH.ld
+++ b/hw/bsp/stm32f7/boards/stm32f769disco/STM32F769ZITx_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32g4/boards/stm32g474nucleo/STM32G474RETx_FLASH.ld b/hw/bsp/stm32g4/boards/stm32g474nucleo/STM32G474RETx_FLASH.ld
index 935d97c87..9c327483b 100644
--- a/hw/bsp/stm32g4/boards/stm32g474nucleo/STM32G474RETx_FLASH.ld
+++ b/hw/bsp/stm32g4/boards/stm32g474nucleo/STM32G474RETx_FLASH.ld
@@ -16,7 +16,7 @@
**
** Target : STMicroelectronics STM32
**
-** Distribution: The file is distributed as is, without any warranty
+** Distribution: The file is distributed �as is,� without any warranty
** of any kind.
**
*****************************************************************************
@@ -151,7 +151,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32g4/boards/stm32g474nucleo/board.h b/hw/bsp/stm32g4/boards/stm32g474nucleo/board.h
index eab0bd5f0..aa2bf20bb 100644
--- a/hw/bsp/stm32g4/boards/stm32g474nucleo/board.h
+++ b/hw/bsp/stm32g4/boards/stm32g474nucleo/board.h
@@ -66,7 +66,7 @@ static inline void board_clock_init(void)
// Configure the main internal regulator output voltage
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
- // Initializes the CPU, AHB and APB busses clocks
+ // Initializes the CPU, AHB and APB buses clocks
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48 | RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
@@ -79,7 +79,7 @@ static inline void board_clock_init(void)
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
- // Initializes the CPU, AHB and APB busses clocks
+ // Initializes the CPU, AHB and APB buses clocks
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
diff --git a/hw/bsp/stm32h7/boards/stm32h723nucleo/board.h b/hw/bsp/stm32h7/boards/stm32h723nucleo/board.h
new file mode 100644
index 000000000..56a48ec24
--- /dev/null
+++ b/hw/bsp/stm32h7/boards/stm32h723nucleo/board.h
@@ -0,0 +1,131 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2021, Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+
+#ifndef BOARD_H_
+#define BOARD_H_
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+#define LED_PORT GPIOB
+#define LED_PIN GPIO_PIN_0
+#define LED_STATE_ON 1
+
+#define BUTTON_PORT GPIOC
+#define BUTTON_PIN GPIO_PIN_13
+#define BUTTON_STATE_ACTIVE 1
+
+#define UART_DEV USART3
+#define UART_CLK_EN __HAL_RCC_USART3_CLK_ENABLE
+#define UART_GPIO_PORT GPIOD
+#define UART_GPIO_AF GPIO_AF7_USART3
+#define UART_TX_PIN GPIO_PIN_8
+#define UART_RX_PIN GPIO_PIN_9
+
+// VBUS Sense detection
+#define OTG_FS_VBUS_SENSE 1
+#define OTG_HS_VBUS_SENSE 0
+
+// STM32F723 has only one USB HS peripheral
+// Nucleo board does not have ULPI so USB will operate in FS mode only
+// For the rest of the synopsys driver it is FS device however there
+// is only USB_OTG_HS defined. Here are required conversions to
+// make peripheral FS.
+#define __HAL_RCC_USB2_OTG_FS_CLK_ENABLE __HAL_RCC_USB1_OTG_HS_CLK_ENABLE
+#define GPIO_AF10_OTG2_HS GPIO_AF10_OTG1_HS
+#define USB_OTG_FS USB_OTG_HS
+
+//--------------------------------------------------------------------+
+// RCC Clock
+//--------------------------------------------------------------------+
+static inline void board_stm32h7_clock_init(void)
+{
+ RCC_ClkInitTypeDef RCC_ClkInitStruct;
+ RCC_OscInitTypeDef RCC_OscInitStruct;
+
+ /* The PWR block is always enabled on the H7 series- there is no clock
+ enable. For now, use the default VOS3 scale mode (lowest) and limit clock
+ frequencies to avoid potential current draw problems from bus
+ power when using the max clock speeds throughout the chip. */
+
+ /* Enable HSE Oscillator and activate PLL1 with HSE as source */
+ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
+ RCC_OscInitStruct.HSEState = RCC_HSE_ON;
+ RCC_OscInitStruct.HSIState = RCC_HSI_OFF;
+ RCC_OscInitStruct.CSIState = RCC_CSI_OFF;
+ RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
+ RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
+ RCC_OscInitStruct.PLL.PLLM = HSE_VALUE/1000000;
+ RCC_OscInitStruct.PLL.PLLN = 336;
+ RCC_OscInitStruct.PLL.PLLP = 2;
+ RCC_OscInitStruct.PLL.PLLQ = 7;
+ RCC_OscInitStruct.PLL.PLLR = 2; /* Unused */
+ RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_0;
+ RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOMEDIUM;
+ RCC_OscInitStruct.PLL.PLLFRACN = 0;
+ HAL_RCC_OscConfig(&RCC_OscInitStruct);
+
+ RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | \
+ RCC_CLOCKTYPE_D1PCLK1 | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 | \
+ RCC_CLOCKTYPE_D3PCLK1);
+ RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
+ RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
+ RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV1;
+
+ /* Unlike on the STM32F4 family, it appears the maximum APB frequencies are
+ device-dependent- 120 MHz for this board according to Figure 2 of
+ the datasheet. Dividing by half will be safe for now. */
+ RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
+ RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
+ RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
+ RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
+
+ /* 4 wait states required for 168MHz and VOS3. */
+ HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4);
+
+ /* Like on F4, on H7, USB's actual peripheral clock and bus clock are
+ separate. However, the main system PLL (PLL1) doesn't have a direct
+ connection to the USB peripheral clock to generate 48 MHz, so we do this
+ dance. This will connect PLL1's Q output to the USB peripheral clock. */
+ RCC_PeriphCLKInitTypeDef RCC_PeriphCLKInitStruct;
+
+ RCC_PeriphCLKInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USB;
+ RCC_PeriphCLKInitStruct.UsbClockSelection = RCC_USBCLKSOURCE_PLL;
+ HAL_RCCEx_PeriphCLKConfig(&RCC_PeriphCLKInitStruct);
+}
+
+static inline void board_stm32h7_post_init(void)
+{
+ // For this board does nothing
+}
+
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif
diff --git a/hw/bsp/stm32h7/boards/stm32h723nucleo/board.mk b/hw/bsp/stm32h7/boards/stm32h723nucleo/board.mk
new file mode 100644
index 000000000..bbd0a0e58
--- /dev/null
+++ b/hw/bsp/stm32h7/boards/stm32h723nucleo/board.mk
@@ -0,0 +1,13 @@
+CFLAGS += -DSTM32H723xx -DHSE_VALUE=8000000
+
+# Default is FulSpeed port
+PORT ?= 0
+
+SRC_S += $(ST_CMSIS)/Source/Templates/gcc/startup_stm32h723xx.s
+LD_FILE = $(BOARD_PATH)/stm32h723xx_flash.ld
+
+# For flash-jlink target
+JLINK_DEVICE = stm32h723zg
+
+# flash target using on-board stlink
+flash: flash-stlink
diff --git a/hw/bsp/stm32h7/boards/stm32h723nucleo/stm32h723xx_flash.ld b/hw/bsp/stm32h7/boards/stm32h723nucleo/stm32h723xx_flash.ld
new file mode 100644
index 000000000..05e0d4e26
--- /dev/null
+++ b/hw/bsp/stm32h7/boards/stm32h723nucleo/stm32h723xx_flash.ld
@@ -0,0 +1,192 @@
+/*
+******************************************************************************
+**
+** File : LinkerScript.ld
+**
+** Author : STM32CubeIDE
+**
+** Abstract : Linker script for STM32H7 series
+** 1024Kbytes FLASH and 560Kbytes RAM
+**
+** Set heap size, stack size and stack location according
+** to application requirements.
+**
+** Set memory bank area and size if external memory is used.
+**
+** Target : STMicroelectronics STM32
+**
+** Distribution: The file is distributed as is, without any warranty
+** of any kind.
+**
+*****************************************************************************
+** @attention
+**
+** Copyright (c) 2021 STMicroelectronics.
+** All rights reserved.
+**
+** This software is licensed under terms that can be found in the LICENSE file
+** in the root directory of this software component.
+** If no LICENSE file comes with this software, it is provided AS-IS.
+**
+****************************************************************************
+*/
+
+/* Entry Point */
+ENTRY(Reset_Handler)
+
+/* Highest address of the user mode stack */
+_estack = ORIGIN(RAM_D1) + LENGTH(RAM_D1); /* end of RAM */
+/* Generate a link error if heap and stack don't fit into RAM */
+_Min_Heap_Size = 0x200 ; /* required amount of heap */
+_Min_Stack_Size = 0x400 ; /* required amount of stack */
+
+/* Specify the memory areas */
+MEMORY
+{
+ ITCMRAM (xrw) : ORIGIN = 0x00000000, LENGTH = 64K
+ DTCMRAM (xrw) : ORIGIN = 0x20000000, LENGTH = 128K
+ FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 1024K
+ RAM_D1 (xrw) : ORIGIN = 0x24000000, LENGTH = 320K
+ RAM_D2 (xrw) : ORIGIN = 0x30000000, LENGTH = 32K
+ RAM_D3 (xrw) : ORIGIN = 0x38000000, LENGTH = 16K
+}
+
+/* Define output sections */
+SECTIONS
+{
+ /* The startup code goes first into FLASH */
+ .isr_vector :
+ {
+ . = ALIGN(4);
+ KEEP(*(.isr_vector)) /* Startup code */
+ . = ALIGN(4);
+ } >FLASH
+
+ /* The program code and other data goes into FLASH */
+ .text :
+ {
+ . = ALIGN(4);
+ *(.text) /* .text sections (code) */
+ *(.text*) /* .text* sections (code) */
+ *(.glue_7) /* glue arm to thumb code */
+ *(.glue_7t) /* glue thumb to arm code */
+ *(.eh_frame)
+
+ KEEP (*(.init))
+ KEEP (*(.fini))
+
+ . = ALIGN(4);
+ _etext = .; /* define a global symbols at end of code */
+ } >FLASH
+
+ /* Constant data goes into FLASH */
+ .rodata :
+ {
+ . = ALIGN(4);
+ *(.rodata) /* .rodata sections (constants, strings, etc.) */
+ *(.rodata*) /* .rodata* sections (constants, strings, etc.) */
+ . = ALIGN(4);
+ } >FLASH
+
+ .ARM.extab : { *(.ARM.extab* .gnu.linkonce.armextab.*) } >FLASH
+ .ARM : {
+ __exidx_start = .;
+ *(.ARM.exidx*)
+ __exidx_end = .;
+ } >FLASH
+
+ .preinit_array :
+ {
+ PROVIDE_HIDDEN (__preinit_array_start = .);
+ KEEP (*(.preinit_array*))
+ PROVIDE_HIDDEN (__preinit_array_end = .);
+ } >FLASH
+
+ .init_array :
+ {
+ PROVIDE_HIDDEN (__init_array_start = .);
+ KEEP (*(SORT(.init_array.*)))
+ KEEP (*(.init_array*))
+ PROVIDE_HIDDEN (__init_array_end = .);
+ } >FLASH
+
+ .fini_array :
+ {
+ PROVIDE_HIDDEN (__fini_array_start = .);
+ KEEP (*(SORT(.fini_array.*)))
+ KEEP (*(.fini_array*))
+ PROVIDE_HIDDEN (__fini_array_end = .);
+ } >FLASH
+
+ /* used by the startup to initialize data */
+ _sidata = LOADADDR(.data);
+
+ /* Initialized data sections goes into RAM, load LMA copy after code */
+ .data :
+ {
+ . = ALIGN(4);
+ _sdata = .; /* create a global symbol at data start */
+ *(.data) /* .data sections */
+ *(.data*) /* .data* sections */
+ *(.RamFunc) /* .RamFunc sections */
+ *(.RamFunc*) /* .RamFunc* sections */
+
+ . = ALIGN(4);
+ _edata = .; /* define a global symbol at data end */
+ } >RAM_D1 AT> FLASH
+
+ /* Uninitialized data section */
+ . = ALIGN(4);
+ .bss :
+ {
+ /* This is used by the startup in order to initialize the .bss section */
+ _sbss = .; /* define a global symbol at bss start */
+ __bss_start__ = _sbss;
+ *(.bss)
+ *(.bss*)
+ *(COMMON)
+
+ . = ALIGN(4);
+ _ebss = .; /* define a global symbol at bss end */
+ __bss_end__ = _ebss;
+ } >RAM_D1
+
+ /* User_heap_stack section, used to check that there is enough RAM left */
+ ._user_heap_stack :
+ {
+ . = ALIGN(8);
+ PROVIDE ( end = . );
+ PROVIDE ( _end = . );
+ . = . + _Min_Heap_Size;
+ . = . + _Min_Stack_Size;
+ . = ALIGN(8);
+ } >RAM_D1
+
+ .usbx_data 0x24027000 (NOLOAD):
+ {
+ *(.UsbHpcdSection)
+
+ } >RAM_D1
+
+ .uart_bss 0x24028000 (NOLOAD):
+ {
+ *(.UsbxAppSection)
+
+ } >RAM_D1
+
+ .usbx_bss 0x24029000 (NOLOAD):
+ {
+ *(.UsbxPoolSection)
+
+ } >RAM_D1
+
+ /* Remove information from the standard libraries */
+ /DISCARD/ :
+ {
+ libc.a ( * )
+ libm.a ( * )
+ libgcc.a ( * )
+ }
+
+ .ARM.attributes 0 : { *(.ARM.attributes) }
+}
diff --git a/hw/bsp/stm32h7/boards/stm32h743eval/stm32h743xx_flash.ld b/hw/bsp/stm32h7/boards/stm32h743eval/stm32h743xx_flash.ld
index 59b9ff4df..7ee40671c 100644
--- a/hw/bsp/stm32h7/boards/stm32h743eval/stm32h743xx_flash.ld
+++ b/hw/bsp/stm32h7/boards/stm32h743eval/stm32h743xx_flash.ld
@@ -134,7 +134,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32h7/boards/stm32h743nucleo/stm32h743xx_flash.ld b/hw/bsp/stm32h7/boards/stm32h743nucleo/stm32h743xx_flash.ld
index 59b9ff4df..7ee40671c 100644
--- a/hw/bsp/stm32h7/boards/stm32h743nucleo/stm32h743xx_flash.ld
+++ b/hw/bsp/stm32h7/boards/stm32h743nucleo/stm32h743xx_flash.ld
@@ -134,7 +134,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32h7/boards/waveshare_openh743i/STM32H743IITX_FLASH.ld b/hw/bsp/stm32h7/boards/waveshare_openh743i/STM32H743IITX_FLASH.ld
index 73d2dedb2..e99bb977b 100644
--- a/hw/bsp/stm32h7/boards/waveshare_openh743i/STM32H743IITX_FLASH.ld
+++ b/hw/bsp/stm32h7/boards/waveshare_openh743i/STM32H743IITX_FLASH.ld
@@ -138,7 +138,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32h7/boards/waveshare_openh743i/board.h b/hw/bsp/stm32h7/boards/waveshare_openh743i/board.h
index 09442c233..2b072e07c 100644
--- a/hw/bsp/stm32h7/boards/waveshare_openh743i/board.h
+++ b/hw/bsp/stm32h7/boards/waveshare_openh743i/board.h
@@ -186,7 +186,7 @@ static inline void timer_board_delay(TIM_HandleTypeDef* tim_hdl, uint32_t ms)
static inline void board_stm32h7_post_init(void)
{
- // walkaround for reseting the ULPI PHY using Timer since systick is not
+ // walkaround for resetting the ULPI PHY using Timer since systick is not
// available when RTOS is used.
// Init timer
diff --git a/hw/bsp/stm32h7/family.c b/hw/bsp/stm32h7/family.c
index 3964f427a..0aa8fe47f 100644
--- a/hw/bsp/stm32h7/family.c
+++ b/hw/bsp/stm32h7/family.c
@@ -66,10 +66,12 @@ void board_init(void)
__HAL_RCC_GPIOC_CLK_ENABLE(); // USB ULPI NXT
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
- __HAL_RCC_GPIOE_CLK_ENABLE();
+ __HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE(); // USB ULPI NXT
+#ifdef __HAL_RCC_GPIOI_CLK_ENABLE
__HAL_RCC_GPIOI_CLK_ENABLE(); // USB ULPI NXT
+#endif
__HAL_RCC_GPIOJ_CLK_ENABLE();
// Enable UART Clock
@@ -84,7 +86,9 @@ void board_init(void)
SysTick->CTRL &= ~1U;
// If freeRTOS is used, IRQ priority is limit by max syscall ( smaller is higher )
+#ifdef USB_OTG_FS_PERIPH_BASE
NVIC_SetPriority(OTG_FS_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
+#endif
NVIC_SetPriority(OTG_HS_IRQn, configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY );
#endif
diff --git a/hw/bsp/stm32l0/boards/stm32l052dap52/STM32L052K8Ux_FLASH.ld b/hw/bsp/stm32l0/boards/stm32l052dap52/STM32L052K8Ux_FLASH.ld
index 1bc16cc7b..dfefe59f1 100644
--- a/hw/bsp/stm32l0/boards/stm32l052dap52/STM32L052K8Ux_FLASH.ld
+++ b/hw/bsp/stm32l0/boards/stm32l052dap52/STM32L052K8Ux_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32l0538disco/STM32L053C8Tx_FLASH.ld b/hw/bsp/stm32l0538disco/STM32L053C8Tx_FLASH.ld
index 787418e09..79427d80b 100644
--- a/hw/bsp/stm32l0538disco/STM32L053C8Tx_FLASH.ld
+++ b/hw/bsp/stm32l0538disco/STM32L053C8Tx_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32l4/boards/stm32l412nucleo/STM32L412KBUx_FLASH.ld b/hw/bsp/stm32l4/boards/stm32l412nucleo/STM32L412KBUx_FLASH.ld
index 28cd96131..d4b86b3e3 100644
--- a/hw/bsp/stm32l4/boards/stm32l412nucleo/STM32L412KBUx_FLASH.ld
+++ b/hw/bsp/stm32l4/boards/stm32l412nucleo/STM32L412KBUx_FLASH.ld
@@ -145,7 +145,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32l4/boards/stm32l476disco/STM32L476VGTx_FLASH.ld b/hw/bsp/stm32l4/boards/stm32l476disco/STM32L476VGTx_FLASH.ld
index 98f468ac0..d6865f49d 100644
--- a/hw/bsp/stm32l4/boards/stm32l476disco/STM32L476VGTx_FLASH.ld
+++ b/hw/bsp/stm32l4/boards/stm32l476disco/STM32L476VGTx_FLASH.ld
@@ -144,7 +144,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32l4/boards/stm32l4r5nucleo/STM32L4RXxI_FLASH.ld b/hw/bsp/stm32l4/boards/stm32l4r5nucleo/STM32L4RXxI_FLASH.ld
index f93a16041..f77c72d1b 100644
--- a/hw/bsp/stm32l4/boards/stm32l4r5nucleo/STM32L4RXxI_FLASH.ld
+++ b/hw/bsp/stm32l4/boards/stm32l4r5nucleo/STM32L4RXxI_FLASH.ld
@@ -130,7 +130,7 @@ SECTIONS
. = ALIGN(4);
.bss :
{
- /* This is used by the startup in order to initialize the .bss secion */
+ /* This is used by the startup in order to initialize the .bss section */
_sbss = .; /* define a global symbol at bss start */
__bss_start__ = _sbss;
*(.bss)
diff --git a/hw/bsp/stm32u5/boards/stm32u575eval/board.h b/hw/bsp/stm32u5/boards/stm32u575eval/board.h
index 5c348b812..5562b95a8 100644
--- a/hw/bsp/stm32u5/boards/stm32u575eval/board.h
+++ b/hw/bsp/stm32u5/boards/stm32u575eval/board.h
@@ -69,7 +69,7 @@ static inline void board_clock_init(void)
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
- /** Initializes the CPU, AHB and APB busses clocks
+ /** Initializes the CPU, AHB and APB buses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48 | RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
@@ -92,7 +92,7 @@ static inline void board_clock_init(void)
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit);
- /** Initializes the CPU, AHB and APB busses clocks
+ /** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2 | RCC_CLOCKTYPE_PCLK3;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
diff --git a/hw/bsp/stm32wb/boards/stm32wb55nucleo/board.h b/hw/bsp/stm32wb/boards/stm32wb55nucleo/board.h
index f42c7d6dd..ea975df03 100644
--- a/hw/bsp/stm32wb/boards/stm32wb55nucleo/board.h
+++ b/hw/bsp/stm32wb/boards/stm32wb55nucleo/board.h
@@ -59,7 +59,7 @@ static inline void board_clock_init(void)
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
- // Initializes the CPU, AHB and APB busses clocks
+ // Initializes the CPU, AHB and APB buses clocks
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48 | RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
@@ -72,7 +72,7 @@ static inline void board_clock_init(void)
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV3;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
- // Initializes the CPU, AHB and APB busses clocks
+ // Initializes the CPU, AHB and APB buses clocks
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
diff --git a/hw/bsp/tm4c123/family.c b/hw/bsp/tm4c123/family.c
index b0947d6c4..87b94289b 100644
--- a/hw/bsp/tm4c123/family.c
+++ b/hw/bsp/tm4c123/family.c
@@ -39,7 +39,7 @@ static void board_uart_init (void)
UART0->LCRH = (0x3 << 5); // 8-bit, no parity, 1 stop bit
UART0->CC = 0x0; // Configure the UART clock source as system clock
- UART0->CTL = (1 << 0) | (1 << 8) | (1 << 9); // UART0 Enable, Transmit Enable, Recieve Enable
+ UART0->CTL = (1 << 0) | (1 << 8) | (1 << 9); // UART0 Enable, Transmit Enable, Receive Enable
}
static void initialize_board_led (GPIOA_Type *port, uint8_t PinMsk, uint8_t dirmsk)
diff --git a/hw/mcu/wch/ch32v307 b/hw/mcu/wch/ch32v307
new file mode 160000
index 000000000..17761f5cf
--- /dev/null
+++ b/hw/mcu/wch/ch32v307
@@ -0,0 +1 @@
+Subproject commit 17761f5cf9dbbf2dcf665b7c04934188add20082
diff --git a/lib/SEGGER_RTT/RTT/SEGGER_RTT.c b/lib/SEGGER_RTT/RTT/SEGGER_RTT.c
index 3ad2b1a12..a7ae013dd 100644
--- a/lib/SEGGER_RTT/RTT/SEGGER_RTT.c
+++ b/lib/SEGGER_RTT/RTT/SEGGER_RTT.c
@@ -1404,7 +1404,7 @@ int SEGGER_RTT_GetKey(void) {
* SEGGER_RTT_WaitKey
*
* Function description
-* Waits until at least one character is avaible in the SEGGER RTT buffer.
+* Waits until at least one character is available in the SEGGER RTT buffer.
* Once a character is available, it is read and this function returns.
*
* Return value
diff --git a/lib/networking/dnserver.c b/lib/networking/dnserver.c
index e4e7c3492..539cc2cea 100644
--- a/lib/networking/dnserver.c
+++ b/lib/networking/dnserver.c
@@ -93,11 +93,11 @@ static uint16_t get_uint16(const uint8_t *pnt)
static int parse_next_query(void *data, int size, dns_query_t *query)
{
int len;
- int lables;
+ int labels;
uint8_t *ptr;
len = 0;
- lables = 0;
+ labels = 0;
ptr = (uint8_t *)data;
while (true)
@@ -107,7 +107,7 @@ static int parse_next_query(void *data, int size, dns_query_t *query)
lable_len = *ptr++;
size--;
if (lable_len == 0) break;
- if (lables > 0)
+ if (labels > 0)
{
if (len == DNS_MAX_HOST_NAME_LEN) return -2;
query->name[len++] = '.';
@@ -118,7 +118,7 @@ static int parse_next_query(void *data, int size, dns_query_t *query)
len += lable_len;
ptr += lable_len;
size -= lable_len;
- lables++;
+ labels++;
}
if (size < 4) return -1;
diff --git a/lib/networking/ndis.h b/lib/networking/ndis.h
index 1c737574c..06258e6bb 100644
--- a/lib/networking/ndis.h
+++ b/lib/networking/ndis.h
@@ -22,7 +22,7 @@
* ntddndis.h modified by Benedikt Spranger
*
* Thanks to the cygwin development team,
- * espacially to Casper S. Hornstrup
+ * especially to Casper S. Hornstrup
*
* THIS SOFTWARE IS NOT COPYRIGHTED
*
diff --git a/lib/networking/rndis_reports.c b/lib/networking/rndis_reports.c
index ee611c883..d129466c8 100644
--- a/lib/networking/rndis_reports.c
+++ b/lib/networking/rndis_reports.c
@@ -220,7 +220,7 @@ static void rndis_handle_set_msg(void)
case OID_802_3_MULTICAST_LIST:
break;
- /* Power Managment: fails for now */
+ /* Power Management: fails for now */
case OID_PNP_ADD_WAKE_UP_PATTERN:
case OID_PNP_REMOVE_WAKE_UP_PATTERN:
case OID_PNP_ENABLE_WAKE_UP:
diff --git a/src/class/audio/audio.h b/src/class/audio/audio.h
index 6f9c1a6b5..ba497906b 100644
--- a/src/class/audio/audio.h
+++ b/src/class/audio/audio.h
@@ -721,11 +721,13 @@ typedef struct TU_ATTR_PACKED
uint8_t bLength ; ///< Size of this descriptor, in bytes: 17.
uint8_t bDescriptorType ; ///< Descriptor Type. Value: TUSB_DESC_CS_INTERFACE.
uint8_t bDescriptorSubType ; ///< Descriptor SubType. Value: AUDIO_CS_AC_INTERFACE_INPUT_TERMINAL.
+ uint8_t bTerminalID ; ///< Constant uniquely identifying the Terminal within the audio function. This value is used in all requests to address this terminal.
uint16_t wTerminalType ; ///< Constant characterizing the type of Terminal. See: audio_terminal_type_t for USB streaming and audio_terminal_input_type_t for other input types.
uint8_t bAssocTerminal ; ///< ID of the Output Terminal to which this Input Terminal is associated.
uint8_t bCSourceID ; ///< ID of the Clock Entity to which this Input Terminal is connected.
uint8_t bNrChannels ; ///< Number of logical output channels in the Terminal’s output audio channel cluster.
uint32_t bmChannelConfig ; ///< Describes the spatial location of the logical channels. See:audio_channel_config_t.
+ uint8_t iChannelNames ; ///< Index of a string descriptor, describing the name of the first logical channel.
uint16_t bmControls ; ///< See: audio_terminal_input_control_pos_t.
uint8_t iTerminal ; ///< Index of a string descriptor, describing the Input Terminal.
} audio_desc_input_terminal_t;
diff --git a/src/class/cdc/cdc.h b/src/class/cdc/cdc.h
index e345139ea..2fecde3ca 100644
--- a/src/class/cdc/cdc.h
+++ b/src/class/cdc/cdc.h
@@ -41,16 +41,6 @@
/** \defgroup ClassDriver_CDC_Common Common Definitions
* @{ */
-// TODO remove
-/// CDC Pipe ID, used to indicate which pipe the API is addressing to (Notification, Out, In)
-typedef enum
-{
- CDC_PIPE_NOTIFICATION , ///< Notification pipe
- CDC_PIPE_DATA_IN , ///< Data in pipe
- CDC_PIPE_DATA_OUT , ///< Data out pipe
- CDC_PIPE_ERROR , ///< Invalid Pipe ID
-}cdc_pipeid_t;
-
//--------------------------------------------------------------------+
// CDC Communication Interface Class
//--------------------------------------------------------------------+
@@ -192,8 +182,30 @@ typedef enum
CDC_REQUEST_MDLM_SEMANTIC_MODEL = 0x60,
}cdc_management_request_t;
+enum
+{
+ CDC_CONTROL_LINE_STATE_DTR = 0x01,
+ CDC_CONTROL_LINE_STATE_RTS = 0x02,
+};
+
+enum
+{
+ CDC_LINE_CONDING_STOP_BITS_1 = 0, // 1 bit
+ CDC_LINE_CONDING_STOP_BITS_1_5 = 1, // 1.5 bits
+ CDC_LINE_CONDING_STOP_BITS_2 = 2, // 2 bits
+};
+
+enum
+{
+ CDC_LINE_CODING_PARITY_NONE = 0,
+ CDC_LINE_CODING_PARITY_ODD = 1,
+ CDC_LINE_CODING_PARITY_EVEN = 2,
+ CDC_LINE_CODING_PARITY_MARK = 3,
+ CDC_LINE_CODING_PARITY_SPACE = 4,
+};
+
//--------------------------------------------------------------------+
-// Management Elemenent Notification (Notification Endpoint)
+// Management Element Notification (Notification Endpoint)
//--------------------------------------------------------------------+
/// 6.3 Notification Codes
@@ -390,8 +402,8 @@ TU_VERIFY_STATIC(sizeof(cdc_line_coding_t) == 7, "size is not correct");
typedef struct TU_ATTR_PACKED
{
- uint16_t dte_is_present : 1; ///< Indicates to DCE if DTE is presentor not. This signal corresponds to V.24 signal 108/2 and RS-232 signal DTR.
- uint16_t half_duplex_carrier_control : 1;
+ uint16_t dtr : 1;
+ uint16_t rts : 1;
uint16_t : 14;
} cdc_line_control_state_t;
diff --git a/src/class/cdc/cdc_device.c b/src/class/cdc/cdc_device.c
index fab6f0035..8d10a416c 100644
--- a/src/class/cdc/cdc_device.c
+++ b/src/class/cdc/cdc_device.c
@@ -62,10 +62,8 @@ typedef struct
uint8_t rx_ff_buf[CFG_TUD_CDC_RX_BUFSIZE];
uint8_t tx_ff_buf[CFG_TUD_CDC_TX_BUFSIZE];
-#if CFG_FIFO_MUTEX
- osal_mutex_def_t rx_ff_mutex;
- osal_mutex_def_t tx_ff_mutex;
-#endif
+ OSAL_MUTEX_DEF(rx_ff_mutex);
+ OSAL_MUTEX_DEF(tx_ff_mutex);
// Endpoint Transfer buffer
CFG_TUSB_MEM_ALIGN uint8_t epout_buf[CFG_TUD_CDC_EP_BUFSIZE];
@@ -171,7 +169,8 @@ uint32_t tud_cdc_n_write(uint8_t itf, void const* buffer, uint32_t bufsize)
uint16_t ret = tu_fifo_write_n(&p_cdc->tx_ff, buffer, (uint16_t) bufsize);
// flush if queue more than packet size
- if ( tu_fifo_count(&p_cdc->tx_ff) >= BULK_PACKET_SIZE )
+ // may need to suppress -Wunreachable-code since most of the time CFG_TUD_CDC_TX_BUFSIZE < BULK_PACKET_SIZE
+ if ( (tu_fifo_count(&p_cdc->tx_ff) >= BULK_PACKET_SIZE) || ((CFG_TUD_CDC_TX_BUFSIZE < BULK_PACKET_SIZE) && tu_fifo_full(&p_cdc->tx_ff)) )
{
tud_cdc_n_write_flush(itf);
}
@@ -247,10 +246,8 @@ void cdcd_init(void)
// In this way, the most current data is prioritized.
tu_fifo_config(&p_cdc->tx_ff, p_cdc->tx_ff_buf, TU_ARRAY_SIZE(p_cdc->tx_ff_buf), 1, true);
-#if CFG_FIFO_MUTEX
tu_fifo_config_mutex(&p_cdc->rx_ff, NULL, osal_mutex_create(&p_cdc->rx_ff_mutex));
tu_fifo_config_mutex(&p_cdc->tx_ff, osal_mutex_create(&p_cdc->tx_ff_mutex), NULL);
-#endif
}
}
@@ -435,7 +432,7 @@ bool cdcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint32_
// Received new data
if ( ep_addr == p_cdc->ep_out )
{
- tu_fifo_write_n(&p_cdc->rx_ff, &p_cdc->epout_buf, (uint16_t) xferred_bytes);
+ tu_fifo_write_n(&p_cdc->rx_ff, p_cdc->epout_buf, (uint16_t) xferred_bytes);
// Check for wanted char and invoke callback if needed
if ( tud_cdc_rx_wanted_cb && (((signed char) p_cdc->wanted_char) != -1) )
diff --git a/src/class/cdc/cdc_device.h b/src/class/cdc/cdc_device.h
index fbc7162a3..f8a004df4 100644
--- a/src/class/cdc/cdc_device.h
+++ b/src/class/cdc/cdc_device.h
@@ -27,7 +27,6 @@
#ifndef _TUSB_CDC_DEVICE_H_
#define _TUSB_CDC_DEVICE_H_
-#include "common/tusb_common.h"
#include "cdc.h"
//--------------------------------------------------------------------+
@@ -81,7 +80,7 @@ int32_t tud_cdc_n_read_char (uint8_t itf);
// Clear the received FIFO
void tud_cdc_n_read_flush (uint8_t itf);
-// Get a byte from FIFO at the specified position without removing it
+// Get a byte from FIFO without removing it
bool tud_cdc_n_peek (uint8_t itf, uint8_t* ui8);
// Write bytes to TX FIFO, data may remain in the FIFO for a while
@@ -135,7 +134,7 @@ TU_ATTR_WEAK void tud_cdc_rx_cb(uint8_t itf);
// Invoked when received `wanted_char`
TU_ATTR_WEAK void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char);
-// Invoked when space becomes available in TX buffer
+// Invoked when a TX is complete and therefore space becomes available in TX buffer
TU_ATTR_WEAK void tud_cdc_tx_complete_cb(uint8_t itf);
// Invoked when line state DTR & RTS are changed via SET_CONTROL_LINE_STATE
diff --git a/src/class/cdc/cdc_host.c b/src/class/cdc/cdc_host.c
index ee824cb4e..e9c3d34cb 100644
--- a/src/class/cdc/cdc_host.c
+++ b/src/class/cdc/cdc_host.c
@@ -33,96 +33,260 @@
#include "cdc_host.h"
+
+// Debug level, TUSB_CFG_DEBUG must be at least this level for debug message
+#define CDCH_DEBUG 2
+
+#define TU_LOG_CDCH(...) TU_LOG(CDCH_DEBUG, __VA_ARGS__)
+
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
-typedef struct {
- uint8_t itf_num;
- uint8_t itf_protocol;
- uint8_t ep_notif;
- uint8_t ep_in;
- uint8_t ep_out;
+typedef struct {
+ uint8_t daddr;
+ uint8_t bInterfaceNumber;
+ uint8_t bInterfaceSubClass;
+ uint8_t bInterfaceProtocol;
cdc_acm_capability_t acm_capability;
+ uint8_t ep_notif;
-} cdch_data_t;
+ cdc_line_coding_t line_coding; // Baudrate, stop bits, parity, data width
+ uint8_t line_state; // DTR (bit0), RTS (bit1)
+
+ tuh_xfer_cb_t user_control_cb;
+
+ struct {
+ tu_edpt_stream_t tx;
+ tu_edpt_stream_t rx;
+
+ uint8_t tx_ff_buf[CFG_TUH_CDC_TX_BUFSIZE];
+ CFG_TUSB_MEM_ALIGN uint8_t tx_ep_buf[CFG_TUH_CDC_TX_EPSIZE];
+
+ uint8_t rx_ff_buf[CFG_TUH_CDC_TX_BUFSIZE];
+ CFG_TUSB_MEM_ALIGN uint8_t rx_ep_buf[CFG_TUH_CDC_TX_EPSIZE];
+ } stream;
+
+} cdch_interface_t;
//--------------------------------------------------------------------+
// INTERNAL OBJECT & FUNCTION DECLARATION
//--------------------------------------------------------------------+
-static cdch_data_t cdch_data[CFG_TUH_DEVICE_MAX];
-static inline cdch_data_t* get_itf(uint8_t dev_addr)
+CFG_TUSB_MEM_SECTION
+static cdch_interface_t cdch_data[CFG_TUH_CDC];
+
+static inline cdch_interface_t* get_itf(uint8_t idx)
{
- return &cdch_data[dev_addr-1];
+ TU_ASSERT(idx < CFG_TUH_CDC, NULL);
+ cdch_interface_t* p_cdc = &cdch_data[idx];
+
+ return (p_cdc->daddr != 0) ? p_cdc : NULL;
}
-bool tuh_cdc_mounted(uint8_t dev_addr)
+static inline uint8_t get_idx_by_ep_addr(uint8_t daddr, uint8_t ep_addr)
{
- cdch_data_t* cdc = get_itf(dev_addr);
- return cdc->ep_in && cdc->ep_out;
-}
-
-bool tuh_cdc_is_busy(uint8_t dev_addr, cdc_pipeid_t pipeid)
-{
- if ( !tuh_cdc_mounted(dev_addr) ) return false;
-
- cdch_data_t const * p_cdc = get_itf(dev_addr);
-
- switch (pipeid)
+ for(uint8_t i=0; iep_notif );
-
- case CDC_PIPE_DATA_IN:
- return usbh_edpt_busy(dev_addr, p_cdc->ep_in );
-
- case CDC_PIPE_DATA_OUT:
- return usbh_edpt_busy(dev_addr, p_cdc->ep_out );
-
- default:
- return false;
+ cdch_interface_t* p_cdc = &cdch_data[i];
+ if ( (p_cdc->daddr == daddr) &&
+ (ep_addr == p_cdc->ep_notif || ep_addr == p_cdc->stream.rx.ep_addr || ep_addr == p_cdc->stream.tx.ep_addr))
+ {
+ return i;
+ }
}
+
+ return TUSB_INDEX_INVALID;
+}
+
+
+static cdch_interface_t* find_new_itf(void)
+{
+ for(uint8_t i=0; idaddr == daddr && p_cdc->bInterfaceNumber == itf_num) return i;
+ }
+
+ return TUSB_INDEX_INVALID;
}
-bool tuh_cdc_send(uint8_t dev_addr, void const * p_data, uint32_t length, bool is_notify)
+bool tuh_cdc_itf_get_info(uint8_t idx, tuh_cdc_itf_info_t* info)
{
- (void) is_notify;
- TU_VERIFY( tuh_cdc_mounted(dev_addr) );
- TU_VERIFY( p_data != NULL && length);
+ cdch_interface_t* p_cdc = get_itf(idx);
+ TU_VERIFY(p_cdc && info);
- uint8_t const ep_out = cdch_data[dev_addr-1].ep_out;
- if ( usbh_edpt_busy(dev_addr, ep_out) ) return false;
+ info->daddr = p_cdc->daddr;
+ info->bInterfaceNumber = p_cdc->bInterfaceNumber;
+ info->bInterfaceSubClass = p_cdc->bInterfaceSubClass;
+ info->bInterfaceProtocol = p_cdc->bInterfaceProtocol;
- return usbh_edpt_xfer(dev_addr, ep_out, (void*)(uintptr_t) p_data, (uint16_t) length);
+ return true;
}
-bool tuh_cdc_receive(uint8_t dev_addr, void * p_buffer, uint32_t length, bool is_notify)
+bool tuh_cdc_mounted(uint8_t idx)
{
- (void) is_notify;
- TU_VERIFY( tuh_cdc_mounted(dev_addr) );
- TU_VERIFY( p_buffer != NULL && length );
-
- uint8_t const ep_in = cdch_data[dev_addr-1].ep_in;
- if ( usbh_edpt_busy(dev_addr, ep_in) ) return false;
-
- return usbh_edpt_xfer(dev_addr, ep_in, p_buffer, (uint16_t) length);
+ cdch_interface_t* p_cdc = get_itf(idx);
+ return p_cdc != NULL;
}
-bool tuh_cdc_set_control_line_state(uint8_t dev_addr, bool dtr, bool rts, tuh_xfer_cb_t complete_cb)
+bool tuh_cdc_get_dtr(uint8_t idx)
{
- cdch_data_t const * p_cdc = get_itf(dev_addr);
+ cdch_interface_t* p_cdc = get_itf(idx);
+ TU_VERIFY(p_cdc);
+
+ return (p_cdc->line_state & CDC_CONTROL_LINE_STATE_DTR) ? true : false;
+}
+
+bool tuh_cdc_get_rts(uint8_t idx)
+{
+ cdch_interface_t* p_cdc = get_itf(idx);
+ TU_VERIFY(p_cdc);
+
+ return (p_cdc->line_state & CDC_CONTROL_LINE_STATE_RTS) ? true : false;
+}
+
+bool tuh_cdc_get_local_line_coding(uint8_t idx, cdc_line_coding_t* line_coding)
+{
+ cdch_interface_t* p_cdc = get_itf(idx);
+ TU_VERIFY(p_cdc);
+
+ *line_coding = p_cdc->line_coding;
+
+ return true;
+}
+
+//--------------------------------------------------------------------+
+// Write
+//--------------------------------------------------------------------+
+
+uint32_t tuh_cdc_write(uint8_t idx, void const* buffer, uint32_t bufsize)
+{
+ cdch_interface_t* p_cdc = get_itf(idx);
+ TU_VERIFY(p_cdc);
+
+ return tu_edpt_stream_write(&p_cdc->stream.tx, buffer, bufsize);
+}
+
+uint32_t tuh_cdc_write_flush(uint8_t idx)
+{
+ cdch_interface_t* p_cdc = get_itf(idx);
+ TU_VERIFY(p_cdc);
+
+ return tu_edpt_stream_write_xfer(&p_cdc->stream.tx);
+}
+
+bool tuh_cdc_write_clear(uint8_t idx)
+{
+ cdch_interface_t* p_cdc = get_itf(idx);
+ TU_VERIFY(p_cdc);
+
+ return tu_edpt_stream_clear(&p_cdc->stream.tx);
+}
+
+uint32_t tuh_cdc_write_available(uint8_t idx)
+{
+ cdch_interface_t* p_cdc = get_itf(idx);
+ TU_VERIFY(p_cdc);
+
+ return tu_edpt_stream_write_available(&p_cdc->stream.tx);
+}
+
+//--------------------------------------------------------------------+
+// Read
+//--------------------------------------------------------------------+
+
+uint32_t tuh_cdc_read (uint8_t idx, void* buffer, uint32_t bufsize)
+{
+ cdch_interface_t* p_cdc = get_itf(idx);
+ TU_VERIFY(p_cdc);
+
+ return tu_edpt_stream_read(&p_cdc->stream.rx, buffer, bufsize);
+}
+
+uint32_t tuh_cdc_read_available(uint8_t idx)
+{
+ cdch_interface_t* p_cdc = get_itf(idx);
+ TU_VERIFY(p_cdc);
+
+ return tu_edpt_stream_read_available(&p_cdc->stream.rx);
+}
+
+bool tuh_cdc_peek(uint8_t idx, uint8_t* ch)
+{
+ cdch_interface_t* p_cdc = get_itf(idx);
+ TU_VERIFY(p_cdc);
+
+ return tu_edpt_stream_peek(&p_cdc->stream.rx, ch);
+}
+
+bool tuh_cdc_read_clear (uint8_t idx)
+{
+ cdch_interface_t* p_cdc = get_itf(idx);
+ TU_VERIFY(p_cdc);
+
+ bool ret = tu_edpt_stream_clear(&p_cdc->stream.rx);
+ tu_edpt_stream_read_xfer(&p_cdc->stream.rx);
+ return ret;
+}
+
+//--------------------------------------------------------------------+
+// Control Endpoint API
+//--------------------------------------------------------------------+
+
+// internal control complete to update state such as line state, encoding
+static void cdch_internal_control_complete(tuh_xfer_t* xfer)
+{
+ uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
+ uint8_t idx = tuh_cdc_itf_get_index(xfer->daddr, itf_num);
+ cdch_interface_t* p_cdc = get_itf(idx);
+ TU_ASSERT(p_cdc, );
+
+ if (xfer->result == XFER_RESULT_SUCCESS)
+ {
+ switch(xfer->setup->bRequest)
+ {
+ case CDC_REQUEST_SET_CONTROL_LINE_STATE:
+ p_cdc->line_state = (uint8_t) tu_le16toh(xfer->setup->wValue);
+ break;
+
+ case CDC_REQUEST_SET_LINE_CODING:
+ {
+ uint16_t const len = tu_min16(sizeof(cdc_line_coding_t), tu_le16toh(xfer->setup->wLength));
+ memcpy(&p_cdc->line_coding, xfer->buffer, len);
+ }
+ break;
+
+ default: break;
+ }
+ }
+
+ xfer->complete_cb = p_cdc->user_control_cb;
+ xfer->complete_cb(xfer);
+}
+
+bool tuh_cdc_set_control_line_state(uint8_t idx, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
+{
+ cdch_interface_t* p_cdc = get_itf(idx);
+ TU_VERIFY(p_cdc && p_cdc->acm_capability.support_line_request);
+
+ TU_LOG_CDCH("CDC Set Control Line State\r\n");
tusb_control_request_t const request =
{
@@ -133,36 +297,154 @@ bool tuh_cdc_set_control_line_state(uint8_t dev_addr, bool dtr, bool rts, tuh_xf
.direction = TUSB_DIR_OUT
},
.bRequest = CDC_REQUEST_SET_CONTROL_LINE_STATE,
- .wValue = tu_htole16((uint16_t) ((dtr ? 1u : 0u) | (rts ? 2u : 0u))),
- .wIndex = tu_htole16(p_cdc->itf_num),
+ .wValue = tu_htole16(line_state),
+ .wIndex = tu_htole16((uint16_t) p_cdc->bInterfaceNumber),
.wLength = 0
};
+ p_cdc->user_control_cb = complete_cb;
tuh_xfer_t xfer =
{
- .daddr = dev_addr,
+ .daddr = p_cdc->daddr,
.ep_addr = 0,
.setup = &request,
.buffer = NULL,
- .complete_cb = complete_cb,
- .user_data = 0
+ .complete_cb = cdch_internal_control_complete,
+ .user_data = user_data
+ };
+
+ return tuh_control_xfer(&xfer);
+}
+
+bool tuh_cdc_set_line_coding(uint8_t idx, cdc_line_coding_t const* line_coding, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
+{
+ cdch_interface_t* p_cdc = get_itf(idx);
+ TU_VERIFY(p_cdc && p_cdc->acm_capability.support_line_request);
+
+ TU_LOG_CDCH("CDC Set Line Conding\r\n");
+
+ tusb_control_request_t const request =
+ {
+ .bmRequestType_bit =
+ {
+ .recipient = TUSB_REQ_RCPT_INTERFACE,
+ .type = TUSB_REQ_TYPE_CLASS,
+ .direction = TUSB_DIR_OUT
+ },
+ .bRequest = CDC_REQUEST_SET_LINE_CODING,
+ .wValue = 0,
+ .wIndex = tu_htole16(p_cdc->bInterfaceNumber),
+ .wLength = tu_htole16(sizeof(cdc_line_coding_t))
+ };
+
+ // use usbh enum buf to hold line coding since user line_coding variable may not live long enough
+ // for the transfer to complete
+ uint8_t* enum_buf = usbh_get_enum_buf();
+ memcpy(enum_buf, line_coding, sizeof(cdc_line_coding_t));
+
+ p_cdc->user_control_cb = complete_cb;
+ tuh_xfer_t xfer =
+ {
+ .daddr = p_cdc->daddr,
+ .ep_addr = 0,
+ .setup = &request,
+ .buffer = enum_buf,
+ .complete_cb = cdch_internal_control_complete,
+ .user_data = user_data
};
return tuh_control_xfer(&xfer);
}
//--------------------------------------------------------------------+
-// USBH-CLASS DRIVER API
+// CLASS-USBH API
//--------------------------------------------------------------------+
+
void cdch_init(void)
{
tu_memclr(cdch_data, sizeof(cdch_data));
+
+ for(size_t i=0; istream.tx, true, true, false,
+ p_cdc->stream.tx_ff_buf, CFG_TUH_CDC_TX_BUFSIZE,
+ p_cdc->stream.tx_ep_buf, CFG_TUH_CDC_TX_EPSIZE);
+
+ tu_edpt_stream_init(&p_cdc->stream.rx, true, false, false,
+ p_cdc->stream.rx_ff_buf, CFG_TUH_CDC_RX_BUFSIZE,
+ p_cdc->stream.rx_ep_buf, CFG_TUH_CDC_RX_EPSIZE);
+ }
}
-bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *itf_desc, uint16_t max_len)
+void cdch_close(uint8_t daddr)
+{
+ for(uint8_t idx=0; idxdaddr == daddr)
+ {
+ // Invoke application callback
+ if (tuh_cdc_umount_cb) tuh_cdc_umount_cb(idx);
+
+ //tu_memclr(p_cdc, sizeof(cdch_interface_t));
+ p_cdc->daddr = 0;
+ p_cdc->bInterfaceNumber = 0;
+ tu_edpt_stream_close(&p_cdc->stream.tx);
+ tu_edpt_stream_close(&p_cdc->stream.rx);
+ }
+ }
+}
+
+bool cdch_xfer_cb(uint8_t daddr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes)
+{
+ // TODO handle stall response, retry failed transfer ...
+ TU_ASSERT(event == XFER_RESULT_SUCCESS);
+
+ uint8_t const idx = get_idx_by_ep_addr(daddr, ep_addr);
+ cdch_interface_t * p_cdc = get_itf(idx);
+ TU_ASSERT(p_cdc);
+
+ if ( ep_addr == p_cdc->stream.tx.ep_addr )
+ {
+ // invoke tx complete callback to possibly refill tx fifo
+ if (tuh_cdc_tx_complete_cb) tuh_cdc_tx_complete_cb(idx);
+
+ if ( 0 == tu_edpt_stream_write_xfer(&p_cdc->stream.tx) )
+ {
+ // If there is no data left, a ZLP should be sent if:
+ // - xferred_bytes is multiple of EP Packet size and not zero
+ tu_edpt_stream_write_zlp_if_needed(&p_cdc->stream.tx, xferred_bytes);
+ }
+ }
+ else if ( ep_addr == p_cdc->stream.rx.ep_addr )
+ {
+ tu_edpt_stream_read_xfer_complete(&p_cdc->stream.rx, xferred_bytes);
+
+ // invoke receive callback
+ if (tuh_cdc_rx_cb) tuh_cdc_rx_cb(idx);
+
+ // prepare for next transfer if needed
+ tu_edpt_stream_read_xfer(&p_cdc->stream.rx);
+ }else if ( ep_addr == p_cdc->ep_notif )
+ {
+ // TODO handle notification endpoint
+ }else
+ {
+ TU_ASSERT(false);
+ }
+
+ return true;
+}
+
+//--------------------------------------------------------------------+
+// Enumeration
+//--------------------------------------------------------------------+
+
+bool cdch_open(uint8_t rhport, uint8_t daddr, tusb_desc_interface_t const *itf_desc, uint16_t max_len)
{
(void) rhport;
- (void) max_len;
// Only support ACM subclass
// Protocol 0xFF can be RNDIS device for windows XP
@@ -170,17 +452,22 @@ bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it
CDC_COMM_SUBCLASS_ABSTRACT_CONTROL_MODEL == itf_desc->bInterfaceSubClass &&
0xFF != itf_desc->bInterfaceProtocol);
- cdch_data_t * p_cdc = get_itf(dev_addr);
+ uint8_t const * p_desc_end = ((uint8_t const*) itf_desc) + max_len;
- p_cdc->itf_num = itf_desc->bInterfaceNumber;
- p_cdc->itf_protocol = itf_desc->bInterfaceProtocol;
+ cdch_interface_t * p_cdc = find_new_itf();
+ TU_VERIFY(p_cdc);
- //------------- Communication Interface -------------//
- uint16_t drv_len = tu_desc_len(itf_desc);
+ p_cdc->daddr = daddr;
+ p_cdc->bInterfaceNumber = itf_desc->bInterfaceNumber;
+ p_cdc->bInterfaceSubClass = itf_desc->bInterfaceSubClass;
+ p_cdc->bInterfaceProtocol = itf_desc->bInterfaceProtocol;
+ p_cdc->line_state = 0;
+
+ //------------- Control Interface -------------//
uint8_t const * p_desc = tu_desc_next(itf_desc);
// Communication Functional Descriptors
- while( TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc) && drv_len <= max_len )
+ while( (p_desc < p_desc_end) && (TUSB_DESC_CS_INTERFACE == tu_desc_type(p_desc)) )
{
if ( CDC_FUNC_DESC_ABSTRACT_CONTROL_MANAGEMENT == cdc_functional_desc_typeof(p_desc) )
{
@@ -188,19 +475,18 @@ bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it
p_cdc->acm_capability = ((cdc_desc_func_acm_t const *) p_desc)->bmCapabilities;
}
- drv_len += tu_desc_len(p_desc);
p_desc = tu_desc_next(p_desc);
}
- if ( TUSB_DESC_ENDPOINT == tu_desc_type(p_desc) )
+ // Open notification endpoint of control interface if any
+ if (itf_desc->bNumEndpoints == 1)
{
- // notification endpoint
+ TU_ASSERT(TUSB_DESC_ENDPOINT == tu_desc_type(p_desc));
tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc;
- TU_ASSERT( tuh_edpt_open(dev_addr, desc_ep) );
+ TU_ASSERT( tuh_edpt_open(daddr, desc_ep) );
p_cdc->ep_notif = desc_ep->bEndpointAddress;
- drv_len += tu_desc_len(p_desc);
p_desc = tu_desc_next(p_desc);
}
@@ -209,52 +495,96 @@ bool cdch_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_interface_t const *it
(TUSB_CLASS_CDC_DATA == ((tusb_desc_interface_t const *) p_desc)->bInterfaceClass) )
{
// next to endpoint descriptor
- drv_len += tu_desc_len(p_desc);
p_desc = tu_desc_next(p_desc);
// data endpoints expected to be in pairs
for(uint32_t i=0; i<2; i++)
{
tusb_desc_endpoint_t const *desc_ep = (tusb_desc_endpoint_t const *) p_desc;
- TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType && TUSB_XFER_BULK == desc_ep->bmAttributes.xfer);
+ TU_ASSERT(TUSB_DESC_ENDPOINT == desc_ep->bDescriptorType &&
+ TUSB_XFER_BULK == desc_ep->bmAttributes.xfer);
- TU_ASSERT(tuh_edpt_open(dev_addr, desc_ep));
+ TU_ASSERT(tuh_edpt_open(daddr, desc_ep));
if ( tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN )
{
- p_cdc->ep_in = desc_ep->bEndpointAddress;
+ tu_edpt_stream_open(&p_cdc->stream.rx, daddr, desc_ep);
}else
{
- p_cdc->ep_out = desc_ep->bEndpointAddress;
+ tu_edpt_stream_open(&p_cdc->stream.tx, daddr, desc_ep);
}
- drv_len += tu_desc_len(p_desc);
- p_desc = tu_desc_next( p_desc );
+ p_desc = tu_desc_next(p_desc);
}
}
return true;
}
-bool cdch_set_config(uint8_t dev_addr, uint8_t itf_num)
+enum
{
- (void) dev_addr; (void) itf_num;
- return true;
+ CONFIG_SET_CONTROL_LINE_STATE,
+ CONFIG_SET_LINE_CODING,
+ CONFIG_COMPLETE
+};
+
+static void process_cdc_config(tuh_xfer_t* xfer)
+{
+ uintptr_t const state = xfer->user_data;
+ uint8_t const itf_num = (uint8_t) tu_le16toh(xfer->setup->wIndex);
+ uint8_t const idx = tuh_cdc_itf_get_index(xfer->daddr, itf_num);
+ TU_ASSERT(idx != TUSB_INDEX_INVALID, );
+
+ switch(state)
+ {
+ case CONFIG_SET_CONTROL_LINE_STATE:
+ #if CFG_TUH_CDC_LINE_CONTROL_ON_ENUM
+ TU_ASSERT( tuh_cdc_set_control_line_state(idx, CFG_TUH_CDC_LINE_CONTROL_ON_ENUM, process_cdc_config, CONFIG_SET_LINE_CODING), );
+ break;
+ #endif
+ TU_ATTR_FALLTHROUGH;
+
+ case CONFIG_SET_LINE_CODING:
+ #ifdef CFG_TUH_CDC_LINE_CODING_ON_ENUM
+ {
+ cdc_line_coding_t line_coding = CFG_TUH_CDC_LINE_CODING_ON_ENUM;
+ TU_ASSERT( tuh_cdc_set_line_coding(idx, &line_coding, process_cdc_config, CONFIG_COMPLETE), );
+ break;
+ }
+ #endif
+ TU_ATTR_FALLTHROUGH;
+
+ case CONFIG_COMPLETE:
+ if (tuh_cdc_mount_cb) tuh_cdc_mount_cb(idx);
+
+ // Prepare for incoming data
+ cdch_interface_t* p_cdc = get_itf(idx);
+ tu_edpt_stream_read_xfer(&p_cdc->stream.rx);
+
+ // notify usbh that driver enumeration is complete
+ // itf_num+1 to account for data interface as well
+ usbh_driver_set_config_complete(xfer->daddr, itf_num+1);
+ break;
+
+ default: break;
+ }
}
-bool cdch_xfer_cb(uint8_t dev_addr, uint8_t ep_addr, xfer_result_t event, uint32_t xferred_bytes)
+bool cdch_set_config(uint8_t daddr, uint8_t itf_num)
{
- (void) ep_addr;
- tuh_cdc_xfer_isr( dev_addr, event, 0, xferred_bytes );
+ // fake transfer to kick-off process
+ tusb_control_request_t request;
+ request.wIndex = tu_htole16((uint16_t) itf_num);
+
+ tuh_xfer_t xfer;
+ xfer.daddr = daddr;
+ xfer.result = XFER_RESULT_SUCCESS;
+ xfer.setup = &request;
+ xfer.user_data = CONFIG_SET_CONTROL_LINE_STATE;
+
+ process_cdc_config(&xfer);
+
return true;
}
-void cdch_close(uint8_t dev_addr)
-{
- TU_VERIFY(dev_addr <= CFG_TUH_DEVICE_MAX, );
-
- cdch_data_t * p_cdc = get_itf(dev_addr);
- tu_memclr(p_cdc, sizeof(cdch_data_t));
-}
-
#endif
diff --git a/src/class/cdc/cdc_host.h b/src/class/cdc/cdc_host.h
index 33dbd2efb..c759527e6 100644
--- a/src/class/cdc/cdc_host.h
+++ b/src/class/cdc/cdc_host.h
@@ -34,89 +34,156 @@
#endif
//--------------------------------------------------------------------+
-// CDC APPLICATION PUBLIC API
+// Class Driver Configuration
//--------------------------------------------------------------------+
-/** \ingroup ClassDriver_CDC Communication Device Class (CDC)
- * \addtogroup CDC_Serial Serial
- * @{
- * \defgroup CDC_Serial_Host Host
- * @{ */
-bool tuh_cdc_set_control_line_state(uint8_t dev_addr, bool dtr, bool rts, tuh_xfer_cb_t complete_cb);
+// Set Line Control state on enumeration/mounted: DTR ( bit 0), RTS (bit 1)
+#ifndef CFG_TUH_CDC_LINE_CONTROL_ON_ENUM
+#define CFG_TUH_CDC_LINE_CONTROL_ON_ENUM 0
+#endif
-static inline bool tuh_cdc_connect(uint8_t dev_addr, tuh_xfer_cb_t complete_cb)
+// Set Line Coding on enumeration/mounted, value for cdc_line_coding_t
+//#ifndef CFG_TUH_CDC_LINE_CODING_ON_ENUM
+//#define CFG_TUH_CDC_LINE_CODING_ON_ENUM { 115200, CDC_LINE_CONDING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }
+//#endif
+
+// RX FIFO size
+#ifndef CFG_TUH_CDC_RX_BUFSIZE
+#define CFG_TUH_CDC_RX_BUFSIZE USBH_EPSIZE_BULK_MAX
+#endif
+
+// RX Endpoint size
+#ifndef CFG_TUH_CDC_RX_EPSIZE
+#define CFG_TUH_CDC_RX_EPSIZE USBH_EPSIZE_BULK_MAX
+#endif
+
+// TX FIFO size
+#ifndef CFG_TUH_CDC_TX_BUFSIZE
+#define CFG_TUH_CDC_TX_BUFSIZE USBH_EPSIZE_BULK_MAX
+#endif
+
+// TX Endpoint size
+#ifndef CFG_TUH_CDC_TX_EPSIZE
+#define CFG_TUH_CDC_TX_EPSIZE USBH_EPSIZE_BULK_MAX
+#endif
+
+//--------------------------------------------------------------------+
+// Application API
+//--------------------------------------------------------------------+
+
+typedef struct
{
- return tuh_cdc_set_control_line_state(dev_addr, true, true, complete_cb);
+ uint8_t daddr;
+ uint8_t bInterfaceNumber;
+ uint8_t bInterfaceSubClass;
+ uint8_t bInterfaceProtocol;
+} tuh_cdc_itf_info_t;
+
+// Get Interface index from device address + interface number
+// return TUSB_INDEX_INVALID (0xFF) if not found
+uint8_t tuh_cdc_itf_get_index(uint8_t daddr, uint8_t itf_num);
+
+// Get Interface information
+// return true if index is correct and interface is currently mounted
+bool tuh_cdc_itf_get_info(uint8_t idx, tuh_cdc_itf_info_t* info);
+
+// Check if a interface is mounted
+bool tuh_cdc_mounted(uint8_t idx);
+
+// Get current DTR status
+bool tuh_cdc_get_dtr(uint8_t idx);
+
+// Get current RTS status
+bool tuh_cdc_get_rts(uint8_t idx);
+
+// Check if interface is connected (DTR active)
+TU_ATTR_ALWAYS_INLINE static inline bool tuh_cdc_connected(uint8_t idx)
+{
+ return tuh_cdc_get_dtr(idx);
}
-static inline bool tuh_cdc_disconnect(uint8_t dev_addr, tuh_xfer_cb_t complete_cb)
+// Get local (saved/cached) version of line coding.
+// This function should return correct values if tuh_cdc_set_line_coding() / tuh_cdc_get_line_coding()
+// are invoked previously or CFG_TUH_CDC_LINE_CODING_ON_ENUM is defined.
+// NOTE: This function does not make any USB transfer request to device.
+bool tuh_cdc_get_local_line_coding(uint8_t idx, cdc_line_coding_t* line_coding);
+
+//--------------------------------------------------------------------+
+// Write API
+//--------------------------------------------------------------------+
+
+// Get the number of bytes available for writing
+uint32_t tuh_cdc_write_available(uint8_t idx);
+
+// Write to cdc interface
+uint32_t tuh_cdc_write(uint8_t idx, void const* buffer, uint32_t bufsize);
+
+// Force sending data if possible, return number of forced bytes
+uint32_t tuh_cdc_write_flush(uint8_t idx);
+
+// Clear the transmit FIFO
+bool tuh_cdc_write_clear(uint8_t idx);
+
+//--------------------------------------------------------------------+
+// Read API
+//--------------------------------------------------------------------+
+
+// Get the number of bytes available for reading
+uint32_t tuh_cdc_read_available(uint8_t idx);
+
+// Read from cdc interface
+uint32_t tuh_cdc_read (uint8_t idx, void* buffer, uint32_t bufsize);
+
+// Get a byte from RX FIFO without removing it
+bool tuh_cdc_peek(uint8_t idx, uint8_t* ch);
+
+// Clear the received FIFO
+bool tuh_cdc_read_clear (uint8_t idx);
+
+//--------------------------------------------------------------------+
+// Control Endpoint (Request) API
+// Each Function will make a USB transfer request to/from device
+//--------------------------------------------------------------------+
+
+// Request to Set Control Line State: DTR (bit 0), RTS (bit 1)
+bool tuh_cdc_set_control_line_state(uint8_t idx, uint16_t line_state, tuh_xfer_cb_t complete_cb, uintptr_t user_data);
+
+// Request to Set Line Coding
+bool tuh_cdc_set_line_coding(uint8_t idx, cdc_line_coding_t const* line_coding, tuh_xfer_cb_t complete_cb, uintptr_t user_data);
+
+// Request to Get Line Coding
+// Should only use if tuh_cdc_set_line_coding() / tuh_cdc_get_line_coding() never got invoked and
+// CFG_TUH_CDC_LINE_CODING_ON_ENUM is not defined
+// bool tuh_cdc_get_line_coding(uint8_t idx, cdc_line_coding_t* coding);
+
+// Connect by set both DTR, RTS
+static inline bool tuh_cdc_connect(uint8_t idx, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
{
- return tuh_cdc_set_control_line_state(dev_addr, false, false, complete_cb);
+ return tuh_cdc_set_control_line_state(idx, CDC_CONTROL_LINE_STATE_DTR | CDC_CONTROL_LINE_STATE_RTS, complete_cb, user_data);
}
-/** \brief Check if device support CDC Serial interface or not
- * \param[in] dev_addr device address
- * \retval true if device supports
- * \retval false if device does not support or is not mounted
- */
-bool tuh_cdc_serial_is_mounted(uint8_t dev_addr);
-
-/** \brief Check if the interface is currently busy or not
- * \param[in] dev_addr device address
- * \param[in] pipeid value from \ref cdc_pipeid_t to indicate target pipe.
- * \retval true if the interface is busy, meaning the stack is still transferring/waiting data from/to device
- * \retval false if the interface is not busy, meaning the stack successfully transferred data from/to device
- * \note This function is used to check if previous transfer is complete (success or error), so that the next transfer
- * can be scheduled. User needs to make sure the corresponding interface is mounted
- * (by \ref tuh_cdc_serial_is_mounted) before calling this function.
- */
-bool tuh_cdc_is_busy(uint8_t dev_addr, cdc_pipeid_t pipeid);
-
-/** \brief Perform USB OUT transfer to device
- * \param[in] dev_addr device address
- * \param[in] p_data Buffer containing data. Must be accessible by USB controller (see \ref CFG_TUSB_MEM_SECTION)
- * \param[in] length Number of bytes to be transferred via USB bus
- * \retval TUSB_ERROR_NONE on success
- * \retval TUSB_ERROR_INTERFACE_IS_BUSY if the interface is already transferring data with device
- * \retval TUSB_ERROR_DEVICE_NOT_READY if device is not yet configured (by SET CONFIGURED request)
- * \retval TUSB_ERROR_INVALID_PARA if input parameters are not correct
- * \note This function is non-blocking and returns immediately. The result of USB transfer will be reported by the
- * interface's callback function. \a p_data must be declared with \ref CFG_TUSB_MEM_SECTION.
- */
-bool tuh_cdc_send(uint8_t dev_addr, void const * p_data, uint32_t length, bool is_notify);
-
-/** \brief Perform USB IN transfer to get data from device
- * \param[in] dev_addr device address
- * \param[in] p_buffer Buffer containing received data. Must be accessible by USB controller (see \ref CFG_TUSB_MEM_SECTION)
- * \param[in] length Number of bytes to be transferred via USB bus
- * \retval TUSB_ERROR_NONE on success
- * \retval TUSB_ERROR_INTERFACE_IS_BUSY if the interface is already transferring data with device
- * \retval TUSB_ERROR_DEVICE_NOT_READY if device is not yet configured (by SET CONFIGURED request)
- * \retval TUSB_ERROR_INVALID_PARA if input parameters are not correct
- * \note This function is non-blocking and returns immediately. The result of USB transfer will be reported by the
- * interface's callback function. \a p_data must be declared with \ref CFG_TUSB_MEM_SECTION.
- */
-bool tuh_cdc_receive(uint8_t dev_addr, void * p_buffer, uint32_t length, bool is_notify);
+// Disconnect by clear both DTR, RTS
+static inline bool tuh_cdc_disconnect(uint8_t idx, tuh_xfer_cb_t complete_cb, uintptr_t user_data)
+{
+ return tuh_cdc_set_control_line_state(idx, 0x00, complete_cb, user_data);
+}
//--------------------------------------------------------------------+
// CDC APPLICATION CALLBACKS
//--------------------------------------------------------------------+
-/** \brief Callback function that is invoked when an transferring event occurred
- * \param[in] dev_addr Address of device
- * \param[in] event an value from \ref xfer_result_t
- * \param[in] pipe_id value from \ref cdc_pipeid_t indicate the pipe
- * \param[in] xferred_bytes Number of bytes transferred via USB bus
- * \note event can be one of following
- * - XFER_RESULT_SUCCESS : previously scheduled transfer completes successfully.
- * - XFER_RESULT_FAILED : previously scheduled transfer encountered a transaction error.
- * - XFER_RESULT_STALLED : previously scheduled transfer is stalled by device.
- * \note
- */
-void tuh_cdc_xfer_isr(uint8_t dev_addr, xfer_result_t event, cdc_pipeid_t pipe_id, uint32_t xferred_bytes);
+// Invoked when a device with CDC interface is mounted
+// idx is index of cdc interface in the internal pool.
+TU_ATTR_WEAK extern void tuh_cdc_mount_cb(uint8_t idx);
-/// @} // group CDC_Serial_Host
-/// @}
+// Invoked when a device with CDC interface is unmounted
+TU_ATTR_WEAK extern void tuh_cdc_umount_cb(uint8_t idx);
+
+// Invoked when received new data
+TU_ATTR_WEAK extern void tuh_cdc_rx_cb(uint8_t idx);
+
+// Invoked when a TX is complete and therefore space becomes available in TX buffer
+TU_ATTR_WEAK extern void tuh_cdc_tx_complete_cb(uint8_t idx);
//--------------------------------------------------------------------+
// Internal Class Driver API
diff --git a/src/class/cdc/cdc_rndis_host.c b/src/class/cdc/cdc_rndis_host.c
index 8f28f51ac..44de85b45 100644
--- a/src/class/cdc/cdc_rndis_host.c
+++ b/src/class/cdc/cdc_rndis_host.c
@@ -117,7 +117,7 @@ void rndish_init(void)
//------------- Task creation -------------//
- //------------- semaphore creation for notificaiton pipe -------------//
+ //------------- semaphore creation for notification pipe -------------//
for(uint8_t i=0; iep_in, hid_itf->epin_buf, hid_itf->epin_size) )
{
- usbh_edpt_claim(dev_addr, hid_itf->ep_in);
+ usbh_edpt_release(dev_addr, hid_itf->ep_in);
return false;
}
diff --git a/src/class/hid/hid_host.h b/src/class/hid/hid_host.h
index fe09b03b2..ffc601d77 100644
--- a/src/class/hid/hid_host.h
+++ b/src/class/hid/hid_host.h
@@ -89,7 +89,7 @@ uint8_t tuh_hid_get_protocol(uint8_t dev_addr, uint8_t instance);
bool tuh_hid_set_protocol(uint8_t dev_addr, uint8_t instance, uint8_t protocol);
// Set Report using control endpoint
-// report_type is either Intput, Output or Feature, (value from hid_report_type_t)
+// report_type is either Input, Output or Feature, (value from hid_report_type_t)
bool tuh_hid_set_report(uint8_t dev_addr, uint8_t instance, uint8_t report_id, uint8_t report_type, void* report, uint16_t len);
//--------------------------------------------------------------------+
diff --git a/src/class/msc/msc_host.c b/src/class/msc/msc_host.c
index 9e88ebc30..6724e486c 100644
--- a/src/class/msc/msc_host.c
+++ b/src/class/msc/msc_host.c
@@ -33,6 +33,11 @@
#include "msc_host.h"
+// Debug level, TUSB_CFG_DEBUG must be at least this level for debug message
+#define MSCH_DEBUG 2
+
+#define TU_LOG_MSCH(...) TU_LOG(MSCH_DEBUG, __VA_ARGS__)
+
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF
//--------------------------------------------------------------------+
@@ -200,7 +205,7 @@ bool tuh_msc_test_unit_ready(uint8_t dev_addr, uint8_t lun, tuh_msc_complete_cb_
return tuh_msc_scsi_command(dev_addr, &cbw, NULL, complete_cb, arg);
}
-bool tuh_msc_request_sense(uint8_t dev_addr, uint8_t lun, void *resposne, tuh_msc_complete_cb_t complete_cb, uintptr_t arg)
+bool tuh_msc_request_sense(uint8_t dev_addr, uint8_t lun, void *response, tuh_msc_complete_cb_t complete_cb, uintptr_t arg)
{
msc_cbw_t cbw;
cbw_init(&cbw, lun);
@@ -217,7 +222,7 @@ bool tuh_msc_request_sense(uint8_t dev_addr, uint8_t lun, void *resposne, tuh_ms
memcpy(cbw.command, &cmd_request_sense, cbw.cmd_len);
- return tuh_msc_scsi_command(dev_addr, &cbw, resposne, complete_cb, arg);
+ return tuh_msc_scsi_command(dev_addr, &cbw, response, complete_cb, arg);
}
bool tuh_msc_read10(uint8_t dev_addr, uint8_t lun, void * buffer, uint32_t lba, uint16_t block_count, tuh_msc_complete_cb_t complete_cb, uintptr_t arg)
@@ -417,7 +422,7 @@ bool msch_set_config(uint8_t dev_addr, uint8_t itf_num)
p_msc->configured = true;
//------------- Get Max Lun -------------//
- TU_LOG2("MSC Get Max Lun\r\n");
+ TU_LOG_MSCH("MSC Get Max Lun\r\n");
tusb_control_request_t const request =
{
.bmRequestType_bit =
@@ -456,7 +461,7 @@ static void config_get_maxlun_complete (tuh_xfer_t* xfer)
p_msc->max_lun++; // MAX LUN is minus 1 by specs
// TODO multiple LUN support
- TU_LOG2("SCSI Test Unit Ready\r\n");
+ TU_LOG_MSCH("SCSI Test Unit Ready\r\n");
uint8_t const lun = 0;
tuh_msc_test_unit_ready(daddr, lun, config_test_unit_ready_complete, 0);
}
@@ -469,14 +474,14 @@ static bool config_test_unit_ready_complete(uint8_t dev_addr, tuh_msc_complete_d
if (csw->status == 0)
{
// Unit is ready, read its capacity
- TU_LOG2("SCSI Read Capacity\r\n");
+ TU_LOG_MSCH("SCSI Read Capacity\r\n");
tuh_msc_read_capacity(dev_addr, cbw->lun, (scsi_read_capacity10_resp_t*) ((void*) _msch_buffer), config_read_capacity_complete, 0);
}else
{
// Note: During enumeration, some device fails Test Unit Ready and require a few retries
// with Request Sense to start working !!
// TODO limit number of retries
- TU_LOG2("SCSI Request Sense\r\n");
+ TU_LOG_MSCH("SCSI Request Sense\r\n");
TU_ASSERT(tuh_msc_request_sense(dev_addr, cbw->lun, _msch_buffer, config_request_sense_complete, 0));
}
diff --git a/src/class/msc/msc_host.h b/src/class/msc/msc_host.h
index 416e4318b..5134b63c2 100644
--- a/src/class/msc/msc_host.h
+++ b/src/class/msc/msc_host.h
@@ -85,7 +85,7 @@ bool tuh_msc_test_unit_ready(uint8_t dev_addr, uint8_t lun, tuh_msc_complete_cb_
// Perform SCSI Request Sense 10 command
// Complete callback is invoked when SCSI op is complete.
-bool tuh_msc_request_sense(uint8_t dev_addr, uint8_t lun, void *resposne, tuh_msc_complete_cb_t complete_cb, uintptr_t arg);
+bool tuh_msc_request_sense(uint8_t dev_addr, uint8_t lun, void *response, tuh_msc_complete_cb_t complete_cb, uintptr_t arg);
// Perform SCSI Read 10 command. Read n blocks starting from LBA to buffer
// Complete callback is invoked when SCSI op is complete.
diff --git a/src/class/net/ecm_rndis_device.c b/src/class/net/ecm_rndis_device.c
index 5f316762f..c7428bcda 100644
--- a/src/class/net/ecm_rndis_device.c
+++ b/src/class/net/ecm_rndis_device.c
@@ -51,7 +51,7 @@ typedef struct
bool ecm_mode;
- // Endpoint descriptor use to open/close when receving SetInterface
+ // Endpoint descriptor use to open/close when receiving SetInterface
// TODO since configuration descriptor may not be long-lived memory, we should
// keep a copy of endpoint attribute instead
uint8_t const * ecm_desc_epdata;
diff --git a/src/class/net/ncm_device.c b/src/class/net/ncm_device.c
index 00892b49c..1cbc0ce01 100644
--- a/src/class/net/ncm_device.c
+++ b/src/class/net/ncm_device.c
@@ -392,7 +392,7 @@ bool netd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t
if (NCM_GET_NTB_PARAMETERS == request->bRequest)
{
- tud_control_xfer(rhport, request, (void*)&ntb_parameters, sizeof(ntb_parameters));
+ tud_control_xfer(rhport, request, (void*)(uintptr_t) &ntb_parameters, sizeof(ntb_parameters));
}
break;
diff --git a/src/class/usbtmc/usbtmc.h b/src/class/usbtmc/usbtmc.h
index e7016ae24..fd52d766e 100644
--- a/src/class/usbtmc/usbtmc.h
+++ b/src/class/usbtmc/usbtmc.h
@@ -158,7 +158,7 @@ enum {
USBTMC_BULK_IN_ERR_DATA_TOO_SHORT = 4u,
USBTMC_BULK_IN_ERR_DATA_TOO_LONG = 5u,
};
-// bult-in halt errors
+// built-in halt errors
enum {
USBTMC_BULK_IN_ERR = 1u, ///< receives a USBTMC command message that expects a response while a
/// Bulk-IN transfer is in progress
diff --git a/src/class/usbtmc/usbtmc_device.c b/src/class/usbtmc/usbtmc_device.c
index af4a92732..0cf0743a7 100644
--- a/src/class/usbtmc/usbtmc_device.c
+++ b/src/class/usbtmc/usbtmc_device.c
@@ -157,12 +157,14 @@ static bool handle_devMsgOut(uint8_t rhport, void *data, size_t len, size_t pack
static uint8_t termChar;
static uint8_t termCharRequested = false;
-osal_mutex_def_t usbtmcLockBuffer;
-static osal_mutex_t usbtmcLock;
+#if OSAL_MUTEX_REQUIRED
+static OSAL_MUTEX_DEF(usbtmcLockBuffer);
+#endif
+osal_mutex_t usbtmcLock;
// Our own private lock, mostly for the state variable.
-#define criticalEnter() do {osal_mutex_lock(usbtmcLock,OSAL_TIMEOUT_WAIT_FOREVER); } while (0)
-#define criticalLeave() do {osal_mutex_unlock(usbtmcLock); } while (0)
+#define criticalEnter() do { (void) osal_mutex_lock(usbtmcLock,OSAL_TIMEOUT_WAIT_FOREVER); } while (0)
+#define criticalLeave() do { (void) osal_mutex_unlock(usbtmcLock); } while (0)
bool atomicChangeState(usbtmcd_state_enum expectedState, usbtmcd_state_enum newState)
{
@@ -362,9 +364,9 @@ bool tud_usbtmc_start_bus_read()
case STATE_RCV:
break;
default:
- TU_VERIFY(false);
+ return false;
}
- TU_VERIFY(usbd_edpt_xfer(usbtmc_state.rhport, usbtmc_state.ep_bulk_out, usbtmc_state.ep_bulk_out_buf, 64));
+ TU_VERIFY(usbd_edpt_xfer(usbtmc_state.rhport, usbtmc_state.ep_bulk_out, usbtmc_state.ep_bulk_out_buf, (uint16_t)usbtmc_state.ep_bulk_out_wMaxPacketSize));
return true;
}
@@ -464,53 +466,52 @@ bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
switch(usbtmc_state.state)
{
case STATE_IDLE:
- TU_VERIFY(xferred_bytes >= sizeof(usbtmc_msg_generic_t));
- msg = (usbtmc_msg_generic_t*)(usbtmc_state.ep_bulk_out_buf);
- uint8_t invInvTag = (uint8_t)~(msg->header.bTagInverse);
- TU_VERIFY(msg->header.bTag == invInvTag);
- TU_VERIFY(msg->header.bTag != 0x00);
+ {
+ TU_VERIFY(xferred_bytes >= sizeof(usbtmc_msg_generic_t));
+ msg = (usbtmc_msg_generic_t*)(usbtmc_state.ep_bulk_out_buf);
+ uint8_t invInvTag = (uint8_t)~(msg->header.bTagInverse);
+ TU_VERIFY(msg->header.bTag == invInvTag);
+ TU_VERIFY(msg->header.bTag != 0x00);
- switch(msg->header.MsgID) {
- case USBTMC_MSGID_DEV_DEP_MSG_OUT:
- if(!handle_devMsgOutStart(rhport, msg, xferred_bytes))
- {
- usbd_edpt_stall(rhport, usbtmc_state.ep_bulk_out);
- TU_VERIFY(false);
- }
- break;
+ switch(msg->header.MsgID) {
+ case USBTMC_MSGID_DEV_DEP_MSG_OUT:
+ if(!handle_devMsgOutStart(rhport, msg, xferred_bytes))
+ {
+ usbd_edpt_stall(rhport, usbtmc_state.ep_bulk_out);
+ return false;
+ }
+ break;
- case USBTMC_MSGID_DEV_DEP_MSG_IN:
- TU_VERIFY(handle_devMsgIn(msg, xferred_bytes));
- break;
+ case USBTMC_MSGID_DEV_DEP_MSG_IN:
+ TU_VERIFY(handle_devMsgIn(msg, xferred_bytes));
+ break;
#if (CFG_TUD_USBTMC_ENABLE_488)
- case USBTMC_MSGID_USB488_TRIGGER:
- // Spec says we halt the EP if we didn't declare we support it.
- TU_VERIFY(usbtmc_state.capabilities->bmIntfcCapabilities488.supportsTrigger);
- TU_VERIFY(tud_usbtmc_msg_trigger_cb(msg));
+ case USBTMC_MSGID_USB488_TRIGGER:
+ // Spec says we halt the EP if we didn't declare we support it.
+ TU_VERIFY(usbtmc_state.capabilities->bmIntfcCapabilities488.supportsTrigger);
+ TU_VERIFY(tud_usbtmc_msg_trigger_cb(msg));
- break;
+ break;
#endif
- case USBTMC_MSGID_VENDOR_SPECIFIC_MSG_OUT:
- case USBTMC_MSGID_VENDOR_SPECIFIC_IN:
- default:
- usbd_edpt_stall(rhport, usbtmc_state.ep_bulk_out);
- TU_VERIFY(false);
- return false;
+ case USBTMC_MSGID_VENDOR_SPECIFIC_MSG_OUT:
+ case USBTMC_MSGID_VENDOR_SPECIFIC_IN:
+ default:
+ usbd_edpt_stall(rhport, usbtmc_state.ep_bulk_out);
+ return false;
+ }
+ return true;
}
- return true;
-
case STATE_RCV:
if(!handle_devMsgOut(rhport, usbtmc_state.ep_bulk_out_buf, xferred_bytes, xferred_bytes))
{
usbd_edpt_stall(rhport, usbtmc_state.ep_bulk_out);
- TU_VERIFY(false);
+ return false;
}
return true;
case STATE_ABORTING_BULK_OUT:
- TU_VERIFY(false);
- return false; // Should be stalled by now, shouldn't have received a packet.
+ return false;
case STATE_TX_REQUESTED:
case STATE_TX_INITIATED:
@@ -518,7 +519,7 @@ bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
case STATE_ABORTING_BULK_IN_SHORTED:
case STATE_ABORTING_BULK_IN_ABORTED:
default:
- TU_VERIFY(false);
+ return false;
}
}
else if(ep_addr == usbtmc_state.ep_bulk_in)
@@ -567,7 +568,6 @@ bool usbtmcd_xfer_cb(uint8_t rhport, uint8_t ep_addr, xfer_result_t result, uint
default:
TU_ASSERT(false);
- return false;
}
}
else if (ep_addr == usbtmc_state.ep_int_in) {
@@ -599,7 +599,7 @@ bool usbtmcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request
// At this point, a transfer MAY be in progress. Based on USB spec, when clearing bulk EP HALT,
// the EP transfer buffer needs to be cleared and DTOG needs to be reset, even if
- // the EP is not halted. The only USBD API interface to do this is to stall and then unstall the EP.
+ // the EP is not halted. The only USBD API interface to do this is to stall and then un-stall the EP.
if(ep_addr == usbtmc_state.ep_bulk_out)
{
criticalEnter();
@@ -871,16 +871,13 @@ bool usbtmcd_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request
case USB488_bREQUEST_LOCAL_LOCKOUT:
{
TU_VERIFY(request->bmRequestType == 0xA1); // in,class,interface
- TU_VERIFY(false);
return false;
}
#endif
default:
- TU_VERIFY(false);
return false;
}
- TU_VERIFY(false);
}
#endif /* CFG_TUD_TSMC */
diff --git a/src/class/usbtmc/usbtmc_device.h b/src/class/usbtmc/usbtmc_device.h
index 144b3315d..c1298ddb8 100644
--- a/src/class/usbtmc/usbtmc_device.h
+++ b/src/class/usbtmc/usbtmc_device.h
@@ -36,7 +36,7 @@
#endif
/***********************************************
- * Functions to be implemeted by the class implementation
+ * Functions to be implemented by the class implementation
*/
// In order to proceed, app must call call tud_usbtmc_start_bus_read(rhport) during or soon after:
diff --git a/src/class/video/video_device.c b/src/class/video/video_device.c
index a6d2724c1..e17d0a90f 100644
--- a/src/class/video/video_device.c
+++ b/src/class/video/video_device.c
@@ -917,7 +917,7 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
switch (request->bRequest) {
case VIDEO_REQUEST_SET_CUR:
if (stage == CONTROL_STAGE_SETUP) {
- TU_VERIFY(sizeof(video_probe_and_commit_control_t) == request->wLength, VIDEO_ERROR_UNKNOWN);
+ TU_VERIFY(sizeof(video_probe_and_commit_control_t) >= request->wLength, VIDEO_ERROR_UNKNOWN);
TU_VERIFY(tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)),
VIDEO_ERROR_UNKNOWN);
} else if (stage == CONTROL_STAGE_DATA) {
@@ -973,7 +973,7 @@ static int handle_video_stm_cs_req(uint8_t rhport, uint8_t stage,
switch (request->bRequest) {
case VIDEO_REQUEST_SET_CUR:
if (stage == CONTROL_STAGE_SETUP) {
- TU_VERIFY(sizeof(video_probe_and_commit_control_t) == request->wLength, VIDEO_ERROR_UNKNOWN);
+ TU_VERIFY(sizeof(video_probe_and_commit_control_t) >= request->wLength, VIDEO_ERROR_UNKNOWN);
TU_VERIFY(tud_control_xfer(rhport, request, self->ep_buf, sizeof(video_probe_and_commit_control_t)), VIDEO_ERROR_UNKNOWN);
} else if (stage == CONTROL_STAGE_DATA) {
TU_VERIFY(_update_streaming_parameters(self, (video_probe_and_commit_control_t*)self->ep_buf), VIDEO_ERROR_INVALID_VALUE_WITHIN_RANGE);
diff --git a/src/common/tusb_debug.h b/src/common/tusb_debug.h
index ac5bee6ec..65fd1920d 100644
--- a/src/common/tusb_debug.h
+++ b/src/common/tusb_debug.h
@@ -66,7 +66,7 @@ static inline void tu_print_arr(uint8_t const* buf, uint32_t bufsize)
#define TU_LOG(n, ...) TU_XSTRCAT(TU_LOG, n)(__VA_ARGS__)
#define TU_LOG_MEM(n, ...) TU_XSTRCAT3(TU_LOG, n, _MEM)(__VA_ARGS__)
#define TU_LOG_ARR(n, ...) TU_XSTRCAT3(TU_LOG, n, _ARR)(__VA_ARGS__)
-#define TU_LOG_VAR(n, ...) TU_XSTRCAT3(TU_LOG, n, _VAR)(__VA_ARGS__)
+#define TU_LOG_PTR(n, ...) TU_XSTRCAT3(TU_LOG, n, _PTR)(__VA_ARGS__)
#define TU_LOG_INT(n, ...) TU_XSTRCAT3(TU_LOG, n, _INT)(__VA_ARGS__)
#define TU_LOG_HEX(n, ...) TU_XSTRCAT3(TU_LOG, n, _HEX)(__VA_ARGS__)
#define TU_LOG_LOCATION() tu_printf("%s: %d:\r\n", __PRETTY_FUNCTION__, __LINE__)
@@ -76,7 +76,7 @@ static inline void tu_print_arr(uint8_t const* buf, uint32_t bufsize)
#define TU_LOG1 tu_printf
#define TU_LOG1_MEM tu_print_mem
#define TU_LOG1_ARR(_x, _n) tu_print_arr((uint8_t const*)(_x), _n)
-#define TU_LOG1_VAR(_x) tu_print_arr((uint8_t const*)(_x), sizeof(*(_x)))
+#define TU_LOG1_PTR(_x) tu_print_arr((uint8_t const*)(_x), sizeof(*(_x)))
#define TU_LOG1_INT(_x) tu_printf(#_x " = %ld\r\n", (unsigned long) (_x) )
#define TU_LOG1_HEX(_x) tu_printf(#_x " = %lX\r\n", (unsigned long) (_x) )
@@ -85,7 +85,7 @@ static inline void tu_print_arr(uint8_t const* buf, uint32_t bufsize)
#define TU_LOG2 TU_LOG1
#define TU_LOG2_MEM TU_LOG1_MEM
#define TU_LOG2_ARR TU_LOG1_ARR
- #define TU_LOG2_VAR TU_LOG1_VAR
+ #define TU_LOG2_PTR TU_LOG1_PTR
#define TU_LOG2_INT TU_LOG1_INT
#define TU_LOG2_HEX TU_LOG1_HEX
#endif
@@ -95,7 +95,7 @@ static inline void tu_print_arr(uint8_t const* buf, uint32_t bufsize)
#define TU_LOG3 TU_LOG1
#define TU_LOG3_MEM TU_LOG1_MEM
#define TU_LOG3_ARR TU_LOG1_ARR
- #define TU_LOG3_VAR TU_LOG1_VAR
+ #define TU_LOG3_PTR TU_LOG1_PTR
#define TU_LOG3_INT TU_LOG1_INT
#define TU_LOG3_HEX TU_LOG1_HEX
#endif
@@ -132,7 +132,7 @@ static inline const char* tu_lookup_find(tu_lookup_table_t const* p_table, uint3
#ifndef TU_LOG
#define TU_LOG(n, ...)
#define TU_LOG_MEM(n, ...)
- #define TU_LOG_VAR(n, ...)
+ #define TU_LOG_PTR(n, ...)
#define TU_LOG_INT(n, ...)
#define TU_LOG_HEX(n, ...)
#define TU_LOG_LOCATION()
@@ -143,14 +143,14 @@ static inline const char* tu_lookup_find(tu_lookup_table_t const* p_table, uint3
#define TU_LOG0(...)
#define TU_LOG0_MEM(...)
-#define TU_LOG0_VAR(...)
+#define TU_LOG0_PTR(...)
#define TU_LOG0_INT(...)
#define TU_LOG0_HEX(...)
#ifndef TU_LOG1
#define TU_LOG1(...)
#define TU_LOG1_MEM(...)
- #define TU_LOG1_VAR(...)
+ #define TU_LOG1_PTR(...)
#define TU_LOG1_INT(...)
#define TU_LOG1_HEX(...)
#endif
@@ -158,7 +158,7 @@ static inline const char* tu_lookup_find(tu_lookup_table_t const* p_table, uint3
#ifndef TU_LOG2
#define TU_LOG2(...)
#define TU_LOG2_MEM(...)
- #define TU_LOG2_VAR(...)
+ #define TU_LOG2_PTR(...)
#define TU_LOG2_INT(...)
#define TU_LOG2_HEX(...)
#endif
@@ -166,7 +166,7 @@ static inline const char* tu_lookup_find(tu_lookup_table_t const* p_table, uint3
#ifndef TU_LOG3
#define TU_LOG3(...)
#define TU_LOG3_MEM(...)
- #define TU_LOG3_VAR(...)
+ #define TU_LOG3_PTR(...)
#define TU_LOG3_INT(...)
#define TU_LOG3_HEX(...)
#endif
diff --git a/src/common/tusb_fifo.c b/src/common/tusb_fifo.c
index 895b9208b..a52c92267 100644
--- a/src/common/tusb_fifo.c
+++ b/src/common/tusb_fifo.c
@@ -28,21 +28,22 @@
#include "osal/osal.h"
#include "tusb_fifo.h"
-// Supress IAR warning
+#define TU_FIFO_DBG 0
+
+// Suppress IAR warning
// Warning[Pa082]: undefined behavior: the order of volatile accesses is undefined in this statement
#if defined(__ICCARM__)
#pragma diag_suppress = Pa082
#endif
-// implement mutex lock and unlock
-#if CFG_FIFO_MUTEX
+#if OSAL_MUTEX_REQUIRED
-static inline void _ff_lock(tu_fifo_mutex_t mutex)
+TU_ATTR_ALWAYS_INLINE static inline void _ff_lock(osal_mutex_t mutex)
{
if (mutex) osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER);
}
-static inline void _ff_unlock(tu_fifo_mutex_t mutex)
+TU_ATTR_ALWAYS_INLINE static inline void _ff_unlock(osal_mutex_t mutex)
{
if (mutex) osal_mutex_unlock(mutex);
}
@@ -66,23 +67,20 @@ typedef enum
bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable)
{
- if (depth > 0x8000) return false; // Maximum depth is 2^15 items
+ // Limit index space to 2*depth - this allows for a fast "modulo" calculation
+ // but limits the maximum depth to 2^16/2 = 2^15 and buffer overflows are detectable
+ // only if overflow happens once (important for unsupervised DMA applications)
+ if (depth > 0x8000) return false;
_ff_lock(f->mutex_wr);
_ff_lock(f->mutex_rd);
- f->buffer = (uint8_t*) buffer;
- f->depth = depth;
- f->item_size = item_size;
+ f->buffer = (uint8_t*) buffer;
+ f->depth = depth;
+ f->item_size = (uint16_t) (item_size & 0x7FFF);
f->overwritable = overwritable;
-
- // Limit index space to 2*depth - this allows for a fast "modulo" calculation
- // but limits the maximum depth to 2^16/2 = 2^15 and buffer overflows are detectable
- // only if overflow happens once (important for unsupervised DMA applications)
- f->max_pointer_idx = (uint16_t) (2*depth - 1);
- f->non_used_index_space = UINT16_MAX - f->max_pointer_idx;
-
- f->rd_idx = f->wr_idx = 0;
+ f->rd_idx = 0;
+ f->wr_idx = 0;
_ff_unlock(f->mutex_wr);
_ff_unlock(f->mutex_rd);
@@ -90,25 +88,22 @@ bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_si
return true;
}
-// Static functions are intended to work on local variables
-static inline uint16_t _ff_mod(uint16_t idx, uint16_t depth)
-{
- while ( idx >= depth) idx -= depth;
- return idx;
-}
+//--------------------------------------------------------------------+
+// Pull & Push
+//--------------------------------------------------------------------+
// Intended to be used to read from hardware USB FIFO in e.g. STM32 where all data is read from a constant address
-// Code adapted from dcd_synopsis.c
+// Code adapted from dcd_synopsys.c
// TODO generalize with configurable 1 byte or 4 byte each read
static void _ff_push_const_addr(uint8_t * ff_buf, const void * app_buf, uint16_t len)
{
- volatile const uint32_t * rx_fifo = (volatile const uint32_t *) app_buf;
+ volatile const uint32_t * reg_rx = (volatile const uint32_t *) app_buf;
// Reading full available 32 bit words from const app address
uint16_t full_words = len >> 2;
while(full_words--)
{
- tu_unaligned_write32(ff_buf, *rx_fifo);
+ tu_unaligned_write32(ff_buf, *reg_rx);
ff_buf += 4;
}
@@ -116,7 +111,7 @@ static void _ff_push_const_addr(uint8_t * ff_buf, const void * app_buf, uint16_t
uint8_t const bytes_rem = len & 0x03;
if ( bytes_rem )
{
- uint32_t tmp32 = *rx_fifo;
+ uint32_t tmp32 = *reg_rx;
memcpy(ff_buf, &tmp32, bytes_rem);
}
}
@@ -125,49 +120,49 @@ static void _ff_push_const_addr(uint8_t * ff_buf, const void * app_buf, uint16_t
// where all data is written to a constant address in full word copies
static void _ff_pull_const_addr(void * app_buf, const uint8_t * ff_buf, uint16_t len)
{
- volatile uint32_t * tx_fifo = (volatile uint32_t *) app_buf;
+ volatile uint32_t * reg_tx = (volatile uint32_t *) app_buf;
- // Pushing full available 32 bit words to const app address
+ // Write full available 32 bit words to const address
uint16_t full_words = len >> 2;
while(full_words--)
{
- *tx_fifo = tu_unaligned_read32(ff_buf);
+ *reg_tx = tu_unaligned_read32(ff_buf);
ff_buf += 4;
}
- // Write the remaining 1-3 bytes into const app address
+ // Write the remaining 1-3 bytes into const address
uint8_t const bytes_rem = len & 0x03;
if ( bytes_rem )
{
uint32_t tmp32 = 0;
memcpy(&tmp32, ff_buf, bytes_rem);
- *tx_fifo = tmp32;
+ *reg_tx = tmp32;
}
}
-// send one item to FIFO WITHOUT updating write pointer
+// send one item to fifo WITHOUT updating write pointer
static inline void _ff_push(tu_fifo_t* f, void const * app_buf, uint16_t rel)
{
memcpy(f->buffer + (rel * f->item_size), app_buf, f->item_size);
}
-// send n items to FIFO WITHOUT updating write pointer
-static void _ff_push_n(tu_fifo_t* f, void const * app_buf, uint16_t n, uint16_t rel, tu_fifo_copy_mode_t copy_mode)
+// send n items to fifo WITHOUT updating write pointer
+static void _ff_push_n(tu_fifo_t* f, void const * app_buf, uint16_t n, uint16_t wr_ptr, tu_fifo_copy_mode_t copy_mode)
{
- uint16_t const nLin = f->depth - rel;
- uint16_t const nWrap = n - nLin;
+ uint16_t const lin_count = f->depth - wr_ptr;
+ uint16_t const wrap_count = n - lin_count;
- uint16_t nLin_bytes = nLin * f->item_size;
- uint16_t nWrap_bytes = nWrap * f->item_size;
+ uint16_t lin_bytes = lin_count * f->item_size;
+ uint16_t wrap_bytes = wrap_count * f->item_size;
// current buffer of fifo
- uint8_t* ff_buf = f->buffer + (rel * f->item_size);
+ uint8_t* ff_buf = f->buffer + (wr_ptr * f->item_size);
switch (copy_mode)
{
case TU_FIFO_COPY_INC:
- if(n <= nLin)
+ if(n <= lin_count)
{
// Linear only
memcpy(ff_buf, app_buf, n*f->item_size);
@@ -177,16 +172,17 @@ static void _ff_push_n(tu_fifo_t* f, void const * app_buf, uint16_t n, uint16_t
// Wrap around
// Write data to linear part of buffer
- memcpy(ff_buf, app_buf, nLin_bytes);
+ memcpy(ff_buf, app_buf, lin_bytes);
// Write data wrapped around
- memcpy(f->buffer, ((uint8_t const*) app_buf) + nLin_bytes, nWrap_bytes);
+ // TU_ASSERT(nWrap_bytes <= f->depth, );
+ memcpy(f->buffer, ((uint8_t const*) app_buf) + lin_bytes, wrap_bytes);
}
break;
case TU_FIFO_COPY_CST_FULL_WORDS:
// Intended for hardware buffers from which it can be read word by word only
- if(n <= nLin)
+ if(n <= lin_count)
{
// Linear only
_ff_push_const_addr(ff_buf, app_buf, n*f->item_size);
@@ -196,17 +192,18 @@ static void _ff_push_n(tu_fifo_t* f, void const * app_buf, uint16_t n, uint16_t
// Wrap around case
// Write full words to linear part of buffer
- uint16_t nLin_4n_bytes = nLin_bytes & 0xFFFC;
+ uint16_t nLin_4n_bytes = lin_bytes & 0xFFFC;
_ff_push_const_addr(ff_buf, app_buf, nLin_4n_bytes);
ff_buf += nLin_4n_bytes;
// There could be odd 1-3 bytes before the wrap-around boundary
- volatile const uint32_t * rx_fifo = (volatile const uint32_t *) app_buf;
- uint8_t rem = nLin_bytes & 0x03;
+ uint8_t rem = lin_bytes & 0x03;
if (rem > 0)
{
- uint8_t remrem = (uint8_t) tu_min16(nWrap_bytes, 4-rem);
- nWrap_bytes -= remrem;
+ volatile const uint32_t * rx_fifo = (volatile const uint32_t *) app_buf;
+
+ uint8_t remrem = (uint8_t) tu_min16(wrap_bytes, 4-rem);
+ wrap_bytes -= remrem;
uint32_t tmp32 = *rx_fifo;
uint8_t * src_u8 = ((uint8_t *) &tmp32);
@@ -224,34 +221,34 @@ static void _ff_push_n(tu_fifo_t* f, void const * app_buf, uint16_t n, uint16_t
}
// Write data wrapped part
- if (nWrap_bytes > 0) _ff_push_const_addr(ff_buf, app_buf, nWrap_bytes);
+ if (wrap_bytes > 0) _ff_push_const_addr(ff_buf, app_buf, wrap_bytes);
}
break;
}
}
-// get one item from FIFO WITHOUT updating read pointer
+// get one item from fifo WITHOUT updating read pointer
static inline void _ff_pull(tu_fifo_t* f, void * app_buf, uint16_t rel)
{
memcpy(app_buf, f->buffer + (rel * f->item_size), f->item_size);
}
-// get n items from FIFO WITHOUT updating read pointer
-static void _ff_pull_n(tu_fifo_t* f, void* app_buf, uint16_t n, uint16_t rel, tu_fifo_copy_mode_t copy_mode)
+// get n items from fifo WITHOUT updating read pointer
+static void _ff_pull_n(tu_fifo_t* f, void* app_buf, uint16_t n, uint16_t rd_ptr, tu_fifo_copy_mode_t copy_mode)
{
- uint16_t const nLin = f->depth - rel;
- uint16_t const nWrap = n - nLin; // only used if wrapped
+ uint16_t const lin_count = f->depth - rd_ptr;
+ uint16_t const wrap_count = n - lin_count; // only used if wrapped
- uint16_t nLin_bytes = nLin * f->item_size;
- uint16_t nWrap_bytes = nWrap * f->item_size;
+ uint16_t lin_bytes = lin_count * f->item_size;
+ uint16_t wrap_bytes = wrap_count * f->item_size;
// current buffer of fifo
- uint8_t* ff_buf = f->buffer + (rel * f->item_size);
+ uint8_t* ff_buf = f->buffer + (rd_ptr * f->item_size);
switch (copy_mode)
{
case TU_FIFO_COPY_INC:
- if ( n <= nLin )
+ if ( n <= lin_count )
{
// Linear only
memcpy(app_buf, ff_buf, n*f->item_size);
@@ -261,15 +258,15 @@ static void _ff_pull_n(tu_fifo_t* f, void* app_buf, uint16_t n, uint16_t rel, tu
// Wrap around
// Read data from linear part of buffer
- memcpy(app_buf, ff_buf, nLin_bytes);
+ memcpy(app_buf, ff_buf, lin_bytes);
// Read data wrapped part
- memcpy((uint8_t*) app_buf + nLin_bytes, f->buffer, nWrap_bytes);
+ memcpy((uint8_t*) app_buf + lin_bytes, f->buffer, wrap_bytes);
}
break;
case TU_FIFO_COPY_CST_FULL_WORDS:
- if ( n <= nLin )
+ if ( n <= lin_count )
{
// Linear only
_ff_pull_const_addr(app_buf, ff_buf, n*f->item_size);
@@ -279,17 +276,18 @@ static void _ff_pull_n(tu_fifo_t* f, void* app_buf, uint16_t n, uint16_t rel, tu
// Wrap around case
// Read full words from linear part of buffer
- uint16_t nLin_4n_bytes = nLin_bytes & 0xFFFC;
- _ff_pull_const_addr(app_buf, ff_buf, nLin_4n_bytes);
- ff_buf += nLin_4n_bytes;
+ uint16_t lin_4n_bytes = lin_bytes & 0xFFFC;
+ _ff_pull_const_addr(app_buf, ff_buf, lin_4n_bytes);
+ ff_buf += lin_4n_bytes;
// There could be odd 1-3 bytes before the wrap-around boundary
- volatile uint32_t * tx_fifo = (volatile uint32_t *) app_buf;
- uint8_t rem = nLin_bytes & 0x03;
+ uint8_t rem = lin_bytes & 0x03;
if (rem > 0)
{
- uint8_t remrem = (uint8_t) tu_min16(nWrap_bytes, 4-rem);
- nWrap_bytes -= remrem;
+ volatile uint32_t * reg_tx = (volatile uint32_t *) app_buf;
+
+ uint8_t remrem = (uint8_t) tu_min16(wrap_bytes, 4-rem);
+ wrap_bytes -= remrem;
uint32_t tmp32=0;
uint8_t * dst_u8 = (uint8_t *)&tmp32;
@@ -301,7 +299,7 @@ static void _ff_pull_n(tu_fifo_t* f, void* app_buf, uint16_t n, uint16_t rel, tu
ff_buf = f->buffer;
while(remrem--) *dst_u8++ = *ff_buf++;
- *tx_fifo = tmp32;
+ *reg_tx = tmp32;
}
else
{
@@ -309,7 +307,7 @@ static void _ff_pull_n(tu_fifo_t* f, void* app_buf, uint16_t n, uint16_t rel, tu
}
// Read data wrapped part
- if (nWrap_bytes > 0) _ff_pull_const_addr(app_buf, ff_buf, nWrap_bytes);
+ if (wrap_bytes > 0) _ff_pull_const_addr(app_buf, ff_buf, wrap_bytes);
}
break;
@@ -317,178 +315,232 @@ static void _ff_pull_n(tu_fifo_t* f, void* app_buf, uint16_t n, uint16_t rel, tu
}
}
-// Advance an absolute pointer
-static uint16_t advance_pointer(tu_fifo_t* f, uint16_t p, uint16_t offset)
-{
- // We limit the index space of p such that a correct wrap around happens
- // Check for a wrap around or if we are in unused index space - This has to be checked first!!
- // We are exploiting the wrap around to the correct index
- if ((p > (uint16_t)(p + offset)) || ((uint16_t)(p + offset) > f->max_pointer_idx))
- {
- p = (uint16_t) ((p + offset) + f->non_used_index_space);
- }
- else
- {
- p += offset;
- }
- return p;
-}
+//--------------------------------------------------------------------+
+// Helper
+//--------------------------------------------------------------------+
-// Backward an absolute pointer
-static uint16_t backward_pointer(tu_fifo_t* f, uint16_t p, uint16_t offset)
+// return only the index difference and as such can be used to determine an overflow i.e overflowable count
+TU_ATTR_ALWAYS_INLINE static inline
+uint16_t _ff_count(uint16_t depth, uint16_t wr_idx, uint16_t rd_idx)
{
- // We limit the index space of p such that a correct wrap around happens
- // Check for a wrap around or if we are in unused index space - This has to be checked first!!
- // We are exploiting the wrap around to the correct index
- if ((p < (uint16_t)(p - offset)) || ((uint16_t)(p - offset) > f->max_pointer_idx))
- {
- p = (uint16_t) ((p - offset) - f->non_used_index_space);
- }
- else
- {
- p -= offset;
- }
- return p;
-}
-
-// get relative from absolute pointer
-static uint16_t get_relative_pointer(tu_fifo_t* f, uint16_t p)
-{
- return _ff_mod(p, f->depth);
-}
-
-// Works on local copies of w and r - return only the difference and as such can be used to determine an overflow
-static inline uint16_t _tu_fifo_count(tu_fifo_t* f, uint16_t wAbs, uint16_t rAbs)
-{
- uint16_t cnt = wAbs-rAbs;
-
// In case we have non-power of two depth we need a further modification
- if (rAbs > wAbs) cnt -= f->non_used_index_space;
-
- return cnt;
+ if (wr_idx >= rd_idx)
+ {
+ return (uint16_t) (wr_idx - rd_idx);
+ } else
+ {
+ return (uint16_t) (2*depth - (rd_idx - wr_idx));
+ }
}
-// Works on local copies of w and r
-static inline bool _tu_fifo_empty(uint16_t wAbs, uint16_t rAbs)
+// return remaining slot in fifo
+TU_ATTR_ALWAYS_INLINE static inline
+uint16_t _ff_remaining(uint16_t depth, uint16_t wr_idx, uint16_t rd_idx)
{
- return wAbs == rAbs;
+ uint16_t const count = _ff_count(depth, wr_idx, rd_idx);
+ return (depth > count) ? (depth - count) : 0;
}
-// Works on local copies of w and r
-static inline bool _tu_fifo_full(tu_fifo_t* f, uint16_t wAbs, uint16_t rAbs)
+//--------------------------------------------------------------------+
+// Index Helper
+//--------------------------------------------------------------------+
+
+// Advance an absolute index
+// "absolute" index is only in the range of [0..2*depth)
+static uint16_t advance_index(uint16_t depth, uint16_t idx, uint16_t offset)
{
- return (_tu_fifo_count(f, wAbs, rAbs) == f->depth);
+ // We limit the index space of p such that a correct wrap around happens
+ // Check for a wrap around or if we are in unused index space - This has to be checked first!!
+ // We are exploiting the wrap around to the correct index
+ uint16_t new_idx = (uint16_t) (idx + offset);
+ if ( (idx > new_idx) || (new_idx >= 2*depth) )
+ {
+ uint16_t const non_used_index_space = (uint16_t) (UINT16_MAX - (2*depth-1));
+ new_idx = (uint16_t) (new_idx + non_used_index_space);
+ }
+
+ return new_idx;
}
-// Works on local copies of w and r
-// BE AWARE - THIS FUNCTION MIGHT NOT GIVE A CORRECT ANSWERE IN CASE WRITE POINTER "OVERFLOWS"
-// Only one overflow is allowed for this function to work e.g. if depth = 100, you must not
-// write more than 2*depth-1 items in one rush without updating write pointer. Otherwise
-// write pointer wraps and you pointer states are messed up. This can only happen if you
-// use DMAs, write functions do not allow such an error.
-static inline bool _tu_fifo_overflowed(tu_fifo_t* f, uint16_t wAbs, uint16_t rAbs)
+#if 0 // not used but
+// Backward an absolute index
+static uint16_t backward_index(uint16_t depth, uint16_t idx, uint16_t offset)
{
- return (_tu_fifo_count(f, wAbs, rAbs) > f->depth);
+ // We limit the index space of p such that a correct wrap around happens
+ // Check for a wrap around or if we are in unused index space - This has to be checked first!!
+ // We are exploiting the wrap around to the correct index
+ uint16_t new_idx = (uint16_t) (idx - offset);
+ if ( (idx < new_idx) || (new_idx >= 2*depth) )
+ {
+ uint16_t const non_used_index_space = (uint16_t) (UINT16_MAX - (2*depth-1));
+ new_idx = (uint16_t) (new_idx - non_used_index_space);
+ }
+
+ return new_idx;
+}
+#endif
+
+// index to pointer, simply an modulo with minus.
+TU_ATTR_ALWAYS_INLINE static inline
+uint16_t idx2ptr(uint16_t depth, uint16_t idx)
+{
+ // Only run at most 3 times since index is limit in the range of [0..2*depth)
+ while ( idx >= depth ) idx -= depth;
+ return idx;
}
// Works on local copies of w
-// For more details see _tu_fifo_overflow()!
-static inline void _tu_fifo_correct_read_pointer(tu_fifo_t* f, uint16_t wAbs)
+// When an overwritable fifo is overflowed, rd_idx will be re-index so that it forms
+// an full fifo i.e _ff_count() = depth
+TU_ATTR_ALWAYS_INLINE static inline
+uint16_t _ff_correct_read_index(tu_fifo_t* f, uint16_t wr_idx)
{
- f->rd_idx = backward_pointer(f, wAbs, f->depth);
+ uint16_t rd_idx;
+ if ( wr_idx >= f->depth )
+ {
+ rd_idx = wr_idx - f->depth;
+ }else
+ {
+ rd_idx = wr_idx + f->depth;
+ }
+
+ f->rd_idx = rd_idx;
+
+ return rd_idx;
}
// Works on local copies of w and r
// Must be protected by mutexes since in case of an overflow read pointer gets modified
-static bool _tu_fifo_peek(tu_fifo_t* f, void * p_buffer, uint16_t wAbs, uint16_t rAbs)
+static bool _tu_fifo_peek(tu_fifo_t* f, void * p_buffer, uint16_t wr_idx, uint16_t rd_idx)
{
- uint16_t cnt = _tu_fifo_count(f, wAbs, rAbs);
+ uint16_t cnt = _ff_count(f->depth, wr_idx, rd_idx);
+
+ // nothing to peek
+ if ( cnt == 0 ) return false;
// Check overflow and correct if required
- if (cnt > f->depth)
+ if ( cnt > f->depth )
{
- _tu_fifo_correct_read_pointer(f, wAbs);
+ rd_idx = _ff_correct_read_index(f, wr_idx);
cnt = f->depth;
}
- // Skip beginning of buffer
- if (cnt == 0) return false;
-
- uint16_t rRel = get_relative_pointer(f, rAbs);
+ uint16_t rd_ptr = idx2ptr(f->depth, rd_idx);
// Peek data
- _ff_pull(f, p_buffer, rRel);
+ _ff_pull(f, p_buffer, rd_ptr);
return true;
}
// Works on local copies of w and r
// Must be protected by mutexes since in case of an overflow read pointer gets modified
-static uint16_t _tu_fifo_peek_n(tu_fifo_t* f, void * p_buffer, uint16_t n, uint16_t wAbs, uint16_t rAbs, tu_fifo_copy_mode_t copy_mode)
+static uint16_t _tu_fifo_peek_n(tu_fifo_t* f, void * p_buffer, uint16_t n, uint16_t wr_idx, uint16_t rd_idx, tu_fifo_copy_mode_t copy_mode)
{
- uint16_t cnt = _tu_fifo_count(f, wAbs, rAbs);
+ uint16_t cnt = _ff_count(f->depth, wr_idx, rd_idx);
+
+ // nothing to peek
+ if ( cnt == 0 ) return 0;
// Check overflow and correct if required
- if (cnt > f->depth)
+ if ( cnt > f->depth )
{
- _tu_fifo_correct_read_pointer(f, wAbs);
- rAbs = f->rd_idx;
+ rd_idx = _ff_correct_read_index(f, wr_idx);
cnt = f->depth;
}
- // Skip beginning of buffer
- if (cnt == 0) return 0;
-
// Check if we can read something at and after offset - if too less is available we read what remains
- if (cnt < n) n = cnt;
+ if ( cnt < n ) n = cnt;
- uint16_t rRel = get_relative_pointer(f, rAbs);
+ uint16_t rd_ptr = idx2ptr(f->depth, rd_idx);
// Peek data
- _ff_pull_n(f, p_buffer, n, rRel, copy_mode);
+ _ff_pull_n(f, p_buffer, n, rd_ptr, copy_mode);
return n;
}
-// Works on local copies of w and r
-static inline uint16_t _tu_fifo_remaining(tu_fifo_t* f, uint16_t wAbs, uint16_t rAbs)
-{
- return f->depth - _tu_fifo_count(f, wAbs, rAbs);
-}
-
static uint16_t _tu_fifo_write_n(tu_fifo_t* f, const void * data, uint16_t n, tu_fifo_copy_mode_t copy_mode)
{
if ( n == 0 ) return 0;
_ff_lock(f->mutex_wr);
- uint16_t w = f->wr_idx, r = f->rd_idx;
+ uint16_t wr_idx = f->wr_idx;
+ uint16_t rd_idx = f->rd_idx;
+
uint8_t const* buf8 = (uint8_t const*) data;
- if (!f->overwritable)
- {
- // Not overwritable limit up to full
- n = tu_min16(n, _tu_fifo_remaining(f, w, r));
- }
- else if (n >= f->depth)
- {
- // Only copy last part
- buf8 = buf8 + (n - f->depth) * f->item_size;
- n = f->depth;
+ TU_LOG(TU_FIFO_DBG, "rd = %3u, wr = %3u, count = %3u, remain = %3u, n = %3u: ",
+ rd_idx, wr_idx, _ff_count(f->depth, wr_idx, rd_idx), _ff_remaining(f->depth, wr_idx, rd_idx), n);
- // We start writing at the read pointer's position since we fill the complete
- // buffer and we do not want to modify the read pointer within a write function!
- // This would end up in a race condition with read functions!
- w = r;
+ if ( !f->overwritable )
+ {
+ // limit up to full
+ uint16_t const remain = _ff_remaining(f->depth, wr_idx, rd_idx);
+ n = tu_min16(n, remain);
+ }
+ else
+ {
+ // In over-writable mode, fifo_write() is allowed even when fifo is full. In such case,
+ // oldest data in fifo i.e at read pointer data will be overwritten
+ // Note: we can modify read buffer contents but we must not modify the read index itself within a write function!
+ // Since it would end up in a race condition with read functions!
+ if ( n >= f->depth )
+ {
+ // Only copy last part
+ if ( copy_mode == TU_FIFO_COPY_INC )
+ {
+ buf8 += (n - f->depth) * f->item_size;
+ }else
+ {
+ // TODO should read from hw fifo to discard data, however reading an odd number could
+ // accidentally discard data.
+ }
+
+ n = f->depth;
+
+ // We start writing at the read pointer's position since we fill the whole buffer
+ wr_idx = rd_idx;
+ }
+ else
+ {
+ uint16_t const overflowable_count = _ff_count(f->depth, wr_idx, rd_idx);
+ if (overflowable_count + n >= 2*f->depth)
+ {
+ // Double overflowed
+ // Index is bigger than the allowed range [0,2*depth)
+ // re-position write index to have a full fifo after pushed
+ wr_idx = advance_index(f->depth, rd_idx, f->depth - n);
+
+ // TODO we should also shift out n bytes from read index since we avoid changing rd index !!
+ // However memmove() is expensive due to actual copying + wrapping consideration.
+ // Also race condition could happen anyway if read() is invoke while moving result in corrupted memory
+ // currently deliberately not implemented --> result in incorrect data read back
+ }else
+ {
+ // normal + single overflowed:
+ // Index is in the range of [0,2*depth) and thus detect and recoverable. Recovering is handled in read()
+ // Therefore we just increase write index
+ // we will correct (re-position) read index later on in fifo_read() function
+ }
+ }
}
- uint16_t wRel = get_relative_pointer(f, w);
+ if (n)
+ {
+ uint16_t wr_ptr = idx2ptr(f->depth, wr_idx);
- // Write data
- _ff_push_n(f, buf8, n, wRel, copy_mode);
+ TU_LOG(TU_FIFO_DBG, "actual_n = %u, wr_ptr = %u", n, wr_ptr);
- // Advance pointer
- f->wr_idx = advance_pointer(f, w, n);
+ // Write data
+ _ff_push_n(f, buf8, n, wr_ptr, copy_mode);
+
+ // Advance index
+ f->wr_idx = advance_index(f->depth, wr_idx, n);
+
+ TU_LOG(TU_FIFO_DBG, "\tnew_wr = %u\n", f->wr_idx);
+ }
_ff_unlock(f->mutex_wr);
@@ -504,12 +556,16 @@ static uint16_t _tu_fifo_read_n(tu_fifo_t* f, void * buffer, uint16_t n, tu_fifo
n = _tu_fifo_peek_n(f, buffer, n, f->wr_idx, f->rd_idx, copy_mode);
// Advance read pointer
- f->rd_idx = advance_pointer(f, f->rd_idx, n);
+ f->rd_idx = advance_index(f->depth, f->rd_idx, n);
_ff_unlock(f->mutex_rd);
return n;
}
+//--------------------------------------------------------------------+
+// Application API
+//--------------------------------------------------------------------+
+
/******************************************************************************/
/*!
@brief Get number of items in FIFO.
@@ -527,7 +583,7 @@ static uint16_t _tu_fifo_read_n(tu_fifo_t* f, void * buffer, uint16_t n, tu_fifo
/******************************************************************************/
uint16_t tu_fifo_count(tu_fifo_t* f)
{
- return tu_min16(_tu_fifo_count(f, f->wr_idx, f->rd_idx), f->depth);
+ return tu_min16(_ff_count(f->depth, f->wr_idx, f->rd_idx), f->depth);
}
/******************************************************************************/
@@ -545,7 +601,7 @@ uint16_t tu_fifo_count(tu_fifo_t* f)
/******************************************************************************/
bool tu_fifo_empty(tu_fifo_t* f)
{
- return _tu_fifo_empty(f->wr_idx, f->rd_idx);
+ return f->wr_idx == f->rd_idx;
}
/******************************************************************************/
@@ -563,7 +619,7 @@ bool tu_fifo_empty(tu_fifo_t* f)
/******************************************************************************/
bool tu_fifo_full(tu_fifo_t* f)
{
- return _tu_fifo_full(f, f->wr_idx, f->rd_idx);
+ return _ff_count(f->depth, f->wr_idx, f->rd_idx) >= f->depth;
}
/******************************************************************************/
@@ -581,7 +637,7 @@ bool tu_fifo_full(tu_fifo_t* f)
/******************************************************************************/
uint16_t tu_fifo_remaining(tu_fifo_t* f)
{
- return _tu_fifo_remaining(f, f->wr_idx, f->rd_idx);
+ return _ff_remaining(f->depth, f->wr_idx, f->rd_idx);
}
/******************************************************************************/
@@ -607,14 +663,14 @@ uint16_t tu_fifo_remaining(tu_fifo_t* f)
/******************************************************************************/
bool tu_fifo_overflowed(tu_fifo_t* f)
{
- return _tu_fifo_overflowed(f, f->wr_idx, f->rd_idx);
+ return _ff_count(f->depth, f->wr_idx, f->rd_idx) > f->depth;
}
// Only use in case tu_fifo_overflow() returned true!
void tu_fifo_correct_read_pointer(tu_fifo_t* f)
{
_ff_lock(f->mutex_rd);
- _tu_fifo_correct_read_pointer(f, f->wr_idx);
+ _ff_correct_read_index(f, f->wr_idx);
_ff_unlock(f->mutex_rd);
}
@@ -643,7 +699,7 @@ bool tu_fifo_read(tu_fifo_t* f, void * buffer)
bool ret = _tu_fifo_peek(f, buffer, f->wr_idx, f->rd_idx);
// Advance pointer
- f->rd_idx = advance_pointer(f, f->rd_idx, ret);
+ f->rd_idx = advance_index(f->depth, f->rd_idx, ret);
_ff_unlock(f->mutex_rd);
return ret;
@@ -682,8 +738,6 @@ uint16_t tu_fifo_read_n_const_addr_full_words(tu_fifo_t* f, void * buffer, uint1
@param[in] f
Pointer to the FIFO buffer to manipulate
- @param[in] offset
- Position to read from in the FIFO buffer with respect to read pointer
@param[in] p_buffer
Pointer to the place holder for data read from the buffer
@@ -742,20 +796,20 @@ bool tu_fifo_write(tu_fifo_t* f, const void * data)
_ff_lock(f->mutex_wr);
bool ret;
- uint16_t const w = f->wr_idx;
+ uint16_t const wr_idx = f->wr_idx;
- if ( _tu_fifo_full(f, w, f->rd_idx) && !f->overwritable )
+ if ( tu_fifo_full(f) && !f->overwritable )
{
ret = false;
}else
{
- uint16_t wRel = get_relative_pointer(f, w);
+ uint16_t wr_ptr = idx2ptr(f->depth, wr_idx);
// Write data
- _ff_push(f, data, wRel);
+ _ff_push(f, data, wr_ptr);
// Advance pointer
- f->wr_idx = advance_pointer(f, w, 1);
+ f->wr_idx = advance_index(f->depth, wr_idx, 1);
ret = true;
}
@@ -817,9 +871,8 @@ bool tu_fifo_clear(tu_fifo_t *f)
_ff_lock(f->mutex_wr);
_ff_lock(f->mutex_rd);
- f->rd_idx = f->wr_idx = 0;
- f->max_pointer_idx = (uint16_t) (2*f->depth-1);
- f->non_used_index_space = UINT16_MAX - f->max_pointer_idx;
+ f->rd_idx = 0;
+ f->wr_idx = 0;
_ff_unlock(f->mutex_wr);
_ff_unlock(f->mutex_rd);
@@ -857,7 +910,7 @@ bool tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable)
As long as the DMA is the only process writing into the FIFO this is safe
to use.
- USE WITH CARE - WE DO NOT CONDUCT SAFTY CHECKS HERE!
+ USE WITH CARE - WE DO NOT CONDUCT SAFETY CHECKS HERE!
@param[in] f
Pointer to the FIFO buffer to manipulate
@@ -867,7 +920,7 @@ bool tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable)
/******************************************************************************/
void tu_fifo_advance_write_pointer(tu_fifo_t *f, uint16_t n)
{
- f->wr_idx = advance_pointer(f, f->wr_idx, n);
+ f->wr_idx = advance_index(f->depth, f->wr_idx, n);
}
/******************************************************************************/
@@ -878,7 +931,7 @@ void tu_fifo_advance_write_pointer(tu_fifo_t *f, uint16_t n)
FIFO. As long as the DMA is the only process reading from the FIFO this is
safe to use.
- USE WITH CARE - WE DO NOT CONDUCT SAFTY CHECKS HERE!
+ USE WITH CARE - WE DO NOT CONDUCT SAFETY CHECKS HERE!
@param[in] f
Pointer to the FIFO buffer to manipulate
@@ -888,7 +941,7 @@ void tu_fifo_advance_write_pointer(tu_fifo_t *f, uint16_t n)
/******************************************************************************/
void tu_fifo_advance_read_pointer(tu_fifo_t *f, uint16_t n)
{
- f->rd_idx = advance_pointer(f, f->rd_idx, n);
+ f->rd_idx = advance_index(f->depth, f->rd_idx, n);
}
/******************************************************************************/
@@ -909,17 +962,18 @@ void tu_fifo_advance_read_pointer(tu_fifo_t *f, uint16_t n)
void tu_fifo_get_read_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
{
// Operate on temporary values in case they change in between
- uint16_t w = f->wr_idx, r = f->rd_idx;
+ uint16_t wr_idx = f->wr_idx;
+ uint16_t rd_idx = f->rd_idx;
- uint16_t cnt = _tu_fifo_count(f, w, r);
+ uint16_t cnt = _ff_count(f->depth, wr_idx, rd_idx);
// Check overflow and correct if required - may happen in case a DMA wrote too fast
if (cnt > f->depth)
{
_ff_lock(f->mutex_rd);
- _tu_fifo_correct_read_pointer(f, w);
+ rd_idx = _ff_correct_read_index(f, wr_idx);
_ff_unlock(f->mutex_rd);
- r = f->rd_idx;
+
cnt = f->depth;
}
@@ -934,22 +988,25 @@ void tu_fifo_get_read_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
}
// Get relative pointers
- w = get_relative_pointer(f, w);
- r = get_relative_pointer(f, r);
+ uint16_t wr_ptr = idx2ptr(f->depth, wr_idx);
+ uint16_t rd_ptr = idx2ptr(f->depth, rd_idx);
// Copy pointer to buffer to start reading from
- info->ptr_lin = &f->buffer[r];
+ info->ptr_lin = &f->buffer[rd_ptr];
// Check if there is a wrap around necessary
- if (w > r) {
+ if (wr_ptr > rd_ptr)
+ {
// Non wrapping case
info->len_lin = cnt;
+
info->len_wrap = 0;
info->ptr_wrap = NULL;
}
else
{
- info->len_lin = f->depth - r; // Also the case if FIFO was full
+ info->len_lin = f->depth - rd_ptr; // Also the case if FIFO was full
+
info->len_wrap = cnt - info->len_lin;
info->ptr_wrap = f->buffer;
}
@@ -972,36 +1029,37 @@ void tu_fifo_get_read_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
/******************************************************************************/
void tu_fifo_get_write_info(tu_fifo_t *f, tu_fifo_buffer_info_t *info)
{
- uint16_t w = f->wr_idx, r = f->rd_idx;
- uint16_t free = _tu_fifo_remaining(f, w, r);
+ uint16_t wr_idx = f->wr_idx;
+ uint16_t rd_idx = f->rd_idx;
+ uint16_t remain = _ff_remaining(f->depth, wr_idx, rd_idx);
- if (free == 0)
+ if (remain == 0)
{
- info->len_lin = 0;
+ info->len_lin = 0;
info->len_wrap = 0;
- info->ptr_lin = NULL;
+ info->ptr_lin = NULL;
info->ptr_wrap = NULL;
return;
}
// Get relative pointers
- w = get_relative_pointer(f, w);
- r = get_relative_pointer(f, r);
+ uint16_t wr_ptr = idx2ptr(f->depth, wr_idx);
+ uint16_t rd_ptr = idx2ptr(f->depth, rd_idx);
// Copy pointer to buffer to start writing to
- info->ptr_lin = &f->buffer[w];
+ info->ptr_lin = &f->buffer[wr_ptr];
- if (w < r)
+ if (wr_ptr < rd_ptr)
{
// Non wrapping case
- info->len_lin = r-w;
+ info->len_lin = rd_ptr-wr_ptr;
info->len_wrap = 0;
info->ptr_wrap = NULL;
}
else
{
- info->len_lin = f->depth - w;
- info->len_wrap = free - info->len_lin; // Remaining length - n already was limited to free or FIFO depth
- info->ptr_wrap = f->buffer; // Always start of buffer
+ info->len_lin = f->depth - wr_ptr;
+ info->len_wrap = remain - info->len_lin; // Remaining length - n already was limited to remain or FIFO depth
+ info->ptr_wrap = f->buffer; // Always start of buffer
}
}
diff --git a/src/common/tusb_fifo.h b/src/common/tusb_fifo.h
index 18db289a1..2f60ec2f4 100644
--- a/src/common/tusb_fifo.h
+++ b/src/common/tusb_fifo.h
@@ -32,7 +32,7 @@
extern "C" {
#endif
-// Due to the use of unmasked pointers, this FIFO does not suffer from loosing
+// Due to the use of unmasked pointers, this FIFO does not suffer from losing
// one item slice. Furthermore, write and read operations are completely
// decoupled as write and read functions do not modify a common state. Henceforth,
// writing or reading from the FIFO within an ISR is safe as long as no other
@@ -42,32 +42,84 @@ extern "C" {
// within a certain number (see tu_fifo_overflow()).
#include "common/tusb_common.h"
+#include "osal/osal.h"
// mutex is only needed for RTOS
// for OS None, we don't get preempted
-#define CFG_FIFO_MUTEX (CFG_TUSB_OS != OPT_OS_NONE)
+#define CFG_FIFO_MUTEX OSAL_MUTEX_REQUIRED
-#if CFG_FIFO_MUTEX
-#include "osal/osal.h"
-#define tu_fifo_mutex_t osal_mutex_t
-#endif
+/* Write/Read index is always in the range of:
+ * 0 .. 2*depth-1
+ * The extra window allow us to determine the fifo state of empty or full with only 2 indices
+ * Following are examples with depth = 3
+ *
+ * - empty: W = R
+ * |
+ * -------------------------
+ * | 0 | RW| 2 | 3 | 4 | 5 |
+ *
+ * - full 1: W > R
+ * |
+ * -------------------------
+ * | 0 | R | 2 | 3 | W | 5 |
+ *
+ * - full 2: W < R
+ * |
+ * -------------------------
+ * | 0 | 1 | W | 3 | 4 | R |
+ *
+ * - Number of items in the fifo can be determined in either cases:
+ * - case W >= R: Count = W - R
+ * - case W < R: Count = 2*depth - (R - W)
+ *
+ * In non-overwritable mode, computed Count (in above 2 cases) is at most equal to depth.
+ * However, in over-writable mode, write index can be repeatedly increased and count can be
+ * temporarily larger than depth (overflowed condition) e.g
+ *
+ * - Overflowed 1: write(3), write(1)
+ * In this case we will adjust Read index when read()/peek() is called so that count = depth.
+ * |
+ * -------------------------
+ * | R | 1 | 2 | 3 | W | 5 |
+ *
+ * - Double Overflowed i.e index is out of allowed range [0,2*depth)
+ * This occurs when we continue to write after 1st overflowed to 2nd overflowed. e.g:
+ * write(3), write(1), write(2)
+ * This must be prevented since it will cause unrecoverable state, in above example
+ * if not handled the fifo will be empty instead of continue-to-be full. Since we must not modify
+ * read index in write() function, which cause race condition. We will re-position write index so that
+ * after data is written it is a full fifo i.e W = depth - R
+ *
+ * re-position W = 1 before write(2)
+ * Note: we should also move data from mem[3] to read index as well, but deliberately skipped here
+ * since it is an expensive operation !!!
+ * |
+ * -------------------------
+ * | R | W | 2 | 3 | 4 | 5 |
+ *
+ * perform write(2), result is still a full fifo.
+ *
+ * |
+ * -------------------------
+ * | R | 1 | 2 | W | 4 | 5 |
+ */
typedef struct
{
- uint8_t* buffer ; ///< buffer pointer
- uint16_t depth ; ///< max items
- uint16_t item_size ; ///< size of each item
- bool overwritable ;
+ uint8_t* buffer ; // buffer pointer
+ uint16_t depth ; // max items
- uint16_t non_used_index_space ; ///< required for non-power-of-two buffer length
- uint16_t max_pointer_idx ; ///< maximum absolute pointer index
+ struct TU_ATTR_PACKED {
+ uint16_t item_size : 15; // size of each item
+ bool overwritable : 1 ; // ovwerwritable when full
+ };
- volatile uint16_t wr_idx ; ///< write pointer
- volatile uint16_t rd_idx ; ///< read pointer
+ volatile uint16_t wr_idx ; // write index
+ volatile uint16_t rd_idx ; // read index
-#if CFG_FIFO_MUTEX
- tu_fifo_mutex_t mutex_wr;
- tu_fifo_mutex_t mutex_rd;
+#if OSAL_MUTEX_REQUIRED
+ osal_mutex_t mutex_wr;
+ osal_mutex_t mutex_rd;
#endif
} tu_fifo_t;
@@ -86,8 +138,6 @@ typedef struct
.depth = _depth, \
.item_size = sizeof(_type), \
.overwritable = _overwritable, \
- .non_used_index_space = UINT16_MAX - (2*(_depth)-1), \
- .max_pointer_idx = 2*(_depth)-1, \
}
#define TU_FIFO_DEF(_name, _depth, _type, _overwritable) \
@@ -99,13 +149,18 @@ bool tu_fifo_set_overwritable(tu_fifo_t *f, bool overwritable);
bool tu_fifo_clear(tu_fifo_t *f);
bool tu_fifo_config(tu_fifo_t *f, void* buffer, uint16_t depth, uint16_t item_size, bool overwritable);
-#if CFG_FIFO_MUTEX
+#if OSAL_MUTEX_REQUIRED
TU_ATTR_ALWAYS_INLINE static inline
-void tu_fifo_config_mutex(tu_fifo_t *f, tu_fifo_mutex_t write_mutex_hdl, tu_fifo_mutex_t read_mutex_hdl)
+void tu_fifo_config_mutex(tu_fifo_t *f, osal_mutex_t wr_mutex, osal_mutex_t rd_mutex)
{
- f->mutex_wr = write_mutex_hdl;
- f->mutex_rd = read_mutex_hdl;
+ f->mutex_wr = wr_mutex;
+ f->mutex_rd = rd_mutex;
}
+
+#else
+
+#define tu_fifo_config_mutex(_f, _wr_mutex, _rd_mutex)
+
#endif
bool tu_fifo_write (tu_fifo_t* f, void const * p_data);
@@ -133,7 +188,7 @@ uint16_t tu_fifo_depth(tu_fifo_t* f)
}
// Pointer modifications intended to be used in combinations with DMAs.
-// USE WITH CARE - NO SAFTY CHECKS CONDUCTED HERE! NOT MUTEX PROTECTED!
+// USE WITH CARE - NO SAFETY CHECKS CONDUCTED HERE! NOT MUTEX PROTECTED!
void tu_fifo_advance_write_pointer(tu_fifo_t *f, uint16_t n);
void tu_fifo_advance_read_pointer (tu_fifo_t *f, uint16_t n);
diff --git a/src/common/tusb_mcu.h b/src/common/tusb_mcu.h
index 86c68baf8..0b10c5118 100644
--- a/src/common/tusb_mcu.h
+++ b/src/common/tusb_mcu.h
@@ -275,12 +275,20 @@
#elif TU_CHECK_MCU(OPT_MCU_F1C100S)
#define TUP_DCD_ENDPOINT_MAX 4
+//------------- WCH -------------//
+#elif TU_CHECK_MCU(OPT_MCU_CH32V307)
+ #define TUP_DCD_ENDPOINT_MAX 16
+ #define TUP_RHPORT_HIGHSPEED 1
#endif
//--------------------------------------------------------------------+
// Default Values
//--------------------------------------------------------------------+
+#ifndef TUP_MCU_MULTIPLE_CORE
+#define TUP_MCU_MULTIPLE_CORE 0
+#endif
+
#ifndef TUP_DCD_ENDPOINT_MAX
#warning "TUP_DCD_ENDPOINT_MAX is not defined for this MCU, default to 8"
#define TUP_DCD_ENDPOINT_MAX 8
diff --git a/src/common/tusb_private.h b/src/common/tusb_private.h
index b34506f65..d5541856c 100644
--- a/src/common/tusb_private.h
+++ b/src/common/tusb_private.h
@@ -28,6 +28,8 @@
#ifndef _TUSB_PRIVATE_H_
#define _TUSB_PRIVATE_H_
+// Internal Helper used by Host and Device Stack
+
#ifdef __cplusplus
extern "C" {
#endif
@@ -39,8 +41,31 @@ typedef struct TU_ATTR_PACKED
volatile uint8_t claimed : 1;
}tu_edpt_state_t;
+typedef struct {
+ bool is_host; // host or device most
+ union {
+ uint8_t daddr;
+ uint8_t rhport;
+ uint8_t hwid;
+ };
+ uint8_t ep_addr;
+ uint8_t ep_speed;
+
+ uint16_t ep_packetsize;
+ uint16_t ep_bufsize;
+
+ // TODO xfer_fifo can skip this buffer
+ uint8_t* ep_buf;
+
+ tu_fifo_t ff;
+
+ // mutex: read if ep rx, write if e tx
+ OSAL_MUTEX_DEF(ff_mutex);
+
+}tu_edpt_stream_t;
+
//--------------------------------------------------------------------+
-// Internal Helper used by Host and Device Stack
+// Endpoint
//--------------------------------------------------------------------+
// Check if endpoint descriptor is valid per USB specs
@@ -58,6 +83,89 @@ bool tu_edpt_claim(tu_edpt_state_t* ep_state, osal_mutex_t mutex);
// Release an endpoint with provided mutex
bool tu_edpt_release(tu_edpt_state_t* ep_state, osal_mutex_t mutex);
+//--------------------------------------------------------------------+
+// Endpoint Stream
+//--------------------------------------------------------------------+
+
+// Init an stream, should only be called once
+bool tu_edpt_stream_init(tu_edpt_stream_t* s, bool is_host, bool is_tx, bool overwritable,
+ void* ff_buf, uint16_t ff_bufsize, uint8_t* ep_buf, uint16_t ep_bufsize);
+
+// Open an stream for an endpoint
+// hwid is either device address (host mode) or rhport (device mode)
+TU_ATTR_ALWAYS_INLINE static inline
+void tu_edpt_stream_open(tu_edpt_stream_t* s, uint8_t hwid, tusb_desc_endpoint_t const *desc_ep)
+{
+ tu_fifo_clear(&s->ff);
+ s->hwid = hwid;
+ s->ep_addr = desc_ep->bEndpointAddress;
+ s->ep_packetsize = tu_edpt_packet_size(desc_ep);
+}
+
+TU_ATTR_ALWAYS_INLINE static inline
+void tu_edpt_stream_close(tu_edpt_stream_t* s)
+{
+ s->hwid = 0;
+ s->ep_addr = 0;
+}
+
+// Clear fifo
+TU_ATTR_ALWAYS_INLINE static inline
+bool tu_edpt_stream_clear(tu_edpt_stream_t* s)
+{
+ return tu_fifo_clear(&s->ff);
+}
+
+//--------------------------------------------------------------------+
+// Stream Write
+//--------------------------------------------------------------------+
+
+// Write to stream
+uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const *buffer, uint32_t bufsize);
+
+// Start an usb transfer if endpoint is not busy
+uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s);
+
+// Start an zero-length packet if needed
+bool tu_edpt_stream_write_zlp_if_needed(tu_edpt_stream_t* s, uint32_t last_xferred_bytes);
+
+// Get the number of bytes available for writing
+TU_ATTR_ALWAYS_INLINE static inline
+uint32_t tu_edpt_stream_write_available(tu_edpt_stream_t* s)
+{
+ return (uint32_t) tu_fifo_remaining(&s->ff);
+}
+
+//--------------------------------------------------------------------+
+// Stream Read
+//--------------------------------------------------------------------+
+
+// Read from stream
+uint32_t tu_edpt_stream_read(tu_edpt_stream_t* s, void* buffer, uint32_t bufsize);
+
+// Start an usb transfer if endpoint is not busy
+uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s);
+
+// Must be called in the transfer complete callback
+TU_ATTR_ALWAYS_INLINE static inline
+void tu_edpt_stream_read_xfer_complete(tu_edpt_stream_t* s, uint32_t xferred_bytes)
+{
+ tu_fifo_write_n(&s->ff, s->ep_buf, (uint16_t) xferred_bytes);
+}
+
+// Get the number of bytes available for reading
+TU_ATTR_ALWAYS_INLINE static inline
+uint32_t tu_edpt_stream_read_available(tu_edpt_stream_t* s)
+{
+ return (uint32_t) tu_fifo_count(&s->ff);
+}
+
+TU_ATTR_ALWAYS_INLINE static inline
+bool tu_edpt_stream_peek(tu_edpt_stream_t* s, uint8_t* ch)
+{
+ return tu_fifo_peek(&s->ff, ch);
+}
+
#ifdef __cplusplus
}
#endif
diff --git a/src/common/tusb_types.h b/src/common/tusb_types.h
index 1bfa7c7d1..32cdba450 100644
--- a/src/common/tusb_types.h
+++ b/src/common/tusb_types.h
@@ -69,6 +69,15 @@ typedef enum
TUSB_DIR_IN_MASK = 0x80
}tusb_dir_t;
+enum
+{
+ TUSB_EPSIZE_BULK_FS = 64,
+ TUSB_EPSIZE_BULK_HS= 512,
+
+ TUSB_EPSIZE_ISO_FS_MAX = 1023,
+ TUSB_EPSIZE_ISO_HS_MAX = 1024,
+};
+
/// Isochronous End Point Attributes
typedef enum
{
@@ -243,7 +252,6 @@ enum
INTERFACE_INVALID_NUMBER = 0xff
};
-
typedef enum
{
MS_OS_20_SET_HEADER_DESCRIPTOR = 0x00,
@@ -265,6 +273,11 @@ enum
CONTROL_STAGE_ACK
};
+enum
+{
+ TUSB_INDEX_INVALID = 0xff
+};
+
//--------------------------------------------------------------------+
// USB Descriptors
//--------------------------------------------------------------------+
diff --git a/src/device/usbd.c b/src/device/usbd.c
index c199e647e..6e0c6710d 100644
--- a/src/device/usbd.c
+++ b/src/device/usbd.c
@@ -39,13 +39,13 @@
// USBD Configuration
//--------------------------------------------------------------------+
-// Debug level of USBD
-#define USBD_DBG 2
-
#ifndef CFG_TUD_TASK_QUEUE_SZ
#define CFG_TUD_TASK_QUEUE_SZ 16
#endif
+// Debug level of USBD
+#define USBD_DBG 2
+
//--------------------------------------------------------------------+
// Device Data
//--------------------------------------------------------------------+
@@ -272,10 +272,12 @@ static uint8_t _usbd_rhport = RHPORT_INVALID;
OSAL_QUEUE_DEF(usbd_int_set, _usbd_qdef, CFG_TUD_TASK_QUEUE_SZ, dcd_event_t);
static osal_queue_t _usbd_q;
-// Mutex for claiming endpoint, only needed when using with preempted RTOS
-#if CFG_TUSB_OS != OPT_OS_NONE
-static osal_mutex_def_t _ubsd_mutexdef;
-static osal_mutex_t _usbd_mutex;
+// Mutex for claiming endpoint
+#if OSAL_MUTEX_REQUIRED
+ static osal_mutex_def_t _ubsd_mutexdef;
+ static osal_mutex_t _usbd_mutex;
+#else
+ #define _usbd_mutex NULL
#endif
@@ -386,10 +388,12 @@ bool tud_init (uint8_t rhport)
TU_LOG(USBD_DBG, "USBD init on controller %u\r\n", rhport);
TU_LOG_INT(USBD_DBG, sizeof(usbd_device_t));
+ TU_LOG_INT(USBD_DBG, sizeof(tu_fifo_t));
+ TU_LOG_INT(USBD_DBG, sizeof(tu_edpt_stream_t));
tu_varclr(&_usbd_dev);
-#if CFG_TUSB_OS != OPT_OS_NONE
+#if OSAL_MUTEX_REQUIRED
// Init device mutex
_usbd_mutex = osal_mutex_create(&_ubsd_mutexdef);
TU_ASSERT(_usbd_mutex);
@@ -504,7 +508,7 @@ void tud_task_ext(uint32_t timeout_ms, bool in_isr)
break;
case DCD_EVENT_SETUP_RECEIVED:
- TU_LOG_VAR(USBD_DBG, &event.setup_received);
+ TU_LOG_PTR(USBD_DBG, &event.setup_received);
TU_LOG(USBD_DBG, "\r\n");
// Mark as connected after receiving 1st setup packet.
@@ -1209,11 +1213,7 @@ bool usbd_edpt_claim(uint8_t rhport, uint8_t ep_addr)
uint8_t const dir = tu_edpt_dir(ep_addr);
tu_edpt_state_t* ep_state = &_usbd_dev.ep_status[epnum][dir];
-#if TUSB_OPT_MUTEX
return tu_edpt_claim(ep_state, _usbd_mutex);
-#else
- return tu_edpt_claim(ep_state, NULL);
-#endif
}
bool usbd_edpt_release(uint8_t rhport, uint8_t ep_addr)
@@ -1224,11 +1224,7 @@ bool usbd_edpt_release(uint8_t rhport, uint8_t ep_addr)
uint8_t const dir = tu_edpt_dir(ep_addr);
tu_edpt_state_t* ep_state = &_usbd_dev.ep_status[epnum][dir];
-#if TUSB_OPT_MUTEX
return tu_edpt_release(ep_state, _usbd_mutex);
-#else
- return tu_edpt_release(ep_state, NULL);
-#endif
}
bool usbd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t total_bytes)
diff --git a/src/device/usbd.h b/src/device/usbd.h
index 731c57aad..ad19d1045 100644
--- a/src/device/usbd.h
+++ b/src/device/usbd.h
@@ -293,7 +293,7 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb
/* MIDI Streaming (MS) Interface */\
9, TUSB_DESC_INTERFACE, (uint8_t)((_itfnum) + 1), 0, 2, TUSB_CLASS_AUDIO, AUDIO_SUBCLASS_MIDI_STREAMING, AUDIO_FUNC_PROTOCOL_CODE_UNDEF, 0,\
/* MS Header */\
- 7, TUSB_DESC_CS_INTERFACE, MIDI_CS_INTERFACE_HEADER, U16_TO_U8S_LE(0x0100), U16_TO_U8S_LE(7 + (_numcables) * TUD_MIDI_DESC_JACK_LEN)
+ 7, TUSB_DESC_CS_INTERFACE, MIDI_CS_INTERFACE_HEADER, U16_TO_U8S_LE(0x0100), U16_TO_U8S_LE(7 + (_numcables) * TUD_MIDI_DESC_JACK_LEN + 2 * TUD_MIDI_DESC_EP_LEN(_numcables))
#define TUD_MIDI_JACKID_IN_EMB(_cablenum) \
(uint8_t)(((_cablenum) - 1) * 4 + 1)
@@ -317,6 +317,7 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb
9, TUSB_DESC_CS_INTERFACE, MIDI_CS_INTERFACE_OUT_JACK, MIDI_JACK_EMBEDDED, TUD_MIDI_JACKID_OUT_EMB(_cablenum), 1, TUD_MIDI_JACKID_IN_EXT(_cablenum), 1, _stridx,\
/* MS Out Jack (External), connected to In Jack Embedded */\
9, TUSB_DESC_CS_INTERFACE, MIDI_CS_INTERFACE_OUT_JACK, MIDI_JACK_EXTERNAL, TUD_MIDI_JACKID_OUT_EXT(_cablenum), 1, TUD_MIDI_JACKID_IN_EMB(_cablenum), 1, _stridx
+
#define TUD_MIDI_DESC_JACK(_cablenum) TUD_MIDI_DESC_JACK_DESC(_cablenum, 0)
#define TUD_MIDI_DESC_EP_LEN(_numcables) (9 + 4 + (_numcables))
@@ -603,7 +604,7 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb
/* optional interrupt endpoint */ \
// _int_pollingInterval : for LS/FS, expressed in frames (1ms each). 16 may be a good number?
#define TUD_USBTMC_INT_DESCRIPTOR(_ep_interrupt, _ep_interrupt_size, _int_pollingInterval ) \
- 7, TUSB_DESC_ENDPOINT, _ep_interrupt, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_interrupt_size), 0x16
+ 7, TUSB_DESC_ENDPOINT, _ep_interrupt, TUSB_XFER_INTERRUPT, U16_TO_U8S_LE(_ep_interrupt_size), _int_pollingInterval
#define TUD_USBTMC_INT_DESCRIPTOR_LEN (7u)
@@ -648,7 +649,7 @@ TU_ATTR_WEAK bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb
#define TUD_DFU_DESC_LEN(_alt_count) (9 + (_alt_count) * 9)
// Interface number, Alternate count, starting string index, attributes, detach timeout, transfer size
-// Note: Alternate count must be numberic or macro, string index is increased by one for each Alt interface
+// Note: Alternate count must be numeric or macro, string index is increased by one for each Alt interface
#define TUD_DFU_DESCRIPTOR(_itfnum, _alt_count, _stridx, _attr, _timeout, _xfer_size) \
TU_XSTRCAT(_TUD_DFU_ALT_,_alt_count)(_itfnum, 0, _stridx), \
/* Function */ \
diff --git a/src/host/usbh.c b/src/host/usbh.c
index d97b160c9..5ac9e9cca 100644
--- a/src/host/usbh.c
+++ b/src/host/usbh.c
@@ -30,7 +30,6 @@
#include "host/hcd.h"
#include "tusb.h"
-#include "common/tusb_private.h"
#include "host/usbh_classdriver.h"
#include "hub.h"
@@ -46,8 +45,10 @@
#define CFG_TUH_INTERFACE_MAX 8
#endif
-// Debug level of USBD
-#define USBH_DBG_LVL 2
+// Debug level, TUSB_CFG_DEBUG must be at least this level for debug message
+#define USBH_DEBUG 2
+
+#define TU_LOG_USBH(...) TU_LOG(USBH_DEBUG, __VA_ARGS__)
//--------------------------------------------------------------------+
// USBH-HCD common data structure
@@ -212,28 +213,12 @@ static usbh_dev0_t _dev0;
// TODO: hub can has its own simpler struct to save memory
CFG_TUSB_MEM_SECTION usbh_device_t _usbh_devices[TOTAL_DEVICES];
-// Mutex for claiming endpoint, only needed when using with preempted RTOS
-#if TUSB_OPT_MUTEX
-static osal_mutex_def_t _usbh_mutexdef;
-static osal_mutex_t _usbh_mutex;
-
-TU_ATTR_ALWAYS_INLINE static inline void usbh_lock(void)
-{
- osal_mutex_lock(_usbh_mutex, OSAL_TIMEOUT_WAIT_FOREVER);
-}
-
-TU_ATTR_ALWAYS_INLINE static inline void usbh_unlock(void)
-{
- osal_mutex_unlock(_usbh_mutex);
-}
-
+// Mutex for claiming endpoint
+#if OSAL_MUTEX_REQUIRED
+ static osal_mutex_def_t _usbh_mutexdef;
+ static osal_mutex_t _usbh_mutex;
#else
-
-#define _usbh_mutex NULL
-
-#define usbh_lock()
-#define usbh_unlock()
-
+ #define _usbh_mutex NULL
#endif
// Event queue
@@ -244,10 +229,10 @@ static osal_queue_t _usbh_q;
CFG_TUSB_MEM_SECTION CFG_TUSB_MEM_ALIGN
static uint8_t _usbh_ctrl_buf[CFG_TUH_ENUMERATION_BUFSIZE];
-// Control transfer: since most controller does not support multiple control transfer
-// on multiple devices concurrently. And control transfer is not used much except enumeration
-// We will only execute control transfer one at a time.
-struct
+// Control transfers: since most controllers do not support multiple control transfers
+// on multiple devices concurrently and control transfers are not used much except for
+// enumeration, we will only execute control transfers one at a time.
+CFG_TUSB_MEM_SECTION struct
{
tusb_control_request_t request TU_ATTR_ALIGNED(4);
uint8_t* buffer;
@@ -277,8 +262,6 @@ static bool usbh_control_xfer_cb (uint8_t daddr, uint8_t ep_addr, xfer_result_t
// TODO rework time-related function later
void osal_task_delay(uint32_t msec)
{
- (void) msec;
-
const uint32_t start = hcd_frame_number(_usbh_controller);
while ( ( hcd_frame_number(_usbh_controller) - start ) < msec ) {}
}
@@ -342,18 +325,20 @@ bool tuh_init(uint8_t controller_id)
// skip if already initialized
if ( tuh_inited() ) return true;
- TU_LOG2("USBH init on controller %u\r\n", controller_id);
- TU_LOG2_INT(sizeof(usbh_device_t));
- TU_LOG2_INT(sizeof(hcd_event_t));
- TU_LOG2_INT(sizeof(_ctrl_xfer));
- TU_LOG2_INT(sizeof(tuh_xfer_t));
+ TU_LOG_USBH("USBH init on controller %u\r\n", controller_id);
+ TU_LOG_INT(USBH_DEBUG, sizeof(usbh_device_t));
+ TU_LOG_INT(USBH_DEBUG, sizeof(hcd_event_t));
+ TU_LOG_INT(USBH_DEBUG, sizeof(_ctrl_xfer));
+ TU_LOG_INT(USBH_DEBUG, sizeof(tuh_xfer_t));
+ TU_LOG_INT(USBH_DEBUG, sizeof(tu_fifo_t));
+ TU_LOG_INT(USBH_DEBUG, sizeof(tu_edpt_stream_t));
// Event queue
_usbh_q = osal_queue_create( &_usbh_qdef );
TU_ASSERT(_usbh_q != NULL);
-#if TUSB_OPT_MUTEX
- // Mutex
+#if OSAL_MUTEX_REQUIRED
+ // Init mutex
_usbh_mutex = osal_mutex_create(&_usbh_mutexdef);
TU_ASSERT(_usbh_mutex);
#endif
@@ -371,7 +356,7 @@ bool tuh_init(uint8_t controller_id)
// Class drivers
for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++)
{
- TU_LOG2("%s init\r\n", usbh_class_drivers[drv_id].name);
+ TU_LOG_USBH("%s init\r\n", usbh_class_drivers[drv_id].name);
usbh_class_drivers[drv_id].init();
}
@@ -419,12 +404,12 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr)
case HCD_EVENT_DEVICE_ATTACH:
// TODO due to the shared _usbh_ctrl_buf, we must complete enumerating
// one device before enumerating another one.
- TU_LOG2("[%u:] USBH DEVICE ATTACH\r\n", event.rhport);
+ TU_LOG_USBH("[%u:] USBH DEVICE ATTACH\r\n", event.rhport);
enum_new_device(&event);
break;
case HCD_EVENT_DEVICE_REMOVE:
- TU_LOG2("[%u:%u:%u] USBH DEVICE REMOVED\r\n", event.rhport, event.connection.hub_addr, event.connection.hub_port);
+ TU_LOG_USBH("[%u:%u:%u] USBH DEVICE REMOVED\r\n", event.rhport, event.connection.hub_addr, event.connection.hub_port);
process_device_unplugged(event.rhport, event.connection.hub_addr, event.connection.hub_port);
#if CFG_TUH_HUB
@@ -443,7 +428,7 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr)
uint8_t const epnum = tu_edpt_number(ep_addr);
uint8_t const ep_dir = tu_edpt_dir(ep_addr);
- TU_LOG2("on EP %02X with %u bytes\r\n", ep_addr, (unsigned int) event.xfer_complete.len);
+ TU_LOG_USBH("on EP %02X with %u bytes\r\n", ep_addr, (unsigned int) event.xfer_complete.len);
if (event.dev_addr == 0)
{
@@ -467,7 +452,7 @@ void tuh_task_ext(uint32_t timeout_ms, bool in_isr)
uint8_t drv_id = dev->ep2drv[epnum][ep_dir];
if(drv_id < USBH_CLASS_DRIVER_COUNT)
{
- TU_LOG2("%s xfer callback\r\n", usbh_class_drivers[drv_id].name);
+ TU_LOG_USBH("%s xfer callback\r\n", usbh_class_drivers[drv_id].name);
usbh_class_drivers[drv_id].xfer_cb(event.dev_addr, ep_addr, event.xfer_complete.result, event.xfer_complete.len);
}
else
@@ -537,8 +522,7 @@ bool tuh_control_xfer (tuh_xfer_t* xfer)
uint8_t const daddr = xfer->daddr;
- // TODO probably better to use semaphore as resource management than mutex
- usbh_lock();
+ (void) osal_mutex_lock(_usbh_mutex, OSAL_TIMEOUT_WAIT_FOREVER);
bool const is_idle = (_ctrl_xfer.stage == CONTROL_STAGE_IDLE);
if (is_idle)
@@ -553,14 +537,16 @@ bool tuh_control_xfer (tuh_xfer_t* xfer)
_ctrl_xfer.user_data = xfer->user_data;
}
- usbh_unlock();
+ (void) osal_mutex_unlock(_usbh_mutex);
TU_VERIFY(is_idle);
const uint8_t rhport = usbh_get_rhport(daddr);
- TU_LOG2("[%u:%u] %s: ", rhport, daddr, xfer->setup->bRequest <= TUSB_REQ_SYNCH_FRAME ? tu_str_std_request[xfer->setup->bRequest] : "Unknown Request");
- TU_LOG2_VAR(xfer->setup);
- TU_LOG2("\r\n");
+ TU_LOG_USBH("[%u:%u] %s: ", rhport, daddr,
+ (xfer->setup->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD && xfer->setup->bRequest <= TUSB_REQ_SYNCH_FRAME) ?
+ tu_str_std_request[xfer->setup->bRequest] : "Class Request");
+ TU_LOG_PTR(USBH_DEBUG, xfer->setup);
+ TU_LOG_USBH("\r\n");
if (xfer->complete_cb)
{
@@ -597,14 +583,14 @@ bool tuh_control_xfer (tuh_xfer_t* xfer)
TU_ATTR_ALWAYS_INLINE static inline void _set_control_xfer_stage(uint8_t stage)
{
- usbh_lock();
+ (void) osal_mutex_lock(_usbh_mutex, OSAL_TIMEOUT_WAIT_FOREVER);
_ctrl_xfer.stage = stage;
- usbh_unlock();
+ (void) osal_mutex_unlock(_usbh_mutex);
}
static void _xfer_complete(uint8_t daddr, xfer_result_t result)
{
- TU_LOG2("\r\n");
+ TU_LOG_USBH("\r\n");
// duplicate xfer since user can execute control transfer within callback
tusb_control_request_t const request = _ctrl_xfer.request;
@@ -637,7 +623,11 @@ static bool usbh_control_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result
if (XFER_RESULT_SUCCESS != result)
{
- TU_LOG1("[%u:%u] Control %s\r\n", rhport, dev_addr, result == XFER_RESULT_STALLED ? "STALLED" : "FAILED");
+ TU_LOG1("[%u:%u] Control %s, xferred_bytes = %lu\r\n", rhport, dev_addr, result == XFER_RESULT_STALLED ? "STALLED" : "FAILED", xferred_bytes);
+ #if CFG_TUSB_DEBUG == 1
+ TU_LOG1_PTR(request);
+ TU_LOG1("\r\n");
+ #endif
// terminate transfer if any stage failed
_xfer_complete(dev_addr, result);
@@ -658,8 +648,8 @@ static bool usbh_control_xfer_cb (uint8_t dev_addr, uint8_t ep_addr, xfer_result
case CONTROL_STAGE_DATA:
if (request->wLength)
{
- TU_LOG2("[%u:%u] Control data:\r\n", rhport, dev_addr);
- TU_LOG2_MEM(_ctrl_xfer.buffer, xferred_bytes, 2);
+ TU_LOG_USBH("[%u:%u] Control data:\r\n", rhport, dev_addr);
+ TU_LOG_MEM(USBH_DEBUG, _ctrl_xfer.buffer, xferred_bytes, 2);
}
_ctrl_xfer.actual_len = (uint16_t) xferred_bytes;
@@ -775,7 +765,7 @@ bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t * b
uint8_t const dir = tu_edpt_dir(ep_addr);
tu_edpt_state_t* ep_state = &dev->ep_status[epnum][dir];
- TU_LOG2(" Queue EP %02X with %u bytes ... ", ep_addr, total_bytes);
+ TU_LOG_USBH(" Queue EP %02X with %u bytes ... ", ep_addr, total_bytes);
// Attempt to transfer on a busy endpoint, sound like an race condition !
TU_ASSERT(ep_state->busy == 0);
@@ -791,7 +781,7 @@ bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t * b
if ( hcd_edpt_xfer(dev->rhport, dev_addr, ep_addr, buffer, total_bytes) )
{
- TU_LOG2("OK\r\n");
+ TU_LOG_USBH("OK\r\n");
return true;
}else
{
@@ -806,7 +796,7 @@ bool usbh_edpt_xfer_with_callback(uint8_t dev_addr, uint8_t ep_addr, uint8_t * b
static bool usbh_edpt_control_open(uint8_t dev_addr, uint8_t max_packet_size)
{
- TU_LOG2("[%u:%u] Open EP0 with Size = %u\r\n", usbh_get_rhport(dev_addr), dev_addr, max_packet_size);
+ TU_LOG_USBH("[%u:%u] Open EP0 with Size = %u\r\n", usbh_get_rhport(dev_addr), dev_addr, max_packet_size);
tusb_desc_endpoint_t ep0_desc =
{
@@ -975,7 +965,7 @@ bool tuh_descriptor_get_serial_string(uint8_t daddr, uint16_t language_id, void*
bool tuh_descriptor_get_hid_report(uint8_t daddr, uint8_t itf_num, uint8_t desc_type, uint8_t index, void* buffer, uint16_t len,
tuh_xfer_cb_t complete_cb, uintptr_t user_data)
{
- TU_LOG2("HID Get Report Descriptor\r\n");
+ TU_LOG_USBH("HID Get Report Descriptor\r\n");
tusb_control_request_t const request =
{
.bmRequestType_bit =
@@ -1014,7 +1004,7 @@ bool tuh_descriptor_get_hid_report(uint8_t daddr, uint8_t itf_num, uint8_t desc_
bool tuh_configuration_set(uint8_t daddr, uint8_t config_num,
tuh_xfer_cb_t complete_cb, uintptr_t user_data)
{
- TU_LOG2("Set Configuration = %d\r\n", config_num);
+ TU_LOG_USBH("Set Configuration = %d\r\n", config_num);
tusb_control_request_t const request =
{
@@ -1118,11 +1108,11 @@ static void process_device_unplugged(uint8_t rhport, uint8_t hub_addr, uint8_t h
(hub_port == 0 || dev->hub_port == hub_port) && // hub_port = 0 means all devices of downstream hub
dev->connected)
{
- TU_LOG2(" Address = %u\r\n", dev_addr);
+ TU_LOG_USBH(" Address = %u\r\n", dev_addr);
if (is_hub_addr(dev_addr))
{
- TU_LOG(USBH_DBG_LVL, "HUB address = %u is unmounted\r\n", dev_addr);
+ TU_LOG(USBH_DEBUG, "HUB address = %u is unmounted\r\n", dev_addr);
// If the device itself is a usb hub, unplug downstream devices.
// FIXME un-roll recursive calls to prevent potential stack overflow
process_device_unplugged(rhport, dev_addr, 0);
@@ -1135,7 +1125,7 @@ static void process_device_unplugged(uint8_t rhport, uint8_t hub_addr, uint8_t h
// Close class driver
for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++)
{
- TU_LOG2("%s close\r\n", usbh_class_drivers[drv_id].name);
+ TU_LOG_USBH("%s close\r\n", usbh_class_drivers[drv_id].name);
usbh_class_drivers[drv_id].close(dev_addr);
}
@@ -1260,7 +1250,7 @@ static void process_enumeration(tuh_xfer_t* xfer)
TU_ASSERT( usbh_edpt_control_open(addr0, 8), );
// Get first 8 bytes of device descriptor for Control Endpoint size
- TU_LOG2("Get 8 byte of Device Descriptor\r\n");
+ TU_LOG_USBH("Get 8 byte of Device Descriptor\r\n");
TU_ASSERT(tuh_descriptor_get_device(addr0, _usbh_ctrl_buf, 8, process_enumeration, ENUM_SET_ADDR), );
}
break;
@@ -1269,13 +1259,13 @@ static void process_enumeration(tuh_xfer_t* xfer)
case ENUM_RESET_2:
// TODO not used by now, but may be needed for some devices !?
// Reset device again before Set Address
- TU_LOG2("Port reset2 \r\n");
+ TU_LOG_USBH("Port reset2 \r\n");
if (_dev0.hub_addr == 0)
{
// connected directly to roothub
hcd_port_reset( _dev0.rhport );
osal_task_delay(RESET_DELAY); // TODO may not work for no-OS on MCU that require reset_end() since
- // sof of controller may not running while reseting
+ // sof of controller may not running while resetting
hcd_port_reset_end(_dev0.rhport);
// TODO: fall through to SET ADDRESS, refactor later
}
@@ -1287,7 +1277,7 @@ static void process_enumeration(tuh_xfer_t* xfer)
break;
}
#endif
- __attribute__((fallthrough));
+ TU_ATTR_FALLTHROUGH;
#endif
case ENUM_SET_ADDR:
@@ -1309,7 +1299,7 @@ static void process_enumeration(tuh_xfer_t* xfer)
TU_ASSERT( usbh_edpt_control_open(new_addr, new_dev->ep0_size), );
// Get full device descriptor
- TU_LOG2("Get Device Descriptor\r\n");
+ TU_LOG_USBH("Get Device Descriptor\r\n");
TU_ASSERT(tuh_descriptor_get_device(new_addr, _usbh_ctrl_buf, sizeof(tusb_desc_device_t), process_enumeration, ENUM_GET_9BYTE_CONFIG_DESC), );
}
break;
@@ -1330,7 +1320,7 @@ static void process_enumeration(tuh_xfer_t* xfer)
// Get 9-byte for total length
uint8_t const config_idx = CONFIG_NUM - 1;
- TU_LOG2("Get Configuration[0] Descriptor (9 bytes)\r\n");
+ TU_LOG_USBH("Get Configuration[0] Descriptor (9 bytes)\r\n");
TU_ASSERT( tuh_descriptor_get_configuration(daddr, config_idx, _usbh_ctrl_buf, 9, process_enumeration, ENUM_GET_FULL_CONFIG_DESC), );
}
break;
@@ -1347,7 +1337,7 @@ static void process_enumeration(tuh_xfer_t* xfer)
// Get full configuration descriptor
uint8_t const config_idx = CONFIG_NUM - 1;
- TU_LOG2("Get Configuration[0] Descriptor\r\n");
+ TU_LOG_USBH("Get Configuration[0] Descriptor\r\n");
TU_ASSERT( tuh_descriptor_get_configuration(daddr, config_idx, _usbh_ctrl_buf, total_len, process_enumeration, ENUM_SET_CONFIG), );
}
break;
@@ -1362,7 +1352,7 @@ static void process_enumeration(tuh_xfer_t* xfer)
case ENUM_CONFIG_DRIVER:
{
- TU_LOG2("Device configured\r\n");
+ TU_LOG_USBH("Device configured\r\n");
usbh_device_t* dev = get_device(daddr);
TU_ASSERT(dev, );
@@ -1395,14 +1385,14 @@ static bool enum_new_device(hcd_event_t* event)
// wait until device is stable TODO non blocking
hcd_port_reset(_dev0.rhport);
osal_task_delay(RESET_DELAY); // TODO may not work for no-OS on MCU that require reset_end() since
- // sof of controller may not running while reseting
+ // sof of controller may not running while resetting
hcd_port_reset_end( _dev0.rhport);
// device unplugged while delaying
if ( !hcd_port_connect_status(_dev0.rhport) ) return true;
_dev0.speed = hcd_port_speed_get(_dev0.rhport );
- TU_LOG2("%s Speed\r\n", tu_str_speed[_dev0.speed]);
+ TU_LOG_USBH("%s Speed\r\n", tu_str_speed[_dev0.speed]);
// fake transfer to kick-off the enumeration process
tuh_xfer_t xfer;
@@ -1459,7 +1449,7 @@ static bool enum_request_set_addr(void)
uint8_t const new_addr = get_new_address(desc_device->bDeviceClass == TUSB_CLASS_HUB);
TU_ASSERT(new_addr != 0);
- TU_LOG2("Set Address = %d\r\n", new_addr);
+ TU_LOG_USBH("Set Address = %d\r\n", new_addr);
usbh_device_t* new_dev = get_device(new_addr);
@@ -1503,9 +1493,12 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur
{
usbh_device_t* dev = get_device(dev_addr);
- uint8_t const* desc_end = ((uint8_t const*) desc_cfg) + tu_le16toh(desc_cfg->wTotalLength);
+ uint16_t const total_len = tu_le16toh(desc_cfg->wTotalLength);
+ uint8_t const* desc_end = ((uint8_t const*) desc_cfg) + total_len;
uint8_t const* p_desc = tu_desc_next(desc_cfg);
+ TU_LOG_USBH("Parsing Configuration descriptor (wTotalLength = %u)\r\n", total_len);
+
// parse each interfaces
while( p_desc < desc_end )
{
@@ -1543,15 +1536,14 @@ static bool _parse_configuration_descriptor(uint8_t dev_addr, tusb_desc_configur
TU_ASSERT(drv_len >= sizeof(tusb_desc_interface_t));
// Find driver for this interface
- uint8_t drv_id;
- for (drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++)
+ for (uint8_t drv_id = 0; drv_id < USBH_CLASS_DRIVER_COUNT; drv_id++)
{
usbh_class_driver_t const * driver = &usbh_class_drivers[drv_id];
if ( driver->open(dev->rhport, dev_addr, desc_itf, drv_len) )
{
// open successfully
- TU_LOG2(" %s opened\r\n", driver->name);
+ TU_LOG_USBH(" %s opened\r\n", driver->name);
// bind (associated) interfaces to found driver
for(uint8_t i=0; i= USBH_CLASS_DRIVER_COUNT )
{
- TU_LOG(USBH_DBG_LVL, "Interface %u: class = %u subclass = %u protocol = %u is not supported\r\n",
+ TU_LOG(USBH_DEBUG, "Interface %u: class = %u subclass = %u protocol = %u is not supported\r\n",
desc_itf->bInterfaceNumber, desc_itf->bInterfaceClass, desc_itf->bInterfaceSubClass, desc_itf->bInterfaceProtocol);
}
}
@@ -1590,12 +1582,13 @@ void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num)
for(itf_num++; itf_num < CFG_TUH_INTERFACE_MAX; itf_num++)
{
// continue with next valid interface
- // TODO skip IAD binding interface such as CDCs
+ // IAD binding interface such as CDCs should return itf_num + 1 when complete
+ // with usbh_driver_set_config_complete()
uint8_t const drv_id = dev->itf2drv[itf_num];
if (drv_id != DRVID_INVALID)
{
usbh_class_driver_t const * driver = &usbh_class_drivers[drv_id];
- TU_LOG2("%s set config: itf = %u\r\n", driver->name, itf_num);
+ TU_LOG_USBH("%s set config: itf = %u\r\n", driver->name, itf_num);
driver->set_config(dev_addr, itf_num);
break;
}
@@ -1608,7 +1601,7 @@ void usbh_driver_set_config_complete(uint8_t dev_addr, uint8_t itf_num)
if (is_hub_addr(dev_addr))
{
- TU_LOG(USBH_DBG_LVL, "HUB address = %u is mounted\r\n", dev_addr);
+ TU_LOG(USBH_DEBUG, "HUB address = %u is mounted\r\n", dev_addr);
}else
{
// Invoke callback if available
diff --git a/src/host/usbh_classdriver.h b/src/host/usbh_classdriver.h
index c156afea0..be9811641 100644
--- a/src/host/usbh_classdriver.h
+++ b/src/host/usbh_classdriver.h
@@ -29,11 +29,16 @@
#include "osal/osal.h"
#include "common/tusb_fifo.h"
+#include "common/tusb_private.h"
#ifdef __cplusplus
extern "C" {
#endif
+enum {
+ USBH_EPSIZE_BULK_MAX = (TUH_OPT_HIGH_SPEED ? TUSB_EPSIZE_BULK_HS : TUSB_EPSIZE_BULK_FS)
+};
+
//--------------------------------------------------------------------+
// Class Driver API
//--------------------------------------------------------------------+
diff --git a/src/osal/osal.h b/src/osal/osal.h
index 9d11866df..afa3826fc 100644
--- a/src/osal/osal.h
+++ b/src/osal/osal.h
@@ -33,17 +33,24 @@
#include "common/tusb_common.h"
-// Return immediately
-#define OSAL_TIMEOUT_NOTIMEOUT (0)
-// Default timeout
-#define OSAL_TIMEOUT_NORMAL (10)
-// Wait forever
-#define OSAL_TIMEOUT_WAIT_FOREVER (UINT32_MAX)
-
-#define OSAL_TIMEOUT_CONTROL_XFER OSAL_TIMEOUT_WAIT_FOREVER
-
typedef void (*osal_task_func_t)( void * );
+// Timeout
+#define OSAL_TIMEOUT_NOTIMEOUT (0) // Return immediately
+#define OSAL_TIMEOUT_NORMAL (10) // Default timeout
+#define OSAL_TIMEOUT_WAIT_FOREVER (UINT32_MAX) // Wait forever
+#define OSAL_TIMEOUT_CONTROL_XFER OSAL_TIMEOUT_WAIT_FOREVER
+
+// Mutex is required when using a preempted RTOS or MCU has multiple cores
+#if (CFG_TUSB_OS == OPT_OS_NONE) && !TUP_MCU_MULTIPLE_CORE
+ #define OSAL_MUTEX_REQUIRED 0
+ #define OSAL_MUTEX_DEF(_name) uint8_t :0
+#else
+ #define OSAL_MUTEX_REQUIRED 1
+ #define OSAL_MUTEX_DEF(_name) osal_mutex_def_t _name
+#endif
+
+// OS thin implementation
#if CFG_TUSB_OS == OPT_OS_NONE
#include "osal_none.h"
#elif CFG_TUSB_OS == OPT_OS_FREERTOS
diff --git a/src/osal/osal_freertos.h b/src/osal/osal_freertos.h
index 52db336f5..9393d1f26 100644
--- a/src/osal/osal_freertos.h
+++ b/src/osal/osal_freertos.h
@@ -37,6 +37,43 @@
extern "C" {
#endif
+//--------------------------------------------------------------------+
+// MACRO CONSTANT TYPEDEF PROTYPES
+//--------------------------------------------------------------------+
+
+#if configSUPPORT_STATIC_ALLOCATION
+ typedef StaticSemaphore_t osal_semaphore_def_t;
+ typedef StaticSemaphore_t osal_mutex_def_t;
+#else
+ // not used therefore defined to smallest possible type to save space
+ typedef uint8_t osal_semaphore_def_t;
+ typedef uint8_t osal_mutex_def_t;
+#endif
+
+typedef SemaphoreHandle_t osal_semaphore_t;
+typedef SemaphoreHandle_t osal_mutex_t;
+
+// _int_set is not used with an RTOS
+#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
+ static _type _name##_##buf[_depth];\
+ osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf };
+
+typedef struct
+{
+ uint16_t depth;
+ uint16_t item_sz;
+ void* buf;
+#if configSUPPORT_STATIC_ALLOCATION
+ StaticQueue_t sq;
+#endif
+}osal_queue_def_t;
+
+typedef QueueHandle_t osal_queue_t;
+
+//--------------------------------------------------------------------+
+// TASK API
+//--------------------------------------------------------------------+
+
TU_ATTR_ALWAYS_INLINE static inline uint32_t _osal_ms2tick(uint32_t msec)
{
if (msec == OSAL_TIMEOUT_WAIT_FOREVER) return portMAX_DELAY;
@@ -51,9 +88,6 @@ TU_ATTR_ALWAYS_INLINE static inline uint32_t _osal_ms2tick(uint32_t msec)
return ticks;
}
-//--------------------------------------------------------------------+
-// TASK API
-//--------------------------------------------------------------------+
TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec)
{
vTaskDelay( pdMS_TO_TICKS(msec) );
@@ -62,12 +96,15 @@ TU_ATTR_ALWAYS_INLINE static inline void osal_task_delay(uint32_t msec)
//--------------------------------------------------------------------+
// Semaphore API
//--------------------------------------------------------------------+
-typedef StaticSemaphore_t osal_semaphore_def_t;
-typedef SemaphoreHandle_t osal_semaphore_t;
TU_ATTR_ALWAYS_INLINE static inline osal_semaphore_t osal_semaphore_create(osal_semaphore_def_t* semdef)
{
+#if configSUPPORT_STATIC_ALLOCATION
return xSemaphoreCreateBinaryStatic(semdef);
+#else
+ (void) semdef;
+ return xSemaphoreCreateBinary();
+#endif
}
TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t sem_hdl, bool in_isr)
@@ -78,7 +115,7 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t se
}
else
{
- BaseType_t xHigherPriorityTaskWoken;
+ BaseType_t xHigherPriorityTaskWoken = pdFALSE;
BaseType_t res = xSemaphoreGiveFromISR(sem_hdl, &xHigherPriorityTaskWoken);
#if CFG_TUSB_MCU == OPT_MCU_ESP32S2 || CFG_TUSB_MCU == OPT_MCU_ESP32S3
@@ -92,7 +129,7 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_post(osal_semaphore_t se
}
}
-TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait (osal_semaphore_t sem_hdl, uint32_t msec)
+TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t sem_hdl, uint32_t msec)
{
return xSemaphoreTake(sem_hdl, _osal_ms2tick(msec));
}
@@ -105,15 +142,18 @@ TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t c
//--------------------------------------------------------------------+
// MUTEX API (priority inheritance)
//--------------------------------------------------------------------+
-typedef StaticSemaphore_t osal_mutex_def_t;
-typedef SemaphoreHandle_t osal_mutex_t;
TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
{
+#if configSUPPORT_STATIC_ALLOCATION
return xSemaphoreCreateMutexStatic(mdef);
+#else
+ (void) mdef;
+ return xSemaphoreCreateMutex();
+#endif
}
-TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock (osal_mutex_t mutex_hdl, uint32_t msec)
+TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_lock(osal_mutex_t mutex_hdl, uint32_t msec)
{
return osal_semaphore_wait(mutex_hdl, msec);
}
@@ -127,25 +167,13 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hd
// QUEUE API
//--------------------------------------------------------------------+
-// _int_set is not used with an RTOS
-#define OSAL_QUEUE_DEF(_int_set, _name, _depth, _type) \
- static _type _name##_##buf[_depth];\
- osal_queue_def_t _name = { .depth = _depth, .item_sz = sizeof(_type), .buf = _name##_##buf };
-
-typedef struct
-{
- uint16_t depth;
- uint16_t item_sz;
- void* buf;
-
- StaticQueue_t sq;
-}osal_queue_def_t;
-
-typedef QueueHandle_t osal_queue_t;
-
TU_ATTR_ALWAYS_INLINE static inline osal_queue_t osal_queue_create(osal_queue_def_t* qdef)
{
- return xQueueCreateStatic(qdef->depth, qdef->item_sz, (uint8_t*) qdef->buf, &qdef->sq);
+#if configSUPPORT_STATIC_ALLOCATION
+ return xQueueCreateStatic(qdef->depth, qdef->item_sz, (uint8_t*) qdef->buf, &qdef->sq);
+#else
+ return xQueueCreate(qdef->depth, qdef->item_sz);
+#endif
}
TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_receive(osal_queue_t qhdl, void* data, uint32_t msec)
@@ -161,7 +189,7 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_queue_send(osal_queue_t qhdl, void
}
else
{
- BaseType_t xHigherPriorityTaskWoken;
+ BaseType_t xHigherPriorityTaskWoken = pdFALSE;
BaseType_t res = xQueueSendToBackFromISR(qhdl, data, &xHigherPriorityTaskWoken);
#if CFG_TUSB_MCU == OPT_MCU_ESP32S2 || CFG_TUSB_MCU == OPT_MCU_ESP32S3
diff --git a/src/osal/osal_none.h b/src/osal/osal_none.h
index 9c80e4548..1ad130557 100644
--- a/src/osal/osal_none.h
+++ b/src/osal/osal_none.h
@@ -82,6 +82,10 @@ TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t s
typedef osal_semaphore_def_t osal_mutex_def_t;
typedef osal_semaphore_t osal_mutex_t;
+#if OSAL_MUTEX_REQUIRED
+// Note: multiple cores MCUs usually do provide IPC API for mutex
+// or we can use std atomic function
+
TU_ATTR_ALWAYS_INLINE static inline osal_mutex_t osal_mutex_create(osal_mutex_def_t* mdef)
{
mdef->count = 1;
@@ -98,6 +102,14 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_mutex_unlock(osal_mutex_t mutex_hd
return osal_semaphore_post(mutex_hdl, false);
}
+#else
+
+#define osal_mutex_create(_mdef) (NULL)
+#define osal_mutex_lock(_mutex_hdl, _ms) (true)
+#define osal_mutex_unlock(_mutex_hdl) (true)
+
+#endif
+
//--------------------------------------------------------------------+
// QUEUE API
//--------------------------------------------------------------------+
diff --git a/src/osal/osal_rtthread.h b/src/osal/osal_rtthread.h
index f8452bfb2..18eb9c693 100644
--- a/src/osal/osal_rtthread.h
+++ b/src/osal/osal_rtthread.h
@@ -63,7 +63,7 @@ TU_ATTR_ALWAYS_INLINE static inline bool osal_semaphore_wait(osal_semaphore_t se
}
TU_ATTR_ALWAYS_INLINE static inline void osal_semaphore_reset(osal_semaphore_t const sem_hdl) {
- // TODO: implement
+ rt_sem_control(sem_hdl, RT_IPC_CMD_RESET, 0);
}
//--------------------------------------------------------------------+
diff --git a/src/portable/bridgetek/ft9xx/dcd_ft9xx.c b/src/portable/bridgetek/ft9xx/dcd_ft9xx.c
index fabe7910b..f4c9b2b27 100644
--- a/src/portable/bridgetek/ft9xx/dcd_ft9xx.c
+++ b/src/portable/bridgetek/ft9xx/dcd_ft9xx.c
@@ -595,7 +595,7 @@ void dcd_remote_wakeup(uint8_t rhport)
SYS->MSC0CFG = SYS->MSC0CFG | MASK_SYS_MSC0CFG_DEV_RMWAKEUP;
- // Atleast 2 ms of delay needed for RESUME Data K state.
+ // At least 2 ms of delay needed for RESUME Data K state.
delayms(2);
SYS->MSC0CFG &= ~MASK_SYS_MSC0CFG_DEV_RMWAKEUP;
diff --git a/src/portable/dialog/da146xx/dcd_da146xx.c b/src/portable/dialog/da146xx/dcd_da146xx.c
index 0bb8b6e41..961da81d6 100644
--- a/src/portable/dialog/da146xx/dcd_da146xx.c
+++ b/src/portable/dialog/da146xx/dcd_da146xx.c
@@ -243,7 +243,7 @@ static struct
// Converts xfer pointer to epnum (0,1,2,3) regardless of xfer direction
#define XFER_EPNUM(xfer) ((xfer - &_dcd.xfer_status[0][0]) >> 1)
-// Converts xfer pinter to EPx_REGS pointer (returns same pointer for IN and OUT with same endpoint number)
+// Converts xfer pointer to EPx_REGS pointer (returns same pointer for IN and OUT with same endpoint number)
#define XFER_REGS(xfer) ep_regs[XFER_EPNUM(xfer)]
// Converts epnum (0,1,2,3) to EPx_REGS pointer
#define EPNUM_REGS(epnum) ep_regs[epnum]
diff --git a/src/portable/ehci/ehci.c b/src/portable/ehci/ehci.c
index 76ba2a921..7140897a1 100644
--- a/src/portable/ehci/ehci.c
+++ b/src/portable/ehci/ehci.c
@@ -188,7 +188,7 @@ tusb_speed_t hcd_port_speed_get(uint8_t rhport)
static void list_remove_qhd_by_addr(ehci_link_t* list_head, uint8_t dev_addr)
{
for(ehci_link_t* prev = list_head;
- !prev->terminate && (tu_align32(prev->address) != (uint32_t) list_head);
+ !prev->terminate && (tu_align32(prev->address) != (uint32_t) list_head) && prev != NULL;
prev = list_next(prev) )
{
// TODO check type for ISO iTD and siTD
@@ -199,7 +199,7 @@ static void list_remove_qhd_by_addr(ehci_link_t* list_head, uint8_t dev_addr)
#pragma GCC diagnostic pop
if ( qhd->dev_addr == dev_addr )
{
- // TODO deactive all TD, wait for QHD to inactive before removal
+ // TODO deactivate all TD, wait for QHD to inactive before removal
prev->address = qhd->next.address;
// EHCI 4.8.2 link the removed qhd to async head (which always reachable by Host Controller)
@@ -839,7 +839,7 @@ static void qhd_init(ehci_qhd_t *p_qhd, uint8_t dev_addr, tusb_desc_endpoint_t c
if (TUSB_SPEED_HIGH == p_qhd->ep_speed)
{
TU_ASSERT( interval <= 16, );
- if ( interval < 4) // sub milisecond interval
+ if ( interval < 4) // sub millisecond interval
{
p_qhd->interval_ms = 0;
p_qhd->int_smask = (interval == 1) ? TU_BIN8(11111111) :
diff --git a/src/portable/ehci/ehci.h b/src/portable/ehci/ehci.h
index ff9ae12e7..36f8649be 100644
--- a/src/portable/ehci/ehci.h
+++ b/src/portable/ehci/ehci.h
@@ -114,7 +114,7 @@ typedef struct
volatile uint32_t current_page : 3 ; ///< Index into the qTD buffer pointer list
uint32_t int_on_complete : 1 ; ///< Interrupt on complete
volatile uint32_t total_bytes : 15 ; ///< Transfer bytes, decreased during transaction
- volatile uint32_t data_toggle : 1 ; ///< Data Toogle bit
+ volatile uint32_t data_toggle : 1 ; ///< Data Toggle bit
/// Buffer Page Pointer List, Each element in the list is a 4K page aligned, physical memory address. The lower 12 bits in each pointer are reserved (except for the first one) as each memory pointer must reference the start of a 4K page
@@ -160,7 +160,7 @@ typedef struct TU_ATTR_ALIGNED(32)
uint8_t used;
uint8_t removing; // removed from asyn list, waiting for async advance
uint8_t pid;
- uint8_t interval_ms; // polling interval in frames (or milisecond)
+ uint8_t interval_ms; // polling interval in frames (or millisecond)
uint16_t total_xferred_bytes; // number of bytes xferred until a qtd with ioc bit set
uint8_t reserved2[2];
@@ -225,7 +225,7 @@ typedef struct TU_ATTR_ALIGNED(32)
uint16_t reserved ; ///< reserved
// Word 3: siTD Transfer Status and Control
- // Status [7:0] TODO indentical to qTD Token'status --> refractor later
+ // Status [7:0] TODO identical to qTD Token'status --> refactor later
volatile uint32_t : 1 ; // reserved
volatile uint32_t split_state : 1 ;
volatile uint32_t missed_uframe : 1 ;
@@ -350,8 +350,8 @@ typedef volatile struct
uint32_t periodic_status : 1 ; ///< Periodic schedule status
uint32_t async_status : 1 ; ///< Async schedule status
uint32_t : 2 ;
- uint32_t nxp_int_async : 1 ; ///< NXP customized: This bit is set by the Host Controller when the cause of an interrupt is a completion of a USB transaction where the Transfer Descriptor (TD) has an interrupt on complete (IOC) bit set andthe TD was from the asynchronous schedule. This bit is also set by the Host when a short packet is detected andthe packet is on the asynchronous schedule.
- uint32_t nxp_int_period : 1 ; ///< NXP customized: This bit is set by the Host Controller when the cause of an interrupt is a completion of a USB transaction where the Transfer Descriptor (TD) has an interrupt on complete (IOC) bit set andthe TD was from the periodic schedule.
+ uint32_t nxp_int_async : 1 ; ///< NXP customized: This bit is set by the Host Controller when the cause of an interrupt is a completion of a USB transaction where the Transfer Descriptor (TD) has an interrupt on complete (IOC) bit set and the TD was from the asynchronous schedule. This bit is also set by the Host when a short packet is detected and the packet is on the asynchronous schedule.
+ uint32_t nxp_int_period : 1 ; ///< NXP customized: This bit is set by the Host Controller when the cause of an interrupt is a completion of a USB transaction where the Transfer Descriptor (TD) has an interrupt on complete (IOC) bit set and the TD was from the periodic schedule.
uint32_t : 12 ;
}status_bm;
};
diff --git a/src/portable/espressif/esp32sx/dcd_esp32sx.c b/src/portable/espressif/esp32sx/dcd_esp32sx.c
index 0b75af627..41240f737 100644
--- a/src/portable/espressif/esp32sx/dcd_esp32sx.c
+++ b/src/portable/espressif/esp32sx/dcd_esp32sx.c
@@ -741,7 +741,7 @@ static void handle_epin_ints(void)
// XFER Timeout
if (USB0.in_ep_reg[n].diepint & USB_D_TIMEOUT0_M) {
- // Clear interrupt or enpoint will hang.
+ // Clear interrupt or endpoint will hang.
USB0.in_ep_reg[n].diepint = USB_D_TIMEOUT0_M;
// Maybe retry?
}
diff --git a/src/portable/microchip/pic32mz/usbhs_registers.h b/src/portable/microchip/pic32mz/usbhs_registers.h
index 757e3f083..93b552322 100644
--- a/src/portable/microchip/pic32mz/usbhs_registers.h
+++ b/src/portable/microchip/pic32mz/usbhs_registers.h
@@ -21,16 +21,16 @@
* THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
*******************************************************************************/
/*******************************************************************************
- USBHS Peripheral Library Register Defintions
+ USBHS Peripheral Library Register Definitions
File Name:
usbhs_registers.h
Summary:
- USBHS PLIB Register Defintions
+ USBHS PLIB Register Definitions
Description:
- This file contains the constants and defintions which are required by the
+ This file contains the constants and definitions which are required by the
the USBHS library.
*******************************************************************************/
diff --git a/src/portable/microchip/samx7x/dcd_samx7x.c b/src/portable/microchip/samx7x/dcd_samx7x.c
index 7507c0f69..24657872b 100644
--- a/src/portable/microchip/samx7x/dcd_samx7x.c
+++ b/src/portable/microchip/samx7x/dcd_samx7x.c
@@ -42,7 +42,7 @@
# define USE_SOF 0
#endif
-// Dual bank can imporve performance, but need 2 times bigger packet buffer
+// Dual bank can improve performance, but need 2 times bigger packet buffer
// As SAM7x has only 4KB packet buffer, use with caution !
// Enable in FS mode as packets are smaller
#ifndef USE_DUAL_BANK
@@ -644,7 +644,7 @@ bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t * buffer, uint16_t
}
__set_PRIMASK(irq_state);
- // Here a ZLP has been recieved
+ // Here a ZLP has been received
// and the DMA transfer must be not started.
// It is the end of transfer
return false;
@@ -734,7 +734,7 @@ bool dcd_edpt_xfer_fifo (uint8_t rhport, uint8_t ep_addr, tu_fifo_t * ff, uint16
}
__set_PRIMASK(irq_state);
- // Here a ZLP has been recieved
+ // Here a ZLP has been received
// and the DMA transfer must be not started.
// It is the end of transfer
return false;
diff --git a/src/portable/nordic/nrf5x/dcd_nrf5x.c b/src/portable/nordic/nrf5x/dcd_nrf5x.c
index ca37d799f..afc14b010 100644
--- a/src/portable/nordic/nrf5x/dcd_nrf5x.c
+++ b/src/portable/nordic/nrf5x/dcd_nrf5x.c
@@ -170,7 +170,7 @@ static void xact_out_dma(uint8_t epnum)
uint32_t xact_len;
// DMA can't be active during read of SIZE.EPOUT or SIZE.ISOOUT, so try to lock,
- // If already running deffer call regardless if it was called from ISR or task,
+ // If already running defer call regardless if it was called from ISR or task,
if ( atomic_flag_test_and_set(&_dcd.dma_running) )
{
usbd_defer_func((osal_task_func_t)xact_out_dma_wrapper, (void *)(uint32_t)epnum, is_in_isr());
diff --git a/src/portable/nuvoton/nuc505/dcd_nuc505.c b/src/portable/nuvoton/nuc505/dcd_nuc505.c
index 886720e33..3fa7c1ec1 100644
--- a/src/portable/nuvoton/nuc505/dcd_nuc505.c
+++ b/src/portable/nuvoton/nuc505/dcd_nuc505.c
@@ -181,7 +181,7 @@ static void dcd_userEP_in_xfer(struct xfer_ctl_t *xfer, USBD_EP_T *ep)
ep->EPINTEN = USBD_EPINTEN_TXPKIEN_Msk;
}
- /* provided buffers are thankfully 32-bit aligned, allowing most data to be transfered as 32-bit */
+ /* provided buffers are thankfully 32-bit aligned, allowing most data to be transferred as 32-bit */
#if 0 // TODO support dcd_edpt_xfer_fifo API
if (xfer->ff)
{
diff --git a/src/portable/nxp/lpc_ip3511/dcd_lpc_ip3511.c b/src/portable/nxp/lpc_ip3511/dcd_lpc_ip3511.c
index 9b490c48c..124656307 100644
--- a/src/portable/nxp/lpc_ip3511/dcd_lpc_ip3511.c
+++ b/src/portable/nxp/lpc_ip3511/dcd_lpc_ip3511.c
@@ -139,7 +139,7 @@ typedef union TU_ATTR_PACKED
uint32_t stall : 1 ;
uint32_t disable : 1 ;
uint32_t active : 1 ;
- };
+ } cmd_sts;
}ep_cmd_sts_t;
TU_VERIFY_STATIC( sizeof(ep_cmd_sts_t) == 4, "size is not correct" );
@@ -329,7 +329,7 @@ void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr)
// TODO cannot able to STALL Control OUT endpoint !!!!! FIXME try some walk-around
uint8_t const ep_id = ep_addr2id(ep_addr);
- _dcd.ep[ep_id][0].stall = 1;
+ _dcd.ep[ep_id][0].cmd_sts.stall = 1;
}
void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
@@ -338,9 +338,9 @@ void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
uint8_t const ep_id = ep_addr2id(ep_addr);
- _dcd.ep[ep_id][0].stall = 0;
- _dcd.ep[ep_id][0].toggle_reset = 1;
- _dcd.ep[ep_id][0].toggle_mode = 0;
+ _dcd.ep[ep_id][0].cmd_sts.stall = 0;
+ _dcd.ep[ep_id][0].cmd_sts.toggle_reset = 1;
+ _dcd.ep[ep_id][0].cmd_sts.toggle_mode = 0;
}
bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc)
@@ -349,10 +349,10 @@ bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const * p_endpoint_desc)
uint8_t ep_id = ep_addr2id(p_endpoint_desc->bEndpointAddress);
// Check if endpoint is available
- TU_ASSERT( _dcd.ep[ep_id][0].disable && _dcd.ep[ep_id][1].disable );
+ TU_ASSERT( _dcd.ep[ep_id][0].cmd_sts.disable && _dcd.ep[ep_id][1].cmd_sts.disable );
edpt_reset(rhport, ep_id);
- _dcd.ep[ep_id][0].is_iso = (p_endpoint_desc->bmAttributes.xfer == TUSB_XFER_ISOCHRONOUS);
+ _dcd.ep[ep_id][0].cmd_sts.is_iso = (p_endpoint_desc->bmAttributes.xfer == TUSB_XFER_ISOCHRONOUS);
// Enable EP interrupt
dcd_registers_t* dcd_reg = _dcd_controller[rhport].regs;
@@ -365,8 +365,8 @@ void dcd_edpt_close_all (uint8_t rhport)
{
for (uint8_t ep_id = 0; ep_id < 2*_dcd_controller[rhport].ep_pairs; ++ep_id)
{
- _dcd.ep[ep_id][0].active = _dcd.ep[ep_id][0].active = 0; // TODO proper way is to EPSKIP then wait ep[][].active then write ep[][].disable (see table 778 in LPC55S69 Use Manual)
- _dcd.ep[ep_id][0].disable = _dcd.ep[ep_id][1].disable = 1;
+ _dcd.ep[ep_id][0].cmd_sts.active = _dcd.ep[ep_id][0].cmd_sts.active = 0; // TODO proper way is to EPSKIP then wait ep[][].active then write ep[][].disable (see table 778 in LPC55S69 Use Manual)
+ _dcd.ep[ep_id][0].cmd_sts.disable = _dcd.ep[ep_id][1].cmd_sts.disable = 1;
}
}
@@ -375,8 +375,8 @@ void dcd_edpt_close(uint8_t rhport, uint8_t ep_addr)
(void) rhport;
uint8_t ep_id = ep_addr2id(ep_addr);
- _dcd.ep[ep_id][0].active = _dcd.ep[ep_id][0].active = 0; // TODO proper way is to EPSKIP then wait ep[][].active then write ep[][].disable (see table 778 in LPC55S69 Use Manual)
- _dcd.ep[ep_id][0].disable = _dcd.ep[ep_id][1].disable = 1;
+ _dcd.ep[ep_id][0].cmd_sts.active = _dcd.ep[ep_id][0].cmd_sts.active = 0; // TODO proper way is to EPSKIP then wait ep[][].active then write ep[][].disable (see table 778 in LPC55S69 Use Manual)
+ _dcd.ep[ep_id][0].cmd_sts.disable = _dcd.ep[ep_id][1].cmd_sts.disable = 1;
}
static void prepare_ep_xfer(uint8_t rhport, uint8_t ep_id, uint16_t buf_offset, uint16_t total_bytes)
@@ -385,7 +385,7 @@ static void prepare_ep_xfer(uint8_t rhport, uint8_t ep_id, uint16_t buf_offset,
if (_dcd_controller[rhport].max_speed == TUSB_SPEED_FULL )
{
- nbytes = tu_min16(total_bytes, _dcd.ep[ep_id][0].is_iso ? NBYTES_ISO_FS_MAX : NBYTES_CBI_FS_MAX);
+ nbytes = tu_min16(total_bytes, _dcd.ep[ep_id][0].cmd_sts.is_iso ? NBYTES_ISO_FS_MAX : NBYTES_CBI_FS_MAX);
_dcd.ep[ep_id][0].buffer_fs.offset = buf_offset;
_dcd.ep[ep_id][0].buffer_fs.nbytes = nbytes;
}else
@@ -397,7 +397,7 @@ static void prepare_ep_xfer(uint8_t rhport, uint8_t ep_id, uint16_t buf_offset,
_dcd.dma[ep_id].nbytes = nbytes;
- _dcd.ep[ep_id][0].active = 1;
+ _dcd.ep[ep_id][0].cmd_sts.active = 1;
}
bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes)
@@ -431,7 +431,7 @@ static void bus_reset(uint8_t rhport)
// disable all endpoints as specified by LPC55S69 UM Table 778
for(uint8_t ep_id = 0; ep_id < 2*MAX_EP_PAIRS; ep_id++)
{
- _dcd.ep[ep_id][0].disable = _dcd.ep[ep_id][1].disable = 1;
+ _dcd.ep[ep_id][0].cmd_sts.disable = _dcd.ep[ep_id][1].cmd_sts.disable = 1;
}
dcd_registers_t* dcd_reg = _dcd_controller[rhport].regs;
@@ -459,7 +459,7 @@ static void process_xfer_isr(uint8_t rhport, uint32_t int_status)
if ( ep_id == 0 || ep_id == 1)
{
// For control endpoint, we need to manually clear Active bit
- ep_cs->active = 0;
+ ep_cs->cmd_sts.active = 0;
}
uint16_t buf_offset;
@@ -556,8 +556,8 @@ void dcd_int_handler(uint8_t rhport)
if ( tu_bit_test(int_status, 0) && (cmd_stat & CMDSTAT_SETUP_RECEIVED_MASK) )
{
// Follow UM flowchart to clear Active & Stall on both Control IN/OUT endpoints
- _dcd.ep[0][0].active = _dcd.ep[1][0].active = 0;
- _dcd.ep[0][0].stall = _dcd.ep[1][0].stall = 0;
+ _dcd.ep[0][0].cmd_sts.active = _dcd.ep[1][0].cmd_sts.active = 0;
+ _dcd.ep[0][0].cmd_sts.stall = _dcd.ep[1][0].cmd_sts.stall = 0;
dcd_reg->DEVCMDSTAT |= CMDSTAT_SETUP_RECEIVED_MASK;
diff --git a/src/portable/ohci/ohci.c b/src/portable/ohci/ohci.c
index 3e523ebc2..228da6ae0 100644
--- a/src/portable/ohci/ohci.c
+++ b/src/portable/ohci/ohci.c
@@ -165,7 +165,7 @@ bool hcd_init(uint8_t rhport)
//------------- Data Structure init -------------//
tu_memclr(&ohci_data, sizeof(ohci_data_t));
for(uint8_t i=0; i<32; i++)
- { // assign all interrupt pointes to period head ed
+ { // assign all interrupt pointers to period head ed
ohci_data.hcca.interrupt_table[i] = (uint32_t) &ohci_data.period_head_ed;
}
diff --git a/src/portable/ohci/ohci.h b/src/portable/ohci/ohci.h
index 9fc954c8f..f40ae24cc 100644
--- a/src/portable/ohci/ohci.h
+++ b/src/portable/ohci/ohci.h
@@ -34,7 +34,7 @@
//--------------------------------------------------------------------+
// OHCI CONFIGURATION & CONSTANTS
//--------------------------------------------------------------------+
-#define HOST_HCD_XFER_INTERRUPT // TODO interrupt is used widely, should always be enalbed
+#define HOST_HCD_XFER_INTERRUPT // TODO interrupt is used widely, should always be enabled
#define OHCI_PERIODIC_LIST (defined HOST_HCD_XFER_INTERRUPT || defined HOST_HCD_XFER_ISOCHRONOUS)
// TODO merge OHCI with EHCI
diff --git a/src/portable/raspberrypi/rp2040/hcd_rp2040.c b/src/portable/raspberrypi/rp2040/hcd_rp2040.c
index 10237d1f9..9e6bdad44 100644
--- a/src/portable/raspberrypi/rp2040/hcd_rp2040.c
+++ b/src/portable/raspberrypi/rp2040/hcd_rp2040.c
@@ -81,89 +81,90 @@ static struct hw_endpoint *get_dev_ep(uint8_t dev_addr, uint8_t ep_addr)
TU_ATTR_ALWAYS_INLINE static inline uint8_t dev_speed(void)
{
- return (usb_hw->sie_status & USB_SIE_STATUS_SPEED_BITS) >> USB_SIE_STATUS_SPEED_LSB;
+ return (usb_hw->sie_status & USB_SIE_STATUS_SPEED_BITS) >> USB_SIE_STATUS_SPEED_LSB;
}
-static bool need_pre(uint8_t dev_addr)
+TU_ATTR_ALWAYS_INLINE static inline bool need_pre(uint8_t dev_addr)
{
- // If this device is different to the speed of the root device
- // (i.e. is a low speed device on a full speed hub) then need pre
- return hcd_port_speed_get(0) != tuh_speed_get(dev_addr);
+ // If this device is different to the speed of the root device
+ // (i.e. is a low speed device on a full speed hub) then need pre
+ return hcd_port_speed_get(0) != tuh_speed_get(dev_addr);
}
static void __tusb_irq_path_func(hw_xfer_complete)(struct hw_endpoint *ep, xfer_result_t xfer_result)
{
- // Mark transfer as done before we tell the tinyusb stack
- uint8_t dev_addr = ep->dev_addr;
- uint8_t ep_addr = ep->ep_addr;
- uint xferred_len = ep->xferred_len;
- hw_endpoint_reset_transfer(ep);
- hcd_event_xfer_complete(dev_addr, ep_addr, xferred_len, xfer_result, true);
+ // Mark transfer as done before we tell the tinyusb stack
+ uint8_t dev_addr = ep->dev_addr;
+ uint8_t ep_addr = ep->ep_addr;
+ uint xferred_len = ep->xferred_len;
+ hw_endpoint_reset_transfer(ep);
+ hcd_event_xfer_complete(dev_addr, ep_addr, xferred_len, xfer_result, true);
}
static void __tusb_irq_path_func(_handle_buff_status_bit)(uint bit, struct hw_endpoint *ep)
{
- usb_hw_clear->buf_status = bit;
- // EP may have been stalled?
- assert(ep->active);
- bool done = hw_endpoint_xfer_continue(ep);
- if (done)
- {
- hw_xfer_complete(ep, XFER_RESULT_SUCCESS);
- }
+ usb_hw_clear->buf_status = bit;
+ // EP may have been stalled?
+ assert(ep->active);
+ bool done = hw_endpoint_xfer_continue(ep);
+ if ( done )
+ {
+ hw_xfer_complete(ep, XFER_RESULT_SUCCESS);
+ }
}
static void __tusb_irq_path_func(hw_handle_buff_status)(void)
{
- uint32_t remaining_buffers = usb_hw->buf_status;
- pico_trace("buf_status 0x%08x\n", remaining_buffers);
+ uint32_t remaining_buffers = usb_hw->buf_status;
+ pico_trace("buf_status 0x%08x\n", remaining_buffers);
- // Check EPX first
- uint bit = 0b1;
- if (remaining_buffers & bit)
+ // Check EPX first
+ uint bit = 0b1;
+ if ( remaining_buffers & bit )
+ {
+ remaining_buffers &= ~bit;
+ struct hw_endpoint * ep = &epx;
+
+ uint32_t ep_ctrl = *ep->endpoint_control;
+ if ( ep_ctrl & EP_CTRL_DOUBLE_BUFFERED_BITS )
{
+ TU_LOG(3, "Double Buffered: ");
+ }
+ else
+ {
+ TU_LOG(3, "Single Buffered: ");
+ }
+ TU_LOG_HEX(3, ep_ctrl);
+
+ _handle_buff_status_bit(bit, ep);
+ }
+
+ // Check "interrupt" (asynchronous) endpoints for both IN and OUT
+ for ( uint i = 1; i <= USB_HOST_INTERRUPT_ENDPOINTS && remaining_buffers; i++ )
+ {
+ // EPX is bit 0 & 1
+ // IEP1 IN is bit 2
+ // IEP1 OUT is bit 3
+ // IEP2 IN is bit 4
+ // IEP2 OUT is bit 5
+ // IEP3 IN is bit 6
+ // IEP3 OUT is bit 7
+ // etc
+ for ( uint j = 0; j < 2; j++ )
+ {
+ bit = 1 << (i * 2 + j);
+ if ( remaining_buffers & bit )
+ {
remaining_buffers &= ~bit;
- struct hw_endpoint *ep = &epx;
-
- uint32_t ep_ctrl = *ep->endpoint_control;
- if (ep_ctrl & EP_CTRL_DOUBLE_BUFFERED_BITS)
- {
- TU_LOG(3, "Double Buffered: ");
- }else
- {
- TU_LOG(3, "Single Buffered: ");
- }
- TU_LOG_HEX(3, ep_ctrl);
-
- _handle_buff_status_bit(bit, ep);
+ _handle_buff_status_bit(bit, &ep_pool[i]);
+ }
}
+ }
- // Check "interrupt" (asynchronous) endpoints for both IN and OUT
- for (uint i = 1; i <= USB_HOST_INTERRUPT_ENDPOINTS && remaining_buffers; i++)
- {
- // EPX is bit 0 & 1
- // IEP1 IN is bit 2
- // IEP1 OUT is bit 3
- // IEP2 IN is bit 4
- // IEP2 OUT is bit 5
- // IEP3 IN is bit 6
- // IEP3 OUT is bit 7
- // etc
- for(uint j = 0; j < 2; j++)
- {
- bit = 1 << (i*2+j);
- if (remaining_buffers & bit)
- {
- remaining_buffers &= ~bit;
- _handle_buff_status_bit(bit, &ep_pool[i]);
- }
- }
- }
-
- if (remaining_buffers)
- {
- panic("Unhandled buffer %d\n", remaining_buffers);
- }
+ if ( remaining_buffers )
+ {
+ panic("Unhandled buffer %d\n", remaining_buffers);
+ }
}
static void __tusb_irq_path_func(hw_trans_complete)(void)
@@ -186,70 +187,72 @@ static void __tusb_irq_path_func(hw_trans_complete)(void)
static void __tusb_irq_path_func(hcd_rp2040_irq)(void)
{
- uint32_t status = usb_hw->ints;
- uint32_t handled = 0;
+ uint32_t status = usb_hw->ints;
+ uint32_t handled = 0;
- if (status & USB_INTS_HOST_CONN_DIS_BITS)
+ if ( status & USB_INTS_HOST_CONN_DIS_BITS )
+ {
+ handled |= USB_INTS_HOST_CONN_DIS_BITS;
+
+ if ( dev_speed() )
{
- handled |= USB_INTS_HOST_CONN_DIS_BITS;
-
- if (dev_speed())
- {
- hcd_event_device_attach(RHPORT_NATIVE, true);
- }
- else
- {
- hcd_event_device_remove(RHPORT_NATIVE, true);
- }
-
- // Clear speed change interrupt
- usb_hw_clear->sie_status = USB_SIE_STATUS_SPEED_BITS;
+ hcd_event_device_attach(RHPORT_NATIVE, true);
+ }
+ else
+ {
+ hcd_event_device_remove(RHPORT_NATIVE, true);
}
- if (status & USB_INTS_STALL_BITS)
- {
- // We have rx'd a stall from the device
- // NOTE THIS SHOULD HAVE PRIORITY OVER BUFF_STATUS
- // AND TRANS_COMPLETE as the stall is an alternative response
- // to one of those events
- pico_trace("Stall REC\n");
- handled |= USB_INTS_STALL_BITS;
- usb_hw_clear->sie_status = USB_SIE_STATUS_STALL_REC_BITS;
- hw_xfer_complete(&epx, XFER_RESULT_STALLED);
- }
+ // Clear speed change interrupt
+ usb_hw_clear->sie_status = USB_SIE_STATUS_SPEED_BITS;
+ }
- if (status & USB_INTS_BUFF_STATUS_BITS)
- {
- handled |= USB_INTS_BUFF_STATUS_BITS;
- TU_LOG(2, "Buffer complete\n");
- hw_handle_buff_status();
- }
+ if ( status & USB_INTS_STALL_BITS )
+ {
+ // We have rx'd a stall from the device
+ // NOTE THIS SHOULD HAVE PRIORITY OVER BUFF_STATUS
+ // AND TRANS_COMPLETE as the stall is an alternative response
+ // to one of those events
+ pico_trace("Stall REC\n");
+ handled |= USB_INTS_STALL_BITS;
+ usb_hw_clear->sie_status = USB_SIE_STATUS_STALL_REC_BITS;
+ hw_xfer_complete(&epx, XFER_RESULT_STALLED);
+ }
- if (status & USB_INTS_TRANS_COMPLETE_BITS)
- {
- handled |= USB_INTS_TRANS_COMPLETE_BITS;
- usb_hw_clear->sie_status = USB_SIE_STATUS_TRANS_COMPLETE_BITS;
- TU_LOG(2, "Transfer complete\n");
- hw_trans_complete();
- }
+ if ( status & USB_INTS_BUFF_STATUS_BITS )
+ {
+ handled |= USB_INTS_BUFF_STATUS_BITS;
+ TU_LOG(2, "Buffer complete\n");
+ hw_handle_buff_status();
+ }
- if (status & USB_INTS_ERROR_RX_TIMEOUT_BITS)
- {
- handled |= USB_INTS_ERROR_RX_TIMEOUT_BITS;
- usb_hw_clear->sie_status = USB_SIE_STATUS_RX_TIMEOUT_BITS;
- }
+ if ( status & USB_INTS_TRANS_COMPLETE_BITS )
+ {
+ handled |= USB_INTS_TRANS_COMPLETE_BITS;
+ usb_hw_clear->sie_status = USB_SIE_STATUS_TRANS_COMPLETE_BITS;
+ TU_LOG(2, "Transfer complete\n");
+ hw_trans_complete();
+ }
- if (status & USB_INTS_ERROR_DATA_SEQ_BITS)
- {
- usb_hw_clear->sie_status = USB_SIE_STATUS_DATA_SEQ_ERROR_BITS;
- TU_LOG(3, " Seq Error: [0] = 0x%04u [1] = 0x%04x\r\n", tu_u32_low16(*epx.buffer_control), tu_u32_high16(*epx.buffer_control));
- panic("Data Seq Error \n");
- }
+ if ( status & USB_INTS_ERROR_RX_TIMEOUT_BITS )
+ {
+ handled |= USB_INTS_ERROR_RX_TIMEOUT_BITS;
+ usb_hw_clear->sie_status = USB_SIE_STATUS_RX_TIMEOUT_BITS;
+ }
- if (status ^ handled)
- {
- panic("Unhandled IRQ 0x%x\n", (uint) (status ^ handled));
- }
+ if ( status & USB_INTS_ERROR_DATA_SEQ_BITS )
+ {
+ usb_hw_clear->sie_status = USB_SIE_STATUS_DATA_SEQ_ERROR_BITS;
+ TU_LOG(3, " Seq Error: [0] = 0x%04u [1] = 0x%04x\r\n",
+ tu_u32_low16(*epx.buffer_control),
+ tu_u32_high16(*epx.buffer_control));
+ panic("Data Seq Error \n");
+ }
+
+ if ( status ^ handled )
+ {
+ panic("Unhandled IRQ 0x%x\n", (uint) (status ^ handled));
+ }
}
void __tusb_irq_path_func(hcd_int_handler)(uint8_t rhport)
@@ -260,116 +263,118 @@ void __tusb_irq_path_func(hcd_int_handler)(uint8_t rhport)
static struct hw_endpoint *_next_free_interrupt_ep(void)
{
- struct hw_endpoint *ep = NULL;
- for (uint i = 1; i < TU_ARRAY_SIZE(ep_pool); i++)
+ struct hw_endpoint * ep = NULL;
+ for ( uint i = 1; i < TU_ARRAY_SIZE(ep_pool); i++ )
+ {
+ ep = &ep_pool[i];
+ if ( !ep->configured )
{
- ep = &ep_pool[i];
- if (!ep->configured)
- {
- // Will be configured by _hw_endpoint_init / _hw_endpoint_allocate
- ep->interrupt_num = (uint8_t) (i - 1);
- return ep;
- }
+ // Will be configured by _hw_endpoint_init / _hw_endpoint_allocate
+ ep->interrupt_num = (uint8_t) (i - 1);
+ return ep;
}
- return ep;
+ }
+ return ep;
}
static struct hw_endpoint *_hw_endpoint_allocate(uint8_t transfer_type)
{
- struct hw_endpoint *ep = NULL;
+ struct hw_endpoint * ep = NULL;
- if (transfer_type != TUSB_XFER_CONTROL)
- {
- // Note: even though datasheet name these "Interrupt" endpoints. These are actually
- // "Asynchronous" endpoints and can be used for other type such as: Bulk (ISO need confirmation)
- ep = _next_free_interrupt_ep();
- pico_info("Allocate %s ep %d\n", tu_edpt_type_str(transfer_type), ep->interrupt_num);
- assert(ep);
- ep->buffer_control = &usbh_dpram->int_ep_buffer_ctrl[ep->interrupt_num].ctrl;
- ep->endpoint_control = &usbh_dpram->int_ep_ctrl[ep->interrupt_num].ctrl;
- // 0 for epx (double buffered): TODO increase to 1024 for ISO
- // 2x64 for intep0
- // 3x64 for intep1
- // etc
- ep->hw_data_buf = &usbh_dpram->epx_data[64 * (ep->interrupt_num + 2)];
- }
- else
- {
- ep = &epx;
- ep->buffer_control = &usbh_dpram->epx_buf_ctrl;
- ep->endpoint_control = &usbh_dpram->epx_ctrl;
- ep->hw_data_buf = &usbh_dpram->epx_data[0];
- }
+ if ( transfer_type != TUSB_XFER_CONTROL )
+ {
+ // Note: even though datasheet name these "Interrupt" endpoints. These are actually
+ // "Asynchronous" endpoints and can be used for other type such as: Bulk (ISO need confirmation)
+ ep = _next_free_interrupt_ep();
+ pico_info("Allocate %s ep %d\n", tu_edpt_type_str(transfer_type), ep->interrupt_num);
+ assert(ep);
+ ep->buffer_control = &usbh_dpram->int_ep_buffer_ctrl[ep->interrupt_num].ctrl;
+ ep->endpoint_control = &usbh_dpram->int_ep_ctrl[ep->interrupt_num].ctrl;
+ // 0 for epx (double buffered): TODO increase to 1024 for ISO
+ // 2x64 for intep0
+ // 3x64 for intep1
+ // etc
+ ep->hw_data_buf = &usbh_dpram->epx_data[64 * (ep->interrupt_num + 2)];
+ }
+ else
+ {
+ ep = &epx;
+ ep->buffer_control = &usbh_dpram->epx_buf_ctrl;
+ ep->endpoint_control = &usbh_dpram->epx_ctrl;
+ ep->hw_data_buf = &usbh_dpram->epx_data[0];
+ }
- return ep;
+ return ep;
}
static void _hw_endpoint_init(struct hw_endpoint *ep, uint8_t dev_addr, uint8_t ep_addr, uint16_t wMaxPacketSize, uint8_t transfer_type, uint8_t bmInterval)
{
- // Already has data buffer, endpoint control, and buffer control allocated at this point
- assert(ep->endpoint_control);
- assert(ep->buffer_control);
- assert(ep->hw_data_buf);
+ // Already has data buffer, endpoint control, and buffer control allocated at this point
+ assert(ep->endpoint_control);
+ assert(ep->buffer_control);
+ assert(ep->hw_data_buf);
- uint8_t const num = tu_edpt_number(ep_addr);
- tusb_dir_t const dir = tu_edpt_dir(ep_addr);
+ uint8_t const num = tu_edpt_number(ep_addr);
+ tusb_dir_t const dir = tu_edpt_dir(ep_addr);
- ep->ep_addr = ep_addr;
- ep->dev_addr = dev_addr;
+ ep->ep_addr = ep_addr;
+ ep->dev_addr = dev_addr;
- // For host, IN to host == RX, anything else rx == false
- ep->rx = (dir == TUSB_DIR_IN);
+ // For host, IN to host == RX, anything else rx == false
+ ep->rx = (dir == TUSB_DIR_IN);
- // Response to a setup packet on EP0 starts with pid of 1
- ep->next_pid = (num == 0 ? 1u : 0u);
- ep->wMaxPacketSize = wMaxPacketSize;
- ep->transfer_type = transfer_type;
+ // Response to a setup packet on EP0 starts with pid of 1
+ ep->next_pid = (num == 0 ? 1u : 0u);
+ ep->wMaxPacketSize = wMaxPacketSize;
+ ep->transfer_type = transfer_type;
- pico_trace("hw_endpoint_init dev %d ep %d %s xfer %d\n", ep->dev_addr, tu_edpt_number(ep->ep_addr), ep_dir_string[tu_edpt_dir(ep->ep_addr)], ep->transfer_type);
- pico_trace("dev %d ep %d %s setup buffer @ 0x%p\n", ep->dev_addr, tu_edpt_number(ep->ep_addr), ep_dir_string[tu_edpt_dir(ep->ep_addr)], ep->hw_data_buf);
- uint dpram_offset = hw_data_offset(ep->hw_data_buf);
- // Bits 0-5 should be 0
- assert(!(dpram_offset & 0b111111));
+ pico_trace("hw_endpoint_init dev %d ep %d %s xfer %d\n", ep->dev_addr, tu_edpt_number(ep->ep_addr),
+ ep_dir_string[tu_edpt_dir(ep->ep_addr)], ep->transfer_type);
+ pico_trace("dev %d ep %d %s setup buffer @ 0x%p\n", ep->dev_addr, tu_edpt_number(ep->ep_addr),
+ ep_dir_string[tu_edpt_dir(ep->ep_addr)], ep->hw_data_buf);
+ uint dpram_offset = hw_data_offset(ep->hw_data_buf);
+ // Bits 0-5 should be 0
+ assert(!(dpram_offset & 0b111111));
- // Fill in endpoint control register with buffer offset
- uint32_t ep_reg = EP_CTRL_ENABLE_BITS
- | EP_CTRL_INTERRUPT_PER_BUFFER
- | (ep->transfer_type << EP_CTRL_BUFFER_TYPE_LSB)
- | dpram_offset;
- if (bmInterval)
+ // Fill in endpoint control register with buffer offset
+ uint32_t ep_reg = EP_CTRL_ENABLE_BITS
+ | EP_CTRL_INTERRUPT_PER_BUFFER
+ | (ep->transfer_type << EP_CTRL_BUFFER_TYPE_LSB)
+ | dpram_offset;
+ if ( bmInterval )
+ {
+ ep_reg |= (uint32_t) ((bmInterval - 1) << EP_CTRL_HOST_INTERRUPT_INTERVAL_LSB);
+ }
+ *ep->endpoint_control = ep_reg;
+ pico_trace("endpoint control (0x%p) <- 0x%x\n", ep->endpoint_control, ep_reg);
+ ep->configured = true;
+
+ if ( ep != &epx )
+ {
+ // Endpoint has its own addr_endp and interrupt bits to be setup!
+ // This is an interrupt/async endpoint. so need to set up ADDR_ENDP register with:
+ // - device address
+ // - endpoint number / direction
+ // - preamble
+ uint32_t reg = (uint32_t) (dev_addr | (num << USB_ADDR_ENDP1_ENDPOINT_LSB));
+
+ if ( dir == TUSB_DIR_OUT )
{
- ep_reg |= (uint32_t) ((bmInterval - 1) << EP_CTRL_HOST_INTERRUPT_INTERVAL_LSB);
+ reg |= USB_ADDR_ENDP1_INTEP_DIR_BITS;
}
- *ep->endpoint_control = ep_reg;
- pico_trace("endpoint control (0x%p) <- 0x%x\n", ep->endpoint_control, ep_reg);
- ep->configured = true;
- if (ep != &epx)
+ if ( need_pre(dev_addr) )
{
- // Endpoint has its own addr_endp and interrupt bits to be setup!
- // This is an interrupt/async endpoint. so need to set up ADDR_ENDP register with:
- // - device address
- // - endpoint number / direction
- // - preamble
- uint32_t reg = (uint32_t) (dev_addr | (num << USB_ADDR_ENDP1_ENDPOINT_LSB));
-
- if (dir == TUSB_DIR_OUT)
- {
- reg |= USB_ADDR_ENDP1_INTEP_DIR_BITS;
- }
-
- if (need_pre(dev_addr))
- {
- reg |= USB_ADDR_ENDP1_INTEP_PREAMBLE_BITS;
- }
- usb_hw->int_ep_addr_ctrl[ep->interrupt_num] = reg;
-
- // Finally, enable interrupt that endpoint
- usb_hw_set->int_ep_ctrl = 1 << (ep->interrupt_num + 1);
-
- // If it's an interrupt endpoint we need to set up the buffer control
- // register
+ reg |= USB_ADDR_ENDP1_INTEP_PREAMBLE_BITS;
}
+ usb_hw->int_ep_addr_ctrl[ep->interrupt_num] = reg;
+
+ // Finally, enable interrupt that endpoint
+ usb_hw_set->int_ep_ctrl = 1 << (ep->interrupt_num + 1);
+
+ // If it's an interrupt endpoint we need to set up the buffer control
+ // register
+ }
}
//--------------------------------------------------------------------+
@@ -434,16 +439,17 @@ tusb_speed_t hcd_port_speed_get(uint8_t rhport)
{
(void) rhport;
assert(rhport == 0);
+
// TODO: Should enumval this register
- switch (dev_speed())
+ switch ( dev_speed() )
{
- case 1:
- return TUSB_SPEED_LOW;
- case 2:
- return TUSB_SPEED_FULL;
- default:
- panic("Invalid speed\n");
- return TUSB_SPEED_INVALID;
+ case 1:
+ return TUSB_SPEED_LOW;
+ case 2:
+ return TUSB_SPEED_FULL;
+ default:
+ panic("Invalid speed\n");
+ return TUSB_SPEED_INVALID;
}
}
@@ -476,8 +482,8 @@ void hcd_device_close(uint8_t rhport, uint8_t dev_addr)
uint32_t hcd_frame_number(uint8_t rhport)
{
- (void) rhport;
- return usb_hw->sof_rd;
+ (void) rhport;
+ return usb_hw->sof_rd;
}
void hcd_int_enable(uint8_t rhport)
@@ -501,117 +507,116 @@ void hcd_int_disable(uint8_t rhport)
bool hcd_edpt_open(uint8_t rhport, uint8_t dev_addr, tusb_desc_endpoint_t const * ep_desc)
{
- (void) rhport;
+ (void) rhport;
- pico_trace("hcd_edpt_open dev_addr %d, ep_addr %d\n", dev_addr, ep_desc->bEndpointAddress);
+ pico_trace("hcd_edpt_open dev_addr %d, ep_addr %d\n", dev_addr, ep_desc->bEndpointAddress);
- // Allocated differently based on if it's an interrupt endpoint or not
- struct hw_endpoint *ep = _hw_endpoint_allocate(ep_desc->bmAttributes.xfer);
- TU_ASSERT(ep);
+ // Allocated differently based on if it's an interrupt endpoint or not
+ struct hw_endpoint *ep = _hw_endpoint_allocate(ep_desc->bmAttributes.xfer);
+ TU_ASSERT(ep);
- _hw_endpoint_init(ep,
- dev_addr,
- ep_desc->bEndpointAddress,
- tu_edpt_packet_size(ep_desc),
- ep_desc->bmAttributes.xfer,
- ep_desc->bInterval);
+ _hw_endpoint_init(ep,
+ dev_addr,
+ ep_desc->bEndpointAddress,
+ tu_edpt_packet_size(ep_desc),
+ ep_desc->bmAttributes.xfer,
+ ep_desc->bInterval);
- return true;
+ return true;
}
bool hcd_edpt_xfer(uint8_t rhport, uint8_t dev_addr, uint8_t ep_addr, uint8_t * buffer, uint16_t buflen)
{
- (void) rhport;
+ (void) rhport;
- pico_trace("hcd_edpt_xfer dev_addr %d, ep_addr 0x%x, len %d\n", dev_addr, ep_addr, buflen);
-
- uint8_t const ep_num = tu_edpt_number(ep_addr);
- tusb_dir_t const ep_dir = tu_edpt_dir(ep_addr);
+ pico_trace("hcd_edpt_xfer dev_addr %d, ep_addr 0x%x, len %d\n", dev_addr, ep_addr, buflen);
- // Get appropriate ep. Either EPX or interrupt endpoint
- struct hw_endpoint *ep = get_dev_ep(dev_addr, ep_addr);
+ uint8_t const ep_num = tu_edpt_number(ep_addr);
+ tusb_dir_t const ep_dir = tu_edpt_dir(ep_addr);
- TU_ASSERT(ep);
+ // Get appropriate ep. Either EPX or interrupt endpoint
+ struct hw_endpoint *ep = get_dev_ep(dev_addr, ep_addr);
- // EP should be inactive
- assert(!ep->active);
+ TU_ASSERT(ep);
- // Control endpoint can change direction 0x00 <-> 0x80
- if ( ep_addr != ep->ep_addr )
- {
- assert(ep_num == 0);
+ // EP should be inactive
+ assert(!ep->active);
- // Direction has flipped on endpoint control so re init it but with same properties
- _hw_endpoint_init(ep, dev_addr, ep_addr, ep->wMaxPacketSize, ep->transfer_type, 0);
- }
+ // Control endpoint can change direction 0x00 <-> 0x80
+ if ( ep_addr != ep->ep_addr )
+ {
+ assert(ep_num == 0);
- // If a normal transfer (non-interrupt) then initiate using
- // sie ctrl registers. Otherwise interrupt ep registers should
- // already be configured
- if (ep == &epx) {
- hw_endpoint_xfer_start(ep, buffer, buflen);
+ // Direction has flipped on endpoint control so re init it but with same properties
+ _hw_endpoint_init(ep, dev_addr, ep_addr, ep->wMaxPacketSize, ep->transfer_type, 0);
+ }
- // That has set up buffer control, endpoint control etc
- // for host we have to initiate the transfer
- usb_hw->dev_addr_ctrl = (uint32_t) (dev_addr | (ep_num << USB_ADDR_ENDP_ENDPOINT_LSB));
+ // If a normal transfer (non-interrupt) then initiate using
+ // sie ctrl registers. Otherwise interrupt ep registers should
+ // already be configured
+ if ( ep == &epx )
+ {
+ hw_endpoint_xfer_start(ep, buffer, buflen);
- uint32_t flags = USB_SIE_CTRL_START_TRANS_BITS | SIE_CTRL_BASE |
- (ep_dir ? USB_SIE_CTRL_RECEIVE_DATA_BITS : USB_SIE_CTRL_SEND_DATA_BITS);
- // Set pre if we are a low speed device on full speed hub
- flags |= need_pre(dev_addr) ? USB_SIE_CTRL_PREAMBLE_EN_BITS : 0;
+ // That has set up buffer control, endpoint control etc
+ // for host we have to initiate the transfer
+ usb_hw->dev_addr_ctrl = (uint32_t) (dev_addr | (ep_num << USB_ADDR_ENDP_ENDPOINT_LSB));
- usb_hw->sie_ctrl = flags;
- }else
- {
- hw_endpoint_xfer_start(ep, buffer, buflen);
- }
+ uint32_t flags = USB_SIE_CTRL_START_TRANS_BITS | SIE_CTRL_BASE |
+ (ep_dir ? USB_SIE_CTRL_RECEIVE_DATA_BITS : USB_SIE_CTRL_SEND_DATA_BITS) |
+ (need_pre(dev_addr) ? USB_SIE_CTRL_PREAMBLE_EN_BITS : 0);
+ usb_hw->sie_ctrl = flags;
+ }else
+ {
+ hw_endpoint_xfer_start(ep, buffer, buflen);
+ }
- return true;
+ return true;
}
bool hcd_setup_send(uint8_t rhport, uint8_t dev_addr, uint8_t const setup_packet[8])
{
- (void) rhport;
+ (void) rhport;
- // Copy data into setup packet buffer
- for(uint8_t i=0; i<8; i++)
- {
- usbh_dpram->setup_packet[i] = setup_packet[i];
- }
+ // Copy data into setup packet buffer
+ for ( uint8_t i = 0; i < 8; i++ )
+ {
+ usbh_dpram->setup_packet[i] = setup_packet[i];
+ }
- // Configure EP0 struct with setup info for the trans complete
- struct hw_endpoint *ep = _hw_endpoint_allocate(0);
- TU_ASSERT(ep);
+ // Configure EP0 struct with setup info for the trans complete
+ struct hw_endpoint * ep = _hw_endpoint_allocate(0);
+ TU_ASSERT(ep);
- // EPX should be inactive
- assert(!ep->active);
+ // EPX should be inactive
+ assert(!ep->active);
- // EP0 out
- _hw_endpoint_init(ep, dev_addr, 0x00, ep->wMaxPacketSize, 0, 0);
- assert(ep->configured);
+ // EP0 out
+ _hw_endpoint_init(ep, dev_addr, 0x00, ep->wMaxPacketSize, 0, 0);
+ assert(ep->configured);
- ep->remaining_len = 8;
- ep->active = true;
+ ep->remaining_len = 8;
+ ep->active = true;
- // Set device address
- usb_hw->dev_addr_ctrl = dev_addr;
+ // Set device address
+ usb_hw->dev_addr_ctrl = dev_addr;
- // Set pre if we are a low speed device on full speed hub
- uint32_t const flags = SIE_CTRL_BASE | USB_SIE_CTRL_SEND_SETUP_BITS | USB_SIE_CTRL_START_TRANS_BITS |
- (need_pre(dev_addr) ? USB_SIE_CTRL_PREAMBLE_EN_BITS : 0);
+ // Set pre if we are a low speed device on full speed hub
+ uint32_t const flags = SIE_CTRL_BASE | USB_SIE_CTRL_SEND_SETUP_BITS | USB_SIE_CTRL_START_TRANS_BITS |
+ (need_pre(dev_addr) ? USB_SIE_CTRL_PREAMBLE_EN_BITS : 0);
- usb_hw->sie_ctrl = flags;
+ usb_hw->sie_ctrl = flags;
- return true;
+ return true;
}
bool hcd_edpt_clear_stall(uint8_t dev_addr, uint8_t ep_addr)
{
- (void) dev_addr;
- (void) ep_addr;
+ (void) dev_addr;
+ (void) ep_addr;
- panic("hcd_clear_stall");
- return true;
+ panic("hcd_clear_stall");
+ return true;
}
#endif
diff --git a/src/portable/raspberrypi/rp2040/rp2040_usb.c b/src/portable/raspberrypi/rp2040/rp2040_usb.c
index d7895175e..5c7645902 100644
--- a/src/portable/raspberrypi/rp2040/rp2040_usb.c
+++ b/src/portable/raspberrypi/rp2040/rp2040_usb.c
@@ -47,6 +47,12 @@ TU_ATTR_ALWAYS_INLINE static inline void _hw_endpoint_lock_update(__unused struc
static void _hw_endpoint_xfer_sync(struct hw_endpoint *ep);
static void _hw_endpoint_start_next_buffer(struct hw_endpoint *ep);
+// if usb hardware is in host mode
+TU_ATTR_ALWAYS_INLINE static inline bool is_host_mode(void)
+{
+ return (usb_hw->main_ctrl & USB_MAIN_CTRL_HOST_NDEVICE_BITS) ? true : false;
+}
+
//--------------------------------------------------------------------+
//
//--------------------------------------------------------------------+
@@ -69,6 +75,8 @@ void rp2040_usb_init(void)
// Mux the controller to the onboard usb phy
usb_hw->muxing = USB_USB_MUXING_TO_PHY_BITS | USB_USB_MUXING_SOFTCON_BITS;
+
+ TU_LOG2_INT(sizeof(hw_endpoint_t));
}
void __tusb_irq_path_func(hw_endpoint_reset_transfer)(struct hw_endpoint *ep)
@@ -80,19 +88,23 @@ void __tusb_irq_path_func(hw_endpoint_reset_transfer)(struct hw_endpoint *ep)
}
void __tusb_irq_path_func(_hw_endpoint_buffer_control_update32)(struct hw_endpoint *ep, uint32_t and_mask, uint32_t or_mask) {
- uint32_t value = 0;
- if (and_mask) {
- value = *ep->buffer_control & and_mask;
- }
- if (or_mask) {
- value |= or_mask;
- if (or_mask & USB_BUF_CTRL_AVAIL) {
- if (*ep->buffer_control & USB_BUF_CTRL_AVAIL) {
- panic("ep %d %s was already available", tu_edpt_number(ep->ep_addr), ep_dir_string[tu_edpt_dir(ep->ep_addr)]);
- }
- *ep->buffer_control = value & ~USB_BUF_CTRL_AVAIL;
- // 12 cycle delay.. (should be good for 48*12Mhz = 576Mhz)
- // Don't need delay in host mode as host is in charge
+ uint32_t value = 0;
+ if ( and_mask )
+ {
+ value = *ep->buffer_control & and_mask;
+ }
+ if ( or_mask )
+ {
+ value |= or_mask;
+ if ( or_mask & USB_BUF_CTRL_AVAIL )
+ {
+ if ( *ep->buffer_control & USB_BUF_CTRL_AVAIL )
+ {
+ panic("ep %d %s was already available", tu_edpt_number(ep->ep_addr), ep_dir_string[tu_edpt_dir(ep->ep_addr)]);
+ }
+ *ep->buffer_control = value & ~USB_BUF_CTRL_AVAIL;
+ // 12 cycle delay.. (should be good for 48*12Mhz = 576Mhz)
+ // Don't need delay in host mode as host is in charge
#if !CFG_TUH_ENABLED
__asm volatile (
"b 1f\n"
@@ -104,9 +116,9 @@ void __tusb_irq_path_func(_hw_endpoint_buffer_control_update32)(struct hw_endpoi
"1:\n"
: : : "memory");
#endif
- }
}
- *ep->buffer_control = value;
+ }
+ *ep->buffer_control = value;
}
// prepare buffer, return buffer control
@@ -152,12 +164,14 @@ static void __tusb_irq_path_func(_hw_endpoint_start_next_buffer)(struct hw_endpo
// always compute and start with buffer 0
uint32_t buf_ctrl = prepare_ep_buffer(ep, 0) | USB_BUF_CTRL_SEL;
- // For now: skip double buffered for Device mode, OUT endpoint since
+ // For now: skip double buffered for OUT endpoint in Device mode, since
// host could send < 64 bytes and cause short packet on buffer0
- // NOTE this could happen to Host mode IN endpoint
- // Also, Host mode interrupt endpoint hardware is only single buffered
- bool const force_single = (!(usb_hw->main_ctrl & USB_MAIN_CTRL_HOST_NDEVICE_BITS) && !tu_edpt_dir(ep->ep_addr)) ||
- ((usb_hw->main_ctrl & USB_MAIN_CTRL_HOST_NDEVICE_BITS) && tu_edpt_number(ep->ep_addr) != 0);
+ // NOTE: this could happen to Host mode IN endpoint
+ // Also, Host mode "interrupt" endpoint hardware is only single buffered,
+ // NOTE2: Currently Host bulk is implemented using "interrupt" endpoint
+ bool const is_host = is_host_mode();
+ bool const force_single = (!is_host && !tu_edpt_dir(ep->ep_addr)) ||
+ (is_host && tu_edpt_number(ep->ep_addr) != 0);
if(ep->remaining_len && !force_single)
{
diff --git a/src/portable/raspberrypi/rp2040/rp2040_usb.h b/src/portable/raspberrypi/rp2040/rp2040_usb.h
index c72dae64c..b65d32fd4 100644
--- a/src/portable/raspberrypi/rp2040/rp2040_usb.h
+++ b/src/portable/raspberrypi/rp2040/rp2040_usb.h
@@ -82,26 +82,30 @@ void hw_endpoint_reset_transfer(struct hw_endpoint *ep);
void _hw_endpoint_buffer_control_update32(struct hw_endpoint *ep, uint32_t and_mask, uint32_t or_mask);
-TU_ATTR_ALWAYS_INLINE static inline uint32_t _hw_endpoint_buffer_control_get_value32(struct hw_endpoint *ep) {
- return *ep->buffer_control;
-}
-
-TU_ATTR_ALWAYS_INLINE static inline void _hw_endpoint_buffer_control_set_value32(struct hw_endpoint *ep, uint32_t value) {
- return _hw_endpoint_buffer_control_update32(ep, 0, value);
-}
-
-TU_ATTR_ALWAYS_INLINE static inline void _hw_endpoint_buffer_control_set_mask32(struct hw_endpoint *ep, uint32_t value) {
- return _hw_endpoint_buffer_control_update32(ep, ~value, value);
-}
-
-TU_ATTR_ALWAYS_INLINE static inline void _hw_endpoint_buffer_control_clear_mask32(struct hw_endpoint *ep, uint32_t value) {
- return _hw_endpoint_buffer_control_update32(ep, ~value, 0);
-}
-
-static inline uintptr_t hw_data_offset(uint8_t *buf)
+TU_ATTR_ALWAYS_INLINE static inline uint32_t _hw_endpoint_buffer_control_get_value32 (struct hw_endpoint *ep)
{
- // Remove usb base from buffer pointer
- return (uintptr_t)buf ^ (uintptr_t)usb_dpram;
+ return *ep->buffer_control;
+}
+
+TU_ATTR_ALWAYS_INLINE static inline void _hw_endpoint_buffer_control_set_value32 (struct hw_endpoint *ep, uint32_t value)
+{
+ return _hw_endpoint_buffer_control_update32(ep, 0, value);
+}
+
+TU_ATTR_ALWAYS_INLINE static inline void _hw_endpoint_buffer_control_set_mask32 (struct hw_endpoint *ep, uint32_t value)
+{
+ return _hw_endpoint_buffer_control_update32(ep, ~value, value);
+}
+
+TU_ATTR_ALWAYS_INLINE static inline void _hw_endpoint_buffer_control_clear_mask32 (struct hw_endpoint *ep, uint32_t value)
+{
+ return _hw_endpoint_buffer_control_update32(ep, ~value, 0);
+}
+
+static inline uintptr_t hw_data_offset (uint8_t *buf)
+{
+ // Remove usb base from buffer pointer
+ return (uintptr_t) buf ^ (uintptr_t) usb_dpram;
}
extern const char *ep_dir_string[];
diff --git a/src/portable/renesas/usba/hcd_usba.c b/src/portable/renesas/usba/hcd_usba.c
index 5246ecb94..18cd5f148 100644
--- a/src/portable/renesas/usba/hcd_usba.c
+++ b/src/portable/renesas/usba/hcd_usba.c
@@ -600,7 +600,7 @@ uint32_t hcd_frame_number(uint8_t rhport)
bool hcd_port_connect_status(uint8_t rhport)
{
(void)rhport;
- return USB0.INTSTS1.BIT.ATTCH ? true: false;
+ return USB0.INTSTS1.BIT.ATTCH ? true : false;
}
void hcd_port_reset(uint8_t rhport)
diff --git a/src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c b/src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c
index c88a8a3a2..54c3c95e7 100644
--- a/src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c
+++ b/src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c
@@ -122,7 +122,7 @@
(TU_CHECK_MCU(OPT_MCU_STM32L4) && defined(STM32L4_FSDEV)) \
)
-// In order to reduce the dependance on HAL, we undefine this.
+// In order to reduce the dependence on HAL, we undefine this.
// Some definitions are copied to our private include file.
#undef USE_HAL_DRIVER
diff --git a/src/portable/sunxi/dcd_sunxi_musb.c b/src/portable/sunxi/dcd_sunxi_musb.c
index a0be846a5..2d0e379ad 100644
--- a/src/portable/sunxi/dcd_sunxi_musb.c
+++ b/src/portable/sunxi/dcd_sunxi_musb.c
@@ -243,7 +243,7 @@ static void USBC_Dev_SetAddress(u8 address)
static void __USBC_Dev_Tx_SendStall(void)
{
- //send stall, and fifo is flushed automaticly
+ //send stall, and fifo is flushed automatically
USBC_REG_set_bit_w(USBC_BP_TXCSR_D_SEND_STALL, USBC_REG_TXCSR(USBC0_BASE));
}
static u32 __USBC_Dev_Tx_IsEpStall(void)
diff --git a/src/portable/sunxi/musb_def.h b/src/portable/sunxi/musb_def.h
index 602b4f113..53da5ded2 100644
--- a/src/portable/sunxi/musb_def.h
+++ b/src/portable/sunxi/musb_def.h
@@ -93,7 +93,7 @@
#define USBC1_BASE 0x01c14000
#define USBC2_BASE 0x01c1E000
-//Some reg whithin musb
+//Some reg within musb
#define USBPHY_CLK_REG 0x01c200CC
#define USBPHY_CLK_RST_BIT 0
#define USBPHY_CLK_GAT_BIT 1
diff --git a/src/portable/synopsys/dwc2/dcd_dwc2.c b/src/portable/synopsys/dwc2/dcd_dwc2.c
index f5ecdfe4f..c6132a1f5 100644
--- a/src/portable/synopsys/dwc2/dcd_dwc2.c
+++ b/src/portable/synopsys/dwc2/dcd_dwc2.c
@@ -529,8 +529,10 @@ void dcd_init (uint8_t rhport)
dwc2->dcfg |= DCFG_NZLSOHSK;
// Clear all interrupts
- dwc2->gintsts |= dwc2->gintsts;
- dwc2->gotgint |= dwc2->gotgint;
+ uint32_t int_mask = dwc2->gintsts;
+ dwc2->gintsts |= int_mask;
+ int_mask = dwc2->gotgint;
+ dwc2->gotgint |= int_mask;
// Required as part of core initialization.
// TODO: How should mode mismatch be handled? It will cause
@@ -1003,7 +1005,7 @@ static void handle_rxflvl_irq(uint8_t rhport)
switch ( pktsts )
{
- // Global OUT NAK: do nothign
+ // Global OUT NAK: do nothing
case GRXSTS_PKTSTS_GLOBALOUTNAK: break;
case GRXSTS_PKTSTS_SETUPRX:
@@ -1219,7 +1221,8 @@ void dcd_int_handler(uint8_t rhport)
{
dwc2_regs_t *dwc2 = DWC2_REG(rhport);
- uint32_t const int_status = dwc2->gintsts & dwc2->gintmsk;
+ uint32_t const int_mask = dwc2->gintmsk;
+ uint32_t const int_status = dwc2->gintsts & int_mask;
if(int_status & GINTSTS_USBRST)
{
diff --git a/src/portable/synopsys/dwc2/dwc2_stm32.h b/src/portable/synopsys/dwc2/dwc2_stm32.h
index ea786362e..cb455bd90 100644
--- a/src/portable/synopsys/dwc2/dwc2_stm32.h
+++ b/src/portable/synopsys/dwc2/dwc2_stm32.h
@@ -116,19 +116,19 @@ static const dwc2_controller_t _dwc2_controller[] =
//
//--------------------------------------------------------------------+
-// SystemCoreClock is alrady included by family header
+// SystemCoreClock is already included by family header
// extern uint32_t SystemCoreClock;
TU_ATTR_ALWAYS_INLINE
static inline void dwc2_dcd_int_enable(uint8_t rhport)
{
- NVIC_EnableIRQ(_dwc2_controller[rhport].irqnum);
+ NVIC_EnableIRQ((IRQn_Type)_dwc2_controller[rhport].irqnum);
}
TU_ATTR_ALWAYS_INLINE
static inline void dwc2_dcd_int_disable (uint8_t rhport)
{
- NVIC_DisableIRQ(_dwc2_controller[rhport].irqnum);
+ NVIC_DisableIRQ((IRQn_Type)_dwc2_controller[rhport].irqnum);
}
TU_ATTR_ALWAYS_INLINE
diff --git a/src/portable/valentyusb/eptri/dcd_eptri.c b/src/portable/valentyusb/eptri/dcd_eptri.c
index e368b5f39..a3f228dd9 100644
--- a/src/portable/valentyusb/eptri/dcd_eptri.c
+++ b/src/portable/valentyusb/eptri/dcd_eptri.c
@@ -465,7 +465,7 @@ void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr)
enable = 1;
usb_out_ctrl_write((0 << CSR_USB_OUT_CTRL_STALL_OFFSET) | (enable << CSR_USB_OUT_CTRL_ENABLE_OFFSET) | tu_edpt_number(ep_addr));
}
- // IN endpoints will get unstalled when more data is written.
+ // IN endpoints will get un-stalled when more data is written.
}
bool dcd_edpt_xfer (uint8_t rhport, uint8_t ep_addr, uint8_t* buffer, uint16_t total_bytes)
diff --git a/src/portable/wch/ch32v307/ch32_usbhs_reg.h b/src/portable/wch/ch32v307/ch32_usbhs_reg.h
new file mode 100644
index 000000000..5a2c1fbc9
--- /dev/null
+++ b/src/portable/wch/ch32v307/ch32_usbhs_reg.h
@@ -0,0 +1,345 @@
+#ifndef _USB_CH32_USBHS_REG_H
+#define _USB_CH32_USBHS_REG_H
+
+#include
+
+/******************* GLOBAL ******************/
+
+// USB CONTROL
+#define USBHS_CONTROL_OFFSET 0x00
+#define USBHS_DMA_EN (1 << 0)
+#define USBHS_ALL_CLR (1 << 1)
+#define USBHS_FORCE_RST (1 << 2)
+#define USBHS_INT_BUSY_EN (1 << 3)
+#define USBHS_DEV_PU_EN (1 << 4)
+#define USBHS_SPEED_MASK (3 << 5)
+#define USBHS_FULL_SPEED (0 << 5)
+#define USBHS_HIGH_SPEED (1 << 5)
+#define USBHS_LOW_SPEED (2 << 5)
+#define USBHS_HOST_MODE (1 << 7)
+
+// USB_INT_EN
+#define USBHS_INT_EN_OFFSET 0x02
+#define USBHS_BUS_RST_EN (1 << 0)
+#define USBHS_DETECT_EN (1 << 0)
+#define USBHS_TRANSFER_EN (1 << 1)
+#define USBHS_SUSPEND_EN (1 << 2)
+#define USBHS_SOF_ACT_EN (1 << 3)
+#define USBHS_FIFO_OV_EN (1 << 4)
+#define USBHS_SETUP_ACT_EN (1 << 5)
+#define USBHS_ISO_ACT_EN (1 << 6)
+#define USBHS_DEV_NAK_EN (1 << 7)
+
+// USB DEV AD
+#define USBHS_DEV_AD_OFFSET 0x03
+// USB FRAME_NO
+#define USBHS_FRAME_NO_OFFSET 0x04
+// USB SUSPEND
+#define USBHS_SUSPEND_OFFSET 0x06
+#define USBHS_DEV_REMOTE_WAKEUP (1 << 2)
+#define USBHS_LINESTATE_MASK (2 << 4) /* Read Only */
+
+// RESERVED0
+
+// USB SPEED TYPE
+#define USBHS_SPEED_TYPE_OFFSET 0x08
+#define USBSPEED_MASK (0x03)
+
+// USB_MIS_ST
+#define USBHS_MIS_ST_OFFSET 0x09
+#define USBHS_SPLIT_CAN (1 << 0)
+#define USBHS_ATTACH (1 << 1)
+#define USBHS_SUSPEND (1 << 2)
+#define USBHS_BUS_RESET (1 << 3)
+#define USBHS_R_FIFO_RDY (1 << 4)
+#define USBHS_SIE_FREE (1 << 5)
+#define USBHS_SOF_ACT (1 << 6)
+#define USBHS_SOF_PRES (1 << 7)
+
+// INT_FLAG
+#define USBHS_INT_FLAG_OFFSET 0x0A
+#define USBHS_BUS_RST_FLAG (1 << 0)
+#define USBHS_DETECT_FLAG (1 << 0)
+#define USBHS_TRANSFER_FLAG (1 << 1)
+#define USBHS_SUSPEND_FLAG (1 << 2)
+#define USBHS_HST_SOF_FLAG (1 << 3)
+#define USBHS_FIFO_OV_FLAG (1 << 4)
+#define USBHS_SETUP_FLAG (1 << 5)
+#define USBHS_ISO_ACT_FLAG (1 << 6)
+
+// INT_ST
+#define USBHS_INT_ST_OFFSET 0x0B
+#define USBHS_DEV_UIS_IS_NAK (1 << 7)
+#define USBHS_DEV_UIS_TOG_OK (1 << 6)
+#define MASK_UIS_TOKEN (3 << 4)
+#define MASK_UIS_ENDP (0x0F)
+#define MASK_UIS_H_RES (0x0F)
+
+#define USBHS_TOGGLE_OK (0x40)
+#define USBHS_HOST_RES (0x0f)
+
+//USB_RX_LEN
+#define USBHS_RX_LEN_OFFSET 0x0C
+/******************* DEVICE ******************/
+
+//UEP_CONFIG
+#define USBHS_UEP_CONFIG_OFFSET 0x10
+#define USBHS_EP0_T_EN (1 << 0)
+#define USBHS_EP0_R_EN (1 << 16)
+
+#define USBHS_EP1_T_EN (1 << 1)
+#define USBHS_EP1_R_EN (1 << 17)
+
+#define USBHS_EP2_T_EN (1 << 2)
+#define USBHS_EP2_R_EN (1 << 18)
+
+#define USBHS_EP3_T_EN (1 << 3)
+#define USBHS_EP3_R_EN (1 << 19)
+
+#define USBHS_EP4_T_EN (1 << 4)
+#define USBHS_EP4_R_EN (1 << 20)
+
+#define USBHS_EP5_T_EN (1 << 5)
+#define USBHS_EP5_R_EN (1 << 21)
+
+#define USBHS_EP6_T_EN (1 << 6)
+#define USBHS_EP6_R_EN (1 << 22)
+
+#define USBHS_EP7_T_EN (1 << 7)
+#define USBHS_EP7_R_EN (1 << 23)
+
+#define USBHS_EP8_T_EN (1 << 8)
+#define USBHS_EP8_R_EN (1 << 24)
+
+#define USBHS_EP9_T_EN (1 << 9)
+#define USBHS_EP9_R_EN (1 << 25)
+
+#define USBHS_EP10_T_EN (1 << 10)
+#define USBHS_EP10_R_EN (1 << 26)
+
+#define USBHS_EP11_T_EN (1 << 11)
+#define USBHS_EP11_R_EN (1 << 27)
+
+#define USBHS_EP12_T_EN (1 << 12)
+#define USBHS_EP12_R_EN (1 << 28)
+
+#define USBHS_EP13_T_EN (1 << 13)
+#define USBHS_EP13_R_EN (1 << 29)
+
+#define USBHS_EP14_T_EN (1 << 14)
+#define USBHS_EP14_R_EN (1 << 30)
+
+#define USBHS_EP15_T_EN (1 << 15)
+#define USBHS_EP15_R_EN (1 << 31)
+
+//UEP_TYPE
+#define USBHS_UEP_TYPE_OFFSET 0x14
+#define USBHS_EP0_T_TYP (1 << 0)
+#define USBHS_EP0_R_TYP (1 << 16)
+
+#define USBHS_EP1_T_TYP (1 << 1)
+#define USBHS_EP1_R_TYP (1 << 17)
+
+#define USBHS_EP2_T_TYP (1 << 2)
+#define USBHS_EP2_R_TYP (1 << 18)
+
+#define USBHS_EP3_T_TYP (1 << 3)
+#define USBHS_EP3_R_TYP (1 << 19)
+
+#define USBHS_EP4_T_TYP (1 << 4)
+#define USBHS_EP4_R_TYP (1 << 20)
+
+#define USBHS_EP5_T_TYP (1 << 5)
+#define USBHS_EP5_R_TYP (1 << 21)
+
+#define USBHS_EP6_T_TYP (1 << 6)
+#define USBHS_EP6_R_TYP (1 << 22)
+
+#define USBHS_EP7_T_TYP (1 << 7)
+#define USBHS_EP7_R_TYP (1 << 23)
+
+#define USBHS_EP8_T_TYP (1 << 8)
+#define USBHS_EP8_R_TYP (1 << 24)
+
+#define USBHS_EP9_T_TYP (1 << 8)
+#define USBHS_EP9_R_TYP (1 << 25)
+
+#define USBHS_EP10_T_TYP (1 << 10)
+#define USBHS_EP10_R_TYP (1 << 26)
+
+#define USBHS_EP11_T_TYP (1 << 11)
+#define USBHS_EP11_R_TYP (1 << 27)
+
+#define USBHS_EP12_T_TYP (1 << 12)
+#define USBHS_EP12_R_TYP (1 << 28)
+
+#define USBHS_EP13_T_TYP (1 << 13)
+#define USBHS_EP13_R_TYP (1 << 29)
+
+#define USBHS_EP14_T_TYP (1 << 14)
+#define USBHS_EP14_R_TYP (1 << 30)
+
+#define USBHS_EP15_T_TYP (1 << 15)
+#define USBHS_EP15_R_TYP (1 << 31)
+
+/* BUF_MOD UEP1~15 */
+#define USBHS_BUF_MOD_OFFSET 0x18
+#define USBHS_EP0_BUF_MOD (1 << 0)
+#define USBHS_EP0_ISO_BUF_MOD (1 << 16)
+
+#define USBHS_EP1_BUF_MOD (1 << 1)
+#define USBHS_EP1_ISO_BUF_MOD (1 << 17)
+
+#define USBHS_EP2_BUF_MOD (1 << 2)
+#define USBHS_EP2_ISO_BUF_MOD (1 << 18)
+
+#define USBHS_EP3_BUF_MOD (1 << 3)
+#define USBHS_EP3_ISO_BUF_MOD (1 << 19)
+
+#define USBHS_EP4_BUF_MOD (1 << 4)
+#define USBHS_EP4_ISO_BUF_MOD (1 << 20)
+
+#define USBHS_EP5_BUF_MOD (1 << 5)
+#define USBHS_EP5_ISO_BUF_MOD (1 << 21)
+
+#define USBHS_EP6_BUF_MOD (1 << 6)
+#define USBHS_EP6_ISO_BUF_MOD (1 << 22)
+
+#define USBHS_EP7_BUF_MOD (1 << 7)
+#define USBHS_EP7_ISO_BUF_MOD (1 << 23)
+
+#define USBHS_EP8_BUF_MOD (1 << 8)
+#define USBHS_EP8_ISO_BUF_MOD (1 << 24)
+
+#define USBHS_EP9_BUF_MOD (1 << 9)
+#define USBHS_EP9_ISO_BUF_MOD (1 << 25)
+
+#define USBHS_EP10_BUF_MOD (1 << 10)
+#define USBHS_EP10_ISO_BUF_MOD (1 << 26)
+
+#define USBHS_EP11_BUF_MOD (1 << 11)
+#define USBHS_EP11_ISO_BUF_MOD (1 << 27)
+
+#define USBHS_EP12_BUF_MOD (1 << 12)
+#define USBHS_EP12_ISO_BUF_MOD (1 << 28)
+
+#define USBHS_EP13_BUF_MOD (1 << 13)
+#define USBHS_EP13_ISO_BUF_MOD (1 << 29)
+
+#define USBHS_EP14_BUF_MOD (1 << 14)
+#define USBHS_EP14_ISO_BUF_MOD (1 << 30)
+
+#define USBHS_EP15_BUF_MOD (1 << 15)
+#define USBHS_EP15_ISO_BUF_MOD (1 << 31)
+//USBHS_EPn_T_EN USBHS_EPn_R_EN USBHS_EPn_BUF_MOD Description: Arrange from low to high with UEPn_DMA as the starting address
+// 0 0 x The endpoint is disabled and the UEPn_*_DMA buffers are not used.
+// 1 0 0 The first address of the receive (OUT) buffer is UEPn_RX_DMA
+// 1 0 1 RB_UEPn_RX_TOG[0]=0, use buffer UEPn_RX_DMA RB_UEPn_RX_TOG[0]=1, use buffer UEPn_TX_DMA
+// 0 1 0 The first address of the transmit (IN) buffer is UEPn_TX_DMA.
+// 0 1 1 RB_UEPn_TX_TOG[0]=0, use buffer UEPn_TX_DMA RB_UEPn_TX_TOG[0]=1, use buffer UEPn_RX_DMA
+
+/* USB0_DMA */
+#define USBHS_UEP0_DMA_OFFSET(n) (0x1C) // endpoint 0 DMA buffer address
+
+/* USBX_RX_DMA */
+#define USBHS_UEPx_RX_DMA_OFFSET(n) (0x1C + 4 * (n)) // endpoint x DMA buffer address
+
+#define USBHS_UEPx_TX_DMA_OFFSET(n) (0x58 + 4 * (n)) // endpoint x DMA buffer address
+
+#define USBHS_UEPx_MAX_LEN_OFFSET(n) (0x98 + 4 * (n)) // endpoint x DMA buffer address
+
+#define USBHS_UEPx_T_LEN_OFFSET(n) (0xD8 + 4 * (n)) // endpoint x DMA buffer address
+#define USBHS_UEPx_TX_CTRL_OFFSET(n) (0xD8 + 4 * (n) + 2) // endpoint x DMA buffer address
+#define USBHS_UEPx_RX_CTRL_OFFSET(n) (0xD8 + 4 * (n) + 3) // endpoint x DMA buffer address
+
+// UEPn_T_LEN
+#define USBHS_EP_T_LEN_MASK (0x7FF)
+
+//UEPn_TX_CTRL
+#define USBHS_EP_T_RES_MASK (3 << 0)
+#define USBHS_EP_T_RES_ACK (0 << 0)
+#define USBHS_EP_T_RES_NYET (1 << 0)
+#define USBHS_EP_T_RES_NAK (2 << 0)
+#define USBHS_EP_T_RES_STALL (3 << 0)
+
+#define USBHS_EP_T_TOG_MASK (3 << 3)
+#define USBHS_EP_T_TOG_0 (0 << 3)
+#define USBHS_EP_T_TOG_1 (1 << 3)
+#define USBHS_EP_T_TOG_2 (2 << 3)
+#define USBHS_EP_T_TOG_M (3 << 3)
+
+#define USBHS_EP_T_AUTOTOG (1 << 5)
+
+//UEPn_RX_CTRL
+#define USBHS_EP_R_RES_MASK (3 << 0)
+#define USBHS_EP_R_RES_ACK (0 << 0)
+#define USBHS_EP_R_RES_NYET (1 << 0)
+#define USBHS_EP_R_RES_NAK (2 << 0)
+#define USBHS_EP_R_RES_STALL (3 << 0)
+
+#define USBHS_EP_R_TOG_MASK (3 << 3)
+#define USBHS_EP_R_TOG_0 (0 << 3)
+#define USBHS_EP_R_TOG_1 (1 << 3)
+#define USBHS_EP_R_TOG_2 (2 << 3)
+#define USBHS_EP_R_TOG_M (3 << 3)
+
+#define USBHS_EP_R_AUTOTOG (1 << 5)
+
+#define USBHS_TOG_MATCH (1 << 6)
+
+/******************* HOST ******************/
+// USB HOST_CTRL
+#define USBHS_SEND_BUS_RESET (1 << 0)
+#define USBHS_SEND_BUS_SUSPEND (1 << 1)
+#define USBHS_SEND_BUS_RESUME (1 << 2)
+#define USBHS_REMOTE_WAKE (1 << 3)
+#define USBHS_PHY_SUSPENDM (1 << 4)
+#define USBHS_UH_SOFT_FREE (1 << 6)
+#define USBHS_SEND_SOF_EN (1 << 7)
+
+//UH_CONFIG
+#define USBHS_HOST_TX_EN (1 << 3)
+#define USBHS_HOST_RX_EN (1 << 18)
+
+// HOST_EP_TYPE
+#define USBHS_ENDP_TX_ISO (1 << 3)
+#define USBHS_ENDP_RX_ISO (1 << (16 + 2))
+
+// R32_UH_EP_PID
+#define USBHS_HOST_MASK_TOKEN (0x0f)
+#define USBHS_HOST_MASK_ENDP (0x0f << 4)
+
+//R8_UH_RX_CTRL
+#define USBHS_EP_R_RES_MASK (3 << 0)
+#define USBHS_EP_R_RES_ACK (0 << 0)
+#define USBHS_EP_R_RES_NYET (1 << 0)
+#define USBHS_EP_R_RES_NAK (2 << 0)
+#define USBHS_EP_R_RES_STALL (3 << 0)
+
+#define USBHS_UH_R_RES_NO (1 << 2)
+#define USBHS_UH_R_TOG_1 (1 << 3)
+#define USBHS_UH_R_TOG_2 (2 << 3)
+#define USBHS_UH_R_TOG_3 (3 << 3)
+#define USBHS_UH_R_TOG_AUTO (1 << 5)
+#define USBHS_UH_R_DATA_NO (1 << 6)
+//R8_UH_TX_CTRL
+#define USBHS_UH_T_RES_MASK (3 << 0)
+#define USBHS_UH_T_RES_ACK (0 << 0)
+#define USBHS_UH_T_RES_NYET (1 << 0)
+#define USBHS_UH_T_RES_NAK (2 << 0)
+#define USBHS_UH_T_RES_STALL (3 << 0)
+
+#define USBHS_UH_T_RES_NO (1 << 2)
+#define USBHS_UH_T_TOG_1 (1 << 3)
+#define USBHS_UH_T_TOG_2 (2 << 3)
+#define USBHS_UH_T_TOG_3 (3 << 3)
+#define USBHS_UH_T_TOG_AUTO (1 << 5)
+#define USBHS_UH_T_DATA_NO (1 << 6)
+
+// 00: OUT, 01:SOF, 10:IN, 11:SETUP
+#define PID_OUT 0
+#define PID_SOF 1
+#define PID_IN 2
+#define PID_SETUP 3
+
+#endif
diff --git a/src/portable/wch/ch32v307/dcd_usbhs.c b/src/portable/wch/ch32v307/dcd_usbhs.c
new file mode 100644
index 000000000..b7a79e166
--- /dev/null
+++ b/src/portable/wch/ch32v307/dcd_usbhs.c
@@ -0,0 +1,391 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Greg Davill
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ * This file is part of the TinyUSB stack.
+ */
+
+#include "tusb_option.h"
+
+#if CFG_TUD_ENABLED && (CFG_TUSB_MCU == OPT_MCU_CH32V307)
+#include "device/dcd.h"
+
+#include "ch32_usbhs_reg.h"
+#include "core_riscv.h"
+
+// Max number of bi-directional endpoints including EP0
+#define EP_MAX 16
+
+typedef struct {
+ uint8_t *buffer;
+ // tu_fifo_t * ff; // TODO support dcd_edpt_xfer_fifo API
+ uint16_t total_len;
+ uint16_t queued_len;
+ uint16_t max_size;
+ bool short_packet;
+} xfer_ctl_t;
+
+#define XFER_CTL_BASE(_ep, _dir) &xfer_status[_ep][_dir]
+static xfer_ctl_t xfer_status[EP_MAX][2];
+
+#define EP_TX_LEN(ep) *(volatile uint16_t *)((volatile uint16_t *)&(USBHSD->UEP0_TX_LEN) + (ep)*2)
+#define EP_TX_CTRL(ep) *(volatile uint8_t *)((volatile uint8_t *)&(USBHSD->UEP0_TX_CTRL) + (ep)*4)
+#define EP_RX_CTRL(ep) *(volatile uint8_t *)((volatile uint8_t *)&(USBHSD->UEP0_RX_CTRL) + (ep)*4)
+#define EP_RX_MAX_LEN(ep) *(volatile uint16_t *)((volatile uint16_t *)&(USBHSD->UEP0_MAX_LEN) + (ep)*2)
+
+#define EP_TX_DMA_ADDR(ep) *(volatile uint32_t *)((volatile uint32_t *)&(USBHSD->UEP1_TX_DMA) + (ep - 1))
+#define EP_RX_DMA_ADDR(ep) *(volatile uint32_t *)((volatile uint32_t *)&(USBHSD->UEP1_RX_DMA) + (ep - 1))
+
+/* Endpoint Buffer */
+TU_ATTR_ALIGNED(4) uint8_t EP0_DatabufHD[64]; // ep0(64)
+
+volatile uint8_t USBHS_Dev_Endp0_Tog = 0x01;
+
+void dcd_init(uint8_t rhport) {
+ (void)rhport;
+
+ memset(&xfer_status, 0, sizeof(xfer_status));
+
+ USBHSD->HOST_CTRL = 0x00;
+ USBHSD->HOST_CTRL = USBHS_PHY_SUSPENDM;
+
+ USBHSD->CONTROL = 0;
+
+#if TUD_OPT_HIGH_SPEED
+ USBHSD->CONTROL = USBHS_DMA_EN | USBHS_INT_BUSY_EN | USBHS_HIGH_SPEED;
+#else
+ #error OPT_MODE_FULL_SPEED not currently supported on CH32V307
+ USBHSD->CONTROL = USBHS_DMA_EN | USBHS_INT_BUSY_EN | USBHS_FULL_SPEED;
+#endif
+
+ USBHSD->INT_EN = 0;
+ USBHSD->INT_EN = USBHS_SETUP_ACT_EN | USBHS_TRANSFER_EN | USBHS_DETECT_EN | USBHS_SUSPEND_EN;
+
+ /* ALL endpoint enable */
+ USBHSD->ENDP_CONFIG = 0xffffffff;
+
+ USBHSD->ENDP_CONFIG = USBHS_EP0_T_EN | USBHS_EP0_R_EN;
+ USBHSD->ENDP_TYPE = 0x00;
+ USBHSD->BUF_MODE = 0x00;
+
+ USBHSD->UEP0_MAX_LEN = 64;
+
+ USBHSD->UEP0_DMA = (uint32_t)EP0_DatabufHD;
+
+ USBHSD->UEP0_TX_LEN = 0;
+ USBHSD->UEP0_TX_CTRL = USBHS_EP_T_RES_NAK;
+ USBHSD->UEP0_RX_CTRL = USBHS_EP_R_RES_ACK;
+
+ for (int ep = 1; ep < EP_MAX; ep++) {
+ EP_TX_LEN(ep) = 0;
+ EP_TX_CTRL(ep) = USBHS_EP_T_AUTOTOG | USBHS_EP_T_RES_NAK;
+ EP_RX_CTRL(ep) = USBHS_EP_R_AUTOTOG | USBHS_EP_R_RES_NAK;
+
+ EP_RX_MAX_LEN(ep) = 512;
+ }
+
+ USBHSD->DEV_AD = 0;
+ USBHSD->CONTROL |= USBHS_DEV_PU_EN;
+}
+
+void dcd_int_enable(uint8_t rhport) {
+ (void)rhport;
+
+ NVIC_EnableIRQ(USBHS_IRQn);
+}
+
+void dcd_int_disable(uint8_t rhport) {
+ (void)rhport;
+
+ NVIC_DisableIRQ(USBHS_IRQn);
+}
+
+void dcd_edpt_close_all(uint8_t rhport) {
+ (void)rhport;
+}
+
+void dcd_set_address(uint8_t rhport, uint8_t dev_addr) {
+ (void)dev_addr;
+
+ // Response with zlp status
+ dcd_edpt_xfer(rhport, 0x80, NULL, 0);
+}
+
+void dcd_remote_wakeup(uint8_t rhport)
+{
+ (void) rhport;
+}
+
+void dcd_edpt0_status_complete(uint8_t rhport, tusb_control_request_t const *request) {
+ (void)rhport;
+
+ if (request->bmRequestType_bit.recipient == TUSB_REQ_RCPT_DEVICE &&
+ request->bmRequestType_bit.type == TUSB_REQ_TYPE_STANDARD &&
+ request->bRequest == TUSB_REQ_SET_ADDRESS) {
+ USBHSD->DEV_AD = (uint8_t)request->wValue;
+ }
+
+ EP_TX_CTRL(0) = USBHS_EP_T_RES_NAK;
+ EP_RX_CTRL(0) = USBHS_EP_R_RES_ACK;
+}
+
+bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const *desc_edpt) {
+ (void)rhport;
+
+ uint8_t const epnum = tu_edpt_number(desc_edpt->bEndpointAddress);
+ uint8_t const dir = tu_edpt_dir(desc_edpt->bEndpointAddress);
+
+ TU_ASSERT(epnum < EP_MAX);
+
+ xfer_ctl_t *xfer = XFER_CTL_BASE(epnum, dir);
+ xfer->max_size = tu_edpt_packet_size(desc_edpt);
+
+ if (epnum != 0) {
+ if (tu_edpt_dir(desc_edpt->bEndpointAddress) == TUSB_DIR_OUT) {
+ EP_RX_CTRL(epnum) = USBHS_EP_R_AUTOTOG | USBHS_EP_R_RES_ACK;
+ } else {
+ EP_TX_LEN(epnum) = 0;
+ EP_TX_CTRL(epnum) = USBHS_EP_T_AUTOTOG | USBHS_EP_T_RES_NAK | USBHS_EP_T_TOG_0;
+ }
+ }
+
+ return true;
+}
+
+int usbd_ep_close(const uint8_t ep) {
+ (void)ep;
+
+ return 0;
+}
+void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr) {
+ (void)rhport;
+
+ uint8_t const epnum = tu_edpt_number(ep_addr);
+ uint8_t const dir = tu_edpt_dir(ep_addr);
+
+ if (epnum == 0) {
+ if (dir == TUSB_DIR_OUT) {
+ USBHSD->UEP0_RX_CTRL = USBHS_EP_R_RES_STALL;
+ } else {
+ USBHSD->UEP0_TX_LEN = 0;
+ USBHSD->UEP0_TX_CTRL = USBHS_EP_T_RES_STALL;
+ }
+ } else {
+ if (dir == TUSB_DIR_OUT) {
+ EP_RX_CTRL(epnum) = (EP_RX_CTRL(epnum) & ~USBHS_EP_R_RES_MASK) | USBHS_EP_R_RES_STALL;
+
+ } else {
+ EP_TX_CTRL(epnum) = (EP_TX_CTRL(epnum) & ~USBHS_EP_T_RES_MASK) | USBHS_EP_T_RES_STALL;
+ }
+ }
+}
+
+void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) {
+ (void)rhport;
+
+ uint8_t const epnum = tu_edpt_number(ep_addr);
+ uint8_t const dir = tu_edpt_dir(ep_addr);
+
+ if (epnum == 0) {
+ if (dir == TUSB_DIR_OUT) {
+ USBHSD->UEP0_RX_CTRL = USBHS_EP_R_RES_ACK;
+ } else {
+ }
+ } else {
+ if (dir == TUSB_DIR_OUT) {
+ EP_RX_CTRL(epnum) = (EP_RX_CTRL(epnum) & ~(USBHS_EP_R_RES_MASK | USBHS_EP_T_TOG_MASK)) | USBHS_EP_T_RES_ACK;
+
+ } else {
+ EP_TX_CTRL(epnum) = (EP_TX_CTRL(epnum) & ~(USBHS_EP_T_RES_MASK | USBHS_EP_T_TOG_MASK)) | USBHS_EP_T_RES_NAK;
+ }
+ }
+}
+
+bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t *buffer, uint16_t total_bytes) {
+ (void)rhport;
+ uint8_t const epnum = tu_edpt_number(ep_addr);
+ uint8_t const dir = tu_edpt_dir(ep_addr);
+
+ xfer_ctl_t *xfer = XFER_CTL_BASE(epnum, dir);
+ xfer->buffer = buffer;
+ // xfer->ff = NULL; // TODO support dcd_edpt_xfer_fifo API
+ xfer->total_len = total_bytes;
+ xfer->queued_len = 0;
+ xfer->short_packet = false;
+
+ // uint16_t num_packets = (total_bytes / xfer->max_size);
+ uint16_t short_packet_size = total_bytes % (xfer->max_size + 1);
+
+ // Zero-size packet is special case.
+ if (short_packet_size == 0 || (total_bytes == 0)) {
+ xfer->short_packet = true;
+ }
+
+ if (tu_edpt_dir(ep_addr) == TUSB_DIR_IN) {
+ if (!total_bytes) {
+ xfer->short_packet = true;
+ if (epnum == 0) {
+ USBHSD->UEP0_TX_LEN = 0;
+ USBHSD->UEP0_TX_CTRL = USBHS_EP_T_RES_ACK | (USBHS_Dev_Endp0_Tog ? USBHS_EP_T_TOG_1 : USBHS_EP_T_TOG_0);
+ USBHS_Dev_Endp0_Tog ^= 1;
+ } else {
+ EP_TX_LEN(epnum) = 0;
+ EP_TX_CTRL(epnum) = (EP_TX_CTRL(epnum) & ~(USBHS_EP_T_RES_MASK)) | USBHS_EP_T_RES_ACK;
+ }
+ } else {
+ if (epnum == 0) {
+ xfer->queued_len += short_packet_size;
+ memcpy(&EP0_DatabufHD[0], buffer, short_packet_size);
+
+ USBHSD->UEP0_TX_LEN = short_packet_size;
+ USBHSD->UEP0_TX_CTRL = USBHS_EP_T_RES_ACK | (USBHS_Dev_Endp0_Tog ? USBHS_EP_T_TOG_1 : USBHS_EP_T_TOG_0);
+ USBHS_Dev_Endp0_Tog ^= 1;
+ } else {
+ xfer->queued_len += short_packet_size;
+
+ EP_TX_DMA_ADDR(epnum) = (uint32_t)buffer;
+ USBHSD->ENDP_CONFIG |= (USBHS_EP0_T_EN << epnum);
+ EP_TX_LEN(epnum) = short_packet_size;
+ EP_TX_CTRL(epnum) = (EP_TX_CTRL(epnum) & ~(USBHS_EP_T_RES_MASK)) | USBHS_EP_T_RES_ACK;
+ }
+ }
+ } else { /* TUSB_DIR_OUT */
+ if (epnum == 0) {
+ uint32_t read_count = USBHSD->RX_LEN;
+ read_count = TU_MIN(read_count, total_bytes);
+
+ if ((total_bytes == 8)) {
+ read_count = 8;
+ memcpy(buffer, &EP0_DatabufHD[0], 8);
+ } else {
+ memcpy(buffer, &EP0_DatabufHD[0], read_count);
+ }
+ } else {
+ EP_RX_DMA_ADDR(epnum) = (uint32_t)xfer->buffer;
+ USBHSD->ENDP_CONFIG |= (USBHS_EP0_R_EN << epnum);
+ }
+
+ // usbd_ep_read(ep_addr, buffer, total_bytes, &ret_bytes);
+ }
+ return true;
+}
+
+
+static void receive_packet(xfer_ctl_t *xfer, uint16_t xfer_size) {
+ // xfer->queued_len = xfer->total_len - remaining;
+
+ uint16_t remaining = xfer->total_len - xfer->queued_len;
+ uint16_t to_recv_size;
+
+ if (remaining <= xfer->max_size) {
+ // Avoid buffer overflow.
+ to_recv_size = (xfer_size > remaining) ? remaining : xfer_size;
+ } else {
+ // Room for full packet, choose recv_size based on what the microcontroller
+ // claims.
+ to_recv_size = (xfer_size > xfer->max_size) ? xfer->max_size : xfer_size;
+ }
+
+ if (to_recv_size) {
+ }
+
+ xfer->queued_len += xfer_size;
+
+ // Per USB spec, a short OUT packet (including length 0) is always
+ // indicative of the end of a transfer (at least for ctl, bulk, int).
+ xfer->short_packet = (xfer_size < xfer->max_size);
+}
+
+void dcd_int_handler(uint8_t rhport) {
+ (void)rhport;
+
+ uint32_t end_num, rx_token;
+ uint8_t intflag = 0;
+
+ intflag = USBHSD->INT_FG;
+
+ if (intflag & USBHS_TRANSFER_FLAG) {
+
+ end_num = (USBHSD->INT_ST) & MASK_UIS_ENDP;
+ rx_token = (((USBHSD->INT_ST) & MASK_UIS_TOKEN) >> 4) & 0x03;
+
+ uint8_t endp = end_num | (rx_token == PID_IN ? TUSB_DIR_IN_MASK : 0);
+
+ xfer_ctl_t *xfer = XFER_CTL_BASE(end_num, tu_edpt_dir(endp));
+
+ if (rx_token == PID_OUT) {
+ uint16_t rx_len = USBHSD->RX_LEN;
+
+ receive_packet(xfer, rx_len);
+
+ if (xfer->short_packet || (xfer->queued_len == xfer->total_len)) {
+ xfer->short_packet = false;
+
+ dcd_event_xfer_complete(0, endp, xfer->queued_len, XFER_RESULT_SUCCESS, true);
+ }
+
+ if (end_num == 0) {
+ USBHSD->UEP0_RX_CTRL = USBHS_EP_R_RES_ACK | USBHS_EP_R_TOG_0;
+ }
+
+ } else if (rx_token == PID_IN) {
+ if (xfer->short_packet || (xfer->queued_len == xfer->total_len)) {
+ xfer->short_packet = false;
+ xfer->total_len = 0;
+ dcd_event_xfer_complete(0, endp, xfer->queued_len, XFER_RESULT_SUCCESS, true);
+
+ EP_TX_CTRL(end_num) = (EP_TX_CTRL(end_num) & ~(USBHS_EP_T_RES_MASK)) | USBHS_EP_T_RES_NAK;
+
+ if (end_num == 0) {
+ }
+ } else {
+ dcd_edpt_xfer(0, endp, xfer->buffer + xfer->queued_len, xfer->total_len - xfer->queued_len);
+ }
+ }
+
+ USBHSD->INT_FG = USBHS_TRANSFER_FLAG; /* Clear flag */
+ } else if (intflag & USBHS_SETUP_FLAG) {
+ USBHS_Dev_Endp0_Tog = 1;
+ dcd_event_setup_received(0, EP0_DatabufHD, true);
+
+ USBHSD->INT_FG = USBHS_SETUP_FLAG; /* Clear flag */
+ } else if (intflag & USBHS_DETECT_FLAG) {
+ USBHS_Dev_Endp0_Tog = 1;
+
+ xfer_status[0][TUSB_DIR_OUT].max_size = 64;
+ xfer_status[0][TUSB_DIR_IN].max_size = 64;
+
+ dcd_event_bus_reset(0, TUSB_SPEED_HIGH, true);
+
+ USBHSD->DEV_AD = 0;
+ USBHSD->UEP0_RX_CTRL = USBHS_EP_R_RES_ACK | USBHS_EP_R_TOG_0;
+
+ USBHSD->INT_FG = USBHS_DETECT_FLAG; /* Clear flag */
+ } else if (intflag & USBHS_SUSPEND_FLAG) {
+ dcd_event_t event = { .rhport = rhport, .event_id = DCD_EVENT_SUSPEND };
+ dcd_event_handler(&event, true);
+
+ USBHSD->INT_FG = USBHS_SUSPEND_FLAG; /* Clear flag */
+ }
+}
+
+#endif
diff --git a/src/tusb.c b/src/tusb.c
index c3787ff8f..a5c820b8d 100644
--- a/src/tusb.c
+++ b/src/tusb.c
@@ -31,11 +31,18 @@
#include "tusb.h"
#include "common/tusb_private.h"
-// TODO clean up
#if CFG_TUD_ENABLED
#include "device/usbd_pvt.h"
#endif
+#if CFG_TUH_ENABLED
+#include "host/usbh_classdriver.h"
+#endif
+
+//--------------------------------------------------------------------+
+// Public API
+//--------------------------------------------------------------------+
+
bool tusb_init(void)
{
#if CFG_TUD_ENABLED && defined(TUD_OPT_RHPORT)
@@ -67,18 +74,16 @@ bool tusb_inited(void)
}
//--------------------------------------------------------------------+
-// Internal Helper for both Host and Device stack
+// Endpoint Helper for both Host and Device stack
//--------------------------------------------------------------------+
bool tu_edpt_claim(tu_edpt_state_t* ep_state, osal_mutex_t mutex)
{
(void) mutex;
-#if TUSB_OPT_MUTEX
// pre-check to help reducing mutex lock
TU_VERIFY((ep_state->busy == 0) && (ep_state->claimed == 0));
- osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER);
-#endif
+ (void) osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER);
// can only claim the endpoint if it is not busy and not claimed yet.
bool const available = (ep_state->busy == 0) && (ep_state->claimed == 0);
@@ -87,9 +92,7 @@ bool tu_edpt_claim(tu_edpt_state_t* ep_state, osal_mutex_t mutex)
ep_state->claimed = 1;
}
-#if TUSB_OPT_MUTEX
- osal_mutex_unlock(mutex);
-#endif
+ (void) osal_mutex_unlock(mutex);
return available;
}
@@ -98,9 +101,7 @@ bool tu_edpt_release(tu_edpt_state_t* ep_state, osal_mutex_t mutex)
{
(void) mutex;
-#if TUSB_OPT_MUTEX
- osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER);
-#endif
+ (void) osal_mutex_lock(mutex, OSAL_TIMEOUT_WAIT_FOREVER);
// can only release the endpoint if it is claimed and not busy
bool const ret = (ep_state->claimed == 1) && (ep_state->busy == 0);
@@ -109,9 +110,7 @@ bool tu_edpt_release(tu_edpt_state_t* ep_state, osal_mutex_t mutex)
ep_state->claimed = 0;
}
-#if TUSB_OPT_MUTEX
- osal_mutex_unlock(mutex);
-#endif
+ (void) osal_mutex_unlock(mutex);
return ret;
}
@@ -204,9 +203,184 @@ uint16_t tu_desc_get_interface_total_len(tusb_desc_interface_t const* desc_itf,
return len;
}
-/*------------------------------------------------------------------*/
-/* Debug
- *------------------------------------------------------------------*/
+//--------------------------------------------------------------------+
+// Endpoint Stream Helper for both Host and Device stack
+//--------------------------------------------------------------------+
+
+bool tu_edpt_stream_init(tu_edpt_stream_t* s, bool is_host, bool is_tx, bool overwritable,
+ void* ff_buf, uint16_t ff_bufsize, uint8_t* ep_buf, uint16_t ep_bufsize)
+{
+ osal_mutex_t new_mutex = osal_mutex_create(&s->ff_mutex);
+ (void) new_mutex;
+ (void) is_tx;
+
+ s->is_host = is_host;
+ tu_fifo_config(&s->ff, ff_buf, ff_bufsize, 1, overwritable);
+ tu_fifo_config_mutex(&s->ff, is_tx ? new_mutex : NULL, is_tx ? NULL : new_mutex);
+
+ s->ep_buf = ep_buf;
+ s->ep_bufsize = ep_bufsize;
+
+ return true;
+}
+
+TU_ATTR_ALWAYS_INLINE static inline
+bool stream_claim(tu_edpt_stream_t* s)
+{
+ if (s->is_host)
+ {
+ #if CFG_TUH_ENABLED
+ return usbh_edpt_claim(s->daddr, s->ep_addr);
+ #endif
+ }else
+ {
+ #if CFG_TUD_ENABLED
+ return usbd_edpt_claim(s->rhport, s->ep_addr);
+ #endif
+ }
+
+ return false;
+}
+
+TU_ATTR_ALWAYS_INLINE static inline
+bool stream_xfer(tu_edpt_stream_t* s, uint16_t count)
+{
+ if (s->is_host)
+ {
+ #if CFG_TUH_ENABLED
+ return usbh_edpt_xfer(s->daddr, s->ep_addr, count ? s->ep_buf : NULL, count);
+ #endif
+ }else
+ {
+ #if CFG_TUD_ENABLED
+ return usbd_edpt_xfer(s->rhport, s->ep_addr, count ? s->ep_buf : NULL, count);
+ #endif
+ }
+
+ return false;
+}
+
+TU_ATTR_ALWAYS_INLINE static inline
+bool stream_release(tu_edpt_stream_t* s)
+{
+ if (s->is_host)
+ {
+ #if CFG_TUH_ENABLED
+ return usbh_edpt_release(s->daddr, s->ep_addr);
+ #endif
+ }else
+ {
+ #if CFG_TUD_ENABLED
+ return usbd_edpt_release(s->rhport, s->ep_addr);
+ #endif
+ }
+
+ return false;
+}
+
+//--------------------------------------------------------------------+
+// Stream Write
+//--------------------------------------------------------------------+
+
+bool tu_edpt_stream_write_zlp_if_needed(tu_edpt_stream_t* s, uint32_t last_xferred_bytes)
+{
+ // ZLP condition: no pending data, last transferred bytes is multiple of packet size
+ TU_VERIFY( !tu_fifo_count(&s->ff) && last_xferred_bytes && (0 == (last_xferred_bytes & (s->ep_packetsize-1))) );
+
+ TU_VERIFY( stream_claim(s) );
+ TU_ASSERT( stream_xfer(s, 0) );
+
+ return true;
+}
+
+uint32_t tu_edpt_stream_write_xfer(tu_edpt_stream_t* s)
+{
+ // skip if no data
+ TU_VERIFY( tu_fifo_count(&s->ff), 0 );
+
+ // Claim the endpoint
+ TU_VERIFY( stream_claim(s), 0 );
+
+ // Pull data from FIFO -> EP buf
+ uint16_t const count = tu_fifo_read_n(&s->ff, s->ep_buf, s->ep_bufsize);
+
+ if ( count )
+ {
+ TU_ASSERT( stream_xfer(s, count), 0 );
+ return count;
+ }else
+ {
+ // Release endpoint since we don't make any transfer
+ // Note: data is dropped if terminal is not connected
+ stream_release(s);
+ return 0;
+ }
+}
+
+uint32_t tu_edpt_stream_write(tu_edpt_stream_t* s, void const *buffer, uint32_t bufsize)
+{
+ TU_VERIFY(bufsize); // TODO support ZLP
+
+ uint16_t ret = tu_fifo_write_n(&s->ff, buffer, (uint16_t) bufsize);
+
+ // flush if fifo has more than packet size or
+ // in rare case: fifo depth is configured too small (which never reach packet size)
+ if ( (tu_fifo_count(&s->ff) >= s->ep_packetsize) || (tu_fifo_depth(&s->ff) < s->ep_packetsize) )
+ {
+ tu_edpt_stream_write_xfer(s);
+ }
+
+ return ret;
+}
+
+//--------------------------------------------------------------------+
+// Stream Read
+//--------------------------------------------------------------------+
+
+uint32_t tu_edpt_stream_read_xfer(tu_edpt_stream_t* s)
+{
+ uint16_t available = tu_fifo_remaining(&s->ff);
+
+ // Prepare for incoming data but only allow what we can store in the ring buffer.
+ // TODO Actually we can still carry out the transfer, keeping count of received bytes
+ // and slowly move it to the FIFO when read().
+ // This pre-check reduces endpoint claiming
+ TU_VERIFY(available >= s->ep_packetsize);
+
+ // claim endpoint
+ TU_VERIFY(stream_claim(s), 0);
+
+ // get available again since fifo can be changed before endpoint is claimed
+ available = tu_fifo_remaining(&s->ff);
+
+ if ( available >= s->ep_packetsize )
+ {
+ // multiple of packet size limit by ep bufsize
+ uint16_t count = (uint16_t) (available & ~(s->ep_packetsize -1));
+ count = tu_min16(count, s->ep_bufsize);
+
+ TU_ASSERT( stream_xfer(s, count), 0 );
+
+ return count;
+ }else
+ {
+ // Release endpoint since we don't make any transfer
+ stream_release(s);
+ return 0;
+ }
+}
+
+uint32_t tu_edpt_stream_read(tu_edpt_stream_t* s, void* buffer, uint32_t bufsize)
+{
+ uint32_t num_read = tu_fifo_read_n(&s->ff, buffer, (uint16_t) bufsize);
+ tu_edpt_stream_read_xfer(s);
+ return num_read;
+}
+
+//--------------------------------------------------------------------+
+// Debug
+//--------------------------------------------------------------------+
+
#if CFG_TUSB_DEBUG
#include
diff --git a/src/tusb_option.h b/src/tusb_option.h
index f95ae6273..67377b7ec 100644
--- a/src/tusb_option.h
+++ b/src/tusb_option.h
@@ -160,6 +160,9 @@ typedef int make_iso_compilers_happy;
// Allwinner
#define OPT_MCU_F1C100S 2100 ///< Allwinner F1C100s family
+// WCH
+#define OPT_MCU_CH32V307 2200 ///< WCH CH32V307
+
// Helper to check if configured MCU is one of listed
// Apply _TU_CHECK_MCU with || as separator to list of input
#define _TU_CHECK_MCU(_m) (CFG_TUSB_MCU == _m)
@@ -256,6 +259,10 @@ typedef int make_iso_compilers_happy;
// For backward compatible
#define TUSB_OPT_HOST_ENABLED CFG_TUH_ENABLED
+// highspeed support indicator
+#define TUH_OPT_HIGH_SPEED (CFG_TUH_MAX_SPEED ? (CFG_TUH_MAX_SPEED & OPT_MODE_HIGH_SPEED) : TUP_RHPORT_HIGHSPEED)
+
+
//--------------------------------------------------------------------+
// TODO move later
//--------------------------------------------------------------------+
@@ -299,9 +306,6 @@ typedef int make_iso_compilers_happy;
#define CFG_TUSB_OS_INC_PATH
#endif
-// mutex is only needed for RTOS TODO also required with multiple core MCUs
-#define TUSB_OPT_MUTEX (CFG_TUSB_OS != OPT_OS_NONE)
-
//--------------------------------------------------------------------
// Device Options (Default)
//--------------------------------------------------------------------
diff --git a/test/fuzz/dcd_fuzz.cc b/test/fuzz/dcd_fuzz.cc
new file mode 100644
index 000000000..7153e20f0
--- /dev/null
+++ b/test/fuzz/dcd_fuzz.cc
@@ -0,0 +1,208 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Nathaniel Brough
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+#include "device/dcd.h"
+#include "fuzz/fuzz_private.h"
+#include
+#include
+#include
+
+#define UNUSED(x) (void)(x)
+
+//--------------------------------------------------------------------+
+// State tracker
+//--------------------------------------------------------------------+
+struct State {
+ bool interrupts_enabled;
+ bool sof_enabled;
+ uint8_t address;
+};
+
+static State state = {false, 0, 0};
+
+//--------------------------------------------------------------------+
+// Controller API
+// All no-ops as we are fuzzing.
+//--------------------------------------------------------------------+
+extern "C" {
+void dcd_init(uint8_t rhport) {
+ UNUSED(rhport);
+ return;
+}
+
+void dcd_int_handler(uint8_t rhport) {
+ assert(_fuzz_data_provider.has_value());
+
+ if (!state.interrupts_enabled) {
+ return;
+ }
+
+ // Choose if we want to generate a signal based on the fuzzed data.
+ if (_fuzz_data_provider->ConsumeBool()) {
+ dcd_event_bus_signal(
+ rhport,
+ // Choose a random event based on the fuzz data.
+ (dcd_eventid_t)_fuzz_data_provider->ConsumeIntegralInRange(
+ DCD_EVENT_INVALID + 1, DCD_EVENT_COUNT - 1),
+ // Identify trigger as either an interrupt or a syncrhonous call
+ // depending on fuzz data.
+ _fuzz_data_provider->ConsumeBool());
+ }
+
+ if (_fuzz_data_provider->ConsumeBool()) {
+ constexpr size_t kSetupFrameLength = 8;
+ std::vector setup =
+ _fuzz_data_provider->ConsumeBytes(kSetupFrameLength);
+ // Fuzz consumer may return less than requested. If this is the case
+ // we want to make sure that at least that length is allocated and available
+ // to the signal handler.
+ if (setup.size() != kSetupFrameLength) {
+ setup.resize(kSetupFrameLength);
+ }
+ dcd_event_setup_received(rhport, setup.data(),
+ // Identify trigger as either an interrupt or a
+ // syncrhonous call depending on fuzz data.
+ _fuzz_data_provider->ConsumeBool());
+ }
+}
+
+void dcd_int_enable(uint8_t rhport) {
+ state.interrupts_enabled = true;
+ UNUSED(rhport);
+ return;
+}
+
+void dcd_int_disable(uint8_t rhport) {
+ state.interrupts_enabled = false;
+ UNUSED(rhport);
+ return;
+}
+
+void dcd_set_address(uint8_t rhport, uint8_t dev_addr) {
+ UNUSED(rhport);
+ state.address = dev_addr;
+ // Respond with status.
+ dcd_edpt_xfer(rhport, tu_edpt_addr(0, TUSB_DIR_IN), NULL, 0);
+ return;
+}
+
+void dcd_remote_wakeup(uint8_t rhport) {
+ UNUSED(rhport);
+ return;
+}
+
+void dcd_connect(uint8_t rhport) {
+ UNUSED(rhport);
+ return;
+}
+
+void dcd_disconnect(uint8_t rhport) {
+ UNUSED(rhport);
+ return;
+}
+
+void dcd_sof_enable(uint8_t rhport, bool en) {
+ state.sof_enabled = en;
+ UNUSED(rhport);
+ return;
+}
+
+//--------------------------------------------------------------------+
+// Endpoint API
+//--------------------------------------------------------------------+
+
+// Configure endpoint's registers according to descriptor
+bool dcd_edpt_open(uint8_t rhport, tusb_desc_endpoint_t const *desc_ep) {
+ UNUSED(rhport);
+ UNUSED(desc_ep);
+ return _fuzz_data_provider->ConsumeBool();
+}
+
+// Close all non-control endpoints, cancel all pending transfers if any.
+// Invoked when switching from a non-zero Configuration by SET_CONFIGURE
+// therefore required for multiple configuration support.
+void dcd_edpt_close_all(uint8_t rhport) {
+ UNUSED(rhport);
+ return;
+}
+
+// Close an endpoint.
+// Since it is weak, caller must TU_ASSERT this function's existence before
+// calling it.
+void dcd_edpt_close(uint8_t rhport, uint8_t ep_addr) {
+ UNUSED(rhport);
+ UNUSED(ep_addr);
+ return;
+}
+
+// Submit a transfer, When complete dcd_event_xfer_complete() is invoked to
+// notify the stack
+bool dcd_edpt_xfer(uint8_t rhport, uint8_t ep_addr, uint8_t *buffer,
+ uint16_t total_bytes) {
+ UNUSED(rhport);
+ UNUSED(buffer);
+ UNUSED(total_bytes);
+
+ uint8_t const dir = tu_edpt_dir(ep_addr);
+
+ if (dir == TUSB_DIR_IN) {
+ std::vector temp =
+ _fuzz_data_provider->ConsumeBytes(total_bytes);
+ std::copy(temp.begin(), temp.end(), buffer);
+ }
+ // Ignore output data as it's not useful for fuzzing without a more
+ // complex fuzzed backend. But we need to make sure it's not
+ // optimised out.
+ volatile uint8_t *dont_optimise0 = buffer;
+ volatile uint16_t dont_optimise1 = total_bytes;
+ UNUSED(dont_optimise0);
+ UNUSED(dont_optimise1);
+
+
+ return _fuzz_data_provider->ConsumeBool();
+}
+
+/* TODO: implement a fuzzed version of this.
+bool dcd_edpt_xfer_fifo(uint8_t rhport, uint8_t ep_addr, tu_fifo_t *ff,
+ uint16_t total_bytes) {}
+*/
+
+// Stall endpoint, any queuing transfer should be removed from endpoint
+void dcd_edpt_stall(uint8_t rhport, uint8_t ep_addr) {
+
+ UNUSED(rhport);
+ UNUSED(ep_addr);
+ return;
+}
+
+// clear stall, data toggle is also reset to DATA0
+// This API never calls with control endpoints, since it is auto cleared when
+// receiving setup packet
+void dcd_edpt_clear_stall(uint8_t rhport, uint8_t ep_addr) {
+
+ UNUSED(rhport);
+ UNUSED(ep_addr);
+ return;
+}
+}
\ No newline at end of file
diff --git a/test/fuzz/device/cdc/CMakeLists.txt b/test/fuzz/device/cdc/CMakeLists.txt
new file mode 100644
index 000000000..fa6e83b7e
--- /dev/null
+++ b/test/fuzz/device/cdc/CMakeLists.txt
@@ -0,0 +1,29 @@
+cmake_minimum_required(VERSION 3.5)
+
+include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)
+
+# gets PROJECT name for the example (e.g. -)
+family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR})
+
+project(${PROJECT})
+
+# Checks this example is valid for the family and initializes the project
+family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})
+
+add_executable(${PROJECT})
+
+# Example source
+target_sources(${PROJECT} PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/msc_disk.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c
+ )
+
+# Example include
+target_include_directories(${PROJECT} PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/src
+ )
+
+# Configure compilation flags and libraries for the example... see the corresponding function
+# in hw/bsp/FAMILY/family.cmake for details.
+family_configure_device_example(${PROJECT})
\ No newline at end of file
diff --git a/test/fuzz/device/cdc/Makefile b/test/fuzz/device/cdc/Makefile
new file mode 100644
index 000000000..ee51936b5
--- /dev/null
+++ b/test/fuzz/device/cdc/Makefile
@@ -0,0 +1,12 @@
+include ../../../../tools/top.mk
+include ../../make.mk
+
+INC += \
+ src \
+ $(TOP)/hw \
+
+# Example source
+SRC_C += $(addprefix $(CURRENT_PATH)/, $(wildcard src/*.c))
+SRC_CXX += $(addprefix $(CURRENT_PATH)/, $(wildcard src/*.cc))
+
+include ../../rules.mk
diff --git a/test/fuzz/device/cdc/cdc_seed_corpus.zip b/test/fuzz/device/cdc/cdc_seed_corpus.zip
new file mode 100644
index 000000000..81bc2240d
Binary files /dev/null and b/test/fuzz/device/cdc/cdc_seed_corpus.zip differ
diff --git a/test/fuzz/device/cdc/src/fuzz.cc b/test/fuzz/device/cdc/src/fuzz.cc
new file mode 100644
index 000000000..d40483ca6
--- /dev/null
+++ b/test/fuzz/device/cdc/src/fuzz.cc
@@ -0,0 +1,174 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Nathaniel Brough
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include
+#include
+#include
+#include
+#include
+
+#include "class/cdc/cdc_device.h"
+#include "fuzz/fuzz.h"
+#include "tusb.h"
+#include
+#include
+#include
+
+extern "C" {
+
+#define FUZZ_ITERATIONS 500
+
+//--------------------------------------------------------------------+
+// MACRO CONSTANT TYPEDEF PROTYPES
+//--------------------------------------------------------------------+
+
+void cdc_task(FuzzedDataProvider *provider);
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
+ FuzzedDataProvider provider(Data, Size);
+ std::vector callback_data = provider.ConsumeBytes(
+ provider.ConsumeIntegralInRange(0, Size));
+ fuzz_init(callback_data.data(), callback_data.size());
+ // init device stack on configured roothub port
+ tud_init(BOARD_TUD_RHPORT);
+
+ for (int i = 0; i < FUZZ_ITERATIONS; i++) {
+ if (provider.remaining_bytes() == 0) {
+ return 0;
+ }
+ tud_int_handler(provider.ConsumeIntegral());
+ tud_task(); // tinyusb device task
+ cdc_task(&provider);
+ }
+
+ return 0;
+}
+
+//--------------------------------------------------------------------+
+// USB CDC
+//--------------------------------------------------------------------+
+enum CdcApiFuncs {
+ kCdcNConnected,
+ kCdcNGetLineState,
+ kCdcNGetLineCoding,
+ kCdcNSetWantedChar,
+ kCdcNAvailable,
+ kCdcNRead,
+ kCdcNReadChar,
+ kCdcNReadFlush,
+ kCdcNPeek,
+ kCdcNWrite,
+ kCdcNWriteChar,
+ kCdcNWriteStr,
+ kCdcNWriteFlush,
+ kCdcNWriteAvailable,
+ kCdcNWriteClear,
+ // We don't need to fuzz tud_cdc_* as they are just wrappers
+ // calling with n==0.
+ kMaxValue,
+};
+
+void cdc_task(FuzzedDataProvider *provider) {
+
+ assert(provider != NULL);
+ const int kMaxBufferSize = 4096;
+ switch (provider->ConsumeEnum()) {
+ case kCdcNConnected:
+ // TODO: Fuzz interface number
+ (void)tud_cdc_n_connected(0);
+ break;
+ case kCdcNGetLineState:
+ // TODO: Fuzz interface number
+ (void)tud_cdc_n_get_line_state(0);
+ break;
+ case kCdcNGetLineCoding: {
+ cdc_line_coding_t coding;
+ // TODO: Fuzz interface number
+ (void)tud_cdc_n_get_line_coding(0, &coding);
+ } break;
+ case kCdcNSetWantedChar:
+ // TODO: Fuzz interface number
+ (void)tud_cdc_n_set_wanted_char(0, provider->ConsumeIntegral());
+ break;
+ case kCdcNAvailable:
+ // TODO: Fuzz interface number
+ (void)tud_cdc_n_available(0);
+ break;
+ case kCdcNRead: {
+ std::vector buffer;
+ buffer.resize(provider->ConsumeIntegralInRange(0, kMaxBufferSize));
+ // TODO: Fuzz interface number
+ (void)tud_cdc_n_read(0, buffer.data(), buffer.size());
+ break;
+ }
+ case kCdcNReadChar:
+ // TODO: Fuzz interface number
+ tud_cdc_n_read_char(0);
+ break;
+ case kCdcNReadFlush:
+ // TODO: Fuzz interface number
+ tud_cdc_n_read_flush(0);
+ break;
+ case kCdcNPeek: {
+ uint8_t peak = 0;
+ tud_cdc_n_peek(0, &peak);
+ break;
+ }
+ case kCdcNWrite: {
+ std::vector buffer = provider->ConsumeBytes(
+ provider->ConsumeIntegralInRange(0, kMaxBufferSize));
+
+ // TODO: Fuzz interface number
+ (void)tud_cdc_n_write(0, buffer.data(), buffer.size());
+ } break;
+
+case kCdcNWriteChar:
+ // TODO: Fuzz interface number
+ (void)tud_cdc_n_write_char(0, provider->ConsumeIntegral());
+ break;
+case kCdcNWriteStr: {
+ std::string str = provider->ConsumeRandomLengthString(kMaxBufferSize);
+ // TODO: Fuzz interface number
+ (void)tud_cdc_n_write_str(0, str.c_str());
+ break;
+}
+case kCdcNWriteFlush:
+ // TODO: Fuzz interface number
+ (void)tud_cdc_n_write_flush(0);
+ break;
+case kCdcNWriteAvailable:
+ // TODO: Fuzz interface number
+ (void)tud_cdc_n_write_available(0);
+ break;
+case kCdcNWriteClear:
+ // TODO: Fuzz interface number
+ (void)tud_cdc_n_write_clear(0);
+ break;
+case kMaxValue:
+ // Noop.
+ break;
+}
+}
+}
\ No newline at end of file
diff --git a/test/fuzz/device/cdc/src/tusb_config.h b/test/fuzz/device/cdc/src/tusb_config.h
new file mode 100644
index 000000000..10a8a825a
--- /dev/null
+++ b/test/fuzz/device/cdc/src/tusb_config.h
@@ -0,0 +1,114 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Nathaniel Brough
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#ifndef _TUSB_CONFIG_H_
+#define _TUSB_CONFIG_H_
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+//--------------------------------------------------------------------+
+// Board Specific Configuration
+//--------------------------------------------------------------------+
+
+// RHPort number used for device can be defined by board.mk, default to port 0
+#ifndef BOARD_TUD_RHPORT
+#define BOARD_TUD_RHPORT 0
+#endif
+
+// RHPort max operational speed can defined by board.mk
+#ifndef BOARD_TUD_MAX_SPEED
+#define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED
+#endif
+
+//--------------------------------------------------------------------
+// Common Configuration
+//--------------------------------------------------------------------
+
+// defined by compiler flags for flexibility
+#ifndef CFG_TUSB_MCU
+#error CFG_TUSB_MCU must be defined
+#endif
+
+#ifndef CFG_TUSB_OS
+#define CFG_TUSB_OS OPT_OS_NONE
+#endif
+
+#ifndef CFG_TUSB_DEBUG
+#define CFG_TUSB_DEBUG 0
+#endif
+
+// Enable Device stack
+#define CFG_TUD_ENABLED 1
+
+// Default is max speed that hardware controller could support with on-chip PHY
+#define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED
+
+/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
+ * Tinyusb use follows macros to declare transferring memory so that they can be put
+ * into those specific section.
+ * e.g
+ * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
+ * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
+ */
+#ifndef CFG_TUSB_MEM_SECTION
+#define CFG_TUSB_MEM_SECTION
+#endif
+
+#ifndef CFG_TUSB_MEM_ALIGN
+#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
+#endif
+
+//--------------------------------------------------------------------
+// DEVICE CONFIGURATION
+//--------------------------------------------------------------------
+
+#ifndef CFG_TUD_ENDPOINT0_SIZE
+#define CFG_TUD_ENDPOINT0_SIZE 64
+#endif
+
+//------------- CLASS -------------//
+#define CFG_TUD_CDC 1
+#define CFG_TUD_MSC 0
+#define CFG_TUD_HID 0
+#define CFG_TUD_MIDI 0
+#define CFG_TUD_VENDOR 0
+
+// CDC FIFO size of TX and RX
+#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+
+// CDC Endpoint transfer buffer size, more is faster
+#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+
+// MSC Buffer size of Device Mass storage
+#define CFG_TUD_MSC_EP_BUFSIZE 512
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif /* _TUSB_CONFIG_H_ */
diff --git a/test/fuzz/device/cdc/src/usb_descriptors.cc b/test/fuzz/device/cdc/src/usb_descriptors.cc
new file mode 100644
index 000000000..0f636f05d
--- /dev/null
+++ b/test/fuzz/device/cdc/src/usb_descriptors.cc
@@ -0,0 +1,229 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Nathaniel Brough
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "tusb.h"
+
+/* A combination of interfaces must have a unique product id, since PC will save
+ * device driver after the first plug.
+ * Auto ProductID layout's Bitmap:
+ * [MSB] HID | CDC [LSB]
+ */
+#define _PID_MAP(itf, n) ((CFG_TUD_##itf) << (n))
+#define USB_PID \
+ (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(HID, 2) | _PID_MAP(MIDI, 3) | \
+ _PID_MAP(VENDOR, 4))
+
+#define USB_VID 0xCafe
+#define USB_BCD 0x0200
+
+//--------------------------------------------------------------------+
+// Device Descriptors
+//--------------------------------------------------------------------+
+
+// Invoked when received GET DEVICE DESCRIPTOR
+// Application return pointer to descriptor
+uint8_t const *tud_descriptor_device_cb(void) {
+ static tusb_desc_device_t const desc_device = {
+ .bLength = sizeof(tusb_desc_device_t),
+ .bDescriptorType = TUSB_DESC_DEVICE,
+ .bcdUSB = USB_BCD,
+
+ // Use Interface Association Descriptor (IAD) for CDC
+ // As required by USB Specs IAD's subclass must be common class (2) and
+ // protocol must be IAD (1)
+ .bDeviceClass = TUSB_CLASS_MISC,
+ .bDeviceSubClass = MISC_SUBCLASS_COMMON,
+ .bDeviceProtocol = MISC_PROTOCOL_IAD,
+
+ .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
+
+ .idVendor = USB_VID,
+ .idProduct = USB_PID,
+ .bcdDevice = 0x0100,
+
+ .iManufacturer = 0x01,
+ .iProduct = 0x02,
+ .iSerialNumber = 0x03,
+
+ .bNumConfigurations = 0x01};
+
+ return (uint8_t const *)&desc_device;
+}
+
+//--------------------------------------------------------------------+
+// Configuration Descriptor
+//--------------------------------------------------------------------+
+
+enum { ITF_NUM_CDC = 0, ITF_NUM_CDC_DATA, ITF_NUM_TOTAL };
+
+#define EPNUM_CDC_NOTIF 0x81
+#define EPNUM_CDC_OUT 0x02
+#define EPNUM_CDC_IN 0x82
+
+#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
+
+// full speed configuration
+uint8_t const desc_fs_configuration[] = {
+ // Config number, interface count, string index, total length, attribute,
+ // power in mA
+ TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
+
+ // Interface number, string index, EP notification address and size, EP data
+ // address (out, in) and size.
+ TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT,
+ EPNUM_CDC_IN, 64),
+};
+
+#if TUD_OPT_HIGH_SPEED
+// Per USB specs: high speed capable device must report device_qualifier and
+// other_speed_configuration
+
+// high speed configuration
+uint8_t const desc_hs_configuration[] = {
+ // Config number, interface count, string index, total length, attribute,
+ // power in mA
+ TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
+
+ // Interface number, string index, EP notification address and size, EP data
+ // address (out, in) and size.
+ TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT,
+ EPNUM_CDC_IN, 512),
+};
+
+// other speed configuration
+uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN];
+
+// device qualifier is mostly similar to device descriptor since we don't change
+// configuration based on speed
+tusb_desc_device_qualifier_t const desc_device_qualifier = {
+ .bLength = sizeof(tusb_desc_device_qualifier_t),
+ .bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER,
+ .bcdUSB = USB_BCD,
+
+ .bDeviceClass = TUSB_CLASS_MISC,
+ .bDeviceSubClass = MISC_SUBCLASS_COMMON,
+ .bDeviceProtocol = MISC_PROTOCOL_IAD,
+
+ .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
+ .bNumConfigurations = 0x01,
+ .bReserved = 0x00};
+
+// Invoked when received GET DEVICE QUALIFIER DESCRIPTOR request
+// Application return pointer to descriptor, whose contents must exist long
+// enough for transfer to complete. device_qualifier descriptor describes
+// information about a high-speed capable device that would change if the device
+// were operating at the other speed. If not highspeed capable stall this
+// request.
+uint8_t const *tud_descriptor_device_qualifier_cb(void) {
+ return (uint8_t const *)&desc_device_qualifier;
+}
+
+// Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request
+// Application return pointer to descriptor, whose contents must exist long
+// enough for transfer to complete Configuration descriptor in the other speed
+// e.g if high speed then this is for full speed and vice versa
+uint8_t const *tud_descriptor_other_speed_configuration_cb(uint8_t index) {
+ (void)index; // for multiple configurations
+
+ // if link speed is high return fullspeed config, and vice versa
+ // Note: the descriptor type is OHER_SPEED_CONFIG instead of CONFIG
+ memcpy(desc_other_speed_config,
+ (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_fs_configuration
+ : desc_hs_configuration,
+ CONFIG_TOTAL_LEN);
+
+ desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG;
+
+ return desc_other_speed_config;
+}
+
+#endif // highspeed
+
+// Invoked when received GET CONFIGURATION DESCRIPTOR
+// Application return pointer to descriptor
+// Descriptor contents must exist long enough for transfer to complete
+uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
+ (void)index; // for multiple configurations
+
+#if TUD_OPT_HIGH_SPEED
+ // Although we are highspeed, host may be fullspeed.
+ return (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_hs_configuration
+ : desc_fs_configuration;
+#else
+ return desc_fs_configuration;
+#endif
+}
+
+//--------------------------------------------------------------------+
+// String Descriptors
+//--------------------------------------------------------------------+
+
+// array of pointer to string descriptors
+char const *string_desc_arr[] = {
+ (const char[]){0x09, 0x04}, // 0: is supported language is English (0x0409)
+ "TinyUSB", // 1: Manufacturer
+ "TinyUSB Device", // 2: Product
+ "123456789012", // 3: Serials, should use chip ID
+ "TinyUSB CDC", // 4: CDC Interface
+};
+
+static uint16_t _desc_str[32];
+
+// Invoked when received GET STRING DESCRIPTOR request
+// Application return pointer to descriptor, whose contents must exist long
+// enough for transfer to complete
+uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
+ (void)langid;
+
+ uint8_t chr_count;
+
+ if (index == 0) {
+ memcpy(&_desc_str[1], string_desc_arr[0], 2);
+ chr_count = 1;
+ } else {
+ // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
+ // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
+
+ if (!(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])))
+ return NULL;
+
+ const char *str = string_desc_arr[index];
+
+ // Cap at max char
+ chr_count = (uint8_t)strlen(str);
+ if (chr_count > 31)
+ chr_count = 31;
+
+ // Convert ASCII string into UTF-16
+ for (uint8_t i = 0; i < chr_count; i++) {
+ _desc_str[1 + i] = str[i];
+ }
+ }
+
+ // first byte is length (including header), second byte is string type
+ _desc_str[0] = (uint16_t)((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
+
+ return _desc_str;
+}
diff --git a/test/fuzz/device/msc/CMakeLists.txt b/test/fuzz/device/msc/CMakeLists.txt
new file mode 100644
index 000000000..fa6e83b7e
--- /dev/null
+++ b/test/fuzz/device/msc/CMakeLists.txt
@@ -0,0 +1,29 @@
+cmake_minimum_required(VERSION 3.5)
+
+include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)
+
+# gets PROJECT name for the example (e.g. -)
+family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR})
+
+project(${PROJECT})
+
+# Checks this example is valid for the family and initializes the project
+family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})
+
+add_executable(${PROJECT})
+
+# Example source
+target_sources(${PROJECT} PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/msc_disk.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c
+ )
+
+# Example include
+target_include_directories(${PROJECT} PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/src
+ )
+
+# Configure compilation flags and libraries for the example... see the corresponding function
+# in hw/bsp/FAMILY/family.cmake for details.
+family_configure_device_example(${PROJECT})
\ No newline at end of file
diff --git a/test/fuzz/device/msc/Makefile b/test/fuzz/device/msc/Makefile
new file mode 100644
index 000000000..ee51936b5
--- /dev/null
+++ b/test/fuzz/device/msc/Makefile
@@ -0,0 +1,12 @@
+include ../../../../tools/top.mk
+include ../../make.mk
+
+INC += \
+ src \
+ $(TOP)/hw \
+
+# Example source
+SRC_C += $(addprefix $(CURRENT_PATH)/, $(wildcard src/*.c))
+SRC_CXX += $(addprefix $(CURRENT_PATH)/, $(wildcard src/*.cc))
+
+include ../../rules.mk
diff --git a/test/fuzz/device/msc/msc_seed_corpus.zip b/test/fuzz/device/msc/msc_seed_corpus.zip
new file mode 100644
index 000000000..81bc2240d
Binary files /dev/null and b/test/fuzz/device/msc/msc_seed_corpus.zip differ
diff --git a/test/fuzz/device/msc/src/fuzz.cc b/test/fuzz/device/msc/src/fuzz.cc
new file mode 100644
index 000000000..568900452
--- /dev/null
+++ b/test/fuzz/device/msc/src/fuzz.cc
@@ -0,0 +1,62 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Nathaniel Brough
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include
+#include
+#include
+#include
+#include
+
+#include "class/cdc/cdc_device.h"
+#include "fuzz/fuzz.h"
+#include "tusb.h"
+#include
+#include
+#include
+
+//--------------------------------------------------------------------+
+// MACRO CONSTANT TYPEDEF PROTYPES
+//--------------------------------------------------------------------+
+#define FUZZ_ITERATIONS 500
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
+ FuzzedDataProvider provider(Data, Size);
+ std::vector callback_data = provider.ConsumeBytes(
+ provider.ConsumeIntegralInRange(0, Size));
+ fuzz_init(callback_data.data(), callback_data.size());
+ // init device stack on configured roothub port
+ tud_init(BOARD_TUD_RHPORT);
+
+ for (int i = 0; i < FUZZ_ITERATIONS; i++) {
+ if (provider.remaining_bytes() == 0) {
+ return 0;
+ }
+ tud_int_handler(provider.ConsumeIntegral());
+ tud_task(); // tinyusb device task
+ }
+
+ return 0;
+}
+
diff --git a/test/fuzz/device/msc/src/tusb_config.h b/test/fuzz/device/msc/src/tusb_config.h
new file mode 100644
index 000000000..ca39c6b0a
--- /dev/null
+++ b/test/fuzz/device/msc/src/tusb_config.h
@@ -0,0 +1,114 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Nathaniel Brough
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#ifndef _TUSB_CONFIG_H_
+#define _TUSB_CONFIG_H_
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+//--------------------------------------------------------------------+
+// Board Specific Configuration
+//--------------------------------------------------------------------+
+
+// RHPort number used for device can be defined by board.mk, default to port 0
+#ifndef BOARD_TUD_RHPORT
+#define BOARD_TUD_RHPORT 0
+#endif
+
+// RHPort max operational speed can defined by board.mk
+#ifndef BOARD_TUD_MAX_SPEED
+#define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED
+#endif
+
+//--------------------------------------------------------------------
+// Common Configuration
+//--------------------------------------------------------------------
+
+// defined by compiler flags for flexibility
+#ifndef CFG_TUSB_MCU
+#error CFG_TUSB_MCU must be defined
+#endif
+
+#ifndef CFG_TUSB_OS
+#define CFG_TUSB_OS OPT_OS_NONE
+#endif
+
+#ifndef CFG_TUSB_DEBUG
+#define CFG_TUSB_DEBUG 0
+#endif
+
+// Enable Device stack
+#define CFG_TUD_ENABLED 1
+
+// Default is max speed that hardware controller could support with on-chip PHY
+#define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED
+
+/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
+ * Tinyusb use follows macros to declare transferring memory so that they can be put
+ * into those specific section.
+ * e.g
+ * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
+ * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
+ */
+#ifndef CFG_TUSB_MEM_SECTION
+#define CFG_TUSB_MEM_SECTION
+#endif
+
+#ifndef CFG_TUSB_MEM_ALIGN
+#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
+#endif
+
+//--------------------------------------------------------------------
+// DEVICE CONFIGURATION
+//--------------------------------------------------------------------
+
+#ifndef CFG_TUD_ENDPOINT0_SIZE
+#define CFG_TUD_ENDPOINT0_SIZE 64
+#endif
+
+//------------- CLASS -------------//
+#define CFG_TUD_CDC 0
+#define CFG_TUD_MSC 1
+#define CFG_TUD_HID 0
+#define CFG_TUD_MIDI 0
+#define CFG_TUD_VENDOR 0
+
+// CDC FIFO size of TX and RX
+#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+
+// CDC Endpoint transfer buffer size, more is faster
+#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+
+// MSC Buffer size of Device Mass storage
+#define CFG_TUD_MSC_EP_BUFSIZE 512
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif /* _TUSB_CONFIG_H_ */
diff --git a/test/fuzz/device/msc/src/usb_descriptors.cc b/test/fuzz/device/msc/src/usb_descriptors.cc
new file mode 100644
index 000000000..ded401fc9
--- /dev/null
+++ b/test/fuzz/device/msc/src/usb_descriptors.cc
@@ -0,0 +1,224 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Nathaniel Brough
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "tusb.h"
+
+/* A combination of interfaces must have a unique product id, since PC will save
+ * device driver after the first plug.
+ * Auto ProductID layout's Bitmap:
+ * [MSB] HID | MSC | CDC [LSB]
+ */
+#define _PID_MAP(itf, n) ((CFG_TUD_##itf) << (n))
+#define USB_PID \
+ (0x4000 | _PID_MAP(MSC, 0) | _PID_MAP(HID, 1) | _PID_MAP(MIDI, 2) | \
+ _PID_MAP(VENDOR, 3))
+#define USB_VID 0xCafe
+#define USB_BCD 0x0200
+
+//--------------------------------------------------------------------+
+// Device Descriptors
+//--------------------------------------------------------------------+
+
+// Invoked when received GET DEVICE DESCRIPTOR
+// Application return pointer to descriptor
+uint8_t const *tud_descriptor_device_cb(void) {
+ static tusb_desc_device_t const desc_device = {
+ .bLength = sizeof(tusb_desc_device_t),
+ .bDescriptorType = TUSB_DESC_DEVICE,
+ .bcdUSB = USB_BCD,
+
+ // Use Interface Association Descriptor (IAD) for CDC
+ // As required by USB Specs IAD's subclass must be common class (2) and
+ // protocol must be IAD (1)
+ .bDeviceClass = TUSB_CLASS_MISC,
+ .bDeviceSubClass = MISC_SUBCLASS_COMMON,
+ .bDeviceProtocol = MISC_PROTOCOL_IAD,
+
+ .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
+
+ .idVendor = USB_VID,
+ .idProduct = USB_PID,
+ .bcdDevice = 0x0100,
+
+ .iManufacturer = 0x01,
+ .iProduct = 0x02,
+ .iSerialNumber = 0x03,
+
+ .bNumConfigurations = 0x01};
+
+ return (uint8_t const *)&desc_device;
+}
+
+//--------------------------------------------------------------------+
+// Configuration Descriptor
+//--------------------------------------------------------------------+
+
+enum { ITF_NUM_MSC = 0, ITF_NUM_TOTAL };
+
+#define EPNUM_MSC_OUT 0x05
+#define EPNUM_MSC_IN 0x85
+
+#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_MSC_DESC_LEN)
+
+// full speed configuration
+uint8_t const desc_fs_configuration[] = {
+ // Config number, interface count, string index, total length, attribute,
+ // power in mA
+ TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
+
+ // Interface number, string index, EP Out & EP In address, EP size
+ TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 4, EPNUM_MSC_OUT, EPNUM_MSC_IN, 64),
+};
+
+#if TUD_OPT_HIGH_SPEED
+// Per USB specs: high speed capable device must report device_qualifier and
+// other_speed_configuration
+
+// high speed configuration
+uint8_t const desc_hs_configuration[] = {
+ // Config number, interface count, string index, total length, attribute,
+ // power in mA
+ TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
+
+ // Interface number, string index, EP Out & EP In address, EP size
+ TUD_MSC_DESCRIPTOR(ITF_NUM_MSC, 4, EPNUM_MSC_OUT, EPNUM_MSC_IN, 512),
+};
+
+// other speed configuration
+uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN];
+
+// device qualifier is mostly similar to device descriptor since we don't change
+// configuration based on speed
+tusb_desc_device_qualifier_t const desc_device_qualifier = {
+ .bLength = sizeof(tusb_desc_device_qualifier_t),
+ .bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER,
+ .bcdUSB = USB_BCD,
+
+ .bDeviceClass = TUSB_CLASS_MISC,
+ .bDeviceSubClass = MISC_SUBCLASS_COMMON,
+ .bDeviceProtocol = MISC_PROTOCOL_IAD,
+
+ .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
+ .bNumConfigurations = 0x01,
+ .bReserved = 0x00};
+
+// Invoked when received GET DEVICE QUALIFIER DESCRIPTOR request
+// Application return pointer to descriptor, whose contents must exist long
+// enough for transfer to complete. device_qualifier descriptor describes
+// information about a high-speed capable device that would change if the device
+// were operating at the other speed. If not highspeed capable stall this
+// request.
+uint8_t const *tud_descriptor_device_qualifier_cb(void) {
+ return (uint8_t const *)&desc_device_qualifier;
+}
+
+// Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request
+// Application return pointer to descriptor, whose contents must exist long
+// enough for transfer to complete Configuration descriptor in the other speed
+// e.g if high speed then this is for full speed and vice versa
+uint8_t const *tud_descriptor_other_speed_configuration_cb(uint8_t index) {
+ (void)index; // for multiple configurations
+
+ // if link speed is high return fullspeed config, and vice versa
+ // Note: the descriptor type is OHER_SPEED_CONFIG instead of CONFIG
+ memcpy(desc_other_speed_config,
+ (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_fs_configuration
+ : desc_hs_configuration,
+ CONFIG_TOTAL_LEN);
+
+ desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG;
+
+ return desc_other_speed_config;
+}
+
+#endif // highspeed
+
+// Invoked when received GET CONFIGURATION DESCRIPTOR
+// Application return pointer to descriptor
+// Descriptor contents must exist long enough for transfer to complete
+uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
+ (void)index; // for multiple configurations
+
+#if TUD_OPT_HIGH_SPEED
+ // Although we are highspeed, host may be fullspeed.
+ return (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_hs_configuration
+ : desc_fs_configuration;
+#else
+ return desc_fs_configuration;
+#endif
+}
+
+//--------------------------------------------------------------------+
+// String Descriptors
+//--------------------------------------------------------------------+
+
+// array of pointer to string descriptors
+char const *string_desc_arr[] = {
+ (const char[]){0x09, 0x04}, // 0: is supported language is English (0x0409)
+ "TinyUSB", // 1: Manufacturer
+ "TinyUSB Device", // 2: Product
+ "123456789012", // 3: Serials, should use chip ID
+ "TinyUSB MSC", // 4: MSC Interface
+
+};
+
+static uint16_t _desc_str[32];
+
+// Invoked when received GET STRING DESCRIPTOR request
+// Application return pointer to descriptor, whose contents must exist long
+// enough for transfer to complete
+uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
+ (void)langid;
+
+ uint8_t chr_count;
+
+ if (index == 0) {
+ memcpy(&_desc_str[1], string_desc_arr[0], 2);
+ chr_count = 1;
+ } else {
+ // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
+ // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
+
+ if (!(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])))
+ return NULL;
+
+ const char *str = string_desc_arr[index];
+
+ // Cap at max char
+ chr_count = (uint8_t)strlen(str);
+ if (chr_count > 31)
+ chr_count = 31;
+
+ // Convert ASCII string into UTF-16
+ for (uint8_t i = 0; i < chr_count; i++) {
+ _desc_str[1 + i] = str[i];
+ }
+ }
+
+ // first byte is length (including header), second byte is string type
+ _desc_str[0] = (uint16_t)((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
+
+ return _desc_str;
+}
diff --git a/test/fuzz/device/net/CMakeLists.txt b/test/fuzz/device/net/CMakeLists.txt
new file mode 100644
index 000000000..fa6e83b7e
--- /dev/null
+++ b/test/fuzz/device/net/CMakeLists.txt
@@ -0,0 +1,29 @@
+cmake_minimum_required(VERSION 3.5)
+
+include(${CMAKE_CURRENT_SOURCE_DIR}/../../../hw/bsp/family_support.cmake)
+
+# gets PROJECT name for the example (e.g. -)
+family_get_project_name(PROJECT ${CMAKE_CURRENT_LIST_DIR})
+
+project(${PROJECT})
+
+# Checks this example is valid for the family and initializes the project
+family_initialize_project(${PROJECT} ${CMAKE_CURRENT_LIST_DIR})
+
+add_executable(${PROJECT})
+
+# Example source
+target_sources(${PROJECT} PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/main.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/msc_disk.c
+ ${CMAKE_CURRENT_SOURCE_DIR}/src/usb_descriptors.c
+ )
+
+# Example include
+target_include_directories(${PROJECT} PUBLIC
+ ${CMAKE_CURRENT_SOURCE_DIR}/src
+ )
+
+# Configure compilation flags and libraries for the example... see the corresponding function
+# in hw/bsp/FAMILY/family.cmake for details.
+family_configure_device_example(${PROJECT})
\ No newline at end of file
diff --git a/test/fuzz/device/net/Makefile b/test/fuzz/device/net/Makefile
new file mode 100644
index 000000000..22241fcdc
--- /dev/null
+++ b/test/fuzz/device/net/Makefile
@@ -0,0 +1,71 @@
+DEPS_SUBMODULES += lib/lwip
+
+include ../../../../tools/top.mk
+include ../../make.mk
+
+# suppress warning caused by lwip
+CFLAGS += \
+ -Wno-error=null-dereference \
+ -Wno-error=unused-parameter \
+ -Wno-error=unused-variable
+
+INC += \
+ src \
+ $(TOP)/hw \
+ $(TOP)/lib/lwip/src/include \
+ $(TOP)/lib/lwip/src/include/ipv4 \
+ $(TOP)/lib/lwip/src/include/lwip/apps \
+ $(TOP)/lib/networking
+
+# Example source
+SRC_C += $(addprefix $(CURRENT_PATH)/, $(wildcard src/*.c))
+SRC_CXX += $(addprefix $(CURRENT_PATH)/, $(wildcard src/*.cc))
+
+# lwip sources
+SRC_C += \
+ lib/lwip/src/core/altcp.c \
+ lib/lwip/src/core/altcp_alloc.c \
+ lib/lwip/src/core/altcp_tcp.c \
+ lib/lwip/src/core/def.c \
+ lib/lwip/src/core/dns.c \
+ lib/lwip/src/core/inet_chksum.c \
+ lib/lwip/src/core/init.c \
+ lib/lwip/src/core/ip.c \
+ lib/lwip/src/core/mem.c \
+ lib/lwip/src/core/memp.c \
+ lib/lwip/src/core/netif.c \
+ lib/lwip/src/core/pbuf.c \
+ lib/lwip/src/core/raw.c \
+ lib/lwip/src/core/stats.c \
+ lib/lwip/src/core/sys.c \
+ lib/lwip/src/core/tcp.c \
+ lib/lwip/src/core/tcp_in.c \
+ lib/lwip/src/core/tcp_out.c \
+ lib/lwip/src/core/timeouts.c \
+ lib/lwip/src/core/udp.c \
+ lib/lwip/src/core/ipv4/autoip.c \
+ lib/lwip/src/core/ipv4/dhcp.c \
+ lib/lwip/src/core/ipv4/etharp.c \
+ lib/lwip/src/core/ipv4/icmp.c \
+ lib/lwip/src/core/ipv4/igmp.c \
+ lib/lwip/src/core/ipv4/ip4.c \
+ lib/lwip/src/core/ipv4/ip4_addr.c \
+ lib/lwip/src/core/ipv4/ip4_frag.c \
+ lib/lwip/src/core/ipv6/dhcp6.c \
+ lib/lwip/src/core/ipv6/ethip6.c \
+ lib/lwip/src/core/ipv6/icmp6.c \
+ lib/lwip/src/core/ipv6/inet6.c \
+ lib/lwip/src/core/ipv6/ip6.c \
+ lib/lwip/src/core/ipv6/ip6_addr.c \
+ lib/lwip/src/core/ipv6/ip6_frag.c \
+ lib/lwip/src/core/ipv6/mld6.c \
+ lib/lwip/src/core/ipv6/nd6.c \
+ lib/lwip/src/netif/ethernet.c \
+ lib/lwip/src/netif/slipif.c \
+ lib/lwip/src/apps/http/httpd.c \
+ lib/lwip/src/apps/http/fs.c \
+ lib/networking/dhserver.c \
+ lib/networking/dnserver.c \
+ lib/networking/rndis_reports.c
+
+include ../../rules.mk
diff --git a/test/fuzz/device/net/src/arch/cc.h b/test/fuzz/device/net/src/arch/cc.h
new file mode 100644
index 000000000..56a0cacf7
--- /dev/null
+++ b/test/fuzz/device/net/src/arch/cc.h
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
+ * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
+ * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * This file is part of the lwIP TCP/IP stack.
+ *
+ * Author: Adam Dunkels
+ *
+ */
+#ifndef __CC_H__
+#define __CC_H__
+
+//#include "cpu.h"
+
+typedef int sys_prot_t;
+
+
+
+/* define compiler specific symbols */
+#if defined (__ICCARM__)
+
+#define PACK_STRUCT_BEGIN
+#define PACK_STRUCT_STRUCT
+#define PACK_STRUCT_END
+#define PACK_STRUCT_FIELD(x) x
+#define PACK_STRUCT_USE_INCLUDES
+
+#elif defined (__CC_ARM)
+
+#define PACK_STRUCT_BEGIN __packed
+#define PACK_STRUCT_STRUCT
+#define PACK_STRUCT_END
+#define PACK_STRUCT_FIELD(x) x
+
+#elif defined (__GNUC__)
+
+#define PACK_STRUCT_BEGIN
+#define PACK_STRUCT_STRUCT __attribute__ ((__packed__))
+#define PACK_STRUCT_END
+#define PACK_STRUCT_FIELD(x) x
+
+#elif defined (__TASKING__)
+
+#define PACK_STRUCT_BEGIN
+#define PACK_STRUCT_STRUCT
+#define PACK_STRUCT_END
+#define PACK_STRUCT_FIELD(x) x
+
+#endif
+
+#define LWIP_PLATFORM_ASSERT(x) do { if(!(x)) while(1); } while(0)
+
+#endif /* __CC_H__ */
diff --git a/test/fuzz/device/net/src/fuzz.cc b/test/fuzz/device/net/src/fuzz.cc
new file mode 100644
index 000000000..ea4943665
--- /dev/null
+++ b/test/fuzz/device/net/src/fuzz.cc
@@ -0,0 +1,99 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Nathaniel Brough
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include
+#include
+#include
+#include
+#include
+
+#include "class/cdc/cdc_device.h"
+#include "class/net/net_device.h"
+#include "fuzz/fuzz.h"
+#include "tusb.h"
+#include
+#include
+#include
+
+extern "C" {
+
+#define FUZZ_ITERATIONS 500
+
+//--------------------------------------------------------------------+
+// MACRO CONSTANT TYPEDEF PROTYPES
+//--------------------------------------------------------------------+
+
+void net_task(FuzzedDataProvider *provider);
+
+extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
+ FuzzedDataProvider provider(Data, Size);
+ std::vector callback_data = provider.ConsumeBytes(
+ provider.ConsumeIntegralInRange(0, Size));
+ fuzz_init(callback_data.data(), callback_data.size());
+ // init device stack on configured roothub port
+ tud_init(BOARD_TUD_RHPORT);
+
+ for (int i = 0; i < FUZZ_ITERATIONS; i++) {
+ if (provider.remaining_bytes() == 0) {
+ return 0;
+ }
+ tud_int_handler(provider.ConsumeIntegral());
+ tud_task(); // tinyusb device task
+ net_task(&provider);
+ }
+
+ return 0;
+}
+
+//--------------------------------------------------------------------+
+// USB CDC
+//--------------------------------------------------------------------+
+enum NetApiFuncs {
+ kNetworkRecvRenew,
+ kNetworkCanXmit,
+ kNetworkXmit,
+ kMaxValue,
+};
+
+void net_task(FuzzedDataProvider *provider) {
+
+ assert(provider != NULL);
+ switch (provider->ConsumeEnum()) {
+
+ case kNetworkRecvRenew:
+ tud_network_recv_renew();
+ break;
+ case kNetworkCanXmit:
+ (void)tud_network_can_xmit(provider->ConsumeIntegral());
+ case kNetworkXmit:
+ // TODO: Actually pass real values here later.
+ tud_network_xmit(NULL, 0);
+
+ case kMaxValue:
+ // Noop.
+ break;
+ }
+}
+}
diff --git a/test/fuzz/device/net/src/lwipopts.h b/test/fuzz/device/net/src/lwipopts.h
new file mode 100644
index 000000000..a215017c7
--- /dev/null
+++ b/test/fuzz/device/net/src/lwipopts.h
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification,
+ * are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions and the following disclaimer in the documentation
+ * and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ * derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
+ * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
+ * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
+ * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
+ * OF SUCH DAMAGE.
+ *
+ * This file is part of the lwIP TCP/IP stack.
+ *
+ * Author: Simon Goldschmidt
+ *
+ */
+#ifndef __LWIPOPTS_H__
+#define __LWIPOPTS_H__
+
+/* Prevent having to link sys_arch.c (we don't test the API layers in unit tests) */
+#define NO_SYS 1
+#define MEM_ALIGNMENT 4
+#define LWIP_RAW 0
+#define LWIP_NETCONN 0
+#define LWIP_SOCKET 0
+#define LWIP_DHCP 0
+#define LWIP_ICMP 1
+#define LWIP_UDP 1
+#define LWIP_TCP 1
+#define LWIP_IPV4 1
+#define LWIP_IPV6 0
+#define ETH_PAD_SIZE 0
+#define LWIP_IP_ACCEPT_UDP_PORT(p) ((p) == PP_NTOHS(67))
+
+#define TCP_MSS (1500 /*mtu*/ - 20 /*iphdr*/ - 20 /*tcphhr*/)
+#define TCP_SND_BUF (2 * TCP_MSS)
+#define TCP_WND (TCP_MSS)
+
+#define ETHARP_SUPPORT_STATIC_ENTRIES 1
+
+#define LWIP_HTTPD_CGI 0
+#define LWIP_HTTPD_SSI 0
+#define LWIP_HTTPD_SSI_INCLUDE_TAG 0
+
+#define LWIP_SINGLE_NETIF 1
+
+#define PBUF_POOL_SIZE 2
+
+#define HTTPD_USE_CUSTOM_FSDATA 0
+
+#define LWIP_MULTICAST_PING 1
+#define LWIP_BROADCAST_PING 1
+#define LWIP_IPV6_MLD 0
+#define LWIP_IPV6_SEND_ROUTER_SOLICIT 0
+
+#endif /* __LWIPOPTS_H__ */
diff --git a/test/fuzz/device/net/src/tusb_config.h b/test/fuzz/device/net/src/tusb_config.h
new file mode 100644
index 000000000..6ad859337
--- /dev/null
+++ b/test/fuzz/device/net/src/tusb_config.h
@@ -0,0 +1,122 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Nathaniel Brough
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#ifndef _TUSB_CONFIG_H_
+#define _TUSB_CONFIG_H_
+
+#ifdef __cplusplus
+ extern "C" {
+#endif
+
+//--------------------------------------------------------------------+
+// Board Specific Configuration
+//--------------------------------------------------------------------+
+
+// RHPort number used for device can be defined by board.mk, default to port 0
+#ifndef BOARD_TUD_RHPORT
+#define BOARD_TUD_RHPORT 0
+#endif
+
+// RHPort max operational speed can defined by board.mk
+#ifndef BOARD_TUD_MAX_SPEED
+#define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED
+#endif
+
+//--------------------------------------------------------------------
+// Common Configuration
+//--------------------------------------------------------------------
+
+// defined by compiler flags for flexibility
+#ifndef CFG_TUSB_MCU
+#error CFG_TUSB_MCU must be defined
+#endif
+
+#ifndef CFG_TUSB_OS
+#define CFG_TUSB_OS OPT_OS_NONE
+#endif
+
+#ifndef CFG_TUSB_DEBUG
+#define CFG_TUSB_DEBUG 0
+#endif
+
+// Enable Device stack
+#define CFG_TUD_ENABLED 1
+
+// Default is max speed that hardware controller could support with on-chip PHY
+#define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED
+
+/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
+ * Tinyusb use follows macros to declare transferring memory so that they can be put
+ * into those specific section.
+ * e.g
+ * - CFG_TUSB_MEM SECTION : __attribute__ (( section(".usb_ram") ))
+ * - CFG_TUSB_MEM_ALIGN : __attribute__ ((aligned(4)))
+ */
+#ifndef CFG_TUSB_MEM_SECTION
+#define CFG_TUSB_MEM_SECTION
+#endif
+
+#ifndef CFG_TUSB_MEM_ALIGN
+#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))
+#endif
+
+//--------------------------------------------------------------------
+// DEVICE CONFIGURATION
+//--------------------------------------------------------------------
+
+#ifndef CFG_TUD_ENDPOINT0_SIZE
+#define CFG_TUD_ENDPOINT0_SIZE 64
+#endif
+
+//------------- CLASS -------------//
+#define CFG_TUD_CDC 1
+#define CFG_TUD_MSC 0
+#define CFG_TUD_HID 0
+#define CFG_TUD_MIDI 0
+#define CFG_TUD_VENDOR 0
+
+// Network class has 2 drivers: ECM/RNDIS and NCM.
+// Only one of the drivers can be enabled
+#define CFG_TUD_ECM_RNDIS 1
+#define CFG_TUD_NCM (1-CFG_TUD_ECM_RNDIS)
+
+// CDC FIFO size of TX and RX
+#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+
+// CDC Endpoint transfer buffer size, more is faster
+#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
+
+// MSC Buffer size of Device Mass storage
+#define CFG_TUD_MSC_EP_BUFSIZE 512
+
+
+
+
+#ifdef __cplusplus
+ }
+#endif
+
+#endif /* _TUSB_CONFIG_H_ */
diff --git a/test/fuzz/device/net/src/usb_descriptors.cc b/test/fuzz/device/net/src/usb_descriptors.cc
new file mode 100644
index 000000000..c600bd801
--- /dev/null
+++ b/test/fuzz/device/net/src/usb_descriptors.cc
@@ -0,0 +1,229 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Ha Thach (tinyusb.org)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "tusb.h"
+
+/* A combination of interfaces must have a unique product id, since PC will save
+ * device driver after the first plug.
+ * Auto ProductID layout's Bitmap:
+ * [MSB] HID | CDC [LSB]
+ */
+#define _PID_MAP(itf, n) ((CFG_TUD_##itf) << (n))
+#define USB_PID \
+ (0x4000 | _PID_MAP(CDC, 0) | _PID_MAP(HID, 2) | _PID_MAP(MIDI, 3) | \
+ _PID_MAP(VENDOR, 4))
+
+#define USB_VID 0xCafe
+#define USB_BCD 0x0200
+
+//--------------------------------------------------------------------+
+// Device Descriptors
+//--------------------------------------------------------------------+
+
+// Invoked when received GET DEVICE DESCRIPTOR
+// Application return pointer to descriptor
+uint8_t const *tud_descriptor_device_cb(void) {
+ static tusb_desc_device_t const desc_device = {
+ .bLength = sizeof(tusb_desc_device_t),
+ .bDescriptorType = TUSB_DESC_DEVICE,
+ .bcdUSB = USB_BCD,
+
+ // Use Interface Association Descriptor (IAD) for CDC
+ // As required by USB Specs IAD's subclass must be common class (2) and
+ // protocol must be IAD (1)
+ .bDeviceClass = TUSB_CLASS_MISC,
+ .bDeviceSubClass = MISC_SUBCLASS_COMMON,
+ .bDeviceProtocol = MISC_PROTOCOL_IAD,
+
+ .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
+
+ .idVendor = USB_VID,
+ .idProduct = USB_PID,
+ .bcdDevice = 0x0100,
+
+ .iManufacturer = 0x01,
+ .iProduct = 0x02,
+ .iSerialNumber = 0x03,
+
+ .bNumConfigurations = 0x01};
+
+ return (uint8_t const *)&desc_device;
+}
+
+//--------------------------------------------------------------------+
+// Configuration Descriptor
+//--------------------------------------------------------------------+
+
+enum { ITF_NUM_CDC = 0, ITF_NUM_CDC_DATA, ITF_NUM_TOTAL };
+
+#define EPNUM_CDC_NOTIF 0x81
+#define EPNUM_CDC_OUT 0x02
+#define EPNUM_CDC_IN 0x82
+
+#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + TUD_CDC_DESC_LEN)
+
+// full speed configuration
+uint8_t const desc_fs_configuration[] = {
+ // Config number, interface count, string index, total length, attribute,
+ // power in mA
+ TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
+
+ // Interface number, string index, EP notification address and size, EP data
+ // address (out, in) and size.
+ TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT,
+ EPNUM_CDC_IN, 64),
+};
+
+#if TUD_OPT_HIGH_SPEED
+// Per USB specs: high speed capable device must report device_qualifier and
+// other_speed_configuration
+
+// high speed configuration
+uint8_t const desc_hs_configuration[] = {
+ // Config number, interface count, string index, total length, attribute,
+ // power in mA
+ TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 100),
+
+ // Interface number, string index, EP notification address and size, EP data
+ // address (out, in) and size.
+ TUD_CDC_DESCRIPTOR(ITF_NUM_CDC, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT,
+ EPNUM_CDC_IN, 512),
+};
+
+// other speed configuration
+uint8_t desc_other_speed_config[CONFIG_TOTAL_LEN];
+
+// device qualifier is mostly similar to device descriptor since we don't change
+// configuration based on speed
+tusb_desc_device_qualifier_t const desc_device_qualifier = {
+ .bLength = sizeof(tusb_desc_device_qualifier_t),
+ .bDescriptorType = TUSB_DESC_DEVICE_QUALIFIER,
+ .bcdUSB = USB_BCD,
+
+ .bDeviceClass = TUSB_CLASS_MISC,
+ .bDeviceSubClass = MISC_SUBCLASS_COMMON,
+ .bDeviceProtocol = MISC_PROTOCOL_IAD,
+
+ .bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
+ .bNumConfigurations = 0x01,
+ .bReserved = 0x00};
+
+// Invoked when received GET DEVICE QUALIFIER DESCRIPTOR request
+// Application return pointer to descriptor, whose contents must exist long
+// enough for transfer to complete. device_qualifier descriptor describes
+// information about a high-speed capable device that would change if the device
+// were operating at the other speed. If not highspeed capable stall this
+// request.
+uint8_t const *tud_descriptor_device_qualifier_cb(void) {
+ return (uint8_t const *)&desc_device_qualifier;
+}
+
+// Invoked when received GET OTHER SEED CONFIGURATION DESCRIPTOR request
+// Application return pointer to descriptor, whose contents must exist long
+// enough for transfer to complete Configuration descriptor in the other speed
+// e.g if high speed then this is for full speed and vice versa
+uint8_t const *tud_descriptor_other_speed_configuration_cb(uint8_t index) {
+ (void)index; // for multiple configurations
+
+ // if link speed is high return fullspeed config, and vice versa
+ // Note: the descriptor type is OHER_SPEED_CONFIG instead of CONFIG
+ memcpy(desc_other_speed_config,
+ (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_fs_configuration
+ : desc_hs_configuration,
+ CONFIG_TOTAL_LEN);
+
+ desc_other_speed_config[1] = TUSB_DESC_OTHER_SPEED_CONFIG;
+
+ return desc_other_speed_config;
+}
+
+#endif // highspeed
+
+// Invoked when received GET CONFIGURATION DESCRIPTOR
+// Application return pointer to descriptor
+// Descriptor contents must exist long enough for transfer to complete
+uint8_t const *tud_descriptor_configuration_cb(uint8_t index) {
+ (void)index; // for multiple configurations
+
+#if TUD_OPT_HIGH_SPEED
+ // Although we are highspeed, host may be fullspeed.
+ return (tud_speed_get() == TUSB_SPEED_HIGH) ? desc_hs_configuration
+ : desc_fs_configuration;
+#else
+ return desc_fs_configuration;
+#endif
+}
+
+//--------------------------------------------------------------------+
+// String Descriptors
+//--------------------------------------------------------------------+
+
+// array of pointer to string descriptors
+char const *string_desc_arr[] = {
+ (const char[]){0x09, 0x04}, // 0: is supported language is English (0x0409)
+ "TinyUSB", // 1: Manufacturer
+ "TinyUSB Device", // 2: Product
+ "123456789012", // 3: Serials, should use chip ID
+ "TinyUSB CDC", // 4: CDC Interface
+};
+
+static uint16_t _desc_str[32];
+
+// Invoked when received GET STRING DESCRIPTOR request
+// Application return pointer to descriptor, whose contents must exist long
+// enough for transfer to complete
+uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
+ (void)langid;
+
+ uint8_t chr_count;
+
+ if (index == 0) {
+ memcpy(&_desc_str[1], string_desc_arr[0], 2);
+ chr_count = 1;
+ } else {
+ // Note: the 0xEE index string is a Microsoft OS 1.0 Descriptors.
+ // https://docs.microsoft.com/en-us/windows-hardware/drivers/usbcon/microsoft-defined-usb-descriptors
+
+ if (!(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])))
+ return NULL;
+
+ const char *str = string_desc_arr[index];
+
+ // Cap at max char
+ chr_count = (uint8_t)strlen(str);
+ if (chr_count > 31)
+ chr_count = 31;
+
+ // Convert ASCII string into UTF-16
+ for (uint8_t i = 0; i < chr_count; i++) {
+ _desc_str[1 + i] = str[i];
+ }
+ }
+
+ // first byte is length (including header), second byte is string type
+ _desc_str[0] = (uint16_t)((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
+
+ return _desc_str;
+}
diff --git a/test/fuzz/dicts/cdc.dict b/test/fuzz/dicts/cdc.dict
new file mode 100644
index 000000000..138775cdc
--- /dev/null
+++ b/test/fuzz/dicts/cdc.dict
@@ -0,0 +1,74 @@
+# List of supported OIDs
+RNDIS_OID_GEN_SUPPORTED_LIST="\x00\x01\x01\x01"
+# Hardware status
+RNDIS_OID_GEN_HARDWARE_STATUS="\x00\x01\x01\x02"
+# Media types supported (encoded)
+RNDIS_OID_GEN_MEDIA_SUPPORTED="\x00\x01\x01\x03"
+# Media types in use (encoded)
+RNDIS_OID_GEN_MEDIA_IN_USE="\x00\x01\x01\x04"
+RNDIS_OID_GEN_MAXIMUM_LOOKAHEAD="\x00\x01\x01\x05"
+# Maximum frame size in bytes
+RNDIS_OID_GEN_MAXIMUM_FRAME_SIZE="\x00\x01\x01\x06"
+# Link speed in units of 100 bps
+RNDIS_OID_GEN_LINK_SPEED="\x00\x01\x01\x07"
+# Transmit buffer space
+RNDIS_OID_GEN_TRANSMIT_BUFFER_SPACE="\x00\x01\x01\x08"
+# Receive buffer space
+RNDIS_OID_GEN_RECEIVE_BUFFER_SPACE="\x00\x01\x01\x09"
+# NDIS version number used by the driver
+RNDIS_OID_GEN_DRIVER_VERSION="\x00\x01\x01\x10"
+# Maximum total packet length in bytes
+RNDIS_OID_GEN_MAXIMUM_TOTAL_SIZE="\x00\x01\x01\x11"
+# Optional protocol flags (encoded)
+RNDIS_OID_GEN_PROTOCOL_OPTIONS="\x00\x01\x01\x12"
+# Optional NIC flags (encoded)
+RNDIS_OID_GEN_MAC_OPTIONS="\x00\x01\x01\x13"
+# Whether the NIC is connected to the network
+RNDIS_OID_GEN_MEDIA_CONNECT_STATUS="\x00\x01\x01\x14"
+# The maximum number of send packets the driver can accept per call to its MiniportSendPacketsfunction
+RNDIS_OID_GEN_MAXIMUM_SEND_PACKETS="\x00\x01\x01\x15"
+# Vendor-assigned version number of the driver
+RNDIS_OID_GEN_VENDOR_DRIVER_VERSION="\x00\x01\x01\x16"
+# The custom GUIDs (Globally Unique Identifier) supported by the miniport driver
+RNDIS_OID_GEN_SUPPORTED_GUIDS="\x00\x01\x01\x17"
+# List of network-layer addresses associated with the binding between a transport and the driver
+RNDIS_OID_GEN_NETWORK_LAYER_ADDRESSES="\x00\x01\x01\x18"
+# Size of packets' additional headers
+RNDIS_OID_GEN_TRANSPORT_HEADER_OFFSET="\x00\x01\x01\x19"
+RNDIS_OID_GEN_MEDIA_CAPABILITIES="\x00\x01\x02\x01"
+# Physical media supported by the miniport driver (encoded)
+RNDIS_OID_GEN_PHYSICAL_MEDIUM="\x00\x01\x02\x02"
+# Permanent station address
+RNDIS_OID_802_3_PERMANENT_ADDRESS="\x01\x01\x01\x01"
+# Current station address
+RNDIS_OID_802_3_CURRENT_ADDRESS="\x01\x01\x01\x02"
+# Current multicast address list
+RNDIS_OID_802_3_MULTICAST_LIST="\x01\x01\x01\x03"
+# Maximum size of multicast address list
+RNDIS_OID_802_3_MAXIMUM_LIST_SIZE="\x01\x01\x01\x04"
+# Directed packets. Directed packets contain a destination address equal to the station address of the NIC.
+RNDIS_PACKET_TYPE_DIRECTED="\x00\x00\x00\x01"
+# Multicast address packets sent to addresses in the multicast address list.
+RNDIS_PACKET_TYPE_MULTICAST="\x00\x00\x00\x02"
+# All multicast address packets, not just the ones enumerated in the multicast address list.
+RNDIS_PACKET_TYPE_ALL_MULTICAST="\x00\x00\x00\x04"
+# Broadcast packets.
+RNDIS_PACKET_TYPE_BROADCAST="\x00\x00\x00\x08"
+# All source routing packets. If the protocol driver sets this bit, the NDIS library attempts to act as a source routing bridge.
+RNDIS_PACKET_TYPE_SOURCE_ROUTING="\x00\x00\x00\x10"
+# Specifies all packets regardless of whether VLAN filtering is enabled or not and whether the VLAN identifier matches or not.
+RNDIS_PACKET_TYPE_PROMISCUOUS="\x00\x00\x00\x20"
+# SMT packets that an FDDI NIC receives.
+RNDIS_PACKET_TYPE_SMT="\x00\x00\x00\x40"
+# All packets sent by installed protocols and all packets indicated by the NIC that is identified by a given NdisBindingHandle.
+RNDIS_PACKET_TYPE_ALL_LOCAL="\x00\x00\x00\x80"
+# Packets sent to the current group address.
+RNDIS_PACKET_TYPE_GROUP="\x00\x00\x10\x00"
+# All functional address packets, not just the ones in the current functional address.
+RNDIS_PACKET_TYPE_ALL_FUNCTIONAL="\x00\x00\x20\x00"
+# Functional address packets sent to addresses included in the current functional address.
+RNDIS_PACKET_TYPE_FUNCTIONAL="\x00\x00\x40\x00"
+# NIC driver frames that a Token Ring NIC receives.
+RNDIS_PACKET_TYPE_MAC_FRAME="\x00\x00\x80\x00"
+RNDIS_PACKET_TYPE_NO_LOCAL="\x00\x01\x00\x00"
+
diff --git a/test/fuzz/fuzz.cc b/test/fuzz/fuzz.cc
new file mode 100644
index 000000000..2e4186f1a
--- /dev/null
+++ b/test/fuzz/fuzz.cc
@@ -0,0 +1,34 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Nathaniel Brough
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "fuzzer/FuzzedDataProvider.h"
+#include
+
+std::optional _fuzz_data_provider;
+
+extern "C" int fuzz_init(const uint8_t *data, size_t size) {
+ _fuzz_data_provider.emplace(data, size);
+ return 0;
+}
diff --git a/test/fuzz/fuzz.h b/test/fuzz/fuzz.h
new file mode 100644
index 000000000..6aa4949a1
--- /dev/null
+++ b/test/fuzz/fuzz.h
@@ -0,0 +1,37 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Nathaniel Brough
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#pragma once
+#include
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int fuzz_init(const uint8_t *data, size_t size);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/test/fuzz/fuzz_private.h b/test/fuzz/fuzz_private.h
new file mode 100644
index 000000000..0d791fcdc
--- /dev/null
+++ b/test/fuzz/fuzz_private.h
@@ -0,0 +1,30 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Nathaniel Brough
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#pragma once
+#include "fuzzer/FuzzedDataProvider.h"
+#include
+
+extern std::optional _fuzz_data_provider;
diff --git a/test/fuzz/make.mk b/test/fuzz/make.mk
new file mode 100644
index 000000000..6717ebc80
--- /dev/null
+++ b/test/fuzz/make.mk
@@ -0,0 +1,108 @@
+# ---------------------------------------
+# Common make definition for all examples
+# ---------------------------------------
+
+# Build directory
+BUILD := _build
+PROJECT := $(notdir $(CURDIR))
+
+# Handy check parameter function
+check_defined = \
+ $(strip $(foreach 1,$1, \
+ $(call __check_defined,$1,$(strip $(value 2)))))
+__check_defined = \
+ $(if $(value $1),, \
+ $(error Undefined make flag: $1$(if $2, ($2))))
+
+#-------------- Fuzz harness compiler ------------
+
+CC ?= clang
+CXX ?= clang++
+GDB ?= gdb
+OBJCOPY = objcopy
+SIZE = size
+MKDIR = mkdir
+
+ifeq ($(CMDEXE),1)
+ CP = copy
+ RM = del
+ PYTHON = python
+else
+ SED = sed
+ CP = cp
+ RM = rm
+ PYTHON = python3
+endif
+
+#-------------- Fuzz harness flags ------------
+COVERAGE_FLAGS ?= -fsanitize-coverage=trace-pc-guard
+SANITIZER_FLAGS ?= -fsanitize=fuzzer \
+ -fsanitize=address
+
+CFLAGS += $(COVERAGE_FLAGS) $(SANITIZER_FLAGS)
+
+#-------------- Source files and compiler flags --------------
+
+
+INC += $(TOP)/test
+
+# Compiler Flags
+CFLAGS += \
+ -ggdb \
+ -fdata-sections \
+ -ffunction-sections \
+ -fno-strict-aliasing \
+ -Wall \
+ -Wextra \
+ -Werror \
+ -Wfatal-errors \
+ -Wdouble-promotion \
+ -Wstrict-prototypes \
+ -Wstrict-overflow \
+ -Werror-implicit-function-declaration \
+ -Wfloat-equal \
+ -Wundef \
+ -Wshadow \
+ -Wwrite-strings \
+ -Wsign-compare \
+ -Wmissing-format-attribute \
+ -Wunreachable-code \
+ -Wcast-align \
+ -Wcast-qual \
+ -Wnull-dereference \
+ -Wuninitialized \
+ -Wunused \
+ -Wredundant-decls \
+ -O1
+
+CFLAGS += \
+ -Wno-error=unreachable-code \
+ -DOPT_MCU_FUZZ=1 \
+ -DCFG_TUSB_MCU=OPT_MCU_FUZZ
+
+CXXFLAGS += \
+ -xc++ \
+ -Wno-c++11-narrowing \
+ -fno-implicit-templates
+
+# conversion is too strict for most mcu driver, may be disable sign/int/arith-conversion
+# -Wconversion
+
+# Debugging/Optimization
+ifeq ($(DEBUG), 1)
+ CFLAGS += -Og
+else
+ CFLAGS += $(CFLAGS_OPTIMIZED)
+endif
+
+# Log level is mapped to TUSB DEBUG option
+ifneq ($(LOG),)
+ CMAKE_DEFSYM += -DLOG=$(LOG)
+ CFLAGS += -DCFG_TUSB_DEBUG=$(LOG)
+endif
+
+# Logger: default is uart, can be set to rtt or swo
+ifneq ($(LOGGER),)
+ CMAKE_DEFSYM += -DLOGGER=$(LOGGER)
+endif
+
diff --git a/test/fuzz/msc_fuzz.cc b/test/fuzz/msc_fuzz.cc
new file mode 100644
index 000000000..e906ca971
--- /dev/null
+++ b/test/fuzz/msc_fuzz.cc
@@ -0,0 +1,162 @@
+#include "fuzz/fuzz_private.h"
+#include "tusb.h"
+#include
+#include
+#include
+
+#if CFG_TUD_MSC==1
+
+// Whether host does safe eject.
+// tud_msc_get_maxlun_cb returns a uint8_t so the max logical units that are
+// allowed is 255, so we need to keep track of 255 fuzzed logical units.
+static std::array::max()> ejected = {false};
+
+extern "C" {
+// Invoked when received SCSI_CMD_INQUIRY
+// Application fill vendor id, product id and revision with string up to 8, 16,
+// 4 characters respectively
+void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8],
+ uint8_t product_id[16], uint8_t product_rev[4]) {
+ (void)lun;
+ assert(_fuzz_data_provider.has_value());
+
+ std::string vid = _fuzz_data_provider->ConsumeBytesAsString(8);
+ std::string pid = _fuzz_data_provider->ConsumeBytesAsString(16);
+ std::string rev = _fuzz_data_provider->ConsumeBytesAsString(4);
+
+ memcpy(vendor_id, vid.c_str(), strlen(vid.c_str()));
+ memcpy(product_id, pid.c_str(), strlen(pid.c_str()));
+ memcpy(product_rev, rev.c_str(), strlen(rev.c_str()));
+}
+
+// Invoked when received Test Unit Ready command.
+// return true allowing host to read/write this LUN e.g SD card inserted
+bool tud_msc_test_unit_ready_cb(uint8_t lun) {
+ // RAM disk is ready until ejected
+ if (ejected[lun]) {
+ // Additional Sense 3A-00 is NOT_FOUND
+ tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
+ return false;
+ }
+
+ return _fuzz_data_provider->ConsumeBool();
+}
+
+// Invoked when received SCSI_CMD_READ_CAPACITY_10 and
+// SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size Application update
+// block count and block size
+void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count,
+ uint16_t *block_size) {
+ (void)lun;
+ *block_count = _fuzz_data_provider->ConsumeIntegral();
+ *block_size = _fuzz_data_provider->ConsumeIntegral();
+}
+
+// Invoked when received Start Stop Unit command
+// - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
+// - Start = 1 : active mode, if load_eject = 1 : load disk storage
+bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start,
+ bool load_eject) {
+ (void)power_condition;
+ assert(_fuzz_data_provider.has_value());
+
+ if (load_eject) {
+ if (start) {
+ // load disk storage
+ } else {
+ // unload disk storage
+ ejected[lun] = true;
+ }
+ }
+
+ return _fuzz_data_provider->ConsumeBool();
+}
+
+// Callback invoked when received READ10 command.
+// Copy disk's data to buffer (up to bufsize) and return number of copied bytes.
+int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset,
+ void *buffer, uint32_t bufsize) {
+ assert(_fuzz_data_provider.has_value());
+ (void)lun;
+ (void)lba;
+ (void)offset;
+
+ std::vector consumed_buffer = _fuzz_data_provider->ConsumeBytes(
+ _fuzz_data_provider->ConsumeIntegralInRange(0, bufsize));
+ memcpy(buffer, consumed_buffer.data(), consumed_buffer.size());
+
+ // Sometimes return an error code;
+ if (_fuzz_data_provider->ConsumeBool()) {
+ return _fuzz_data_provider->ConsumeIntegralInRange(
+ std::numeric_limits::min(), -1);
+ }
+
+ return consumed_buffer.size();
+}
+
+bool tud_msc_is_writable_cb(uint8_t lun) {
+ assert(_fuzz_data_provider.has_value());
+ (void)lun;
+ return _fuzz_data_provider->ConsumeBool();
+}
+
+// Callback invoked when received WRITE10 command.
+// Process data in buffer to disk's storage and return number of written bytes
+int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset,
+ uint8_t *buffer, uint32_t bufsize) {
+ // Ignore these as they are outputs and don't affect the return value.
+ (void)lun;
+ (void)lba;
+ (void)offset;
+ (void)buffer;
+ assert(_fuzz_data_provider.has_value());
+
+ // -ve error codes -> bufsize.
+ return _fuzz_data_provider->ConsumeIntegralInRange(
+ std::numeric_limits::min(), bufsize);
+}
+
+// Callback invoked when received an SCSI command not in built-in list below
+// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
+// - READ10 and WRITE10 has their own callbacks
+int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer,
+ uint16_t bufsize) {
+ (void)buffer;
+ (void)bufsize;
+ assert(_fuzz_data_provider.has_value());
+
+ switch (scsi_cmd[0]) {
+ case SCSI_CMD_TEST_UNIT_READY:
+ break;
+ case SCSI_CMD_INQUIRY:
+ break;
+ case SCSI_CMD_MODE_SELECT_6:
+ break;
+ case SCSI_CMD_MODE_SENSE_6:
+ break;
+ case SCSI_CMD_START_STOP_UNIT:
+ break;
+ case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
+ break;
+ case SCSI_CMD_READ_CAPACITY_10:
+ break;
+ case SCSI_CMD_REQUEST_SENSE:
+ break;
+ case SCSI_CMD_READ_FORMAT_CAPACITY:
+ break;
+ case SCSI_CMD_READ_10:
+ break;
+ case SCSI_CMD_WRITE_10:
+ break;
+ default:
+ // Set Sense = Invalid Command Operation
+ tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
+ return _fuzz_data_provider->ConsumeIntegralInRange(
+ std::numeric_limits::min(), -1);
+ }
+
+ return 0;
+}
+}
+
+#endif
\ No newline at end of file
diff --git a/test/fuzz/net_fuzz.cc b/test/fuzz/net_fuzz.cc
new file mode 100644
index 000000000..8467d6d8e
--- /dev/null
+++ b/test/fuzz/net_fuzz.cc
@@ -0,0 +1,82 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Nathaniel Brough
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "tusb_config.h"
+#if defined(CFG_TUD_ECM_RNDIS) || defined(CFG_TUD_NCM)
+
+#include "class/net/net_device.h"
+#include "fuzz_private.h"
+#include
+#include
+#include
+#include "lwip/sys.h"
+
+extern "C" {
+bool tud_network_recv_cb(const uint8_t *src, uint16_t size) {
+ assert(_fuzz_data_provider.has_value());
+ (void)src;
+ (void)size;
+ return _fuzz_data_provider->ConsumeBool();
+}
+
+// client must provide this: copy from network stack packet pointer to dst
+uint16_t tud_network_xmit_cb(uint8_t *dst, void *ref, uint16_t arg) {
+ (void)ref;
+ (void)arg;
+
+ assert(_fuzz_data_provider.has_value());
+
+ uint16_t size = _fuzz_data_provider->ConsumeIntegral();
+ std::vector temp = _fuzz_data_provider->ConsumeBytes(size);
+ memcpy(dst, temp.data(), temp.size());
+ return size;
+}
+
+/* lwip has provision for using a mutex, when applicable */
+sys_prot_t sys_arch_protect(void) { return 0; }
+void sys_arch_unprotect(sys_prot_t pval) { (void)pval; }
+
+//------------- ECM/RNDIS -------------//
+
+// client must provide this: initialize any network state back to the beginning
+void tud_network_init_cb(void) {
+ // NoOp.
+}
+
+// client must provide this: 48-bit MAC address
+// TODO removed later since it is not part of tinyusb stack
+const uint8_t tud_network_mac_address[6] = {0};
+
+//------------- NCM -------------//
+
+// callback to client providing optional indication of internal state of network
+// driver
+void tud_network_link_state_cb(bool state) {
+ (void)state;
+ // NoOp.
+}
+}
+
+#endif
\ No newline at end of file
diff --git a/test/fuzz/rules.mk b/test/fuzz/rules.mk
new file mode 100644
index 000000000..d9c2a1a77
--- /dev/null
+++ b/test/fuzz/rules.mk
@@ -0,0 +1,161 @@
+# ---------------------------------------
+# Common make rules for all examples
+# ---------------------------------------
+
+# Set all as default goal
+.DEFAULT_GOAL := all
+
+# ---------------------------------------
+# Compiler Flags
+# ---------------------------------------
+
+LIBS_GCC ?= -lm
+
+# libc
+LIBS += $(LIBS_GCC)
+
+ifneq ($(BOARD), spresense)
+LIBS += -lc -Wl,-Bstatic -lc++ -Wl,-Bdynamic
+endif
+
+# TinyUSB Stack source
+SRC_C += \
+ src/tusb.c \
+ src/common/tusb_fifo.c \
+ src/device/usbd.c \
+ src/device/usbd_control.c \
+ src/class/audio/audio_device.c \
+ src/class/cdc/cdc_device.c \
+ src/class/dfu/dfu_device.c \
+ src/class/dfu/dfu_rt_device.c \
+ src/class/hid/hid_device.c \
+ src/class/midi/midi_device.c \
+ src/class/msc/msc_device.c \
+ src/class/net/ecm_rndis_device.c \
+ src/class/net/ncm_device.c \
+ src/class/usbtmc/usbtmc_device.c \
+ src/class/video/video_device.c \
+ src/class/vendor/vendor_device.c
+
+
+# Fuzzers are c++
+SRC_CXX += \
+ test/fuzz/dcd_fuzz.cc \
+ test/fuzz/fuzz.cc \
+ test/fuzz/msc_fuzz.cc \
+ test/fuzz/net_fuzz.cc \
+ test/fuzz/usbd_fuzz.cc
+
+# TinyUSB stack include
+INC += $(TOP)/src
+
+CFLAGS += $(addprefix -I,$(INC))
+CXXFLAGS += -std=c++17
+
+# LTO makes it difficult to analyze map file for optimizing size purpose
+# We will run this option in ci
+ifeq ($(NO_LTO),1)
+CFLAGS := $(filter-out -flto,$(CFLAGS))
+endif
+
+ifneq ($(LD_FILE),)
+LDFLAGS_LD_FILE ?= -Wl,-T,$(TOP)/$(LD_FILE)
+endif
+
+LDFLAGS += $(CFLAGS) $(LDFLAGS_LD_FILE) -fuse-ld=lld -Wl,-Map=$@.map -Wl,--cref -Wl,-gc-sections
+ifneq ($(SKIP_NANOLIB), 1)
+endif
+
+ASFLAGS += $(CFLAGS)
+
+# Assembly files can be name with upper case .S, convert it to .s
+SRC_S := $(SRC_S:.S=.s)
+
+# Due to GCC LTO bug https://bugs.launchpad.net/gcc-arm-embedded/+bug/1747966
+# assembly file should be placed first in linking order
+# '_asm' suffix is added to object of assembly file
+OBJ += $(addprefix $(BUILD)/obj/, $(SRC_S:.s=_asm.o))
+OBJ += $(addprefix $(BUILD)/obj/, $(SRC_C:.c=.o))
+OBJ += $(addprefix $(BUILD)/obj/, $(SRC_CXX:.cc=_cxx.o))
+
+# Verbose mode
+ifeq ("$(V)","1")
+$(info CFLAGS $(CFLAGS) ) $(info )
+$(info LDFLAGS $(LDFLAGS)) $(info )
+$(info ASFLAGS $(ASFLAGS)) $(info )
+endif
+
+# ---------------------------------------
+# Rules
+# ---------------------------------------
+
+all: $(BUILD)/$(PROJECT)
+
+OBJ_DIRS = $(sort $(dir $(OBJ)))
+$(OBJ): | $(OBJ_DIRS)
+$(OBJ_DIRS):
+ifeq ($(CMDEXE),1)
+ @$(MKDIR) $(subst /,\,$@)
+else
+ @$(MKDIR) -p $@
+endif
+
+$(BUILD)/$(PROJECT): $(OBJ)
+ @echo LINK $@
+ @ $(CXX) -o $@ $(LIB_FUZZING_ENGINE) $^ $(LIBS) $(LDFLAGS)
+
+# We set vpath to point to the top of the tree so that the source files
+# can be located. By following this scheme, it allows a single build rule
+# to be used to compile all .c files.
+vpath %.c . $(TOP)
+$(BUILD)/obj/%.o: %.c
+ @echo CC $(notdir $@)
+ @$(CC) $(CFLAGS) -c -MD -o $@ $<
+
+# All cpp srcs
+vpath %.cc . $(TOP)
+$(BUILD)/obj/%_cxx.o: %.cc
+ @echo CXX $(notdir $@)
+ @$(CXX) $(CFLAGS) $(CXXFLAGS) -c -MD -o $@ $<
+
+# ASM sources lower case .s
+vpath %.s . $(TOP)
+$(BUILD)/obj/%_asm.o: %.s
+ @echo AS $(notdir $@)
+ @$(CC) -x assembler-with-cpp $(ASFLAGS) -c -o $@ $<
+
+# ASM sources upper case .S
+vpath %.S . $(TOP)
+$(BUILD)/obj/%_asm.o: %.S
+ @echo AS $(notdir $@)
+ @$(CC) -x assembler-with-cpp $(ASFLAGS) -c -o $@ $<
+
+.PHONY: clean
+clean:
+ifeq ($(CMDEXE),1)
+ rd /S /Q $(subst /,\,$(BUILD))
+else
+ $(RM) -rf $(BUILD)
+endif
+# ---------------- GNU Make End -----------------------
+
+# get depenecies
+.PHONY: get-deps
+get-deps:
+ ifdef DEPS_SUBMODULES
+ git -C $(TOP) submodule update --init $(DEPS_SUBMODULES)
+ endif
+
+size: $(BUILD)/$(PROJECT)
+ -@echo ''
+ @$(SIZE) $<
+ -@echo ''
+
+# linkermap must be install previously at https://github.com/hathach/linkermap
+linkermap: $(BUILD)/$(PROJECT)
+ @linkermap -v $<.map
+
+# Print out the value of a make variable.
+# https://stackoverflow.com/questions/16467718/how-to-print-out-a-variable-in-makefile
+print-%:
+ @echo $* = $($*)
\ No newline at end of file
diff --git a/test/fuzz/usbd_fuzz.cc b/test/fuzz/usbd_fuzz.cc
new file mode 100644
index 000000000..45c5fd7ea
--- /dev/null
+++ b/test/fuzz/usbd_fuzz.cc
@@ -0,0 +1,73 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2022 Nathaniel Brough
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ *
+ */
+
+#include "fuzz/fuzz_private.h"
+#include "tusb.h"
+// #include "usb_descriptors.h"
+
+#ifndef CFG_FUZZ_MAX_STRING_LEN
+#define CFG_FUZZ_MAX_STRING_LEN 1000
+#endif
+
+extern "C" {
+
+/* TODO: Implement a fuzzed version of this.
+uint8_t const *tud_descriptor_bos_cb(void) { }
+*/
+
+/* TODO: Implement a fuzzed version of this.
+uint8_t const *tud_descriptor_device_qualifier_cb(void) {}
+*/
+
+/* TODO: Implement a fuzzed version of this.
+uint8_t const *tud_descriptor_other_speed_configuration_cb(uint8_t index) {}
+*/
+
+void tud_mount_cb(void) {
+ // NOOP
+}
+
+void tud_umount_cb(void) {
+ // NOOP
+}
+
+void tud_suspend_cb(bool remote_wakeup_en) {
+ (void)remote_wakeup_en;
+ // NOOP
+}
+
+void tud_resume_cb(void) {
+ // NOOP
+}
+
+/* TODO: Implement a fuzzed version of this.
+bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage,
+ tusb_control_request_t const *request) {}
+*/
+
+/* TODO: Implement a fuzzed version of this.
+uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {}
+*/
+}
diff --git a/test/ceedling b/test/unit-test/ceedling
similarity index 100%
rename from test/ceedling
rename to test/unit-test/ceedling
diff --git a/test/project.yml b/test/unit-test/project.yml
similarity index 71%
rename from test/project.yml
rename to test/unit-test/project.yml
index dc4a9cb28..562dbca09 100644
--- a/test/project.yml
+++ b/test/unit-test/project.yml
@@ -15,6 +15,7 @@
# :release_build: TRUE
:test_file_prefix: test_
:which_ceedling: vendor/ceedling
+ :ceedling_version: 0.31.1
:default_tasks:
- test:all
@@ -35,7 +36,7 @@
- +:test/**
- -:test/support
:source:
- - ../src/**
+ - ../../src/**
:support:
- test/support
@@ -77,10 +78,24 @@
:html_high_threshold: 90
:xml_report: FALSE
-#:tools:
-# Ceedling defaults to using gcc for compiling, linking, etc.
-# As [:tools] is blank, gcc will be used (so long as it's in your system path)
-# See documentation to configure a given toolchain for use
+:tools:
+ :test_compiler:
+ :executable: clang
+ :name: 'clang compiler'
+ :arguments:
+ - -I"$": COLLECTION_PATHS_TEST_TOOLCHAIN_INCLUDE #expands to -I search paths
+ - -I"$": COLLECTION_PATHS_TEST_SUPPORT_SOURCE_INCLUDE_VENDOR #expands to -I search paths
+ - -D$: COLLECTION_DEFINES_TEST_AND_VENDOR #expands to all -D defined symbols
+ - -fsanitize=address
+ - -c ${1} #source code input file (Ruby method call param list sub)
+ - -o ${2} #object file output (Ruby method call param list sub)
+ :test_linker:
+ :executable: clang
+ :name: 'clang linker'
+ :arguments:
+ - -fsanitize=address
+ - ${1} #list of object files to link (Ruby method call param list sub)
+ - -o ${2} #executable file output (Ruby method call param list sub)
# LIBRARIES
# These libraries are automatically injected into the build process. Those specified as
diff --git a/test/test/device/msc/test_msc_device.c b/test/unit-test/test/device/msc/test_msc_device.c
similarity index 99%
rename from test/test/device/msc/test_msc_device.c
rename to test/unit-test/test/device/msc/test_msc_device.c
index 00bb86ccf..63684e76a 100644
--- a/test/test/device/msc/test_msc_device.c
+++ b/test/unit-test/test/device/msc/test_msc_device.c
@@ -28,6 +28,7 @@
#include "unity.h"
// Files to test
+#include "osal/osal.h"
#include "tusb_fifo.h"
#include "tusb.h"
#include "usbd.h"
diff --git a/test/test/device/usbd/test_usbd.c b/test/unit-test/test/device/usbd/test_usbd.c
similarity index 99%
rename from test/test/device/usbd/test_usbd.c
rename to test/unit-test/test/device/usbd/test_usbd.c
index c90383b57..ad95eb47a 100644
--- a/test/test/device/usbd/test_usbd.c
+++ b/test/unit-test/test/device/usbd/test_usbd.c
@@ -25,6 +25,7 @@
#include "unity.h"
// Files to test
+#include "osal/osal.h"
#include "tusb_fifo.h"
#include "tusb.h"
#include "usbd.h"
diff --git a/test/test/support/tusb_config.h b/test/unit-test/test/support/tusb_config.h
similarity index 98%
rename from test/test/support/tusb_config.h
rename to test/unit-test/test/support/tusb_config.h
index 0455f933b..00818fae5 100644
--- a/test/test/support/tusb_config.h
+++ b/test/unit-test/test/support/tusb_config.h
@@ -51,7 +51,7 @@
// CFG_TUSB_DEBUG is defined by compiler in DEBUG build
#ifndef CFG_TUSB_DEBUG
-#define CFG_TUSB_DEBUG 0
+#define CFG_TUSB_DEBUG 1
#endif
/* USB DMA on some MCUs can only access a specific SRAM region with restriction on alignment.
diff --git a/test/test/test_fifo.c b/test/unit-test/test/test_fifo.c
similarity index 62%
rename from test/test/test_fifo.c
rename to test/unit-test/test/test_fifo.c
index 0a5f4d3b9..28be6d8fc 100644
--- a/test/test/test_fifo.c
+++ b/test/unit-test/test/test_fifo.c
@@ -26,17 +26,27 @@
#include
#include "unity.h"
+
+#include "osal/osal.h"
#include "tusb_fifo.h"
-#define FIFO_SIZE 10
-TU_FIFO_DEF(tu_ff, FIFO_SIZE, uint8_t, false);
+#define FIFO_SIZE 64
+uint8_t tu_ff_buf[FIFO_SIZE * sizeof(uint8_t)];
+tu_fifo_t tu_ff = TU_FIFO_INIT(tu_ff_buf, FIFO_SIZE, uint8_t, false);
+
tu_fifo_t* ff = &tu_ff;
tu_fifo_buffer_info_t info;
+uint8_t test_data[4096];
+uint8_t rd_buf[FIFO_SIZE];
+
void setUp(void)
{
tu_fifo_clear(ff);
memset(&info, 0, sizeof(tu_fifo_buffer_info_t));
+
+ for(int i=0; i 4
- rd_count = tu_fifo_read_n(&ff4, rd, 5);
+ rd_count = tu_fifo_read_n(&ff4, rd_buf4, 5);
TEST_ASSERT_EQUAL( 5, rd_count );
- TEST_ASSERT_EQUAL_UINT32_ARRAY( data, rd, rd_count ); // 0 -> 4
+ TEST_ASSERT_EQUAL_UINT32_ARRAY( data4, rd_buf4, rd_count ); // 0 -> 4
- tu_fifo_write_n(&ff4, data+10, 5);
+ tu_fifo_write_n(&ff4, data4+FIFO_SIZE, 5);
- // read 5 -> 14
- rd_count = tu_fifo_read_n(&ff4, rd, 10);
- TEST_ASSERT_EQUAL( 10, rd_count );
- TEST_ASSERT_EQUAL_UINT32_ARRAY( data+5, rd, rd_count ); // 5 -> 14
+ // read all 5 -> 68
+ rd_count = tu_fifo_read_n(&ff4, rd_buf4, FIFO_SIZE);
+ TEST_ASSERT_EQUAL( FIFO_SIZE, rd_count );
+ TEST_ASSERT_EQUAL_UINT32_ARRAY( data4+5, rd_buf4, rd_count ); // 5 -> 68
}
void test_read_n(void)
{
- // prepare data
- uint8_t data[20];
- for(int i=0; i 4
- rd_count = tu_fifo_read_n(ff, rd, 5);
+ rd_count = tu_fifo_read_n(ff, rd_buf, 5);
TEST_ASSERT_EQUAL( 5, rd_count );
- TEST_ASSERT_EQUAL_MEMORY( data, rd, rd_count ); // 0 -> 4
+ TEST_ASSERT_EQUAL_MEMORY( test_data, rd_buf, rd_count ); // 0 -> 4
// case 2: Read index + count > depth
// write 10, 11, 12
- tu_fifo_write(ff, data+10);
- tu_fifo_write(ff, data+11);
- tu_fifo_write(ff, data+12);
+ tu_fifo_write(ff, test_data+FIFO_SIZE);
+ tu_fifo_write(ff, test_data+FIFO_SIZE+1);
+ tu_fifo_write(ff, test_data+FIFO_SIZE+2);
- rd_count = tu_fifo_read_n(ff, rd, 7);
+ rd_count = tu_fifo_read_n(ff, rd_buf, 7);
TEST_ASSERT_EQUAL( 7, rd_count );
- TEST_ASSERT_EQUAL_MEMORY( data+5, rd, rd_count ); // 5 -> 11
+ TEST_ASSERT_EQUAL_MEMORY( test_data+5, rd_buf, rd_count ); // 5 -> 11
// Should only read until empty
- TEST_ASSERT_EQUAL( 1, tu_fifo_read_n(ff, rd, 100) );
+ TEST_ASSERT_EQUAL( FIFO_SIZE-5+3-7, tu_fifo_read_n(ff, rd_buf, 100) );
}
void test_write_n(void)
{
- // prepare data
- uint8_t data[20];
- for(int i=0; i 4
+ rd_count = tu_fifo_read_n(ff, rd_buf, 16); // wr = 32, count = 16
+ TEST_ASSERT_EQUAL( 16, rd_count );
+ TEST_ASSERT_EQUAL_MEMORY( test_data, rd_buf, rd_count );
// case 2: wr + count > depth
- tu_fifo_write_n(ff, data+8, 6); // wr = 3, count = 9
+ tu_fifo_write_n(ff, test_data+32, 40); // wr = 72 -> 8, count = 56
- for(rd_count=0; rd_count<7; rd_count++) tu_fifo_read(ff, rd+rd_count); // wr = 3, count = 2
+ tu_fifo_read_n(ff, rd_buf, 32); // count = 24
+ TEST_ASSERT_EQUAL_MEMORY( test_data+16, rd_buf, rd_count);
- TEST_ASSERT_EQUAL_MEMORY( data+5, rd, rd_count); // 5 -> 11
+ TEST_ASSERT_EQUAL(24, tu_fifo_count(ff));
+}
- TEST_ASSERT_EQUAL(2, tu_fifo_count(ff));
+void test_write_double_overflowed(void)
+{
+ tu_fifo_set_overwritable(ff, true);
+
+ uint8_t rd_buf[FIFO_SIZE] = { 0 };
+ uint8_t* buf = test_data;
+
+ // full
+ buf += tu_fifo_write_n(ff, buf, FIFO_SIZE);
+ TEST_ASSERT_EQUAL(FIFO_SIZE, tu_fifo_count(ff));
+
+ // write more, should still full
+ buf += tu_fifo_write_n(ff, buf, FIFO_SIZE-8);
+ TEST_ASSERT_EQUAL(FIFO_SIZE, tu_fifo_count(ff));
+
+ // double overflowed: in total, write more than > 2*FIFO_SIZE
+ buf += tu_fifo_write_n(ff, buf, 16);
+ TEST_ASSERT_EQUAL(FIFO_SIZE, tu_fifo_count(ff));
+
+ // reading back should give back data from last FIFO_SIZE write
+ tu_fifo_read_n(ff, rd_buf, FIFO_SIZE);
+
+ TEST_ASSERT_EQUAL_MEMORY(buf-16, rd_buf+FIFO_SIZE-16, 16);
+
+ // TODO whole buffer should match, but we deliberately not implement it
+ // TEST_ASSERT_EQUAL_MEMORY(buf-FIFO_SIZE, rd_buf, FIFO_SIZE);
+}
+
+static uint16_t help_write(uint16_t total, uint16_t n)
+{
+ tu_fifo_write_n(ff, test_data, n);
+ total = tu_min16(FIFO_SIZE, total + n);
+
+ TEST_ASSERT_EQUAL(total, tu_fifo_count(ff));
+ TEST_ASSERT_EQUAL(FIFO_SIZE - total, tu_fifo_remaining(ff));
+
+ return total;
+}
+
+void test_write_overwritable2(void)
+{
+ tu_fifo_set_overwritable(ff, true);
+
+ // based on actual crash tests detected by fuzzing
+ uint16_t total = 0;
+
+ total = help_write(total, 12);
+ total = help_write(total, 55);
+ total = help_write(total, 73);
+ total = help_write(total, 55);
+ total = help_write(total, 75);
+ total = help_write(total, 84);
+ total = help_write(total, 1);
+ total = help_write(total, 10);
+ total = help_write(total, 12);
+ total = help_write(total, 25);
+ total = help_write(total, 192);
}
void test_peek(void)
@@ -315,4 +375,4 @@ void test_rd_idx_wrap()
n = tu_fifo_read_n(&ff10, dst, 4);
TEST_ASSERT_EQUAL(n, 2);
TEST_ASSERT_EQUAL(ff10.rd_idx, 6);
-}
\ No newline at end of file
+}
diff --git a/test/vendor/ceedling/bin/ceedling b/test/unit-test/vendor/ceedling/bin/ceedling
similarity index 81%
rename from test/vendor/ceedling/bin/ceedling
rename to test/unit-test/vendor/ceedling/bin/ceedling
index fa099590a..d110f3d4c 100644
--- a/test/vendor/ceedling/bin/ceedling
+++ b/test/unit-test/vendor/ceedling/bin/ceedling
@@ -49,16 +49,16 @@ unless (project_found)
end
desc "upgrade PROJECT_NAME", "upgrade ceedling for a project (not req'd if gem used)"
- method_option :docs, :type => :boolean, :default => false, :desc => "Add docs in project vendor directory"
- method_option :local, :type => :boolean, :default => false, :desc => "Create a copy of Ceedling in the project vendor directory"
- method_option :no_configs, :type => :boolean, :default => false, :desc => "Don't install starter configuration files"
- method_option :noconfigs, :type => :boolean, :default => false
-
- #deprecated:
- method_option :no_docs, :type => :boolean, :default => false
- method_option :nodocs, :type => :boolean, :default => false
def upgrade(name, silent = false)
- copy_assets_and_create_structure(name, silent, true, options || {:upgrade => true})
+ as_local = true
+ begin
+ require "yaml"
+ as_local = (YAML.load_file(File.join(name, "project.yml"))[:project][:which_ceedling] != 'gem')
+ rescue
+ raise "ERROR: Could not find valid project file '#{yaml_path}'"
+ end
+ found_docs = File.exists?( File.join(name, "docs", "CeedlingPacket.md") )
+ copy_assets_and_create_structure(name, silent, true, {:upgrade => true, :no_configs => true, :local => as_local, :docs => found_docs})
end
no_commands do
@@ -90,26 +90,30 @@ unless (project_found)
FileUtils.touch(File.join(test_support_path, '.gitkeep'))
# If documentation requested, create a place to dump them and do so
+ doc_path = ""
if use_docs
- doc_path = File.join(ceedling_path, 'docs')
+ doc_path = use_gem ? File.join(name, 'docs') : File.join(ceedling_path, 'docs')
FileUtils.mkdir_p doc_path
in_doc_path = lambda {|f| File.join(doc_path, f)}
- doc_files = [
- 'docs/CeedlingPacket.md',
- 'vendor/c_exception/docs/CException.md',
- 'vendor/cmock/docs/CMock_Summary.md',
- 'vendor/unity/docs/UnityAssertionsCheatSheetSuitableforPrintingandPossiblyFraming.pdf',
- 'vendor/unity/docs/UnityAssertionsReference.md',
- 'vendor/unity/docs/UnityConfigurationGuide.md',
- 'vendor/unity/docs/UnityGettingStartedGuide.md',
- 'vendor/unity/docs/UnityHelperScriptsGuide.md',
- 'vendor/unity/docs/ThrowTheSwitchCodingStandard.md',
- ]
+ # Add documentation from main projects to list
+ doc_files = {}
+ ['docs','vendor/unity/docs','vendor/cmock/docs','vendor/cexception/docs'].each do |p|
+ Dir[ File.expand_path(File.join(here, p, '*.md')) ].each do |f|
+ doc_files[ File.basename(f) ] = f unless(doc_files.include? f)
+ end
+ end
- doc_files.each do |f|
- copy_file(f, in_doc_path.call(File.basename(f)), :force => force)
+ # Add documentation from plugins to list
+ Dir[ File.join(here, 'plugins/**/README.md') ].each do |plugin_path|
+ k = "plugin_" + plugin_path.split(/\\|\//)[-2] + ".md"
+ doc_files[ k ] = File.expand_path(plugin_path)
+ end
+
+ # Copy all documentation
+ doc_files.each_pair do |k, v|
+ copy_file(v, in_doc_path.call(k), :force => force)
end
end
@@ -133,7 +137,6 @@ unless (project_found)
{:src => 'vendor/cmock/config/', :dst => 'vendor/cmock/config'},
{:src => 'vendor/cmock/lib/', :dst => 'vendor/cmock/lib'},
{:src => 'vendor/cmock/src/', :dst => 'vendor/cmock/src'},
- {:src => 'vendor/deep_merge/lib/', :dst => 'vendor/deep_merge/lib'},
{:src => 'vendor/diy/lib', :dst => 'vendor/diy/lib'},
{:src => 'vendor/unity/auto/', :dst => 'vendor/unity/auto'},
{:src => 'vendor/unity/src/', :dst => 'vendor/unity/src'},
@@ -146,16 +149,24 @@ unless (project_found)
# We're copying in a configuration file if we haven't said not to
if (use_configs)
- if use_gem
- copy_file(File.join('assets', 'project_as_gem.yml'), File.join(name, 'project.yml'), :force => force)
+ dst_yaml = File.join(name, 'project.yml')
+ src_yaml = if use_gem
+ File.join(here, 'assets', 'project_as_gem.yml')
else
- copy_file(File.join('assets', 'project_with_guts.yml'), File.join(name, 'project.yml'), :force => force)
if is_windows?
copy_file(File.join('assets', 'ceedling.cmd'), File.join(name, 'ceedling.cmd'), :force => force)
else
copy_file(File.join('assets', 'ceedling'), File.join(name, 'ceedling'), :force => force)
File.chmod(0755, File.join(name, 'ceedling'))
end
+ File.join(here, 'assets', 'project_with_guts.yml')
+ end
+
+ # Perform the actual clone of the config file, while updating the version
+ File.open(dst_yaml,'w') do |dst|
+ require File.expand_path(File.join(File.dirname(__FILE__),"..","lib","ceedling","version.rb"))
+ dst << File.read(src_yaml).gsub(":ceedling_version: '?'",":ceedling_version: #{Ceedling::Version::CEEDLING}")
+ puts " create #{dst_yaml}"
end
end
@@ -167,8 +178,8 @@ unless (project_found)
unless silent
puts "\n"
puts "Project '#{name}' #{force ? "upgraded" : "created"}!"
- puts " - Tool documentation is located in vendor/ceedling/docs" if use_docs
- puts " - Execute 'ceedling help' to view available test & build tasks"
+ puts " - Tool documentation is located in #{doc_path}" if use_docs
+ puts " - Execute 'ceedling help' from #{name} to view available test & build tasks"
puts ''
end
end
@@ -206,10 +217,10 @@ unless (project_found)
desc "version", "return the version of the tools installed"
def version()
- require 'ceedling/version.rb'
- puts " Ceedling:: #{Ceedling::Version::CEEDLING}"
- puts " CMock:: #{Ceedling::Version::CMOCK}"
- puts " Unity:: #{Ceedling::Version::UNITY}"
+ require File.expand_path(File.join(File.dirname(__FILE__),"..","lib","ceedling","version.rb"))
+ puts " Ceedling:: #{Ceedling::Version::CEEDLING}"
+ puts " CMock:: #{Ceedling::Version::CMOCK}"
+ puts " Unity:: #{Ceedling::Version::UNITY}"
puts " CException:: #{Ceedling::Version::CEXCEPTION}"
end
end
@@ -287,6 +298,8 @@ else
options[:list_tasks] = true
when /^-T$/
options[:list_tasks] = true
+ when /^--tasks$/
+ options[:list_tasks] = true
when /^project:(\w+)/
ENV['CEEDLING_USER_PROJECT_FILE'] = "#{$1}.yml"
else
diff --git a/test/vendor/ceedling/lib/ceedling.rb b/test/unit-test/vendor/ceedling/lib/ceedling.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling.rb
diff --git a/test/vendor/ceedling/lib/ceedling/build_invoker_utils.rb b/test/unit-test/vendor/ceedling/lib/ceedling/build_invoker_utils.rb
similarity index 97%
rename from test/vendor/ceedling/lib/ceedling/build_invoker_utils.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/build_invoker_utils.rb
index b7d57f8f4..5727bcabc 100644
--- a/test/vendor/ceedling/lib/ceedling/build_invoker_utils.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/build_invoker_utils.rb
@@ -9,7 +9,7 @@ class BuildInvokerUtils
##
# Processes exceptions and tries to display a useful message for the user.
#
- # ==== Attriboops...utes
+ # ==== Attributes
#
# * _exception_: The exception given by a rescue statement.
# * _context_: A symbol representing where in the build the exception
diff --git a/test/vendor/ceedling/lib/ceedling/cacheinator.rb b/test/unit-test/vendor/ceedling/lib/ceedling/cacheinator.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/cacheinator.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/cacheinator.rb
diff --git a/test/unit-test/vendor/ceedling/lib/ceedling/cacheinator_helper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/cacheinator_helper.rb
new file mode 100644
index 000000000..14e8a6ef0
--- /dev/null
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/cacheinator_helper.rb
@@ -0,0 +1,35 @@
+
+class CacheinatorHelper
+
+ constructor :file_wrapper, :yaml_wrapper
+
+ def diff_cached_config?(cached_filepath, hash)
+ return false if ( not @file_wrapper.exist?(cached_filepath) )
+ return true if (@yaml_wrapper.load(cached_filepath) != hash)
+ return false
+ end
+
+ def diff_cached_defines?(cached_filepath, files)
+ changed_defines = false
+ current_defines = COLLECTION_DEFINES_TEST_AND_VENDOR.reject(&:empty?)
+
+ current_dependencies = Hash[files.collect { |source| [source, current_defines.dup] }]
+ if not @file_wrapper.exist?(cached_filepath)
+ @yaml_wrapper.dump(cached_filepath, current_dependencies)
+ return changed_defines
+ end
+
+ dependencies = @yaml_wrapper.load(cached_filepath)
+ common_dependencies = current_dependencies.select { |file, defines| dependencies.has_key?(file) }
+
+ if dependencies.values_at(*common_dependencies.keys) != common_dependencies.values
+ changed_defines = true
+ end
+
+ dependencies.merge!(current_dependencies)
+ @yaml_wrapper.dump(cached_filepath, dependencies)
+
+ return changed_defines
+ end
+
+end
diff --git a/test/vendor/ceedling/lib/ceedling/cmock_builder.rb b/test/unit-test/vendor/ceedling/lib/ceedling/cmock_builder.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/cmock_builder.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/cmock_builder.rb
diff --git a/test/vendor/ceedling/lib/ceedling/configurator.rb b/test/unit-test/vendor/ceedling/lib/ceedling/configurator.rb
similarity index 91%
rename from test/vendor/ceedling/lib/ceedling/configurator.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/configurator.rb
index b5ad8982e..0ae4d04a8 100644
--- a/test/vendor/ceedling/lib/ceedling/configurator.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/configurator.rb
@@ -54,6 +54,7 @@ class Configurator
:test_fixture,
:test_includes_preprocessor,
:test_file_preprocessor,
+ :test_file_preprocessor_directives,
:test_dependencies_generator,
:release_compiler,
:release_assembler,
@@ -183,17 +184,22 @@ class Configurator
@rake_plugins = @configurator_plugins.find_rake_plugins(config, paths_hash)
@script_plugins = @configurator_plugins.find_script_plugins(config, paths_hash)
config_plugins = @configurator_plugins.find_config_plugins(config, paths_hash)
- plugin_defaults = @configurator_plugins.find_plugin_defaults(config, paths_hash)
+ plugin_yml_defaults = @configurator_plugins.find_plugin_yml_defaults(config, paths_hash)
+ plugin_hash_defaults = @configurator_plugins.find_plugin_hash_defaults(config, paths_hash)
config_plugins.each do |plugin|
plugin_config = @yaml_wrapper.load(plugin)
config.deep_merge(plugin_config)
end
- plugin_defaults.each do |defaults|
+ plugin_yml_defaults.each do |defaults|
@configurator_builder.populate_defaults( config, @yaml_wrapper.load(defaults) )
end
+ plugin_hash_defaults.each do |defaults|
+ @configurator_builder.populate_defaults( config, defaults )
+ end
+
# special plugin setting for results printing
config[:plugins][:display_raw_test_results] = true if (config[:plugins][:display_raw_test_results].nil?)
@@ -203,10 +209,19 @@ class Configurator
def merge_imports(config)
if config[:import]
- until config[:import].empty?
- path = config[:import].shift
- path = @system_wrapper.module_eval(path) if (path =~ RUBY_STRING_REPLACEMENT_PATTERN)
- config.deep_merge!(@yaml_wrapper.load(path))
+ if config[:import].is_a? Array
+ until config[:import].empty?
+ path = config[:import].shift
+ path = @system_wrapper.module_eval(path) if (path =~ RUBY_STRING_REPLACEMENT_PATTERN)
+ config.deep_merge!(@yaml_wrapper.load(path))
+ end
+ else
+ config[:import].each_value do |path|
+ if !path.nil?
+ path = @system_wrapper.module_eval(path) if (path =~ RUBY_STRING_REPLACEMENT_PATTERN)
+ config.deep_merge!(@yaml_wrapper.load(path))
+ end
+ end
end
end
config.delete(:import)
@@ -222,7 +237,11 @@ class Configurator
interstitial = ((key == :path) ? File::PATH_SEPARATOR : '')
items = ((value.class == Array) ? hash[key] : [value])
- items.each { |item| item.replace( @system_wrapper.module_eval( item ) ) if (item =~ RUBY_STRING_REPLACEMENT_PATTERN) }
+ items.each do |item|
+ if item.is_a? String and item =~ RUBY_STRING_REPLACEMENT_PATTERN
+ item.replace( @system_wrapper.module_eval( item ) )
+ end
+ end
hash[key] = items.join( interstitial )
@system_wrapper.env_set( key.to_s.upcase, hash[key] )
diff --git a/test/vendor/ceedling/lib/ceedling/configurator_builder.rb b/test/unit-test/vendor/ceedling/lib/ceedling/configurator_builder.rb
similarity index 93%
rename from test/vendor/ceedling/lib/ceedling/configurator_builder.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/configurator_builder.rb
index da8a816f5..f202d8a65 100644
--- a/test/vendor/ceedling/lib/ceedling/configurator_builder.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/configurator_builder.rb
@@ -250,8 +250,8 @@ class ConfiguratorBuilder
def collect_test_support_source_include_vendor_paths(in_hash)
return {
:collection_paths_test_support_source_include_vendor =>
- in_hash[:collection_paths_test_support_source_include] +
- get_vendor_paths(in_hash)
+ get_vendor_paths(in_hash) +
+ in_hash[:collection_paths_test_support_source_include]
}
end
@@ -384,14 +384,26 @@ class ConfiguratorBuilder
end
+ def get_vendor_defines(in_hash)
+ defines = in_hash[:unity_defines].clone
+ defines.concat(in_hash[:cmock_defines]) if (in_hash[:project_use_mocks])
+ defines.concat(in_hash[:cexception_defines]) if (in_hash[:project_use_exceptions])
+
+ return defines
+ end
+
+
+ def collect_vendor_defines(in_hash)
+ return {:collection_defines_vendor => get_vendor_defines(in_hash)}
+ end
+
+
def collect_test_and_vendor_defines(in_hash)
- test_defines = in_hash[:defines_test].clone
+ defines = in_hash[:defines_test].clone
+ vendor_defines = get_vendor_defines(in_hash)
+ defines.concat(vendor_defines) if vendor_defines
- test_defines.concat(in_hash[:unity_defines])
- test_defines.concat(in_hash[:cmock_defines]) if (in_hash[:project_use_mocks])
- test_defines.concat(in_hash[:cexception_defines]) if (in_hash[:project_use_exceptions])
-
- return {:collection_defines_test_and_vendor => test_defines}
+ return {:collection_defines_test_and_vendor => defines}
end
@@ -418,28 +430,33 @@ class ConfiguratorBuilder
# Note: Symbols passed to compiler at command line can change Unity and CException behavior / configuration;
# we also handle those dependencies elsewhere in compilation dependencies
- objects = [UNITY_C_FILE]
+ sources = [UNITY_C_FILE]
- in_hash[:files_support].each { |file| objects << File.basename(file) }
+ in_hash[:files_support].each { |file| sources << file }
# we don't include paths here because use of plugins or mixing different compilers may require different build paths
- objects << CEXCEPTION_C_FILE if (in_hash[:project_use_exceptions])
- objects << CMOCK_C_FILE if (in_hash[:project_use_mocks])
+ sources << CEXCEPTION_C_FILE if (in_hash[:project_use_exceptions])
+ sources << CMOCK_C_FILE if (in_hash[:project_use_mocks])
# if we're using mocks & a unity helper is defined & that unity helper includes a source file component (not only a header of macros),
# then link in the unity_helper object file too
if ( in_hash[:project_use_mocks] and in_hash[:cmock_unity_helper] )
in_hash[:cmock_unity_helper].each do |helper|
if @file_wrapper.exist?(helper.ext(in_hash[:extension_source]))
- objects << File.basename(helper)
+ sources << helper
end
end
end
+ # create object files from all the sources
+ objects = sources.map { |file| File.basename(file) }
+
# no build paths here so plugins can remap if necessary (i.e. path mapping happens at runtime)
objects.map! { |object| object.ext(in_hash[:extension_object]) }
- return { :collection_test_fixture_extra_link_objects => objects }
+ return { :collection_all_support => sources,
+ :collection_test_fixture_extra_link_objects => objects
+ }
end
diff --git a/test/vendor/ceedling/lib/ceedling/configurator_plugins.rb b/test/unit-test/vendor/ceedling/lib/ceedling/configurator_plugins.rb
similarity index 81%
rename from test/vendor/ceedling/lib/ceedling/configurator_plugins.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/configurator_plugins.rb
index 70ca884a2..75bcd982d 100644
--- a/test/vendor/ceedling/lib/ceedling/configurator_plugins.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/configurator_plugins.rb
@@ -26,6 +26,7 @@ class ConfiguratorPlugins
if is_script_plugin
@system_wrapper.add_load_path( File.join( path, 'lib') )
+ @system_wrapper.add_load_path( File.join( path, 'config') )
end
break
end
@@ -92,7 +93,7 @@ class ConfiguratorPlugins
# gather up and return default .yml filepaths that exist on-disk
- def find_plugin_defaults(config, plugin_paths)
+ def find_plugin_yml_defaults(config, plugin_paths)
defaults_with_path = []
config[:plugins][:enabled].each do |plugin|
@@ -108,4 +109,23 @@ class ConfiguratorPlugins
return defaults_with_path
end
+ # gather up and return
+ def find_plugin_hash_defaults(config, plugin_paths)
+ defaults_hash= []
+
+ config[:plugins][:enabled].each do |plugin|
+ if path = plugin_paths[(plugin + '_path').to_sym]
+ default_path = File.join(path, "config", "defaults_#{plugin}.rb")
+ if @file_wrapper.exist?(default_path)
+ @system_wrapper.require_file( "defaults_#{plugin}.rb")
+
+ object = eval("get_default_config()")
+ defaults_hash << object
+ end
+ end
+ end
+
+ return defaults_hash
+ end
+
end
diff --git a/test/vendor/ceedling/lib/ceedling/configurator_setup.rb b/test/unit-test/vendor/ceedling/lib/ceedling/configurator_setup.rb
similarity index 98%
rename from test/vendor/ceedling/lib/ceedling/configurator_setup.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/configurator_setup.rb
index 1d029b065..c43bb5c12 100644
--- a/test/vendor/ceedling/lib/ceedling/configurator_setup.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/configurator_setup.rb
@@ -39,6 +39,7 @@ class ConfiguratorSetup
flattened_config.merge!(@configurator_builder.collect_headers(flattened_config))
flattened_config.merge!(@configurator_builder.collect_release_existing_compilation_input(flattened_config))
flattened_config.merge!(@configurator_builder.collect_all_existing_compilation_input(flattened_config))
+ flattened_config.merge!(@configurator_builder.collect_vendor_defines(flattened_config))
flattened_config.merge!(@configurator_builder.collect_test_and_vendor_defines(flattened_config))
flattened_config.merge!(@configurator_builder.collect_release_and_vendor_defines(flattened_config))
flattened_config.merge!(@configurator_builder.collect_release_artifact_extra_link_objects(flattened_config))
diff --git a/test/vendor/ceedling/lib/ceedling/configurator_validator.rb b/test/unit-test/vendor/ceedling/lib/ceedling/configurator_validator.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/configurator_validator.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/configurator_validator.rb
diff --git a/test/vendor/ceedling/lib/ceedling/constants.rb b/test/unit-test/vendor/ceedling/lib/ceedling/constants.rb
similarity index 98%
rename from test/vendor/ceedling/lib/ceedling/constants.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/constants.rb
index 993ce8db2..19484f063 100644
--- a/test/vendor/ceedling/lib/ceedling/constants.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/constants.rb
@@ -95,3 +95,5 @@ NULL_FILE_PATH = '/dev/null'
TESTS_BASE_PATH = TEST_ROOT_NAME
RELEASE_BASE_PATH = RELEASE_ROOT_NAME
+
+VENDORS_FILES = %w(unity UnityHelper cmock CException).freeze
diff --git a/test/vendor/ceedling/lib/ceedling/defaults.rb b/test/unit-test/vendor/ceedling/lib/ceedling/defaults.rb
similarity index 79%
rename from test/vendor/ceedling/lib/ceedling/defaults.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/defaults.rb
index 9e391fa09..1300a1aab 100644
--- a/test/vendor/ceedling/lib/ceedling/defaults.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/defaults.rb
@@ -7,17 +7,20 @@ CEEDLING_VENDOR = File.expand_path(File.dirname(__FILE__) + '/../../vendor') unl
CEEDLING_PLUGINS = [] unless defined? CEEDLING_PLUGINS
DEFAULT_TEST_COMPILER_TOOL = {
- :executable => FilePathUtils.os_executable_ext('gcc').freeze,
+ :executable => ENV['CC'].nil? ? FilePathUtils.os_executable_ext('gcc').freeze : ENV['CC'].split[0],
:name => 'default_test_compiler'.freeze,
:stderr_redirect => StdErrRedirect::NONE.freeze,
:background_exec => BackgroundExec::NONE.freeze,
:optional => false.freeze,
:arguments => [
+ ENV['CC'].nil? ? "" : ENV['CC'].split[1..-1],
+ ENV['CPPFLAGS'].nil? ? "" : ENV['CPPFLAGS'].split,
{"-I\"$\"" => 'COLLECTION_PATHS_TEST_SUPPORT_SOURCE_INCLUDE_VENDOR'}.freeze,
{"-I\"$\"" => 'COLLECTION_PATHS_TEST_TOOLCHAIN_INCLUDE'}.freeze,
{"-D$" => 'COLLECTION_DEFINES_TEST_AND_VENDOR'}.freeze,
"-DGNU_COMPILER".freeze,
"-g".freeze,
+ ENV['CFLAGS'].nil? ? "" : ENV['CFLAGS'].split,
"-c \"${1}\"".freeze,
"-o \"${2}\"".freeze,
# gcc's list file output options are complex; no use of ${3} parameter in default config
@@ -27,16 +30,21 @@ DEFAULT_TEST_COMPILER_TOOL = {
}
DEFAULT_TEST_LINKER_TOOL = {
- :executable => FilePathUtils.os_executable_ext('gcc').freeze,
+ :executable => ENV['CCLD'].nil? ? FilePathUtils.os_executable_ext('gcc').freeze : ENV['CCLD'].split[0],
:name => 'default_test_linker'.freeze,
:stderr_redirect => StdErrRedirect::NONE.freeze,
:background_exec => BackgroundExec::NONE.freeze,
:optional => false.freeze,
:arguments => [
+ ENV['CCLD'].nil? ? "" : ENV['CCLD'].split[1..-1],
+ ENV['CFLAGS'].nil? ? "" : ENV['CFLAGS'].split,
+ ENV['LDFLAGS'].nil? ? "" : ENV['LDFLAGS'].split,
"\"${1}\"".freeze,
+ "${5}".freeze,
"-o \"${2}\"".freeze,
"".freeze,
- "${4}".freeze
+ "${4}".freeze,
+ ENV['LDLIBS'].nil? ? "" : ENV['LDLIBS'].split
].freeze
}
@@ -50,12 +58,14 @@ DEFAULT_TEST_FIXTURE_TOOL = {
}
DEFAULT_TEST_INCLUDES_PREPROCESSOR_TOOL = {
- :executable => FilePathUtils.os_executable_ext('gcc').freeze,
+ :executable => ENV['CC'].nil? ? FilePathUtils.os_executable_ext('gcc').freeze : ENV['CC'].split[0],
:name => 'default_test_includes_preprocessor'.freeze,
:stderr_redirect => StdErrRedirect::NONE.freeze,
:background_exec => BackgroundExec::NONE.freeze,
:optional => false.freeze,
:arguments => [
+ ENV['CC'].nil? ? "" : ENV['CC'].split[1..-1],
+ ENV['CPPFLAGS'].nil? ? "" : ENV['CPPFLAGS'].split,
'-E'.freeze, # OSX clang
'-MM'.freeze,
'-MG'.freeze,
@@ -67,18 +77,38 @@ DEFAULT_TEST_INCLUDES_PREPROCESSOR_TOOL = {
{"-D$" => 'COLLECTION_DEFINES_TEST_AND_VENDOR'}.freeze,
{"-D$" => 'DEFINES_TEST_PREPROCESS'}.freeze,
"-DGNU_COMPILER".freeze, # OSX clang
- '-w'.freeze,
# '-nostdinc'.freeze, # disabled temporarily due to stdio access violations on OSX
"\"${1}\"".freeze
].freeze
}
DEFAULT_TEST_FILE_PREPROCESSOR_TOOL = {
- :executable => FilePathUtils.os_executable_ext('gcc').freeze,
+ :executable => ENV['CC'].nil? ? FilePathUtils.os_executable_ext('gcc').freeze : ENV['CC'].split[0],
:name => 'default_test_file_preprocessor'.freeze,
:stderr_redirect => StdErrRedirect::NONE.freeze,
:background_exec => BackgroundExec::NONE.freeze,
:optional => false.freeze,
+ :arguments => [
+ ENV['CC'].nil? ? "" : ENV['CC'].split[1..-1],
+ ENV['CPPFLAGS'].nil? ? "" : ENV['CPPFLAGS'].split,
+ '-E'.freeze,
+ {"-I\"$\"" => 'COLLECTION_PATHS_TEST_SUPPORT_SOURCE_INCLUDE_VENDOR'}.freeze,
+ {"-I\"$\"" => 'COLLECTION_PATHS_TEST_TOOLCHAIN_INCLUDE'}.freeze,
+ {"-D$" => 'COLLECTION_DEFINES_TEST_AND_VENDOR'}.freeze,
+ {"-D$" => 'DEFINES_TEST_PREPROCESS'}.freeze,
+ "-DGNU_COMPILER".freeze,
+ # '-nostdinc'.freeze, # disabled temporarily due to stdio access violations on OSX
+ "\"${1}\"".freeze,
+ "-o \"${2}\"".freeze
+ ].freeze
+ }
+
+DEFAULT_TEST_FILE_PREPROCESSOR_DIRECTIVES_TOOL = {
+ :executable => FilePathUtils.os_executable_ext('gcc').freeze,
+ :name => 'default_test_file_preprocessor_directives'.freeze,
+ :stderr_redirect => StdErrRedirect::NONE.freeze,
+ :background_exec => BackgroundExec::NONE.freeze,
+ :optional => false.freeze,
:arguments => [
'-E'.freeze,
{"-I\"$\"" => 'COLLECTION_PATHS_TEST_SUPPORT_SOURCE_INCLUDE_VENDOR'}.freeze,
@@ -86,6 +116,7 @@ DEFAULT_TEST_FILE_PREPROCESSOR_TOOL = {
{"-D$" => 'COLLECTION_DEFINES_TEST_AND_VENDOR'}.freeze,
{"-D$" => 'DEFINES_TEST_PREPROCESS'}.freeze,
"-DGNU_COMPILER".freeze,
+ '-fdirectives-only'.freeze,
# '-nostdinc'.freeze, # disabled temporarily due to stdio access violations on OSX
"\"${1}\"".freeze,
"-o \"${2}\"".freeze
@@ -100,12 +131,14 @@ else
end
DEFAULT_TEST_DEPENDENCIES_GENERATOR_TOOL = {
- :executable => FilePathUtils.os_executable_ext('gcc').freeze,
+ :executable => ENV['CC'].nil? ? FilePathUtils.os_executable_ext('gcc').freeze : ENV['CC'].split[0],
:name => 'default_test_dependencies_generator'.freeze,
:stderr_redirect => StdErrRedirect::NONE.freeze,
:background_exec => BackgroundExec::NONE.freeze,
:optional => false.freeze,
:arguments => [
+ ENV['CC'].nil? ? "" : ENV['CC'].split[1..-1],
+ ENV['CPPFLAGS'].nil? ? "" : ENV['CPPFLAGS'].split,
'-E'.freeze,
{"-I\"$\"" => 'COLLECTION_PATHS_TEST_SUPPORT_SOURCE_INCLUDE_VENDOR'}.freeze,
{"-I\"$\"" => 'COLLECTION_PATHS_TEST_TOOLCHAIN_INCLUDE'}.freeze,
@@ -123,12 +156,14 @@ DEFAULT_TEST_DEPENDENCIES_GENERATOR_TOOL = {
}
DEFAULT_RELEASE_DEPENDENCIES_GENERATOR_TOOL = {
- :executable => FilePathUtils.os_executable_ext('gcc').freeze,
+ :executable => ENV['CC'].nil? ? FilePathUtils.os_executable_ext('gcc').freeze : ENV['CC'].split[0],
:name => 'default_release_dependencies_generator'.freeze,
:stderr_redirect => StdErrRedirect::NONE.freeze,
:background_exec => BackgroundExec::NONE.freeze,
:optional => false.freeze,
:arguments => [
+ ENV['CC'].nil? ? "" : ENV['CC'].split[1..-1],
+ ENV['CPPFLAGS'].nil? ? "" : ENV['CPPFLAGS'].split,
'-E'.freeze,
{"-I\"$\"" => 'COLLECTION_PATHS_SOURCE_INCLUDE_VENDOR'}.freeze,
{"-I\"$\"" => 'COLLECTION_PATHS_RELEASE_TOOLCHAIN_INCLUDE'}.freeze,
@@ -147,16 +182,19 @@ DEFAULT_RELEASE_DEPENDENCIES_GENERATOR_TOOL = {
DEFAULT_RELEASE_COMPILER_TOOL = {
- :executable => FilePathUtils.os_executable_ext('gcc').freeze,
+ :executable => ENV['CC'].nil? ? FilePathUtils.os_executable_ext('gcc').freeze : ENV['CC'].split[0],
:name => 'default_release_compiler'.freeze,
:stderr_redirect => StdErrRedirect::NONE.freeze,
:background_exec => BackgroundExec::NONE.freeze,
:optional => false.freeze,
:arguments => [
+ ENV['CC'].nil? ? "" : ENV['CC'].split[1..-1],
+ ENV['CPPFLAGS'].nil? ? "" : ENV['CPPFLAGS'].split,
{"-I\"$\"" => 'COLLECTION_PATHS_SOURCE_INCLUDE_VENDOR'}.freeze,
{"-I\"$\"" => 'COLLECTION_PATHS_RELEASE_TOOLCHAIN_INCLUDE'}.freeze,
{"-D$" => 'COLLECTION_DEFINES_RELEASE_AND_VENDOR'}.freeze,
"-DGNU_COMPILER".freeze,
+ ENV['CFLAGS'].nil? ? "" : ENV['CFLAGS'].split,
"-c \"${1}\"".freeze,
"-o \"${2}\"".freeze,
# gcc's list file output options are complex; no use of ${3} parameter in default config
@@ -166,12 +204,14 @@ DEFAULT_RELEASE_COMPILER_TOOL = {
}
DEFAULT_RELEASE_ASSEMBLER_TOOL = {
- :executable => FilePathUtils.os_executable_ext('as').freeze,
+ :executable => ENV['AS'].nil? ? FilePathUtils.os_executable_ext('as').freeze : ENV['AS'].split[0],
:name => 'default_release_assembler'.freeze,
:stderr_redirect => StdErrRedirect::NONE.freeze,
:background_exec => BackgroundExec::NONE.freeze,
:optional => false.freeze,
:arguments => [
+ ENV['AS'].nil? ? "" : ENV['AS'].split[1..-1],
+ ENV['ASFLAGS'].nil? ? "" : ENV['ASFLAGS'].split,
{"-I\"$\"" => 'COLLECTION_PATHS_SOURCE_AND_INCLUDE'}.freeze,
"\"${1}\"".freeze,
"-o \"${2}\"".freeze,
@@ -179,16 +219,21 @@ DEFAULT_RELEASE_ASSEMBLER_TOOL = {
}
DEFAULT_RELEASE_LINKER_TOOL = {
- :executable => FilePathUtils.os_executable_ext('gcc').freeze,
+ :executable => ENV['CCLD'].nil? ? FilePathUtils.os_executable_ext('gcc').freeze : ENV['CCLD'].split[0],
:name => 'default_release_linker'.freeze,
:stderr_redirect => StdErrRedirect::NONE.freeze,
:background_exec => BackgroundExec::NONE.freeze,
:optional => false.freeze,
:arguments => [
+ ENV['CCLD'].nil? ? "" : ENV['CCLD'].split[1..-1],
+ ENV['CFLAGS'].nil? ? "" : ENV['CFLAGS'].split,
+ ENV['LDFLAGS'].nil? ? "" : ENV['LDFLAGS'].split,
"\"${1}\"".freeze,
+ "${5}".freeze,
"-o \"${2}\"".freeze,
"".freeze,
- "${4}".freeze
+ "${4}".freeze,
+ ENV['LDLIBS'].nil? ? "" : ENV['LDLIBS'].split
].freeze
}
@@ -205,6 +250,7 @@ DEFAULT_TOOLS_TEST_PREPROCESSORS = {
:tools => {
:test_includes_preprocessor => DEFAULT_TEST_INCLUDES_PREPROCESSOR_TOOL,
:test_file_preprocessor => DEFAULT_TEST_FILE_PREPROCESSOR_TOOL,
+ :test_file_preprocessor_directives => DEFAULT_TEST_FILE_PREPROCESSOR_DIRECTIVES_TOOL,
}
}
@@ -245,8 +291,10 @@ DEFAULT_CEEDLING_CONFIG = {
:compile_threads => 1,
:test_threads => 1,
:use_test_preprocessor => false,
+ :use_preprocessor_directives => false,
:use_deep_dependencies => false,
:generate_deep_dependencies => true, # only applicable if use_deep_dependencies is true
+ :auto_link_deep_dependencies => false,
:test_file_prefix => 'test_',
:options_paths => [],
:release_build => false,
@@ -263,6 +311,7 @@ DEFAULT_CEEDLING_CONFIG = {
:source => [], # must be populated by user
:support => [],
:include => [],
+ :libraries => [],
:test_toolchain_include => [],
:release_toolchain_include => [],
},
@@ -290,6 +339,8 @@ DEFAULT_CEEDLING_CONFIG = {
},
:libraries => {
+ :flag => '-l${1}',
+ :path_flag => '-L ${1}',
:test => [],
:test_preprocess => [],
:release => [],
@@ -303,6 +354,7 @@ DEFAULT_CEEDLING_CONFIG = {
:source => '.c',
:assembly => '.s',
:object => '.o',
+ :libraries => ['.a','.so'],
:executable => ( SystemWrapper.windows? ? EXTENSION_WIN_EXE : EXTENSION_NONWIN_EXE ),
:map => '.map',
:list => '.lst',
@@ -345,6 +397,7 @@ DEFAULT_CEEDLING_CONFIG = {
},
:test_includes_preprocessor => { :arguments => [] },
:test_file_preprocessor => { :arguments => [] },
+ :test_file_preprocessor_directives => { :arguments => [] },
:test_dependencies_generator => { :arguments => [] },
:release_compiler => { :arguments => [] },
:release_linker => { :arguments => [] },
diff --git a/test/vendor/ceedling/lib/ceedling/dependinator.rb b/test/unit-test/vendor/ceedling/lib/ceedling/dependinator.rb
similarity index 92%
rename from test/vendor/ceedling/lib/ceedling/dependinator.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/dependinator.rb
index ebd123772..accfe80c9 100644
--- a/test/vendor/ceedling/lib/ceedling/dependinator.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/dependinator.rb
@@ -86,13 +86,12 @@ class Dependinator
def enhance_results_dependencies(result_filepath)
- @rake_wrapper[result_filepath].enhance( [@configurator.project_test_force_rebuild_filepath] ) if (@project_config_manager.test_config_changed ||
- @project_config_manager.test_defines_changed)
+ @rake_wrapper[result_filepath].enhance( [@configurator.project_test_force_rebuild_filepath] ) if @project_config_manager.test_config_changed
end
- def setup_test_executable_dependencies(test, objects)
- @rake_wrapper.create_file_task( @file_path_utils.form_test_executable_filepath(test), objects )
+ def enhance_test_executable_dependencies(test, objects)
+ @rake_wrapper[ @file_path_utils.form_test_executable_filepath(test) ].enhance( objects )
end
end
diff --git a/test/vendor/ceedling/lib/ceedling/erb_wrapper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/erb_wrapper.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/erb_wrapper.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/erb_wrapper.rb
diff --git a/test/vendor/ceedling/lib/ceedling/file_finder.rb b/test/unit-test/vendor/ceedling/lib/ceedling/file_finder.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/file_finder.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/file_finder.rb
diff --git a/test/vendor/ceedling/lib/ceedling/file_finder_helper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/file_finder_helper.rb
similarity index 85%
rename from test/vendor/ceedling/lib/ceedling/file_finder_helper.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/file_finder_helper.rb
index 0a31b44bd..a168e5cb5 100644
--- a/test/vendor/ceedling/lib/ceedling/file_finder_helper.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/file_finder_helper.rb
@@ -25,10 +25,12 @@ class FileFinderHelper
end
- case (complain)
- when :error then blow_up(file_name, extra_message) if (file_to_find.nil?)
- when :warn then gripe(file_name, extra_message) if (file_to_find.nil?)
- #when :ignore then
+ if file_to_find.nil?
+ case (complain)
+ when :error then blow_up(file_name, extra_message)
+ when :warn then gripe(file_name, extra_message)
+ #when :ignore then
+ end
end
return file_to_find
diff --git a/test/vendor/ceedling/lib/ceedling/file_path_utils.rb b/test/unit-test/vendor/ceedling/lib/ceedling/file_path_utils.rb
similarity index 98%
rename from test/vendor/ceedling/lib/ceedling/file_path_utils.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/file_path_utils.rb
index 607039fd5..89a28ba7f 100644
--- a/test/vendor/ceedling/lib/ceedling/file_path_utils.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/file_path_utils.rb
@@ -21,9 +21,11 @@ class FilePathUtils
# standardize path to use '/' path separator & have no trailing path separator
def self.standardize(path)
- path.strip!
- path.gsub!(/\\/, '/')
- path.chomp!('/')
+ if path.is_a? String
+ path.strip!
+ path.gsub!(/\\/, '/')
+ path.chomp!('/')
+ end
return path
end
diff --git a/test/vendor/ceedling/lib/ceedling/file_system_utils.rb b/test/unit-test/vendor/ceedling/lib/ceedling/file_system_utils.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/file_system_utils.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/file_system_utils.rb
diff --git a/test/vendor/ceedling/lib/ceedling/file_system_wrapper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/file_system_wrapper.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/file_system_wrapper.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/file_system_wrapper.rb
diff --git a/test/vendor/ceedling/lib/ceedling/file_wrapper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/file_wrapper.rb
similarity index 88%
rename from test/vendor/ceedling/lib/ceedling/file_wrapper.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/file_wrapper.rb
index 1680ca520..9e5a909b4 100644
--- a/test/vendor/ceedling/lib/ceedling/file_wrapper.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/file_wrapper.rb
@@ -33,15 +33,15 @@ class FileWrapper
end
def rm_f(filepath, options={})
- FileUtils.rm_f(filepath, options)
+ FileUtils.rm_f(filepath, **options)
end
def rm_r(filepath, options={})
- FileUtils.rm_r(filepath, options={})
+ FileUtils.rm_r(filepath, **options={})
end
def cp(source, destination, options={})
- FileUtils.cp(source, destination, options)
+ FileUtils.cp(source, destination, **options)
end
def compare(from, to)
@@ -59,7 +59,7 @@ class FileWrapper
end
def touch(filepath, options={})
- FileUtils.touch(filepath, options)
+ FileUtils.touch(filepath, **options)
end
def write(filepath, contents, flags='w')
diff --git a/test/vendor/ceedling/lib/ceedling/flaginator.rb b/test/unit-test/vendor/ceedling/lib/ceedling/flaginator.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/flaginator.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/flaginator.rb
diff --git a/test/vendor/ceedling/lib/ceedling/generator.rb b/test/unit-test/vendor/ceedling/lib/ceedling/generator.rb
similarity index 96%
rename from test/vendor/ceedling/lib/ceedling/generator.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/generator.rb
index 828a0c025..0b8902475 100644
--- a/test/vendor/ceedling/lib/ceedling/generator.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/generator.rb
@@ -101,19 +101,21 @@ class Generator
shell_result = ex.shell_result
raise ex
ensure
+ arg_hash[:shell_command] = command[:line]
arg_hash[:shell_result] = shell_result
@plugin_manager.post_compile_execute(arg_hash)
end
end
- def generate_executable_file(tool, context, objects, executable, map='', libraries=[])
+ def generate_executable_file(tool, context, objects, executable, map='', libraries=[], libpaths=[])
shell_result = {}
arg_hash = { :tool => tool,
:context => context,
:objects => objects,
:executable => executable,
:map => map,
- :libraries => libraries
+ :libraries => libraries,
+ :libpaths => libpaths
}
@plugin_manager.pre_link_execute(arg_hash)
@@ -125,7 +127,8 @@ class Generator
arg_hash[:objects],
arg_hash[:executable],
arg_hash[:map],
- arg_hash[:libraries]
+ arg_hash[:libraries],
+ arg_hash[:libpaths]
)
@streaminator.stdout_puts("Command: #{command}", Verbosity::DEBUG)
diff --git a/test/vendor/ceedling/lib/ceedling/generator_helper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/generator_helper.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/generator_helper.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/generator_helper.rb
diff --git a/test/vendor/ceedling/lib/ceedling/generator_test_results.rb b/test/unit-test/vendor/ceedling/lib/ceedling/generator_test_results.rb
similarity index 81%
rename from test/vendor/ceedling/lib/ceedling/generator_test_results.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/generator_test_results.rb
index 1d0c5201f..3af2d720a 100644
--- a/test/vendor/ceedling/lib/ceedling/generator_test_results.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/generator_test_results.rb
@@ -37,6 +37,10 @@ class GeneratorTestResults
elements = extract_line_elements(line, results[:source][:file])
results[:successes] << elements[0]
results[:stdout] << elements[1] if (!elements[1].nil?)
+ when /(:PASS \(.* ms\)$)/
+ elements = extract_line_elements(line, results[:source][:file])
+ results[:successes] << elements[0]
+ results[:stdout] << elements[1] if (!elements[1].nil?)
when /(:FAIL)/
elements = extract_line_elements(line, results[:source][:file])
results[:failures] << elements[0]
@@ -73,6 +77,7 @@ class GeneratorTestResults
# handle anything preceding filename in line as extra output to be collected
stdout = nil
stdout_regex = /(.+)#{Regexp.escape(filename)}.+/i
+ unity_test_time = 0
if (line =~ stdout_regex)
stdout = $1.clone
@@ -82,8 +87,14 @@ class GeneratorTestResults
# collect up test results minus and extra output
elements = (line.strip.split(':'))[1..-1]
- return {:test => elements[1], :line => elements[0].to_i, :message => (elements[3..-1].join(':')).strip}, stdout if elements.size >= 3
- return {:test => '???', :line => -1, :message => nil} #fallback safe option. TODO better handling
+ # find timestamp if available
+ if (elements[-1] =~ / \((\d*(?:\.\d*)?) ms\)/)
+ unity_test_time = $1.to_f / 1000
+ elements[-1].sub!(/ \((\d*(?:\.\d*)?) ms\)/, '')
+ end
+
+ return {:test => elements[1], :line => elements[0].to_i, :message => (elements[3..-1].join(':')).strip, :unity_test_time => unity_test_time}, stdout if elements.size >= 3
+ return {:test => '???', :line => -1, :message => nil, :unity_test_time => unity_test_time} #fallback safe option. TODO better handling
end
end
diff --git a/test/vendor/ceedling/lib/ceedling/generator_test_results_sanity_checker.rb b/test/unit-test/vendor/ceedling/lib/ceedling/generator_test_results_sanity_checker.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/generator_test_results_sanity_checker.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/generator_test_results_sanity_checker.rb
diff --git a/test/vendor/ceedling/lib/ceedling/generator_test_runner.rb b/test/unit-test/vendor/ceedling/lib/ceedling/generator_test_runner.rb
similarity index 89%
rename from test/vendor/ceedling/lib/ceedling/generator_test_runner.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/generator_test_runner.rb
index 6999faf96..79ed7140f 100644
--- a/test/vendor/ceedling/lib/ceedling/generator_test_runner.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/generator_test_runner.rb
@@ -44,13 +44,15 @@ class GeneratorTestRunner
def generate(module_name, runner_filepath, test_cases, mock_list, test_file_includes=[])
require 'generate_test_runner.rb'
+ header_extension = @configurator.extension_header
+
#actually build the test runner using Unity's test runner generator
#(there is no need to use preprocessor here because we've already looked up test cases and are passing them in here)
@test_runner_generator ||= UnityTestRunnerGenerator.new( @configurator.get_runner_config )
@test_runner_generator.generate( module_name,
runner_filepath,
test_cases,
- mock_list,
- test_file_includes)
+ mock_list.map{|f| File.basename(f,'.*')+header_extension},
+ test_file_includes.map{|f| File.basename(f,'.*')+header_extension})
end
end
diff --git a/test/vendor/ceedling/lib/ceedling/loginator.rb b/test/unit-test/vendor/ceedling/lib/ceedling/loginator.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/loginator.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/loginator.rb
diff --git a/test/vendor/ceedling/lib/ceedling/makefile.rb b/test/unit-test/vendor/ceedling/lib/ceedling/makefile.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/makefile.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/makefile.rb
diff --git a/test/vendor/ceedling/lib/ceedling/objects.yml b/test/unit-test/vendor/ceedling/lib/ceedling/objects.yml
similarity index 88%
rename from test/vendor/ceedling/lib/ceedling/objects.yml
rename to test/unit-test/vendor/ceedling/lib/ceedling/objects.yml
index 2e2e9b9d2..43bbc066c 100644
--- a/test/vendor/ceedling/lib/ceedling/objects.yml
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/objects.yml
@@ -17,11 +17,11 @@ reportinator:
rake_utils:
compose:
- - rake_wrapper
+ - rake_wrapper
system_utils:
compose:
- - system_wrapper
+ - system_wrapper
file_path_utils:
compose:
@@ -203,13 +203,13 @@ generator_helper:
generator_test_results:
compose:
- - configurator
+ - configurator
- generator_test_results_sanity_checker
- - yaml_wrapper
+ - yaml_wrapper
generator_test_results_sanity_checker:
compose:
- - configurator
+ - configurator
- streaminator
generator_test_runner:
@@ -223,43 +223,46 @@ dependinator:
- configurator
- project_config_manager
- test_includes_extractor
- - file_path_utils
+ - file_path_utils
- rake_wrapper
- file_wrapper
preprocessinator:
compose:
- - preprocessinator_helper
+ - preprocessinator_helper
- preprocessinator_includes_handler
- preprocessinator_file_handler
- - task_invoker
+ - task_invoker
- file_path_utils
- yaml_wrapper
+ - project_config_manager
+ - configurator
preprocessinator_helper:
- compose:
- - configurator
- - test_includes_extractor
- - task_invoker
- - file_finder
- - file_path_utils
+ compose:
+ - configurator
+ - test_includes_extractor
+ - task_invoker
+ - file_finder
+ - file_path_utils
preprocessinator_includes_handler:
compose:
- - configurator
- - tool_executor
- - task_invoker
- - file_path_utils
- - yaml_wrapper
- - file_wrapper
+ - configurator
+ - tool_executor
+ - task_invoker
+ - file_path_utils
+ - yaml_wrapper
+ - file_wrapper
+ - file_finder
preprocessinator_file_handler:
compose:
- preprocessinator_extractor
- - configurator
- - tool_executor
+ - configurator
+ - tool_executor
- file_path_utils
- - file_wrapper
+ - file_wrapper
preprocessinator_extractor:
diff --git a/test/vendor/ceedling/lib/ceedling/par_map.rb b/test/unit-test/vendor/ceedling/lib/ceedling/par_map.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/par_map.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/par_map.rb
diff --git a/test/vendor/ceedling/lib/ceedling/plugin.rb b/test/unit-test/vendor/ceedling/lib/ceedling/plugin.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/plugin.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/plugin.rb
diff --git a/test/vendor/ceedling/lib/ceedling/plugin_builder.rb b/test/unit-test/vendor/ceedling/lib/ceedling/plugin_builder.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/plugin_builder.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/plugin_builder.rb
diff --git a/test/vendor/ceedling/lib/ceedling/plugin_manager.rb b/test/unit-test/vendor/ceedling/lib/ceedling/plugin_manager.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/plugin_manager.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/plugin_manager.rb
diff --git a/test/vendor/ceedling/lib/ceedling/plugin_manager_helper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/plugin_manager_helper.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/plugin_manager_helper.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/plugin_manager_helper.rb
diff --git a/test/vendor/ceedling/lib/ceedling/plugin_reportinator.rb b/test/unit-test/vendor/ceedling/lib/ceedling/plugin_reportinator.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/plugin_reportinator.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/plugin_reportinator.rb
diff --git a/test/vendor/ceedling/lib/ceedling/plugin_reportinator_helper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/plugin_reportinator_helper.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/plugin_reportinator_helper.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/plugin_reportinator_helper.rb
diff --git a/test/unit-test/vendor/ceedling/lib/ceedling/preprocessinator.rb b/test/unit-test/vendor/ceedling/lib/ceedling/preprocessinator.rb
new file mode 100644
index 000000000..52d82ca29
--- /dev/null
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/preprocessinator.rb
@@ -0,0 +1,56 @@
+
+class Preprocessinator
+
+ constructor :preprocessinator_helper, :preprocessinator_includes_handler, :preprocessinator_file_handler, :task_invoker, :file_path_utils, :yaml_wrapper, :project_config_manager, :configurator
+
+
+ def setup
+ # fashion ourselves callbacks @preprocessinator_helper can use
+ @preprocess_includes_proc = Proc.new { |filepath| self.preprocess_shallow_includes(filepath) }
+ @preprocess_mock_file_proc = Proc.new { |filepath| self.preprocess_file(filepath) }
+ @preprocess_test_file_directives_proc = Proc.new { |filepath| self.preprocess_file_directives(filepath) }
+ @preprocess_test_file_proc = Proc.new { |filepath| self.preprocess_file(filepath) }
+ end
+
+ def preprocess_shallow_source_includes(test)
+ @preprocessinator_helper.preprocess_source_includes(test)
+ end
+
+ def preprocess_test_and_invoke_test_mocks(test)
+ @preprocessinator_helper.preprocess_includes(test, @preprocess_includes_proc)
+
+ mocks_list = @preprocessinator_helper.assemble_mocks_list(test)
+
+ @project_config_manager.process_test_defines_change(mocks_list)
+
+ @preprocessinator_helper.preprocess_mockable_headers(mocks_list, @preprocess_mock_file_proc)
+
+ @task_invoker.invoke_test_mocks(mocks_list)
+
+ if (@configurator.project_use_preprocessor_directives)
+ @preprocessinator_helper.preprocess_test_file(test, @preprocess_test_file_directives_proc)
+ else
+ @preprocessinator_helper.preprocess_test_file(test, @preprocess_test_file_proc)
+ end
+
+ return mocks_list
+ end
+
+ def preprocess_shallow_includes(filepath)
+ includes = @preprocessinator_includes_handler.extract_includes(filepath)
+
+ @preprocessinator_includes_handler.write_shallow_includes_list(
+ @file_path_utils.form_preprocessed_includes_list_filepath(filepath), includes)
+ end
+
+ def preprocess_file(filepath)
+ @preprocessinator_includes_handler.invoke_shallow_includes_list(filepath)
+ @preprocessinator_file_handler.preprocess_file( filepath, @yaml_wrapper.load(@file_path_utils.form_preprocessed_includes_list_filepath(filepath)) )
+ end
+
+ def preprocess_file_directives(filepath)
+ @preprocessinator_includes_handler.invoke_shallow_includes_list( filepath )
+ @preprocessinator_file_handler.preprocess_file_directives( filepath,
+ @yaml_wrapper.load( @file_path_utils.form_preprocessed_includes_list_filepath( filepath ) ) )
+ end
+end
diff --git a/test/vendor/ceedling/lib/ceedling/preprocessinator_extractor.rb b/test/unit-test/vendor/ceedling/lib/ceedling/preprocessinator_extractor.rb
similarity index 56%
rename from test/vendor/ceedling/lib/ceedling/preprocessinator_extractor.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/preprocessinator_extractor.rb
index 49509a8b0..62026e15a 100644
--- a/test/vendor/ceedling/lib/ceedling/preprocessinator_extractor.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/preprocessinator_extractor.rb
@@ -16,6 +16,7 @@ class PreprocessinatorExtractor
lines = []
File.readlines(filepath).each do |line|
+ line.encode!('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
if found_file and not line =~ not_pragma
lines << line
else
@@ -27,4 +28,28 @@ class PreprocessinatorExtractor
return lines
end
+
+ def extract_base_file_from_preprocessed_directives(filepath)
+ # preprocessing by way of toolchain preprocessor eliminates directives only
+ # like #ifdef's and leave other code
+
+ # iterate through all lines and only get last chunk of file after a last
+ # '#'line containing file name of our filepath
+
+ base_name = File.basename(filepath)
+ pattern = /^#.*(\s|\/|\\|\")#{Regexp.escape(base_name)}/
+ found_file = false # have we found the file we care about?
+
+ lines = []
+ File.readlines(filepath).each do |line|
+ line.encode!('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
+ lines << line
+
+ if line =~ pattern
+ lines = []
+ end
+ end
+
+ return lines
+ end
end
diff --git a/test/vendor/ceedling/lib/ceedling/preprocessinator_file_handler.rb b/test/unit-test/vendor/ceedling/lib/ceedling/preprocessinator_file_handler.rb
similarity index 54%
rename from test/vendor/ceedling/lib/ceedling/preprocessinator_file_handler.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/preprocessinator_file_handler.rb
index b6b5efbc6..978fa0d05 100644
--- a/test/vendor/ceedling/lib/ceedling/preprocessinator_file_handler.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/preprocessinator_file_handler.rb
@@ -18,4 +18,17 @@ class PreprocessinatorFileHandler
@file_wrapper.write(preprocessed_filepath, contents.join("\n"))
end
+ def preprocess_file_directives(filepath, includes)
+ preprocessed_filepath = @file_path_utils.form_preprocessed_file_filepath(filepath)
+
+ command = @tool_executor.build_command_line(@configurator.tools_test_file_preprocessor_directives, [], filepath, preprocessed_filepath)
+ @tool_executor.exec(command[:line], command[:options])
+
+ contents = @preprocessinator_extractor.extract_base_file_from_preprocessed_directives(preprocessed_filepath)
+
+ includes.each{|include| contents.unshift("#include \"#{include}\"")}
+
+ @file_wrapper.write(preprocessed_filepath, contents.join("\n"))
+ end
+
end
diff --git a/test/vendor/ceedling/lib/ceedling/preprocessinator_helper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/preprocessinator_helper.rb
similarity index 93%
rename from test/vendor/ceedling/lib/ceedling/preprocessinator_helper.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/preprocessinator_helper.rb
index 1419a5616..4bbda67fc 100644
--- a/test/vendor/ceedling/lib/ceedling/preprocessinator_helper.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/preprocessinator_helper.rb
@@ -15,6 +15,10 @@ class PreprocessinatorHelper
end
end
+ def preprocess_source_includes(test)
+ @test_includes_extractor.parse_test_file_source_include(test)
+ end
+
def assemble_mocks_list(test)
return @file_path_utils.form_mocks_source_filelist( @test_includes_extractor.lookup_raw_mock_list(test) )
end
diff --git a/test/vendor/ceedling/lib/ceedling/preprocessinator_includes_handler.rb b/test/unit-test/vendor/ceedling/lib/ceedling/preprocessinator_includes_handler.rb
similarity index 54%
rename from test/vendor/ceedling/lib/ceedling/preprocessinator_includes_handler.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/preprocessinator_includes_handler.rb
index 703c84f3c..8b89c0b3a 100644
--- a/test/vendor/ceedling/lib/ceedling/preprocessinator_includes_handler.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/preprocessinator_includes_handler.rb
@@ -2,7 +2,7 @@
class PreprocessinatorIncludesHandler
- constructor :configurator, :tool_executor, :task_invoker, :file_path_utils, :yaml_wrapper, :file_wrapper
+ constructor :configurator, :tool_executor, :task_invoker, :file_path_utils, :yaml_wrapper, :file_wrapper, :file_finder
@@makefile_cache = {}
# shallow includes: only those headers a source file explicitly includes
@@ -65,6 +65,7 @@ class PreprocessinatorIncludesHandler
to_process = [filepath]
ignore_list = []
list = []
+ all_mocks = []
include_paths = @configurator.project_config_hash[:collection_paths_include]
include_paths = [] if include_paths.nil?
@@ -73,12 +74,10 @@ class PreprocessinatorIncludesHandler
while to_process.length > 0
target = to_process.shift()
ignore_list << target
- # puts "[HELL] Processing: \t\t#{target}"
- new_deps, new_to_process = extract_includes_helper(target, include_paths, ignore_list)
+ new_deps, new_to_process, all_mocks = extract_includes_helper(target, include_paths, ignore_list, all_mocks)
list += new_deps
to_process += new_to_process
- if (!@configurator.project_config_hash.has_key?(:project_auto_link_deep_dependencies) or
- !@configurator.project_config_hash[:project_auto_link_deep_dependencies])
+ if !@configurator.project_config_hash[:project_auto_link_deep_dependencies]
break
else
list = list.uniq()
@@ -89,93 +88,102 @@ class PreprocessinatorIncludesHandler
return list
end
- def extract_includes_helper(filepath, include_paths, ignore_list)
+ def extract_includes_helper(filepath, include_paths, ignore_list, mocks)
# Extract the dependencies from the make rule
- hdr_ext = @configurator.extension_header
make_rule = self.form_shallow_dependencies_rule(filepath)
- dependencies = make_rule.split.find_all {|path| path.end_with?(hdr_ext) }.uniq
- dependencies.map! {|hdr| hdr.gsub('\\','/') }
+ target_file = make_rule.split[0].gsub(':', '').gsub('\\','/')
+ base = File.basename(target_file, File.extname(target_file))
+ make_rule_dependencies = make_rule.gsub(/.*\b#{Regexp.escape(base)}\S*/, '').gsub(/\\$/, '')
+
+ # Extract the headers dependencies from the make rule
+ hdr_ext = @configurator.extension_header
+ headers_dependencies = make_rule_dependencies.split.find_all {|path| path.end_with?(hdr_ext) }.uniq
+ headers_dependencies.map! {|hdr| hdr.gsub('\\','/') }
+ full_path_headers_dependencies = extract_full_path_dependencies(headers_dependencies)
- # Separate the real files form the annotated ones and remove the '@@@@'
- annotated_headers, real_headers = dependencies.partition {|hdr| hdr =~ /^@@@@/ }
- annotated_headers.map! {|hdr| hdr.gsub('@@@@','') }
- # Matching annotated_headers values against real_headers to ensure that
- # annotated_headers contain full path entries (as returned by make rule)
- annotated_headers.map! {|hdr| real_headers.find {|real_hdr| !real_hdr.match(/(.*\/)?#{Regexp.escape(hdr)}/).nil? } }
- annotated_headers = annotated_headers.compact
+ # Extract the sources dependencies from the make rule
+ src_ext = @configurator.extension_source
+ sources_dependencies = make_rule_dependencies.split.find_all {|path| path.end_with?(src_ext) }.uniq
+ sources_dependencies.map! {|src| src.gsub('\\','/') }
+ full_path_sources_dependencies = extract_full_path_dependencies(sources_dependencies)
- # Find which of our annotated headers are "real" dependencies. This is
- # intended to weed out dependencies that have been removed due to build
- # options defined in the project yaml and/or in the headers themselves.
- list = annotated_headers.find_all do |annotated_header|
- # find the index of the "real" include that matches the annotated one.
- idx = real_headers.find_index do |real_header|
- real_header =~ /^(.*\/)?#{Regexp.escape(annotated_header)}$/
- end
- # If we found a real include, delete it from the array and return it,
- # otherwise return nil. Since nil is falsy this has the effect of making
- # find_all return only the annotated headers for which a real include was
- # found/deleted
- idx ? real_headers.delete_at(idx) : nil
+ list = full_path_headers_dependencies + full_path_sources_dependencies
+
+ mock_prefix = @configurator.project_config_hash[:cmock_mock_prefix]
+ # Creating list of mocks
+ mocks += full_path_headers_dependencies.find_all do |header|
+ File.basename(header) =~ /^#{mock_prefix}.*$/
end.compact
- # Extract direct dependencies that were also added
- src_ext = @configurator.extension_source
- sdependencies = make_rule.split.find_all {|path| path.end_with?(src_ext) }.uniq
- sdependencies.map! {|hdr| hdr.gsub('\\','/') }
- list += sdependencies
+ # ignore real file when both mock and real file exist
+ mocks.each do |mock|
+ list.each do |filename|
+ if File.basename(filename) == File.basename(mock).sub(mock_prefix, '')
+ ignore_list << filename
+ end
+ end
+ end.compact
+
+ # Filtering list of final includes to only include mocks and anything that is NOT in the ignore_list
+ list = list.select do |item|
+ mocks.include? item or !(ignore_list.any? { |ignore_item| !item.match(/^(.*\/)?#{Regexp.escape(ignore_item)}$/).nil? })
+ end
to_process = []
- if @configurator.project_config_hash.has_key?(:project_auto_link_deep_dependencies) && @configurator.project_config_hash[:project_auto_link_deep_dependencies]
- # Creating list of mocks
- mocks = annotated_headers.find_all do |annotated_header|
- File.basename(annotated_header) =~ /^#{@configurator.project_config_hash[:cmock_mock_prefix]}.*$/
- end.compact
-
+ if @configurator.project_config_hash[:project_auto_link_deep_dependencies]
# Creating list of headers that should be recursively pre-processed
- # Skipping mocks and unity.h
- headers_to_deep_link = annotated_headers.select do |annotated_header|
- !(mocks.include? annotated_header) and (annotated_header.match(/^(.*\/)?unity\.h$/).nil?)
- end
- headers_to_deep_link.map! {|hdr| File.expand_path(hdr)}
-
- mocks.each do |mock|
- dirname = File.dirname(mock)
- #basename = File.basename(mock).delete_prefix(@configurator.project_config_hash[:cmock_mock_prefix])
- basename = File.basename(mock).sub(@configurator.project_config_hash[:cmock_mock_prefix], '')
- if dirname != "."
- ignore_list << File.join(dirname, basename)
- else
- ignore_list << basename
- end
- end.compact
-
- # Filtering list of final includes to only include mocks and anything that is NOT in the ignore_list
- list = list.select do |item|
- mocks.include? item or !(ignore_list.any? { |ignore_item| !item.match(/^(.*\/)?#{Regexp.escape(ignore_item)}$/).nil? })
+ # Skipping mocks and vendor headers
+ headers_to_deep_link = full_path_headers_dependencies.select do |hdr|
+ !(mocks.include? hdr) and (hdr.match(/^(.*\/)(#{VENDORS_FILES.join('|')}) + #{Regexp.escape(hdr_ext)}$/).nil?)
end
+ headers_to_deep_link.map! {|hdr| File.expand_path(hdr) }
+ headers_to_deep_link.compact!
headers_to_deep_link.each do |hdr|
if (ignore_list.none? {|ignore_header| hdr.match(/^(.*\/)?#{Regexp.escape(ignore_header)}$/)} and
include_paths.none? {|include_path| hdr =~ /^#{include_path}\.*/})
if File.exist?(hdr)
to_process << hdr
- #source_file = hdr.delete_suffix(hdr_ext) + src_ext
- source_file = hdr.chomp(hdr_ext) + src_ext
- if source_file != hdr and File.exist?(source_file)
- to_process << source_file
- end
+ src = @file_finder.find_compilation_input_file(hdr, :ignore)
+ to_process << src if src
end
end
end
end
- return list, to_process
+ return list, to_process, mocks
end
def write_shallow_includes_list(filepath, list)
@yaml_wrapper.dump(filepath, list)
end
+
+ private
+
+ def extract_full_path_dependencies(dependencies)
+ # Separate the real files form the annotated ones and remove the '@@@@'
+ annotated_files, real_files = dependencies.partition {|file| file =~ /^@@@@/}
+ annotated_files.map! {|file| file.gsub('@@@@','') }
+ # Matching annotated_files values against real_files to ensure that
+ # annotated_files contain full path entries (as returned by make rule)
+ annotated_files.map! {|file| real_files.find {|real| !real.match(/^(.*\/)?#{Regexp.escape(file)}$/).nil?}}
+ annotated_files = annotated_files.compact
+
+ # Find which of our annotated files are "real" dependencies. This is
+ # intended to weed out dependencies that have been removed due to build
+ # options defined in the project yaml and/or in the files themselves.
+ return annotated_files.find_all do |annotated_file|
+ # find the index of the "real" file that matches the annotated one.
+ idx = real_files.find_index do |real_file|
+ real_file =~ /^(.*\/)?#{Regexp.escape(annotated_file)}$/
+ end
+ # If we found a real file, delete it from the array and return it,
+ # otherwise return nil. Since nil is falsy this has the effect of making
+ # find_all return only the annotated filess for which a real file was
+ # found/deleted
+ idx ? real_files.delete_at(idx) : nil
+ end.compact
+ end
end
diff --git a/test/vendor/ceedling/lib/ceedling/project_config_manager.rb b/test/unit-test/vendor/ceedling/lib/ceedling/project_config_manager.rb
similarity index 75%
rename from test/vendor/ceedling/lib/ceedling/project_config_manager.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/project_config_manager.rb
index 31f7e3a68..ed7a73b8c 100644
--- a/test/vendor/ceedling/lib/ceedling/project_config_manager.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/project_config_manager.rb
@@ -21,9 +21,15 @@ class ProjectConfigManager
@options_files << File.basename( option_filepath )
config_hash.deep_merge!( @yaml_wrapper.load( option_filepath ) )
end
-
-
+
+ def filter_internal_sources(sources)
+ filtered_sources = sources.clone
+ filtered_sources.delete_if { |item| item =~ /#{CMOCK_MOCK_PREFIX}.+#{Regexp.escape(EXTENSION_SOURCE)}$/ }
+ filtered_sources.delete_if { |item| item =~ /#{VENDORS_FILES.map{|source| '\b' + Regexp.escape(source.ext(EXTENSION_SOURCE)) + '\b'}.join('|')}$/ }
+ return filtered_sources
+ end
+
def process_release_config_change
# has project configuration changed since last release build
@release_config_changed = @cacheinator.diff_cached_release_config?( @config_hash )
@@ -40,7 +46,7 @@ class ProjectConfigManager
@test_defines_changed = @cacheinator.diff_cached_test_defines?( files )
if @test_defines_changed
# update timestamp for rake task prerequisites
- @file_wrapper.touch( @configurator.project_test_force_rebuild_filepath )
+ @file_wrapper.touch( @configurator.project_test_force_rebuild_filepath, :mtime => Time.now + 10 )
end
end
end
diff --git a/test/vendor/ceedling/lib/ceedling/project_file_loader.rb b/test/unit-test/vendor/ceedling/lib/ceedling/project_file_loader.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/project_file_loader.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/project_file_loader.rb
diff --git a/test/vendor/ceedling/lib/ceedling/rake_utils.rb b/test/unit-test/vendor/ceedling/lib/ceedling/rake_utils.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/rake_utils.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/rake_utils.rb
diff --git a/test/vendor/ceedling/lib/ceedling/rake_wrapper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/rake_wrapper.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/rake_wrapper.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/rake_wrapper.rb
diff --git a/test/vendor/ceedling/lib/ceedling/rakefile.rb b/test/unit-test/vendor/ceedling/lib/ceedling/rakefile.rb
similarity index 97%
rename from test/vendor/ceedling/lib/ceedling/rakefile.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/rakefile.rb
index 37001bacc..1bcb82491 100644
--- a/test/vendor/ceedling/lib/ceedling/rakefile.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/rakefile.rb
@@ -10,7 +10,6 @@ $LOAD_PATH.unshift( CEEDLING_LIB )
$LOAD_PATH.unshift( File.join(CEEDLING_VENDOR, 'unity/auto') )
$LOAD_PATH.unshift( File.join(CEEDLING_VENDOR, 'diy/lib') )
$LOAD_PATH.unshift( File.join(CEEDLING_VENDOR, 'cmock/lib') )
-$LOAD_PATH.unshift( File.join(CEEDLING_VENDOR, 'deep_merge/lib') )
require 'rake'
diff --git a/test/vendor/ceedling/lib/ceedling/release_invoker.rb b/test/unit-test/vendor/ceedling/lib/ceedling/release_invoker.rb
similarity index 66%
rename from test/vendor/ceedling/lib/ceedling/release_invoker.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/release_invoker.rb
index 5bfb6cd70..19bbca727 100644
--- a/test/vendor/ceedling/lib/ceedling/release_invoker.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/release_invoker.rb
@@ -56,15 +56,40 @@ class ReleaseInvoker
end
def convert_libraries_to_arguments(libraries)
- args = (libraries || []) + ((defined? LIBRARIES_SYSTEM) ? LIBRARIES_SYSTEM : [])
+ args = ((libraries || []) + ((defined? LIBRARIES_SYSTEM) ? LIBRARIES_SYSTEM : [])).flatten
if (defined? LIBRARIES_FLAG)
args.map! {|v| LIBRARIES_FLAG.gsub(/\$\{1\}/, v) }
end
return args
end
+ def get_library_paths_to_arguments()
+ paths = (defined? PATHS_LIBRARIES) ? (PATHS_LIBRARIES || []).clone : []
+ if (defined? LIBRARIES_PATH_FLAG)
+ paths.map! {|v| LIBRARIES_PATH_FLAG.gsub(/\$\{1\}/, v) }
+ end
+ return paths
+ end
+
def sort_objects_and_libraries(both)
- extension = "\\" + ((defined? EXTENSION_SUBPROJECTS) ? EXTENSION_SUBPROJECTS : ".LIBRARY")
+ extension = if ((defined? EXTENSION_SUBPROJECTS) && (defined? EXTENSION_LIBRARIES))
+ extension_libraries = if (EXTENSION_LIBRARIES.class == Array)
+ EXTENSION_LIBRARIES.join(")|(?:\\")
+ else
+ EXTENSION_LIBRARIES
+ end
+ "(?:\\#{EXTENSION_SUBPROJECTS})|(?:\\#{extension_libraries})"
+ elsif (defined? EXTENSION_SUBPROJECTS)
+ "\\#{EXTENSION_SUBPROJECTS}"
+ elsif (defined? EXTENSION_LIBRARIES)
+ if (EXTENSION_LIBRARIES.class == Array)
+ "(?:\\#{EXTENSION_LIBRARIES.join(")|(?:\\")})"
+ else
+ "\\#{EXTENSION_LIBRARIES}"
+ end
+ else
+ "\\.LIBRARY"
+ end
sorted_objects = both.group_by {|v| v.match(/.+#{extension}$/) ? :libraries : :objects }
libraries = sorted_objects[:libraries] || []
objects = sorted_objects[:objects] || []
diff --git a/test/vendor/ceedling/lib/ceedling/release_invoker_helper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/release_invoker_helper.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/release_invoker_helper.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/release_invoker_helper.rb
diff --git a/test/vendor/ceedling/lib/ceedling/reportinator.rb b/test/unit-test/vendor/ceedling/lib/ceedling/reportinator.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/reportinator.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/reportinator.rb
diff --git a/test/vendor/ceedling/lib/ceedling/rules_cmock.rake b/test/unit-test/vendor/ceedling/lib/ceedling/rules_cmock.rake
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/rules_cmock.rake
rename to test/unit-test/vendor/ceedling/lib/ceedling/rules_cmock.rake
diff --git a/test/vendor/ceedling/lib/ceedling/rules_preprocess.rake b/test/unit-test/vendor/ceedling/lib/ceedling/rules_preprocess.rake
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/rules_preprocess.rake
rename to test/unit-test/vendor/ceedling/lib/ceedling/rules_preprocess.rake
diff --git a/test/vendor/ceedling/lib/ceedling/rules_release.rake b/test/unit-test/vendor/ceedling/lib/ceedling/rules_release.rake
similarity index 73%
rename from test/vendor/ceedling/lib/ceedling/rules_release.rake
rename to test/unit-test/vendor/ceedling/lib/ceedling/rules_release.rake
index ae39a76e3..4a583bd6c 100644
--- a/test/vendor/ceedling/lib/ceedling/rules_release.rake
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/rules_release.rake
@@ -2,6 +2,17 @@
RELEASE_COMPILE_TASK_ROOT = RELEASE_TASK_ROOT + 'compile:' unless defined?(RELEASE_COMPILE_TASK_ROOT)
RELEASE_ASSEMBLE_TASK_ROOT = RELEASE_TASK_ROOT + 'assemble:' unless defined?(RELEASE_ASSEMBLE_TASK_ROOT)
+# If GCC and Releasing a Library, Update Tools to Automatically Have Necessary Tags
+if (TOOLS_RELEASE_COMPILER[:executable] == DEFAULT_RELEASE_COMPILER_TOOL[:executable])
+ if (File.extname(PROJECT_RELEASE_BUILD_TARGET) == '.so')
+ TOOLS_RELEASE_COMPILER[:arguments] << "-fPIC" unless TOOLS_RELEASE_COMPILER[:arguments].include?("-fPIC")
+ TOOLS_RELEASE_LINKER[:arguments] << "-shared" unless TOOLS_RELEASE_LINKER[:arguments].include?("-shared")
+ elsif (File.extname(PROJECT_RELEASE_BUILD_TARGET) == '.a')
+ TOOLS_RELEASE_COMPILER[:arguments] << "-fPIC" unless TOOLS_RELEASE_COMPILER[:arguments].include?("-fPIC")
+ TOOLS_RELEASE_LINKER[:executable] = 'ar'
+ TOOLS_RELEASE_LINKER[:arguments] = ['rcs', '${2}', '${1}'].compact
+ end
+end
if (RELEASE_BUILD_USE_ASSEMBLY)
rule(/#{PROJECT_RELEASE_BUILD_OUTPUT_ASM_PATH}\/#{'.+\\'+EXTENSION_OBJECT}$/ => [
@@ -37,16 +48,18 @@ end
rule(/#{PROJECT_RELEASE_BUILD_TARGET}/) do |bin_file|
objects, libraries = @ceedling[:release_invoker].sort_objects_and_libraries(bin_file.prerequisites)
- tool = TOOLS_RELEASE_LINKER.clone
- lib_args = @ceedling[:release_invoker].convert_libraries_to_arguments(libraries)
- map_file = @ceedling[:configurator].project_release_build_map
+ tool = TOOLS_RELEASE_LINKER.clone
+ lib_args = @ceedling[:release_invoker].convert_libraries_to_arguments(libraries)
+ lib_paths = @ceedling[:release_invoker].get_library_paths_to_arguments()
+ map_file = @ceedling[:configurator].project_release_build_map
@ceedling[:generator].generate_executable_file(
tool,
RELEASE_SYM,
objects,
bin_file.name,
map_file,
- lib_args )
+ lib_args,
+ lib_paths )
@ceedling[:release_invoker].artifactinate( bin_file.name, map_file, @ceedling[:configurator].release_build_artifacts )
end
diff --git a/test/vendor/ceedling/lib/ceedling/rules_release_deep_dependencies.rake b/test/unit-test/vendor/ceedling/lib/ceedling/rules_release_deep_dependencies.rake
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/rules_release_deep_dependencies.rake
rename to test/unit-test/vendor/ceedling/lib/ceedling/rules_release_deep_dependencies.rake
diff --git a/test/vendor/ceedling/lib/ceedling/rules_tests.rake b/test/unit-test/vendor/ceedling/lib/ceedling/rules_tests.rake
similarity index 91%
rename from test/vendor/ceedling/lib/ceedling/rules_tests.rake
rename to test/unit-test/vendor/ceedling/lib/ceedling/rules_tests.rake
index 2b8f7af5b..61e15e2cc 100644
--- a/test/vendor/ceedling/lib/ceedling/rules_tests.rake
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/rules_tests.rake
@@ -34,16 +34,16 @@ end
rule(/#{PROJECT_TEST_BUILD_OUTPUT_PATH}\/#{'.+\\'+EXTENSION_EXECUTABLE}$/) do |bin_file|
-
lib_args = @ceedling[:test_invoker].convert_libraries_to_arguments()
-
+ lib_paths = @ceedling[:test_invoker].get_library_paths_to_arguments()
@ceedling[:generator].generate_executable_file(
TOOLS_TEST_LINKER,
TEST_SYM,
bin_file.prerequisites,
bin_file.name,
@ceedling[:file_path_utils].form_test_build_map_filepath( bin_file.name ),
- lib_args )
+ lib_args,
+ lib_paths )
end
@@ -66,8 +66,7 @@ namespace TEST_SYM do
@ceedling[:file_finder].find_test_from_file_path(test)
end
]) do |test|
- @ceedling[:rake_wrapper][:directories].reenable if @ceedling[:task_invoker].first_run == false && @ceedling[:project_config_manager].test_defines_changed
- @ceedling[:rake_wrapper][:directories].invoke
+ @ceedling[:rake_wrapper][:test_deps].invoke
@ceedling[:test_invoker].setup_and_invoke([test.source])
end
end
diff --git a/test/vendor/ceedling/lib/ceedling/rules_tests_deep_dependencies.rake b/test/unit-test/vendor/ceedling/lib/ceedling/rules_tests_deep_dependencies.rake
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/rules_tests_deep_dependencies.rake
rename to test/unit-test/vendor/ceedling/lib/ceedling/rules_tests_deep_dependencies.rake
diff --git a/test/vendor/ceedling/lib/ceedling/setupinator.rb b/test/unit-test/vendor/ceedling/lib/ceedling/setupinator.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/setupinator.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/setupinator.rb
index 8347b42ab..ea78fd97e 100644
--- a/test/vendor/ceedling/lib/ceedling/setupinator.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/setupinator.rb
@@ -25,8 +25,8 @@ class Setupinator
@ceedling[:configurator].populate_cmock_defaults( config_hash )
@ceedling[:configurator].find_and_merge_plugins( config_hash )
@ceedling[:configurator].merge_imports( config_hash )
- @ceedling[:configurator].tools_setup( config_hash )
@ceedling[:configurator].eval_environment_variables( config_hash )
+ @ceedling[:configurator].tools_setup( config_hash )
@ceedling[:configurator].eval_paths( config_hash )
@ceedling[:configurator].standardize_paths( config_hash )
@ceedling[:configurator].validate( config_hash )
diff --git a/test/vendor/ceedling/lib/ceedling/stream_wrapper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/stream_wrapper.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/stream_wrapper.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/stream_wrapper.rb
diff --git a/test/vendor/ceedling/lib/ceedling/streaminator.rb b/test/unit-test/vendor/ceedling/lib/ceedling/streaminator.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/streaminator.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/streaminator.rb
diff --git a/test/vendor/ceedling/lib/ceedling/streaminator_helper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/streaminator_helper.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/streaminator_helper.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/streaminator_helper.rb
diff --git a/test/vendor/ceedling/lib/ceedling/system_utils.rb b/test/unit-test/vendor/ceedling/lib/ceedling/system_utils.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/system_utils.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/system_utils.rb
diff --git a/test/vendor/ceedling/lib/ceedling/system_wrapper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/system_wrapper.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/system_wrapper.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/system_wrapper.rb
diff --git a/test/vendor/ceedling/lib/ceedling/target_loader.rb b/test/unit-test/vendor/ceedling/lib/ceedling/target_loader.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/target_loader.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/target_loader.rb
diff --git a/test/vendor/ceedling/lib/ceedling/task_invoker.rb b/test/unit-test/vendor/ceedling/lib/ceedling/task_invoker.rb
similarity index 80%
rename from test/vendor/ceedling/lib/ceedling/task_invoker.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/task_invoker.rb
index 642695c46..7bfabbb12 100644
--- a/test/vendor/ceedling/lib/ceedling/task_invoker.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/task_invoker.rb
@@ -46,25 +46,31 @@ class TaskInvoker
return @rake_utils.task_invoked?(regex)
end
-
+ def reset_rake_task_for_changed_defines(file)
+ if !(file =~ /#{VENDORS_FILES.map{|ignore| '\b' + ignore.ext(File.extname(file)) + '\b'}.join('|')}$/)
+ @rake_wrapper[file].clear_actions if @first_run == false && @project_config_manager.test_defines_changed
+ @rake_wrapper[file].reenable if @first_run == false && @project_config_manager.test_defines_changed
+ end
+ end
+
def invoke_test_mocks(mocks)
@dependinator.enhance_mock_dependencies( mocks )
mocks.each { |mock|
- @rake_wrapper[mock].reenable if @first_run == false && @project_config_manager.test_defines_changed
+ reset_rake_task_for_changed_defines( mock )
@rake_wrapper[mock].invoke
}
end
def invoke_test_runner(runner)
@dependinator.enhance_runner_dependencies( runner )
- @rake_wrapper[runner].reenable if @first_run == false && @project_config_manager.test_defines_changed
+ reset_rake_task_for_changed_defines( runner )
@rake_wrapper[runner].invoke
end
def invoke_test_shallow_include_lists(files)
@dependinator.enhance_shallow_include_lists_dependencies( files )
par_map(PROJECT_COMPILE_THREADS, files) do |file|
- @rake_wrapper[file].reenable if @first_run == false && @project_config_manager.test_defines_changed
+ reset_rake_task_for_changed_defines( file )
@rake_wrapper[file].invoke
end
end
@@ -72,7 +78,7 @@ class TaskInvoker
def invoke_test_preprocessed_files(files)
@dependinator.enhance_preprocesed_file_dependencies( files )
par_map(PROJECT_COMPILE_THREADS, files) do |file|
- @rake_wrapper[file].reenable if @first_run == false && @project_config_manager.test_defines_changed
+ reset_rake_task_for_changed_defines( file )
@rake_wrapper[file].invoke
end
end
@@ -80,14 +86,14 @@ class TaskInvoker
def invoke_test_dependencies_files(files)
@dependinator.enhance_dependencies_dependencies( files )
par_map(PROJECT_COMPILE_THREADS, files) do |file|
- @rake_wrapper[file].reenable if @first_run == false && @project_config_manager.test_defines_changed
+ reset_rake_task_for_changed_defines( file )
@rake_wrapper[file].invoke
end
end
def invoke_test_objects(objects)
par_map(PROJECT_COMPILE_THREADS, objects) do |object|
- @rake_wrapper[object].reenable if @first_run == false && @project_config_manager.test_defines_changed
+ reset_rake_task_for_changed_defines( object )
@rake_wrapper[object].invoke
end
end
@@ -98,7 +104,6 @@ class TaskInvoker
def invoke_test_results(result)
@dependinator.enhance_results_dependencies( result )
- @rake_wrapper[result].reenable if @first_run == false && @project_config_manager.test_defines_changed
@rake_wrapper[result].invoke
end
diff --git a/test/vendor/ceedling/lib/ceedling/tasks_base.rake b/test/unit-test/vendor/ceedling/lib/ceedling/tasks_base.rake
similarity index 66%
rename from test/vendor/ceedling/lib/ceedling/tasks_base.rake
rename to test/unit-test/vendor/ceedling/lib/ceedling/tasks_base.rake
index 8c8253099..a35cde75e 100644
--- a/test/vendor/ceedling/lib/ceedling/tasks_base.rake
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/tasks_base.rake
@@ -4,28 +4,10 @@ require 'ceedling/version'
desc "Display build environment version info."
task :version do
- puts " Ceedling:: #{Ceedling::Version::CEEDLING}"
-
- [
- ['CException', File.join( CEEDLING_VENDOR, CEXCEPTION_ROOT_PATH)],
- [' CMock', File.join( CEEDLING_VENDOR, CMOCK_ROOT_PATH)],
- [' Unity', File.join( CEEDLING_VENDOR, UNITY_ROOT_PATH)],
- ].each do |tool|
- name = tool[0]
- base_path = tool[1]
-
- version_string = begin
- @ceedling[:file_wrapper].read( File.join(base_path, 'release', 'version.info') ).strip
- rescue
- "UNKNOWN"
- end
- build_string = begin
- @ceedling[:file_wrapper].read( File.join(base_path, 'release', 'build.info') ).strip
- rescue
- "UNKNOWN"
- end
- puts "#{name}:: #{version_string.empty? ? '#.#.' : (version_string + '.')}#{build_string.empty? ? '?' : build_string}"
- end
+ puts " Ceedling:: #{Ceedling::Version::CEEDLING}"
+ puts " Unity:: #{Ceedling::Version::UNITY}"
+ puts " CMock:: #{Ceedling::Version::CMOCK}"
+ puts " CException:: #{Ceedling::Version::CEXCEPTION}"
end
desc "Set verbose output (silent:[#{Verbosity::SILENT}] - obnoxious:[#{Verbosity::OBNOXIOUS}])."
@@ -65,6 +47,12 @@ task :sanity_checks, :level do |t, args|
@ceedling[:configurator].sanity_checks = check_level
end
+# non advertised catch for calling upgrade in the wrong place
+task :upgrade do
+ puts "WARNING: You're currently IN your project directory. Take a step out and try"
+ puts "again if you'd like to perform an upgrade."
+end
+
# list expanded environment variables
if (not ENVIRONMENT.empty?)
desc "List all configured environment variables."
@@ -73,7 +61,7 @@ task :environment do
ENVIRONMENT.each do |env|
env.each_key do |key|
name = key.to_s.upcase
- env_list.push(" - #{name}: \"#{env[key]}\"")
+ env_list.push(" - #{name}: \"#{env[key]}\"")
end
end
env_list.sort.each do |env_line|
@@ -88,7 +76,7 @@ namespace :options do
option = File.basename(option_path, '.yml')
desc "Merge #{option} project options."
- task option.downcase.to_sym do
+ task option.to_sym do
hash = @ceedling[:project_config_manager].merge_options( @ceedling[:setupinator].config_hash, option_path )
@ceedling[:setupinator].do_setup( hash )
if @ceedling[:configurator].project_release_build
@@ -97,6 +85,23 @@ namespace :options do
end
end
+ # This is to give nice errors when typing options
+ rule /^options:.*/ do |t, args|
+ filename = t.to_s.split(':')[-1] + '.yml'
+ filelist = COLLECTION_PROJECT_OPTIONS.map{|s| File.basename(s) }
+ @ceedling[:file_finder].find_file_from_list(filename, filelist, :error)
+ end
+
+ # This will output the fully-merged tools options to their own project.yml file
+ desc "Export tools options to a new project file"
+ task :export, :filename do |t, args|
+ outfile = args.filename || 'tools.yml'
+ toolcfg = {}
+ @ceedling[:configurator].project_config_hash.each_pair do |k,v|
+ toolcfg[k] = v if (k.to_s[0..5] == 'tools_')
+ end
+ File.open(outfile,'w') {|f| f << toolcfg.to_yaml({:indentation => 2})}
+ end
end
diff --git a/test/vendor/ceedling/lib/ceedling/tasks_filesystem.rake b/test/unit-test/vendor/ceedling/lib/ceedling/tasks_filesystem.rake
similarity index 74%
rename from test/vendor/ceedling/lib/ceedling/tasks_filesystem.rake
rename to test/unit-test/vendor/ceedling/lib/ceedling/tasks_filesystem.rake
index 58fa6511f..7b950ca0b 100644
--- a/test/vendor/ceedling/lib/ceedling/tasks_filesystem.rake
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/tasks_filesystem.rake
@@ -45,26 +45,35 @@ task(:clobber => [:clean]) do
@ceedling[:streaminator].stdout_puts("\nClobbering all generated files...\n(For large projects, this task may take a long time to complete)\n\n")
begin
CLOBBER.each { |fn| REMOVE_FILE_PROC.call(fn) }
+ @ceedling[:rake_wrapper][:directories].invoke
+ @ceedling[:dependinator].touch_force_rebuild_files
rescue
end
end
-
+# create a directory task for each of the paths, so we know how to build them
PROJECT_BUILD_PATHS.each { |path| directory(path) }
-# create directories that hold build output and generated files & touching rebuild dependency sources
-task(:directories => PROJECT_BUILD_PATHS) { @ceedling[:dependinator].touch_force_rebuild_files }
+# create a single directory task which verifies all the others get built
+task :directories => PROJECT_BUILD_PATHS
+# when the force file doesn't exist, it probably means we clobbered or are on a fresh
+# install. In either case, stuff was deleted, so assume we want to rebuild it all
+file @ceedling[:configurator].project_test_force_rebuild_filepath do
+ unless File.exists?(@ceedling[:configurator].project_test_force_rebuild_filepath)
+ @ceedling[:dependinator].touch_force_rebuild_files
+ end
+end
# list paths discovered at load time
namespace :paths do
-
- paths = @ceedling[:setupinator].config_hash[:paths]
- paths.each_key do |section|
- name = section.to_s.downcase
+ standard_paths = ['test','source','include']
+ paths = @ceedling[:setupinator].config_hash[:paths].keys.map{|n| n.to_s.downcase}
+ paths = (paths + standard_paths).uniq
+ paths.each do |name|
path_list = Object.const_get("COLLECTION_PATHS_#{name.upcase}")
- if (path_list.size != 0)
+ if (path_list.size != 0) || (standard_paths.include?(name))
desc "List all collected #{name} paths."
task(name.to_sym) { puts "#{name} paths:"; path_list.sort.each {|path| puts " - #{path}" } }
end
@@ -77,10 +86,11 @@ end
namespace :files do
categories = [
- ['test', COLLECTION_ALL_TESTS],
- ['source', COLLECTION_ALL_SOURCE],
- ['header', COLLECTION_ALL_HEADERS]
- ]
+ ['test', COLLECTION_ALL_TESTS],
+ ['source', COLLECTION_ALL_SOURCE],
+ ['include', COLLECTION_ALL_HEADERS],
+ ['support', COLLECTION_ALL_SUPPORT]
+ ]
using_assembly = (defined?(TEST_BUILD_USE_ASSEMBLY) && TEST_BUILD_USE_ASSEMBLY) ||
(defined?(RELEASE_BUILD_USE_ASSEMBLY) && RELEASE_BUILD_USE_ASSEMBLY)
diff --git a/test/vendor/ceedling/lib/ceedling/tasks_release.rake b/test/unit-test/vendor/ceedling/lib/ceedling/tasks_release.rake
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/tasks_release.rake
rename to test/unit-test/vendor/ceedling/lib/ceedling/tasks_release.rake
diff --git a/test/vendor/ceedling/lib/ceedling/tasks_release_deep_dependencies.rake b/test/unit-test/vendor/ceedling/lib/ceedling/tasks_release_deep_dependencies.rake
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/tasks_release_deep_dependencies.rake
rename to test/unit-test/vendor/ceedling/lib/ceedling/tasks_release_deep_dependencies.rake
diff --git a/test/vendor/ceedling/lib/ceedling/tasks_tests.rake b/test/unit-test/vendor/ceedling/lib/ceedling/tasks_tests.rake
similarity index 86%
rename from test/vendor/ceedling/lib/ceedling/tasks_tests.rake
rename to test/unit-test/vendor/ceedling/lib/ceedling/tasks_tests.rake
index 5d09c1aff..6c51ebcc9 100644
--- a/test/vendor/ceedling/lib/ceedling/tasks_tests.rake
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/tasks_tests.rake
@@ -1,13 +1,15 @@
require 'ceedling/constants'
-task :test => [:directories] do
+task :test_deps => [:directories]
+
+task :test => [:test_deps] do
Rake.application['test:all'].invoke
end
namespace TEST_SYM do
desc "Run all unit tests (also just 'test' works)."
- task :all => [:directories] do
+ task :all => [:test_deps] do
@ceedling[:test_invoker].setup_and_invoke(COLLECTION_ALL_TESTS)
end
@@ -21,17 +23,17 @@ namespace TEST_SYM do
end
desc "Run tests for changed files."
- task :delta => [:directories] do
+ task :delta => [:test_deps] do
@ceedling[:test_invoker].setup_and_invoke(COLLECTION_ALL_TESTS, TEST_SYM, {:force_run => false})
end
desc "Just build tests without running."
- task :build_only => [:directories] do
+ task :build_only => [:test_deps] do
@ceedling[:test_invoker].setup_and_invoke(COLLECTION_ALL_TESTS, TEST_SYM, {:build_only => true})
end
desc "Run tests by matching regular expression pattern."
- task :pattern, [:regex] => [:directories] do |t, args|
+ task :pattern, [:regex] => [:test_deps] do |t, args|
matches = []
COLLECTION_ALL_TESTS.each { |test| matches << test if (test =~ /#{args.regex}/) }
@@ -44,7 +46,7 @@ namespace TEST_SYM do
end
desc "Run tests whose test path contains [dir] or [dir] substring."
- task :path, [:dir] => [:directories] do |t, args|
+ task :path, [:dir] => [:test_deps] do |t, args|
matches = []
COLLECTION_ALL_TESTS.each { |test| matches << test if File.dirname(test).include?(args.dir.gsub(/\\/, '/')) }
diff --git a/test/vendor/ceedling/lib/ceedling/tasks_tests_deep_dependencies.rake b/test/unit-test/vendor/ceedling/lib/ceedling/tasks_tests_deep_dependencies.rake
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/tasks_tests_deep_dependencies.rake
rename to test/unit-test/vendor/ceedling/lib/ceedling/tasks_tests_deep_dependencies.rake
diff --git a/test/vendor/ceedling/lib/ceedling/tasks_vendor.rake b/test/unit-test/vendor/ceedling/lib/ceedling/tasks_vendor.rake
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/tasks_vendor.rake
rename to test/unit-test/vendor/ceedling/lib/ceedling/tasks_vendor.rake
diff --git a/test/vendor/ceedling/lib/ceedling/test_includes_extractor.rb b/test/unit-test/vendor/ceedling/lib/ceedling/test_includes_extractor.rb
similarity index 75%
rename from test/vendor/ceedling/lib/ceedling/test_includes_extractor.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/test_includes_extractor.rb
index 50cc7c041..393b0be8b 100644
--- a/test/vendor/ceedling/lib/ceedling/test_includes_extractor.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/test_includes_extractor.rb
@@ -19,6 +19,11 @@ class TestIncludesExtractor
gather_and_store_includes( test, extract_from_file(test) )
end
+ # open, scan for, and sort & store includes of test file
+ def parse_test_file_source_include(test)
+ return extract_source_include_from_file(test)
+ end
+
# mocks with no file extension
def lookup_raw_mock_list(test)
file_key = form_file_key(test)
@@ -65,6 +70,27 @@ class TestIncludesExtractor
return includes.uniq
end
+ def extract_source_include_from_file(file)
+ source_includes = []
+ source_extension = @configurator.extension_source
+
+ contents = @file_wrapper.read(file)
+
+ # remove line comments
+ contents = contents.gsub(/\/\/.*$/, '')
+ # remove block comments
+ contents = contents.gsub(/\/\*.*?\*\//m, '')
+
+ contents.split("\n").each do |line|
+ # look for include statement
+ scan_results = line.scan(/#include\s+\"\s*(.+#{'\\'+source_extension})\s*\"/)
+
+ source_includes << scan_results[0][0] if (scan_results.size > 0)
+ end
+
+ return source_includes.uniq
+ end
+
def gather_and_store_includes(file, includes)
mock_prefix = @configurator.cmock_mock_prefix
header_extension = @configurator.extension_header
diff --git a/test/vendor/ceedling/lib/ceedling/test_invoker.rb b/test/unit-test/vendor/ceedling/lib/ceedling/test_invoker.rb
similarity index 60%
rename from test/vendor/ceedling/lib/ceedling/test_invoker.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/test_invoker.rb
index 652cb318b..ae686a110 100644
--- a/test/vendor/ceedling/lib/ceedling/test_invoker.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/test_invoker.rb
@@ -23,49 +23,24 @@ class TestInvoker
@mocks = []
end
- def get_test_definition_str(test)
- return "-D" + File.basename(test, File.extname(test)).upcase.sub(/@.*$/, "")
- end
-
- def get_tools_compilers
- tools_compilers = Hash.new
- tools_compilers["for unit test"] = TOOLS_TEST_COMPILER if defined? TOOLS_TEST_COMPILER
- tools_compilers["for gcov"] = TOOLS_GCOV_COMPILER if defined? TOOLS_GCOV_COMPILER
- return tools_compilers
- end
-
- def add_test_definition(test)
- test_definition_str = get_test_definition_str(test)
- get_tools_compilers.each do |tools_compiler_key, tools_compiler_value|
- tools_compiler_value[:arguments].push("-D#{File.basename(test, ".*").strip.upcase.sub(/@.*$/, "")}")
- @streaminator.stdout_puts("Add the definition value in the build option #{tools_compiler_value[:arguments][-1]} #{tools_compiler_key}", Verbosity::OBNOXIOUS)
- end
- end
-
- def delete_test_definition(test)
- test_definition_str = get_test_definition_str(test)
- get_tools_compilers.each do |tools_compiler_key, tools_compiler_value|
- num_options = tools_compiler_value[:arguments].size
- @streaminator.stdout_puts("Delete the definition value in the build option #{tools_compiler_value[:arguments][-1]} #{tools_compiler_key}", Verbosity::OBNOXIOUS)
- tools_compiler_value[:arguments].delete_if{|i| i == test_definition_str}
- if num_options > tools_compiler_value[:arguments].size + 1
- @streaminator.stderr_puts("WARNING: duplicated test definition.")
- end
- end
- end
# Convert libraries configuration form YAML configuration
# into a string that can be given to the compiler.
def convert_libraries_to_arguments()
- if @configurator.project_config_hash.has_key?(:libraries_test)
- lib_args = @configurator.project_config_hash[:libraries_test]
- lib_args.flatten!
- lib_flag = @configurator.project_config_hash[:libraries_flag]
- lib_args.map! {|v| lib_flag.gsub(/\$\{1\}/, v) } if (defined? lib_flag)
- return lib_args
+ args = ((@configurator.project_config_hash[:libraries_test] || []) + ((defined? LIBRARIES_SYSTEM) ? LIBRARIES_SYSTEM : [])).flatten
+ if (defined? LIBRARIES_FLAG)
+ args.map! {|v| LIBRARIES_FLAG.gsub(/\$\{1\}/, v) }
end
+ return args
end
+ def get_library_paths_to_arguments()
+ paths = (defined? PATHS_LIBRARIES) ? (PATHS_LIBRARIES || []).clone : []
+ if (defined? LIBRARIES_PATH_FLAG)
+ paths.map! {|v| LIBRARIES_PATH_FLAG.gsub(/\$\{1\}/, v) }
+ end
+ return paths
+ end
def setup_and_invoke(tests, context=TEST_SYM, options={:force_run => true, :build_only => false})
@@ -83,18 +58,25 @@ class TestInvoker
test_name ="#{File.basename(test)}".chomp('.c')
def_test_key="defines_#{test_name.downcase}"
- # Re-define the project out path and pre-processor defines.
- if @configurator.project_config_hash.has_key?(def_test_key.to_sym)
- @project_config_manager.test_config_changed
+ if @configurator.project_config_hash.has_key?(def_test_key.to_sym) || @configurator.defines_use_test_definition
defs_bkp = Array.new(COLLECTION_DEFINES_TEST_AND_VENDOR)
- printf " ************** Specific test definitions for #{test_name} !!! \n"
- tst_defs_cfg = @configurator.project_config_hash[def_test_key.to_sym]
+ tst_defs_cfg = Array.new(defs_bkp)
+ if @configurator.project_config_hash.has_key?(def_test_key.to_sym)
+ tst_defs_cfg.replace(@configurator.project_config_hash[def_test_key.to_sym])
+ tst_defs_cfg .concat(COLLECTION_DEFINES_VENDOR) if COLLECTION_DEFINES_VENDOR
+ end
+ if @configurator.defines_use_test_definition
+ tst_defs_cfg << File.basename(test, ".*").strip.upcase.sub(/@.*$/, "")
+ end
+ COLLECTION_DEFINES_TEST_AND_VENDOR.replace(tst_defs_cfg)
+ end
+ # redefine the project out path and preprocessor defines
+ if @configurator.project_config_hash.has_key?(def_test_key.to_sym)
+ @streaminator.stdout_puts("Updating test definitions for #{test_name}", Verbosity::NORMAL)
orig_path = @configurator.project_test_build_output_path
@configurator.project_config_hash[:project_test_build_output_path] = File.join(@configurator.project_test_build_output_path, test_name)
@file_wrapper.mkdir(@configurator.project_test_build_output_path)
- COLLECTION_DEFINES_TEST_AND_VENDOR.replace(tst_defs_cfg)
- # printf " * new defines = #{COLLECTION_DEFINES_TEST_AND_VENDOR}\n"
end
# collect up test fixture pieces & parts
@@ -103,16 +85,15 @@ class TestInvoker
sources = @test_invoker_helper.extract_sources( test )
extras = @configurator.collection_test_fixture_extra_link_objects
core = [test] + mock_list + sources
- objects = @file_path_utils.form_test_build_objects_filelist( [runner] + core + extras )
+ objects = @file_path_utils.form_test_build_objects_filelist( [runner] + core + extras ).uniq
results_pass = @file_path_utils.form_pass_results_filepath( test )
results_fail = @file_path_utils.form_fail_results_filepath( test )
- @project_config_manager.process_test_defines_change(sources)
+ # identify all the objects shall not be linked and then remove them from objects list.
+ no_link_objects = @file_path_utils.form_test_build_objects_filelist(@preprocessinator.preprocess_shallow_source_includes( test ))
+ objects = objects.uniq - no_link_objects
- # add the definition value in the build option for the unit test
- if @configurator.defines_use_test_definition
- add_test_definition(test)
- end
+ @project_config_manager.process_test_defines_change(@project_config_manager.filter_internal_sources(sources))
# clean results files so we have a missing file with which to kick off rake's dependency rules
@test_invoker_helper.clean_results( {:pass => results_pass, :fail => results_fail}, options )
@@ -129,7 +110,7 @@ class TestInvoker
@dependinator.enhance_test_build_object_dependencies( objects )
# associate object files with executable
- @dependinator.setup_test_executable_dependencies( test, objects )
+ @dependinator.enhance_test_executable_dependencies( test, objects )
# build test objects
@task_invoker.invoke_test_objects( objects )
@@ -146,18 +127,14 @@ class TestInvoker
rescue => e
@build_invoker_utils.process_exception( e, context )
ensure
- # delete the definition value in the build option for the unit test
- if @configurator.defines_use_test_definition
- delete_test_definition(test)
- end
@plugin_manager.post_test( test )
# restore the project test defines
- if @configurator.project_config_hash.has_key?(def_test_key.to_sym)
- # @configurator.project_config_hash[:defines_test] =
+ if @configurator.project_config_hash.has_key?(def_test_key.to_sym) || @configurator.defines_use_test_definition
COLLECTION_DEFINES_TEST_AND_VENDOR.replace(defs_bkp)
- # printf " ---- Restored defines at #{defs_bkp}"
- @configurator.project_config_hash[:project_test_build_output_path] = orig_path
- printf " ************** Restored defines and build path\n"
+ if @configurator.project_config_hash.has_key?(def_test_key.to_sym)
+ @configurator.project_config_hash[:project_test_build_output_path] = orig_path
+ @streaminator.stdout_puts("Restored defines and build path to standard", Verbosity::NORMAL)
+ end
end
end
diff --git a/test/vendor/ceedling/lib/ceedling/test_invoker_helper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/test_invoker_helper.rb
similarity index 97%
rename from test/vendor/ceedling/lib/ceedling/test_invoker_helper.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/test_invoker_helper.rb
index b0a223f56..403d93e3e 100644
--- a/test/vendor/ceedling/lib/ceedling/test_invoker_helper.rb
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/test_invoker_helper.rb
@@ -11,7 +11,7 @@ class TestInvokerHelper
def process_deep_dependencies(files)
return if (not @configurator.project_use_deep_dependencies)
- dependencies_list = @file_path_utils.form_test_dependencies_filelist( files )
+ dependencies_list = @file_path_utils.form_test_dependencies_filelist( files ).uniq
if @configurator.project_generate_deep_dependencies
@task_invoker.invoke_test_dependencies_files( dependencies_list )
diff --git a/test/vendor/ceedling/lib/ceedling/tool_executor.rb b/test/unit-test/vendor/ceedling/lib/ceedling/tool_executor.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/tool_executor.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/tool_executor.rb
diff --git a/test/vendor/ceedling/lib/ceedling/tool_executor_helper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/tool_executor_helper.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/tool_executor_helper.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/tool_executor_helper.rb
diff --git a/test/vendor/ceedling/lib/ceedling/verbosinator.rb b/test/unit-test/vendor/ceedling/lib/ceedling/verbosinator.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/verbosinator.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/verbosinator.rb
diff --git a/test/unit-test/vendor/ceedling/lib/ceedling/version.rb b/test/unit-test/vendor/ceedling/lib/ceedling/version.rb
new file mode 100644
index 000000000..ebda10b78
--- /dev/null
+++ b/test/unit-test/vendor/ceedling/lib/ceedling/version.rb
@@ -0,0 +1,54 @@
+
+# @private
+module Ceedling
+ module Version
+ { "UNITY" => File.join("unity","src","unity.h"),
+ "CMOCK" => File.join("cmock","src","cmock.h"),
+ "CEXCEPTION" => File.join("c_exception","lib","CException.h")
+ }.each_pair do |name, path|
+ # Check for local or global version of vendor directory in order to look up versions
+ path1 = File.expand_path( File.join("..","..","vendor",path) )
+ path2 = File.expand_path( File.join(File.dirname(__FILE__),"..","..","vendor",path) )
+ filename = if (File.exists?(path1))
+ path1
+ elsif (File.exists?(path2))
+ path2
+ elsif File.exists?(CEEDLING_VENDOR)
+ path3 = File.expand_path( File.join(CEEDLING_VENDOR,path) )
+ if (File.exists?(path3))
+ path3
+ else
+ basepath = File.join( CEEDLING_VENDOR, path.split(/\\\//)[0], 'release')
+ begin
+ [ @ceedling[:file_wrapper].read( File.join(base_path, 'release', 'version.info') ).strip,
+ @ceedling[:file_wrapper].read( File.join(base_path, 'release', 'build.info') ).strip ].join('.')
+ rescue
+ "#{name}"
+ end
+ end
+ else
+ module_eval("#{name} = 'unknown'")
+ continue
+ end
+
+ # Actually look up the versions
+ a = [0,0,0]
+ begin
+ File.readlines(filename).each do |line|
+ ["VERSION_MAJOR", "VERSION_MINOR", "VERSION_BUILD"].each_with_index do |field, i|
+ m = line.match(/#{name}_#{field}\s+(\d+)/)
+ a[i] = m[1] unless (m.nil?)
+ end
+ end
+ rescue
+ abort("Can't collect data for vendor component: \"#{filename}\" . \nPlease check your setup.")
+ end
+
+ # splat it to return the final value
+ eval("#{name} = '#{a.join(".")}'")
+ end
+
+ GEM = "0.31.1"
+ CEEDLING = GEM
+ end
+end
diff --git a/test/vendor/ceedling/lib/ceedling/yaml_wrapper.rb b/test/unit-test/vendor/ceedling/lib/ceedling/yaml_wrapper.rb
similarity index 100%
rename from test/vendor/ceedling/lib/ceedling/yaml_wrapper.rb
rename to test/unit-test/vendor/ceedling/lib/ceedling/yaml_wrapper.rb
diff --git a/test/vendor/ceedling/plugins/beep/README.md b/test/unit-test/vendor/ceedling/plugins/beep/README.md
similarity index 100%
rename from test/vendor/ceedling/plugins/beep/README.md
rename to test/unit-test/vendor/ceedling/plugins/beep/README.md
diff --git a/test/vendor/ceedling/plugins/beep/lib/beep.rb b/test/unit-test/vendor/ceedling/plugins/beep/lib/beep.rb
similarity index 100%
rename from test/vendor/ceedling/plugins/beep/lib/beep.rb
rename to test/unit-test/vendor/ceedling/plugins/beep/lib/beep.rb
diff --git a/test/unit-test/vendor/ceedling/plugins/bullseye/README.md b/test/unit-test/vendor/ceedling/plugins/bullseye/README.md
new file mode 100644
index 000000000..ab0b53b45
--- /dev/null
+++ b/test/unit-test/vendor/ceedling/plugins/bullseye/README.md
@@ -0,0 +1,76 @@
+ceedling-bullseye
+=================
+
+# Plugin Overview
+
+Plugin for integrating Bullseye code coverage tool into Ceedling projects.
+This plugin requires a working license to Bullseye code coverage tools. The tools
+must be within the path or the path should be added to the environment in the
+`project.yml file`.
+
+## Configuration
+
+The bullseye plugin supports configuration options via your `project.yml` provided
+by Ceedling. The following is a typical configuration example:
+
+```
+:bullseye:
+ :auto_license: TRUE
+:plugins:
+ :bullseye_lib_path: []
+:paths:
+ :bullseye_toolchain_include: []
+
+:tools:
+ :bullseye_instrumentation:
+ :executable: covc
+ :arguments:
+ - '--file $': ENVIRONMENT_COVFILE
+ - -q
+ - ${1}
+ :bullseye_compiler:
+ :executable: gcc
+ :arguments:
+ - -g
+ - -I"$": COLLECTION_PATHS_TEST_SUPPORT_SOURCE_INCLUDE_VENDOR
+ - -I"$": COLLECTION_PATHS_BULLSEYE_TOOLCHAIN_INCLUDE
+ - -D$: COLLECTION_DEFINES_TEST_AND_VENDOR
+ - -DBULLSEYE_COMPILER
+ - -c "${1}"
+ - -o "${2}"
+ :bullseye_linker:
+ :executable: gcc
+ :arguments:
+ - ${1}
+ - -o ${2}
+ - -L$: PLUGINS_BULLSEYE_LIB_PATH
+ - -lcov
+ :bullseye_fixture:
+ :executable: ${1}
+ :bullseye_report_covsrc:
+ :executable: covsrc
+ :arguments:
+ - '--file $': ENVIRONMENT_COVFILE
+ - -q
+ - -w140
+ :bullseye_report_covfn:
+ :executable: covfn
+ :stderr_redirect: :auto
+ :arguments:
+ - '--file $': ENVIRONMENT_COVFILE
+ - --width 120
+ - --no-source
+ - '"${1}"'
+ :bullseye_browser:
+ :executable: CoverageBrowser
+ :background_exec: :auto
+ :optional: TRUE
+ :arguments:
+ - '"$"': ENVIRONMENT_COVFILE
+```
+
+## Example Usage
+
+```sh
+ceedling bullseye:all utils:bullseye
+```
diff --git a/test/vendor/ceedling/plugins/bullseye/assets/template.erb b/test/unit-test/vendor/ceedling/plugins/bullseye/assets/template.erb
similarity index 100%
rename from test/vendor/ceedling/plugins/bullseye/assets/template.erb
rename to test/unit-test/vendor/ceedling/plugins/bullseye/assets/template.erb
diff --git a/test/vendor/ceedling/plugins/bullseye/bullseye.rake b/test/unit-test/vendor/ceedling/plugins/bullseye/bullseye.rake
similarity index 93%
rename from test/vendor/ceedling/plugins/bullseye/bullseye.rake
rename to test/unit-test/vendor/ceedling/plugins/bullseye/bullseye.rake
index a7bbebe3a..11073e786 100644
--- a/test/vendor/ceedling/plugins/bullseye/bullseye.rake
+++ b/test/unit-test/vendor/ceedling/plugins/bullseye/bullseye.rake
@@ -32,12 +32,16 @@ rule(/#{BULLSEYE_BUILD_OUTPUT_PATH}\/#{'.+\\'+EXTENSION_OBJECT}$/ => [
end
rule(/#{BULLSEYE_BUILD_OUTPUT_PATH}\/#{'.+\\'+EXTENSION_EXECUTABLE}$/) do |bin_file|
+ lib_args = @ceedling[:test_invoker].convert_libraries_to_arguments()
+ lib_paths = @ceedling[:test_invoker].get_library_paths_to_arguments()
@ceedling[:generator].generate_executable_file(
TOOLS_BULLSEYE_LINKER,
BULLSEYE_SYM,
bin_file.prerequisites,
bin_file.name,
- @ceedling[:file_path_utils].form_test_build_map_filepath(bin_file.name)
+ @ceedling[:file_path_utils].form_test_build_map_filepath(bin_file.name),
+ lib_args,
+ lib_paths
)
end
@@ -69,7 +73,7 @@ namespace BULLSEYE_SYM do
task source_coverage: COLLECTION_ALL_SOURCE.pathmap("#{BULLSEYE_BUILD_OUTPUT_PATH}/%n#{@ceedling[:configurator].extension_object}")
desc 'Run code coverage for all tests'
- task all: [:directories] do
+ task all: [:test_deps] do
@ceedling[:configurator].replace_flattened_config(@ceedling[BULLSEYE_SYM].config)
@ceedling[BULLSEYE_SYM].enableBullseye(true)
@ceedling[:test_invoker].setup_and_invoke(COLLECTION_ALL_TESTS, BULLSEYE_SYM)
@@ -81,18 +85,18 @@ namespace BULLSEYE_SYM do
message = "\nOops! '#{BULLSEYE_ROOT_NAME}:*' isn't a real task. " +
"Use a real test or source file name (no path) in place of the wildcard.\n" +
"Example: rake #{BULLSEYE_ROOT_NAME}:foo.c\n\n"
-
+
@ceedling[:streaminator].stdout_puts( message )
end
-
+
desc 'Run tests by matching regular expression pattern.'
- task :pattern, [:regex] => [:directories] do |_t, args|
+ task :pattern, [:regex] => [:test_deps] do |_t, args|
matches = []
-
+
COLLECTION_ALL_TESTS.each do |test|
matches << test if test =~ /#{args.regex}/
end
-
+
if !matches.empty?
@ceedling[:configurator].replace_flattened_config(@ceedling[BULLSEYE_SYM].config)
@ceedling[BULLSEYE_SYM].enableBullseye(true)
@@ -104,13 +108,13 @@ namespace BULLSEYE_SYM do
end
desc 'Run tests whose test path contains [dir] or [dir] substring.'
- task :path, [:dir] => [:directories] do |_t, args|
+ task :path, [:dir] => [:test_deps] do |_t, args|
matches = []
-
+
COLLECTION_ALL_TESTS.each do |test|
matches << test if File.dirname(test).include?(args.dir.tr('\\', '/'))
end
-
+
if !matches.empty?
@ceedling[:configurator].replace_flattened_config(@ceedling[BULLSEYE_SYM].config)
@ceedling[BULLSEYE_SYM].enableBullseye(true)
@@ -122,13 +126,13 @@ namespace BULLSEYE_SYM do
end
desc 'Run code coverage for changed files'
- task delta: [:directories] do
+ task delta: [:test_deps] do
@ceedling[:configurator].replace_flattened_config(@ceedling[BULLSEYE_SYM].config)
@ceedling[BULLSEYE_SYM].enableBullseye(true)
@ceedling[:test_invoker].setup_and_invoke(COLLECTION_ALL_TESTS, BULLSEYE_SYM, {:force_run => false})
@ceedling[:configurator].restore_config
end
-
+
# use a rule to increase efficiency for large projects
# bullseye test tasks by regex
rule(/^#{BULLSEYE_TASK_ROOT}\S+$/ => [
@@ -138,7 +142,7 @@ namespace BULLSEYE_SYM do
@ceedling[:file_finder].find_test_from_file_path(test)
end
]) do |test|
- @ceedling[:rake_wrapper][:directories].invoke
+ @ceedling[:rake_wrapper][:test_deps].invoke
@ceedling[:configurator].replace_flattened_config(@ceedling[BULLSEYE_SYM].config)
@ceedling[BULLSEYE_SYM].enableBullseye(true)
@ceedling[:test_invoker].setup_and_invoke([test.source], BULLSEYE_SYM)
@@ -159,11 +163,11 @@ end
end
namespace UTILS_SYM do
-
+
desc "Open Bullseye code coverage browser"
task BULLSEYE_SYM do
command = @ceedling[:tool_executor].build_command_line(TOOLS_BULLSEYE_BROWSER, [])
@ceedling[:tool_executor].exec(command[:line], command[:options])
end
-
+
end
diff --git a/test/vendor/ceedling/plugins/bullseye/config/defaults.yml b/test/unit-test/vendor/ceedling/plugins/bullseye/config/defaults.yml
similarity index 100%
rename from test/vendor/ceedling/plugins/bullseye/config/defaults.yml
rename to test/unit-test/vendor/ceedling/plugins/bullseye/config/defaults.yml
diff --git a/test/vendor/ceedling/plugins/bullseye/lib/bullseye.rb b/test/unit-test/vendor/ceedling/plugins/bullseye/lib/bullseye.rb
similarity index 100%
rename from test/vendor/ceedling/plugins/bullseye/lib/bullseye.rb
rename to test/unit-test/vendor/ceedling/plugins/bullseye/lib/bullseye.rb
diff --git a/test/unit-test/vendor/ceedling/plugins/colour_report/README.md b/test/unit-test/vendor/ceedling/plugins/colour_report/README.md
new file mode 100644
index 000000000..4e0fcd45e
--- /dev/null
+++ b/test/unit-test/vendor/ceedling/plugins/colour_report/README.md
@@ -0,0 +1,20 @@
+ceedling-colour-report
+======================
+
+## Overview
+
+The colour_report replaces the normal ceedling "pretty" output with
+a colorized variant, in order to make the results easier to read from
+a standard command line. This is very useful on developer machines, but
+can occasionally cause problems with parsing on CI servers.
+
+## Setup
+
+Enable the plugin in your project.yml by adding `colour_report`
+to the list of enabled plugins.
+
+``` YAML
+:plugins:
+ :enabled:
+ - colour_report
+```
diff --git a/test/vendor/ceedling/plugins/colour_report/lib/colour_report.rb b/test/unit-test/vendor/ceedling/plugins/colour_report/lib/colour_report.rb
similarity index 100%
rename from test/vendor/ceedling/plugins/colour_report/lib/colour_report.rb
rename to test/unit-test/vendor/ceedling/plugins/colour_report/lib/colour_report.rb
diff --git a/test/vendor/ceedling/plugins/command_hooks/README.md b/test/unit-test/vendor/ceedling/plugins/command_hooks/README.md
similarity index 100%
rename from test/vendor/ceedling/plugins/command_hooks/README.md
rename to test/unit-test/vendor/ceedling/plugins/command_hooks/README.md
diff --git a/test/vendor/ceedling/plugins/command_hooks/lib/command_hooks.rb b/test/unit-test/vendor/ceedling/plugins/command_hooks/lib/command_hooks.rb
similarity index 100%
rename from test/vendor/ceedling/plugins/command_hooks/lib/command_hooks.rb
rename to test/unit-test/vendor/ceedling/plugins/command_hooks/lib/command_hooks.rb
diff --git a/test/unit-test/vendor/ceedling/plugins/compile_commands_json/README.md b/test/unit-test/vendor/ceedling/plugins/compile_commands_json/README.md
new file mode 100644
index 000000000..ea80b7397
--- /dev/null
+++ b/test/unit-test/vendor/ceedling/plugins/compile_commands_json/README.md
@@ -0,0 +1,29 @@
+compile_commands_json
+=====================
+
+## Overview
+
+Syntax highlighting and code completion are hard. Historically each editor or IDE has implemented their own and then competed amongst themselves to offer the best experience for developers. Often developers would still to an IDE that felt cumbersome and slow just because it had the best syntax highlighting on the market. If doing it for one language is hard (and it is) imagine doing it for dozens of them. Imagine a full stack developer who has to work with CSS, HTML, JavaScript and some Ruby - they need excellent support in all those languages which just made things even harder.
+
+In June of 2016, Microsoft with Red Hat and Codenvy got together to create a standard called the Language Server Protocol (LSP). The idea was simple, by standardising on one protocol, all the IDEs and editors out there would only have to support LSP, and not have custom plugins for each language. In turn, the backend code that actually does the highlighting can be written once and used by any IDE that supports LSP. Many editors already support it such as Sublime Text, vim and emacs. This means that if you're using a crufty old IDE or worse, you're using a shiny new editor without code completion, then this could be just the upgrade you're looking for!
+
+For C and C++ projects, many people use the `clangd` backend. So that it can do things like "go to definition", `clangd` needs to know how to build the project so that it can figure out all the pieces to the puzzle. There are manual tools such as `bear` which can be run with `gcc` or `clang` to extract this information it has a big limitation in that if run with `ceedling release` you won't get any auto completion for Unity and you'll also get error messages reported by your IDE because of what it perceives as missing headers. If you do the same with `ceedling test` now you get Unity but you might miss things that are only seen in the release build.
+
+This plugin resolves that issue. As it is run by Ceedling, it has access to all the build information it needs to create the perfect `compile_commands.json`. Once enabled, this plugin will generate that file and place it in `./build/artifacts/compile_commands.json`. `clangd` will search your project for this file, but it is easier to symlink it into the root directory (for example `ln -s ./build/artifacts/compile_commands.json`.
+
+For more information on LSP and to find out if your editor supports it, check out https://langserver.org/
+
+## Setup
+
+Enable the plugin in your project.yml by adding `compile_commands_json` to the list
+of enabled plugins.
+
+``` YAML
+:plugins:
+ :enabled:
+ - compile_commands_json
+```
+
+## Configuration
+
+There is no additional configuration necessary to run this plugin.
diff --git a/test/unit-test/vendor/ceedling/plugins/compile_commands_json/lib/compile_commands_json.rb b/test/unit-test/vendor/ceedling/plugins/compile_commands_json/lib/compile_commands_json.rb
new file mode 100644
index 000000000..269cea4de
--- /dev/null
+++ b/test/unit-test/vendor/ceedling/plugins/compile_commands_json/lib/compile_commands_json.rb
@@ -0,0 +1,35 @@
+require 'ceedling/plugin'
+require 'ceedling/constants'
+require 'json'
+
+class CompileCommandsJson < Plugin
+ def setup
+ @fullpath = File.join(PROJECT_BUILD_ARTIFACTS_ROOT, "compile_commands.json")
+ @database = if (File.exists?(@fullpath))
+ JSON.parse( File.read(@fullpath) )
+ else
+ []
+ end
+ end
+
+ def post_compile_execute(arg_hash)
+
+ # Create the new Entry
+ value = {
+ "directory" => Dir.pwd,
+ "command" => arg_hash[:shell_command],
+ "file" => arg_hash[:source]
+ }
+
+ # Determine if we're updating an existing file description or adding a new one
+ index = @database.index {|h| h["file"] == arg_hash[:source]}
+ if index
+ @database[index] = value
+ else
+ @database << value
+ end
+
+ # Update the Actual compile_commands.json file
+ File.open(@fullpath,'w') {|f| f << JSON.pretty_generate(@database)}
+ end
+end
diff --git a/test/unit-test/vendor/ceedling/plugins/dependencies/README.md b/test/unit-test/vendor/ceedling/plugins/dependencies/README.md
new file mode 100644
index 000000000..256467dfa
--- /dev/null
+++ b/test/unit-test/vendor/ceedling/plugins/dependencies/README.md
@@ -0,0 +1,254 @@
+ceedling-dependencies
+=====================
+
+Plugin for supporting release dependencies. It's rare for an embedded project to
+be built completely free of other libraries and modules. Some of these may be
+standard internal libraries. Some of these may be 3rd party libraries. In either
+case, they become part of the project's ecosystem.
+
+This plugin is intended to make that relationship easier. It allows you to specify
+a source for dependencies. If required, it will automatically grab the appropriate
+version of that dependency.
+
+Most 3rd party libraries have a method of building already in place. While we'd
+love to convert the world to a place where everything downloads with a test suite
+in Ceedling, that's not likely to happen anytime soon. Until then, this plugin
+will allow the developer to specify what calls Ceedling should make to oversee
+the build process of those third party utilities. Are they using Make? CMake? A
+custom series of scripts that only a mad scientist could possibly understand? No
+matter. Ceedling has you covered. Just specify what should be called, and Ceedling
+will make it happen whenever it notices that the output artifacts are missing.
+
+Output artifacts? Sure! Things like static and dynamic libraries, or folders
+containing header files that might want to be included by your release project.
+
+So how does all this magic work?
+
+First, you need to add the `:dependencies` plugin to your list. Then, we'll add a new
+section called :dependencies. There, you can list as many dependencies as you desire. Each
+has a series of fields which help Ceedling to understand your needs. Many of them are
+optional. If you don't need that feature, just don't include it! In the end, it'll look
+something like this:
+
+```
+:dependencies:
+ :libraries:
+ - :name: WolfSSL
+ :source_path: third_party/wolfssl/source
+ :build_path: third_party/wolfssl/build
+ :artifact_path: third_party/wolfssl/install
+ :fetch:
+ :method: :zip
+ :source: \\shared_drive\third_party_libs\wolfssl\wolfssl-4.2.0.zip
+ :environment:
+ - CFLAGS+=-DWOLFSSL_DTLS_ALLOW_FUTURE
+ :build:
+ - "autoreconf -i"
+ - "./configure --enable-tls13 --enable-singlethreaded"
+ - make
+ - make install
+ :artifacts:
+ :static_libraries:
+ - lib/wolfssl.a
+ :dynamic_libraries:
+ - lib/wolfssl.so
+ :includes:
+ - include/**
+```
+
+Let's take a deeper look at each of these features.
+
+The Starting Dash & Name
+------------------------
+
+Yes, that opening dash tells the dependencies plugin that the rest of these fields
+belong to our first dependency. If we had a second dependency, we'd have another
+dash, lined up with the first, and followed by all the fields indented again.
+
+By convention, we use the `:name` field as the first field for each tool. Ceedling
+honestly doesn't care which order the fields are given... but as humans, it makes
+it easier for us to see the name of each dependency with starting dash.
+
+The name field is only used to print progress while we're running Ceedling. You may
+call the name of the field whatever you wish.
+
+Working Folders
+---------------
+
+The `:source_path` field allows us to specify where the source code for each of our
+dependencies is stored. If fetching the dependency from elsewhere, it will be fetched
+to this location. All commands to build this dependency will be executed from
+this location (override this by specifying a `:build_path`). Finally, the output
+artifacts will be referenced to this location (override this by specifying a `:artifact_path`)
+
+If unspecified, the `:source_path` will be `dependencies\dep_name` where `dep_name`
+is the name specified in `:name` above (with special characters removed). It's best,
+though, if you specify exactly where you want your dependencies to live.
+
+If the dependency is directly included in your project (you've specified `:none` as the
+`:method` for fetching), then `:source_path` should be where your Ceedling can find the
+source for your dependency in you repo.
+
+All artifacts are relative to the `:artifact_path` (which defaults to be the same as
+`:source_path`)
+
+Fetching Dependencies
+---------------------
+
+The `:dependencies` plugin supports the ability to automatically fetch your dependencies
+for you... using some common methods of fetching source. This section contains only a
+couple of fields:
+
+- `:method` -- This is the method that this dependency is fetched.
+ - `:none` -- This tells Ceedling that the code is already included in the project.
+ - `:zip` -- This tells Ceedling that we want to unpack a zip file to our source path.
+ - `:git` -- This tells Ceedling that we want to clone a git repo to our source path.
+ - `:svn` -- This tells Ceedling that we want to checkout a subversion repo to our source path.
+ - `:custom` -- This tells Ceedling that we want to use a custom command or commands to fetch the code.
+- `:source` -- This is the path or url to fetch code when using the zip or git method.
+- `:tag`/`:branch` -- This is the specific tag or branch that you wish to retrieve (git only. optional).
+- `:hash` -- This is the specific SHA1 hash you want to fetch (git only. optional, requires a deep clone).
+- `:revision` -- This is the specific revision you want to fetch (svn only. optional).
+- `:executable` -- This is a list of commands to execute when using the `:custom` method
+
+
+Environment Variables
+---------------------
+
+Many build systems support customization through environment variables. By specifying
+an array of environment variables, Ceedling will customize the shell environment before
+calling the build process.
+
+Environment variables may be specified in three ways. Let's look at one of each:
+
+```
+ :environment:
+ - ARCHITECTURE=ARM9
+ - CFLAGS+=-DADD_AWESOMENESS
+ - CFLAGS-=-DWASTE
+```
+
+In the first example, you see the most straightforward method. The environment variable
+`ARCHITECTURE` is set to the value `ARM9`. That's it. Simple.
+
+The next two options modify an existing symbol. In the first one, we use `+=`, which tells
+Ceedling to add the define `ADD_AWESOMENESS` to the environment variable `CFLAGS`. The second
+tells Ceedling to remove the define `WASTE` from the same environment variable.
+
+There are a couple of things to note here.
+
+First, when adding to a variable, Ceedling has no way of knowing
+what delimiter you are expecting. In this example you can see we manually added some whitespace.
+If we had been modifying `PATH` instead, we might have had to use a `:` on a unux or `;` on
+Windows.
+
+Second, removing an argument will have no effect on the argument if that argument isn't found
+precisely. It's case sensitive and the entire string must match. If symbol doesn't already exist,
+it WILL after executing this command... however it will be assigned to nothing.
+
+Building Dependencies
+---------------------
+
+The heart of the `:dependencies` plugin is the ability for you, the developer, to specify the
+build process for each of your dependencies. You will need to have any required tools installed
+before using this feature.
+
+The steps are specified as an array of strings. Ceedling will execute those steps in the order
+specified, moving from step to step unless an error is encountered. By the end of the process,
+the artifacts should have been created by your process... otherwise an error will be produced.
+
+Artifacts
+---------
+
+These are the outputs of the build process. There are there types of artifacts. Any dependency
+may have none or some of these. Calling out these files tells Ceedling that they are important.
+Your dependency's build process may produce many other files... but these are the files that
+Ceedling understands it needs to act on.
+
+### `static_libraries`
+
+Specifying one or more static libraries will tell Ceedling where it should find static libraries
+output by your build process. These libraries are automatically added to the list of dependencies
+and will be linked with the rest of your code to produce the final release.
+
+If any of these libraries don't exist, Ceedling will trigger your build process in order for it
+to produce them.
+
+### `dynamic_libraries`
+
+Specifying one or more dynamic libraries will tell Ceedling where it should find dynamic libraries
+output by your build process. These libraries are automatically copied to the same folder as your
+final release binary.
+
+If any of these libraries don't exist, Ceedling will trigger your build process in order for it
+to produce them.
+
+### `includes`
+
+Often when libraries are built, the same process will output a collection of includes so that
+your release code knows how to interact with that library. It's the public API for that library.
+By specifying the directories that will contain these includes (don't specify the files themselves,
+Ceedling only needs the directories), Ceedling is able to automatically add these to its internal
+include list. This allows these files to be used while building your release code, as well we making
+them mockable during unit testing.
+
+### `source`
+
+It's possible that your external dependency will just produce additional C files as its output.
+In this case, Ceedling is able to automatically add these to its internal source list. This allows
+these files to be used while building your release code.
+
+Tasks
+-----
+
+Once configured correctly, the `:dependencies` plugin should integrate seamlessly into your
+workflow and you shouldn't have to think about it. In the real world, that doesn't always happen.
+Here are a number of tasks that are added or modified by this plugin.
+
+### `ceedling dependencies:clean`
+
+This can be issued in order to completely remove the dependency from its source path. On the
+next build, it will be refetched and rebuilt from scratch. This can also apply to a particular
+dependency. For example, by specifying `dependencies:clean:DepName`.
+
+### `ceedling dependencies:fetch`
+
+This can be issued in order to fetch each dependency from its origin. This will have no effect on
+dependencies that don't have fetch instructions specified. This can also apply to a particular
+dependency. For example, by specifying `dependencies:fetch:DepName`.
+
+### `ceedling dependencies:make`
+
+This will force the dependencies to all build. This should happen automatically when a release
+has been triggered... but if you're just getting your dependency configured at this moment, you
+may want to just use this feature instead. A single dependency can also be built by specifying its
+name, like `dependencies:make:MyTunaBoat`.
+
+### `ceedling dependencies:deploy`
+
+This will force any dynamic libraries produced by your dependencies to be copied to your release
+build directory... just in case you clobbered them.
+
+### `paths:include`
+
+Maybe you want to verify that all the include paths are correct. If you query Ceedling with this
+request, it will list all the header file paths that it's found, including those produced by
+dependencies.
+
+### `files:include`
+
+Maybe you want to take that query further and actually get a list of ALL the header files
+Ceedling has found, including those belonging to your dependencies.
+
+Testing
+=======
+
+Hopefully all your dependencies are fully tested... but we can't always depend on that.
+In the event that they are tested with Ceedling, you'll probably want to consider using
+the `:subprojects` plugin instead of this one. The purpose of this plugin is to pull in
+third party code for release... and to provide a mockable interface for Ceedling to use
+during its tests of other modules.
+
+If that's what you're after... you've found the right plugin!
+
+Happy Testing!
diff --git a/test/unit-test/vendor/ceedling/plugins/dependencies/config/defaults.yml b/test/unit-test/vendor/ceedling/plugins/dependencies/config/defaults.yml
new file mode 100644
index 000000000..0415f8ea1
--- /dev/null
+++ b/test/unit-test/vendor/ceedling/plugins/dependencies/config/defaults.yml
@@ -0,0 +1,5 @@
+---
+:dependencies:
+ :libraries: []
+
+...
diff --git a/test/unit-test/vendor/ceedling/plugins/dependencies/dependencies.rake b/test/unit-test/vendor/ceedling/plugins/dependencies/dependencies.rake
new file mode 100644
index 000000000..08a1a48eb
--- /dev/null
+++ b/test/unit-test/vendor/ceedling/plugins/dependencies/dependencies.rake
@@ -0,0 +1,147 @@
+
+DEPENDENCIES_LIBRARIES.each do |deplib|
+
+ # Look up the name of this dependency library
+ deplib_name = @ceedling[DEPENDENCIES_SYM].get_name(deplib)
+
+ # Make sure the required working directories exists
+ # (don't worry about the subdirectories. That's the job of the dep's build tool)
+ paths = @ceedling[DEPENDENCIES_SYM].get_working_paths(deplib)
+ paths.each {|path| directory(path) }
+ task :directories => paths
+
+ all_deps = @ceedling[DEPENDENCIES_SYM].get_static_libraries_for_dependency(deplib) +
+ @ceedling[DEPENDENCIES_SYM].get_dynamic_libraries_for_dependency(deplib) +
+ @ceedling[DEPENDENCIES_SYM].get_include_directories_for_dependency(deplib) +
+ @ceedling[DEPENDENCIES_SYM].get_source_files_for_dependency(deplib)
+
+ # Add a rule for building the actual libraries from dependency list
+ (@ceedling[DEPENDENCIES_SYM].get_static_libraries_for_dependency(deplib) +
+ @ceedling[DEPENDENCIES_SYM].get_dynamic_libraries_for_dependency(deplib)
+ ).each do |libpath|
+ file libpath do |filetask|
+ path = filetask.name
+
+ # We double-check that it doesn't already exist, because this process sometimes
+ # produces multiple files, but they may have already been flagged as invoked
+ unless (File.exists?(path))
+
+ # Set Environment Variables, Fetch, and Build
+ @ceedling[DEPENDENCIES_SYM].set_env_if_required(path)
+ @ceedling[DEPENDENCIES_SYM].fetch_if_required(path)
+ @ceedling[DEPENDENCIES_SYM].build_if_required(path)
+ end
+ end
+ end
+
+ # Add a rule for building the source and includes from dependency list
+ (@ceedling[DEPENDENCIES_SYM].get_include_directories_for_dependency(deplib) +
+ @ceedling[DEPENDENCIES_SYM].get_source_files_for_dependency(deplib)
+ ).each do |libpath|
+ task libpath do |filetask|
+ path = filetask.name
+
+ unless (File.file?(path) || File.directory?(path))
+
+ # Set Environment Variables, Fetch, and Build
+ @ceedling[DEPENDENCIES_SYM].set_env_if_required(path)
+ @ceedling[DEPENDENCIES_SYM].fetch_if_required(path)
+ @ceedling[DEPENDENCIES_SYM].build_if_required(path)
+ end
+ end
+ end
+
+ # Give ourselves a way to trigger individual dependencies
+ namespace DEPENDENCIES_SYM do
+ namespace :deploy do
+ # Add task to directly just build this dependency
+ task(deplib_name => @ceedling[DEPENDENCIES_SYM].get_dynamic_libraries_for_dependency(deplib)) do |t,args|
+ @ceedling[DEPENDENCIES_SYM].deploy_if_required(deplib_name)
+ end
+ end
+
+ namespace :make do
+ # Add task to directly just build this dependency
+ task(deplib_name => all_deps)
+ end
+
+ namespace :clean do
+ # Add task to directly clobber this dependency
+ task(deplib_name) do
+ @ceedling[DEPENDENCIES_SYM].clean_if_required(deplib_name)
+ end
+ end
+
+ namespace :fetch do
+ # Add task to directly clobber this dependency
+ task(deplib_name) do
+ @ceedling[DEPENDENCIES_SYM].fetch_if_required(deplib_name)
+ end
+ end
+ end
+
+ # Add source files to our list of things to build during release
+ source_files = @ceedling[DEPENDENCIES_SYM].get_source_files_for_dependency(deplib)
+ task PROJECT_RELEASE_BUILD_TARGET => source_files
+
+ # Finally, add the static libraries to our RELEASE build dependency list
+ static_libs = @ceedling[DEPENDENCIES_SYM].get_static_libraries_for_dependency(deplib)
+ task RELEASE_SYM => static_libs
+
+ # Add the dynamic libraries to our RELEASE task dependency list so that they will be copied automatically
+ dynamic_libs = @ceedling[DEPENDENCIES_SYM].get_dynamic_libraries_for_dependency(deplib)
+ task RELEASE_SYM => dynamic_libs
+
+ # Add the include dirs / files to our list of dependencies for release
+ headers = @ceedling[DEPENDENCIES_SYM].get_include_directories_for_dependency(deplib)
+ task RELEASE_SYM => headers
+
+ # Paths to Libraries need to be Added to the Lib Path List
+ all_libs = static_libs + dynamic_libs
+ PATHS_LIBRARIES ||= []
+ all_libs.each {|lib| PATHS_LIBRARIES << File.dirname(lib) }
+ PATHS_LIBRARIES.uniq!
+ PATHS_LIBRARIES.reject!{|s| s.empty?}
+
+ # Libraries Need to be Added to the Library List
+ LIBRARIES_SYSTEM ||= []
+ all_libs.each {|lib| LIBRARIES_SYSTEM << File.basename(lib,'.*').sub(/^lib/,'') }
+ LIBRARIES_SYSTEM.uniq!
+ LIBRARIES_SYSTEM.reject!{|s| s.empty?}
+end
+
+# Add any artifact:include or :source folders to our release & test includes paths so linking and mocking work.
+@ceedling[DEPENDENCIES_SYM].add_headers_and_sources()
+
+# Add tasks for building or cleaning ALL depencies
+namespace DEPENDENCIES_SYM do
+ desc "Deploy missing dependencies."
+ task :deploy => DEPENDENCIES_LIBRARIES.map{|deplib| "#{DEPENDENCIES_SYM}:deploy:#{@ceedling[DEPENDENCIES_SYM].get_name(deplib)}"}
+
+ desc "Build any missing dependencies."
+ task :make => DEPENDENCIES_LIBRARIES.map{|deplib| "#{DEPENDENCIES_SYM}:make:#{@ceedling[DEPENDENCIES_SYM].get_name(deplib)}"}
+
+ desc "Clean all dependencies."
+ task :clean => DEPENDENCIES_LIBRARIES.map{|deplib| "#{DEPENDENCIES_SYM}:clean:#{@ceedling[DEPENDENCIES_SYM].get_name(deplib)}"}
+
+ desc "Fetch all dependencies."
+ task :fetch => DEPENDENCIES_LIBRARIES.map{|deplib| "#{DEPENDENCIES_SYM}:fetch:#{@ceedling[DEPENDENCIES_SYM].get_name(deplib)}"}
+end
+
+namespace :files do
+ desc "List all collected dependency libraries."
+ task :dependencies do
+ puts "dependency files:"
+ deps = []
+ DEPENDENCIES_LIBRARIES.each do |deplib|
+ deps << @ceedling[DEPENDENCIES_SYM].get_static_libraries_for_dependency(deplib)
+ deps << @ceedling[DEPENDENCIES_SYM].get_dynamic_libraries_for_dependency(deplib)
+ end
+ deps.flatten!
+ deps.sort.each {|dep| puts " - #{dep}"}
+ puts "file count: #{deps.size}"
+ end
+end
+
+# Make sure that we build dependencies before attempting to tackle any of the unit tests
+Rake::Task[:test_deps].enhance ['dependencies:make']
diff --git a/test/unit-test/vendor/ceedling/plugins/dependencies/lib/dependencies.rb b/test/unit-test/vendor/ceedling/plugins/dependencies/lib/dependencies.rb
new file mode 100644
index 000000000..fc8ae9968
--- /dev/null
+++ b/test/unit-test/vendor/ceedling/plugins/dependencies/lib/dependencies.rb
@@ -0,0 +1,237 @@
+require 'ceedling/plugin'
+require 'ceedling/constants'
+
+DEPENDENCIES_ROOT_NAME = 'dependencies'
+DEPENDENCIES_TASK_ROOT = DEPENDENCIES_ROOT_NAME + ':'
+DEPENDENCIES_SYM = DEPENDENCIES_ROOT_NAME.to_sym
+
+class Dependencies < Plugin
+
+ def setup
+ @plugin_root = File.expand_path(File.join(File.dirname(__FILE__), '..'))
+
+ # Set up a fast way to look up dependencies by name or static lib path
+ @dependencies = {}
+ @dynamic_libraries = []
+ DEPENDENCIES_LIBRARIES.each do |deplib|
+
+ @dependencies[ deplib[:name] ] = deplib.clone
+ all_deps = get_static_libraries_for_dependency(deplib) +
+ get_dynamic_libraries_for_dependency(deplib) +
+ get_include_directories_for_dependency(deplib) +
+ get_source_files_for_dependency(deplib)
+ all_deps.each do |key|
+ @dependencies[key] = @dependencies[ deplib[:name] ]
+ end
+
+ @dynamic_libraries += get_dynamic_libraries_for_dependency(deplib)
+ end
+ end
+
+ def config
+ updates = {
+ :collection_paths_include => COLLECTION_PATHS_INCLUDE,
+ :collection_all_headers => COLLECTION_ALL_HEADERS,
+ }
+
+ @ceedling[DEPENDENCIES_SYM].get_include_directories_for_dependency(deplib).each do |incpath|
+ updates[:collection_paths_include] << incpath
+ Dir[ File.join(incpath, "*#{EXTENSION_HEADER}") ].each do |f|
+ updates[:collection_all_headers] << f
+ end
+ end
+
+ return updates
+ end
+
+ def get_name(deplib)
+ raise "Each dependency must have a name!" if deplib[:name].nil?
+ return deplib[:name].gsub(/\W*/,'')
+ end
+
+ def get_source_path(deplib)
+ return deplib[:source_path] || File.join('dependencies', get_name(deplib))
+ end
+
+ def get_build_path(deplib)
+ return deplib[:build_path] || deplib[:source_path] || File.join('dependencies', get_name(deplib))
+ end
+
+ def get_artifact_path(deplib)
+ return deplib[:artifact_path] || deplib[:source_path] || File.join('dependencies', get_name(deplib))
+ end
+
+ def get_working_paths(deplib)
+ paths = [deplib[:source_path], deplib[:build_path], deplib[:artifact_paths]].compact.uniq
+ paths = [ File.join('dependencies', get_name(deplib)) ] if (paths.empty?)
+ return paths
+ end
+
+ def get_static_libraries_for_dependency(deplib)
+ (deplib[:artifacts][:static_libraries] || []).map {|path| File.join(get_artifact_path(deplib), path)}
+ end
+
+ def get_dynamic_libraries_for_dependency(deplib)
+ (deplib[:artifacts][:dynamic_libraries] || []).map {|path| File.join(get_artifact_path(deplib), path)}
+ end
+
+ def get_source_files_for_dependency(deplib)
+ (deplib[:artifacts][:source] || []).map {|path| File.join(get_artifact_path(deplib), path)}
+ end
+
+ def get_include_directories_for_dependency(deplib)
+ paths = (deplib[:artifacts][:includes] || []).map {|path| File.join(get_artifact_path(deplib), path)}
+ @ceedling[:file_system_utils].collect_paths(paths)
+ end
+
+ def set_env_if_required(lib_path)
+ blob = @dependencies[lib_path]
+ raise "Could not find dependency '#{lib_path}'" if blob.nil?
+ return if (blob[:environment].nil?)
+ return if (blob[:environment].empty?)
+
+ blob[:environment].each do |e|
+ m = e.match(/^(\w+)\s*(\+?\-?=)\s*(.*)$/)
+ unless m.nil?
+ case m[2]
+ when "+="
+ ENV[m[1]] = (ENV[m[1]] || "") + m[3]
+ when "-="
+ ENV[m[1]] = (ENV[m[1]] || "").gsub(m[3],'')
+ else
+ ENV[m[1]] = m[3]
+ end
+ end
+ end
+ end
+
+ def fetch_if_required(lib_path)
+ blob = @dependencies[lib_path]
+ raise "Could not find dependency '#{lib_path}'" if blob.nil?
+ return if (blob[:fetch].nil?)
+ return if (blob[:fetch][:method].nil?)
+ return if (directory(blob[:source_path]) && !Dir.empty?(blob[:source_path]))
+
+ steps = case blob[:fetch][:method]
+ when :none
+ return
+ when :zip
+ [ "gzip -d #{blob[:fetch][:source]}" ]
+ when :git
+ branch = blob[:fetch][:tag] || blob[:fetch][:branch] || ''
+ branch = ("-b " + branch) unless branch.empty?
+ unless blob[:fetch][:hash].nil?
+ # Do a deep clone to ensure the commit we want is available
+ retval = [ "git clone #{branch} #{blob[:fetch][:source]} ." ]
+ # Checkout the specified commit
+ retval << "git checkout #{blob[:fetch][:hash]}"
+ else
+ # Do a thin clone
+ retval = [ "git clone #{branch} --depth 1 #{blob[:fetch][:source]} ." ]
+ end
+ when :svn
+ revision = blob[:fetch][:revision] || ''
+ revision = ("--revision " + branch) unless branch.empty?
+ retval = [ "svn checkout #{revision} #{blob[:fetch][:source]} ." ]
+ retval
+ when :custom
+ blob[:fetch][:executable]
+ else
+ raise "Unknown fetch method '#{blob[:fetch][:method].to_s}' for dependency '#{blob[:name]}'"
+ end
+
+ # Perform the actual fetching
+ @ceedling[:streaminator].stdout_puts("Fetching dependency #{blob[:name]}...", Verbosity::NORMAL)
+ Dir.chdir(get_source_path(blob)) do
+ steps.each do |step|
+ @ceedling[:tool_executor].exec( step )
+ end
+ end
+ end
+
+ def build_if_required(lib_path)
+ blob = @dependencies[lib_path]
+ raise "Could not find dependency '#{lib_path}'" if blob.nil?
+
+ # We don't clean anything unless we know how to fetch a new copy
+ if (blob[:build].nil? || blob[:build].empty?)
+ @ceedling[:streaminator].stdout_puts("Nothing to build for dependency #{blob[:name]}", Verbosity::NORMAL)
+ return
+ end
+
+ # Perform the build
+ @ceedling[:streaminator].stdout_puts("Building dependency #{blob[:name]}...", Verbosity::NORMAL)
+ Dir.chdir(get_build_path(blob)) do
+ blob[:build].each do |step|
+ @ceedling[:tool_executor].exec( step )
+ end
+ end
+ end
+
+ def clean_if_required(lib_path)
+ blob = @dependencies[lib_path]
+ raise "Could not find dependency '#{lib_path}'" if blob.nil?
+
+ # We don't clean anything unless we know how to fetch a new copy
+ if (blob[:fetch].nil? || blob[:fetch][:method].nil? || (blob[:fetch][:method] == :none))
+ @ceedling[:streaminator].stdout_puts("Nothing to clean for dependency #{blob[:name]}", Verbosity::NORMAL)
+ return
+ end
+
+ # Perform the actual Cleaning
+ @ceedling[:streaminator].stdout_puts("Cleaning dependency #{blob[:name]}...", Verbosity::NORMAL)
+ get_working_paths(blob).each do |path|
+ FileUtils.rm_rf(path) if File.directory?(path)
+ end
+ end
+
+ def deploy_if_required(lib_path)
+ blob = @dependencies[lib_path]
+ raise "Could not find dependency '#{lib_path}'" if blob.nil?
+
+ # We don't need to deploy anything if there isn't anything to deploy
+ if (blob[:artifacts].nil? || blob[:artifacts][:dynamic_libraries].nil? || blob[:artifacts][:dynamic_libraries].empty?)
+ @ceedling[:streaminator].stdout_puts("Nothing to deploy for dependency #{blob[:name]}", Verbosity::NORMAL)
+ return
+ end
+
+ # Perform the actual Deploying
+ @ceedling[:streaminator].stdout_puts("Deploying dependency #{blob[:name]}...", Verbosity::NORMAL)
+ FileUtils.cp( lib_path, File.dirname(PROJECT_RELEASE_BUILD_TARGET) )
+ end
+
+ def add_headers_and_sources()
+ # Search for header file paths and files to add to our collections
+ DEPENDENCIES_LIBRARIES.each do |deplib|
+ get_include_directories_for_dependency(deplib).each do |header|
+ cfg = @ceedling[:configurator].project_config_hash
+ cfg[:collection_paths_include] << header
+ cfg[:collection_paths_source_and_include] << header
+ cfg[:collection_paths_test_support_source_include] << header
+ cfg[:collection_paths_test_support_source_include_vendor] << header
+ cfg[:collection_paths_release_toolchain_include] << header
+ Dir[ File.join(header, "*#{EXTENSION_HEADER}") ].each do |f|
+ cfg[:collection_all_headers] << f
+ end
+ end
+
+ get_source_files_for_dependency(deplib).each do |source|
+ cfg = @ceedling[:configurator].project_config_hash
+ cfg[:collection_paths_source_and_include] << source
+ cfg[:collection_paths_test_support_source_include] << source
+ cfg[:collection_paths_test_support_source_include_vendor] << source
+ cfg[:collection_paths_release_toolchain_include] << source
+ Dir[ File.join(source, "*#{EXTENSION_SOURCE}") ].each do |f|
+ cfg[:collection_all_source] << f
+ end
+ end
+ end
+
+ # Make all these updated files findable by Ceedling
+ @ceedling[:file_finder].prepare_search_sources()
+ end
+end
+
+# end blocks always executed following rake run
+END {
+}
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/README.md b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/README.md
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/README.md
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/README.md
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/Rakefile b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/Rakefile
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/Rakefile
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/Rakefile
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/project.yml b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/project.yml
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/project.yml
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/project.yml
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/rakefile.rb b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/rakefile.rb
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/rakefile.rb
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/rakefile.rb
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/bar.c b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/bar.c
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/bar.c
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/bar.c
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/bar.h b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/bar.h
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/bar.h
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/bar.h
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/custom_types.h b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/custom_types.h
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/custom_types.h
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/custom_types.h
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/display.c b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/display.c
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/display.c
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/display.c
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/display.h b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/display.h
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/display.h
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/display.h
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/event_processor.c b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/event_processor.c
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/event_processor.c
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/event_processor.c
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/event_processor.h b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/event_processor.h
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/event_processor.h
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/event_processor.h
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/foo.c b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/foo.c
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/foo.c
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/foo.c
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/foo.h b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/foo.h
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/foo.h
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/foo.h
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/subfolder/zzz.c b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/subfolder/zzz.c
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/subfolder/zzz.c
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/subfolder/zzz.c
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/subfolder/zzz.h b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/subfolder/zzz.h
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/subfolder/zzz.h
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/src/subfolder/zzz.h
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/test/test_event_processor.c b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/test/test_event_processor.c
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/test/test_event_processor.c
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/test/test_event_processor.c
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/test/test_foo.c b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/test/test_foo.c
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/test/test_foo.c
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/examples/fff_example/test/test_foo.c
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/lib/fake_function_framework.rb b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/lib/fake_function_framework.rb
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/lib/fake_function_framework.rb
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/lib/fake_function_framework.rb
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/lib/fff_mock_generator.rb b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/lib/fff_mock_generator.rb
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/lib/fff_mock_generator.rb
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/lib/fff_mock_generator.rb
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/spec/fff_mock_header_generator_spec.rb b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/spec/fff_mock_header_generator_spec.rb
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/spec/fff_mock_header_generator_spec.rb
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/spec/fff_mock_header_generator_spec.rb
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/spec/fff_mock_source_generator_spec.rb b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/spec/fff_mock_source_generator_spec.rb
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/spec/fff_mock_source_generator_spec.rb
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/spec/fff_mock_source_generator_spec.rb
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/spec/header_generator.rb b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/spec/header_generator.rb
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/spec/header_generator.rb
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/spec/header_generator.rb
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/spec/spec_helper.rb b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/spec/spec_helper.rb
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/spec/spec_helper.rb
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/spec/spec_helper.rb
diff --git a/test/vendor/ceedling/plugins/fake_function_framework/src/fff_unity_helper.h b/test/unit-test/vendor/ceedling/plugins/fake_function_framework/src/fff_unity_helper.h
similarity index 100%
rename from test/vendor/ceedling/plugins/fake_function_framework/src/fff_unity_helper.h
rename to test/unit-test/vendor/ceedling/plugins/fake_function_framework/src/fff_unity_helper.h
diff --git a/test/unit-test/vendor/ceedling/plugins/gcov/README.md b/test/unit-test/vendor/ceedling/plugins/gcov/README.md
new file mode 100644
index 000000000..b144e3b74
--- /dev/null
+++ b/test/unit-test/vendor/ceedling/plugins/gcov/README.md
@@ -0,0 +1,433 @@
+ceedling-gcov
+=============
+
+# Plugin Overview
+
+Plugin for integrating GNU GCov code coverage tool into Ceedling projects.
+Currently only designed for the gcov command (like LCOV for example). In the
+future we could configure this to work with other code coverage tools.
+
+This plugin currently uses [gcovr](https://www.gcovr.com/) and / or
+[ReportGenerator](https://danielpalme.github.io/ReportGenerator/)
+as utilities to generate HTML, XML, JSON, or Text reports. The normal gcov
+plugin _must_ be run first for these reports to generate.
+
+## Installation
+
+gcovr can be installed via pip like so:
+
+```sh
+pip install gcovr
+```
+
+ReportGenerator can be installed via .NET Core like so:
+
+```sh
+dotnet tool install -g dotnet-reportgenerator-globaltool
+```
+
+It is not required to install both `gcovr` and `ReportGenerator`. Either utility
+may be installed to create reports.
+
+## Configuration
+
+The gcov plugin supports configuration options via your `project.yml` provided
+by Ceedling.
+
+### Utilities
+
+Gcovr and / or ReportGenerator may be enabled to create coverage reports.
+
+```yaml
+:gcov:
+ :utilities:
+ - gcovr # Use gcovr to create the specified reports (default).
+ - ReportGenerator # Use ReportGenerator to create the specified reports.
+```
+
+### Reports
+
+Various reports are available and may be enabled with the following
+configuration item. See the specific report sections in this README
+for additional options and information. All generated reports will be found in `build/artifacts/gcov`.
+
+```yaml
+:gcov:
+ # Specify one or more reports to generate.
+ # Defaults to HtmlBasic.
+ :reports:
+ # Make an HTML summary report.
+ # Supported utilities: gcovr, ReportGenerator
+ - HtmlBasic
+
+ # Make an HTML report with line by line coverage of each source file.
+ # Supported utilities: gcovr, ReportGenerator
+ - HtmlDetailed
+
+ # Make a Text report, which may be output to the console with gcovr or a file in both gcovr and ReportGenerator.
+ # Supported utilities: gcovr, ReportGenerator
+ - Text
+
+ # Make a Cobertura XML report.
+ # Supported utilities: gcovr, ReportGenerator
+ - Cobertura
+
+ # Make a SonarQube XML report.
+ # Supported utilities: gcovr, ReportGenerator
+ - SonarQube
+
+ # Make a JSON report.
+ # Supported utilities: gcovr
+ - JSON
+
+ # Make a detailed HTML report with CSS and JavaScript included in every HTML page. Useful for build servers.
+ # Supported utilities: ReportGenerator
+ - HtmlInline
+
+ # Make a detailed HTML report with a light theme and CSS and JavaScript included in every HTML page for Azure DevOps.
+ # Supported utilities: ReportGenerator
+ - HtmlInlineAzure
+
+ # Make a detailed HTML report with a dark theme and CSS and JavaScript included in every HTML page for Azure DevOps.
+ # Supported utilities: ReportGenerator
+ - HtmlInlineAzureDark
+
+ # Make a single HTML file containing a chart with historic coverage information.
+ # Supported utilities: ReportGenerator
+ - HtmlChart
+
+ # Make a detailed HTML report in a single file.
+ # Supported utilities: ReportGenerator
+ - MHtml
+
+ # Make SVG and PNG files that show line and / or branch coverage information.
+ # Supported utilities: ReportGenerator
+ - Badges
+
+ # Make a single CSV file containing coverage information per file.
+ # Supported utilities: ReportGenerator
+ - CsvSummary
+
+ # Make a single TEX file containing a summary for all files and detailed reports for each files.
+ # Supported utilities: ReportGenerator
+ - Latex
+
+ # Make a single TEX file containing a summary for all files.
+ # Supported utilities: ReportGenerator
+ - LatexSummary
+
+ # Make a single PNG file containing a chart with historic coverage information.
+ # Supported utilities: ReportGenerator
+ - PngChart
+
+ # Command line output interpreted by TeamCity.
+ # Supported utilities: ReportGenerator
+ - TeamCitySummary
+
+ # Make a text file in lcov format.
+ # Supported utilities: ReportGenerator
+ - lcov
+
+ # Make a XML file containing a summary for all classes and detailed reports for each class.
+ # Supported utilities: ReportGenerator
+ - Xml
+
+ # Make a single XML file containing a summary for all files.
+ # Supported utilities: ReportGenerator
+ - XmlSummary
+```
+
+### Gcovr HTML Reports
+
+Generation of Gcovr HTML reports may be modified with the following configuration items.
+
+```yaml
+:gcov:
+ # Set to 'true' to enable HTML reports or set to 'false' to disable.
+ # Defaults to enabled. (gcovr --html)
+ # Deprecated - See the :reports: configuration option.
+ :html_report: [true|false]
+
+ # Gcovr supports generating two types of HTML reports. Use 'basic' to create
+ # an HTML report with only the overall file information. Use 'detailed' to create
+ # an HTML report with line by line coverage of each source file.
+ # Defaults to 'basic'. Set to 'detailed' for (gcovr --html-details).
+ # Deprecated - See the :reports: configuration option.
+ :html_report_type: [basic|detailed]
+
+
+ :gcovr:
+ # HTML report filename.
+ :html_artifact_filename: