Skip to content

Board definition for CH32X033F8P6 TSSOP20 #171

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

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions boards.txt
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,23 @@ CH32X035_EVT.menu.pnum.CH32X035G8U.build.IQ_math_RV32=
CH32X035_EVT.menu.pnum.CH32X035G8U.build.ch_extra_lib=-lprintf


#CH32X033F8P EVT Board
CH32X035_EVT.menu.pnum.CH32X033F8P=CH32X033F8P EVT
CH32X035_EVT.menu.pnum.CH32X033F8P.node=NODE_X033F8P
CH32X035_EVT.menu.pnum.CH32X033F8P.upload.maximum_size=63488
CH32X035_EVT.menu.pnum.CH32X033F8P.upload.maximum_data_size=20480
CH32X035_EVT.menu.pnum.CH32X033F8P.build.mcu=QingKe-V4C
CH32X035_EVT.menu.pnum.CH32X033F8P.build.board=CH32X033F8P
CH32X035_EVT.menu.pnum.CH32X033F8P.build.series=CH32X035
CH32X035_EVT.menu.pnum.CH32X033F8P.build.variant=CH32X035/CH32X033F8P
CH32X035_EVT.menu.pnum.CH32X033F8P.build.chip=CH32X033F8P
CH32X035_EVT.menu.pnum.CH32X033F8P.build.march=rv32imacxw
CH32X035_EVT.menu.pnum.CH32X033F8P.build.mabi=ilp32
CH32X035_EVT.menu.pnum.CH32X033F8P.build.math_lib_gcc=-lm
CH32X035_EVT.menu.pnum.CH32X033F8P.build.IQ_math_RV32=
CH32X035_EVT.menu.pnum.CH32X033F8P.build.ch_extra_lib=-lprintf


# Upload menu
CH32X035_EVT.menu.upload_method.swdMethod=WCH-SWD
CH32X035_EVT.menu.upload_method.swdMethod.upload.protocol=
Expand Down
19 changes: 16 additions & 3 deletions libraries/EEPROM/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ The EEPROM library provides an easy to use interface to interact with the intern
This CH32 version of the library provides a familiar API to emulated EEPROM using the Option bytes area in flash memory.

Ported to CH32 by Maxint R&D, based on multiple sources:
- Code from the Option Data example of CH32V003fun by @CNLOHR.
- Code from the Option Data example of CH32V003fun by @CNLOHR and various WCH EVT Flash examples.
- Arduino original copyright (c) 2006 David A. Mellis. All right reserved. New version by Christopher Andrews 2015.
- ESP8266 version copyright (c) 2014 Ivan Grokhotkov. All rights reserved.

