source: firmware/FTU/test_firmware/FTU_test6/FTU_test6_rs485_interface.vhd@ 14468

Last change on this file since 14468 was 9621, checked in by weitzel, 14 years ago
FTU_test6 added (RS485 receiver)
File size: 3.7 KB
Line 
1--
2-- VHDL Architecture FACT_FAD_lib.rs485_interface.beha
3--
4-- Created:
5-- by - Benjamin Krumm.UNKNOWN (EEPC8)
6-- at - 13:24:23 08.06.2010
7--
8-- using Mentor Graphics HDL Designer(TM) 2009.1 (Build 12)
9--
10--
11-- modified by Q. Weitzel, 30 July 2010
12--
13--
14LIBRARY ieee;
15USE ieee.std_logic_1164.all;
16USE ieee.std_logic_arith.all;
17
18ENTITY FTU_test6_rs485_interface IS
19 GENERIC(
20 CLOCK_FREQUENCY : integer := 50000000; -- Hertz
21 BAUD_RATE : integer := 250000 -- bits / sec
22 );
23 PORT(
24 clk : IN std_logic;
25 -- RS485
26 rx_d : IN std_logic;
27 rx_en : OUT std_logic;
28 tx_d : OUT std_logic;
29 tx_en : OUT std_logic;
30 -- FPGA
31 rx_data : OUT std_logic_vector (7 DOWNTO 0);
32 rx_busy : OUT std_logic := '0';
33 rx_valid : OUT std_logic := '0';
34 tx_data : IN std_logic_vector (7 DOWNTO 0);
35 tx_busy : OUT std_logic := '0';
36 tx_start : IN std_logic
37 );
38
39-- Declarations
40
41END FTU_test6_rs485_interface;
42
43ARCHITECTURE beha OF FTU_test6_rs485_interface IS
44
45 signal tx_start_f : std_logic := '0';
46 signal tx_sr : std_logic_vector(10 downto 0) := (others => '1'); -- start bit, 8 data bits, 2 stop bit
47 signal tx_bitcnt : integer range 0 to 11 := 11;
48 signal tx_cnt : integer range 0 to ((CLOCK_FREQUENCY / BAUD_RATE) - 1);
49
50 signal flow_ctrl : std_logic := '0'; -- '0' -> RX enable, '1' -> TX enable
51
52 signal rx_dsr : std_logic_vector(3 downto 0) := (others => '1');
53 signal rx_sr : std_logic_vector(7 downto 0) := (others => '0');
54 signal rx_bitcnt : integer range 0 to 11 := 11;
55 signal rx_cnt : integer range 0 to ((CLOCK_FREQUENCY / BAUD_RATE) - 1);
56
57BEGIN
58
59
60 -- Senden
61 tx_data_proc: process(clk)
62 begin
63 if rising_edge(clk) then
64 tx_start_f <= tx_start;
65 if (tx_start = '1' or tx_bitcnt < 11) then
66 flow_ctrl <= '1';
67 else
68 flow_ctrl <= '0';
69 end if;
70 if (tx_start = '1' and tx_start_f = '0') then -- steigende Flanke, los gehts
71 tx_cnt <= 0; -- Zaehler initialisieren
72 tx_bitcnt <= 0;
73 tx_sr <= "11" & tx_data & '0'; -- 2 x Stopbit, 8 Datenbits, Startbit, rechts gehts los
74 else
75 if (tx_cnt < (CLOCK_FREQUENCY/BAUD_RATE) - 1) then
76 tx_cnt <= tx_cnt + 1;
77 else -- naechstes Bit ausgeben
78 if (tx_bitcnt < 11) then
79 tx_cnt <= 0;
80 tx_bitcnt <= tx_bitcnt + 1;
81 tx_sr <= '1' & tx_sr(tx_sr'left downto 1);
82 end if;
83 end if;
84 end if;
85 end if;
86 end process;
87
88 tx_en <= flow_ctrl;
89 tx_d <= tx_sr(0); -- LSB first
90 tx_busy <= '1' when (tx_start = '1' or tx_bitcnt < 11) else '0';
91
92 -- Empfangen
93 rx_data_proc: process(clk)
94 begin
95 if rising_edge(clk) then
96 rx_dsr <= rx_dsr(rx_dsr'left - 1 downto 0) & rx_d;
97 if (rx_bitcnt < 11) then -- Empfang laeuft
98 if (rx_cnt < (CLOCK_FREQUENCY/BAUD_RATE) - 1) then
99 rx_cnt <= rx_cnt + 1;
100 else
101 rx_cnt <= 0;
102 rx_bitcnt <= rx_bitcnt + 1;
103 if (rx_bitcnt < 9) then
104 rx_sr <= rx_dsr(rx_dsr'left - 1) & rx_sr(rx_sr'left downto 1); -- rechts schieben, weil LSB first
105 else
106 rx_valid <= '1';
107 end if;
108 end if;
109 else
110 if (rx_dsr(3 downto 2) = "10") then -- warten auf Start bit
111 rx_valid <= '0';
112 rx_cnt <= ((CLOCK_FREQUENCY / BAUD_RATE) - 1) / 2;
113 rx_bitcnt <= 0;
114 end if;
115 end if;
116 end if;
117 end process;
118
119 rx_en <= flow_ctrl;
120 rx_data <= rx_sr;
121 rx_busy <= '1' when (rx_bitcnt < 11) else '0';
122
123END ARCHITECTURE beha;
124
Note: See TracBrowser for help on using the repository browser.