1
1
-- ------------------------------------------------------------------------------
2
2
-- PROJECT: SIMPLE UART FOR FPGA
3
3
-- ------------------------------------------------------------------------------
4
- -- MODULE: UART RECEIVER
5
4
-- AUTHORS: Jakub Cabal <jakubcabal@gmail.com>
6
5
-- LICENSE: The MIT License (MIT), please read LICENSE file
7
6
-- WEBSITE: https://github.com/jakubcabal/uart-for-fpga
10
9
library IEEE;
11
10
use IEEE.STD_LOGIC_1164.ALL ;
12
11
use IEEE.NUMERIC_STD.ALL ;
13
- use IEEE.MATH_REAL.ALL ;
14
12
15
13
entity UART_RX is
16
14
Generic (
17
- CLK_FREQ : integer := 50e6 ; -- system clock frequency in Hz
18
- BAUD_RATE : integer := 115200 ; -- baud rate value
19
- PARITY_BIT : string := " none" -- type of parity: "none", "even", "odd", "mark", "space"
15
+ CLK_DIV_VAL : integer := 16 ;
16
+ PARITY_BIT : string := " none" -- type of parity: "none", "even", "odd", "mark", "space"
20
17
);
21
18
Port (
22
19
CLK : in std_logic ; -- system clock
@@ -34,19 +31,14 @@ end entity;
34
31
35
32
architecture RTL of UART_RX is
36
33
37
- constant OS_CLK_DIV_VAL : integer := integer (real (CLK_FREQ)/ real (16 * BAUD_RATE));
38
- constant UART_CLK_DIV_VAL : integer := integer (real (CLK_FREQ)/ real (OS_CLK_DIV_VAL* BAUD_RATE));
39
- constant UART_CLK_DIV_WIDTH : integer := integer (ceil (log2 (real (UART_CLK_DIV_VAL))));
40
-
41
34
signal rx_clk_en : std_logic ;
42
- signal rx_ticks : unsigned (UART_CLK_DIV_WIDTH- 1 downto 0 );
43
35
signal rx_data : std_logic_vector (7 downto 0 );
44
36
signal rx_bit_count : unsigned (2 downto 0 );
45
37
signal rx_parity_bit : std_logic ;
46
38
signal rx_parity_error : std_logic ;
47
39
signal rx_parity_check_en : std_logic ;
48
40
signal rx_done : std_logic ;
49
- signal fsm_receiving : std_logic ;
41
+ signal fsm_idle : std_logic ;
50
42
signal fsm_databits : std_logic ;
51
43
signal fsm_stopbit : std_logic ;
52
44
@@ -60,37 +52,18 @@ begin
60
52
-- UART RECEIVER CLOCK DIVIDER AND CLOCK ENABLE FLAG
61
53
-- -------------------------------------------------------------------------
62
54
63
- uart_rx_clk_divider_p : process (CLK)
64
- begin
65
- if (rising_edge (CLK)) then
66
- if (fsm_receiving = '1' ) then
67
- if (UART_CLK_EN = '1' ) then
68
- if (rx_ticks = UART_CLK_DIV_VAL- 1 ) then
69
- rx_ticks <= (others => '0' );
70
- else
71
- rx_ticks <= rx_ticks + 1 ;
72
- end if ;
73
- else
74
- rx_ticks <= rx_ticks;
75
- end if ;
76
- else
77
- rx_ticks <= (others => '0' );
78
- end if ;
79
- end if ;
80
- end process ;
81
-
82
- uart_rx_clk_en_p : process (CLK)
83
- begin
84
- if (rising_edge (CLK)) then
85
- if (RST = '1' ) then
86
- rx_clk_en <= '0' ;
87
- elsif (UART_CLK_EN = '1' AND rx_ticks = 3 ) then
88
- rx_clk_en <= '1' ;
89
- else
90
- rx_clk_en <= '0' ;
91
- end if ;
92
- end if ;
93
- end process ;
55
+ rx_clk_divider_i : entity work .UART_CLK_DIV
56
+ generic map (
57
+ DIV_MAX_VAL => CLK_DIV_VAL,
58
+ DIV_MARK_POS => 3
59
+ )
60
+ port map (
61
+ CLK => CLK,
62
+ RST => RST,
63
+ CLEAR => fsm_idle,
64
+ ENABLE => UART_CLK_EN,
65
+ DIV_MARK => rx_clk_en
66
+ );
94
67
95
68
-- -------------------------------------------------------------------------
96
69
-- UART RECEIVER BIT COUNTER
@@ -198,9 +171,9 @@ begin
198
171
case fsm_pstate is
199
172
200
173
when idle =>
201
- fsm_stopbit <= '0' ;
202
- fsm_databits <= '0' ;
203
- fsm_receiving <= '0 ' ;
174
+ fsm_stopbit <= '0' ;
175
+ fsm_databits <= '0' ;
176
+ fsm_idle <= '1 ' ;
204
177
205
178
if (UART_RXD = '0' ) then
206
179
fsm_nstate <= startbit;
@@ -209,9 +182,9 @@ begin
209
182
end if ;
210
183
211
184
when startbit =>
212
- fsm_stopbit <= '0' ;
213
- fsm_databits <= '0' ;
214
- fsm_receiving <= '1 ' ;
185
+ fsm_stopbit <= '0' ;
186
+ fsm_databits <= '0' ;
187
+ fsm_idle <= '0 ' ;
215
188
216
189
if (rx_clk_en = '1' ) then
217
190
fsm_nstate <= databits;
@@ -220,9 +193,9 @@ begin
220
193
end if ;
221
194
222
195
when databits =>
223
- fsm_stopbit <= '0' ;
224
- fsm_databits <= '1' ;
225
- fsm_receiving <= '1 ' ;
196
+ fsm_stopbit <= '0' ;
197
+ fsm_databits <= '1' ;
198
+ fsm_idle <= '0 ' ;
226
199
227
200
if ((rx_clk_en = '1' ) AND (rx_bit_count = "111" )) then
228
201
if (PARITY_BIT = " none" ) then
@@ -235,9 +208,9 @@ begin
235
208
end if ;
236
209
237
210
when paritybit =>
238
- fsm_stopbit <= '0' ;
239
- fsm_databits <= '0' ;
240
- fsm_receiving <= '1 ' ;
211
+ fsm_stopbit <= '0' ;
212
+ fsm_databits <= '0' ;
213
+ fsm_idle <= '0 ' ;
241
214
242
215
if (rx_clk_en = '1' ) then
243
216
fsm_nstate <= stopbit;
@@ -246,9 +219,9 @@ begin
246
219
end if ;
247
220
248
221
when stopbit =>
249
- fsm_stopbit <= '1' ;
250
- fsm_databits <= '0' ;
251
- fsm_receiving <= '1 ' ;
222
+ fsm_stopbit <= '1' ;
223
+ fsm_databits <= '0' ;
224
+ fsm_idle <= '0 ' ;
252
225
253
226
if (rx_clk_en = '1' ) then
254
227
fsm_nstate <= idle;
@@ -257,10 +230,10 @@ begin
257
230
end if ;
258
231
259
232
when others =>
260
- fsm_stopbit <= '0' ;
261
- fsm_databits <= '0' ;
262
- fsm_receiving <= '0' ;
263
- fsm_nstate <= idle;
233
+ fsm_stopbit <= '0' ;
234
+ fsm_databits <= '0' ;
235
+ fsm_idle <= '0' ;
236
+ fsm_nstate <= idle;
264
237
265
238
end case ;
266
239
end process ;
0 commit comments