STM32F2 bootloader issue - arm

I have write a bootloader to jump into my app.
First I have try with a simple blinky-led app => I am able to jump into the app from the bootloader.
Now I want to jump into my real app. The app is working well alone but when I jump into it from my bootloader the app crash as soon as the interrupts are enabled, my jumping code :
__disable_irq();
SCB->VTOR = (uint32_t)0x0800BA00;
JumpAddress = *(__IO uint32_t*) (0X0800BA04);
JumpToApplication = (pFunction) JumpAddress;
__set_MSP(*(__IO uint32_t*) 0X0800BA00);
JumpToApplication();
I don't know what is wrong and why the activation of interruptions crash the app.
Thank you for your help

Before you jump to the application, you should deinitialize everything that you've initialized in the bootloader. If your bootloader uses USART with interrupts, you should disable this USART (for example using RCC->AHBxRST/RCC->APBxRST registers) and disable its interrupts. You should also jump to your application with interrupts enabled. Your application should get the chip just as it would be after a normal reset.
If your application uses this crap code from ST called SPL or HAL, then make sure that this code does NOT reset SCB->VTOR back to 0 or 0x8000000, because normally it does that in SystemInit(), which is called from Reset_Handler(), before main().
BTW - are you absolutely sure about the address of your application? You usually put the application at the page boundary, while your code does not indicate that - 0x800ba00 (46.5kB) is pretty far from the closest page boundaries 32kB and 48kB...

set clock setting of boot code & application code same

Related

Cannot change value of LCD_CR register

I am currently playing with L152C Discovery board and trying to make simple clock that would use the RTC build into the STM32 and onboard Glass LCD with LCD HAL library configured via CubeMX.
But I am currently facing a problem I can't get my head around:
CubeMX does not have an option to enable segment mux in the LCD_CR register. I would like to enable it, because it would make the segment mapping easier.
So I thought, fine, I will make an direct register manipulation, enabling the mux (bit 7 in the LCD_CR).
I used the command LCD->CR |= LCD_CR_MUX_SEG; But even after executing the command, the MUX_SEG bit is still zero. (I checked in the debug session with command stepping and SFRs memory map)
Is there something that I am doing wrong? Or is there another way to change init parameters that CubeMX configured but does not have graphical implementation of this settings option?
The application is using FreeRTOS and I executed LCD->CR |= LCD_CR_MUX_SEG; after HAL_LCD_Init(&hlcd); so I sappose that the LCD peripheral clock is running (and segments are updating).
I recorded a short video showing this problem:
https://youtu.be/0X6Zu5EPudU
To be honest, I am not skilled at direct register manipulation, so I am probably doing something wrong.
Any help would be appreciated!😇
As #KIIV said:
RM0038 Liquid crystal display controller (LCD) Note: The VSEL, MUX_SEG, BIAS and DUTY bits are write protected when the LCD is enabled (ENS bit in LCD_SR to 1).
The LCD must be disabled when making changes to the above registers.

Hard fault in STM32F101RF due to MRC2 Disassembly?

I am having a bootloader code wherein I will sending/receiving data via USART . I have configured USART to operate in interrupt mode.
USART functionality works perfectly fine independently. Verified this with multiple read/write instances.
When I integrate USART code with my bootloader code, bootloader will keep on checking if there is any pending data to read from USART.
If there is any pending data, bootloader will read the Data Register (DR) for the data received already through interrupt. (kind of polling + interrupt)
My problem :
Whenever there is an USART receive interrupt is triggered , inside receive interrupt service routine hard fault error occurs.
PC says its inside hard fault routine where I read data from DR.
But a strange thing I saw is, from the location where hard fault hits, in the disassembly I see only MRC2 commands
Is the issue occuring because of this ?? 0x8004802 is the location where my hardfault hits.
Kindly enlighten me on this
Look at the raw hex values: 0xFF all the way starting with a suspicious address 0x8004800 which is for sure a page boundary.
In other words: The flash memory is bad or was erased and not completely written. Verifying the flashed program (bootloader) should fail.
If that was in your bootloader code, it might have tried to overwrite itself - or simply erased the wrong memory page.

Soft bootloader error on stm32f205rg

