1 | Library ieee;
|
2 | use ieee.std_logic_1164.all;
|
3 | use ieee.numeric_std.all;
|
4 | use ieee.std_logic_arith.all;
|
5 |
|
6 | entity uart_rx is
|
7 | generic (
|
8 | baudrate_clk_count : integer:=5029
|
9 | );
|
10 |
|
11 | port (
|
12 | i_inclk : in std_logic;
|
13 | i_serial_bit : in std_logic;
|
14 | i_rx_data_bits : out std_logic_vector(0 to 7):=(others=>'0');
|
15 | i_rx_done : out std_logic;
|
16 | test_bench_test : out std_logic;
|
17 | test_led : out std_logic:='0';
|
18 | test_gpio : out std_logic;
|
19 | o_rx_line_busy : out std_logic
|
20 |
|
21 | );
|
22 |
|
23 | end uart_rx;
|
24 |
|
25 | architecture behav of uart_rx is
|
26 |
|
27 | type rx_main_states is(rx_idle,rx_start,rx_data,rx_stop);
|
28 | signal rx_state: rx_main_states:= rx_idle;
|
29 |
|
30 | signal baud_rate_tic : std_logic:='0';
|
31 |
|
32 | signal i_rx_data_byte : std_logic_vector(0 to 7):=(others=>'0');
|
33 |
|
34 | signal baud_rate_counter : integer range 0 to baudrate_clk_count-1:=0;
|
35 | signal rx_data_r : std_logic:='0';
|
36 | signal r_RX_Data_R : std_logic:='0';
|
37 | signal index_reg : integer range 0 to 7:=0;
|
38 |
|
39 |
|
40 |
|
41 | signal baud_rate_clock : std_logic:='0';
|
42 |
|
43 | signal delay_counter : integer:=0;
|
44 |
|
45 |
|
46 | begin
|
47 |
|
48 | --Baud rate clock process
|
49 | process(i_inclk)
|
50 | begin
|
51 | if(rising_edge(i_inclk))then
|
52 | if(baud_rate_counter<baudrate_clk_count)then
|
53 | baud_rate_counter <= baud_rate_counter+1;
|
54 | baud_rate_tic <= '0';
|
55 | else
|
56 | baud_rate_counter <= 0;
|
57 | baud_rate_clock <= not(baud_rate_clock);
|
58 | baud_rate_tic <= '1';
|
59 | end if;
|
60 | end if;
|
61 | end process;
|
62 |
|
63 | test_bench_test <= baud_rate_clock;
|
64 |
|
65 |
|
66 | --Recieving the UART data frame
|
67 | process(i_inclk)
|
68 | begin
|
69 | if(rising_edge(i_inclk))then
|
70 | if(baud_rate_tic='1')then
|
71 | r_RX_Data_R <= i_serial_bit;
|
72 | rx_data_r <= r_RX_Data_R;
|
73 | end if;
|
74 | end if;
|
75 | end process;
|
76 |
|
77 | --UART receiption process
|
78 | process(i_inclk)
|
79 | begin
|
80 | if(rising_edge(i_inclk))then
|
81 | if(baud_rate_tic='1')then
|
82 |
|
83 | case rx_state is
|
84 |
|
85 | when rx_idle =>
|
86 |
|
87 | index_reg <= 0;
|
88 | o_rx_line_busy <= '0';
|
89 | i_rx_done <= '0';
|
90 | rx_state <= rx_start;
|
91 |
|
92 | when rx_start =>
|
93 |
|
94 | if(rx_data_r = '0')then
|
95 | o_rx_line_busy <= '1';
|
96 | rx_state <= rx_data;
|
97 | else
|
98 | rx_state <= rx_start;
|
99 | end if;
|
100 |
|
101 | when rx_data =>
|
102 | i_rx_data_byte(index_reg) <= rx_data_r;
|
103 |
|
104 |
|
105 | if(index_reg<7)then
|
106 | index_reg <= index_reg+1;
|
107 | rx_state <= rx_data;
|
108 | else
|
109 | index_reg<= 0;
|
110 | rx_state <= rx_stop;
|
111 | end if;
|
112 |
|
113 | when rx_stop =>
|
114 |
|
115 | if(rx_data_r = '1')then
|
116 | o_rx_line_busy <= '0';
|
117 | i_rx_done <= '1';
|
118 | test_led <= '1';
|
119 | rx_state <= rx_idle;
|
120 | else
|
121 | rx_state <= rx_stop;
|
122 | end if;
|
123 |
|
124 | when others =>
|
125 | rx_state <= rx_idle;
|
126 |
|
127 | end case;
|
128 | end if;
|
129 | end if;
|
130 | end process;
|
131 |
|
132 | i_rx_data_bits <= i_rx_data_byte;
|
133 | test_gpio <= i_serial_bit;
|
134 |
|
135 | end behav;
|