# 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. ## 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 ``` You will also need to clone the [`ch32fun repo`](https://github.com/cnlohr/ch32fun) somewhere and modify the `Makefile`. You will also need to compile `minichlink`. Included in the `ch32fun` repo. ### ???.. Or just use PlatformIO idc. It has debugging. ## Register index Registers are 8 bit each, but they hold 16-bit values. Bytes in even registers are high, bytes in odd registers are low. | Register address | Function | R/W | |------------------|-----------------------------------------------------------|-----| | 0x00, 0x01 | whoami | 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 | Bit | Description | |--------------|----------------------------------------------------------------------------------------------------------------------------------------------------------| | 0 (0000000x) | Steady state flag. **0** - fan didn't (yet) reach the target speed. **1** - fan reached the requested target speed or fan running in constant duty mode. | | 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** - Target RPM mode, the controller will target RPM set in register 0x04. **This is the initial mode.** | | 1 (000000x0) | Fan type: **0** - Fan controlled by PWM pin (4 pin plug), **this is the initial mode**. **1** - Fan controlled by DC voltage (3 pin plug) | | 2 (00000x00) | Fan power disable: **0** - Fan operates normally. **1** - 12V supply to the fan is cut. | # TODO: - fan modes, states - pwm/dc mode