forked from EmbedME/ISPnub
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
288 lines (223 loc) · 6.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/**
* @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 + ATmega16, ATmega32, ATmega644 (all TQFP-44)
* for testing ATmega328P@ext. 16MHZ Oscillator is supported but it has to be flashed via ISP (not bootloader) to work
* - Compiler: avr-gcc (GCC) 4.9.2
*
* @section history Change History
* - ISPnubMod v1.0, based on ISPnub 1.3 (2017-02-26)
* - Improvements for Battery-Powered Devices
* - Added Buzzer
* - Fix: Made slowticker volatile
* - Added HAL for other TQFP44-AVRs
* - Added Yellow LED (red is only for errors)
*
*/
/**
* @file main.c
*
* @brief This file contains the main routine with application entry point for
* the ISPnub firmware project
*
* @author Thomas Fischl, Michael G.
* @copyright (c) 2013-2014, 2017 Thomas Fischl, Michael G.
*
* 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 "main.h"
#include "hal.h"
#include <inttypes.h>
#include <stdio.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <avr/sleep.h>
#include <avr/power.h>
#include "clock.h"
#include "isp.h"
#include "counter.h"
#include "script.h"
#include "debounce.h"
/**
* @brief Main routine of firmware and application entry point
* @return Application return code
*/
int main(void) {
hal_init();
clock_init();
set_sleep_mode(SLEEP_MODE_PWR_DOWN);
uint8_t ticker10MS = clock_getTickerFast();
uint8_t ticker250MS = clock_getTickerSlow();
uint8_t sleeptimer = clock_getTickerSlow();
uint8_t success = 1;
uint8_t buzzer = 0; //time to turn on in multiple by 10ms
uint8_t toggle250MS = 0;
uint8_t state = S_INIT;
// enable interrupts
sei();
// main loop
while (1) {
//slow ticks
if (clock_getTickerSlowDiff(ticker250MS) > CLOCK_TICKER_SLOW_250MS) {
ticker250MS = clock_getTickerSlow();
toggle250MS = !toggle250MS;
}
//fast ticks
if (clock_getTickerFastDiff(ticker10MS) > CLOCK_TICKER_FAST_10MS) {
ticker10MS = clock_getTickerFast();
tickDebounce();
if(buzzer>0)
buzzer--;
}
//buzzer
if (buzzer==0) {
hal_setBuzzer(0);
} else {
hal_setBuzzer(1);
}
//led signaling
switch(state) {
case S_INIT:
case S_WAKEUP:
hal_setLEDgreen(1);
hal_setLEDyellow(0);
hal_setLEDred(0);
hal_setBuzzer(0);
break;
case S_IDLE:
if(success==1) {
hal_setLEDgreen(1);
hal_setLEDyellow(0);
hal_setLEDred(0);
} else {
hal_setLEDgreen(0);
hal_setLEDyellow(0);
hal_setLEDred(toggle250MS);
}
break;
case S_PROGRAMMING:
hal_setLEDgreen(0);
#if COMPATIBILITY_MODE_ISPNUB_ORIGINAL == 1 || DONT_USE_YELLOW_LED == 1
hal_setLEDyellow(0);
hal_setLEDred(1);
#else
hal_setLEDyellow(1);
hal_setLEDred(0);
#endif
break;
case S_NO_MORE:
hal_setLEDgreen(toggle250MS);
hal_setLEDyellow(0);
hal_setLEDred(toggle250MS);
break;
case S_NO_PROGRAM:
hal_setLEDgreen(!toggle250MS);
hal_setLEDyellow(0);
hal_setLEDred(toggle250MS);
break;
case S_SLEEP:
//disable all signals for max. power-saving
hal_setLEDgreen(0);
hal_setLEDyellow(0);
hal_setLEDred(0);
hal_setBuzzer(0);
break;
}
//processing
switch(state) {
case S_INIT:
case S_WAKEUP:
//remaining cycles to program?
if(counter_read()==0) {
state=S_NO_MORE;
}
state=S_IDLE;
break;
case S_IDLE:
if( get_key_press(1 << IO_SWITCH ) || get_key_press(1 << IO_EXT_SWITCH) ) {
sleeptimer=clock_getTickerSlow();
if(counter_read()>0) {
state=S_PROGRAMMING;
} else {
buzzer=60;
state=S_NO_MORE;
}
}
break;
case S_PROGRAMMING:
success = script_run();
if(success==1) { // programming OK
buzzer=3;
state=S_IDLE;
} else if(success==0) { // programming failed (connection, wrong avr, ...)
buzzer=30;
state=S_IDLE;
} else { // programming failed due to missing program
buzzer=60;
state=S_NO_PROGRAM;
}
//update sleeptimer to prevent falling asleep after a long-lasting programming task
sleeptimer=clock_getTickerSlow();
break;
case S_NO_MORE:
case S_NO_PROGRAM:
//nothing to do any more (except from going to sleep)...
break;
case S_SLEEP:
//enable interrupts for wakeup
hal_enableINT0();
hal_enableINT1();
sleep_enable();
#if HAS_DYNAMIC_BOD_CONTROL == 1
sleep_bod_disable();
#endif
sei();
sleep_cpu();
//execution is resumed here after processing interrupt
sleep_disable();
//update timer to prevent direct entry of sleepmode after wakeup
sleeptimer=clock_getTickerSlow();
state=S_WAKEUP;
break;
}
//go to sleep?
cli(); //for atomic check of condition
if (clock_getTickerSlowDiff(sleeptimer) > CLOCK_TICKER_SLOW_8S) {
state=S_SLEEP; //go to sleep
} else {
sei(); //stay awake
}
}
return (0);
}
ISR(INT1_vect) {
hal_disableINT1(); //disable interrupt, since its a level interrupt, fired as long as switch is hold.
}
ISR(INT0_vect) {
hal_disableINT0(); //disable interrupt, since its a level interrupt, fired as long as switch is hold.
}