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

Subversion Repositories wb4pb

[/] [wb4pb/] [trunk/] [rtl/] [wbs_uart.v] - Blame information for rev 31

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 ste.fis
////////////////////////////////////////////////////////////////////////////////
2
// This sourcecode is released under BSD license.
3
// Please see http://www.opensource.org/licenses/bsd-license.php for details!
4
////////////////////////////////////////////////////////////////////////////////
5
//
6
// Copyright (c) 2010, Stefan Fischer <Ste.Fis@OpenCores.org>
7
// All rights reserved.
8
//
9
// Redistribution and use in source and binary forms, with or without 
10
// modification, are permitted provided that the following conditions are met:
11
//
12
//  * Redistributions of source code must retain the above copyright notice, 
13
//    this list of conditions and the following disclaimer.
14
//  * Redistributions in binary form must reproduce the above copyright notice,
15
//    this list of conditions and the following disclaimer in the documentation
16 31 ste.fis
//    and/or other materials provided with the distribution.
17 12 ste.fis
//
18
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
19
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
20
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
21
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
22
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
23
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
24
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
25
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
26
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
27
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
28
// POSSIBILITY OF SUCH DAMAGE.
29
//
30
////////////////////////////////////////////////////////////////////////////////
31
// filename: wbs_uart.v
32
// description: synthesizable wishbone slave uart sio module using Xilinx (R)
33
//              macros and adding some functionality like a configurable 
34
//              baud rate and buffer level checking 
35
// todo4user: add other uart functionality as needed, i. e. interrupt logic or
36
//            modem control signals
37
// version: 0.0.0
38
// changelog: - 0.0.0, initial release
39
//            - ...
40
////////////////////////////////////////////////////////////////////////////////
41
 
42
 
43
module wbs_uart (
44
  rst,
45
  clk,
46
 
47
  wbs_cyc_i,
48
  wbs_stb_i,
49
  wbs_we_i,
50
  wbs_adr_i,
51
  wbs_dat_m2s_i,
52
  wbs_dat_s2m_o,
53
  wbs_ack_o,
54
 
55
  uart_rx_si_i,
56
  uart_tx_so_o
57
);
58
 
59
  input rst;
60
  wire  rst;
61
  input clk;
62
  wire  clk;
63
 
64
  input wbs_cyc_i;
65
  wire  wbs_cyc_i;
66
  input wbs_stb_i;
67
  wire  wbs_stb_i;
68
  input wbs_we_i;
69
  wire  wbs_we_i;
70
  input[7:0] wbs_adr_i;
71
  wire [7:0] wbs_adr_i;
72
  input[7:0] wbs_dat_m2s_i;
73
  wire [7:0] wbs_dat_m2s_i;
74
  output[7:0] wbs_dat_s2m_o;
75
  reg   [7:0] wbs_dat_s2m_o;
76
  output wbs_ack_o;
77
  reg    wbs_ack_o;
78
 
79
  input uart_rx_si_i;
80
  wire  uart_rx_si_i;
81
  output uart_tx_so_o;
82
  wire   uart_tx_so_o;
83
 
84
  wire wb_reg_we;
85
 
86
  parameter ADDR_MSB = 1;
87
  parameter[7:0] UART_RXTX_ADDR = 8'h00;
88
  parameter[7:0] UART_SR_ADDR = 8'h01;
89
  parameter UART_SR_RX_F_FLAG = 0;
90
  parameter UART_SR_RX_HF_FLAG = 1;
91
  parameter UART_SR_RX_DP_FLAG = 2;
92
  parameter UART_SR_TX_F_FLAG = 4;
93
  parameter UART_SR_TX_HF_FLAG = 5;
94
  parameter[7:0] UART_BAUD_LO_ADDR = 8'h02;
95
  parameter[7:0] UART_BAUD_HI_ADDR = 8'h03;
96
 
97
  reg[15:0] baud_count;
98
  reg[15:0] baud_limit;
99
 
100
  reg en_16_x_baud;
101
 
102
  reg rx_read_buffer;
103
  wire rx_buffer_full;
104
  wire rx_buffer_half_full;
105
  wire rx_buffer_data_present;
106
  wire[7:0] rx_data_out;
107
 
108
  reg tx_write_buffer;
109
  wire tx_buffer_full;
110
  wire tx_buffer_half_full;
111
 
112
  // internal register write enable signal
113
  assign wb_reg_we = wbs_cyc_i && wbs_stb_i && wbs_we_i;
114
 
115
  always@(posedge clk) begin
116
 
117
    // baud rate configuration:
