-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.c
executable file
·292 lines (254 loc) · 8.77 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
289
290
291
292
//***************************************************************************
//
// File : main.c
// Copyright : 2009-2012 Johnny McClymont, Paul Chote
// Description : Main program logic
//
// This file is part of Karaka, which is free software. It is made available
// to you under the terms of version 3 of the GNU General Public License, as
// published by the Free Software Foundation. For more information, see LICENSE.
//
//***************************************************************************
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <util/atomic.h>
#include "main.h"
#include "gps.h"
#include "display.h"
#include "usb.h"
#include "camera.h"
const char msg_duplicate_pulse[] PROGMEM = "WARNING: Missed serial data or duplicate time pulse";
const char msg_missing_pulse[] PROGMEM = "WARNING: Missed time pulse";
const char fmt_time_drift[] PROGMEM = "WARNING: %dms time drift";
// Internal timing mode
// MODE_PULSECOUNTER counts the 1Hz input signal and
// flags the next time packet as the download time
// MODE_HIGHRES uses the 1Hz signal for initial alignment
// and stability checks, but all timing is tracked
// from the hardware timers (assumes stable CPU clock)
uint8_t timing_mode = MODE_PULSECOUNTER;
uint16_t exposure_total = 0;
uint8_t trigger_stride = 0;
uint8_t align_boundary = 0;
volatile uint16_t exposure_countdown = 0;
volatile uint8_t trigger_countdown = 0;
volatile enum message_flags message_flags = 0;
volatile enum timer_status timer_status = TIMER_IDLE;
volatile enum gps_status gps_status = GPS_UNAVAILABLE;
enum gps_last_data {GPS_UNKNOWN, GPS_PULSE, GPS_SERIAL};
volatile enum gps_last_data gps_last_data = GPS_UNKNOWN;
inline void set_timer_status(enum timer_status status)
{
timer_status = status;
message_flags |= FLAG_SEND_STATUS;
}
inline void set_gps_status(enum gps_status status)
{
gps_status = status;
message_flags |= FLAG_SEND_STATUS;
}
// Internal millisecond count
// Remains zero if timing_mode == MODE_PULSECOUNTER
volatile uint16_t millisecond_count = 0;
volatile int16_t millisecond_drift = 0;
volatile struct timestamp download_timestamp;
volatile bool record_trigger = false;
struct timestamp current_timestamp;
int main(void)
{
// Enable pin change interrupt for pulse input
PCMSK3 |= _BV(PCINT28);
PCICR |= _BV(PCIE3);
// Enable pullup resistor on unused pins
PORTA = 0xFF;
// Set millisecond-timer period to 1ms
OCR1A = 9999;
STOP_MILLISECOND_TIMER;
TIMSK1 |= _BV(OCIE1A);
// Set other init
usb_initialize();
camera_initialize();
display_initialize();
// Enable relay mode until reboot
if (eeprom_read_byte(RELAY_EEPROM_OFFSET) == RELAY_ENABLED)
{
set_timer_status(TIMER_RELAY);
eeprom_update_byte(RELAY_EEPROM_OFFSET, RELAY_DISABLED);
}
// Enable interrupts
sei();
gps_initialize();
// Main program loop
for (;;)
{
// Handle message flags set via interrupt
if (message_flags && timer_status != TIMER_RELAY)
{
uint8_t temp_int_flags = 0;
ATOMIC_BLOCK(ATOMIC_FORCEON)
{
temp_int_flags = message_flags;
message_flags = 0;
}
if (temp_int_flags & FLAG_SEND_TRIGGER)
usb_send_trigger();
if (temp_int_flags & FLAG_SEND_TIMESTAMP)
usb_send_timestamp();
if (temp_int_flags & FLAG_SEND_STATUS)
usb_send_status(timer_status, gps_status);
if (temp_int_flags & FLAG_STOP_EXPOSURE)
usb_stop_exposure();
if (temp_int_flags & FLAG_DUPLICATE_PULSE)
usb_send_message_P(msg_duplicate_pulse);
if (temp_int_flags & FLAG_MISSING_PULSE)
usb_send_message_P(msg_missing_pulse);
if (temp_int_flags & FLAG_TIME_DRIFT)
usb_send_message_fmt_P(fmt_time_drift, millisecond_drift);
}
camera_tick();
usb_tick();
gps_tick();
display_update();
}
}
/*
* Millisecond timer interrupt handler
* Fired every 1ms when timing_mode == MODE_HIGHRES
*/
ISR(TIMER1_COMPA_vect)
{
millisecond_count++;
// End of exposure - send a trigger and save the time
// This is a 16-bit operation, but we are in an interrupt so it is atomic
if (--exposure_countdown == 0)
{
camera_trigger_readout();
exposure_countdown = exposure_total;
if (--trigger_countdown == 0)
{
download_timestamp = current_timestamp;
download_timestamp.milliseconds = millisecond_count;
trigger_countdown = trigger_stride;
message_flags |= FLAG_SEND_TRIGGER;
}
}
}
/*
* GPS time pulse interrupt handler
* Fired on any level change from the pulse input (PD4)
*/
ISR(PCINT3_vect)
{
// Trigger on the falling edge of the GPS pulse
// Note that the input buffer inverts the signal
//
// Triggering on the rising edge is unreliable as
// this check will fail if other interrupts delay
// the interrupt by >10us (which is often the case in
// high-resolution mode where the timer interrupt fires
// at the same time as the pulse arrives)
if (bit_is_clear(PIND, PD4))
return;
switch (timer_status)
{
case TIMER_EXPOSING:
case TIMER_READOUT:
if (timing_mode == MODE_HIGHRES)
{
// Test for time drift
// Doesn't need to be atomic, as interrupts are disabled in an interrupt context
uint16_t drift = millisecond_count;
while (drift >= 1000)
drift -= 1000;
if (drift != 0)
{
millisecond_drift = drift > 500 ? (drift - 1000) : drift;
message_flags |= FLAG_TIME_DRIFT;
}
}
else
{
// End of exposure - send a trigger to the camera
// and store a flag so the gps can save the synctime.
// This is a 16-bit operation, but we are in an interrupt so it is atomic
if (--exposure_countdown == 0)
{
camera_trigger_readout();
exposure_countdown = exposure_total;
record_trigger = true;
}
}
break;
case TIMER_ALIGN:
// Start the first exposure so that a (potentially future) exposure
// boundary will occur on the minute
if (current_timestamp.seconds % align_boundary != align_boundary - 1)
break;
set_timer_status(TIMER_EXPOSING);
if (timing_mode == MODE_HIGHRES)
{
// Enable the millisecond timer to begin sending triggers
// Start timer with an initial count to align trigger as close to
// the pulse as possible. This figure is determined experimentally
// by comparing the GPS and trigger pulses with an oscilloscope
//
// Constants for configuring the millisecond timer
// MILLISECOND_TCNT is calibrated with an oscilloscope
// to minimize the offset between 1Hz signal and triggers
TCNT1 = 355;
START_MILLISECOND_TIMER;
}
else
{
camera_trigger_readout();
exposure_countdown = exposure_total;
record_trigger = true;
}
break;
case TIMER_RELAY:
camera_trigger_readout();
break;
case TIMER_WAITING:
case TIMER_IDLE:
// Do nothing
break;
}
// Send a warning about the duplicate pulse
if (gps_last_data == GPS_PULSE)
message_flags |= FLAG_DUPLICATE_PULSE;
gps_last_data = GPS_PULSE;
}
void set_time(struct timestamp *t)
{
ATOMIC_BLOCK(ATOMIC_FORCEON)
{
current_timestamp = *t;
}
message_flags |= FLAG_SEND_TIMESTAMP;
if (gps_status != GPS_ACTIVE)
set_gps_status(GPS_ACTIVE);
if (timing_mode == MODE_PULSECOUNTER && record_trigger)
{
record_trigger = false;
if (--trigger_countdown == 0)
{
download_timestamp = current_timestamp;
trigger_countdown = trigger_stride;
message_flags |= FLAG_SEND_TRIGGER;
}
}
else
{
// Reset rollover count
ATOMIC_BLOCK(ATOMIC_FORCEON)
{
while (millisecond_count > 1000)
millisecond_count -= 1000;
}
}
// Send a warning about the missing pulse
if (gps_last_data == GPS_SERIAL)
message_flags |= FLAG_MISSING_PULSE;
gps_last_data = GPS_SERIAL;
}