1#!/usr/bin/perl -w
2
3#
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.
8#
9# Copyright 2010 Raphael Neider <rneider AT web.de>
10#
11# This file is part of SDCC.
12#
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.
17#
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.
22#
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/>.
25#
26
27#
28# Usage: perl pic18fam-h-gen.pl
29#
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
35#
36
37use strict;
38
39my $head = '__SDCC_PIC';
40
41my %families = ();
42my %adc = ();
43my %usart = ();
44my $update = "Please update your pic16/pic18fam.h manually and/or inform the maintainer.";
45
46while (<DATA>) {
47    chomp;
48    s/\s*#.*$//;                # remove comments
49    s/\s*//g;                   # strip whitespace
50    next if (/^\s*$/);         # ignore empty lines
51
52    my $line = $_;
53
54    my @fields = split(/:/, $line);
55
56    die "Invalid record >$line<" if (4 != scalar @fields);
57
58    my ($id, $memberlist, $adcstyle, $usartstyle) = @fields;
59
60    # extract numeric family id
61    $id = 0+$id;
62
63    # extract family members
64    my @arr = split(/,/, $memberlist);
65    @arr = sort(map { uc($_); } @arr);
66    $families{$id} = \@arr;
67
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;
74
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;
81}
82
83my $fname = "pic18fam.h.gen";
84open(FH, ">", "$fname") or die "Could not open >$fname<";
85
86print FH <<EOT
87/*
88 * pic18fam.h - PIC16 families
89 *
90 * This file is has been generated using $0 .
91 */
92#ifndef __SDCC_PIC18FAM_H__
93#define __SDCC_PIC18FAM_H__ 1
94
95/*
96 * Define device families.
97 */
98#undef  __SDCC_PIC16_FAMILY
99
100EOT
101;
102my $pp = "#if   ";
103for 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}
109
110EOT
111;
112    $pp = "#elif ";
113} # for
114print FH <<EOT
115#else
116#warning No family associated with the target device. ${update}
117#endif
118
119/*
120 * Define ADC style per device family.
121 */
122#undef  __SDCC_ADC_STYLE
123
124EOT
125;
126$pp = "#if   ";
127for 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}
132
133EOT
134;
135    $pp = "#elif ";
136} # for
137print FH <<EOT
138#else
139#warning No ADC style associated with the target device. ${update}
140#endif
141
142/*
143 * Define (E)USART style per device family.
144 */
145#undef  __SDCC_USART_STYLE
146
147EOT
148;
149$pp = "#if   ";
150for 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}
155
156EOT
157;
158    $pp = "#elif ";
159} # for
160print FH <<EOT
161#else
162#warning No (E)USART style associated with the target device. ${update}
163#endif
164
165EOT
166;
167
168print FH <<EOT
169#endif /* !__SDCC_PIC18FAM_H__ */
170EOT
171;
172__END__
173#
174# <id>:<head>{,<member>}:<adc>:<usart>
175#
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)
185#
186# This data has been gathered manually from data sheets published by
187# Microchip Technology Inc.
188#
1891812200:18f1220,18f1320:1812200:1812200
1901812300:18f1230,18f1330:1812300:1812300
1911813502:18f13k50,18f14k50:1813502:1813502
1921822200:18f2220,18f2320,18f4220,18f4320:1822200:1822200
1931822210:18f2221,18f2321,18f4221,18f4321:1822200:1822210
1941823310:18f2331,18f2431,18f4331,18f4431:0:1822210
1951823202:18f23k20,18f24k20,18f25k20,18f26k20,18f43k20,18f44k20,18f45k20,18f46k20:1822200:1822210
1961823222:18f23k22,18f24k22,18f25k22,18f26k22,18f43k22,18f44k22,18f45k22,18f46k22:1823222:1822210
1971824100:18f2410,18f2510,18f2515,18f2610,18f4410,18f4510,18f4515,18f4610:1822200:1822210
1981802420:18f242,18f252,18f442,18f452:1802420:1822200                             # TODO: verify family members and USART
1991824200:18f2420,18f2520,18f4420,18f4520:1822200:1822210
2001824230:18f2423,18f2523,18f4423,18f4523:1822200:1822210
2011824500:18f2450,18f4450:1822200:1824500
2021824550:18f2455,18f2550,18f4455,18f4550:1822200:1822210
2031802480:18f248,18f258,18f448,18f458:1802420:1822200                             # TODO: verify family members and USART
2041824800:18f2480,18f2580,18f4480,18f4580:1822200:1824500
2051824101:18f24j10,18f25j10,18f44j10,18f45j10:1822200:1822210
2061824501:18f24j50,18f25j50,18f26j50,18f44j50,18f45j50,18f46j50:1824501:1824501
2071825250:18f2525,18f2620,18f4525,18f4620:1822200:1822210
2081825850:18f2585,18f2680,18f4585,18f4680:1822200:1824500
2091826820:18f2682,18f2685,18f4682,18f4685:1822200:1824500
2101865200:18f6520,18f6620,18f6720,18f8520,18f8620,18f8720:1822200:1865200
2111865270:18f6527,18f6622,18f6627,18f6722,18f8527,18f8622,18f8627,18f8722:1822200:1824501
2121865850:18f6585,18f6680,18f8585,18f8680:1822200:1865850
2131865501:18f65j50,18f66j50,18f66j55,18f67j50,18f85j50,18f86j50,18f86j55,18f87j50:1865501:1824501
2141866601:18f66j60,18f66j65,18f67j60,18f86j60,18f86j65,18f87j60,18f96j60,18f96j65,18f97j60:1822200:1824501
215