Arduino Community Logo: Arduino.landARDUINO.LANDCODE LIBRARIESSmallSetup

An AVR specific start up code to replace the Arduino default.

About SmallSetup

The SmallSetup library is designed to minimize flash usage for AVR based controllers using the Arduino core.
Even though the functionality is Arduino specific, the code used to achieve the result is available using AVR Studio or anything that uses AVR-GCC as the compiler. The code modifies the initial program flow by taking advantage of the predefined memory sections provided by the Arduino/AVR-GCC tool chain. There is a special subset of these memory sections which runs as a single 'function' and is useful for storing all the initialization code that is only ever needed once. For more information, visit the AVR documentation for memory sections .

Depending on your particular application, savings can be from 20 to hundreds of bytes. For example, the BlinkWithoutDelay sample sketch compiles 184 bytes smaller on an Arduino Uno while using this library. This library maintains the use of the Arduino standard functions setup() and loop(). If you wish to use a standard C++ setup visit this article for direction: Can I use int main() with Arduino?
This code will not improve anything when using Arduino boards such as the Due or Zero as they are not AVR based. There are also a few Arduino boards which incorporate AVR chips in their design, these include the Arduino Tre, and Yun boards, which can utilize this library.

How to use

The simplest way to use this library is to download the code and place the header in your Arduino libraries folder, or if you only want the code for a single project, you can place it directly in your desired sketch folder. Once done, all you have to do is include the file at the top of your sketch. If the file is added as a library use angular brackets for the include, otherwise for local sketch files use double quotes ( <> or "" ).

#include <SmallSetup.h>

void setup() {

}

void loop() {

}

The next method involves inlining setup() and loop() which can usually provide an additional reduction in code size, however experimenting is encouraged as your particular usage may prove otherwise.

inline void setup() {

}

inline void loop() {

}

#include <SmallSetup.h>

Source

If you wish to download or clone the library ( or add an issue ), you can visit the library's home on GitHub here.

//Written by Christopher Andrews, Provided under MIT & GPL licences.
#if defined( __AVR__ ) && !defined( HEADER_SMALLSETUP )
	#define HEADER_SMALLSETUP
	
	#if ARDUINO >= 157
		inline void yield( void ){ return; }
	#endif
	
	#include "wiring.c"

	/*** init runs before constructors ***/
	__attribute__ ((naked)) __attribute__ ((section (".init5"))) void init();
	
	/*** USBAttach runs after constructors ***/
	#if defined( USBCON )
		__attribute__ ((naked)) __attribute__ ((section (".init7"))) void USBAttach();
		void USBAttach(){ USBDevice.attach(); }
	#endif	
	
	/*** setup runs after USBAttach to ensure Serial is active ***/
	__attribute__ ((naked)) __attribute__ ((section (".init8")))  void setup();

	/*** main() runs as normal, only contains runtime code ***/
	__attribute__ ((naked)) int main();
	
	#if defined( __AVR_ATtiny24__ ) || defined( __AVR_ATtiny25__ ) || \
		defined( __AVR_ATtiny44__ ) || defined( __AVR_ATtiny45__ ) || \
		defined( __AVR_ATtiny85__ ) || defined( __AVR_ATtiny85__ ) || \
		defined( __AVR_ATtiny167__ ) || defined( __AVR_ATtiny861__ ) || \
		defined( __AVR_ATtiny2313__ ) || defined( __AVR_ATtiny4313__ )
		int main(){ return (({while( loop(), true );}),0x00); }
	#else
		int main(){ return (({while( ( serialEventRun ? serialEventRun() : ( void ) 0 ), loop(), true );}),0x00); }
	#endif
#endif

And that is it, there is nothing else you have to do. If you happen to have a scenario which has adverse effects on flash size, please report an issue here, thanks.

Tags
Source
Comments

Comment system coming soon.

Designed & created by Christopher Andrews. 2014