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 14

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
signal current_s,next_s: rxStates;
21
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
                                        if current_s = rx_stop then
73
                                                filterRx <= s0;
74
                                        end if;
75 2 leonardoar
                        end case;
76
                end if;
77
        end process;
78
 
79
        -- Next state logic for rx Receiver (On this case our reset is the syncDetected signal
80
        process (syncDetected, baudClk, serial_in)
81
        begin
82
                if syncDetected = '0' then
83
                        current_s <= rx_idle;
84
                elsif rising_edge(baudClk) then
85
                        current_s <= next_s;
86
                end if;
87
        end process;
88
 
89
        -- Process to handle the serial receive
90 14 leonardoar
        process (current_s, serial_in)
91 4 leonardoar
        variable byteReceived : STD_LOGIC_VECTOR ((nBits-1) downto 0);
92 2 leonardoar
        begin
93
                case current_s is
94
                        when rx_idle =>
95
                                data_ready <= '0';
96 4 leonardoar
                                byteReceived := (others => 'Z');
97 2 leonardoar
                                next_s <=  bit0;
98
 
99
                        when bit0 =>
100
                                data_ready <= '0';
101 4 leonardoar
                                byteReceived(0) := serial_in;
102 2 leonardoar
                                next_s <=  bit1;
103
 
104
                        when bit1 =>
105
                                data_ready <= '0';
106 4 leonardoar
                                byteReceived(1) := serial_in;
107 2 leonardoar
                                next_s <=  bit2;
108
 
109
                        when bit2 =>
110
                                data_ready <= '0';
111 4 leonardoar
                                byteReceived(2) := serial_in;
112 2 leonardoar
                                next_s <=  bit3;
113
 
114
                        when bit3 =>
115
                                data_ready <= '0';
116 4 leonardoar
                                byteReceived(3) := serial_in;
117 2 leonardoar
                                next_s <=  bit4;
118
 
119
                        when bit4 =>
120
                                data_ready <= '0';
121 4 leonardoar
                                byteReceived(4) := serial_in;
122 2 leonardoar
                                next_s <=  bit5;
123
 
124
                        when bit5 =>
125
                                data_ready <= '0';
126 4 leonardoar
                                byteReceived(5) := serial_in;
127 2 leonardoar
                                next_s <=  bit6;
128
 
129
                        when bit6 =>
130
                                data_ready <= '0';
131 4 leonardoar
                                byteReceived(6) := serial_in;
132 2 leonardoar
                                next_s <=  bit7;
133
 
134
                        when bit7 =>
135
                                data_ready <= '0';
136 10 leonardoar
                                byteReceived(7) := serial_in;
137
                                data_byte <= byteReceived;
138 2 leonardoar
                                next_s <=  rx_stop;
139
 
140
                        when rx_stop =>
141 10 leonardoar
                                data_ready <= '1';
142 4 leonardoar
                                next_s <=  rx_stop;
143
                end case;
144 2 leonardoar
 
145
        end process;
146
 
147
end Behavioral;
148
 

powered by: WebSVN 2.1.0

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