ATtiny10 resources

UPDATE: 
A huge thanks to Keri DuPrey, Nat Blundell and others who have been continually improving the code. The latest version(as of 2014-3-22) is here:
ATtiny4_5_9_10_20_40Programmer.ino

Also, avr-libc 1.7 and newer support ATtiny10! I have not tried it, but I reckon you can now use avr-gcc for the tiny10. However, with only 1kB of program memory, assembly is still useful.

The ATtiny10 is an intriguing little thing. It's the size of a grain of rice and has just 6 little SOT23 pins, but inside lies all the capabilities of an 8-bit AVR microcontroller. Everything from 4 analog input channels to a 16-bit timer with 2 possible pwm outputs to all your basic digital functionality. And at 45 yen a piece, there's little reason not to pick one up to play with. Or two... or ten.

Then once you get home and sit down at your computer, you realize that you are in for a bit more homework than you expected. ICSP doesn't work, common C compilers don't work, hardly anyone online has done DIY stuff with them. 

But hark! Do not forsake hope. I am here to aid you on your journey. First, I will guide you to the references that aided me: (The most useful of these was the last one, but it is in Japanese.)
Hardware
Now let's think about hardware. If you are willing to purchase an AVR programmer, go ahead. It's an easy solution that will definitely work. In that case, you can skip down to the assembly examples.
If you would rather do this the hard way(possibly more educational and satisfying), I'll show you how to program the ATtiny10 with only your Arduino, AVR Studio(or anything that will turn your assembly into a hex file) and a few resistors. Also, if you want to make use of all four I/O pins, you will need a 12V power supply for reprogramming.

