STM32F7 bootloader based on ChibiOS

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderators: RoccoMarco, barthess

elagil
Posts: 92
Joined: Tue Sep 19, 2017 7:38 am
Has thanked: 8 times
Been thanked: 7 times

STM32F7 bootloader based on ChibiOS

Postby elagil » Tue Mar 20, 2018 4:27 pm

Hello!

I am trying to write a bootloader that allows loading of a new image over ethernet. I did not really find anything recent regarding this topic, so I looked for inspiration in older STM32F4 code.

The bootloader shall always be started first and wait for a new image or boot signal over ethernet (alternatively, timeout). This is important for in application programming of many processors at once.

The first step is to write a ChibiOS based bootloader that just jumps to the user application. I found some code snippets and combined them to the following routine. As far as I understood, the steps to take are as follows (please correct me if this is wrong):

- A user application address is given. The start of the user application contains the vector table
- From this vector table, the actual application address (reset interrupt vector?) is extracted
- Disable and clear pending interrupts
- Move stack pointer to user application vector table
- The previously extracted application address is finally called

Here is the code so far, for loading a user program:

Code: Select all

void jumpToUser(uint32_t address)
{
  typedef void (*pFunction)(void);
  pFunction Jump_To_Application;

  /*
   * variable that will be loaded with the start address of the application
   */
  __IO uint32_t * JumpAddress;
  const __IO uint32_t * ApplicationAddress = (__IO uint32_t *) address;

  /*
   * get jump address from application vector table
   */
  JumpAddress = (__IO uint32_t *) ApplicationAddress[1];

  /*
   * load this address into function pointer
   */
  Jump_To_Application = (pFunction) JumpAddress;

  /* Stop system? */
  chSysDisable();

  /*
   * Clear pending interrupts
   * Removes the pending state from the PendSV exception.
   */
  SCB_ICSR = ICSR_PENDSVCLR;

  /* Disable all interrupts */
  int i;
  for(i = 0; i < 8; ++i)
    NVIC->ICER[i] = NVIC->IABR[i];

  /* set stack pointer as in application's vector table */
  __set_MSP((uint32_t) (ApplicationAddress[0]));
  Jump_To_Application();
}


I have some problems with compilation on STM32F7 processors.

This:

Code: Select all

  /*
   * Clear pending interrupts
   * Removes the pending state from the PendSV exception.
   */
  SCB_ICSR = ICSR_PENDSVCLR;


does not exist on STM32F7. How can I clear all pending interrupts (or is it even necessary)? I found "__disable_irq()" which disables all interrupts but does not clear pending interrupts. I guess it is similar to what "chSysDisable()" does.

Generally, the user programm shall find conditions just like after a reset. How can I accomplish that?

Thank you in advance,
Adrian

steved
Posts: 825
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: STM32F7 bootloader based on ChibiOS

Postby steved » Tue Mar 20, 2018 5:33 pm

elagil wrote:This:

Code: Select all

  /*
   * Clear pending interrupts
   * Removes the pending state from the PendSV exception.
   */
  SCB_ICSR = ICSR_PENDSVCLR;


does not exist on STM32F7.


I just used:

Code: Select all

SCB->ICSR = (0x1U << 27);//ICSR_PENDSVCLR;

Either it works, or its harmless!

The earlier code in your post looks OK (you probably found the same sources I did!).

This sort of thing does seem to necessitate reading of bits of Arm's programming manual.

elagil
Posts: 92
Joined: Tue Sep 19, 2017 7:38 am
Has thanked: 8 times
Been thanked: 7 times

Re: STM32F7 bootloader based on ChibiOS

Postby elagil » Wed Mar 21, 2018 8:58 am

Thank you for the suggestion. By the way, I just found this defined in /os/common/ext/CMSIS/include/core_cm7.h

Code: Select all

#define SCB_ICSR_PENDSVCLR_Pos             27U                                            /*!< SCB ICSR: PENDSVCLR Position */
#define SCB_ICSR_PENDSVCLR_Msk             (1UL << SCB_ICSR_PENDSVCLR_Pos)                /*!< SCB ICSR: PENDSVCLR Mask */


I will use this for clearing the interrupts.

elagil
Posts: 92
Joined: Tue Sep 19, 2017 7:38 am
Has thanked: 8 times
Been thanked: 7 times

Re: STM32F7 bootloader based on ChibiOS

Postby elagil » Wed Mar 21, 2018 11:26 am

So, the bootloader seems to do something but it does not jump to my user application.

I suspect that I do something wrong when building the user application for use with the bootloader. For enabling bootloader compatibility, I changed the linker file, so that flash0 starts at 0x08004000. I let my bootloader jump to that location as well.

Also, I added

Code: Select all

#define CORTEX_VTOR_INIT 0x4000


to the chconf.h file of my user application.

Am I missing something? When "Jump_To_Application()" is called, the reset handler jumps back to the start of the bootloader.

I noticed that the application start addresses of both the bootloader and the user program are equal (contents of 0x08000000+4 and 0x08004000+4 are both 0x00200201). Is that expected?

User avatar
Giovanni
Site Admin
Posts: 14455
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: STM32F7 bootloader based on ChibiOS

Postby Giovanni » Wed Mar 21, 2018 12:12 pm

Note that CORTEX_VTOR_INIT is no more required in recent versions, VTOR is initialized by crt0.S.

Giovanni

elagil
Posts: 92
Joined: Tue Sep 19, 2017 7:38 am
Has thanked: 8 times
Been thanked: 7 times

Re: STM32F7 bootloader based on ChibiOS

Postby elagil » Wed Mar 21, 2018 12:59 pm

That is good to know. Then, changing the flash start address in the linker file should be enough.

I am still not sure, if the application entry point in my user application (stored at 0x08004000+4) is correct (I assume not).

elagil
Posts: 92
Joined: Tue Sep 19, 2017 7:38 am
Has thanked: 8 times
Been thanked: 7 times

Re: STM32F7 bootloader based on ChibiOS

Postby elagil » Wed Mar 21, 2018 3:33 pm

I noticed that I seem to be using ITCM flash. So, I changed something else in the linker file of the user application:

Code: Select all

    flash1  : org = 0x00204000, len = 1M - 0x4000        /* Flash as ITCM */


This seems to work. I think that the bootloader does jump to the user application now. However, the user application is trapped in this function when I debug it (after the jump):

Code: Select all

/**
 * @brief   Returns the system time.
 *
 * @return              The system time.
 *
 * @notapi
 */
static inline systime_t port_timer_get_time(void) {

  return stGetCounter();
}


Probably, I have to de-initialize all hardware including clock generation. Currently, I am using HSE and PLL for my clock. Can you point me to some resources with regard to completely stopping the system? I found the link to the chibiOS documentation, but it is very generic.

steved
Posts: 825
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 135 times

Re: STM32F7 bootloader based on ChibiOS

Postby steved » Wed Mar 21, 2018 6:19 pm

If bootloader and application clock configurations are the same, there's a #define STM32_NO_INIT which leaves that bit alone. Otherwise you could well be right about the clock setting code making assumptions about register settings.
The clocks are initialised by a call from the board file, so you can always write your own replacement routine.
The HAL initialisation also resets many of the peripherals - could that affect something?

It's not quite clear why your code hangs where it does - on a quick look stGetCounter() is just a register read.

elagil
Posts: 92
Joined: Tue Sep 19, 2017 7:38 am
Has thanked: 8 times
Been thanked: 7 times

Re: STM32F7 bootloader based on ChibiOS

Postby elagil » Thu Mar 22, 2018 10:07 am

It seems like there was some driver problem after standby of my PC. After a restart, the bootloader jump works fine and the user application runs!

Thanks for the suggestions.

franek.bmw
Posts: 57
Joined: Thu Mar 13, 2014 11:15 am
Has thanked: 2 times

Re: STM32F7 bootloader based on ChibiOS

Postby franek.bmw » Wed Apr 24, 2019 8:14 am

Hello,

could you share your LD file from applicatoin project?
I have some problems with starting app after jump from bootloader.
Thank you in advance!

Franek


Return to “STM32 Support”

Who is online

Users browsing this forum: No registered users and 11 guests