Skip to content

feat(port/cherryusb): add cherryusb for usb usage, based on cdc acm#6

Merged
MDLZCOOL merged 1 commit into
mainfrom
port_cherryusb
May 5, 2026
Merged

feat(port/cherryusb): add cherryusb for usb usage, based on cdc acm#6
MDLZCOOL merged 1 commit into
mainfrom
port_cherryusb

Conversation

@MDLZCOOL
Copy link
Copy Markdown
Owner

@MDLZCOOL MDLZCOOL commented May 5, 2026

Summary by CodeRabbit

Release Notes

  • New Features

    • Added CherryUSB as a selectable platform port option
    • Enabled USB-based shell communication via CherryUSB transport
  • Documentation

    • Added CherryUSB port setup documentation with configuration requirements

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 5, 2026

Warning

Rate limit exceeded

@MDLZCOOL has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 16 minutes and 53 seconds before requesting another review.

To keep reviews running without waiting, you can enable usage-based add-on for your organization. This allows additional reviews beyond the hourly cap. Account admins can enable it under billing.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6eacffe2-11b5-485a-89de-486573547492

📥 Commits

Reviewing files that changed from the base of the PR and between eab717f and 2d2951b.

📒 Files selected for processing (5)
  • CMakeLists.txt
  • Kconfig
  • port/cherryusb/README.md
  • port/cherryusb/ctshell_cherryusb.c
  • port/cherryusb/ctshell_cherryusb.h
📝 Walkthrough

Walkthrough

This PR adds support for a CherryUSB-based USB CDC-ACM transport layer for ctshell. It includes conditional build configuration, USB descriptor setup, data transmission/reception handlers, DTR state management, and documentation.

Changes

CherryUSB Port Addition

Layer / File(s) Summary
Build Configuration
Kconfig
Adds CTSHELL_PORT_CHERRYUSB as a selectable boolean option within the CTSHELL_PORT_SELECT choice.
Conditional Compilation
CMakeLists.txt
Conditionally appends CherryUSB source file, include directory, and preprocessor definition when the CherryUSB port is enabled.
USB Transport Implementation
port/cherryusb/ctshell_cherryusb.c
Implements USB CDC-ACM descriptors, bulk IN/OUT endpoint handlers, DTR state tracking, and transmit/receive logic. Exposes shell_usb_write() for transmission and integrates ctshell_input() for reception. Manages busy flags and endpoint state on USB configuration.
Documentation
port/cherryusb/README.md
Documents that CherryUSB must be pre-configured by users and notes the port derives from the CherryUSB CDC-ACM template.

Sequence Diagram

sequenceDiagram
    participant Host as Host/PC
    participant USB as USB Device<br/>(CherryUSB)
    participant Shell as ctshell<br/>Application

    Note over Host,Shell: Data Reception Path
    Host->>USB: Sends data (bulk OUT)
    USB->>USB: Bulk OUT endpoint handler
    USB->>Shell: ctshell_input(byte)
    Shell->>Shell: Process input

    Note over Host,Shell: Data Transmission Path
    Shell->>USB: shell_usb_write(buffer)
    USB->>USB: Check DTR enabled & busy flag
    USB->>USB: Copy to IN buffer
    USB->>Host: Sends data (bulk IN)
    Host->>Host: Receives data

    Note over Host,Shell: DTR Control
    Host->>USB: Sets/clears DTR
    USB->>USB: usbd_cdc_acm_set_dtr(dtr_state)
    USB->>Shell: Gates transmission capability
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 A USB rabbit hops with glee,
Through CherryUSB's CDC tree,
Descriptors bloom, endpoints align,
DTR guards each data line—
Shell and host now dance in sync! 🌳✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ 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%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 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 change: adding CherryUSB port support with CDC-ACM as the transport layer, matching the new files and configuration options introduced.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch port_cherryusb

Tip