Here is the basic hardware setup. Thanks to pcm1723 for this drawing and so much more.
 *                                                *
 * Arduino                 ATtiny10               *
 * ----------+          +----------------         *
 * (SS#)  10 |--[R]-----| 6 (RESET#/PB3)          *
 *           |          |                         *     
 * (MOSI) 11 |--[R]--+--| 1 (TPIDATA/PB0)         *
 *           |       |  |                         *
 * (MISO) 12 |--[R]--+  |                         *
 *           |          |                         *     
 * (SCK)  13 |--[R]-----| 3 (TPICLK/PB1)          *
 * ----------+          +----------------         *
 *                                                *
 *  -[R]-  =  a few killo-Ohm resistor            *
 *                                                *
 * 2011/12/08 by pcm1723                          *
 Really, some of those resistors may be unnecessary, but they don't hurt. You can see that it is making use of the usual SPI pins, and indeed we are going to make use of the SPI library to handle the communication details. Anyway, I got tired of setting up wires on a breadboard, so I just made this little pcb with 2.2kohm resistors that plugs right into the Arduino.  It also has a connector and jumper for applying 12V to the reset pin. This is only necessary if you disable the reset pin for I/O use.
  
Note that pin 1 goes in the top right corner. It faces the same direction as the chip on the arduino. Also note that the ATtiny10 needs to be on an adapter to plug into the DIP socket. I would like the option of using the chips without an adapter, so I want to make something that clamps the chip onto a breakout board without solder. I'll post again if I make something like that.

Software
Well, that was simple enough. Now for the software. 
First, you will need to generate a hex file for your program. One simple and guaranteed to work  method is by using AVR Studio, which is free to download from the Atmel website(here). Set your device to ATtiny10, make a new assembly file project thing, write your code, and click "build solution" in the build menu. Your hex file will be buried in a folder in the AVR Studio projects folder. Look for a folder with your project's name and somewhere inside will be a debug folder. hex is inside of that one.

Then we have to get the program onto the chip.
A big thanks again to pcm1723 for getting me started with the code for the arduino. They posted a simple sketch that uses the TPI interface to read all the memory on the chip and send it via serial to a computer. It was then up to me to extend this code to include all the necessary programming functions. You can find the original code on the fourth link up above. Most of it is included in the code below.

*EDIT* - Thanks to Keri DuPrey for making some nice changes to the code. See the comments below to find a link to the updated code. Changes: supports tiny20 and 40, read,program and verify in one streamlined command, can handle programs larger than 1024 bytes. 

*UPDATE* - Thanks to a commenter for pointing out that the fuse setting and clearing didn't work. With their help I have updated the code and it should work now.

Upload this sketch to an arduino. I ran into a very strange problem where it just wouldn't work right if the sketch size was much larger than 7,000 bytes. It would just hang or reset while programming an ATtiny10. I cannot fathom why this is. There should be more than ample memory on the arduino. If anyone has an idea, please let me know(leave comment on home page). Anyway, this version compiled to 6,996 bytes and worked fine. 
Then power off(unplug) the arduino, connect the ATtiny10 as shown, and power up the arduino again. Open up the serial monitor in the tools menu. Set the serial speed to match the one used by the arduino. The default is 38400. If everything is working so far, you should see the message
  NVM enabled
  ATtiny10 connected
If you don't see this, either you aren't using an ATtiny10 or there could be a hardware problem. After you get this far, you can start using the programming functions. To do this, input the one-character commands described below. Only send one command at a time because some of them require additional input.

Commands:
  • D = dump memory. Displays all current memory on the chip
  • E = erase chip. Erases all program memory
  • R = read program. Sends the program to the arduino. After sending this command  you have 20 seconds to copy and paste the entire contents of a .hex file into the serial monitor and send. You can do the whole file at once. If it was successful, you should see "program received".
  • P = write program. After reading the program with the R command, use this to write the program to the ATtiny10.
  • V = verify. Verifies that the program was written correctly. If not, it will display the errors.
  • F = finish. Not necessary, but this disables further access until the arduino is reset.
  • S = set fuse. Follow the displayed instructions to program one of the three fuses.
  • C = clear fuse. Follow the displayed instructions to UNprogram one of the three fuses.
The 'D' command is quite a useful and harmless way to test your programmer and the chip. It simply reads all the memory on the ATtiny10 and displays it in the serial monitor in an easy to read way. You may want to avoid the 'S' and 'C' commands unless you are sure you want to mess with the fuses. There are only three fuses. One sends the clock signal to a pin, one enables the watchdog timer, and one disables the reset pin so you can use it for I/O. If you disable the reset pin, I hope you have a 12V power supply, because you will need it the next time you want to program the chip.

A little caution: if you want to verify after programming, be sure to press shift+v and not ctrl+v as I have done many times. ctrl+v will probably paste your hex file to the monitor again. If you accidentally send it, you will see a lot of strange behavior as the programmer reads 'C', 'D', 'E', 'F' commands along with a bunch of ignored characters. Don't worry, the worst that could happen is that you will erase your chip and disable further programming until you reset the arduino. Just reset and try again.

Examples
OK, now that you can program the chip the hard part begins. Actually making an interesting program. If you are like me and this is the first time you have used assembly for AVRs, just google "AVR assembly tutorial" and you will find everything you need. It takes a little more work than something like C, so I will give you some simple programs that make use of the various functionality of the chip. They are by no means perfect or interesting, but you can use them as building blocks.
 Good luck.

46 comments:

Anonymous said...

Great, thank you.

Laura Becerra Fajardo said...

Great! I started using Tiny10 in february 2012, and there was almost nothing about it.
There is information in avr freak too.
In addition, if someone hates assembler, another option is to write a C code in CodeVision and copy the asm code in AVRstudio. It is a simple way to avoid asm.

Anonymous said...

About the crash issue. After Taking a quick look at the source code, It looks like the problem may be all the strings that are being printed, each one takes space in ram. I had a similar problem with a project, kept resetting on me, once I moved all the strings to program space I had no more crashes.

Mizotor said...

Hi I am having problems programming the attiny10 using the arduino and your code. The arduino accepts the code and programs then upon verification it has not been programed, any suggestions would be helpful.

ME said...

@Mizotor -
Thanks for trying my code. Here are some ideas:
If the serial monitor says "NVM enabled" and then "ATtiny10 connected" at the top, then your connections are good and you are properly communicating with the chip. If you don't see these messages, check connections and voltage(needs 5V to program). Once that is working, try the "D" command to dump chip memory to the monitor. If you see something that looks like your program in the program memory space, then you are successfully writing your program. In this case, take a close look at the HEX file. Look at the 8th number of each line. If it is not '0' or '1'(usually near the beginning or end of the file). then it is a special command line, and my code is not set up to do anything with these lines since they are really not necessary for the attiny10. Try deleting the line entirely.

If all attempts fail, send me the HEX file and I'll try programming one of mine.
Good luck.

Mizotor said...

Hello Again and thanks for the prompt response.

The arduino replies with NVM enabled and ATtiny10 connected, so as you say i believe the chip is connected correctly. I am using your blink program to test the programming capability and the hex is generated by the atmel studio 6 program and is 5 lines long with the 8th character is either 0/1. This hex is accepted and it states program received, then upon 'P' it states Wrote program 62 of 1024 bytes, this is as i expected but with 'D' it returns FF in all the program memory and as i stated before the verify fails as to be expected with all FF returned. I have put the TPI Data on a scope and it appears to be toggling so i don't think it is held at one.

This is a very confusing problem and i can only think that the reset is not happening so i tried to do a 12v reset but no change.

Any ideas?

Regards Mizotor

Scott8211 said...

Interesting project - I wonder can this be used to reprogram ATtiny20 MPU's?

Keri DuPrey said...
This comment has been removed by the author.
ME said...

@Mizotor -
Sorry I didn't reply sooner. I'm afraid I don't have any idea what's wrong. You are able to enter programming mode and read the memory, so I don't know why you can't write to memory. I really hesitate to suggest a problem with the chip, but if you have another one handy, try a different chip.

If you figure out the problem, please tell me/us about it.

ME said...

@Keri DuPrey -
Thanks! Putting the strings in program memory is a big improvement.

This code could actually work with any ATtiny that uses TPI, but the program size would be limited to 1024 bytes unless you made some significant modification to the code.

The bug concerns me. I have programmed ATtiny10s numerous times without such a problem. I wonder what's happening.

Keri DuPrey said...

I figured out what the "Bug" is. The Tiny10/20 use the same write opcode of 0x1D, but its for a different word count, Word/Dword. So the chip wont write unless all the words have been sent.

Keri DuPrey said...
This comment has been removed by the author.
ME said...

@ Keri DuPrey -
Wow, thanks for that. The programming method is much more streamlined as long as there are no serial buffer issues. I'll update this post to point out your new code.

Keri DuPrey said...
This comment has been removed by the author.
Chris said...

Hi there, thanks for all your efforts put into this! I'm attempting to program at ATtiny10 from an Arduino Uno using the methods described and I'm hoping you could offer some troubleshooting tips.

I have everything wired up and I successfully get the "NVM enabled" and "ATtiny10 connected" when opening the serial monitor. From there, I can send D and it will dump the data from the chip, but will follow the dump with a single "Received unknown command". If I try R, it simply returns with "Received unknown command" twice.

Any idea what the issue might be? I'm not quite sure where to start my troubleshooting. Thanks.

ME said...

@Chris -
Thanks. Are you using my code or the updated version by Keri DuPrey(linked in a comment above)? Also, tell me about your software setup. The "Received unknown command" message appears when any character that is not a command is sent, except when reading in hex data. This includes a newline character. I'm using the current Arduino serial monitor in Windows and I've never seen this problem. I'm wondering if you are automatically sending a newline character. But even then the 'R' command should be accepted.
Unless you are using Keri DuPrey's code which has slightly different commands. Those are documented at the top of that code.

Chris said...

I could have sworn I read the comments at the top at least a couple times! Turns out that's most of the problem: I am using the updated code and should be using P to program. Also, the automatic newline sending is indeed why I was getting the extra error after normal accepted commands.

Now I'm successfully able to program it! One last issue I'm running into though: I have to copy/paste the hex file line by line as I think copy / pasting the entire thing incorrectly converts the line endings and only uploads part of the program before outputting a bunch of unknown command errors.

I am using a mac running 10.7.5, so I'm not sure you would have any suggestions for this sort of issue but I thought couldn't hurt to ask. Copy/paste in general does not seem to work well in the Arduino IDE. What character should each line be separated by?

ME said...

@Chris -
Good, I'm glad you got it working. I don't really know much about a mac, but the actual line ending character doesn't matter. Actually, you don't even need one. The line length info is included in the hex code, so the arduino code does not check for line endings and if there is an extra character that is not ':' it is just ignored. However, if there is more than one extra character, it will cause an error. So maybe you are sending more than one character after the line? Maybe a tab or space character?

Keri DuPrey said...

@Chris -
The problem for programming the entire flash at one time may be from a serial buffer overrun. Try dropping the speed to 9600, or less, I don't have any 10's to test but they might write/verify slower.

Keri DuPrey said...

I have dropped the speed down to 9600.

Chris said...

That was it! It gives me one extra "Received unknown command" error after successful programming, but successfully programs none the less.

Looks like it's time for me to delve into some assembly, thanks for the help getting this working.

Fahad said...

Thanks for the programmer code guys, awesome work. I haven't used it yet, but about to as I am making my first ATtiny10 board. I just have a question about the Hex code for the Attiny10, can I use the arduino IDE itself to write a code, compile that code and then use generated HEX to program ATtint10? How about adding information about attiny10 to the arduino board.txt file?

Fahad said...

What i mean is somehting like this http://elabz.com/arduino-shrunk-how-to-use-attiny13-with-arduino-ide/

but for ATtiny10/9/5/4 instead of ATtiny13

ME said...

@Fahad -
Unfortunately you cannot use the Arduino IDE because the C compiler, avr-gcc, does not support the attiny4/5/8/10 yet. I think the reason is that there are only 16 useable registers while the rest of the attiny series has 32. But with only 1kB or less of program memory, you will probably want to do the coding in assembly anyway.

On the other hand, if you write only assembly code in the arduino IDE in proper C assembly format you may be able to do it. I think it would be simpler to just use AVRStudio which is free on the Atmel website.

Fahad said...

@ME
That is actually what I am trying to avoid, learning AVR C or Assembly.

But you are right with such limited device I think assembly is the way to go. Is there any website that you recommend to start learning about programming those tiny mCUs?

I also found you mosquito code which will help me a lot since I am planing to use an IR receiver as part of my design.
So thank you twice ;)

