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 35

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

powered by: WebSVN 2.1.0

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