Compare commits

..
2 Commits
Author SHA1 Message Date
Gatsby 79f59d15f7 feat: write RISC-V binary file with weakly linked, overridable symbols
- Adds functionality to write RISC-V binary files using weak symbols, allowing overrides in linked projects
- Supports flexible integration where default binary data can be replaced by custom definitions if needed
2024-10-31 20:00:42 +08:00
Gatsby a7cf13b37a feat: add Python script for generating RISC-V binary as C code 2024-10-31 19:58:23 +08:00
5 changed files with 99 additions and 5 deletions
+1
View File
@@ -1,6 +1,7 @@
# Buid files
build/
*.tar.gz
cx9261a_riscv_bin.c
# System generated files
# -- macOS
+11 -5
View File
@@ -26,6 +26,7 @@ CFLAGS_DEBUG := $(CFLAGS) $(DEBUG_FLAGS)
CFLAGS_RELEASE := $(CFLAGS) $(RELEASE_FLAGS)
# Collect source files
BIN_SRC := $(CURDIR)/cx9261a_riscv_bin.c
SRCS := $(CURDIR)/cx9261adriver_c.cpp $(CURDIR)/cx9261A_api/cx9261adriver.cpp
TEST_SRC := ld_test.c
PACKAGE_HEADERS := ./cx9261adriver_c.h
@@ -33,6 +34,9 @@ PACKAGE_HEADERS := ./cx9261adriver_c.h
# Default target
all: $(foreach arch, $(ARCHS), $(BUILD_DIR)/$(arch)/lib$(PROJECT_NAME)_dbg.a $(BUILD_DIR)/$(arch)/lib$(PROJECT_NAME).a)
$(BIN_SRC): $(CURDIR)/cx9261A_api/MultiPointsCalib_v1.7.bin
$(CURDIR)/tools/gen_riscvbin.py $< -o $(CURDIR)
# Rule to create debug libraries for different architectures
$(BUILD_DIR)/%/lib$(PROJECT_NAME)_dbg.a: $(SRCS)
@mkdir -p $(BUILD_DIR)/$*/debug
@@ -48,21 +52,22 @@ $(BUILD_DIR)/%/lib$(PROJECT_NAME).a: $(SRCS)
# Clean up
clean:
rm -rf $(BUILD_DIR) *.tar.gz
rm -rf $(CURDIR)/cx9261a_riscv_bin.c
# Run link tests
test: all $(foreach arch, $(ARCHS), $(BUILD_DIR)/$(arch)/ld_test $(BUILD_DIR)/$(arch)/ld_test_dbg)
@echo "Link test success!"
# Debug link tests
$(BUILD_DIR)/%/ld_test_dbg: $(SRCS) $(TEST_SRC)
@$(CC) $(CFLAGS_DEBUG) -mcpu=$* ld_test.c -o $@ -L$(BUILD_DIR)/$* -l$(PROJECT_NAME)_dbg -lstdc++ -lm -specs=nosys.specs
$(BUILD_DIR)/%/ld_test_dbg: $(SRCS) $(TEST_SRC) $(BIN_SRC)
@$(CC) $(CFLAGS_DEBUG) -mcpu=$* ld_test.c $(BIN_SRC) -o $@ -L$(BUILD_DIR)/$* -l$(PROJECT_NAME)_dbg -lstdc++ -lm -specs=nosys.specs
# Release link tests
$(BUILD_DIR)/%/ld_test: $(SRCS) $(TEST_SRC)
@$(CC) $(CFLAGS_RELEASE) -mcpu=$* ld_test.c -o $@ -L$(BUILD_DIR)/$* -l$(PROJECT_NAME) -lstdc++ -lm -specs=nosys.specs
$(BUILD_DIR)/%/ld_test: $(SRCS) $(TEST_SRC) $(BIN_SRC)
@$(CC) $(CFLAGS_RELEASE) -mcpu=$* ld_test.c $(BIN_SRC) -o $@ -L$(BUILD_DIR)/$* -l$(PROJECT_NAME) -lstdc++ -lm -specs=nosys.specs
# Package for release - bundle debug and release versions for each architecture separately
package: all
package: all $(BIN_SRC)
@echo "Packaging for release..."
@$(foreach arch, $(ARCHS), \
mkdir -p release_$(arch)/lib; \
@@ -72,6 +77,7 @@ package: all
cp $(BUILD_DIR)/$(arch)/lib$(PROJECT_NAME).a release_$(arch)/lib/; \
cp $(PACKAGE_HEADERS) release_$(arch)/include/; \
cp $(SRCS) release_$(arch)/dbg/cx9261a_c/; \
cp $(CURDIR)/cx9261a_riscv_bin.c release_$(arch)/; \
tar -czvf release_$(arch).tar.gz -C release_$(arch) .; \
rm -rf release_$(arch); \
)
Binary file not shown.
+14
View File
@@ -5,6 +5,17 @@
#include "cx9261adriver.h"
#include "utils.h"
#ifdef __cplusplus
extern "C" {
#endif
__attribute__((weak)) uint32_t CX9261A_RiscvBinLen = 0;
__attribute__((weak)) uint8_t *CX9261A_RiscvBin = nullptr;
#ifdef __cplusplus
}
#endif
namespace CX9261A {
void initialChip()
@@ -672,6 +683,9 @@ void configRISCV(const bool on)
if (on) {
writeSpi(0x071, 0x00);
writeSpi(0x071, 0x29);
for (size_t i = 0; i < CX9261A_RiscvBinLen; i++) {
writeSpi(0x070, CX9261A_RiscvBin[i]);
}
writeSpi(0x236, 0x0F);
writeSpi(0x20A, 0x01);
writeSpi(0x071, 0x26);
+73
View File
@@ -0,0 +1,73 @@
#!/usr/bin/env python3
import argparse
import os
import glob
from datetime import datetime
def convert_bin_to_c(bin_file, output_dir):
# Read the bin file
with open(bin_file, 'rb') as f:
bin_data = f.read()
# Get the length of the bin file
bin_len = len(bin_data)
# Set the output file path within the specified directory
output_file = os.path.join(output_dir, 'cx9261a_riscv_bin.c')
with open(output_file, 'w') as f:
# Write metadata comments
f.write(f'// Generated C code array\n')
f.write(f'// Date: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}\n')
f.write(f'// Source file: {os.path.basename(bin_file)}\n\n')
# Write headers and data length
f.write('#include <stdint.h>\n')
f.write('#include "cx9261adriver_c.h"\n\n')
f.write(f'uint32_t CX9261A_RiscvBinLen = {bin_len};\n')
f.write(f'uint8_t *CX9261A_RiscvBin = (uint8_t[]){{\n\t')
# Write data as hex values in the array
for i, byte in enumerate(bin_data):
f.write(f'0x{byte:02X}, ')
# Add a newline every 12 bytes
if (i + 1) % 12 == 0:
f.write('\n\t')
# Add a newline if the last line has fewer than 12 bytes
if bin_len % 12 != 0:
f.write('\n')
# End the array
f.write('};\n')
print(f"Generated C file: {output_file}")
if __name__ == "__main__":
# Set up command line argument parsing
parser = argparse.ArgumentParser(description="Convert a CX9261A Riscv bin file to a C code array.")
parser.add_argument("bin_file", type=str, nargs='?', help="Path to the bin file (optional)")
parser.add_argument("-o", "--output_dir", type=str, default=".", help="Output directory (default is the current directory)")
# Parse arguments
args = parser.parse_args()
# If no bin file is provided, search for a .bin file in the current directory
if args.bin_file is None:
bin_files = glob.glob("*.bin")
if len(bin_files) == 0:
print("Error: No .bin file found in the current directory.")
exit(1)
elif len(bin_files) > 1:
print("Error: Multiple .bin files found in the current directory. Specify one as an argument.")
exit(1)
else:
args.bin_file = bin_files[0]
# Check if output directory exists, create if not
if not os.path.isdir(args.output_dir):
os.makedirs(args.output_dir)
# Perform the conversion
convert_bin_to_c(args.bin_file, args.output_dir)