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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [rtl/] [rxuart.v] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 dgisselq
/////////////////////////////////////////////////////////////////////////
2
//
3
//
4
// Filename:    rxuart.v
5
//
6
// Project:     FPGA library development (Spartan 3E development board)
7
//
8
// Purpose:     Receive and decode inputs from a single UART line.
9
//
10
//
11
//      To interface with this module, connect it to your system clock,
12
//      pass it the 32 bit setup register (defined below) and the UART
13
//      input.  When data becomes available, the o_wr line will be asserted
14
//      for one clock cycle.  On parity or frame errors, the o_parity_err
15
//      or o_frame_err lines will be asserted.  Likewise, on a break 
16
//      condition, o_break will be asserted.  These lines are self clearing.
17
//
18
//      There is a synchronous reset line, logic high.
19
//
20
//      Now for the setup register.  The register is 32 bits, so that this
21
//      UART may be set up over a 32-bit bus.
22
//
23
//      i_setup[29:28]  Indicates the number of data bits per word.  This will
24
//      either be 2'b00 for an 8-bit word, 2'b01 for a 7-bit word, 2'b10
25
//      for a six bit word, or 2'b11 for a five bit word.
26
//
27
//      i_setup[27]     Indicates whether or not to use one or two stop bits.
28
//              Set this to one to expect two stop bits, zero for one.
29
//
30
//      i_setup[26]     Indicates whether or not a parity bit exists.  Set this
31
//              to 1'b1 to include parity.
32
//
33
//      i_setup[25]     Indicates whether or not the parity bit is fixed.  Set
34
//              to 1'b1 to include a fixed bit of parity, 1'b0 to allow the
35
//              parity to be set based upon data.  (Both assume the parity
36
//              enable value is set.)
37
//
38
//      i_setup[24]     This bit is ignored if parity is not used.  Otherwise,
39
//              in the case of a fixed parity bit, this bit indicates whether
40
//              mark (1'b1) or space (1'b0) parity is used.  Likewise if the
41
//              parity is not fixed, a 1'b1 selects even parity, and 1'b0
42
//              selects odd.
43
//
44
//      i_setup[23:0]   Indicates the speed of the UART in terms of clocks.
45
//              So, for example, if you have a 200 MHz clock and wish to
46
//              run your UART at 9600 baud, you would take 200 MHz and divide
47
//              by 9600 to set this value to 24'd20834.  Likewise if you wished
48
//              to run this serial port at 115200 baud from a 200 MHz clock,
49
//              you would set the value to 24'd1736
50
//
51
//      Thus, to set the UART for the common setting of an 8-bit word, 
52
//      one stop bit, no parity, and 115200 baud over a 200 MHz clock, you
53
//      would want to set the setup value to:
54
//
55
//      32'h0006c8              // For 115,200 baud, 8 bit, no parity
56
//      32'h005161              // For 9600 baud, 8 bit, no parity
57
//      
58
//
59
//
60
// Creator:     Dan Gisselquist, Ph.D.
61
//              Gisselquist Technology, LLC
62
//
63
// Copyright:   2015
64
//
65
//
66
/////////////////////////////////////////////////////////////////////////
67
//
68
// This software is the ownership of Gisselquist Technology, LLC, and as
69
// such it is proprietary.  It is provided without any warrantees, either
70
// express or implied, so that it may be tested.  Upon completion, I ask
71
// that working code be returned and not further distributed beyond those
72
// that it is originally offered to.
73
//
74
// Thank you.
75
//
76
 
77
// States: (@ baud counter == 0)
78
//      0        First bit arrives
79
//      ..7     Bits arrive
80
//      8       Stop bit (x1)
81
//      9       Stop bit (x2)
82
///     c       break condition
83
//      d       Waiting for the channel to go high
84
//      e       Waiting for the reset to complete
85
//      f       Idle state
86
`define RXU_BIT_ZERO            4'h0
87
`define RXU_BIT_ONE             4'h1
88
`define RXU_BIT_TWO             4'h2
89
`define RXU_BIT_THREE           4'h3
90
`define RXU_BIT_FOUR            4'h4
91
`define RXU_BIT_FIVE            4'h5
92
`define RXU_BIT_SIX             4'h6
93
`define RXU_BIT_SEVEN           4'h7
94
`define RXU_PARITY              4'h8
95
`define RXU_STOP                4'h9
96
`define RXU_SECOND_STOP         4'ha
97
// Unused 4'hb
98
// Unused 4'hc
99
`define RXU_BREAK               4'hd
100
`define RXU_RESET_IDLE          4'he
101
`define RXU_IDLE                4'hf
102
 
103
module rxuart(i_clk, i_reset, i_setup, i_uart, o_wr, o_data, o_break,
104
                        o_parity_err, o_frame_err, o_ck_uart);
