updates
[inav.git] / src / utils / def_generated.pl
blob0bd9ef6f7ac8eaf18713ae0f3043d0b2482bee3f
1 #!/usr/bin/perl
2 use warnings;
3 use strict;
5 # This sript will generate templated files for peripherals
7 # io_def_generated.h
9 my @ports = ('A', 'B', 'C', 'D', 'E', 'F', 'G');
10 my @pins = 0 .. 15;
11 my @timers = (1,2,3,4,6,7,8,15,16,17);
12 my $drivers_dir = "../main/drivers";
14 # change list separator to newline - we use @{} interpolation to merge multiline strings
15 $" = "\n";
17 chomp(my $license = <<"END");
19 * This file is part of Cleanflight.
21 * Cleanflight is free software: you can redistribute it and/or modify
22 * it under the terms of the GNU General Public License as published by
23 * the Free Software Foundation, either version 3 of the License, or
24 * (at your option) any later version.
26 * Cleanflight is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
34 END
36 chomp(my $disclaimer_generated = <<"END");
37 // this file is automatically generated by src/utils/def_generated.pl script
38 // do not modify this file directly, your changes will be lost
39 END
41 my $io_def_file="$drivers_dir/io_def_generated.h";
42 my $fh;
43 open $fh, '>', $io_def_file or die "Cannot open $io_def_file: $!";
44 print { $fh} <<"END" or die "Cannot write into $io_def_file: $!"; close $fh;
45 ${license}
47 #pragma once
49 ${disclaimer_generated}
51 // DEFIO_PORT_<port>_USED_MASK is bitmask of used pins on target
52 // DEFIO_PORT_<port>_USED_COUNT is count of used pins on target
54 @{[do {
55 my @prev_ports = ();
56 map { my $port = $_; my $ret = << "END2"; push @prev_ports, $port; $ret } @ports; }]}
57 #if defined(TARGET_IO_PORT${port})
58 # define DEFIO_PORT_${port}_USED_MASK TARGET_IO_PORT${port}
59 # define DEFIO_PORT_${port}_USED_COUNT BITCOUNT(DEFIO_PORT_${port}_USED_MASK)
60 #else
61 # define DEFIO_PORT_${port}_USED_MASK 0
62 # define DEFIO_PORT_${port}_USED_COUNT 0
63 #endif
64 #define DEFIO_PORT_${port}_OFFSET (@{[join('+', map { "DEFIO_PORT_${_}_USED_COUNT" } @prev_ports) || '0']})
65 END2
68 // DEFIO_GPIOID__<port> maps to port index
69 @{[ map { my $port = $_; chomp(my $ret = << "END2"); $ret } @ports ]}
70 #define DEFIO_GPIOID__${port} @{[ord($port)-ord('A')]}
71 END2
73 // DEFIO_TAG__P<port><pin> will expand to TAG if defined for target, error is triggered otherwise
74 // DEFIO_TAG_E__P<port><pin> will expand to TAG if defined, to NONE otherwise (usefull for tables that are CPU-specific)
75 // DEFIO_REC__P<port><pin> will expand to ioRec* (using DEFIO_REC_INDEX(idx))
77 @{[do {
78 my @prev_ports = ();
79 map { my $port = $_; my @ret = map { my $pin = $_; chomp(my $ret = << "END2"); $ret } @pins ; push @prev_ports, $port; @ret } @ports; }]}
80 #if DEFIO_PORT_${port}_USED_MASK & BIT(${pin})
81 # define DEFIO_TAG__P${port}${pin} DEFIO_TAG_MAKE(DEFIO_GPIOID__${port}, ${pin})
82 # define DEFIO_TAG_E__P${port}${pin} DEFIO_TAG_MAKE(DEFIO_GPIOID__${port}, ${pin})
83 # define DEFIO_REC__P${port}${pin} DEFIO_REC_INDEXED(BITCOUNT(DEFIO_PORT_${port}_USED_MASK & (BIT(${pin}) - 1)) + @{[join('+', map { "DEFIO_PORT_${_}_USED_COUNT" } @prev_ports) || '0']})
84 #else
85 # define DEFIO_TAG__P${port}${pin} defio_error_P${port}${pin}_is_not_supported_on_TARGET
86 # define DEFIO_TAG_E__P${port}${pin} DEFIO_TAG_E__NONE
87 # define DEFIO_REC__P${port}${pin} defio_error_P${port}${pin}_is_not_supported_on_TARGET
88 #endif
89 END2
91 // DEFIO_IO_USED_COUNT is number of io pins supported on target
92 #define DEFIO_IO_USED_COUNT (@{[join('+', map { "DEFIO_PORT_${_}_USED_COUNT" } @ports) || '0']})
94 // DEFIO_PORT_USED_LIST - comma separated list of bitmask for all used ports.
95 // DEFIO_PORT_OFFSET_LIST - comma separated list of port offsets (count of pins before this port)
96 // unused ports on end of list are skipped
97 @{[do {
98 my @used_ports = @ports;
99 map { my $port = $_; chomp(my $ret = << "END2"); @used_ports = grep {$_ ne $port} @used_ports; $ret } reverse(@ports) }]}
100 #if !defined DEFIO_PORT_USED_LIST && DEFIO_PORT_${port}_USED_COUNT > 0
101 # define DEFIO_PORT_USED_COUNT @{[scalar @used_ports]}
102 # define DEFIO_PORT_USED_LIST @{[join(',', map { "DEFIO_PORT_${_}_USED_MASK" } @used_ports)]}
103 # define DEFIO_PORT_OFFSET_LIST @{[join(',', map { "DEFIO_PORT_${_}_OFFSET" } @used_ports)]}
104 #endif
105 END2
107 #if !defined(DEFIO_PORT_USED_LIST)
108 # if !defined DEFIO_NO_PORTS // supress warnings if we really don't want any pins
109 # warning "No pins are defined. Maybe you forgot to define TARGET_IO_PORTx in target.h"
110 # endif
111 # define DEFIO_PORT_USED_COUNT 0
112 # define DEFIO_PORT_USED_LIST /* empty */
113 # define DEFIO_PORT_OFFSET_LIST /* empty */
114 #endif
118 exit; # only IO code is merged now
120 my $timer_def_file="$drivers_dir/timer_def_generated.h";
121 open $fh, '>', $timer_def_file or die "Cannot open $timer_def_file: $!";
122 print { $fh} <<"END" or die "Cannot write into $timer_def_file: $!"; close $fh;
123 #pragma once
124 ${disclaimer_generated}
126 // make sure macros for all timers are defined
127 @{[ map { my $timer = $_; chomp(my $ret = << "END2"); $ret } @timers ]}
128 #ifndef TARGET_TIMER_TIM${timer}
129 # define TARGET_TIMER_TIM${timer} -1
130 #endif
131 END2
133 // generate mask with used timers
134 @{[do {
135 my @prev_timers = ();
136 map { my $timer = $_; chomp(my $ret = << "END2"); push @prev_timers, $timer; $ret } @timers }]}
137 #if TARGET_TIMER_TIM${timer} > 0
138 # define TARGET_TIMER_TIM${timer}_BIT BIT(${timer})
139 # define TARGET_TIMER_TIM${timer}_INDEX ( @{[ join("+", map("(TARGET_TIMER_TIM${_} >= 0)", @prev_timers)) || 0 ]} )
140 #else
141 # define TARGET_TIMER_TIM${timer}_BIT 0
142 # define TARGET_TIMER_TIM${timer}_INDEX deftimer_error_TIMER${timer}_is_not_enabled_on_target
143 #endif
144 END2
145 #define TIMER_USED_BITS ( @{[ join "|", map("TARGET_TIMER_TIM${_}_BIT", @timers) ]} )
146 #define TIMER_USED_COUNT ( @{[ join "+", map("(TARGET_TIMER_TIM${_} >= 0)", @timers) ]} )
148 // structure to hold all timerRec_t.
149 // Number of channels per timer is user specifed, structure will ensure correct packing
150 struct timerRec_all {
151 @{[ map { my $timer = $_; chomp(my $ret = << "END2"); $ret } @timers ]}
152 #if TARGET_TIMER_TIM${timer} >= 0
153 timerRec_t rec_TIM${timer};
154 # if TARGET_TIMER_TIM${timer} > 0
155 timerChRec_t rec_TIM${timer}_ch[TARGET_TIMER_TIM${timer}];
156 # endif
157 #endif
158 END2
162 my $timer_inc_file="$drivers_dir/timer_c_generated.inc";
163 open $fh, '>', $timer_inc_file or die "Cannot open $timer_inc_file: $!";
164 print { $fh} <<"END" or die "Cannot write into $timer_inc_file: $!"; close $fh;
165 ${disclaimer_generated}
167 // this code is included into timer.c file
169 const timerDef_t timerDefs[] = {
170 @{[ map { my $timer = $_; chomp(my $ret = << "END2"); $ret } @timers ]}
171 #if TARGET_TIMER_TIM${timer} >= 0
172 DEF_TIMER_DEFINE(${timer}),
173 #endif
174 END2
177 timerRec_t* const timerRecPtrs[] = {
178 @{[ map { my $timer = $_; chomp(my $ret = << "END2"); $ret } @timers ]}
179 #if TARGET_TIMER_TIM${timer} >= 0
180 &timerRecs.rec_TIM${timer},
181 #endif
182 END2
183 NULL // terminate the list