1 |
2 |
jclaytons |
//-----------------------------------------------------------------------------
|
2 |
|
|
// Auto Baud core
|
3 |
|
|
//
|
4 |
|
|
// This file is part of the "auto_baud" project.
|
5 |
|
|
// http://www.opencores.org/
|
6 |
|
|
//
|
7 |
|
|
//
|
8 |
|
|
// Description: See description below (which suffices for IP core
|
9 |
|
|
// specification document.)
|
10 |
|
|
//
|
11 |
|
|
// Copyright (C) 2002 John Clayton and OPENCORES.ORG
|
12 |
|
|
//
|
13 |
|
|
// This source file may be used and distributed without restriction provided
|
14 |
|
|
// that this copyright statement is not removed from the file and that any
|
15 |
|
|
// derivative work contains the original copyright notice and the associated
|
16 |
|
|
// disclaimer.
|
17 |
|
|
//
|
18 |
|
|
// This source file is free software; you can redistribute it and/or modify
|
19 |
|
|
// it under the terms of the GNU Lesser General Public License as published
|
20 |
|
|
// by the Free Software Foundation; either version 2.1 of the License, or
|
21 |
|
|
// (at your option) any later version.
|
22 |
|
|
//
|
23 |
|
|
// This source is distributed in the hope that it will be useful, but WITHOUT
|
24 |
|
|
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
25 |
|
|
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
|
26 |
|
|
// License for more details.
|
27 |
|
|
//
|
28 |
|
|
// You should have received a copy of the GNU Lesser General Public License
|
29 |
|
|
// along with this source.
|
30 |
|
|
// If not, download it from http://www.opencores.org/lgpl.shtml
|
31 |
|
|
//
|
32 |
|
|
//-----------------------------------------------------------------------------
|
33 |
|
|
//
|
34 |
|
|
// Author: John Clayton
|
35 |
|
|
// Date : Aug. 20, 2002
|
36 |
|
|
// Update: Aug. 20, 2002 copied this file from rs232_syscon.v (pared down).
|
37 |
|
|
// Update: Sep. 4, 2002 First test of this module. The baud rate appears
|
38 |
|
|
// to be produced at 1/2 of the desired rate!
|
39 |
|
|
// Update: Sep. 5, 2002 First working results. Fixed measurement (shift had
|
40 |
|
|
// been left out.) Removed debug port since the unit
|
41 |
|
|
// appears to be working fine. It Worked for all of the
|
42 |
|
|
// following BAUD rates, using 49.152 MHz clock and
|
43 |
|
|
// CLK_FACTOR = 8 and 16 bit main counter:
|
44 |
|
|
// 300, 1200, 2400, 9600, 19200, 38400, 57600, 115200.
|
45 |
|
|
// Next step is to build the "tracking" version that
|
46 |
|
|
// doesn't need a reset to find a new BAUD rate...
|
47 |
|
|
// Update: Sep. 13, 2002 Added test data from "auto_baud_with_tracking.v"
|
48 |
|
|
// module tests. This module has also been tested
|
49 |
|
|
// at various speeds, and it works well.
|
50 |
|
|
//
|
51 |
|
|
// Description
|
52 |
|
|
//-----------------------------------------------------------------------------
|
53 |
|
|
// This is a state-machine driven core that measures transition intervals
|
54 |
|
|
// in a particular character arriving via rs232 transmission (i.e. PC serial
|
55 |
|
|
// port.) Measurements of time intervals between transitions in the received
|
56 |
|
|
// character are then used to generate a baud rate clock for use in serial
|
57 |
|
|
// communications back and forth with the device that originally transmitted
|
58 |
|
|
// the measured character. The clock which is generated is in reality a
|
59 |
|
|
// clock enable pulse, one single clock wide, occurring at a rate suitable
|
60 |
|
|
// for use in serial communications. (This means that it will be generated
|
61 |
|
|
// at 4x or 8x or 16x the actual measured baud rate of the received character.
|
62 |
|
|
// The multiplication factor is called "CLOCK_FACTOR_PP" and is a settable
|
63 |
|
|
// parameter within this module. The parameter "CLOCK_FACTOR_PP" need not
|
64 |
|
|
// be a power of two, but it should be a number between 2 and 16 inclusive.)
|
65 |
|
|
//
|
66 |
|
|
// The particular character which is targeted for measurement and verification
|
67 |
|
|
// in this module is: carriage return (CR) = 0x0d = 13.
|
68 |
|
|
// This particular character was chosen because it is frequently used at the
|
69 |
|
|
// end of a command line, as entered at a keyboard by a human user interacting
|
70 |
|
|
// with a command interpreter. It is anticipated that the user would press
|
71 |
|
|
// the "enter" key once upon initializing communications with the electronic
|
72 |
|
|
// device, and the resulting carriage return character would be used for
|
73 |
|
|
// determining BAUD rate, thus allowing the device to respond at the correct
|
74 |
|
|
// rate, and to carry on further communications. The electronic device using
|
75 |
|
|
// this "auto_baud" module adjusts its baud rate to match the baud rate of
|
76 |
|
|
// the received data. This works for all baud rates, within certain limits,
|
77 |
|
|
// and for all system clock rates, within certain limits.
|
78 |
|
|
//
|
79 |
|
|
// Received serially, the carriage return appears as the following waveform:
|
80 |
|
|
// ________ __ ____ _______________
|
81 |
|
|
// |__|d0|__|d2d3|________|stop
|
82 |
|
|
// start d1 d4d5d6d7
|
83 |
|
|
//
|
84 |
|
|
// The waveform is shown with an identical "high" time and "low" time for
|
85 |
|
|
// each bit. However, actual measurements taken using a logic analyzer
|
86 |
|
|
// on characters received from a PC show that the times are not equal.
|
87 |
|
|
// The "high" times turned out shorter, and the "low" times longer...
|
88 |
|
|
// Therefore, this module attempts to average out this discrepancy by
|
89 |
|
|
// measuring one low time and one high time.
|
90 |
|
|
//
|
91 |
|
|
// Since the transition measurements must unavoidably contain small amounts
|
92 |
|
|
// of error, the measurements are made during the beginning 2 bits of
|
93 |
|
|
// the received character, (that is, start bit and data bit zero).
|
94 |
|
|
// Then the measurement is immediately transformed into a baud rate clock,
|
95 |
|
|
// used to verify correct reception of the remaining 8 bits of the character.
|
96 |
|
|
// If the entire character is not received correctly using the generated
|
97 |
|
|
// baud rate, then the measurement is scrapped, and the unit goes into an
|
98 |
|
|
// idle scanning mode waiting for another character to test.
|
99 |
|
|
//
|
100 |
|
|
// This effectively filters out characters that the unit is not interested in
|
101 |
|
|
// receiving (anything that is not a carriage return.) There is a slight
|
102 |
|
|
// possibility that a group of other characters could appear by random
|
103 |
|
|
// chance in a configuration that resembles a carriage return closely enough
|
104 |
|
|
// that the unit might accept the measurement and produce a baud clock too
|
105 |
|
|
// low. But the probability of this happening is remote enough that the
|
106 |
|
|
// unit is considered highly "robust" in normal use, especially when used
|
107 |
|
|
// for command entry by humans. It would take a very clever user indeed, to
|
108 |
|
|
// enter the correct series of characters with the correct intercharacter
|
109 |
|
|
// timing needed to possibly "fool" the unit!
|
110 |
|
|
//
|
111 |
|
|
// (Also, the baud rate produced falls within certain limits imposed by
|
112 |
|
|
// the hardware of the unit, which prevents the auto_baud unit from mistaking
|
113 |
|
|
// a series of short glitches on the serial data line for a really
|
114 |
|
|
// fast CR character.)
|
115 |
|
|
//
|
116 |
|
|
// The first carriage return character received will produce a BAUD rate clock
|
117 |
|
|
// and the unit will indicate a "locked" condition. From that point onward,
|
118 |
|
|
// the unit will continue running, but it will not scan for any more
|
119 |
|
|
// input characters. The only way to reset the unit to its initial condition
|
120 |
|
|
// is through the use of the "reset_i" pin. Following reset, the unit is
|
121 |
|
|
// once again looking for a carriage return character to lock on to.
|
122 |
|
|
// Another module, called "auto_baud_with_tracking.v" handles situations where
|
123 |
|
|
// you might want to have the BAUD rate change dynamically.
|
124 |
|
|
//
|
125 |
|
|
//
|
126 |
|
|
// NOTES:
|
127 |
|
|
// - This module uses a counter to divide down the clk_i signal to produce the
|
128 |
|
|
// baud_clk_o signal. Since the frequency of baud_clk_o is nominally
|
129 |
|
|
// CLOCK_FACTOR_PP * rx_baud_rate, where "rx_baud_rate" is the baud rate
|
130 |
|
|
// of the received character, then the higher you make CLOCK_FACTOR_PP, the
|
131 |
|
|
// higher the generated baud_clk_o signal frequency, and hence the lower the
|
132 |
|
|
// resolution of the divider. Therefore, using a lower value for the
|
133 |
|
|
// CLOCK_FACTOR_PP will allow you to use a lower clk_i with this module.
|
134 |
|
|
// - To set LOG2_MAX_COUNT_PP, remember (max_count*CLOCK_FACTOR_PP)/Fclk_i
|
135 |
|
|
// is the maximum measurement time that can be accomodated by the circuit.
|
136 |
|
|
// (where Fclk_i is the frequency of clk_i, and 1/Fclk_i is the period.)
|
137 |
|
|
// Therefore, set LOG2_MAX_COUNT_PP so that the maximum measurement time
|
138 |
|
|
// is at least as long as 2x the baud interval of the slowest received
|
139 |
|
|
// serial data (2x because there are two bits involved in the measurement!)
|
140 |
|
|
// For example, for Fclk_i = 20MHz, CLOCK_FACTOR_PP = 4 and a minimum
|
141 |
|
|
// baud rate of 115,200, you would calculate:
|
142 |
|
|
//
|
143 |
|
|
// (max_count * CLOCK_FACTOR_PP)*1/Fclk_i >= 2/Fbaud_max
|
144 |
|
|
//
|
145 |
|
|
// Solving for the bit width of the max_count counter...
|
146 |
|
|
//
|
147 |
|
|
// LOG2_MAX_COUNT_PP >= ceil(log_base_2(max_count))
|
148 |
|
|
//
|
149 |
|
|
// >= ceil(log_base_2(2*Fclk_i/(Fbaud_max*CLOCK_FACTOR_PP)))
|
150 |
|
|
//
|
151 |
|
|
// >= ceil(log_base_2(2*20E6/(115200*4)))
|
152 |
|
|
//
|
153 |
|
|
// >= ceil(log_base_2(86.8))
|
154 |
|
|
//
|
155 |
|
|
// >= 7 bits.
|
156 |
|
|
//
|
157 |
|
|
// - In the above example, the maximum count would approach 87, which means
|
158 |
|
|
// that a measurement error of 1 count is about (1/87)=approx. 1.15%. This
|
159 |
|
|
// is an acceptable level of error for a baud rate clock. Notice that the
|
160 |
|
|
// lower baud rates have an even smaller error percentage (Yes!) but that
|
161 |
|
|
// they require a much larger measurement counter... For instance,
|
162 |
|
|
// to lock onto 300 baud using the same example above, would require:
|
163 |
|
|
//
|
164 |
|
|
// LOG2_MAX_COUNT_PP >= ceil(log_base_2(40000000/1200))
|
165 |
|
|
//
|
166 |
|
|
// >= ceil(log_base_2(33333.3))
|
167 |
|
|
//
|
168 |
|
|
// >= ceil(15.024678)
|
169 |
|
|
//
|
170 |
|
|
// >= 16 bits.
|
171 |
|
|
//
|
172 |
|
|
// - If the percentage error for your highest desired baud rate is greater
|
173 |
|
|
// than a few percent, you might want to use a higher Fclk_i or else a
|
174 |
|
|
// lower CLOCK_FACTOR_PP.
|
175 |
|
|
//
|
176 |
|
|
// - Using the default settings: CLK_FACTOR_PP = 8, LOG2_MAX_COUNT_PP = 16
|
177 |
|
|
// The following test results were obtained, using an actual session in
|
178 |
|
|
// hyperterm, looking for correct readable character transmission both
|
179 |
|
|
// directions. (Note: These tests were performed at "human interface"
|
180 |
|
|
// speeds. High speed or "back-to-back" character transmission might
|
181 |
|
|
// exhibit worse performance than these results.)
|
182 |
|
|
// The test results shown below were actually obtained using the more
|
183 |
|
|
// complex "auto_baud_with_tracking.v" module. Similar or better results
|
184 |
|
|
// are expected with this module.
|
185 |
|
|
//
|
186 |
|
|
// Clk_i
|
187 |
|
|
// Freq.
|
188 |
|
|
// (MHz) 110 300 1200 2400 4800 9600 19200 57600 115200
|
189 |
|
|
// ------ ---------------------------------------------------------------
|
190 |
|
|
// 98 FAIL pass pass pass pass pass pass pass pass
|
191 |
|
|
// 55 FAIL pass pass pass pass pass pass pass pass
|
192 |
|
|
// 49 pass pass pass pass pass pass pass pass pass
|
193 |
|
|
// 24.5 pass pass pass pass pass pass pass pass FAIL
|
194 |
|
|
// 12 pass pass pass pass pass pass pass pass FAIL
|
195 |
|
|
// 6 pass pass pass pass pass pass pass FAIL FAIL
|
196 |
|
|
// 3 pass pass pass pass pass FAIL FAIL FAIL FAIL
|
197 |
|
|
// 1.5 pass pass pass pass FAIL FAIL FAIL FAIL FAIL
|
198 |
|
|
//
|
199 |
|
|
//
|
200 |
|
|
//-------------------------------------------------------------------------------------
|
201 |
|
|
|
202 |
|
|
`define LOG2_MAX_CLOCK_FACTOR 4 // Sets the size of the CLOCK_FACTOR
|
203 |
|
|
// prescaler.
|
204 |
|
|
`define BITS_PER_CHAR 8 // Include parity, if used, but not
|
205 |
|
|
// start and stop bits...
|
206 |
|
|
|
207 |
|
|
// Note: Simply changing the template bits does not reconfigure the
|
208 |
|
|
// module to look for a different character (because a new measurement
|
209 |
|
|
// window would have to be defined for a different character...)
|
210 |
|
|
// The template bits are the exact bits used during verify, against
|
211 |
|
|
// which the incoming character is checked.
|
212 |
|
|
// The LSB of the character is discarded, and the stop bit is appended
|
213 |
|
|
// since it is the last bit used during verify.
|
214 |
|
|
//
|
215 |
|
|
// so, for N:8:1 (no parity, 8 data bits, 1 stop bit) it is:
|
216 |
|
|
// = {1,(character>>1)} = 9'h086
|
217 |
|
|
// or, with parity included it is:
|
218 |
|
|
// = {1,parity_bit,(character>>1)} = 9'h106
|
219 |
|
|
//
|
220 |
|
|
`define TEMPLATE_BITS 9'h086 // Carriage return && termination flag
|
221 |
|
|
|
222 |
|
|
|
223 |
|
|
module auto_baud
|
224 |
|
|
(
|
225 |
|
|
clk_i,
|
226 |
|
|
reset_i,
|
227 |
|
|
serial_dat_i,
|
228 |
|
|
auto_baud_locked_o,
|
229 |
|
|
baud_clk_o
|
230 |
|
|
);
|
231 |
|
|
|
232 |
|
|
|
233 |
|
|
// Parameters
|
234 |
|
|
|
235 |
|
|
// CLOCK_FACTOR_PP can be from [2..16] inclusive.
|
236 |
|
|
parameter CLOCK_FACTOR_PP = 8; // Baud clock multiplier
|
237 |
|
|
parameter LOG2_MAX_COUNT_PP = 16; // Bit width of measurement counter
|
238 |
|
|
|
239 |
|
|
// State encodings, provided as parameters
|
240 |
|
|
// for flexibility to the one instantiating the module.
|
241 |
|
|
// In general, the default values need not be changed.
|
242 |
|
|
|
243 |
|
|
// There is one state machines: m1.
|
244 |
|
|
// "default" state upon power-up and configuration is:
|
245 |
|
|
// "m1_idle" because that is the all zero state.
|
246 |
|
|
|
247 |
|
|
parameter m1_idle = 4'h0; // Initial state (scanning)
|
248 |
|
|
parameter m1_measure_0 = 4'h1; // start bit (measuring)
|
249 |
|
|
parameter m1_measure_1 = 4'h2; // debounce (measuring)
|
250 |
|
|
parameter m1_measure_2 = 4'h3; // data bit 0 (measuring)
|
251 |
|
|
parameter m1_measure_3 = 4'h4; // debounce (measuring)
|
252 |
|
|
parameter m1_measure_4 = 4'h5; // measurement done (headed to verify)
|
253 |
|
|
parameter m1_verify_0 = 4'h8; // data bit 1 (verifying)
|
254 |
|
|
parameter m1_verify_1 = 4'h9; // data bit 2 (verifying)
|
255 |
|
|
parameter m1_run = 4'h6; // running
|
256 |
|
|
parameter m1_verify_failed = 4'h7; // resetting (headed back to idle)
|
257 |
|
|
|
258 |
|
|
// I/O declarations
|
259 |
|
|
input clk_i; // System clock input
|
260 |
|
|
input reset_i; // Reset signal for this module
|
261 |
|
|
input serial_dat_i; // TTL level serial data signal
|
262 |
|
|
|
263 |
|
|
output auto_baud_locked_o; // Indicates BAUD clock is being generated
|
264 |
|
|
output baud_clk_o; // BAUD clock output (actually a clock enable)
|
265 |
|
|
|
266 |
|
|
|
267 |
|
|
// Internal signal declarations
|
268 |
|
|
wire mid_bit_count; // During measurement, advances measurement counter
|
269 |
|
|
// (Using the mid bit time to advance the
|
270 |
|
|
// measurement timer accomplishes a timing "round"
|
271 |
|
|
// so that the timing measurement is as accurate
|
272 |
|
|
// as possible.)
|
273 |
|
|
// During verify, pulses at mid bit time are used
|
274 |
|
|
// to signal the state machine to check a data bit.
|
275 |
|
|
wire main_count_rollover; // Causes main_count to roll over
|
276 |
|
|
wire clock_count_rollover; // Causes clock_count to roll over
|
277 |
|
|
// (when clock_count is used as a
|
278 |
|
|
// clock_factor prescaler)
|
279 |
|
|
wire enable_clock_count; // Logic that determines when clock_count
|
280 |
|
|
// should be counting
|
281 |
|
|
wire verify_done; // Indicates finish of verification time
|
282 |
|
|
|
283 |
|
|
reg idle; // Indicates state
|
284 |
|
|
reg run; // Indicates state
|
285 |
|
|
reg measure; // Indicates state
|
286 |
|
|
reg clear_counters; // Pulses once when measurement is done.
|
287 |
|
|
reg verify; // Indicates state
|
288 |
|
|
reg character_miscompare; // Indicates character did not verify
|
289 |
|
|
reg [`LOG2_MAX_CLOCK_FACTOR-1:0] clock_count; // Clock_factor prescaler,
|
290 |
|
|
// and mid_bit counter.
|
291 |
|
|
reg [LOG2_MAX_COUNT_PP-1:0] main_count; // Main counter register
|
292 |
|
|
reg [LOG2_MAX_COUNT_PP-1:0] measurement; // Stored measurement count
|
293 |
|
|
reg [`BITS_PER_CHAR:0] target_bits; // Character bits to compare
|
294 |
|
|
// (lsb is not needed, since it is used up during measurement time,
|
295 |
|
|
// but the stop bit and possible parity bit are needed.)
|
296 |
|
|
|
297 |
|
|
// For the state machine
|
298 |
|
|
reg [3:0] m1_state;
|
299 |
|
|
reg [3:0] m1_next_state;
|
300 |
|
|
|
301 |
|
|
|
302 |
|
|
|
303 |
|
|
//--------------------------------------------------------------------------
|
304 |
|
|
// Instantiations
|
305 |
|
|
//--------------------------------------------------------------------------
|
306 |
|
|
|
307 |
|
|
|
308 |
|
|
//--------------------------------------------------------------------------
|
309 |
|
|
// Module code
|
310 |
|
|
//--------------------------------------------------------------------------
|
311 |
|
|
|
312 |
|
|
|
313 |
|
|
// This is the CLOCK_FACTOR_PP prescaler and also mid_bit_count counter
|
314 |
|
|
assign enable_clock_count = measure || (verify && main_count_rollover);
|
315 |
|
|
always @(posedge clk_i or posedge reset_i)
|
316 |
|
|
begin
|
317 |
|
|
if (reset_i) clock_count <= 0;
|
318 |
|
|
else if (clear_counters) clock_count <= 0;
|
319 |
|
|
else if (enable_clock_count)
|
320 |
|
|
begin // Must have been clk_i edge
|
321 |
|
|
if (clock_count_rollover) clock_count <= 0;
|
322 |
|
|
else clock_count <= clock_count + 1;
|
323 |
|
|
end
|
324 |
|
|
end
|
325 |
|
|
// Counter rollover condition
|
326 |
|
|
assign clock_count_rollover = (clock_count == (CLOCK_FACTOR_PP-1));
|
327 |
|
|
// This condition signals the middle of a bit time, for use during the
|
328 |
|
|
// verify stage. Also, this signal is used to advance the main count,
|
329 |
|
|
// instead of "clock_count_rollover." This produces an effective "rounding"
|
330 |
|
|
// operation for the measurement (which would otherwise "truncate" any
|
331 |
|
|
// fraction of time contained in this counter at the instant the measurement
|
332 |
|
|
// is finished.
|
333 |
|
|
// (The "enable_clock_count" is included in order to make the pulse narrow,
|
334 |
|
|
// only one clock wide...)
|
335 |
|
|
assign mid_bit_count = (
|
336 |
|
|
(clock_count == ((CLOCK_FACTOR_PP>>1)-1))
|
337 |
|
|
&& enable_clock_count
|
338 |
|
|
);
|
339 |
|
|
|
340 |
|
|
// This is the main counter. During measurement, it advances once for
|
341 |
|
|
// each CLOCK_FACTOR_PP cycles of clk_i. This accumulated measurement
|
342 |
|
|
// is then latched into "measurement" when the state machine determines that
|
343 |
|
|
// the measurement interval is finished.
|
344 |
|
|
// During verify and idle_run times (whenever the baud rate clock is used)
|
345 |
|
|
// this counter is allowed to run freely, advancing once each clk_i, but being
|
346 |
|
|
// reset when it reaches a total count of "measurement" clock cycles. The
|
347 |
|
|
// signal that reset the counter during this type of operation is the baud
|
348 |
|
|
// rate clock.
|
349 |
|
|
always @(posedge clk_i or posedge reset_i)
|
350 |
|
|
begin
|
351 |
|
|
if (reset_i) main_count <= 0;
|
352 |
|
|
else // must have been clk_i edge
|
353 |
|
|
begin
|
354 |
|
|
// Clear main count when measurement is done
|
355 |
|
|
if (clear_counters) main_count <= 0;
|
356 |
|
|
// If measuring, advance once per CLOCK_FACTOR_PP clk_i pulses.
|
357 |
|
|
else if (measure && mid_bit_count) main_count <= main_count + 1;
|
358 |
|
|
// If verifying or running, check reset conditions,
|
359 |
|
|
// otherwise advance always.
|
360 |
|
|
else if (verify || run)
|
361 |
|
|
begin
|
362 |
|
|
if (main_count_rollover) main_count <= 0;
|
363 |
|
|
else main_count <= main_count + 1;
|
364 |
|
|
end
|
365 |
|
|
end
|
366 |
|
|
end
|
367 |
|
|
|
368 |
|
|
// This is a shift register used to provide "target" character bits one at
|
369 |
|
|
// a time for verification as they are "received" (sampled) using the
|
370 |
|
|
// candidate baud clock.
|
371 |
|
|
always @(posedge clk_i or posedge reset_i)
|
372 |
|
|
begin
|
373 |
|
|
if (reset_i) target_bits <= `TEMPLATE_BITS;
|
374 |
|
|
else // must have been a clock edge
|
375 |
|
|
begin
|
376 |
|
|
if (~verify) target_bits <= `TEMPLATE_BITS;
|
377 |
|
|
if (verify && mid_bit_count) target_bits <= {0,(target_bits>>1)};
|
378 |
|
|
end
|
379 |
|
|
end
|
380 |
|
|
// It is done when only the stop bit is left in the shift register.
|
381 |
|
|
assign verify_done = (
|
382 |
|
|
(target_bits == 1)
|
383 |
|
|
&& verify
|
384 |
|
|
&& mid_bit_count
|
385 |
|
|
);
|
386 |
|
|
|
387 |
|
|
// This is a flip-flop used to keep track of whether the verify operation
|
388 |
|
|
// is succeeding or not. Any target bits that do not match the received
|
389 |
|
|
// data at the sampling edge, will cause the verify_failed bit to go high.
|
390 |
|
|
// This is what the state machine looks at to determine whether it passed
|
391 |
|
|
// or not.
|
392 |
|
|
always @(posedge clk_i or posedge reset_i)
|
393 |
|
|
begin
|
394 |
|
|
if (reset_i) character_miscompare <= 0;
|
395 |
|
|
else // Must have been a clock edge
|
396 |
|
|
begin
|
397 |
|
|
if (idle) character_miscompare <= 0;
|
398 |
|
|
if (verify && mid_bit_count
|
399 |
|
|
&& (target_bits[0] ^ serial_dat_i)) character_miscompare <= 1;
|
400 |
|
|
end
|
401 |
|
|
end
|
402 |
|
|
|
403 |
|
|
|
404 |
|
|
// This is the measurement storage latch. The final measured time count
|
405 |
|
|
// from main_count is stored in this latch upon completion of the measurement
|
406 |
|
|
// interval. The value stored in this latch is used whenever the baud clock
|
407 |
|
|
// is being generated, to reset the main count (causing a "rollover").
|
408 |
|
|
always @(posedge clk_i or posedge idle)
|
409 |
|
|
begin
|
410 |
|
|
// Set to all ones during idle (asynchronous).
|
411 |
|
|
if (idle) measurement <= -1;
|
412 |
|
|
// Otherwise, there must have been a clk_i edge
|
413 |
|
|
// When the measurement is done, the counters are cleared, and the time
|
414 |
|
|
// interval must be stored before it is cleared away...
|
415 |
|
|
// This also causes a store following a failed verify state on the way back
|
416 |
|
|
// into idle, but the idle state clears out the false measurement anyway.
|
417 |
|
|
else if (clear_counters) measurement <= (main_count>>1);
|
418 |
|
|
end
|
419 |
|
|
|
420 |
|
|
// This is effectively the baud clock signal
|
421 |
|
|
// But it is prevented from reaching the output pin during verification...
|
422 |
|
|
// It is only allowed out of the module during idle_run state.
|
423 |
|
|
assign main_count_rollover = (main_count == measurement);
|
424 |
|
|
assign baud_clk_o = (main_count_rollover && run);
|
425 |
|
|
|
426 |
|
|
|
427 |
|
|
// This is state machine m1. It checks the status of the serial_dat_i line
|
428 |
|
|
// and coordinates the measurement of the time interval of the first two
|
429 |
|
|
// bits of the received character, which is the "measurement interval."
|
430 |
|
|
// Following the measurement interval, the state machine enters a new
|
431 |
|
|
// phase of bit verification. If the measured time interval is accurate
|
432 |
|
|
// enough to measure the remaining 8 bits of the character correctly, then
|
433 |
|
|
// the measurement is accepted, and the baud rate clock is driven onto
|
434 |
|
|
// the baud_clk_o output pin. Incidentally, the process of verification
|
435 |
|
|
// effectively filters out all characters which are not the desired target
|
436 |
|
|
// character for measurement. In this case, the target character is the
|
437 |
|
|
// carriage return.
|
438 |
|
|
|
439 |
|
|
|
440 |
|
|
// State register
|
441 |
|
|
always @(posedge clk_i or posedge reset_i)
|
442 |
|
|
begin : m1_state_register
|
443 |
|
|
if (reset_i) m1_state <= m1_idle; // asynchronous reset
|
444 |
|
|
else m1_state <= m1_next_state;
|
445 |
|
|
end
|
446 |
|
|
|
447 |
|
|
// State transition logic
|
448 |
|
|
always @(m1_state
|
449 |
|
|
or mid_bit_count
|
450 |
|
|
or serial_dat_i
|
451 |
|
|
or verify_done
|
452 |
|
|
or character_miscompare
|
453 |
|
|
)
|
454 |
|
|
begin : m1_state_logic
|
455 |
|
|
|
456 |
|
|
// Default values for outputs. The individual states can override these.
|
457 |
|
|
idle <= 1'b0;
|
458 |
|
|
run <= 1'b0;
|
459 |
|
|
measure <= 1'b0;
|
460 |
|
|
clear_counters <= 1'b0;
|
461 |
|
|
verify <= 1'b0;
|
462 |
|
|
|
463 |
|
|
case (m1_state) // synthesis parallel_case
|
464 |
|
|
|
465 |
|
|
m1_idle :
|
466 |
|
|
begin
|
467 |
|
|
idle <= 1'b1;
|
468 |
|
|
if (serial_dat_i == 0) m1_next_state <= m1_measure_0;
|
469 |
|
|
else m1_next_state <= m1_idle;
|
470 |
|
|
end
|
471 |
|
|
|
472 |
|
|
m1_measure_0 :
|
473 |
|
|
begin
|
474 |
|
|
measure <= 1'b1;
|
475 |
|
|
// Check at mid bit time, to make sure serial line is still low...
|
476 |
|
|
// (At this time, "mid_bit_count" is simply CLOCK_FACTOR_PP>>1 clk_i's.)
|
477 |
|
|
if (mid_bit_count && ~serial_dat_i) m1_next_state <= m1_measure_1;
|
478 |
|
|
// If it is not low, then it must have been a "glitch"...
|
479 |
|
|
else if (mid_bit_count && serial_dat_i) m1_next_state <= m1_idle;
|
480 |
|
|
else m1_next_state <= m1_measure_0;
|
481 |
|
|
end
|
482 |
|
|
|
483 |
|
|
m1_measure_1 :
|
484 |
|
|
begin
|
485 |
|
|
measure <= 1'b1;
|
486 |
|
|
// Look for first data bit (high).
|
487 |
|
|
if (serial_dat_i) m1_next_state <= m1_measure_2;
|
488 |
|
|
// If it is not high keep waiting...
|
489 |
|
|
// (Put detection of measurement overflow in here if necessary...)
|
490 |
|
|
else m1_next_state <= m1_measure_1;
|
491 |
|
|
end
|
492 |
|
|
|
493 |
|
|
m1_measure_2 :
|
494 |
|
|
begin
|
495 |
|
|
measure <= 1'b1;
|
496 |
|
|
// Check using mid bit time, to make sure serial line is still high...
|
497 |
|
|
// (At this time, "mid_bit_count" is simply CLOCK_FACTOR_PP>>1 clk_i's.)
|
498 |
|
|
if (mid_bit_count && serial_dat_i) m1_next_state <= m1_measure_3;
|
499 |
|
|
// If it is not high, then it must have been a "glitch"...
|
500 |
|
|
else if (mid_bit_count && ~serial_dat_i) m1_next_state <= m1_idle;
|
501 |
|
|
else m1_next_state <= m1_measure_2;
|
502 |
|
|
end
|
503 |
|
|
|
504 |
|
|
m1_measure_3 :
|
505 |
|
|
begin
|
506 |
|
|
measure <= 1'b1;
|
507 |
|
|
// Look for end of measurement interval (low)
|
508 |
|
|
if (!serial_dat_i) m1_next_state <= m1_measure_4;
|
509 |
|
|
// If it is not high keep waiting...
|
510 |
|
|
// (Put detection of measurement overflow in here if necessary...)
|
511 |
|
|
else m1_next_state <= m1_measure_3;
|
512 |
|
|
end
|
513 |
|
|
|
514 |
|
|
// This state outputs a reset pulse, to clear counters and store the
|
515 |
|
|
// measurement from main_count.
|
516 |
|
|
m1_measure_4 :
|
517 |
|
|
begin
|
518 |
|
|
clear_counters <= 1'b1; // Clears counters, stores measurement
|
519 |
|
|
m1_next_state <= m1_verify_0;
|
520 |
|
|
end
|
521 |
|
|
|
522 |
|
|
m1_verify_0 : // Wait for verify operations to finish
|
523 |
|
|
begin
|
524 |
|
|
verify <= 1'b1;
|
525 |
|
|
if (verify_done) m1_next_state <= m1_verify_1;
|
526 |
|
|
else m1_next_state <= m1_verify_0;
|
527 |
|
|
end
|
528 |
|
|
|
529 |
|
|
// NOTE: This "extra" state is needed because the character_miscompare
|
530 |
|
|
// information is not valid until 1 cycle after verify_done is
|
531 |
|
|
// active.
|
532 |
|
|
m1_verify_1 : // Checks for character miscompare
|
533 |
|
|
begin
|
534 |
|
|
if (character_miscompare) m1_next_state <= m1_verify_failed;
|
535 |
|
|
else m1_next_state <= m1_run;
|
536 |
|
|
end
|
537 |
|
|
|
538 |
|
|
m1_verify_failed : // Resets counters on the way back to idle
|
539 |
|
|
begin
|
540 |
|
|
clear_counters <= 1'b1;
|
541 |
|
|
m1_next_state <= m1_idle;
|
542 |
|
|
end
|
543 |
|
|
|
544 |
|
|
// This state is for successful verification results!
|
545 |
|
|
// Since this is a single measurement unit, only reset can exit this
|
546 |
|
|
// state.
|
547 |
|
|
m1_run :
|
548 |
|
|
begin
|
549 |
|
|
run <= 1'b1;
|
550 |
|
|
m1_next_state <= m1_run;
|
551 |
|
|
end
|
552 |
|
|
|
553 |
|
|
default : m1_next_state <= m1_idle;
|
554 |
|
|
endcase
|
555 |
|
|
end
|
556 |
|
|
|
557 |
|
|
|
558 |
|
|
assign auto_baud_locked_o = run;
|
559 |
|
|
|
560 |
|
|
|
561 |
|
|
endmodule
|
562 |
|
|
|
563 |
|
|
//`undef LOG2_MAX_CLOCK_FACTOR
|