Author Topic: I'm sure I've messed up the bootloader/firmware somehow.....  (Read 15528 times)

xamboozi

  • Newbie
  • *
  • Posts: 4
I'm sure I've messed up the bootloader/firmware somehow.....
« on: January 11, 2013, 12:49:54 pm »
I believe I may have messed up my brand new Digispark... I believe what I did was flash the cdc232.hex file to it, and currently it will not blink after 5 seconds or accept any sketches.

I understand Micronucleus is a bootloader for the AtTiny85. I don't understand which hex file is which. I see two directories within the micronucleus zip archive with hex files in them: /firmware/releases/ and /commandline/ . What is the difference between the cdc232.hex, littlewire_v11.hex, and the micronucleus-1.04.hex? Which hex files are applied to the stock Digispark?

I can successfully flash both the cdc232, and the littlewire hex files to it, but I cannot flash the micronucleus-1.04(I don't have a programmer and the micronucleus executable says the hex is to large). I currently cannot write any sketches to the Digispark when the cdc232, or the littlewire hex is on it.

If anyone has a clear understanding of how everything relates to each other, I would love to learn. What is a bootloader and what is its relation to the Digispark? What about the Firmware? Where are sketches stored? Since I don't quite understand this, I feel like a caveman bashing rocks against it just to see what it does :D

TL;DR How do I just reset my Digispark back to stock?
« Last Edit: January 11, 2013, 12:49:54 pm by xamboozi »

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
I\'m sure I\'ve messed up the bootloader/firmware somehow.....
« Reply #1 on: January 11, 2013, 01:05:26 pm »
Bootloader (micronucleaus) - is the the code that is installed on the device at the factory using an avr programmer. This code allows the Digispark to act like a USB device, receives code, and when it receives code erase the code previously loaded. It also runs the code loaded onto it after a 5 second delay if it does not receive a request to upload new code within that 5 seconds.  You cannot upload micronucleaus to the device because it has to be put on the device with an avr programmer (in other words can not be used to program itself).

A hex file is a way to store data, in this case compiled code for an avr microcontroller. It is a common file format and something being a hex file does not mean it can be used on the Digispark. When you use the Arduino IDE to upload a file to the Digispark your code is compiled into a hex file and then uploaded using the command line tool which we\'ve built into Arduino.

On to the issue you are having:

The Digispark will no longer flash after 5 seconds once you have loaded any code on to it - this is because you\'ve replaced the code that made it flash.

When you try to upload a sketch from the Arduino IDE - what message to you receive when it cannot upload the sketch? Have you gone through the instructions and troubleshooting on the wiki: http://digistump.com/wiki ?

To put the blink program back on the Digispark you\'d upload the Examples->Digispark->Start sketch , the bootloader in all of our testing won\'t be corrupted with normal usage - that isn\'t to say that someone won\'t find a way to break it though, the only issue I see here is that you can\'t upload sketches - please provide the details requested above and we\'ll see what we can figure out.

Thanks,
Erik

xamboozi

  • Newbie
  • *
  • Posts: 4
I\'m sure I\'ve messed up the bootloader/firmware somehow.....
« Reply #2 on: January 11, 2013, 01:29:36 pm »
\"in other words can not be used to program itself\" - PHEW!

Then what exactly is the cdc232.hex file? I was able to write that hex to the Digispark via the compiled micronucleus executable in Ubuntu.

I was attempting to use the Arduino IDE on Ubuntu, but I was having a ton of difficulty and finally gave up when it asked me to plug in the Digispark and errored with

micronucleus: library/micronucleus_lib.c:63: micronucleus_connect: Assertion `res >= 4` failed.
> Please plug in the device....
> Press CTRL+C to terminate the program.
Aborted (core dumped)

So I switched to Windows but was having problems with Windows recognizing the device even after installing the drivers successfully. The Digispark was coming up with the classic yellow exclamation point.

Just as I tried it again to look for what Windows was calling it when it wasn\'t recognized, suddenly it started working again! The Arduino IDE programmed it immediately.

I would really like to get the Arduino IDE working on Ubuntu however, as I\'m trying to migrate all of my stuff to Ubuntu.

xamboozi

  • Newbie
  • *
  • Posts: 4
I\'m sure I\'ve messed up the bootloader/firmware somehow.....
« Reply #3 on: January 11, 2013, 01:41:36 pm »
Aaaaaaaaand I feel like an idiot.... Not only is my Windows box working now, but after reading the Troubleshooting Guide for Linux which is in big bold cant-miss-even-if-you-tried letters all over the place, my Ubuntu box is working as well.

Thanks for the help:D

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
I\'m sure I\'ve messed up the bootloader/firmware somehow.....
« Reply #4 on: January 11, 2013, 01:46:55 pm »
cdc232 is a version of this project: http://www.recursion.jp/avrcdc/cdc-232.html that runs on the Digispark, Bluebie, the maker of micronucleus included this in the micronucleus repository for people who might want it - basically it makes a Digispark into a cheap USB to serial converter.

Glad you got it working!
« Last Edit: January 11, 2013, 01:47:10 pm by digistump »

xamboozi

  • Newbie
  • *
  • Posts: 4
I\'m sure I\'ve messed up the bootloader/firmware somehow.....
« Reply #5 on: January 11, 2013, 02:02:55 pm »
Ok I think I understand now. The cdc232.hex is just the running program which is equivalent to anything I would write in the Arduino IDE. When I wrote the example blink sketch to it just now, the cdc232 program was overwritten.

I think my confusion may have come from both the bootloader and the micronucleus example programs being hex files and very little distinction between them.

Well this is just cool. I have a USB to serial converter for $12! I\'m really starting to love this little thing.

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
I\'m sure I\'ve messed up the bootloader/firmware somehow.....
« Reply #6 on: January 11, 2013, 02:41:24 pm »
For the more technically curious, here\'s how micronucleus works:

Micronucleus is a small V-USB program, similar to the DigiUSB, DigiKeyboard, and other usb related libraries in the digispark arduino software. Normally programs exist at the very beginning of the flash memory in the attiny85 chip, but micronucleus has been modified so the start of the program is about 6kb of 0xFF bytes. After that, micronucleus begins and uses up the final 2kb. This leaves room at the start of the chip for your own programs, but micronucleus always stays installed at the end. 0xFF bytes are interpreted as NOP (no operation) instructions by the AVR chip, so the first time you run it, or if you run it after an erase but no write (sometimes this happens if there\'s an error during the erase part of an upload attempt), next time the chip turns on it will execute all those NOPs and slam in to the bootloader code. This is called a NOP sled, which is a rather interesting concept used extensively in space exploration and computer hacking!

When you use micronucleus to upload a program, there\'s a trick to it - USB requires the device always respond to requests, but the tiny85 chip can\'t do that - whenever it\'s erasing or writing part of it\'s own program memory it has to go to sleep for about 4.5 milliseconds. Some of the more expensive chips like the mega328 have special bootloader support which lets them keep running in the background while an erase or write happens in another section of memory. Embedded Creations discovered however that if you craft your computer program to just not send any requests during that frozen time, the computer never notices the device has frozen up and doesn\'t crash the USB connection. This is pretty fragile, which is why the USB connection to the bootloader can sometimes crash if you run other intense usb software in the background, like an instance of digiterm polling for a device to appear.

So when the micronucleus command line tool first finds a digispark, it asks it \"How much memory do you have, and how long should I wait after each type of request?\" - when you see that assertion fail on ubuntu, it\'s talking about that request - the program tried to ask that question and had an error response due to some annoying linux permissions things. Next, it asks the device to erase it\'s memory and waits the right amount of time for it to do so - about 50 milliseconds to do all 6kb of flash pages. Once that\'s done, it starts uploading 64 byte chunks of your new program. Micronucleus writes in these bytes at the starting 6kb of flash memory, but with one special exception:

In the first page there\'s an interrupt vector table. The bootloader (on the device) replaces the reset vector and the pinchange vector with jump instructions pointing to it\'s own interrupt vector table 6kb later. Other than that, the program is left alone.

When the computer is finished uploading, the bootloader finally writes down what the original values of the user\'s reset vector and pinchange vector were in the very last four bytes of that first 6kb chunk of blank memory.

This little modification ensures the bootloader will run first when the chip is powered, and the pinchange interrupt is necessary for V-USB on the device to function in the bootloader. But wait - the user program needs to be able to use the V-USB to talk over USB as well! Embedded Creations came up with a really neat solution for that in their USBaspLoader-tiny85 project: Whenever the bootloader is running a special part of memory contains 0xB007 - whenever the pin change interrupt handler function is run inside of the bootloader, it checks if those two bytes are there, and if not, it immediately jumps to the user program\'s pinchange handler. This detect and jump behaviour is fast enough to not cause any problems with the V-USB software, but does mean other programs using PCINT (pin change interrupt) on the digispark will find there\'s a slightly longer delay before their function runs than there is on a raw chip with no bootloader.

For more information on the tricks micronucleus uses to add a bootloader on a chip with no built in bootloader features, check out embedded-creations\' USBaspLoader-tiny85 site.

So that\'s how that works.

Finally, I don\'t recommend anyone upload anything to their digisparks that they found in the micronucleus repository. Especially please stay away from the upgrader! It could ruin your digisparks and isn\'t ready for general use!! Digispark comes with micronucleus 1.02 installed - all the other versions are less reliable and while they work great on my computers, they might not work at all on yours, which would leave you no way to downgrade your chip back to 1.02 unless you have a high voltage serial programmer!

If you like uploading mysterious hex files, try this one: mystery.hex - see if you can figure out what it does! ^_^ <3

illwill

  • Newbie
  • *
  • Posts: 8
I\'m sure I\'ve messed up the bootloader/firmware somehow.....
« Reply #7 on: January 11, 2013, 09:07:58 pm »
im trying to figure out how to get this working on ubuntu, anyone have a better guide than the cryptic one in the getting started section?

tried installing arduino from apt-get then copying the files from https://github.com/digistump/DigisparkArduinoIntegration/archive/master.zip to the /usr/share/arduino folder

then grabbing the already compiled avrdude and micronucleus from this zip:http://s3.amazonaws.com/digispark/builds/arduino-1.03-linux32-digispark-2013-01-05.zip
and putting them in the /usr/share/arduino/hardware/tools folder

i think its almost working

the final error i get is:
In file included from Keyboard.cpp:1:0:
/usr/share/arduino/libraries/DigisparkKeyboard/DigiKeyboard.h: In constructor ?DigiKeyboardDevice::DigiKeyboardDevice()?:
/usr/share/arduino/libraries/DigisparkKeyboard/DigiKeyboard.h:133:5: error: ?TIMSK? was not declared in this scope
« Last Edit: January 11, 2013, 09:35:57 pm by illwill »

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
I\'m sure I\'ve messed up the bootloader/firmware somehow.....
« Reply #8 on: January 11, 2013, 09:38:03 pm »
Did you select Digispark from Tools->Board and Tools->Programmer?

illwill

  • Newbie
  • *
  • Posts: 8
I\'m sure I\'ve messed up the bootloader/firmware somehow.....
« Reply #9 on: January 11, 2013, 11:09:09 pm »
oops forgot to set that again.

here\'s the new error:

In file included from /usr/lib/gcc/avr/4.5.3/../../../avr/include/util/delay.h:44:0,
                 from /usr/lib/gcc/avr/4.5.3/../../../avr/include/avr/delay.h:37,
                 from /usr/share/arduino/hardware/tiny-digispark/cores/tiny/wiring_private.h:32,
                 from /usr/share/arduino/hardware/tiny-digispark/cores/tiny/WInterrupts.c:37:
/usr/lib/gcc/avr/4.5.3/../../../avr/include/math.h:426:15: error: expected identifier or ?(? before ?double?
/usr/lib/gcc/avr/4.5.3/../../../avr/include/math.h:426:15: error: expected ?)? before ?>=? token

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
I\'m sure I\'ve messed up the bootloader/firmware somehow.....
« Reply #10 on: January 11, 2013, 11:29:52 pm »
Is this with an example? And if so which one? And have you made any changes?

illwill

  • Newbie
  • *
  • Posts: 8
I\'m sure I\'ve messed up the bootloader/firmware somehow.....
« Reply #11 on: January 11, 2013, 11:38:04 pm »
trying out the keyboard example , didnt change anything in the sketch

this one is from the \"start\" sketch example:
In file included from /usr/lib/gcc/avr/4.5.3/../../../avr/include/util/delay.h:44:0,
                 from /usr/lib/gcc/avr/4.5.3/../../../avr/include/avr/delay.h:37,
                 from /usr/share/arduino/hardware/tiny-digispark/cores/tiny/wiring_private.h:32,
                 from /usr/share/arduino/hardware/tiny-digispark/cores/tiny/WInterrupts.c:37:
/usr/lib/gcc/avr/4.5.3/../../../avr/include/math.h:426:15: error: expected identifier or ?(? before ?double?
/usr/lib/gcc/avr/4.5.3/../../../avr/include/math.h:426:15: error: expected ?)? before ?>=? token

digistump

  • Administrator
  • Hero Member
  • *****
  • Posts: 1465
I\'m sure I\'ve messed up the bootloader/firmware somehow.....
« Reply #12 on: January 11, 2013, 11:51:42 pm »
I\'m thinking this hasn\'t come up before because people have been downloading the arduino copy from arduino.cc instead of using apt-get - but I could be wrong about that

Anyway here is what some google-ing found

http://mattgreensmith.wordpress.com/2011/11/24/installing-arduino-0023-on-ubuntu-11-10-oneiric-ocelot/
 about half way down the post

illwill

  • Newbie
  • *
  • Posts: 8
I\'m sure I\'ve messed up the bootloader/firmware somehow.....
« Reply #13 on: January 12, 2013, 09:11:51 am »
OK This is what i tried next to get it to work:
Dled the arduino zip from arduino.cc and extracted to desktop
copied the folders from
https://github.com/digistump/DigisparkArduinoIntegration/archive/master.zip
into the arduino folder on the desktop
copied the avrdude and micronucleus binaries into the /hardware/tools folder then right clicked them both and checkmarked \"allow executing file as a program\"

everything seemed to work, now im doing this remotely 2 cities away, from nxclient so i cant plugin the digistump when it says to, but so far so good :)
ill let you know when i can check it out tonight
« Last Edit: January 12, 2013, 09:12:11 am by illwill »