uart.vhd


1
--------------------------------------------------------------------------------
2
-- Company: 
3
-- Engineer:
4
--
5
-- Create Date:    13:23:22 12/28/05
6
-- Design Name:    
7
-- Module Name:    sersenden - Behavioral
8
-- Project Name:   
9
-- Target Device:  
10
-- Tool versions:  
11
-- Description:
12
--
13
-- Dependencies:
14
-- 
15
-- Revision:
16
-- Revision 0.01 - File Created
17
-- Additional Comments:
18
-- 
19
--------------------------------------------------------------------------------
20
library IEEE;
21
use IEEE.STD_LOGIC_1164.ALL;
22
use IEEE.STD_LOGIC_ARITH.ALL;
23
use IEEE.STD_LOGIC_UNSIGNED.ALL;
24
25
---- Uncomment the following library declaration if instantiating
26
---- any Xilinx primitives in this code.
27
--library UNISIM;
28
--use UNISIM.VComponents.all;
29
30
entity uart is
31
generic(bitleng: integer:=(868-1));
32
port(serout: out std_logic:='1';
33
     clk: in std_logic;
34
    txenable: in std_logic;
35
    reset: in std_logic;
36
    serin: in std_logic;
37
38
    txbuffer: in std_logic_vector(7 downto 0);
39
     txerror: out std_logic:='0';
40
     rxready: out std_logic:='0';
41
     rxbuffer: out std_logic_vector(7 downto 0);
42
     txbusy: out std_logic:='0';
43
     rxerror: out std_logic:='0'
44
);
45
end uart;
46
47
architecture uneu of uart is
48
signal statussenden: integer range 0 to 4:=0;
49
signal counterout: integer range 0 to 1023:=0;
50
signal statusempfang: integer range 0 to 4:=0;
51
signal counterin: integer range 0 to 1023:=0;
52
signal txstartbuffer: std_logic_vector(7 downto 0):=x"00";
53
signal rxempfangen:std_logic_vector(7 downto 0):=x"FF";
54
signal txstart:std_logic:='0';
55
signal txbusyser: std_logic:='0';
56
signal sperren: std_logic:='0';
57
58
begin
59
60
seroutx: process(clk,reset,statussenden)
61
variable counter2: integer range 0 to 7:=0;
62
variable txsenden:std_logic_vector(7 downto 0):=x"00";
63
64
begin
65
if reset='1' then
66
if clk'event and clk='1'then
67
68
case statussenden is
69
 when 0 => if txstart='1' then    -- Warten auf Start und starten.
70
           serout<='0';
71
           statussenden<=1;
72
        counter2:=0;
73
        counterout<=0;
74
        txbusyser<='1';
75
        txsenden:=txstartbuffer;
76
        end if;
77
 when 1 => counterout<=counterout+1;   -- Byte rausschieben
78
           if counterout>=bitleng then
79
          counterout<=0;
80
             serout<=txsenden(counter2);
81
             if counter2>=7 then
82
          statussenden<=2;
83
          else
84
          counter2:=counter2+1;
85
          end if;
86
           end if;
87
 when 2 => counterout<=counterout+1;   -- Beginn des Stoppbits
88
           txbusyser<='0';
89
           if counterout>=bitleng then
90
           counterout<=0;
91
              serout<='1';
92
           statussenden<=3;        
93
        end if;
94
 when 3 => counterout<=counterout+1;   -- Ende des Stoppbits und wenn nötig einleiten des nächsten Starts.
95
           if counterout>=bitleng then
96
          counterout<=0;
97
          if txstart='1' then
98
                 serout<='0';
99
                 statussenden<=1;
100
              counter2:=0;
101
              txsenden:=txstartbuffer;
102
              txbusyser<='1';
103
          else
104
             statussenden<=0;
105
          end if;
106
        end if;
107
 when others => null;
108
end case;
109
end if;
110
--end if;
111
else
112
serout<='1';
113
counterout<=0;
114
statussenden<=0;
115
end if;
116
end process seroutx;
117
118
serstarten: process(txenable,txbusyser,reset)
119
120
begin
121
if reset='1' then
122
  if txbusyser = '1' then
123
    txstart<='0';
124
    --start<='0';
125
    else
126
      if txenable'event and txenable='1' then
127
        txstart<='1';
128
           txstartbuffer<=txbuffer;
129
         end if;
130
   end if;
131
else
132
  --sperren:=0;
133
  txstart<='0';
134
end if;
135
end process serstarten;
136
137
138
serinx: process(clk,serin,reset)
139
variable counter2x: integer range 0 to 7:=0;
140
variable errorstart: std_logic:='0';
141
begin
142
if reset='1' then
143
if clk='1' and clk'event then
144
case statusempfang is
145
when 0 => if (serin='0' and errorstart='0') then
146
             statusempfang<=1;
147
         counterin<=bitleng/2;
148
         counter2x:=0;
149
       end if;
150
          if (serin='1' and errorstart='1') then
151
          errorstart:='0';
152
          end if;
153
when 1 => counterin<=counterin+1;
154
          if counterin>=bitleng then
155
          statusempfang<=2;
156
         counterin<=0;
157
         rxready<='0';
158
          end if;
159
when 2 => counterin<=counterin+1;
160
          if counterin>=bitleng then
161
          rxempfangen(counter2x)<=serin;
162
         counterin<=0;
163
         --counter2<=counter2+1;
164
165
         if counter2x >= 7 then
166
          statusempfang<=3;
167
         counterin<=0;
168
         else
169
         counter2x:=counter2x+1;
170
          end if;
171
          end if;
172
when 3 => counterin<=counterin+1;
173
          if counterin>=bitleng then
174
          if serin='1' then
175
             statusempfang<=0;
176
         rxbuffer<=rxempfangen;
177
         rxerror<='0';
178
         rxready<='1';
179
         --outx4<='1';
180
          else
181
          statusempfang<=0;
182
          rxerror<='1';
183
         errorstart:='1';
184
         rxready<='1';
185
          end if;
186
       end if;
187
       
188
when others => null;
189
end case;
190
end if;
191
else
192
-- Reset
193
rxerror<='0';
194
errorstart:='0';
195
rxready<='0';
196
rxbuffer <=(others=>'0');
197
end if;
198
199
end process serinx;
200
201
202
txbusy<=txbusyser or txstart;
203
end uneu;