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

Subversion Repositories rtfsimpleuart

[/] [rtfsimpleuart/] [trunk/] [rtl/] [verilog/] [rtfSimpleUartRx.v] - Blame information for rev 5

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

Line No. Rev Author Line
1 5 robfinch
/* ============================================================================
2
        2011  Robert Finch
3
        robfinch@<remove>sympatico.ca
4
 
5
        rtfSimpleUartRx.v
6
 
7
    This source code is available for evaluation and validation purposes
8
    only. This copyright statement and disclaimer must remain present in
9
    the file.
10
 
11
 
12
        NO WARRANTY.
13
    THIS Work, IS PROVIDEDED "AS IS" WITH NO WARRANTIES OF ANY KIND, WHETHER
14
    EXPRESS OR IMPLIED. The user must assume the entire risk of using the
15
    Work.
16
 
17
    IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY
18
    INCIDENTAL, CONSEQUENTIAL, OR PUNITIVE DAMAGES WHATSOEVER RELATING TO
19
    THE USE OF THIS WORK, OR YOUR RELATIONSHIP WITH THE AUTHOR.
20
 
21
    IN ADDITION, IN NO EVENT DOES THE AUTHOR AUTHORIZE YOU TO USE THE WORK
22
    IN APPLICATIONS OR SYSTEMS WHERE THE WORK'S FAILURE TO PERFORM CAN
23
    REASONABLY BE EXPECTED TO RESULT IN A SIGNIFICANT PHYSICAL INJURY, OR IN
24
    LOSS OF LIFE. ANY SUCH USE BY YOU IS ENTIRELY AT YOUR OWN RISK, AND YOU
25
    AGREE TO HOLD THE AUTHOR AND CONTRIBUTORS HARMLESS FROM ANY CLAIMS OR
26
    LOSSES RELATING TO SUCH UNAUTHORIZED USE.
27
 
28
 
29
        Simple UART receiver core
30
                Features:
31
                        false start bit detection
32
                        framing error detection
33
                        overrun state detection
34
                        resynchronization on every character
35
                        fixed format 1 start - 8 data - 1 stop bits
36
                        uses 16x clock rate
37
 
38
                This core may be used as a standalone peripheral
39
        on a SoC bus if all that is desired is recieve
40
        capability. It requires a 16x baud rate clock.
41
 
42
        +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43
        |WISHBONE Datasheet
44
        |WISHBONE SoC Architecture Specification, Revision B.3
45
        |
46
        |Description:                                           Specifications:
47
        +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
48
        |General Description:                           simple serial UART receiver
49
        +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
50
        |Supported Cycles:                                      SLAVE,READ
51
        |                                                                       SLAVE,BLOCK READ
52
        +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
53
        |Data port, size:                                       8 bit
54
        |Data port, granularity:                        8 bit
55
        |Data port, maximum operand size:       8 bit
56
        |Data transfer ordering:                        Undefined
57
        |Data transfer sequencing:                      Undefined
58
        +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
59
        |Clock frequency constraints:           none
60
        +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
61
        |Supported signal list and                      Signal Name             WISHBONE equiv.
62
        |cross reference to equivalent          ack_o                   ACK_O
63
        |WISHBONE signals
64
        |                                                                       clk_i                   CLK_I
65
        |                                   rst_i           RST_I
66
        |                                                                       dat_o(7:0)              DAT_O()
67
        |                                                                       cyc_i                   CYC_I
68
        |                                                                       stb_i                   STB_I
69
        |                                                                       we_i                    WE_I
70
        |
71
        +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72
        |Special requirements:
73
        +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
74
 
75
        Ref: Spartan3 -4
76
        27 LUTs / 24 slices / 170 MHz
77
============================================================================ */
78
 
79
`define IDLE    0
80
`define CNT             1
81
 