ME said...

@Fahad -
Get ready for quite a learning experience. Don't rely on your knowledge of other languages because you will need very different techniques with assembly. But don't be discouraged.
First, download the AVR instruction set here. It will be your manual. Also, this guide has some useful info and examples. Then download my small example files on this page and go over them carefully till you understand what's happening.

As for the IR stuff, my code is designed to record a signal of unknown protocol and length. If you are using a protocol that you know, you can make it vastly, vaaastly simpler. I bet you could do it in 20 lines rather than several hundred. If you have any questions, let me know.

Fahad said...

Thanks a lot.
I have two questions:
1- AVR-GCC that comes with AVR Studio 6 has support for attiny10, but the one with arduino doesn't! Don't they both belong to the same project? and both should be GPL and open source? or simply the arduino people are not interested in including the code since they don't use that type mcu?

2- My project will read IR codes sent from any type of IR remote control (so what you did exactly matches what I need) and will compare that code against a code that is stored in the attiny10. I am interested in matching one code only however, I don't want that code to be hard-coded. I rather want the user to be able to reprogram that code to match any remote control button they want. But the attiny10 doesn't have EEPROM, so can we store data into the flash from the running code itself? or do I need to use another chip like the attiny13a.

ME said...

@Fahad -
You're right! avr-libc 1.7 and newer support ATtiny4/5/9/10. Great, this changes everything. Arduino might not come with the latest version of avr-libc(the libraries used by avr-gcc) yet, but I think you could just replace the appropriate files to use the newest version with arduino.

