Starting "bare metal" programming...before I melt my uC a question

Go To Last Post
20 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

 

(I am on linux)

 

since I want to start something different, I want to learn how to program the "bare metal" with C.

That is: AVRTools (https://github.com/igormiktor/AV...) + avr-gcc + avr-libc + avrdude + vim....no Arduino (WHICH IS *NO* EXPLICIT

NOR IMPLICIT CRITISM TOWARDS ARDUINO!)

The "bare metal" is in my case an Inhaos Mega CORE mini - a ATmega2560 board which breakout

every pin of the uC.

But before I melt my board a question:

Is it possible to harm the uC by accident using software only and attaching only digital voltmeters or oscilloscope inputs

to the pins (when keeping of my fingers of the fuses)?

Or, the other way round: What is the safest way to experiment with pin settings, control registers etc... without the fear

to burn something?

 

Thanks a lot for any help in advance!

Cheers

mcc

 

This topic has a solution.
Last Edited: Wed. Sep 13, 2017 - 06:18 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 You can't harm your chip by overloading it with too heavy calculations or something like that.

The only way in which you can damage it is by overloading the pins with too much current.

This could happen happen if you short an output or apply an external voltage where it doesn't belong. Or of course by connecting GND and VCC the wrong way around or applying too much voltage to those pins.

And one thing that gets often forgotten: ESD. In my experience the Atmel (or should I say Microchip) parts are very resistant against ESD, but others might have other experiences there.

 

You mention only DMMs and an Oscilloscope. As long as you don't make short circuits, you should be safe as these devices usually don't source any current and sink very little.

And concerning the fuses, just be careful. Most things can be fixed with a little trickery though.

 

Just a little recommendation should you ever decide to buy more hardware: For beginners, the cheap Arduino Uno knockoffs with the AtMega in the socket are very handy. The boards are dirt cheap so if you damage it, it doesn't hurt that much. And if it's "only" the AtMega that's burnt, you can replace just the chip for even cheaper.

 

-Patrick

 

Edit: One more thing: You asked for the safest way to experiment: I don't really recommend it, but you can just leave all ports as inputs and toggle only the PORT register. This switches the pull-up on and off, that should be enough to monitor the pin as long as you also provide a weak(!, >100k) pull-down resistor. The advantage is that a short circuit doesn't do any harm at all, but the problem is that the pins have a small parasitic capacitance, which leads to an RC circuit. The time constant might just be too long to see anything useful on the scope, and you can't even reliably drive the input of another chip.

"Some people die at 25 and aren't buried until 75." -Benjamin Franklin

 

What is life's greatest illusion?"  "Innocence, my brother." -Skyrim

 

Last Edited: Tue. Sep 12, 2017 - 06:06 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mcc wrote:

...avrdude...Is it possible to harm the uC by accident using software only...

 

Just be very very sure that you understand the role of the various 'fuses' that can be set within the processor. Especially those related to clock selection. Whilst you can't kill the processor, you can certainly make it go deaf and getting it back is a right PITA.

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

mcc wrote:
....no Arduino (WHICH IS *NO* EXPLICIT NOR IMPLICIT CRITISM TOWARDS ARDUINO!)
mcc wrote:
The "bare metal" is in my case an Inhaos Mega CORE mini - a ATmega2560 board which breakout every pin of the uC.

OK, I'll bite. 

 

You pay $10 -$15 for that board.  For somewhat less, you get an Arduino with the same processor, e.g.

 

When you write your first program(s) and load onto each of the boards, which is more "bare metal" and why?

 

When you progress and want to explore attaching devices for which Arduino shields are readily available, which starting platform would be easier to lash up?

 

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, shorted output pins are probably the biggest danger, followed by ESD.  If you want, post your first one or two LED blinky programs (and connections) so other eyes can check them.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi pawi77,

 

thanks for your reply ! :)

 

It sounds, as I would be relatively save when keeping off my fingers of fuse settings and use mainly measurement

equioment to check for the results of my doing.

 

One thing: The Inhaos Mega CORE mini (in opposite to the Arduino MEGA) breaks out all pins of the ATmega2560.

Before using it I did an image copy of the flash (*.hex and *.bin). I searched the bin-copy via the UNIX command

'strings' for ...strings (hence the name of the tool, hahahahaha :) and found lot stuff, what is in the original

Arduino bootloader.

That could imply, that the extra pins of the CORE are initialized somehow.

