arduino / Arduino

Arduino IDE 1.x
https://www.arduino.cc/en/software
Other
14.12k stars 7k forks source link

Factory-Fresh Atmegas not correctly initialized when using "Upload using Programmer" #1514

Closed deltaphi closed 9 years ago

deltaphi commented 11 years ago

To reproduce:

For comparison:

For reference: I'm using an Arduino as ISP for programming. I made a similar observation on Atmega8, this may affect other Atmegas as well.

Tested using Arduino 1.0.5 on OSX, although I believe I have seen this since at least Arduino 1.0.3 on both OSX and Windows.

So clearly, uploading a bootloader has a different effect on the Atmega than simply uploading a sketch. I have not seen this behavior documented, so it quickly leads to thinking that you ordered a broken batch of Atmegas.

My personal suspicion, purely from observation without much of an idea of the underlying process: Some Fuse settings are wrong for Arduino on Atmegass fresh from the factory. These settings are not corrected when a sketch is uploaded using the programmer.

The behavior of the Arduino IDE should be changed to correctly initialize the Atmegas. At the very least, this behavior should be documented, e.g., on http://arduino.cc/en/Hacking/Programmer

nospam2000 commented 11 years ago

When you buy a ATmega with factory settings, it is your task to set the fuses correctly, I don't see this as a bug.

Setting the fuses each time when uploading a sketch using an external programmer could be done, but some board definitions don't have fuses defined (e.g. my AT90USB644 with a FLIP/DFU bootloader).

The easiest way to set the fuses is to upload the bootloader using the Arduino IDE.

deltaphi commented 11 years ago

As I stated, this is not a documented behavior, so at the very least this is a documentation bug and should be changed at the link mentioned in the initial post.

On the other hand: Arduino must know the correct fuse settings. Otherwise, how could it set them correctly when flashing the bootloader? Mind you, I'm talking about regular arduino-supported chips, e.g., Atmega8, 168, 328, whichever chip is on the Mega, ... .

When a user adds support for a new device, he has to figure out the correct fuses anyway. If that user choses to find his own way to set the fuses fine, but if he documents them in his boards.txt, shouldn't the Arduino IDE actually use them?

If nothing else, not setting fuses correctly during programmer upload violates the principle of least surprise.

damellis commented 11 years ago

To me, it would be surprising if a command entitled "upload using programmer" also set the fuses.

deltaphi commented 11 years ago

If "Upload using Programmer" should not set the fuses, why should "Burn Bootloader" do it? They are both just uploading a bit of code using a programmer.

In addition, Arduino is all about hiding away technical details of working with the Atmega chips. However, when someone wants to do a little more advanced work, they might switch to working without bootloaders - which will work just fine on a Chip that was previously bootloaded.

But when advancing even further, this will fail miserably when using factory-fresh chips - and there is absolutely no information to help that person along. Especially if there is no other way to determine whether the chips are just broken or it is acually a software problem. (Which, by the way, is pretty much what happened to me.)

As I said previously: While I do not understand your decision not to set fuse bits on "Upload using Programmer", I would be fine if this was a documented problem. However, there isn't even so much as a mention of fuse bits! In contrast, when you try googling 'fuses "upload using programmer" site:arduino.cc' there is about a thousand hits of people on the Arduino forum struggling with fuses. (In addition, googling for this term requires that you figured out that its a fuse problem and not a broken chip - which in turn requires you to know about fuses in the first place, which is nowhere to be found in the documentation.)

niesteszeck commented 11 years ago

I think (just my feeling): that if you have a bootloader into an atmega (with fuses set) why to set again fuses on every upload?

If you load a program directly using a programmer with avr dude, you are advanced enough to set the fuses yourself, or at least you can leanr how to do it.

If you use a factory fresh chip, you have to burn a bootloader (that sets fuses) if you want to program it over serial using arduino, the arduino bl looks at serial communications on boot to watch if there's some program that must be downloaded, if not it just boot the program in memory (this is a resume how I see the bl). Or just set the correct fuses before programming it with other tools (like avrdude) and other kind of uploading interface (like ICSP), but this is not an arduino matter. If you just want to program it with pure avr c code, this is related to avr, not arduino.

The bootloaders help programming the avrs over serial, and not using other method (like ICSP), so for making advanced things the bootloader is still helpfull. To program it without bootloader I recommend only when you need the space that the bl is using, or when you the bootloader itself make something not convenient for your project, like waiting for 2 or more seconds before booting the actual program.

~~if you want to know what ship are you using and if is or not broken I recommend the atmega board detector: http://www.gammon.com.au/forum/?id=11637~~

I don't want to make a discussion here, this is just what i think about arduino + avr + bootloader, and I share this to try to put my grain of sand to the cause

Sorry for talking out of topic. Regarding the real topic, maybe an option is to rewrite the peace in the documentation of programmer mentioned:

This allows you to use the full program space (flash)
 of the chip on the Arduino board

to

...This allows you to use the full program space (flash)
 of the chip on the Arduino board or a bare chip loaded
 with the correct bootloader for the circuit...

This way we avoid the mentioned problem produced for not knowing all the underground work of arduino if someone is trying to learn how to use this option in arduino ide.

Other option, should be add a menu entry to just flash fuses, something like Refuse (can i say it like that? re-fuse?) but I think the other option should be easiest.