As for the memory question, I'm sorry to say that there is no way to write to flash memory from running code. You only have access to SRAM and the registers. If you really need this feature, ATtiny13 and others will be a better choice.

Anonymous said...

Hi,

first of all: this is a very useful blog, and gives a "nearly" ;-) perfect solution.
Thanks a lot.

But did you already set a fuse on a tiny10? I want to see the System clock on PB2, therefore i issued command 'S' followed by 'c'. But nothing changed. The configuration word still says 0xFF 0xFF.
I think the code for Setting/Clearing the fuses is wrong, as it only writes the LSB.
The mcu waits for the MSB before it really writes the fuses.

Greetings

Mic

ME said...

@Anonymous -
You're right! Thanks for telling me. I never tried changing fuses, so I didn't notice, but I actually left out a few important steps including writing the MSB.
I've updated the code and it should work. I don't have time to test it right now, but if it still doesn't work, let me know.

Also, this update is only in the file provided on this blog. It is not included in Keri DuPrey's newer code yet.

Anonymous said...

Hi, thanks for your quick reply, i tried your code fix, and Setting the fuse works now fine.
Clearing does not. I read the Atmel Manual, and Clearing fuses Needs a SECTION_ERASE instead of WORD_WRITE in NVMCMD Register.

I changed the code in setConfig
to:
setPointer(0x3F40);
writeIO(NVMCMD, (val ? NVM_WORD_WRITE : NVM_SECTION_ERASE) );

which works.

Greetings,
Mic

ME said...

@Anonymous -
Thanks again. I put your fix into the code. I hope there are no more surprises like that. More importantly I hope it hasn't caused too many headaches for people using it.

Keri DuPrey said...

I've made the changes to the code as well.

Nat Blundell said...

This patch for Keri DuPrey's code re-enables the ATtiny after each command. This allows faster iterations of development as you don't need to re-wire the RST line to test.

