blob: 04bc67add129b1f1de08d2321532070e49ddf287 [file] [log] [blame]
Roman Okhrimenko977b3752022-03-31 14:40:48 +03001#!/usr/bin/env python
2
3import os
4import sys
5import argparse
6import binascii
7
8number_of_columns = 16
9
10copyright = """/***************************************************************************//**
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
26header = """
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
36const uint8_t cy_m0p_image[] = {
37"""
38
39c_list = []
40
41
42# Get component name from the command line
43parser = argparse.ArgumentParser()
44parser.add_argument("bin_path", help="Specify path the input binary file to be converted to a C array.")
45parser.add_argument("c_path", help="Specify path the output C file with the C array.")
46parser.add_argument("c_macro", help="Specify the C preprocessor macro to wrap the variable.")
47args = parser.parse_args()
48bin_path = args.bin_path
49c_path = args.c_path
50c_macro = args.c_macro
51
52c_file=os.path.basename(c_path)
53
54f = open(bin_path, "rb")
55data = list(f.read())
56
57with 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")
95f.close()