protocol_control_fsm.vhd 123 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
--------------------------------------------------------------------------------
-- 
-- CTU CAN FD IP Core
-- Copyright (C) 2015-2018
-- 
-- Authors:
--     Ondrej Ille <ondrej.ille@gmail.com>
--     Martin Jerabek <martin.jerabek01@gmail.com>
-- 
-- Project advisors: 
-- 	Jiri Novak <jnovak@fel.cvut.cz>
-- 	Pavel Pisa <pisa@cmp.felk.cvut.cz>
-- 
-- Department of Measurement         (http://meas.fel.cvut.cz/)
-- Faculty of Electrical Engineering (http://www.fel.cvut.cz)
-- Czech Technical University        (http://www.cvut.cz/)
-- 
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this VHDL component and associated documentation files (the "Component"),
-- to deal in the Component without restriction, including without limitation
-- the rights to use, copy, modify, merge, publish, distribute, sublicense,
-- and/or sell copies of the Component, and to permit persons to whom the
-- Component is furnished to do so, subject to the following conditions:
-- 
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Component.
-- 
-- THE COMPONENT IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHTHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-- FROM, OUT OF OR IN CONNECTION WITH THE COMPONENT OR THE USE OR OTHER DEALINGS
-- IN THE COMPONENT.
-- 
-- The CAN protocol is developed by Robert Bosch GmbH and protected by patents.
-- Anybody who wants to implement this IP core on silicon has to obtain a CAN
-- protocol license from Bosch.
-- 
--------------------------------------------------------------------------------

--------------------------------------------------------------------------------
43 44 45
-- Module:
--  Protocol control FSM.
--
46
-- Purpose:
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
--  State machine handling CAN FD protocol according to CAN FD 1.0 and ISO CAN
--  FD (ISO 11898-1 2015) standard.
--  Processes RX Data in "Process" pipeline stage with "rx_trigger". This
--  corresponds to moment one clock cycle behind sample point. In the same clock
--  cycle loads TX Shift register. FSM communicates with following modules:
--    1. TXT Buffers (HW Commands)
--    2. TX Arbitrator (HW Commands)
--    3. RX Buffer (Storing protocol)
--    4. TX Shift Register (Load)
--    5. RX Shift Register (Store commands)
--    6. Error detector (Error enabling, CRC check command)
--    7. Prescaler (Sample control, Synchronisation control)
--    8. Bus sampling (Transceiver Delay measurement)
--    9. Bit Stuffing (Stuffing method, Length of Stuff rule, Data halt)
--   10. Bit De-Stuffing (Stuffing method, Length of Stuff rule, Destuffed bit)
--   11. CAN CRC (Enable, speculative enable, Calculated CRC sequence)
--   12. Control, Retransmitt, Re-integration counters.
--   13. Fault confinement (Fault confinement protocol as in ISO 11898-1 2015.
--   14. Operation control (Setting transmitter/receiver idle).
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
--------------------------------------------------------------------------------

Library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.ALL;

Library work;
use work.id_transfer.all;
use work.can_constants.all;
use work.can_components.all;
use work.can_types.all;
use work.cmn_lib.all;
use work.drv_stat_pkg.all;
use work.reduce_lib.all;

use work.CAN_FD_register_map.all;
use work.CAN_FD_frame_format.all;

entity protocol_control_fsm is
85 86 87 88
    generic(
        -- Reset polarity
        G_RESET_POLARITY        :    std_logic := '0'
    );
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    port(
        -----------------------------------------------------------------------
        -- Clock and Asynchronous Reset
        -----------------------------------------------------------------------
        -- System clock
        clk_sys                 :in   std_logic;
        
        -- Asynchronous reset
        res_n                   :in   std_logic;
        
        -----------------------------------------------------------------------
        -- Signals which cause state change
        -----------------------------------------------------------------------
        -- RX Trigger
        rx_trigger              :in   std_logic;
104 105 106 107

        -- Error frame request
        err_frm_req             :in   std_logic;

108 109 110 111 112 113 114 115 116 117 118
        -----------------------------------------------------------------------
        -- Memory registers interface
        -----------------------------------------------------------------------
        -- CTU CAN FD is enabled
        drv_ena                 :in   std_logic;
        
        -- CAN FD type (ISO / NON-ISO)
        drv_fd_type             :in   std_logic;
        
        -- Command to start re-integration in Bus-off
        drv_bus_off_reset       :in   std_logic;
119 120 121 122 123 124 125 126 127 128 129 130
        
        -- Forbidding acknowledge mode
        drv_ack_forb            :in   std_logic;
        
        -- Self Test Mode enabled
        drv_self_test_ena       :in   std_logic;

        -- Bus Monitoring mode enabled
        drv_bus_mon_ena         :in   std_logic;
        
        -- Retransmition limit enabled for errornous frames
        drv_retr_lim_ena        :in   std_logic;
131
        
132 133 134 135 136 137
        -- Internal Loopback enabled
        drv_int_loopback_ena    :in   std_logic;
        
        -- Reception of CAN FD Frames is enabled
        drv_can_fd_ena          :in   std_logic;
        
138 139 140
        -- Secondary sampling point delay select
        drv_ssp_delay_select    :in   std_logic_vector(1 downto 0);
        
141 142 143 144 145 146
        -- Control field is being transmitted
        is_control              :out  std_logic;

        -- Data field is being transmitted
        is_data                 :out  std_logic;

147 148 149
        -- Stuff Count field is being transmitted
        is_stuff_count          :out  std_logic;

150 151 152
        -- CRC field is being transmitted
        is_crc                  :out  std_logic;
        
153 154 155 156 157 158 159 160 161
        -- CRC Delimiter is being transmitted
        is_crc_delim            :out  std_logic;
        
        -- ACK field is being transmitted
        is_ack_field            :out  std_logic;
        
        -- ACK Delimiter is being transmitted
        is_ack_delim            :out  std_logic;
        
162 163
        -- End of Frame field is being transmitted
        is_eof                  :out  std_logic;
164 165 166 167 168 169
        
        -- Intermission is being transmitted
        is_intermission         :out  std_logic;
        
        -- Suspend transmission is being transmitted
        is_suspend              :out  std_logic;
170 171

        -- Error frame is being transmitted
172
        is_err_frm              :out  std_logic;
173 174 175
        
        -- Overload frame is being transmitted
        is_overload             :out  std_logic;
176 177 178
        
        -- Start of Frame
        is_sof                  :out  std_logic;
179 180 181 182

        -----------------------------------------------------------------------
        -- Data-path interface
        -----------------------------------------------------------------------
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
183 184
        -- Actual TX Data (post bit stuffing)
        tx_data_wbs             :in   std_logic;
185 186
        
        -- Actual RX Data
187
        rx_data_nbs             :in   std_logic;
188 189 190 191 192 193
        
        -----------------------------------------------------------------------
        -- RX Buffer interface
        -----------------------------------------------------------------------
        -- Command to store CAN frame metadata to RX Buffer
        store_metadata          :out  std_logic;
194

195
        -- Command to store word of CAN Data
196
        store_data              :out  std_logic;
197 198 199 200
        
        -- Received frame valid
        rec_valid               :out  std_logic;
        
201
        -- Command to abort storing of RX frame (due to Error frame)
202 203 204 205
        rec_abort               :out  std_logic;
        
        -- Start of Frame pulse
        sof_pulse               :out  std_logic;
206

207 208 209 210 211 212 213
        -----------------------------------------------------------------------
        -- TXT Buffer, TX Arbitrator interface
        -----------------------------------------------------------------------
        -- There is a valid frame for transmission
        tran_frame_valid        :in   std_logic;
        
        -- HW Commands to TXT Buffers
214
        txtb_hw_cmd             :out  t_txtb_hw_cmd;
215
        
216
        -- Pointer to TXT Buffer memory
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
217
        txtb_ptr                :out  natural range 0 to 19;
218
        
219 220 221 222 223 224 225 226
        -- TX Data length code
        tran_dlc                :in   std_logic_vector(3 downto 0);
        
        -- TX Remote transmission request flag
        tran_is_rtr             :in   std_logic;
        
        -- TX Frame type (0-CAN 2.0, 1-CAN FD)
        tran_frame_type         :in   std_logic;
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
227 228 229
        
        -- Identifier type (BASIC, EXTENDED)
        tran_ident_type         :in   std_logic;
230 231 232 233

        -- TX Bit rate shift
        tran_brs                :in   std_logic;
                
234 235 236
        -----------------------------------------------------------------------
        -- TX Shift register interface
        -----------------------------------------------------------------------
237 238 239 240 241 242 243 244 245
        -- Load Base Identifier to TX Shift register
        tx_load_base_id         :out  std_logic;

        -- Load extended Identifier to TX Shift register
        tx_load_ext_id          :out  std_logic;

        -- Load DLC
        tx_load_dlc             :out  std_logic;

246 247 248
        -- Load Data word to TX Shift register
        tx_load_data_word       :out  std_logic;
        
249 250 251
        -- Load Stuff count
        tx_load_stuff_count     :out  std_logic;

252 253
        -- Load CRC to TX Shift register
        tx_load_crc             :out  std_logic;
254 255 256 257 258 259

        -- Shift register enable (shifts with TX Trigger)
        tx_shift_ena            :out  std_logic;

        -- Force Dominant value instead of value from shift register
        tx_dominant             :out  std_logic;
260 261 262 263
        
        -----------------------------------------------------------------------
        -- RX Shift register interface
        -----------------------------------------------------------------------
264 265 266
        -- Clear all registers in RX Shift register
        rx_clear                :out  std_logic;
        
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
        -- Store Base Identifier 
        rx_store_base_id        :out  std_logic;
        
        -- Store Extended Identifier
        rx_store_ext_id         :out  std_logic;
        
        -- Store Identifier extension
        rx_store_ide            :out  std_logic;
        
        -- Store Remote transmission request
        rx_store_rtr            :out  std_logic;
        
        -- Store EDL (FDF) bit
        rx_store_edl            :out  std_logic;
        
        -- Store DLC
        rx_store_dlc            :out  std_logic;
        
        -- Store ESI
        rx_store_esi            :out  std_logic;
        
        -- Store BRS
        rx_store_brs            :out  std_logic;
        
        -- Store stuff count and Stuff Count parity
        rx_store_stuff_count    :out  std_logic;
        
        -- Clock Enable RX Shift register for each byte.
295
        rx_shift_ena            :out  std_logic_vector(3 downto 0);
296 297 298 299 300 301 302 303 304
        
        -- Selector for inputs of each byte of shift register
        -- (0-Previous byte output, 1- RX Data input)
        rx_shift_in_sel         :out  std_logic;
        
        -- RX value of Remote transmission request
        rec_is_rtr              :in   std_logic;

        -- RX value of DLC (combinational), valid only in last bit of DLC
305 306 307 308
        rec_dlc_d               :in   std_logic_vector(3 downto 0);
        
        -- RX value of DLC (captured)
        rec_dlc_q               :in   std_logic_vector(3 downto 0);
309 310 311
        
        -- RX frame type (0-CAN 2.0, 1- CAN FD)
        rec_frame_type          :in   std_logic;
312

313 314 315 316
        -----------------------------------------------------------------------
        -- Control counter interface
        -----------------------------------------------------------------------
        -- Preload control counter
317
        ctrl_ctr_pload          :out   std_logic;
318 319
        
        -- Control counter preload value
320
        ctrl_ctr_pload_val      :out   std_logic_vector(8 downto 0);
321
        
322 323 324
        -- Control counter is enabled
        ctrl_ctr_ena            :out   std_logic;
        
325
        -- Control counter is zero
326
        ctrl_ctr_zero           :in    std_logic;
327 328 329
        
        -- Control counter is equal to 1
        ctrl_ctr_one            :in    std_logic;
330 331 332 333 334 335

        -- Control counter counted multiple of 8 bits
        ctrl_counted_byte       :in    std_logic;

        -- Control counter byte index within a memory word
        ctrl_counted_byte_index :in    std_logic_vector(1 downto 0);
336
        
337 338
        -- Control counter - TXT Buffer memory index
        ctrl_ctr_mem_index      :in    std_logic_vector(4 downto 0);
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
339 340 341
        
        -- Complementary counter enable
        compl_ctr_ena           :out   std_logic;
342 343 344
        
        -- Arbitration lost capture ID field
        alc_id_field            :out   std_logic_vector(2 downto 0);
345

346 347 348 349 350 351 352
        -----------------------------------------------------------------------
        -- Reintegration counter interface
        -----------------------------------------------------------------------
        -- Reintegration counter Clear (synchronous)
        reinteg_ctr_clr         :out   std_logic;

        -- Enable counting (with RX Trigger)
353
        reinteg_ctr_enable      :out   std_logic;
354 355
        
        -- Reintegration counter expired (reached 128)
356 357
        reinteg_ctr_expired     :in    std_logic;

358
        -----------------------------------------------------------------------
359
        -- Retransmitt counter interface
360
        -----------------------------------------------------------------------
361 362 363 364 365 366 367 368 369
        -- Clear Retransmitt counter
        retr_ctr_clear          :out   std_logic;

        -- Increment Retransmitt counter by 1
        retr_ctr_add            :out   std_logic;

        -- Retransmitt limit was reached
        retr_limit_reached      :in    std_logic;

370 371 372 373
        -----------------------------------------------------------------------
        -- Error detector interface
        -----------------------------------------------------------------------
        -- Form Error has occurred
374
        form_err                :out   std_logic;
375

376
        -- ACK Error has occurred
377
        ack_err                 :out   std_logic;
378

379 380 381
        -- Perform CRC check
        crc_check               :out   std_logic;
        
382
        -- Bit Error in arbitration field
383
        bit_err_arb             :out   std_logic;
384
        
385 386 387 388
        -- Calculated CRC and Stuff count are matching received ones
        crc_match               :in   std_logic;

        -- CRC error signalling
389
        crc_err                 :out  std_logic;
390 391 392 393

        -- Clear CRC Match flag
        crc_clear_match_flag    :out   std_logic;

394 395 396
        -- CRC Source (CRC15, CRC17, CRC21)
        crc_src                 :out   std_logic_vector(1 downto 0);
        
397
        -- Error position field (for Error capture)
398 399 400 401 402 403 404 405 406 407
        err_pos                 :out   std_logic_vector(4 downto 0);
        
        -- Arbitration field is being transmitted / received
        is_arbitration          :out   std_logic;
        
        -----------------------------------------------------------------------
        -- Bit Stuffing/Destuffing control signals
        -----------------------------------------------------------------------
        -- Bit Stuffing is enabled
        stuff_enable            :out   std_logic;
408 409 410
        
        -- Bit De-stuffing is enabled
        destuff_enable          :out   std_logic;
411

412 413 414 415 416
        -- Length of Bit stuffing rule
        stuff_length            :out   std_logic_vector(2 downto 0);
        
        -- Fixed Bit stuffing method
        fixed_stuff             :out   std_logic;
417 418 419 420
        
        -- Frame transmission without SOF started
        tx_frame_no_sof         :out   std_logic;
        
421 422 423 424 425 426 427 428
        -----------------------------------------------------------------------
        -- Operation control interface
        -----------------------------------------------------------------------
        -- Unit is transmitter
        is_transmitter          :in   std_logic;
        
        -- Unit is receiver
        is_receiver             :in   std_logic;
429

430 431
        -- Unit is idle
        is_idle                 :in   std_logic;
432

433 434
        -- Loss of arbitration -> Turn receiver!
        arbitration_lost        :out  std_logic;
435

436 437
        -- Set unit to be transmitter (in SOF)
        set_transmitter         :out  std_logic;
438

439 440
        -- Set unit to be receiver
        set_receiver            :out  std_logic;
441

442 443
        -- Set unit to be idle
        set_idle                :out  std_logic;
444

445 446 447 448
        -----------------------------------------------------------------------
        -- Fault confinement interface
        -----------------------------------------------------------------------
        -- Primary Error
449
        primary_err             :out  std_logic;
450 451 452
        
        -- Active Error or Overload flag is being tranmsmitted
        act_err_ovr_flag        :out  std_logic;
453

454 455 456
        -- Set unit to be error active
        set_err_active          :out   std_logic;

457 458 459 460 461 462 463 464 465 466 467
        -- Error delimiter too late
        err_delim_late          :out  std_logic;

        -- Unit is error active
        is_err_active           :in   std_logic;
        
        -- Unit is error passive
        is_err_passive          :in   std_logic;
        
        -- Unit is Bus off
        is_bus_off              :in   std_logic;
468
        
469 470 471 472 473
        -----------------------------------------------------------------------
        -- Other control signals
        -----------------------------------------------------------------------
        -- Sample control (Nominal, Data, Secondary)
        sp_control              :out   std_logic_vector(1 downto 0);
474
        
475 476 477
        -- Sample control (Registered)
        sp_control_q            :out   std_logic_vector(1 downto 0);
        
478 479
        -- Enable Nominal Bit time counters.
        nbt_ctrs_en             :out   std_logic;
480
        
481 482
        -- Enable Data Bit time counters.
        dbt_ctrs_en             :out   std_logic;
483

484
        -- Synchronisation control (No synchronisation, Hard Synchronisation,
485
        -- Resynchronisation)
486
        sync_control            :out   std_logic_vector(1 downto 0);
487 488 489 490

        -- No Resynchronisation due to positive phase error
        no_pos_resync           :out   std_logic;

491 492
        -- Clear the Shift register for secondary sampling point.
        ssp_reset               :out   std_logic;
493

494 495
        -- Enable measurement of Transmitter delay
        tran_delay_meas         :out   std_logic;
496

497
        -- Protocol control FSM state output
498 499
        pc_state                :out   t_protocol_control_state;

500 501
        -- Transmitted frame is valid
        tran_valid              :out   std_logic;
502

503
        -- ACK received
504 505 506 507
        ack_received            :out   std_logic;

        -- CRC calculation enabled
        crc_enable              :out   std_logic;
508 509 510
        
        -- CRC calculation - speculative enable
        crc_spec_enable         :out   std_logic;
511 512 513
        
        -- Use RX Data for CRC calculation
        crc_calc_from_rx        :out   std_logic;
514

515 516 517
        -- Load CRC Initialization vector
        load_init_vect          :out   std_logic;

518
        -- Bit error enable
519
        bit_err_enable          :out   std_logic;
520 521

        -- Bit rate shifted
522 523 524 525 526 527 528 529 530 531
        br_shifted              :out   std_logic;
        
        -- Reset Bit time measurement counter
        btmc_reset              :out   std_logic;
    
        -- Start Measurement of data bit time (in TX Trigger)
        dbt_measure_start       :out  std_logic;
    
        -- First SSP generated (in ESI bit)
        gen_first_ssp           :out  std_logic
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
    );
end entity;

architecture rtl of protocol_control_fsm is

    ---------------------------------------------------------------------------
    -- FSM related signals
    ---------------------------------------------------------------------------
    -- Protocol control FSM
    signal curr_state : t_protocol_control_state;
    signal next_state : t_protocol_control_state;

    -- Clock enable for state register
    signal state_reg_ce : std_logic;
    
    ---------------------------------------------------------------------------
    -- Internal combinational signals
    ---------------------------------------------------------------------------
    -- No data field should be transmitted
    signal no_data_transmitter : std_logic;
552
    signal no_data_receiver : std_logic;
553 554 555 556 557 558 559 560
    signal no_data_field : std_logic;
    
    -- Allow 2 bit long CRC delimiter
    signal allow_2bit_crc_delim : std_logic;
    
    -- Allow 2 bit long ACK slot
    signal allow_2bit_ack : std_logic;

561
    -- Preload control counter internal signal
562
    signal ctrl_ctr_pload_i : std_logic;
563 564 565 566 567 568 569 570 571 572 573 574
    
    -- CRC Selection
    signal crc_use_21         : std_logic;
    signal crc_use_17         : std_logic;
    signal crc_src_i          : std_logic_vector(1 downto 0);
    signal crc_length_i       : std_logic_vector(8 downto 0);

    -- Length of data field (decoded from DLC, does not take RTR into account)
    signal tran_data_length   : std_logic_vector(6 downto 0);
    signal rec_data_length    : std_logic_vector(6 downto 0);
    signal rec_data_length_c  : std_logic_vector(6 downto 0);

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
575 576 577 578
    signal data_length_c         : std_logic_vector(6 downto 0);
    signal data_length_shifted_c : std_logic_vector(9 downto 0);
    signal data_length_sub_c     : unsigned(9 downto 0);
    signal data_length_bits_c    : std_logic_vector(8 downto 0);
579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
    
    -- FD Frame is being transmitted/received
    signal is_fd_frame        : std_logic;
    
    -- Frame transmission/reception can be started from idle or intermission!
    signal frame_start        : std_logic;
    
    -- There is TX Frame ready for transmission
    signal tx_frame_ready     : std_logic;
    
    -- IDE bit is part of arbitration
    signal ide_is_arbitration : std_logic;
    
    -- Arbitration lost condition
    signal arbitration_lost_condition : std_logic;
594 595 596

    -- Loss of arbitration -> Turn receiver!
    signal arbitration_lost_i   : std_logic;
597 598 599 600 601 602 603 604 605 606 607 608
    
    -- Transmission failed (due to reached number of retransmissions), or
    -- first error, arb lost when there are 0 retransmissions allowed!
    signal tx_failed            : std_logic;
    
    -- Internal commands for RX Buffer
    signal store_metadata_d     : std_logic;
    signal store_data_d         : std_logic;
    signal rec_valid_d          : std_logic;
    signal rec_abort_d          : std_logic;

    -- Internal commands for TXT Buffers
609 610
    signal txtb_hw_cmd_d         : t_txtb_hw_cmd;
    signal txtb_hw_cmd_q         : t_txtb_hw_cmd;
611 612 613 614
    
    -- Unit should go to suspend transmission field!
    signal go_to_suspend        : std_logic;
    
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
615 616 617
    -- Unit should go to stuff count field
    signal go_to_stuff_count    : std_logic;
    
618 619 620 621 622 623 624 625 626 627 628
    -- Internal store commands for RX Shift register
    signal rx_store_base_id_i        :  std_logic;
    signal rx_store_ext_id_i         :  std_logic;
    signal rx_store_ide_i            :  std_logic;
    signal rx_store_rtr_i            :  std_logic;
    signal rx_store_edl_i            :  std_logic;
    signal rx_store_dlc_i            :  std_logic;
    signal rx_store_esi_i            :  std_logic;
    signal rx_store_brs_i            :  std_logic;
    signal rx_store_stuff_count_i    :  std_logic;
    
629
    signal rx_clear_i                :  std_logic;
630 631 632 633 634 635 636 637 638 639 640 641

    -- Internal commands for TX Shift register
    signal tx_load_base_id_i         :  std_logic;
    signal tx_load_ext_id_i          :  std_logic;
    signal tx_load_dlc_i             :  std_logic;
    signal tx_load_data_word_i       :  std_logic;
    signal tx_load_stuff_count_i     :  std_logic;
    signal tx_load_crc_i             :  std_logic;

    signal tx_shift_ena_i            :  std_logic;

    -- Internal signals for detected errors
642 643 644 645
    signal form_err_i                :  std_logic;
    signal ack_err_i                 :  std_logic;  
    signal crc_err_i                 :  std_logic;
    signal bit_err_arb_i             :  std_logic;
646 647 648 649 650

    -- Sample control (Bit Rate) signals
    signal sp_control_switch_data    :  std_logic;
    signal sp_control_switch_nominal :  std_logic;
    
651 652 653
    -- Secondary sampling point is used
    signal switch_to_ssp             :  std_logic;
    
654
    signal sp_control_ce             :  std_logic;
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
655
    signal sp_control_d              :  std_logic_vector(1 downto 0);
656
    signal sp_control_q_i              :  std_logic_vector(1 downto 0);
657 658

    -- Secondary sampling point shift register reset
659
    signal ssp_reset_i               :  std_logic;
660 661 662 663 664 665 666 667 668
    
    -- Synchronisation control
    signal sync_control_d            :  std_logic_vector(1 downto 0);
    signal sync_control_q            :  std_logic_vector(1 downto 0);
    
    -- Hard synchronisation should be performed
    signal perform_hsync             :  std_logic;
    
    -- Fault confinemnt interface
669
    signal primary_err_i             :  std_logic;
670
    signal err_delim_late_i          :  std_logic;
671
    signal set_err_active_i          :  std_logic;
672
    
673 674 675 676 677
    -- Operation state handling internal
    signal set_transmitter_i         :  std_logic;
    signal set_receiver_i            :  std_logic;
    signal set_idle_i                :  std_logic;
    
678 679 680 681 682 683 684
    -- Flag which holds whether FSM is in first bit of error delimiter 
    signal first_err_delim_d         :  std_logic;
    signal first_err_delim_q         :  std_logic;
    
    -- Bit stuffing 
    signal stuff_enable_set          :  std_logic;
    signal stuff_enable_clear        :  std_logic;
685 686 687 688
    
    -- Bit stuffing disable
    signal destuff_enable_set        :  std_logic;
    signal destuff_enable_clear      :  std_logic;
689 690 691 692 693 694

    -- Bit error disable (internal)
    -- Note: Bit Error is rather disabled than enabled, since it is disabled on less
    -- places than enabled!
    signal bit_err_disable           :  std_logic;
    
695 696 697
    -- Bit Error is disabled for receiver in most of the frame!
    signal bit_err_disable_receiver  :  std_logic;
    
698
    -- TXT Buffer pointer
699 700
    signal txtb_ptr_d             :  natural range 0 to 19;
    signal txtb_ptr_q             :  natural range 0 to 19;
701 702 703 704
    
    -- Start of frame pulse
    signal sof_pulse_i               :  std_logic;
    
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
705 706 707
    -- Complementary counter enable
    signal compl_ctr_ena_i           :  std_logic;
    
708 709 710 711
    -- State register will be clocked extra, not only in in Sample point or
    -- error frame request.
    signal tick_state_reg            :  std_logic;
    
712 713 714
    -- Bit-rate shifted (internal value)
    signal br_shifted_i              :  std_logic;
    
715 716 717 718 719 720
    -- Arbitration field is being transmitted / received
    signal is_arbitration_i          :  std_logic;
    
    -- CRC calculation - speculative enable
    signal crc_spec_enable_i         :  std_logic;
    
721 722 723
    -- CRC Load initialization vector - internal value
    signal load_init_vect_i          :  std_logic;
    
724 725
    -- Capture register to synchronize Bus off reset request till next Sample point
    signal drv_bus_off_reset_q       :  std_logic;
726 727 728
    
    -- Retransmitt counter clear (internal value)
    signal retr_ctr_clear_i          :  std_logic;
729 730 731 732
    -- Increment Retransmitt counter by 1
    signal retr_ctr_add_i            :  std_logic;


733 734 735 736
    
    -- Blocking register for retransmitt counter add signal.
    signal retr_ctr_add_block        :  std_logic;
    signal retr_ctr_add_block_clr    :  std_logic;
737
    
738 739
    -- Blocking HW command for Unlock.
    signal block_txtb_unlock         :  std_logic;
740
    
741 742 743 744
    -- No SOF transmitted
    signal tx_frame_no_sof_d         :  std_logic;
    signal tx_frame_no_sof_q         :  std_logic;
    
745 746 747
    -- Control signal should be updated!
    signal ctrl_signal_upd          :   std_logic;
    
748 749
begin

750 751 752 753
    tx_frame_ready <= '1' when (tran_frame_valid = '1' and drv_bus_mon_ena = '0')
                          else
                      '0';

754 755
    no_data_transmitter <= '1' when (tran_dlc = "0000" or 
                                    (tran_is_rtr = RTR_FRAME and tran_frame_type = NORMAL_CAN))
756 757 758
                               else
                           '0';

759
    no_data_receiver <= '1' when (rec_is_rtr = RTR_FRAME or rec_dlc_d = "0000")
760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
                            else
                        '0';

    no_data_field <= '1' when (is_transmitter = '1' and no_data_transmitter = '1')
                         else
                     '1' when (is_receiver = '1' and no_data_receiver = '1')
                         else
                     '0';

    allow_2bit_crc_delim <= '1' when (is_transmitter = '1' and 
                                      tran_frame_type = FD_CAN)
                                else
                            '0';

    allow_2bit_ack <= '1' when (is_transmitter = '1' and tran_frame_type = FD_CAN)
                          else
                      '1' when (is_receiver = '1' and rec_frame_type = FD_CAN)
                          else
                      '0';

780 781 782 783
    go_to_suspend <= '1' when (is_err_passive = '1' and is_transmitter = '1')
                         else
                     '0';

784
    ide_is_arbitration <= '1' when (tran_ident_type = EXTENDED or is_receiver = '1')
785 786 787 788
                              else
                          '0';

    arbitration_lost_condition <= '1' when (is_transmitter = '1' and 
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
789
                                            tx_data_wbs = RECESSIVE and
790
                                            rx_data_nbs = DOMINANT and
791
                                            rx_trigger = '1')
792 793 794 795 796 797 798 799 800 801 802 803
                                      else
                                  '0';

    tx_failed <= '1' when (drv_retr_lim_ena = '1' and retr_limit_reached = '1')
                     else
                 '0';

    is_fd_frame <= '1' when (is_transmitter = '1' and tran_frame_type = FD_CAN)
                       else
                   '1' when (is_receiver = '1' and rec_frame_type = FD_CAN)
                       else
                   '0';
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
804 805 806 807 808

    go_to_stuff_count <= '1' when (drv_fd_type = ISO_FD and is_fd_frame = '1')
                             else
                         '0';

809
    frame_start <= '1' when (tx_frame_ready = '1' and go_to_suspend = '0') else
810
                   '1' when (rx_data_nbs = DOMINANT) else
811 812
                   '0';

813 814 815 816
    block_txtb_unlock <= '1' when (curr_state = s_pc_act_err_flag or
                                   curr_state = s_pc_pas_err_flag or
                                   curr_state = s_pc_err_delim_wait or
                                   curr_state = s_pc_err_delim or
817 818 819
                                   curr_state = s_pc_ovr_flag or
                                   curr_state = s_pc_ovr_delim_wait or
                                   curr_state = s_pc_ovr_delim)
820 821
                             else
                         '0';
822

823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
    ---------------------------------------------------------------------------
    -- CRC sequence selection
    ---------------------------------------------------------------------------
    crc_use_21 <= '1' when (is_transmitter = '1' and tran_frame_type = FD_CAN and
                            to_integer(unsigned(tran_data_length)) > 16)
                      else
                  '1' when (is_receiver = '1' and rec_frame_type = FD_CAN and
                            to_integer(unsigned(rec_data_length)) > 16)
                      else
                  '0';

    crc_use_17 <= '1' when (is_transmitter = '1' and tran_frame_type = FD_CAN and
                            crc_use_21 = '0')
                      else
                  '1' when (is_receiver = '1' and rec_frame_type = FD_CAN and
                            crc_use_21 = '0')
                      else
                  '0';
                            

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
843 844 845
    crc_src_i <= C_CRC21_SRC when (crc_use_21 = '1') else
                 C_CRC17_SRC when (crc_use_17 = '1') else
                 C_CRC15_SRC;
846

Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
847 848
    crc_length_i <= C_CRC15_DURATION when (crc_src_i = C_CRC15_SRC) else
                    C_CRC17_DURATION when (crc_src_i = C_CRC17_SRC) else
849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
                    C_CRC21_DURATION;

    ---------------------------------------------------------------------------
    -- DLC to Data length decoders
    ---------------------------------------------------------------------------
    dlc_decoder_tx_inst : dlc_decoder
    port map(
        dlc           => tran_dlc,
        frame_type    => tran_frame_type,

        data_length   => tran_data_length,
        is_valid      => open
    );

    dlc_decoder_rx_inst : dlc_decoder
    port map(
        dlc           => rec_dlc_q,
        frame_type    => rec_frame_type,

        data_length   => rec_data_length,
        is_valid      => open
    );
    
    dlc_decoder_rx_inst_comb : dlc_decoder
    port map(
        dlc           => rec_dlc_d,
        frame_type    => rec_frame_type,

        data_length   => rec_data_length_c,
        is_valid      => open
    );

    -- Data field length (valid only in Sample point of last bit of DLC)
    data_length_c <= tran_data_length when (is_transmitter = '1') else
                     rec_data_length_c;
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
884 885 886 887 888 889 890 891 892 893 894
                     
    -- Shift by 3 (Multiply by 8)
    data_length_shifted_c <= data_length_c & "000";
    
    -- Subtract 1 (control counter counts till field length minus 1)
    data_length_sub_c <= unsigned(data_length_shifted_c) - 1;
    
    -- Convert to length of control counter
    data_length_bits_c <= std_logic_vector(
            data_length_sub_c(ctrl_ctr_pload_val'length - 1 downto 0));
    
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912
    ---------------------------------------------------------------------------
    -- Bus off reset request capture register.
    -- Capture when there is write to memory registers, clear when request is
    -- processed (in next Sample Point).
    ---------------------------------------------------------------------------
    bus_off_req_capt_proc : process(res_n, clk_sys)
    begin
        if (res_n = G_RESET_POLARITY) then
            drv_bus_off_reset_q <= '0';
        elsif (rising_edge(clk_sys)) then
            if (drv_bus_off_reset = '1') then
                drv_bus_off_reset_q <= '1';
            elsif (rx_trigger = '1') then
                drv_bus_off_reset_q <= '0';
            end if;
        end if;
    end process;

913 914 915
    ---------------------------------------------------------------------------
    -- Next state process
    ---------------------------------------------------------------------------
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
916 917 918
    next_state_proc : process(
        curr_state, drv_ena, err_frm_req, ctrl_ctr_zero, no_data_field,
        drv_fd_type, allow_2bit_crc_delim, allow_2bit_ack, is_receiver,
919
        is_bus_off, go_to_suspend, tx_frame_ready, drv_bus_off_reset_q,
920
        reinteg_ctr_expired, rx_data_nbs, is_err_active, go_to_stuff_count
921
        )
922 923 924 925 926 927
    begin
        next_state <= curr_state;

        if (drv_ena = CTU_CAN_DISABLED) then
            next_state <= s_pc_off;
            
928
        elsif (err_frm_req = '1') then
929 930 931 932 933
            if (is_err_active = '1') then
                next_state <= s_pc_act_err_flag;
            else
                next_state <= s_pc_pas_err_flag;
            end if;
934
            
935 936 937 938 939 940 941 942 943 944 945 946 947
        else
            case curr_state is
    
            -------------------------------------------------------------------
            -- Unit is Off (drv_ena = '0')
            -------------------------------------------------------------------
            when s_pc_off =>
                next_state <= s_pc_integrating;
            
            -------------------------------------------------------------------
            -- Unit is integrating (first integration after enabling)
            -------------------------------------------------------------------
            when s_pc_integrating =>
948
                if (ctrl_ctr_zero = '1') then
949 950 951 952 953 954 955 956 957 958 959 960 961
                    next_state <= s_pc_idle;
                end if;

            -------------------------------------------------------------------
            -- Start of frame
            -------------------------------------------------------------------
            when s_pc_sof =>
                next_state <= s_pc_base_id;
                
            -------------------------------------------------------------------
            -- Base identifier
            -------------------------------------------------------------------
            when s_pc_base_id =>
962
                if (ctrl_ctr_zero = '1') then
963 964 965 966 967 968 969 970 971 972 973 974 975
                    next_state <= s_pc_rtr_srr_r1;
                end if;
            
            -------------------------------------------------------------------
            -- RTR/SRR/R1 bit. First bit after Base identifier.
            -------------------------------------------------------------------
            when s_pc_rtr_srr_r1 =>
                next_state <= s_pc_ide;
            
            -------------------------------------------------------------------
            -- IDE bit
            -------------------------------------------------------------------
            when s_pc_ide =>
976
                if (rx_data_nbs = DOMINANT) then
977 978 979 980 981 982 983 984 985
                   next_state <= s_pc_edl_r0;
                else
                   next_state <= s_pc_ext_id; 
                end if;
            
            -------------------------------------------------------------------
            -- Extended identifier
            -------------------------------------------------------------------
            when s_pc_ext_id =>
986
                if (ctrl_ctr_zero = '1') then    
987 988 989 990 991 992 993 994 995 996 997 998 999
                    next_state <= s_pc_rtr_r1;
                end if;

            -------------------------------------------------------------------
            -- RTR/R1 bit after the Extended identifier
            -------------------------------------------------------------------
            when s_pc_rtr_r1 =>
                next_state <= s_pc_edl_r1;    
                
            -------------------------------------------------------------------
            -- EDL/r1 bit after RTR/r1 bit in Extended Identifier
            -------------------------------------------------------------------
            when s_pc_edl_r1 =>
1000
                if (rx_data_nbs = DOMINANT) then
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
                    next_state <= s_pc_r0_ext;
                else
                    next_state <= s_pc_r0_fd; 
                end if;
                    
            -------------------------------------------------------------------
            -- r0 bit after EDL/r1 bit in Extended CAN Frames.
            -------------------------------------------------------------------
            when s_pc_r0_ext =>
                next_state <= s_pc_dlc;

            -------------------------------------------------------------------
            -- r0 bit in CAN FD Frames (both Base and Extended identifier)
            ------------------------------------------------------------------- 
            when s_pc_r0_fd =>
                next_state <= s_pc_brs;    
                
            -------------------------------------------------------------------
            -- EDL/r0 bit in CAN 2.0 and CAN FD Frames with BASE identifier
            -- only!
            -------------------------------------------------------------------
            when s_pc_edl_r0 =>
1023
                if (rx_data_nbs = DOMINANT) then
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
                    next_state <= s_pc_dlc;
                else
                    next_state <= s_pc_r0_fd;
                end if; 
            
            -------------------------------------------------------------------
            -- BRS (Bit rate shift) Bit
            -------------------------------------------------------------------
            when s_pc_brs =>
                next_state <= s_pc_esi;
            
            -------------------------------------------------------------------
            -- ESI (Error State Indicator) Bit
            ------------------------------------------------------------------- 
            when s_pc_esi =>
                next_state <= s_pc_dlc;
            
            -------------------------------------------------------------------
            -- DLC (Data length code)
            -------------------------------------------------------------------
            when s_pc_dlc =>
1045
                if (ctrl_ctr_zero = '1') then 
1046
                    if (no_data_field = '1') then
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1047
                        if (go_to_stuff_count = '1') then
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
                            next_state <= s_pc_stuff_count;
                        else
                            next_state <= s_pc_crc;
                        end if;
                    else
                        next_state <= s_pc_data;
                    end if;
                end if;

            -------------------------------------------------------------------
            -- Data field
            -------------------------------------------------------------------
            when s_pc_data =>
1061
                if (ctrl_ctr_zero = '1') then
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1062
                    if (go_to_stuff_count = '1') then
1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
                        next_state <= s_pc_stuff_count;
                    else
                        next_state <= s_pc_crc;
                    end if;
                end if;

            -------------------------------------------------------------------
            -- Stuff count + Stuff parity field
            -------------------------------------------------------------------
            when s_pc_stuff_count =>
1073
                if (ctrl_ctr_zero = '1') then
1074 1075 1076 1077 1078 1079 1080
                    next_state <= s_pc_crc;
                end if;

            -------------------------------------------------------------------
            -- CRC field
            -------------------------------------------------------------------
            when s_pc_crc =>
1081
                if (ctrl_ctr_zero = '1') then
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1082
                    next_state <= s_pc_crc_delim;
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
                end if;
           
            -------------------------------------------------------------------
            -- CRC Delimiter
            -------------------------------------------------------------------
            when s_pc_crc_delim =>
                if (allow_2bit_crc_delim = '1') then
                    next_state <= s_pc_crc_delim_sec;
                else
                    next_state <= s_pc_ack;
                end if;

            -------------------------------------------------------------------
            -- Secondary CRC Delimiter, or an ACK Slot if DOMINANT.
            -------------------------------------------------------------------
            when s_pc_crc_delim_sec =>
1099
                if (rx_data_nbs = DOMINANT) then
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
                    next_state <= s_pc_ack_sec;
                else
                    next_state <= s_pc_ack;
                end if;

            -------------------------------------------------------------------
            -- ACK Slot
            -------------------------------------------------------------------
            when s_pc_ack =>
                if (allow_2bit_ack = '1') then
                    next_state <= s_pc_ack_sec;
                else
                    next_state <= s_pc_ack_delim;
                end if;

            -------------------------------------------------------------------
            -- Secondary ACK field (in FD Frames),or ACK Delimiter if RECESSIVE
            -------------------------------------------------------------------
            when s_pc_ack_sec =>
1119
                if (rx_data_nbs = DOMINANT) then
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135
                    next_state <= s_pc_ack_delim;
                else
                    next_state <= s_pc_eof;
                end if;

            -------------------------------------------------------------------
            -- ACK Delimiter
            -------------------------------------------------------------------
            when s_pc_ack_delim =>
                next_state <= s_pc_eof;

            -------------------------------------------------------------------
            -- End of Frame. Receiver sampling DOMINANT in last bit interprets
            -- this as Overload flag!
            -------------------------------------------------------------------
            when s_pc_eof =>
1136
                if (ctrl_ctr_zero = '1') then
1137
                    if (rx_data_nbs = RECESSIVE) then
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1138
                        next_state <= s_pc_intermission;
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
                    elsif (is_receiver = '1') then
                        next_state <= s_pc_ovr_flag;
                    end if;
                end if;

            -------------------------------------------------------------------
            -- Intermission field
            -------------------------------------------------------------------
            when s_pc_intermission =>
                if (is_bus_off = '1') then
                    next_state <= s_pc_reintegrating_wait;
                    
                -- Last bit of intermission!
1152
                elsif (ctrl_ctr_zero = '1') then
1153
                    if (rx_data_nbs = DOMINANT) then
1154
                        next_state <= s_pc_base_id;
1155
                    elsif (go_to_suspend = '1') then
1156
                        next_state <= s_pc_suspend;
1157 1158
                    elsif (tx_frame_ready = '1') then
                        next_state <= s_pc_sof;
1159 1160 1161 1162 1163
                    else
                        next_state <= s_pc_idle;
                    end if;
                
                -- First or second bit of intermission!
1164
                elsif (rx_data_nbs = DOMINANT) then
1165 1166 1167 1168 1169 1170 1171
                    next_state <= s_pc_ovr_flag;
                end if;

            -------------------------------------------------------------------
            -- Suspend transmission
            -------------------------------------------------------------------
            when s_pc_suspend =>
1172
                if (rx_data_nbs = DOMINANT) then
1173
                    next_state <= s_pc_base_id;
1174
                elsif (ctrl_ctr_zero = '1') then
1175 1176
                    -- Start transmission after suspend if we have what to
                    -- transmitt!
1177
                    if (tx_frame_ready = '1') then
1178 1179 1180 1181 1182 1183 1184 1185 1186 1187
                        next_state <= s_pc_sof;
                    else    
                        next_state <= s_pc_idle;
                    end if;
                end if;

            -------------------------------------------------------------------
            -- Unit is in Bus idle period.
            -------------------------------------------------------------------
            when s_pc_idle =>
1188 1189 1190 1191

               if (is_bus_off = '1') then
                   next_state <= s_pc_reintegrating_wait;
               elsif (rx_data_nbs = DOMINANT) then
1192
                   next_state <= s_pc_base_id;
1193
               elsif (tx_frame_ready = '1') then
1194 1195 1196 1197 1198 1199 1200
                   next_state <= s_pc_sof;
               end if;

            -------------------------------------------------------------------
            -- Wait till command from User to start re-integration!
            -------------------------------------------------------------------
            when s_pc_reintegrating_wait =>
1201
                if (drv_bus_off_reset_q = '1') then
1202 1203 1204 1205 1206 1207 1208
                    next_state <= s_pc_reintegrating;    
                end if;

            -------------------------------------------------------------------
            -- Unit is re-integrating, waiting till re-integration counter
            -- expires!
            -------------------------------------------------------------------
1209
            when s_pc_reintegrating =>            
1210
                if (reinteg_ctr_expired = '1' and ctrl_ctr_zero = '1') then
1211 1212 1213 1214 1215 1216 1217
                    next_state <= s_pc_idle;
                end if;
                
            -------------------------------------------------------------------
            -- Active error flag.
            -------------------------------------------------------------------
            when s_pc_act_err_flag =>
1218
                if (ctrl_ctr_zero = '1') then
1219 1220 1221 1222 1223 1224 1225
                    next_state <= s_pc_err_delim_wait;
                end if;
            
            -------------------------------------------------------------------
            -- Passive error flag.
            -------------------------------------------------------------------
            when s_pc_pas_err_flag =>
1226
                if (ctrl_ctr_zero = '1') then
1227 1228 1229 1230 1231 1232 1233
                    next_state <= s_pc_err_delim_wait;
                end if;
            
            -------------------------------------------------------------------
            -- Wait till Error delimiter (detection of recessive bit)
            -------------------------------------------------------------------
            when s_pc_err_delim_wait =>
1234
                if (rx_data_nbs = RECESSIVE) then
1235
                    next_state <= s_pc_err_delim;
1236 1237
                elsif (ctrl_ctr_zero = '1') then
                    next_state <= s_pc_err_ovr_flag_too_long;
1238
                end if;
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248

            -------------------------------------------------------------------
            -- 13 dominant bits (6 Error flag + 7 Error delimiter) has been
            -- detected.
            -------------------------------------------------------------------
            when s_pc_err_ovr_flag_too_long =>
                if (rx_data_nbs = RECESSIVE) then
                    next_state <= s_pc_err_delim;
                end if;

1249 1250 1251 1252
            -------------------------------------------------------------------
            -- Error delimiter
            -------------------------------------------------------------------
            when s_pc_err_delim =>
1253
                if (ctrl_ctr_zero = '1') then
1254
                    if (rx_data_nbs = DOMINANT) then
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
                        next_state <= s_pc_ovr_flag;
                    else
                        next_state <= s_pc_intermission;
                    end if;
                end if;
            
            -------------------------------------------------------------------
            -- Overload flag
            -------------------------------------------------------------------
            when s_pc_ovr_flag =>
1265
                if (ctrl_ctr_zero = '1') then
1266 1267 1268 1269 1270 1271 1272
                    next_state <= s_pc_ovr_delim_wait;
                end if;
            
            -------------------------------------------------------------------
            -- Wait till overload delimiter.
            -------------------------------------------------------------------
            when s_pc_ovr_delim_wait =>
1273
                if (rx_data_nbs = RECESSIVE) then
1274
                    next_state <= s_pc_ovr_delim;
1275 1276
                elsif (ctrl_ctr_zero = '1') then
                    next_state <= s_pc_err_ovr_flag_too_long;
1277 1278 1279 1280 1281 1282
                end if;

            -------------------------------------------------------------------
            -- Overload delimiter
            -------------------------------------------------------------------
            when s_pc_ovr_delim  =>
1283
                if (ctrl_ctr_zero = '1') then
1284
                    if (rx_data_nbs = DOMINANT) then
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296
                        next_state <= s_pc_ovr_flag;
                    else
                        next_state <= s_pc_intermission;
                    end if;
                end if;
            end case;
        end if;
    end process;
    
    ---------------------------------------------------------------------------
    -- Current state process
    ---------------------------------------------------------------------------
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1297
    curr_state_proc : process(
1298
        curr_state, err_frm_req, sp_control_q_i, tx_failed, drv_ena, rx_data_nbs,
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1299 1300 1301 1302 1303
        ctrl_ctr_zero, arbitration_lost_condition, tx_data_wbs, is_transmitter,
        tran_ident_type, tran_frame_type, tran_is_rtr, ide_is_arbitration,
        drv_can_fd_ena, tran_brs, rx_trigger, is_err_active, no_data_field,
        drv_fd_type, ctrl_counted_byte, ctrl_counted_byte_index, is_fd_frame,
        is_receiver, crc_match, drv_ack_forb, drv_self_test_ena, tx_frame_ready,
1304
        go_to_suspend, frame_start, ctrl_ctr_one, drv_bus_off_reset_q,
1305
        reinteg_ctr_expired, first_err_delim_q, go_to_stuff_count,
1306
        crc_length_i, data_length_bits_c, ctrl_ctr_mem_index, is_bus_off
1307
        )
1308
    begin
1309 1310 1311 1312

        -----------------------------------------------------------------------
        -- Default values
        -----------------------------------------------------------------------
1313 1314
        
        -- Control counter
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1315
        ctrl_ctr_pload_i     <= '0';
1316
        ctrl_ctr_pload_val   <= (OTHERS => '0');
Ille, Ondrej, Ing.'s avatar
Ille, Ondrej, Ing. committed
1317 1318
        ctrl_ctr_ena         <= '0';
        compl_ctr_ena_i      <= '0';