OpenCores
URL https://opencores.org/ocsvn/rs232_interface/rs232_interface/trunk

Subversion Repositories rs232_interface

[/] [rs232_interface/] [trunk/] [uart.vhd] - Blame information for rev 7

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 akram.mash
----------------------------------------------------------------------------------
2 6 akram.mash
-- Creation Date: 21:12:48 05/06/2010 
3
-- Module Name: RS232/UART Interface - Behavioral
4 3 akram.mash
-- Used TAB of 4 Spaces
5
----------------------------------------------------------------------------------
6
library IEEE;
7
use IEEE.STD_LOGIC_1164.ALL;
8
use IEEE.STD_LOGIC_ARITH.ALL;
9
use IEEE.STD_LOGIC_UNSIGNED.ALL;
10
 
11
entity uart is
12
generic (
13
        CLK_FREQ        : integer := 50;                -- Main frequency (MHz)
14 5 akram.mash
        SER_FREQ        : integer := 9600               -- Baud rate (bps)
15 3 akram.mash
);
16
port (
17
        -- Control
18 5 akram.mash
        clk                     : in    std_logic;              -- Main clock
19
        rst                     : in    std_logic;              -- Main reset
20 3 akram.mash
        -- External Interface
21 5 akram.mash
        rx                      : in    std_logic;              -- RS232 received serial data
22
        tx                      : out   std_logic;              -- RS232 transmitted serial data
23 7 akram.mash
        -- RS232/UART Configuration
24
        par_en          : in    std_logic;              -- Parity bit enable
25 3 akram.mash
        -- uPC Interface
26 5 akram.mash
        tx_req          : in    std_logic;                                              -- Request SEND of data
27
        tx_end          : out   std_logic;                                              -- Data SENDED
28
        tx_data         : in    std_logic_vector(7 downto 0);    -- Data to transmit
29
        rx_ready        : out   std_logic;                                              -- Received data ready to uPC read
30
        rx_data         : out   std_logic_vector(7 downto 0)     -- Received data 
31 3 akram.mash
);
32
end uart;
33
 
34
architecture Behavioral of uart is
35
 
36
        -- Constants
37 7 akram.mash
        constant UART_IDLE      :       std_logic := '1';
38
        constant UART_START     :       std_logic := '0';
39
        constant PARITY_EN      :       std_logic := '1';
40
        constant RST_LVL        :       std_logic := '1';
41 3 akram.mash
 
42
        -- Types
43 7 akram.mash
        type state is (idle,data,parity,stop1,stop2);                   -- Stop1 and Stop2 are inter frame gap
44 3 akram.mash
 
45
        -- Signals
46 7 akram.mash
        signal rx_fsm           :       state;                                                  -- Control of reception
47
        signal tx_fsm           :       state;                                                  -- Control of transmission
48
        signal clock_en         :       std_logic;                                              -- Internal clock enable
49 3 akram.mash
 
50 7 akram.mash
        -- RX Data Temp
51
        signal rx_par_bit       :       std_logic;
52 3 akram.mash
        signal rx_data_tmp      :       std_logic_vector(7 downto 0);
53 7 akram.mash
        signal rx_data_cnt      :       std_logic_vector(2 downto 0);
54
 
55
        -- TX Data Temp
56
        signal tx_par_bit       :       std_logic;
57 3 akram.mash
        signal tx_data_tmp      :       std_logic_vector(7 downto 0);
58 7 akram.mash
        signal tx_data_cnt      :       std_logic_vector(2 downto 0);
59 3 akram.mash
 
60
begin
61
 
62
        clock_manager:process(clk)
63
                variable counter        :       integer range 0 to conv_integer((CLK_FREQ*1_000_000)/SER_FREQ-1);
64
        begin
65
                if clk'event and clk = '1' then
66
                        -- Normal Operation
67
                        if counter = (CLK_FREQ*1_000_000)/SER_FREQ-1 then
68 6 akram.mash
                                clock_en        <=      '1';
69
                                counter         :=      0;
70 3 akram.mash
                        else
71 6 akram.mash
                                clock_en        <=      '0';
72
                                counter         :=      counter + 1;
73 3 akram.mash
                        end if;
74
                        -- Reset condition
75 7 akram.mash
                        if rst = RST_LVL then
76 3 akram.mash
                                counter         :=      0;
77
                        end if;
78
                end if;
79
        end process;
80
 
81
        tx_proc:process(clk)
82
                variable data_cnt       : std_logic_vector(2 downto 0);
83
        begin
84
                if clk'event and clk = '1' then
85
                        if clock_en = '1' then
86
                                -- Default values
87 6 akram.mash
                                tx_end                                  <=      '0';
88 7 akram.mash
                                tx                                              <=      UART_IDLE;
89 3 akram.mash
                                -- FSM description
90
                                case tx_fsm is
91
                                        -- Wait to transfer data
92
                                        when idle =>
93
                                                -- Send Init Bit
94
                                                if tx_req = '1' then
95 7 akram.mash
                                                        tx                      <=      UART_START;
96 3 akram.mash
                                                        tx_data_tmp     <=      tx_data;