Good day, i have a problem with the loading of
firmware of the mc stm32f205rg. i used function
boot_softs to enter to the bootloader , which is
situated in system memory.
void boot_softs() {
volatile uint32_t addr = 0x1FFF0000;
void (*SysMemBootJump)(void);
SysMemBootJump = (void (*)(void)) (*((uint32_t *)(addr + 4)));
HAL_RCC_DeInit();
SysTick->CTRL = 0;
SysTick->LOAD = 0;
SysTick->VAL = 0;
_disable_irq();
SYSCFG->MEMRMP = 0x01;
__set_MSP(*(__IO uint32_t*)0x20001000);
SysMemBootJump();
}
But during the checking with the help of Flash loader
demonstrator program i have found out that mc dont
enter in firmware reloading state.
I rechecked the same function on stm32f207 with no
changes and stm32f103c8 with address of programm
beginning changed.in both cases everything works
correctly and mc reloades.
Maybe someone faced such a problem and can help me
with this problem. Thanks in advance.
I don't think system memory can be accessed via code, You need to make changes to the external PINs that are responsible for mode selection. M0 and M1.
Upon selecting these pins , you may enter in system mode of your STM32. Here is one link that explains how to send command to bootloader. Click here!
You have to check in system bootloader address because it varies depending on MCU.
So check out this documentation from ST: in system bootloader guide from ST
As I see you are using wrong RAM address.
Part from bootloader cook book:
Moreover, there should be a trigger for entering to system bootloader. As I understand you are using software call method from your main application. This method is not reliable if BOOT pins of MCU are uncontrolled because if serial line or updating software fails you will loose main application and you will be unable to update device. Than you'll need programmer.
So updating, overwriting software which has system bootloader call is unsafe updating method. I would suggest it only as a own usage update method.
To have safety you should control BOOT pins.
Or implement mini custom bootloader which could be triggered by button (to start main app) and before triggering that mini bootloader would stay quiet and let system bootloader update main app. Update can't touch mini bootloader memory, this own customized bootloader should be untouched while update. But that is a suggestion.
Easer way to take control on update is to control BOOT pins.
For example:
put 3.3V or GND (depends on MCU) on BOOT pins with jumpers , set power and than device automatically will enter to system bootloader.
After update remove jumpers, and after power reset device will enter to your new update application.
That's all.
If you do not have BOOT0, BOOT1 pins control from hardware. I strongly recommending you to check those pins control.
There is a guide from ST about BOOT pins: reference manual
Part of description from reference manual:

Start to writing a bootloader for stm32l0 in IAR