💬 Introducing Slack Agent: The best way for teams to turn conversations into code.

Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.

  • Generate code and open pull requests
  • Plan features and break down work
  • Investigate incidents and troubleshoot customer tickets together
  • Automate recurring tasks and respond to alerts with triggers
  • Summarize progress and report instantly

Built for teams:

  • Shared memory across your entire org—no repeating context
  • Per-thread sandboxes to safely plan and execute work
  • Governance built-in—scoped access, auditability, and budget controls

One agent for your entire SDLC. Right inside Slack.

👉 Get started


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
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: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@port/cherryusb/ctshell_cherryusb.c`:
- Around line 179-191: cdc_acm_init stores the active USB bus via its busid but
shell_usb_write is still hardcoding bus 0 and truncating payloads to 2048 bytes;
update the code so shell_usb_write uses the configured busid from cdc_acm_init
(e.g., store busid in a static/global like current_bus or pass it through)
instead of 0, and remove the silent 2048-byte truncation so the function
transmits the full payload length (or fragments/sends in a loop if underlying
USB endpoint limits require it) using the write_buffer/its actual length when
calling the send/submit routines.
- Around line 116-141: The USB event handler must clear session state when the
link is lost: in usbd_event_handler handle USBD_EVENT_RESET and
USBD_EVENT_DISCONNECTED by resetting dtr_enable = false and ep_tx_busy_flag =
false (and any other per-session flags your transmit path uses) so subsequent
writes won't spin; keep the existing behavior for USBD_EVENT_CONFIGURED (start
read) but ensure you also stop/cancel or ignore pending transfers if applicable.
Apply the same changes to the other equivalent event block noted (the second
handler around lines 194-203) so both RESET and DISCONNECTED clear dtr_enable
and ep_tx_busy_flag.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 55b8315c-ceea-4c4c-b016-2ae52f6609e7

📥 Commits

Reviewing files that changed from the base of the PR and between 6a30f50 and eab717f.

📒 Files selected for processing (4)
  • CMakeLists.txt
  • Kconfig
  • port/cherryusb/README.md
  • port/cherryusb/ctshell_cherryusb.c

Comment on lines +116 to +141
static void usbd_event_handler(uint8_t busid, uint8_t event)
{
switch (event) {
case USBD_EVENT_RESET:
break;
case USBD_EVENT_CONNECTED:
break;
case USBD_EVENT_DISCONNECTED:
break;
case USBD_EVENT_RESUME:
break;
case USBD_EVENT_SUSPEND:
break;
case USBD_EVENT_CONFIGURED:
ep_tx_busy_flag = false;
/* setup first out ep read transfer */
usbd_ep_start_read(busid, CDC_OUT_EP, read_buffer, 2048);
break;
case USBD_EVENT_SET_REMOTE_WAKEUP:
break;
case USBD_EVENT_CLR_REMOTE_WAKEUP:
break;

default:
break;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Clear the session flags on reset/disconnect.

dtr_enable is only driven from usbd_cdc_acm_set_dtr(). If the host drops off the bus after setting DTR, later writes still enter the transmit path and spin until timeout because the link state was never invalidated.

Proposed fix
-volatile bool ep_tx_busy_flag = false;
+volatile bool ep_tx_busy_flag = false;
+static volatile uint8_t dtr_enable = 0;
@@
 	case USBD_EVENT_RESET:
+	  dtr_enable = 0;
+	  ep_tx_busy_flag = false;
 	  break;
@@
 	case USBD_EVENT_DISCONNECTED:
+	  dtr_enable = 0;
+	  ep_tx_busy_flag = false;
 	  break;
@@
 	case USBD_EVENT_SUSPEND:
+	  dtr_enable = 0;
+	  ep_tx_busy_flag = false;
 	  break;
@@
-volatile uint8_t dtr_enable = 0;
-
 void usbd_cdc_acm_set_dtr(uint8_t busid, uint8_t intf, bool dtr)

Also applies to: 194-203

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@port/cherryusb/ctshell_cherryusb.c` around lines 116 - 141, The USB event
handler must clear session state when the link is lost: in usbd_event_handler
handle USBD_EVENT_RESET and USBD_EVENT_DISCONNECTED by resetting dtr_enable =
false and ep_tx_busy_flag = false (and any other per-session flags your transmit
path uses) so subsequent writes won't spin; keep the existing behavior for
USBD_EVENT_CONFIGURED (start read) but ensure you also stop/cancel or ignore
pending transfers if applicable. Apply the same changes to the other equivalent
event block noted (the second handler around lines 194-203) so both RESET and
DISCONNECTED clear dtr_enable and ep_tx_busy_flag.

