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

Subversion Repositories uart2bus

[/] [uart2bus/] [trunk/] [verilog/] [rtl/] [uart_rx.v] - Blame information for rev 10

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

Line No. Rev Author Line
1 2 motilito
//---------------------------------------------------------------------------------------
2
// uart receive module  
3
//
4
//---------------------------------------------------------------------------------------
5
 
6
module uart_rx
7
(
8
        clock, reset,
9
        ce_16, ser_in,
10
        rx_data, new_rx_data
11
);
12
//---------------------------------------------------------------------------------------
13
// modules inputs and outputs 
14
input                   clock;                  // global clock input 
15
input                   reset;                  // global reset input 
16
input                   ce_16;                  // baud rate multiplyed by 16 - generated by baud module 
17
input                   ser_in;                 // serial data input 
18
output  [7:0]    rx_data;                // data byte received 
19
output                  new_rx_data;    // signs that a new byte was received 
20
 
21
// internal wires 
22
wire ce_1;              // clock enable at bit rate 
23
wire ce_1_mid;  // clock enable at the middle of each bit - used to sample data 
24
 
25
// internal registers 
26
reg     [7:0] rx_data;
27
reg     new_rx_data;
28
reg [1:0] in_sync;
29
reg rx_busy;
30
reg [3:0]        count16;
31
reg [3:0]        bit_count;
32
reg [7:0]        data_buf;
33
//---------------------------------------------------------------------------------------
34
// module implementation 
35
// input async input is sampled twice 
36
always @ (posedge clock or posedge reset)
37
begin
38
        if (reset)
39
                in_sync <= 2'b11;
40
        else
41
                in_sync <= {in_sync[0], ser_in};
42
end
43
 
44
// a counter to count 16 pulses of ce_16 to generate the ce_1 and ce_1_mid pulses.
45
// this counter is used to detect the start bit while the receiver is not receiving and 
46
// signs the sampling cycle during reception. 
47
always @ (posedge clock or posedge reset)
48
begin
49
        if (reset)
50
                count16 <= 4'b0;
51
        else if (ce_16)
52
        begin
53
                if (rx_busy | (in_sync[1] == 1'b0))
54
                        count16 <= count16 + 4'b1;
55
                else
56
                        count16 <= 4'b0;
57
        end
58
end
59
 
60
// ce_1 pulse indicating expected end of current bit 
61
assign ce_1 = (count16 == 4'b1111) & ce_16;
62
// ce_1_mid pulse indication the sampling clock cycle of the current data bit 
63
assign ce_1_mid = (count16 == 4'b0111) & ce_16;
64
 
65
// receiving busy flag 
66
always @ (posedge clock or posedge reset)
67
begin
68
        if (reset)
69
                rx_busy <= 1'b0;
70
        else if (~rx_busy & ce_1_mid)
71
                rx_busy <= 1'b1;
72 9 motilito
        else if (rx_busy & (bit_count == 4'h8) & ce_1_mid)
73 2 motilito
                rx_busy <= 1'b0;
74
end
75
 
76
// bit counter 
77
always @ (posedge clock or posedge reset)
78
begin
79
        if (reset)
80
                bit_count <= 4'h0;
81
        else if (~rx_busy)
82
                bit_count <= 4'h0;
83
        else if (rx_busy & ce_1_mid)
84
                bit_count <= bit_count + 4'h1;
85
end
86
 
87
// data buffer shift register 
88
always @ (posedge clock or posedge reset)
89
begin
90
        if (reset)
91
                data_buf <= 8'h0;
92
        else if (rx_busy & ce_1_mid)
93
                data_buf <= {in_sync[1], data_buf[7:1]};
94
end
95
 
96
// data output and flag 
97
always @ (posedge clock or posedge reset)
98
begin
99
        if (reset)
100
        begin
101
                rx_data <= 8'h0;
102
                new_rx_data <= 1'b0;
103
        end
104
        else if (rx_busy & (bit_count == 4'h8) & ce_1)
105
        begin
106
                rx_data <= data_buf;
107
                new_rx_data <= 1'b1;
108
        end
109
        else
110
                new_rx_data <= 1'b0;
111
end
112
 
113
endmodule
114
//---------------------------------------------------------------------------------------
115
//                                              Th.. Th.. Th.. Thats all folks !!!
116
//---------------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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