1 |
13 |
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 |
|
|
## and/or other materials provided with the distribution.
|
17 |
|
|
## * Neither the name of the author nor the names of his contributors may be
|
18 |
|
|
## used to endorse or promote products derived from this software without
|
19 |
|
|
## specific prior written permission.
|
20 |
|
|
##
|
21 |
|
|
## THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
22 |
|
|
## AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
23 |
|
|
## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
24 |
|
|
## ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
25 |
|
|
## LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
26 |
|
|
## CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
27 |
|
|
## SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
28 |
|
|
## INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
29 |
|
|
## CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
30 |
|
|
## ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
31 |
|
|
## POSSIBILITY OF SUCH DAMAGE.
|
32 |
|
|
##
|
33 |
|
|
################################################################################
|
34 |
|
|
## filename: baud_rate_calculator.tcl
|
35 |
|
|
## description: tcl-script generating assembler constants for standard baud
|
36 |
|
|
## rates
|
37 |
|
|
## todo4user: 1. modify output file path, SYSTEM_CLOCK and BAUD_RATES as needed
|
38 |
|
|
## 2. run script with TCL interpreter of your choice, i. e. from
|
39 |
|
|
## ModelSim (R) GUI: Menu->Tools->TCL->Execute Macro...
|
40 |
|
|
## 3. copy and paste file content from generated
|
41 |
|
|
## baud_rate_constants.txt to assembler source code
|
42 |
|
|
## version: 0.0.0
|
43 |
|
|
## changelog: - 0.0.0, initial release
|
44 |
|
|
## - ...
|
45 |
|
|
################################################################################
|
46 |
|
|
|
47 |
|
|
# output file path
|
48 |
|
|
cd "e:/home_users/ste.fis/projects/wb4pb/trunk/asm"
|
49 |
|
|
|
50 |
|
|
# reference clock, normally several MHz
|
51 |
|
|
set SYSTEM_CLOCK 50.0E6
|
52 |
|
|
|
53 |
|
|
# some standard baud rates
|
54 |
|
|
set BAUD_RATES {
|
55 |
|
|
300
|
56 |
|
|
600
|
57 |
|
|
1200
|
58 |
|
|
2400
|
59 |
|
|
4800
|
60 |
|
|
9600
|
61 |
|
|
19200
|
62 |
|
|
38400
|
63 |
|
|
57600
|
64 |
|
|
115200
|
65 |
|
|
230400
|
66 |
|
|
460800
|
67 |
|
|
921600
|
68 |
|
|
}
|
69 |
|
|
|
70 |
|
|
# open file handle
|
71 |
|
|
set f [open "baud_rate_constants.txt" w]
|
72 |
|
|
|
73 |
|
|
# baud rate configuration:
|
74 |
|
|
# baud_limit = round( system clock frequency / (16 * baud rate) ) - 1
|
75 |
|
|
# i. e. 9600 baud at 50 MHz system clock =>
|
76 |
|
|
# baud_limit = round( 50.0E6 / (16 * 9600) ) - 1 = 325 = 0x0145
|
77 |
|
|
|
78 |
|
|
# WARNING, baud rate error should not exceed 1.0 % for reliable operation!
|
79 |
|
|
|
80 |
|
|
# calculating baud min. and max. values
|
81 |
|
|
set baud_max [expr floor($SYSTEM_CLOCK / 16)]
|
82 |
|
|
set baud_min [expr ceil($SYSTEM_CLOCK / (16 * 65536))]
|
83 |
|
|
|
84 |
|
|
puts $f "; baud rate settings for $SYSTEM_CLOCK Hz system reference clock"
|
85 |
|
|
puts $f "; max. $baud_max baud"
|
86 |
|
|
puts $f "; min. $baud_min baud (16 bit baud timer)"
|
87 |
|
|
foreach baud_rate $BAUD_RATES {
|
88 |
|
|
# checking, if current baud rate works with system hardware parameters
|
89 |
|
|
if {[expr $baud_min <= $baud_rate] && [expr $baud_rate <= $baud_max]} {
|
90 |
|
|
# calculating hardware baud timer limit
|
91 |
|
|
set baud_limit [expr round($SYSTEM_CLOCK / (16 * $baud_rate)) - 1]
|
92 |
|
|
# calculating actual hardware baud rate
|
93 |
|
|
set actual_baud_rate [expr $SYSTEM_CLOCK / (16 * ($baud_limit + 1))]
|
94 |
|
|
# calculating baud rate error, value is commented out automatically, if
|
95 |
|
|
# frequency error is greater than the recommended 1 %
|
96 |
|
|
if {
|
97 |
|
|
[set baud_error [expr abs(($baud_rate / $actual_baud_rate) - 1)]] > 0.01
|
98 |
|
|
} {
|
99 |
|
|
set comment ";"
|
100 |
|
|
} else {
|
101 |
|
|
set comment ""
|
102 |
|
|
}
|
103 |
|
|
# generating a 16 bit, 4 character hexadecimal string of hardware baud timer
|
104 |
|
|
# limit
|
105 |
|
|
set baud_limit_hex [format %04X $baud_limit]
|
106 |
|
|
# creating strings with constant definitions in kcpsm3 assembler syntax,
|
107 |
|
|
# splitting into high and low byte
|
108 |
|
|
puts $f "${comment}CONSTANT UART_BAUD_LO_${baud_rate}_VALUE , [string range\
|
109 |
16 |
ste.fis |
$baud_limit_hex 2 3] ; actual baud rate [format %.2f\
|
110 |
13 |
ste.fis |
$actual_baud_rate]"
|
111 |
|
|
puts $f "${comment}CONSTANT UART_BAUD_HI_${baud_rate}_VALUE , [string range\
|
112 |
|
|
$baud_limit_hex 0 1] ; => baud rate error [format %.3f [expr 100 *\
|
113 |
|
|
$baud_error]] %"
|
114 |
|
|
}
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
# closing file
|
118 |
|
|
close $f
|