Skip to content

Commit

Permalink
Allow enter to DFU:
Browse files Browse the repository at this point in the history
H - not need hw mod, enter to DFU if middle button pressed
H4 - allow enter to DFU from UI and from 'reset dfu' command
  • Loading branch information
DiSlord Live committed Jan 7, 2023
1 parent 3e89874 commit 83b7cd1
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 60 deletions.
27 changes: 26 additions & 1 deletion NANOVNA_STM32_F072/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,38 @@ const PALConfig pal_default_config = {
};
#endif

static bool needDFU(void) {
// Magick data in memory before reset
if (*((unsigned long *)BOOT_FROM_SYTEM_MEMORY_MAGIC_ADDRESS) == BOOT_FROM_SYTEM_MEMORY_MAGIC)
return true;
// init PortA (leveler port) and check press
rccEnableAHB(STM32_GPIO_EN_MASK, FALSE);
GPIOA->OTYPER = VAL_GPIOA_OTYPER;
// GPIOA->OSPEEDR = VAL_GPIOA_OSPEEDR;
GPIOA->PUPDR = VAL_GPIOA_PUPDR;
GPIOA->ODR = VAL_GPIOA_ODR;
// GPIOA->AFR[0] = VAL_GPIOA_AFRL;
// GPIOA->AFR[1] = VAL_GPIOA_AFRH;
GPIOA->MODER = VAL_GPIOA_MODER;
if (GPIOA->IDR & (1<<GPIOA_PUSH)) {
while(GPIOA->IDR & (1<<GPIOA_PUSH)) {}; // Wait press
return true;
}
return false;
}

void boardDFUEnter(void) {
*((unsigned long *)BOOT_FROM_SYTEM_MEMORY_MAGIC_ADDRESS) = BOOT_FROM_SYTEM_MEMORY_MAGIC;
NVIC_SystemReset();
}

/*
* Early initialization code.
* This initialization must be performed just after stack setup and before
* any other initialization.
*/
void __early_init(void) {
if ( *((unsigned long *)BOOT_FROM_SYTEM_MEMORY_MAGIC_ADDRESS) == BOOT_FROM_SYTEM_MEMORY_MAGIC ) {
if (needDFU()) {
// require irq
__enable_irq();
// reset magic bytes
Expand Down
1 change: 1 addition & 0 deletions NANOVNA_STM32_F072/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,7 @@
extern "C" {
#endif
void boardInit(void);
void boardDFUEnter(void);
#ifdef __cplusplus
}
#endif
Expand Down
56 changes: 5 additions & 51 deletions NANOVNA_STM32_F303/board.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,63 +62,17 @@ const PALConfig pal_default_config = {
};
#endif

void boardDFUEnter(void) {
__set_MSP(SYSTEM_BOOT_MSP);
( (void (*)(void)) (*((uint32_t *)(STM32F303xC_SYSTEM_MEMORY + 4))) )(); // jump to DFU
}

/*
* Early initialization code.
* This initialization must be performed just after stack setup and before
* any other initialization.
*/
void __early_init(void) {
// Refer to thess pages for how to start dfu from software
// https://community.st.com/s/question/0D50X00009XkeeWSAR/stm32l476rg-jump-to-bootloader-from-software
// https://stm32f4-discovery.net/2017/04/tutorial-jump-system-memory-software-stm32/
#if 0
if ( *((unsigned long *)BOOT_FROM_SYTEM_MEMORY_MAGIC_ADDRESS) == BOOT_FROM_SYTEM_MEMORY_MAGIC ) {
// require irq
// __enable_irq();
// reset magic bytes
*((unsigned long *)BOOT_FROM_SYTEM_MEMORY_MAGIC_ADDRESS) = 0;
#if 1
// https://stm32f4-discovery.net/2017/04/tutorial-jump-system-memory-software-stm32/
// Step: Disable systick timer and reset it to default values
#if 0
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
#endif
// Step: Disable all interrupts
__disable_irq();
// Remap system memory to address 0x0000 0000 in address space
typedef void (*pFunction)(void);
pFunction bootloader;
uint32_t msp;
uint32_t foo = SYSCFG->CFGR1;
foo = (foo & ~SYSCFG_CFGR1_MEM_MODE) || SYSCFG_CFGR1_MEM_MODE_0;
SYSCFG->CFGR1 = foo;
//foo = SYSCFG->CFGR1;
__DSB();
__ISB();
//__DSB();
//__ISB();
#if 1
bootloader = (void (*)(void)) (*((uint32_t *)(STM32F303xC_SYSTEM_MEMORY+4)));
//msp = *(uint32_t *) STM32F303xC_SYSTEM_MEMORY;
msp = 0x20002250;
#else
bootloader = (void (*)(void)) (*((uint32_t *)(4)));
//msp = *(uint32_t *) 0;
msp = 0x20002250;
#endif
__set_MSP(msp);
bootloader();
while(1);
#else
__set_MSP(SYSTEM_BOOT_MSP);
( (void (*)(void)) (*((uint32_t *)(STM32F303xC_SYSTEM_MEMORY+4))) )();
while(1);
#endif
}
#endif

stm32_clock_init();
}

Expand Down
1 change: 1 addition & 0 deletions NANOVNA_STM32_F303/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -794,6 +794,7 @@
extern "C" {
#endif
void boardInit(void);
void boardDFUEnter(void);
#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3035,7 +3035,7 @@ static const VNAShellCommand commands[] =
#endif
{"vbat" , cmd_vbat , CMD_RUN_IN_LOAD},
{"tcxo" , cmd_tcxo , CMD_RUN_IN_LOAD},
{"reset" , cmd_reset , CMD_RUN_IN_LOAD},
{"reset" , cmd_reset , CMD_WAIT_MUTEX|CMD_BREAK_SWEEP|CMD_RUN_IN_LOAD},
#ifdef __USE_SMOOTH__
{"smooth" , cmd_smooth , CMD_WAIT_MUTEX|CMD_BREAK_SWEEP|CMD_RUN_IN_UI|CMD_RUN_IN_LOAD},
#endif
Expand Down
4 changes: 0 additions & 4 deletions nanovna.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@
// Use DAC (in H4 used for brightness used DAC, so need enable __LCD_BRIGHTNESS__ for it)
//#define __VNA_ENABLE_DAC__
// Allow enter to DFU from menu or command
#if defined(NANOVNA_F303)
//#define __DFU_SOFTWARE_MODE__
#else
#define __DFU_SOFTWARE_MODE__
#endif
// Add RTC clock support
#define __USE_RTC__
// Add RTC backup registers support
Expand Down
4 changes: 1 addition & 3 deletions ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,9 +615,7 @@ enter_dfu(void)
lcd_clear_screen();
lcd_drawstring(x, y, "DFU: Device Firmware Update Mode\n"
"To exit DFU mode, please reset device yourself.");
// see __early_init in ./NANOVNA_STM32_F072/board.c
*((unsigned long *)BOOT_FROM_SYTEM_MEMORY_MAGIC_ADDRESS) = BOOT_FROM_SYTEM_MEMORY_MAGIC;
NVIC_SystemReset();
boardDFUEnter();
}
#endif

Expand Down

0 comments on commit 83b7cd1

Please sign in to comment.