Skip to content

Commit

Permalink
Replace bootloader handoff
Browse files Browse the repository at this point in the history
  • Loading branch information
Disasm committed Oct 12, 2019
1 parent e818797 commit 28b2253
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/stm32f103/bluepill/DFU/DFU.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,17 @@
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include "DFU/DFU.h"
#include "backup.h"

#include <libopencm3/cm3/scb.h>

#include "DFU/DFU.h"
/* Boot command for DAPBoot bootloader */
static const uint32_t CMD_BOOT_VIA_DAPBOOT = 0x544F4F42UL;

/* Writes a DFU command to the backup register and resets */
void DFU_reset_and_jump_to_bootloader(void) {
/* Trigger the STLink/v2-1 bootloader via software reset */
backup_write(BKP0, CMD_BOOT_VIA_DAPBOOT);
scb_reset_system();
while (1);
}
Expand Down
41 changes: 41 additions & 0 deletions src/stm32f103/bluepill/DFU/backup.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (c) 2016, Devan Lai
*
* Permission to use, copy, modify, and/or distribute this software
* for any purpose with or without fee is hereby granted, provided
* that the above copyright notice and this permission notice
* appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/rtc.h>
#include <libopencm3/stm32/pwr.h>

#include "backup.h"

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

void backup_write(enum BackupRegister reg, uint32_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*2) = value & 0xFFFFUL;
RTC_BKP_DR((int)reg*2+1) = (value & 0xFFFF0000UL) >> 16;
pwr_enable_backup_domain_write_protect();
}

uint32_t backup_read(enum BackupRegister reg) {
uint32_t value = ((uint32_t)RTC_BKP_DR((int)reg*2+1) << 16)
| ((uint32_t)RTC_BKP_DR((int)reg*2) << 0);
return value;
}
35 changes: 35 additions & 0 deletions src/stm32f103/bluepill/DFU/backup.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2016, Devan Lai
*
* Permission to use, copy, modify, and/or distribute this software
* for any purpose with or without fee is hereby granted, provided
* that the above copyright notice and this permission notice
* appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
* WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
* AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#ifndef BACKUP_H_INCLUDED
#define BACKUP_H_INCLUDED

#include <stdint.h>

enum BackupRegister {
BKP0 = 0,
BKP1,
BKP2,
BKP3,
BKP4,
};

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

#endif

0 comments on commit 28b2253

Please sign in to comment.