I am glad you liked my answer, but to be honest, I didn't, because I was sort of guessing at some things. Not that they were really wrong or anything like that, but I just didn't know exactly what was going on.
So, I couldn't sleep (because it is 30c inside even though it is 20c outside), I figured I'd set up an atmega328p on a breadboard, and see if I could provide better instructions.
I will tell you some of what I did, so you can recreate it, and experiment.
But, the big conclusion, which is very important, and why things do not work as you'd expect, is the following:
If you put an atmega on a breadboard, and provide power to it, so you can program its flash, it will be running whatever is loaded inside it, and that can give errors and problems flashing.
What does that mean? Well, when the chip gets power, it starts running whatever it is on the flash. So, if you then tell it to program itsself, it will do that, but at the same time, keep interacting with the flash to keep running whatever it is running, so it doesn't write things properly, and corrupt the flash-area.
The good thing is, you can tell avrdude to erase the chip, which will stop it running, and then you can upload new content. So 'corrupt the flash-area' sounds scary and dangerous, but it's not that bad fortunatly.
Lucky for us, there is an exception. If you flash the bootloader to the chip, it will not run the 'sketch' area, because that will be empty, and you can upload stuff and keep the bootloader intact, and it will verify and work correctly.
Of course, after uploading, the chip will reset, the bootloader starts, which sees there is content, and start running that program, so it only works on a chip that only has the bootloader.
First, I don't have an usbtinyisp (yet), so as programmer, I used an arduino, with the ArduinoISP sketch on it.
I know you do have an usbtinyisp, but I'll still write it like I did, so you can follow along and should get the same results. After that, I'll write how to make it work for usbtinyisp.
I also don't have any crystals, so I changed arduinoIsp a little, to generate an 8Mhz signal, which I use to get the chip on the breadboard going. (I didn't know how to do that myself though. But somebody did!)
Unfortunatly, the pin that is used for that (because it is connected to the timer) is already in use by arduinoisp for an led, so I also had to change that.
So, instead of
#define LED_HB 9
I made it:
#define LED_HB 6
( I also don't have any LEDs connected, but better safe than sorry )
In the setup function, below the led-stuff, so near the end of that, I put the following:
- Code: Select all
// set up 8 MHz timer on pin 9
// Code from Atmega Board Programmer by Nick Gammon
// at http://www.gammon.com.au/forum/?id=11635&page=999
// Also info at http://www.gammon.com.au/forum/?id=11643
pinMode (9, OUTPUT);
// set up Timer 1
TCCR1A = _BV (COM1A0); // toggle OC1A on Compare Match
TCCR1B = _BV(WGM12) | _BV(CS10); // CTC, no prescaling
OCR1A = 0; // output every cycle
After I had that running, I went to build the breadboard. Generally, I used the bare breadboard-layout for uploading the bootloader that is at http://arduino.cc/en/Tutorial/ArduinoToBreadboard
It provides a good overview, but the pictures are a bit small, so, I load up my changed arduinoIsp sketch in the arduino-ide, with the list of names, and the pins they have to be connected to on the arduino.
Now, if I did this a lot, I would definatly get some of the stickers
from adafruit , but I don't, so instead, I also grab the datasheets from the Documents section at http://www.atmel.com/devices/atmega328p.aspx
. Then, using a pdf-reader, I zoom in on the pin-layout, and hold the breadboard so the chip faces the same direction.
Then, I first connect the groundrails on the breadboard to both gnd pins, and the powerrails to vcc and avcc
And then, it is just searching jumperwires from the list in the arduino-ide, to the pins on the breadboard. I do one at the time, so it is easier, and do reset, sck, miso, and mosi.
Then, I connect pin 9 on the arduino to tosc1, because I don't have that scheme on my screen, I can just count the pins and see if they are connected properly. then I connect ground and voltage on the breadboard to a gnd pin on the arduino, and the 5v pin.
Oh, and of course that is with the arduino not being powered. Because only after checking all the wires again, I connect the arduino to my computer.
Remember, my arduino is connected to Com4, so you may have to change that
Then, to verify arduinoisp is working and seeing the chip, I run:
avrdude -pm328p -cstk500v1 -P\\.\COM4 -b19200
The sketch talks stk500v1 so we specify that, and as speed only 19200, because the sketch can't handle faster.
If it works, it should say something about the AVR device being ready. Much like your first screenshot, except it won't complain about an expected signature.
After that, I wanted to put the bootloader on, and use it as a backup in case the chip on my arduino failed, so I select my board, the serial port, and then as programmer Arduino as Isp.
Then, I clicked Burn Bootloader, and it went to do that.
Now, after that, came a lot of diagnosis, to figure out why things worked only sometimes, involving a lot of flashing, erasing, and exchanging of chips on the breadboard and the arduino, to see what worked, and what didn't. I'll save you that, cause this is long enough already! But if you really want to know how I figure it out, ask.
Anyway, knowing what I knew after that, I would recommend the following after the bootloader. Of course we can just burn the bootloader again with the IDE, but maybe for some reason that doesn't work or you don't want to use that. So I'll act like you have this nice chip with bootloader, but no ide (no idea how you compile sketches then, but I'm not going to figure out everything!)
First, I would make a copy of the contents of the chip with bootloader, so I would do either:
avrdude -pm328p -cstk500v1 -P\\.\COM4 -b19200 -Uflash:r:"bootloader.bin":r
for making a copy in raw format, or
avrdude -pm328p -cstk500v1 -P\\.\COM4 -b19200 -Uflash:r:"bootloader.hex":i
for a copy in intel-hex-format (or both!)
Then, if I wanted to put something on it, but keep the bootloader intact, you would go
avrdude -pm328p -cstk500v1 -P\\.\COM4 -b19200 -D -Uflash:w:"program.hex":i
However, if there was something already on it, I would have to clear it and program the bootloader again,
so I would go:
avrdude -pm328p -cstk500v1 -P\\.\COM4 -b19200 -e
avrdude -pm328p -cstk500v1 -P\\.\COM4 -b19200 -Uflash:w:"bootloader.bin":r
avrdude -pm328p -cstk500v1 -P\\.\COM4 -b19200 -Uflash:w:"bootloader.hex":i
One idea I do have, is that it should be possible to blank the first one or two bytes, by writing FF (255) to them, but I haven't found a way to do that.
Anyway, now about tinyusb.
If all that works, I would pull all the cables from arduino to breadboard, but otherwise keep the breadboard the same, upload a sketch to the arduino, that did not have arduinoisp, but only the lines for 8Mhz on pin 9. disconnect the arduino, and replace the pin 9 to tosc1 cable.
Then, I would connect the 6-pin cable to the breadboard, at the right places. 'But,' I hear you think 'I have no idea what the connections re on the cable'.
Well, don't worry! Again, if I did this a lot, I would make a cable like this: http://forums.adafruit.com/viewtopic.php?f=44&t=20284
because the colours are pretty.
However, you could also use one of the breadboard-breakout-thingies you can find at http://arduino.cc/en/Main/Standalone
For now though, that page is handy for another reason. If you have wired up your 6-pin cable correctly and connected it correctly to your usbtinyisp, it has a picture of the cable from the bottom, so you can see which is pin 1 and so on, and below that, a similar image with labels that show what the pin-layout is.
So with that, you can connect miso, mosi, reset and sck to the same places you connected them from the arduino, and vcc and gnd to the rails on your breadboard.
The avrdude commands should then be the same, except that the protocol etc is different, so for example, instead of
avrdude -pm328p -cstk500v1 -P\\.\COM4 -b19200 -e
you would write
avrdude -pm328p -cusbtiny -e
If you have more questions, or need help, post, and if things work like I said, feel free to write that too, so I know I didn't write all this for nothing (but really so other people will know it does actually work)