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

Subversion Repositories can

[/] [can/] [tags/] [rel_16/] [rtl/] [verilog/] [can_btl.v] - Blame information for rev 161

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 108 mohor
// Revision 1.24  2003/07/10 15:32:28  mohor
54
// Unused signal removed.
55
//
56 106 mohor
// Revision 1.23  2003/07/10 01:59:04  tadejm
57
// Synchronization fixed. In some strange cases it didn't work according to
58
// the VHDL reference model.
59
//
60 104 tadejm
// Revision 1.22  2003/07/07 11:21:37  mohor
61
// Little fixes (to fix warnings).
62
//
63 102 mohor
// Revision 1.21  2003/07/03 09:32:20  mohor
64
// Synchronization changed.
65
//
66 100 mohor
// Revision 1.20  2003/06/20 14:51:11  mohor
67
// Previous change removed. When resynchronization occurs we go to seg1
68
// stage. sync stage does not cause another start of seg1 stage.
69
//
70 88 mohor
// Revision 1.19  2003/06/20 14:28:20  mohor
71
// When hard_sync or resync occure we need to go to seg1 segment. Going to
72
// sync segment is in that case blocked.
73
//
74 87 mohor
// Revision 1.18  2003/06/17 15:53:33  mohor
75
// clk_cnt reduced from [8:0] to [6:0].
76
//
77 84 mohor
// Revision 1.17  2003/06/17 14:32:17  mohor
78
// Removed few signals.
79
//
80 82 mohor
// Revision 1.16  2003/06/16 13:57:58  mohor
81
// tx_point generated one clk earlier. rx_i registered. Data corrected when
82
// using extended mode.
83
//
84 78 mohor
// Revision 1.15  2003/06/13 15:02:24  mohor
85
// Synchronization is also needed when transmitting a message.
86
//
87 77 mohor
// Revision 1.14  2003/06/13 14:55:11  mohor
88
// Counters width changed.
89
//
90 76 mohor
// Revision 1.13  2003/06/11 14:21:35  mohor
91
// When switching to tx, sync stage is overjumped.
92
//
93 75 mohor
// Revision 1.12  2003/02/14 20:17:01  mohor
94
// Several registers added. Not finished, yet.
95
//
96 35 mohor
// Revision 1.11  2003/02/09 18:40:29  mohor
97
// Overload fixed. Hard synchronization also enabled at the last bit of
98
// interframe.
99
//
100 29 mohor
// Revision 1.10  2003/02/09 02:24:33  mohor
101
// Bosch license warning added. Error counters finished. Overload frames
102
// still need to be fixed.
103
//
104 28 mohor
// Revision 1.9  2003/01/31 01:13:38  mohor
105
// backup.
106
//
107 24 mohor
// Revision 1.8  2003/01/10 17:51:34  mohor
108
// Temporary version (backup).
109
//
110 15 mohor
// Revision 1.7  2003/01/08 02:10:53  mohor
111
// Acceptance filter added.
112
//
113 11 mohor
// Revision 1.6  2002/12/28 04:13:23  mohor
114
// Backup version.
115
//
116 10 mohor
// Revision 1.5  2002/12/27 00:12:52  mohor
117
// Header changed, testbench improved to send a frame (crc still missing).
118
//
119 9 mohor
// Revision 1.4  2002/12/26 01:33:05  mohor
120
// Tripple sampling supported.
121
//
122 7 mohor
// Revision 1.3  2002/12/25 23:44:16  mohor
123
// Commented lines removed.
124
//
125 6 mohor
// Revision 1.2  2002/12/25 14:17:00  mohor
126
// Synchronization working.
127
//
128 5 mohor
// Revision 1.1.1.1  2002/12/20 16:39:21  mohor
129
// Initial
130 2 mohor
//
131
//
132 5 mohor
//
133 2 mohor
 
134
// synopsys translate_off
135
`include "timescale.v"
136
// synopsys translate_on
137
`include "can_defines.v"
138
 
139
module can_btl
140
(
141
  clk,
142
  rst,
143
  rx,
144
 
145
  /* Bus Timing 0 register */
146
  baud_r_presc,
147
  sync_jump_width,
148
 
149
  /* Bus Timing 1 register */
150
  time_segment1,
151
  time_segment2,
152
  triple_sampling,
153
 
154
  /* Output signals from this module */
155 10 mohor
  sample_point,
156
  sampled_bit,
157
  sampled_bit_q,
158 24 mohor
  tx_point,
159 11 mohor
  hard_sync,
160 2 mohor
 
161 10 mohor
  /* Output from can_bsp module */
162 24 mohor
  rx_idle,
163 104 tadejm
  not_first_bit_of_inter,
164 106 mohor
  transmitting
165 2 mohor
 
166
);
167
 
