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

Subversion Repositories can

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

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

powered by: WebSVN 2.1.0

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