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

Subversion Repositories rtfsimpleuart

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

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

Line No. Rev Author Line
1 13 robfinch
// ============================================================================
2
//      (C) 2011,2013  Robert Finch
3
//  All rights reserved.
4
//      robfinch@<remove>finitron.ca
5
//
6
//      rtfSimpleUartRx.v
7
//
8
// Redistribution and use in source and binary forms, with or without
9
// modification, are permitted provided that the following conditions are met:
10
//     * Redistributions of source code must retain the above copyright
11
//       notice, this list of conditions and the following disclaimer.
12
//     * Redistributions in binary form must reproduce the above copyright
13
//       notice, this list of conditions and the following disclaimer in the
14
//       documentation and/or other materials provided with the distribution.
15
//     * Neither the name of the <organization> nor the
16
//       names of its contributors may be used to endorse or promote products
17
//       derived from this software without specific prior written permission.
18
//
19
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
// DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
23
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
//
30
//      Simple UART receiver core
31
//              Features:
32
//                      false start bit detection
33
//                      framing error detection
34
//                      overrun state detection
35
//                      resynchronization on every character
36
//                      fixed format 1 start - 8 data - 1 stop bits
37
//                      uses 16x clock rate
38
//                      
39
//              This core may be used as a standalone peripheral
40
//      on a SoC bus if all that is desired is recieve
41
//      capability. It requires a 16x baud rate clock.
42
//      
43
//      +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44
//      |WISHBONE Datasheet
45
//      |WISHBONE SoC Architecture Specification, Revision B.3
46
//      |
47
//      |Description:                                           Specifications:
48
//      +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49
//      |General Description:                           simple serial UART receiver
50
//      +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
51
//      |Supported Cycles:                                      SLAVE,READ
52
//      |                                                                       SLAVE,BLOCK READ
53
//      +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
54
//      |Data port, size:                                       8 bit
55
//      |Data port, granularity:                        8 bit
56
//      |Data port, maximum operand size:       8 bit
57
//      |Data transfer ordering:                        Undefined
58
//      |Data transfer sequencing:                      Undefined
59
//      +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
60
//      |Clock frequency constraints:           none
61
//      +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
62
//      |Supported signal list and                      Signal Name             WISHBONE equiv.
63
//      |cross reference to equivalent          ack_o                   ACK_O
64
//      |WISHBONE signals                                       
65
//      |                                                                       clk_i                   CLK_I
66
//      |                                   rst_i           RST_I
67
//      |                                                                       dat_o(7:0)              DAT_O()
68
//      |                                                                       cyc_i                   CYC_I
69
//      |                                                                       stb_i                   STB_I
70
//      |                                                                       we_i                    WE_I
71
//      |
72
//      +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
73
//      |Special requirements:
74
//      +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
75
//
76
//      Ref: Spartan3 -4
77
//      27 LUTs / 24 slices / 170 MHz
78
//==============================================================================
79 5 robfinch
 
80
`define IDLE    0
81
`define CNT             1
82
 
83
module rtfSimpleUartRx(
84
        // WISHBONE SoC bus interface
85
        input rst_i,                    // reset
86
        input clk_i,                    // clock
87
        input cyc_i,                    // cycle is valid
88
        input stb_i,                    // strobe
89
        output ack_o,                   // data is ready
90
        input we_i,                             // write (this signal is used to qualify reads)
91
        output [7:0] dat_o,              // data out
92
        //------------------------
93
        input cs_i,                             // chip select
94
        input baud16x_ce,               // baud rate clock enable
95 12 AlexRayne
    input tri0 baud8x,       // switches to mode baudX8
96 5 robfinch
        input clear,                    // clear reciever
97
        input rxd,                              // external serial input
98
        output reg data_present,        // data present in fifo
99
        output reg frame_err,           // framing error
100
        output reg overrun                      // receiver overrun
101
);
102
 
103 12 AlexRayne
//0 - simple sampling at middle of symbol period
104
//>0 - sampling of 3 middle ticks of sumbol perion and results as majority
105
parameter SamplerStyle = 0;
106
 
107 5 robfinch
// variables
108
reg [7:0] cnt;                   // sample bit rate counter
109
reg [9:0] rx_data;               // working receive data register
110
reg state;                              // state machine
111
reg wf;                                 // buffer write
112
reg [7:0] dat;
113
 
114 12 AlexRayne
wire isX8;
115
buf(isX8, baud8x);
116
reg modeX8;
117
 
118 5 robfinch
assign ack_o = cyc_i & stb_i & cs_i;
119
assign dat_o = ack_o ? dat : 8'b0;
120
 
