usbfanctrl/main.c

114 lines
3.8 KiB
C
Raw Normal View History

2021-02-13 21:17:34 +01:00
/***************************************************************************
* Copyright (C) 02/2021 by Olaf Rempel *
* 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. *
***************************************************************************/
#include <avr/interrupt.h>
2021-01-31 21:00:05 +01:00
#include <avr/io.h>
#include <avr/power.h>
2021-02-06 21:07:31 +01:00
#include "event.h"
2021-02-06 22:33:52 +01:00
#include "ledfade.h"
2021-02-06 21:07:31 +01:00
#include "pwm.h"
#include "timer.h"
2021-02-13 21:17:34 +01:00
#include "usbcdc.h"
2021-01-31 21:00:05 +01:00
2021-02-13 21:17:34 +01:00
/* *********************************************************************** */
2021-01-31 21:00:05 +01:00
2021-02-13 21:17:34 +01:00
/* *************************************************************************
* disable_wdt_timer
* ************************************************************************* */
void disable_wdt_timer(void) __attribute__((naked, section(".init3")));
void disable_wdt_timer(void)
{
MCUSR = 0;
WDTCSR = (1<<WDCE) | (1<<WDE);
WDTCSR = (0<<WDE);
} /* disable_wdt_timer */
2021-01-31 21:00:05 +01:00
2021-02-13 21:17:34 +01:00
/* *************************************************************************
* main
* ************************************************************************* */
2021-01-31 21:00:05 +01:00
int main(void)
{
2021-02-13 21:17:34 +01:00
DDRD = (1<<PORTD5);
PORTD = (1<<PORTD5);
/* Disable clock division */
clock_prescale_set(clock_div_1);
2021-01-31 21:00:05 +01:00
2021-02-13 21:17:34 +01:00
usbcdc_init();
2021-02-06 21:07:31 +01:00
timer_init();
pwm_init();
2021-02-13 21:17:34 +01:00
sei();
2021-01-31 21:00:05 +01:00
2021-02-13 21:17:34 +01:00
// fade LEDs to max, set fan PWM to 50%
2021-02-06 22:33:52 +01:00
event_queue(EVENT_TYPE_LEDFADE_COMMAND, EVENT_NUM_LED_CH0, EVENT_VALUE_LEDFADE_MAX);
event_queue(EVENT_TYPE_LEDFADE_COMMAND, EVENT_NUM_LED_CH1, EVENT_VALUE_LEDFADE_MAX);
event_queue(EVENT_TYPE_PWM_VALUE, EVENT_NUM_PWM_CH2, 0x8000);
2021-02-06 21:07:31 +01:00
2021-01-31 21:00:05 +01:00
for (;;)
{
2021-02-13 21:17:34 +01:00
usbcdc_task();
2021-02-06 21:07:31 +01:00
timer_check(0);
event_entry_t* p_event = event_get();
2021-02-06 22:33:52 +01:00
switch (p_event->type)
2021-02-06 21:07:31 +01:00
{
2021-02-06 22:33:52 +01:00
case EVENT_TYPE_TIMER_SET:
timer_event_handler(p_event);
break;
case EVENT_TYPE_TIMER_ELAPSED:
if (p_event->num == EVENT_NUM_TIMER_LEDFADE)
{
ledfade_event_handler(p_event);
}
break;
case EVENT_TYPE_PWM_VALUE:
pwm_event_handler(p_event);
break;
case EVENT_TYPE_LEDFADE_COMMAND:
case EVENT_TYPE_LEDFADE_VALUE:
ledfade_event_handler(p_event);
2021-02-13 21:17:34 +01:00
if (p_event->num == EVENT_NUM_LED_CH0)
{
PORTD &= ~(1<<PORTD5);
}
2021-02-06 22:33:52 +01:00
break;
case EVENT_TYPE_LEDFADE_STATUS:
2021-02-13 21:17:34 +01:00
usbcdc_event_handler(p_event);
if (p_event->num == EVENT_NUM_LED_CH0)
{
PORTD |= (1<<PORTD5);
}
2021-02-06 22:33:52 +01:00
break;
default:
break;
2021-02-06 21:07:31 +01:00
}
2021-02-06 22:33:52 +01:00
event_clear(p_event);
2021-01-31 21:00:05 +01:00
}
}