cancel
Showing results for 
Search instead for 
Did you mean: 

Calling a custom C function from the startup file (STM32F479) CubeMX

JGrab.2
Associate II

Hi, how can I call C function defined as uint32_t getValue(void) inside startup file (in Reset_Handler routine)? What is needed syntax? I tried importing it and setting it as extern (other standard keywords are not working), but nothing seems to work.

I generated the project using CubeMX and use gcc for compilation. The uC is STM32F479VIT (cortex m4).

1 ACCEPTED SOLUTION

Accepted Solutions

Correct. An int will be returned in r0, no issues.

Check the map file for getValue or similar entries.

If it is a static function that is never called from C, the compiler might optimize it away at higher opt levels?

hth

KnarfB

View solution in original post

10 REPLIES 10

bl getValue

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
JGrab.2
Associate II

Thank you for your reply. When I call it I have linking error undefined reference to `getValue'. Do I need to import it somehow into startup file, or mark it in C source to get it to work?

gbm
Lead III

Check how SystemInit() and main() are called from startup and do the same. However, someho I believe you are on a crash course. Do you realize that you have almost no chance to use the result of this function?

Thanks, but as I understand, my function is defined exactly as SystemInit. That is one of the reasons for asking the question. Yes, maybe I omitted something in the process. I thought there is a list of requirements or something that I could use to double-check.

On the other hand, why is almost no chance to use the result? It wouldn't be stored in one of the Rx registers?

gbm
Lead III

The result cannot be stored in memory unless you fiddle with linker script.

I don't need it in RAM, but as I understand it will be written to the Rx register, so until something overrides the value, it can be used. Or am I mistaken?

Correct. An int will be returned in r0, no issues.

Check the map file for getValue or similar entries.

If it is a static function that is never called from C, the compiler might optimize it away at higher opt levels?

hth

KnarfB

Thank you for the map file hint. The name I found inside was _Z15getValuev and when I used it everything worked as intended.

Oh, this looks like C++ name mangling. You may prevent this by defining the function "extern C" in C++. You're not mistaken with the registers. It's also easy to define RAM space in assembler if needed.

hth

KnarfB