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 105

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