Skip to content

Commit

Permalink
Use only one backup register for CMD_BOOT for stm32f103 based targets
Browse files Browse the repository at this point in the history
This is behavior is more akin to what most STM32F103xx applications wanting to
enter the bootloader are accustomed to. Should retain backwards compatibility
with existing applications writing to both registers, the higher one just won't
be checked by the bootloader anymore.
  • Loading branch information
twelho committed Sep 10, 2021
1 parent aea139a commit 30e54d0
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 11 deletions.
11 changes: 4 additions & 7 deletions src/stm32f103/backup.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,15 @@

#define RTC_BKP_DR(reg) MMIO16(BACKUP_REGS_BASE + 4 + (4 * (reg)))

void backup_write(enum BackupRegister reg, uint32_t value) {
void backup_write(enum BackupRegister reg, uint16_t value) {
rcc_periph_clock_enable(RCC_PWR);
rcc_periph_clock_enable(RCC_BKP);

pwr_disable_backup_domain_write_protect();
RTC_BKP_DR((int)reg) = value & 0xFFFFUL;
RTC_BKP_DR((int)reg+1) = (value & 0xFFFF0000UL) >> 16;
RTC_BKP_DR((int)reg) = value;
pwr_enable_backup_domain_write_protect();
}

uint32_t backup_read(enum BackupRegister reg) {
uint32_t value = (uint32_t)RTC_BKP_DR((int)reg)
| ((uint32_t)RTC_BKP_DR((int)reg+1) << 16);
return value;
uint16_t backup_read(enum BackupRegister reg) {
return RTC_BKP_DR((int)reg);
}
4 changes: 2 additions & 2 deletions src/stm32f103/backup.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ enum BackupRegister {
BKP10,
};

extern void backup_write(enum BackupRegister reg, uint32_t value);
extern uint32_t backup_read(enum BackupRegister reg);
extern void backup_write(enum BackupRegister reg, uint16_t value);
extern uint16_t backup_read(enum BackupRegister reg);

#endif
4 changes: 2 additions & 2 deletions src/stm32f103/target_stm32f103.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ _Static_assert((FLASH_BASE + FLASH_SIZE_OVERRIDE >= APP_BASE_ADDRESS),
#endif

#ifndef CMD_BOOT
#define CMD_BOOT 0x544F4F42UL
#define CMD_BOOT 0x4F42UL
#endif

void target_clock_setup(void) {
Expand Down Expand Up @@ -166,7 +166,7 @@ const usbd_driver* target_usb_init(void) {
bool target_get_force_bootloader(void) {
bool force = false;
/* Check the RTC backup register */
uint32_t cmd = backup_read(REG_BOOT);
uint16_t cmd = backup_read(REG_BOOT);
if (cmd == CMD_BOOT) {
force = true;
}
Expand Down

0 comments on commit 30e54d0

Please sign in to comment.