LNSC-2420-Firmware/utils/config_to_hex.py

70 lines
2.4 KiB
Python
Executable file

#!/usr/bin/env python3
import sys
import struct
import yaml
from intelhex import IntelHex
BASE_ADDR = 0x08007C00
CALIBRATION_KEY_TO_OFFSET = {
"CAL_FACTOR_U_BAT": 0x0000,
"CAL_FACTOR_U_SOLAR": 0x0002,
"CAL_FACTOR_U_SW": 0x0004,
"CAL_FACTOR_I_SOLAR": 0x0006,
"CAL_FACTOR_I_LOAD": 0x0008,
}
CONFIG_KEY_TO_OFFSET = {
"U_BAT_REGULATION_CORRIDOR": 0x0100,
"U_BAT_INITIAL_FULL": 0x0104,
"U_BAT_INITIAL_HOLD_CANCEL": 0x0108,
"INITIAL_CHARGE_HOLD_TIME": 0x010C,
"INITIAL_TO_FLOAT_TRANSITION_TIME": 0x0110,
"U_BAT_FLOAT_FULL": 0x0114,
"SLEEP_SOLAR_EXCESS_VOLTAGE": 0x0118,
"SLEEP_SOLAR_CURRENT": 0x011C,
"SLEEP_STATE_DELAY": 0x0120,
"SLEEP_SWITCH_DELAY": 0x0124,
"INTERNAL_TEMPERATURE_LIMIT": 0x0128,
"INTERNAL_TEMPERATURE_RECOVERY": 0x012C,
"U_BAT_LOAD_ON": 0x0130,
"U_BAT_LOAD_OFF": 0x0134,
"LOAD_CURRENT_LIMIT_MA": 0x0138,
"LOAD_CURRENT_INRUSH_TIME": 0x013C,
"MIN_CHARGE_PUMP_EXCESS_VOLTAGE": 0x0140,
"LOAD_ON_DELAY": 0x0144,
"AVG_ALPHA_I_SOLAR": 0x0148,
"AVG_ALPHA_I_LOAD": 0x014C,
"AVG_ALPHA_U_BAT": 0x0150,
"AVG_ALPHA_U_SW": 0x0154,
"AVG_ALPHA_U_SOLAR": 0x0158,
"AVG_ALPHA_TEMP": 0x015C,
"DEEPSLEEP_DELAY": 0x0160,
"DEEPSLEEP_DURATION": 0x0164,
}
if __name__ == "__main__":
output = IntelHex()
if len(sys.argv) < 3:
print(f"usage: {sys.argv[0]} <yaml-config> <output-hex>")
sys.exit(1)
with open(sys.argv[1], 'r') as configfile:
config = yaml.safe_load(configfile)
for key, offset in CALIBRATION_KEY_TO_OFFSET.items():
value = config['calibration'][key]
enc_bytes = struct.pack('<H', value)
output.puts(BASE_ADDR + offset, enc_bytes)
for key, offset in CONFIG_KEY_TO_OFFSET.items():
value = config['config'][key]
enc_bytes = struct.pack('<I', value)
output.puts(BASE_ADDR + offset, enc_bytes)
output.write_hex_file(sys.argv[2])