From f85ff6bf3eee41421aa287abf5a27f03593c809e Mon Sep 17 00:00:00 2001 From: Leon Krieg Date: Tue, 1 Oct 2024 03:12:17 +0200 Subject: [PATCH] Support FAN03 PWM with reduced frequency on m32a until m1284p port is finished --- src/bus/pwm.c | 19 ++++++++----------- src/bus/pwm.h | 2 +- src/main.c | 6 +++--- 3 files changed, 12 insertions(+), 15 deletions(-) diff --git a/src/bus/pwm.c b/src/bus/pwm.c index 58050ff..9c30197 100644 --- a/src/bus/pwm.c +++ b/src/bus/pwm.c @@ -93,15 +93,10 @@ int PWM_Init(void) OCR1B = FAN01_MIN_DUTY; // PD4 OCR1A = FAN02_MIN_DUTY; // PD5 - // TIMER2: Fast mode, non-inverting, top=0xFF, prescale /8 - // TOP set to 8000000 (f_cpu) / 8 (prescale) / 25000 (f_pwm) - 1 + // TIMER2: Fast mode, non-inverting, top=0xFF, prescale /1 - TCCR2 = BIT(WGM20) | BIT(WGM21) | BIT(COM21) | BIT(CS21); - OCR2 = 255; // XXX: OCR2A=TOP OCR2B=duty - - // TCCR2 = 0; // Normal operation - // OCR2 = FAN03_MIN_DUTY; // PD7 - // PORTD &= ~BIT(PD7); // Low + TCCR2 = BIT(WGM20) | BIT(WGM21) | BIT(COM21) | BIT(CS20); + OCR2 = FAN03_MIN_DUTY; return 0; } @@ -109,18 +104,20 @@ int PWM_Init(void) // Value in range 0-100 is expected void PWM_SetValue(int port, int value) { - int n; + int n, m; if (port != FAN01 && port != FAN02 && port != FAN03) return; // Invalid port - n = CLAMP(value, 100, 0) * (PWM_CYCLE_TOP / 100.0f); + // Workaround: Missing third 16-bit timer output + m = (port != FAN03) ? PWM_CYCLE_TOP : 0xFF; + n = CLAMP(value, 100, 0) * m / 100.0f; Info("Setting duty cycle for %s to %d/%d...", (port == FAN01) ? "FAN01" : (port == FAN02) ? "FAN02" : (port == FAN03) ? "FAN03" : - "UNKNOWN", n, PWM_CYCLE_TOP); + "UNKNOWN", n, m); switch (port) { case PD4: OCR1B = n; break; diff --git a/src/bus/pwm.h b/src/bus/pwm.h index 9a8f68f..e0deaf2 100644 --- a/src/bus/pwm.h +++ b/src/bus/pwm.h @@ -11,7 +11,7 @@ #define PWM_CYCLE_TOP (F_CPU / 25000 - 1) // 8 MHz / 25 KHz #define FAN01_MIN_DUTY (PWM_CYCLE_TOP * 0.2f) #define FAN02_MIN_DUTY (PWM_CYCLE_TOP * 0.2f) -#define FAN03_MIN_DUTY (PWM_CYCLE_TOP * 0.2f) +#define FAN03_MIN_DUTY (0xFF * 0.2f) int PWM_Init(void); void PWM_SetValue(int port, int value); diff --git a/src/main.c b/src/main.c index 18ffd34..481fd9d 100644 --- a/src/main.c +++ b/src/main.c @@ -118,9 +118,9 @@ static int Init(void) // complicated so it might be worth it to switch to // something like an ATmega328PB. - PWM_SetValue(FAN01, 50); // Fan Peltier Hot side - PWM_SetValue(FAN02, 50); // Fan Peltier Cold Side - // PWM_SetValue(FAN03, 20); // Fan Heating + PWM_SetValue(FAN01, 50); // Fan Peltier Hot side + PWM_SetValue(FAN02, 50); // Fan Peltier Cold Side + PWM_SetValue(FAN03, 50); // Fan Heating // The I2C_SetChannel command changes the channel // setting of the PCA9546 I2C multiplexer. Any