Go Down

Topic: Arduino Working (Read 243 times) previous topic - next topic

SagarDev

Hey Guys,

I have been using the Arduino for quite a long time when I got this doubt...

How does the Arduino Work..?

I went through the Schematics and got to know how the USB is connected to the ATmega328 through the ATmega16U2.

I learnt that the ATmega328 and all other Microcontrollers run on the data in the .hex file which is uploaded onto the flash memory of the Microcontroller.

Assuming that the ATmega16U2 is also a Microcontroller:
  • What program is uploaded on it to convert the USB data to the .hex file..?
  • How does the Software recognise the Arduino and its details..?
  • What will be the USB output from the Arduino Software..?
  • How is the .hex file uploaded to the ATmega328..?
  • If I use a different target microcontroller, how do I program it..?


Please provide detailed, in-depth answers...

Thanks in Advance

SDA
Shakespeare's pen is an Electronics Engineer's Soldering Iron...

CrossRoads

#1
Jun 16, 2017, 06:50 pm Last Edit: Jun 16, 2017, 06:53 pm by CrossRoads
1. The USB data is not converted to a hex file. The .ino file is converted to a hex file on your PC.
The 16U2 only translates the high speed USB data (~ +/- 3V, up to 12 megabytes/sec) into 115200 bytes/sec 0/5V level data so the PC can converse with then 328P.

2. The IDE is coded to translate the .ino file you write into a .hex file that the 328P runs. It sends commands to the AVR-GCC compiler which does the actual translation. If you turn on Verbose output (see #3) you can watch the commands being run.

3. During a code download into the 328P, the USB output can be seen by selecting File:Preferences and turning on Verbose Outputs.
There are 2 stages - the first is the output from the .ino to .hex creation process, the second is the transfer of the .hex to the bootloader code running on the '328P. The bootloader selfwrites the .hex contents into flash memory.

4. The IDE talks converses with the bootloader on the 328P, see #3.
Optionally, a Programmer can connect to the ICSP header and write the flash memory directly. A Programmer is another microcontroller with a USB port for receiving data from the PC. Or it can be a standalone programmer getting the .hex file from an SD card, such as this one I offer that runs code developed by member Nick Gammon:
http://www.crossroadsfencing.com/BobuinoRev17/Rev7Battery_with_Arduino.jpg[/img]

5. Different microcontrollers can be selected in the IDE, for the most part one of the other 8-bit parts in the Atmel AVR family.  Atmega8, Atmega48, Atmega168, '328P, 324, 644, 1284P, 1280, 2560, 2561, Attiny24/44/84, Attiny2313, Attiny25/45/85, etc.
Many of the parts have had support added to the IDE by forum members, such as Hansibull, see the additions he has provided at Github, for example:
[url]https://github.com/MCUdude/MightyCore]http://www.crossroadsfencing.com/BobuinoRev17/[url]


5. Different microcontrollers can be selected in the IDE, for the most part one of the other 8-bit parts in the Atmel AVR family.  Atmega8, Atmega48, Atmega168, '328P, 324, 644, 1284P, 1280, 2560, 2561, Attiny24/44/84, Attiny2313, Attiny25/45/85, etc.
Many of the parts have had support added to the IDE by forum members, such as Hansibull, see the additions he has provided at Github, for example:
[url]https://github.com/MCUdude/MightyCore]


5. Different microcontrollers can be selected in the IDE, for the most part one of the other 8-bit parts in the Atmel AVR family.  Atmega8, Atmega48, Atmega168, '328P, 324, 644, 1284P, 1280, 2560, 2561, Attiny24/44/84, Attiny2313, Attiny25/45/85, etc.
Many of the parts have had support added to the IDE by forum members, such as Hansibull, see the additions he has provided at Github, for example:
[url]https://github.com/MCUdude/MightyCore]http://www.crossroadsfencing.com/BobuinoRev17/[url]


5. Different microcontrollers can be selected in the IDE, for the most part one of the other 8-bit parts in the Atmel AVR family.  Atmega8, Atmega48, Atmega168, '328P, 324, 644, 1284P, 1280, 2560, 2561, Attiny24/44/84, Attiny2313, Attiny25/45/85, etc.
Many of the parts have had support added to the IDE by forum members, such as Hansibull, see the additions he has provided at Github, for example:
[url]https://github.com/MCUdude/MightyCore

Then the code is written the IDE the same, way, compiled, and downloaded.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

SagarDev

3. During a code download into the 328P, the USB output can be seen by selecting File:Preferences and turning on Verbose Outputs.
There are 2 stages - the first is the output from the .ino to .hex creation process, the second is the transfer of the .hex to the bootloader code running on the '328P. The bootloader selfwrites the .hex contents into flash memory.

4. The IDE talks converses with the bootloader on the 328P, see #3.
Optionally, a Programmer can connect to the ICSP header and write the flash memory directly. A Programmer is another microcontroller with a USB port for receiving data from the PC. Or it can be a standalone programmer getting the .hex file from an SD card, such as this one I offer that runs code developed by member Nick Gammon:
http://www.crossroadsfencing.com/BobuinoRev17/Rev7Battery_with_Arduino.jpg[/img]
I don't know much about a bootloader. What is it..? What does it contain..? How does it work..?

Can you please explain in detail..?

I want to learn more about Bootloaders, Memory Locations, Flash and other related topics...
Can you please suggest a book..? Please make sure that it is available in India...

Shakespeare's pen is an Electronics Engineer's Soldering Iron...

sterretje

In layman's terms

The bootliader is a piece of software that is permanently loaded in the 328 micro. When the 328 is reset, the bootloader is started. It will monitor the serial communications for a while for specific commands that indicate an upload.

If it sees these commands, it will switch to upload mode and the pc can upload the code after which it will start the loaded code.

If these specific commands are not received, the bootloader will start the loaded application.

You can compare the bootloader with the bios in a pc. The bios is the first program that runs when a pc is started; when it's finished, it will hand over control to the operating system (again layman's terms).
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Jiggy-Ninja

