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

Subversion Repositories can

[/] [can/] [tags/] [rel_3/] [rtl/] [verilog/] [can_bsp.v] - Blame information for rev 21

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 mohor
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  can_bsp.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
//////////////////////////////////////////////////////////////////////
44
//
45
// CVS Revision History
46
//
47
// $Log: not supported by cvs2svn $
48 21 mohor
// Revision 1.12  2003/01/15 21:05:11  mohor
49
// CRC checking fixed (when bitstuff occurs at the end of a CRC sequence).
50
//
51 20 mohor
// Revision 1.11  2003/01/15 14:40:23  mohor
52
// RX state machine fixed to receive "remote request" frames correctly. No data bytes are written to fifo when such frames are received.
53
//
54 19 mohor
// Revision 1.10  2003/01/15 13:16:47  mohor
55
// When a frame with "remote request" is received, no data is stored to fifo, just the frame information (identifier, ...). Data length that is stored is the received data length and not the actual data length that is stored to fifo.
56
//
57 18 mohor
// Revision 1.9  2003/01/14 12:19:35  mohor
58
// rx_fifo is now working.
59
//
60 16 mohor
// Revision 1.8  2003/01/10 17:51:33  mohor
61
// Temporary version (backup).
62
//
63 15 mohor
// Revision 1.7  2003/01/09 21:54:45  mohor
64
// rx fifo added. Not 100 % verified, yet.
65
//
66 14 mohor
// Revision 1.6  2003/01/09 14:46:58  mohor
67
// Temporary files (backup).
68
//
69 13 mohor
// Revision 1.5  2003/01/08 13:30:31  mohor
70
// Temp version.
71
//
72 12 mohor
// Revision 1.4  2003/01/08 02:10:53  mohor
73
// Acceptance filter added.
74
//
75 11 mohor
// Revision 1.3  2002/12/28 04:13:23  mohor
76
// Backup version.
77
//
78 10 mohor
// Revision 1.2  2002/12/27 00:12:52  mohor
79
// Header changed, testbench improved to send a frame (crc still missing).
80
//
81 9 mohor
// Revision 1.1.1.1  2002/12/20 16:39:21  mohor
82
// Initial
83 2 mohor
//
84
//
85 9 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_bsp
93
(
94
  clk,
95 10 mohor
  rst,
96
 
97
  sample_point,
98
  sampled_bit,
99
  sampled_bit_q,
100 11 mohor
  hard_sync,
101
  resync,
102
 
103 14 mohor
  addr,
104
  data_out,
105
 
106
 
107 11 mohor
  /* Mode register */
108 10 mohor
  reset_mode,
109 11 mohor
  acceptance_filter_mode,
110
 
111 15 mohor
  /* Command register */
112
  release_buffer,
113
 
114
  /* Clock Divider register */
115 11 mohor
  extended_mode,
116
 
117
  rx_idle,
118
 
119
  /* This section is for BASIC and EXTENDED mode */
120
  /* Acceptance code register */
121
  acceptance_code_0,
122
 
123
  /* Acceptance mask register */
124
  acceptance_mask_0,
125
  /* End: This section is for BASIC and EXTENDED mode */
126 10 mohor
 
127 11 mohor
  /* This section is for EXTENDED mode */
128
  /* Acceptance code register */
129
  acceptance_code_1,
130
  acceptance_code_2,
131
  acceptance_code_3,
132 10 mohor
 
133 11 mohor
  /* Acceptance mask register */
134
  acceptance_mask_1,
135
  acceptance_mask_2,
136 18 mohor
  acceptance_mask_3,
137 11 mohor
  /* End: This section is for EXTENDED mode */
138 10 mohor
 
139 18 mohor
  /* Tx data registers. Holding identifier (basic mode), tx frame information (extended mode) and data */
140
  tx_data_0,
141
  tx_data_1,
142
  tx_data_2,
143
  tx_data_3,
144
  tx_data_4,
145
  tx_data_5,
146
  tx_data_6,
147
  tx_data_7,
148
  tx_data_8,
149
  tx_data_9,
150
  tx_data_10,
151
  tx_data_11,
152
  tx_data_12
153
  /* End: Tx data registers */
154
 
155 2 mohor
);
156
 
