-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathbf2inav.py
executable file
·983 lines (800 loc) · 31.1 KB
/
bf2inav.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
#
# This script can be used to Generate a basic working target from a Betaflight Configuration.
# The idea is that this target can be used as a starting point for full INAV target.
#
# The generated target will often be enough to get INAV working, but may need some manual editing.
#
# Betaflight Configuration files are available at https://github.com/betaflight/config
#
# Common things to look for: target.c timer definitions. You may need to change timers around
# to get all features working. The script will add commented out lines for all timer possibilites
# for a given pin.
import sys
import os
import io
import getopt
import re
import json
import random
import string
import yaml
version = '0.1'
def translateFunctionName(bffunction, index):
return bffunction + '_' + index
def translatePin(bfpin):
pin = re.sub(r'^([A-Z])0*(\d+)$', r'P\1\2', bfpin)
return pin
def mcu2target(mcu):
#mcu STM32F405
if mcu['type'] == 'STM32F405':
return 'target_stm32f405xg'
#mcu STM32F411
if mcu['type'] == 'STM32F411':
return 'target_stm32f411xe'
#mcu STM32F7X2
if mcu['type'] == 'STM32F7X2':
return 'target_stm32f722xe'
#mcu STM32F745
if mcu['type'] == 'STM32F745':
return 'target_stm32f745xg'
#mcu STM32H743
if mcu['type'] == 'STM32H743':
return 'target_stm32h743xi'
#mcu 'AT32F435G'
if mcu['type'] == 'AT32F435G':
return 'target_at32f43x_xGT7'
#mcu 'AT32F435M'
if mcu['type'] == 'AT32F435M':
return 'target_at32f43x_xMT7'
print("Unknown MCU: %s!" % (mcu))
sys.exit(-1)
def getPortConfig(map):
mcu = map['mcu']
#mcu STM32F405
if mcu['type'] == 'STM32F405':
return """
#define TARGET_IO_PORTA 0xffff
#define TARGET_IO_PORTB 0xffff
#define TARGET_IO_PORTC 0xffff
#define TARGET_IO_PORTD 0xffff
#define TARGET_IO_PORTE 0xffff
#define TARGET_IO_PORTF 0xffff
"""
#mcu STM32F411
if mcu['type'] == 'STM32F411':
return """
#define TARGET_IO_PORTA 0xffff
#define TARGET_IO_PORTB 0xffff
#define TARGET_IO_PORTC 0xffff
#define TARGET_IO_PORTD 0xffff
#define TARGET_IO_PORTE 0xffff
#define TARGET_IO_PORTF 0xffff
"""
#mcu STM32F7X2
if mcu['type'] == 'STM32F7X2':
return """
#define TARGET_IO_PORTA 0xffff
#define TARGET_IO_PORTB 0xffff
#define TARGET_IO_PORTC 0xffff
#define TARGET_IO_PORTD 0xffff
#define TARGET_IO_PORTE 0xffff
#define TARGET_IO_PORTF 0xffff
"""
#mcu STM32F745
if mcu['type'] == 'STM32F745':
return """
#define TARGET_IO_PORTA 0xffff
#define TARGET_IO_PORTB 0xffff
#define TARGET_IO_PORTC 0xffff
#define TARGET_IO_PORTD 0xffff
#define TARGET_IO_PORTE 0xffff
#define TARGET_IO_PORTF 0xffff
"""
#mcu STM32H743
if mcu['type'] == 'STM32H743':
return """
#define TARGET_IO_PORTA 0xffff
#define TARGET_IO_PORTB 0xffff
#define TARGET_IO_PORTC 0xffff
#define TARGET_IO_PORTD 0xffff
#define TARGET_IO_PORTE 0xffff
#define TARGET_IO_PORTF 0xffff
#define TARGET_IO_PORTG 0xffff
"""
#mcu 'AT32F435G'
if mcu['type'] == 'AT32F435G':
return """#define TARGET_IO_PORTA 0xffff
#define TARGET_IO_PORTB 0xffff
#define TARGET_IO_PORTC 0xffff
#define TARGET_IO_PORTD 0xffff
#define TARGET_IO_PORTH 0xffff
"""
#mcu 'AT32F435M'
if mcu['type'] == 'AT32F435M':
return """#define TARGET_IO_PORTA 0xffff
#define TARGET_IO_PORTB 0xffff
#define TARGET_IO_PORTC 0xffff
#define TARGET_IO_PORTD 0xffff
#define TARGET_IO_PORTH 0xffff
"""
print("Unknown MCU: %s" % (mcu))
sys.exit(-1)
def writeCmakeLists(outputFolder, map):
file = open(outputFolder + '/CMakeLists.txt', "w+")
t = mcu2target(map['mcu'])
file.write("%s(%s SKIP_RELEASES)\n" % (t, map['board_name']))
return
def findPinsByFunction(function, map):
result = []
for func in map['funcs']:
pattern = r"^%s" % (function)
if re.search(pattern, func):
#print ("%s: %s" % (function, func))
result.append(map['funcs'][func])
return result
def findPinByFunction(function, map):
if function in map['funcs']:
return map['funcs'][function]
return None
def getPwmOutputCount(map):
motors = findPinsByFunction("MOTOR", map)
servos = findPinsByFunction("SERVO", map)
return len(motors) + len(servos)
def getGyroAlign(map):
bfalign = map['defines'].get('GYRO_1_ALIGN', 'CW0_DEG')
return bfalign
#m = re.search(r"^CW(\d+)(FLIP)?$", bfalign)
#if m:
# deg = m.group(1)
# flip = m.group(2)
# if flip:
# return "CW%s_DEG_FLIP" % (deg)
# else:
# return "CW%s_DEG" % (deg)
def getSerialByFunction(map, function):
for serial in map.get("serial"):
if map['serial'][serial].get('FUNCTION') == function:
return serial
return None
def getSerialMspDisplayPort(map):
return getSerialByFunction(map, "131072")
def getSerialRx(map):
rx = getSerialByFunction(map, "64")
if(rx != None):
return int(rx) + 1
return None
def writeTargetH(folder, map):
file = open(folder + '/target.h', "w+")
file.write("""/*
* This file is part of INAV.
*
* INAV is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* INAV is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with INAV. If not, see <http://www.gnu.org/licenses/>.
*
* This target has been autgenerated by bf2inav.py
*/
#pragma once
//#define USE_TARGET_CONFIG
#define DEFAULT_FEATURES (FEATURE_OSD | FEATURE_CURRENT_METER | FEATURE_VBAT | FEATURE_TELEMETRY )
\n"""
)
board_id = ''.join(random.choice(string.ascii_uppercase) for i in range(4))
file.write("#define TARGET_BOARD_IDENTIFIER \"%s\"\n" % (board_id))
file.write("#define USBD_PRODUCT_STRING \"%s\"\n" % (map['board_name']))
# beeper
file.write("// Beeper\n")
pin = findPinByFunction('BEEPER', map)
#print ("BEEPER")
if pin:
#print ("BEEPER: %s" % (pin))
file.write("#define USE_BEEPER\n")
file.write("#define BEEPER %s\n" % (pin))
if 'BEEPER_INVERTED' in map['empty_defines']:
file.write("#define BEEPER_INVERTED\n")
#print ("INVERTED")
# Leds
file.write("// Leds\n")
pin = findPinByFunction('LED_STRIP', map)
#print ("LED")
if pin:
#print ("LED: %s" % (pin))
file.write('#define USE_LED_STRIP\n')
file.write("#define WS2811_PIN %s\n" % (pin))
for i in range(0, 9):
pin = findPinByFunction("LED%i" % (i), map)
if pin:
#print ("LED%i: %s" % (i, pin))
file.write("#define LED%i %s\n" % (i, pin))
# Serial ports and usb
#print ("SERIAL")
file.write("// UARTs\n")
file.write("#define USB_IO\n")
file.write("#define USE_VCP\n")
serial_count = 1
pin = findPinByFunction('USB_DETECT', map)
if pin:
file.write("#define USE_USB_DETECT\n")
file.write("#define USB_DETECT_PIN %s\n" % (pin))
#file.write("#define VBUS_SENSING_ENABLED\n");
for i in range(1, 9):
txpin = findPinByFunction("UART%i_TX" % (i), map)
rxpin = findPinByFunction("UART%i_RX" % (i), map)
if txpin or rxpin:
#print ("UART%s" % (i))
file.write("#define USE_UART%i\n" % (i))
serial_count+=1
else:
continue
if rxpin:
file.write("#define UART%i_RX_PIN %s\n" % (i, rxpin))
if txpin:
file.write("#define UART%i_TX_PIN %s\n" % (i, txpin))
else:
file.write("#define UART%i_TX_PIN %s\n" % (i, rxpin))
# soft serial
for i in range(1, 9):
txpin = findPinByFunction("SOFTSERIAL%i_TX" % (i), map)
rxpin = findPinByFunction("SOFTSERIAL%i_RX" % (i), map)
idx = i
if txpin != None or rxpin != None:
#print ("SOFTUART%s" % (i))
file.write("#define USE_SOFTSERIAL%i\n" % (idx))
serial_count+=1
else:
continue
if txpin != None:
file.write("#define SOFTSERIAL_%i_TX_PIN %s\n" % (idx, txpin))
else:
file.write("#define SOFTSERIAL_%i_TX_PIN %s\n" % (idx, rxpin))
if rxpin != None:
file.write("#define SOFTSERIAL_%i_RX_PIN %s\n" % (idx, rxpin))
else:
file.write("#define SOFTSERIAL_%i_RX_PIN %s\n" % (idx, txpin))
file.write("#define SERIAL_PORT_COUNT %i\n" % (serial_count))
file.write("#define DEFAULT_RX_TYPE RX_TYPE_SERIAL\n")
file.write("#define SERIALRX_PROVIDER SERIALRX_CRSF\n")
# TODO: map default serial uart
#serial_rx = getSerialRx(map)
serial_rx = None
if serial_rx != None:
file.write("#define SERIALRX_UART SERIAL_PORT_USART%s\n" % (serial_rx))
file.write("// SPI\n")
use_spi_defined = False
for i in range(1, 9):
sckpin = findPinByFunction("SPI%i_SCK" % (i), map)
misopin = findPinByFunction("SPI%i_SDI" % (i), map)
mosipin = findPinByFunction("SPI%i_SDO" % (i), map)
if (sckpin or misopin or mosipin):
if (not use_spi_defined):
use_spi_defined = True
file.write("#define USE_SPI\n")
file.write("#define USE_SPI_DEVICE_%i\n" % (i))
if sckpin:
file.write("#define SPI%i_SCK_PIN %s\n" % (i, sckpin))
if misopin:
file.write("#define SPI%i_MISO_PIN %s\n" % (i, misopin))
if mosipin:
file.write("#define SPI%i_MOSI_PIN %s\n" % (i, mosipin))
use_i2c_defined = False
for i in range(1, 9):
sclpin = findPinByFunction("I2C%i_SCL" % (i), map)
sdapin = findPinByFunction("I2C%i_SDA" % (i), map)
if (sclpin or sdapin):
if (not use_i2c_defined):
file.write("// I2C\n")
#print ("I2C")
use_i2c_defined = True
file.write("#define USE_I2C\n")
file.write("#define USE_I2C_DEVICE_%i\n" % (i))
if sclpin:
file.write("#define I2C%i_SCL %s\n" % (i, sclpin))
if sdapin:
file.write("#define I2C%i_SDA %s\n" % (i, sdapin))
if 'MAG_I2C_INSTANCE' in map['defines']:
file.write("// MAG\n")
bfinstance = map['defines']['MAG_I2C_INSTANCE']
file.write("#define USE_MAG\n")
file.write("#define USE_MAG_ALL\n")
# (I2CDEV_1)
m = re.search(r'^\s*#define\s+MAG_I2C_INSTANCE\s+\(?I2CDEV_(\d+)\)?\s*$', bfinstance)
if m:
file.write("#define MAG_I2C_BUS BUS_I2C%i" % (m.group(1)))
file.write("// ADC\n")
# ADC_BATT ch1
use_adc = False
pin = findPinByFunction('ADC_VBAT', map)
if pin:
use_adc = True
file.write("#define ADC_CHANNEL_1_PIN %s\n" % (pin))
file.write("#define VBAT_ADC_CHANNEL ADC_CHN_1\n");
# ADC_CURR ch2
pin = findPinByFunction('ADC_CURR', map)
if pin:
use_adc = True
file.write("#define ADC_CHANNEL_2_PIN %s\n" % (pin))
file.write("#define CURRENT_METER_ADC_CHANNEL ADC_CHN_2\n");
# ADC_RSSI ch3
pin = findPinByFunction('ADC_RSSI', map)
if pin:
use_adc = True
file.write("#define ADC_CHANNEL_3_PIN %s\n" % (pin))
file.write("#define RSSI_ADC_CHANNEL ADC_CHN_3\n");
# ADC_EXT ch4 (airspeed?)
pin = findPinByFunction('ADC_EXT_1', map)
if pin:
use_adc = True
file.write("#define ADC_CHANNEL_4_PIN %s\n" % (pin))
file.write("#define AIRSPEED_ADC_CHANNEL ADC_CHN_4\n");
if use_adc:
file.write("#define USE_ADC\n")
file.write("#define ADC_INSTANCE ADC1\n")
# TODO:
#define ADC1_DMA_STREAM DMA2_Stream4
file.write("// Gyro & ACC\n")
for supportedgyro in ['BMI160', 'BMI270', 'ICM20689', 'ICM42605', 'MPU6000', 'MPU6500', 'MPU9250']:
found = False
for var in ['USE_ACCGYRO_', 'USE_ACC_', 'USE_ACC_SPI', 'USE_GYRO_', 'USE_GYRO_SPI_']:
val = var + supportedgyro
if val in map['empty_defines']:
found = True
break
if found:
#print (supportedgyro)
file.write("#define USE_IMU_%s\n" % (supportedgyro))
file.write("#define %s_CS_PIN %s\n" % (supportedgyro, findPinByFunction('GYRO_1_CS', map)))
file.write("#define %s_SPI_BUS BUS_%s\n" % (supportedgyro, map['defines']['GYRO_1_SPI_INSTANCE']))
file.write("#define IMU_%s_ALIGN %s\n" % (supportedgyro, getGyroAlign(map)))
if 'USE_BARO' in map['empty_defines']:
#print ("BARO")
file.write("// BARO\n")
file.write("#define USE_BARO\n")
if 'BARO_I2C_INSTANCE' in map['defines']:
file.write("#define USE_BARO_ALL\n")
m = re.search(r'I2CDEV_(\d+)', map['defines']['BARO_I2C_INSTANCE'])
if m:
file.write("#define BARO_I2C_BUS BUS_I2C%s\n" % (m.group(1)))
if 'BARO_SPI_INSTANCE' in map['defines']:
file.write("#define USE_BARO_BMP280\n")
file.write("#define USE_BARO_SPI_BMP280\n")
file.write("#define BMP280_SPI_BUS BUS_%s\n" % (map['defines']['BARO_SPI_INSTANCE']))
file.write("#define BMP280_CS_PIN %s\n" % (findPinByFunction('BARO_CS', map)))
file.write("// OSD\n")
if 'USE_MAX7456' in map['empty_defines']:
#print ("ANALOG OSD")
file.write("#define USE_MAX7456\n")
pin = findPinByFunction('MAX7456_SPI_CS', map)
file.write("#define MAX7456_CS_PIN %s\n" % (pin))
file.write("#define MAX7456_SPI_BUS BUS_%s\n" % (map['defines']['MAX7456_SPI_INSTANCE']))
file.write("// Blackbox\n")
# Flash:
if 'USE_FLASH' in map['empty_defines']:
#print ("FLASH BLACKBOX")
cs = findPinByFunction("FLASH_CS", map)
spiflash_bus = map['defines'].get('FLASH_SPI_INSTANCE')
if cs:
# TODO: add more drivers
suppored_flash_chips = [
'M25P16',
'W25M',
'W25M02G',
'W25M512',
'W25N01G'
]
file.write("#define USE_FLASHFS\n")
file.write("#define ENABLE_BLACKBOX_LOGGING_ON_SPIFLASH_BY_DEFAULT\n")
for flash in suppored_flash_chips:
file.write("#define USE_FLASH_%s\n" % (flash))
file.write("#define %s_SPI_BUS BUS_%s\n" % (flash, spiflash_bus))
file.write("#define %s_CS_PIN %s\n" % (flash, cs))
# SD Card:
use_sdcard = False
for i in range(1, 9):
sdio_cmd = findPinByFunction("SDIO_CMD_%i" % (i), map)
if sdio_cmd:
if not use_sdcard:
file.write("#define USE_SDCARD\n")
file.write("#define USE_SDCARD_SDIO\n")
file.write("#define ENABLE_BLACKBOX_LOGGING_ON_SDCARD_BY_DEFAULT\n")
use_sdcard = True
file.write("#define SDCARD_SDIO_4BIT\n")
file.write("#define SDCARD_SDIO_DEVICE SDIODEV_%i\n" % (i))
# PINIO
use_pinio = False
for i in range(1, 9):
pinio = findPinByFunction("PINIO%i" % (i), map)
if pinio != None:
if not use_pinio:
use_pinio = True
file.write("\n// PINIO\n\n")
file.write("#define USE_PINIO\n")
file.write("#define USE_PINIOBOX\n")
file.write("#define PINIO%i_PIN %s\n" % (i, pinio))
file.write("\n\n// Others\n\n")
pwm_outputs = getPwmOutputCount(map)
file.write("#define MAX_PWM_OUTPUT_PORTS %i\n" % (pwm_outputs))
file.write("#define USE_SERIAL_4WAY_BLHELI_INTERFACE\n")
file.write("#define USE_DSHOT\n")
file.write("#define USE_ESC_SENSOR\n")
if 'DEFAULT_VOLTAGE_METER_SCALE' in map['defines']:
file.write("#define VOLTAGE_METER_SCALE %s\n" % (map['defines']['DEFAULT_VOLTAGE_METER_SCALE']))
if 'DEFAULT_CURRENT_METER_SCALE' in map['defines']:
file.write("#define CURRENT_METER_SCALE %s\n" % (map['defines']['DEFAULT_CURRENT_METER_SCALE']))
port_config = getPortConfig(map)
file.write(port_config)
file.close()
return
def mcu2timerKey(mcu):
m = re.search(r'^AT32F435[GM]', mcu)
if m:
return 'AT32F435'
m = re.search(r'^STM32F405', mcu)
if m:
return 'STM32F405'
m = re.search(r'^STM32F7[2Xx]2', mcu)
if m:
return 'STM32F722'
m = re.search(r'^STM32F7[Xx46]5', mcu)
if m:
return 'STM32F745'
m = re.search(r'^STM32H7[45]3', mcu)
if m:
return 'STM32H743'
print ("Unsupported MCU: %s" % (mcu))
sys.exit(-1)
def getTimerInfo(map, pin):
with open("timer_pins.yaml", "r") as f:
pindb = yaml.safe_load(f)
f.close()
mcu = map['mcu']['type']
tk = mcu2timerKey(mcu)
if not tk in pindb:
print ("PINDB not available for MCU: %s" % (mcu))
sys.exit(-1)
timers = pindb[tk].get(pin, None)
if timers:
result = []
for ti in timers:
timer = list(ti.keys())[0]
channel = ti[timer]
result.append([timer, channel])
return result
return None
def writeTargetC(folder, map):
file = open(folder + '/target.c', "w+")
file.write("""/*
* This file is part of INAV.
*
* INAV is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* INAV is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with INAV. If not, see <http://www.gnu.org/licenses/>.
*
* This target has been autgenerated by bf2inav.py
*/
#include <stdint.h>
#include "platform.h"
#include "drivers/bus.h"
#include "drivers/io.h"
#include "drivers/pwm_mapping.h"
#include "drivers/timer.h"
#include "drivers/pinio.h"
//#include "drivers/sensor.h"
""")
#for supportedgyro in ['BMI160', 'BMI270', 'ICM20689', 'ICM42605', 'MPU6000', 'MPU6500', 'MPU9250']:
# found = False
# for var in ['USE_ACCGYRO_', 'USE_ACC_', 'USE_ACC_SPI', 'USE_GYRO_', 'USE_GYRO_SPI_']:
# val = var + supportedgyro
# if val in map['empty_defines']:
# found = True
# break
# if found:
# file.write("//BUSDEV_REGISTER_SPI_TAG(busdev_%s, DEVHW_%s, %s_SPI_BUS, %s_CS_PIN, NONE, 0, DEVFLAGS_NONE, IMU_%s_ALIGN);\n" % (supportedgyro.lower(), supportedgyro, supportedgyro, supportedgyro, supportedgyro))
snum=1
file.write("\ntimerHardware_t timerHardware[] = {\n")
motors = findPinsByFunction("MOTOR", map)
if motors:
for motor in motors:
timerInfo = getTimerInfo(map, motor)
if timerInfo:
first = True
#print (timerInfo)
for (t, ch) in timerInfo:
if first:
file.write(" DEF_TIM(%s, %s, %s, TIM_USE_OUTPUT_AUTO, 0, %s), // S%i\n" % (t, ch, motor, 0, snum))
first = False
snum += 1
else:
file.write(" //DEF_TIM(%s, %s, %s, TIM_USE_OUTPUT_AUTO, 0, %s),\n" % (t, ch, motor, 0))
file.write("\n")
servos = findPinsByFunction("SERVO", map)
if servos:
for servo in servos:
timerInfo = getTimerInfo(map, servo)
if timerInfo:
first = True
#print (timerInfo)
for (t, ch) in timerInfo:
if first:
file.write(" DEF_TIM(%s, %s, %s, TIM_USE_OUTPUT_AUTO, 0, %s), // S%i\n" % (t, ch, servo, 0, snum))
first = False
snum += 1
else:
file.write(" //DEF_TIM(%s, %s, %s, TIM_USE_OUTPUT_AUTO, 0, %s),\n" % (t, ch, servo, 0))
file.write("\n")
beeper = findPinByFunction("BEEPER", map)
if beeper:
timerInfo = getTimerInfo(map, beeper)
if timerInfo:
first = True
#print ("BEEPER: %s" % (timerInfo))
for (t, ch) in timerInfo:
if first:
file.write(" DEF_TIM(%s, %s, %s, TIM_USE_BEEPER, 0, %s),\n" % (t, ch, beeper, 0))
first = False
else:
file.write(" //DEF_TIM(%s, %s, %s, TIM_USE_BEEPER, 0, %s),\n" % (t, ch, beeper, 0))
file.write("\n")
led = findPinByFunction("LED_STRIP", map)
if led:
timerInfo = getTimerInfo(map, led)
if timerInfo:
first = True
#print (timerInfo)
for (t, ch) in timerInfo:
if first:
file.write(" DEF_TIM(%s, %s, %s, TIM_USE_LED, 0, %s),\n" % (t, ch, led, 0))
first = False
else:
file.write(" //DEF_TIM(%s, %s, %s, TIM_USE_LED, 0, %s),\n" % (t, ch, led, 0))
file.write("\n")
file.write("""};
const int timerHardwareCount = sizeof(timerHardware) / sizeof(timerHardware[0]);
""")
file.close()
return
def writeConfigC(folder, map):
file = open(folder + '/config.c', "w+")
file.write("""/*
* This file is part of INAV.
*
* INAV is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* INAV is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with INAV. If not, see <http://www.gnu.org/licenses/>.
*
* This target has been autgenerated by bf2inav.py
*/
#include <stdint.h>
#include "platform.h"
#include "fc/fc_msp_box.h"
#include "fc/config.h"
#include "io/piniobox.h"
void targetConfiguration(void)
{
""")
#//pinioBoxConfigMutable()->permanentId[0] = BOX_PERMANENT_ID_USER1;
#//pinioBoxConfigMutable()->permanentId[1] = BOX_PERMANENT_ID_USER2;
#//beeperConfigMutable()->pwmMode = true;
file.write("""
}
""")
file.close()
return
def writeTarget(outputFolder, map):
writeCmakeLists(outputFolder, map)
writeTargetH(outputFolder, map)
writeTargetC(outputFolder, map)
writeConfigC(outputFolder, map)
return
def buildMap(inputFile):
map = { 'defines': {}, 'empty_defines': [], 'features': ['FEATURE_OSD', 'FEATURE_TELEMETRY', 'FEATURE_CURRENT_METER', 'FEATURE_VBAT', 'FEATURE_TX_PROF_SEL', 'FEATURE_BLACKBOX'], 'pins': {}, 'funcs': {}, 'timer_pin_map': {}}
f = open(inputFile, 'r')
while True:
l = f.readline()
if not l:
break
m = re.search(r'^#define\s+FC_TARGET_MCU\s+([0-9A-Za-z]+)$', l)
if m:
map['mcu'] = {'type': m.group(1)}
m = re.search(r'^#define\s+BOARD_NAME\s+(\w+)$', l)
if m:
map['board_name'] = m.group(1)
m = re.search(r'^#define\s+MANUFACTURER_ID\s+(\w+)$', l)
if m:
map['manufacturer_id'] = m.group(1)
m = re.search(r'^#define\s+(\w+)\s*$', l)
if m:
map['empty_defines'].append(re.sub('ICM42688P', 'ICM42605', m.group(1)))
m = re.search(r'^\s*#define\s+DEFAULT_FEATURES\s+\((.+?)\)\s*$', l)
if m:
features = m.group(1).split('|')
for feat in features:
feat = feat.strip()
if not feat in map['features']:
map['features'].append(feat)
m = re.search(r'^#define\s+(\w+)\s+(\S+)\s*$', l)
if m:
map['defines'][m.group(1)] = m.group(2)
# i: timer index
# p: pin
# o: timer channel? 1 = first
# d: dma opts
# i p o d
# TIMER_PIN_MAP( 0, PB8 , 2, -1) \
m = re.search(r'^\s*TIMER_PIN_MAP\s*\(\s*(\d+)\s*,\s*([0-9A-Za-z]+)\s*,\s*(\d+)\s*,\s*(-?\d+)\s*\).+', l)
if m:
map['timer_pin_map'][m.group(1)] = {
'i': m.group(1),
'p': m.group(2),
'o': m.group(3),
'd': m.group(4)
}
m = re.search(r'^\s*#define\s+(\w+)_PIN\s+([A-Z0-9]+)\s*$', l)
if m:
pin = m.group(2)
func = m.group(1)
if not map['funcs'].get(func):
map['funcs'][func] = {}
map['funcs'][func] = pin
if not map['pins'].get(pin):
map['pins'][pin] = {}
map['pins'][pin] = func
#m = re.search(r'^feature\s+(-?\w+)$', l)
#if m:
# map['features'].append(m.group(1))
#m = re.search(r'^resource\s+(-?\w+)\s+(\d+)\s+(\w+)$', l)
#if m:
# resource_type = m.group(1)
# resource_index = m.group(2)
# pin = translatePin(m.group(3))
# if not map['pins'].get(pin):
# map['pins'][pin] = {}
# map['pins'][pin]['function'] = translateFunctionName(resource_type, resource_index)
#m = re.search(r'^timer\s+(\w+)\s+AF(\d+)$', l)
#if m:
# pin = translatePin(m.group(1))
# if not map['pins'].get(pin):
# map['pins'][pin] = {}
#
# map['pins'][pin]['AF'] = m.group(2)
#m = re.search(r'^#\s*pin\s+(\w+):\s*(TIM\d+)\s+(CH\d+).+$', l)
#if m:
# pin = translatePin(m.group(1))
# if not map['pins'].get(pin):
# map['pins'][pin] = {}
# map['pins'][pin]['TIM'] = m.group(2)
# map['pins'][pin]['CH'] = m.group(3)
#m = re.search(r'^dma\s+([A-Za-z0-9]+)\s+([A-Za-z0-9]+)\s+(\d+).*$', l)
#if m:
# if(m.group(1) == 'ADC'):
# pin = 'ADC' + m.group(2)
# else:
# pin = translatePin(m.group(2))
# if not map['dmas'].get(pin):
# map['dmas'][pin] = {}
# map['dmas'][pin]['DMA'] = m.group(3)
# 1 2 3 4
# pin B04: DMA1 Stream 4 Channel 5
#m = re.search(r'^#\s+pin\s+(\w+):\s+(DMA\d+)\s+Stream\s+(\d+)\s+Channel\s+(\d+)\s*$', l)
#if m:
# pin = translatePin(m.group(1))
# if not map['pins'].get(pin):
# map['pins'][pin] = {}
# map['pins'][pin]['DMA_STREAM'] = m.group(3)
# map['pins'][pin]['DMA_CHANNEL'] = m.group(4)
#m = re.search(r'^#\s+ADC\s+(\d+):\s+(DMA\d+)\s+Stream\s+(\d+)\s+Channel\s+(\d+)\s*$', l)
#if m:
# pin = 'ADC' + m.group(1)
# if not map['dmas'].get(pin):
# map['dmas'][pin] = {}
# map['dmas'][pin]['DMA_STREAM'] = m.group(3)
# map['dmas'][pin]['DMA_CHANNEL'] = m.group(4)
#m = re.search(r'^#\s+TIMUP\s+(\d+):\s+(DMA\d+)\s+Stream\s+(\d+)\s+Channel\s+(\d+)\s*$', l)
#if m:
# pin = 'TIMUP' + m.group(1)
# if not map['dmas'].get(pin):
# map['dmas'][pin] = {}
# map['dmas'][pin]['DMA_STREAM'] = m.group(3)
# map['dmas'][pin]['DMA_CHANNEL'] = m.group(4)
#m = re.search(r'^#\s+ADC\s+(\d+):\s+(DMA\d+)\s+Stream\s+(\d+)\s+Request\s+(\d+)\s*$', l)
#if m:
# pin = 'ADC' + m.group(1)
# if not map['dmas'].get(pin):
# map['dmas'][pin] = {}
# map['dmas'][pin]['DMA_STREAM'] = m.group(3)
# map['dmas'][pin]['DMA_REQUEST'] = m.group(4)
#m = re.search(r'^#\s+TIMUP\s+(\d+):\s+(DMA\d+)\s+Stream\s+(\d+)\s+Channel\s+(\d+)\s*$', l)
#if m:
# pin = 'TIMUP' + m.group(1)
# if not map['dmas'].get(pin):
# map['dmas'][pin] = {}
# map['dmas'][pin]['DMA_STREAM'] = m.group(3)
# map['dmas'][pin]['DMA_CHANNEL'] = m.group(4)
#m = re.search(r'^#\s+TIMUP\s+(\d+):\s+(DMA\d+)\s+Stream\s+(\d+)\s+Request\s+(\d+)\s*$', l)
#if m:
# pin = 'TIMUP' + m.group(1)
# if not map['dmas'].get(pin):
# map['dmas'][pin] = {}
# map['dmas'][pin]['DMA_STREAM'] = m.group(3)
# map['dmas'][pin]['DMA_REQUEST'] = m.group(4)
#m = re.search(r'^serial\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$', l)
#if m:
# idx = m.group(1)
# if not map['serial'].get(idx):
# map['serial'][idx] = {}
# map['serial'][idx]['FUNCTION'] = m.group(2)
# map['serial'][idx]['MSP_BAUD'] = m.group(3)
# map['serial'][idx]['GPS_BAUD'] = m.group(4)
# map['serial'][idx]['TELEMETRY_BAUD'] = m.group(5)
# map['serial'][idx]['BLACKBOX_BAUD'] = m.group(6)
#m = re.search(r'^set\s+(\w+)\s*=\s*(\w+)$', l)
#if m:
# map['variables'][m.group(1)] = m.group(2)
return map
def printHelp():
print ("%s -i bf-target.config -o output-directory" % (sys.argv[0]))
print (" -i | --input-config=<file> -- print this help")
print (" -o | --output-dir=<targetdir> -- print this help")
print (" -h | --help -- print this help")
print (" -v | --version -- print version")
return
def main(argv):
inputfile = ''
outputdir = '.'
global version
try:
opts, args = getopt.getopt(argv,"hvi:o:", ["input-config=", "output-dir=", 'version', 'help'])
except getopt.GeoptError:
printHelp()
sys.exit(2)
for opt, arg in opts:
if opt in ('-h', '--help'):
printHelp()
sys.exit(1)
elif opt in ('-i', '--input-config'):
inputfile = arg
elif opt in ('-o', '--output-dir'):
outputdir = arg
elif opt in ('-v', '--version'):
print ("%s: %s" % (sys.argv[0], version))
sys.exit(0)
if (not os.path.isfile(inputfile) ):
print("no such file %s" % inputfile)
sys.exit(2)
if (not os.path.isdir(outputdir) ):
print("no such directory %s" % outputdir)
sys.exit(2)
else:
targetDefinition = buildMap(inputfile)
map = buildMap(inputfile)
print (json.dumps(map, indent=4))
writeTarget(outputdir, map)
sys.exit(0)
if( __name__ == "__main__"):
main(sys.argv[1:])