xref: /btstack/port/apollo2-em9304/create_examples.py (revision 6ccd8248590f666db07dd7add13fecb4f5664fb5)
1*6ccd8248SMilanka Ringwald#!/usr/bin/env python3
2e6c96737SMatthias Ringwald#
3e6c96737SMatthias Ringwald# Create project files for all BTstack embedded examples in AmbiqSuite/boards/apollo2_evb_am_ble
4e6c96737SMatthias Ringwald
5e6c96737SMatthias Ringwaldimport os
6e6c96737SMatthias Ringwaldimport shutil
7e6c96737SMatthias Ringwaldimport sys
8e6c96737SMatthias Ringwaldimport time
9e6c96737SMatthias Ringwaldimport subprocess
10e6c96737SMatthias Ringwald
11e6c96737SMatthias Ringwaldmakefile_gatt_add_on = '''
12e6c96737SMatthias Ringwald$(CONFIG)/EXAMPLE.o: $(CONFIG)/EXAMPLE.h
13e6c96737SMatthias Ringwald$(CONFIG)/EXAMPLE.h: ../../../../../third_party/btstack/example/EXAMPLE.gatt
14e6c96737SMatthias Ringwald\t../../../../../third_party/btstack/tool/compile_gatt.py $^ $@
15e6c96737SMatthias Ringwald'''
16e6c96737SMatthias Ringwald
17e6c96737SMatthias Ringwald# get script path
18e6c96737SMatthias Ringwaldscript_path = os.path.abspath(os.path.dirname(sys.argv[0]))
19e6c96737SMatthias Ringwald
20e6c96737SMatthias Ringwald# validate AmbiqSuite root by reading VERSION.txt
21e6c96737SMatthias Ringwaldam_root = script_path + "/../../../../"
22e6c96737SMatthias Ringwaldam_version_txt = ""
23e6c96737SMatthias Ringwaldtry:
24e6c96737SMatthias Ringwald    with open(am_root + 'VERSION.txt', 'r') as fin:
25e6c96737SMatthias Ringwald        am_version_txt = fin.read()  # Read the contents of the file into memory.
26e6c96737SMatthias Ringwaldexcept:
27e6c96737SMatthias Ringwald    pass
28e6c96737SMatthias Ringwaldif len(am_version_txt) == 0:
29e6c96737SMatthias Ringwald    print("Cannot find AmbiqSuite root. Make sure BTstack is checked out as AmbiqSuite/third/btstack");
30e6c96737SMatthias Ringwald    sys.exit(1)
31e6c96737SMatthias Ringwald
32e6c96737SMatthias Ringwald# show WICED version
33e6c96737SMatthias Ringwaldprint("Found AmbiqSuite SDK version: %s" % am_version_txt)
34e6c96737SMatthias Ringwald
35e6c96737SMatthias Ringwald# path to examples
36e6c96737SMatthias Ringwaldexamples_embedded = script_path + "/../../example/"
37e6c96737SMatthias Ringwald
38e6c96737SMatthias Ringwald# path to example template
39e6c96737SMatthias Ringwaldexample_template = script_path + "/example-template/"
40e6c96737SMatthias Ringwald
41e6c96737SMatthias Ringwald# path to AmbiqSuite/boards/apollo2_evb_am_ble/examples
42e6c96737SMatthias Ringwaldapps_btstack = am_root + "/boards/apollo2_evb_am_ble/examples/"
43e6c96737SMatthias Ringwald
44e6c96737SMatthias Ringwaldprint("Creating examples in /boards/apollo2_evb_am_ble/examples:")
45e6c96737SMatthias Ringwald
46bdc352b1SMatthias RingwaldLE_EXAMPLES = ["ancs_client_demo", "gap_le_advertisements", "gatt_battery_query", "gatt_browser", "gatt_counter", "gatt_streamer", "le_streamer_client", "sm_pairing_peripheral", "sm_pairing_central"]
47e6c96737SMatthias Ringwald
48e6c96737SMatthias Ringwald# iterate over btstack examples
49e6c96737SMatthias Ringwaldfor example in LE_EXAMPLES:
50e6c96737SMatthias Ringwald
51e6c96737SMatthias Ringwald    # create example folder
52e6c96737SMatthias Ringwald    apps_folder = apps_btstack + "btstack_" + example + "/"
53e6c96737SMatthias Ringwald    if not os.path.exists(apps_folder):
54e6c96737SMatthias Ringwald        os.makedirs(apps_folder)
55e6c96737SMatthias Ringwald
56e6c96737SMatthias Ringwald    # copy project makefile
57e6c96737SMatthias Ringwald    shutil.copyfile(example_template + "Makefile", apps_folder + "Makefile");
58e6c96737SMatthias Ringwald
59e6c96737SMatthias Ringwald    # create GCC folder
60e6c96737SMatthias Ringwald    gcc_folder = apps_folder + "/gcc/"
61e6c96737SMatthias Ringwald    if not os.path.exists(gcc_folder):
62e6c96737SMatthias Ringwald        os.makedirs(gcc_folder)
63e6c96737SMatthias Ringwald
64e6c96737SMatthias Ringwald    # add rule to generate .h file in src folder if .gatt is present
65e6c96737SMatthias Ringwald    gatt_path = examples_embedded + example + ".gatt"
66e6c96737SMatthias Ringwald    need_h = False
67e6c96737SMatthias Ringwald    if os.path.exists(gatt_path):
68e6c96737SMatthias Ringwald        # create src folder
69e6c96737SMatthias Ringwald        src_folder = apps_folder + "/src/"
70e6c96737SMatthias Ringwald        if not os.path.exists(src_folder):
71e6c96737SMatthias Ringwald            os.makedirs(src_folder)
72e6c96737SMatthias Ringwald        need_h = True
73e6c96737SMatthias Ringwald
74e6c96737SMatthias Ringwald    # copy makefile and update project name
75e6c96737SMatthias Ringwald    with open(gcc_folder + 'Makefile', "wt") as fout:
76e6c96737SMatthias Ringwald        with open(example_template + 'gcc/Makefile', "rt") as fin:
77e6c96737SMatthias Ringwald            for line in fin:
78e6c96737SMatthias Ringwald                fout.write(line.replace('TARGET := EXAMPLE', 'TARGET := ' + example))
79e6c96737SMatthias Ringwald        if (need_h):
80e6c96737SMatthias Ringwald            fout.write(makefile_gatt_add_on.replace("EXAMPLE",example))
81e6c96737SMatthias Ringwald            fout.write("INCLUDES += -I${CONFIG}\n")
82e6c96737SMatthias Ringwald
83e6c96737SMatthias Ringwald    # copy other files
84e6c96737SMatthias Ringwald    for file in ['startup_gcc.c', 'btstack_template.ld']:
85e6c96737SMatthias Ringwald        shutil.copyfile(example_template + "gcc/" + file, apps_folder + "/gcc/" + file);
86e6c96737SMatthias Ringwald
87e6c96737SMatthias Ringwald
88e6c96737SMatthias Ringwald    print("- %s" % example)
89