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

Subversion Repositories can

[/] [can/] [tags/] [asyst_2/] [rtl/] [verilog/] [can_btl.v] - Blame information for rev 29

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

powered by: WebSVN 2.1.0

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