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 7

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
////  This file is part of the CAN Protocal Controller            ////
7
////  http://www.opencores.org/projects/can/                      ////
8
////                                                              ////
9
////                                                              ////
10
////  Author(s):                                                  ////
11
////       Igor Mohor                                             ////
12
////       igorm@opencores.org                                    ////
13
////                                                              ////
14
////                                                              ////
15
////  All additional information is avaliable in the README.txt   ////
16
////  file.                                                       ////
17
////                                                              ////
18
//////////////////////////////////////////////////////////////////////
19
////                                                              ////
20
//// Copyright (C) 2002 Authors                                   ////
21
////                                                              ////
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
//////////////////////////////////////////////////////////////////////
44
//
45
// CVS Revision History
46
//
47
// $Log: not supported by cvs2svn $
48 7 mohor
// Revision 1.3  2002/12/25 23:44:16  mohor
49
// Commented lines removed.
50
//
51 6 mohor
// Revision 1.2  2002/12/25 14:17:00  mohor
52
// Synchronization working.
53
//
54 5 mohor
// Revision 1.1.1.1  2002/12/20 16:39:21  mohor
55
// Initial
56 2 mohor
//
57
//
58 5 mohor
//
59 2 mohor
 
60
// synopsys translate_off
61
`include "timescale.v"
62
// synopsys translate_on
63
`include "can_defines.v"
64
 
65
module can_btl
66
(
67
  clk,
68
  rst,
69
  rx,
70
 
71
  /* Mode register */
72
  reset_mode,           // Not used !!!
73
 
74
  /* Bus Timing 0 register */
75
  baud_r_presc,
76
  sync_jump_width,
77
 
78
  /* Bus Timing 1 register */
79
  time_segment1,
80
  time_segment2,
81
  triple_sampling,
82
 
83
  /* Output signals from this module */
84
  take_sample,
85
  clk_en,
86
 
87
  /* States */
88
  idle,
89
 
90
  /* bit stream processor (can_bsp.v) */
91
  sync_mode
92
 
93
 
94
);
95
 
96
parameter Tp = 1;
97
 
98
input         clk;
99
input         rst;
100
input         rx;
101
 
102
/* Mode register */
103
input         reset_mode;
104
 
105
/* Bus Timing 0 register */
106
input   [5:0] baud_r_presc;
107
input   [1:0] sync_jump_width;
108
 
109
/* Bus Timing 1 register */
110
input   [3:0] time_segment1;
111
input   [2:0] time_segment2;
112
input         triple_sampling;
113
 
114
/* Output signals from this module */
115 7 mohor
output        take_sample;      // NOT USED, YET
116 2 mohor
output        clk_en;
117
 
118
input         idle;
119
 
120
/* bit stream processor (can_bsp.v) */
121
input         sync_mode;        // NOT USED, YET
122
 
123
 
124
reg     [8:0] clk_cnt;
125
reg           clk_en;
126 5 mohor
reg           sync_blocked;
127 2 mohor
reg           sampled_bit;
128
reg     [7:0] quant_cnt;
129 6 mohor
reg     [3:0] delay;
130
reg           sync;
131
reg           seg1;
132
reg           seg2;
133
reg           resync_latched;
134
reg           sample_pulse;
135 7 mohor
reg     [1:0] sample;
136 2 mohor
 
137 6 mohor
wire          go_sync;
138
wire          go_seg1;
139
wire          go_seg2;
140
wire [8:0]    preset_cnt;
141
wire          hard_sync;
142
wire          resync;
143
wire          sync_window;
144 2 mohor
 
145 5 mohor
 
146
 