105
        //  parameter // CLOCKS_PER_BAUD = 25'd004340,
106
                        //  BREAK_CONDITION = CLOCKS_PER_BAUD * 12,
107
                        //  CLOCKS_PER_HALF_BAUD = CLOCKS_PER_BAUD/2;
108
        // 8 data bits, no parity, (at least 1) stop bit
109
        input                   i_clk, i_reset;
110
        input           [29:0]   i_setup;
111
        input                   i_uart;
112
        output  reg             o_wr;
113
        output  reg     [7:0]    o_data;
114
        output  reg             o_break;
115
        output  reg             o_parity_err, o_frame_err;
116
        output  wire            o_ck_uart;
117
 
118
 
119
        wire    [27:0]   clocks_per_baud, break_condition, half_baud;
120
        wire    [1:0]    data_bits;
121
        wire            use_parity, parity_even, dblstop, fixd_parity;
122
        reg     [29:0]   r_setup;
123
        assign  clocks_per_baud = { 4'h0, r_setup[23:0] };
124
        assign  data_bits   = r_setup[29:28];
125
        assign  dblstop     = r_setup[27];
126
        assign  use_parity  = r_setup[26];
127
        assign  fixd_parity = r_setup[25];
128
        assign  parity_even = r_setup[24];
129
        assign  break_condition = { r_setup[23:0], 4'h0 };
130
        assign  half_baud = { 5'h00, r_setup[23:1] };
131
 
132
        reg     q_uart, qq_uart, ck_uart;
133
        initial q_uart  = 1'b0;
134
        initial qq_uart = 1'b0;
135
        initial ck_uart = 1'b0;
136
        always @(posedge i_clk)
137
        begin
138
                q_uart <= i_uart;
139
                qq_uart <= q_uart;
140
                ck_uart <= qq_uart;
141
        end
142
        assign  o_ck_uart = ck_uart;
143
 
144
        reg     [27:0]   chg_counter;
145
        initial chg_counter = 28'h00;
146
        always @(posedge i_clk)
147
                if (i_reset)
148
                        chg_counter <= 28'h00;
149
                else if (qq_uart != ck_uart)
150
                        chg_counter <= 28'h00;
151
                else if (chg_counter < break_condition)
152
                        chg_counter <= chg_counter + 1;
153
 
154
        always @(posedge i_clk)
155
                o_break <=((chg_counter >= break_condition)&&(~ck_uart))? 1'b1:1'b0;
156
 
157
        reg     [3:0]    state;
158
        reg     [27:0]   baud_counter;
159
        reg     [7:0]    data_reg;
160
        reg             calc_parity;
161
        initial o_wr = 1'b0;
162
        initial state = `RXU_RESET_IDLE;
163
        initial o_parity_err = 1'b0;
164
        initial o_frame_err  = 1'b0;
165
        // initial      baud_counter = clocks_per_baud;
166
        always @(posedge i_clk)
167
        begin
168
                if (i_reset)
169
                begin
170
                        o_wr <= 1'b0;
171
                        o_data <= 8'h00;
172
                        state <= `RXU_RESET_IDLE;
173
                        baud_counter <= clocks_per_baud; // Set, not reset
174
                        data_reg <= 8'h00;
175
                        calc_parity <= 1'b0;
176
                        o_parity_err <= 1'b0;
177
                        o_frame_err <= 1'b0;
178
                end else if (state == `RXU_RESET_IDLE)
179
                begin
180
                        r_setup <= i_setup;
181
                        data_reg <= 8'h00; o_data <= 8'h00; o_wr <= 1'b0;
182
                        baud_counter <= clocks_per_baud-28'h01;// Set, not reset
183
                        if ((ck_uart)&&(chg_counter >= break_condition))
184
                                // Goto idle state from a reset
185
                                state <= `RXU_IDLE;
186
                        else // Otherwise, stay in this condition 'til reset
187
                                state <= `RXU_RESET_IDLE;
188
                        calc_parity <= 1'b0;
189
                        o_parity_err <= 1'b0;
190
                        o_frame_err <= 1'b0;
191
                end else if ((~ck_uart)&&(chg_counter >= break_condition))
192
                begin // We are in a break condition
193
                        state <= `RXU_BREAK;
194
                        o_wr <= 1'b0;
195
                        o_data <= 8'h00;
196
                        baud_counter <= clocks_per_baud-28'h01;// Set, not reset
197
                        data_reg <= 8'h00;
198
                        calc_parity <= 1'b0;
199
                        o_parity_err <= 1'b0;
200
                        o_frame_err <= 1'b0;
201
                        r_setup <= i_setup;
202
                end else if (state == `RXU_BREAK)
203
                begin // Goto idle state following return ck_uart going high
204
                        data_reg <= 8'h00; o_data <= 8'h00; o_wr <= 1'b0;
205
                        baud_counter <= clocks_per_baud - 28'h01;
206
                        if (ck_uart)
207
                                state <= `RXU_IDLE;
208
                        else
209
                                state <= `RXU_BREAK;
210
                        calc_parity <= 1'b0;
211
                        o_parity_err <= 1'b0;
212
                        o_frame_err <= 1'b0;
213
                        r_setup <= i_setup;
214
                end else if (state == `RXU_IDLE)
215
                begin // Idle state, independent of baud counter
216
                        data_reg <= 8'h00; o_data <= 8'h00; o_wr <= 1'b0;
217
                        baud_counter <= clocks_per_baud - 28'h01;
218
                        if ((ck_uart == 1'b0)&&(chg_counter > half_baud))
219
                        begin
220
                                // We are in the center of a valid start bit
221
                                case (data_bits)
222
                                2'b00: state <= `RXU_BIT_ZERO;
223
                                2'b01: state <= `RXU_BIT_ONE;
224
                                2'b10: state <= `RXU_BIT_TWO;
225
                                2'b11: state <= `RXU_BIT_THREE;
226
                                endcase
227
                        end else // Otherwise, just stay here in idle
228
                                state <= `RXU_IDLE;
229
                        calc_parity <= 1'b0;
230
                        o_parity_err <= 1'b0;
231
                        o_frame_err <= 1'b0;
232
                end else if (baud_counter == 0)
233
                begin
234
                        baud_counter <= clocks_per_baud-28'h1;
235
                        if (state < `RXU_BIT_SEVEN)
236
                        begin
237
                                // Data arrives least significant bit first.
238
                                // By the time this is clocked in, it's what
239
                                // you'll have.
240
                                data_reg <= { ck_uart, data_reg[7:1] };
241
                                calc_parity <= calc_parity ^ ck_uart;
242
                                o_data <= 8'h00;
243
                                o_wr <= 1'b0;
244
                                state <= state + 1;
245
                                o_parity_err <= 1'b0;
246
                                o_frame_err <= 1'b0;
247
                        end else if (state == `RXU_BIT_SEVEN)
248
                        begin
249
                                data_reg <= { ck_uart, data_reg[7:1] };
250
                                calc_parity <= calc_parity ^ ck_uart;
251
                                o_data <= 8'h00;
252
                                o_wr <= 1'b0;
253
                                state <= (use_parity) ? `RXU_PARITY:`RXU_STOP;
254
                                o_parity_err <= 1'b0;
255
                                o_frame_err <= 1'b0;
256
                        end else if (state == `RXU_PARITY)
257
                        begin
258
                                if (fixd_parity)
259
                                        o_parity_err <= (ck_uart ^ parity_even);
260
                                else
261
                                        o_parity_err <= ((parity_even && (calc_parity != ck_uart))
262
                                                ||((~parity_even)&&(calc_parity==ck_uart)));
263
                                state <= `RXU_STOP;
264
                                o_frame_err <= 1'b0;
265
                        end else if (state == `RXU_STOP)
266
                        begin // Stop (or parity) bit(s)
267
                                case (data_bits)
268
                                2'b00: o_data <= data_reg;
269
                                2'b01: o_data <= { 1'b0, data_reg[7:1] };
270
                                2'b10: o_data <= { 2'b0, data_reg[7:2] };
271
                                2'b11: o_data <= { 3'b0, data_reg[7:3] };
272
                                endcase
273
                                o_wr <= 1'b1; // Pulse the write
274
                                o_frame_err <= (~ck_uart);
275
                                if (~ck_uart)
276
                                        state <= `RXU_RESET_IDLE;
277
                                else if (dblstop)
278
                                        state <= `RXU_SECOND_STOP;
279
                                else
280
                                        state <= `RXU_IDLE;
281
                                // o_parity_err <= 1'b0;
282
                        end else // state must equal RX_SECOND_STOP
283
                        begin
284
                                if (~ck_uart)
285
                                begin
286
                                        o_frame_err <= 1'b1;
287
                                        state <= `RXU_RESET_IDLE;
288
                                end else begin
289
                                        state <= `RXU_IDLE;
290
                                        o_frame_err  <= 1'b0;
291
                                end
292
                                o_parity_err <= 1'b0;
293
                        end
294
                end else begin
295
                        o_wr <= 1'b0;   // data_reg = data_reg
296
                        baud_counter <= baud_counter - 28'd1;
297
                        o_parity_err <= 1'b0;
298
                        o_frame_err  <= 1'b0;
299
                end
300
        end
301
 
302
endmodule
303
 
304
 

powered by: WebSVN 2.1.0

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