0% found this document useful (0 votes)
64 views

Integrating 8-Bit AVR Micro-Controllers in Ada: Pablo Vieira Rego

This document describes an approach for developing embedded applications for 8-bit AVR micro-controllers in Ada using the GNAT AVR cross-compiler. It discusses using the Arduino Duemilanove board with an ATmega328P microcontroller as a development platform. It outlines mapping the microcontroller's I/O registers in a specification package to support a memory-mapped I/O approach. The goal is to generate a .hex file for burning onto the microcontroller to support monotask applications following the Zero Footprint Profile.

Uploaded by

doaa mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Integrating 8-Bit AVR Micro-Controllers in Ada: Pablo Vieira Rego

This document describes an approach for developing embedded applications for 8-bit AVR micro-controllers in Ada using the GNAT AVR cross-compiler. It discusses using the Arduino Duemilanove board with an ATmega328P microcontroller as a development platform. It outlines mapping the microcontroller's I/O registers in a specification package to support a memory-mapped I/O approach. The goal is to generate a .hex file for burning onto the microcontroller to support monotask applications following the Zero Footprint Profile.

Uploaded by

doaa mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

301

Integrating 8-bit AVR Micro-Controllers in Ada


Pablo Vieira Rego
Embraer, S.A., Av. Brigadeiro Faria Lima 2170, Putim, São José dos Campos, SP, Brazil;email: pvrego@gmail.com

Abstract description and some code examples6 . For RTOS’es support,


there are plenty of choices for AVR development, we could
The increasing popularity of 8-bit AVR micro-controllers cite AvrX7 , FreeRTOS8 and RTEMS9 , most of them written
for using with robotics and low-complexity embedded in C/C++.
applications has called the attention of much of the
software community. The purpose of this paper is to For the development in Ada, there are some academic refer-
present an approach for developing embedded applica- ences for development on AVRs, most focused on the 32-bit
tions for these micro-controllers in Ada, using GNAT micro-controllers. Gregertsen and Skavhaug [6] describe a
AVR as Microsoft Windows cross-compiler and GNAT deterministic multitasking run-time environment supporting
Programming Studio as IDE. the Ravenscar tasking model of Ada 2005 implemented on
the Atmel AVR32 UC3A micro-controller. In [7], they de-
Keywords: 8-bit AVR micro-controller, GNAT AVR, Ada, scribe an object-oriented real-time framework for Ada 2005
ZFP. and Ravenscar profile implemented on AVR32 UC3 micro-
controller; in [8], they propose the addition of execution-time
1 Introduction control features for interrupt handling as an addition to the
Ada standard library and describes it using an implementation
The Atmel AVRs1 are presented as a family of 8- and 32- of GNAT bare-board Ravenscar run-time environment on the
bit micro-controllers designed to provide flexibility, based a Atmel AVR32 architecture; and finally in [9, 10], Gregertsen
priori in C and assembly programming. More specifically, and Skavgaug describe an implementation of timing event and
Arduino is a family of boards with a central 8-bit AVR micro- execution time control features with support for interruption
controller and peripheral I/Os which implement a bunch of on the same architecture.
low-level hardware support such as analog and digital data
write, read and storage, USART protocols, Ethernet, wifi For the 8-bit micro-controllers, however, there are fewer
modules and others, for a reasonable low cost. They became academic references for the development in Ada. Ras and
very popular in the development of memory mapped robotics Cheng [11] describe a deterministic run-time environment for
with open-source hardware for hobbyists and low-to-medium Ada-05 on the 8-bit ATmega16 micro-controller; and Ander-
complexity embedded products and introductory robotics pro- sen [12] presented an approach for developing Arduinos with
gramming courses [1, 2, 3, 4, 5]. the AVR-Ada cross-compiler.
For open-source development in C/C++, there are four main
branches: Atmel Studio, WinAVR, Arduino IDE and Eclipse. There are some open-source cross-compiler options for pro-
Atmel Studio2 provides the IDE with C and assembly com- gramming AVR micro-controllers in Ada, we can cite AVR-
piler and debugger and several in-built libraries with pre- Ada10 and GNAT AVR GPL [13]. In this paper we will show
implemented features and specific configurations for the an approach to generate a .hex target burning file for the
whole 8- and 32-bit families. WinAVR3 is a suite of ex- micro-controller using the open-source GNAT AVR cross
ecutable, open source software development tools for the compiler and the GPS IDE. The GNAT AVR provides a Mi-
Atmel AVR series of RISC microprocessors hosted on the crosoft Windows cross-compiler and builder for AVR 8-bit
Windows platform, which includes the GNU GCC compiler micro-controllers [14] and implements Zero Footprint Profile
for C and C++. Arduino IDE4 is a platform written in Java (ZFP), which does not require any run-time routines, and is
and based on Processing and avr-gcc and provides C++ devel- intended for high-critically applications related to DEF Stan
opment based on application examples. On Eclipse, the AVR 00-55 certification [15]. Thus it does not have support for
Eclipse Plugin5 provides tools for developing C applications tasking or exception propagation. So, the purpose of this pa-
on AVR micro-controllers. In all cases, Atmel’s Application per is to serve as an user guide for development of monotask
Notes serve as development guidelines, as well as the micro- applications in 8-bit AVR micro-controllers in Ada, following
controllers datasheets, which contain the complete hardware the ZFP.
1 http://www.atmel.com/products/microcontrollers/ 6 http://www.atmel.com/products/microcontrollers/

