|
|
@ -22,12 +22,14 @@ |
|
|
|
|
|
|
|
#include "event.h" |
|
|
|
#include "pwm.h" |
|
|
|
|
|
|
|
#include "target.h" |
|
|
|
|
|
|
|
/* *********************************************************************** */ |
|
|
|
|
|
|
|
const uint16_t pwmtable[128] PROGMEM = |
|
|
|
#if (PWM_TIM_16BIT) |
|
|
|
#define PWM_TABLE_SIZE 128 |
|
|
|
#define PWM_TABLE_GET(x) pgm_read_word(&pwmtable16[x]) |
|
|
|
const uint16_t pwmtable16[PWM_TABLE_SIZE] PROGMEM = |
|
|
|
{ |
|
|
|
0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, |
|
|
|
6, 6, 7, 8, 9, 9, 10, 11, 12, 14, 15, 16, 18, 20, 22, 24, 26, |
|
|
@ -41,8 +43,17 @@ const uint16_t pwmtable[128] PROGMEM = |
|
|
|
25267, 27553, 30047, 32767, 35733, 38967, 42494, 46340, 50534, |
|
|
|
55108, 60096, 65535 |
|
|
|
}; |
|
|
|
|
|
|
|
#define PWMTABLE_SIZE (sizeof(pwmtable) / sizeof(uint16_t)) |
|
|
|
#else |
|
|
|
#define PWM_TABLE_SIZE 32 |
|
|
|
#define PWM_TABLE_GET(x) pgm_read_byte(&pwmtable8[x]) |
|
|
|
const uint8_t pwmtable8[PWM_TABLE_SIZE] PROGMEM = |
|
|
|
{ |
|
|
|
0, 0, 1, 1, 1, 2, 2, 3, |
|
|
|
4, 5, 6, 7, 9, 10, 12, 15, |
|
|
|
18, 22, 26, 31, 37, 44, 53, 63, |
|
|
|
75, 90, 107, 127, 151, 180, 214, 255 |
|
|
|
}; |
|
|
|
#endif |
|
|
|
|
|
|
|
static uint8_t pwm_index[2]; |
|
|
|
static uint8_t pwm_setpoint[2]; |
|
|
@ -64,7 +75,7 @@ void pwm_event_handler(struct event_entry *event) |
|
|
|
|
|
|
|
/* increment PWM */ |
|
|
|
case EVENT_VALUE_PWM_INC: |
|
|
|
if (pwm_setpoint[channel] < (PWMTABLE_SIZE -1)) |
|
|
|
if (pwm_setpoint[channel] < (PWM_TABLE_SIZE -1)) |
|
|
|
{ |
|
|
|
pwm_setpoint[channel]++; |
|
|
|
} |
|
|
@ -85,11 +96,10 @@ void pwm_event_handler(struct event_entry *event) |
|
|
|
|
|
|
|
/* fade to max */ |
|
|
|
case EVENT_VALUE_PWM_FADE_MAX: |
|
|
|
pwm_setpoint[channel] = (PWMTABLE_SIZE -1); |
|
|
|
pwm_setpoint[channel] = (PWM_TABLE_SIZE -1); |
|
|
|
break; |
|
|
|
|
|
|
|
// FIXME: move to some other module? |
|
|
|
case PWM_MODE_TOGGLE: |
|
|
|
case EVENT_VALUE_PWM_TOGGLE: |
|
|
|
if (pwm_setpoint[channel] > 5) |
|
|
|
{ |
|
|
|
pwm_setpoint_save[channel] = pwm_setpoint[channel]; |
|
|
@ -109,7 +119,14 @@ void pwm_event_handler(struct event_entry *event) |
|
|
|
} |
|
|
|
else if (event->type == EVENT_TYPE_PWM_VALUE) |
|
|
|
{ |
|
|
|
pwm_setpoint[event->num] = event->value; |
|
|
|
if (event->value < PWM_TABLE_SIZE) |
|
|
|
{ |
|
|
|
pwm_setpoint[event->num] = event->value; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
pwm_setpoint[event->num] = (PWM_TABLE_SIZE -1); |
|
|
|
} |
|
|
|
|
|
|
|
PWM_TIM_ENABLE(); |
|
|
|
} |
|
|
@ -119,16 +136,32 @@ void pwm_event_handler(struct event_entry *event) |
|
|
|
static void pwm_update(uint8_t channel) |
|
|
|
{ |
|
|
|
if ((pwm_setpoint[channel] > pwm_index[channel]) && |
|
|
|
(pwm_index[channel] < (PWMTABLE_SIZE -1)) |
|
|
|
) |
|
|
|
(pwm_index[channel] < (PWM_TABLE_SIZE -1)) |
|
|
|
) |
|
|
|
{ |
|
|
|
pwm_index[channel]++; |
|
|
|
|
|
|
|
/* setpoint reached, notify others */ |
|
|
|
if (pwm_index[channel] == pwm_setpoint[channel]) |
|
|
|
{ |
|
|
|
event_queue(EVENT_TYPE_PWM_STATUS, |
|
|
|
channel, |
|
|
|
pwm_setpoint[channel]); |
|
|
|
} |
|
|
|
} |
|
|
|
else if ((pwm_setpoint[channel] < pwm_index[channel]) && |
|
|
|
(pwm_index[channel] > 0) |
|
|
|
(pwm_index[channel] > 0) |
|
|
|
) |
|
|
|
{ |
|
|
|
pwm_index[channel]--; |
|
|
|
|
|
|
|
/* setpoint reached, notify others */ |
|
|
|
if (pwm_index[channel] == pwm_setpoint[channel]) |
|
|
|
{ |
|
|
|
event_queue(EVENT_TYPE_PWM_STATUS, |
|
|
|
channel, |
|
|
|
pwm_setpoint[channel]); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/* if PWM is zero, disable output */ |
|
|
@ -144,7 +177,7 @@ static void pwm_update(uint8_t channel) |
|
|
|
} |
|
|
|
} |
|
|
|
/* if PWM is max, enable output */ |
|
|
|
else if (pwm_index[channel] == (PWMTABLE_SIZE -1)) |
|
|
|
else if (pwm_index[channel] == (PWM_TABLE_SIZE -1)) |
|
|
|
{ |
|
|
|
if (channel == 0) |
|
|
|
{ |
|
|
@ -158,14 +191,14 @@ static void pwm_update(uint8_t channel) |
|
|
|
/* else load new PWM into timer */ |
|
|
|
else |
|
|
|
{ |
|
|
|
if (channel == 0) |
|
|
|
{ |
|
|
|
PWM_CH0_PWM(pgm_read_word(&pwmtable[pwm_index[0]])); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
PWM_CH1_PWM(pgm_read_word(&pwmtable[pwm_index[1]])); |
|
|
|
} |
|
|
|
if (channel == 0) |
|
|
|
{ |
|
|
|
PWM_CH0_PWM(PWM_TABLE_GET(pwm_index[0])); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
PWM_CH1_PWM(PWM_TABLE_GET(pwm_index[1])); |
|
|
|
} |
|
|
|
} |
|
|
|
} /* pwm_set */ |
|
|
|
|
|
|
@ -175,7 +208,7 @@ ISR(PWM_TIM_VECT) |
|
|
|
static uint8_t delay; |
|
|
|
|
|
|
|
delay++; |
|
|
|
if (delay == 4) |
|
|
|
if (delay == PWM_DELAY_COUNT) |
|
|
|
{ |
|
|
|
delay = 0; |
|
|
|
|
|
|
@ -196,6 +229,19 @@ ISR(PWM_TIM_VECT) |
|
|
|
} /* TIM1_OVF_vect */ |
|
|
|
|
|
|
|
|
|
|
|
uint8_t pwm_get_sleep_mode(void) |
|
|
|
{ |
|
|
|
if (PWM_TIM_RUNNING()) |
|
|
|
{ |
|
|
|
return SLEEP_MODE_IDLE; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
return SLEEP_MODE_PWR_DOWN; |
|
|
|
} |
|
|
|
} /* pwm_get_sleep_mode */ |
|
|
|
|
|
|
|
|
|
|
|
void pwm_init(void) |
|
|
|
{ |
|
|
|
PWM_TIM_INIT(); |
|
|
|