Fix Serial.available() returning -1 on all CH32 series MCUs - #225
Fix Serial.available() returning -1 on all CH32 series MCUs#225jobitjoseph wants to merge 2 commits into
Conversation
|
Hello @jobitjoseph , thank you for your contribution. A quick look at your changes shows that they implement interrupt handlers for up to 8 usart instances. At the moment I don't have time for a full look at the code. Can you tell what the other differences offer? Do you have hardware to support up to 8 usart instances? Perhaps we can see if these two PRs can be merged into one that replaces PR #201. |
|
Okay, I only had V003/V002/V006/X033 (TSSOP20) for testing. The TSSOP20 package of the X033 doesn't have all serials fully available, so I didn't implement more than two. I added some defines in a header file to allow selection of either one instance of those two, or using two instances simultaneously. If I remember well, the changes of PR #180 were also required for that, so I included those in #201. Whenever I have a bit more time to look into it, I will look again at your changes. |
|
Is there any progress on that ? I hit the problem today |
Did you apply any of the PR's mentioned above? |
The pull request was not merged yet. You can either manually apply this patch or you can use my fork of the Arduino core with this patch already applied. https://raw.githubusercontent.com/jobitjoseph/CH32_Arduino_Core/main/board_manager/package_ch32_index.json |
Problem
The current HardwareSerial implementation has
Serial.available()hardcoded to return-1, making it impossible to use standard Arduino serial patterns like echo loops or data polling. This affects all CH32 series MCUs (CH32V003, CH32V203, CH32X035, CH32V103, CH32V307).Root Cause
available()function returns-1instead of the actual byte countNo RX buffering implementation
No interrupt-driven reception
Solution
This PR implements proper UART RX buffering with interrupt handlers for all CH32 series MCUs while maintaining backwards compatibility with other Arduino cores.
Changes Made
Added conditional RX buffering - Only enabled for CH32 series MCUs
Implemented interrupt handlers - For all UART variants (USART1-8, UART4-8)
Fixed core functions:
available()- Returns actual buffered byte countpeek()- Returns next byte without consuming itread()- Reads from interrupt-filled bufferMaintained compatibility - Non-CH32 MCUs use original behaviour
Safe implementation - Only creates handlers for existing Serial objects
Technical Details
64-byte RX buffer per UART with overflow protection
Conditional compilation prevents linker errors
Interrupt priority set to 2 for all UARTs
Buffer variables are only allocated on CH32 MCUs
Testing
Verified on CH32X035 board
Serial.available()now returns0when no data (instead of-1)Echo functionality works correctly
No compilation errors on any supported MCU
Backward Compatibility
Zero impact on existing projects
Non-CH32 Arduino cores unchanged
Existing CH32 code works without modification
Before:
Serial.available(); // Always returned -1After:
Serial.available(); // Returns actual byte count (0, 1, 2, etc.)Tested on CH32X035