# I2C Fan Controller v2 ~ because I found a cool new microcontroller ~ This one is based on WCH CH32V003 and it's (should be) much better than the shitty ATTiny one. The `i2c_driver.h` is copied wholesale from ch32fun i2c_slave examples folder. It literally just works. RPM readings are getting unstable at ~310-320 RPM but that's probably okay. ## How compile? There's a Makefile shithead We compile on Debian/ubuntu. Other than the usual `build-essential` stuff you need also ```bash sudo apt install gcc-riscv64-unknown-elf libnewlib-dev libudev-dev libusb-1.0-0-dev ``` There is a distrobox.ini provided. You will also need to clone the [`ch32fun repo`](https://github.com/cnlohr/ch32fun) somewhere and modify the `Makefile`. To flash you will also need to compile `minichlink`. Included in the `ch32fun` repo. ### ???.. Or just use PlatformIO idc. It has debugging. ## Pins For the TSSOP20 package, CH32V003FxPx | Pin # | Port | Function | |-------|------|----------| | 3 | PD5 | TX | | 4 | PD7 | NRST | | 11 | PC1 | SDA | | 12 | PC2 | SCL | | 16 | PC6 | POWER_EN | | 18 | PD1 | SWIO | | 19 | PD2 | PWM_OUT | | 20 | PD3 | TACH_IN | ## Register index Registers are 8 bit each, but most of them hold 16-bit values. Bytes in even registers are high, bytes in odd registers are low. | Register address | Function | R/W | |------------------|-----------------------------------------------------------|-----| | 0x00, 0x01 | whoami (always returns 0x69 0x2b) | RO | | 0x02, 0x03 | Current fan speed (RPM) | RO | | 0x04, 0x05 | Target fan speed (RPM). Initial default speed is 750 RPM. | R/W | | 0x06, 0x07 | Current duty cycle | R/W | | 0x08, 0x09 | Maximum duty cycle | RO | | 0x0a | State register | RO | | 0x0b | Mode register | R/W | ### State register State equal to 0 means all is good. | Bit | Description | |--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------| | 0 (0000000x) | Steady state flag. **0** - fan reached the requested target speed or fan running in constant duty mode. **1** - fan didn't (yet) reach the target speed. | | 1 (000000x0) | Fan stalled. **0** - fan operating normally. **1** - fan stalled. | ### Mode register | Bit | Description | |--------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | 0 (0000000x) | Mode of operation: **0** - Constant duty cycle mode, the controller will keep the fan at a duty cycle set in register 0x06 and not change it automatically. **1** - Constant RPM mode, the controller continuously adjust PWM duty until target RPM is reached. **This is the initial mode.** | | 1 (000000x0) | Fan power disable: **0** - Fan operates normally. **1** - 12V supply to the fan is cut. |