11#include <stdint.h>
2-
2+ #include <math.h>
33#include <hardware/i2c.h>
44
55#include "config.h"
@@ -27,6 +27,27 @@ bool fan_ctl_i2c_write(uint8_t reg_id, uint8_t val) {
2727 return pwr_brd_i2c_write_reg (PWR_BRD_FAN_CTL_ADDR , reg_id , & val , 1 );
2828}
2929
30+ bool fan_ctl_set_pwm_frequency (uint8_t fan1 , uint8_t fan2 , uint8_t fan3 , uint8_t fan4 , uint8_t fan5 ) {
31+ uint8_t f123 = fan1 | (fan2 <<2 ) | (fan3 <<4 );
32+ bool ret = fan_ctl_i2c_write (0x2D , f123 );
33+ if (!ret ) {
34+ return ret ;
35+ }
36+
37+ uint8_t f45 = fan4 | (fan5 <<2 );
38+ return fan_ctl_i2c_write (0x2C , f45 );
39+ }
40+
41+ void pwr_brd_fan_init () {
42+ uint64_t now = time_us_64 ();
43+
44+ fan_ctl_set_pwm_frequency (0 , 0 , 0 , 0 , 0 );
45+ for (int i = 0 ; i < NUMFAN ; i ++ ) {
46+ time_next_cmd [i ] = now ;
47+ desired_fan_speed [i ] = DESIRED_RPM ;
48+ }
49+ }
50+
3051bool fan_ctl_i2c_write_and_check (uint8_t reg_id , uint8_t val ) {
3152 if (!pwr_brd_i2c_write_reg (PWR_BRD_FAN_CTL_ADDR , reg_id , & val , 1 )) {
3253 return false;
@@ -160,18 +181,12 @@ void fan_ctl_task() {
160181 continue ;
161182 }
162183
163- // in all cases below, fan should be on
164-
165- if (
166- fanspeed > desired_fan_speed [i ] - DESIRED_RPM_THRESH_LOWER &&
167- fanspeed < desired_fan_speed [i ] + DESIRED_RPM_THRESH_UPPER
168- ) {
169- // fan is within threshold, do nothing
170- continue ;
171- }
172-
184+ int difference = desired_fan_speed [i ] - fanspeed ;
173185 uint8_t pwm ;
174186 fan_ctl_get_pwm (i , & pwm );
187+ float smooth = 0.0005f ;
188+ pwm += ((difference >>2 ) * (1.0f - exp (- smooth )));
189+ fan_ctl_set_pwm (i , pwm );
175190
176191 if (pwm > FAN_MAX_PWM ) {
177192 // PWM was set by the fan controller's default power on value
@@ -195,39 +210,5 @@ void fan_ctl_task() {
195210 continue ;
196211 }
197212
198- if (fanspeed > desired_fan_speed [i ] * 2 ) {
199- // fan is way too fast, dividing PWM in half
200- fan_ctl_set_pwm (i , pwm >> 1 );
201- continue ;
202- }
203-
204- if (fanspeed > desired_fan_speed [i ] + DESIRED_RPM_THRESH_UPPER ) {
205- // fan is a bit too fast
206-
207- if (pwm < 1 ) {
208- // fan is already at minimum PWN, nothing to do
209- continue ;
210- }
211-
212- // decrease PWM by 1
213- fan_ctl_set_pwm (i , pwm - 1 );
214-
215- // give it more time to spin down
216- time_next_cmd [i ] = now + 2 * CMD_WAIT_TIME_US ;
217- continue ;
218- }
219-
220- if (fanspeed < desired_fan_speed [i ] - DESIRED_RPM_THRESH_LOWER ) {
221- // fan is a bit too slow
222-
223- if (pwm >= FAN_MAX_PWM ) {
224- // fan is already at max PWM, nothing to do
225- continue ;
226- }
227-
228- // increase PWM by 1
229- fan_ctl_set_pwm (i , pwm + 1 );
230- continue ;
231- }
232213 }
233214}
0 commit comments