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

Subversion Repositories can

[/] [can/] [tags/] [rel_22/] [rtl/] [verilog/] [can_btl.v] - Blame information for rev 87

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 28 mohor
//// The CAN protocol is developed by Robert Bosch GmbH and       ////
44
//// protected by patents. Anybody who wants to implement this    ////
45
//// CAN IP core on silicon has to obtain a CAN protocol license  ////
46
//// from Bosch.                                                  ////
47
////                                                              ////
48 2 mohor
//////////////////////////////////////////////////////////////////////
49
//
50
// CVS Revision History
51
//
52
// $Log: not supported by cvs2svn $
53 87 mohor
// Revision 1.18  2003/06/17 15:53:33  mohor
54
// clk_cnt reduced from [8:0] to [6:0].
55
//
56 84 mohor
// Revision 1.17  2003/06/17 14:32:17  mohor
57
// Removed few signals.
58
//
59 82 mohor
// Revision 1.16  2003/06/16 13:57:58  mohor
60
// tx_point generated one clk earlier. rx_i registered. Data corrected when
61
// using extended mode.
62
//
63 78 mohor
// Revision 1.15  2003/06/13 15:02:24  mohor
64
// Synchronization is also needed when transmitting a message.
65
//
66 77 mohor
// Revision 1.14  2003/06/13 14:55:11  mohor
67
// Counters width changed.
68
//
69 76 mohor
// Revision 1.13  2003/06/11 14:21:35  mohor
70
// When switching to tx, sync stage is overjumped.
71
//
72 75 mohor
// Revision 1.12  2003/02/14 20:17:01  mohor
73
// Several registers added. Not finished, yet.
74
//
75 35 mohor
// Revision 1.11  2003/02/09 18:40:29  mohor
76
// Overload fixed. Hard synchronization also enabled at the last bit of
77
// interframe.
78
//
79 29 mohor
// Revision 1.10  2003/02/09 02:24:33  mohor
80
// Bosch license warning added. Error counters finished. Overload frames
81
// still need to be fixed.
82
//
83 28 mohor
// Revision 1.9  2003/01/31 01:13:38  mohor
84
// backup.
85
//
86 24 mohor
// Revision 1.8  2003/01/10 17:51:34  mohor
87
// Temporary version (backup).
88
//
89 15 mohor
// Revision 1.7  2003/01/08 02:10:53  mohor
90
// Acceptance filter added.
91
//
92 11 mohor
// Revision 1.6  2002/12/28 04:13:23  mohor
93
// Backup version.
94
//
95 10 mohor
// Revision 1.5  2002/12/27 00:12:52  mohor
96
// Header changed, testbench improved to send a frame (crc still missing).
97
//
98 9 mohor
// Revision 1.4  2002/12/26 01:33:05  mohor
99
// Tripple sampling supported.
100
//
101 7 mohor
// Revision 1.3  2002/12/25 23:44:16  mohor
102
// Commented lines removed.
103
//
104 6 mohor
// Revision 1.2  2002/12/25 14:17:00  mohor
105
// Synchronization working.
106
//
107 5 mohor
// Revision 1.1.1.1  2002/12/20 16:39:21  mohor
108
// Initial
109 2 mohor
//
110
//
111 5 mohor
//
112 2 mohor
 
113
// synopsys translate_off
114
`include "timescale.v"
115
// synopsys translate_on
116
`include "can_defines.v"
117
 
118
module can_btl
119
(
120
  clk,
121
  rst,
122
  rx,
123
 
124
  /* Mode register */
125 10 mohor
  reset_mode,
126 2 mohor
 
127
  /* Bus Timing 0 register */
128
  baud_r_presc,
129
  sync_jump_width,
130
 
131
  /* Bus Timing 1 register */
132
  time_segment1,
133
  time_segment2,
134
  triple_sampling,
135
 
136
  /* Output signals from this module */
137 10 mohor
  sample_point,
138
  sampled_bit,
139
  sampled_bit_q,
140 24 mohor
  tx_point,
141 11 mohor
  hard_sync,
142 2 mohor
 
143 10 mohor
  /* Output from can_bsp module */
144 24 mohor
  rx_idle,
145 29 mohor
  last_bit_of_inter
146 2 mohor
 
147 10 mohor
 
148 11 mohor
 
149 2 mohor
 
150
 
151
);
152
 
153
parameter Tp = 1;
154
 
155
input         clk;
156
input         rst;
157
input         rx;
158
 
159 10 mohor
  /* Mode register */
160 2 mohor
input         reset_mode;
161
 
162
/* Bus Timing 0 register */
163
input   [5:0] baud_r_presc;
164
input   [1:0] sync_jump_width;
165
 
166
/* Bus Timing 1 register */
167
input   [3:0] time_segment1;
168
input   [2:0] time_segment2;
169
input         triple_sampling;
170
 
171 10 mohor
/* Output from can_bsp module */
172
input         rx_idle;
173 29 mohor
input         last_bit_of_inter;
174 10 mohor
 
