1 |
3 |
jlechner |
-----------------------------------------------------------------------
|
2 |
|
|
-- This file is part of SCARTS.
|
3 |
|
|
--
|
4 |
|
|
-- SCARTS 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 |
|
|
-- SCARTS 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 SCARTS. If not, see <http://www.gnu.org/licenses/>.
|
16 |
|
|
-----------------------------------------------------------------------
|
17 |
|
|
|
18 |
|
|
|
19 |
|
|
-------------------------------------------------------------------------------
|
20 |
|
|
-- Title : miniUART Receiver v2
|
21 |
|
|
-- Module : ext_miniUART
|
22 |
|
|
-- Project : HW/SW-Codesign
|
23 |
|
|
-------------------------------------------------------------------------------
|
24 |
|
|
-- File : miniUART_receiver.vhd
|
25 |
|
|
-- Author : Roman Seiger
|
26 |
|
|
-- Company : TU Wien - Institut für Technische Informatik
|
27 |
|
|
-- Created : 2005-03-08
|
28 |
|
|
-- Last update: 2007-05-29
|
29 |
|
|
-------------------------------------------------------------------------------
|
30 |
|
|
|
31 |
|
|
|
32 |
|
|
|
33 |
|
|
----------------------------------------------------------------------------------
|
34 |
|
|
-- LIBRARY
|
35 |
|
|
----------------------------------------------------------------------------------
|
36 |
|
|
LIBRARY IEEE;
|
37 |
|
|
USE IEEE.std_logic_1164.all;
|
38 |
|
|
USE IEEE.std_logic_arith.all;
|
39 |
|
|
use IEEE.std_logic_UNSIGNED."+";
|
40 |
|
|
use work.pkg_basic.all;
|
41 |
|
|
use work.pkg_miniUART.all;
|
42 |
|
|
|
43 |
|
|
----------------------------------------------------------------------------------
|
44 |
|
|
-- ENTITY
|
45 |
|
|
----------------------------------------------------------------------------------
|
46 |
|
|
entity miniUART_receiver is
|
47 |
|
|
|
48 |
|
|
port (
|
49 |
|
|
clk : in std_logic;
|
50 |
|
|
reset : in std_logic;
|
51 |
|
|
enable : in std_logic; -- Receiver eingeschaltet?
|
52 |
|
|
MsgLength : in MsgLength_type;
|
53 |
|
|
Stop2 : in std_logic; -- Zweites Stopbit?
|
54 |
|
|
ParEna : in std_logic; -- Parity?
|
55 |
|
|
rp : in std_logic; -- Receivepulse vom BRG
|
56 |
|
|
RxD : in std_logic; -- Empfangseingang
|
57 |
|
|
|
58 |
|
|
Data : out Data_type;
|
59 |
|
|
ParBit : out std_logic; -- Empfangenes Paritybit
|
60 |
|
|
RecEna : out std_logic; -- Busdriver einschalten
|
61 |
|
|
StartRecPulse : out std_logic; -- Receivepulse generieren
|
62 |
|
|
busy : out std_logic; -- Receiving / Startbit detected
|
63 |
|
|
RecComplete : out std_logic; -- komplettes Frame empfangen
|
64 |
|
|
FrameErr : out std_logic
|
65 |
|
|
);
|
66 |
|
|
|
67 |
|
|
end miniUART_receiver;
|
68 |
|
|
|
69 |
|
|
----------------------------------------------------------------------------------
|
70 |
|
|
-- ARCHITECTURE
|
71 |
|
|
----------------------------------------------------------------------------------
|
72 |
|
|
architecture behaviour of miniUART_receiver is
|
73 |
|
|
|
74 |
|
|
-- max. 20 empfangene Bits (1+16+1+2); 20d = 10100b
|
75 |
|
|
constant BITCOUNTERWIDTH : integer := 5;
|
76 |
|
|
|
77 |
|
|
-- Bitnummern der Parity/Stopbits
|
78 |
|
|
constant PARST1 : std_logic_vector(BITCOUNTERWIDTH-1 downto 0) := "10010";
|
79 |
|
|
constant ST1ST2 : std_logic_vector(BITCOUNTERWIDTH-1 downto 0) := "10011";
|
80 |
|
|
constant ST2 : std_logic_vector(BITCOUNTERWIDTH-1 downto 0) := "10100";
|
81 |
|
|
|
82 |
|
|
|
83 |
|
|
-- Definition der States
|
84 |
|
|
type rec_states is (DISABLE_S, STARTBITDETECTION_S, RECEIVE_S);
|
85 |
|
|
|
86 |
|
|
signal Rec_State : rec_states;
|
87 |
|
|
signal next_Rec_State : rec_states;
|
88 |
|
|
|
89 |
|
|
-- interne Signale zur synchronisation der Ausgänge
|
90 |
|
|
signal ParBit_i : std_logic;
|
91 |
|
|
signal ParBit_old : std_logic;
|
92 |
|
|
signal ParEna_i : std_logic;
|
93 |
|
|
signal Stop2_i : std_logic;
|
94 |
|
|
signal RecEna_i : std_logic;
|
95 |
|
|
signal busy_i : std_logic;
|
96 |
|
|
signal StartRecPulse_i : std_logic;
|
97 |
|
|
signal RecComplete_i : std_logic;
|
98 |
|
|
signal FrameErr1_i : std_logic;
|
99 |
|
|
signal FrameErr2_i : std_logic;
|
100 |
|
|
signal FrameErr_int : std_logic;
|
101 |
|
|
signal Data_i : Data_type;
|
102 |
|
|
-- signal Data : Data_type;
|
103 |
|
|
signal Data_nxt : Data_type;
|
104 |
|
|
|
105 |
|
|
-- interne Signale zur Zwischenspeicherung der Eingänge
|
106 |
|
|
-- signal RxD_i, RxD_i_nxt : std_logic;
|
107 |
|
|
|
108 |
|
|
-- Bitzähler
|
109 |
|
|
signal Bitcounter : std_logic_vector(BITCOUNTERWIDTH-1 downto 0);
|
110 |
|
|
signal next_Bitcounter : std_logic_vector(BITCOUNTERWIDTH-1 downto 0);
|
111 |
|
|
|
112 |
|
|
-- Nachrichtenlänge
|
113 |
|
|
signal Length : std_logic_vector(3 downto 0);
|
114 |
|
|
signal Length_nxt : std_logic_vector(3 downto 0);
|
115 |
|
|
-- signal Length : integer;
|
116 |
|
|
|
117 |
|
|
-- alter Empfangswert
|
118 |
|
|
signal old_RxD : std_logic;
|
119 |
|
|
|
120 |
|
|
-- alter Pulsewert
|
121 |
|
|
-- signal old_rp : std_logic;
|
122 |
|
|
|
123 |
|
|
begin -- behaviour
|
124 |
|
|
|
125 |
|
|
Data <= Data_i;
|
126 |
|
|
|
127 |
|
|
REC_STATECHANGE: process (clk, reset)
|
128 |
|
|
begin -- process REC_STATECHANGE
|
129 |
|
|
-- Receiver ausgeschalten, kommt einem reset gleich
|
130 |
|
|
if reset = RST_ACT then
|
131 |
|
|
RecEna <= not BUSDRIVER_ON;
|
132 |
|
|
StartRecPulse <= not BRG_ON;
|
133 |
|
|
busy <= not REC_BUSY;
|
134 |
|
|
RecComplete <= not REC_COMPLETE;
|
135 |
|
|
Bitcounter <= (others => '0');
|
136 |
|
|
Data_i <= (others => '0');
|
137 |
|
|
ParBit <= '0';
|
138 |
|
|
FrameErr_int <= not FRAME_ERROR;
|
139 |
|
|
ParBit_old <= '0';
|
140 |
|
|
old_RxD <= '1';
|
141 |
|
|
Rec_State <= DISABLE_S;
|
142 |
|
|
Length <= (others => '0');
|
143 |
|
|
-- Signale ausgeben, Statechange
|
144 |
|
|
elsif (clk'event and clk = '1') then
|
145 |
|
|
Bitcounter <= next_Bitcounter;
|
146 |
|
|
RecEna <= RecEna_i;
|
147 |
|
|
StartRecPulse <= StartRecPulse_i;
|
148 |
|
|
busy <= busy_i;
|
149 |
|
|
RecComplete <= RecComplete_i;
|
150 |
|
|
ParBit <= ParBit_i;
|
151 |
|
|
ParBit_old <= ParBit_i;
|
152 |
|
|
FrameErr_int <= FrameErr1_i or FrameErr2_i;
|
153 |
|
|
Data_i <= Data_nxt;
|
154 |
|
|
-- Empfangszustand merken (um Flanke zu erkennen!)
|
155 |
|
|
old_RxD <= RxD;
|
156 |
|
|
Rec_State <= next_Rec_State;
|
157 |
|
|
Length <= Length_nxt;
|
158 |
|
|
end if;
|
159 |
|
|
|
160 |
|
|
end process REC_STATECHANGE;
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
|
164 |
|
|
REC_STATEMACHINE: process (enable, MsgLength, ParEna, Stop2, ParEna_i, Stop2_i,
|
165 |
|
|
Length, Rec_State, Bitcounter, RxD, Data_i,
|
166 |
|
|
ParBit_old, FrameErr_int, old_RxD, rp)
|
167 |
|
|
begin -- process REC_STATEMACHINE
|
168 |
|
|
|
169 |
|
|
-- Defaultwerte
|
170 |
|
|
RecEna_i <= not BUSDRIVER_ON;
|
171 |
|
|
StartRecPulse_i <= not BRG_ON;
|
172 |
|
|
busy_i <= not REC_BUSY;
|
173 |
|
|
RecComplete_i <= not REC_COMPLETE;
|
174 |
|
|
next_Bitcounter <= Bitcounter;
|
175 |
|
|
ParEna_i <= ParEna;
|
176 |
|
|
Stop2_i <= Stop2;
|
177 |
|
|
Data_nxt <= Data_i;
|
178 |
|
|
ParBit_i <= ParBit_old;
|
179 |
|
|
FrameErr1_i <= FrameErr_int;
|
180 |
|
|
FrameErr2_i <= FrameErr_int;
|
181 |
|
|
Length_nxt <= Length;
|
182 |
|
|
|
183 |
|
|
case Rec_State is
|
184 |
|
|
when DISABLE_S =>
|
185 |
|
|
Data_nxt <= (others => '0');
|
186 |
|
|
ParBit_i <= '0';
|
187 |
|
|
FrameErr1_i <= not FRAME_ERROR;
|
188 |
|
|
FrameErr2_i <= not FRAME_ERROR;
|
189 |
|
|
next_Rec_State <= DISABLE_S;
|
190 |
|
|
next_Bitcounter <= (others => '0');
|
191 |
|
|
-- Receiver eingeschalten
|
192 |
|
|
if enable = RECEIVER_ENABLED then
|
193 |
|
|
Length_nxt <= MsgLength; -- Nachrichtenlänge
|
194 |
|
|
ParEna_i <= ParEna;
|
195 |
|
|
Stop2_i <= Stop2;
|
196 |
|
|
RecEna_i <= BUSDRIVER_ON;
|
197 |
|
|
next_Rec_State <= STARTBITDETECTION_S;
|
198 |
|
|
end if;
|
199 |
|
|
|
200 |
|
|
|
201 |
|
|
when STARTBITDETECTION_S =>
|
202 |
|
|
Data_nxt <= (others => '0');
|
203 |
|
|
ParBit_i <= '0';
|
204 |
|
|
FrameErr1_i <= '0';
|
205 |
|
|
FrameErr2_i <= '0';
|
206 |
|
|
|
207 |
|
|
RecEna_i <= BUSDRIVER_ON; -- Bustreiber einschalten
|
208 |
|
|
next_Rec_State <= STARTBITDETECTION_S;
|
209 |
|
|
next_Bitcounter <= (others => '0');
|
210 |
|
|
if (RxD = '0') and (old_RxD = '1') then -- Startbit empfangen!
|
211 |
|
|
StartRecPulse_i <= BRG_ON; -- Receivepulse generieren
|
212 |
|
|
busy_i <= REC_BUSY; -- working
|
213 |
|
|
next_Bitcounter <= Bitcounter + '1';
|
214 |
|
|
next_Rec_State <= RECEIVE_S;
|
215 |
|
|
end if;
|
216 |
|
|
|
217 |
|
|
when RECEIVE_S =>
|
218 |
|
|
next_Rec_State <= RECEIVE_S;
|
219 |
|
|
RecEna_i <= BUSDRIVER_ON; -- Bustreiber einschalten
|
220 |
|
|
StartRecPulse_i <= BRG_ON; -- Receivepulse generieren
|
221 |
|
|
busy_i <= REC_BUSY; -- Beschäftigt
|
222 |
|
|
next_Bitcounter <= Bitcounter;
|
223 |
|
|
if rp = '1' then
|
224 |
|
|
-- RxD_i_nxt <= RxD;
|
225 |
|
|
next_Bitcounter <= Bitcounter +1 ;
|
226 |
|
|
-- end if; -
|
227 |
|
|
-- next_Bitcounter <= Bitcounter + '1';
|
228 |
|
|
case Bitcounter is
|
229 |
|
|
when "00000" => -- sollte nicht auftreten!!!
|
230 |
|
|
assert false
|
231 |
|
|
report "Wait on first Receceive Impuls -> Recieve Startbit"
|
232 |
|
|
severity NOTE;
|
233 |
|
|
|
234 |
|
|
when "00001" => -- Startbit
|
235 |
|
|
null;
|
236 |
|
|
|
237 |
|
|
-- Daten
|
238 |
|
|
when "00010" => -- Bit 0
|
239 |
|
|
Data_nxt(0) <= RxD;
|
240 |
|
|
if Length = "0000" then
|
241 |
|
|
next_Bitcounter <= PARST1;
|
242 |
|
|
end if;
|
243 |
|
|
when "00011" => -- Bit 1
|
244 |
|
|
Data_nxt(1) <= RxD;
|
245 |
|
|
if Length = "0001" then
|
246 |
|
|
next_Bitcounter <= PARST1;
|
247 |
|
|
end if;
|
248 |
|
|
when "00100" => -- Bit 2
|
249 |
|
|
Data_nxt(2) <= RxD;
|
250 |
|
|
if Length = "0010" then
|
251 |
|
|
next_Bitcounter <= PARST1;
|
252 |
|
|
end if;
|
253 |
|
|
when "00101" => -- Bit 3
|
254 |
|
|
Data_nxt(3) <= RxD;
|
255 |
|
|
if Length = "0011" then
|
256 |
|
|
next_Bitcounter <= PARST1;
|
257 |
|
|
end if;
|
258 |
|
|
when "00110" => -- Bit 4
|
259 |
|
|
Data_nxt(4) <= RxD;
|
260 |
|
|
if Length = "0100" then
|
261 |
|
|
next_Bitcounter <= PARST1;
|
262 |
|
|
end if;
|
263 |
|
|
when "00111" => -- Bit 5
|
264 |
|
|
Data_nxt(5) <= RxD;
|
265 |
|
|
if Length = "0101" then
|
266 |
|
|
next_Bitcounter <= PARST1;
|
267 |
|
|
end if;
|
268 |
|
|
when "01000" => -- Bit 6
|
269 |
|
|
Data_nxt(6) <= RxD;
|
270 |
|
|
if Length = "0110" then
|
271 |
|
|
next_Bitcounter <= PARST1;
|
272 |
|
|
end if;
|
273 |
|
|
when "01001" => -- Bit 7
|
274 |
|
|
Data_nxt(7) <= RxD;
|
275 |
|
|
if Length = "0111" then
|
276 |
|
|
next_Bitcounter <= PARST1;
|
277 |
|
|
end if;
|
278 |
|
|
when "01010" => -- Bit 8
|
279 |
|
|
Data_nxt(8) <= RxD;
|
280 |
|
|
if Length = "1000" then
|
281 |
|
|
next_Bitcounter <= PARST1;
|
282 |
|
|
end if;
|
283 |
|
|
when "01011" => -- Bit 9
|
284 |
|
|
Data_nxt(9) <= RxD;
|
285 |
|
|
if Length = "1001" then
|
286 |
|
|
next_Bitcounter <= PARST1;
|
287 |
|
|
end if;
|
288 |
|
|
when "01100" => -- Bit 10
|
289 |
|
|
Data_nxt(10) <= RxD;
|
290 |
|
|
if Length = "1010" then
|
291 |
|
|
next_Bitcounter <= PARST1;
|
292 |
|
|
end if;
|
293 |
|
|
when "01101" => -- Bit 11
|
294 |
|
|
Data_nxt(11) <= RxD;
|
295 |
|
|
if Length = "1011" then
|
296 |
|
|
next_Bitcounter <= PARST1;
|
297 |
|
|
end if;
|
298 |
|
|
when "01110" => -- Bit 12
|
299 |
|
|
Data_nxt(12) <= RxD;
|
300 |
|
|
if Length = "1100" then
|
301 |
|
|
next_Bitcounter <= PARST1;
|
302 |
|
|
end if;
|
303 |
|
|
when "01111" => -- Bit 13
|
304 |
|
|
Data_nxt(13) <= RxD;
|
305 |
|
|
if Length = "1101" then
|
306 |
|
|
next_Bitcounter <= PARST1;
|
307 |
|
|
end if;
|
308 |
|
|
when "10000" => -- Bit 14
|
309 |
|
|
Data_nxt(14) <= RxD;
|
310 |
|
|
if Length = "1110" then
|
311 |
|
|
next_Bitcounter <= PARST1;
|
312 |
|
|
end if;
|
313 |
|
|
when "10001" => -- Bit 15
|
314 |
|
|
Data_nxt(15) <= RxD;
|
315 |
|
|
if Length = "1111" then
|
316 |
|
|
next_Bitcounter <= PARST1;
|
317 |
|
|
end if;
|
318 |
|
|
|
319 |
|
|
-- Paritybit
|
320 |
|
|
when PARST1 => -- Parity oder 1. Stopbit
|
321 |
|
|
if ParEna_i = PARITY_ENABLE then
|
322 |
|
|
ParBit_i <= RxD;
|
323 |
|
|
else
|
324 |
|
|
FrameErr1_i <= not RxD;
|
325 |
|
|
if Stop2_i /= SECOND_STOPBIT then
|
326 |
|
|
next_Rec_State <= DISABLE_S; -- fertig
|
327 |
|
|
RecComplete_i <= REC_COMPLETE;
|
328 |
|
|
StartRecPulse_i <= not BRG_ON;
|
329 |
|
|
next_Bitcounter <= (others => '0');
|
330 |
|
|
end if;
|
331 |
|
|
end if;
|
332 |
|
|
|
333 |
|
|
-- Stopbits
|
334 |
|
|
when ST1ST2 => -- 1. oder 2. Stopbit
|
335 |
|
|
FrameErr2_i <= not RxD;
|
336 |
|
|
if (ParEna_i /= PARITY_ENABLE) or (Stop2_i /= SECOND_STOPBIT) then
|
337 |
|
|
next_Rec_State <= DISABLE_S; -- fertig
|
338 |
|
|
RecComplete_i <= REC_COMPLETE;
|
339 |
|
|
StartRecPulse_i <= not BRG_ON;
|
340 |
|
|
next_Bitcounter <= (others => '0');
|
341 |
|
|
end if;
|
342 |
|
|
|
343 |
|
|
when ST2 => -- 2. Stopbit
|
344 |
|
|
FrameErr1_i <= not RxD; -- fertig
|
345 |
|
|
next_Rec_State <= DISABLE_S; -- fertig
|
346 |
|
|
RecComplete_i <= REC_COMPLETE;
|
347 |
|
|
StartRecPulse_i <= not BRG_ON;
|
348 |
|
|
next_Bitcounter <= (others => '0');
|
349 |
|
|
|
350 |
|
|
when others => -- nicht erreicht!
|
351 |
|
|
next_Rec_State <= DISABLE_S;
|
352 |
|
|
RecComplete_i <= REC_COMPLETE;
|
353 |
|
|
StartRecPulse_i <= not BRG_ON;
|
354 |
|
|
next_Bitcounter <= (others => '0');
|
355 |
|
|
|
356 |
|
|
assert false
|
357 |
|
|
report "Bitcounter overrun (miniUART_receiver.vhd)!!!"
|
358 |
|
|
severity ERROR;
|
359 |
|
|
|
360 |
|
|
end case;
|
361 |
|
|
end if;
|
362 |
|
|
when others => --DISABLE_S
|
363 |
|
|
next_Rec_State <= DISABLE_S;
|
364 |
|
|
end case;
|
365 |
|
|
end process REC_STATEMACHINE;
|
366 |
|
|
|
367 |
|
|
FrameErr <= FrameErr_int;
|
368 |
|
|
end behaviour;
|
369 |
|
|
|
370 |
|
|
|
371 |
|
|
|