Skip to content

[update] port: hpmicro: update usb_dc_hpm.c#316

Merged
sakumisu merged 1 commit intocherry-embedded:masterfrom
chenzhihong007:master
May 12, 2025
Merged

[update] port: hpmicro: update usb_dc_hpm.c#316
sakumisu merged 1 commit intocherry-embedded:masterfrom
chenzhihong007:master

Conversation

@chenzhihong007
Copy link
Copy Markdown
Contributor

@chenzhihong007 chenzhihong007 commented May 12, 2025

  • update usb_dc_hpm.c

Summary by CodeRabbit

  • Refactor
    • Improved interrupt mask handling by using predefined hardware-specific constants.
    • Consolidated DCD data storage for multiple buses into a single, aligned array for improved clarity and efficiency.
    • Simplified initialization logic for device controller data assignment.
    • Enhanced endpoint transfer completion handling for better resource management.
  • Style
    • Added spacing in the IRQ handler for improved code readability.

- update usb_dc_hpm.c

Signed-off-by: Zhihong Chen <zhihong.chen@hpmicro.com>
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented May 12, 2025

Walkthrough

The update refactors internal logic in the USB device controller driver for HPM platforms. It standardizes interrupt mask usage, consolidates DCD data storage into a 2D array, simplifies initialization logic, and improves endpoint transfer completion by iterating over QTD chains and marking them as not in use. No public interfaces are changed.

Changes

File(s) Change Summary
port/hpm/usb_dc_hpm.c Updated copyright year; replaced manual bitmasks with hardware-specific masks; consolidated DCD data storage into a 2D array; simplified dcd_data pointer assignment; updated interrupt mask usage; improved endpoint QTD completion handling by iterating over QTD chains and marking them as not in use.

Poem

In circuits where the data flows,
A rabbit tweaks how USB grows.
With masks aligned and data neat,
Transfers hop without defeat.
QTDs now rest, their work all done—
This code refactor’s just begun!
🐇✨

Tip

⚡️ Faster reviews with caching
  • CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.

Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @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
Copy Markdown

@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

🔭 Outside diff range comments (2)
port/hpm/usb_dc_hpm.c (2)

311-314: ⚠️ Potential issue

Typo breaks the build: USB_SOS_DCD_MAX_QHD_COUNTUSB_SOC_DCD_MAX_QHD_COUNT

The loop uses an undefined macro (USB_SOS…), leading to a compile-time error.

-for (uint8_t ep_idx = 0; ep_idx < USB_SOS_DCD_MAX_QHD_COUNT; ep_idx++) {
+for (uint8_t ep_idx = 0; ep_idx < USB_SOC_DCD_MAX_QHD_COUNT; ep_idx++) {

318-331: 🛠️ Refactor suggestion

Null-pointer guard required before dereferencing attached_qtd

p_qhd->attached_qtd can legally be NULL (e.g. if SW cleared it after recycle).
Dereferencing it unconditionally triggers undefined behaviour.

-dcd_qtd_t *p_qtd = p_qhd->attached_qtd;
-if (p_qtd == NULL) {
-    /* Nothing to process – continue with next endpoint */
-    continue;
-}
+dcd_qtd_t *p_qtd = p_qhd->attached_qtd;
+if (p_qtd == NULL) {
+    /* No queued TDs, skip this endpoint */
+    continue;
+}

Optionally, after the while-loop completes, set p_qhd->attached_qtd = NULL so a reused QTD doesn’t get processed twice.

🧹 Nitpick comments (1)
port/hpm/usb_dc_hpm.c (1)

47-52: Alignment per-bus is implicitly preserved, but add a static assert

HPM_ALIGN_UP(sizeof(dcd_data_t), USB_SOC_DCD_DATA_RAM_ADDRESS_ALIGNMENT) guarantees that the stride is a multiple of the required alignment, so every element stays aligned.
However, if either macro is ever modified the guarantee becomes non-obvious. A compile-time assert would document and enforce the constraint:

+#include <assert.h>
+#define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(COND)?1:-1]
+
 STATIC_ASSERT(
     (HPM_ALIGN_UP(sizeof(dcd_data_t), USB_SOC_DCD_DATA_RAM_ADDRESS_ALIGNMENT) %
      USB_SOC_DCD_DATA_RAM_ADDRESS_ALIGNMENT) == 0,
     dcd_stride_must_be_multiple_of_alignment);
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 6064c0b and c81f020.

📒 Files selected for processing (1)
  • port/hpm/usb_dc_hpm.c (8 hunks)
🔇 Additional comments (2)
port/hpm/usb_dc_hpm.c (2)

16-23: Interrupt-mask constants look correct – nice cleanup

Switching to the HW-supplied USB_USBINTR_* masks removes the manual bit-twiddling and eliminates a class of maintenance errors.
LGTM.


84-88: Re-evaluate dropping intr_nak from the default mask

The previous implementation enabled NAK interrupts; the new one omits them.
If upper layers rely on NAK events for flow-control (common for isochronous endpoints), this change may cause silent stalls.

Please confirm that:

  1. Higher layers never expect NAK callbacks, or
  2. Another code path enables intr_nak later.

If neither is true, simply OR it back in:

-int_mask = (intr_usb | intr_error |intr_port_change | intr_reset | intr_suspend);
+int_mask = (intr_usb | intr_error | intr_port_change | intr_reset |
+            intr_suspend | intr_nak);

Comment thread port/hpm/usb_dc_hpm.c
Comment on lines +69 to 70
g_hpm_udc[busid].handle->dcd_data = (dcd_data_t *)&_dcd_data[busid][0];

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Avoid C-aliasing UB by typing the backing store correctly

_dcd_data is declared as uint8_t [...] and then cast to dcd_data_t*.
This can break strict-aliasing optimisations on some compilers.

-uint8_t _dcd_data[CONFIG_USBDEV_MAX_BUS][HPM_ALIGN_UP(sizeof(dcd_data_t), USB_SOC_DCD_DATA_RAM_ADDRESS_ALIGNMENT)];
+dcd_data_t _dcd_data[CONFIG_USBDEV_MAX_BUS]  /* already aligned by attribute */;

The attribute can stay unchanged – GCC/Clang apply it to the entire object.
With the array typed correctly the cast at line 69 is no longer required.

@sakumisu sakumisu merged commit 8c100cd into cherry-embedded:master May 12, 2025
2 checks passed
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.

2 participants