Bare metal coding function issues

basically, i have been playing with bare metal programming in the atmega328p recently, and i've come across a very weird issue
so basically, i tried to declare other functions within the code i was using, so
void pindothis()
{
}

or stuff like that. But for some reason, the function was being executed even without being called, while main() wasnt executed at all, but it was all solved when i added static void pindothis() to the function, so now i can call them like normal

however, i've come across the exact same problem while using interrupts, so i basically cant use interrupts anymore, as the interrupt function executes instead of main(), the arduino detects an end to said function (as there is no infinite loop there), and thus resets the board, so i cant do absolutely nothing.

anyone has any idea on a solution for this problem?

The first thing you need to do is to read the forum guide in the sticky post near the top of the forum category. This will tell you what you need to post so that others can help you.

The first rule you broke was to post in a form category that clearly states "do not post in this category". Fortunately someone has already moved your post for you.

Topic moved. Please do not post in "Uncategorized"; see the sticky topics in Uncategorized - Arduino Forum.


It might be a good idea to post the code and a wiring / schematic diagram.

That doesn't happen in C/C++, or any other language I can think of.

You don't normally use main() in Arduino code, although you can do it. Instead, you use setup() and loop(). You can declare your own functions and call them from setup() or loop(), or from other functions.

You cannot declare a function inside another function in C/C++, it will give an error message and not compile. Many other languages do allow this.

Since you joined just a few minutes ago, what are the chances that your conclusions are incorrect, as already pointed out about several of your conclusions by another helper?
Perhaps adopting (as documented in a pinned post) a different attitude and simply saying Here is my code, here is my wiring diagram, I expected this behaviour, but got the following behaviour. Things I tried are x, y, z.
That might solicit a more helpful response.

What is your definition of "bare metal"? Does it include / exclude the following:

  • Arduino framework
  • Arduino libraries
  • c run time library
  • code to do initialisation before your main() is executed, e.g. set up stack pointer.

How are you creating the machine code that you run?

It sounds to be like your code is just being run from the start of the loaded code. Did you do something to cause your bare metal code to start executing from main()?

please just post the code to avoid crystal ball gazers

my mistake original gangster, i was just pretty desperate to solve this issue so i just went straight here without reading anything! Sorry for that, but i'm not quite new to this, i've been at uni for a year now, so i'd say i can draw some pretty concrete conclusions

here is a code example, as you can see here, there's a void function up there that just does absolutely nothing at all, and yet, setting is as void messes everything up! However, when i set it as static void, everything is fixed, and the universe is in harmony.
So, what is happening? As i described earlier, the function is, for some reason, executing even when it's not called, except for whenever it's set as anything other than static.

Hello fellow human person! i am programming using the AVR libc, and AVRDUDE, on an arch linux system.
Dude, the strangest thing of all is this: i have previously also come to the conclusion that it may be just executing at the start of the loaded code, but i tried moving the other declared functions to a couple lines after void main() was declared, and it still didn't work ):
also, a lot of people were criticizing me for it, so here's some example code that i made to illustrate this interesting problem:


as you can see, there's a void function up there that does nothing, but just having it there already messes up the entire code! Weird i know! The solution i found was setting it as static void, yet im not sure why that worked ): And the problem that i now face is with interruptions, as the same thing happens with them and (obviously) i cant set a static ISR()

Sorry for not posting the code before! Thanks a bunch for advising me to do so

Sorry friend! I was pretty desperate to find a solution, so i kinda just went straight here looking for answers, and because of that i just sort of skipped that part, again, sorry! And thanks for the learning experience!

Hello again friend! remember i'm programming it "bare metal", so only using C, no other tools, so i have to use main(). It's the norm. Here's some example code that i made to illustrate my issue:


this code (to blink the led_builtin) only works if i set the void function declared above as static void, for some reason (i tested it many, many times, unfortunately, and lost countless hours of my life) , while just having it as void messes up the entire code
But thank you for your time dear Sir! hope you have great day / night

how do you know PinSoSomething() is being executed?

please use code tags <code>, not images

Hello! Sorry for messing up again with the code screenshot, basically, i made it so that inside the function there was code to turn an led on, and sure enough, without mine calling the function, the led turned on, even though there wasnt code to so so in the main() function, only in this other function that i created that wasnt being called in any part of main()

thank you for your time!

it sounds like you're saying this funciton turned on the LED and the LED turned on without the funciton being called. is this correct?

did you post that code?

yesyes! let me post it:

#include <avr/io.h>
#include <util/delay.h>

void PinDoSomething()
{
        PORTD |= (1 << 3);
}

void main()
{
        DDRD |= (1 << 5);               //PIN3 SET AS OUTPUT
        while (1);
}


i just rewrote this code, tested it again on my arduino uno, and it displays the behaviour i stated before! Strange problem! The led is turning on without mine ever calling the function!

Take a look at where the linker is placing the functions using avr-objdump

[Edit added the following reason for the above suggestion]
This:

Is perhaps suggesting that adding 'static' caused the linker to locate the code for that function differently than it did without it

Dear Friend, thank you for the very useful reply!!!! I shall try at once that which you suggest me to do! I just need to learn how to use avr-objdump
[edit]:
yes, yes, i also think that about the static, something about the mixing up of the program's main function address, that may be getting mixed up with that of the new function

also, i was just testing new code out, where the new function that i just created (the simple void function) lights up an LED, that is not lit up anywhere in the code, i am not calling this function and yet the LED still turns on! So the conclusion is that, indeed, the function is being executed without me calling it anywhere! so strange! hope you can continue to help me in this peculiar issue

Perhaps also take a look at this discussion about minimum start up code requirements.

did you mean

PORTB |= (1 << 5);

and

DDRB |= (1 << 5);