Skip to content
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

Fix ch58x fs ip send bug and add ch585 usbhs dc #290

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

HaiMianBBao
Copy link
Contributor

@HaiMianBBao HaiMianBBao commented Feb 10, 2025

Summary by CodeRabbit

  • New Features

    • Introduced a comprehensive USB device driver for the CH585 microcontroller, enabling effective communication through multiple endpoints.
    • Added functions for USB initialization, endpoint management, and handling remote wakeup requests.
  • Bug Fixes

    • Enhanced USB communication reliability by ensuring that data transfers use precise transmission lengths, contributing to more consistent and dependable device connectivity during data transfers.
  • Documentation

    • Added a new header file defining register mappings and control structures for USB high-speed functionality, facilitating direct register access and management of USB operations.

Copy link

coderabbitai bot commented Feb 10, 2025

Walkthrough

The changes introduce a USB device controller implementation for the CH585 microcontroller, including modifications to the USBD_IRQHandler function to enhance USB IN token transfer handling. A new line sets the transmission length for endpoints when data length is within the maximum packet size. Additionally, a new header file defines register mappings for USB high-speed functionality, and several functions for endpoint management and USB operations are added.

Changes

File Path Change Summary
port/ch32/..._usb_ch58x_dc_usbfs.c Added a call (EPn_SET_TX_LEN(epid, usb_dc_cfg.ep_in[epid].xfer_len);) in the USBD_IRQHandler to set the endpoint transmission length when data length ≤ maximum packet size.
port/ch32/ch585_usbhs_dc.c Introduced USB device controller functions: usb_dc_init, usb_dc_deinit, usbd_set_address, usbd_set_remote_wakeup, usbd_get_port_speed, usbd_ep_open, usbd_ep_close, usbd_ep_set_stall, usbd_ep_clear_stall, usbd_ep_is_stalled, usbd_ep_start_write, usbd_ep_start_read, and the USBD_IRQHandler function.
port/ch32/ch585_usbhs_reg.h Added a new header file defining register mappings and control structures for USB high-speed functionality, including type definitions and macros.

Poem

I'm just a bunny, coding with delight,
Hopping through interrupts, day and night.
I set the length with a nimble hop,
Making sure USB transfers don't drop.
In a world of bytes, my changes shine so bright!
🐰✨ Happy coding, every byte's a delight!


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 147dc4a and 559c944.

📒 Files selected for processing (1)
  • port/ch32/usb_ch58x_dc_usbfs.c (1 hunks)

