Author Topic: Bootloader & Driver for Microprocessor  (Read 5451 times)

Lyncheese

  • Newbie
  • *
  • Posts: 2
Bootloader & Driver for Microprocessor
« on: June 18, 2014, 06:50:53 am »
Hello,

Could someone give some 'enlightment' about what exactly is a bootloader and how to make our own bootloader?

I'm really curious about this.
For example,
Arduino had it's own bootloader and so the Digispark.

Actually what is a bootloader ???
How we can make our own bootloader ???

And then if we already had a bootloader, do we still need a driver ??
If so, how we can make our own driver ???

If I want to make my own programming board like Digispark ATTiny85,
beside the hardware, bootloader and driver.. What else do I need ?

I don't know if this is a silly question or not. If so, I apologize...

But still, I will really appreciate any information regarding to my questions.


Thank you before.

Warm regards.

dougal

  • Sr. Member
  • ****
  • Posts: 289
Re: Bootloader & Driver for Microprocessor
« Reply #1 on: June 20, 2014, 06:13:33 am »
I'll answer what I can (with my imperfect understanding of it), but there are certainly others who have deeper knowledge of this than me. If I get something wrong, hopefully someone will jump in and correct me.

The bootloader is code that lives in protected memory, and is responsible for loading user code (i.e., sketches compiled from the Arduino IDE) into the device. Most devices use a serial interface to transmit the code from the host computer to the Arduino device. Even on most boards that connect by USB, it is actually using a USB-Serial conversion, either on-board, or requiring an extra device like a USB-ASP, or FTDI Friend.

By default, Digispark uses the micronucleus bootloader, which you can find here:
  https://github.com/micronucleus/micronucleus

In the case of the Digispark, it bypasses the hardware cost of a serial converter by incorporating support for low-level USB HID data transfer directly into the bootloader. This USB support is software-based, using "bit-banging" techniques. This is really low-level, not like a USB keyboard or mouse. Basically, all it can do is transfer small data packets back and forth (about 16 bytes per packet, if I remember correctly). And because it's not a "standard" type of USB device, I think this is why Windows requires a driver to recognize it and communicate (Mac OSX and Linux make it a little easier to access the raw USB interfaces). I'm guessing that the primary function of the Windows driver is to make windows recognize the Digispark USB VendorID and ProductID? Maybe Erik can shed light on that, if he can find a few minutes.

Because of this special communication method, micronucleus also requires a special command line tool that knows how to do these data transfers. This is why we need a specially modified version of the Arduino IDE, to support these non-standard ways of loading code into the Digispark.

If you are interested in making your own board, you might like looking at this:
  http://cpldcpu.wordpress.com/2014/04/25/the-nanite-85/

Again, I admit that I could have some details wrong above, but this is what I've interpreted from what I've learned over the past year or so.

Lyncheese

  • Newbie
  • *
  • Posts: 2
Re: Bootloader & Driver for Microprocessor
« Reply #2 on: June 22, 2014, 03:37:25 am »
Hi !

First, Thank you very much for your answer...

I don't think you're wrong about anything... I'm totally agree with what you said.

Actually, I'm using FTDI chip in my own programming board, which I want to build right now
so It can communicate through USB directly.
But, I'm just wondering If I already made the prototype (the hardware), of course It's not as simple as plug it to my PC
and voila ! My computer can recognized it and program it... It will need both bootlader and the driver.
My guess is, the driver's function is for recognizing the programming board and the bootloader is for "booting" so the board ready to accept any program or run it.

I just want to know how they made their own bootloader and the driver... I can't find any useful resources till now.
But, of course I'll keep searching....

Anyway, Thank you for sharing your opinion.
I hope someone with real experince will jump in and sharing his knowledges with us....

Bluebie

  • Sr. Member
  • ****
  • Posts: 486
Re: Bootloader & Driver for Microprocessor
« Reply #3 on: July 09, 2014, 06:59:38 pm »
Hey there! I started the micronucleus project, so I know a bit about it.

@dougal the protected memory thing is a generalisation - the ATtiny85 doesn't have the hardware bootloader support the mega's have, so the bootloader is totally unprotected! This is why digistump and myself discourage using flash/progmem as storage.. need to be very careful if you do!

Dougal is basically right about what the bootloader does and how it functions. The driver on windows is to allow it to recognise the device, so libusb can talk directly to it. I believe it is just the standard libusb driver, customized to micronucleus's vid/pid pair. The driver itself doesn't know anything about bootloaders - it's just lets programs like the micronucleus command line tool talk directly to devices that identify themselves as one. Our bootloader is designed to be extremely compact, and I believe it still is the reigning champ in smallness by far. Adafruit's attempts pale in comparison!

Back to the subject though.. when you make a bootloader, you're making an AVR program which listens to some interface (serial, usb, spi, audio, whatever) and if it detects any traffic within a second or so, it goes in to a mode where it listens and downloads a program, reading bytes in, and erasing and writing to parts of the device's own flash memory. AVR devices can do flash self programming like this. On the attiny85, this means the bootloader buffers in 64 bytes IIRC, then goes to sleep for a couple of milliseconds while the hardware copies those bytes in to flash. On some of the atmega chips like the ones in arduino's the chip doesn't need to sleep while it writes.

When the bootloader is done with any program downloading, it then has to jump in to the program, after which the bootloader is ideally totally inactive. With micronucleus, this hasn't been true in the past - it has to hijack the pin change interrupt, so whenever that interrupt fires, it goes through micronucleus, which detects it isn't running, and jumps in to the user program, adding a little bit of latency. People working on micronucleus now are building a 2.0 version, which will require an updated command line tool, but won't hijack the interrupt and will be quite a lot smaller. It's considered experimental for now but is really promising. I've been busy with other stuff lately so I haven't really been working on micronucleus in a while. It's pretty rad that people like it enough to keep making it better, and in really impressive ways.

Since you're making a device where the AVR will be talking serial, not USB, you have heaps of choices for bootloaders. I strongly recommend using an existing bootloader over trying to make your own. Bootloaders are difficult and complex to debug. TinySafeBoot is a good option. It's about 550 bytes big (micronucleus, depending on the version, is around 2000 bytes!) giving you heaps more space for your own programs. http://jtxp.org/tech/tinysafeboot_en.htm It also supports a single wire version, so you can use it in really cramped environments.