From 4b543e78cef4e950e2c28339a01ac99373ef40d9 Mon Sep 17 00:00:00 2001 From: lulko Date: Wed, 19 Mar 2025 18:56:00 +0300 Subject: [PATCH] =?UTF-8?q?=D0=92=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6=D0=BD?= =?UTF-8?q?=D0=B0=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D1=88=D0=B8=D0=B2=D0=BA=D0=B8,=20=D0=BD?= =?UTF-8?q?=D0=BE=20=D0=BD=D0=B5=20=D0=BF=D0=B5=D1=80=D0=B5=D1=85=D0=BE?= =?UTF-8?q?=D0=B4=D0=B8=D1=82=20=D0=B2=20=D0=BD=D0=B5=D1=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/fw/embed/bootloader/flash.h | 13 +- controller/fw/embed/bootloader/main.cpp | 80 ++++++--- controller/fw/embed/test/python_test_boot.py | 169 +++++++++++-------- 3 files changed, 160 insertions(+), 102 deletions(-) diff --git a/controller/fw/embed/bootloader/flash.h b/controller/fw/embed/bootloader/flash.h index a8ac795..fe9b413 100644 --- a/controller/fw/embed/bootloader/flash.h +++ b/controller/fw/embed/bootloader/flash.h @@ -13,6 +13,8 @@ enum { addr_id = 0 }; + + #define FLASH_RECORD_SIZE sizeof(FLASH_RECORD) //size flash struct #define PARAM_COUNT 1 // count data in flash @@ -20,10 +22,11 @@ enum { #define FLAG_BOOT 0x08060000 // Адрес хранения флага для обновления прошивки #define UPDATE_FLAG 0xDEADBEEF // Уникальное 32-битное значение -#define APP_ADDRESS 0x08004000 // Адрес основной прошивки -#define BOOT_CAN_ID 0x721 // CAN ID бутлоадера -#define BOOT_CAN_END 0x722 // CAN ID завершения передачи -#define DATA_CAN_ID 0x730 // CAN ID данных +#define APP_ADDRESS 0x08008000 // Адрес основной прошивки +#define BOOT_CAN_ID 0x71 // CAN ID бутлоадера +#define BOOT_CAN_END 0x72 // CAN ID завершения передачи +#define DATA_CAN_ID 0x73 // CAN ID данных +#define ACK_CAN_ID 0x75 // CAN ID подтверждения #define MAX_FW_SIZE 0x3FFF // Макс. размер прошивки (256KB) @@ -37,7 +40,7 @@ enum { #define SECTOR_6 0x08040000 // 128KB #define SECTOR_6_END (SECTOR_6 + 128 * 1024) // sector 6 end -#define SECTOR_7 0x08060000 + // Flash keys for unlocking flash memory diff --git a/controller/fw/embed/bootloader/main.cpp b/controller/fw/embed/bootloader/main.cpp index ee20b35..ae8a7dd 100644 --- a/controller/fw/embed/bootloader/main.cpp +++ b/controller/fw/embed/bootloader/main.cpp @@ -5,11 +5,13 @@ STM32_CAN Can(CAN2, DEF); + volatile bool fw_update = false; volatile uint32_t fw_size = 0; volatile uint32_t fw_crc = 0; -volatile uint32_t write_ptr = APP_ADDRESS; + static FLASH_RECORD flash_record = {0}; +static uint32_t ptr_flash; // Прототипы функций void jump_to_app(); void process_can_message(const CAN_message_t &msg); @@ -20,10 +22,18 @@ void send_ack(uint8_t status); void setup() { // Инициализация периферии + Serial.setRx(HARDWARE_SERIAL_RX_PIN); + Serial.setTx(HARDWARE_SERIAL_TX_PIN); Serial.begin(115200); - Can.begin(1000000); // 1 Mbps - Can.setFilter(0,BOOT_CAN_ID,STD); - + Can.begin(); + Can.setBaudRate(1000000); + TIM_TypeDef *Instance = TIM2; + HardwareTimer *SendTimer = new HardwareTimer(Instance); + SendTimer->setOverflow(100, HERTZ_FORMAT); // 50 Hz + // SendTimer->attachInterrupt(process_can_message); + SendTimer->resume(); + // Разрешить все ID (маска 0x00000000) + Can.setFilter(0, 0, STD); // Настройка GPIO RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; @@ -31,17 +41,18 @@ void setup() { GPIOC->ODR |= GPIO_ODR_OD11; // Проверка флага обновления - /*erase_sector(6); + /* erase_sector(6); flash_program_word(FLAG_BOOT,0xDEADBEEF,0); flash_record.data_id = addr_id; flash_record.crc = 0x6933; flash_record.value = 0x69; - flash_record.write_ptr_now = SECTOR_6;*/ - flash_write(SECTOR_6, &flash_record); - flash_record = load_params(); + flash_record.write_ptr_now = SECTOR_6; + flash_write(SECTOR_6, &flash_record);*/ + flash_record = load_params(); /* Добавить проверку адреса, т.е во время отправки запроса прошивки по CAN мы сохраняем как флаг, так и аддрес устройства к которому будет обращатлься во время прошивки */ + if(*(volatile uint32_t*)(FLAG_BOOT) == UPDATE_FLAG) { fw_update = true; GPIOC->ODR |= GPIO_ODR_OD10; // Индикация обновления @@ -54,11 +65,14 @@ void setup() { void loop() { if(fw_update) { + GPIOC->ODR ^= GPIO_ODR_OD10; + HAL_Delay(100); CAN_message_t msg; - if(Can.read(msg)) { + while(Can.read(msg)) { process_can_message(msg); } } + } void process_can_message(const CAN_message_t &msg) { @@ -66,16 +80,16 @@ void process_can_message(const CAN_message_t &msg) { case BOOT_CAN_ID: if(msg.buf[0] == 0x01) { // Старт передачи fw_size = *(uint32_t*)&msg.buf[1]; //размер прошивки тип 4 байта - fw_crc = *(uint32_t*)&msg.buf[5]; //crc - write_ptr = APP_ADDRESS; + fw_crc = *(uint16_t*)&msg.buf[5]; //crc + ptr_flash = APP_ADDRESS; send_ack(0x01); } break; case DATA_CAN_ID: // Пакет данных - if(write_ptr < (APP_ADDRESS + fw_size)) { + if(ptr_flash < (APP_ADDRESS + fw_size)) { write_flash_page((const uint8_t*)msg.buf, msg.len); - write_ptr += msg.len; + ptr_flash += msg.len; send_ack(0x02); } break; @@ -86,26 +100,32 @@ void process_can_message(const CAN_message_t &msg) { send_ack(0xAA); NVIC_SystemReset(); } else { + erase_sector(7); // Сброс флага send_ack(0x55); + NVIC_SystemReset(); } break; } } void jump_to_app() { + volatile uint32_t* addr; + uint32_t appjump_addr; typedef void (*app_entry_t)(void); - auto app_entry = (app_entry_t)(*(volatile uint32_t*)(APP_ADDRESS + 4)); + addr = (__IO uint32_t*)APP_ADDRESS; + appjump_addr = (uint32_t)addr; + app_entry_t app_entry = (app_entry_t)appjump_addr; // SCB->VTOR = APP_ADDRESS; - __set_MSP(*(volatile uint32_t*)APP_ADDRESS); + __set_MSP(appjump_addr); app_entry(); } void erase_flash_pages() { FLASH_EraseInitTypeDef erase; erase.TypeErase = FLASH_TYPEERASE_SECTORS; - erase.Sector = FLASH_SECTOR_1; - erase.NbSectors = 5; + erase.Sector = FLASH_SECTOR_2; + erase.NbSectors = 4; erase.VoltageRange = FLASH_VOLTAGE_RANGE_3; uint32_t error; @@ -117,30 +137,38 @@ void erase_flash_pages() { // CRC16 implementation for STM32 uint16_t CalculateCRC16(const uint8_t* data, uint32_t length) { - uint16_t crc = 0xFFFF; + uint16_t crc = 0xFFFF; // Начальное значение while (length--) { - crc ^= (uint16_t)(*data++) << 8; - for(uint8_t i = 0; i < 8; i++) { - crc = crc & 0x8000 ? (crc << 1) ^ 0x8005 : crc << 1; + crc ^= *data++; // Обрабатываем LSB первым + for (uint8_t i = 0; i < 8; i++) { + if (crc & 0x0001) { // Проверяем младший бит + crc = (crc >> 1) ^ 0xA001; // Полином 0x8005 (reverse) + } else { + crc >>= 1; + } } } - return crc; + return crc; // Финальный XOR = 0x0000 (не требуется) } + bool verify_firmware() { uint32_t calculated_crc = 0; - calculated_crc = CalculateCRC16((uint8_t*)fw_crc,fw_size); + calculated_crc = CalculateCRC16((uint8_t*)APP_ADDRESS,fw_size); if(calculated_crc != (uint16_t)fw_crc) return false; // Реализация проверки CRC // ... // return (calculated_crc == fw_crc); - return true; + else + return true; } void send_ack(uint8_t status) { + CAN_message_t ack; - ack.id = BOOT_CAN_ID + 2; + ack.id = ACK_CAN_ID; ack.len = 1; ack.buf[0] = status; Can.write(ack); -} \ No newline at end of file +} + diff --git a/controller/fw/embed/test/python_test_boot.py b/controller/fw/embed/test/python_test_boot.py index b8d3124..918095f 100644 --- a/controller/fw/embed/test/python_test_boot.py +++ b/controller/fw/embed/test/python_test_boot.py @@ -2,94 +2,119 @@ import can import time from intelhex import IntelHex from crc import Calculator, Crc16 - -# Конфигурация CAN +# Конфигурация CAN_CHANNEL = 'socketcan' CAN_INTERFACE = 'can0' CAN_BITRATE = 1000000 -# Параметры из заголовочного файла -BOOT_CAN_ID = 0x721 -DATA_CAN_ID = 0x730 -BOOT_CAN_END = 0x722 -ACK_CAN_ID = 0x723 +BOOT_CAN_ID = 0x71 +DATA_CAN_ID = 0x73 +BOOT_CAN_END = 0x72 +ACK_CAN_ID = 0x75 -# Конфигурация CRC16 -CRC16_POLYNOMIAL = 0x8005 # Стандартный полином CRC-16-IBM -CRC16_INIT = 0xFFFF +#конфиг для crc16 ibm + + + +def debug_print(msg): + print(f"[DEBUG] {msg}") def send_firmware(hex_file): - bus = can.interface.Bus(channel='can0', - bustype='socketcan') - # Чтение и преобразование HEX-файла - ih = IntelHex(hex_file) - binary_data = ih.tobinarray() - fw_size = len(binary_data) - - # Расчет CRC16 - # calculator = Calculator(Crc16.CCITT, optimize=True) - fw_crc = 0x6933 - - # Отправка команды START - start_data = bytearray([0x01]) - start_data += fw_size.to_bytes(4, 'little') - start_data += fw_crc.to_bytes(2, 'little') # 2 байта для CRC16 - - start_msg = can.Message( - arbitration_id=BOOT_CAN_ID, - data=start_data, - is_extended_id=False - ) - - bus.send(start_msg) - - # Ожидание подтверждения - ack = wait_for_ack(bus) - if not ack or ack.data[0] != 0x01: - print("Ошибка инициализации!") - bus.shutdown() - return - - # Отправка данных - packet_size = 8 - for i in range(0, len(binary_data), packet_size): - chunk = binary_data[i:i+packet_size] - chunk += b'\x00' * (8 - len(chunk)) + try: + debug_print("Инициализация CAN...") + bus = can.interface.Bus( + channel=CAN_INTERFACE, + bustype=CAN_CHANNEL, + bitrate=CAN_BITRATE + ) - data_msg = can.Message( - arbitration_id=DATA_CAN_ID, - data=chunk, + debug_print("Чтение HEX-файла...") + ih = IntelHex(hex_file) + binary_data = ih.tobinstr() # Исправлено на tobinstr() + fw_size = len(binary_data) + debug_print(f"Размер прошивки: {fw_size} байт") + + # Расчет CRC + debug_print("Расчёт CRC...") + calculator = Calculator(Crc16.IBM) + fw_crc = calculator.checksum(binary_data) + debug_print(f"CRC: 0x{fw_crc:04X}") + + # Отправка START + start_data = bytearray([0x01]) + start_data += fw_size.to_bytes(4, 'little') + start_data += fw_crc.to_bytes(2, 'little') + + debug_print(f"START: {list(start_data)}") + start_msg = can.Message( + arbitration_id=BOOT_CAN_ID, + data=bytes(start_data), is_extended_id=False ) - bus.send(data_msg) + try: + bus.send(start_msg) + except can.CanError as e: + debug_print(f"Ошибка отправки START: {str(e)}") + return + + # Ожидание ACK + debug_print("Ожидание ACK...") ack = wait_for_ack(bus) - if not ack or ack.data[0] != 0x02: - print("Ошибка передачи данных!") - break + if not ack: + debug_print("Таймаут ACK START") + return + debug_print(f"Получен ACK: {list(ack.data)}") + + # Отправка данных + packet_size = 8 + for i in range(0, len(binary_data), packet_size): + chunk = binary_data[i:i+packet_size] + # Дополнение до 8 байт + if len(chunk) < 8: + chunk += b'\xFF' * (8 - len(chunk)) - progress = (i + len(chunk)) / fw_size * 100 - print(f"\rПрогресс: {progress:.1f}%", end='') + debug_print(f"Пакет {i//8}: {list(chunk)}") + data_msg = can.Message( + arbitration_id=DATA_CAN_ID, + data=chunk, + is_extended_id=False + ) + + try: + bus.send(data_msg) + except can.CanError as e: + debug_print(f"Ошибка отправки данных: {str(e)}") + return - # Завершение передачи - finish_msg = can.Message( - arbitration_id=BOOT_CAN_END, - data=[0xAA], - is_extended_id=False - ) - bus.send(finish_msg) - - ack = wait_for_ack(bus, timeout=5) - if ack and ack.data[0] == 0xAA: - print("\nПрошивка успешно загружена!") - else: - print("\nОшибка верификации!") + ack = wait_for_ack(bus) + if not ack: + debug_print("Таймаут ACK DATA") + return - bus.shutdown() + # Финал + debug_print("Отправка FINISH...") + finish_msg = can.Message( + arbitration_id=BOOT_CAN_END, + data=bytes([0xAA]), + is_extended_id=False + ) + bus.send(finish_msg) + + ack = wait_for_ack(bus, timeout=5) + if ack == 0xAA: + debug_print("Прошивка подтверждена!") + else: + debug_print("Ошибка верификации!") + + except Exception as e: + debug_print(f"Критическая ошибка: {str(e)}") + finally: + bus.shutdown() def wait_for_ack(bus, timeout=1.0): start_time = time.time() while time.time() - start_time < timeout: - msg = bus.recv(timeout=0.1) + msg = bus.recv(timeout=0) # Неблокирующий режим if msg and msg.arbitration_id == ACK_CAN_ID: return msg return None @@ -97,5 +122,7 @@ def wait_for_ack(bus, timeout=1.0): if __name__ == "__main__": import sys if len(sys.argv) != 2: - print("Использование: python can_flasher.py firmware.hex") + print("Использование: sudo python3 can_flasher.py firmware.hex") sys.exit(1) + + send_firmware(sys.argv[1])