Iam doing a Bootloader for an arm cortex m0 based microcontroller and for that I have gone through some projects in github and I donot understand some of the lines in the following code. What is the meaning of the line *(__IO uint32_t*)APP_ADDRESS) & ~(RAM_SIZE-1) in the following code. It would be great if someone explains this. Thanks in advance.
#define APP_ADDRESS (uint32_t)0x08008000
#define RAM_SIZE (uint32_t)0x00040000
uint8_t Bootloader_CheckForApplication(void)
{
return ( ((*(__IO uint32_t*)APP_ADDRESS) & ~(RAM_SIZE-1)) == 0x20000000 ) ? BL_OK : BL_NO_APP;
}
I believe you are referring to this project at https://github.com/akospasztor/stm32-bootloader
The purpose of Bootloader_CheckForApplication() is to determine whether a valid application is located from APP_ADDRESS or 0x0800 8000. The start of this application image is actually the vector table , thus the value *(__IO uint32_t*)APP_ADDRESS is the initial value of the Stack Pointer (SP). (The link discusses about Cortex-M3 but the first two entries of the vector table are the same for all Cortex-M processors)
You can see the use of this initial SP and the reset vector in function Bootloader_JumpToApplication() in the same source file at https://github.com/akospasztor/stm32-bootloader/blob/master/Src/bootloader.c
Note that the author is using STM32L4, of which the starting address of RAM is 0x2000 0000, the last address is 0x2003 FFFF, and the RAM size is 0x0004 0000. In this case, Bootloader_CheckForApplication() is checking if the stored initial SP is within RAM area.
However, this check will not be correct if the initial SP happens to be 0x2004 0000, which is the highest valid value in this case. (Note that Cortex-M processors use full descending stack)
User contributions licensed under CC BY-SA 3.0