157
parameter Tp = 1;
158
 
159 10 mohor
input         clk;
160
input         rst;
161
input         sample_point;
162
input         sampled_bit;
163
input         sampled_bit_q;
164 11 mohor
input         hard_sync;
165
input         resync;
166 14 mohor
input   [7:0] addr;
167
output  [7:0] data_out;
168 11 mohor
 
169 14 mohor
 
170 10 mohor
input         reset_mode;
171 11 mohor
input         acceptance_filter_mode;
172
input         extended_mode;
173 2 mohor
 
174 15 mohor
/* Command register */
175
input         release_buffer;
176 11 mohor
 
177 10 mohor
output        rx_idle;
178 2 mohor
 
179 11 mohor
/* This section is for BASIC and EXTENDED mode */
180
/* Acceptance code register */
181
input   [7:0] acceptance_code_0;
182
 
183
/* Acceptance mask register */
184
input   [7:0] acceptance_mask_0;
185
 
186
/* End: This section is for BASIC and EXTENDED mode */
187
 
188
 
189
/* This section is for EXTENDED mode */
190
/* Acceptance code register */
191
input   [7:0] acceptance_code_1;
192
input   [7:0] acceptance_code_2;
193
input   [7:0] acceptance_code_3;
194
 
195
/* Acceptance mask register */
196
input   [7:0] acceptance_mask_1;
197
input   [7:0] acceptance_mask_2;
198
input   [7:0] acceptance_mask_3;
199
/* End: This section is for EXTENDED mode */
200
 
201 18 mohor
  /* Tx data registers. Holding identifier (basic mode), tx frame information (extended mode) and data */
202
input   [7:0] tx_data_0;
203
input   [7:0] tx_data_1;
204
input   [7:0] tx_data_2;
205
input   [7:0] tx_data_3;
206
input   [7:0] tx_data_4;
207
input   [7:0] tx_data_5;
208
input   [7:0] tx_data_6;
209
input   [7:0] tx_data_7;
210
input   [7:0] tx_data_8;
211
input   [7:0] tx_data_9;
212
input   [7:0] tx_data_10;
213
input   [7:0] tx_data_11;
214
input   [7:0] tx_data_12;
215
  /* End: Tx data registers */
216 11 mohor
 
217
 
218 10 mohor
reg           reset_mode_q;
219
reg     [5:0] bit_cnt;
220 2 mohor
 
221 10 mohor
reg     [3:0] data_len;
222
reg    [28:0] id;
223
reg     [2:0] bit_stuff_cnt;
224
reg           stuff_error;
225
 
226
wire          bit_de_stuff;
227
 
228
 
229
/* Rx state machine */
230
wire          go_rx_idle;
231
wire          go_rx_id1;
232
wire          go_rx_rtr1;
233
wire          go_rx_ide;
234
wire          go_rx_id2;
235
wire          go_rx_rtr2;
236
wire          go_rx_r1;
237
wire          go_rx_r0;
238
wire          go_rx_dlc;
239
wire          go_rx_data;
240
wire          go_rx_crc;
241 11 mohor
wire          go_rx_crc_lim;
242 10 mohor
wire          go_rx_ack;
243 11 mohor
wire          go_rx_ack_lim;
244 10 mohor
wire          go_rx_eof;
245
 
246 11 mohor
wire          go_crc_enable;
247
wire          rst_crc_enable;
248
 
249
wire          bit_de_stuff_set;
250
wire          bit_de_stuff_reset;
251
 
252 10 mohor
reg           rx_idle;
253
reg           rx_id1;
254
reg           rx_rtr1;
255
reg           rx_ide;
256
reg           rx_id2;
257
reg           rx_rtr2;
258
reg           rx_r1;
259
reg           rx_r0;
260
reg           rx_dlc;
261
reg           rx_data;
262
reg           rx_crc;
263 11 mohor
reg           rx_crc_lim;
264 10 mohor
reg           rx_ack;
265 11 mohor
reg           rx_ack_lim;
266 10 mohor
reg           rx_eof;
267
 