175 2 mohor
/* Output signals from this module */
176 10 mohor
output        sample_point;
177
output        sampled_bit;
178
output        sampled_bit_q;
179 24 mohor
output        tx_point;
180 11 mohor
output        hard_sync;
181 2 mohor
 
182
 
183
 
184 84 mohor
reg     [6:0] clk_cnt;
185 2 mohor
reg           clk_en;
186 78 mohor
reg           clk_en_q;
187 5 mohor
reg           sync_blocked;
188 24 mohor
reg           resync_blocked;
189 2 mohor
reg           sampled_bit;
190 10 mohor
reg           sampled_bit_q;
191 76 mohor
reg     [4:0] quant_cnt;
192 6 mohor
reg     [3:0] delay;
193
reg           sync;
194
reg           seg1;
195
reg           seg2;
196
reg           resync_latched;
197 10 mohor
reg           sample_point;
198 7 mohor
reg     [1:0] sample;
199 76 mohor
reg           go_sync;
200 2 mohor
 
201 76 mohor
wire          go_sync_unregistered;
202 6 mohor
wire          go_seg1;
203
wire          go_seg2;
204
wire [8:0]    preset_cnt;
205
wire          sync_window;
206 75 mohor
wire          resync;
207 82 mohor
wire          quant_cnt_rst;
208 2 mohor
 
209 5 mohor
 
210 76 mohor
 
