MKEVAL-STBOX1V1 (sensortile box) enter DFU through STM32duino?

All about boards manufactured by ST
Post Reply
gemelko
Posts: 3
Joined: Thu May 18, 2023 5:26 pm

MKEVAL-STBOX1V1 (sensortile box) enter DFU through STM32duino?

Post by gemelko »

I am working on a project that I'd like to be able to force into DFU mode through software. I know this can be done, because in the core product, ST Micro has a "Debug console" and if you type "DFU" into that console, the board enters DFU mode.

But I'm not having so much luck. I've tried the following however I might be missing something somewhere:

In the header:

#include "stm32l4xx.h"
typedef void (*Jump)(void);
Jump JumpToDFU;

In the code (where I want to enter DFU):

noInterrupts();
SCB->VTOR = 0x1FFF0000;
JumpToDFU = (void (*)(void)) (*((uint32_t *)(0x1FFF0000 + 4)));
__disable_irq();
NVIC->ICER[0] = 0xFFFFFFFF;
NVIC->ICER[1] = 0xFFFFFFFF;
__set_MSP(*(volatile uint32_t *)0x1FFF0000);
JumpToDFU();

What am I missing?

Thanks for the look over...

Glenn
gemelko
Posts: 3
Joined: Thu May 18, 2023 5:26 pm

Re: MKEVAL-STBOX1V1 (sensortile box) enter DFU through STM32duino?

Post by gemelko »

So I did a lot of digging around, found AN2606, and tried adding this to my code before the JumpToDFU:

__enable_irq();
HAL_Init();
__HAL_RCC_PWR_CLK_ENABLE();

HAL_FLASH_Unlock(); // Unlock the Flash memory for writing
HAL_FLASH_OB_Unlock(); // Unlock the Option Bytes

FLASH_OBProgramInitTypeDef obConfig;

obConfig.OptionType = OPTIONBYTE_USER;
obConfig.USERType = OB_USER_nBOOT0;
obConfig.USERConfig = OB_BOOT0_RESET;
HAL_FLASHEx_OBProgram(&obConfig); // Program the Option Bytes

obConfig.OptionType = OPTIONBYTE_USER;
obConfig.USERType = OB_USER_nBOOT1;
obConfig.USERConfig = OB_BOOT1_SYSTEM;
HAL_FLASHEx_OBProgram(&obConfig); // Program the Option Bytes

obConfig.OptionType = OPTIONBYTE_USER;
obConfig.USERType = OB_USER_nSWBOOT0;
obConfig.USERConfig = OB_BOOT0_FROM_OB;
HAL_FLASHEx_OBProgram(&obConfig); // Program the Option Bytes

HAL_FLASH_OB_Lock(); // Lock the Option Bytes
HAL_FLASH_Lock(); // Lock the Flash memory

While this still ran the application code rather than going into DFU mode, I lost control of DFU mode with the external BOOT0 pin. I was able to recover using the ST-Link/V2 pod plugged into the board, and I added the following code to my startup which restored the normal BOOT0 pin functionality:

HAL_Init();
__HAL_RCC_PWR_CLK_ENABLE();

HAL_FLASH_Unlock(); // Unlock the Flash memory for writing
HAL_FLASH_OB_Unlock(); // Unlock the Option Bytes

FLASH_OBProgramInitTypeDef obConfig;

obConfig.OptionType = OPTIONBYTE_USER;
obConfig.USERType = OB_USER_nSWBOOT0;
obConfig.USERConfig = OB_BOOT0_FROM_PIN;
HAL_FLASHEx_OBProgram(&obConfig); // Program the Option Bytes

HAL_FLASH_OB_Lock(); // Lock the Option Bytes
HAL_FLASH_Lock(); // Lock the Flash memory

I think I'm on the right track, but I still can't seem to force the bootloader into DFU mode. I really need another hint here, as I've been struggling with this for several days.

Glenn
Post Reply

Return to “STM boards (Discovery, Eval, Nucleo, ...)”