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

Subversion Repositories can

[/] [can/] [tags/] [rel_6/] [rtl/] [verilog/] [can_bsp.v] - Blame information for rev 19

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

powered by: WebSVN 2.1.0

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