Arduino Leonardo Simple Sketch reserves ~3KB of ISP flash

Hello Community,

I am using the AtMega32U4 for one of my projects

It has 32KB of ISP Flash

I recently checked that it reserves ~3KB (12% of the total flash) of the memory for the simple Arduino sketch having a definition of setup and loop functions.

I want to reduce this,

anyone, please help me to do so.

Thanks and regards.

It contains the Arduino core in that space.
It initializes the functions of USB and other microcontroller peripherals, and further defines useful functions used in sketching.

If you find it annoying, write an int main (void) { } and write everything yourself.
But you lost all of useful features provided by Arduino core.
Of course, you have to do all the initialization of the microcontroller and the USB settings by yourself.

In that case, it's not an Arduino, it's just an ATmega32U4.
So I recommend using Microchip Studio instead of the Arduino IDE for development.

EDIT:
Here is the source for the Arduino core for AVR microcontroller.
A part of this (only the necessary part) was compiled and occupancy that ROM.

@chrisknightley
Yes, I am getting this.

I want to use a microcontroller atmega32U4 which is used in Arduino Leonardo for BL and sketch both.

Now if I upload the code using the ISP will it update the BLS or not?

I don't want to change the bootloader firmware of mega32u4

please help, I am new to it.

Yes.
When writing a sketch with ICSP using a programmer, the bootloader is not written.

Why? If you can use ISP, so you can write back your favorite bootloader.

Hello @chrisknightley

when I connect the Arduino Leonardo to my host it shows HID as well as CDC/ACM port

Is this enabled by Arduino-core or mega32u4 bootloader firmware itself?

No.
I think it is defined by the sketch.

The Leonardo bootloader port is a port that is only momentarily displayed by a software reset when writing.
If you want to force the bootloader mode, press the RESET button twice quickly with Leonardo connected to the USB.

@chrisknightley

yes, that's correct.

I checked by loading the simple empty sketch it is not showing hid port in the device folder. As I included the mouse.h, it is now showing the hid port inside the device folder.

will the fresh atmega32u4 chip be shipped with default bootloader?

where can I find the bootloader firmware of arduino leonardo?

I previously did this for mega16u2 arduino UNO, and it's LUFA bootloader firmware was easy to make.

can't find any good support for mega32u4

Atmel DFU loader is pre-installed and shipped.
It is not arduino.

Just connect ISP to 32U4, select Leonardo in the IDE's board menu and click "Burn bootloader".

@chrisknightley

If I directly connect 32U4 with ISP, should I make changes in board.txt of avrdude.conf?

because IDE will not detect this chip with it's own VID, PID

do I need to change this?

or what if I make the compiled hex file using arduino and later load into this chip?

will this work?

Probably something process is wrong.
The VID and PID of the board are irrelevant when writing with ISP.
Because what about programs that don't use 32U4's USB functionality at all?
If want to transfer that program with use ISP, what is VID and PID? :expressionless:


When using an ISP, the chip signature bytes is directly detected to determine the microcontroller.
This is a fixed value numbered by Microchip.
It is listed in the data sheet and cannot be modified by the user.

https://www.microchip.com/content/dam/mchp/documents/OTH/ProductDocuments/DataSheets/Atmel-7766-8-bit-AVR-ATmega16U4-32U4_Datasheet.pdf#page=356

@chrisknightley

okay, I got it,

I tried and it worked!!

thanks !!

@chrisknightley

but after writing using ISP headers, now I am not able to write directly from the upload button,
I am sure, I selected all the correct configurations

  • If you program a sketch from ISP;
    The bootloader will be removed and the contents of ATmega will be "sketch".

  • If you burn the bootloader from ISP;
    The contents of ATmega will be "bootloader".

The normal upload button works only for the ATmega where the "bootloader" is written.
If do upload, the contents of ATmega will be "bootloader + sketch".


If the bootloader gets in the way of you, you should always use ISP for all programs.

If you want to get the bootloader back, you need to connect ISP and do the burn bootloader.
EDIT:
After did [Burn bootloader] and writing back the bootloader, you don't need an ISP.
You can remove ISP, and program from USB as usual.

@chrisknightley

thanks for your help

it's now done!!

Next, select the post you like and check :white_check_mark: Solution to indicate that it has been resolved and help other users.

Good luck, have fun. :wink:

@chrisknightley

I am trying to upload my sketch to atmega32u4 chip using ISP header.

the chip is fresh from microchip.

arudino IDE shows that the device signature mismatching

I am uploading my program via ISP programmer (arduino NANO as ISP programmer) , by selecting arduino leonardo in tools->boards.

what could be wrong?

I tried writing to actual arduino LEONARDO via ICSP header and it worked!!

Please post a complete error message when trying to write using ISP.

please note I am trying to write to fresh mega32u4 not arduino leonardo board
@chrisknightley

Arduino: 1.8.15 (Linux), Board: "Arduino Leonardo"

Warning: Board arduino:avr:atmegasa16 doesn't define a 'build.board' preference. Auto-set to: AVR_ATMEGASA16
Sketch uses 15260 bytes (53%) of program storage space. Maximum is 28672 bytes.
Global variables use 590 bytes (23%) of dynamic memory, leaving 1970 bytes for local variables. Maximum is 2560 bytes.
/home/rucha/Downloads/arduino-1.8.15/hardware/tools/avr/bin/avrdude -C/home/rucha/Downloads/arduino-1.8.15/hardware/tools/avr/etc/avrdude.conf -v -patmega32u4 -cstk500v1 -P/dev/ttyUSB0 -b19200 -Uflash:w:/tmp/arduino_build_503885/TrackPoint.ino.hex:i 

avrdude: Version 6.3-20190619
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/home/rucha/Downloads/arduino-1.8.15/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/home/rucha/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyUSB0
         Using Programmer              : stk500v1
         Overriding Baud Rate          : 19200
         AVR Part                      : ATmega32U4
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PA0
         RESET disposition             : dedicated
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no       1024    4      0  9000  9000 0x00 0x00
           flash         65     6   128    0 yes     32768  128    256  4500  4500 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : STK500
         Description     : Atmel STK500 Version 1.x firmware
         Hardware Version: 2
         Firmware Version: 1.18
         Topcard         : Unknown
         Vtarget         : 0.0 V
         Varef           : 0.0 V
         Oscillator      : Off
         SCK period      : 0.1 us

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x000000 (retrying)

Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x000000 (retrying)

An error occurred while uploading the sketch
Reading | ################################################## | 100% 0.02s

avrdude: Device signature = 0x000000
avrdude: Yikes!  Invalid device signature.
         Double check connections and try again, or use -F to override
         this check.


avrdude done.  Thank you.



This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

ATmega32U4 is not responding.

Are the crystal and capacitor properly connected?
Also 0.1uF capacitor is required for VCC.

Show me the full schematic of your homebrew board?

@chrisknightley

yes, we have cross-checked all the connections of ISP header and necessary peripherals.

we've already connected 0.1 uF capacitor with all Vcc pins and AVcc.

We also tried programming 32u4 using Arduino Leonardo as ISP programmer