Skip to content

Commit 6e94f7e

Browse files
authored
Board definition for CH32X033F8P6 TSSOP20 #171
At the current stage it works for me. Have fun! Info below is also in the readme of the board definition directory. +------v------+ MISO/A6 D6~ 1-+PA6 PA5+-20 D5 SCK/A5 MOSI/A8/TX4 D7 2-+PA7=PB0 PA4+-19 D4~ CS/A4 A9/RX4 D8 3-+PB1 PC19+-18 D17 SWCLK /RST D9 4-+PB7 PA3+-17 D3~ RX2/A3* USBDM D10 5-+PC16=PC11 PA2+-16 D2~ TX2/A2 USBDP D11 6-+PC17=PC10 PA1+-15 D1~ A1 GND 7-+VSS PA0+-14 D0~ A0 SWDIO D16 8-+PC18 PC3+-13 D15~ A13 VCC 9-+VDD PA10+-12 D14 SCL/TX1 D12 10-+PA9 PA11+-11 D13 SDA/RX1 +-------------+ *A3, VREF and hardware I2C don't work on CH32X033F8P6 0-series (lot number with the penultimate bit 5 being 0). Tested features digitalWrite()/digitalRead() - can use Arduino pin numbers or PAx notation. analogWrite() - 12-bit resolution, pins marked with ~ in pinout above. analogRead() - very stable 12-bit resolution, but issues with A3/A0/PADC_VREF Serial.print() - Tested 115200 bps on Serial2 (PA2/PA3) and Serial1 (PA10/PA11), as set in variant_CH32X033F8P6.h EEPROM library - may need improvement (updated to support 122 bytes). Pin PB7 (Arduino pin 9) can be configured as hardware reset /RST using WCH Link Utility. (See issue Hardware reset pin doesn't work on CH32X033 or CH32X035 #123) Known issues/limitations Pins PA7/PB0, PC16/PC11 and PC17/PC10 cannot be used for output. Any signal on A0 seems to show on other ADC pins when disconnected. A3, PADC_VREF and I2C don't work on CH32X033F8P6 0-series (lot number with the penultimate bit 5 being 0). To counter missing I2C the Software_I2C library by Seeed Studio is a good alternative, although it needs some changes to improve compatibility in I2C scanning. openwch/arduino_core_ch32#171
1 parent 824cf4a commit 6e94f7e

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