deltaphi commented 11 years ago

Note that this question is about flashing using the Arduino IDE, not avrdude! I agree that for using avrdude, coming up with correct fuse settings is a user responsibility.

However, you cannot expect a newbie, possibly using "Upload using Programmer" for the first time, to immediately know about fuse settings (and that "Upload using Programmer" does not even touch the Fuses!). Especially when you just (only!) advertise that "Upload using Programmer" will give you the extra time and space.

matthijskooijman commented 11 years ago

To me, the proposal by niesteszeck doesn't really look useful. While it would solve the problem if people interpret it exactly like it says (on a bare Atmega, first burn a bootloader and then use upload using programmer to get back the full space), I'd expect people to get confused since they might (correctly) think they don't need a bootloader at all in "upload using programmer"-mode.

Having a separate "burn fuses" command seems useful to me, then "Burn bootloader" could be changed to no longer set the fuses perhaps.

In addition to this, perhaps it seems sensible to read out the current fuse values and print a warning if they are not at the expected values? This would mean the fuses have to be read on every upload, but it is a lot better than actually writing them on every upload, I think? This post suggests an avrdude command to read the fuses: http://riktronic.prismaticmedia.com/?p=15

deltaphi commented 11 years ago

If a separate initialization step is needed, it might as well be the existing "Burn bootloader". It doesn't take that long, anyway.

As for documentation, I would suggest something as simple as "Note: Even when you are writing your sketch directly to flash without using the bootloader, every Microcontroller used with the Arduino software must be initialized once by burning a bootloader."

On the other hand, I still do not see the point of not setting the fuses on every upload. When you are using the Arduino IDE, you are working in the Arduino system which will not let you change fuses (and possibly not even work when fuses are set differently?). Therefore, you expect the fuses to be correctly (in so far as you know of fuses at all). What would be a sensible use case, where code is upload using the Arduino IDE but the fuse settings are expected to be non-Arduino-default?

Or is there a technical reason I'm not seeing for limiting the number of times the fuses are set?

As for the proposed solution of reading fuses and printing a warning: If this includes instructions on how to fix the problem (e.g., "This uC is not initialized as an Arduino. You should run "Burn bootloader" once before uploading your sketch"), that would eliminate the problem. A hint in the documentation to avoid a pletzora of new forum thread might still be nice touch, though.

niesteszeck commented 11 years ago

Yes, you are right, my proposal may be confuse. The proposal whit the note "Note: Even when you are writing your sketch directly to flash without using the bootloader, every Microcontroller used with the Arduino software must be initialized once by burning a bootloader." ( @deltaphi ) looks more clear.

On the other hand, if there is an option to just set the fuses, Then the upload bootloader doesn't need to set the fuses every time, maybe this function (burn bl) can check if the fuses are correctly set and if not it run the set fuses before it burn the bootloader. (all this transparently to the user).

Edit: my second option, doesn't guarantee that a newbie have the fuses set when using program using programmer option. hmmmm

husio-org commented 10 years ago

Just found this issue while googling whether "upload using programmer" does or does not set the fuses.

deltapi has a point. The documentation should state it clearly. Sketches will not work as expected with wrong fuse settings.

I always suspected that this was the case, and I have being uploading bootloader to "initialize" the chip just before "upload using programmer".

A nice feature would be a setting: "check/ set fuses before upload using programmer".

Testato commented 10 years ago

In addition to this confusion there is a Bug on the Mega, the "Upload using Programmer" completely not work on Mega, i write an issue on this: https://github.com/arduino/Arduino/issues/388#issuecomment-53134423

matthijskooijman commented 9 years ago

@agdl, what was the reason for closing this? I don't think this is resolved yet - at the very least it needs improved documentation.

Testato commented 9 years ago

+1 It's time for write a "Burn Fuse" option, that pick the correct one from the board.txt

And the burn bootloader+the upload using programmer, will check the fuse before it job, if not equal to the board.txt appear a message that invite to use "Burn Fuse" option

agdl commented 9 years ago

@matthijskooijman sorry, I don't even remember that I closed this issue, maybe a wrong click. However I changed the documentation at https://www.arduino.cc/en/Guide.Environment to state that Burn Bootloader sets the fuses while Upload Using a Programmer doesn't.

deltaphi commented 9 years ago

Thank you very much, @agdl! At least there is now some documentation on this issue. However, when searching for "Programmer" on the Arduino website, the first hit that comes up is https://www.arduino.cc/en/Hacking/Programmer. While that page has been greatly reduced in size since I filed this bug 2 years ago, it still tells you how to upload a Sketch without a bootloader, but does not mention that a factory-fresh Atmega has to be bootloaded anyway. So I maintain that having a hint on handling Factory-fresh Atmegas on that page would be very helpful.

agdl commented 9 years ago

@deltaphi I added this statement:

However you must have in mind that the ''Upload Using Programmer'' procedure doesn't burn fuses so, if you have a fresh factory micro-controller you have to burn the boot-loader first in order to have a properly working device.

in the page you linked. If it is ok for you please close the issue otherwise I will modify it in base of your requests! Thank you!

Testato commented 9 years ago

I only suggest to add to the page that upload using programming do not work on Mega.

deltaphi commented 9 years ago

@agdl Awesome, thank you! I'm closing this issue.

@Testato That would be a different point - I suggest you open a new issue for that point.