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

Subversion Repositories uart_block

[/] [uart_block/] [trunk/] [hdl/] [iseProject/] [uart_control.vhd] - Blame information for rev 11

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

Line No. Rev Author Line
1 9 leonardoar
--! uart control unit
2
library IEEE;
3
use IEEE.STD_LOGIC_1164.ALL;
4
 
5
--! Use CPU Definitions package
6
use work.pkgDefinitions.all;
7
 
8
entity uart_control is
9 10 leonardoar
    Port ( rst : in  STD_LOGIC;                                                                                                         -- Global reset
10
           clk : in  STD_LOGIC;                                                                                                         -- Global clock
11
                          WE    : in STD_LOGIC;                                                                                                         -- Write enable
12
           reg_addr : in  STD_LOGIC_VECTOR (1 downto 0);                                         -- Register address
13
                          start : in std_logic;                                                                                                         -- Start (Strobe)
14
                          done : out std_logic;                                                                                                         -- Done (ACK)
15
           DAT_I : in  STD_LOGIC_VECTOR ((nBitsLarge-1) downto 0);               -- Data Input (Wishbone)
16
           DAT_O : out  STD_LOGIC_VECTOR ((nBitsLarge-1) downto 0);              -- Data output (Wishbone)
17
                          baud_wait : out STD_LOGIC_VECTOR ((nBitsLarge-1) downto 0);    -- Signal to control the baud rate frequency
18
                          data_byte_tx : out std_logic_vector((nBits-1) downto 0);               -- 1 Byte to be send to serial_transmitter
19
                          data_byte_rx : in std_logic_vector((nBits-1) downto 0);        -- 1 Byte to be received by serial_receiver
20
           tx_data_sent : in  STD_LOGIC;                                                                                        -- Signal comming from serial_transmitter
21
           rx_data_ready : in  STD_LOGIC);                                                                              -- Signal comming from serial_receiver
22 9 leonardoar
end uart_control;
23
 
24
architecture Behavioral of uart_control is
25
signal config_clk : std_logic_vector((nBitsLarge-1) downto 0);
26
signal config_baud : std_logic_vector((nBitsLarge-1) downto 0);
27 11 leonardoar
signal byte_to_receive : std_logic_vector((nBits-1) downto 0);
28
signal byte_to_transmitt : std_logic_vector((nBits-1) downto 0);
29 9 leonardoar
signal controlStates : uartControl;
30
 
31
signal sigDivRst : std_logic;
32
signal sigDivDone : std_logic;
33
signal sigDivQuotient : std_logic_vector((nBitsLarge-1) downto 0);
34
signal sigDivReminder : std_logic_vector((nBitsLarge-1) downto 0);
35
signal sigDivNumerator : std_logic_vector((nBitsLarge-1) downto 0);
36
signal sigDivDividend : std_logic_vector((nBitsLarge-1) downto 0);
37
 
38
-- Divisor component
39
component divisor is
40
    Port ( rst : in  STD_LOGIC;
41
           clk : in  STD_LOGIC;
42
           quotient : out  STD_LOGIC_VECTOR ((nBitsLarge-1) downto 0);
43
                          reminder : out  STD_LOGIC_VECTOR ((nBitsLarge-1) downto 0);
44
           numerator : in  STD_LOGIC_VECTOR ((nBitsLarge-1) downto 0);
45
           divident : in  STD_LOGIC_VECTOR ((nBitsLarge-1) downto 0);
46
           done : out  STD_LOGIC);
47
end component;
48
 
49
begin
50
        -- Instantiate block for calculate division
51
        uDiv : divisor port map (
52
                rst => sigDivRst,
53
                clk => clk,
54
                quotient => sigDivQuotient,
55
                reminder => sigDivReminder,
56
                numerator => sigDivNumerator,
57
                divident => sigDivDividend,
58
                done => sigDivDone
59
        );
60
 
61 10 leonardoar
        -- Process that read uart control registers
62
        process (rst, clk, reg_addr,WE)
63
        begin
64
                if rising_edge(clk) then
65
                        if (WE = '0') and (start = '1') then
66
                                case reg_addr is
67
                                        when "00" =>
68
                                                DAT_O <= config_clk;
69
                                        when "01" =>
70
                                                DAT_O <= config_baud;
71
                                        when "10" =>
72
                                                -- Byte that will be transmitted
73 11 leonardoar
                                                DAT_O <= "000000000000000000000000" & byte_to_transmitt;
74 10 leonardoar
                                        when "11" =>
75
                                                -- Byte that will be received
76 11 leonardoar
                                                DAT_O <= "000000000000000000000000" & byte_to_receive;
77 10 leonardoar
                                        when others =>
78
                                                null;
79
                                end case;
80
                        end if;
81
                end if;
82
        end process;
83
 
84
        -- Process that populate the uart control registers
85 9 leonardoar
        process (rst, clk, reg_addr,WE)
86
        begin
87
                if rst = '1' then
88
                        config_clk <= (others => '0');
89
                        config_baud <= (others => '0');
90 11 leonardoar
                        byte_to_transmitt <= (others => '0');
91 9 leonardoar
                elsif rising_edge(clk) then
