Добавлена адресация к бутлоадеру
This commit is contained in:
parent
a1bcbdb33b
commit
cf1c6eb05c
4 changed files with 50 additions and 21 deletions
|
@ -1,4 +1,5 @@
|
|||
import can
|
||||
import sys
|
||||
import time
|
||||
from intelhex import IntelHex
|
||||
from crc import Calculator, Crc16
|
||||
|
@ -6,10 +7,12 @@ from crc import Calculator, Crc16
|
|||
CAN_CHANNEL = 'socketcan'
|
||||
CAN_INTERFACE = 'can0'
|
||||
CAN_BITRATE = 1000000
|
||||
BOOT_CAN_ID = 0x71
|
||||
DATA_CAN_ID = 0x73
|
||||
BOOT_CAN_END = 0x72
|
||||
ACK_CAN_ID = 0x75
|
||||
#ch =int(input("Введите id устройства:"))
|
||||
ch = int(sys.argv[2])
|
||||
BOOT_CAN_ID = (ch * 16) + 1
|
||||
DATA_CAN_ID = (ch * 16) + 3
|
||||
BOOT_CAN_END = (ch * 16) + 2
|
||||
ACK_CAN_ID = 0x05
|
||||
|
||||
#конфиг для crc16 ibm
|
||||
|
||||
|
@ -18,6 +21,17 @@ ACK_CAN_ID = 0x75
|
|||
def debug_print(msg):
|
||||
print(f"[DEBUG] {msg}")
|
||||
|
||||
def calculate_crc16_modbus(data: bytes) -> int:
|
||||
crc = 0xFFFF
|
||||
for byte in data:
|
||||
crc ^= byte
|
||||
for _ in range(8):
|
||||
if crc & 0x0001:
|
||||
crc = (crc >> 1) ^ 0xA001
|
||||
else:
|
||||
crc >>= 1
|
||||
return crc
|
||||
|
||||
def send_firmware(hex_file):
|
||||
try:
|
||||
debug_print("Инициализация CAN...")
|
||||
|
@ -36,7 +50,7 @@ def send_firmware(hex_file):
|
|||
# Расчет CRC
|
||||
debug_print("Расчёт CRC...")
|
||||
calculator = Calculator(Crc16.IBM)
|
||||
fw_crc = calculator.checksum(binary_data)
|
||||
fw_crc = calculate_crc16_modbus(binary_data)
|
||||
debug_print(f"CRC: 0x{fw_crc:04X}")
|
||||
|
||||
# Отправка START
|
||||
|
@ -100,8 +114,8 @@ def send_firmware(hex_file):
|
|||
)
|
||||
bus.send(finish_msg)
|
||||
|
||||
ack = wait_for_ack(bus, timeout=5)
|
||||
if ack == 0xAA:
|
||||
ack = wait_for_ack(bus, timeout=3)
|
||||
if ack and ack.data[0] == 0xAA:
|
||||
debug_print("Прошивка подтверждена!")
|
||||
else:
|
||||
debug_print("Ошибка верификации!")
|
||||
|
@ -121,7 +135,7 @@ def wait_for_ack(bus, timeout=1.0):
|
|||
|
||||
if __name__ == "__main__":
|
||||
import sys
|
||||
if len(sys.argv) != 2:
|
||||
if len(sys.argv) != 3:
|
||||
print("Использование: sudo python3 can_flasher.py firmware.hex")
|
||||
sys.exit(1)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue