Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions demo/cdc_rndis_template.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,14 @@ void usbd_rndis_data_send_done(uint32_t len)
#error rndis must enable RT_LWIP_DHCP
#endif

#ifndef LWIP_USING_DHCPD
#error rndis must enable LWIP_USING_DHCPD
#ifdef LWIP_USING_DHCPD
#include <dhcp_server.h>
#endif

#include <rtthread.h>
#include <rtdevice.h>
#include <netif/ethernetif.h>
#include <dhcp_server.h>
#include <netdev.h>

struct eth_device rndis_dev;

Expand Down Expand Up @@ -250,7 +250,14 @@ void rndis_lwip_init(void)
eth_device_init(&rndis_dev, "u0");

eth_device_linkchange(&rndis_dev, RT_TRUE);
#ifdef LWIP_USING_DHCPD
dhcpd_start("u0");
#else
struct netdev *netdev = netdev_get_by_name("u0");
if (netdev) {
netdev_dhcp_enabled(netdev, RT_TRUE);
}
#endif
}

void usbd_rndis_data_recv_done(uint32_t len)
Expand Down
8 changes: 8 additions & 0 deletions port/kinetis/usb_glue_mcx.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,12 @@ void usb_dc_low_level_deinit(uint8_t busid)

void usbd_kinetis_delay_ms(uint8_t ms)
{
#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
Comment on lines +85 to +92
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.

}