-
Notifications
You must be signed in to change notification settings - Fork 416
Initial support for GD32C103 and GD32E103 #413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
79efe16
00048c8
43304a2
85865f4
d8777b1
f3416c8
17235b0
d259d3f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| /* | ||
| * Copyright (c) 2024, sakumisu | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| #include "usbd_core.h" | ||
| #include "stdint.h" | ||
| #include "usb_dwc2_reg.h" | ||
| #include "usb_dwc2_param.h" | ||
| #include "board_config.h" | ||
|
|
||
| const struct dwc2_user_params param_pa11_pa12 = { | ||
| .phy_type = DWC2_PHY_TYPE_PARAM_FS, | ||
| .device_dma_enable = false, | ||
| .device_dma_desc_enable = false, | ||
| .device_rx_fifo_size = (320 - 16 * 4), | ||
| .device_tx_fifo_size = { | ||
| [0] = 16, // 64 byte | ||
| [1] = 16, // 64 byte | ||
| [2] = 16, // 64 byte | ||
| [3] = 16, // 64 byte | ||
| [4] = 0, | ||
| [5] = 0, | ||
| [6] = 0, | ||
| [7] = 0, | ||
| [8] = 0, | ||
| [9] = 0, | ||
| [10] = 0, | ||
| [11] = 0, | ||
| [12] = 0, | ||
| [13] = 0, | ||
| [14] = 0, | ||
| [15] = 0 }, | ||
| .device_gccfg = ((1 << 16) | (1 << 19) |(1 << 21)), | ||
| .total_fifo_size = 320 // 1280 byte | ||
| }; | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| #if CONFIG_USBDEV_EP_NUM != 4 && CONFIG_USBDEV_EP_NUM != 6 | ||
| #error "gd32 only has 4 endpoints for pa11/pa12 and 6 endpoints for pb14/pb15" | ||
| #endif | ||
|
|
||
| void usb_dc_low_level_init(uint8_t busid) { | ||
| NVIC_EnableIRQ(USBFS_IRQn); | ||
| } | ||
|
|
||
| void usb_dc_low_level_deinit(uint8_t busid) { | ||
| NVIC_DisableIRQ(USBFS_IRQn); | ||
| } | ||
|
Comment on lines
+42
to
+48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Confirm whether USBFS clock / GPIO setup happens elsewhere in the tree.
rg -nP -C2 'RCU_USBFS|rcu_usb_clock_config|rcu_periph_clock_enable\s*\(\s*RCU_USBFS'
rg -nP -C2 '\b(GPIO|gpio)_(init|mode_set|af_set).*PA1[12]\b'
fd -t f -e c -e h | xargs rg -lP 'usb_dc_low_level_init' 2>/dev/nullRepository: cherry-embedded/CherryUSB Length of output: 996 🏁 Script executed: cat -n port/dwc2/usb_glue_gd.cRepository: cherry-embedded/CherryUSB Length of output: 1489 🏁 Script executed: cat -n port/dwc2/usb_glue_gd_arm.cRepository: cherry-embedded/CherryUSB Length of output: 2892 🏁 Script executed: rg -n 'RCU_USBFS|rcu_usb_clock_config' port/dwc2/ -A 3 -B 3Repository: cherry-embedded/CherryUSB Length of output: 51 🏁 Script executed: rg -n 'PA1[12]|PB1[45]' port/dwc2/ -A 2 -B 2Repository: cherry-embedded/CherryUSB Length of output: 530 🏁 Script executed: wc -l port/dwc2/usb_glue_gd.cRepository: cherry-embedded/CherryUSB Length of output: 96 🏁 Script executed: cat -n port/dwc2/usb_glue_gd.c | tail -100Repository: cherry-embedded/CherryUSB Length of output: 1489 🏁 Script executed: find . -name '*gd*' -path '*/port/*' -type f | grep -E '\.(c|h)$'Repository: cherry-embedded/CherryUSB Length of output: 125 🏁 Script executed: rg -n 'RCU_USBFS|rcu_periph_clock_enable.*USBFS|rcu_usb_clock_config' --type c --type hRepository: cherry-embedded/CherryUSB Length of output: 51 🏁 Script executed: rg -n 'board_config|board.h' port/dwc2/usb_glue_gd_arm.cRepository: cherry-embedded/CherryUSB Length of output: 98 🏁 Script executed: find . -name 'board_config.h' -o -name 'board.h' | head -5Repository: cherry-embedded/CherryUSB Length of output: 51 🏁 Script executed: rg -n 'rcu_periph_clock_enable|RCU_' --type c --type h | head -20Repository: cherry-embedded/CherryUSB Length of output: 51 🏁 Script executed: rg -n 'gpio.*init|GPIO.*AFIO' --type c --type h -i | grep -i 'gd32\|pa1[12]\|pb1[45]' | head -10Repository: cherry-embedded/CherryUSB Length of output: 51 🏁 Script executed: rg -n 'usb_dc_low_level_init|usb_dc_init' -A 10 port/ --type c | head -80Repository: cherry-embedded/CherryUSB Length of output: 4321 🏁 Script executed: rg -n 'rcu_periph_clock_enable|clock.*enable' port/ --type c -B2 -A2 | grep -E '(usb|USB)' | head -30Repository: cherry-embedded/CherryUSB Length of output: 1805 🏁 Script executed: ls -la port/dwc2/ | grep glueRepository: cherry-embedded/CherryUSB Length of output: 684 🏁 Script executed: cat -n port/dwc2/usb_glue_st.c | head -100Repository: cherry-embedded/CherryUSB Length of output: 3461 🏁 Script executed: cat -n port/dwc2/usb_glue_esp.c | head -100Repository: cherry-embedded/CherryUSB Length of output: 4179 🏁 Script executed: rg -n 'void usb_dc_low_level_init' port/dwc2/ -A 15 --type cRepository: cherry-embedded/CherryUSB Length of output: 7618
The function only calls
Other DWC2 port implementations (ST, Nation, Infineon, HC, Kendryte) perform such setup in their 🤖 Prompt for AI Agents |
||
|
|
||
| #ifndef CONFIG_USB_DWC2_CUSTOM_PARAM | ||
| void dwc2_get_user_params(uint32_t reg_base, struct dwc2_user_params *params) | ||
| { | ||
| memcpy(params, ¶m_pa11_pa12, sizeof(struct dwc2_user_params)); | ||
|
|
||
| #ifdef CONFIG_USB_DWC2_CUSTOM_FIFO | ||
| struct usb_dwc2_user_fifo_config s_dwc2_fifo_config; | ||
|
|
||
| dwc2_get_user_fifo_config(reg_base, &s_dwc2_fifo_config); | ||
|
|
||
| params->device_rx_fifo_size = s_dwc2_fifo_config.device_rx_fifo_size; | ||
| for (uint8_t i = 0; i < MAX_EPS_CHANNELS; i++) { | ||
| params->device_tx_fifo_size[i] = s_dwc2_fifo_config.device_tx_fifo_size[i]; | ||
| } | ||
| #endif | ||
| } | ||
| void dwc2_override_hw_params(uint32_t reg_base, struct dwc2_hw_params *hw) { | ||
| /* HWCFG2 reads 0, this is unknown why */ | ||
| hw->num_dev_ep = 4; | ||
| } | ||
|
Comment on lines
+67
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
rg -nP --type=c -C2 '\bnum_dev_ep\b' port/dwc2/usb_dc_dwc2.c
rg -nP --type=c -C1 '\bUSBD_BASE\b|\bUSBFS_BASE\b' port/dwc2/Repository: cherry-embedded/CherryUSB Length of output: 2750 🏁 Script executed: #!/bin/bash
# Search for CONFIG_USBDEV_EP_NUM definition and usage
rg -n 'CONFIG_USBDEV_EP_NUM' --type=c --type=h
# Search for all definitions of USBD_BASE and USBFS_BASE
rg -n 'define\s+(USBD_BASE|USBFS_BASE)' --type=c --type=h
# Search for 0x50000000 to see where the magic literal appears
rg -n '0x50000000' --type=c --type=h
# Check GD32-related header includes
rg -n 'include.*gd32' --type=c --type=h -i | head -20Repository: cherry-embedded/CherryUSB Length of output: 5664 🏁 Script executed: #!/bin/bash
# Search for GD32-related includes or USB definitions
fd -type f -name "*.h" | xargs grep -l "GD32\|gd32" 2>/dev/null | head -20
# Search for USB base defines in the codebase
rg -n 'define.*BASE.*0x50000000' --type=h --type=c
# Check what's in the GD files - look for the glue file itself and related headers
cat -n port/dwc2/usb_glue_gd_arm.c | head -75
# Look for includes in usb_glue_gd_arm.c
rg -n '#include' port/dwc2/usb_glue_gd_arm.cRepository: cherry-embedded/CherryUSB Length of output: 3084 🏁 Script executed: #!/bin/bash
# Find board_config.h references
rg -l 'board_config.h' --type=c --type=h | head -10
# Search for USBFS or USB related defines in GD32 context
rg -n 'USBFS\|USBD' --type=h | grep -i gd32 | head -20
# Look for any GD32 CMakeLists or config files
fd -name 'CMakeLists.txt' -o -name '*.cmake' | xargs grep -l 'gd32\|GD32' 2>/dev/null
# Check examples directory structure for GD32
find . -type d -name '*gd*' 2>/dev/null | head -10
# Search for where USBFS_IRQn is defined (used in usb_glue_gd_arm.c)
rg -n 'USBFS_IRQn' --type=c --type=hRepository: cherry-embedded/CherryUSB Length of output: 493 🏁 Script executed: #!/bin/bash
# Search for any GD32-related header files or documentation
find . -type f \( -name "*.h" -o -name "*.md" \) -exec grep -l "GD32\|0x50000000" {} \; | head -20
# Look at includes in the glue file more carefully
grep -E '#include|USBFS' port/dwc2/usb_glue_gd_arm.c
# Search for any constants that might map to 0x50000000
rg '0x50000000' -B2 -A2 --type=c --type=h
# Check the dwc2_override_hw_params function calls
rg -A5 -B5 'dwc2_override_hw_params' --type=c | head -40Repository: cherry-embedded/CherryUSB Length of output: 3329 Fix the off-by-one error in Two critical issues require correction:
🛠️ Proposed fix void dwc2_override_hw_params(uint32_t reg_base, struct dwc2_hw_params *hw) {
- /* HWCFG2 reads 0, this is unknown why */
- if(reg_base == 0x50000000UL) {
- hw->num_dev_ep = 4;
- }
+ /* HWCFG2 reads 0 on GD32C103/E103; supply known values manually. */
+ if (reg_base == USBFS_BASE) {
+ hw->num_dev_ep = CONFIG_USBDEV_EP_NUM - 1; /* highest EP index */
+ }
/* TODO: For other GD32, potentially will need to update like this as well*/
}Note: Verify that 🤖 Prompt for AI Agents |
||
| #endif | ||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
|
|
||
| void usbd_dwc2_delay_ms(uint8_t ms) | ||
| { | ||
| uint32_t count = SystemCoreClock / 1000 * ms; | ||
| while (count--) { | ||
| __asm volatile("nop"); | ||
| } | ||
| } | ||
|
Comment on lines
+75
to
+81
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: #!/bin/bash
# Find all callers of usbd_dwc2_delay_ms and the duration values they pass,
# to gauge timing sensitivity.
rg -nP '\busbd_dwc2_delay_ms\s*\(' -C1Repository: cherry-embedded/CherryUSB Length of output: 1818 🏁 Script executed: #!/bin/bash
# Check implementations across multiple glue files to see if pattern is consistent
echo "=== GD ARM implementation ==="
sed -n '72,78p' port/dwc2/usb_glue_gd_arm.c
echo -e "\n=== GD implementation ==="
sed -n '44,50p' port/dwc2/usb_glue_gd.c
echo -e "\n=== ST implementation ==="
sed -n '717,723p' port/dwc2/usb_glue_st.c
echo -e "\n=== HC implementation ==="
sed -n '297,303p' port/dwc2/usb_glue_hc.cRepository: cherry-embedded/CherryUSB Length of output: 776 🏁 Script executed: #!/bin/bash
# Search for broader context of the delay calls and related functions
echo "=== Context around delay calls in usb_dc_dwc2.c ==="
sed -n '125,145p' port/dwc2/usb_dc_dwc2.c
echo -e "\n=== Context around second delay call ==="
sed -n '660,670p' port/dwc2/usb_dc_dwc2.c
echo -e "\n=== Search for dwc2_set_mode and usbd_set_remote_wakeup mentioned in review ==="
rg -n 'dwc2_set_mode|usbd_set_remote_wakeup' port/dwc2/ -A2 -B2 | head -50Repository: cherry-embedded/CherryUSB Length of output: 2448 🏁 Script executed: #!/bin/bash
# Check if there's SystemCoreClock definition or any other delay implementations
echo "=== Looking for other delay timing approaches in the codebase ==="
rg -n 'SysTick|DWT|SystemTick' port/dwc2/ | head -20
echo ""
echo "=== Checking if there are calibration constants or timing configs ==="
rg -n 'calibrat|timing|cycle' port/dwc2/ -i | head -20Repository: cherry-embedded/CherryUSB Length of output: 345 Loop-based delay is subject to significant cycle overhead. The implementation assumes each loop iteration takes exactly one cycle, but the actual loop body ( Recommend either:
Additionally, 🤖 Prompt for AI Agents |
||
|
|
||
| uint32_t usbd_dwc2_get_system_clock(void) | ||
| { | ||
| return SystemCoreClock; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing
<string.h>and prefer angle-bracket form for<stdint.h>.memcpyis called at line 53 but<string.h>is not included anywhere in this file (it may transitively be available viausbd_core.h, but relying on that is fragile). Also,stdint.his a system header and should use angle brackets, matching the rest of the project.🛠️ Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents