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

Subversion Repositories uart_block

[/] [uart_block/] [trunk/] [hdl/] [iseProject/] [serial_receiver.vhd] - Blame information for rev 34

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

Line No. Rev Author Line
1 2 leonardoar
--! Data receiver
2
--! http://www.fpga4fun.com/SerialInterface.html
3
library IEEE;
4
use IEEE.STD_LOGIC_1164.ALL;
5
 
6
--! Use CPU Definitions package
7
use work.pkgDefinitions.all;
8
 
9
entity serial_receiver is
10
    Port (
11
                          rst : in STD_LOGIC;
12
                          baudClk : in  STD_LOGIC;
13
                          baudOverSampleClk : in  STD_LOGIC;
14
           serial_in : in  STD_LOGIC;
15
           data_ready : out  STD_LOGIC;
16
           data_byte : out  STD_LOGIC_VECTOR ((nBits-1) downto 0));
17
end serial_receiver;
18
 
19
architecture Behavioral of serial_receiver is
20 15 leonardoar
signal current_s: rxStates;
21 2 leonardoar
signal filterRx : rxFilterStates;
22 32 leonardoar
signal syncDetected : std_logic;
23 34 leonardoar
signal getPoint : std_logic;
24 2 leonardoar
 
25
begin
26 12 leonardoar
        -- First we need to oversample(4x baud rate) out serial channel to syncronize with the PC
27 4 leonardoar
        process (rst, baudOverSampleClk, serial_in, current_s)
28 2 leonardoar
        begin
29
                if rst = '1' then
30
                        filterRx <= s0;
31
                        syncDetected <= '0';
32
                elsif rising_edge(baudOverSampleClk) then
33
                        case filterRx is
34
                                when s0 =>
35
                                        syncDetected <= '0';
36 12 leonardoar
                                        -- Spike down detected, verify if it's valid for at least 3 cycles
37
                                        -- We shoose a little bit on the end to enforce the baud clk to sample 
38
                                        -- the data at the right time... iE we're going to start sampling when
39
                                        -- the stop has been detected and we already for some of the first bit
40
                                        -- signal
41 2 leonardoar
                                        if serial_in = '0' then
42
                                                filterRx <= s1;
43
                                        else
44
                                                filterRx <= s0;
45
                                        end if;
46
 
47
                                when s1 =>
48
                                        syncDetected <= '0';
49
                                        if serial_in = '0' then
50
                                                filterRx <= s2;
51
                                                syncDetected <= '0';
52
                                        else
53
                                                filterRx <= s0;
54
                                        end if;
55
 
56 4 leonardoar
                                when s2 =>
57
                                        syncDetected <= '0';
58
                                        if serial_in = '0' then
59
                                                filterRx <= s3;
60
                                                syncDetected <= '0';
61
                                        else
62
                                                filterRx <= s0;
63 32 leonardoar
                                        end if;
64
 
65
                                when s3 =>
66
                                        syncDetected <= '0';
67
                                        if serial_in = '0' then
68
                                                filterRx <= s4;
69
                                                syncDetected <= '0';
70
                                        else
71
                                                filterRx <= s0;
72 4 leonardoar
                                        end if;
73
 
74 32 leonardoar
                                when s4 =>
75 2 leonardoar
                                        -- Real Beginning of start bit detected 
76
                                        if serial_in = '0' then
77 32 leonardoar
                                                filterRx <= s4;
78 4 leonardoar
                                                syncDetected <= '1';
79 2 leonardoar
                                        end if;
80 4 leonardoar
 
81
                                        -- Reset out sync detector when finished to receive a byte
82 15 leonardoar
                                        if current_s = rx_idle then
83 4 leonardoar
                                                filterRx <= s0;
84
                                        end if;
85 2 leonardoar
                        end case;
86
                end if;
87
        end process;
88 15 leonardoar
 
