|
| 1 | +/* |
| 2 | + * PWM Fan Controller: |
| 3 | + * PWM fan controller is developed using WiringPi C library for RPi4. It is |
| 4 | + * intended for "Noctua NF-A4x10 5V PWM" fan, it may work for any PWM fan |
| 5 | + * with slight adjustment according to the fan specs. |
| 6 | + * Copyright (c) 2021 - ar51an |
| 7 | + */ |
| 8 | + |
| 9 | +#include <stdio.h> |
| 10 | +#include <time.h> |
| 11 | +#include <signal.h> |
| 12 | +#include <wiringPi.h> |
| 13 | +#include <systemd/sd-journal.h> |
| 14 | + |
| 15 | +int PWM_PIN = 18; // HW PWM works at GPIO [12, 13, 18 & 19] on RPi4B |
| 16 | +int TACHO_PIN = 23; |
| 17 | +int RPM_MAX = 5000; // Noctua Specs: Max=5000 |
| 18 | +int RPM_MIN = 1500; // Noctua Specs: Min=1000 [Kept 1500 as Min] |
| 19 | +int RPM_OFF = 0; |
| 20 | +int TEMP_MAX = 55; // Above this temperature [FAN=ON At Max speed] |
| 21 | +int TEMP_LOW = 40; // Below this temperature [FAN=OFF] |
| 22 | +int WAIT = 5000; // MilliSecs before adjusting RPM |
| 23 | +int TACHO_ENABLED = 0; // TACHO Specific [Enable Tacho: 0=Disable 1=Enable] |
| 24 | +const int PULSE = 2; // TACHO Specific [Noctua fan puts out 2 pulses per revolution] |
| 25 | +volatile int intCount = 0; // TACHO Specific [Interrupt Counter] |
| 26 | +int getRpmStartTime = 0; // TACHO Specific |
| 27 | +int origPwmPinMode = -1; |
| 28 | +int origTachoPinMode = -1; |
| 29 | +float tempLimitDiffPct = 0.0f; |
| 30 | +char thermalFilename[] = "/sys/class/thermal/thermal_zone0/temp"; |
| 31 | +static volatile sig_atomic_t keepRunning = 1; |
| 32 | + |
| 33 | +void logConfParams () { |
| 34 | + char logFormat[] = "Config values loaded: PWM_PIN=%d | TACHO_PIN=%d | RPM_MAX=%d | RPM_MIN=%d | RPM_OFF=%d " |
| 35 | + "| TEMP_MAX=%d | TEMP_LOW=%d | WAIT=%d | TACHO_ENABLED=%d | THERMAL_FILE=%s"; |
| 36 | + sd_journal_print(LOG_INFO, logFormat, PWM_PIN, TACHO_PIN, RPM_MAX, RPM_MIN, RPM_OFF, \ |
| 37 | + TEMP_MAX, TEMP_LOW, WAIT, TACHO_ENABLED, thermalFilename); |
| 38 | +} |
| 39 | + |
| 40 | +void initFanControl () { |
| 41 | + /* Assign global vars with config file (if provided) values */ |
| 42 | + FILE *confFile; |
| 43 | + char confFilename[] = "/opt/gpio/fan/params.conf"; |
| 44 | + confFile = fopen(confFilename, "r"); |
| 45 | + if (confFile != NULL) { |
| 46 | + char confFormat[] = "PWM_PIN=%d TACHO_PIN=%d RPM_MAX=%d RPM_MIN=%d RPM_OFF=%d TEMP_MAX=%d " |
| 47 | + "TEMP_LOW=%d WAIT=%d TACHO_ENABLED=%d THERMAL_FILE=%s"; |
| 48 | + fscanf(confFile, confFormat, &PWM_PIN, &TACHO_PIN, &RPM_MAX, &RPM_MIN, &RPM_OFF, \ |
| 49 | + &TEMP_MAX, &TEMP_LOW, &WAIT, &TACHO_ENABLED, thermalFilename); |
| 50 | + logConfParams(); |
| 51 | + fclose(confFile); |
| 52 | + } |
| 53 | + else |
| 54 | + sd_journal_print(LOG_WARNING, "params.conf not found - Default values loaded"); |
| 55 | + /* Calculate values of global vars */ |
| 56 | + tempLimitDiffPct = (float) (TEMP_MAX-TEMP_LOW)/100; |
| 57 | + if (TACHO_ENABLED && TACHO_ENABLED != 1) TACHO_ENABLED = 0; |
| 58 | +} |
| 59 | + |
| 60 | +void initWiringPi () { |
| 61 | + /* Initialize wiringPi, calling 1 of 4 setup methods */ |
| 62 | + wiringPiSetupGpio(); // Defaults to GPIO/BCM pin numbers |
| 63 | +} |
| 64 | + |
| 65 | +int getPinMode (int pin) { |
| 66 | + /* Mode Name Mapping: INPUT=0, OUTPUT=1, ALT0=4, ALT1=5, ALT2=6, ALT3=7, ALT4=3, ALT5=2 */ |
| 67 | + return getAlt(pin); |
| 68 | +} |
| 69 | + |
| 70 | +void setFanSpeed (int pin, int speed) { |
| 71 | + pwmWrite(pin, speed); |
| 72 | +} |
| 73 | + |
| 74 | +int getCurrTemp () { |
| 75 | + int currTemp = 0; |
| 76 | + FILE *thermalFile; |
| 77 | + thermalFile = fopen(thermalFilename, "r"); |
| 78 | + fscanf(thermalFile, "%d", &currTemp); |
| 79 | + fclose(thermalFile); |
| 80 | + currTemp = ((float) currTemp/1000)+0.5; |
| 81 | + return currTemp; |
| 82 | +} |
| 83 | + |
| 84 | +void setupPwm () { |
| 85 | + /* Set pwm fan freq=25kHz (Noctua whitepaper stated as Intel's recommendation for PWM FANs) |
| 86 | + * PWM crystal oscillator clock base frequency: RPI3=19.2MHz & RPI4=54MHz. pwmSetClock() |
| 87 | + * takes a divisor of base fequency: "19200000/768=25kHz". It adjusts the divisor for RPI4 |
| 88 | + * 54MHz in the code itself as "divisor=540*divisor/192" |
| 89 | + */ |
| 90 | + int pwmClock = 768; |
| 91 | + origPwmPinMode = getPinMode(PWM_PIN); |
| 92 | + pinMode(PWM_PIN, PWM_OUTPUT); |
| 93 | + // Using default balanced mode instead of mark:space mode |
| 94 | + //pwmSetMode(PWM_MODE_MS); |
| 95 | + pwmSetClock(pwmClock); |
| 96 | + pwmSetRange(RPM_MAX); // Set PWM range to Max RPM |
| 97 | + setFanSpeed(PWM_PIN, RPM_OFF); // Set Fan speed to 0 initially |
| 98 | + //printf("[PWM] GPIO:Mode | %d:%d\n", PWM_PIN, origPwmPinMode); |
| 99 | + return; |
| 100 | +} |
| 101 | + |
| 102 | +void interruptHandler () { |
| 103 | + /* Number of interrupts generated between call to getFanRpm */ |
| 104 | + intCount++; |
| 105 | + return; |
| 106 | +} |
| 107 | + |
| 108 | +void setupTacho () { |
| 109 | + origTachoPinMode = getPinMode(TACHO_PIN); |
| 110 | + pinMode(TACHO_PIN, INPUT); |
| 111 | + pullUpDnControl(TACHO_PIN, PUD_UP); |
| 112 | + getRpmStartTime = time(NULL); |
| 113 | + wiringPiISR(TACHO_PIN, INT_EDGE_FALLING, interruptHandler); |
| 114 | + return; |
| 115 | +} |
| 116 | + |
| 117 | +void getFanRpm () { |
| 118 | + int duration = 0; |
| 119 | + float frequency = 0.0f; |
| 120 | + int rpm = RPM_OFF; |
| 121 | + duration = (time(NULL)-getRpmStartTime); |
| 122 | + frequency = (intCount/duration); |
| 123 | + rpm = (int) (frequency*60)/PULSE; |
| 124 | + getRpmStartTime = time(NULL); |
| 125 | + intCount = 0; |
| 126 | + //if (rpm) printf("\e[30;38;5;67m[TACHO] Frequency: %.1fHz | RPM: %d\n\e[0m", frequency, rpm); |
| 127 | + if (rpm) sd_journal_print(LOG_DEBUG, "[TACHO] Frequency: %.1fHz | RPM: %d", frequency, rpm); |
| 128 | + return; |
| 129 | +} |
| 130 | + |
| 131 | +void setFanRpm () { |
| 132 | + int rpm = RPM_OFF; |
| 133 | + static int lastRpm = 0; |
| 134 | + float currTempDiffPct = 0.0f; |
| 135 | + int currTemp = getCurrTemp(); |
| 136 | + int tempDiff = (currTemp-TEMP_LOW); |
| 137 | + if (tempDiff > 0) { |
| 138 | + currTempDiffPct = (tempDiff/tempLimitDiffPct); |
| 139 | + rpm = (int) (currTempDiffPct*RPM_MAX)/100; |
| 140 | + rpm = rpm < RPM_MIN ? RPM_MIN : rpm > RPM_MAX ? RPM_MAX : rpm; |
| 141 | + //printf("\e[30;38;5;139m[PWM] Temp: %d | TempDiff: %.1f%% | RPM: %d\n\e[0m", currTemp, currTempDiffPct, rpm); |
| 142 | + sd_journal_print(LOG_DEBUG, "[PWM] Temp: %d | TempDiff: %.1f%% | RPM: %d", currTemp, currTempDiffPct, rpm); |
| 143 | + } |
| 144 | + if (lastRpm != rpm) setFanSpeed(PWM_PIN, rpm); |
| 145 | + lastRpm = rpm; |
| 146 | + return; |
| 147 | +} |
| 148 | + |
| 149 | +void start () { |
| 150 | + if (TACHO_ENABLED) { |
| 151 | + setupTacho(); |
| 152 | + while (keepRunning) { setFanRpm(); getFanRpm(); delay(WAIT); } |
| 153 | + } |
| 154 | + else { |
| 155 | + while (keepRunning) { setFanRpm(); delay(WAIT); } |
| 156 | + } |
| 157 | + return; |
| 158 | +} |
| 159 | + |
| 160 | +static void signalHandler (int _) { |
| 161 | + // Exit controller on Ctrl+C |
| 162 | + (void)_; |
| 163 | + keepRunning = 0; |
| 164 | + //printf("\r"); |
| 165 | +} |
| 166 | + |
| 167 | +void cleanup () { |
| 168 | + // PWM pin cleanup |
| 169 | + setFanSpeed(PWM_PIN, RPM_OFF); |
| 170 | + pinMode(PWM_PIN, origPwmPinMode); |
| 171 | + //pullUpDnControl(PWM_PIN, PUD_DOWN); |
| 172 | + // TACHO pin cleanup |
| 173 | + if (TACHO_ENABLED) pinMode(TACHO_PIN, origTachoPinMode); |
| 174 | + //pullUpDnControl(TACHO_PIN, PUD_DOWN); |
| 175 | + sd_journal_print(LOG_INFO, "Cleaned up - Exiting ..."); |
| 176 | + return; |
| 177 | +} |
| 178 | + |
| 179 | +int main (void) |
| 180 | +{ |
| 181 | + struct sigaction act = {0}; |
| 182 | + act.sa_handler = signalHandler; |
| 183 | + sigaction(SIGINT, &act, NULL); |
| 184 | + initFanControl(); |
| 185 | + initWiringPi(); |
| 186 | + setupPwm(); |
| 187 | + sd_journal_print(LOG_INFO, "Initialized and running ..."); |
| 188 | + start(); |
| 189 | + cleanup(); |
| 190 | + return 0; |
| 191 | +} |
0 commit comments