-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.c
139 lines (113 loc) · 3.68 KB
/
main.c
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
/**
* @mainpage
* ISPnub firmware project
*
* @section description Description
* ISPnub is a stand-alone AVR programming module. The programming instructions
* are defined within scripts. These scripts are converted with an additional
* tool (ISPnubCreator) into binary information and are stored in flash. This
* firmware interprets this binary programming instructions.
*
* The firmware hex file is packed into the JAR file of ISPnubCreator which
* merges the firmware hex data with programming instructions from scripts.
*
* Environment:
* - Target: ATmega1284P
* - Compiler: avr-gcc (GCC) 4.7.2
*
* @section history Change History
* - v1.0 (2013-06-14)
* - Initial release
* - v1.1 (2013-10-06)
* - Code cleanup and documentation
* - v1.2 (2014-04-08)
* - Added EEPROM programming
*
*/
/**
* @file main.c
*
* @brief This file contains the main routine with application entry point for
* the ISPnub firmware project
*
* @author Thomas Fischl
* @copyright (c) 2013-2014 Thomas Fischl
*
* 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 3 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, see <http://www.gnu.org/licenses/>.
*
*/
#include <inttypes.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "clock.h"
#include "isp.h"
#include "counter.h"
#include "hal.h"
#include "script.h"
/**
* @brief Main routine of firmware and application entry point
* @return Application return code
*/
int main(void) {
hal_init();
clock_init();
// enable interrupts
sei();
uint8_t ticker = clock_getTickerSlow();
uint8_t toggle = 0;
uint16_t counter = counter_read();
uint8_t success = 1;
uint8_t keyticker = clock_getTickerSlow();
uint8_t keylocked = 1;
hal_setLEDgreen(1);
hal_setLEDred(0);
// main loop
while (1) {
if (keylocked) {
// do debouncing
if (hal_getSwitch()) {
keyticker = clock_getTickerSlow();
} else if (clock_getTickerSlowDiff(keyticker) > CLOCK_TICKER_SLOW_500MS) {
keylocked = 0;
}
} else if (hal_getSwitch()) {
// key pressed
if (counter > 0) {
hal_setLEDgreen(1);
hal_setLEDred(1);
success = script_run();
counter = counter_read();
hal_setLEDgreen(success);
hal_setLEDred(0);
ticker = clock_getTickerSlow();
} else {
success = 0;
}
keylocked = 1;
keyticker = clock_getTickerSlow();
}
// do led signaling
if (clock_getTickerSlowDiff(ticker) > CLOCK_TICKER_SLOW_250MS) {
ticker = clock_getTickerSlow();
toggle = !toggle;
if (counter == 0) hal_setLEDgreen(toggle);
else hal_setLEDgreen(success);
if (!success) hal_setLEDred(toggle);
else hal_setLEDred(0);
}
}
return (0);
}