aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/main.c
blob: 2ab545b80540b2bb5d3c7c754b76d6bdfb6cad8f (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
 * Xytronic LF-1600
 * Open Source firmware
 *
 * Copyright (c) 2015-2017 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 "presets.h"

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


/* Configure unused package pins. */
static void unused_pins_init(void)
{
	/* Configure unused pins as inputs with pull up
	 * to get a well defined level on the input driver.
	 */

	/* PB5 */
	DDRB &= (uint8_t)~(1u << DDB5);
	PORTB |= 1u << PB5;

	/* PB6 */
	DDRB &= (uint8_t)~(1u << DDB6);
	PORTB |= 1u << PB6;

	/* PC0 */
	DDRC &= (uint8_t)~(1u << DDC0);
	PORTC |= 1u << PC0;
}

/* Early boot routine. Runs before main().
 * This function does not have a stack frame.
 */
void early_init(void) __attribute__((naked, section(".init3"), used));
void early_init(void)
{
	/* Reconfigure watchdog. */
	MCUSR = 0;
	wdt_enable(WDTO_2S);
}

/* One iteration of the main loop. */
#ifndef SIMULATOR
static
#endif
void main_loop_once(void)
{
	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();
}

#ifndef RUN_MAIN_LOOP
# define RUN_MAIN_LOOP	1
#endif

/* Main entry point. */
int main(void) _mainfunc;
int main(void)
{
	/* Disable all interrupt sources. */
	TIMSK0 = 0u;
	TIMSK1 = 0u;

	/* 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();

	/* Configure unused package pins. */
	unused_pins_init();

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

	wdt_enable(WDTO_250MS);
	irq_enable();
	while (RUN_MAIN_LOOP)
		main_loop_once();

	return 0;
}
bues.ch cgit interface