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

Subversion Repositories openarty

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

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

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

powered by: WebSVN 2.1.0

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