147 6 mohor
assign preset_cnt = (baud_r_presc + 1'b1)<<1;        // (BRP+1)*2
148
assign hard_sync  =   idle  & (~rx) & sampled_bit & (~sync_blocked);  // Hard synchronization
149
assign resync     = (~idle) & (~rx) & sampled_bit & (~sync_blocked);  // Re-synchronization
150 5 mohor
 
151
 
152 6 mohor
/* Generating general enable signal that defines baud rate. */
153 2 mohor
always @ (posedge clk or posedge rst)
154
begin
155
  if (rst)
156
    begin
157
      clk_cnt <= 0;
158
      clk_en  <= 1'b0;
159
    end
160
  else if (clk_cnt == (preset_cnt-1))
161
    begin
162
      clk_cnt <=#Tp 0;
163
      clk_en  <=#Tp 1'b1;
164
    end
165
  else
166
    begin
167
      clk_cnt <=#Tp clk_cnt + 1;
168
      clk_en  <=#Tp 1'b0;
169
    end
170
end
171
 
172
 
173 5 mohor
 
174 6 mohor
/* Changing states */
175 7 mohor
//assign go_sync = clk_en & (seg2 & (~resync) & ((quant_cnt == time_segment2)));
176
assign go_sync = clk_en & (seg2 & ((quant_cnt == time_segment2)));
177 6 mohor
assign go_seg1 = clk_en & (sync | hard_sync | (resync & seg2 & sync_window) | (resync_latched & sync_window));
178
assign go_seg2 = clk_en & (seg1 & (quant_cnt == (time_segment1 + delay)));
179 5 mohor
 
180
 
181 6 mohor
/* When early edge is detected outside of the SJW field, synchronization request is latched and performed when
182
   SJW is reached */
183 2 mohor
always @ (posedge clk or posedge rst)
184
begin
185
  if (rst)
186 5 mohor
    resync_latched <= 1'b0;
187 6 mohor
  else if (resync & seg2 & (~sync_window))
188 5 mohor
    resync_latched <=#Tp 1'b1;
189
  else if (go_seg1)
190
    resync_latched <= 1'b0;
191
end
192
 
193
 
194
 
195 6 mohor
/* Synchronization stage/segment */
196 5 mohor
always @ (posedge clk or posedge rst)
197
begin
198
  if (rst)
199
    sync <= 1;
200
  else if (go_sync)
201
    sync <=#Tp 1'b1;
202
  else if (go_seg1)
203
    sync <=#Tp 1'b0;
204
end
205
 
206
 
207 6 mohor
/* Seg1 stage/segment (together with propagation segment which is 1 quant long) */
208 5 mohor
always @ (posedge clk or posedge rst)
209
begin
210
  if (rst)
211
    seg1 <= 0;
212
  else if (go_seg1)
213
    seg1 <=#Tp 1'b1;
214
  else if (go_seg2)
215
    seg1 <=#Tp 1'b0;
216
end
217
 
218
 
219 6 mohor
/* Seg2 stage/segment */
220 5 mohor
always @ (posedge clk or posedge rst)
221
begin
222
  if (rst)
223
    seg2 <= 0;
224
  else if (go_seg2)
225
    seg2 <=#Tp 1'b1;
226
  else if (go_sync | go_seg1)
227
    seg2 <=#Tp 1'b0;
228
end
229
 
230
 
231 6 mohor
/* Quant counter */
232 5 mohor
always @ (posedge clk or posedge rst)
233
begin
234
  if (rst)
235
    quant_cnt <= 0;
236
  else if (go_sync || go_seg1 || go_seg2)
237
    quant_cnt <=#Tp 0;
238
  else if (clk_en)
239
    quant_cnt <=#Tp quant_cnt + 1'b1;
240
end
241
 
242
 
243 6 mohor
/* When late edge is detected (in seg1 stage), stage seg1 is prolonged. */
244 5 mohor
always @ (posedge clk or posedge rst)
245
begin
246
  if (rst)
247 6 mohor
    delay <= 0;
248 5 mohor
  else if (clk_en & resync & seg1)
249 6 mohor
    delay <=#Tp (quant_cnt > sync_jump_width)? (sync_jump_width + 1) : (quant_cnt + 1);
250 5 mohor
  else if (go_sync | go_seg1)
251 6 mohor
    delay <=#Tp 0;
252 5 mohor
end
253
 
254
 
255 6 mohor
// If early edge appears within this window (in seg2 stage), phase error is fully compensated
256
assign sync_window = ((time_segment2 - quant_cnt) < ( sync_jump_width + 1));
257 5 mohor
 
258
 
259 7 mohor
// Sampling data (memorizing two samples all the time).
260 5 mohor
always @ (posedge clk or posedge rst)
261
begin
262
  if (rst)
263 7 mohor
    sample <= 2'b11;
264
  else if (clk_en)
265
    sample <= {sample[0], rx};
266
end
267
 
268
 
269
// When enabled, tripple sampling is done here.
270
always @ (posedge clk or posedge rst)
271
begin
272
  if (rst)
273 2 mohor
    begin
274
      sampled_bit <= 1;
275 5 mohor
      sample_pulse <= 0;
276 2 mohor
    end
277 7 mohor
  else if (clk_en)
278 2 mohor
    begin
279 7 mohor
      if (seg1 & (quant_cnt == (time_segment1 + delay)))
280
        begin
281
          sample_pulse <=#Tp 1;
282
          if (triple_sampling)
283
            sampled_bit <=#Tp (sample[0] & sample[1]) | ( sample[0] & rx) | (sample[1] & rx);
284
          else
285
            sampled_bit <=#Tp rx;
286
        end
287 2 mohor
    end
288 5 mohor
  else
289 6 mohor
    sample_pulse <=#Tp 0;       // Sample pulse is for development purposes only. REMOVE ME.
290 2 mohor
end
291
 
292
 
293
 
294 5 mohor
/* Blocking synchronization (can occur only once in a bit time) */
295
always @ (posedge clk or posedge rst)
296
begin
297
  if (rst)
298
    sync_blocked <=#Tp 1'b0;
299
  else if (clk_en)
300
    begin
301
      if (hard_sync || resync)
302
        sync_blocked <=#Tp 1'b1;
303
      else if (seg2 & quant_cnt == time_segment2)
304
        sync_blocked <=#Tp 1'b0;
305
    end
306
end
307 2 mohor
 
308
 
309
 
310
 
311 5 mohor
 
312 2 mohor
endmodule

powered by: WebSVN 2.1.0

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