Update module 'pwm' to run on atmega1284p hardware

This commit is contained in:
2024-09-30 22:04:53 +02:00
parent c3dc289d5f
commit ac7cd8a96b

View File

@@ -1,23 +1,15 @@
#include "common.h" #include "common.h"
#include "bus/pwm.h" #include "bus/pwm.h"
// TODO: Add documentation for timer3: TCCR3A, TCCR3B, etc.
int PWM_Init(void) int PWM_Init(void)
{ {
// PD4: PWM NF-12 Fan Peltier Hot Side // PD4: PWM NF-12 Fan Peltier Hot Side
// PD5: PWM NF-A8 Fan Peltier Cold Side // PD5: PWM NF-A8 Fan Peltier Cold Side
// PD7: PWM NF-R8 Fan Heating Element // PD7: PWM NF-R8 Fan Heating Element
// ATMega32A does not have more than two outputs for the
// 16-bit timer and the other 8-bit timers don't have modes
// where the value of TOP can be changed. We can only get
// 25 KHz with software PWM on this chip as far as I know.
// The 328P would allow us to use OCR2A as top but with
// 8-bit this gives us a really low duty step size of 2.5%.
// Ideal would be two 16-bit timers with two outputs each.
DDRD |= BIT(PD4) | BIT(PD5) | BIT(PD7); DDRD |= BIT(PD4) | BIT(PD5) | BIT(PD7);
// PORTD &= ~BIT(PD7); // Turn off PD7
// TCCR1A Timer1 Control Register A // TCCR1A Timer1 Control Register A
// 7 6 5 4 3 2 1 0 // 7 6 5 4 3 2 1 0
@@ -90,18 +82,13 @@ int PWM_Init(void)
TCCR1B = BIT(WGM12) | BIT(WGM13) | BIT(CS10); TCCR1B = BIT(WGM12) | BIT(WGM13) | BIT(CS10);
ICR1 = PWM_CYCLE_TOP; // 8000 MHz / 25000 KHz ICR1 = PWM_CYCLE_TOP; // 8000 MHz / 25000 KHz
TCCR3A = BIT(WGM31) | BIT(COM3A1) | BIT(COM3B1);
TCCR3B = BIT(WGM32) | BIT(WGM33) | BIT(CS30);
ICR3 = PWM_CYCLE_TOP; // 8000 MHz / 25000 KHz
OCR1B = FAN01_MIN_DUTY; // PD4 OCR1B = FAN01_MIN_DUTY; // PD4
OCR1A = FAN02_MIN_DUTY; // PD5 OCR1A = FAN02_MIN_DUTY; // PD5
OCR2A = FAN03_MIN_DUTY; // PD7
// TIMER2: Fast mode, non-inverting, top=0xFF, prescale /8
// TOP set to 8000000 (f_cpu) / 8 (prescale) / 25000 (f_pwm) - 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
return 0; return 0;
} }
@@ -125,6 +112,6 @@ void PWM_SetValue(int port, int value)
switch (port) { switch (port) {
case PD4: OCR1B = n; break; case PD4: OCR1B = n; break;
case PD5: OCR1A = n; break; case PD5: OCR1A = n; break;
case PD7: OCR2 = n; break; case PD7: OCR2A = n; break;
} }
} }