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

Subversion Repositories can

[/] [can/] [tags/] [asyst_2/] [rtl/] [verilog/] [can_btl.v] - Blame information for rev 126

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