cancel
Showing results for 
Search instead for 
Did you mean: 

Manage private variables of function without having access to function

SeyyedMohammad
Senior III

Using linker script and GCC variable attribute 

char stack[100] __attribute__ ((section (".DTCMRAM"))) = { 0 };

 we can define the location of variables. But I have pre compiled function. I've looked at it's source:

void arm_lms_norm_f32(arm_lms_norm_instance_f32 * S,const float32_t * pSrc,float32_t * pRef,float32_t * pOut,float32_t * pErr,uint32_t blockSize)
{
  float32_t *pState = S->pState;                 /* State pointer */
  float32_t *pCoeffs = S->pCoeffs;               /* Coefficient pointer */
  float32_t *pStateCurnt;                        /* Points to the current sample of the state */
  float32_t *px, *pb;                            /* Temporary pointers for state and coefficient buffers */
  float32_t mu = S->mu;                          /* Adaptive factor */
  uint32_t numTaps = S->numTaps;                 /* Number of filter coefficients in the filter */
  uint32_t tapCnt, blkCnt;                       /* Loop counters */
  float32_t energy;
 
...

As you can see we have variables with auto type (no matter static or auto) that I want is to place function variables to my desired memory section, how can I do that? Ia there any way?

I can use attribute to manage location of input variables, but not the variables inside function.

1 ACCEPTED SOLUTION

Accepted Solutions
gbm
Lead III

Function arguments and local non-static variables are on the stack - by definition, not "forced"; you cannot change this. More or less temporarily they may be (and are) placed by the compiler in processor's registers. No attribute may influence this behavior.

The section attribute may change the placement of code and static data only, where "static" means something different from static keyword in C language and includes all the items declared outside the functions, at file scope.

View solution in original post

9 REPLIES 9
gbm
Lead III

You can do it for static variables declared inside of a function but it requires slightly bigger effort. .DTCMRAM is not a section name. You should modify the linker script, create a section which will be put into DTCMRAM. You should also modify the startup module to initialize your new data section.

What you mean I think will move all the static variable in whole program to DTCM not only the function, doesn't it?

Your code slow down if inner function variables you place to memory.

For example

uint32_t tapCnt, blkCnt;

in function exist only in registers etc.

Piranha
Chief II

Just move the stack to the DTCM and all local variables will be there. That's often the best solution anyway.

Good idea, But How and Why you've predicted that these variables must be places only on the cpu register?

There is no way to force specific function variables zone?

gbm
Lead III

Local non-static variables are on the stack. There is one stack used by all the functions within a thread. It's a good idea to put stack in DTCM and you may do it easily by fiddling with .ld file. Static variables normally go to .data or .bss section. If you don't use DMA with these varaibles, they could also go to DTCM. You may easily put all the variables to DTCM by editing the .ld file but if you want to do it selectively (having some in DTCM and others in ordinary RAM), you need to apply section atributes, modify .ld file and modify the startup module to zero and/or initialize the extra sections you have added.

It might be a good idea to move all "normal" data to DTCM and to use normal RAM only for DMA buffers and big data structures by using section attributes only with them. Then maybe there will be no need to modify startup as you may probably initialize these big items explicitly in your code or maybe they don't need to be initialized at all.

Then if the stack variables are forcibly places on the stack area, even using attribute section will not change their location, doesn't it? Also maybe compiler tries to moving them to cpu register (referring to @MM..1​ ) and __attribute__ will create disruption?

gbm
Lead III

Function arguments and local non-static variables are on the stack - by definition, not "forced"; you cannot change this. More or less temporarily they may be (and are) placed by the compiler in processor's registers. No attribute may influence this behavior.

The section attribute may change the placement of code and static data only, where "static" means something different from static keyword in C language and includes all the items declared outside the functions, at file scope.