Quote
What program is uploaded on it to convert the USB data to the .hex file..?
Go to the folder you installed the Arduino IDE in, then follow this path:
Code: [Select]
hardware\arduino\avr\firmwares\atmegaxxu2\arduino-usbserial
It is a USB-Serial converter that, from the computer's perspective, emulated a COM port.

Many clones use purpose-made chips for this role, such as an FT232 or CH340.
Quote
How does the Software recognise the Arduino and its details..?
A driver is installed on your computer when you install the IDE.
Quote
What will be the USB output from the Arduino Software..?
The AVR-GCC compiler outputs the HEX file, then the IDE uses a program called avrdude to upload the hex file. avrdude communicated with the Arduino using a Serial protocol, it doesn't care about the details of the actual physical connection. The driver on the computer and the converter chip on the board transparently handle the conversion and deconversion of the communication through the intermediate USB connection.
Quote
If I use a different target microcontroller, how do I program it..?
You need a programmer. If you're asking about programming a different AVR, there are quite a lot of options, including using an Arduino as the programming device.

If you're asking about completely different types of chips, like ARM chips of different manufacturers' architectures, they will have their own toolchain that you will need to setup.
Quote
I don't know much about a bootloader. What is it..? What does it contain..? How does it work..?
On a high level, a bootloader is a program that lets you load another program onto the device.
Quote
I want to learn more about Bootloaders, Memory Locations, Flash and other related topics...
Can you please suggest a book..? Please make sure that it is available in India...
Source code is one good place to start: hardware\arduino\avr\bootloaders\optiboot

Datasheets and application notes are also great places to get information, if you are willing to invest the time to search through them and find the diamonds in the rough.
Hackaday: https://hackaday.io/MarkRD
Advanced C++ Techniques: https://forum.arduino.cc/index.php?topic=493075.0

SagarDev

Thanks for those Replies...

In layman's terms

The bootliader is a piece of software that is permanently loaded in the 328 micro. When the 328 is reset, the bootloader is started. It will monitor the serial communications for a while for specific commands that indicate an upload.

If it sees these commands, it will switch to upload mode and the pc can upload the code after which it will start the loaded code.

If these specific commands are not received, the bootloader will start the loaded application.
What are these specific commands..?

Source code is one good place to start: hardware\arduino\avr\bootloaders\optiboot

Datasheets and application notes are also great places to get information, if you are willing to invest the time to search through them and find the diamonds in the rough.
I will go through the source code and datasheets...
Shakespeare's pen is an Electronics Engineer's Soldering Iron...

CrossRoads

See this section of the 328P datasheet as well:

27.8.13 Simple Assembly Code Example for a Boot Loader
Code: [Select]

;-the routine writes one page of data from RAM to Flash
; the first data location in RAM is pointed to by the Y pointer
; the first data location in Flash is pointed to by the Z-pointer
;-error handling is not included
;-the routine must be placed inside the Boot space
; (at least the Do_spm sub routine). Only code inside NRWW section can
; be read during Self-Programming (Page Erase and Page Write).
;-registers used: r0, r1, temp1 (r16), temp2 (r17), looplo (r24),
; loophi (r25), spmcrval (r20)
; storing and restoring of registers is not included in the routine
; register usage can be optimized at the expense of code size
;-It is assumed that either the interrupt table is moved to the Boot
; loader section or that the interrupts are disabled.
.equ PAGESIZEB = PAGESIZE*2;PAGESIZEB is page size in BYTES, not
words
.org SMALLBOOTSTART
Write_page:
; Page Erase
ldi spmcrval, (1<<PGERS) | (1<<SPMEN)
call Do_spm
; re-enable the RWW section
ldi spmcrval, (1<<RWWSRE) | (1<<SPMEN)
call Do_spm
; transfer data from RAM to Flash page buffer
ldi looplo, low(PAGESIZEB);init loop variable
ldi loophi, high(PAGESIZEB);not required for
PAGESIZEB<=256
Wrloop:
ld r0, Y+
ld r1, Y+
ldi spmcrval, (1<<SPMEN)
call Do_spm
adiw ZH:ZL, 2
sbiw loophi:looplo, 2;use subi for PAGESIZEB<=256
brne Wrloop
; execute Page Write
subi ZL, low(PAGESIZEB);restore pointer
sbci ZH, high(PAGESIZEB);not required for PAGESIZEB<=256
ldi spmcrval, (1<<PGWRT) | (1<<SPMEN)
call Do_spm
; re-enable the RWW section
ldi spmcrval, (1<<RWWSRE) | (1<<SPMEN)
call Do_spm
; read back and check, optional
ldi looplo, low(PAGESIZEB);init loop variable
ldi loophi, high(PAGESIZEB);not required for
PAGESIZEB<=256
subi YL, low(PAGESIZEB);restore pointer
sbci YH, high(PAGESIZEB)
Rdloop:
lpm r0, Z+
ld r1, Y+
cpse r0, r1
jmp Error
sbiw loophi:looplo, 1;use subi for PAGESIZEB<=256
brne Rdloop
; return to RWW section
; verify that RWW section is safe to read
Return:
in temp1, SPMCSR
sbrs temp1, RWWSB ; If RWWSB is set, the RWW section is not
ready yet
ret
; re-enable the RWW section
ldi spmcrval, (1<<RWWSRE) | (1<<SPMEN)
call Do_spm
rjmp Return
Do_spm:
; check for previous SPM complete
Wait_spm:
in temp1, SPMCSR
sbrc temp1, SPMEN
rjmp Wait_spm
; input: spmcrval determines SPM action
; disable interrupts if enabled, store status
in temp2, SREG
cli
; check that no EEPROM write access is present
Wait_ee:
sbic EECR, EEPE
rjmp Wait_ee
; SPM timed sequence
out SPMCSR, spmcrval
spm
; restore SREG (to enable interrupts if originally enabled)
out SREG, temp2
ret
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

westfw


Go Up