Возможна загрузка прошивки, но не переходит в неё
This commit is contained in:
parent
1de6c1bda1
commit
4b543e78ce
3 changed files with 160 additions and 102 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue