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

Subversion Repositories altor32

[/] [altor32/] [trunk/] [rtl/] [peripheral/] [timer_periph.v] - Blame information for rev 29

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

Line No. Rev Author Line
1 27 ultra_embe
//-----------------------------------------------------------------
2
//                           AltOR32 
3
//                Alternative Lightweight OpenRisc 
4
//                            V2.0
5
//                     Ultra-Embedded.com
6
//                   Copyright 2011 - 2013
7
//
8
//               Email: admin@ultra-embedded.com
9
//
10
//                       License: LGPL
11
//-----------------------------------------------------------------
12
//
13
// Copyright (C) 2011 - 2013 Ultra-Embedded.com
14
//
15
// This source file may be used and distributed without         
16
// restriction provided that this copyright statement is not    
17
// removed from the file and that any derivative work contains  
18
// the original copyright notice and the associated disclaimer. 
19
//
20
// This source file is free software; you can redistribute it   
21
// and/or modify it under the terms of the GNU Lesser General   
22
// Public License as published by the Free Software Foundation; 
23
// either version 2.1 of the License, or (at your option) any   
24
// later version.
25
//
26
// This source is distributed in the hope that it will be       
27
// useful, but WITHOUT ANY WARRANTY; without even the implied   
28
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      
29
// PURPOSE.  See the GNU Lesser General Public License for more 
30
// details.
31
//
32
// You should have received a copy of the GNU Lesser General    
33
// Public License along with this source; if not, write to the 
34
// Free Software Foundation, Inc., 59 Temple Place, Suite 330, 
35
// Boston, MA  02111-1307  USA
36
//-----------------------------------------------------------------
37
 