97 6 akram.mash
                                                        tx_fsm          <=      data;
98 7 akram.mash
                                                        tx_data_cnt     <=      (others=>'1');
99
                                                        tx_par_bit      <=      '0';
100 3 akram.mash
                                                end if;
101
                                        -- Data receive
102
                                        when data =>
103 7 akram.mash
                                                tx                              <=      tx_data_tmp(0);
104
                                                tx_par_bit              <=      tx_par_bit xor tx_data_tmp(0);
105
                                                if tx_data_cnt = 0 then
106
                                                        if par_en = PARITY_EN then
107
                                                                tx_fsm  <=      parity;
108
                                                        else
109
                                                                tx_fsm  <=      stop1;
110
                                                        end if;
111
                                                        tx_data_cnt     <=      (others=>'1');
112 3 akram.mash
                                                else
113
                                                        tx_data_tmp     <=      '0' & tx_data_tmp(7 downto 1);
114 7 akram.mash
                                                        tx_data_cnt     <=      tx_data_cnt - 1;
115 3 akram.mash
                                                end if;
116 7 akram.mash
                                        when parity =>
117
                                                tx                              <=      tx_par_bit;
118
                                                tx_fsm                  <=      stop1;
119 3 akram.mash
                                        -- End of communication
120
                                        when stop1 =>
121
                                                -- Send Stop Bit
122 7 akram.mash
                                                tx                              <=      UART_IDLE;
123 3 akram.mash
                                                tx_fsm                  <=      stop2;
124
                                        when stop2 =>
125
                                                -- Send Stop Bit
126 6 akram.mash
                                                tx_end                  <=      '1';
127 7 akram.mash
                                                tx                              <=      UART_IDLE;
128 3 akram.mash
                                                tx_fsm                  <=      idle;
129
                                        -- Invalid States
130
                                        when others => null;
131
                                end case;
132
                                -- Reset condition
133 7 akram.mash
                                if rst = RST_LVL then
134 3 akram.mash
                                        tx_fsm                          <=      idle;
135 7 akram.mash
                                        tx_par_bit                      <=      '0';
136 6 akram.mash
                                        tx_data_tmp                     <=      (others=>'0');
137 7 akram.mash
                                        tx_data_cnt                     <=      (others=>'0');
138 3 akram.mash
                                end if;
139
                        end if;
140
                end if;
141
        end process;
142
 
143
        rx_proc:process(clk)
144
        begin
145
                if clk'event and clk = '1' then
146
                        if clock_en = '1' then
147
                                -- Default values
148 7 akram.mash
                                rx_ready                <=      '0';
149 3 akram.mash
                                -- FSM description
150
                                case rx_fsm is
151
                                        -- Wait to transfer data
152
                                        when idle =>
153 7 akram.mash
                                                if rx = UART_START then
154 3 akram.mash
                                                        rx_fsm          <=      data;
155
                                                end if;
156 7 akram.mash
                                                rx_par_bit              <=      '0';
157
                                                rx_data_cnt             <=      (others=>'0');
158 3 akram.mash
                                        -- Data receive
159
                                        when data =>
160 7 akram.mash
                                                -- Check data to generate parity
161
                                                if par_en = PARITY_EN then
162
                                                        rx_par_bit              <=      rx_par_bit xor rx;
163
                                                end if;
164
 
165
                                                if rx_data_cnt = 7 then
166
                                                        -- Data path
167
                                                        rx_data(7)              <=      rx;
168 3 akram.mash
                                                        for i in 0 to 6 loop
169 7 akram.mash
                                                                rx_data(i)      <=      rx_data_tmp(6-i);
170 3 akram.mash
                                                        end loop;
171 7 akram.mash
 
172
                                                        -- With parity verification
173
                                                        if par_en = PARITY_EN then
174
                                                                rx_fsm          <=      parity;
175
                                                        -- Without parity verification
176
                                                        else
177
                                                                rx_ready        <=      '1';
178
                                                                rx_fsm          <=      idle;
179
                                                        end if;
180 3 akram.mash
                                                else
181 7 akram.mash
                                                        rx_data_tmp             <=      rx_data_tmp(6 downto 0) & rx;
182
                                                        rx_data_cnt             <=      rx_data_cnt + 1;
183 3 akram.mash
                                                end if;
184 7 akram.mash
                                        when parity =>
185
                                                -- Check received parity
186
                                                rx_fsm                          <=      idle;
187
                                                if rx_par_bit = rx then
188
                                                        rx_ready                <=      '1';
189
                                                end if;
190 3 akram.mash
                                        when others => null;
191
                                end case;
192
                                -- Reset condition
193 7 akram.mash
                                if rst = RST_LVL then
194 3 akram.mash
                                        rx_fsm                  <=      idle;
195 6 akram.mash
                                        rx_ready                <=      '0';
196
                                        rx_data                 <=      (others=>'0');
197
                                        rx_data_tmp             <=      (others=>'0');
198 7 akram.mash
                                        rx_data_cnt             <=      (others=>'0');
199 3 akram.mash
                                end if;
200
                        end if;
201
                end if;
202
        end process;
203
 
204
end Behavioral;
205
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.