Index: ATtiny45910Programmer.ino
===================================================================
--- ATtiny45910Programmer.ino (revision 2)
+++ ATtiny45910Programmer.ino (working copy)
@@ -137,6 +137,16 @@
SPI.setDataMode(SPI_MODE0);
SPI.setClockDivider(SPI_CLOCK_DIV32);

+ start_tpi();
+
+ // initialize memory pointer register
+ setPointer(0x0000);
+
+ timeout = 20000;
+ idChecked = false;
+} // end setup()
+
+void start_tpi() {
// enter TPI programming mode
digitalWrite(SS, LOW); // assert RESET on tiny
delay(1); // t_RST min = 400 ns @ Vcc = 5 V
@@ -153,13 +163,7 @@
// wait
}
Serial.println(F("NVM enabled"));
-
- // initialize memory pointer register
- setPointer(0x0000);
-
- timeout = 20000;
- idChecked = false;
-} // end setup()
+}

void loop(){
if(!idChecked){
@@ -171,6 +175,8 @@
while(Serial.available() < 1){
// wait
}
+ start_tpi();
+
// the first byte is a command
//** 'P' = program the ATtiny using the read program
//** 'D' = dump memory to serial monitor
@@ -209,6 +215,8 @@
default:
Serial.println(F("Received unknown command"));
}
+
+ finish();
}
void ERROR_pgmSize(void)
{
@@ -552,7 +560,7 @@
writeCSS(0x00, 0x00);
SPI.transfer(0xff);
SPI.transfer(0xff);
- // digitalWrite(SS, HIGH); // release RESET
+ digitalWrite(SS, HIGH); // release RESET
delay(1); // t_RST min = 400 ns @ Vcc = 5 V
}

Anonymous said...

what arduino did you used?

Anonymus said...

can i use this software with other programmer wich is capable if ISP? I mean :can I use something different from Arduino ? Something similar into Arduino?

ME said...

@Anonymous -
Of course you can use something instead of Arduino. The arduino is essentially just using SPI to act as a TPI programer. Look at the data sheet to see the differences between SPI and TPI, but they are pretty much the same.
Of course the software I have written is specific to arduino, but I imagine it could easily be ported to whatever SPI capable programmer you are using.

Jimmus said...

You are awesome!

It took a bit of fixing to get the tinyblink program to work, but the attiny10programmer.ino worked flawlessly.

I used tavrasm because it was easier for my setup, and I had to change the call instructions to rcall.

I still have to make sure I can re-program after disabling RESET, but it's over there blinking away.

Thank you for your work, and for sharing it with people like me.

ME said...

@Jimmus -
I'm glad I could help.
I haven't tried disabling the reset pin, so good luck.
Have fun!

Keri DuPrey said...

I had an erratic issue with the nibbles being swapped when programming, and made a change that seemed to clear up the problem. I also added Nat Blundell's patch to the code.


ATtiny4_5_9_10_20_40Programmer.ino

ME said...

@Keri DuPrey-
Thank for doing that and thanks for keeping this code up to date. I haven't run into that problem, but I'm glad it was easy to fix.
Also, thanks to Nat Blundell. I think I forgot to thank you for your patch.

Anonymous said...

Keri,
sketch : ATtiny4_5_9_10_20_40Programmer.ino

posted:
March 17, 2014 at 5:45 AM

Line 597 is missing an 'else'

.Causes serial monitor to report TINY and Unknown chip.

Keri DuPrey said...

I've added the missing else statement and a quick reset option. Also, Added High Voltage Programming, on pin 9, to the code, along with select-able High/low enable, depending on the users design for providing 12v.

Anonymous said...

I had to mod the quickReset function to get one of my tiny4's to reset.
I added a SS,HIGH immediately before the existing SS,LOW.
Another tiny4 reset without it.
The offender is at the end of 10 inches of ribbon cable -- in case that helps.

void quickReset()
{
digitalWrite(SS,HIGH); //pull /RST high for an instant
digitalWrite(SS,LOW);
delay(10);
digitalWrite(SS,HIGH);
}

Before adding the SS,HIGH line, I could short the /RST pin to GND, while the SS output was HIGH and the tiny would reset. I have 1K5 resistors as per the diagram in sketch, so the output was safe.

ME said...

@Anonymous -
That's really strange. I'm not sure what flicking the reset line high like that would change. But if it works, great. Thanks for the idea.

Keri DuPrey said...

I've added the quick send to high for the reset. Thanks for the tip!