PlatformIO + CH32V003 + SSD1306 (I2C OLED display 128x64) + two VL53L0X (I2C ToF) + servo + DC motor
Never-stopping car.
- This README.md on gitflic
- License: CC0 1.0 Universal (отказ от своих авторских прав и передача произведения в общественное достояние)
// MCU:
// ========================
// MOTORF-[1 (PD6) (PD4,PD5) 8]- MOTORB, and to programmer SWIO
// GND -[2 (VSS) MCU (PC4) 7]- to SERVO
// XSHUT -[3 (PA2) SOP8 (PC2) 6]- to I2C SCL
// V3.3 -[4 (VDD) (PC1) 5]- to I2C SDA
// ========================
// where
// XSHUT: to XSHUT pin of TOF1 (forward) to control its disabling, while changing standard address of TOF1 (backward)
// SERVO: PWM (using TIM1 timer)
// MOTORF(Forward), MOTORB(Backward): to H-bridge forward/backward input (H-bridge forward/backward output -- to DC motor)
Repo: https://gitflic.ru/project/ikarus512/platformio-ch32v003-oled-vl53l0x
Based on PlatformIO (VS Code)/CH32V platform 1.1.0/blinky-none-os example (same examples as in CH32V003EVT.zip from https://www.wch.cn)/framework = none-os, board = genericCH32V003J4M6.
MCU: CH32V003 (CH32V003J4M6 SOP8) QingKe 32-bit RISC-V
Programmer: WCH-LinkE that should be switched to RV mode by holding ModeS button during powering on (Way3). I did not need to reprogram it (MounRiverStudio not needed at all). Desire in future: to use Arduino board with sketch as programmer (similar to ArduinoISP sketch for programming AVR MCU like ATtiny85).
Using I2C HAL and SSD1306 (OLED display 128x64) from https://habr.com/ru/articles/775724 (https://github.com/Sergo101/CH32V003_stHAL and some parts taken from STM32 https://github.com/stm32f1/vl53l0x_stm32).
Using two I2C VL53L0X ToF sensors, based on https://github.com/pololu/vl53l0x-arduino (rewritten from C++ to C). Second ToF sensor controlled using XSHUT pin, so it cane be send to standby, while first ToF sensor address can be changed (new address is forgotten on power off).
Servo: PWM example from https://github.com/JimMerkle/CH32V003F4P6_PWM/blob/master/User/main.c
Other components
- DC-DC boost converter 1.5 to 3.3V, max power ~300mW?, hence max current 100mA?)
- H-bridge (DIP8) Can be controlled by single input pin (non-stop mode, only backward or forward)
- H-bridge (SOP8)
Additional tools
build and upload command:
pio run -e genericCH32V003J4M6 -t upload
WCH ISP (working?):
$HOME/.platformio/packages/tool-wchisp/wchisp info
Disassembler:
./tools/disasm.sh
Recovery tool in case of chip SWIO pin is blocked (programmed for output) If you accidentally set SWIO pin for output, and cannot re-programn CH32V003 MCU anymore, use ch32v003_unbricker.py taken from https://gitverse.ru/zhevak/unbricker/content/master/ch32v003_unbricker.py
./tools/ch32v003_unbricker.py
If needed, install python and needed packages:
sudo apt install -y python3 pip3; pip3 install pyusb pyserial hid -U
TODO notes
Conflicting delay functions
- Delay_Ms(ms) is not working because of conflicts between:
- Delay_Init() + Delay_Ms() (from .platformio/packages/framework-wch-noneos-sdk/Debug/ch32v00x/debug.c)
- HAL_Systick_Init() + HAL_delay_1ms() (from systick_hal.c)
Memory space optimization
TODO: investigate if anything can be done.
- Fonts: use only one small font (probably cape letters can be eliminated), ...
- Optimization of space by converting some functions to macros, or making functions inline, did not helped (seems already inlined, check disassembling).
- PROGMEM no big sense, because on CH32V003, 16K Flash. https://wellys.com/posts/avr_c_progmem like on Arduino: PROGMEM attribute, F("strings"), ...
- GPIO programing structures are not needed (check if not already optimized by compiler)
How to build PlatformIO based project
=====================================
- Install PlatformIO Core
- Download development platform with examples
- Extract ZIP archive
- Run these commands:
# Change directory to example
$ cd platform-ch32v/examples/blinky-none-os
# Build project
$ pio run
# Upload firmware
$ pio run --target upload
# Upload firmware for the specific environment
$ pio run -e genericCH32V003J4M6 --target upload
# Clean build files
$ pio run --target clean
$ pio run --list-targets
Environment Group Name Title Description
genericCH32V003J4M6 Platform check_flash_protection Check Flash Protection
genericCH32V003J4M6 Platform disable_flash_protection Disable Flash Protection
genericCH32V003J4M6 Platform disable_sdi_printf Disable SDI Printf
genericCH32V003J4M6 Platform enable_flash_protection Enable Flash Protection
genericCH32V003J4M6 Platform erase Erase Flash
genericCH32V003J4M6 Platform sdi_printf Enable SDI Printf
genericCH32V003J4M6 Platform size Program Size Calculate program size
genericCH32V003J4M6 Platform upload Upload
pio run -e genericCH32V003J4M6 --target upload