OpenCores
URL https://opencores.org/ocsvn/rtcclock/rtcclock/trunk

Subversion Repositories rtcclock

[/] [rtcclock/] [trunk/] [rtl/] [rtcgps.v] - Blame information for rev 7

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 7 dgisselq
///////////////////////////////////////////////////////////////////////////
2
//
3
// Filename:    rtcgps.v
4
//              
5
// Project:     A Wishbone Controlled Real--time Clock Core, w/ GPS synch
6
//
7
// Purpose:     Implement a real time clock, including alarm, count--down
8
//              timer, stopwatch, variable time frequency, and more.
9
//
10
//      This particular version has hooks for a GPS 1PPS, as well as a 
11
//      finely tracked clock speed output, to allow for fine clock precision
12
//      and good freewheeling even if/when GPS is lost.
13
//
14
//
15
// Creator:     Dan Gisselquist, Ph.D.
16
//              Gisselquist Tecnology, LLC
17
//
18
///////////////////////////////////////////////////////////////////////////
19
//
20
// Copyright (C) 2015, Gisselquist Technology, LLC
21
//
22
// This program is free software (firmware): you can redistribute it and/or
23
// modify it under the terms of  the GNU General Public License as published
24
// by the Free Software Foundation, either version 3 of the License, or (at
25
// your option) any later version.
26
//
27
// This program is distributed in the hope that it will be useful, but WITHOUT
28
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
29
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
30
// for more details.
31
//
32
// You should have received a copy of the GNU General Public License along
33
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
34
// target there if the PDF file isn't present.)  If not, see
35
// <http://www.gnu.org/licenses/> for a copy.
36
//
37
// License:     GPL, v3, as defined and found on www.gnu.org,
38
//              http://www.gnu.org/licenses/gpl.html
39
//
40
//
41
///////////////////////////////////////////////////////////////////////////
42
module  rtcgps(i_clk,
43
                // Wishbone interface
44
                i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data,
45
                //      o_wb_ack, o_wb_stb, o_wb_data, // no reads here
46
                // // Button inputs
47
                // i_btn,
48
                // Output registers
49
                o_data, // multiplexed based upon i_wb_addr
50
                // Output controls
51
                o_sseg, o_led, o_interrupt,
52
                // A once-per-day strobe on the last clock of the day
53
                o_ppd,
54
                // GPS interface
55
                i_gps_valid, i_gps_pps, i_gps_ckspeed);
56
        parameter       DEFAULT_SPEED = 32'd2814750; //2af31e = 2^48 / 100e6 MHz
57
        input   i_clk;
58
        input   i_wb_cyc, i_wb_stb, i_wb_we;
59
        input   [1:0]    i_wb_addr;
60
        input   [31:0]   i_wb_data;
61
        // input                i_btn;
62
        output  reg     [31:0]   o_data;
63
        output  reg     [31:0]   o_sseg;
64
        output  wire    [15:0]   o_led;
65
        output  wire            o_interrupt, o_ppd;
66
        // GPS interface
67
        input                   i_gps_valid, i_gps_pps;
68
        input           [31:0]   i_gps_ckspeed;
69
 
70
        reg     [23:0]   clock;
71
        reg     [31:0]   stopwatch, ckspeed;
72
        reg     [25:0]   timer;
73
 
74
        wire    ck_sel, tm_sel, sw_sel, al_sel;
75
        assign  ck_sel = ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr==2'b00));
76
        assign  tm_sel = ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr==2'b01));
77
        assign  sw_sel = ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr==2'b10));
