Skip to content

fix: correct CH32X035 RX_RES bit positions and shared control register handling - #1

Draft
21km43 with Copilot wants to merge 2 commits into
masterfrom
copilot/review-pull-request-486
Draft

fix: correct CH32X035 RX_RES bit positions and shared control register handling#1
21km43 with Copilot wants to merge 2 commits into
masterfrom
copilot/review-pull-request-486

Conversation

Copilot AI commented May 1, 2026

Copy link
Copy Markdown

概要

PR adafruit#486 (adafruit/Adafruit_TinyUSB_Arduino) のコードレビューで特定されたバグを修正します。

修正内容

Bug 1: USBFS_EP_R_RES_* のビット位置不整合 (ch32_usbfs_reg.h)

CH32X035 向けの RX 応答値が (x << 0) で定義されていたが、マスク USBFS_EP_R_RES_MASK(3 << 2)(ビット [3:2])を使用しており不整合。

// 修正前 (誤り)
#define USBFS_EP_R_RES_NAK   (2 << 0)  // = 0x02 (bits [1:0])
// 修正後 (正しい)
#define USBFS_EP_R_RES_NAK   (2 << 2)  // = 0x08 (bits [3:2])

Bug 2: EP_TX_CTRL / EP_RX_CTRL が同一アドレスを指す (dcd_ch32_usbfs.c)

CH32X035 では TX と RX が同一の 8 ビットレジスタを共有する:

  • bits [1:0] = TX response, bit [6] = TX toggle
  • bits [3:2] = RX response, bit [7] = RX toggle
  • bit [4] = AUTO_TOG(TX/RX 共有)

従来コードは EP_TX_CTRL(ep) = val の後に EP_RX_CTRL(ep) = val を書くと前の値が上書きされるため、初期化で TX 設定が失われていた。

修正方法:

  • ヘルパーマクロ CH32X035_PRESERVE_TX_BITS(ep) / CH32X035_PRESERVE_RX_BITS(ep) を追加
  • 全制御レジスタ書き込み箇所に #if defined(CH32X035) ガードを追加し、read-modify-write または結合書き込みを使用

影響箇所:

  • dcd_init: EP0・EP1-7 の初期化
  • update_in, update_out: EP0 制御中の CTRL 書き込み
  • dcd_int_handler: SETUP/BUS_RST 割り込みハンドラ
  • dcd_edpt0_status_complete, dcd_edpt_open, dcd_edpt_stall, dcd_edpt_clear_stall

関連

Copilot AI and others added 2 commits May 1, 2026 01:53
- ch32_usbfs_reg.h: Fix USBFS_EP_R_RES_* values from (x<<0) to (x<<2) to match
  USBFS_EP_R_RES_MASK at bits [3:2] for CH32X035
- dcd_ch32_usbfs.c: Add #if defined(CH32X035) guards throughout driver to handle
  the shared TX/RX control register in CH32X035:
  - dcd_init: combine TX+RX writes for EP0 and EP1-7 initialization
  - update_in: preserve RX bits when updating TX for EP0
  - update_out: preserve TX bits when setting RX=ACK for EP0
  - dcd_int_handler: fix SETUP and BUS_RST EP0 register writes
  - dcd_edpt0_status_complete: combine TX+RX writes
  - dcd_edpt_open: use read-modify-write to preserve other direction
  - dcd_edpt_stall/clear_stall: use read-modify-write for EP0

Agent-Logs-Url: https://github.com/21km43/Adafruit_TinyUSB_Arduino/sessions/5141344a-6785-407d-8fca-757e32bbc9c4

Co-authored-by: 21km43 <48169975+21km43@users.noreply.github.com>
…omment

- Extract repeated read-modify-write patterns into CH32X035_PRESERVE_TX_BITS
  and CH32X035_PRESERVE_RX_BITS helper macros
- Use EP_RX_CTRL in PRESERVE_RX_BITS for semantic clarity
- Clarify AUTO_TOG comment in ch32_usbfs_reg.h to document shared-bit behavior

Agent-Logs-Url: https://github.com/21km43/Adafruit_TinyUSB_Arduino/sessions/5141344a-6785-407d-8fca-757e32bbc9c4

Co-authored-by: 21km43 <48169975+21km43@users.noreply.github.com>
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