168
parameter Tp = 1;
169
 
170
input         clk;
171
input         rst;
172
input         rx;
173
 
174
 
175
/* Bus Timing 0 register */
176
input   [5:0] baud_r_presc;
177
input   [1:0] sync_jump_width;
178
 
179
/* Bus Timing 1 register */
180
input   [3:0] time_segment1;
181
input   [2:0] time_segment2;
182
input         triple_sampling;
183
 
184 10 mohor
/* Output from can_bsp module */
185
input         rx_idle;
186 104 tadejm
input         not_first_bit_of_inter;
187 100 mohor
input         transmitting;
188 10 mohor
 
189 2 mohor
/* Output signals from this module */
190 10 mohor
output        sample_point;
191
output        sampled_bit;
192
output        sampled_bit_q;
193 24 mohor
output        tx_point;
194 11 mohor
output        hard_sync;
195 2 mohor
 
196
 
197
 
198 84 mohor
reg     [6:0] clk_cnt;
199 2 mohor
reg           clk_en;
200 78 mohor
reg           clk_en_q;
201 5 mohor
reg           sync_blocked;
202 100 mohor
reg           hard_sync_blocked;
203 2 mohor
reg           sampled_bit;
204 10 mohor
reg           sampled_bit_q;
205 108 mohor
reg     [3:0] quant_cnt;
206 6 mohor
reg     [3:0] delay;
207
reg           sync;
208
reg           seg1;
209
reg           seg2;
210
reg           resync_latched;
211 10 mohor
reg           sample_point;
212 7 mohor
reg     [1:0] sample;
213 76 mohor
reg           go_sync;
214 100 mohor
reg           go_seg1;
215
reg           go_seg2;
216
reg           tx_point;
217 2 mohor
 
218 76 mohor
wire          go_sync_unregistered;
219 100 mohor
wire          go_seg1_unregistered;
220
wire          go_seg2_unregistered;
221 108 mohor
wire [7:0]    preset_cnt;
222 6 mohor
wire          sync_window;
223 75 mohor
wire          resync;
224 2 mohor
 
225 5 mohor
 
226 76 mohor
 
