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

Subversion Repositories can

[/] [can/] [tags/] [rel_10/] [rtl/] [verilog/] [can_btl.v] - Blame information for rev 10

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

Line No. Rev Author Line
1 2 mohor
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  can_btl.v                                                   ////
4
////                                                              ////
5
////                                                              ////
6 9 mohor
////  This file is part of the CAN Protocol Controller            ////
7 2 mohor
////  http://www.opencores.org/projects/can/                      ////
8
////                                                              ////
9
////                                                              ////
10
////  Author(s):                                                  ////
11
////       Igor Mohor                                             ////
12
////       igorm@opencores.org                                    ////
13
////                                                              ////
14
////                                                              ////
15 9 mohor
////  All additional information is available in the README.txt   ////
16 2 mohor
////  file.                                                       ////
17
////                                                              ////
18
//////////////////////////////////////////////////////////////////////
19
////                                                              ////
20 9 mohor
//// Copyright (C) 2002, 2003 Authors                             ////
21 2 mohor
////                                                              ////
22
//// This source file may be used and distributed without         ////
23
//// restriction provided that this copyright statement is not    ////
24
//// removed from the file and that any derivative work contains  ////
25
//// the original copyright notice and the associated disclaimer. ////
26
////                                                              ////
27
//// This source file is free software; you can redistribute it   ////
28
//// and/or modify it under the terms of the GNU Lesser General   ////
29
//// Public License as published by the Free Software Foundation; ////
30
//// either version 2.1 of the License, or (at your option) any   ////
31
//// later version.                                               ////
32
////                                                              ////
33
//// This source is distributed in the hope that it will be       ////
34
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
35
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
36
//// PURPOSE.  See the GNU Lesser General Public License for more ////
37
//// details.                                                     ////
38
////                                                              ////
39
//// You should have received a copy of the GNU Lesser General    ////
40
//// Public License along with this source; if not, download it   ////
41
//// from http://www.opencores.org/lgpl.shtml                     ////
42
////                                                              ////
43
//////////////////////////////////////////////////////////////////////
44
//
45
// CVS Revision History
46
//
47
// $Log: not supported by cvs2svn $
48 10 mohor
// Revision 1.5  2002/12/27 00:12:52  mohor
49
// Header changed, testbench improved to send a frame (crc still missing).
50
//
51 9 mohor
// Revision 1.4  2002/12/26 01:33:05  mohor
52
// Tripple sampling supported.
53
//
54 7 mohor
// Revision 1.3  2002/12/25 23:44:16  mohor
55
// Commented lines removed.
56
//
57 6 mohor
// Revision 1.2  2002/12/25 14:17:00  mohor
58
// Synchronization working.
59
//
60 5 mohor
// Revision 1.1.1.1  2002/12/20 16:39:21  mohor
61
// Initial
62 2 mohor
//
63
//
64 5 mohor
//
65 2 mohor
 
66
// synopsys translate_off
67
`include "timescale.v"
68
// synopsys translate_on
69
`include "can_defines.v"
70
 
71
module can_btl
72
(
73
  clk,
74
  rst,
75
  rx,
76
 
77
  /* Mode register */
78 10 mohor
  reset_mode,
79 2 mohor
 
80
  /* Bus Timing 0 register */
81
  baud_r_presc,
82
  sync_jump_width,
83
 
84
  /* Bus Timing 1 register */
85
  time_segment1,
86
  time_segment2,
87
  triple_sampling,
88
 
89
  /* Output signals from this module */
90
  clk_en,
91 10 mohor
  sample_point,
92
  sampled_bit,
93
  sampled_bit_q,
94 2 mohor
 
95 10 mohor
  /* Output from can_bsp module */
96
  rx_idle
97 2 mohor
 
98 10 mohor
 
99 2 mohor
 
100
 
101
);
102
 
103
parameter Tp = 1;
104
 
105
input         clk;
106
input         rst;
107
input         rx;
108
 
109 10 mohor
  /* Mode register */
110 2 mohor
input         reset_mode;
111
 
112
/* Bus Timing 0 register */
113
input   [5:0] baud_r_presc;
114
input   [1:0] sync_jump_width;
115
 
