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

Subversion Repositories can

[/] [can/] [tags/] [rel_6/] [rtl/] [verilog/] [can_btl.v] - Blame information for rev 84

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

powered by: WebSVN 2.1.0

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