227 6 mohor
assign preset_cnt = (baud_r_presc + 1'b1)<<1;        // (BRP+1)*2
228 104 tadejm
assign hard_sync  =   (rx_idle | not_first_bit_of_inter)    & (~rx) & sampled_bit & (~hard_sync_blocked);  // Hard synchronization
229
assign resync     =  (~rx_idle) & (~not_first_bit_of_inter) & (~rx) & sampled_bit & (~sync_blocked);       // Re-synchronization
230 5 mohor
 
231
 
232 6 mohor
/* Generating general enable signal that defines baud rate. */
233 2 mohor
always @ (posedge clk or posedge rst)
234
begin
235
  if (rst)
236 108 mohor
    clk_cnt <= 7'h0;
237 78 mohor
  else if (clk_cnt >= (preset_cnt-1'b1))
238 108 mohor
    clk_cnt <=#Tp 7'h0;
239 10 mohor
  else
240 76 mohor
    clk_cnt <=#Tp clk_cnt + 1'b1;
241 10 mohor
end
242
 
243
 
244
always @ (posedge clk or posedge rst)
245
begin
246
  if (rst)
247
    clk_en  <= 1'b0;
248 108 mohor
  else if ({1'b0, clk_cnt} == (preset_cnt-1'b1))
249 10 mohor
    clk_en  <=#Tp 1'b1;
250 2 mohor
  else
251 10 mohor
    clk_en  <=#Tp 1'b0;
252 2 mohor
end
253
 
254
 
255 5 mohor
 
256 78 mohor
always @ (posedge clk or posedge rst)
257
begin
258
  if (rst)
259
    clk_en_q  <= 1'b0;
260
  else
261
    clk_en_q  <=#Tp clk_en;
262
end
263
 
264
 
265
 
266 6 mohor
/* Changing states */
267 76 mohor
 assign go_sync_unregistered = clk_en & (seg2 & (~hard_sync) & (~resync) & ((quant_cnt[2:0] == time_segment2)));
268 104 tadejm
 assign go_seg1_unregistered = clk_en & (((sync | hard_sync) & (~seg1)) | (resync & seg2 & sync_window) | (resync_latched & sync_window) | (seg1 & hard_sync));
269 100 mohor
 assign go_seg2_unregistered = clk_en & (seg1 & (~hard_sync) & (quant_cnt == (time_segment1 + delay)));
270 5 mohor
 
271
 
272 76 mohor
always @ (posedge clk or posedge rst)
273
begin
274
  if (rst)
275 108 mohor
    go_sync <=#Tp 1'b0;
276 76 mohor
  else
277 88 mohor
    go_sync <=#Tp go_sync_unregistered;
278 76 mohor
end
279
 
280
 
281 100 mohor
always @ (posedge clk or posedge rst)
282
begin
283
  if (rst)
284 108 mohor
    go_seg1 <=#Tp 1'b0;
285 100 mohor
  else
286
    go_seg1 <=#Tp go_seg1_unregistered;
287
end
288
 
289
 
290
always @ (posedge clk or posedge rst)
291
begin
292
  if (rst)
293 108 mohor
    go_seg2 <=#Tp 1'b0;
294 100 mohor
  else
295
    go_seg2 <=#Tp go_seg2_unregistered;
296
end
297
 
298
 
299
always @ (posedge clk or posedge rst)
300
begin
301
  if (rst)
302
    tx_point <= 1'b0;
303
  else
304 104 tadejm
    tx_point <=#Tp go_sync_unregistered | (go_seg1_unregistered & (~sync));
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 5 mohor
  else if (go_sync)
328
    sync <=#Tp 1'b1;
329 104 tadejm
  else if (clk_en_q | go_seg1)
330 5 mohor
    sync <=#Tp 1'b0;
331
end
332
 
333
 
334 6 mohor
/* Seg1 stage/segment (together with propagation segment which is 1 quant long) */
335 5 mohor
always @ (posedge clk or posedge rst)
336
begin
337
  if (rst)
338 108 mohor
    seg1 <= 1'b1;
339 5 mohor
  else if (go_seg1)
340
    seg1 <=#Tp 1'b1;
341
  else if (go_seg2)
342
    seg1 <=#Tp 1'b0;
343
end
344
 
345
 
346 6 mohor
/* Seg2 stage/segment */
347 5 mohor
always @ (posedge clk or posedge rst)
348
begin
349
  if (rst)
350 108 mohor
    seg2 <= 1'b0;
351 5 mohor
  else if (go_seg2)
352
    seg2 <=#Tp 1'b1;
353
  else if (go_sync | go_seg1)
354
    seg2 <=#Tp 1'b0;
355
end
356
 
357
 
358 6 mohor
/* Quant counter */
359 5 mohor
always @ (posedge clk or posedge rst)
360
begin
361
  if (rst)
362 108 mohor
    quant_cnt <= 4'h0;
363
  else if (go_sync | go_seg1 | go_seg2)
364
    quant_cnt <=#Tp 4'h0;
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 108 mohor
    delay <= 4'h0;
375 104 tadejm
  else if (resync & seg1 & (~transmitting))  // when transmitting 0 with positive error delay is set to 0
376 108 mohor
    delay <=#Tp (quant_cnt > {2'h0, sync_jump_width})? ({2'h0, sync_jump_width} + 1'b1) : (quant_cnt + 1'b1);
377 5 mohor
  else if (go_sync | go_seg1)
378 108 mohor
    delay <=#Tp 4'h0;
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 108 mohor
      sampled_bit <= 1'b1;
402
      sampled_bit_q <= 1'b1;
403
      sample_point <= 1'b0;
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 108 mohor
          sample_point <=#Tp 1'b1;
410 10 mohor
          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 108 mohor
    sample_point <=#Tp 1'b0;
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 (go_seg2)
434 5 mohor
        sync_blocked <=#Tp 1'b0;
435
    end
436
end
437 2 mohor
 
438
 
439 100 mohor
/* Blocking hard synchronization when occurs once or when we are transmitting a msg */
440 24 mohor
always @ (posedge clk or posedge rst)
441
begin
442
  if (rst)
443 100 mohor
    hard_sync_blocked <=#Tp 1'b0;
444 104 tadejm
  else if (hard_sync & clk_en_q)
445 100 mohor
    hard_sync_blocked <=#Tp 1'b1;
446 104 tadejm
  else if (go_seg2)
447 100 mohor
    hard_sync_blocked <=#Tp 1'b0;
448 24 mohor
end
449 2 mohor
 
450
 
451 5 mohor
 
452 24 mohor
 
453
 
454 2 mohor
endmodule

powered by: WebSVN 2.1.0

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