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

Subversion Repositories can

[/] [can/] [trunk/] [rtl/] [verilog/] [can_btl.v] - Blame information for rev 137

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 137 mohor
//// Copyright (C) 2002, 2003, 2004 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 137 mohor
// Revision 1.27  2003/09/30 00:55:13  mohor
54
// Error counters fixed to be compatible with Bosch VHDL reference model.
55
// Small synchronization changes.
56
//
57 126 mohor
// Revision 1.26  2003/09/25 18:55:49  mohor
58
// Synchronization changed, error counters fixed.
59
//
60 125 mohor
// Revision 1.25  2003/07/16 13:40:35  mohor
61
// Fixed according to the linter.
62
//
63 108 mohor
// Revision 1.24  2003/07/10 15:32:28  mohor
64
// Unused signal removed.
65
//
66 106 mohor
// Revision 1.23  2003/07/10 01:59:04  tadejm
67
// Synchronization fixed. In some strange cases it didn't work according to
68
// the VHDL reference model.
69
//
70 104 tadejm
// Revision 1.22  2003/07/07 11:21:37  mohor
71
// Little fixes (to fix warnings).
72
//
73 102 mohor
// Revision 1.21  2003/07/03 09:32:20  mohor
74
// Synchronization changed.
75
//
76 100 mohor
// Revision 1.20  2003/06/20 14:51:11  mohor
77
// Previous change removed. When resynchronization occurs we go to seg1
78
// stage. sync stage does not cause another start of seg1 stage.
79
//
80 88 mohor
// Revision 1.19  2003/06/20 14:28:20  mohor
81
// When hard_sync or resync occure we need to go to seg1 segment. Going to
82
// sync segment is in that case blocked.
83
//
84 87 mohor
// Revision 1.18  2003/06/17 15:53:33  mohor
85
// clk_cnt reduced from [8:0] to [6:0].
86
//
87 84 mohor
// Revision 1.17  2003/06/17 14:32:17  mohor
88
// Removed few signals.
89
//
90 82 mohor
// Revision 1.16  2003/06/16 13:57:58  mohor
91
// tx_point generated one clk earlier. rx_i registered. Data corrected when
92
// using extended mode.
93
//
94 78 mohor
// Revision 1.15  2003/06/13 15:02:24  mohor
95
// Synchronization is also needed when transmitting a message.
96
//
97 77 mohor
// Revision 1.14  2003/06/13 14:55:11  mohor
98
// Counters width changed.
99
//
100 76 mohor
// Revision 1.13  2003/06/11 14:21:35  mohor
101
// When switching to tx, sync stage is overjumped.
102
//
103 75 mohor
// Revision 1.12  2003/02/14 20:17:01  mohor
104
// Several registers added. Not finished, yet.
105
//
106 35 mohor
// Revision 1.11  2003/02/09 18:40:29  mohor
107
// Overload fixed. Hard synchronization also enabled at the last bit of
108
// interframe.
109
//
110 29 mohor
// Revision 1.10  2003/02/09 02:24:33  mohor
111
// Bosch license warning added. Error counters finished. Overload frames
112
// still need to be fixed.
113
//
114 28 mohor
// Revision 1.9  2003/01/31 01:13:38  mohor
115
// backup.
116
//
117 24 mohor
// Revision 1.8  2003/01/10 17:51:34  mohor
118
// Temporary version (backup).
119
//
120 15 mohor
// Revision 1.7  2003/01/08 02:10:53  mohor
121
// Acceptance filter added.
122
//
123 11 mohor
// Revision 1.6  2002/12/28 04:13:23  mohor
124
// Backup version.
125
//
126 10 mohor
// Revision 1.5  2002/12/27 00:12:52  mohor
127
// Header changed, testbench improved to send a frame (crc still missing).
128
//
129 9 mohor
// Revision 1.4  2002/12/26 01:33:05  mohor
130
// Tripple sampling supported.
131
//
132 7 mohor
// Revision 1.3  2002/12/25 23:44:16  mohor
133
// Commented lines removed.
134
//
135 6 mohor
// Revision 1.2  2002/12/25 14:17:00  mohor
136
// Synchronization working.
137
//
138 5 mohor
// Revision 1.1.1.1  2002/12/20 16:39:21  mohor
139
// Initial
140 2 mohor
//
141
//
142 5 mohor
//
143 2 mohor
 
