blob: b1df3419521c77c7ed850b5b60547994ae6afccd (
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
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
|
/*
* Copyright 2013-2014 Ettus Research LLC
*
* 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/>.
*/
/*
* Welcome to the firmware code for the USRP Octoclock accessory product!
*
* Notes regarding this firmware:
* NOT in M103 compatibility mode
* CKOPT full rail-to-rail
* xtal osc
* 16K CK (16K clock cycles)
* additional delay 65ms for Crystal Oscillator
* slowly rising power
* persistent EEPROM
*
* These settings are very conservative. If a lower power oscillator is
* required, change CKOPT to '1' (UNPROGRAMMED).
*
* M103C = [ ]
* WDTON = [ ]
* OCDEN = [ ]
* JTAGEN = [X]
* SPIEN = [X]
* EESAVE = [X]
* BOOTSZ = 4095W_F000
* BOOTRST = [ ]
* CKOPT = [X]
* BODLEVEL = 2V7
* BODEN = [ ]
* SUT_CKSEL = EXTHIFXTALRES_16KCK_64MS
*
* EXTENDED = 0xFF (valid)
* HIGH = 0x81 (valid)
* LOW = 0xFF (valid)
*
*/
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <avr/eeprom.h>
#include <avr/interrupt.h>
#include <avr/io.h>
#include <avrlibdefs.h>
#include <octoclock.h>
#include <debug.h>
#include <clkdist.h>
#include <state.h>
#include <network.h>
#include <usart.h>
#include <net/udp_handlers.h>
/*
* Timer 3 (16-bit)
* * Set input capture trigger to rising edge
* * Set prescaler to 1
* * Enable overflow interrupt
* * Set timer to 0
*/
#define TIMER3_INIT() TCCR3B = (1 << ICES3) | (1 << CS30); \
ETIMSK |= (1 << TOIE3); \
TCNT3 = 0; \
ICR3 = 0;
/*
* We use TIMER3 as a watchdog timer for external reference
* detection. Once a signal is detected, we allow for five
* timer overflows (~26 ms) without another signal before
* deciding that there is no external reference connected.
*/
#define EXT_REF_TIMEOUT 5
static volatile uint16_t num_overflows = 0;
static uint16_t current_num_overflows = 0;
static uint16_t prev_num_overflows = 0;
static uint16_t current_ICR3 = 0;
static uint16_t prev_ICR3 = 0;
static ref_t prev_ref = NO_REF;
static switch_pos_t prev_switch_pos = PREFER_EXTERNAL;
bool top = false;
ISR(TIMER3_OVF_vect){
num_overflows++;
}
/*******************************************************************************
* Main Routine
*******************************************************************************/
int main(void){
/*
* Initializations
*/
cli();
// Make sure interrupts belong to us
MCUCR = (1<<IVCE);
MCUCR = 0;
// Initialize global variables
g_ext_ref_present = false;
g_gps_present = false;
g_switch_pos = PREFER_INTERNAL;
g_ref = NO_REF;
// Atmega128
setup_atmel_io_ports();
// Reset ClkDist chip
reset_TI_CDCE18005();
// GPSDO communication
usart_init();
// Ethernet stack
network_init();
register_udp_listener(OCTOCLOCK_UDP_CTRL_PORT, handle_udp_ctrl_packet);
register_udp_listener(OCTOCLOCK_UDP_GPSDO_PORT, handle_udp_gpsdo_packet);
// Timers
TIMER1_INIT(); // Network
TIMER3_INIT(); // External reference check
// Debug (does nothing when not in debug mode)
DEBUG_INIT();
DEBUG_LOG(" "); // Force a newline between runs
leds_off();
sei();
// Check if GPS present (should only need to happen once)
g_gps_present = (PIND & (1<<DDD4));
// State of previous iteration
prev_ref = NO_REF;
prev_switch_pos = PREFER_EXTERNAL;
cli();
prev_ICR3 = ICR3;
sei();
prev_num_overflows = 0;
/*
* Main loop
*/
while(true){
// Check switch position
g_switch_pos = (PINC & (1<<DDC1)) ? PREFER_EXTERNAL : PREFER_INTERNAL;
/*
* Check input capture pin for external reference detection.
*
* 16-bit reads could be corrupted during an interrupt, so
* disable interrupts for safety.
*/
cli();
current_ICR3 = ICR3;
current_num_overflows = num_overflows;
sei();
// Signal detected, reset timer
if(current_ICR3 != prev_ICR3){
cli();
TCNT3 = 0;
num_overflows = 0;
sei();
g_ext_ref_present = true;
}
// Timeout, no external reference detected
if(current_num_overflows >= EXT_REF_TIMEOUT){
g_ext_ref_present = false;
}
// Determine and set reference based on state
if(!g_gps_present && !g_ext_ref_present) g_ref = NO_REF;
else if(g_gps_present && !g_ext_ref_present) g_ref = INTERNAL;
else if(!g_gps_present && g_ext_ref_present) g_ref = EXTERNAL;
else g_ref = (g_switch_pos == PREFER_INTERNAL) ? INTERNAL : EXTERNAL;
if((g_ref != prev_ref) || (g_switch_pos != prev_switch_pos)){
if(g_switch_pos == PREFER_INTERNAL) prefer_internal();
else prefer_external();
}
// Record this iteration's state
prev_ref = g_ref;
prev_switch_pos = g_switch_pos;
prev_ICR3 = current_ICR3;
prev_num_overflows = current_num_overflows;
// Handle incoming Ethernet packets
network_check();
}
}
|