Struggling with custom bootloader on STM32L433 (only simple applications work)
I have written a custom bootloader that is running on a Nucleo-L433RC-P board.
The application uses USB CDC to call the bootloader and the bootloader uses USB DFU to download the application.
I wanted to use USB to call the custom bootloader, rather than having to use a physical switch on the BOOT0 pin to call the internal bootloader
I can successfully download a simple application which just flashes an LED using the USB DFU bootloader.
However, when I download my actual project, it appears to download successfully but does not run.
My actual project is more complicated and uses the following peripherals:
- RTC
- SPI2 and DAC1 (using DMA)
- SPI1
- TIM6
- TIM7
- I2C1
- I2C2
- USB
- Shutdown and standby using WKUP and RTC wake sources
I used the following guide to write the custom bootloader and simple application...
The following web tool is used to download the application...
I have spent days trying to debug why my actual project will not work, yet the simple application does.
Now I am at a complete loss and don't know how to proceed.
The implementation is shown below.
Bootloader USB configuration...

Changes to bootloader linker script...

Bootloader main.c...
#include "main.h"
#include "usb_device.h"
typedef void (*pFunction)(void);
#define DFU_BOOT_FLAG 0xDEADBEEF
RTC_HandleTypeDef hrtc;
uint32_t dfu_boot_flag;
pFunction JumpToApplication;
uint32_t JumpAddress;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_RTC_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USB_DEVICE_Init();
MX_RTC_Init();
dfu_boot_flag = HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR5);
if (dfu_boot_flag != DFU_BOOT_FLAG)
{
if (((*(__IO uint32_t*) USBD_DFU_APP_DEFAULT_ADD) & 0x2FFC0000) == 0x20000000)
{
JumpAddress = *(__IO uint32_t*) (USBD_DFU_APP_DEFAULT_ADD + 4);
JumpToApplication = (pFunction) JumpAddress;
__set_MSP(*(__IO uint32_t*) USBD_DFU_APP_DEFAULT_ADD);
JumpToApplication();
}
}
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR5, 0); // So next boot won't be affected
uint32_t now = 0, next_blink = 100;
while (1)
{
now = uwTick;
if (now >= next_blink)
{
HAL_GPIO_TogglePin(STATUS_LED_GPIO_Port, STATUS_LED_Pin);
next_blink = now + 100;
}
}
}
Changes to bootloader usbd_dfu_if.c....








Changes to bootloader usbd_dfu_if.h....

Changes to application linker script...

Simple application main.c...
#include "main.h"
#include "usb_device.h"
#define DFU_BOOT_FLAG 0xDEADBEEF
#define DFU_BOOT_REQ 0xAA
RTC_HandleTypeDef hrtc;uint32_t *dfu_boot_flag;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_RTC_Init(void);
int main(void)
{
uint32_t now = 0, next_blink = 1000;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USB_DEVICE_Init();
MX_RTC_Init();
HAL_PWR_EnableBkUpAccess();
while (1)
{
now = uwTick;
if (now >= next_blink)
{
HAL_GPIO_TogglePin(STATUS_LED_GPIO_Port, STATUS_LED_Pin);
next_blink = now + 1000;
}
}
}
#define USB_RX_BUF_SIZE 512
volatile uint8_t usb_rx_buffer[USB_RX_BUF_SIZE];
volatile uint32_t usb_rx_count = 0; // number of bytes in buffer
void USB_CDC_RxHandler(uint8_t* Buf, uint32_t Len)
{
if ((usb_rx_count + Len) < USB_RX_BUF_SIZE)
{
memcpy((uint8_t *)&usb_rx_buffer[usb_rx_count], Buf, Len);
usb_rx_count += Len;
}
if (usb_rx_buffer[0] == DFU_BOOT_REQ)
{
HAL_RTCEx_BKUPWrite(&hrtc, RTC_BKP_DR5, DFU_BOOT_FLAG);
HAL_NVIC_SystemReset();
}
}
Changes to application system_stm32l4xx.c...

Changes to application usbd_cdc_if.c...

