Commit 8ebf32f3 authored by Jan Kasprzak's avatar Jan Kasprzak
Browse files

pwm.c: single PWM channel only

Simplify it as much as possible - single PWM channel only, no
sub-LSB PWM value.
parent 8f7b420b
......@@ -31,17 +31,11 @@ void susp_adc();
void timer_start_slow_adcs();
/* pwm.c */
/*
* The real Timer/Counter 1 frequency should not be too close to the
* A/D converter frequency (125 kHz). Note that this is not the Top
* value of T/C 1, it is shifted by PWM_STEP_SHIFT as described in pwm.c
*/
#define PWM_MAX 0x340
#define PWM_MAX 0xFF
void init_pwm();
void susp_pwm();
void pwm_off(unsigned char n);
void pwm_set(unsigned char n, uint16_t stride);
void pwm_timer();
void pwm_off();
void pwm_set(uint8_t stride);
/* tmr.c */
extern volatile uint16_t jiffies;
......
......@@ -5,16 +5,12 @@
#include "lights.h"
#define PWM_STEP_SHIFT 2 /* sub-LSB precision */
#define PWM_TOP (((PWM_MAX) + (4 << (PWM_STEP_SHIFT))) >> (PWM_STEP_SHIFT))
#if PWM_TOP > 0x0FF
#error PWM_TOP too high
#endif
static uint16_t pwm[N_PWMLEDS];
static volatile unsigned char step;
/*
* Single PWM channel on OC1B (pin PB4 of Tiny45).
* Counts from 0 to 0xFF, without OCR1C compare.
*/
static void enable_pll()
static void inline enable_pll()
{
/* Async clock */
PLLCSR = _BV(PLLE);
......@@ -28,23 +24,16 @@ static void enable_pll()
void init_pwm()
{
int i;
step = 0;
for (i = 0; i < N_PWMLEDS; i++)
pwm[i] = 0;
enable_pll();
TCCR1 = _BV(CTC1) | _BV(CS10); // no clock prescaling
GTCCR = _BV(COM1A1) | _BV(COM1B1) | _BV(PWM1A) | _BV(PWM1B);
GTCCR = _BV(COM1B1) | _BV(PWM1B);
OCR1C = PWM_TOP;
OCR1A = OCR1B = 0; // initial stride is 0
OCR1C = PWM_MAX;
OCR1B = 0; // initial stride is 0
DDRB &= ~(_BV( PB1 ) | _BV( PB4 )); // tristate it
PORTB &= ~(_BV( PB1 ) | _BV( PB4 )); // set to zero
DDRB &= ~_BV(PB4); // tristate it
PORTB &= ~_BV(PB4); // set to zero
}
#if 0
......@@ -64,58 +53,14 @@ void susp_pwm()
}
#endif
void pwm_off(unsigned char n)
void pwm_off()
{
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
pwm[n] = 0;
switch (n) {
case 0: DDRB &= ~_BV(PB1); break;
case 1: DDRB &= ~_BV(PB4); break;
}
}
OCR1B = 0;
DDRB &= ~_BV(PB4);
}
static void pwm_update_hw(unsigned char n)
void pwm_set(uint8_t stride)
{
uint16_t stride = (pwm[n] + step) >> PWM_STEP_SHIFT;
switch (n) {
case 0:
OCR1A = stride;
break;
case 1:
OCR1B = stride;
break;
}
OCR1B = stride;
DDRB |= _BV(PB4);
}
void pwm_set(unsigned char n, uint16_t stride)
{
if (stride > PWM_MAX)
stride = PWM_MAX;
ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
pwm[n] = stride;
pwm_update_hw(n);
switch(n) {
case 0: DDRB |= _BV(PB1); break;
case 1: DDRB |= _BV(PB4); break;
}
}
}
void pwm_timer()
{
unsigned char i;
if (++step >= (1 << PWM_STEP_SHIFT))
step = 0;
for (i = 0; i < N_PWMLEDS; i++)
if (pwm[i])
pwm_update_hw(i);
}
......@@ -72,6 +72,7 @@ void init_pwmled()
led->err_sums[j] = 0;
}
}
pwmleds[0].state = ST_DISABLED;
}
void pwmled_set_mode(unsigned char n, unsigned char mode)
......@@ -94,10 +95,10 @@ void pwmled_set_mode(unsigned char n, unsigned char mode)
led->pwm = led->mode_pwm[mode - 1];
led->err_sum = led->err_sums[mode - 1];
led->mode_changed = 1;
pwm_set(n, led->pwm);
pwm_set(led->pwm);
} else {
led->state = ST_OFF;
pwm_off(n);
pwm_off();
}
}
......@@ -135,7 +136,7 @@ static inline unsigned char pwmled_probed_ok(unsigned char n, uint16_t old_pwm)
unsigned char i;
led->state = ST_OFF;
pwm_off(n);
pwm_off();
log_byte(0xF0);
log_byte(n);
......@@ -156,7 +157,7 @@ static inline unsigned char pwmled_probed_ok(unsigned char n, uint16_t old_pwm)
static inline void pwmled_err(unsigned char n)
{
pwmleds[n].state = ST_DISABLED;
pwm_off(n);
pwm_off();
log_byte(0xF1);
log_byte(n);
......@@ -208,6 +209,6 @@ void pwmled_adc(unsigned char n, uint16_t adcval)
if (led->pwm == old_pwm)
return;
pwm_set(n, led->pwm);
pwm_set(led->pwm);
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment