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

Subversion Repositories wb4pb

[/] [wb4pb/] [trunk/] [tools/] [baud_rate_calculator.tcl] - Blame information for rev 31

Details | Compare with Previous | View Log

Line No. Rev Author Line
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 31 ste.fis
##    and/or other materials provided with the distribution.
17 13 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: baud_rate_calculator.tcl
32
## description: tcl-script generating assembler constants for standard baud
33
##              rates
34
## todo4user: 1. modify output file path, SYSTEM_CLOCK and BAUD_RATES as needed
35
##            2. run script with TCL interpreter of your choice, i. e. from
36
##               ModelSim (R) GUI: Menu->Tools->TCL->Execute Macro...
37
##            3. copy and paste file content from generated
38
##               baud_rate_constants.txt to assembler source code
39
## version: 0.0.0
40
## changelog: - 0.0.0, initial release
41
##            - ...
42
################################################################################
43
 
44
# output file path
45
cd "e:/home_users/ste.fis/projects/wb4pb/trunk/asm"
46
 
47
# reference clock, normally several MHz
48
set SYSTEM_CLOCK 50.0E6
49
 
50
# some standard baud rates
51
set BAUD_RATES {
52
  300
53
  600
54
  1200
55
  2400
56
  4800
57
  9600
58
  19200
59
  38400
60
  57600
61
  115200
62
  230400
63
  460800
64
  921600
65
}
66
 
67
# open file handle
68
set f [open "baud_rate_constants.txt" w]
69
 
70
# baud rate configuration:
71
# baud_limit = round( system clock frequency / (16 * baud rate) ) - 1
72
# i. e. 9600 baud at 50 MHz system clock =>
73
# baud_limit = round( 50.0E6 / (16 * 9600) ) - 1 = 325 = 0x0145
74
 
75
# WARNING, baud rate error should not exceed 1.0 % for reliable operation!
76
 
77
# calculating baud min. and max. values
78
set baud_max [expr floor($SYSTEM_CLOCK / 16)]
79
set baud_min [expr ceil($SYSTEM_CLOCK / (16 * 65536))]
80
 
81
puts $f "; baud rate settings for $SYSTEM_CLOCK Hz system reference clock"
82
puts $f "; max. $baud_max baud"
83
puts $f "; min. $baud_min baud (16 bit baud timer)"
84
foreach baud_rate $BAUD_RATES {
85
  # checking, if current baud rate works with system hardware parameters
86
  if {[expr $baud_min <= $baud_rate] && [expr $baud_rate <= $baud_max]} {
87
    # calculating hardware baud timer limit
88
    set baud_limit [expr round($SYSTEM_CLOCK / (16 * $baud_rate)) - 1]
89
    # calculating actual hardware baud rate
90
    set actual_baud_rate [expr $SYSTEM_CLOCK / (16 * ($baud_limit + 1))]
91
    # calculating baud rate error, value is commented out automatically, if 
92
    # frequency error is greater than the recommended 1 %
93
    if {
94
      [set baud_error [expr abs(($baud_rate / $actual_baud_rate) - 1)]] > 0.01
95
    } {
96
      set comment ";"
97
    } else {
98
      set comment ""
99
    }
100
    # generating a 16 bit, 4 character hexadecimal string of hardware baud timer
101
    # limit
102
    set baud_limit_hex [format %04X $baud_limit]
103
    # creating strings with constant definitions in kcpsm3 assembler syntax,
104
    # splitting into high and low byte
105
    puts $f "${comment}CONSTANT UART_BAUD_LO_${baud_rate}_VALUE , [string range\
106 16 ste.fis
             $baud_limit_hex 2 3] ; actual baud rate [format %.2f\
107 13 ste.fis
             $actual_baud_rate]"
108
    puts $f "${comment}CONSTANT UART_BAUD_HI_${baud_rate}_VALUE , [string range\
109
             $baud_limit_hex 0 1] ; => baud rate error [format %.3f [expr 100 *\
110
             $baud_error]] %"
111
  }
112
}
113
 
114
# closing file
115
close $f

powered by: WebSVN 2.1.0

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