268 19 mohor
reg           rtr1;
269
reg           ide;
270
reg           rtr2;
271
reg    [14:0] crc_in;
272
 
273 11 mohor
reg           crc_enable;
274
 
275 10 mohor
reg     [2:0] eof_cnt;
276 11 mohor
wire   [14:0] calculated_crc;
277 19 mohor
wire          remote_rq;
278
wire [3:0]    limited_data_len;
279 10 mohor
 
280 11 mohor
assign go_rx_idle     =                   sample_point &  rx_eof  & (eof_cnt == 6);
281
assign go_rx_id1      =                   sample_point &  rx_idle & (~sampled_bit);
282
assign go_rx_rtr1     = (~bit_de_stuff) & sample_point &  rx_id1  & (bit_cnt == 10);
283
assign go_rx_ide      = (~bit_de_stuff) & sample_point &  rx_rtr1;
284
assign go_rx_id2      = (~bit_de_stuff) & sample_point &  rx_ide  &   sampled_bit;
285
assign go_rx_rtr2     = (~bit_de_stuff) & sample_point &  rx_id2  & (bit_cnt == 17);
286
assign go_rx_r1       = (~bit_de_stuff) & sample_point &  rx_rtr2;
287
assign go_rx_r0       = (~bit_de_stuff) & sample_point & (rx_ide  & (~sampled_bit) | rx_r1);
288
assign go_rx_dlc      = (~bit_de_stuff) & sample_point &  rx_r0;
289 19 mohor
assign go_rx_data     = (~bit_de_stuff) & sample_point &  rx_dlc  & (bit_cnt == 3) &  (sampled_bit   |   (|data_len[2:0])) & (~remote_rq);
290
assign go_rx_crc      = (~bit_de_stuff) & sample_point & (rx_dlc  & (bit_cnt == 3) & ((~sampled_bit) & (~(|data_len[2:0])) | remote_rq) |
291
                                                          rx_data & (bit_cnt == ((limited_data_len<<3) - 1'b1)));
292 20 mohor
assign go_rx_crc_lim  = (~bit_de_stuff) & sample_point &  rx_crc  & (bit_cnt == 14);
293 11 mohor
assign go_rx_ack      =                   sample_point &  rx_crc_lim;
294
assign go_rx_ack_lim  =                   sample_point &  rx_ack;
295
assign go_rx_eof      =                   sample_point &  rx_ack_lim  | (~reset_mode) & reset_mode_q;
296 10 mohor
 
297 11 mohor
assign go_crc_enable  = hard_sync;
298
assign rst_crc_enable = go_rx_crc;
299 10 mohor
 
300 11 mohor
assign bit_de_stuff_set   = go_rx_id1;
301
assign bit_de_stuff_reset = go_rx_crc_lim;
302
 
303 19 mohor
assign remote_rq = ((~ide) & rtr1) | (ide & rtr2);
304
assign limited_data_len = (data_len < 8)? data_len : 4'h8;
305 11 mohor
 
306 19 mohor
 
307 10 mohor
// Rx idle state
308
always @ (posedge clk or posedge rst)
309
begin
310
  if (rst)
311
    rx_idle <= 1'b1;
312
  else if (reset_mode | go_rx_id1)
313
    rx_idle <=#Tp 1'b0;
314
  else if (go_rx_idle)
315
    rx_idle <=#Tp 1'b1;
316
end
317
 
318
 
319
// Rx id1 state
320
always @ (posedge clk or posedge rst)
321
begin
322
  if (rst)
323
    rx_id1 <= 1'b0;
324
  else if (reset_mode | go_rx_rtr1)
325
    rx_id1 <=#Tp 1'b0;
326
  else if (go_rx_id1)
327
    rx_id1 <=#Tp 1'b1;
328
end
329
 
330
 
331
// Rx rtr1 state
332
always @ (posedge clk or posedge rst)
333
begin
334
  if (rst)
335
    rx_rtr1 <= 1'b0;
336
  else if (reset_mode | go_rx_ide)
337
    rx_rtr1 <=#Tp 1'b0;
338
  else if (go_rx_rtr1)
339
    rx_rtr1 <=#Tp 1'b1;
340
end
341
 
342
 
343
// Rx ide state
344
always @ (posedge clk or posedge rst)
345
begin
346
  if (rst)
347
    rx_ide <= 1'b0;
348
  else if (reset_mode | go_rx_r0 | go_rx_id2)
349
    rx_ide <=#Tp 1'b0;
350
  else if (go_rx_ide)
351
    rx_ide <=#Tp 1'b1;
352
end
353
 
354
 
355
// Rx id2 state
356
always @ (posedge clk or posedge rst)
357
begin
358
  if (rst)
359
    rx_id2 <= 1'b0;
360
  else if (reset_mode | go_rx_rtr2)
361
    rx_id2 <=#Tp 1'b0;
362
  else if (go_rx_id2)
363
    rx_id2 <=#Tp 1'b1;
364
end
365
 
366
 
367
// Rx rtr2 state
368
always @ (posedge clk or posedge rst)
369
begin
370
  if (rst)
371
    rx_rtr2 <= 1'b0;
372
  else if (reset_mode | go_rx_r1)
373
    rx_rtr2 <=#Tp 1'b0;
374
  else if (go_rx_rtr2)
375
    rx_rtr2 <=#Tp 1'b1;
376
end
377
 
378
 
379
// Rx r0 state
380
always @ (posedge clk or posedge rst)
381
begin
382
  if (rst)
383
    rx_r1 <= 1'b0;
384
  else if (reset_mode | go_rx_r0)
385
    rx_r1 <=#Tp 1'b0;
386
  else if (go_rx_r1)
387
    rx_r1 <=#Tp 1'b1;
388
end
389
 
390
 
391
// Rx r0 state
392
always @ (posedge clk or posedge rst)
393
begin
394
  if (rst)
395
    rx_r0 <= 1'b0;
396
  else if (reset_mode | go_rx_dlc)
397
    rx_r0 <=#Tp 1'b0;
398
  else if (go_rx_r0)
399
    rx_r0 <=#Tp 1'b1;
400
end
401
 
402
 
403
// Rx dlc state
404
always @ (posedge clk or posedge rst)
405
begin
406
  if (rst)
407
    rx_dlc <= 1'b0;
408
  else if (reset_mode | go_rx_data | go_rx_crc)
409
    rx_dlc <=#Tp 1'b0;
410
  else if (go_rx_dlc)
411
    rx_dlc <=#Tp 1'b1;
412
end
413
 
414
 
415
// Rx data state
416
always @ (posedge clk or posedge rst)
417
begin
418
  if (rst)
419
    rx_data <= 1'b0;
420
  else if (reset_mode | go_rx_crc)
421
    rx_data <=#Tp 1'b0;
422
  else if (go_rx_data)
423
    rx_data <=#Tp 1'b1;
424
end
425
 
426
 
427
// Rx crc state
428
always @ (posedge clk or posedge rst)
429
begin
430
  if (rst)
431
    rx_crc <= 1'b0;
432 11 mohor
  else if (reset_mode | go_rx_crc_lim)
433 10 mohor
    rx_crc <=#Tp 1'b0;
434
  else if (go_rx_crc)
435
    rx_crc <=#Tp 1'b1;
436
end
437
 
438
 
439 11 mohor
// Rx crc delimiter state
440
always @ (posedge clk or posedge rst)
441
begin
442
  if (rst)
443
    rx_crc_lim <= 1'b0;
444
  else if (reset_mode | go_rx_ack)
445
    rx_crc_lim <=#Tp 1'b0;
446
  else if (go_rx_crc_lim)
447
    rx_crc_lim <=#Tp 1'b1;
448
end
449
 
450
 
451 10 mohor
// Rx ack state
452
always @ (posedge clk or posedge rst)
453
begin
454
  if (rst)
455
    rx_ack <= 1'b0;
456 11 mohor
  else if (reset_mode | go_rx_ack_lim)
457 10 mohor
    rx_ack <=#Tp 1'b0;
458
  else if (go_rx_ack)
459
    rx_ack <=#Tp 1'b1;
460
end
461
 
462
 
463 11 mohor
// Rx ack delimiter state
464
always @ (posedge clk or posedge rst)
465
begin
466
  if (rst)
467
    rx_ack_lim <= 1'b0;
468
  else if (reset_mode | go_rx_eof)
469
    rx_ack_lim <=#Tp 1'b0;
470
  else if (go_rx_ack_lim)
471
    rx_ack_lim <=#Tp 1'b1;
472
end
473
 
474
 
475 10 mohor
// Rx eof state
476
always @ (posedge clk or posedge rst)
477
begin
478
  if (rst)
479
    rx_eof <= 1'b0;
480
  else if (go_rx_idle)
481
    rx_eof <=#Tp 1'b0;
482
  else if (go_rx_eof)
483
    rx_eof <=#Tp 1'b1;
484
end
485
 
486
 
487
// ID register
488
always @ (posedge clk or posedge rst)
489
begin
490
  if (rst)
491
    id <= 0;
492 11 mohor
  else if (sample_point & (rx_id1 | rx_id2) & (~bit_de_stuff))
493 10 mohor
    id <=#Tp {id[27:0], sampled_bit};
494
end
495
 
496 11 mohor
 
497
// rtr1 bit
498
always @ (posedge clk or posedge rst)
499
begin
500
  if (rst)
501
    rtr1 <= 0;
502
  else if (sample_point & rx_rtr1 & (~bit_de_stuff))
503
    rtr1 <=#Tp sampled_bit;
504
end
505
 
506
 
507
// rtr2 bit
508
always @ (posedge clk or posedge rst)
509
begin
510
  if (rst)
511
    rtr2 <= 0;
512
  else if (sample_point & rx_rtr2 & (~bit_de_stuff))
513
    rtr2 <=#Tp sampled_bit;
514
end
515
 
516
 
517
// ide bit
518
always @ (posedge clk or posedge rst)
519
begin
520
  if (rst)
521
    ide <= 0;
522
  else if (sample_point & rx_ide & (~bit_de_stuff))
523
    ide <=#Tp sampled_bit;
524
end
525
 
526
 
527 10 mohor
// Data length
528
always @ (posedge clk or posedge rst)
529
begin
530
  if (rst)
531
    data_len <= 0;
532
  else if (sample_point & rx_dlc & (~bit_de_stuff))
533
    data_len <=#Tp {data_len[2:0], sampled_bit};
534
end
535
 
536
 
537 11 mohor
// Data
538
reg [7:0] tmp_data;
539
reg [7:0] tmp_fifo [0:7];
540
always @ (posedge clk or posedge rst)
541
begin
542
  if (rst)
543
    tmp_data <= 0;
544
  else if (sample_point & rx_data & (~bit_de_stuff))
545
    tmp_data <=#Tp {tmp_data[6:0], sampled_bit};
546
end
547
 
548
 
549
reg write_data_to_tmp_fifo;
550
always @ (posedge clk or posedge rst)
551
begin
552
  if (rst)
553
    write_data_to_tmp_fifo <= 0;
554
  else if (sample_point & rx_data & (~bit_de_stuff) & (&bit_cnt[2:0]))
555
    write_data_to_tmp_fifo <=#Tp 1'b1;
556
  else
557
    write_data_to_tmp_fifo <=#Tp 0;
558
end
559
 
560
 
561
reg [2:0] byte_cnt;
562
always @ (posedge clk or posedge rst)
563
begin
564
  if (rst)
565
    byte_cnt <= 0;
566
  else if (write_data_to_tmp_fifo)
567
    byte_cnt <=#Tp byte_cnt + 1;
568
  else if (sample_point & go_rx_crc_lim)
569
    byte_cnt <=#Tp 0;
570
end
571
 
572
 
573
always @ (posedge clk or posedge rst)
574
begin
575
  if (write_data_to_tmp_fifo)
576
    tmp_fifo[byte_cnt] <=#Tp tmp_data;
577
end
578
 
579
 
580
 
581
// CRC
582
always @ (posedge clk or posedge rst)
583
begin
584
  if (rst)
585
    crc_in <= 0;
586
  else if (sample_point & rx_crc & (~bit_de_stuff))
587
    crc_in <=#Tp {crc_in[13:0], sampled_bit};
588
end
589
 
590
 
591 10 mohor
// bit_cnt
592
always @ (posedge clk or posedge rst)
593
begin
594
  if (rst)
595
    bit_cnt <= 0;
596
  else if (go_rx_id1 | go_rx_id2 | go_rx_dlc | go_rx_data | go_rx_crc | go_rx_ack | go_rx_eof)
597
    bit_cnt <=#Tp 0;
598 11 mohor
  else if (sample_point & (~bit_de_stuff))
599 10 mohor
    bit_cnt <=#Tp bit_cnt + 1'b1;
600
end
601
 
602
 
603
// eof_cnt
604
always @ (posedge clk or posedge rst)
605
begin
606
  if (rst)
607
    eof_cnt <= 0;
608
  else if (sample_point)
609
    begin
610
      if (rx_eof & sampled_bit)
611
        eof_cnt <=#Tp eof_cnt + 1'b1;
612
      else
613
        eof_cnt <=#Tp 0;
614
    end
615
end
616
 
617
 
618 11 mohor
// Enabling bit de-stuffing
619
reg bit_stuff_cnt_en;
620
always @ (posedge clk or posedge rst)
621
begin
622
  if (rst)
623
    bit_stuff_cnt_en <= 1'b0;
624
  else if (bit_de_stuff_set)
625
    bit_stuff_cnt_en <=#Tp 1'b1;
626
  else if (bit_de_stuff_reset)
627
    bit_stuff_cnt_en <=#Tp 1'b0;
628
end
629 10 mohor
 
630
// bit_stuff_cnt
631
always @ (posedge clk or posedge rst)
632
begin
633
  if (rst)
634
    bit_stuff_cnt <= 1;
635 15 mohor
  else if (bit_de_stuff_reset)
636
    bit_stuff_cnt <=#Tp 1;
637 11 mohor
  else if (sample_point & bit_stuff_cnt_en)
638 10 mohor
    begin
639
      if (bit_stuff_cnt == 5)
640
        bit_stuff_cnt <=#Tp 1;
641
      else if (sampled_bit == sampled_bit_q)
642
        bit_stuff_cnt <=#Tp bit_stuff_cnt + 1'b1;
643
      else
644
        bit_stuff_cnt <=#Tp 1;
645
    end
646
end
647
 
648
 
649
assign bit_de_stuff = bit_stuff_cnt == 5;
650
 
651
 
652
// stuff_error
653
always @ (posedge clk or posedge rst)
654
begin
655
  if (rst)
656
    stuff_error <= 0;
657
  else if (sample_point & (rx_id1) & bit_de_stuff & (sampled_bit == sampled_bit_q))   // Add other stages (data, control, etc.) !!!
658
    stuff_error <=#Tp 1'b1;
659
//  else if (reset condition)       // Add reset condition
660
//    stuff_error <=#Tp 0;
661
end
662
 
663
 
664
// Generating delayed reset_mode signal
665
always @ (posedge clk)
666
begin
667
  reset_mode_q <=#Tp reset_mode;
668
end
669
 
670
 
671 11 mohor
 
672
always @ (posedge clk or posedge rst)
673
begin
674
  if (rst)
675
    crc_enable <= 1'b0;
676
  else if (go_crc_enable)
677
    crc_enable <=#Tp 1'b1;
678
  else if (reset_mode | rst_crc_enable)
679
    crc_enable <=#Tp 1'b0;
680
end
681
 
682
 
683
reg crc_error;
684
// CRC error generation
685
always @ (posedge clk or posedge rst)
686
begin
687
  if (rst)
688
    crc_error <= 0;
689 12 mohor
  else if (go_rx_ack)
690 11 mohor
    crc_error <=#Tp crc_in != calculated_crc;
691
  else if (reset_mode | rx_eof)
692
    crc_error <=#Tp 0;
693
end
694
 
695
 
696
 
697
// Instantiation of the RX CRC module
698
can_crc i_can_crc_rx
699
(
700
  .clk(clk),
701
//  .data(sampled_bit & (~rx_crc)),     // Zeros are shifted in for calculation when we are in crc stage
702
  .data(sampled_bit ),     // Zeros are shifted in for calculation when we are in crc stage
703
  .enable(crc_enable & sample_point & (~bit_de_stuff)),
704
  .initialize(rx_eof),
705
  .crc(calculated_crc)
706
);
707
 
708
 
709
 
710
wire          id_ok;        // If received ID matches ID set in registers
711 16 mohor
wire          no_byte0;     // There is no byte 0 (RTR bit set to 1 or DLC field equal to 0). Signal used for acceptance filter.
712
wire          no_byte1;     // There is no byte 1 (RTR bit set to 1 or DLC field equal to 1). Signal used for acceptance filter.
713 11 mohor
 
714 16 mohor
assign no_byte0 = rtr1 | (data_len<1);
715
assign no_byte1 = rtr1 | (data_len<2);
716 11 mohor
 
717
can_acf i_can_acf
718
(
719
  .clk(clk),
720
  .rst(rst),
721
 
722
  .id(id),
723
 
724
  /* Mode register */
725
  .reset_mode(reset_mode),
726
  .acceptance_filter_mode(acceptance_filter_mode),
727
 
728 12 mohor
  // Clock Divider register
729 11 mohor
  .extended_mode(extended_mode),
730
 
731
  /* This section is for BASIC and EXTENDED mode */
732
  /* Acceptance code register */
733
  .acceptance_code_0(acceptance_code_0),
734
 
735
  /* Acceptance mask register */
736
  .acceptance_mask_0(acceptance_mask_0),
737
  /* End: This section is for BASIC and EXTENDED mode */
738
 
739
  /* This section is for EXTENDED mode */
740
  /* Acceptance code register */
741
  .acceptance_code_1(acceptance_code_1),
742
  .acceptance_code_2(acceptance_code_2),
743
  .acceptance_code_3(acceptance_code_3),
744
 
745
  /* Acceptance mask register */
746
  .acceptance_mask_1(acceptance_mask_1),
747
  .acceptance_mask_2(acceptance_mask_2),
748
  .acceptance_mask_3(acceptance_mask_3),
749
  /* End: This section is for EXTENDED mode */
750
 
751
  .sample_point(sample_point),
752
  .go_rx_crc_lim(go_rx_crc_lim),
753
  .go_rx_idle(go_rx_idle),
754
 
755
  .data0(tmp_fifo[0]),
756
  .data1(tmp_fifo[1]),
757
  .rtr1(rtr1),
758
  .rtr2(rtr2),
759
  .ide(ide),
760 16 mohor
  .no_byte0(no_byte0),
761
  .no_byte1(no_byte1),
762 11 mohor
 
763
  .id_ok(id_ok)
764
 
765
);
766
 
767
 
768
 
769 13 mohor
reg [3:0]   data_cnt;       // Counting the data bytes that are written to FIFO
770
reg [2:0]   header_cnt;     // Counting header length
771
reg         wr_fifo;        // Write data and header to 64-byte fifo
772
reg [7:0]   data_for_fifo;  // Multiplexed data that is stored to 64-byte fifo
773 11 mohor
 
774 12 mohor
 
775 13 mohor
wire [2:0]  header_len;
776
wire        storing_header;
777 19 mohor
wire [3:0]  limited_data_len_minus1;
778 16 mohor
wire        reset_wr_fifo;
779 20 mohor
wire        no_error;
780 16 mohor
assign header_len[2:0] = extended_mode ? (ide? (3'h5) : (3'h3)) : 3'h2;
781 13 mohor
assign storing_header = header_cnt < header_len;
782 19 mohor
assign limited_data_len_minus1[3:0] = remote_rq? 4'hf : ((data_len < 8)? (data_len -1'b1) : 4'h7);   // - 1 because counter counts from 0
783
assign reset_wr_fifo = data_cnt == (limited_data_len_minus1 + header_len);
784 20 mohor
assign no_error = ~crc_error;
785 12 mohor
 
786 13 mohor
// Write enable signal for 64-byte rx fifo
787
always @ (posedge clk or posedge rst)
788
begin
789
  if (rst)
790
    wr_fifo <= 1'b0;
791 16 mohor
  else if (reset_wr_fifo)
792
    wr_fifo <=#Tp 1'b0;
793 21 mohor
  else if (go_rx_idle & id_ok & no_error)
794 13 mohor
    wr_fifo <=#Tp 1'b1;
795
end
796 12 mohor
 
797
 
798 13 mohor
// Header counter. Header length depends on the mode of operation and frame format.
799 12 mohor
always @ (posedge clk or posedge rst)
800
begin
801
  if (rst)
802 13 mohor
    header_cnt <= 0;
803 16 mohor
  else if (reset_wr_fifo)
804
    header_cnt <=#Tp 0;
805
  else if (wr_fifo & storing_header)
806 13 mohor
    header_cnt <=#Tp header_cnt + 1;
807 12 mohor
end
808
 
809
 
810 13 mohor
// Data counter. Length of the data is limited to 8 bytes.
811 12 mohor
always @ (posedge clk or posedge rst)
812
begin
813
  if (rst)
814 13 mohor
    data_cnt <= 0;
815 16 mohor
  else if (reset_wr_fifo)
816
    data_cnt <=#Tp 0;
817
  else if (wr_fifo)
818 13 mohor
    data_cnt <=#Tp data_cnt + 1;
819 12 mohor
end
820
 
821
 
822 13 mohor
// Multiplexing data that is stored to 64-byte fifo depends on the mode of operation and frame format
823
always @ (extended_mode or ide or data_cnt or header_cnt or storing_header or id or rtr1 or rtr2 or data_len or
824
          tmp_fifo[0] or tmp_fifo[2] or tmp_fifo[4] or tmp_fifo[6] or
825
          tmp_fifo[1] or tmp_fifo[3] or tmp_fifo[5] or tmp_fifo[7])
826 12 mohor
begin
827 13 mohor
  if (storing_header)
828 12 mohor
    begin
829 13 mohor
      if (extended_mode)      // extended mode
830 12 mohor
        begin
831 13 mohor
          if (ide)              // extended format
832
            begin
833
              case (header_cnt)            // synopsys parallel_case synopsys full_case
834
                4'h0  : data_for_fifo <= {1'b1, rtr2, 2'h0, data_len};
835
                4'h1  : data_for_fifo <= id[28:21];
836
                4'h2  : data_for_fifo <= id[20:13];
837
                4'h3  : data_for_fifo <= id[12:5];
838
                4'h4  : data_for_fifo <= {id[4:0], 3'h0};
839
              endcase
840
            end
841
          else                  // standard format
842
            begin
843
              case (header_cnt)            // synopsys parallel_case synopsys full_case
844
                4'h0  : data_for_fifo <= {1'b0, rtr1, 2'h0, data_len};
845
                4'h1  : data_for_fifo <= id[10:3];
846
                4'h2  : data_for_fifo <= {id[2:0], 5'h0};