What are the appropriate steps to write add a custom bootloader for stm32l0 in IAR? The following questions are not clear:
Do I make a new IAR Project?
If yes, do I write the bootloader like a normal project and just change my original .icf file so there is a small ROM and an small RAM region for the bootloader?
if no, what things do I have to configure in the IAR proejct apart from icf file and code?
what other things do I need to think of?
I'm having trouble starting into this.
So the icf would be for the main project:
__region_ROM_start__ = 0x08000000;
__region_ROM_end__ = 0x08008FFF;
So the icf would be for the bootloader project:
__region_Bootloader_ROM_start__ = 0x08009000;
__region_Bootloader_ROM_end__ = 0x08009FFF;
and the same thing for about 0xFF of RAM?
You do not need to restrict the RAM - you can use all of it because when you switch to the application a new run-time environment will be established and the RAM will be reused.
The flash you reserve for the bootloader must be a whole number of flash pages starting from the reset address The STM32L0 has very small flash pages so there should be minimal waste, but you don't want to have to change it if your bootloader grows, because then you will have to rebuild your application code for the new start address and old application images will no longer be loadable. So consider giving yourself a little headroom.
The bootloader can be built just like any other STM32L0xx project; the application code ROM configuration must start from an address above the bootloader. So for example say you have a 1Kbyte bootloader:
Boot ROM Start: 0x0800 0000
Boot ROM End: 0x0800 03FF
Application Start: 0x0800 0400
Application End: Part size dependent.
The bootloader itself must have a means of determining that an update is available, if an update is available it must then read the application data and write it to the application flash memory, it must then disable any interrupts that may have been enabled, it may also be necessary to deinitialise any peripherals used (if they remain active when the switch to the application is made it may cause problems), then the switch to the application code is made.
It is possible if the bootloader and application both run from the same clock configuration to minimise the configuration in the application and rely on the bootloader. This is a small space saving, but less flexible. If for example you make the bootloader run using the internal RC oscillator it will be portable across multiple hardware designs that may have differing application speed and clocking requirements and different external oscillator frequencies
The switch to the application is pretty simple on Cortex-M, it simply requires the vector table to be switched to the application's vector table, then the program-counter to be loaded - the latter requires a little assembly code. The following is for Cortex-M3, it may need some adaptation for M0+ but possibly not:
Given the following in-line assembly function:
__asm void boot_jump( uint32_t address )
{
LDR SP, [R0] ;Load new stack pointer address
LDR PC, [R0, #4] ;Load new program counter address
}
The bootloader switched to the application image thus:
// Switch off core clock before switching vector table
SysTick->CTRL = 0 ;
// Switch off any other enabled interrupts too
...
// Switch vector table
SCB->VTOR = APPLICATION_START_ADDR ;
//Jump to start address
boot_jump( APPLICATION_START_ADDR ) ;
Where APPLICATION_START_ADDR is the base address of the application area; this address is the start of the application's vector table, which starts with the initial stack pointer and reset vector, the boot_jump() function loads these into the SP and PC registers to start the application as if it had been started at reset. The application's reset vector contains the application's execution start address.
Your needs may vary, but in my experience a serial bootloader (using UART) using XMODEM and decoding an image in Intel Hex format takes about 4Kb of Flash. On an STM32L0 you may want to use something simpler - 1Kb is probably feasible if you simply stream raw binary the data and use hardware flow control (you need to control data flow because erasing and programming the flash takes time and also stops the CPU from running because you cannot on STM32 write flash memory while simultaneously fetching instructions from it).
See also: How to jump between programs in Stellaris

IAP Bootloader could not load RTX application image

I have splitted software into two parts: Bootloader(without RTX), Application image with RTX.
But the bootloader could not load the application image with RTX.
The Flash settings are:
--------------------------------------------------------------------
start address size
IROM 1: 0x08000000 0x2800 - Bootloader (without RTX)
IROM 2: 0x08002800 0xD000 - Application Image (with RTX)
I have test 3 ways:
(1) Use another App without RTX. The bootloader could load the app successfully.
(2) Change the application with RTX project IROM setting. I change the application project IROM start address from 0x08002800 to 0x08000000. And I download the application image into flash from the address 0x08000000. Ihe image could run from 0x08000000 successfully.
(3) The application image IROM start address setting is 0x08002800. After downloading bootloader and app image into flash, I debug the app project in keil step by step. I found that there is a "osTimerthread stack overflow" error. Then the main thread stack is also overflowed. I have tried to increase the stack size, but it doesn't work.
I found that the app starks in the RTX kernel switching. All threads are in the waiting state, and are not running.
Ps, when I am debugging in the keil,test item(2) also have stack overflow errors during kernel initialization. The item(2) works fine till now. So I just put any information needed here.
This is the debugging picture for item (3).
Are you actually changing the linker script to link starting at 0x08002800 when using the bootloader or just loading the application (linked at 0x08000000) at an offset of 0x2800? Double check this (look in the map file) for your linked output to ensure that all your symbols are not linked in the 0x08000000 - 0x08002800 range.
Additionally, make sure you are using the correct entry point and stack pointer. The application's stack pointer should be at 0x08002800, and the reset vector will be at 0x08002804. Your bootloader will need to setup the MSP register with the correct stack pointer before jumping to the application. Here is some example code from ST's USB DFU bootloader:
typedef void (*pFunction)(void);
pFunction JumpToApplication;
uint32_t JumpAddress;
/* Jump to user application */
JumpAddress = *(__IO uint32_t*) (USBD_DFU_APP_DEFAULT_ADD + 4);
JumpToApplication = (pFunction) JumpAddress;
/* Initialize user application's Stack Pointer */
__set_MSP(*(__IO uint32_t*) USBD_DFU_APP_DEFAULT_ADD);
JumpToApplication();
Additionally, depending on how much your bootloader configures before jumping to the application, you may need to 'deconfigure' certain peripherals. As an example, if you setup your clocks in the bootloader before deciding to jump to the application, you may run into problems in your application if it assumes that the clocks are in the default configuration already. Similar things can happen with the NVIC and SysTick if your bootloader is using these before jumping to the application.
Lastly, along the same lines as the previous section, the application may be making assumptions about the state of peripherals being default, but it also may be making assumptions that the peripheral defaults are correct. For example: SCB->VTOR has a default value (I believe it is always 0x00000000), and this points to the vector table. Your bootloader will be linked to have its vector table at that location. You'll need to make sure that when your application is starting up, it updates the VTOR register to point to the actual location of its vector table.
Hopefully one of these sections helps you identify the problem.

Resources