rlink: fix speed table generation
[openocd.git] / tools / rlink_make_speed_table / rlink_make_speed_table.pl
1 #!/bin/perl
2 #***************************************************************************
3 #* Copyright (C) 2008 Lou Deluxe *
4 #* lou.openocd012@fixit.nospammail.net *
5 #* *
6 #* This program is free software; you can redistribute it and/or modify *
7 #* it under the terms of the GNU General Public License as published by *
8 #* the Free Software Foundation; either version 2 of the License, or *
9 #* (at your option) any later version. *
10 #* *
11 #* This program is distributed in the hope that it will be useful, *
12 #* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 #* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 #* GNU General Public License for more details. *
15 #* *
16 #* You should have received a copy of the GNU General Public License *
17 #* along with this program; if not, write to the *
18 #* Free Software Foundation, Inc., *
19 #* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20 #***************************************************************************
21
22 # A simple utility to read a list of files (names composed by numeric prescaler arguments) and compose a C source file defining data structures which hold the binary data read from those files.
23
24 my @speed_table = ();
25
26 print <<HEADER;
27 /* This file was created automatically by the following script:
28 * $0
29 */
30 #ifdef HAVE_CONFIG_H
31 #include "config.h"
32 #endif
33
34 #include "rlink.h"
35 #include "rlink_st7.h"
36
37 HEADER
38
39 for $prescaler (sort {$b <=> $a} @ARGV) {
40 my(@ary) = (
41 byte_array_from_file(${prescaler} . "_init.dtc"),
42 byte_array_from_file(${prescaler} . "_call.dtc")
43 );
44
45 for $i (@ary) {
46 $i = sprintf("%d", $i);
47 }
48 $bytes = join(', ', @ary);
49 $bytes =~ s/(^|\s)(.{70}?\S*)/\2\n/go; # break up long lines
50 $bytes =~ s/\n +/\n/go;
51 $bytes =~ s/(^|\n)/\1\t/go; # format nicely
52 printf("static const uint8_t dtc_%d[] = {\n%s\n};\n\n", $prescaler, $bytes);
53 push(@speed_table, sprintf("\tdtc_%d, sizeof(dtc_%d), (ST7_FOSC * 2) / (1000 * %d), %d\n", $prescaler, $prescaler, $prescaler, $prescaler));
54 }
55
56 printf("const struct rlink_speed_table rlink_speed_table[] = { {\n%s} };\n\n", join("}, {\n", @speed_table));
57 printf("const size_t rlink_speed_table_size = ARRAY_SIZE(rlink_speed_table);\n\n");
58
59
60 sub byte_array_from_file {
61 my($filename) = @_;
62
63 my(@array, $text, $i) = ();
64
65 open(IN, '<', $filename) || die "$filename: $!";
66 undef($/);
67 $text = <IN>;
68 close(IN);
69
70 for($i = 0; $i < length($text); $i++) {
71 push(@array, ord(substr($text, $i, 1)));
72 }
73
74 @array;
75 }