ATmega2560 bootloader to use more i/o pins

I'm new to the forum, I'm rather a novice at coding, and despite my exhaustive search, I have not found much information to help me. The Arduino MEGA Rev3 does not make use of all the I/O pins, and I want to know if it's possible to use all the pins (if broken out to headers), if a modified bootloader exists that utilizes all the pins, or how difficult it would be for me to make a modified bootloader myself. While I don't know much about the '2560, I've been teaching myself a lot about the '168/'328 series, and have built my own series of boards based on the pro-mini. Is this a simple matter of adding some methods in the source code using the physical pin addresses, or will it roast what little programming skill I have?

The bootloader does not determine the IO pin usage. That is defined in the pins_arduino.h file that goes with a board.
The bootloader only controls talking over the serial port after a reset to see if serial download will happen, or if the sketch will start up.

Okay. I've skimmed through the file, and I see some tables full of things that look like pin assignments, but I'm not sure how to interpret, let alone change, them. Any tips?

In -0023, I would have confidently said yes.
In 1.0, I am much less sure.

A good start would finding the 3 sections where the pins are assigned.
The first assigns the port assignments to the D#s, starting with D0 and working up.
The second assigns the bits of the port called out in the first, these can be in order, or they can be jumbled.
The third assigns whether the bit has a timer associated with it (so it can be PWM'd).

After that, more research is needed. Somehow the analog pins are assigned digital #s also, or maybe the other way around - the digital pin assignments are given analog numbers if they are the ADC inputs.

Hope that gets you started.

With all respect, if you thought that the bootloader had any thing to do with pin assignment, then id advise you to do a lot of research first on the AVR way of working and coding, and the way the Arduino IDE works through it by minimizing our need to set it all !

iyahdub, You are missing linemouth's intent.
The Mega does not bring out all the ATMega2560's IO pins. Only 54 of 82 or some similar quantity. The goal was to have access to more of them. One way to do that is to update pins_arduino.h so the rest are accessible from the IDE.

Okay, it looks like I've located the sections of pin assignments, and it's making vague sense. Now, it would be nice to have the pins in physical order instead of their port order, and I'm noticing that some pins have timers associated with them that aren't used. I also see that all four rx/tx serial ports use PWM (OSC[0..4][A..D]) pins- is this required hardware for the serial to work, or can any digital pin do the trick? On this graphic http://arduino.cc/en/Hacking/PinMapping2560, I see a mismatch- on the chip side, it has the four rx/tx ports scattered on all four sides of the chip, while the arduino pin map (red) has them grouped on the left edge. Does this say that those non-pwm pins can be used for rx/tx, too?

The pin assignment go from D0 to D54. If you are making your own pins_arduinio.h you can change the order however you'd like.
Whether a pin is connected to a UART and is PWMable is defined by the actual hardware, so have the ATMega2560 data sheet handy as you go.

I would agree, the stuff in Red on the top of that link appears to be a little off.

Yeah i did miss his point then...Soz
Its good i did then, cause ill start a development of a project on a mega quite soon, and its definitely good to know that then !!

Oh, hey! In pins_arduino.c, it says:
// ATMEL ATMEGA1280 / ARDUINO
//
// 0-7 PE0-PE7 works
// 8-13 PB0-PB5 works
// 14-21 PA0-PA7 works
// 22-29 PH0-PH7 works
// 30-35 PG5-PG0 works
// 36-43 PC7-PC0 works
// 44-51 PJ7-PJ0 works
// 52-59 PL7-PL0 works
// 60-67 PD7-PD0 works
// A0-A7 PF0-PF7
// A8-A15 PK0-PK7
That totals to 68 digital and 16 analog (84I/O), and the 128 is the same as the 256 in everything except for memory size, right? With this commented, I can basically just use these pins for I/O.

Now, the PWM timers- are they hardwired to particular ports/pins, or can they be assigned to operate on a specific pin?

The hardware PWM timers are hardwired (part of the ATMega chip architecture) to particular ports/pins.

It is possible to do software PWM as well, but that is not part of pins_arduino.h

Okay, I'm going through the datasheet and I'm mapping the capabilities of each pin. Once I do that, I should have a clear path to my goal. Thanks so much for the help!

Keep us updated later on then, if you dont mind of your finding, please !!

I reviewed the schematic vs the pin mapping page, everything is in sync.
Some signal names are brought out to floating headers, with the schematic showing more clearly that TX3/RX3 are D14/D15 on the COMMUNICATION header, while the notes in Red on the mapping page are not so clear, and the mapping table Not showing them on physical pins 64/63 but instead has physical pins 14/15 (PH2/3) seeming to indicate they are TX3/RX3. So the Red Notes and the table could use a little cleanup.

They do agree (almost - pin 14/PH2 should be blank) that there are 16 pins available to be mapped.

Not a problem! :slight_smile: In that previous message, the pins_arduino.c says that just about every pin is available for I/O, the exception being PB6 + 7. Why this is, I'm not sure, and these pins ARE used on the official MEGA. I'm pretty certain, then, that these pins can be used, giving us a total of 70 digital and 16 analog pins. Among the digital are four UART ports, one SPI port, one JTAG port, and I2C/TWI port, and several pins that can be used for an external parallel memory interface (16 address/data pins and three strobes). There are 15 pins with PWM connections, and the official MEGA uses RX0 and TX0 for PWM as well. It's strange, though, that pin 26 (PB7/ digital pin 13) has two PWM timers wired to it: 0A and 1C. Anyway, that's 17 PWMs that I can see, and only two of them coincide with communication pins (unlike the ATmega328, which uses half its PWMs for SPI pins. I've tabulated my findings so far below:

A couple things to note: there are 11 ports ranging from A-L, excluding I. Port G only has six pins.

A Com PWM Int I/O
PA0 D
PA1 D
PA2 D
PA3 D
PA4 D
PA5 D
PA6 D
PA7 D
B Com PWM Int I/O
PB0 SS PC0 D
PB1 SCK PC1 D
PB2 MOSI PC2 D
PB3 MISO PC3 D
PB4 2A PC4 D
PB5 1A PC5 D
PB6 1B PC6 D
PB7 0A+1C PC7 D
C Com PWM Int I/O
PC0 D
PC1 D
PC2 D
PC3 D
PC4 D
PC5 D
PC6 D
PC7 D
D Com PWM Int I/O
PD0 SCL EX0 D
PD1 SDA EX1 D
PD2 RX1 EX2 D
PD3 TX1 EX3 D
PD4 D
PD5 D
PD6 D
PD7 D
E Com PWM Int I/O
PE0 RX0 PC8 D
PE1 TX0 D
PE2 D
PE3 3A D
PE4 3B EX4 D
PE5 3C EX5 D
PE6 EX6 D
PE7 EX7 D
F Com PWM Int I/O
PF0 A
PF1 A
PF2 A
PF3 A
PF4 TCK A
PF5 TMS A
PF6 TDO A
PF7 TDI A
G Com PWM Int I/O
PG0 wr D
PG1 rd D
PG2 ale D
PG3 D
PG4 D
PG5 0B D
H Com PWM Int I/O
PH0 RX2 D
PH1 TX2 D
PH2 D
PH3 4A D
PH4 4B D
PH5 4C D
PH6 2B D
PH7 D
J Com PWM Int I/O
PJ0 RX3 PC9 D
PJ1 TX3 PC10 D
PJ2 PC11 D
PJ3 PC12 D
PJ4 PC13 D
PJ5 PC14 D
PJ6 PC15 D
PJ7 D
K Com PWM Int I/O
PK0 PC16 A
PK1 PC17 A
PK2 PC18 A
PK3 PC19 A
PK4 PC20 A
PK5 PC21 A
PK6 PC22 A
PK7 PC23 A
L Com PWM Int I/O
PL0 D
PL1 D
PL2 D
PL3 5A D
PL4 5B D
PL5 5C D
PL6 D
PL7 D

Hmm, that loses a lot in formatting!
Would help to include the D# or A# assigments currently used.
Odd about PB6/7, shown on the schematic as D12/13.

Hello CrossRoad
Its been a while since this post came up
It looks like I need your help

I went through the post and tried to understand what to do
I have built a PCB using AT2560 (blank) ion my board
I am burning the board using ISP MKII which works fine (I only take th HEX file from the Arduino compiler and burn the file using AVR studio) this is the only way I can do it.

Any way ...
I found out that some ot the pins doesnt work
For example pin A4 on the Arduino board(93 on the Mega chip) , when I burn my Sketch on the Arduino Board it works fine but when i try to burn it on my cosume board it doesnt do anything!!!

Now the questions...
Assuming that I have a bootloader (which I found in the Arduino_pin.h)
What do I need to do with it
Should I burn it every time I load a new sketch ?
Or should I burn it on the EEPROM

I will try to ask it in a different way ...
Assuming I have a sketch that al it does is toggling that pin I was talking about,
The sketch is very small and contains of a very small amount of lines.
Do I need to add aline that calls the H file ?

Please advise (if possible with an example)

Thank you so much for your help

With a new chip, you only need to burn the bootloader one time, you do that to set the fuses for correct operation.
You can set the fuses with Atmel Studio also, I used to do that for 1284 chips. Eventually I got tired of the really long time it took for Atmel Studio to start and now I use the Arduino IDE & load the bootloader to set the fuses. It takes a little longer to run, but not nearly as long as Atmel Studio took to start up.
Once the fuses are set, they're set.
The pins being used and their mapping are set from within pins_arduino.h. When the sketch compiles, it uses the information from that file, so it should be included in the .hex file you are downloading.
If the pin isn't working, check the soldering of the board. Perhaps the pin is not soldered and not making contact. You can use a small wooden stick and try pushing it down gently while the sketch runs, see if the output starts changing.

CrossRoad you are great
Thank you for you quick replay
But as you probobbly thought.....I have few more questions :roll_eyes:

Iused to work with AVR (i.e. Atmega32 )and burned them with the ISP MKII.
Never used a Bootloader but every time Iplayed with the fuses something went wrong and I had to burn them in High votage using the STK500 or some thing like that !
So I am very scared when I hear the word Fuses!!

  1. If I would brn the Bootloader would I be able to run it over if something went wrog?
  2. Can I try to burn Bootloader on my ATmega2560 arduino Board- would it damage the USB port (I wont be able to load program via the USB?)
  3. Should I burn the Bootloader that is in the arduino compiler
  4. How can I burn the Bootloader - is it aregular Hexfile that I can load using the studio ?
  5. If I burn the Bootloader, and then burn a sketch (lets say the blink one) wouldnt it run over the bootloader?
  6. why am I asking too much questions.....because I built aPCB and put about 3000$ on assmbley and design and I am so scared to damadge it and I have a very rough dead line

Thank you again for your help

  1. Yes indeed. The MKii is pretty versatile. I have never locked up a chip using it and the Arduino standard fuse settings.
  2. If the bootload failed, just load it again.
  3. Yes, use the Ardiuno bootloader.
  4. Yes, you tell Atmel Studio what file you want to load into the boot flash space.
  5. Yes, uploading a sketch via ISCP wipes out the bootloader - but the fuses do not change. Using the bootloader to load via the Serial interface does not affect the bootloader.
  6. As long as you have an ICSP header and a serial header, you should have a lot of flexibility program loading wise.