@@ -533,6 +533,7 @@ USBD_IRQHandler(void)
memcpy(usb_dc_cfg.ep_in[epid].ep_ram_addr, usb_dc_cfg.ep_in[epid].xfer_buf, usb_dc_cfg.ep_in[epid].mps);
} else {
memcpy(usb_dc_cfg.ep_in[epid].ep_ram_addr, usb_dc_cfg.ep_in[epid].xfer_buf, usb_dc_cfg.ep_in[epid].xfer_len);
EPn_SET_TX_LEN(epid, usb_dc_cfg.ep_in[epid].xfer_len);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

LGTM! Critical bug fix for USB IN transfers.

The added line correctly sets the transfer length before enabling the endpoint for transmission. This fixes a critical bug where the endpoint would use an undefined/previous length for partial transfers, potentially causing data corruption or incomplete transfers.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (4)
port/ch32/ch585_usbhs_dc.c (2)

123-126: Implement or remove unimplemented function.
Currently, usbd_set_remote_wakeup always returns -1. If remote wakeup support is not required by your application or the hardware, consider removing the function altogether. Otherwise, implement the required logic to enable/disable remote wakeup.


261-273: Check alignment constraints for DMA buffers.
The code checks (uint32_t)data & 0x03 to reject unaligned buffers. Ensure that higher-level code employing usbd_ep_start_write always provides 4-byte aligned buffers. Alternatively, you might add fallback logic to handle unaligned buffers if hardware or performance constraints allow.

port/ch32/ch585_usbhs_reg.h (2)

15-16: Correct typographical errors in comments.
Minor spelling issues can reduce clarity. Consider fixing them for better readability:

  • Line 15: “suspeng” → “suspend”
  • Line 16: “interupt” → “interrupt”
  • Line 23: “expceted” → “expected”
  • Line 61: “requirment” → “requirement”

Example fix (doc lines only):

-#define USBHS_UD_PHY_SUSPENDM   0x08                     // RW, suspeng USB PHY
+#define USBHS_UD_PHY_SUSPENDM   0x08                     // RW, suspend USB PHY

-#define USBHS_UD_CLR_ALL        0x04                     // RW, clear all interupt flag
+#define USBHS_UD_CLR_ALL        0x04                     // RW, clear all interrupt flag

-#define USBHS_UD_SPEED_TYPE     0x03                      // RW, speed mode excpeted by the equipment
+#define USBHS_UD_SPEED_TYPE     0x03                      // RW, speed mode expected by the equipment

-#define USBHS_UDMS_SUSP_REQ     0x10                      // RO, requirment of suspending USB
+#define USBHS_UDMS_SUSP_REQ     0x10                      // RO, requirement of suspending USB

Also applies to: 23-23, 61-61


354-450: Maintain consistent naming and style for registers.
You have used a robust naming convention (R8_USB2_, R16_USB2_, R32_USB2_). Ensure that future additions remain consistent to prevent confusion. Also, ensure macros describing register fields match the naming style for clarity and easy maintenance.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 559c944 and a4aa61a.

📒 Files selected for processing (2)
  • port/ch32/ch585_usbhs_dc.c (1 hunks)
  • port/ch32/ch585_usbhs_reg.h (1 hunks)
🔇 Additional comments (3)
port/ch32/ch585_usbhs_dc.c (2)

510-511: Confirm EPn_SET_RX_VALID(0) usage.
Line 511 leaves the EPn_SET_RX_VALID(0) call commented out. Confirm that explicitly enabling the endpoint 0 RX side is not required post-reset. If necessary, uncomment or call EPn_SET_RX_VALID(0) to ensure the control endpoint can receive new setup packets immediately after reset.


80-89: Validate register writes for control and mode.
CH585_USBHS_DEV->CONTROL is assigned twice (lines 82 and 89). Confirm that overwriting the earlier bits (like USBHS_UD_RST_LINK) is intentional and that the final register value is correct for initialization without missing any needed bits from the first assignment.

port/ch32/ch585_usbhs_reg.h (1)

46-54: Clarify interrupt flag clearing logic.
The comments for the interrupt flag register macros often use phrases like "clear ... interrupt flag," but some flags appear to be read-only or cleared by writing 1. Confirm that each enumerated bit is correctly documented and that writing 1 indeed clears the flag, as is common practice with hardware-based interrupt registers.

Comment on lines +236 to +242
int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled)
{
if (USB_EP_DIR_IS_OUT(ep)) {
} else {
}
return 0;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Complete the stall status check.
The usbd_ep_is_stalled function does not populate the stalled parameter, leaving it perpetually empty. You may need to read the corresponding register or a cached endpoint stall state to return the correct stall status.

Example fix:

 int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled)
 {
+    uint8_t ep_idx = USB_EP_GET_IDX(ep);
+    if (USB_EP_DIR_IS_OUT(ep)) {
+        *stalled = ((USB_GET_RX_CTRL(ep_idx) & USBHS_UEP_R_RES_MASK) == USBHS_UEP_R_RES_STALL);
+    } else {
+        *stalled = ((USB_GET_TX_CTRL(ep_idx) & USBHS_UEP_T_RES_MASK) == USBHS_UEP_T_RES_STALL);
+    }
     return 0;
 }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled)
{
if (USB_EP_DIR_IS_OUT(ep)) {
} else {
}
return 0;
}
int usbd_ep_is_stalled(uint8_t busid, const uint8_t ep, uint8_t *stalled)
{
uint8_t ep_idx = USB_EP_GET_IDX(ep);
if (USB_EP_DIR_IS_OUT(ep)) {
*stalled = ((USB_GET_RX_CTRL(ep_idx) & USBHS_UEP_R_RES_MASK) == USBHS_UEP_R_RES_STALL);
} else {
*stalled = ((USB_GET_TX_CTRL(ep_idx) & USBHS_UEP_T_RES_MASK) == USBHS_UEP_T_RES_STALL);
}
return 0;
}

@HaiMianBBao HaiMianBBao changed the title Fix ch58x fs ip send bug Fix ch58x fs ip send bug and add ch585 usbhs dc Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant