Pages: [1] 2   Go Down
Author Topic: What's going on behind the scenes?  (Read 1366 times)
0 Members and 1 Guest are viewing this topic.
North Carolina, USA
Offline Offline
Full Member
***
Karma: 1
Posts: 137
:O Arduino!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So all of you god members out there seem to know everything ever about everything ever. I want this.  ;D  However, I would be content with knowing how arduino code (eg "digitalWrite(ledPin, HIGH));") gets turned into whatever the heck is going on in the chip itself. I understand it goes through a lot of processing on the way, but what specifically happens to it? What is the language the chip runs called? What does it look like? Is it possible to write that language directly?

Also, what is the name of the arduino language? I know it's based on wiring etc, but is it just 'arduino'?
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 32
Posts: 4259
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Let's start with the chip and work backwards.  I'm simplifying some of this so it may not all be exactly technically correct.

Chip level - Machine Language.  Binary numbers that are most frequently expressed by their hexadecimal equivalent.
Each of these numbers is an 'instruction' that tells the chip to do some very simple operation.

Low Level - Assembly Language.  Short alphabetical sort-of English instructions that correspond to the machine language instructions.  There is a near one for one relationship between the assembly language instructions and the machine language instructions.  The programmer uses a text editor to create a list of these assembly language instructions along with some other information which is then saved as a .ASM file.  An 'Assembler' program (or human) converts this list of assembly language instructions into a list of the corresponding hex versions of the machine language instructions and saves them as a .HEX file.

High Level - C++.  More complex sort-of English functions that correspond roughly with the machine language instructions that the processor understands or with more complex tasks that require many machine language instructions.  The programmer uses a text editor to create a list of these functions along with some other information which is then saved as a .CPP file.  A 'Compiler' program converts this list of C++ functions into the corresponding hex version of the required machine language instructions.  These are usually saved as a .HEX file just like the one created by the assembler.

Arduino - ???.  One more magical step similar to that described for C++ which someone else will have to explain.  The Arduino functions are expanded into C++ functions which are compiled by a more or less standard C compiler as described above.  Remember the phrase "along with some other information" also mentioned above?  The Arduino environment (for lack of a better term) takes care of most of this as well.

Don
Logged

North Carolina, USA
Offline Offline
Full Member
***
Karma: 1
Posts: 137
:O Arduino!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, that all makes sense. I think the biggest thing I don't understand is the leap from machine language to a blinking LED. To my understanding, a chip like the atmega 328 is pretty much a zillion transistors with maybe some other stuff too (like what, an oscillator/resonator/whatever?), so I guess the hex loaded onto the chip is stored in the transistors themselves? However, this cannot be the case because the transistors would reset whenever power is lost. I guess the main thing is just how do the internals of a chip store/execute a program?
Logged

