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

Subversion Repositories can

[/] [can/] [tags/] [asyst_3/] [rtl/] [verilog/] [can_btl.v] - Blame information for rev 82

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

powered by: WebSVN 2.1.0

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