211 6 mohor
assign preset_cnt = (baud_r_presc + 1'b1)<<1;        // (BRP+1)*2
212 77 mohor
assign hard_sync  =   (rx_idle | last_bit_of_inter)  & (~rx) & sampled_bit & (~sync_blocked);  // Hard synchronization
213
assign resync     =  (~rx_idle)                      & (~rx) & sampled_bit & (~sync_blocked) & (~resync_blocked);  // Re-synchronization
214 5 mohor
 
215
 
216 6 mohor
/* Generating general enable signal that defines baud rate. */
217 2 mohor
always @ (posedge clk or posedge rst)
218
begin
219
  if (rst)
220 10 mohor
    clk_cnt <= 0;
221 78 mohor
  else if (clk_cnt >= (preset_cnt-1'b1))
222 10 mohor
    clk_cnt <=#Tp 0;
223
  else
224 76 mohor
    clk_cnt <=#Tp clk_cnt + 1'b1;
225 10 mohor
end
226
 
227
 
228
always @ (posedge clk or posedge rst)
229
begin
230
  if (rst)
231
    clk_en  <= 1'b0;
232 76 mohor
  else if (clk_cnt == (preset_cnt-1'b1))
233 10 mohor
    clk_en  <=#Tp 1'b1;
234 2 mohor
  else
235 10 mohor
    clk_en  <=#Tp 1'b0;
236 2 mohor
end
237
 
238
 
239 5 mohor
 
240 78 mohor
always @ (posedge clk or posedge rst)
241
begin
242
  if (rst)
243
    clk_en_q  <= 1'b0;
244
  else
245
    clk_en_q  <=#Tp clk_en;
246
end
247
 
248
 
249
 
250 6 mohor
/* Changing states */
251 76 mohor
 assign go_sync_unregistered = clk_en & (seg2 & (~hard_sync) & (~resync) & ((quant_cnt[2:0] == time_segment2)));
252 78 mohor
 assign go_seg1 = clk_en_q & (sync | hard_sync | (resync & seg2 & sync_window) | (resync_latched & sync_window));
253
 assign go_seg2 = clk_en_q & (seg1 & (~hard_sync) & (quant_cnt == (time_segment1 + delay)));
254 5 mohor
 
255
 
256 76 mohor
always @ (posedge clk or posedge rst)
257
begin
258
  if (rst)
259
    go_sync <= 1'b0;
260
  else
261 87 mohor
    go_sync <=#Tp go_sync_unregistered  & (~hard_sync) & (~resync);
262 76 mohor
end
263
 
264
 
265 6 mohor
/* When early edge is detected outside of the SJW field, synchronization request is latched and performed when
266
   SJW is reached */
267 2 mohor
always @ (posedge clk or posedge rst)
268
begin
269
  if (rst)
270 5 mohor
    resync_latched <= 1'b0;
271 6 mohor
  else if (resync & seg2 & (~sync_window))
272 5 mohor
    resync_latched <=#Tp 1'b1;
273
  else if (go_seg1)
274
    resync_latched <= 1'b0;
275
end
276
 
277
 
278
 
279 6 mohor
/* Synchronization stage/segment */
280 5 mohor
always @ (posedge clk or posedge rst)
281
begin
282
  if (rst)
283 10 mohor
    sync <= 0;
284 5 mohor
  else if (go_sync)
285
    sync <=#Tp 1'b1;
286 78 mohor
  else if (clk_en_q)
287 5 mohor
    sync <=#Tp 1'b0;
288
end
289
 
290
 
291 24 mohor
assign tx_point = go_sync;
292
 
293 6 mohor
/* Seg1 stage/segment (together with propagation segment which is 1 quant long) */
294 5 mohor
always @ (posedge clk or posedge rst)
295
begin
296
  if (rst)
297 10 mohor
    seg1 <= 1;
298 5 mohor
  else if (go_seg1)
299
    seg1 <=#Tp 1'b1;
300
  else if (go_seg2)
301
    seg1 <=#Tp 1'b0;
302
end
303
 
304
 
305 6 mohor
/* Seg2 stage/segment */
306 5 mohor
always @ (posedge clk or posedge rst)
307
begin
308
  if (rst)
309
    seg2 <= 0;
310
  else if (go_seg2)
311
    seg2 <=#Tp 1'b1;
312
  else if (go_sync | go_seg1)
313
    seg2 <=#Tp 1'b0;
314
end
315
 
316
 
317 6 mohor
/* Quant counter */
318 82 mohor
assign quant_cnt_rst = go_sync | go_seg1 | go_seg2;
319 76 mohor
 
320 5 mohor
always @ (posedge clk or posedge rst)
321
begin
322
  if (rst)
323
    quant_cnt <= 0;
324 82 mohor
  else if (quant_cnt_rst)
325 5 mohor
    quant_cnt <=#Tp 0;
326 78 mohor
  else if (clk_en_q)
327 5 mohor
    quant_cnt <=#Tp quant_cnt + 1'b1;
328
end
329
 
330
 
331 6 mohor
/* When late edge is detected (in seg1 stage), stage seg1 is prolonged. */
332 5 mohor
always @ (posedge clk or posedge rst)
333
begin
334
  if (rst)
335 6 mohor
    delay <= 0;
336 78 mohor
  else if (clk_en_q & resync & seg1)
337 76 mohor
    delay <=#Tp (quant_cnt > {3'h0, sync_jump_width})? (sync_jump_width + 1'b1) : (quant_cnt + 1'b1);
338 5 mohor
  else if (go_sync | go_seg1)
339 6 mohor
    delay <=#Tp 0;
340 5 mohor
end
341
 
342
 
343 6 mohor
// If early edge appears within this window (in seg2 stage), phase error is fully compensated
344 76 mohor
assign sync_window = ((time_segment2 - quant_cnt[2:0]) < ( sync_jump_width + 1'b1));
345 5 mohor
 
346
 
347 7 mohor
// Sampling data (memorizing two samples all the time).
348 5 mohor
always @ (posedge clk or posedge rst)
349
begin
350
  if (rst)
351 7 mohor
    sample <= 2'b11;
352 78 mohor
  else if (clk_en_q)
353 7 mohor
    sample <= {sample[0], rx};
354
end
355
 
356
 
357
// When enabled, tripple sampling is done here.
358
always @ (posedge clk or posedge rst)
359
begin
360
  if (rst)
361 2 mohor
    begin
362
      sampled_bit <= 1;
363 10 mohor
      sampled_bit_q <= 1;
364
      sample_point <= 0;
365 2 mohor
    end
366 78 mohor
  else if (clk_en_q & (~hard_sync))
367 2 mohor
    begin
368 7 mohor
      if (seg1 & (quant_cnt == (time_segment1 + delay)))
369
        begin
370 10 mohor
          sample_point <=#Tp 1;
371
          sampled_bit_q <=#Tp sampled_bit;
372 7 mohor
          if (triple_sampling)
373
            sampled_bit <=#Tp (sample[0] & sample[1]) | ( sample[0] & rx) | (sample[1] & rx);
374
          else
375
            sampled_bit <=#Tp rx;
376
        end
377 2 mohor
    end
378 5 mohor
  else
379 10 mohor
    sample_point <=#Tp 0;
380 2 mohor
end
381
 
382
 
383
 
384 5 mohor
/* Blocking synchronization (can occur only once in a bit time) */
385 35 mohor
 
386 5 mohor
always @ (posedge clk or posedge rst)
387
begin
388
  if (rst)
389
    sync_blocked <=#Tp 1'b0;
390 78 mohor
  else if (clk_en_q)
391 5 mohor
    begin
392 35 mohor
      if (hard_sync | resync)
393 5 mohor
        sync_blocked <=#Tp 1'b1;
394 76 mohor
      else if (seg2 & (quant_cnt[2:0] == time_segment2))
395 5 mohor
        sync_blocked <=#Tp 1'b0;
396
    end
397
end
398 2 mohor
 
399
 
400 24 mohor
/* Blocking resynchronization until reception starts (needed because after reset mode exits we are waiting for
401
   end-of-frame and interframe. No resynchronization is needed meanwhile). */
402
always @ (posedge clk or posedge rst)
403
begin
404
  if (rst)
405
    resync_blocked <=#Tp 1'b1;
406
  else if (reset_mode)
407
    resync_blocked <=#Tp 1'b1;
408
  else if (hard_sync)
409
    resync_blocked <=#Tp 1'b0;
410
end
411 2 mohor
 
412
 
413 5 mohor
 
414 24 mohor
 
415
 
416 2 mohor
endmodule

powered by: WebSVN 2.1.0

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