144
// synopsys translate_off
145
`include "timescale.v"
146
// synopsys translate_on
147
`include "can_defines.v"
148
 
149
module can_btl
150
(
151
  clk,
152
  rst,
153
  rx,
154 125 mohor
  tx,
155 2 mohor
 
156
  /* Bus Timing 0 register */
157
  baud_r_presc,
158
  sync_jump_width,
159
 
160
  /* Bus Timing 1 register */
161
  time_segment1,
162
  time_segment2,
163
  triple_sampling,
164
 
165
  /* Output signals from this module */
166 10 mohor
  sample_point,
167
  sampled_bit,
168
  sampled_bit_q,
169 24 mohor
  tx_point,
170 11 mohor
  hard_sync,
171 2 mohor
 
172 10 mohor
  /* Output from can_bsp module */
173 24 mohor
  rx_idle,
174 126 mohor
  rx_inter,
175 125 mohor
  transmitting,
176
  transmitter,
177
  go_rx_inter,
178
  tx_next,
179 2 mohor
 
180 125 mohor
  go_overload_frame,
181
  go_error_frame,
182
  go_tx,
183
  send_ack,
184
  node_error_passive
185 2 mohor
);
186
 
187
parameter Tp = 1;
188
 
189
input         clk;
190
input         rst;
191
input         rx;
192 125 mohor
input         tx;
193 2 mohor
 
194
 
195
/* Bus Timing 0 register */
196
input   [5:0] baud_r_presc;
197
input   [1:0] sync_jump_width;
198
 
199
/* Bus Timing 1 register */
200
input   [3:0] time_segment1;
201
input   [2:0] time_segment2;
202
input         triple_sampling;
203
 
204 10 mohor
/* Output from can_bsp module */
205
input         rx_idle;
206 126 mohor
input         rx_inter;
207 100 mohor
input         transmitting;
208 125 mohor
input         transmitter;
209
input         go_rx_inter;
210
input         tx_next;
211 10 mohor
 
212 125 mohor
input         go_overload_frame;
213
input         go_error_frame;
214
input         go_tx;
215
input         send_ack;
216
input         node_error_passive;
217
 
218 2 mohor
/* Output signals from this module */
219 10 mohor
output        sample_point;
220
output        sampled_bit;
221
output        sampled_bit_q;
222 24 mohor
output        tx_point;
223 11 mohor
output        hard_sync;
224 2 mohor
 
225
 
226
 
227 84 mohor
reg     [6:0] clk_cnt;
228 2 mohor
reg           clk_en;
229 78 mohor
reg           clk_en_q;
230 5 mohor
reg           sync_blocked;
231 100 mohor
reg           hard_sync_blocked;
232 2 mohor
reg           sampled_bit;
233 10 mohor
reg           sampled_bit_q;
234 108 mohor
reg     [3:0] quant_cnt;
235 6 mohor
reg     [3:0] delay;
236
reg           sync;
237
reg           seg1;
238
reg           seg2;
239
reg           resync_latched;
240 10 mohor
reg           sample_point;
241 7 mohor
reg     [1:0] sample;
242 100 mohor
reg           tx_point;
243 125 mohor
reg           tx_next_sp;
244 2 mohor
 
245 125 mohor
wire          go_sync;
246
wire          go_seg1;
247
wire          go_seg2;
248 108 mohor
wire [7:0]    preset_cnt;
249 6 mohor
wire          sync_window;
250 75 mohor
wire          resync;
251 2 mohor
 
252 5 mohor
 
253 76 mohor
 
