About Homemade STM8 Bootloader

Source: Internet
Author: User

Due to my project needs, to do STM8L052R8 bootloader, for the remote program upgrade function, for security reasons, do not use the St comes with bootloader, but homemade bootloader.

The basic function is this, First the program runs in a V1.0 version, and with boot, when the program received a command, the program ran into a dead loop, waiting for the hardware watchdog reset, after the program reset into the Bootload area, waiting for the second command to receive, received the correct data frame, bootloader began to erase Flash, and receive packets, Until the last packet has been received, flashing at a frequency of 2HZ through the LEDs indicates that the upgrade is complete.
The first step: Boot area program design, first modify the link file,

    Define Region Nearfunccode = [from0x80000xAFFF];           = [from0x80000xAFFF];           = [from0x80000xAFFF];          Place at start of Nearfunccode  {block Intvec};


The above is the link file section, you can see the flash address for 0x8000 start, ending in 0x17fff; The length is 64kB, the interrupt vector address is 0x8000,
So, know this we can modify the boot program link and the main program link, here I divide the Bootload division into 8K, the application area of 48K
Set the link file as follows, so that the boot area and the app area apart, do not interfere with each other, you can also adjust their size as needed.

Program:define Region Nearfunccode= [ from 0xb000To0xFFFF]; Define Region Farfunccode= [ from 0xb000To0xFFFF]| [ from 0x10000To0x17fff]; Define Region Hugefunccode= [ from 0xb000To0x17fff];         Place at start of Nearfunccode {block Intvec}; Bootload:define Region Nearfunccode= [ from 0x8000To0xAFFF]; Define Region Farfunccode=    [ from 0x8000To0xAFFF]; Define Region Hugefunccode= [ from 0x8000To0xAFFF]; Place at start of Nearfunccode {block Intvec};


such as STM32 chip interrupt vector address can be arbitrary, all boot and app area can use interrupts, and do not interfere with each other, but the STM8 interrupt vector table fixed in 0x8000 address, cannot be modified, so boot zone can not be interrupted, otherwise will and app area interruption fight, However, once the app area is interrupted, it jumps to the 0x8000 address and jumps to the boot area, so you need to jump back to the app area with a jump command.

__rootConst LongReintvec[]@". Intvec"={ 0x82008080,0x8200b004,0x8200b008,0x8200b00c,//when the application address is not a 0xb000, change the first        0x8200b010,0x8200b014,0x8200b018,0x8200b01c,//a value other than 0x82008080        0x8200b020,0x8200b024,0x8200b028,0x8200b02c,        0x8200b030,0x8200b034,0x8200b038,0x8200b03c,        0x8200b040,0x8200b044,0x8200b048,0x8200b04c,        0x8200b050,0x8200b054,0x8200b058,0x8200b05c,        0x8200b060,0x8200b064,0x8200b068,0x8200b06c,        0x8200b070,0x8200b074,0x8200b078,0x8200b07c,}; 


  The approximate meaning here is to redefine STM8 interrupts, STM8 interrupt vector redefinition, as for why this is written here, please see for yourself, I am not very clear.
    
          Second step: After power on the chip, jump from bootload start to Program Start
          when we power up, we often don't need to use bootload, But the program will first run to the beginning of the ROM address, so need to do a jump
from bootload start jump to program start. The design is as follows

ASM ("LDW X,  "); ASM ("LD  A,  $FF") ASM ("  LD"); ASM ("" ); ASM ("JPF $B");


Here we use B000, indicating that the boot zone does not receive the upgrade request after the power is directly to the app area, 0xb000 is the starting address of my app, you can define according to your own requirements.
Step three: Write Flash online

 voidBoot_erasechip (void) {U16 uscnt; /*Define Flash Programming time*/Flash_setprogrammingtime (Flash_programtime_standard);  Boot_unlock (); /*Erase Flash*/Led_alarm_h ();  Led_run_l ();  for(uscnt = user_flase_all_of_block;uscnt >0; uscnt--) {HD_CLEAR_WDT (); Flash_eraseblock (User_flash_start_block+ (uscnt-1), Flash_memtype_program); /*Wait until End of high voltage flag is set*/     while(Flash_getflagstatus (flash_flag_hvoff) = =RESET)  {}} led_alarm_l ();  Led_run_h (); Boot_lock ();}


This function inside my boot program once received the upgrade command, the Flash erase, here to use a library function,
There are stm8l15x_flash.h stm8l15x_flash.c in the Lib library of STM8.
This file contains a comment, as shown below, meaning to use flash erase, must be defined in the Stm8l15x.h

#define Ram_execution  (1)  , Flash erase functions are defined in the RAM address, so before these functions have a in_ram from    Ram can either Uncomment the following define  in  your toolchain compiler preprocessor   #define Ram_execution  


And since the STM8 library is different for different chip processing, you need to define the chip model you use, here I use the chip is
stm8l052, so the macro used is:

#define Stm8l05x_hd_vl


This can call the Erase function to upgrade the app, the other work is in the boot area to send and receive data waiting, data validation and other work done, nothing special. No matter what chip, as long as the Flash is chunked, you can do boot, to load the app.

About Homemade STM8 Bootloader

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.