How to execute code from RAM (STM32F3 CCM RAM)

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

Moderator: RoccoMarco

Critek
Posts: 4
Joined: Tue Sep 08, 2015 8:30 am

Re: How to execute code from RAM (STM32F3 CCM RAM)

Post by Critek »

I already browsed through many doscussions in order to find what I ma looking for. I think your idea is sensible and will work but I don't have any file named vectors.c
and the only place where I find my vector is in a startup_stm32f3303xc.s. How do you think, is it a good idea to invoke the memcpy function on the begining of the SytemInit() function?
mobyfab
Posts: 484
Joined: Sat Nov 19, 2011 6:47 pm
Has thanked: 21 times
Been thanked: 31 times

Re: How to execute code from RAM (STM32F3 CCM RAM)

Post by mobyfab »

I should work, but you can do a manual copy in the init file, ie:

Code: Select all

#if CRT0_INIT_CCM
  /* CCM segment initialization.*/
  {
    uint32_t *tp, *dp, *end;
    extern vectors_t _vectors_CCM;

    tp = &_siccm;
    dp = &_sccm;
    while (dp < &_eccm)
      *dp++ = *tp++;

    tp = (uint32_t*)&_vectors;
    dp = (uint32_t*)&_vectors_CCM;
    end = (uint32_t*)((&_vectors_CCM)+sizeof(vectors_t));

    while (dp < end)
      *dp++ = *tp++;

   SCB->VTOR = (uint32_t)&_vectors_CCM;
  }
#endif

I think now the init file is in assembly, but that works as well if you know how to do it.

The vector file is here: os/common/ports/ARMCMx/compilers/GCC/vectors.c

I will test this code tonight but it compiles :)
mobyfab
Posts: 484
Joined: Sat Nov 19, 2011 6:47 pm
Has thanked: 21 times
Been thanked: 31 times

Re: How to execute code from RAM (STM32F3 CCM RAM)

Post by mobyfab »

I tried to make it a bit cleaner.

Code: Select all

/*
 * init.c
 */

#include "ch.h"
#include "vectors.h"

/**
 * @brief   CCM segment initialization switch.
 */
#if !defined(CRT0_INIT_CCM) || defined(__DOXYGEN__)
#define CRT0_INIT_CCM              TRUE
#endif

/**
 * @brief   ROM image of the CCM segment start.
 * @pre     The symbol must be aligned to a 32 bits boundary.
 */
extern uint32_t _siccm;

/**
 * @brief   CCM segment start.
 * @pre     The symbol must be aligned to a 32 bits boundary.
 */
extern uint32_t _sccm;

/**
 * @brief   CCM segment end.
 * @pre     The symbol must be aligned to a 32 bits boundary.
 */
extern uint32_t _eccm;

/**
 * @brief   ISR Vector table.
 * @pre     The symbol must be aligned to a 32 bits boundary.
 */
extern vectors_t _vectors;

/**
 * @brief   STM32 CCM vectors table.
 */
vectors_t _vectors_CCM;

void __late_init(void)
{

#if CRT0_INIT_CCM
  /* CCM segment initialization.*/
  {
    uint32_t *tp, *dp, *end;

    tp = &_siccm;
    dp = &_sccm;
    while (dp < &_eccm)
      *dp++ = *tp++;

    tp = (uint32_t*)&_vectors;
    dp = (uint32_t*)&_vectors_CCM;
    end = (uint32_t*)((&_vectors_CCM)+sizeof(vectors_t));

    while (dp < end)
      *dp++ = *tp++;

   SCB->VTOR = (uint32_t)&_vectors_CCM;
  }
#endif
}

Code: Select all

File: bootloader/build/bootloader.elf
    65: 10000000   416 OBJECT  LOCAL  DEFAULT    3 _vectors_CCM.5062
   425: 08000000   416 OBJECT  GLOBAL DEFAULT    1 _vectors


You will have to have a vector.h file with the vector_t typedef from vectors.c
What I did is split the definition out of the c file.

That would be nice if giovanni could do that. (move the vector typedefs to a header file or put some kind of hook/helper fuction to copy/move the table)
User avatar
Giovanni
Site Admin
Posts: 14892
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 996 times

Re: How to execute code from RAM (STM32F3 CCM RAM)

Post by Giovanni »

I will look into it when I return on startup files.

Giovanni
mobyfab
Posts: 484
Joined: Sat Nov 19, 2011 6:47 pm
Has thanked: 21 times
Been thanked: 31 times

Re: How to execute code from RAM (STM32F3 CCM RAM)

Post by mobyfab »

Awesome :)

There is probably some noticeable performance gains here.
Even for devices without CCM, putting the vectors in ram would help.

I tested the code and it works fine, I just had to fix a cast.

Code: Select all

void __late_init(void)
{

#if CRT0_INIT_CCM
  /* CCM segment initialization.*/
  {
    uint32_t *tp, *dp, *end;

    tp = &_siccm;
    dp = &_sccm;
    while (dp < &_eccm)
      *dp++ = *tp++;

    tp = (uint32_t*)&_vectors;
    dp = (uint32_t*)&_vectors_CCM;
    end = ((uint32_t*)(&_vectors_CCM)+sizeof(vectors_t));

    while (dp < end)
      *dp++ = *tp++;

   SCB->VTOR = (uint32_t)&_vectors_CCM;
  }
#endif
}
Post Reply