254 6 mohor
assign preset_cnt = (baud_r_presc + 1'b1)<<1;        // (BRP+1)*2
255 126 mohor
assign hard_sync  =   (rx_idle | rx_inter)    & (~rx) & sampled_bit & (~hard_sync_blocked);  // Hard synchronization
256
assign resync     =  (~rx_idle) & (~rx_inter) & (~rx) & sampled_bit & (~sync_blocked);       // Re-synchronization
257 5 mohor
 
258
 
259 6 mohor
/* Generating general enable signal that defines baud rate. */
260 2 mohor
always @ (posedge clk or posedge rst)
261
begin
262
  if (rst)
263 108 mohor
    clk_cnt <= 7'h0;
264 78 mohor
  else if (clk_cnt >= (preset_cnt-1'b1))
265 108 mohor
    clk_cnt <=#Tp 7'h0;
266 10 mohor
  else
267 76 mohor
    clk_cnt <=#Tp clk_cnt + 1'b1;
268 10 mohor
end
269
 
270
 
271
always @ (posedge clk or posedge rst)
272
begin
273
  if (rst)
274
    clk_en  <= 1'b0;
275 108 mohor
  else if ({1'b0, clk_cnt} == (preset_cnt-1'b1))
276 10 mohor
    clk_en  <=#Tp 1'b1;
277 2 mohor
  else
278 10 mohor
    clk_en  <=#Tp 1'b0;
279 2 mohor
end
280
 
281
 
282 5 mohor
 
283 78 mohor
always @ (posedge clk or posedge rst)
284
begin
285
  if (rst)
286
    clk_en_q  <= 1'b0;
287
  else
288
    clk_en_q  <=#Tp clk_en;
289
end
290
 
291
 
292
 
293 6 mohor
/* Changing states */
294 125 mohor
assign go_sync = clk_en_q & seg2 & (quant_cnt[2:0] == time_segment2) & (~hard_sync) & (~resync);
295
assign go_seg1 = clk_en_q & (sync | hard_sync | (resync & seg2 & sync_window) | (resync_latched & sync_window));
296
assign go_seg2 = clk_en_q & (seg1 & (~hard_sync) & (quant_cnt == (time_segment1 + delay)));
297 5 mohor
 
298
 
299 76 mohor
 
300 100 mohor
always @ (posedge clk or posedge rst)
301
begin
302
  if (rst)
303
    tx_point <= 1'b0;
304
  else
305 125 mohor
    tx_point <=#Tp ~tx_point & seg2 & (  clk_en & (quant_cnt[2:0] == time_segment2)
306
                                       | clk_en_q & (resync | hard_sync)
307
                                      );    // When transmitter we should transmit as soon as possible.
308 100 mohor
end
309
 
310
 
311 6 mohor
/* When early edge is detected outside of the SJW field, synchronization request is latched and performed when
312
   SJW is reached */
313 2 mohor
always @ (posedge clk or posedge rst)
314
begin
315
  if (rst)
316 5 mohor
    resync_latched <= 1'b0;
317 6 mohor
  else if (resync & seg2 & (~sync_window))
318 5 mohor
    resync_latched <=#Tp 1'b1;
319
  else if (go_seg1)
320
    resync_latched <= 1'b0;
321
end
322
 
323
 
324
 
325 6 mohor
/* Synchronization stage/segment */
326 5 mohor
always @ (posedge clk or posedge rst)
327
begin
328
  if (rst)
329 108 mohor
    sync <= 1'b0;
330 125 mohor
  else if (clk_en_q)
331
    sync <=#Tp go_sync;
332 5 mohor
end
333
 
334
 
335 6 mohor
/* Seg1 stage/segment (together with propagation segment which is 1 quant long) */
336 5 mohor
always @ (posedge clk or posedge rst)
337
begin
338
  if (rst)
339 108 mohor
    seg1 <= 1'b1;
340 5 mohor
  else if (go_seg1)
341
    seg1 <=#Tp 1'b1;
342
  else if (go_seg2)
343
    seg1 <=#Tp 1'b0;
344
end
345
 
346
 
347 6 mohor
/* Seg2 stage/segment */
348 5 mohor
always @ (posedge clk or posedge rst)
349
begin
350
  if (rst)
351 108 mohor
    seg2 <= 1'b0;
352 5 mohor
  else if (go_seg2)
353
    seg2 <=#Tp 1'b1;
354
  else if (go_sync | go_seg1)
355
    seg2 <=#Tp 1'b0;
356
end
357
 
358
 
359 6 mohor
/* Quant counter */
360 5 mohor
always @ (posedge clk or posedge rst)
361
begin
362
  if (rst)
363 108 mohor
    quant_cnt <= 4'h0;
364
  else if (go_sync | go_seg1 | go_seg2)
365
    quant_cnt <=#Tp 4'h0;
366 78 mohor
  else if (clk_en_q)
367 5 mohor
    quant_cnt <=#Tp quant_cnt + 1'b1;
368
end
369
 
370
 
371 6 mohor
/* When late edge is detected (in seg1 stage), stage seg1 is prolonged. */
372 5 mohor
always @ (posedge clk or posedge rst)
373
begin
374
  if (rst)
375 108 mohor
    delay <= 4'h0;
376 125 mohor
  else if (resync & seg1 & (~transmitting | transmitting & (tx_next_sp | (tx & (~rx)))))  // when transmitting 0 with positive error delay is set to 0
377 108 mohor
    delay <=#Tp (quant_cnt > {2'h0, sync_jump_width})? ({2'h0, sync_jump_width} + 1'b1) : (quant_cnt + 1'b1);
378 5 mohor
  else if (go_sync | go_seg1)
379 108 mohor
    delay <=#Tp 4'h0;
380 5 mohor
end
381
 
382
 
383 6 mohor
// If early edge appears within this window (in seg2 stage), phase error is fully compensated
384 76 mohor
assign sync_window = ((time_segment2 - quant_cnt[2:0]) < ( sync_jump_width + 1'b1));
385 5 mohor
 
386
 
387 7 mohor
// Sampling data (memorizing two samples all the time).
388 5 mohor
always @ (posedge clk or posedge rst)
389
begin
390
  if (rst)
391 7 mohor
    sample <= 2'b11;
392 78 mohor
  else if (clk_en_q)
393 7 mohor
    sample <= {sample[0], rx};
394
end
395
 
396
 
397
// When enabled, tripple sampling is done here.
398
always @ (posedge clk or posedge rst)
399
begin
400
  if (rst)
401 2 mohor
    begin
402 108 mohor
      sampled_bit <= 1'b1;
403
      sampled_bit_q <= 1'b1;
404
      sample_point <= 1'b0;
405 2 mohor
    end
406 78 mohor
  else if (clk_en_q & (~hard_sync))
407 2 mohor
    begin
408 7 mohor
      if (seg1 & (quant_cnt == (time_segment1 + delay)))
409
        begin
410 108 mohor
          sample_point <=#Tp 1'b1;
411 10 mohor
          sampled_bit_q <=#Tp sampled_bit;
412 7 mohor
          if (triple_sampling)
413
            sampled_bit <=#Tp (sample[0] & sample[1]) | ( sample[0] & rx) | (sample[1] & rx);
414
          else
415
            sampled_bit <=#Tp rx;
416
        end
417 2 mohor
    end
418 5 mohor
  else
419 108 mohor
    sample_point <=#Tp 1'b0;
420 2 mohor
end
421
 
422
 
423 125 mohor
// tx_next_sp shows next value that will be driven on the TX. When driving 1 and receiving 0 we
424
// need to synchronize (even when we are a transmitter)
425
always @ (posedge clk or posedge rst)
426
begin
427
  if (rst)
428
    tx_next_sp <= 1'b0;
429
  else if (go_overload_frame | (go_error_frame & (~node_error_passive)) | go_tx | send_ack)
430
    tx_next_sp <=#Tp 1'b0;
431
  else if (go_error_frame & node_error_passive)
432
    tx_next_sp <=#Tp 1'b1;
433
  else if (sample_point)
434
    tx_next_sp <=#Tp tx_next;
435
end
436 2 mohor
 
437 125 mohor
 
438
 
439 5 mohor
/* Blocking synchronization (can occur only once in a bit time) */
440 35 mohor
 
441 5 mohor
always @ (posedge clk or posedge rst)
442
begin
443
  if (rst)
444 104 tadejm
    sync_blocked <=#Tp 1'b1;
445 78 mohor
  else if (clk_en_q)
446 5 mohor
    begin
447 100 mohor
      if (resync)
448 5 mohor
        sync_blocked <=#Tp 1'b1;
449 104 tadejm
      else if (go_seg2)
450 5 mohor
        sync_blocked <=#Tp 1'b0;
451
    end
452
end
453 2 mohor
 
454
 
455 100 mohor
/* Blocking hard synchronization when occurs once or when we are transmitting a msg */
456 24 mohor
always @ (posedge clk or posedge rst)
457
begin
458
  if (rst)
459 100 mohor
    hard_sync_blocked <=#Tp 1'b0;
460 126 mohor
  else if (hard_sync & clk_en_q | transmitting & transmitter & tx_point & (~tx_next))
461 100 mohor
    hard_sync_blocked <=#Tp 1'b1;
462 126 mohor
  else if (go_rx_inter | (rx_idle | rx_inter) & sample_point & sampled_bit)  // When a glitch performed synchronization
463 100 mohor
    hard_sync_blocked <=#Tp 1'b0;
464 24 mohor
end
465 2 mohor
 
466
 
467 5 mohor
 
468 24 mohor
 
469
 
470 2 mohor
endmodule

powered by: WebSVN 2.1.0

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