38
//-----------------------------------------------------------------
39
// Includes
40
//-----------------------------------------------------------------
41
`include "timer_defs.v"
42
 
43
//-----------------------------------------------------------------
44
// Module:
45
//-----------------------------------------------------------------
46
module timer_periph
47
(
48
    // General - Clocking & Reset
49
    clk_i,
50
    rst_i,
51
 
52
    // Interrupts
53
    intr_systick_o,
54
    intr_hires_o,
55
 
56
    // Peripheral bus
57
    addr_i,
58
    data_o,
59
    data_i,
60
    wr_i,
61
    rd_i
62
);
63
 
64
//-----------------------------------------------------------------
65
// Params
66
//-----------------------------------------------------------------
67
parameter  [31:0]   CLK_KHZ                = 12288;
68
parameter           SYSTICK_INTR_MS        = 1;
69
parameter           ENABLE_SYSTICK_TIMER   = "ENABLED";
70
parameter           ENABLE_HIGHRES_TIMER   = "ENABLED";
71
 
72
//-----------------------------------------------------------------
73
// I/O
74
//-----------------------------------------------------------------
75
input               clk_i /*verilator public*/;
76
input               rst_i /*verilator public*/;
77
 
78
output              intr_systick_o /*verilator public*/;
79
output              intr_hires_o /*verilator public*/;
80
 
81
input [7:0]         addr_i /*verilator public*/;
82
output [31:0]       data_o /*verilator public*/;
83
input [31:0]        data_i /*verilator public*/;
84
input [3:0]         wr_i /*verilator public*/;
85
input               rd_i /*verilator public*/;
86
 
87
//-----------------------------------------------------------------
88
// Registers / Wires
89
//-----------------------------------------------------------------
90
 
91
reg [31:0]          data_o;
92
 
93
// Systick Timer
94
reg                 systick_event;
95
reg [31:0]          systick_count;
96
reg [31:0]          systick_clk_count;
97
 
98
// Hi-res system clock tick counter
99
reg                 hr_timer_intr;
100
reg [31:0]          hr_timer_cnt;
101
reg [31:0]          hr_timer_match;
102
 
103
//-----------------------------------------------------------------
104
// Systick
105
//-----------------------------------------------------------------
106
generate
107
if (ENABLE_SYSTICK_TIMER == "ENABLED")
108
begin
109
 
110
    // SysTick Timer (1 ms resolution)
111
    always @ (posedge rst_i or posedge clk_i )
112
    begin
113
        if (rst_i == 1'b1)
114
        begin
115
            systick_count        <= 32'h00000000;
116
            systick_clk_count    <= 32'h00000000;
117
            systick_event        <= 1'b0;
118
        end
119
        else
120
        begin
121
            systick_event         <= 1'b0;
122
 
123
            if (systick_clk_count == CLK_KHZ)
124
            begin
125
                systick_count     <= (systick_count + 1);
126
                systick_event     <= 1'b1;
127
                systick_clk_count <= 32'h00000000;
128
            end
129
            else
130
                systick_clk_count <= (systick_clk_count + 1);
131
        end
132
    end
133
 
134
    // SysTick Interrupt
135
    integer systick_event_count;
136
    reg     systick_event_intr;
137
 
138
    always @ (posedge rst_i or posedge clk_i )
139
    begin
140
        if (rst_i == 1'b1)
141
        begin
142
            systick_event_count  <= 0;
143
            systick_event_intr   <= 1'b0;
144
        end
145
        else
146
        begin
147
            systick_event_intr  <= 1'b0;
148
 
149
            if (systick_event)
150
            begin
151
                systick_event_count <= (systick_event_count + 1);
152
 
153
                if (systick_event_count == (SYSTICK_INTR_MS-1))
154
                begin
155
                    systick_event_intr  <= 1'b1;
156
                    systick_event_count <= 0;
157
                end
158
            end
159
        end
160
    end
161
 
162
    assign intr_systick_o = systick_event_intr;
163
end
164
else
165
begin
166
    // Systick disabled
167
    always @ (posedge rst_i or posedge clk_i )
168
    begin
169
        if (rst_i == 1'b1)
170
            systick_count   <= 32'h00000000;
171
        else
172
            systick_count   <= 32'h00000000;
173
    end
174
 
175
    assign  intr_systick_o  = 1'b0;
176
end
177
endgenerate
178
 
179
//-----------------------------------------------------------------
180
// Hi Resolution Timer
181
//-----------------------------------------------------------------
182
generate
183
if (ENABLE_HIGHRES_TIMER == "ENABLED")
184
begin
185
 
186
    always @ (posedge rst_i or posedge clk_i)
187
    begin
188
        if (rst_i == 1'b1)
189
        begin
190
            hr_timer_cnt     <= 32'h00000000;
191
            hr_timer_intr    <= 1'b0;
192
        end
193
        else
194
        begin
195
            hr_timer_intr   <= 1'b0;
196
 
197
            // Clock tick counter
198
            hr_timer_cnt    <= (hr_timer_cnt + 1);
199
 
200
            // Hi-res Timer IRQ
201
            if ((hr_timer_match != 32'h00000000) && (hr_timer_match == hr_timer_cnt))
202
                hr_timer_intr   <= 1'b1;
203
        end
204
    end
205
 
206
    assign intr_hires_o        = hr_timer_intr;
207
end
208
else
209
begin
210
    // Hi resolution timer disabled
211
    always @ (posedge rst_i or posedge clk_i )
212
    begin
213
        if (rst_i == 1'b1)
214
            hr_timer_cnt   <= 32'h00000000;
215
        else
216
            hr_timer_cnt   <= 32'h00000000;
217
    end
218
 
219
    assign intr_hires_o     = 1'b0;
220
end
221
endgenerate
222
//-----------------------------------------------------------------
223
// Peripheral Register Write
224
//-----------------------------------------------------------------
225
always @ (posedge rst_i or posedge clk_i )
226
begin
227
   if (rst_i == 1'b1)
228
   begin
229
       hr_timer_match   <= 32'h00000000;
230
   end
231
   else
232
   begin
233
       // Write Cycle
234
       if (wr_i != 4'b0000)
235
       begin
236
           case (addr_i)
237
 
238
           `TIMER_HIRES :
239
                hr_timer_match <= data_i;
240
 
241
           default :
242
               ;
243
           endcase
244
        end
245
   end
246
end
247
 
248
//-----------------------------------------------------------------
249
// Peripheral Register Read
250
//-----------------------------------------------------------------
251
always @ (posedge rst_i or posedge clk_i )
252
begin
253
   if (rst_i == 1'b1)
254
   begin
255
       data_o       <= 32'h00000000;
256
   end
257
   else
258
   begin
259
       // Read cycle?
260
       if (rd_i == 1'b1)
261
       begin
262
           case (addr_i[7:0])
263
 
264
           // 32-bit systick/1ms counter
265
           `TIMER_SYSTICK_VAL :
266
                data_o <= systick_count;
267
 
268
           // Hi res timer (clock rate)
269
           `TIMER_HIRES :
270
                data_o <= hr_timer_cnt;
271
 
272
           default :
273
                data_o <= 32'h00000000;
274
           endcase
275
        end
276
   end
277
end
278
 
279
endmodule

powered by: WebSVN 2.1.0

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