89
        -- Process to handle the serial receive (On this case our reset is the syncDetected signal
90
        -- Always include all of your signals on the sensivity list!! (Even if the simulation is already ok)
91 32 leonardoar
        process (syncDetected, baudOverSampleClk, serial_in)
92
        variable byteReceived : STD_LOGIC_VECTOR ((nBits-1) downto 0);
93
        variable waitBestPoint : integer range 0 to 10;
94
        constant numTicks : integer := 7;
95 2 leonardoar
        begin
96
                if syncDetected = '0' then
97 15 leonardoar
                        current_s <= bit0;
98
                        data_ready <= '0';
99 32 leonardoar
                        byteReceived := (others => '0');
100
                        waitBestPoint := 0;
101 34 leonardoar
                        getPoint <= '0';
102 32 leonardoar
                elsif rising_edge(baudOverSampleClk) then
103 15 leonardoar
                        case current_s is
104
                                when bit0 =>
105 32 leonardoar
                                        data_ready <= '0';
106
                                        if (waitBestPoint < numTicks) then
107
                                                waitBestPoint := waitBestPoint + 1;
108 34 leonardoar
                                                getPoint <= '0';
109 32 leonardoar
                                        else
110
                                                waitBestPoint := 0;
111
                                                byteReceived(0) := serial_in;
112
                                                current_s <=  bit1;
113 34 leonardoar
                                                getPoint <= '1';
114 32 leonardoar
                                        end if;
115 15 leonardoar
 
116
                                when bit1 =>
117 32 leonardoar
                                        data_ready <= '0';
118
                                        if (waitBestPoint < numTicks) then
119
                                                waitBestPoint := waitBestPoint + 1;
120 34 leonardoar
                                                getPoint <= '0';
121 32 leonardoar
                                        else
122
                                                waitBestPoint := 0;
123
                                                byteReceived(1) := serial_in;
124
                                                current_s <=  bit2;
125 34 leonardoar
                                                getPoint <= '1';
126 32 leonardoar
                                        end if;
127 15 leonardoar
 
128
                                when bit2 =>
129 32 leonardoar
                                        data_ready <= '0';
130
                                        if (waitBestPoint < numTicks) then
131
                                                waitBestPoint := waitBestPoint + 1;
132 34 leonardoar
                                                getPoint <= '0';
133 32 leonardoar
                                        else
134
                                                waitBestPoint := 0;
135
                                                byteReceived(2) := serial_in;
136
                                                current_s <=  bit3;
137 34 leonardoar
                                                getPoint <= '1';
138 32 leonardoar
                                        end if;
139 15 leonardoar
 
140
                                when bit3 =>
141 32 leonardoar
                                        data_ready <= '0';
142
                                        if (waitBestPoint < numTicks) then
143
                                                waitBestPoint := waitBestPoint + 1;
144 34 leonardoar
                                                getPoint <= '0';
145 32 leonardoar
                                        else
146
                                                waitBestPoint := 0;
147
                                                byteReceived(3) := serial_in;
148
                                                current_s <=  bit4;
149 34 leonardoar
                                                getPoint <= '1';
150 32 leonardoar
                                        end if;
151 15 leonardoar
 
152
                                when bit4 =>
153 32 leonardoar
                                        data_ready <= '0';
154
                                        if (waitBestPoint < numTicks) then
155
                                                waitBestPoint := waitBestPoint + 1;
156 34 leonardoar
                                                getPoint <= '0';
157 32 leonardoar
                                        else
158
                                                waitBestPoint := 0;
159
                                                byteReceived(4) := serial_in;
160
                                                current_s <=  bit5;
161 34 leonardoar
                                                getPoint <= '1';
162 32 leonardoar
                                        end if;
163 15 leonardoar
 
164
                                when bit5 =>
165 32 leonardoar
                                        data_ready <= '0';
166
                                        if (waitBestPoint < numTicks) then
167
                                                waitBestPoint := waitBestPoint + 1;
168 34 leonardoar
                                                getPoint <= '0';
169 32 leonardoar
                                        else
170
                                                waitBestPoint := 0;
171
                                                byteReceived(5) := serial_in;
172
                                                current_s <=  bit6;
173 34 leonardoar
                                                getPoint <= '1';
174 32 leonardoar
                                        end if;
175 15 leonardoar
 
176
                                when bit6 =>
177 32 leonardoar
                                        data_ready <= '0';
178
                                        if (waitBestPoint < numTicks) then
179
                                                waitBestPoint := waitBestPoint + 1;
180 34 leonardoar
                                                getPoint <= '0';
181 32 leonardoar
                                        else
182
                                                waitBestPoint := 0;
183
                                                byteReceived(6) := serial_in;
184
                                                current_s <=  bit7;
185 34 leonardoar
                                                getPoint <= '1';
186 32 leonardoar
                                        end if;
187 15 leonardoar
 
188
                                when bit7 =>
189 32 leonardoar
                                        data_ready <= '0';
190
                                        if (waitBestPoint < numTicks) then
191
                                                waitBestPoint := waitBestPoint + 1;
192 34 leonardoar
                                                getPoint <= '0';
193 32 leonardoar
                                        else
194
                                                waitBestPoint := 0;
195
                                                byteReceived(7) := serial_in;
196
                                                data_byte <= byteReceived;
197
                                                current_s <=  rx_stop;
198 34 leonardoar
                                                getPoint <= '1';
199 32 leonardoar
                                        end if;
200 15 leonardoar
 
201
                                when rx_stop =>
202
                                        data_ready <= '1';
203
                                        data_byte <= byteReceived;
204
                                        current_s <=  rx_idle;
205
 
206
                                when rx_idle =>
207
                                        data_ready <= '1';
208
                                        data_byte <= byteReceived;
209
                                        current_s <=  rx_idle;
210
 
211
                        end case;
212 2 leonardoar
                end if;
213
        end process;
214 15 leonardoar
 
215 2 leonardoar
end Behavioral;
216
 

powered by: WebSVN 2.1.0

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