Roman Okhrimenko | 977b375 | 2022-03-31 14:40:48 +0300 | [diff] [blame^] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | import argparse |
| 6 | import binascii |
| 7 | |
| 8 | number_of_columns = 16 |
| 9 | |
| 10 | copyright = """/***************************************************************************//** |
| 11 | * \\file {} |
| 12 | * |
| 13 | * \\brief |
| 14 | * Cortex-M0+ prebuilt application image. |
| 15 | * |
| 16 | ******************************************************************************** |
| 17 | * \\copyright |
| 18 | * Copyright (c) 2018-2019 Cypress Semiconductor Corporation |
| 19 | * SPDX-License-Identifier: LicenseRef-PBL |
| 20 | * |
| 21 | * Licensed under the Permissive Binary License |
| 22 | *******************************************************************************/ |
| 23 | |
| 24 | """ |
| 25 | |
| 26 | header = """ |
| 27 | #if defined(__APPLE__) && defined(__clang__) |
| 28 | __attribute__ ((__section__("__CY_M0P_IMAGE,__cy_m0p_image"), used)) |
| 29 | #elif defined(__GNUC__) || defined(__ARMCC_VERSION) |
| 30 | __attribute__ ((__section__(".cy_m0p_image"), used)) |
| 31 | #elif defined(__ICCARM__) |
| 32 | #pragma location=".cy_m0p_image" |
| 33 | #else |
| 34 | #error "An unsupported toolchain" |
| 35 | #endif |
| 36 | const uint8_t cy_m0p_image[] = { |
| 37 | """ |
| 38 | |
| 39 | c_list = [] |
| 40 | |
| 41 | |
| 42 | # Get component name from the command line |
| 43 | parser = argparse.ArgumentParser() |
| 44 | parser.add_argument("bin_path", help="Specify path the input binary file to be converted to a C array.") |
| 45 | parser.add_argument("c_path", help="Specify path the output C file with the C array.") |
| 46 | parser.add_argument("c_macro", help="Specify the C preprocessor macro to wrap the variable.") |
| 47 | args = parser.parse_args() |
| 48 | bin_path = args.bin_path |
| 49 | c_path = args.c_path |
| 50 | c_macro = args.c_macro |
| 51 | |
| 52 | c_file=os.path.basename(c_path) |
| 53 | |
| 54 | f = open(bin_path, "rb") |
| 55 | data = list(f.read()) |
| 56 | |
| 57 | with open(c_path, "w") as c_fd: |
| 58 | |
| 59 | # Clear content of the file |
| 60 | c_fd.seek(0) |
| 61 | c_fd.truncate() |
| 62 | |
| 63 | # Write copyright |
| 64 | c_fd.write(copyright.format(c_file)) |
| 65 | |
| 66 | # Include headers |
| 67 | c_fd.write("#include <stdint.h>\n") |
| 68 | c_fd.write("#include \"cy_device_headers.h\"\n") |
| 69 | c_fd.write("\n") |
| 70 | |
| 71 | # Open conditional block |
| 72 | if c_macro: |
| 73 | c_fd.write("#if defined(" + c_macro + ")\n") |
| 74 | |
| 75 | # Write template |
| 76 | c_fd.write(header) |
| 77 | |
| 78 | # Generate list of the data bytes |
| 79 | for n in data: |
| 80 | c_list.append(format(n, '#04x')) |
| 81 | |
| 82 | for i in range(int(len(c_list) / number_of_columns) + 1): |
| 83 | line_list = c_list[i * number_of_columns: (i + 1) * number_of_columns] |
| 84 | c_fd.write(" ") |
| 85 | for item in line_list: |
| 86 | c_fd.write(" %su," % item) |
| 87 | c_fd.write("\n") |
| 88 | |
| 89 | c_fd.write("};\n") |
| 90 | |
| 91 | # Close conditional block |
| 92 | if c_macro: |
| 93 | c_fd.write("#endif /* defined(" + c_macro + ") */") |
| 94 | c_fd.write("\n") |
| 95 | f.close() |