#include "general.h"
#include "usbdfu.h"
#include <libopencm3/stm32/flash.h>
#include <libopencm3/cm3/scb.h>
#define FLASH_OBP_RDP 0x1ffff800U
#define FLASH_OBP_WRP10 0x1ffff808U
#define FLASH_OBP_RDP_KEY 0x5aa5U
#if defined(STM32_CAN)
#define FLASHBLOCKSIZE 2048U
#else
#define FLASHBLOCKSIZE 1024U
#endif
static uint32_t last_erased_page = 0xffffffffU;
void dfu_check_and_do_sector_erase(uint32_t sector)
{
sector &= ~(FLASHBLOCKSIZE - 1U);
if (sector != last_erased_page) {
flash_erase_page(sector);
last_erased_page = sector;
}
}
void dfu_flash_program_buffer(const uint32_t baseaddr, const void *const buf, const size_t len)
{
const uint16_t *const buffer = (const uint16_t *)buf;
for (size_t i = 0; i < len; i += 2U)
flash_program_half_word(baseaddr + i, buffer[i >> 1U]);
dfu_event();
}
uint32_t dfu_poll_timeout(uint8_t cmd, uint32_t addr, uint16_t blocknum)
{
(void)cmd;
(void)addr;
(void)blocknum;
return 100;
}
void dfu_protect(bool enable)
{
#ifdef DFU_SELF_PROTECT
if (enable) {
if (FLASH_WRPR & 0x03U) {
flash_unlock();
FLASH_CR = 0;
flash_erase_option_bytes();
flash_program_option_bytes(FLASH_OBP_RDP, FLASH_OBP_RDP_KEY);
flash_program_option_bytes(FLASH_OBP_WRP10, 0x03fc);
}
}
#else
(void)enable;
#endif
#if 0#endif
}
void dfu_jump_app_if_valid(void)
{
const uint32_t stack_pointer = *((uint32_t *)app_address);
if ((stack_pointer & 0x2ffe0000U) == 0x20000000U) {
SCB_VTOR = app_address & 0x001fffffU;
__asm__(
"msr msp, %1\n"
"ldr pc, [%0, 4]\n"
: : "l"(app_address), "l"(stack_pointer) : "r0"
);
while (true)
continue;
}
}