82
module rtfSimpleUartRx(
83
        // WISHBONE SoC bus interface
84
        input rst_i,                    // reset
85
        input clk_i,                    // clock
86
        input cyc_i,                    // cycle is valid
87
        input stb_i,                    // strobe
88
        output ack_o,                   // data is ready
89
        input we_i,                             // write (this signal is used to qualify reads)
90
        output [7:0] dat_o,              // data out
91
        //------------------------
92
        input cs_i,                             // chip select
93
        input baud16x_ce,               // baud rate clock enable
94
        input clear,                    // clear reciever
95
        input rxd,                              // external serial input
96
        output reg data_present,        // data present in fifo
97
        output reg frame_err,           // framing error
98
        output reg overrun                      // receiver overrun
99
);
100
 
101
// variables
102
reg [3:0] rxdd;                  // synchronizer flops
103
reg [7:0] cnt;                   // sample bit rate counter
104
reg [9:0] rx_data;               // working receive data register
105
reg state;                              // state machine
106
reg wf;                                 // buffer write
107
reg [7:0] dat;
108
 
109
assign ack_o = cyc_i & stb_i & cs_i;
110
assign dat_o = ack_o ? dat : 8'b0;
111
 
112
// update data register
113
always @(posedge clk_i)
114
        if (wf) dat <= rx_data[8:1];
115
 
116
// on a read clear the data present status
117
// but set the status when the data register
118
// is updated by the receiver           
119
always @(posedge clk_i)
120
        if (wf) data_present <= 1;
121
        else if (ack_o & ~we_i) data_present <= 0;
122
 
123
 
124
// Three stage synchronizer to synchronize incoming data to
125
// the local clock (avoids metastability).
126
always @(posedge clk_i)
127
        rxdd <= {rxdd[2:0],rxd};
128
 
129
always @(posedge clk_i) begin
130
        if (rst_i) begin
131
                state <= `IDLE;
132
                wf <= 1'b0;
133
                overrun <= 1'b0;
134
        end
135
        else begin
136
 
137
                // Clear write flag
138
                wf <= 1'b0;
139
 
140
                if (clear) begin
141
                        wf <= 1'b0;
142
                        state <= `IDLE;
143
                        overrun <= 1'b0;
144
                end
145
 
146
                else if (baud16x_ce) begin
147
 
148
                        case (state)
149
 
150
                        // Sit in the idle state until a start bit is
151
                        // detected.
152
                        `IDLE:
153
                                // look for start bit
154
                                if (~rxdd[3])
155
                                        state <= `CNT;
156
 
157
                        `CNT:
158
                                begin
159
                                        // End of the frame ?
160
                                        // - check for framing error
161
                                        // - write data to read buffer
162
                                        if (cnt==8'h97)
163
                                                begin
164
                                                        frame_err <= ~rxdd[3];
165
                                                        if (!data_present)
166
                                                                wf <= 1'b1;
167
                                                        else
168
                                                                overrun <= 1'b1;
169
                                                end
170
                                        // Switch back to the idle state a little
171
                                        // bit too soon.
172
                                        if (cnt==8'h9D)
173
                                                state <= `IDLE;
174
 
175
                                        // On start bit check make sure the start
176
                                        // bit is low, otherwise go back to the
177
                                        // idle state because it's a false start.
178
                                        if (cnt==8'h07 && rxdd[3])
179
                                                state <= `IDLE;
180
 
181
                                        if (cnt[3:0]==4'h7)
182
                                                rx_data <= {rxdd[3],rx_data[9:1]};
183
                                end
184
 
185
                        endcase
186
                end
187
        end
188
end
189
 
190
 
191
// bit rate counter
192
always @(posedge clk_i)
193
        if (baud16x_ce) begin
194
                if (state == `IDLE)
195
                        cnt <= 0;
196
                else
197
                        cnt <= cnt + 1;
198
        end
199
 
200
endmodule
201
 

powered by: WebSVN 2.1.0

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