struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / scripts / pic18fam-h-gen.pl
blob0cd640d8405d3694fad1e96108b838c605386b55
1 #!/usr/bin/perl -w
4 # This script generates a C header file that maps the target device (as
5 # indicated via the sdcc generated -Dpic18fxxx macro) to its device
6 # family and the device families to their respective style of ADC and
7 # USART programming for use in the SDCC PIC16 I/O library.
9 # Copyright 2010 Raphael Neider <rneider AT web.de>
11 # This file is part of SDCC.
13 # SDCC is free software: you can redistribute it and/or modify it under
14 # the terms of the GNU General Public License as published by the Free
15 # Software Foundation, either version 2 of the License, or (at your
16 # option) any later version.
18 # SDCC is distributed in the hope that it will be useful, but WITHOUT
19 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
20 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 # for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with SDCC. If not, see <http://www.gnu.org/licenses/>.
28 # Usage: perl pic18fam-h-gen.pl
30 # This will create pic18fam.h.gen in your current directory.
31 # Check sanity of the file and move it to .../include/pic16/pic18fam.h.
32 # If you assigned new I/O styles, implement them in
33 # .../include/pic16/{adc,i2c,usart}.h and
34 # .../lib/pic16/libio/*/*.c
37 use strict;
39 my $head = '__SDCC_PIC';
41 my %families = ();
42 my %adc = ();
43 my %usart = ();
44 my $update = "Please update your pic16/pic18fam.h manually and/or inform the maintainer.";
46 while (<DATA>) {
47 chomp;
48 s/\s*#.*$//; # remove comments
49 s/\s*//g; # strip whitespace
50 next if (/^\s*$/); # ignore empty lines
52 my $line = $_;
54 my @fields = split(/:/, $line);
56 die "Invalid record >$line<" if (4 != scalar @fields);
58 my ($id, $memberlist, $adcstyle, $usartstyle) = @fields;
60 # extract numeric family id
61 $id = 0+$id;
63 # extract family members
64 my @arr = split(/,/, $memberlist);
65 @arr = sort(map { uc($_); } @arr);
66 $families{$id} = \@arr;
68 # ADC style per device family
69 $adcstyle = 0+$adcstyle;
70 if (not defined $adc{$adcstyle}) {
71 $adc{$adcstyle} = [];
72 } # if
73 push @{$adc{$adcstyle}}, $id;
75 # (E)USART style per device family
76 $usartstyle = 0+$usartstyle;
77 if (not defined $usart{$usartstyle}) {
78 $usart{$usartstyle} = [];
79 } # if
80 push @{$usart{$usartstyle}}, $id;
83 my $fname = "pic18fam.h.gen";
84 open(FH, ">", "$fname") or die "Could not open >$fname<";
86 print FH <<EOT
88 * pic18fam.h - PIC16 families
90 * This file is has been generated using $0 .
92 #ifndef __SDCC_PIC18FAM_H__
93 #define __SDCC_PIC18FAM_H__ 1
96 * Define device families.
98 #undef __SDCC_PIC16_FAMILY
102 my $pp = "#if ";
103 for my $id (sort keys %families) {
104 my $list = $families{$id};
105 my $memb = "defined($head" . join(") \\\n || defined($head", @$list) . ")";
106 print FH <<EOT
107 ${pp} ${memb}
108 #define __SDCC_PIC16_FAMILY ${id}
112 $pp = "#elif ";
113 } # for
114 print FH <<EOT
115 #else
116 #warning No family associated with the target device. ${update}
117 #endif
120 * Define ADC style per device family.
122 #undef __SDCC_ADC_STYLE
126 $pp = "#if ";
127 for my $s (sort keys %adc) {
128 my $fams = join (" \\\n || ", map { "(__SDCC_PIC16_FAMILY == $_)" } sort @{$adc{$s}});
129 print FH <<EOT
130 ${pp} ${fams}
131 #define __SDCC_ADC_STYLE ${s}
135 $pp = "#elif ";
136 } # for
137 print FH <<EOT
138 #else
139 #warning No ADC style associated with the target device. ${update}
140 #endif
143 * Define (E)USART style per device family.
145 #undef __SDCC_USART_STYLE
149 $pp = "#if ";
150 for my $s (sort keys %usart) {
151 my $fams = join (" \\\n || ", map { "(__SDCC_PIC16_FAMILY == $_)" } sort @{$usart{$s}});
152 print FH <<EOT
153 ${pp} ${fams}
154 #define __SDCC_USART_STYLE ${s}
158 $pp = "#elif ";
159 } # for
160 print FH <<EOT
161 #else
162 #warning No (E)USART style associated with the target device. ${update}
163 #endif
168 print FH <<EOT
169 #endif /* !__SDCC_PIC18FAM_H__ */
172 __END__
174 # <id>:<head>{,<member>}:<adc>:<usart>
176 # Each line provides a colon separated list of
177 # * a numeric family name, derived from the first family member as follows:
178 # - 18F<num> -> printf("18%04d0", <num>)
179 # - 18F<num1>J<num2> -> printf("18%02d%02d1", <num1>, <num2>)
180 # - 18F<num1>K<num2> -> printf("18%02d%02d2", <num1>, <num2>)
181 # * a comma-separated list of members of a device family,
182 # where a family comprises all devices that share a single data sheet,
183 # * the ADC style (numeric family name or 0, if not applicable)
184 # * the USART style (numeric family name or 0, if not applicable)
186 # This data has been gathered manually from data sheets published by
187 # Microchip Technology Inc.
189 1812200:18f1220,18f1320:1812200:1812200
190 1812300:18f1230,18f1330:1812300:1812300
191 1813502:18f13k50,18f14k50:1813502:1813502
192 1822200:18f2220,18f2320,18f4220,18f4320:1822200:1822200
193 1822210:18f2221,18f2321,18f4221,18f4321:1822200:1822210
194 1823310:18f2331,18f2431,18f4331,18f4431:0:1822210
195 1823202:18f23k20,18f24k20,18f25k20,18f26k20,18f43k20,18f44k20,18f45k20,18f46k20:1822200:1822210
196 1823222:18f23k22,18f24k22,18f25k22,18f26k22,18f43k22,18f44k22,18f45k22,18f46k22:1823222:1822210
197 1824100:18f2410,18f2510,18f2515,18f2610,18f4410,18f4510,18f4515,18f4610:1822200:1822210
198 1802420:18f242,18f252,18f442,18f452:1802420:1822200 # TODO: verify family members and USART
199 1824200:18f2420,18f2520,18f4420,18f4520:1822200:1822210
200 1824230:18f2423,18f2523,18f4423,18f4523:1822200:1822210
201 1824500:18f2450,18f4450:1822200:1824500
202 1824550:18f2455,18f2550,18f4455,18f4550:1822200:1822210
203 1802480:18f248,18f258,18f448,18f458:1802420:1822200 # TODO: verify family members and USART
204 1824800:18f2480,18f2580,18f4480,18f4580:1822200:1824500
205 1824101:18f24j10,18f25j10,18f44j10,18f45j10:1822200:1822210
206 1824501:18f24j50,18f25j50,18f26j50,18f44j50,18f45j50,18f46j50:1824501:1824501
207 1825250:18f2525,18f2620,18f4525,18f4620:1822200:1822210
208 1825850:18f2585,18f2680,18f4585,18f4680:1822200:1824500
209 1826820:18f2682,18f2685,18f4682,18f4685:1822200:1824500
210 1865200:18f6520,18f6620,18f6720,18f8520,18f8620,18f8720:1822200:1865200
211 1865270:18f6527,18f6622,18f6627,18f6722,18f8527,18f8622,18f8627,18f8722:1822200:1824501
212 1865850:18f6585,18f6680,18f8585,18f8680:1822200:1865850
213 1865501:18f65j50,18f66j50,18f66j55,18f67j50,18f85j50,18f86j50,18f86j55,18f87j50:1865501:1824501
214 1866601:18f66j60,18f66j65,18f67j60,18f86j60,18f86j65,18f87j60,18f96j60,18f96j65,18f97j60:1822200:1824501