-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBusPuter.ino
390 lines (294 loc) · 7.99 KB
/
BusPuter.ino
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/****************************************************
* Busputer
* https://github.com/brvdg/busputer
*
* Author: Brun
* License: MIT
****************************************************/
#define VERSION "0.8"
#define BUILD "180219a"
#include <stdarg.h>
#include <stdio.h>
#include <stdint.h>
#include <avr/pgmspace.h>
#include <avr/dtostrf.h>
// timer
//#include <Adafruit_SleepyDog.h>
//#include <Adafruit_ZeroTimer.h>
//Adafruit_ZeroTimer zt3 = Adafruit_ZeroTimer(3);
#include <SDU.h>
/*
* Copy the .bin file to the SD card and rename
* the file to UPDATE.bin.
*/
// Include EEPROM-like API for FlashStorage
#include <FlashAsEEPROM.h>
#include <FlashStorage.h>
// include configuration file
#include "config.h"
#include "vars.h"
void setup() {
int i;
uint32_t tmp = 0;
uint32_t tmp2 = 0;
uint32_t old_val = 0;
Serial.begin(250000);
delay(2000);
Serial.print( F("#Compiled: "));
Serial.print( F(__DATE__));
Serial.print( F(", "));
Serial.print( F(__TIME__));
Serial.print( F(", "));
Serial.println( F(__VERSION__));
/*
* Display
*/
#ifdef U8G2_DISPLAY
display_init();
#endif
display_bootmsg(F("Booting..."));
display_bootmsg(F(BUILD));
//delay(5000);
/*
* IO
*/
#ifdef FeatherLED8
pinMode(FeatherLED8, OUTPUT);
digitalWrite(FeatherLED8, LOW);
#endif //FeatherLED8
/*
* read config from FlashStorage
*/
display_bootmsg(F("READ FLASH"));
read_virtual_eeprom();
// initialize the IO Ports
IO_init();
// Button
pinMode(BUTTON_PIN_1, INPUT_PULLUP);
// Dumping SDCard
if (digitalRead(BUTTON_PIN_1) == LOW) {
display_bootmsg(F("Waiting for Serial"));
#ifdef FeatherLED8
digitalWrite(FeatherLED8, HIGH);
#endif FeatherLED8
while (! Serial);
INFO_PRINTLN( F("#Serial export is active"));
serial_export = true;
//#ifdef SDCARD
display_bootmsg(F("Hold Button to dump"));
INFO_PRINTLN( F("#Hold Button to dump data"));
delay(3000);
if (digitalRead(BUTTON_PIN_1) == LOW) {
dump_sd_card();
}
//#endif //SDCARD
}
/*
* SD Card
*/
//#ifdef SDCARD
if ( enable_sd == 1 ) {
enable_sdcard();
//get_last_log();
//#endif //SDCARD
if ( SDmount ) {
//open the configuration from SD
sdcard_open_config();
//Check the logs
get_last_log();
//delay(3000);
}
}
else {
display_bootmsg(F("SD is disabled"));
delay(10000);
}
/*
* Simcom TinyGSM
*/
#ifdef TinyGSM
if ( enable_tinygsm == 1 ) {
TRACE_PRINTLN(F("#tinygsm_init()"));
tinygsm_init();
}
#endif
/*
* OneWire Bus
*/
#ifdef ONEWIRE
onewire_init();
#endif // ONEWIRE
/*
* I2C Sensors
*/
#ifdef I2C
i2c_init();
#endif // I2C
/*
* decide the temp sensors
*/
if ( lm75_1_available ) {
temp_out_port = 1;
INFO_PRINTLN(F("#LM75 for temp. out"));
}
else if ( onewire_available ) {
temp_out_port = 2;
INFO_PRINTLN(F("#DS18B20 for temp. out"));
}
else {
INFO_PRINTLN(F("#nothing for temp. out"));
}
/*
* RTC
*/
rtc.begin();
/*
* enable Timer
*/
// set time for Watchdog
watchdog_timer = millis() + WATCHDOG_TIMER*10;
// Enable clock for TC
REG_GCLK_CLKCTRL = (uint16_t) (GCLK_CLKCTRL_CLKEN | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_ID ( GCM_TCC2_TC3 ) ) ;
while ( GCLK->STATUS.bit.SYNCBUSY == 1 ); // wait for sync
// The type cast must fit with the selected timer mode
TcCount16* TC = (TcCount16*) TC3; // get timer struct
TC->CTRLA.reg &= ~TC_CTRLA_ENABLE; // Disable TCx
while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync
TC->CTRLA.reg |= TC_CTRLA_MODE_COUNT16; // Set Timer counter Mode to 16 bits
//TC->CTRLA.reg |= TC_CTRLA_MODE_COUNT8; // Set Timer counter Mode to 16 bits
while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync
TC->CTRLA.reg |= TC_CTRLA_WAVEGEN_NFRQ; // Set TC as normal Normal Frq
while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync
TC->CTRLA.reg |= TC_CTRLA_PRESCALER_DIV1024; // Set perscaler
while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync
// TC->PER.reg = 0xFF; // Set counter Top using the PER register but the 16/32 bit timer counts allway to max
// while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync
TC->CC[0].reg = 0xFFF;
while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync
// Interrupts
TC->INTENSET.reg = 0; // disable all interrupts
TC->INTENSET.bit.OVF = 1; // enable overfollow
//TC->INTENSET.bit.MC0 = 1; // enable compare match to CC0
// Enable InterruptVector
NVIC_EnableIRQ(TC3_IRQn);
// Enable TC
TC->CTRLA.reg |= TC_CTRLA_ENABLE;
while (TC->STATUS.bit.SYNCBUSY == 1); // wait for sync
//
/********************* Timer #3, 8 bit, one callback with adjustable period = 350KHz ~ 2.86us for DAC updates */
/*zt3.configure(TC_CLOCK_PRESCALER_DIV1, // prescaler
TC_COUNTER_SIZE_8BIT, // bit width of timer/counter
TC_WAVE_GENERATION_MATCH_PWM // match style
);
zt3.setPeriodMatch(150, 1, 0); // ~350khz, 1 match, channel 0
zt3.setCallback(true, TC_CALLBACK_CC_CHANNEL0, Timer3Callback0); // set callback
zt3.enable(true);*/
// at least custom functions
#ifdef CUSTOM
custom_init();
#endif // CUSTOM
//read_flash();
display_bootmsg(F("Ready"));
set_alarm(1000,50,10, false);
// First a normal example of using the watchdog timer.
// Enable the watchdog by calling Watchdog.enable() as below. This will turn
// on the watchdog timer with a ~4 second timeout before reseting the Arduino.
// The estimated actual milliseconds before reset (in milliseconds) is returned.
// Make sure to reset the watchdog before the countdown expires or the Arduino
// will reset!
/*int countdownMS = Watchdog.enable(65000);
Serial.print("#Enabled the watchdog with max countdown of ");
Serial.print(countdownMS, DEC);
Serial.println(" milliseconds!");
Serial.println();*/
// Watchdog
watchdog_timer = millis() + WATCHDOG_TIMER*10;
#ifdef U8G2_DISPLAY
MainMenuPos = 1;
display_update_timer_lock = true;
display_update();
display_update_timer_lock = false;
#endif // LCD
}
/*
* The main loop
*/
void loop() {
// Reset the watchdog with every loop to make sure the sketch keeps running.
// If you comment out this call watch what happens after about 4 iterations!
watchdog_timer = millis() + WATCHDOG_TIMER;
//Serial.print(F("WD: "));
//Serial.println(watchdog_timer, DEC);
#ifdef FeatherLED8
digitalWrite(FeatherLED8, HIGH);
#endif FeatherLED8
#ifdef TinyGSM
//if (engine_running) {
if ( tinygsminit ) {
tinygsm_loop();
}
//}
#endif
#ifdef FeatherLED8
digitalWrite(FeatherLED8, LOW);
#endif FeatherLED8
#ifdef ONEWIRE
onewire_loop();
#endif // ONEWIRE
#ifdef I2C
i2c_loop();
#endif // SI7021
update_vars();
// calculating trip informations
trip();
#ifdef SDCARD
if ( engine_running ) {
log_to_sdcard();
}
#endif
#ifdef PRINT_STATUS
print_status();
#endif // PRINT_STATUS
IO_loop();
#ifdef U8G2_DISPLAY
if ( display_update_timer < millis() ) {
display_update_timer = millis() + U8G2_DISPLAY_UPDATE_TIMER;
display_loop();
}
#endif // U8G2_DISPLAY
button();
// Status Checker
status_checker();
alarm = 1;
//make_alarm();
alarm_loop();
// at least custom functions
#ifdef CUSTOM
custom_loop();
#endif // CUSTOM
delay(100);
}
/*
* Internal Timer
*/
void TC3_Handler()
{
TcCount16* TC = (TcCount16*) TC3; // get timer struct
if (TC->INTFLAG.bit.OVF == 1) { // A overflow caused the interrupt
//Serial.print(".");
TC->INTFLAG.bit.OVF = 1; // writing a one clears the flag ovf flag
if ( alarm_timer < millis() ) {
alarm_timer = millis() + ALARM_TIMER;
alarm_loop();
}
// Wagchdog
if ( watchdog_timer < millis() ) {
// reset the system
Serial.println(F("#Watchdog reset"));
//wdreset PROGMEM = true;
flash_watchdog_reset.write(true);
NVIC_SystemReset();
}
button();
}
}