Programming Micro-controller in Linux (Without IDE) from CLI

What Is a Micro-controller?

A micro-controller is a single-chip computer. It has internal RAM, ROM, timers, counters, interrupt circuitry, I/O ports, analog comparators, serial USARTs, analog to digital converters, watchdog timers, and a RISC architecture. When you are using a microprocessor, you cannot program it alone. You need other components, like RAM, ROM, timers, etc. For programming, you should know its architecture thoroughly: You must read the data-sheet for your micro-controller.

In this example We are taking AVR micro-controller for our demo.

Software Required

binutils: Tools like the assembler, linker, etc.

gcc-avr: The GNU C compiler (cross-compiler for avr).

avr-libc: Package for the AVR C library, containing many utility functions.

uisp: A Micro In-System Programmer for Atmel’s AVR MCUs (for burning code to MCUs’ memory).

The following Atmel microcontrollers are supported by avr-gcc in Linux:

at90s Type Devices

at90s2313, at90s2323, at90s2333, at90s2343, at90s4414, at90s4433, at90s4434, at90s8515, at90s8515, at90s8515, at90s8535, at90s1200.

atmega Type Devices

atmega103, atmega603, atmega8, atmega48, atmega88, atmega8515, atmega8535, atmega16, atmega161, atmega162, atmega163, atmega165, atmega168, atmega169, atmega32, atmega323, atmega325, atmega3250, atmega64, atmega645, atmega6450, atmega128.

attiny Type Devices

attiny22, attiny26, attiny26, attiny13, attiny13, attiny13, attiny13, attiny2313, attiny11, attiny12, attiny15, attiny28.

Other AVR Devices

avr2, at90c8534, at86rf401, avr3, at43usb320, at43usb355, at76c711, avr4, avr5, at90can128, at94k, avr1.

binutils: Programs to manipulate binary and object files that may have been created for Atmel’s AVR architecture. This package is primarily for AVR developers and cross-compilers.

gcc-avr: The GNU C compiler, a fairly portable optimising compiler that supports multiple languages. This package includes C language support.

avr-libc: Standard library used for developing C programs for Atmel AVR microcontrollers. This package contains static libraries, as well as needed header files.

uisp: Utility to program AVR chips with object code created by gcc-avr. It supports in-system programming.

Install the tools

The commands to install the tools are for Ubuntu/Debian machine.

First, we update the package.

>$ sudo apt-get update>$ sudo apt-get upgrade -y

And then we install the package required by avr and avrdude.

>$ sudo apt-get install gcc-avr binutils-avr avr-libc>$ sudo apt-get install avrdude

Type in the terminal avr- and press the tab twice (do not press enter) to see all the tools installed, and type avrdude -v to see the version of avrdude installed.

Now Lets code :

#include <avr/io.h>
#include <util/delay.h>

#define MS_DELAY 3000

int main (void) {
/*Set to one the fifth bit of DDRB to one
**Set digital pin 13 to output mode */
DDRB |= _BV(DDB5);

while(1) {
/*Set to one the fifth bit of PORTB to one
**Set to HIGH the pin 13 */
PORTB |= _BV(PORTB5);

/*Wait 3000 ms */
_delay_ms(MS_DELAY);

/*Set to zero the fifth bit of PORTB
**Set to LOW the pin 13 */
PORTB &= ~_BV(PORTB5);

/*Wait 3000 ms */
_delay_ms(MS_DELAY);
}
}

Compile and upload

Now we need to compile, so we first create the object file from the source code specifying the micro-controller in which we will run the program:

>$ avr-gcc -Os -DF_CPU=16000000UL -mmcu=atmega328p -c -o blink_led.o blink_led.c

We create the executable:

>$ avr-gcc -mmcu=atmega328p blink_led.o -o blink_led

And we convert the executable to a binary file:

>$ avr-objcopy -O ihex -R .eeprom blink_led blink_led.hex

Finally, we can upload the binary file:

>$ avrdude -F -V -c arduino -p ATMEGA328P -P /dev/ttyACM0 -b 115200 -U flash:w:blink_led.hex

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.00s

avrdude: Device signature = 0x1e950f (probably m328p)
avrdude: NOTE: “flash” memory has been specified, an erase cycle will be performed
To disable this feature, specify the -D option.
avrdude: erasing chip
avrdude: reading input file “blink_led.hex”
avrdude: input file blink_led.hex auto detected as Intel Hex
avrdude: writing flash (176 bytes):

Writing | ################################################## | 100% 0.04s

avrdude: 176 bytes of flash written

avrdude: safemode: Fuses OK (E:00, H:00, L:00)

avrdude done. Thank you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s