Arduino IDE with ATTINY2313, how?

Well, I managed to use an Arduino UNO as an ISP programmer to get a Arduino bootloader into an ATmega328, so I guess I could use the very same setup for uploading code into an attiny2313.

Now, I installed the Arduino-tiny and the Alternate-Core core files in order to compile some code for the attiny but I always get .hex files bigger than 2KB (which is the maximum an attiny2313 could load).

What am I doing wrong? I tryed compiling the very basic Blink sketch. Is the IDE loading some unnended libraries that make the .hex file so big? What’s going on in the background? I have no clue right now. Any hint/guide/suggestion would be very much appreciated.

Roberto

robitabu:
Well, I managed to use an Arduino UNO as an ISP programmer to get a Arduino bootloader into an ATmega328, so I guess I could use the very same setup for uploading code into an attiny2313.

Yes, you can.

.hex files bigger than 2KB

The dot-hex files are bigger than 2 KB or the binary sketch size reported by the IDE is more than 2 KB?

I was talking about the .hex file the IDE saves in a temporary directory before uploading it to mcu.

The IDE reports a "Binary sketch size: 788 bytes".

I was talking about the .hex file the IDE saves in a temporary directory before uploading it to mcu.

The size of the dot-hex file is always more than double the size of the data that is uploaded to the processor (the binary program data). I suggest you ignore the size of the dot-hex file.

The IDE reports a "Binary sketch size: 788 bytes".

That's the number of interest. The sketch uses 788 bytes of program space (Flash). You have 1260 bytes left to do with as you please.

Good to know. I probably misunderstood something I've read in the past regarding those .hex files. Thanks :slight_smile:

Tryed and working fine :slight_smile:
Now I can move on with the prototyping ...

Blinking is good but I still have to clear out how to correctly call the attiny2313 pins inside the Arduino IDE.
I modified the blink sketch using "digitalWrite(1, HIGH)" since attiny2313 doesn't have a pin13 (the one used in the original Arduino example) obviously and I've found out the pin 1 corresponds to attiny2313's pin named PD1. I have to assume PD0..PD6 are digital pins 0..6, and PB0..PB7 are analog pins 0..7, right? Sorry, that's still very confused in my mind.

Mmhh....
Digged into the arduino-tiny source files for my very first time trying to understand something. It looks like it exposes all attiny2313 pins as digital pins to the Arduino IDE, following this scheme:

// ATMEL ATTINY2313
//
//                   +-\/-+
//      (D 17) PA2  1|    |29  VCC
// RX   (D  0) PD0  2|    |19  PB7 (D  16)
// TX   (D  1) PD1  3|    |18  PB6 (D  15)
//      (D  2) PA1  4|    |17  PB5 (D  14)
//      (D  3) PA0  5|    |16  PB4 (D  13)
// INT0 (D  4) PD2  6|    |15  PB3 (D  12)
// INT1 (D  5) PD3  7|    |14  PB2 (D  11)
//      (D  6) PD4  8|    |13  PB1 (D  10)
//     *(D  7) PD5  9|    |12  PB0 (D  9)
//             GND 10|    |11  PD6 (D  8)
//                   +----+
//
// * indicates PWM port

and:

#if defined( __AVR_ATtiny2313__ )

#define PIN_D0  ( 0)
#define PIN_D1  ( 1)
#define PIN_A1  ( 2)
#define PIN_A0  ( 3)
#define PIN_D2  ( 4)
#define PIN_D3  ( 5)
#define PIN_D4  ( 6)
#define PIN_D5  ( 7)
#define PIN_D6  ( 8)
#define PIN_B0  ( 9)
#define PIN_B1  (10)
#define PIN_B2  (11)
#define PIN_B3  (12)
#define PIN_B4  (13)
#define PIN_B5  (14)
#define PIN_B6  (15)
#define PIN_B7  (16)
#define PIN_A2  (17)  /* RESET */

But then ... attiny2313's datasheet state that PA0...PA2 are 3bit bidirectional I/O, PB0...PB7 are 8bit I/O and PD0..PD6 are 7bit I/O. I wonder how it all works together ... and, do I have to consider them all working as digital ports only (I doubt that)?

I will write down some code and try it out myself.
But, of course, any hint from the forum gets a big thank you :slight_smile:

It looks like it exposes all attiny2313 pins as digital pins to the Arduino IDE, following this scheme

Yes.

attiny2313's datasheet state that...

Ah, there it is: the Overview / Pin Descriptions section.

All digital I/O pins on AVR processors can be accessed as a "port". On the 2313 processor, three pins are connected to port A, eight pins to port B, and seven pins to port D. In an Arduino sketch, the "ports" can be read using a "PIN" variable...

  byte ValueOfPortA;
  ValueOfPortA = PINA;

Some digital I/O pins on AVR processors can also be accessed directly using bit manipulation instructions.

But, you needed worry about any of that. pinMode, digitalRead, digitalWrite, and the GCC compiler manage all of those details.

Thank you man for the clarification. I’m testing it all by myself right now, I’m figuring out what the source means and how it works in practice with a few simple input/output tests.

I didn’t have much time for that but … so far I’ve tested digital and analog output.
The only pin not working as expected is the PA2 (D17) which should work as a digital output too, but it doesn’t. In my schematics it’s the tiny’s Reset that gets connected to Arduino’s Digital10. I tested a simple led blink on it (like with the other pins) with and without leaving it connected to Digital10 … no luck, the led stays off :frowning: There’s obviously a reason I don’t know about.

The only pin not working as expected is the PA2 (D17) which should work as a digital output too, but it doesn't. In my schematics it's the tiny's Reset that gets connected to Arduino's Digital10.

The PA2 can be either RESET --or-- a digital I/O pin; it cannot be both. It's function is determined by a fuse setting. In order to program the processor with the equipment you have, PA2 must be left as RESET. If PA2 is changed to a digital I/O pin, the only way to reprogram the processor is: "high voltage" programmer or, I believe, debugWIRE. In other words, if PA2 is changed to a digital I/O pin, you will have to buy a dedicated programmer.

I've already come to the same conclusion reading the datasheet and looking at the Fuse Calculator (where the Reset Disabled option is pretty obvious). Nevertheless, thak you man :slight_smile:

Regarding the "high voltage" stuff ... well, I still don't know about that. I've read somewhere about it but I don't really need this level of complexity right now; so I'll leave the Reset enabled.