CH32V003libi2c Ver 5.4
July 19, 2025 · View on GitHub
Library Description
lib_i2c is a fully featured, but lightweight library for the I2C peripheral
on the CH32V003 with the following features:
- Support for All 3 Alternative Pinouts
- Support 7-bit Addresses (7-bit aligned, eg
0bx1101000 - 0x68) - Support 8-bit Registers
- Up to 1MHz Bus Frequency has been tested. Can be set higher.
- Easy to use I2C Error Status'
- Funcion to Scan the Interface for devices
- Master Mode Only
Based on CNLohr's ch32fun
Changelog
v5.4- Added aread_reg_delay()function for when clock-stretching is inadequatev5.3- Added an easier user-definable clock-stretch timout variablev5.2- Fixed a define bug in the header - Testing complete of multi-byte registersv5.1- Refactored a few things to be more efficient, added support for 10Bit Addressesv5.0- Added Multi-Byte Register support (up to 4 bytes) with config in i2c_device_t
Changed init() to use the device pointer, multiple devices can still be used at once
Added read & write raw functions - without use of registersv4.3- Began refactor to allow 7bit, 10bit and 16bit addresses. Only supports 7bit currentlyv4.2- Protected most loops with timeout and error propegationv4.1- Added i2c_wait to simplify waiting for bus to be freev4.0- Refactored using inlined functions for expandability (i2c_start(), i2c_stop() etc)v3.5- Converted from ch32v003fun to ch32funv3.4- Fixed an off-by-one error in i2c_read
TODO
- Neaten library so there are less repeated chunks of code
- Test on other MCU Variants:
- CH32V003 ✔️
Thanks
Thank you niansa for continued help refining the library
Thank you Johannes JKT for fixing an i2c_read off_by_one bug
If you want to use ANY pins for I2C, see lib_swi2c
Copyright (c) 2024 - 2025 ADBeta