libraries/EEPROM/src/EEPROM.cpp

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,44 @@
1818
These 8 bytes use 16 bytes of flash. The total storage area page is 64 bytes, leaving 48 bytes available,
1919
(including required inverse values). This is 24 bytes that can be used plus the two data0 and data1 bytes.
2020
Layout for uint8_t _data[26]: { ob[4], ob[6], ob[16...62] ].
21+
At the current stage it works for me. Have fun!
22+
Info below is also in the readme of the board definition directory.
23+
24+
+------v------+
25+
MISO/A6 D6~ 1-+PA6 PA5+-20 D5 SCK/A5
26+
MOSI/A8/TX4 D7 2-+PA7=PB0 PA4+-19 D4~ CS/A4
27+
A9/RX4 D8 3-+PB1 PC19+-18 D17 SWCLK
28+
/RST D9 4-+PB7 PA3+-17 D3~ RX2/A3*
29+
USBDM D10 5-+PC16=PC11 PA2+-16 D2~ TX2/A2
30+
USBDP D11 6-+PC17=PC10 PA1+-15 D1~ A1
31+
GND 7-+VSS PA0+-14 D0~ A0
32+
SWDIO D16 8-+PC18 PC3+-13 D15~ A13
33+
VCC 9-+VDD PA10+-12 D14 SCL/TX1
34+
D12 10-+PA9 PA11+-11 D13 SDA/RX1
35+
+-------------+
36+
*A3, VREF and hardware I2C don't work on CH32X033F8P6 0-series (lot number with the penultimate bit 5 being 0).
37+
38+
39+
Tested features
40+
41+
digitalWrite()/digitalRead() - can use Arduino pin numbers or PAx notation.
42+
analogWrite() - 12-bit resolution, pins marked with ~ in pinout above.
43+
analogRead() - very stable 12-bit resolution, but issues with A3/A0/PADC_VREF
44+
Serial.print() - Tested 115200 bps on Serial2 (PA2/PA3) and Serial1 (PA10/PA11), as set in variant_CH32X033F8P6.h
45+
EEPROM library - may need improvement (updated to support 122 bytes).
46+
Pin PB7 (Arduino pin 9) can be configured as hardware reset /RST using WCH Link Utility. (See issue Hardware reset pin doesn't work on CH32X033 or CH32X035 #123)
47+
Known issues/limitations
48+
49+
Pins PA7/PB0, PC16/PC11 and PC17/PC10 cannot be used for output.
50+
Any signal on A0 seems to show on other ADC pins when disconnected.
51+
A3, PADC_VREF and I2C don't work on CH32X033F8P6 0-series (lot number with the penultimate bit 5 being 0).
52+
To counter missing I2C the Software_I2C library by Seeed Studio is a good alternative, although it needs some changes to improve compatibility in I2C scanning.
53+
54+
https://github.com/openwch/arduino_core_ch32/pull/171
2155
*/
2256
#include <EEPROM.h>
2357

24-
#define OB_AVAIL_DATA_START 8 // valid for CH32V003; how about others?
58+
#define OB_AVAIL_DATA_START 8 // valid for CH32V003; how about others? (same for X03x, V20x, V30x, V31x)
2559

2660
EEPROMClass::EEPROMClass(void) {
2761
}
@@ -44,7 +78,11 @@ uint32_t EEPROMClass::ReadOptionBytes()
4478

4579
void EEPROMClass::begin(void)
4680
{
47-
_size = 26; // Option bytes available on CH32V003: 64 - 16 = 48; 48/2=24, 2+24=26
81+
#if defined(CH32VM00X) || defined(CH32X035)
82+
_size = 122; // Option bytes available on CH32V002/V006, CH32X035/X033: 256- 16 = 240; 240/2=120, 2+120=122
83+
#else
84+
_size = 26; // Option bytes available on CH32V003: 64 - 16 = 48; 48/2=24, 2+24=26
85+
#endif
4886

4987
// Allocate data buffer and copy the current content from storage
5088
if(!_data)
@@ -141,13 +179,29 @@ bool EEPROMClass::commit()
141179
// Note that when a byte is written as a word, it automatically gets its inverse value as second byte
142180
if(_data && _size)
143181
{
182+
#if defined(CH32VM00X) || defined(CH32X035)
183+
// To write to the larger 256B option byte area of V00X, we use fast page programming as shown by EVT EXAM code
184+
// (see FLASH_OptionBytePR() in /EVT/EXAM/SRC/Peripheral/src/ch32v00X_flash.c)
185+
// Tested OK for V002/V006. X035 seems similar, but first needs testing
186+
FLASH_Unlock_Fast();
187+
FLASH_BufReset();
188+
for (int i=2;i<_size; i++) {
189+
uint32_t val;
190+
val=_data[i+1]<<16 | _data[i];
191+
FLASH_BufLoad((OB_BASE + (OB_AVAIL_DATA_START + (i-2))*2), val);
192+
i++;
193+
}
194+
FLASH_ProgramPage_Fast(OB_BASE);
195+
FLASH_Lock_Fast();
196+
#else
144197
uint16_t *ob16p=(uint16_t *)OB_BASE;
145198
for (int i=2;i<_size; i++) {
146199
if(_data[i]!=0xFF) {
147200
ob16p[OB_AVAIL_DATA_START+(i-2)]=_data[i];
148201
while (FLASH->STATR & FLASH_BUSY); // Wait for flash operation to be done
149202
}
150203
}
204+
#endif
151205
}
152206

153207
FLASH->CTLR &= CR_OPTPG_Reset; // Disable programming mode
@@ -167,3 +221,4 @@ bool EEPROMClass::end() {
167221
#if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_EEPROM)
168222
EEPROMClass EEPROM;
169223
#endif
224+

0 commit comments

Comments
 (0)