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

Subversion Repositories openarty

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

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

powered by: WebSVN 2.1.0

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