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 78

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

powered by: WebSVN 2.1.0

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