## Table of contents
- [CH32V003 emulated EEPROM](#ch32v003-emulated-eeprom)
- [CH32 boards with larger emulated EEPROM](#ch32-boards-with-larger-emulated-eeprom)
- [How to use this library](#how-to-use-this-library)
- [Library functions](#library-functions)
- [Features & limitations](#features--limitations)
Expand Down Expand Up @@ -41,6 +42,17 @@ Layout for uint8_t _data[26]: { ob[4], ob[6], ob[16...62] ].

The first release of this library was made for the CH32V003 and only uses the user select word storage area.
It was tested using Arduino IDE 2.3.2 and OpenWCH core 1.0.4.

### CH32 boards with larger emulated EEPROM
Depending on the board, the size of the user-defined information storage area can be different, allowing for a larger emulated EEPROM.
For all CH32 chips the starting address is 0x1FFFF800. The first 16 bytes is for configuration, including 4 bytes for data0/data1.
The remainder can be used for EEPROM values and their inverse. The method to write these bytes can be different per chip.
- 64B => 2+(64-16)/2=2+24=26B: CH32V003
- 128B => 2+(128-16)/2=2+56=58B: CH32V103, CH32V20x, CH32V30x/31x
- 256B => 2+(256-16)/2=2+120=122B: CH32V002/V004/V006/V007, CH32X033/X035

Currently tested to work: CH32V003/CH32V002/CH32V006/CH32X033.
(CH32VM00X required updated ch32v00x_flash.c from latest EVT code)
Future releases of this library may support other CH32 processors and allow for larger memory sizes.

### **How to use this library**
Expand Down Expand Up @@ -150,8 +162,9 @@ The method returns a `uint32_t` value, containing the data0 and data1 bytes and
---

## Features & limitations
- The first release of this library was made only for the CH32V003 and has been tested on that MCU only. Other members of the CH32 may behave incorrectly or not work at all.
- This EEPROM implementation for the CH32V003 has only 26 bytes available. When addressing more, things are likely to go wrong. A future release may allow using more pages from the flash memory.
- The first release of this library was made for the CH32V003, offering 26 bytes of emulated EEPROM. It was tested on CH32V003 and CH32X033.
. The next release offered 122 bytes of emulated EEPROM on CH32X035/X033 and CH32VM00X (V002/V004/V006/V008). It passed tests on V002, V006 and X033.
- Other members of the CH32 may behave incorrectly or not work at all. A test on V103 showed issues writing more than the first two bytes.
- Most CH32 EEPROM methods are the same as their equivalent on regular Arduino's. BEWARE: The begin() and end() methods are like their counterparts for ESP8266/ESP32, but are very different from the begin() and end() methods of EEPROM v2.0 by Christopher Andrews, who introduced them to support C++ iterators. This library follows the begin() convention introduced by the Serial and Wire classes, i.e. to initialize the object.

## Disclaimer
Expand Down
2 changes: 1 addition & 1 deletion libraries/EEPROM/library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=EEPROM
version=1.0.0
version=1.0.2507
author=Maxint
maintainer=https://github.com/maxint-rd
sentence=Enables reading and writing to non-volatile storage in the processor.
Expand Down
58 changes: 56 additions & 2 deletions libraries/EEPROM/src/EEPROM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,44 @@
These 8 bytes use 16 bytes of flash. The total storage area page is 64 bytes, leaving 48 bytes available,
(including required inverse values). This is 24 bytes that can be used plus the two data0 and data1 bytes.
Layout for uint8_t _data[26]: { ob[4], ob[6], ob[16...62] ].


From the CH32X035 Data Sheet:
"Built-in 3328 bytes system storage area (System FLASH), i.e. BOOT area, for system boot program storage, builtin
bootstrap loading program. 256-byte system non-volatile configuration information storage area, used for vendor configuration word storage,
factory-cured, user cannot be modified. 256-byte user-defined information storage area for user-selected word storage."
So next to 62kB of Code FLASH, the chip features 3kB Flash for boot, configuration and storage. That 256-byte
user-selected word storage area is the area we can use to emulate on chip EEPROM memory.

The size of the user-defined information storage area can be different, allowing for a larger emulated EEPROM.
For all CH32 chips the starting address is 0x1FFFF800. The first 16 bytes is for configuration + 4-bytes for data0/data1.
The remainder can be used for EEPROM values and their inverse. The method to write these bytes can be different per chip.
64B => 2+(64-16)/2=2+24=26B: CH32V003
128B => 2+(128-16)/2=2+56=58B: CH32V103, CH32V20x, CH32V30x/31x
256B => 2+(256-16)/2=2+120=122B: CH32V002/V004/V006/V007, CH32X033/X035
Currently tested to work: CH32V003/CH32V002/CH32V006/CH32X033
(CH32VM00X required updated ch32v00x_flash.c from latest EVT code)

TODO: Allow larger EEPROM for CH32V10x/V20x/V30x/V31x. Note: User option bytes storage area is 128B

Note: for some board the current Arduino core may have incorrect latency set
V00X RM page 242 suggests this:
FLASH_ACTLR [1:0] LATENCY[1:0] RW, reset value is 0 => 0 wait state
FLASH wait status number.
00: 0 wait (Recommended 0<=SYSCLK<=15MHz);
01: 1 wait (Recommended 15<SYSCLK<=24MHz);
10: 2 wait (Recommended 24<SYSCLK<=48MHz);
11: Reserved.
Same for V003 RM page 171; same for X035 RM page 237; different for V103
Latency is set by subfunction of SetSysClock(). E.g. SetSysClockTo_48MHz_HSI() should set FLASH_Latency_2.
For some variants it sets to FLASH_Latency_1. For higher clocks latency may need to be even higher.
Note: V20x/V30x have no ACTLR register???
//FLASH_SetLatency(FLASH_Latency_2); // TODO: do only where needed

*/
#include <EEPROM.h>

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

EEPROMClass::EEPROMClass(void) {
}
Expand All @@ -44,7 +78,11 @@ uint32_t EEPROMClass::ReadOptionBytes()

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

// Allocate data buffer and copy the current content from storage
if(!_data)
Expand Down Expand Up @@ -141,15 +179,31 @@ bool EEPROMClass::commit()
// Note that when a byte is written as a word, it automatically gets its inverse value as second byte
if(_data && _size)
{
#if defined(CH32VM00X) || defined(CH32X035)
// To write to the larger 256B option byte area of V00X, we use fast page programming as shown by EVT EXAM code
// (see FLASH_OptionBytePR() in /EVT/EXAM/SRC/Peripheral/src/ch32v00X_flash.c)
// Tested OK for V002/V006. X035 seems similar, but first needs testing
FLASH_Unlock_Fast();
FLASH_BufReset();
for (int i=2;i<_size; i++) {
uint32_t val;
val=_data[i+1]<<16 | _data[i];
FLASH_BufLoad((OB_BASE + (OB_AVAIL_DATA_START + (i-2))*2), val);
i++;
}
FLASH_ProgramPage_Fast(OB_BASE);
FLASH_Lock_Fast();
#else
uint16_t *ob16p=(uint16_t *)OB_BASE;
for (int i=2;i<_size; i++) {
if(_data[i]!=0xFF) {
ob16p[OB_AVAIL_DATA_START+(i-2)]=_data[i];
while (FLASH->STATR & FLASH_BUSY); // Wait for flash operation to be done
}
}
#endif
}

FLASH->CTLR &= CR_OPTPG_Reset; // Disable programming mode
FLASH->CTLR|=CR_LOCK_Set; // Lock flash memories again
return(true);
Expand Down
30 changes: 30 additions & 0 deletions variants/CH32X035/CH32X033F8P/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# v3.21 implemented semantic changes regarding $<TARGET_OBJECTS:...>
# See https://cmake.org/cmake/help/v3.21/command/target_link_libraries.html#linking-object-libraries-via-target-objects
cmake_minimum_required(VERSION 3.21)

add_library(variant INTERFACE)
add_library(variant_usage INTERFACE)

target_include_directories(variant_usage INTERFACE
.
)


target_link_libraries(variant_usage INTERFACE
base_config
)

target_link_libraries(variant INTERFACE variant_usage)



add_library(variant_bin STATIC EXCLUDE_FROM_ALL
PeripheralPins.c
variant_CH32X033F8P.cpp
)
target_link_libraries(variant_bin PUBLIC variant_usage)

target_link_libraries(variant INTERFACE
variant_bin
)

Loading