121
// update data register
122
always @(posedge clk_i)
123
        if (wf) dat <= rx_data[8:1];
124
 
125
// on a read clear the data present status
126
// but set the status when the data register
127
// is updated by the receiver           
128
always @(posedge clk_i)
129 12 AlexRayne
    if (rst_i)
130
        data_present <= 0;
131
    else if (wf)
132
        data_present <= 1;
133 5 robfinch
        else if (ack_o & ~we_i) data_present <= 0;
134
 
135
 
136
// Three stage synchronizer to synchronize incoming data to
137
// the local clock (avoids metastability).
138 12 AlexRayne
reg [5:0] rxdd          /* synthesis ramstyle = "logic" */; // synchronizer flops
139
reg rxdsmp;             // majority samples
140
reg rdxstart;           // for majority style sample solid 3tik-wide sample
141
reg [1:0] rxdsum;
142
always @(posedge clk_i) begin
143
        rxdd <= {rxdd[4:0],rxd};
144
    if (SamplerStyle == 0) begin
145
        rxdsmp <= rxdd[3];
146
        rdxstart <= rxdd[4]&~rxdd[3];
147
    end
148
    else begin
149
        rxdsum[1] <= rxdsum[0];
150
        rxdsum[0] <= {1'b0,rxdd[3]} + {1'b0,rxdd[4]} + {1'b0,rxdd[5]};
151
        rxdsmp <= rxdsum[1];
152
        rdxstart <= (rxdsum[1] == 2'b00) & ((rxdsum[1] == 2'b11));
153
    end
154
end
155 5 robfinch
 
156 12 AlexRayne
`define CNT_FRAME  (8'h97)
157
`define CNT_FINISH (8'h9D)
158
 
159 5 robfinch
always @(posedge clk_i) begin
160
        if (rst_i) begin
161
                state <= `IDLE;
162
                wf <= 1'b0;
163
                overrun <= 1'b0;
164 12 AlexRayne
        frame_err <= 1'b0;
165 5 robfinch
        end
166
        else begin
167
 
168
                // Clear write flag
169
                wf <= 1'b0;
170
 
171
                if (clear) begin
172
                        wf <= 1'b0;
173
                        state <= `IDLE;
174
                        overrun <= 1'b0;
175 12 AlexRayne
            frame_err <= 1'b0;
176 5 robfinch
                end
177
 
178
                else if (baud16x_ce) begin
179
 
180
                        case (state)
181
 
182
                        // Sit in the idle state until a start bit is
183
                        // detected.
184
                        `IDLE:
185
                                // look for start bit
186 12 AlexRayne
                                if (rdxstart)
187 5 robfinch
                                        state <= `CNT;
188
 
189
                        `CNT:
190
                                begin
191
                                        // End of the frame ?
192
                                        // - check for framing error
193
                                        // - write data to read buffer
194 12 AlexRayne
                                        if (cnt==`CNT_FRAME)
195 5 robfinch
                                                begin
196 12 AlexRayne
                                                        frame_err <= ~rxdsmp;
197
                            overrun <= data_present;
198 5 robfinch
                                                        if (!data_present)
199
                                                                wf <= 1'b1;
200 12 AlexRayne
                            state <= `IDLE;
201 5 robfinch
                                                end
202
                                        // Switch back to the idle state a little
203
                                        // bit too soon.
204 12 AlexRayne
                                        //if (cnt==`CNT_FINISH) begin
205
                                        //      state <= `IDLE;
206
                    //end
207 5 robfinch
 
208
                                        // On start bit check make sure the start
209
                                        // bit is low, otherwise go back to the
210
                                        // idle state because it's a false start.
211 12 AlexRayne
                                        if (cnt==8'h07 && rxdsmp)
212 5 robfinch
                                                state <= `IDLE;
213
 
214
                                        if (cnt[3:0]==4'h7)
215 12 AlexRayne
                                                rx_data <= {rxdsmp,rx_data[9:1]};
216 5 robfinch
                                end
217
 
218
                        endcase
219
                end
220
        end
221
end
222
 
223
 
224
// bit rate counter
225
always @(posedge clk_i)
226
        if (baud16x_ce) begin
227 12 AlexRayne
                if (state == `IDLE) begin
228
                        cnt <= modeX8;
229
            modeX8 <= isX8;
230
        end
231
        else begin
232
            cnt[7:1] <= cnt[7:1] + cnt[0];
233
            cnt[0] <= ~cnt[0] | (modeX8);
234
        end
235 5 robfinch
        end
236
 
237
endmodule
238
 

powered by: WebSVN 2.1.0

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