92 10 leonardoar
                        if (WE = '1') and (start = '1') then
93 9 leonardoar
                                case reg_addr is
94
                                        when "00" =>
95
                                                config_clk <= DAT_I;
96
                                        when "01" =>
97
                                                config_baud <= DAT_I;
98
                                        when "10" =>
99 10 leonardoar
                                                -- Byte that will be transmitted
100
                                                byte_to_transmitt <= DAT_I((nBits-1) downto 0);
101 9 leonardoar
                                        when others =>
102
                                                null;
103
                                end case;
104
                        end if;
105
                end if;
106
        end process;
107
 
108
        -- Process to handle the next state logic
109
        process (rst, clk, reg_addr, WE)
110
        variable baud_configured : std_logic;
111 10 leonardoar
        variable clk_configured : std_logic;
112
        variable div_result_baud_wait : std_logic_vector ((nBitsLarge-1) downto 0);
113 9 leonardoar
        begin
114
                if rst = '1' then
115
                        controlStates <= idle;
116 11 leonardoar
                        baud_configured := '0';
117
                        clk_configured := '0';
118
                        div_result_baud_wait := (others => '0');
119 10 leonardoar
                        done <= '0';
120 9 leonardoar
                elsif rising_edge(clk) then
121
                        case controlStates is
122
                                when idle =>
123 10 leonardoar
                                        done <= '0';
124 9 leonardoar
                                        -- Go to config state
125
                                        if (reg_addr = "00") and (WE = '1') then
126
                                                controlStates <= config_state_clk;
127 11 leonardoar
                                                clk_configured := '1';
128 9 leonardoar
                                        elsif (reg_addr = "01") and (WE = '1') then
129
                                                controlStates <= config_state_baud;
130 11 leonardoar
                                                baud_configured := '1';
131 9 leonardoar
                                        end if;
132
 
133
                                when config_state_clk =>
134
                                        sigDivRst <= '1';
135 10 leonardoar
                                        sigDivNumerator <= config_clk;
136 9 leonardoar
                                        if baud_configured = '0' then
137
                                                -- Baud not configured yet so wait for it...
138 10 leonardoar
                                                controlStates <= idle;
139
                                                done <= '1';
140 9 leonardoar
                                        else
141
                                                -- If already configured wait for division completion...
142
                                                controlStates <= start_division;
143
                                        end if;
144
 
145
                                when config_state_baud =>
146
                                        sigDivRst <= '1';
147 10 leonardoar
                                        sigDivDividend <= config_baud;
148 9 leonardoar
                                        if clk_configured = '0' then
149
                                                -- Clock not configured yet so wait for it...
150 10 leonardoar
                                                controlStates <= idle;
151
                                                done <= '1';
152 9 leonardoar
                                        else
153
                                                -- If already configured wait for division completion...
154
                                                controlStates <= start_division;
155
                                        end if;
156
 
157
                                when start_division =>
158
                                        sigDivRst <= '0';
159
                                        controlStates <= wait_division;
160
 
161
                                when wait_division =>
162
                                        if sigDivDone = '0' then
163
                                                controlStates <= wait_division;
164
                                        else
165 10 leonardoar
                                                -- Division done, get the result to put on the wait_cycles signal of the baud generator
166
                                                div_result_baud_wait := sigDivQuotient;
167
                                                controlStates <= config_state_baud_generator;
168
                                        end if;
169
 
170
                                when config_state_baud_generator =>
171
                                        -- Configure the wait_cycle for the desired baud rate...
172
                                        baud_wait <= div_result_baud_wait;
173
                                        controlStates <= rx_tx_state;
174
                                        done <= '1';
175
 
176
                                -- Control the serial_receiver or serial_transmitter block
177
                                when rx_tx_state =>
178
                                        controlStates <= rx_tx_state;
179
                                        if (WE = '1') and (start = '1') then
180
                                                if reg_addr = "10" then
181
                                                        controlStates <= tx_state_wait;
182
                                                        done <= '0';
183
                                                end if;
184
                                        end if;
185
 
186
                                        if (WE = '0') and (start = '1') then
187
                                                if reg_addr = "11" then
188
                                                        controlStates <= rx_state_wait;
189
                                                        done <= '0';
190
                                                end if;
191
                                        end if;
192
 
193
 
194
                                -- Send data and wait to transmit
195
                                when tx_state_wait =>
196
                                        data_byte_tx <= byte_to_transmitt;
197
                                        if tx_data_sent = '0' then
198
                                                controlStates <= tx_state_wait;
199
                                        else
200
                                                controlStates <= rx_tx_state;
201
                                                done <= '1';
202
                                        end if;
203
 
204
                                -- Receive data and wait to receive
205
                                when rx_state_wait =>
206
                                        if rx_data_ready = '1' then
207
                                                byte_to_receive <= data_byte_rx;
208
                                                done <= '1';
209
                                                controlStates <= rx_tx_state;
210
                                        else
211
                                                controlStates <= rx_state_wait;
212
                                        end if;
213 9 leonardoar
                        end case;
214
                end if;
215
        end process;
216
 
217
end Behavioral;
218
 

powered by: WebSVN 2.1.0

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