Bootloader CAN work

This commit is contained in:
lulko 2025-03-21 14:00:00 +03:00
parent 4b543e78ce
commit a1bcbdb33b

View file

@ -9,14 +9,13 @@ STM32_CAN Can(CAN2, DEF);
volatile bool fw_update = false; volatile bool fw_update = false;
volatile uint32_t fw_size = 0; volatile uint32_t fw_size = 0;
volatile uint32_t fw_crc = 0; volatile uint32_t fw_crc = 0;
volatile uint32_t jump;
static FLASH_RECORD flash_record = {0}; static FLASH_RECORD flash_record = {0};
static uint32_t ptr_flash; static uint32_t ptr_flash;
// Прототипы функций // Прототипы функций
void jump_to_app(); void jump_to_app();
void process_can_message(const CAN_message_t &msg); void process_can_message(const CAN_message_t &msg);
void erase_flash_pages(); void erase_flash_pages();
bool verify_firmware(); bool verify_firmware();
void send_ack(uint8_t status); void send_ack(uint8_t status);
@ -41,13 +40,13 @@ void setup() {
GPIOC->ODR |= GPIO_ODR_OD11; GPIOC->ODR |= GPIO_ODR_OD11;
// Проверка флага обновления // Проверка флага обновления
/* erase_sector(6); // erase_sector(6);
flash_program_word(FLAG_BOOT,0xDEADBEEF,0); // flash_program_word(FLAG_BOOT,0xDEADBEEF,0);
flash_record.data_id = addr_id; // flash_record.data_id = addr_id;
flash_record.crc = 0x6933; // flash_record.crc = 0x6933;
flash_record.value = 0x69; // flash_record.value = 0x69;
flash_record.write_ptr_now = SECTOR_6; // flash_record.write_ptr_now = SECTOR_6;
flash_write(SECTOR_6, &flash_record);*/ // flash_write(SECTOR_6, &flash_record);
flash_record = load_params(); flash_record = load_params();
/* Добавить проверку адреса, т.е во время отправки запроса прошивки по CAN /* Добавить проверку адреса, т.е во время отправки запроса прошивки по CAN
мы сохраняем как флаг, так и аддрес устройства к которому будет обращатлься во мы сохраняем как флаг, так и аддрес устройства к которому будет обращатлься во
@ -66,14 +65,14 @@ void setup() {
void loop() { void loop() {
if(fw_update) { if(fw_update) {
GPIOC->ODR ^= GPIO_ODR_OD10; GPIOC->ODR ^= GPIO_ODR_OD10;
HAL_Delay(100); // HAL_Delay(100);
CAN_message_t msg; CAN_message_t msg;
while(Can.read(msg)) { while(Can.read(msg))
process_can_message(msg); process_can_message(msg);
} }
} }
}
void process_can_message(const CAN_message_t &msg) { void process_can_message(const CAN_message_t &msg) {
switch(msg.id) { switch(msg.id) {
@ -91,33 +90,38 @@ void process_can_message(const CAN_message_t &msg) {
write_flash_page((const uint8_t*)msg.buf, msg.len); write_flash_page((const uint8_t*)msg.buf, msg.len);
ptr_flash += msg.len; ptr_flash += msg.len;
send_ack(0x02); send_ack(0x02);
} }
break; break;
case BOOT_CAN_END: // Завершение передачи case BOOT_CAN_END: // Завершение передачи
if(verify_firmware()) { if(verify_firmware()) {
erase_sector(7); // Сброс флага erase_sector(7); // Сброс флага
send_ack(0xAA); send_ack(0xAA);
NVIC_SystemReset(); NVIC_SystemReset();
} else { } else {
erase_sector(7); // Сброс флага
send_ack(0x55); send_ack(0x55);
NVIC_SystemReset();
} }
break; break;
} }
} }
void jump_to_app() {
volatile uint32_t* addr;
uint32_t appjump_addr;
typedef void (*app_entry_t)(void);
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(appjump_addr); void jump_to_app() {
__disable_irq();
jump = *(volatile uint32_t*)(APP_ADDRESS + 4);
void (*app_entry)(void);
app_entry = (void (*)(void))jump;
for (uint32_t i = 0; i < 3; i++) {
NVIC->ICPR[i] = 0xFFFFFFFF;
}
__set_MSP(*(volatile uint32_t*)APP_ADDRESS);
SCB->VTOR = APP_ADDRESS;
app_entry(); app_entry();
} }
@ -135,7 +139,8 @@ void erase_flash_pages() {
} }
// CRC16 implementation for STM32
uint16_t CalculateCRC16(const uint8_t* data, uint32_t length) { uint16_t CalculateCRC16(const uint8_t* data, uint32_t length) {
uint16_t crc = 0xFFFF; // Начальное значение uint16_t crc = 0xFFFF; // Начальное значение
while (length--) { while (length--) {
@ -151,6 +156,7 @@ uint16_t CalculateCRC16(const uint8_t* data, uint32_t length) {
return crc; // Финальный XOR = 0x0000 (не требуется) return crc; // Финальный XOR = 0x0000 (не требуется)
} }
bool verify_firmware() { bool verify_firmware() {
uint32_t calculated_crc = 0; uint32_t calculated_crc = 0;
calculated_crc = CalculateCRC16((uint8_t*)APP_ADDRESS,fw_size); calculated_crc = CalculateCRC16((uint8_t*)APP_ADDRESS,fw_size);