avr/default.aspx avr/default.aspx
2 http://www.atmel.com/microsite/atmel_studio6/ 7 http://www.barello.net/avrx/index.htm
3 http://winavr.sourceforge.net/index.html 8 http://www.freertos.org/
4 http://arduino.cc/en/Main/Software 9 http://www.rtems.com/
5 http://avr-eclipse.sourceforge.net/wiki/index. 10 http://sourceforge.net/apps/mediawiki/avr-ada/

php/The_AVR_Eclipse_Plugin index.php?title=Main_Page

Ada User Journal Vo l u m e 3 3 , N u m b e r 4 , D e c e m b e r 2 0 1 2


302 I n t e g r a t i n g 8 - b i t AV R M i c r o - C o n t r o l l e r s i n A d a

2 Materials and Methods 2.2.1 The Specification Package


Each AVR device has a number of I/Os associated with its
2.1 Arduino Duemilanove (ATmega328P) internal registers. These descriptions are present in micro-
controller datasheet and the specification package shall map
The Arduino Duemilanove is a development board based
the memory address entries defined in the datasheet.
on Atmel ATmega328P/ATmega168 8-bit micro-controllers.
This paper concerns to ATmega328P, but can be extended For the ATmega328P micro-controller, datasheet [16] section
to other AVR 8-bit chips with minor changes. Duemilanove Register Summary on page 423 describes all the memory
provides 14 digital I/O pins, 6 analog inputs, a 16 MHz crystal addresses available to develop the applications. Using the
oscillator, a USB connection, a power jack, an ICSP header Memory Mapped I/O approach as described by McCormick
and a reset button11 . Table 1 resumes some characteristics of et al. [17], each word shall be mapped, and sometimes it is
this board. usefull to map also the meaning of each one of the bits. The
words so should be pointed exactly to the memory address it
Table 1: Characteristics of Arduino Duemilanove with AT- describes.
mega328p
For example, the registers Port B Data Register (PortB), Port
micro-controller ATmega328P B Data Direction Register (DDRB) and Port B Input Pins
Address (PINB) could be mapped in a specification package
Operating Voltage 5V
as
Input Voltage (recommended) 7-12 V
Input Voltage (limits) 6-20 V Listing 1: Simplified Specification Package for ATmega328P
Digital I/O Pins 14 −− atmega328p.ads
Analog Input Pins 6 with Interfaces ; use Interfaces;
with System;
DC Current per I/O Pin 40 mA
package ATmega328P is
DC Current for 3.3V Pin 50 mA
Flash Memory 32 KB12 −− PORTB: Port B Data Register
PORTB : Unsigned_8;
SRAM 2 KB for PORTB’Address use System’To_Address (16#25#);
EEPROM 1 KB −− DDRB: Port B Data Direction Register
Clock Speed 16 MHz DDRB : Unsigned_8;
for DDRB’Address use System’To_Address (16#24#);

−− PINB: Port B Input Pins


2.2 Development Environment and Target Gener- PINB : Unsigned_8;
ation for PINB’Address use System’To_Address (16#23#);

end ATmega328P;
The required applications are the GNAT AVR GPL edition13 ,
which comes with the IDE and the Windows cross-compiler;
and WinAVR, which provides CRT libraries for the micro- 2.2.2 GPS Project File
controllers. The GPS project file has to include the AVR configurations
for the packages Ide, Compiler, Builder and Linker. Below
For generating the output .hex file on Windows, the simple are presented some suggestions on how to configure these
project consists of the following elements: project file packages in order to build the .elf output which
will be used to generate the .hex output.
• Specification package which will map the AVR registers;

• Main file which will include the chip specification pack- Listing 2: GPS Project File for ATmega328P
age;
−− arduino.gpr
• GPS .gpr configuration file; project Arduino is

for Source_Dirs use (".", "src" ) ;


• An object library provided by WinAVR;
for Object_Dir use "obj";
for Exec_Dir use "bin";
• A batch script which will provide .elf/.hex conversion for Main use ("main.adb");
and a pre-configured burning tool.
package Ide is
for Gnat use "avr−gnat";
for Gnatlist use "avr−gnatls";
11 General description of Arduino Duemilanove can be found at http:
for Debugger_Command use "avr−gdb";
//arduino.cc/en/Main/arduinoBoardDuemilanove end Ide;
12 In which 2 KB is used by bootloader.
13 http://libre.adacore.com package Compiler is

Vo l u m e 3 3 , N u m b e r 4 , D e c e m b e r 2 0 1 2 Ada User Journal


P. V. R e g o 303

for Default_Switches ("ada") use ("−mmcu=avr5"); 2.2.4 The Hex & Burn Batch Script
end Compiler;
An option for a batch script to generate the .hex and burn it
package Builder is on a micro-controller in COM7 follows.
for Executable_Suffix use ". elf " ;
for Default_Switches ("ada") use ("−−RTS=rts−zfp");
end Builder;
Listing 4: Batch script for converting the .elf and burn the re-
sulting .hex
package Linker is
for Default_Switches ("ada") use ("obj\crtm328p._o", " @rem hexitandburn_duemilanove.bat
−nostdlib", "−lgcc", "−mavr5", "−Tdata=0
x00800200", "−−mmcu=avr5"); @echo Converts .elf to .hex
end Linker; avr−objcopy −O ihex main.elf main.hex

end Arduino; @echo Burn the chip


c :\ WinAVR\bin\avrdude.exe −p m328p −c avrisp −P com7
−b 57600 −e −U flash:w:main.hex
The Ide package configures the tools related to the AVR-
GNAT installation. The Builder package sets the suffix for In the case of using the ATmega2560 micro-controller, the
the builder output file and specifies the Zero Footprint Profile avrdude line would change to
as the Real-Time System. c :\ WinAVR\bin\avrdude.exe −p m2560 −c stk500v2 −P
com7 −b 115200 −e −U flash:w:bin\main.hex
In the Compiler package, the option -mmcu=avr5 enables
compilation using AVR5 architecture, which is the case of the And it is suggested to create a .xml file in Windows user pro-
ATmega328P. file folder (eg. c:\Documents and Settings\user\.gps\plug-
ins) to enable a menu to execute the actions of convert and
In the Linker package, the crtm328p._o is the CRT14 got from
burn the .hex in the micro-controller.
WinAVR installation15 , since GNAT AVR GPL only provides
CRT for ATmega2560. The option -nostdlib avoids linking
Listing 5: Script embedded.xml to enable a menu in GPS to
with standard libraries, not provided by GNAT AVR GPL, and convert and burn Arduino Duemilanove
the option -lgcc tells the linker to use the compiler support
library. The options -mavr5 and -mmcu=avr5 set the device <?xml version="1.0" ?>
architecture for AVR5. And the option -Tdata sets the start <embedded>
address of the data section [18]. <action name="Hexit and Burn Duemilanove">
<external>cmd /c
hexitandburn_duemilanove.bat</
For the ATmega2560 micro-controller, we should exchange external>
from AVR5 to AVR6 architecture and the CRT. GNAT pro- </action>
vides this CRT in a form of crt1-atmega2560.S file16 , which
<submenu after="Build">
shall be compiled in order of obtaining the crt2560._o. One
< title >Embedded</title>
can compile it using <menu action="Hexit and Burn
Duemilanove">
avr−gcc −c −mmcu=avr6 −o crt2560._o crtl−atmega2560.S
< title >[Hexit and Burn]
Duemilanove</title>
</menu>
2.2.3 The Main File </submenu>
</embedded>

The Main file contains the main task available by ZFP and
the libraries related to the code. The following dummy code 3 Limitations of the Zero Footprint Profile
should be modified in order to code the micro-controller.
The Zero Footprint Profile defines an Ada run-time certifiable
run-time with a memory footprint reduced to null. It excludes
Listing 3: Dummy main file in particular the use of dynamic Ada semantic. The ZFP still
allows the use of the major Ada features such as generics,
with ATmega328P; use ATmega328P;
child units, library-level tagged types, interfaces and local
procedure Main is exception handling [19]. The ZFP is suitable for software
begin certification using DEF Stan 00-55 standard [15].
null ;
end Main; The ZFP limitations are [20]:
• Constructs defined in the ARM Section 9 (Tasking and
14 C Run-Time Library. Synchronization)
15 In this case, the crtm328p.o has its extension renamed to _o to avoid

been deleted when the user makes a project clean. • Exception propagation
16 The file crt1-atmega2560.S can be found on GNAT AVR GPL

2012 installation in the folder C:\GNAT\2012\share\examples\gnat- • Packed arrays with component size other than 1, 2, 4, 8
cross\avr\atmega2560. or 16 bits

Ada User Journal Vo l u m e 3 3 , N u m b e r 4 , D e c e m b e r 2 0 1 2


304 I n t e g r a t i n g 8 - b i t AV R M i c r o - C o n t r o l l e r s i n A d a

• Some exponentiation operation COIL2 : constant := 2 ∗∗ 1;


COIL3 : constant := 2 ∗∗ 3;
• 64-bit integer and fixed-point types
−− Default delay and number of cycles
• Boolean operation on packed arrays DEFAULT_DELAY : constant := 100;
DEFAULT_CYCLES : constant := 50;
• Some comparison operations on arrays of discrete com-
ponents −− Null instruction delay
procedure Custom_Delay (Wait_Cycle : Integer) is
• The attributes Image, Value, Body_Version, Version, begin
Width, and Mantissa for i in 1 .. Wait_Cycle loop
for j in 1 .. Wait_Cycle loop
• Controlled types null ;
end loop;
• Some applications of the attributes Address, Access, end loop;
Unchecked_Access, Unrestricted_Access end Custom_Delay;

• Non library-level tagged types procedure Blink_Times (Times : Integer) is


begin
• Annex E (Distributed Systems) for Counter in 1 .. Times loop
PortB := 2#00100000#; Custom_Delay (5 ∗
DEFAULT_DELAY);
4 An Example Implementation PortB := 2#00000000#; Custom_Delay (5 ∗
DEFAULT_DELAY);
In this section we will show a very simple example appli- end loop;
cation. For this implementation, it is enough to use the end Blink_Times;
arduino.gpr project file described in section 2.2.2 and the
begin
atmega328p.ads described in section 2.2.1 as specification
package and a main file. So, suppose we desire to move a −− Initialize all PortB pins as outputs
5-wires Stepper Motor some steps forward, blink a led once, DDRB := 2#11111111#;
move the Stepper Motor some steps backward and blink the
−− Forward over the coils
same led twice. The Stepper Motor used is a Mototech S35S5 for Counter in 1 .. DEFAULT_CYCLES loop
extracted from an old optical scanner. PortB := COIL0; Custom_Delay (DEFAULT_DELAY);
PortB := COIL1; Custom_Delay (DEFAULT_DELAY);
Figure 1 shows the connections among the components. The PortB := COIL2; Custom_Delay (DEFAULT_DELAY);
Arduino Duemilanove is connected to the Stepper Motor PortB := COIL3; Custom_Delay (DEFAULT_DELAY);
through the current driver ULN2803AP using pins B0, B1, end loop;
B2 and B3, and the led is connected using pin B5.
−− Blink led once
Blink_Times (1);
Vcc 12V
−− Backward over the coils
for Counter in 1 .. DEFAULT_CYCLES loop
PortB := COIL3; Custom_Delay (DEFAULT_DELAY);
ATmega328P

ULN2803AP

B0 5 10 0 PortB := COIL2; Custom_Delay (DEFAULT_DELAY);


Mototech

PortB := COIL1; Custom_Delay (DEFAULT_DELAY);


B1 6 11 1
S35S5

PortB := COIL0; Custom_Delay (DEFAULT_DELAY);


B2 7 12 2 end loop;
B3 8 13 3
−− Blink led twice
GND 9 14 4 Blink_Times (2);
B5
−− Disable all output pins
PortB := 0;
LED 1k end Main;

GND
5 Conclusion
Figure 1: Connections between Arduino Duemilanove, Led,
This paper shows an approach for developing Ada applica-
ULN2803AP and Stepper Motor.
tions for 8-bit AVR micro-controllers, using GNAT AVR as
the Microsoft Windows cross-compiler, and GNAT Program-
ming Studio as IDE. GNAT AVR follows the Zero Footprint
−− main.adb Profile for Real-Time Library, thus not all Ada Real-Time
with ATmega328P; use ATmega328P;
features are present in this development such as multi-tasks.
procedure Main is We believe that Ada can improve the quality of the applica-
−− Stepper Motor coils definitions
tions for AVRs. The AVR community is invited to try this
COIL0 : constant := 2 ∗∗ 0; approach. Also the Ada community is encouraged to propa-
COIL1 : constant := 2 ∗∗ 2; gate its knowledge about real-time applications to the AVRs

Vo l u m e 3 3 , N u m b e r 4 , D e c e m b e r 2 0 1 2 Ada User Journal


P. V. R e g o 305

field in a mean that it certainly will gain more popularity over [10] K. N. Gregertsen, “Execution time control : A hard-
more one embedded software branch. ware accelerated ada implementation with novel support
for interrupt handling,” Ph.D. dissertation, Norwegian
University of Science and Technology, Department of
References Engineering Cybernetics, 2012.
[1] J. D. Brock, R. F. Bruce, and S. L. Reiser, “Using [11] J. Ras and A. M. Cheng, “A deterministic run-
Arduino for introductory programming courses,” J. time environment for Ada-05 on the ATmega16
Comput. Sci. Coll., vol. 25, no. 2, pp. 129–130, Dec. microcontroller,” Ada Lett., vol. 30, no. 3, pp. 13–22,
2009. [Online]. Available: http://dl.acm.org/citation. Oct. 2010. [Online]. Available: http://doi.acm.org/10.
cfm?id=1629036.1629057 1145/1879097.1879072

[2] P. Jamieson, “Arduino for Teaching Embedded Systems. [12] J. S. Andersen, “Programming Arduinos in Ada.” Free
Are Computer Scientists and Engineering Educators and Open Source Software Developers’ European Meet-
Missing the Boat?” International Conference on ing (FOSDEM’12), February 2012.
Frontiers in Education: Computer Science and [13] AdaCore. (2009, Mar) GNATPro available for 8-bit
Computer Engineering (FECS’11), 2011, accessed: AVR Microcontroller. NY, USA. Accessed: 23/06/2012.
23/06/2012. [Online]. Available: http://www.users. [Online]. Available: http://www.adacore.com/press/
muohio.edu/jamiespa/html_papers/fecs_11.pdf 8-bit-avr-microcontroller/
[3] R. Balogh. Robotics course with the Acrob robot. [14] ——. Ada Development Environment for the Atmel
Österreichische Gesellschaft für innovative Comput- AVR 8-bit microcontroller. NY, USA. [Online]. Avail-
erwissenschaften. Accessed: 23/06/2012. [Online]. able: http://www.adacore.com/gnatpro-safety-critical/
Available: http://www.innoc.at/fileadmin/user_upload/ platforms/avr/
_temp_/RiE/Proceedings/15.pdf
[15] Ministry of Defence, Requirements for Safety Related
[4] ——, “Acrob - an Educational Robotic Platform,” AT&P Software in Defense Equipment, Directorate of Standard-
Journal Plus, vol. 10, no. 2, pp. 6–9, 2010, ISSN 1336- ization - Defense Standard, Rev. INTERIM Def Stan
5010. 00-55/Issue 1, Apr 1991.

[5] I. B. Gartseev, L.-F. Lee, and V. N. Krovi, “A Low- [16] Atmel Corporation. 8-bit AVR Microcontroller with
Cost Real-Time Mobile Robot Platform (ArEduBot) 4/8/16/32K Bytes In-System Programmable Flash:
to support Project-Based Learning in Robotics & ATmega48PA/ ATmega88PA/ ATmega168PA/ AT-
Mechatronics,” R. Stelzer and K. Jafarmadar, Eds. mega328P. San Jose, CA, USA. Accessed: 21/06/2012.
INNOC - Austrian Society for Innovative Computer [Online]. Available: http://www.atmel.com/Images/
Sciences, 2011, pp. 117–124, accessed: 23/06/2012. doc8161.pdf
[Online]. Available: http://www.innoc.at/fileadmin/ [17] J. W. McCormick, F. Singhoff, and J. Hugues, Building
user_upload/_temp_/RiE/Proceedings/25.pdf Parallel, Embedded, and Real-Time Applications with
Ada, 1st ed. New York, USA: Cambridge University
[6] K. N. Gregertsen and A. Skavhaug, “An efficient and Press, 2011.
deterministic multi-tasking run-time environment for
Ada and the Ravenscar profile on the Atmel AVR32 [18] AVR Topics: J.2 Compiler and Linker Flags for AVR.
UC3 microcontroller,” in Design, Automation Test in Accessed: 24/06/2012. [Online]. Available: http://docs.
Europe Conference Exhibition, 2009., April 2009, pp. adacore.com/gnat-cross-docs/html/gnat_ugx_12.html
1572 –1575. [19] AdaCore. Runtime Profiles. [Online]. Available:
[7] ——, “A Real-Time Framework for Ada 2005 and the http://www.adacore.com/gnatpro/toolsuite/runtimes/
Ravenscar Profile,” in Software Engineering and Ad- [20] ——. Ada Restrictions in the Zero Footprint Pro-
vanced Applications, 2009. SEAA ’09. 35th Euromicro file. [Online]. Available: http://docs.adacore.com/
Conference on, aug. 2009, pp. 515 –522. gnat-hie-docs/html/gnathie_ug_4.html#SEC27
[8] ——, “Execution-time control for interrupt han-
dling,” Ada Lett., vol. 30, no. 1, pp. 33–
44, May 2010. [Online]. Available: http:
//doi.acm.org/10.1145/1806546.1806550

[9] ——, “Implementing the new Ada 2005 timing


event and execution time control features on the
AVR32 architecture,” Journal of Systems Architecture,
vol. 56, no. 10, pp. 509 – 522, 2010. [Online]. Avail-
able: http://www.sciencedirect.com/science/article/pii/
S1383762110000937

Ada User Journal Vo l u m e 3 3 , N u m b e r 4 , D e c e m b e r 2 0 1 2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy