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

Subversion Repositories can

[/] [can/] [tags/] [rel_11/] [rtl/] [verilog/] [can_btl.v] - Blame information for rev 102

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

powered by: WebSVN 2.1.0

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