116
/* Bus Timing 1 register */
117
input   [3:0] time_segment1;
118
input   [2:0] time_segment2;
119
input         triple_sampling;
120
 
121 10 mohor
/* Output from can_bsp module */
122
input         rx_idle;
123
 
124 2 mohor
/* Output signals from this module */
125
output        clk_en;
126 10 mohor
output        sample_point;
127
output        sampled_bit;
128
output        sampled_bit_q;
129 2 mohor
 
130
 
131
 
132
reg     [8:0] clk_cnt;
133
reg           clk_en;
134 5 mohor
reg           sync_blocked;
135 2 mohor
reg           sampled_bit;
136 10 mohor
reg           sampled_bit_q;
137 2 mohor
reg     [7:0] quant_cnt;
138 6 mohor
reg     [3:0] delay;
139
reg           sync;
140
reg           seg1;
141
reg           seg2;
142
reg           resync_latched;
143 10 mohor
reg           sample_point;
144 7 mohor
reg     [1:0] sample;
145 2 mohor
 
146 6 mohor
wire          go_sync;
147
wire          go_seg1;
148
wire          go_seg2;
149
wire [8:0]    preset_cnt;
150
wire          hard_sync;
151
wire          resync;
152
wire          sync_window;
153 2 mohor
 
154 5 mohor
 
155
 
