Skip to content

Conversation

@Rbb666
Copy link
Contributor

@Rbb666 Rbb666 commented Nov 7, 2025

Sync:RT-Thread/rt-thread#10844

Summary by CodeRabbit

  • Refactor
    • Improved DHCP handling with conditional configuration support for better flexibility across different deployment scenarios.
    • Enhanced delay implementation with optimized timing behavior on supported runtime environments.

@Rbb666 Rbb666 changed the title fix:Fix RNDIS DHCP dependency,and delay implementation fix:RNDIS DHCP dependency,and delay implementation Nov 7, 2025
@coderabbitai
Copy link

coderabbitai bot commented Nov 7, 2025

Walkthrough

Two files modified to introduce conditional compilation logic: DHCP initialization in RNDIS CDC template now branches between DHCPD server mode and netdev-based DHCP, and USB Kinetis delay function conditionally uses RT-Thread sleep or busy-wait depending on thread support availability.

Changes

Cohort / File(s) Summary
DHCP Conditional Handling
demo/cdc_rndis_template.c
DHCP initialization made conditional: if LWIP_USING_DHCPD is defined, calls dhcpd_start("u0"); otherwise, uses netdev_dhcp_enabled(). Includes adjusted to conditionally include dhcp_server.h only when DHCPD is used. Logic applied to both RNDIS and non-RNDIS LWIP paths.
Delay Implementation Conditional
port/kinetis/usb_glue_mcx.c
usbd_kinetis_delay_ms() function now conditionally implements delay: if __RTTHREAD__ is defined, uses rt_thread_mdelay(ms); otherwise, uses nested loop busy-wait delay.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

  • Changes consist of straightforward preprocessor conditionals and logic branches
  • DHCP path selection is localized to initialization sequence without cascading effects
  • Delay implementation is a simple function-level conditional
  • Consider verifying: inclusion guard order in cdc_rndis_template.c, and whether busy-wait loop timing is acceptable as fallback in usb_glue_mcx.c

Poem

🐰 Hop, skip, and choose the DHCP way,
RT-Thread sleeps, or loops delay,
Conditional branches bloom so bright,
Each path selected just right!

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main changes: fixing RNDIS DHCP dependency handling and delay implementation across the modified files.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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

Comment @coderabbitai help to get the list of available commands and usage tips.

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 (2)
port/kinetis/usb_glue_mcx.c (1)

83-83: Parameter type limits maximum delay to 255ms.

The uint8_t parameter restricts the delay to a maximum of 255 milliseconds. If longer delays might be needed in the future, consider using uint32_t for the parameter type.

demo/cdc_rndis_template.c (1)

192-199: Make netdev.h include conditional to match usage.

The netdev.h header is only used when LWIP_USING_DHCPD is not defined (lines 256-259), but it's included unconditionally. Consider making it conditional to avoid unnecessary dependencies.

Apply this diff:

 #ifdef LWIP_USING_DHCPD
 #include <dhcp_server.h>
+#else
+#include <netdev.h>
 #endif
 
 #include <rtthread.h>
 #include <rtdevice.h>
 #include <netif/ethernetif.h>
-#include <netdev.h>
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e453e72 and 9cdafea.

📒 Files selected for processing (2)
  • demo/cdc_rndis_template.c (2 hunks)
  • port/kinetis/usb_glue_mcx.c (1 hunks)
🔇 Additional comments (1)
demo/cdc_rndis_template.c (1)

253-260: Consider adding error handling and logging for DHCP initialization failures.

Both DHCP initialization paths lack error handling:

  • dhcpd_start("u0") (line 254) return value is not checked
  • When netdev_get_by_name returns NULL (lines 256-259), the failure is silent

Silent failures could lead to connectivity issues that are difficult to debug in production.

Consider adding error logging:

 #ifdef LWIP_USING_DHCPD
-    dhcpd_start("u0");
+    if (dhcpd_start("u0") != 0) {
+        /* Log error or handle failure */
+    }
 #else
     struct netdev *netdev = netdev_get_by_name("u0");
     if (netdev) {
         netdev_dhcp_enabled(netdev, RT_TRUE);
+    } else {
+        /* Log error: network device not found */
     }
 #endif

Would you like me to generate a script to verify if error handling patterns exist elsewhere in the codebase?

Comment on lines +85 to +92
#ifdef __RTTHREAD__
rt_thread_mdelay(ms);
#else
for (uint32_t i = 0; i < ms; i++)
{
for (volatile uint32_t j = 0; j < 10000; j++);
}
#endif
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Magic constant makes delay inaccurate across different MCU models.

The busy-wait calibration constant 10000 is CPU frequency dependent and will produce inaccurate delays across the supported Kinetis MCU models (MCXC444, MCXA153, MCXN947, MCXA156), which have different maximum clock frequencies (e.g., MCXN947 at 150MHz vs MCXA153 at 96MHz).

Consider using SystemCoreClock to calculate the iteration count dynamically:

 #ifdef __RTTHREAD__
     rt_thread_mdelay(ms);
 #else
+    /* Approximate delay based on core clock frequency */
+    uint32_t cycles_per_ms = SystemCoreClock / 1000;
+    uint32_t iterations = cycles_per_ms / 10;  /* Rough estimate accounting for loop overhead */
     for (uint32_t i = 0; i < ms; i++)
     {
-        for (volatile uint32_t j = 0; j < 10000; j++);
+        for (volatile uint32_t j = 0; j < iterations; j++);
     }
 #endif

Alternatively, if precise timing is not critical, document the expected CPU frequency for the current calibration value.

📝 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
#ifdef __RTTHREAD__
rt_thread_mdelay(ms);
#else
for (uint32_t i = 0; i < ms; i++)
{
for (volatile uint32_t j = 0; j < 10000; j++);
}
#endif
#ifdef __RTTHREAD__
rt_thread_mdelay(ms);
#else
/* Approximate delay based on core clock frequency */
uint32_t cycles_per_ms = SystemCoreClock / 1000;
uint32_t iterations = cycles_per_ms / 10; /* Rough estimate accounting for loop overhead */
for (uint32_t i = 0; i < ms; i++)
{
for (volatile uint32_t j = 0; j < iterations; j++);
}
#endif
🤖 Prompt for AI Agents
In port/kinetis/usb_glue_mcx.c around lines 85-92 the busy-wait uses a magic
constant (10000) which is CPU-frequency dependent; replace the hardcoded
inner-loop count with a calculation derived from SystemCoreClock so the loop
produces ~1 ms per outer iteration (e.g., compute iterations = (SystemCoreClock
/ 1000) / estimated_cycles_per_loop and use that value instead of 10000), or
better yet call a platform delay API (rt_thread_mdelay or HAL_Delay / SysTick)
if available; if SystemCoreClock is not present, add a clear comment documenting
the CPU frequency assumption and provide a #define fallback calibration constant
and TODO to calibrate per-MCU.

@sakumisu sakumisu merged commit eec89fd into cherry-embedded:master Nov 7, 2025
1 check 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