f35c9edb59d2a471af465c2b00f24107d9cdf2fe
[fw/openocd] / 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 @speed_table = ();
25
26 printf("/* This file was created automatically by %s. */\n\n", $0);
27 for $i ('rlink', 'st7') {
28         printf("#include \"$i.h\"\n");
29 }
30 printf("\n");
31
32 for $prescaler (sort {$b <=> $a} @ARGV) {
33         my(@ary) = (
34                 byte_array_from_file(${prescaler} . "_init.dtc"),
35                 byte_array_from_file(${prescaler} . "_call.dtc")
36         );
37
38         for $i (@ary) {
39                 $i = sprintf("%d", $i);
40         }
41         $bytes = join(', ', @ary);
42         $bytes =~ s/(^|\s)(.{70}?\S*)/\2\n/go;  # break up long lines
43         $bytes =~ s/\n +/\n/go;
44         $bytes =~ s/(^|\n)/\1\t/go;             # format nicely
45         printf("static const u8 dtc_%d[] = {\n%s\n};\n\n", $prescaler, $bytes);
46         push(@speed_table, sprintf("\tdtc_%d, sizeof(dtc_%d), (ST7_FOSC * 2) / (1000 * %d), %d\n", $prescaler, $prescaler, $prescaler, $prescaler));
47 }
48
49 printf("const rlink_speed_table_t rlink_speed_table[] = {{\n%s}};\n\n", join("}, {\n", @speed_table));
50 printf("const size_t rlink_speed_table_size = sizeof(rlink_speed_table) / sizeof(*rlink_speed_table);\n\n");
51
52
53 sub byte_array_from_file {
54         my($filename) = @_;
55
56         my(@array, $text, $i) = ();
57
58         open(IN, '<', $filename) || die "$filename: $!";
59         undef($/);
60         $text = <IN>;
61         close(IN);
62
63         for($i = 0; $i < length($text); $i++) {
64                 push(@array, ord(substr($text, $i, 1)));
65         }
66
67         @array;
68 }