summaryrefslogtreecommitdiffstats
path: root/firmware/main.c
blob: 8749a3dae77dad53dfa1de2b49ccc0bc0a04c4b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Xytronic LF-1600
 * Open Source firmware
 *
 * Copyright (c) 2015 Michael Buesch <m@bues.ch>
 *
 * 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; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "main.h"
#include "measure.h"
#include "measure_current.h"
#include "measure_temp.h"
#include "controller_current.h"
#include "controller_temp.h"
#include "pwm_current.h"
#include "timer.h"
#include "util.h"
#include "debug_uart.h"
#include "display.h"
#include "buttons.h"
#include "menu.h"
#include "settings.h"
#include "calib_current.h"
#include "presets.h"

#include <avr/io.h>
#include <avr/wdt.h>


void early_init(void) __attribute__((naked, section(".init3"), used));
void early_init(void)
{
	MCUSR = 0;
	wdt_enable(WDTO_2S);
}

int main(void) _mainfunc;
int main(void)
{
	/* Wait a bit for the capacitors to charge
	 * and the reference voltage to stabilize.
	 */
	_delay_ms(300);

	/* Initialize basic system functions. */
	timer_init();
	buttons_init();
	debug_uart_init();
	settings_init();
	display_init();

	/* Initialize the measurement subsystem */
	measure_init();
	meascurr_init();
	meastemp_init();

	/* Initialize the controllers. */
	contrcurr_init();
	presets_init();
	contrtemp_init();

	/* Initialize the current actuator. */
	pwmcurr_init();

	/* Initialize the user interface. */
	menu_init();

	/* Startup measurements to get everything going. */
	measure_start();

	wdt_enable(WDTO_250MS);
	irq_enable();
	while (1) {
		wdt_reset();

		/* Handle measurement results (if any).
		 * This will also run the controllers, if needed.
		 */
		measure_work();

		/* Handle user interface events. */
		menu_work();
		display_work();
		buttons_work();
		settings_work();
		calcurr_work();
	}
}
bues.ch cgit interface