Arduino Leonardo C programming

Hello Gentelmens,

I bought my arduino leonardo and I am planning to program it in C language using arduino IDE.

I try to delete arduino code from editor and just start write program like in atmel studio in way like

#include <avr\io.h>

int main (void)
{
  while (1) {}
  return 0;
}

My problem is that after uploading program like that arduino leonardo is not detected by computer. I cannot upload any other program. Arduino is comming back to live after upload bootloader.

If I am uploading arduino sketch there is no any troubles like that.

Where is problem and how to solve it???

The Arduino IDE expects to see at least the functions setup() and loop(). It deals with the function main() in the background so you don't have to worry about it.

Study the examples that come with the Arduino IDE.

...R

You are welcome to define your own main function but you must understand that the code in the default main function is there for a reason. If you leave this code out then things may not work as they would normally. In this case there is some special USB code that normally gets added to your sketch by the default main function. You can see the default main function here (or at the equivalent path on your computer): https://github.com/arduino/ArduinoCore-avr/blob/master/cores/arduino/main.cpp#L33-L51

int main(void)
{
    init();

    initVariant();

#if defined(USBCON)
    USBDevice.attach();
#endif

    setup();
    
    for (;;) {
        loop();
        if (serialEventRun) serialEventRun();
    }
        
    return 0;
}

The important things are the calls to init() and USBDevice.attach(). I haven't figured out how to call initVariant() from the sketch but luckily that is not used by many Arduino boards, including the Leonardo so don't worry about that.

You're welcome to dig into the core library to see the implementations of init() and USBDevice.attach().

So the last weird thing is this:

if (serialEventRun) serialEventRun();

That code is only used for the serialEvent() feature: https://www.arduino.cc/reference/en/language/functions/communication/serial/serialevent/ Even though almost nobody ever uses this stupid function, it requires code to run on every single loop. That's the nice thing about being able to define your own main(), you can get rid of that garbage.

Remember that the Arduino IDE does some preprocessing of .ino files (which appear as tabs with no extensions in the Arduino IDE). If you don't want that preprocessing then you can add files (AKA tabs) to the sketch with specific extensions like .h, .cpp, .c. Keep in mind that most of the Arduino core library code is in C++. You can use C if you like but you can't call C++ functions from C code. You can call C functions from C++ code, you only need to wrap the declarations in extern "C" {}.

My problem is that after uploading program like that arduino leonardo is not detected by computer. I cannot upload any other program. Arduino is comming back to live after upload bootloader.

What pert said. If you're going to program your Leonardo in "pure C", then you'll have to write (or otherwise include) code to initialize and drive the USB peripheral of the chip.

(PS: doing that is pretty difficult! Try just putting the code you would have put in main() in setup() instead, and let the Arduino IDE/core "do its stuff")