Comment on lines +179 to +191
void cdc_acm_init(uint8_t busid, uintptr_t reg_base)
{
const uint8_t data[10] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30 };

memcpy(&write_buffer[0], data, 10);
memset(&write_buffer[10], 'a', 2038);

usbd_desc_register(busid, cdc_descriptor);
usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &intf0));
usbd_add_interface(busid, usbd_cdc_acm_init_intf(busid, &intf1));
usbd_add_endpoint(busid, &cdc_out_ep);
usbd_add_endpoint(busid, &cdc_in_ep);
usbd_initialize(busid, reg_base, usbd_event_handler);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Make shell_usb_write() use the configured bus and send the full payload.

Line 228 hard-codes bus 0 even though cdc_acm_init() accepts busid, and Line 220 silently truncates anything larger than 2048 bytes. That makes non-zero bus configurations fail and cuts off longer shell output.

Proposed fix
+static uint8_t cdc_busid;
+
 void cdc_acm_init(uint8_t busid, uintptr_t reg_base)
 {
+  cdc_busid = busid;
   const uint8_t data[10] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x30 };
 
   memcpy(&write_buffer[0], data, 10);
   memset(&write_buffer[10], 'a', 2038);
@@
 void shell_usb_write(const char *buf, uint16_t len, void *priv)
 {
   if (!dtr_enable || len == 0) {
 	return;
   }
-  uint16_t send_len = (len > 2048) ? 2048 : len;
-  uint32_t timeout = 0xFFFFF;
-  while (ep_tx_busy_flag && timeout > 0) {
-	timeout--;
-  }
-  if (timeout == 0) return;
-  memcpy(write_buffer, buf, send_len);
-  ep_tx_busy_flag = true;
-  usbd_ep_start_write(0, CDC_IN_EP, write_buffer, send_len);
+  while (len > 0) {
+	uint16_t send_len = (len > sizeof(write_buffer)) ? sizeof(write_buffer) : len;
+	uint32_t timeout = 0xFFFFF;
+
+	while (ep_tx_busy_flag && timeout > 0) {
+	  timeout--;
+	}
+	if (timeout == 0) {
+	  return;
+	}
+
+	memcpy(write_buffer, buf, send_len);
+	ep_tx_busy_flag = true;
+	usbd_ep_start_write(cdc_busid, CDC_IN_EP, write_buffer, send_len);
+
+	buf += send_len;
+	len -= send_len;
+  }
 }

Also applies to: 215-228

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@port/cherryusb/ctshell_cherryusb.c` around lines 179 - 191, cdc_acm_init
stores the active USB bus via its busid but shell_usb_write is still hardcoding
bus 0 and truncating payloads to 2048 bytes; update the code so shell_usb_write
uses the configured busid from cdc_acm_init (e.g., store busid in a
static/global like current_bus or pass it through) instead of 0, and remove the
silent 2048-byte truncation so the function transmits the full payload length
(or fragments/sends in a loop if underlying USB endpoint limits require it)
using the write_buffer/its actual length when calling the send/submit routines.

@MDLZCOOL MDLZCOOL merged commit 963fe7a into main May 5, 2026
2 checks passed
MDLZCOOL added a commit that referenced this pull request May 5, 2026
feat(port/cherryusb): add cherryusb for usb usage, based on cdc acm
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