118
    // baud_limit = round( system clock frequency / (16 * baud rate) ) - 1
119
    // i. e. 9600 baud at 50 MHz system clock =>
120
    // baud_limit = round( 50.0E6 / (16 * 9600) ) - 1 = 325 = 0x0145
121
 
122
    // baud timer
123
    if (baud_count == baud_limit) begin
124
      baud_count <= 16'h0000;
125
      en_16_x_baud <= 1'b1;
126
    end else begin
127
      baud_count <= baud_count + 1;
128
      en_16_x_baud <= 1'b0;
129
    end
130
 
131
    rx_read_buffer <= 1'b0;
132
    tx_write_buffer <= 1'b0;
133
 
134
    wbs_dat_s2m_o <= 8'h00;
135
    // registered wishbone slave handshake (default)
136
    wbs_ack_o <= wbs_cyc_i && wbs_stb_i && (! wbs_ack_o);
137
 
138
    case(wbs_adr_i[ADDR_MSB:0])
139
      // receive/transmit buffer access
140
      UART_RXTX_ADDR[ADDR_MSB:0]: begin
141
        if (wbs_cyc_i && wbs_stb_i)
142
          // overwriting wishbone slave handshake for blocking transactions 
143
          // to rx/tx fifos by using buffer status flags
144
          if (wbs_we_i) begin
145
            tx_write_buffer <= (! tx_buffer_full) && (! wbs_ack_o);
146
            wbs_ack_o <= (! tx_buffer_full) && (! wbs_ack_o);
147
          end else begin
148
            rx_read_buffer <= rx_buffer_data_present && (! wbs_ack_o);
149
            wbs_ack_o <= rx_buffer_data_present && (! wbs_ack_o);
150
          end
151
        wbs_dat_s2m_o <= rx_data_out;
152
      end
153
      // status register access
154
      UART_SR_ADDR[ADDR_MSB:0]: begin
155
        wbs_dat_s2m_o[UART_SR_RX_F_FLAG] <= rx_buffer_full;
156
        wbs_dat_s2m_o[UART_SR_RX_HF_FLAG] <= rx_buffer_half_full;
157
        wbs_dat_s2m_o[UART_SR_RX_DP_FLAG] <= rx_buffer_data_present;
158
        wbs_dat_s2m_o[UART_SR_TX_F_FLAG] <= tx_buffer_full;
159
        wbs_dat_s2m_o[UART_SR_TX_HF_FLAG] <= tx_buffer_half_full;
160
      end
161
      // baud rate register access / low byte
162
      UART_BAUD_LO_ADDR[ADDR_MSB:0]: begin
163
        if (wb_reg_we) begin
164
          baud_limit[7:0] <= wbs_dat_m2s_i;
165
          baud_count <= 16'h0000;
166
        end
167
        wbs_dat_s2m_o <= baud_limit[7:0];
168
      end
169
      // baud rate register access / high byte
170
      UART_BAUD_HI_ADDR[ADDR_MSB:0]: begin
171
        if (wb_reg_we)  begin
172
          baud_limit[15:8] <= wbs_dat_m2s_i;
173
          baud_count <= 16'h0000;
174
        end
175
        wbs_dat_s2m_o <= baud_limit[15:8];
176
      end
177
      default: ;
178
    endcase
179
 
180 17 ste.fis
    if (rst)
181 12 ste.fis
      wbs_ack_o <= 1'b0;
182
 
183
  end
184
 
185
  // Xilinx (R) uart macro instances
186
  //////////////////////////////////
187
 
188
  uart_rx inst_uart_rx (
189
    .serial_in(uart_rx_si_i),
190
    .data_out(rx_data_out),
191
    .read_buffer(rx_read_buffer),
192
    .reset_buffer(rst),
193
    .en_16_x_baud(en_16_x_baud),
194
    .buffer_data_present(rx_buffer_data_present),
195
    .buffer_full(rx_buffer_full),
196
    .buffer_half_full(rx_buffer_half_full),
197
    .clk(clk)
198
  );
199
 
200
  uart_tx inst_uart_tx (
201
    .data_in(wbs_dat_m2s_i),
202
    .write_buffer(tx_write_buffer),
203
    .reset_buffer(rst),
204
    .en_16_x_baud(en_16_x_baud),
205
    .serial_out(uart_tx_so_o),
206
    .buffer_full(tx_buffer_full),
207
    .buffer_half_full(tx_buffer_half_full),
208
    .clk(clk)
209
  );
210
 
211
endmodule

powered by: WebSVN 2.1.0

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