78
        assign  al_sel = ((i_wb_cyc)&&(i_wb_stb)&&(i_wb_addr==2'b11));
79
 
80
        reg     [39:0]   ck_counter;
81
        reg             ck_carry;
82
        always @(posedge i_clk)
83
                { ck_carry, ck_counter } <= ck_counter + { 8'h00, ckspeed };
84
 
85
        wire            ck_pps;
86
        reg             ck_prepps, ck_ppm, ck_pph, ck_ppd;
87
        reg     [7:0]    ck_sub;
88
        initial clock = 24'h00000000;
89
        assign  ck_pps = (ck_carry)&&(ck_prepps);
90
        always @(posedge i_clk)
91
        begin
92
                if ((i_gps_valid)&&(i_gps_pps))
93
                        ck_sub <= 0;
94
                else if (ck_carry)
95
                        ck_sub <= ck_sub + 1;
96
                if (i_gps_valid)
97
                        ck_prepps <= i_gps_pps;
98
                else
99
                        ck_prepps <= (ck_sub == 8'hff);
100
 
101
                if (ck_pps)
102
                begin // advance the seconds
103
                        if (clock[3:0] >= 4'h9)
104
                                clock[3:0] <= 4'h0;
105
                        else
106
                                clock[3:0] <= clock[3:0] + 4'h1;
107
                        if (clock[7:0] >= 8'h59)
108
                                clock[7:4] <= 4'h0;
109
                        else if (clock[3:0] >= 4'h9)
110
                                clock[7:4] <= clock[7:4] + 4'h1;
111
                end
112
                ck_ppm <= (clock[7:0] == 8'h59);
113
 
114
                if ((ck_pps)&&(ck_ppm))
115
                begin // advance the minutes
116
                        if (clock[11:8] >= 4'h9)
117
                                clock[11:8] <= 4'h0;
118
                        else
119
                                clock[11:8] <= clock[11:8] + 4'h1;
120
                        if (clock[15:8] >= 8'h59)
121
                                clock[15:12] <= 4'h0;
122
                        else if (clock[11:8] >= 4'h9)
123
                                clock[15:12] <= clock[15:12] + 4'h1;
124
                end
125
                ck_pph <= (clock[15:0] == 16'h5959);
126
 
127
                if ((ck_pps)&&(ck_pph))
128
                begin // advance the hours
129
                        if (clock[21:16] >= 6'h23)
130
                        begin
131
                                clock[19:16] <= 4'h0;
132
                                clock[21:20] <= 2'h0;
133
                        end else if (clock[19:16] >= 4'h9)
134
                        begin
135
                                clock[19:16] <= 4'h0;
136
                                clock[21:20] <= clock[21:20] + 2'h1;
137
                        end else begin
138
                                clock[19:16] <= clock[19:16] + 4'h1;
139
                        end
140
                end
141
                ck_ppd <= (clock[21:0] == 22'h235959);
142
 
143
 
144
                if ((ck_sel)&&(i_wb_we))
145
                begin
146
                        if (8'hff != i_wb_data[7:0])
147
                        begin
148
                                clock[7:0] <= i_wb_data[7:0];
149
                                ck_ppm <= (i_wb_data[7:0] == 8'h59);
150
                        end
151
                        if (8'hff != i_wb_data[15:8])
152
                        begin
153
                                clock[15:8] <= i_wb_data[15:8];
154
                                ck_pph <= (i_wb_data[15:8] == 8'h59);
155
                        end
156
                        if (6'h3f != i_wb_data[21:16])
157
                                clock[21:16] <= i_wb_data[21:16];
158
                        clock[23:22] <= i_wb_data[25:24];
159
                        if ((~i_gps_valid)&&(8'h00 == i_wb_data[7:0]))
160
                                ck_sub <= 8'h00;
161
                end
162
        end
163
 
164
        reg     [21:0]   ck_last_clock;
165
        always @(posedge i_clk)
166
                ck_last_clock <= clock[21:0];
167
 
168
        reg     tm_pps, tm_ppm, tm_int;
169
        wire    tm_stopped, tm_running, tm_alarm;
170
        assign  tm_stopped = ~timer[24];
171
        assign  tm_running =  timer[24];
172
        assign  tm_alarm   =  timer[25];
173
        reg     [23:0]           tm_start;
174
        reg     [7:0]            tm_sub;
175
        initial tm_start = 24'h00;
176
        initial timer    = 26'h00;
177
        initial tm_int   = 1'b0;
178
        initial tm_pps   = 1'b0;
179
        always @(posedge i_clk)
180
        begin
181
                if (ck_carry)
182
                begin
183
                        tm_sub <= tm_sub + 1;
184
                        tm_pps <= (tm_sub == 8'hff);
185
                end else
186
                        tm_pps <= 1'b0;
187
 
188
                if ((~tm_alarm)&&(tm_running)&&(tm_pps))
189
                begin // If we are running ...
190
                        timer[25] <= 1'b0;
191
                        if (timer[23:0] == 24'h00)
192
                                timer[25] <= 1'b1;
193
                        else if (timer[3:0] != 4'h0)
194
                                timer[3:0] <= timer[3:0]-4'h1;
195
                        else begin // last digit is a zero
196
                                timer[3:0] <= 4'h9;
197
                                if (timer[7:4] != 4'h0)
198
                                        timer[7:4] <= timer[7:4]-4'h1;
199
                                else begin // last two digits are zero
200
                                        timer[7:4] <= 4'h5;
201
                                        if (timer[11:8] != 4'h0)
202
                                                timer[11:8] <= timer[11:8]-4'h1;
203
                                        else begin // last three digits are zero
204
                                                timer[11:8] <= 4'h9;
205
                                                if (timer[15:12] != 4'h0)
206
                                                        timer[15:12] <= timer[15:12]-4'h1;
207
                                                else begin
208
                                                        timer[15:12] <= 4'h5;
209
                                                        if (timer[19:16] != 4'h0)
210
                                                                timer[19:16] <= timer[19:16]-4'h1;
211
                                                        else begin
212
                                                        //
213
                                                                timer[19:16] <= 4'h9;
214
                                                                timer[23:20] <= timer[23:20]-4'h1;
215
                                                        end
216
                                                end
217
                                        end
218
                                end
219
                        end
220
                end
221
 
222
                if((~tm_alarm)&&(tm_running))
223
                begin
224
                        timer[25] <= (timer[23:0] == 24'h00);
225
                        tm_int <= (timer[23:0] == 24'h00);
226
                end else tm_int <= 1'b0;
227
                if (tm_alarm)
228
                        timer[24] <= 1'b0;
229
 
230
                if ((tm_sel)&&(i_wb_we)&&(tm_running)) // Writes while running
231
                        // Only allowed to stop the timer, nothing more
232
                        timer[24] <= i_wb_data[24];
233
                else if ((tm_sel)&&(i_wb_we)&&(tm_stopped)) // Writes while off
234
                begin
235
                        timer[24] <= i_wb_data[24];
236
                        if ((timer[24])||(i_wb_data[24]))
237
                                timer[25] <= 1'b0;
238
                        if (i_wb_data[23:0] != 24'h0000)
239
                        begin
240
                                timer[23:0] <= i_wb_data[23:0];
241
                                tm_start <= i_wb_data[23:0];
242
                                tm_sub <= 8'h00;
243
                        end else if (timer[23:0] == 24'h00)
244
                        begin // Resetting timer to last valid timer start val
245
                                timer[23:0] <= tm_start;
246
                                tm_sub <= 8'h00;
247
                        end
248
                        // Any write clears the alarm
249
                        timer[25] <= 1'b0;
250
                end
251
        end
252
 
253
        //
254
        // Stopwatch functionality
255
        //
256
        // Setting bit '0' starts the stop watch, clearing it stops it.
257
        // Writing to the register with bit '1' high will clear the stopwatch,
258
        // and return it to zero provided that the stopwatch is stopped either
259
        // before or after the write.  Hence, writing a '2' to the device
260
        // will always stop and clear it, whereas writing a '3' to the device
261
        // will only clear it if it was already stopped.
262
        reg             sw_pps, sw_ppm, sw_pph;
263
        reg     [7:0]    sw_sub;
264
        wire    sw_running;
265
        assign  sw_running = stopwatch[0];
266
        initial stopwatch = 32'h00000;
267
        always @(posedge i_clk)
268
        begin
269
                sw_pps <= 1'b0;
270
                if (sw_running)
271
                begin
272
                        if (ck_carry)
273
                        begin
274
                                sw_sub <= sw_sub + 1;
275
                                sw_pps <= (sw_sub == 8'hff);
276
                        end
277
                end
278
 
279
                stopwatch[7:1] <= sw_sub[7:1];
280
 
281
                if (sw_pps)
282
                begin // Second hand
283
                        if (stopwatch[11:8] >= 4'h9)
284
                                stopwatch[11:8] <= 4'h0;
285
                        else
286
                                stopwatch[11:8] <= stopwatch[11:8] + 4'h1;
287
 
288
                        if (stopwatch[15:8] >= 8'h59)
289
                                stopwatch[15:12] <= 4'h0;
290
                        else if (stopwatch[11:8] >= 4'h9)
291
                                stopwatch[15:12] <= stopwatch[15:12] + 4'h1;
292
                        sw_ppm <= (stopwatch[15:8] == 8'h59);
293
                end else sw_ppm <= 1'b0;
294
 
295
                if (sw_ppm)
296
                begin // Minutes
297
                        if (stopwatch[19:16] >= 4'h9)
298
                                stopwatch[19:16] <= 4'h0;
299
                        else
300
                                stopwatch[19:16] <= stopwatch[19:16]+4'h1;
301
 
302
                        if (stopwatch[23:16] >= 8'h59)
303
                                stopwatch[23:20] <= 4'h0;
304
                        else if (stopwatch[19:16] >= 4'h9)
305
                                stopwatch[23:20] <= stopwatch[23:20]+4'h1;
306
                        sw_pph <= (stopwatch[23:16] == 8'h59);
307
                end else sw_pph <= 1'b0;
308
 
309
                if (sw_pph)
310
                begin // And hours
311
                        if (stopwatch[27:24] >= 4'h9)
312
                                stopwatch[27:24] <= 4'h0;
313
                        else
314
                                stopwatch[27:24] <= stopwatch[27:24]+4'h1;
315
 
316
                        if((stopwatch[27:24] >= 4'h9)&&(stopwatch[31:28] < 4'hf))
317
                                stopwatch[31:28] <= stopwatch[27:24]+4'h1;
318
                end
319
 
320
                if ((sw_sel)&&(i_wb_we))
321
                begin
322
                        stopwatch[0] <= i_wb_data[0];
323
                        if((i_wb_data[1])&&((~stopwatch[0])||(~i_wb_data[0])))
324
                        begin
325
                                stopwatch[31:1] <= 31'h00;
326
                                sw_sub <= 8'h00;
327
                                sw_pps <= 1'b0;
328
                                sw_ppm <= 1'b0;
329
                                sw_pph <= 1'b0;
330
                        end
331
                end
332
        end
333
 
334
        //
335
        // The alarm code
336
        //
337
        // Set the alarm register to the time you wish the board to "alarm".
338
        // The "alarm" will take place once per day at that time.  At that
339
        // time, the RTC code will generate a clock interrupt, and the CPU/host
340
        // can come and see that the alarm tripped.
341
        //
342
        // 
343
        reg     [21:0]           alarm_time;
344
        reg                     al_int,         // The alarm interrupt line
345
                                al_enabled,     // Whether the alarm is enabled
346
                                al_tripped;     // Whether the alarm has tripped
347
        initial al_enabled= 1'b0;
348
        initial al_tripped= 1'b0;
349
        always @(posedge i_clk)
350
        begin
351
                if ((al_sel)&&(i_wb_we))
352
                begin
353
                        // Only adjust the alarm hours if the requested hours
354
                        // are valid.  This allows writes to the register,
355
                        // without a prior read, to leave these configuration
356
                        // bits alone.
357
                        if (i_wb_data[21:16] != 6'h3f)
358
                                alarm_time[21:16] <= i_wb_data[21:16];
359
                        // Here's the same thing for the minutes: only adjust
360
                        // the alarm minutes if the new bits are not all 1's. 
361
                        if (i_wb_data[15:8] != 8'hff)
362
                                alarm_time[15:8] <= i_wb_data[15:8];
363
                        // Here's the same thing for the seconds: only adjust
364
                        // the alarm minutes if the new bits are not all 1's. 
365
                        if (i_wb_data[7:0] != 8'hff)
366
                                alarm_time[7:0] <= i_wb_data[7:0];
367
                        al_enabled <= i_wb_data[24];
368
                        // Reset the alarm if a '1' is written to the tripped
369
                        // register, or if the alarm is disabled.
370
                        if ((i_wb_data[25])||(~i_wb_data[24]))
371
                                al_tripped <= 1'b0;
372
                end
373
 
374
                al_int <= 1'b0;
375
                if ((ck_last_clock != alarm_time)&&(clock[21:0] == alarm_time)&&(al_enabled))
376
                begin
377
                        al_tripped <= 1'b1;
378
                        al_int <= 1'b1;
379
                end
380
        end
381
 
382
        //
383
        // The ckspeed register is equal to 2^48 divded by the number of
384
        // clock ticks you expect per second.  Adjust high for a slower
385
        // clock, lower for a faster clock.  In this fashion, a single
386
        // real time clock RTL file can handle tracking the clock in any
387
        // device.  Further, because this is only the lower 32 bits of a 
388
        // 48 bit counter per seconds, the clock jitter is kept below
389
        // 1 part in 65 thousand.
390
        //
391
        initial ckspeed = DEFAULT_SPEED;
392
        // In the case of verilator, comment the above and uncomment the line
393
        // below.  The clock constant below is "close" to simulation time,
394
        // meaning that my verilator simulation is running about 300x slower
395
        // than board time.
396
        // initial      ckspeed = 32'd786432000;
397
        always @(posedge i_clk)
398
                if (i_gps_valid)
399
                        ckspeed <= i_gps_ckspeed;
400
 
401
        reg     [15:0]   h_sseg;
402
        reg     [3:1]   dmask;
403
        always @(posedge i_clk)
404
                case(clock[23:22])
405
                2'h1: begin h_sseg <= timer[15:0];
406
                        if (tm_alarm) dmask <= 3'h7;
407
                        else begin
408
                                dmask[3] <= (12'h000 != timer[23:12]); // timer[15:12]
409
                                dmask[2] <= (16'h000 != timer[23: 8]); // timer[11: 8]
410
                                dmask[1] <= (20'h000 != timer[23: 4]); // timer[ 7: 4]
411
                                // dmask[0] <= 1'b1; // Always on
412
                        end end
413
                2'h2: begin h_sseg <= stopwatch[19:4];
414
                                dmask[3] <= (12'h00  != stopwatch[27:16]);
415
                                dmask[2] <= (16'h000 != stopwatch[27:12]);
416
                                dmask[1] <= 1'b1; // Always on, stopwatch[11:8]
417
                                // dmask[0] <= 1'b1; // Always on, stopwatch[7:4]
418
                        end
419
                2'h3: begin h_sseg <= clock[15:0];
420
                                dmask[3:1] <= 3'h7;
421
                        end
422
                default: begin // 4'h0
423
                        h_sseg <= { 2'b00, clock[21:8] };
424
                        dmask[2:1] <= 2'b11;
425
                        dmask[3] <= (2'b00 != clock[21:20]);
426
                        end
427
                endcase
428
 
429
        wire    [31:0]   w_sseg;
430
        assign  w_sseg[ 0] =  (~ck_sub[7]);
431
        assign  w_sseg[ 8] =  (~ck_sub[6])&&(~ck_sub[7]);
432
        assign  w_sseg[16] =  (~ck_sub[5])&&(~ck_sub[6])&&(~ck_sub[7]);
433
        assign  w_sseg[24] = 1'b0;
434
        hexmap  ha(i_clk, h_sseg[ 3: 0], w_sseg[ 7: 1]);
435
        hexmap  hb(i_clk, h_sseg[ 7: 4], w_sseg[15: 9]);
436
        hexmap  hc(i_clk, h_sseg[11: 8], w_sseg[23:17]);
437
        hexmap  hd(i_clk, h_sseg[15:12], w_sseg[31:25]);
438
 
439
        always @(posedge i_clk)
440
                if ((tm_alarm || al_tripped)&&(ck_sub[7]))
441
                        o_sseg <= 32'h0000;
442
                else
443
                        o_sseg <= {
444
                                (dmask[3])?w_sseg[31:24]:8'h00,
445
                                (dmask[2])?w_sseg[23:16]:8'h00,
446
                                (dmask[1])?w_sseg[15: 8]:8'h00,
447
                                w_sseg[ 7: 0] };
448
 
449
        reg     [17:0]   ledreg;
450
        always @(posedge i_clk)
451
                if ((ck_pps)&&(ck_ppm))
452
                        ledreg <= 18'h00;
453
                else if (ck_carry)
454
                        ledreg <= ledreg + 18'h11;
455
        assign  o_led = (tm_alarm||al_tripped)?{ (16){ck_sub[7]}}:
456
                                { ledreg[17:10],
457
                                ledreg[10], ledreg[11], ledreg[12], ledreg[13],
458
                                ledreg[14], ledreg[15], ledreg[16], ledreg[17] };
459
 
460
        assign  o_interrupt = tm_int || al_int;
461
 
462
        // A once-per day strobe, on the last second of the day so that the
463
        // the next clock is the first clock of the day.  This is useful for
464
        // connecting this module to a year/month/date date/calendar module.
465
        assign  o_ppd = (ck_ppd)&&(ck_pps);
466
 
467
        always @(posedge i_clk)
468
                case(i_wb_addr)
469
                2'b00: o_data <= { ~i_gps_valid, 5'h0, clock[23:22], 2'b00, clock[21:0] };
470
                2'b01: o_data <= { 6'h00, timer };
471
                2'b10: o_data <= stopwatch;
472
                2'b11: o_data <= { 6'h00, al_tripped, al_enabled, 2'b00, alarm_time };
473
                endcase
474
 
475
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.