usbfanctrl/pwm.c

187 lines
5.7 KiB
C
Raw Normal View History

2021-02-06 21:07:31 +01:00
/***************************************************************************
2021-02-06 22:33:52 +01:00
* Copyright (C) 02/2021 by Olaf Rempel *
2021-02-06 21:07:31 +01:00
* razzor@kopf-tisch.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; version 2 of the License, *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
2021-02-06 22:33:52 +01:00
#include <avr/io.h>
2021-02-06 21:07:31 +01:00
#include "pwm.h"
/* *********************************************************************** */
2021-02-06 22:33:52 +01:00
#define PWM_MIN 0x0000
#define PWM_MAX 0xFFFF
2021-02-06 21:07:31 +01:00
#define PWM_TIM_INIT() { \
/* enable output for OC1A, OC1B, OC3A */ \
DDRB |= (1<<PORTB5) | (1<<PORTB6); \
DDRC |= (1<<PORTC6); \
/* FastModePWM, ICRn is TOP */ \
TCCR1A = (1<<WGM11); \
TCCR1B = (1<<WGM13) | (1<<WGM12); \
ICR1 = 0xFFFF; \
TCCR3A = (1<<WGM31); \
TCCR3B = (1<<WGM33) | (1<<WGM32); \
ICR3 = 0xFFFF; \
}
#define PWM_TIM1_ENABLE() { TCCR1B |= (1<<CS10); }
#define PWM_TIM1_DISABLE() { TCCR1B &= ~(1<<CS10); }
#define PWM_TIM1_RUNNING() (TCCR1B & ((1<<CS10) | (1<<CS11) | (1<<CS12)))
#define PWM_TIM1_CHECK() (TCCR1A & ((1<<COM1A1) | (1<<COM1B1)))
#define PWM_TIM3_ENABLE() { TCCR3B |= (1<<CS30); }
#define PWM_TIM3_DISABLE() { TCCR3B &= ~(1<<CS30); }
#define PWM_TIM3_RUNNING() (TCCR3B & ((1<<CS30) | (1<<CS31) | (1<<CS32)))
#define PWM_TIM3_CHECK() (TCCR3A & ((1<<COM3A1) | (1<<COM3B1)))
#define PWM_CH0_PORT PORTB
#define PWM_CH0_NUM 5
#define PWM_CH0_OFF() { PWM_CH0_PORT &= ~(1<<PWM_CH0_NUM); TCCR1A &= ~(1<<COM1A1); }
#define PWM_CH0_ON() { PWM_CH0_PORT |= (1<<PWM_CH0_NUM); TCCR1A &= ~(1<<COM1A1); }
#define PWM_CH0_PWM(x) { PWM_CH0_PORT &= ~(1<<PWM_CH0_NUM); TCCR1A |= (1<<COM1A1); OCR1A = x; }
2021-02-06 22:33:52 +01:00
#define PWM_CH1_PORT PORTC
2021-02-06 21:07:31 +01:00
#define PWM_CH1_NUM 6
2021-02-06 22:33:52 +01:00
#define PWM_CH1_OFF() { PWM_CH1_PORT &= ~(1<<PWM_CH1_NUM); TCCR3A &= ~(1<<COM3A1); }
#define PWM_CH1_ON() { PWM_CH1_PORT |= (1<<PWM_CH1_NUM); TCCR3A &= ~(1<<COM3A1); }
#define PWM_CH1_PWM(x) { PWM_CH1_PORT &= ~(1<<PWM_CH1_NUM); TCCR3A |= (1<<COM3A1); OCR3A = x; }
2021-02-06 21:07:31 +01:00
2021-02-06 22:33:52 +01:00
#define PWM_CH2_PORT PORTB
2021-02-06 21:07:31 +01:00
#define PWM_CH2_NUM 6
2021-02-06 22:33:52 +01:00
#define PWM_CH2_OFF() { PWM_CH2_PORT &= ~(1<<PWM_CH2_NUM); TCCR1A &= ~(1<<COM1B1); }
#define PWM_CH2_ON() { PWM_CH2_PORT |= (1<<PWM_CH2_NUM); TCCR1A &= ~(1<<COM1B1); }
#define PWM_CH2_PWM(x) { PWM_CH2_PORT &= ~(1<<PWM_CH2_NUM); TCCR1A |= (1<<COM1B1); OCR1B = x; }
2021-02-06 21:07:31 +01:00
/* *********************************************************************** */
2021-02-06 22:33:52 +01:00
static void pwm_update(uint8_t channel, uint16_t value)
2021-02-06 21:07:31 +01:00
{
/* if PWM is zero, disable output */
2021-02-06 22:33:52 +01:00
if (value == PWM_MIN)
2021-02-06 21:07:31 +01:00
{
switch (channel)
{
case 0:
PWM_CH0_OFF();
break;
case 1:
PWM_CH1_OFF();
break;
case 2:
PWM_CH2_OFF();
break;
default:
break;
}
}
/* if PWM is max, enable output */
2021-02-06 22:33:52 +01:00
else if (value == PWM_MAX)
2021-02-06 21:07:31 +01:00
{
switch (channel)
{
case 0:
PWM_CH0_ON();
break;
case 1:
PWM_CH1_ON();
break;
case 2:
PWM_CH2_ON();
break;
default:
break;
}
}
/* else load new PWM into timer */
else
{
switch (channel)
{
case 0:
2021-02-06 22:33:52 +01:00
PWM_CH0_PWM(value);
2021-02-06 21:07:31 +01:00
break;
case 1:
2021-02-06 22:33:52 +01:00
PWM_CH1_PWM(value);
2021-02-06 21:07:31 +01:00
break;
case 2:
2021-02-06 22:33:52 +01:00
PWM_CH2_PWM(value);
2021-02-06 21:07:31 +01:00
break;
default:
break;
}
}
switch (channel)
{
case 0:
2021-02-06 22:33:52 +01:00
case 2:
2021-02-06 21:07:31 +01:00
/* disable timer if all channels are ON or OFF */
if (PWM_TIM1_CHECK())
{
PWM_TIM1_ENABLE();
}
else
{
PWM_TIM1_DISABLE();
}
break;
2021-02-06 22:33:52 +01:00
case 1:
2021-02-06 21:07:31 +01:00
/* disable timer if all channels are ON or OFF */
if (PWM_TIM3_CHECK())
{
PWM_TIM3_ENABLE();
}
else
{
PWM_TIM3_DISABLE();
}
break;
}
} /* pwm_update */
void pwm_event_handler(event_entry_t * p_event)
{
2021-02-06 22:33:52 +01:00
if (p_event->type == EVENT_TYPE_PWM_VALUE)
2021-02-06 21:07:31 +01:00
{
2021-02-06 22:33:52 +01:00
pwm_update(p_event->num, p_event->value);
2021-02-06 21:07:31 +01:00
}
} /* pwm_event_handler */
uint8_t pwm_get_sleep_mode(void)
{
2021-02-06 22:33:52 +01:00
return !!(PWM_TIM1_RUNNING() || PWM_TIM3_RUNNING());
2021-02-06 21:07:31 +01:00
} /* pwm_get_sleep_mode */
void pwm_init(void)
{
PWM_TIM_INIT();
} /* pwm_init */