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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [rtl/] [rtcgps.v] - Blame information for rev 60

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

Line No. Rev Author Line
1 30 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 3 dgisselq
//
3
// Filename:    rtcgps.v
4
//              
5
// Project:     OpenArty, an entirely open SoC based upon the Arty platform
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 Technology, LLC
17
//
18 30 dgisselq
////////////////////////////////////////////////////////////////////////////////
19 3 dgisselq
//
20
// Copyright (C) 2015-2016, 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 30 dgisselq
////////////////////////////////////////////////////////////////////////////////
42
//
43
//
44 3 dgisselq
module  rtcgps(i_clk,
45
                // Wishbone interface
46
                i_wb_cyc, i_wb_stb, i_wb_we, i_wb_addr, i_wb_data,
47
                //      o_wb_ack, o_wb_stb, o_wb_data, // no reads here
48
                // Output registers
49
                o_data, // multiplexed based upon i_wb_addr
50
                // Output controls
51
                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
                // Our personal timing, for debug purposes
57
                o_rtc_pps);
58
        parameter       DEFAULT_SPEED = 32'd2814750; //2af31e = 2^48 / 100e6 MHz
59 25 dgisselq
        //
60
        input                   i_clk;
61
        //
62
        input                   i_wb_cyc, i_wb_stb, i_wb_we;
63
        input           [1:0]    i_wb_addr;
64
        input           [31:0]   i_wb_data;
65 3 dgisselq
        // input                i_btn;
66
        output  reg     [31:0]   o_data;
67
        output  wire            o_interrupt, o_ppd;
68
        // GPS interface
69
        input                   i_gps_valid, i_gps_pps;
70
        input           [31:0]   i_gps_ckspeed;
71
        // Personal PPS
72
        output  wire            o_rtc_pps;
73
 
74
        reg     [21:0]   clock;
75
        reg     [31:0]   stopwatch, ckspeed;
76
        reg     [25:0]   timer;
77
 
78
        reg     ck_wr, tm_wr, sw_wr, al_wr, r_data_zero_byte;
79
        reg     [25:0]   r_data;
80
        always @(posedge i_clk)
81
        begin
