Skip to main content
JGrab.2
Associate
April 13, 2022
Solved

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

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).

This topic has been closed for replies.
KnarfB
KnarfBBest answer
Super User
April 13, 2022

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

10 replies

Tesla DeLorean
Guru
April 13, 2022

bl getValue

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
JGrab.2
JGrab.2Author
Associate
April 13, 2022

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
Super User
April 13, 2022

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?

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
JGrab.2
JGrab.2Author
Associate
April 13, 2022

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?

KnarfB
KnarfBBest answer
Super User
April 13, 2022

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

gbm
Super User
April 13, 2022

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

My STM32 stuff on github - compact USB device stack and more: https://github.com/gbm-ii/gbmUSBdevice
JGrab.2
JGrab.2Author
Associate
April 13, 2022

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?