847
              endcase
848
            end
849 12 mohor
        end
850 13 mohor
      else                    // normal mode
851 12 mohor
        begin
852 13 mohor
          case (header_cnt)            // synopsys parallel_case synopsys full_case
853
            4'h0  : data_for_fifo <= id[10:3];
854
            4'h1  : data_for_fifo <= {id[2:0], rtr1, data_len};
855 12 mohor
          endcase
856
        end
857
    end
858 13 mohor
  else
859 16 mohor
    data_for_fifo <= tmp_fifo[data_cnt-header_len];
860 12 mohor
end
861
 
862
 
863
 
864
 
865
// Instantiation of the RX fifo module
866 13 mohor
can_fifo i_can_fifo
867 12 mohor
(
868
  .clk(clk),
869
  .rst(rst),
870
 
871 13 mohor
  .wr(wr_fifo),
872 12 mohor
 
873 13 mohor
  .data_in(data_for_fifo),
874 14 mohor
  .addr(addr),
875
  .data_out(data_out),
876 12 mohor
 
877
  .reset_mode(reset_mode),
878 15 mohor
  .release_buffer(release_buffer),
879 14 mohor
  .extended_mode(extended_mode)
880 12 mohor
 
881
 
882
);
883
 
884
 
885
 
886
 
887
 
888
 
889
 
890 13 mohor
 
891 2 mohor
endmodule

powered by: WebSVN 2.1.0

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