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

Subversion Repositories gpib_controller

[/] [gpib_controller/] [trunk/] [vhdl/] [src/] [comm/] [Uart.vhd] - Blame information for rev 13

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 Andrewski
--------------------------------------------------------------------------------
2 13 Andrewski
--This file is part of fpga_gpib_controller.
3
--
4
-- Fpga_gpib_controller is free software: you can redistribute it and/or modify
5
-- it under the terms of the GNU General Public License as published by
6
-- the Free Software Foundation, either version 3 of the License, or
7
-- (at your option) any later version.
8
--
9
-- Fpga_gpib_controller is distributed in the hope that it will be useful,
10
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
11
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
-- GNU General Public License for more details.
13
 
14
-- You should have received a copy of the GNU General Public License
15
-- along with Fpga_gpib_controller.  If not, see <http://www.gnu.org/licenses/>.
16
--------------------------------------------------------------------------------
17 3 Andrewski
-- Entity: Uart
18
-- Date:2011-11-26  
19 13 Andrewski
-- Author: Andrzej Paluch
20 3 Andrewski
--
21
-- Description ${cursor}
22
--------------------------------------------------------------------------------
23
library ieee;
24
use ieee.std_logic_1164.all;
25
use ieee.std_logic_unsigned.all;
26
 
27
 
28
entity Uart is
29
        port (
30
                reset : in std_logic;
31
                clk : in std_logic;
32
                ---------- UART ---------------
33
                RX : in std_logic;
34
                TX : out std_logic;
35
                ---------- gpib ---------------
36
                data_out : out std_logic_vector(7 downto 0);
37
                data_out_ready : out std_logic;
38
                data_in : in std_logic_vector(7 downto 0);
39
                data_in_ready : in std_logic;
40
                ready_to_send : out std_logic
41
        );
42
end Uart;
43
 
44
architecture arch of Uart is
45
 
46
        constant BIT_TIME : integer :=
47
                -- 115200
48
                --572
49
                -- 921600
50
                72
51
                ;
52
        constant HALF_BIT_TIME : integer := BIT_TIME / 2;
53
 
54
        type RX_STATES is (
55
                ST_RX_IDLE,
56
                ST_RX_HALF_START,
57
                ST_RX_RECEIVE_BITS,
58
                ST_RX_RECEIVE_STOP_BIT
59
        );
60
 
61
        type TX_STATES is (
62
                ST_TX_IDLE,
63
                ST_TX_SEND_BITS,
64
                ST_TX_SEND_STOP_BIT
65
        );
66
 
67
        signal syncRX : std_logic;
68
 
69
        signal rxState : RX_STATES;
70
        signal lastRx : std_logic;
71
        signal rxTimeCounter : integer range 0 to BIT_TIME;
72
        signal rxBitCounter : integer range 0 to 7;
73
        signal innerBuf : std_logic_vector(6 downto 0);
74
 
75
        signal txState : TX_STATES;
76
        signal lastData_in_ready : std_logic;
77
        signal txTimeCounter : integer range 0 to BIT_TIME;
78
        signal txBitCounter : integer range 0 to 8;
79
 
80
begin
81
 
82
        -- RX synchronizer
83
        process(clk, RX) begin
84
                if rising_edge(clk) then
85
                        syncRX <= RX;
86
                end if;
87
        end process;
88
 
89
        -- RX
90
        process(reset, clk, syncRX) begin
91
                if reset = '1' then
92
                        rxState <= ST_RX_IDLE;
93
                        lastRx <= syncRX;
94
                        data_out_ready <= '0';
95
                elsif rising_edge(clk) then
96
 
97
                        lastRx <= syncRX;
98
 
99
                        if rxTimeCounter < BIT_TIME then
100
                                rxTimeCounter <= rxTimeCounter + 1;
101
                        end if;
102
 
103
                        case rxState is
104
                                when ST_RX_IDLE =>
105
                                        if lastRx /= syncRX and syncRX = '0' then
106
                                                rxTimeCounter <= 1;
107
                                                rxState <= ST_RX_HALF_START;
108
                                        end if;
109
 
110
                                when ST_RX_HALF_START =>
111
                                        if rxTimeCounter >= HALF_BIT_TIME then
112
                                                rxBitCounter <= 0;
113
                                                rxTimeCounter <= 1;
114
                                                rxState <= ST_RX_RECEIVE_BITS;
115
                                        end if;
116
 
117
                                when ST_RX_RECEIVE_BITS =>
118
                                        if rxTimeCounter >= BIT_TIME then
119
 
120
                                                if rxBitCounter < 7 then
121
                                                        innerBuf(rxBitCounter) <= syncRX;
122
                                                        rxBitCounter <= rxBitCounter + 1;
123
                                                        rxTimeCounter <= 1;
124
                                                elsif rxBitCounter = 7 then
125
                                                        data_out(7) <= syncRX;
126
                                                        data_out(6 downto 0) <= innerBuf;
127
                                                        data_out_ready <= '0';
128
                                                        rxTimeCounter <= 1;
129
 
130
                                                        rxState <= ST_RX_RECEIVE_STOP_BIT;
131
                                                end if;
132
                                        end if;
133
 
134
                                when ST_RX_RECEIVE_STOP_BIT =>
135
                                        if rxTimeCounter >= BIT_TIME then
136
                                                data_out_ready <= '1';
137
 
138
                                                rxState <= ST_RX_IDLE;
139
                                        end if;
140
 
141
                                when others =>
142
                                        rxState <= ST_RX_IDLE;
143
                        end case;
144
                end if;
145
        end process;
146
 
147
        -- TX
148
        process(reset, clk, data_in_ready) begin
149
                if reset = '1' then
150
                        TX <= '1';
151
                        ready_to_send <= '1';
152
                        txState <= ST_TX_IDLE;
153
                elsif rising_edge(clk) then
154
 
155
                        lastData_in_ready <= data_in_ready;
156
 
157
                        if txTimeCounter < BIT_TIME then
158
                                txTimeCounter <= txTimeCounter + 1;
159
                        end if;
160
 
161
                        case txState is
162
                                when ST_TX_IDLE =>
163
                                        if lastData_in_ready /= data_in_ready and
164
                                                        data_in_ready = '1' then
165
                                                TX <= '0';
166
                                                txTimeCounter <= 1;
167
                                                txBitCounter <= 0;
168
                                                ready_to_send <= '0';
169
 
170
                                                txState <= ST_TX_SEND_BITS;
171
                                        end if;
172
                                when ST_TX_SEND_BITS =>
173
                                        if txTimeCounter >= BIT_TIME then
174
 
175
                                                if txBitCounter < 8 then
176
                                                        TX <= data_in(txBitCounter);
177
                                                        txBitCounter <= txBitCounter + 1;
178
                                                        txTimeCounter <= 1;
179
                                                elsif txBitCounter = 8 then
180
                                                        TX <= '1';
181
                                                        txTimeCounter <= 1;
182
 
183
                                                        txState <= ST_TX_SEND_STOP_BIT;
184
                                                end if;
185
                                        end if;
186
                                when ST_TX_SEND_STOP_BIT =>
187
                                        if txTimeCounter >= BIT_TIME then
188
                                                ready_to_send <= '1';
189
 
190
                                                txState <= ST_TX_IDLE;
191
                                        end if;
192
                                when others =>
193
                                        txState <= ST_TX_IDLE;
194
                        end case;
195
                end if;
196
        end process;
197
 
198
end arch;
199
 

powered by: WebSVN 2.1.0

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