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 24

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
signal syncDetected : std_logic;
23
 
24
begin
25 12 leonardoar
        -- First we need to oversample(4x baud rate) out serial channel to syncronize with the PC
26 4 leonardoar
        process (rst, baudOverSampleClk, serial_in, current_s)
27 2 leonardoar
        begin
28
                if rst = '1' then
29
                        filterRx <= s0;
30
                        syncDetected <= '0';
31
                elsif rising_edge(baudOverSampleClk) then
32
                        case filterRx is
33
                                when s0 =>
34
                                        syncDetected <= '0';
35 12 leonardoar
                                        -- Spike down detected, verify if it's valid for at least 3 cycles
36
                                        -- We shoose a little bit on the end to enforce the baud clk to sample 
37
                                        -- the data at the right time... iE we're going to start sampling when
38
                                        -- the stop has been detected and we already for some of the first bit
39
                                        -- signal
40 2 leonardoar
                                        if serial_in = '0' then
41
                                                filterRx <= s1;
42
                                        else
43
                                                filterRx <= s0;
44
                                        end if;
45
 
46
                                when s1 =>
47
                                        syncDetected <= '0';
48
                                        if serial_in = '0' then
49
                                                filterRx <= s2;
50
                                                syncDetected <= '0';
51
                                        else
52
                                                filterRx <= s0;
53
                                        end if;
54
 
55 4 leonardoar
                                when s2 =>
56
                                        syncDetected <= '0';
57
                                        if serial_in = '0' then
58
                                                filterRx <= s3;
59
                                                syncDetected <= '0';
60
                                        else
61
                                                filterRx <= s0;
62
                                        end if;
63
 
64
                                when s3 =>
65 2 leonardoar
                                        -- Real Beginning of start bit detected 
66
                                        if serial_in = '0' then
67 4 leonardoar
                                                filterRx <= s3;
68
                                                syncDetected <= '1';
69 2 leonardoar
                                        end if;
70 4 leonardoar
 
71
                                        -- Reset out sync detector when finished to receive a byte
72 15 leonardoar
                                        if current_s = rx_idle then
73 4 leonardoar
                                                filterRx <= s0;
74
                                        end if;
75 2 leonardoar
                        end case;
76
                end if;
77
        end process;
78 15 leonardoar
 
79
        -- Process to handle the serial receive (On this case our reset is the syncDetected signal
80
        -- Always include all of your signals on the sensivity list!! (Even if the simulation is already ok)
81
        process (syncDetected, baudClk, serial_in)
82
        variable byteReceived : STD_LOGIC_VECTOR ((nBits-1) downto 0);
83 2 leonardoar
        begin
84
                if syncDetected = '0' then
85 15 leonardoar
                        current_s <= bit0;
86
                        data_ready <= '0';
87
                        byteReceived := (others => '0');
88 2 leonardoar
                elsif rising_edge(baudClk) then
89 15 leonardoar
                        case current_s is
90
                                when bit0 =>
91
                                        data_ready <= '0';
92
                                        byteReceived(0) := serial_in;
93
                                        current_s <=  bit1;
94
 
95
                                when bit1 =>
96
                                        data_ready <= '0';
97
                                        byteReceived(1) := serial_in;
98
                                        current_s <=  bit2;
99
 
100
                                when bit2 =>
101
                                        data_ready <= '0';
102
                                        byteReceived(2) := serial_in;
103
                                        current_s <=  bit3;
104
 
105
                                when bit3 =>
106
                                        data_ready <= '0';
107
                                        byteReceived(3) := serial_in;
108
                                        current_s <=  bit4;
109
 
110
                                when bit4 =>
111
                                        data_ready <= '0';
112
                                        byteReceived(4) := serial_in;
113
                                        current_s <=  bit5;
114
 
115
                                when bit5 =>
116
                                        data_ready <= '0';
117
                                        byteReceived(5) := serial_in;
118
                                        current_s <=  bit6;
119
 
120
                                when bit6 =>
121
                                        data_ready <= '0';
122
                                        byteReceived(6) := serial_in;
123
                                        current_s <=  bit7;
124
 
125
                                when bit7 =>
126
                                        data_ready <= '0';
127
                                        byteReceived(7) := serial_in;
128
                                        data_byte <= byteReceived;
129
                                        current_s <=  rx_stop;
130
 
131
                                when rx_stop =>
132
                                        data_ready <= '1';
133
                                        data_byte <= byteReceived;
134
                                        current_s <=  rx_idle;
135
 
136
                                when rx_idle =>
137
                                        data_ready <= '1';
138
                                        data_byte <= byteReceived;
139
                                        current_s <=  rx_idle;
140
 
141
                        end case;
142 2 leonardoar
                end if;
143
        end process;
144 15 leonardoar
 
145 2 leonardoar
end Behavioral;
146
 

powered by: WebSVN 2.1.0

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