1 |
2 |
olivier.gi |
//----------------------------------------------------------------------------
|
2 |
117 |
olivier.gi |
// Copyright (C) 2009 , Olivier Girard
|
3 |
2 |
olivier.gi |
//
|
4 |
117 |
olivier.gi |
// Redistribution and use in source and binary forms, with or without
|
5 |
|
|
// modification, are permitted provided that the following conditions
|
6 |
|
|
// are met:
|
7 |
|
|
// * Redistributions of source code must retain the above copyright
|
8 |
|
|
// notice, this list of conditions and the following disclaimer.
|
9 |
|
|
// * Redistributions in binary form must reproduce the above copyright
|
10 |
|
|
// notice, this list of conditions and the following disclaimer in the
|
11 |
|
|
// documentation and/or other materials provided with the distribution.
|
12 |
|
|
// * Neither the name of the authors nor the names of its contributors
|
13 |
|
|
// may be used to endorse or promote products derived from this software
|
14 |
|
|
// without specific prior written permission.
|
15 |
2 |
olivier.gi |
//
|
16 |
117 |
olivier.gi |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
17 |
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
18 |
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
19 |
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
20 |
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
21 |
|
|
// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
22 |
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
23 |
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
24 |
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
25 |
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
26 |
|
|
// THE POSSIBILITY OF SUCH DAMAGE
|
27 |
2 |
olivier.gi |
//
|
28 |
|
|
//----------------------------------------------------------------------------
|
29 |
|
|
//
|
30 |
34 |
olivier.gi |
// *File Name: omsp_dbg_uart.v
|
31 |
202 |
olivier.gi |
//
|
32 |
2 |
olivier.gi |
// *Module Description:
|
33 |
|
|
// Debug UART communication interface (8N1, Half-duplex)
|
34 |
|
|
//
|
35 |
|
|
// *Author(s):
|
36 |
|
|
// - Olivier Girard, olgirard@gmail.com
|
37 |
|
|
//
|
38 |
|
|
//----------------------------------------------------------------------------
|
39 |
17 |
olivier.gi |
// $Rev: 202 $
|
40 |
|
|
// $LastChangedBy: olivier.girard $
|
41 |
|
|
// $LastChangedDate: 2015-07-01 23:13:32 +0200 (Wed, 01 Jul 2015) $
|
42 |
|
|
//----------------------------------------------------------------------------
|
43 |
103 |
olivier.gi |
`ifdef OMSP_NO_INCLUDE
|
44 |
|
|
`else
|
45 |
23 |
olivier.gi |
`include "openMSP430_defines.v"
|
46 |
103 |
olivier.gi |
`endif
|
47 |
2 |
olivier.gi |
|
48 |
34 |
olivier.gi |
module omsp_dbg_uart (
|
49 |
2 |
olivier.gi |
|
50 |
|
|
// OUTPUTs
|
51 |
|
|
dbg_addr, // Debug register address
|
52 |
|
|
dbg_din, // Debug register data input
|
53 |
|
|
dbg_rd, // Debug register data read
|
54 |
|
|
dbg_uart_txd, // Debug interface: UART TXD
|
55 |
|
|
dbg_wr, // Debug register data write
|
56 |
202 |
olivier.gi |
|
57 |
2 |
olivier.gi |
// INPUTs
|
58 |
106 |
olivier.gi |
dbg_clk, // Debug unit clock
|
59 |
2 |
olivier.gi |
dbg_dout, // Debug register data output
|
60 |
|
|
dbg_rd_rdy, // Debug register data is ready for read
|
61 |
106 |
olivier.gi |
dbg_rst, // Debug unit reset
|
62 |
2 |
olivier.gi |
dbg_uart_rxd, // Debug interface: UART RXD
|
63 |
|
|
mem_burst, // Burst on going
|
64 |
|
|
mem_burst_end, // End TX/RX burst
|
65 |
|
|
mem_burst_rd, // Start TX burst
|
66 |
|
|
mem_burst_wr, // Start RX burst
|
67 |
106 |
olivier.gi |
mem_bw // Burst byte width
|
68 |
2 |
olivier.gi |
);
|
69 |
|
|
|
70 |
|
|
// OUTPUTs
|
71 |
|
|
//=========
|
72 |
|
|
output [5:0] dbg_addr; // Debug register address
|
73 |
|
|
output [15:0] dbg_din; // Debug register data input
|
74 |
|
|
output dbg_rd; // Debug register data read
|
75 |
|
|
output dbg_uart_txd; // Debug interface: UART TXD
|
76 |
|
|
output dbg_wr; // Debug register data write
|
77 |
|
|
|
78 |
|
|
// INPUTs
|
79 |
|
|
//=========
|
80 |
106 |
olivier.gi |
input dbg_clk; // Debug unit clock
|
81 |
2 |
olivier.gi |
input [15:0] dbg_dout; // Debug register data output
|
82 |
|
|
input dbg_rd_rdy; // Debug register data is ready for read
|
83 |
106 |
olivier.gi |
input dbg_rst; // Debug unit reset
|
84 |
2 |
olivier.gi |
input dbg_uart_rxd; // Debug interface: UART RXD
|
85 |
|
|
input mem_burst; // Burst on going
|
86 |
|
|
input mem_burst_end; // End TX/RX burst
|
87 |
|
|
input mem_burst_rd; // Start TX burst
|
88 |
|
|
input mem_burst_wr; // Start RX burst
|
89 |
|
|
input mem_bw; // Burst byte width
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
//=============================================================================
|
93 |
|
|
// 1) UART RECEIVE LINE SYNCHRONIZTION & FILTERING
|
94 |
|
|
//=============================================================================
|
95 |
|
|
|
96 |
111 |
olivier.gi |
// Synchronize RXD input
|
97 |
2 |
olivier.gi |
//--------------------------------
|
98 |
111 |
olivier.gi |
`ifdef SYNC_DBG_UART_RXD
|
99 |
|
|
|
100 |
|
|
wire uart_rxd_n;
|
101 |
|
|
|
102 |
|
|
omsp_sync_cell sync_cell_uart_rxd (
|
103 |
134 |
olivier.gi |
.data_out (uart_rxd_n),
|
104 |
|
|
.data_in (~dbg_uart_rxd),
|
105 |
|
|
.clk (dbg_clk),
|
106 |
|
|
.rst (dbg_rst)
|
107 |
111 |
olivier.gi |
);
|
108 |
|
|
wire uart_rxd = ~uart_rxd_n;
|
109 |
|
|
`else
|
110 |
|
|
wire uart_rxd = dbg_uart_rxd;
|
111 |
|
|
`endif
|
112 |
202 |
olivier.gi |
|
113 |
111 |
olivier.gi |
// RXD input buffer
|
114 |
|
|
//--------------------------------
|
115 |
|
|
reg [1:0] rxd_buf;
|
116 |
106 |
olivier.gi |
always @ (posedge dbg_clk or posedge dbg_rst)
|
117 |
111 |
olivier.gi |
if (dbg_rst) rxd_buf <= 2'h3;
|
118 |
|
|
else rxd_buf <= {rxd_buf[0], uart_rxd};
|
119 |
2 |
olivier.gi |
|
120 |
|
|
// Majority decision
|
121 |
|
|
//------------------------
|
122 |
|
|
reg rxd_maj;
|
123 |
|
|
|
124 |
134 |
olivier.gi |
wire rxd_maj_nxt = (uart_rxd & rxd_buf[0]) |
|
125 |
202 |
olivier.gi |
(uart_rxd & rxd_buf[1]) |
|
126 |
|
|
(rxd_buf[0] & rxd_buf[1]);
|
127 |
|
|
|
128 |
106 |
olivier.gi |
always @ (posedge dbg_clk or posedge dbg_rst)
|
129 |
134 |
olivier.gi |
if (dbg_rst) rxd_maj <= 1'b1;
|
130 |
106 |
olivier.gi |
else rxd_maj <= rxd_maj_nxt;
|
131 |
2 |
olivier.gi |
|
132 |
134 |
olivier.gi |
wire rxd_s = rxd_maj;
|
133 |
|
|
wire rxd_fe = rxd_maj & ~rxd_maj_nxt;
|
134 |
|
|
wire rxd_re = ~rxd_maj & rxd_maj_nxt;
|
135 |
|
|
wire rxd_edge = rxd_maj ^ rxd_maj_nxt;
|
136 |
202 |
olivier.gi |
|
137 |
2 |
olivier.gi |
//=============================================================================
|
138 |
|
|
// 2) UART STATE MACHINE
|
139 |
|
|
//=============================================================================
|
140 |
|
|
|
141 |
|
|
// Receive state
|
142 |
|
|
//------------------------
|
143 |
134 |
olivier.gi |
reg [2:0] uart_state;
|
144 |
|
|
reg [2:0] uart_state_nxt;
|
145 |
2 |
olivier.gi |
|
146 |
134 |
olivier.gi |
wire sync_done;
|
147 |
|
|
wire xfer_done;
|
148 |
|
|
reg [19:0] xfer_buf;
|
149 |
|
|
wire [19:0] xfer_buf_nxt;
|
150 |
2 |
olivier.gi |
|
151 |
|
|
// State machine definition
|
152 |
|
|
parameter RX_SYNC = 3'h0;
|
153 |
|
|
parameter RX_CMD = 3'h1;
|
154 |
|
|
parameter RX_DATA1 = 3'h2;
|
155 |
|
|
parameter RX_DATA2 = 3'h3;
|
156 |
|
|
parameter TX_DATA1 = 3'h4;
|
157 |
|
|
parameter TX_DATA2 = 3'h5;
|
158 |
|
|
|
159 |
|
|
// State transition
|
160 |
134 |
olivier.gi |
always @(uart_state or xfer_buf_nxt or mem_burst or mem_burst_wr or mem_burst_rd or mem_burst_end or mem_bw)
|
161 |
2 |
olivier.gi |
case (uart_state)
|
162 |
|
|
RX_SYNC : uart_state_nxt = RX_CMD;
|
163 |
|
|
RX_CMD : uart_state_nxt = mem_burst_wr ?
|
164 |
|
|
(mem_bw ? RX_DATA2 : RX_DATA1) :
|
165 |
|
|
mem_burst_rd ?
|
166 |
|
|
(mem_bw ? TX_DATA2 : TX_DATA1) :
|
167 |
134 |
olivier.gi |
(xfer_buf_nxt[`DBG_UART_WR] ?
|
168 |
|
|
(xfer_buf_nxt[`DBG_UART_BW] ? RX_DATA2 : RX_DATA1) :
|
169 |
|
|
(xfer_buf_nxt[`DBG_UART_BW] ? TX_DATA2 : TX_DATA1));
|
170 |
2 |
olivier.gi |
RX_DATA1 : uart_state_nxt = RX_DATA2;
|
171 |
|
|
RX_DATA2 : uart_state_nxt = (mem_burst & ~mem_burst_end) ?
|
172 |
|
|
(mem_bw ? RX_DATA2 : RX_DATA1) :
|
173 |
|
|
RX_CMD;
|
174 |
|
|
TX_DATA1 : uart_state_nxt = TX_DATA2;
|
175 |
|
|
TX_DATA2 : uart_state_nxt = (mem_burst & ~mem_burst_end) ?
|
176 |
|
|
(mem_bw ? TX_DATA2 : TX_DATA1) :
|
177 |
|
|
RX_CMD;
|
178 |
134 |
olivier.gi |
// pragma coverage off
|
179 |
2 |
olivier.gi |
default : uart_state_nxt = RX_CMD;
|
180 |
134 |
olivier.gi |
// pragma coverage on
|
181 |
2 |
olivier.gi |
endcase
|
182 |
202 |
olivier.gi |
|
183 |
2 |
olivier.gi |
// State machine
|
184 |
106 |
olivier.gi |
always @(posedge dbg_clk or posedge dbg_rst)
|
185 |
|
|
if (dbg_rst) uart_state <= RX_SYNC;
|
186 |
2 |
olivier.gi |
else if (xfer_done | sync_done |
|
187 |
|
|
mem_burst_wr | mem_burst_rd) uart_state <= uart_state_nxt;
|
188 |
|
|
|
189 |
|
|
// Utility signals
|
190 |
|
|
wire cmd_valid = (uart_state==RX_CMD) & xfer_done;
|
191 |
134 |
olivier.gi |
wire rx_active = (uart_state==RX_DATA1) | (uart_state==RX_DATA2) | (uart_state==RX_CMD);
|
192 |
2 |
olivier.gi |
wire tx_active = (uart_state==TX_DATA1) | (uart_state==TX_DATA2);
|
193 |
|
|
|
194 |
202 |
olivier.gi |
|
195 |
2 |
olivier.gi |
//=============================================================================
|
196 |
|
|
// 3) UART SYNCHRONIZATION
|
197 |
|
|
//=============================================================================
|
198 |
106 |
olivier.gi |
// After DBG_RST, the host needs to fist send a synchronization character (0x80)
|
199 |
2 |
olivier.gi |
// If this feature doesn't work properly, it is possible to disable it by
|
200 |
|
|
// commenting the DBG_UART_AUTO_SYNC define in the openMSP430.inc file.
|
201 |
|
|
|
202 |
|
|
reg sync_busy;
|
203 |
106 |
olivier.gi |
always @ (posedge dbg_clk or posedge dbg_rst)
|
204 |
|
|
if (dbg_rst) sync_busy <= 1'b0;
|
205 |
2 |
olivier.gi |
else if ((uart_state==RX_SYNC) & rxd_fe) sync_busy <= 1'b1;
|
206 |
|
|
else if ((uart_state==RX_SYNC) & rxd_re) sync_busy <= 1'b0;
|
207 |
|
|
|
208 |
|
|
assign sync_done = (uart_state==RX_SYNC) & rxd_re & sync_busy;
|
209 |
|
|
|
210 |
|
|
`ifdef DBG_UART_AUTO_SYNC
|
211 |
|
|
|
212 |
74 |
olivier.gi |
reg [`DBG_UART_XFER_CNT_W+2:0] sync_cnt;
|
213 |
106 |
olivier.gi |
always @ (posedge dbg_clk or posedge dbg_rst)
|
214 |
134 |
olivier.gi |
if (dbg_rst) sync_cnt <= {{`DBG_UART_XFER_CNT_W{1'b1}}, 3'b000};
|
215 |
|
|
else if (sync_busy | (~sync_busy & sync_cnt[2])) sync_cnt <= sync_cnt+{{`DBG_UART_XFER_CNT_W+2{1'b0}}, 1'b1};
|
216 |
2 |
olivier.gi |
|
217 |
74 |
olivier.gi |
wire [`DBG_UART_XFER_CNT_W-1:0] bit_cnt_max = sync_cnt[`DBG_UART_XFER_CNT_W+2:3];
|
218 |
2 |
olivier.gi |
`else
|
219 |
74 |
olivier.gi |
wire [`DBG_UART_XFER_CNT_W-1:0] bit_cnt_max = `DBG_UART_CNT;
|
220 |
2 |
olivier.gi |
`endif
|
221 |
202 |
olivier.gi |
|
222 |
|
|
|
223 |
2 |
olivier.gi |
//=============================================================================
|
224 |
|
|
// 4) UART RECEIVE / TRANSMIT
|
225 |
|
|
//=============================================================================
|
226 |
202 |
olivier.gi |
|
227 |
2 |
olivier.gi |
// Transfer counter
|
228 |
|
|
//------------------------
|
229 |
74 |
olivier.gi |
reg [3:0] xfer_bit;
|
230 |
|
|
reg [`DBG_UART_XFER_CNT_W-1:0] xfer_cnt;
|
231 |
2 |
olivier.gi |
|
232 |
|
|
wire txd_start = dbg_rd_rdy | (xfer_done & (uart_state==TX_DATA1));
|
233 |
|
|
wire rxd_start = (xfer_bit==4'h0) & rxd_fe & ((uart_state!=RX_SYNC));
|
234 |
74 |
olivier.gi |
wire xfer_bit_inc = (xfer_bit!=4'h0) & (xfer_cnt=={`DBG_UART_XFER_CNT_W{1'b0}});
|
235 |
134 |
olivier.gi |
assign xfer_done = rx_active ? (xfer_bit==4'ha) : (xfer_bit==4'hb);
|
236 |
202 |
olivier.gi |
|
237 |
106 |
olivier.gi |
always @ (posedge dbg_clk or posedge dbg_rst)
|
238 |
|
|
if (dbg_rst) xfer_bit <= 4'h0;
|
239 |
2 |
olivier.gi |
else if (txd_start | rxd_start) xfer_bit <= 4'h1;
|
240 |
|
|
else if (xfer_done) xfer_bit <= 4'h0;
|
241 |
|
|
else if (xfer_bit_inc) xfer_bit <= xfer_bit+4'h1;
|
242 |
|
|
|
243 |
106 |
olivier.gi |
always @ (posedge dbg_clk or posedge dbg_rst)
|
244 |
|
|
if (dbg_rst) xfer_cnt <= {`DBG_UART_XFER_CNT_W{1'b0}};
|
245 |
134 |
olivier.gi |
else if (rx_active & rxd_edge) xfer_cnt <= {1'b0, bit_cnt_max[`DBG_UART_XFER_CNT_W-1:1]};
|
246 |
2 |
olivier.gi |
else if (txd_start | xfer_bit_inc) xfer_cnt <= bit_cnt_max;
|
247 |
134 |
olivier.gi |
else if (|xfer_cnt) xfer_cnt <= xfer_cnt+{`DBG_UART_XFER_CNT_W{1'b1}};
|
248 |
2 |
olivier.gi |
|
249 |
|
|
|
250 |
|
|
// Receive/Transmit buffer
|
251 |
|
|
//-------------------------
|
252 |
134 |
olivier.gi |
assign xfer_buf_nxt = {rxd_s, xfer_buf[19:1]};
|
253 |
2 |
olivier.gi |
|
254 |
106 |
olivier.gi |
always @ (posedge dbg_clk or posedge dbg_rst)
|
255 |
|
|
if (dbg_rst) xfer_buf <= 20'h00000;
|
256 |
2 |
olivier.gi |
else if (dbg_rd_rdy) xfer_buf <= {1'b1, dbg_dout[15:8], 2'b01, dbg_dout[7:0], 1'b0};
|
257 |
|
|
else if (xfer_bit_inc) xfer_buf <= xfer_buf_nxt;
|
258 |
|
|
|
259 |
|
|
|
260 |
|
|
// Generate TXD output
|
261 |
|
|
//------------------------
|
262 |
|
|
reg dbg_uart_txd;
|
263 |
202 |
olivier.gi |
|
264 |
106 |
olivier.gi |
always @ (posedge dbg_clk or posedge dbg_rst)
|
265 |
|
|
if (dbg_rst) dbg_uart_txd <= 1'b1;
|
266 |
2 |
olivier.gi |
else if (xfer_bit_inc & tx_active) dbg_uart_txd <= xfer_buf[0];
|
267 |
|
|
|
268 |
202 |
olivier.gi |
|
269 |
2 |
olivier.gi |
//=============================================================================
|
270 |
|
|
// 5) INTERFACE TO DEBUG REGISTERS
|
271 |
|
|
//=============================================================================
|
272 |
|
|
|
273 |
|
|
reg [5:0] dbg_addr;
|
274 |
106 |
olivier.gi |
always @ (posedge dbg_clk or posedge dbg_rst)
|
275 |
|
|
if (dbg_rst) dbg_addr <= 6'h00;
|
276 |
134 |
olivier.gi |
else if (cmd_valid) dbg_addr <= xfer_buf_nxt[`DBG_UART_ADDR];
|
277 |
2 |
olivier.gi |
|
278 |
|
|
reg dbg_bw;
|
279 |
106 |
olivier.gi |
always @ (posedge dbg_clk or posedge dbg_rst)
|
280 |
|
|
if (dbg_rst) dbg_bw <= 1'b0;
|
281 |
134 |
olivier.gi |
else if (cmd_valid) dbg_bw <= xfer_buf_nxt[`DBG_UART_BW];
|
282 |
2 |
olivier.gi |
|
283 |
|
|
wire dbg_din_bw = mem_burst ? mem_bw : dbg_bw;
|
284 |
|
|
|
285 |
134 |
olivier.gi |
wire [15:0] dbg_din = dbg_din_bw ? {8'h00, xfer_buf_nxt[18:11]} :
|
286 |
|
|
{xfer_buf_nxt[18:11], xfer_buf_nxt[9:2]};
|
287 |
2 |
olivier.gi |
wire dbg_wr = (xfer_done & (uart_state==RX_DATA2));
|
288 |
|
|
wire dbg_rd = mem_burst ? (xfer_done & (uart_state==TX_DATA2)) :
|
289 |
134 |
olivier.gi |
(cmd_valid & ~xfer_buf_nxt[`DBG_UART_WR]) | mem_burst_rd;
|
290 |
2 |
olivier.gi |
|
291 |
202 |
olivier.gi |
|
292 |
|
|
|
293 |
34 |
olivier.gi |
endmodule // omsp_dbg_uart
|
294 |
2 |
olivier.gi |
|
295 |
103 |
olivier.gi |
`ifdef OMSP_NO_INCLUDE
|
296 |
|
|
`else
|
297 |
33 |
olivier.gi |
`include "openMSP430_undefines.v"
|
298 |
103 |
olivier.gi |
`endif
|