Size optimization for 2k flash (attiny2313)

General discussions about V-USB, our firmware-only implementation of a low speed USB device on Atmel's AVR microcontrollers
Post Reply
jokkebk
Posts: 4
Joined: Tue Apr 12, 2011 10:26 pm

Size optimization for 2k flash (attiny2313)

Post by jokkebk » Tue Apr 12, 2011 10:42 pm

Just downloaded the PowerSwitch demo and compiled it for attiny2313 (I think). The program seems to take 1856 bytes for the V-USB library and the "most basic example program" alone. I think it's a great library but it seems unlikely I will be able to fit much additional logic into the remaining 150 bytes or so - are there any size optimization tips or efforts underway to make it the firmware codebase even smaller (something like 1.2k would be great, although probably not possible : ) in the future?

Thanks and keep up the good work!

Daid

Re: Size optimization for 2k flash (attiny2313)

Post by Daid » Fri Apr 15, 2011 3:01 pm

I don't think V-USB can get much smaller, but can experiment a bit to get the code a bit smaller.

First, 20Mhz seems to generate the smallest code.
Next, including "usbdrv.c" seems to generate larger code in my case, even with the "#define USB_PUBLIC static" define.

With the HID-Mouse example then I'm down to 1782 bytes.

Now, you can remove the Vendor and Device name, which is not allowed when you use the free PID/VID supplied. After removing the default example of "obdev.at" and "Mouse" it saves 68 bytes. Bringing the size down to 1714 bytes.

Removing all the watchdog code from the main.c (just make sure it's never enabled), saves 22 bytes. Down to 1692.

This is a mouse, so the HidReportDescriptor is 52 bytes. But if you want just a single report you can get down to 24 bytes. Saving another 28 bytes.

If you don't use other interrupts then INT0 then you can provide a custom crt0 to put code in the interrupt vector table, after the INT0 vector. And get rid of the __bad_interrupt. Saving another 36 bytes.
As we are hacking crt0 then already, we can get rid of the _exit routine, as we know our main never returns. Removing the jump to _exit, and removing _exit itself saves 6 bytes.

With still a working mouse example, I'm down to 1650 bytes. One of the hid-mouse functions is 122 bytes (advanceCircleByFixedAngle)
So if I get rid of that one, I have about 450~500 bytes to work with in the ATTiny2313.

Daid
Rank 2
Rank 2
Posts: 55
Joined: Mon Apr 18, 2011 12:19 pm

Re: Size optimization for 2k flash (attiny2313)

Post by Daid » Tue Apr 19, 2011 5:03 pm

Just reporting back that I got down to 1200 bytes. Which seems to be the 1.2k you where asking for. You can get down to 1400~1300 bytes by removing the mouse code, and not using usbFunctionRead/usbFunctionWrite.
The last 100~200 bytes where gained by a lot of tiny optimizations.

dzairo
Rank 1
Rank 1
Posts: 35
Joined: Wed Sep 22, 2010 3:31 pm

Re: Size optimization for 2k flash (attiny2313)

Post by dzairo » Wed Apr 20, 2011 5:07 pm

Hi.
I read your post and it's very interesting.
Is possible to make low size byte project and post in this web???
if you can then optimize to minimum code size , there are more people who want it.

regards

Daid
Rank 2
Rank 2
Posts: 55
Joined: Mon Apr 18, 2011 12:19 pm

Re: Size optimization for 2k flash (attiny2313)

Post by Daid » Thu Apr 21, 2011 11:04 pm

My lastest code can be found at:
http://daid2.mine.nu/~daid/tiny2313_vusb.tgz

It stands at 1206 bytes. You could save a few more bytes by moving more variables to GPIOR (GPIOR0 is used as usbMsgFlags, which saves a 8 bytes, the ATTiny2313 has 3 GPIO registors)

Note, I haven't actually tested this version, so it might not work!

klalle
Posts: 1
Joined: Tue May 17, 2011 9:33 pm

Re: Size optimization for 2k flash (attiny2313)

Post by klalle » Tue May 17, 2011 9:57 pm

Hi!
I am a new to the v-usb thing, and i probably have a really stupid question about the filesize:
when i download the powerswitch files (or your reduzed version above) and unzips it, it tells me that the filesize of the .hex is 5,11kB, ( 3,34kB on your reduced version...). Am i trying to upload the wrong .hex file to my attiny2313? (main.hex)?
same size when i recompile the makefile my selves with WinAVR... (not sure i am actually getting the avr-gcc to work!?, but it makes an hex-file anyway)
Of course the files are not even close to fit on my chip!
would be very happy if someone could help me out and send me a working .hex file to upload to my chip!
I just want to be able to use the on and off functions, so i am not really keen on learning all the functions available on the v-usb (yet ;)
Thanks allot for your help!
//Kalle (kalle.lofgren (at) gmail.com)

Daid
Rank 2
Rank 2
Posts: 55
Joined: Mon Apr 18, 2011 12:19 pm

Re: Size optimization for 2k flash (attiny2313)

Post by Daid » Mon May 23, 2011 1:30 pm

Your are right, the HEX file is larger then 2k, because the HEX file is in plain text HEX with extra data (Intel HEX format) So this uses about 2bytes in the hex file for each final byte in the AVR flash.
The correct way to get the size of the final flash is by using the "avr-size" tool on the hex file.

Note, if you are new to V-USB I recommend you use a larger chip then the ATTiny2313, this chip is very limited and you are better off with a larger ATMega.

ploravi
Posts: 2
Joined: Sun Jul 31, 2011 11:36 am

Re: Size optimization for 2k flash (attiny2313)

Post by ploravi » Mon Aug 01, 2011 6:40 pm

Daid, i checkd your code, as you mentioned it is not working. but i did few modificaions and used with my app. seems it doing wel. thanks a lot.

AReaderr

Re: Size optimization for 2k flash (attiny2313)

Post by AReaderr » Tue Aug 02, 2011 9:49 pm

Hi Ploravi,

now that you fixed it a patch would be great ;-)

Cheers

Post Reply