Is there a program, which I can flash onto my CORE, which dumps all configuration register settings (so to say

"the contents of the datasheet of the ATmega2560 ;) to serial out?

It may also be a good starting point of information.

 

Last question (before the next, I fear... ;)

I read that the bootloader of the ATmega2560 can be put into a special mode by sending a couple of '!' via

serial to the bootloader right after pressing RESET.

The strings output (see above) matches some of the menu entries of that bootloader too.

I did that and the blinking of the LED changed to a constant light...something happened.

Sending a 'L' results in lot of garbage output on the serial monitor.

I tried a lot...but...What are the correct settings for the serial monitor to change that

garbage into readable something?

 

On the Maker Faire (Hannover) I recently bought an original Arduino UNO R3, which survived me a little more than two weeks:

I think the ATmega32u4 (the USB-prozessor on the board) got fried while connecting the UNO with my Inhaos CORE mini via

ISP -- no /dev/ttyACM0 will appear when the UNO is plugged in anymore.

I pulled out the ATmega328 to prevent, that it will pull the RESET pin permamently if that one got fried...but...no, still

no USB-device comes up.

If someone has an idea to revive or repair my UNO...I would be happy...

 

I know of sources like Aliexpress and it is my main source of electronic stuff. I bought the original UNO R3 to support Arduino.

 

Thanks a lot for any given information and any support in advance!

Cheers

mcc

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think the ATmega32u4 (the USB-prozessor on the board) got fried while connecting the UNO with my Inhaos CORE mini via

ISP

Have you tried re-flashing the 32U4's firmware?  Perhaps that's what happened in the first place.  The UNO R3 has two 6-pin ISP headers.  One for the 328P, one for the 32U4.  Which one did you use?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

H i Brian,

 

thanks for your reply ! :)

 

for the first, I will keep off my finger of any fuse settings. Or are there any commands to avrdude, which are not obviously fuse setters but do

some fuse settings in the background?

 

Cheers

mcc

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi theusch,

 

thanks for your reply! :)

 

...sorry...I am no native speaker and confused some things as it seems.

 

With "bare metal" in opposite to "Arduino" I dont meant the hardware...I own a lot of

RobotDyn ProMinis and like them a lot.

With "bare metal" programming I meant: Using "pure" C and doing all that register

settings by hand instead of using "the cosy home" of the Arduino libraries.

I think, I would learn a little more about uC programming when urged to read and

understand the datasheet.

 

Cheers

mcc

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi kk6gm,

 

thanks for your reply ! :)

 

Thank you for your offer to check my sources. As soon I wil have some, I will post them here...

 

Cheers

mcc

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi joeymorin,

 

thanks for reply ! ;)

 

Man, your are a genious!!! YES ! THAT could have happened! :) :) : :)

Where can I find the firmware...I think "burn bootloader" of the Arduino IDE is the wrong place... ???

 

Cheers

mcc

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A new chip might be $1--$4...The biggest danger is actually that you will blow out your PC  by sending it 12V , 24V  or what ever down the board USB cable by mistake...get yourself a USB isolator. 

 

Spend your time in isolation---you'll be safer.

When in the dark remember-the future looks brighter than ever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

All the UNO R3 I have use the 16U2, not the 32U4.  Are you sure yours is a 32U4?  For that matter, are you sure you've got an UNO R3?  The Leonardo uses the 32U4, and >>only<< that.  It's a single-chip board.

 

If you've got at 16U2, then the firmware you want can be found in the firmware folder, somewhere in your Arduino software folder.  The file you want is:

Arduino-COMBINED-dfu-usbserial-atmega16u2-Uno-Rev3.hex

 

 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi joeymorin,

 

...it is an 16u2...you were right.

 

I flashed the chip with this command (LINUX!)

 

avrdude -C/usr/local/arduino-1.8.3/hardware/tools/avr/etc/avrdude.conf -v -pm16u2 -cbuspirate -P/dev/ttyUSB0 -D -Uflash:w:/usr/local/arduino-1.8.4/hardware/arduino/avr/firmwares/atmegaxxu2/Arduino-COMBINED-dfu-usbserial-atmega16u2-Uno-Rev3.hex:i

 

which works fine and avrdude was happy with its work afterwards.

 

I wasn't that happy though...still no USB device under /dev

 

Cheers

mcc

 

 

 

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Passing -D to avrdude will forgo the chip erase.  You don't want that.  Try it again without -D.

What are the fuses set to?  They should be:

lfuse=0xEF

hfuse=0xD9

efuse=0xF4

 

To read them:

avrdude -C/usr/local/arduino-1.8.3/hardware/tools/avr/etc/avrdude.conf -v -pm16u2 -cbuspirate -P/dev/ttyUSB0 -t

This places you into interactive mode.  Then type:

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.08s

avrdude: Device signature = 0x1e9489
avrdude> dump lfuse
>>> dump lfuse
0000  ef                                                |.               |

avrdude> dump hfuse
>>> dump hfuse
0000  d9                                                |.               |

avrdude>
avrdude> dump efuse
>>> dump efuse
0000  f4                                                |.               |

avrdude> quit
>>> quit 

avrdude: safemode: Fuses OK

avrdude done.  Thank you.

Do NOT change fuses unless you know what you are doing.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi joeymorin,

 

I reattached my Buspirate to the UNO. The first of the commands you gave me results in:

