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

Subversion Repositories rtfsimpleuart

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

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 12 AlexRayne
    input tri0 baud8x,       // switches to mode baudX8
95 5 robfinch
        input clear,                    // clear reciever
96
        input rxd,                              // external serial input
97
        output reg data_present,        // data present in fifo
98
        output reg frame_err,           // framing error
99
        output reg overrun                      // receiver overrun
100
);
101
 
102 12 AlexRayne
//0 - simple sampling at middle of symbol period
103
//>0 - sampling of 3 middle ticks of sumbol perion and results as majority
104
parameter SamplerStyle = 0;
105
 
106 5 robfinch
// variables
107
reg [7:0] cnt;                   // sample bit rate counter
108
reg [9:0] rx_data;               // working receive data register
109
reg state;                              // state machine
110
reg wf;                                 // buffer write
111
reg [7:0] dat;
112
 
113 12 AlexRayne
wire isX8;
114
buf(isX8, baud8x);
115
reg modeX8;
116
 
117 5 robfinch
assign ack_o = cyc_i & stb_i & cs_i;
118
assign dat_o = ack_o ? dat : 8'b0;
119
 
120
// update data register
121
always @(posedge clk_i)
122
        if (wf) dat <= rx_data[8:1];
123
 
124
// on a read clear the data present status
125
// but set the status when the data register
126
// is updated by the receiver           
127
always @(posedge clk_i)
128 12 AlexRayne
    if (rst_i)
129
        data_present <= 0;
130
    else if (wf)
131
        data_present <= 1;
132 5 robfinch
        else if (ack_o & ~we_i) data_present <= 0;
133
 
134
 
135
// Three stage synchronizer to synchronize incoming data to
136
// the local clock (avoids metastability).
137 12 AlexRayne
reg [5:0] rxdd          /* synthesis ramstyle = "logic" */; // synchronizer flops
138
reg rxdsmp;             // majority samples
139
reg rdxstart;           // for majority style sample solid 3tik-wide sample
140
reg [1:0] rxdsum;
141
always @(posedge clk_i) begin
142
        rxdd <= {rxdd[4:0],rxd};
143
    if (SamplerStyle == 0) begin
144
        rxdsmp <= rxdd[3];
145
        rdxstart <= rxdd[4]&~rxdd[3];
146
    end
147
    else begin
148
        rxdsum[1] <= rxdsum[0];
149
        rxdsum[0] <= {1'b0,rxdd[3]} + {1'b0,rxdd[4]} + {1'b0,rxdd[5]};
150
        rxdsmp <= rxdsum[1];
151
        rdxstart <= (rxdsum[1] == 2'b00) & ((rxdsum[1] == 2'b11));
152
    end
153
end
154 5 robfinch
 
155 12 AlexRayne
`define CNT_FRAME  (8'h97)
156
`define CNT_FINISH (8'h9D)
157
 
158 5 robfinch
always @(posedge clk_i) begin
159
        if (rst_i) begin
160
                state <= `IDLE;
161
                wf <= 1'b0;
162
                overrun <= 1'b0;
163 12 AlexRayne
        frame_err <= 1'b0;
164 5 robfinch
        end
165
        else begin
166
 
167
                // Clear write flag
168
                wf <= 1'b0;
169
 
170
                if (clear) begin
171
                        wf <= 1'b0;
172
                        state <= `IDLE;
173
                        overrun <= 1'b0;
174 12 AlexRayne
            frame_err <= 1'b0;
175 5 robfinch
                end
176
 
177
                else if (baud16x_ce) begin
178
 
179
                        case (state)
180
 
181
                        // Sit in the idle state until a start bit is
182
                        // detected.
183
                        `IDLE:
184
                                // look for start bit
185 12 AlexRayne
                                if (rdxstart)
186 5 robfinch
                                        state <= `CNT;
187
 
188
                        `CNT:
189
                                begin
190
                                        // End of the frame ?
191
                                        // - check for framing error
192
                                        // - write data to read buffer
193 12 AlexRayne
                                        if (cnt==`CNT_FRAME)
194 5 robfinch
                                                begin
195 12 AlexRayne
                                                        frame_err <= ~rxdsmp;
196
                            overrun <= data_present;
197 5 robfinch
                                                        if (!data_present)
198
                                                                wf <= 1'b1;
199 12 AlexRayne
                            state <= `IDLE;
200 5 robfinch
                                                end
201
                                        // Switch back to the idle state a little
202
                                        // bit too soon.
203 12 AlexRayne
                                        //if (cnt==`CNT_FINISH) begin
204
                                        //      state <= `IDLE;
205
                    //end
206 5 robfinch
 
207
                                        // On start bit check make sure the start
208
                                        // bit is low, otherwise go back to the
209
                                        // idle state because it's a false start.
210 12 AlexRayne
                                        if (cnt==8'h07 && rxdsmp)
211 5 robfinch
                                                state <= `IDLE;
212
 
213
                                        if (cnt[3:0]==4'h7)
214 12 AlexRayne
                                                rx_data <= {rxdsmp,rx_data[9:1]};
215 5 robfinch
                                end
216
 
217
                        endcase
218
                end
219
        end
220
end
221
 
222
 
223
// bit rate counter
224
always @(posedge clk_i)
225
        if (baud16x_ce) begin
226 12 AlexRayne
                if (state == `IDLE) begin
227
                        cnt <= modeX8;
228
            modeX8 <= isX8;
229
        end
230
        else begin
231
            cnt[7:1] <= cnt[7:1] + cnt[0];
232
            cnt[0] <= ~cnt[0] | (modeX8);
233
        end
234 5 robfinch
        end
235
 
236
endmodule
237
 

powered by: WebSVN 2.1.0

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