Altera FPGA硬件(有问题)与ModelSim仿真(ok) - 自实现UART

时间:2016-05-23 23:02:38

标签: vhdl fpga intel-fpga

我在VHDL中遇到了自我实现的UART问题。

我编写的VHDL代码在Altera ModelSim上运行时生成正确的波形:

enter image description here

UART.vhd:

LIBRARY ieee;
    USE ieee.std_logic_1164.all;
    use ieee.numeric_std.ALL;

entity UART is
    port (
        clk_10mhz: in STD_LOGIC;
        uart_clk: out STD_LOGIC;
        txPin: out STD_LOGIC
    );
end entity;

architecture Test of UART is
    signal txStart: STD_LOGIC := '0';
    signal txIdle: STD_LOGIC;
    signal txData: STD_LOGIC_VECTOR(7 downto 0);

    component TX is
        port (
            clk_in: in STD_LOGIC;
            start: in STD_LOGIC;
            data: in STD_LOGIC_VECTOR(7 downto 0);
            tx: out STD_LOGIC;
            txIdle: out STD_LOGIC;
            debug_clk: out STD_LOGIC
        );
    end component TX;


begin
    process (clk_10mhz) 
        variable clkDividerCounter : integer range 0 to 10000000;
        variable textToSend : string(1 to 31) := "Hello darkness my old friend!" & LF & CR; 
        variable currentCharacterIndex : integer range 1 to 31 := 1;
        variable startSending : std_logic := '0';
        variable characterReceivedByTX : std_logic := '1';
    begin       
        if (rising_edge(clk_10mhz)) then
            if (startSending = '1') then
                if (txIdle = '0') then
                    characterReceivedByTX := '1';
                end if;

                if (txIdle = '1' and characterReceivedByTX = '1') then
                    txData <= std_logic_vector(to_unsigned(character'pos(textToSend(currentCharacterIndex)), 8));
                    txStart <= '1';

                    if (currentCharacterIndex < 31) then
                        currentCharacterIndex := currentCharacterIndex + 1;
                        characterReceivedByTX := '0';
                    else 
                        txStart <= '0';
                        currentCharacterIndex := 1;
                        startSending := '0';
                    end if;
                end if;
            else
                if (clkDividerCounter < 10000000) then
                    clkDividerCounter := clkDividerCounter + 1;
                    startSending := '0';
                else
                    clkDividerCounter := 0;
                    startSending := '1';
                end if;
            end if;
        end if;
    end process;


    u1: TX port map (clk_10mhz, txStart, txData, txPin, txIdle, uart_clk);
end Test;

TX.vhd:

LIBRARY ieee;
    USE ieee.std_logic_1164.all;
    use ieee.numeric_std.ALL;

entity TX is
    port (
        clk_in: in STD_LOGIC;
        start: in STD_LOGIC;
        data: in STD_LOGIC_VECTOR(7 downto 0);

        tx: out STD_LOGIC := '1';
        txIdle: out STD_LOGIC := '1';
        debug_clk: out STD_LOGIC := '0'

    );
end entity;

architecture Test of TX is
    signal idle: STD_LOGIC := '1';
begin
    process (clk_in) 
        variable bitIndex : integer range 0 to 9;
        variable clkDividerCounter : integer range 0 to 1042;

        variable dataFrame : STD_LOGIC_VECTOR(9 downto 0);
        variable dataFrameCurrentIndex : integer range 0 to 9;
    begin
        if (rising_edge(clk_in)) then
            if (start = '1' and idle = '1') then
                dataFrame(0) := '0';
                dataFrame(8 downto 1) := data;
                dataFrame(9) := '1';

                dataFrameCurrentIndex := 0;

                idle <= '0';
            end if;


            if (idle = '0') then                
                if (clkDividerCounter < 521) then
                    debug_clk <= '0';
                else
                    debug_clk <= '1';
                end if;

                if (clkDividerCounter < 1041) then
                    clkDividerCounter := clkDividerCounter + 1;
                else
                    if (dataFrameCurrentIndex < 9) then
                        tx <= dataFrame(dataFrameCurrentIndex);
                        dataFrameCurrentIndex := dataFrameCurrentIndex + 1;
                    else 
                        tx <= dataFrame(dataFrameCurrentIndex);
                        idle <= '1';
                    end if;

                    clkDividerCounter := 0;
                end if;
            else
                debug_clk <= '0';
            end if;

        end if;
    end process;


    txIdle <= idle; 
end Test;

不幸的是,在硬件上,而不是“你好黑暗我的老朋友!”发送,它发送“HHello黑暗我的老朋友!”开头用双H。

我在SignalTap II上检查了它,波形确认了问题:

enter image description here

什么可能导致这个问题?我该如何调试这样的问题?

0 个答案:

没有答案