avrdude -C/usr/local/arduino-1.8.3/hardware/tools/avr/etc/avrdude.conf -v -pm16u2 -cbuspirate -P/dev/ttyUSB0 -t

avrdude: Version 6.3, compiled on Jan 17 2017 at 11:00:16
         Copyright (c) 2000-2005 Brian Dean, http://www.bdmicro.com/
         Copyright (c) 2007-2014 Joerg Wunsch

         System wide configuration file is "/usr/local/arduino-1.8.3/hardware/tools/avr/etc/avrdude.conf"
         User configuration file is "/home/mccramer/.avrduderc"
         User configuration file does not exist or is not a regular file, skipping

         Using Port                    : /dev/ttyUSB0
         Using Programmer              : buspirate
         AVR Part                      : ATmega16U2
         Chip Erase delay              : 9000 us
         PAGEL                         : PD7
         BS2                           : PC6
         RESET disposition             : possible i/o
         RETRY pulse                   : SCK
         serial program mode           : yes
         parallel program mode         : yes
         Timeout                       : 200
         StabDelay                     : 100
         CmdexeDelay                   : 25
         SyncLoops                     : 32
         ByteDelay                     : 0
         PollIndex                     : 3
         PollValue                     : 0x53
         Memory Detail                 :

                                  Block Poll               Page                       Polled
           Memory Type Mode Delay Size  Indx Paged  Size   Size #Pages MinW  MaxW   ReadBack
           ----------- ---- ----- ----- ---- ------ ------ ---- ------ ----- ----- ---------
           eeprom        65    20     4    0 no        512    4    128  9000  9000 0x00 0x00
           flash         65     6   128    0 yes     16384  128    128  4500  4500 0x00 0x00
           lfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           hfuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           efuse          0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           lock           0     0     0    0 no          1    0      0  9000  9000 0x00 0x00
           calibration    0     0     0    0 no          1    0      0     0     0 0x00 0x00
           signature      0     0     0    0 no          3    0      0     0     0 0x00 0x00

         Programmer Type : BusPirate
         Description     : The Bus Pirate

Attempting to initiate BusPirate binary mode...
BusPirate binmode version: 1
BusPirate SPI version: 1
avrdude: Paged flash write enabled.
AVR Extended Commands version 1
avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.09s

avrdude: Device signature = 0x1e9489 (probably m16u2)
avrdude: safemode: hfuse reads as D9
avrdude: safemode: efuse reads as F4

avrdude>

 

This looks nice so far

Following your posting

avrdude> dump lfuse
>>> dump lfuse
0000  ef                                                |.               |

avrdude> dump hfuse
>>> dump hfuse
0000  d9                                                |.               |

avrdude> dump efuse
>>> dump efuse
0000  f4                                                |.               |

avrdude> quit
>>> quit

avrdude: safemode: hfuse reads as D9
avrdude: safemode: efuse reads as F4
avrdude: safemode: Fuses OK (E:F4, H:D9, L:EF)
BusPirate is back in the text mode

avrdude done.  Thank you.

 

I did not change anything... :)

 

Cheers

mcc

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK the fuses are correct.  Have you reflashed without -D as I suggested?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi joeymorin,

 

I reflashed the UNO without using '-D' and TADA! it works again!

 

You safed not only my day -- you safed my Arduino UNO R3!

 

Thank you very much, Sir! Gracia mille! :)

 

Only to get the correct idea of what happens:

 

With -D, the flash will not be erased but overwritten at the addressed parts only.

Wiithout -D the flash will be nullified (or better FF'yfied ?).

 

What I dont understand: The firmware as such is "closed" in the sense of data

and control flow. It is selfcontaind as all its need is "in it".

Such a software should run even if "garbage" sourounds it in the

memory of the neighbourhood.

 

But it does not (as scientifically proofed...see above :)

 

Conclusion: The idea of the process, which I have in my head, is

wrong...

 

But where and what?

(Reflashing my brain without -D has vastly negative effects, I fear... ;)

 

Cheers

mcc

 

 

 

 

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A chip erase restores all of flash (and EEPROM, provided EESAVE fuse is not programmed) to 0xFF.  That is, all the bits are restored to 1s.

 

Programming consists of changing those 1s to 0s.  Programming cannot change a 0 back to a 1.  Only an erase operation can do that.

 

So, if you program without first erasing, flash will contain the bitwise-and of what was there before, and what you programmed over top.

 

Take an existing byte value of 0xAA.  That's binary 0b10101010.  Now program it with 0x55, which is binary 0b01010101

 

current: 10101010

    new: 01010101

-----------------

    and: 00000000

 

So programming without first erasing will result in flash containing neither the old contents, nor the desired new contents, but something entirely different... >>unless<< the new content is identical to the old content.
 

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"Read a lot.  Write a lot."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm fairly certain that Flash works the same as most other non-volatile memories in that when you program a location you can change a '1' to a '0' but not the other way around.

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.