82
                ck_wr <= ((i_wb_stb)&&(i_wb_addr==2'b00)&&(i_wb_we));
83
                tm_wr <= ((i_wb_stb)&&(i_wb_addr==2'b01)&&(i_wb_we));
84
                sw_wr <= ((i_wb_stb)&&(i_wb_addr==2'b10)&&(i_wb_we));
85
                al_wr <= ((i_wb_stb)&&(i_wb_addr==2'b11)&&(i_wb_we));
86
                r_data <= i_wb_data[25:0];
87
                r_data_zero_byte <= (i_wb_data[7:0] == 8'h00);
88
        end
89
 
90
        reg     [39:0]   ck_counter;
91
        reg             ck_carry, ck_sub_carry;
92
        always @(posedge i_clk)
93
                if ((i_gps_valid)&&(i_gps_pps))
94
                begin
95
                        ck_carry   <= 0;
96
                        // Start our counter 2 clocks into the future.
97
                        // Why?  Because if we hit the PPS, we'll be delayed
98
                        // one clock from true time.  This (hopefully) locks
99
                        // us back onto true time.  Further, if we end up
100
                        // off (i.e., go off before the GPS tick ...) then
101
                        // the GPS tick will put us back on track ... likewise
102
                        // we've got code following that should keep us from
103
                        // ever producing two PPS's per second.
104
                        ck_counter <= { 7'h00, ckspeed, 1'b0 };
105
                        ck_sub_carry <= ckspeed[31];
106
                end else begin
107
                        { ck_sub_carry, ck_counter[31:0] }
108
                                <= ck_counter[31:0] + ckspeed;
109
                        { ck_carry, ck_counter[39:32] }
110
                                <= ck_counter[39:32] + { 7'h0, ck_sub_carry };
111
                end
112
 
113
        reg             ck_pps;
114
        reg             ck_ppm, ck_pph, ck_ppd;
115
        reg     [7:0]    ck_sub;
116
        initial clock = 22'h00000000;
117
        always @(posedge i_clk)
118
                if ((i_gps_pps)&&(i_gps_valid)&&(ck_sub[7]))
119
                        ck_pps <= 1'b1;
120
                else if ((ck_carry)&&(ck_sub == 8'hff))
121
                        ck_pps <= 1'b1;
122
                else
123
                        ck_pps <= 1'b0;
124
 
125
        reg     [6:0]    next_clock_secs;
126
        always @(posedge i_clk)
127
        begin
128
                next_clock_secs[3:0] <= (clock[3:0] >= 4'h9) ? 4'h0 // clk 1
129
                                                : (clock[3:0] + 4'h1);
130
                next_clock_secs[6:4] <= (ck_ppm) ? 3'h0 // clk 2
131
                                        : (clock[3:0] >= 4'h9)
132
                                                ? (clock[6:4] + 3'h1)
133
                                                : clock[6:4];
134
        end
135
 
136
        reg     [6:0]    next_clock_mins;
137
        always @(posedge i_clk)
138
        begin
139
                next_clock_mins[3:0] <= (clock[11:8] >= 4'h9) ? 4'h0
140
                                                : (clock[11:8] + 4'h1);
141
                next_clock_mins[6:4] <= (ck_pph) ? 3'h0
142
                                        : (clock[11:8] >= 4'h9)
143
                                                ? (clock[14:12] + 3'h1)
144
                                                : clock[14:12];
145
        end
146
 
147
        reg     [5:0]    next_clock_hrs;
148
        always @(posedge i_clk)
149
        begin
150
                next_clock_hrs[3:0] <= (clock[19:16] >= 4'h9) ? 4'h0
151
                                                : (clock[19:16] + 4'h1);
152
                next_clock_hrs[5:4] <= (ck_ppd) ? 2'h0
153
                                        : (clock[19:16] >= 4'h9)
154
                                                ? (clock[21:20] + 2'h1)
155
                                                : (clock[21:20]);
156
        end
157
 
158
        reg     [4:0] ck_pending;
159
        assign  o_rtc_pps = ck_pps;
160
        always @(posedge i_clk)
161
        begin
162
                if ((i_gps_valid)&&(i_gps_pps))
163
                        ck_sub <= 0;
164
                else if (ck_carry)
165
                        ck_sub <= ck_sub + 1;
166
 
167
                if ((ck_pps)&&(~ck_pending[4])) // advance the seconds
168
                        clock[6:0] <= next_clock_secs;
169
                clock[7] <= 1'b0;
170
                ck_ppm <= (clock[6:0] == 7'h59);
171
 
172
                if ((ck_pps)&&(ck_ppm)&&(~ck_pending[4])) // advance the minutes
173
                        clock[14:8] <= next_clock_mins;
174
                clock[15] <= 1'b0;
175
                ck_pph <= (clock[14:8] == 7'h59)&&(ck_ppm);
176
 
177
                if ((ck_pps)&&(ck_pph)&&(~ck_pending[4])) // advance the hours
178
                        clock[21:16] <= next_clock_hrs;
179
                ck_ppd <= (clock[21:16] == 6'h23)&&(ck_pph);
180
 
181
                clock[ 7] <= 1'b0;
182
                clock[15] <= 1'b0;
183
 
184
                if (ck_wr)
185
                begin
186
                        if (~r_data[7])
187
                                clock[6:0] <= i_wb_data[6:0];
188
                        if (~r_data[15])
189
                                clock[14:8] <= i_wb_data[14:8];
190
                        if (~r_data[22])
191
                                clock[21:16] <= i_wb_data[21:16];
192
                        if ((~i_gps_valid)&&(r_data_zero_byte))
193
                                ck_sub <= 8'h00;
194
                        ck_pending <= 5'h1f;
195
                end else
196
                        ck_pending <= { ck_pending[3:0], 1'b0 };
197
        end
198
 
199
        reg     [21:0]   ck_last_clock;
200
        always @(posedge i_clk)
201
                ck_last_clock <= clock[21:0];
202
 
203
 
204
 
205
        // 
206
        reg     [23:0]   next_timer;
207
        reg             ztimer;
208
        reg     [4:0]    tmr_carry;
209
        always @(posedge i_clk)
210
        begin
211
                tmr_carry[0] <= (timer[ 3: 0]== 4'h0);
212
                tmr_carry[1] <= (timer[ 6: 4]== 3'h0)&&(tmr_carry[0]);
213
                tmr_carry[2] <= (timer[11: 8]== 4'h0)&&(tmr_carry[1]);
214
                tmr_carry[3] <= (timer[14:12]== 3'h0)&&(tmr_carry[2]);
215
                tmr_carry[4] <= (timer[19:16]== 4'h0)&&(tmr_carry[3]);
216
                ztimer <= (timer[23:0]== 24'h0);
217
 
218
                // Keep unused bits at zero
219
                next_timer <= 24'h00;
220
                // Seconds
221
                next_timer[ 3: 0] <= (tmr_carry[0])? 4'h9: (timer[ 3: 0]-4'h1);
222
                next_timer[ 6: 4] <= (tmr_carry[1])? 3'h5: (timer[ 6: 4]-3'h1);
223
                // Minutes
224
                next_timer[11: 8] <= (tmr_carry[2])? 4'h9: (timer[11: 8]-4'h1);
225
                next_timer[14:12] <= (tmr_carry[3])? 3'h5: (timer[14:12]-3'h1);
226
                // Hours
227
                next_timer[19:16] <= (tmr_carry[4])? 4'h9: (timer[19:16]-4'h1);
228
                next_timer[23:20] <= (timer[23:20]-4'h1);
229
        end
230
 
231
        reg     new_timer, new_timer_set, new_timer_last, tm_pending_start;
232
        reg     [23:0]   new_timer_val;
233
 
234
        reg     tm_pps, tm_ppm, tm_int;
235
        wire    tm_stopped, tm_running, tm_alarm;
236
        assign  tm_stopped = ~timer[24];
237
        assign  tm_running =  timer[24];
238
        assign  tm_alarm   =  timer[25];
239
        reg     [23:0]           tm_start;
240
        reg     [7:0]            tm_sub;
241
        initial tm_start = 24'h00;
242
        initial timer    = 26'h00;
243
        initial tm_int   = 1'b0;
244
        initial tm_pps   = 1'b0;
245
        initial tm_pending_start = 1'b0;
246
        always @(posedge i_clk)
247
        begin
248
                if (ck_carry)
249
                begin
250
                        tm_sub <= tm_sub + 1;
251
                        tm_pps <= (tm_sub == 8'hff);
252
                end else
253
                        tm_pps <= 1'b0;
254
 
255
                if (new_timer_set) // Conclude a write
256
                        timer[23:0] <= new_timer_val;
257
                else if ((~tm_alarm)&&(tm_running)&&(tm_pps))
258
                begin // Otherwise, if we are running ...
259
                        timer[25] <= 1'b0; // Clear any alarm
260
                        if (ztimer) // unless we've hit zero
261
                                timer[25] <= 1'b1;
262
                        else if (~new_timer)
263
                                timer[23:0] <= next_timer;
264
                end
265
 
266
                tm_int <= (tm_running)&&(tm_pps)&&(~tm_alarm)&&(ztimer);
267
 
268
                if (tm_alarm) // Stop the timer on an alarm
269
                        timer[24] <= 1'b0;
270
 
271
                new_timer <= 1'b0;
272
                tm_pending_start <= 1'b0;
273
                if ((tm_wr)&&(tm_running)) // Writes while running
274
                        // Only allow the timer to stop, nothing more
275
                        timer[24] <= r_data[24];
276
                else if ((tm_wr)&&(tm_stopped)) // Writes while off
277
                begin
278
                        // We're going to pipeline this change by a couple
279
                        // of clocks, to get it right
280
                        new_timer <= 1'b1;
281
                        new_timer_val <= r_data[23:0];
282
                        tm_pending_start <= r_data[24];
283
 
284
                        // Still ... any write clears the alarm
285
                        timer[25] <= 1'b0;
286
                end
287
 
288
                new_timer_set  <= (new_timer)&&(new_timer_val != 24'h000);
289
                new_timer_last <= (new_timer)&&(new_timer_val == 24'h000);
290
                if (new_timer_last)
291
                begin
292
                        new_timer_val <= tm_start;
293
                        tm_sub <= 8'h00;
294
                        new_timer_set <= 1'b1;
295
                        tm_pending_start <= 1'b1;
296
                end else if (new_timer_set)
297
                begin
298
                        tm_start <= new_timer_val;
299
                        tm_sub <= 8'h00;
300
                        tm_pending_start <= 1'b1;
301
                        timer[24] <= 1'b1;
302
                end
303
        end
304
 
305
        //
306
        // Stopwatch functionality
307
        //
308
        // Setting bit '0' starts the stop watch, clearing it stops it.
309
        // Writing to the register with bit '1' high will clear the stopwatch,
310
        // and return it to zero provided that the stopwatch is stopped either
311
        // before or after the write.  Hence, writing a '2' to the device
312
        // will always stop and clear it, whereas writing a '3' to the device
313
        // will only clear it if it was already stopped.
314
        reg     [6:0]    next_sw_secs;
315
        always @(posedge i_clk)
316
        begin
317
                next_sw_secs[3:0] <= (stopwatch[11:8] >= 4'h9) ? 4'h0
318
                                                : (stopwatch[11:8] + 4'h1);
319
                next_sw_secs[6:4] <= (stopwatch[14:8] == 7'h59) ? 3'h0
320
                                        : (stopwatch[11:8] == 4'h9)
321
                                                ? (stopwatch[14:12]+3'h1)
322
                                                : stopwatch[14:12];
323
        end
324
 
325
        reg     [6:0]    next_sw_mins;
326
        always @(posedge i_clk)
327
        begin
328
                next_sw_mins[3:0] <= (stopwatch[19:16] >= 4'h9) ? 4'h0
329
                                                : (stopwatch[19:16] + 4'h1);
330
                next_sw_mins[6:4] <= (stopwatch[22:16] == 7'h59) ? 3'h0
331
                                        : (stopwatch[19:16]==4'h9)
332
                                                ? (stopwatch[22:20]+3'h1)
333
                                                : stopwatch[22:20];
334
        end
335
 
336
        reg     [5:0]    next_sw_hrs;
337
        always @(posedge i_clk)
338
        begin
339
                next_sw_hrs[3:0] <= (stopwatch[27:24] >= 4'h9) ? 4'h0
340
                                                : (stopwatch[27:24] + 4'h1);
341
                next_sw_hrs[5:4] <= (stopwatch[29:24] >= 6'h23) ? 2'h0
342
                                        : (stopwatch[27:24]==4'h9)
343
                                                ? (stopwatch[29:28]+2'h1)
344
                                                : stopwatch[29:28];
345
        end
346
 
347
        reg             sw_pps, sw_ppm, sw_pph;
348
        reg     [7:0]    sw_sub;
349
        wire    sw_running;
350
        assign  sw_running = stopwatch[0];
351
        initial stopwatch = 32'h00000;
352
        always @(posedge i_clk)
353
        begin
354
                sw_pps <= 1'b0;
355
                if ((sw_running)&&(ck_carry))
356
                begin
357
                        sw_sub <= sw_sub + 1;
358
                        sw_pps <= (sw_sub == 8'hff);
359
                end
360
 
361
                stopwatch[7:1] <= sw_sub[7:1];
362
 
363
                if (sw_pps) // Second hand
364
                        stopwatch[14:8] <= next_sw_secs;
365
                sw_ppm <= (stopwatch[14:8] == 7'h59);
366
 
367
                if ((sw_pps)&&(sw_ppm)) // Minutes
368
                        stopwatch[22:16] <= next_sw_mins;
369
                sw_pph <= (stopwatch[23:16] == 8'h59)&&(sw_ppm);
370
 
371
                if ((sw_pps)&&(sw_pph)) // And hours
372
                        stopwatch[29:24] <= next_sw_hrs;
373
 
374
                if (sw_wr)
375
                begin
376
                        stopwatch[0] <= r_data[0];
377
                        if((r_data[1])&&((~stopwatch[0])||(~r_data[0])))
378
                        begin
379
                                stopwatch[31:1] <= 31'h00;
380
                                sw_sub <= 8'h00;
381
                                sw_pps <= 1'b0;
382
                                sw_ppm <= 1'b0;
383
                                sw_pph <= 1'b0;
384
                        end
385
                end
386
        end
387
 
388
        //
389
        // The alarm code
390
        //
391
        // Set the alarm register to the time you wish the board to "alarm".
392
        // The "alarm" will take place once per day at that time.  At that
393
        // time, the RTC code will generate a clock interrupt, and the CPU/host
394
        // can come and see that the alarm tripped.
395
        //
396
        // 
397
        reg     [21:0]           alarm_time;
398
        reg                     al_int,         // The alarm interrupt line
399
                                al_enabled,     // Whether the alarm is enabled
400
                                al_tripped;     // Whether the alarm has tripped
401
        initial al_enabled= 1'b0;
402
        initial al_tripped= 1'b0;
403
        always @(posedge i_clk)
404
        begin
405
                if (al_wr)
406
                begin
407
                        // Only adjust the alarm hours if the requested hours
408
                        // are valid.  This allows writes to the register,
409
                        // without a prior read, to leave these configuration
410
                        // bits alone.
411
                        if (r_data[21:20] != 2'h3)
412
                                alarm_time[21:16] <= i_wb_data[21:16];
413
                        // Here's the same thing for the minutes: only adjust
414
                        // the alarm minutes if the new bits are not all 1's. 
415
                        if (~r_data[15])
416
                                alarm_time[15:8] <= i_wb_data[15:8];
417
                        // Here's the same thing for the seconds: only adjust
418
                        // the alarm seconds if the new bits are not all 1's. 
419
                        if (~r_data[7])
420
                                alarm_time[7:0] <= i_wb_data[7:0];
421
                        al_enabled <= i_wb_data[24];
422
                        // Reset the alarm if a '1' is written to the tripped
423
                        // register, or if the alarm is disabled.
424
                        if ((r_data[25])||(~r_data[24]))
425
                                al_tripped <= 1'b0;
426
                end
427
 
428
                al_int <= ((ck_last_clock != alarm_time)
429
                                &&(clock[21:0] == alarm_time)&&(al_enabled));
430
                if (al_int)
431
                        al_tripped <= 1'b1;
432
        end
433
 
434
        //
435
        // The ckspeed register is equal to 2^48 divded by the number of
436
        // clock ticks you expect per second.  Adjust high for a slower
437
        // clock, lower for a faster clock.  In this fashion, a single
438
        // real time clock RTL file can handle tracking the clock in any
439
        // device.  Further, because this is only the lower 32 bits of a 
440
        // 48 bit counter per seconds, the clock jitter is kept below
441
        // 1 part in 65 thousand.
442
        //
443
        initial ckspeed = DEFAULT_SPEED;
444
        // In the case of verilator, comment the above and uncomment the line
445
        // below.  The clock constant below is "close" to simulation time,
446
        // meaning that my verilator simulation is running about 300x slower
447
        // than board time.
448
        // initial      ckspeed = 32'd786432000;
449
        always @(posedge i_clk)
450
                if (i_gps_valid)
451
                        ckspeed <= i_gps_ckspeed;
452
 
453
        assign  o_interrupt = tm_int || al_int;
454
 
455
        // A once-per day strobe, on the last second of the day so that the
456
        // the next clock is the first clock of the day.  This is useful for
457
        // connecting this module to a year/month/date date/calendar module.
458
        assign  o_ppd = (ck_ppd)&&(ck_pps);
459
 
460
        always @(posedge i_clk)
461
                case(i_wb_addr)
462
                2'b00: o_data <= { ~i_gps_valid, 7'h0, 2'b00, clock[21:0] };
463
                2'b01: o_data <= { 6'h00, timer };
464
                2'b10: o_data <= stopwatch;
465
                2'b11: o_data <= { 6'h00, al_tripped, al_enabled, 2'b00, alarm_time };
466
                endcase
467
 
468
endmodule

powered by: WebSVN 2.1.0

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