Skip to content

Resolve: CH32V003 Once write a sketch, can't write a sketch again #203

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

hidori
Copy link

@hidori hidori commented Jun 20, 2025

Close #202

In CH32V003, PD1 and SWIO are assigned to the same pin, and whether it behaves as GPIO or SWIO is determined by the setting of the built-in register of muc.

In $/system/CH32V00x/USER/system_ch32v00x.c, PD1 is configured to act as gpio as shown below.

RCC->APB2PCENR |= RCC_AFIOEN;
AFIO->PCFR1 |= (1<<15);

Therefore, once you write a sketch to CH32V003, you will never be able to write a sketch to it again.

When developing using the Arduino IDE, most people will likely write sketches multiple times, so this behavior is unacceptable.

@Copilot Copilot AI review requested due to automatic review settings June 20, 2025 15:34
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR addresses Issue #202 by removing the configuration of PD1 as a GPIO in several system clock setup functions, thereby preventing the device from locking out subsequent sketch uploads.

  • Removed duplicated PD1 GPIO configuration calls in clock initialization functions for 8MHz, 24MHz, and 48MHz modes.
  • Ensures that the SWIO functionality is preserved for subsequent sketch programming.
Comments suppressed due to low confidence (1)

system/CH32V00x/USER/system_ch32v00x.c:249

 */

@maxint-rd
Copy link
Contributor

maxint-rd commented Jun 20, 2025

Hello @hidori - I'm not sure if I understand your issue completely. In all my V003 projects I have been able to flash the chip again and again from the IDE, do some interactive debugging and then flash is many more times without issues. However, the ability to do so may become different after using the PD1 pin. Using the SWIO pin for any other function disables the SWIOO function. I believe this is intended behavior.

In the SOP8 package (CH32V003J4M6) pins have multiple functions and some pins even have multiple GPIO's. This feature allows efficient usage of the limited number of pins. One should know that for instance using Serial (PD5/TX) disables the SWIO pin in that package.

Other packages of the V003 do not have such sharing of the SWIO pin, but one might still want to use PD1 as an extra GPIO..
In my opinion removing the possibility to use PD1 introduces a limitation. Perhaps your PR should add a define to optionally disable PD1. (In my board definition (see PR #200) I've added frequently used peripheral selection as menu options in the IDE. Previously these were defines in some system file.)

NOTE: reusing the SWIO pin doesn't brick the chip. After using features such as serial on the SOP8 chip, one can flash it only once. To restore the SWIO function of the pin for flashing one needs to use the WCH-LinkUtility on the LinkE and use the function Clear all code flash - by power off

(Did you know about this method? This is a known feature that has been mentioned in other issues. See for instance issue #143, #157, #194)
Please let me know if I misunderstood your issue.

For reference see pin usage of the J4M6 package:
SOP8 pinout

@hidori hidori closed this by deleting the head repository Jun 24, 2025
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.

CH32V003: Once write a sketch, can't write a sketch again
2 participants