156 6 mohor
assign preset_cnt = (baud_r_presc + 1'b1)<<1;        // (BRP+1)*2
157 10 mohor
assign hard_sync  =  rx_idle   & (~rx) & sampled_bit & (~sync_blocked);  // Hard synchronization
158
assign resync     = (~rx_idle) & (~rx) & sampled_bit & (~sync_blocked);  // Re-synchronization
159 5 mohor
 
160
 
161 6 mohor
/* Generating general enable signal that defines baud rate. */
162 2 mohor
always @ (posedge clk or posedge rst)
163
begin
164
  if (rst)
165 10 mohor
    clk_cnt <= 0;
166
  else if (clk_cnt == (preset_cnt-1) | reset_mode)
167
    clk_cnt <=#Tp 0;
168
  else
169
    clk_cnt <=#Tp clk_cnt + 1;
170
end
171
 
172
 
173
always @ (posedge clk or posedge rst)
174
begin
175
  if (rst)
176
    clk_en  <= 1'b0;
177 2 mohor
  else if (clk_cnt == (preset_cnt-1))
178 10 mohor
    clk_en  <=#Tp 1'b1;
179 2 mohor
  else
180 10 mohor
    clk_en  <=#Tp 1'b0;
181 2 mohor
end
182
 
183
 
184 5 mohor
 
185 6 mohor
/* Changing states */
186 10 mohor
 assign go_sync = clk_en & (seg2 & (~hard_sync) & (~resync) & ((quant_cnt == time_segment2)));
187
 assign go_seg1 = clk_en & (sync | hard_sync | (resync & seg2 & sync_window) | (resync_latched & sync_window));
188
 assign go_seg2 = clk_en & (seg1 & (~hard_sync) & (quant_cnt == (time_segment1 + delay)));
189 5 mohor
 
190
 
191 10 mohor
 
192 6 mohor
/* When early edge is detected outside of the SJW field, synchronization request is latched and performed when
193
   SJW is reached */
194 2 mohor
always @ (posedge clk or posedge rst)
195
begin
196
  if (rst)
197 5 mohor
    resync_latched <= 1'b0;
198 6 mohor
  else if (resync & seg2 & (~sync_window))
199 5 mohor
    resync_latched <=#Tp 1'b1;
200
  else if (go_seg1)
201
    resync_latched <= 1'b0;
202
end
203
 
204
 
205
 
206 6 mohor
/* Synchronization stage/segment */
207 5 mohor
always @ (posedge clk or posedge rst)
208
begin
209
  if (rst)
210 10 mohor
    sync <= 0;
211 5 mohor
  else if (go_sync)
212
    sync <=#Tp 1'b1;
213
  else if (go_seg1)
214
    sync <=#Tp 1'b0;
215
end
216
 
217
 
218 6 mohor
/* Seg1 stage/segment (together with propagation segment which is 1 quant long) */
219 5 mohor
always @ (posedge clk or posedge rst)
220
begin
221
  if (rst)
222 10 mohor
    seg1 <= 1;
223 5 mohor
  else if (go_seg1)
224
    seg1 <=#Tp 1'b1;
225
  else if (go_seg2)
226
    seg1 <=#Tp 1'b0;
227
end
228
 
229
 
230 6 mohor
/* Seg2 stage/segment */
231 5 mohor
always @ (posedge clk or posedge rst)
232
begin
233
  if (rst)
234
    seg2 <= 0;
235
  else if (go_seg2)
236
    seg2 <=#Tp 1'b1;
237
  else if (go_sync | go_seg1)
238
    seg2 <=#Tp 1'b0;
239
end
240
 
241
 
242 6 mohor
/* Quant counter */
243 5 mohor
always @ (posedge clk or posedge rst)
244
begin
245
  if (rst)
246
    quant_cnt <= 0;
247
  else if (go_sync || go_seg1 || go_seg2)
248
    quant_cnt <=#Tp 0;
249
  else if (clk_en)
250
    quant_cnt <=#Tp quant_cnt + 1'b1;
251
end
252
 
253
 
254 6 mohor
/* When late edge is detected (in seg1 stage), stage seg1 is prolonged. */
255 5 mohor
always @ (posedge clk or posedge rst)
256
begin
257
  if (rst)
258 6 mohor
    delay <= 0;
259 5 mohor
  else if (clk_en & resync & seg1)
260 6 mohor
    delay <=#Tp (quant_cnt > sync_jump_width)? (sync_jump_width + 1) : (quant_cnt + 1);
261 5 mohor
  else if (go_sync | go_seg1)
262 6 mohor
    delay <=#Tp 0;
263 5 mohor
end
264
 
265
 
266 6 mohor
// If early edge appears within this window (in seg2 stage), phase error is fully compensated
267
assign sync_window = ((time_segment2 - quant_cnt) < ( sync_jump_width + 1));
268 5 mohor
 
269
 
270 7 mohor
// Sampling data (memorizing two samples all the time).
271 5 mohor
always @ (posedge clk or posedge rst)
272
begin
273
  if (rst)
274 7 mohor
    sample <= 2'b11;
275
  else if (clk_en)
276
    sample <= {sample[0], rx};
277
end
278
 
279
 
280
// When enabled, tripple sampling is done here.
281
always @ (posedge clk or posedge rst)
282
begin
283
  if (rst)
284 2 mohor
    begin
285
      sampled_bit <= 1;
286 10 mohor
      sampled_bit_q <= 1;
287
      sample_point <= 0;
288 2 mohor
    end
289 7 mohor
  else if (clk_en)
290 2 mohor
    begin
291 7 mohor
      if (seg1 & (quant_cnt == (time_segment1 + delay)))
292
        begin
293 10 mohor
          sample_point <=#Tp 1;
294
          sampled_bit_q <=#Tp sampled_bit;
295 7 mohor
          if (triple_sampling)
296
            sampled_bit <=#Tp (sample[0] & sample[1]) | ( sample[0] & rx) | (sample[1] & rx);
297
          else
298
            sampled_bit <=#Tp rx;
299
        end
300 2 mohor
    end
301 5 mohor
  else
302 10 mohor
    sample_point <=#Tp 0;
303 2 mohor
end
304
 
305
 
306
 
307 5 mohor
/* Blocking synchronization (can occur only once in a bit time) */
308
always @ (posedge clk or posedge rst)
309
begin
310
  if (rst)
311
    sync_blocked <=#Tp 1'b0;
312
  else if (clk_en)
313
    begin
314
      if (hard_sync || resync)
315
        sync_blocked <=#Tp 1'b1;
316
      else if (seg2 & quant_cnt == time_segment2)
317
        sync_blocked <=#Tp 1'b0;
318
    end
319
end
320 2 mohor
 
321
 
322
 
323
 
324 5 mohor
 
325 2 mohor
endmodule

powered by: WebSVN 2.1.0

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