Portugal
Offline Offline
God Member
*****
Karma: 6
Posts: 962
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I had the exact same question, i all got answered in my Computer Architecture class in university.
The atmel avr chip that is used in the arduino contains on integrated ressonator and some circuits that can put one cristal oscilating and producing one nice square wave that supply the chip internals with a stable known clock, the arduino uses the second.
The program that you create in the Arduino IDE is stored in one Flash memory, pretty much like your usb pen, so like your pen it doesnt "forget" what was inside it when you remove it from the usb(aka remove power).
When the avr chip gets a nice and stable source of power it runs and hardware init sequence that puts the clock/ressonator runing and waits some time so the square wave can stabilize and thus become usable.
This square wave is used to drive a lot of peripherals, just to name a few: clock source for timers, for the flash memory, for the adc circuitry and for the core itself.
In the core there is special register called program counter, this register as always a start up value of 0, the avr core grabs this value and use it as an address to where to read the flash memory, 0 being the very first instruction(as said above in the chip level its just 0 and 1's, but think of it as assembly), then each instruction can do things like saying to the program counter to jump to a specific value, this would be the result of one if or case comparation or a function call/return in C/C++ or some of the multiple types of branches or jump's that you can do in assembly, if it isnt a jump instruction then the program counter just gets increased by one, the new address is read from the flash memory, the new data read from the flash goes through some circuits that can decode it and say what instruction is and what and where things will go, now it can be and add instruction, so there must be two/three registers involved, and the ALU(google about this).
It isnt very complex but it is extremely fun being able to really understand how a CPU works.
Hope you understand this smiley-grin
Logged

Portugal
Offline Offline
God Member
*****
Karma: 6
Posts: 962
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Let's start with the chip and work backwards.  I'm simplifying some of this so it may not all be exactly technically correct.

Chip level - Machine Language.  Binary numbers that are most frequently expressed by their hexadecimal equivalent.
Each of these numbers is an 'instruction' that tells the chip to do some very simple operation.

Low Level - Assembly Language.  Short alphabetical sort-of English instructions that correspond to the machine language instructions.  There is a near one for one relationship between the assembly language instructions and the machine language instructions.  The programmer uses a text editor to create a list of these assembly language instructions along with some other information which is then saved as a .ASM file.  An 'Assembler' program (or human) converts this list of assembly language instructions into a list of the corresponding hex versions of the machine language instructions and saves them as a .HEX file.

High Level - C++.  More complex sort-of English functions that correspond roughly with the machine language instructions that the processor understands or with more complex tasks that require many machine language instructions.  The programmer uses a text editor to create a list of these functions along with some other information which is then saved as a .CPP file.  A 'Compiler' program converts this list of C++ functions into the corresponding hex version of the required machine language instructions.  These are usually saved as a .HEX file just like the one created by the assembler.

Arduino - ???.  One more magical step similar to that described for C++ which someone else will have to explain.  The Arduino functions are expanded into C++ functions which are compiled by a more or less standard C compiler as described above.  Remember the phrase "along with some other information" also mentioned above?  The Arduino environment (for lack of a better term) takes care of most of this as well.

Don

Actually even the assembly gets compiled first, because not every instruction writen in assembly exists, for example you can just type mul ..... even for micro-controllers that dont have and wired multiplier, but the compiler will generate the right code so you can do things like multiplications.

When coding in C, once again the code isnt directly passed from C to binary/hex code, it is first parsed, then optimized(in the case of the avr-gcc and avr-g++ compilers), and only then the optimized code is translated into assembly and finally to hex.
Optimizing compilers are great because they can really reduce your code size and make it faster, but it can make the debugging of said code a very painful thing.

The only thing that Arduino gives to you is simplified functions to interact with your avr, digitalWrite(13,HIGH), is in fact PORTB |= (1<<PB5), and the second runs about 40 times faster than the previous, because there is a lot of things done when you call the digitalWrite function.
Logged

Phoenix, Arizona USA
Offline Offline
Faraday Member
**
Karma: 39
Posts: 5557
Where's the beer?
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It isnt very complex but it is extremely fun being able to really understand how a CPU works.

I've always thought of the "basic" idea of a CPU as a very fast player piano. What is really interesting, is when you start reading about how player pianos worked. Some of the "later" models actually had some very advanced features, including some basic "logic"/"branching" codes and such.

Then again, Babbage's Analytical Engine was the epitome of mechanical calculation (had it been realized).

Once you get your mind wrapped around what is really happening (how program counters/registers, address registers, data registers, etc - and the logic switching works) - you realize how simple a CPU really is (at a certain level - then you start to learn about things like micro-code, pipelining, etc - and it goes complex again).

A fast player piano, a fast player piano...

 ;D
Logged

I will not respond to Arduino help PM's from random forum users; if you have such a question, start a new topic thread.

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 525
Posts: 26538
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, some of the God Members have a lot more history with the ATMega than some of the more recent God Members (me especially, I've only been Arduinoing since August.) But there are some Electrical Engineers here, some Software Engineers, that always helps :-)
If you browse the ATMega 168/328 datasheet, there is a table there that lists all the instructions if you really want to do some low level programming.
I did some assembly level programming in college, some higher level programming since then, but mostly hardware design over the years. I am finding writing in arduino-code to be pretty straightforward.
Browse the datasheet, see just what kind of non-volatile memory it has, then google that to see how transistors can be made to store bits after power is removed. It can be quite fascinating.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

North Carolina, USA
Offline Offline
Full Member
***
Karma: 1
Posts: 137
:O Arduino!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok, to the wikipedia. I went from flash memory -> floating gate mosfet and I just wanted to be sure that I'm getting the general idea. So a floating gate mosfet is sort of a capacitor and a transistor combined? It gets a charge and then keeps it until the next time?

Now this has raised some more questions. So the Uno for instance has 32K of flash, 2K of SRAM, and 1K of EEPROM. I've used the EEPROM before and get the idea behind that. I'm thinking the sram is used for variable storage etc during execution and the flash would hold the code itself. So what happens if the sram gets full? would it lock up, maybe start overwriting old variables, or start filling up something else perhaps?

Quote
PORTB |= (1<<PB5)

oof. not good. However, I do like the 40 times faster bit. I seem to recall from my browsing that the digitalWrite command actually checks that the pin is indeed the state you specified? Can you use code like PORTB |= (1<<PB5) mixed in with 'normal' code, like so?

Code:
digitalWrite(pin, state);
PORTB |= (1<<PB5);
for(int i = 0;...

I think I saw an instructable the other day about making one's own flash storage from transistors, I may have a look at that and see if I can reason out some facts.
Logged

Global Moderator
Boston area, metrowest
Online Online
Brattain Member
*****
Karma: 525
Posts: 26538
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't believe the data sheet says what kind of FLASH the ATMega's use - not that I could find anyway.
It takes a few transistors to make a single memory bit.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

Left Coast, CA (USA)
Online Online
Brattain Member
*****
Karma: 361
Posts: 17263
Measurement changes behavior
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

 One of the beautiful things about the Arduino platform is that the user gets to decide if s/he wants to keep digging down lower and lower in details and complexity or rather stay at a higher level and just build stuff that works.

 It's said the original user target for the Arduino team was artist and other creative but possibly non-technical people, such that they might be able to utilize microcontroller technology without necessarily having to go via the normal time consuming learning curve of more traditional computer programmers/developers. However under the hood are all the traditional parts and tools of a true computing system. And while limited in speed and power compared to say PC computers, the underlining computing principles all apply.

  At the lowest level there is a microprocessor chip (actually several different models can be used) made by the Atmel Co. It is a small 8 bit computer that has all the basic elements that a traditional computer has. Being a micro-controller rather then just a microprocessor it also has many built in specialty digital and analog hardware features not necessarily found in traditional 'computers', as well as built in memories of several types, flash, sram, eeprom. The programming instructions used by this chip are called it's instruction set and is unique to the Atmel AVR 8 bit chips. A user could if they want, program the chip at this level using AVR assembly language. The Arduino IDE platform does not directly support this level of programming by the user but it does get accomplished under the automated verify/upload operation. And by using other open source software tools users can write and develop programs at this level is they so desire. The Atmel datasheets for the specific chip (mega8,168,328,1280,2560,etc) is an essential document for starting to learn this assembly level of programming/development.

 In the next high level step up in the Arduino system is the C/C++ compiler/linker that takes industry standard C/C++ statements from the users 'sketch' and outputs the AVR instructions that can then be uploaded to the chip for execution. The upload process is handled by an open source program called AVRDUDE that is included in the Arduino platform. The Arduino people didn't develop the compiler, but rather just integrated into their platform the open source AVR g++ compiler tool chain project.

 Next level up is the built in functions that the Arduino platform has available for the user to use in their 'sketches'. These can be simple functions or more complex libraries that save the user from having to develop from scratch very common and/or useful functions. A user can also develop their own functions and libraries if they wish, to be included in future programs they may develop. 'Wiring' is one of the higher level libraries that the Arduino platform includes that simplifies a lot of the special I/O features of the AVR chip. Wiring is itself from a different open source project that the Arduino team integrated into their platform. The Arduino team also developed a lot of additional functions and libraries that are available to the user and new features get added with every new IDE release, which is currently at version 21!

 Lastly there is the actual Arduino IDE software that the user interfaces with and runs on a PC. This IDE itself is developed from yet another open source project called Processing. This is where the user writes their sketch (program) and yes the user writes it in real legal C/C++ statements,  and has the advantage of a lot of pre written libraries and functions that make writing programs for the AVR microcontroller quicker and easier then having to master super low level details of the controller.

 So it should be very apparent that one unique part of the Arduino system is that it is built on top of several prior independent open source software projects. That and the fact that their hardware design is also open sourced and can run on all three major PC operating systems makes the Arduino pretty unique and probably the first to hit on all those major points.

 That my not answer your questions about what happens ' behind the scenes', it does I think add value to point out the various levels and functions that the Arduino platform spans. From there maybe more specific questions can arise in clearer context.

Lefty  
« Last Edit: December 15, 2010, 12:41:52 am by retrolefty » Logged

SF Bay Area (USA)
Offline Offline
Tesla Member
***
Karma: 124
Posts: 6653
Strongly opinionated, but not official!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
for example you can just type mul ..... even for micro-controllers that dont have and wired multiplier, but the compiler will generate the right code so you can do things like multiplications.
this depends on the compiler or assembler, and the particular processor involved.  I'm pretty sure that if you type "mul ..." in an AVR assembly language program, and the processor you've selected doesn't include a multiplier, the assembler will just give you an error message...
Logged

Western New York, USA
Offline Offline
Faraday Member
**
Karma: 32
Posts: 4259
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
ok, that all makes sense. I think the biggest thing I don't understand is the leap from machine language to a blinking LED.
Without resorting to Senso's solution (a Computer Architecture class in university) we have to simplify things a bit.   The microcontroller does all of its work essentially by moving ones and zeroes around.  One of the things it can do quite easily is apply 5 volts to a specific output pin when there is a 1 at a specific bit location and remove that 5 volts when there is a zero at that same location.  The 5v turns the LED on and the lack of 5v turns it off.

Quote
To my understanding, a chip like the atmega 328 is pretty much a zillion transistors with maybe some other stuff too (like what, an oscillator/resonator/whatever?), so I guess the hex loaded onto the chip is stored in the transistors themselves?
That is essentially correct.

Quote
However, this cannot be the case because the transistors would reset whenever power is lost. I guess the main thing is just how do the internals of a chip store/execute a program?
Modern microcontrollers possess internal non-volatile memory of one sort or another which would be in the category of 'other stuff' up above.  The program is stored here and it is not lost when power is removed.  

Don
« Last Edit: December 15, 2010, 09:20:51 am by floresta » Logged

Silly-con Valley, Ca, U.S.
Offline Offline
Sr. Member
****
Karma: 17
Posts: 420
Lernin' to tinker
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Then again, Babbage's Analytical Engine was the epitome of mechanical calculation (had it been realized).

It may be someday. Have you heard of http://plan28.org/. Some guys are trying to raise enough money to build it. I've pledged $20 US.

Also there is a great series of podcasts on how computers work. The guy literally starts with what a voltage divider is and how it works, then what a transistor is and how it works, and how you can use them in different combinations to create and/nand/or/nor gates. He move on to flip-flops and eventually how to store a bit of information. Following his explanation you could almost build it yourself on a breadboard. (I know it's not all 100% accurate but he's trying to give a general overview.)

Really amazing series. No replacement for school but a good listen if you're interested.

The podcasts you can download here... http://www.grc.com/securitynow.htm

Here are the ones you should look for...

How a Computer Works series:

Let's Design a Computer (part 1)
#233

Machine Language
#235

Indirection: The Power of Pointers
#237

Stacks, Registers & Recursion
#239

Hardware Interrupts
#241

The “Multi”-verse
#247

Operating Systems
#250

RISCy Business
#252

What We'll Do for Speed
#254


One other one that's good is when he was a kid he created what he calls a "Portable Dog Killer" which really was just an ultrasonic gun to teach a neighborhood dog a lesson. That one is episode #248.

Sorry for the long post.
Logged

10 PRINT CHR$(7)
20 GOTO 10

Austin, TX
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6141
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
http://www.grc.com/securitynow.htm
How a Computer Works series:
Let's Design a Computer (part 1)  #233
Steve does a very good job of explaining (relatively) technical topics in plain english, and he does so without any visual aids.  That being said, I'd recommend listening to the "Design A Computer" series with a piece of paper so you can draw what he describes.

(Note:  I am listening to the latest Security Now as I write this.)
« Last Edit: December 15, 2010, 02:15:34 pm by cmiyc » Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 604
Posts: 33419
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Then again, Babbage's Analytical Engine was the epitome of mechanical calculation (had it been realized).


It may be someday.

It already has been:-

http://en.wikipedia.org/wiki/Difference_engine

Quote
Based on Babbage's original plans, the London Science Museum constructed a working Difference Engine No. 2 from 1989 to 1991, under Doron Swade, the then Curator of Computing. This was to celebrate the 200th anniversary of Babbage's birth.
Logged

Pages: [1] 2   Go Up
Jump to: