Bootloader CAN work
This commit is contained in:
parent
4b543e78ce
commit
a1bcbdb33b
1 changed files with 31 additions and 25 deletions
|
@ -9,14 +9,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 jump;
|
||||
static FLASH_RECORD flash_record = {0};
|
||||
static uint32_t ptr_flash;
|
||||
// Прототипы функций
|
||||
void jump_to_app();
|
||||
void process_can_message(const CAN_message_t &msg);
|
||||
void erase_flash_pages();
|
||||
|
||||
bool verify_firmware();
|
||||
void send_ack(uint8_t status);
|
||||
|
||||
|
@ -33,7 +32,7 @@ void setup() {
|
|||
// SendTimer->attachInterrupt(process_can_message);
|
||||
SendTimer->resume();
|
||||
// Разрешить все ID (маска 0x00000000)
|
||||
Can.setFilter(0, 0, STD);
|
||||
Can.setFilter(0, 0, STD);
|
||||
|
||||
// Настройка GPIO
|
||||
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;
|
||||
|
@ -41,13 +40,13 @@ void setup() {
|
|||
GPIOC->ODR |= GPIO_ODR_OD11;
|
||||
|
||||
// Проверка флага обновления
|
||||
/* 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);*/
|
||||
// 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();
|
||||
/* Добавить проверку адреса, т.е во время отправки запроса прошивки по CAN
|
||||
мы сохраняем как флаг, так и аддрес устройства к которому будет обращатлься во
|
||||
|
@ -66,14 +65,14 @@ void setup() {
|
|||
void loop() {
|
||||
if(fw_update) {
|
||||
GPIOC->ODR ^= GPIO_ODR_OD10;
|
||||
HAL_Delay(100);
|
||||
// HAL_Delay(100);
|
||||
CAN_message_t msg;
|
||||
while(Can.read(msg)) {
|
||||
while(Can.read(msg))
|
||||
process_can_message(msg);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
void process_can_message(const CAN_message_t &msg) {
|
||||
switch(msg.id) {
|
||||
|
@ -91,8 +90,10 @@ void process_can_message(const CAN_message_t &msg) {
|
|||
write_flash_page((const uint8_t*)msg.buf, msg.len);
|
||||
ptr_flash += msg.len;
|
||||
send_ack(0x02);
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case BOOT_CAN_END: // Завершение передачи
|
||||
if(verify_firmware()) {
|
||||
|
@ -100,24 +101,27 @@ 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);
|
||||
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);
|
||||
__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();
|
||||
}
|
||||
|
||||
|
@ -135,7 +139,8 @@ void erase_flash_pages() {
|
|||
}
|
||||
|
||||
|
||||
// CRC16 implementation for STM32
|
||||
|
||||
|
||||
uint16_t CalculateCRC16(const uint8_t* data, uint32_t length) {
|
||||
uint16_t crc = 0xFFFF; // Начальное значение
|
||||
while (length--) {
|
||||
|
@ -151,6 +156,7 @@ uint16_t CalculateCRC16(const uint8_t* data, uint32_t length) {
|
|||
return crc; // Финальный XOR = 0x0000 (не требуется)
|
||||
}
|
||||
|
||||
|
||||
bool verify_firmware() {
|
||||
uint32_t calculated_crc = 0;
|
||||
calculated_crc = CalculateCRC16((uint8_t*)APP_ADDRESS,fw_size);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue