-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BHI260AP] Added firmware to header tool
- Loading branch information
Showing
2 changed files
with
142 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
/** | ||
* Copyright (c) 2023 Bosch Sensortec GmbH. All rights reserved. | ||
* | ||
* BSD-3-Clause | ||
* | ||
* Redistribution and use in source and binary forms, with or without | ||
* modification, are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright | ||
* notice, this list of conditions and the following disclaimer. | ||
* | ||
* 2. Redistributions in binary form must reproduce the above copyright | ||
* notice, this list of conditions and the following disclaimer in the | ||
* documentation and/or other materials provided with the distribution. | ||
* | ||
* 3. Neither the name of the copyright holder nor the names of its | ||
* contributors may be used to endorse or promote products derived from | ||
* this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | ||
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | ||
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | ||
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | ||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | ||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | ||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING | ||
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
* POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* @file fw2h.c | ||
* @brief Firmware to Header example for the BHI260/BHA260 | ||
* | ||
*/ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <stdint.h> | ||
|
||
#define N_ROWS 12 | ||
|
||
/*lint -e818 suppressing parameter(argv) could be declared as pointer to constant info as the info is not addressed | ||
* after declaring the parameter as pointer to constant */ | ||
int main(int argc, char *argv[]) | ||
{ | ||
FILE *input_file, *output_file; | ||
char output_file_name[256]; /* Limit file name to 256 characters */ | ||
unsigned char fw[N_ROWS]; | ||
|
||
printf("Utility to convert binary files to .h\n"); | ||
if (argc == 1) | ||
{ | ||
printf("Pass a firmware file as an argument. Exiting\n"); | ||
exit(-1); | ||
} | ||
else | ||
{ | ||
for (int i = 1; i < argc; i++) | ||
{ | ||
input_file = fopen(argv[i], "rb"); | ||
if (input_file) | ||
{ | ||
sprintf(output_file_name, "%s" ".h", (char *)argv[i]); | ||
printf("Copying firmware to %s\n", output_file_name); | ||
output_file = fopen(output_file_name, "w"); | ||
if (output_file) | ||
{ | ||
fprintf(output_file, "const unsigned char bhy2_firmware_image[] = {\n"); | ||
unsigned char bytes_read = 0; | ||
unsigned char j = 0; | ||
do | ||
{ | ||
bytes_read = (uint8_t)fread(fw, 1, N_ROWS, input_file); | ||
if (bytes_read) | ||
{ | ||
fprintf(output_file, " "); | ||
for (j = 0; j < bytes_read; j++) | ||
{ | ||
fprintf(output_file, "0x%02x, ", fw[j]); | ||
} | ||
|
||
fprintf(output_file, "\n"); | ||
} | ||
} while (bytes_read != 0); | ||
fprintf(output_file, "};\n"); | ||
fclose(output_file); | ||
} | ||
else | ||
{ | ||
printf("Could not create %s\n", output_file_name); | ||
} | ||
} | ||
else | ||
{ | ||
printf("Could not open %s.\n", argv[i]); | ||
} | ||
|
||
fclose(input_file); | ||
} | ||
} | ||
} | ||
|
||
/*lint +e818*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
''' | ||
* @file fw2h.py | ||
* @author Lewis He (lewishe@outlook.com) | ||
* @date 2024-07-24 | ||
''' | ||
|
||
import sys | ||
import struct | ||
|
||
def convert_binary_to_header(input_file_path): | ||
output_file_name = 'py_' + input_file_path + ".h" | ||
print(f"Utility to convert binary files to .h") | ||
print(f"Copying firmware to {output_file_name}") | ||
|
||
with open(input_file_path, "rb") as input_file, open(output_file_name, "w") as output_file: | ||
output_file.write("const unsigned char bhy2_firmware_image[] = {\n") | ||
fw = bytearray(12) # Assuming 12 rows as in the C program | ||
bytes_read = 0 | ||
|
||
while True: | ||
bytes_read = input_file.readinto(fw) | ||
if not bytes_read: | ||
break | ||
|
||
output_file.write(" ") | ||
for j in range(bytes_read): | ||
output_file.write(f"0x{fw[j]:02x}, ") | ||
output_file.write("\n") | ||
|
||
output_file.write("};\n") | ||
|
||
if __name__ == "__main__": | ||
if len(sys.argv) == 1: | ||
print("Pass a firmware file as an argument. Exiting") | ||
sys.exit(-1) | ||
|
||
for i in range(1, len(sys.argv)): | ||
convert_binary_to_header(sys.argv[i]) |