1 | Page
GertDuino Board
Exclusively From
User Manual
By: G.J. van Loo, Version 1.3
Dated: 6th Nov 2013
2 | Page
Contents .......................................................................................................................... 2
1 Introduction ........................................................................................................................................ 3
1.1 Identify ......................................................................................................................................... 3
1.2 Comparison ................................................................................................................................. 4
1.3 Vext ............................................................................................................................................... 4
2 RS232/UART .................................................................................................................................... 4
2.1 Atmega-328 & Pi UART ........................................................................................................... 5
2.2 Atmega-48 UART ....................................................................................................................... 5
3 Atmega-328 ..................................................................................................................................... 6
3.1 Features ....................................................................................................................................... 6
3.2 Program the Atmega-328 ....................................................................................................... 6
3.3 Usin g /running th e Atmega-328 ............................................................................................ 7
4 Atmega-48 ........................................................................................................................................ 7
4.1 Features ....................................................................................................................................... 7
4.2 Program the Atmega-48 ......................................................................................................... 7
4.3 Usin g /running th e Atmega-48 ............................................................................................... 8
4.4 Real Time Cloc ........................................................................................................................... 8
4.5 Infra-red recei ver/remote control recei ver ....................................................................... 8
4.6 Battery Drain .............................................................................................................................. 9
4.7 Atmega-48 L ED tr i ck .............................................................................................................. 10
5 Connectors ...................................................................................................................................... 10
5.1 Alternate functions. ................................................................................................................ 10
5.2 Atmega-328 .............................................................................................................................. 12
5.3 Atmega-48 ................................................................................................................................ 14
5.4 Raspberry-Pi ............................................................................................................................. 15
6 F req uently Asked Questions (FAQs) .................................................................................. 16
7 How to start ................................................................................................................................... 17
7.1 On the Raspberry-Pi: ............................................................................................................. 17
7.2 On a PC ...................................................................................................................................... 17
8 Example programs ...................................................................................................................... 19
8.1 Atmega-328 .............................................................................................................................. 19
8.2 Atmega-48 ................................................................................................................................ 22
9 C ont rol Arduino Reset ............................................................................................................. 265
10 Appendix A : GertDuino Schematic ..................................................................................... 26
3 | Page
1 Introduction
The GertDuino is a Raspberry-Pi add-on board which offers the same functionality as an
Arduino-Uno but with some extra added features.
1.1 Identify
The picture below lets you identify the various functions on the board.
RS232 level conver ter can be used by:
o Raspberry-Pi
o or A tmega -328
o or A tmega -48
At meg a 32 8 ( Arduino-Uno® compatible) with:
o Arduino-Uno® comp atib le connector s
o Reset button
o 2 user push buttons
o 6 LEDs.
At meg a 48 wi th:
o I/O connector w ith 20 pins.
o High precision RTC crystal
o Battery backup power supply
o IRDA interface
PCB Overview:
Atmega 328
R232 Jumpers
Atmega 48
2 user buttons 6 LEDS
Reset button
Battery for RTC
IRDA receiver
More I/O RTC X-tal
Picture
1: GertDuino Functions
4 | Page
1.2 Comparison
There are some differences between a norm al Arduino-Uno a nd th e GertDuino.
Table 1: Comparison GertDuino vs Arduino -Uno
1.3 Vext
As the board does not have a separate supply the Vext is not connected. If you want it
connected you have to add the followi ng components:
J1 , L4 (or a short inste a d of L4), D20 (or a shor t inste a d of D20) .
2 RS232/UART
The Gerduino board has a RS232 level converter which will convert the signals form a UART to
the RS232 standard voltages (And invert them as per that same st andard). The RS232 signals
com e from J12.
Pin 3 is the r ec eive
Pin 2 is t he transm i t
Pin 1 is the ground
Function
Arduino-Uno
GertDuino
USB
Slave in te rface
-
Reset button
Yes
Yes
Power supply
7..12V, ~250mA
<5V Raspberry-Pi>
3V3 supply
~50mA
~150mA.
LED's
One Not-buffered
Six Buffered
User pus hbuttons
-
Two
RS232 buffer
-
Yes
Real-Time-Clock
-
Yes
Infra-red interface
-
Yes
5 | Page
Connections can be made in many ways:
2.1 Atmega-328 & Pi UART
Pi to RS232 buffers Atmega-328 to RS232 buffers Pi to A tme g a -328
Raspberry-Pi
Atmega 328
RS232
buffers
Raspberry-Pi
Atmega 328
RS232
buffers
Raspberry-Pi
Atmega 328
RS232
buffers
Atmega
48
Atmega
48
Atmega
48
2.2 Atmega-48 UART
Atmega-48 to RS232 buffers Atmega-48 to Atmega-328 Atmega-48 to Pi
Raspberry-Pi
Atmega 328
RS232
buffers
Raspberry-Pi
Atmega 328
RS232
buffers
Raspberry-Pi
Atmega 328
RS232
buffers
Atmega
48
Atmega
48
Atmega
48
6 | Page
3 Atmega-328
3.1 Features
This device is compatible with the Arduino Uno. In contrast to the 328 on the GERTBOARD this
device runs of 5V, has the 16MHz oscillator and has connectors which are 100% Arduino-Uno
compatible. It also contains the reset switch.
This board als o has the following c omponents which you will not find on th e Uno :
2 User push buttons
6 LEDs1
LEDs
One LED is connected to PB5 (aka Port-13 aka SCK). This is compatible with the UNO. The
GertDuino has a five more LEDs. The total list of LEDs is:
- PB5 (Port-13)
- PB1 (Port-9)
- PB2 (Port-10)
- PD3 (Port-3)
- PD5 (Port-5)
- PD6 (Port-6)
The LEDs are not directly connected but are buffered and thus do not give any significant load
on the signal pins.
User butt ons
The two user buttons are connected to pins PC2 and PC3. They will only function correctly if the
pin s ha ve a n i nternal o r exter na l p u ll-up. T he b ut to n ar e co n ne cted th r o ugh a 1K Ohm resis to r
so they will not cause a short if a pi n is set as output and the button is pressed.
3.2 Program the Atmega-328
To program this device from th e Raspberry-Pi you have to place the following 4 jumpers:
Then run the script Pro gram 328as descr ibed i n sect ion 8.1.Atmega-328.
To pro g ra m t he 3 2 8 using a JTAG-ICE you need to use the "squid" cable and make the following
connections:
1 LED: T he first debug tool any programmer grabs for.
7 | Page
At the left there are th e GND (white ) and 5V (Purp le) connec tio ns.
At the top row right are the Reset (green), Mosi (Red), Clk (Black) and Miso (G rey)2.
The equivalen t JTAG n ames for these are: nSRST, TDI, TCK, TDO
3.3 Usi ng/ru nning the Atmega-328
When the device has been programmed it will run that program independent of the Raspberry-
Pi. In fact you can remove the board from the Raspberry-Pi and use it standalone.
When developing programs you may leave the jumpers in place as the programme will tri-state
its pins and set the reset pin high when it has finished. Th is is NOT the case i f the PI is reset or
not powe red. Especial ly the rese t-jumper needs to be removed otherwise the Raspberry-Pi GPIO
pin 8 (which is default low) will the keep the 328 device in reset or you can run the reset_off
script.
You should also remove the jumpers if you wa nt to use an y of the fol lowing pins: B3,B4,B5,C 6.
4 Atmega-48
4.1 Features
This device is intended to be used as Real Time Clock (RTC) and/or as IRDA fron t end. However
it is also freely programmable by the user and thus can be used for any other application, giving
the user the power of not one but TWO Atmega devices to play with.
Note: The I2C interface of the Atmega-48 is connected permanently to the Raspberry-Pi I2C
interface <GOIO0/1 on rev1, GPIO 2/3 on rev2>.
Also beware that if you make programming errors with the Atmege-328 the device can easily be
replaced. This is not the case with the Atmega-48. It is therefore strongly recommended that
you are extra careful and do not damage any of the I /O p orts.
Spare c onnections.
The following I/O pins of the Atmega-48 are not used and are brought out to a connector:
B0,B1,B2, B3, B4,B5, C0,C1,C2,C3,D0,D1,D4, D5, D6, D7.
Beware that B3, B4 and B5 are also used for programming the device.
4.2 Program the Atmega-48
To program the Atmega-48 from the Raspberry-Pi you have to place the following 4 jumpers:
The programming is the same as the 328 but replace "328p" with "48pa".
2 The colours used her e are the sam e as on MY sq ui d cable but I can 't guar ant ee all squid ca bl es are th e same.
8 | Page
To program the 48 us ing a JTAG-ICE you n eed to us e the "squi d " cable and make the foll ow ing
connections:
At the left there are the GND (white) and 5V (Purple) con nec tio ns.
At the bottom row right are the R es et (green), Mosi (Red), Clk (Black) and Miso (Grey).3
The equivalent JTAG names for these are: nSRST, TDI, TCK, TDO
4.3 Usi ng/ru nning the Atmega-48
What was written about the 328 also is valid for the 48: when the device has been programmed
it will run that program independent of the Raspberry-Pi. I n fact you can remove the board from
the Raspberry-Pi a nd u se i t sta ndalone.
When developing programs you may leave the ju mpers in place as the programme will tri-state
its pins and set the reset pin high when it has finished. This is NOT th e case if the PI is reset or
not powered. Especially the reset -jumper needs to be removed otherwise the Raspberry -Pi GPIO
pin 8 (which is default low) will the keep the 48 device in reset or you can run the reset_off
script.
You should also remove the jumpers if you wa nt to u se any of the following pins : B3,B4,B5,C 6.
4.4 Real Time Clock
The Atmege-48 device has a 32768Hz crystal connected to operate as a Real-Time-Clock (RTC) .
Example code for this can be found under sectio n 8.2 Atmega-48. The Crystal is a high quality
type and und er normal c onditions a deviation is les s t han 1 sec/3 days.
The other part of the RTC is that t h e A tme ga-48 has a 3V battery. It will switch to that battery
when the 5V power is removed. As the Atmega-48 is a fully p rogram med m icrocontroller it can
be set-up to pe rf orm ot her op er ati ons or h old ot h er dat a wh en the main pow er of th e B CM2835
is removed.
If you have programmed the Atmege-48 correctly it uses ~1µA when powered down.
4.5 Infra-red receiv er/rem ot e control receiver
Th e B CM2 83 5 does not have a native IRDA interface. The protocol can be implemented using a
standard GPIO pin but that puts a very heavy bu rden on th e CPU. To su pport IRDA the Atmega-
48 has a TSSOP4038 IRD device connected to pin D3. This device supports the most common
IRDA protocol: 38KHz IR signal.
Unfortunately we could not run the IRDA interface from the battery as it uses too much current
(~450 µA). Thu s y ou need th e 5V p resent for it to operate.
The IRDA can also be used if the GertDuino is used stand-alone to control the connected logic
using a remote control.
3 The colours used her e are the sam e as on MY sq ui d cable but I can 't guar ant ee all squid ca bl es are th e same.
9 | Page
Note that 95% of all TV/Video/CD remote controls use the 38KHz infra-red signal, but the
coding varies greatly from type to type.
4.6 Battery Drain
If a battery is present and the power of the Raspberry-Pi is switch ed of th e Atmega-48 will still
remain powered by the Battery. It will also keep running. Unless the battery is removed or the
Atmega-48 is programmed to go into a special ultra-low-power condition, the battery will be
draine d in a short time.
Even if you think the device is in ultra-low-power mode it can still consume power if it has to
drive outputs high.
Measurements have also show that if a UART connection exists between the Atmega-48 to th e
Raspberry-Pi (even if it is not used) that increases the lower power current from 1µA to about
100µA.
To m easure th e c urrent c onsumpti on you have to us e a 3V su pply and conn ect it t o t he batt ery
holder but between the supply and the battery holder you have to place a current (Ampere)
me ter. Opt i onall y y ou can c onne ct a sc ope on on e of th e I /O p ins of t he Atm ege48 t o se e if th e
program is running. You should NOT connect anything to one of the output which loads an I/O
pin as that will cause extra current consumption.
This is a block diagram of the setup:
A
3V
Atmega48pa
Gertduino
GND
And this is how it looks in rea l life:
The meter shows a current consumption of 1.Amp. (The meter is shown enlarged in the lower
left hand corner of the picture) .
10 | Page
If possible limit the current from your power source to a few milli-amps. I managed to blow a
fuse of my meter performing the measur ements because I accidentally shorted the supply when
placing th e probe on th e batter y holder.
4.7 Atmega-4 8 LED trick
If you are debugging, an LEDs is often the first tool you reach for. But the Atmega-48 does not
have any LEDs. H owever the Atmega-328 does! Th ere are two ways in which you can use th ese
LEDs :
The safest way is to remove the 328 from its socket.
The second way is to erase the 328 so that all its pins are inputs.
You can then use the connectors to feed a signal to an LED. Simplest way is to use a female-
male strap between connector J10 and e.g. pins, 2,3 or 6 of J14 .
5 Connectors
The board contains a number of connec tors. Y ou will find that in the document the connectors of
the Atmega devices have two ways of numbering: There are the single numbers 0..13 and
A1..A3. These are the numbers used in man y Arduino exam ple programs. A longside those I use
th e official pin names (PB0..PB7, PD0..PD7, PCO..PC3). Th e latter are easier t o use i f you have
to work with the AVR datasheet.
5.1 Alternate functions.
The Atmega-328 and the Atemege-48 have exactly the same pins with the same functionality.
The devices only differ in the size of their v ar i ou s m em o r i es . Th e f ollow ing is a t ab le of th e pins
and all the functions they can carry. These where copied from the AVR datashe et. F or de ta ils of
the functions you should read that datasheet.
11 | Page
#
Functions
-
XTAL2 (Chip Clock Oscillator pin 2)
TO SC2 (Timer Oscillator pin 2)
PCINT 7 (Pi n Change Interrupt 7)
-
XTAL1 (Chip Clock Oscillator pin 1 or External clock input)
TO SC1 (Timer Oscillator pin 1)
PCINT 6 (Pi n Change Interrupt 6)
13
SCK (SPI Bus Master clock Input)
PCINT 5 (Pi n Change Interrupt 5)
12
MISO (SPI Bus Master Input/Slave Output)
PCINT 4 (Pi n Change Interrupt 4)
11
MOSI (S PI Bus Master Output/Slave Input)
OC2A (Timer/Counter2 Output Compare Match A Output)
PCINT 3 (Pi n Change Interrupt 3)
10
SS (SPI Bus Master Slave select)
OC1B (Timer/Counter1 Output Compare Match B Output)
PCINT 2 (Pi n Change Interrupt 2)
9
OC1A (T im er/Counter1 Output Compare Match A Output)
PCINT 1 (Pi n Change Interrupt 1)
8
ICP1 (Time r/Counter1 Input Captu r e Input)
CLKO (Divided System Clock Output)
PCINT 0 (Pi n Change Interrupt 0)
#
Name
Functions
A5 PC5
ADC5 (ADC Input Chan nel 5)
SCL (2-wire Serial Bus Clock Line)
PCINT13 (Pin Change Interrup t 13)
A4 PC4
ADC4 (ADC Input Chan nel 4)
SDA (2-wire Serial Bus Data Input/Output Line)
PCINT12 (Pin Change Interrup t 12)
A3 PC3
ADC3 (ADC Input Chan nel 3)
PCINT11 (Pin Change Interrup t 11)
A2
PC2
ADC2 (ADC Input Channel 2)
PCINT10 (Pin Change Interrup t 10)
A1
PC1
ADC1 (ADC Input Chan nel 1)
PCINT 9 (Pi n Change Interrupt 9)
A0
PC0
ADC0 (ADC Input Chan nel 0)
PCINT 8 (Pi n Change Interrupt 8)
12 | Page
#
Name
Functions
7 PD7
AIN1 (Analog Comparator Negative Input)
PCINT23 (Pin Change Interrup t 23)
6
PD6
AIN0 (Ana log Comparat or Positive Input)
OC0A (Timer/Counter0 Output Compare Match A Output)
PCINT22 (Pin Change Interrup t 22)
5
PD5
T1 (Timer/Counter 1 External Counter Input)
OC0B (Timer /Count er0 Outp ut Com pare Match B Output )
PCINT21 (Pin Change Interrup t 21)
4
PD4
XCK (USART External Clock Input/Output)
T0 (Timer/Counter 0 External Counter Input)
PCINT20 (Pin Change Interrup t 20)
3
PD3
INT 1 ( E x terna l Interrup t 1 Input)
OC2B (Timer /Count er2 Outp ut Com pare Match B Output )
PCINT19 (Pin Change Interrup t 19)
2
PD2
INT 0 ( E x terna l Interrup t 0 Input)
PCINT18 (Pin Change Interrup t 18)
1
PD1
TXD (USART Outpu t Pin)
PCINT17 (Pin Change Interrup t 17)
0
PD0
RXD (USART Input Pin)
PCINT16 (Pin Change Interrup t 16)
5.2 Atmega-328
T he Atmega-328 pins are brought to connectors compatible with the Arduino-Uno.
J14 J7
1 1
11
1
1
1
1
1
1 1
11
1
1
1
1
1
Pin
No.
Signal
Pin
No.
Signal
10
A5/PC5/SCL
8
7/PD7/AIN1
9
A4/PC4/SDA
7
6/PD6/AIN0/LED6
8
AREF
6
5/PD5/T1/LED5
7
Ground
5
4/PD4/T0
6
13/PB5/SCK/LED0
4
3/PD3/INT1/LED4
5
12/PB4/MISO
3
2/PD2/INT0
4
11/PB3/MOSI
2
1/PD1/TXD
3
10/PB2/SS/LED2
1
0/PD0/RXD
2
9/PB1/PCINT1/LED1
13 | Page
1
8/PB0/CLK0
Pin 1 is on the right-hand side so these tables top-to-bottom are the pins from left-to-right.
J9 J6
11
11
1
1
1
1
1
11
11
1
1
1
1
1
Pin
No.
Signal
Pin
No.
Signal
8
NC
6
A5/PC5/SCL
7
Ground
5
A4/PC4/SDA
6
Ground
4
A3/PC3/ADC3/BUT1
5
5V
3
A2/PC2/ADC2/BUT0
4
3V3
2
A1/PC1/ADC1
3
Reset (Active low)
1
A0/PC0/ADC0
2
5V
1
NC
Pin 1 is on the lef t-ha nd sid e so the se ta bles to p-to-bottom are the pins fr om right-to-left.
Bew a r e that Pin 8 of J9 is normally connected directly to the input voltage which has been
removed an d thus is NC here.
14 | Page
5.3 Atmega-48
All unused pin of the Atmega-48 are brought out to a 20 -pin connector:
11
11
1
1
1
1
1
1
20
13 : PB5/SCK
12 : PB4/MISO
11 : PB3/MOSI
10 : PB2/SS
9 : PB1/PCINT1
8 : PB0/CLK0
7 : PD7/AIN1
6 : PD6/AIN0
5 : PD5/T1
GroundGround
T0/PD4 : 4
TXD/PD1 : 1
RXD/PD0 : 0
ADC3/PC3 : A3
ADC2/PC2 : A2
ADC1/PC1 : A1
ADC0/PC0 : A0
VBAT/5V
5V
The supply 5V/VBAT which goes to the Atmega-48 also goes to the connector pin 3. Any
equipment connected to that pin will also draw current from the battery if the 5V is switched off.
The supply comes through a Schottky diode so the actual voltage is lower: ~4.5 Volts. Also the
current consumption should be limited ~100mA.
The follow ing pins of the ATm ega -48 are dedicated connected:
Pin
Hard w ired to
Function
PD2
5V Supply
Detect absence of 5V supply (f or RT C)
PD3
IRDA output
Receive IRDA sig na l
PC5
SCL
I2C connecti on with the Pi
PC4
SDA
I2C connecti on with the Pi
PB7
XTAL1
32768Hz Tuning crystal
PB6
XTAL2
32768Hz Tuning crystal
PC6
Program res et
Reset when programming
T he Atmege-48 does not have a dedi c ated reset pin as that would interfere with its function as
real-time-clock . A res et can b e obt a ined by pu llin g pin 4 of J13 low .
15 | Page
5.4 Raspberry-Pi
All connections between the board and the Raspberry-Pi are protected against 5V signals. The
I2C bus has FET level switches. All the other signals use resistive dividers.
The follow ing connecti ons of the Ra spberr y-Pi are used:
5V
3V 3 ( I2C level converters only)
GPIO0/2 (I2C SD A)
GPIO1/3 (I2C SCL)
The follow ing connecti ons of the Ra spberr y-Pi are us ed if the programming jumpers or UART
jumpers are placed:
GPIO14 (UART-Tx)
GPIO15 (UART-Rx)
GPIO8 (Reset)
GPIO9 (MISO)
GPIO10 (MOSI)
GPIO11 (SCLK)
16 | Page
6 Frequently Asked Questions (FAQs)
Some questions you may ask and the answers.
avrd ud e: AVR device not responding
Q: When I try to program the device I get an error: "avrdude: A VR device not responding."
A: The most likely cause is that you have forgotten to place the four programming jumpers.
See section 3.2 Program the Atmega-328.
Why is my program slow?
Q: When I run the program it is very slow. Where I expect a delay of 1second it takes much
longer.
A: Straight from the factory the CPU runs from the internal 8MHz clock and that is divided by
8. Thus the processor runs at 1 MHz. To switch to the full speed, using the external 16MHz
oscillator run the avrdude command as described in 8.1Atmega-328under "Initial clock
setup"
Why does my prog ram not run?
Q: When I upload the program it runs fine but when I halt the Raspberry-Pi or when I start the
Raspberry-P i my p rogram does not work.
A: GPIO 8 controls the Reset of the Arduino. This pins must be high but for your program to
run. The simplest solution is to remove the programming jumpers. Alternative is to prog ram
the GP I O -8 p in h ig h using the reset_off script. The avrdude with the -c gpi o op tion d oes this
for you so normally after programming th e reset has be en removed.
I have a different comp i ler
Q: I use th e AVR compiler on my PC. How do I pr ogram the Atmega on the Raspberry-Pi?
A: I have only experience with the GCC version (AVR 5.1 and higher). After compilation you
find a .hex file in the debug directory. You have to transfer that file somehow to the
Raspberry-Pi and use the programmer scri pt Program 328 as described in 8.1Atmega-328
to program the device(s) on the GertDuino. (If you have the script already installed use
"./program_328 <hex file>)
The Ras p ber ry -Pi b oots dif fer ent: it has big text and not the normal prompt!
Q: When I pl ug the GertDuino on the Raspb erry-P i i t bo ots diff er ent: It ha s b ig te xt and no t t he
normal prompt!
A: Pin 5 of the GPIO connector is used to indicate ‘safe boot mode’. If that pin is low when
booti ng the Raspberry-Pi boots in “safe mo de. Pi n 5 is also connected to the Atmge-48. It
is one of the I2C pins. Thus if your 48 is driving a LOW on that pin the Pi always boots in
safe mode.
To prevent this you can put “avoid_safe_mode=1” in the config.txt file and the pi will boot
normally.
Why i s t here no battery sup plied
Q: The GertDuino has a battery holder but there is no battery in there. Why do I have to buy
my own?
A: These batteries are lithium batteries. Those are classified as ‘Dangerous Goods’ and
require special paper work, warning labels and other precautions when shipped. And th at i s
for shipping within the UK. International shipping becomes a nightmare. So we decided to
leave it off.
17 | Page
7 How to start
Before you can program the devices you need to have a cross compiler. A cross compiler is a
compiler which runs on one type of processor, but generates code for a different type. In this
case the compiler runs on the Raspberry-Pi (ARM11 device) but makes code for the Atmel
devices.
7.1 On the Raspber ry-Pi:
When programming the Atmel devices on the Raspberry-Pi you have two choices:
Use t he Ardui no GUI
Use the GCC Atmel compiler
For b oth you need to have a cross compiler for the Atmega dev ices. Ea s ie st is to insta ll the
Arduino p a ckage:
sudo apt-get install arduino
avrdude
You need to use a program called "avrdude" to program the devices BUT you need a special
ve rsion of "avrdude" which can program the devices using the GPIO of the Raspberry-Pi. Thanks
for Gordon Henderson (projects.drogon.net) who has provided these:
Standard Debian Squeeze:
cd /tmp
wget http://project-downloads.drogon.net/gertboard/avrdude_5.10-4_armel.deb
sudodpkg -i avrdude_5.10-4_armel.deb
sudochmod 4755 /usr/bin/avrdude
Debian Raspbian:
cd /tmp
wget http://project-downloads.drogon.net/gertboard/avrdude_5.10-4_armhf.deb
sudodpkg -i avrdude_5.10-4_armhf.deb
sudochmod 4755 /usr/bin/avrdude
You can n ow com pile p rogr ams for t he At mega dev ices and up loa d the progr am in to the chi p on
the GertDuino. Example source code, Makefile and how to upload the program can all be found
in section 8 Example progra m s .
If you want to use the Arduino development environment you have to adapt it. See
projects.drogon.net/raspberry-pi/gertboard/arduino-ide-installation-isp / ho w to do that.
7.2 On a PC
Atmel have a free C-compiler. You can get information about the latest version here:
http://www.atmel.com/tools/ATMELSTUDIO.aspx
You can comp ile on the PC bu t you need to tr a nsfer the final .hex file t o the Raspberry-Pi before
you can program the Atmega devices.
18 | Page
Alternative is that you buy a JTAG-ICE box and use that to program and the devices but th at is
a lot more expensive. It does have the advantage that you can use it for debugging as well:
St ep through the program, set breakpoints ,inspect variables etc .
19 | Page
8 Example programs
8.1 Atmega-328
blink.c source code:
/*
* blink.c
*
* Created: 23/09/2013 21:04:02
* Author: G.J. van Loo
* Simple example program to 'walk' the LEDs
*/
#include <avr/io.h>
#define DELAY 250
#define F_CPU 16000000
// Some macros that make the code more readable
#define output_low(port,pin) port &= ~(1<<pin)
#define output_high(port,pin) port |= (1<<pin)
#define set_input(portdir,pin) portdir&= ~(1<<pin)
#define set_output(portdir,pin) portdir |= (1<<pin)
// Outputs are:
// LED0 = PB5
// LED1 = PB1
// LED2 = PB2
// LED3 = PD3
// LED4 = PD5
// LED5 = PD6
void delay_ms(unsigned intms)
{
uint16_t delay_count = F_CPU / 17500;
volatile uint16_t i;
while (ms != 0) {
for (i=0; i != delay_count; i++);
ms--;
}
} // delay_ms
void delay()
{ long d;
unsigned char oldb,oldd;
for (d=0; d<DELAY; d++)
{
delay_ms(1);
if ((PINC & 0b00001000)==0)
{
oldb = PORTB;
oldd = PORTD;
PORTB = 0xFF;
PORTD = 0xFF;
delay_ms(1);
PORTB = oldb;
20 | Page
PORTD = oldd;
d--;
}
else
{ if ((PINC & 0b00000100)==0)
d--;
else
delay_ms(1);
} // if button pressed
} // if button pressed
} // delay
int main(void)
{ // int b;
// Set all LED connections to output
DDRB = 0b00100110;
DDRD = 0b01101000;
PORTB = 0x00;
PORTD = 0x00;
// Set button (port C) to input
DDRC = 0b00000000;
// pull-up on C2 & C3:
PORTC = 0b00001100;
while(1)
{ // convoluted but simple walk the leds
output_high(PORTB,5);
delay();
output_low (PORTB,5);
output_high(PORTB,1);
delay();
output_low (PORTB,1);
output_high(PORTB,2);
delay();
output_low (PORTB,2);
output_high(PORTD,3);
delay();
output_low (PORTD,3);
output_high(PORTD,5);
delay();
output_low (PORTD,5);
output_high(PORTD,6);
delay();
output_low (PORTD,6);
output_high(PORTD,5);
delay();
output_low (PORTD,5);
output_high(PORTD,3);
delay();
output_low (PORTD,3);
output_high(PORTB,2);
delay();
output_low (PORTB,2);
output_high(PORTB,1);
delay();
output_low (PORTB,1);
} // forever
} /} // main
21 | Page
Makefile
# Makefile:
# Make the GertDuino m328p firmware.
#
# Copyright (c) 2013 Gordon Henderson <projects@drogon.net>
#################################################################################
# This file is part of gertduino-m328:
#Software to run on the Atmega328p processor on the Gerduino board
#Can be used for the Atmega328p processor on the GERTBOARD as well
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this. If not, see <http://www.gnu.org/licenses/>.
#################################################################################
TARGET=blink
MCU=atmega328p
FREWQ=16000000
# Debug
#DEBUG = -gstabs
# C flags
CC = avr-gcc
#CFLAGS = $(DEBUG) -O3 -Wall -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(FREWQ) $(INCLUDE)
CFLAGS = $(DEBUG) -O2 -mcall-prologues -Wall -std=gnu99 -mmcu=$(MCU) -
DF_CPU=$(FREWQ) $(INCLUDE)
LD = avr-gcc
#LDFLAGS2=-Wl,-uvfprintf -lprintf_flt
LDFLAGS = -mmcu=$(MCU) $(DEBUG) $(LIBLOC) $(LDFLAGS2)
#LIBS = -ldross -lm
SRC = $(TARGET).c
OBJ = $(SRC:.c=.o)
all: $(TARGET).hex
$(TARGET).hex: $(TARGET).elf
@echo [hex] $<
@avr-objcopy -j .text -j .data -O ihex $(TARGET).elf $(TARGET).hex
$(TARGET).elf: $(OBJ)
@echo [Link] $<
@$(LD) -o $@ $(OBJ) $(LDFLAGS) $(LIBS)
@avr-size $(TARGET).elf
# Generate .lst file rule
%.lst : %.o
@echo [lst] $<
@avr-objdump -h -S $<> $@
22 | Page
avrdude -qq -c gpio -p atmega328p -U lock:w:0x3F:m -U efuse:w:0x07:m -
U lfuse:w:0xE7:m -U hfuse:w:0xD9:m
.c.o:
@echo [CC] $<
@$(CC) -c $(CFLAGS) $< -o $@
.PHONEY: clean
clean:
rm -f *.o *.elf *.hex *.lst Makefile.bak *~
Program 328
#!/bin/bash
# script to program 328p device using AVRDUDE and a hex file
if [ "$1" == "" ]; then echo Missing argument
exit 1;
fi
# if ends in .hex use full argument
# otherwise add the .hex
ext=${1:${#1}-4}
if [ "$ext" == ".hex" ]; then
/usr/bin/avrdude -c gpio -p m328p $1 -Uflash:w:$1
else
/usr/bin/avrdude -c gpio -p m328p $1.hex -Uflash:w:$1.hex
fi
Sav e the abov e code in a fil e c alled prog ram_328 and th en run " chmod 777 program_328". Use
./ prog ram_328 <hex file> to program the Atmega device.
Initial clock setup
You n ormally run the abov e command when you get a brand new device. It programs the
Atmega328 to use the external 16MHz Crystal.
8.2 Atmega-48
This section shows an example program for the Atmega48. You will find that the makefile and
the programming files are very simil ar to the 328 example.
low_p ower.c source code:
// Example code which uses the 32767KHz
// Crystal to implement a 1-second event
// handler
//
// Atmega Low power operation example
// Using a 32768 Khz crystal on timer 2 and full power down mode
// to implement a 1-second event handler
//
// This code is written for the GCC compiler
// Example for the GertDuino Atmega 48PA device
// (This program will NOT run on the 328!)
// This code is freeware
23 | Page
//
#include <avr/interrupt.h>
#include <avr/sleep.h>
volatile unsigned long count_seconds;
main()
{
// set PB0 as output
DDRB = 0xFE;
// Set-up 32 KHz oscillator
TIMSK2 = 0x00; // No interrupts
ASSR = 0x20; // async run from xtal
TCNT2 = 0; // clear counter
TCCR2B = 0x05; // prescale 5=/128
// Wait for all 'busy' bits to be clear
// That happens on the first timer overflow
// which can take 8 seconds if you have a max pre-scaler!!
while (ASSR&0x07) ;
TIMSK2 = 0x01; // overflow IRQ enable
count_seconds = 0; // clear seconds counter
sei(); //set the Global Interrupt Enable Bit
while (1)
{
SMCR = 0x7; // Go into lowest power sleep mode
asm("sleep");
asm("nop");
// Interrupt woke us up
// If we get here the interrupt routine has already been called
// Toggle LED on port B0 using LS timer bit
PORTB = count_seconds & 0x01;
}
} // main
//
// Timer 2 overflow
// if we set timer2 up correctly this routine is called every second
//
ISR(TIMER2_OVF_vect)
{ count_seconds++; // all we do here is count seconds elapsed
}
24 | Page
Makefile:
# Makefile:
# Make the GertDuino m48p firmware.
#
# Copyright (c) 2013 Gordon Henderson <projects@drogon.net>
###########################################################################
# This file is part of gertduino-m328:
#Software to run on the Atmega328p processor on the Gerduino board
#Can be used for the Atmega328p processor on the GERTBOARD as well
# This is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this. If not, see <http://www.gnu.org/licenses/>.
###########################################################################
TARGET=low_power
MCU=atmega48p
FREWQ=1000000
# Debug
#DEBUG = -gstabs
# C flags
CC = avr-gcc
#CFLAGS = $(DEBUG) -O3 -Wall -std=gnu99 -mmcu=$(MCU) -
DF_CPU=$(FREWQ) $(INCLUDE)
CFLAGS = $(DEBUG) -O2 -mcall-prologues -Wall -std=gnu99 -mmcu=$(MCU) -
DF_CPU=$(FREWQ) $(INCLUDE)
LD = avr-gcc
#LDFLAGS2=-Wl,-uvfprintf -lprintf_flt
LDFLAGS = -mmcu=$(MCU) $(DEBUG) $(LIBLOC) $(LDFLAGS2)
#LIBS = -ldross -lm
SRC = $(TARGET).c
OBJ = $(SRC:.c=.o)
all: $(TARGET).hex
$(TARGET).hex: $(TARGET).elf
@echo [hex] $<
@avr-objcopy -j .text -j .data -O ihex $(TARGET).elf $(TARGET).hex
$(TARGET).elf: $(OBJ)
@echo [Link] $<
@$(LD) -o $@ $(OBJ) $(LDFLAGS) $(LIBS)
@avr-size $(TARGET).elf
# Generate .lst file rule
%.lst : %.o
@echo [lst] $<
@avr-objdump -h -S $<> $@
25 | Page
.c.o:
@echo [CC] $<
@$(CC) -c $(CFLAGS) $< -o $@
.PHONEY: clean
clean:
rm -f *.o *.elf *.hex *.lst Makefile.bak *~
Program 48
#!/bin/bash
# script to program 48pa device using AVRDUDE and a hex file
if [ "$1" == "" ]; then
echo Missing argument
exit 1;
fi
# if ends in .hex use full argument
# otherwise add the .hex
ext=${1:${#1}-4}
if [ "$ext" == ".hex" ]; then
/usr/bin/avrdude -c gpio -p m48p $1 -Uflash:w:$1
else
/usr/bin/avrdude -c gpio -p m48p $1.hex -Uflash:w:$1.hex
fi
Save the above code in a file called "program_48" and then run "chmod 777 program_48". Use
./ prog ram_48 <hex file > to program the Atmega 48 device.
9 Control Arduino Reset
The Raspberry-Pi GPIO 8 pin controls the Arduino reset pin when the jumpers are in place.
When starting the pin is LOW and thus the Arduino chip is held in reset. To control the reset
(gpio-8 pin ) you can use the scripts shown below.
Don’t forget to change the mode of the text file to executable format: (chmod 777 reset_off).
Depending on your path you may have to call the script starting with a <dot><slash>:
./reset_off”.
Alternative copy the scripts to /usr/bin: “sudo cp reset_off /usr/bin”. I f you want the Raspberry
Pi to always execute the script at boot up you have to edit the /etc/rc.local file. Make sure that
you have the full path in there. Thus if you have installed the script in /usr/bin you have to add
the following line to /etc/rc.local:
/usr/bin/reset_off
26 | Page
reset_off
The following script will release the Arduino reset and thus make that the Arduino chip runs. It
only w o rk s if t he GertDuino is plug ged in to t he Raspberry Pi and the reset jumper is in place.
#!/usr/bin/sudo bash
# Set GPIO pin 8, high releasing Arduino reset
sudo echo "8" >/sys/class/gpio/export
sudo echo "out" >/sys/class/gpio/gpio8/direction
sudo echo "1" >/sys/class/gpio/gpio8/value
sudo echo "8" >/sys/class/gpio/unexport
reset_on
The following script will assert the Arduino reset and thus make that the Arduino chip stops, is
held in reset. It only works if the GertDuino is plugged in to the Raspberry Pi and the reset
jumper is in place.
#!/usr/bin/sudo bash
# Set GPIO pin 8, low activating Arduino reset
sudo echo "8" >/sys/class/gpio/export
sudo echo "out" >/sys/class/gpio/gpio8/direction
sudo echo "0" >/sys/class/gpio/gpio8/value
sudo echo "8" >/sys/class/gpio/unexport
10 Appe ndix A : GertDuino Schematic
5
5
4
4
3
3
2
2
1
1
D D
C C
B B
A A
Tx
Rx
R is on Pi
GRM31CR60J476M
R is on Pi
MOSI
MISO
SCLK
[wurth]
Do Not Fit
GPIO17
GPIO1/3
GPIO7
GPIO9
GPIO23
GPIO4
GPIO0/2
GPIO10
GPIO24
GPIO14
GPIO22
GPIO25
GPIO15
GPIO11
GPIO21
GPIO8
GPIO18
MISO
MOSI
DBG
SCK
MOSI
MISO
GPIO14 pi_tx
pi_rx
GPIO0/2 SDA
SCLGPIO1/3
DBGGPIO8
SCK
GPIO11
GPIO9
GPIO10
pi_tx
pi_rx
GPIO15
EX_MISO
MC_MISO
SER_RX
SER_TX
MC_SCK
MC_MOSI
MC_DBG/RESETn
EX_SCK
EX_MOSI
EX_DBG/RESETn
SDA
SCL
PD1
PD0
Vin
3V3_RASP 5V
3V3_RASP 5V
3V3_RASP 5V
5V
5V LOC3V3
5V
Title
Size Document Number Rev
Date: Sheet of
- 5.1
Gertduino Pi & Power
A4
1 4Saturday, September 28, 2013
Title
Size Document Number Rev
Date: Sheet of
- 5.1
Gertduino Pi & Power
A4
1 4Saturday, September 28, 2013
Title
Size Document Number Rev
Date: Sheet of
- 5.1
Gertduino Pi & Power
A4
1 4Saturday, September 28, 2013
U4
APL5320-33AI-TRG
VOUT 2
VIN
3
GND
1
C20
330nF_0603
C24
100nF_0603
D23
BAS70-06/SOT23
3
2
1
D21
B330A-13-F
J1
CONNECTOR_13X2
1 2
3 4
5 6
7 8
9 10
11 12
13 14
15 16
17 18
19 20
21 22
23 24
25 26
R8
2K7_0603
C19
2n2F_0603
R31
8K25_0603
C23
100nF_0603
+
C15
RGA221M1VBK-1013G
C18
22pF_0603
L4
FB_1206 C16
22uF_1210_20V
C27
1nF_0603
C22
22uF_1206
R21 56K_0603
R16
22K_0603
C17
330nF_0603
R13
2K7_0603
R20
22K_0603
C21
100nF_0603
J17
HEADER 7X2
2
4
6
8
10
12
14
1
3
5
7
9
11
13
RN3
3K3X4_TC164
1
2
3
45
6
7
8
RN4
5K6X4_TC164
1
2
3
4 5
6
7
8
R28
12K7-0603
D2TS4148RY
L1 6u8H
R30
33K2_0603
J13 HEADER 6X1
1
2
3
4
5
6
R10
2K7_0603
R9 1K5_0603
Q1 2N7002
3
1
2
R29
15K8_0603
C25
1nF_0603
U6
SC4525C
Vin
2
SS/EN
5
COMP
6
Boost 8
Switch 1
FB 7
GND
4
RSET
3
PAD
9
J11
CON_PWR3
1
2
3
D20
B330A-13-F
1 2
Q2 2N7002
3
1
2
R15 12K_0603
5
5
4
4
3
3
2
2
1
1
D D
C C
B B
A A
PD0
XTAL_IN
PD2
PD6
PD3
PB0
PC0
PB1
PD1
XTAL_IN
PC1
PC3
PC2
SS
PD7
PD4
PD5
PC4
PC5
AREF
AREF
MC_SCK
MC_MISO
MC_MOSI
SS
PB0
PB1
PC4
PC3
PC2
PC5
PC0
PC1
PD7
PD5
PD6
PD3
PD4
PD2
PD1
PD0
MC_SCK
PB1
SS
PD3
PD5
PD6
PC2
PC3
MC_DBG/RESETn
Vin
PC4
PC5
MC_MOSI
MC_SCK
MC_DBG/RESETn
PD1
PD0
LED0
LED1
LED2
LED3
LED4
LED5
BUT1
BUT0
MC_MISO
Vin
5V
5VLOC3V3
Title
Size Document Number Rev
Date: Sheet of
<Doc> 5.1
Gertduino Arduino
A4
2 4Saturday, September 28, 2013
Title
Size Document Number Rev
Date: Sheet of
<Doc> 5.1
Gertduino Arduino
A4
2 4Saturday, September 28, 2013
Title
Size Document Number Rev
Date: Sheet of
<Doc> 5.1
Gertduino Arduino
A4
2 4Saturday, September 28, 2013
D1
TS4148RY
C2
100nF_0603
C14
12pF_0402
J6
CONNECTOR_6X1
1
2
3
4
5
6
Y1
16MHz
C3
4u7F_0805
R17
22K_0603
J8
HEADER 1X1
1
J14
CONNECTOR_10X1
1
2
3
4
5
6
7
8
9
10
S1
1 3
2 4
C4
100nF_0603
J9 CONNECTOR_8X1
1
2
3
4
5
6
7
8
J7
CONNECTOR_8X1
1
2
3
4
5
6
7
8
C1
100nF_0603
C26
12pF_0402
U1
ATmega328P
PC6/Reset_n
1
PD0/RXD
2
PD1/TXD
3
PD2/INT0
4
PD4/XCK/T0
6
VCC 7
PB6/XTAL1
9
GND
8
PB7/XTAL2
10
PD5/OC0B/T1
11
PD6/OC0A/AIN0
12
PD7/AIN1
13
PB0/CLK0/ICP1
14
GND
22 AVCC 20
AREF 21
OC1A/PB1 15
SS_n/OC1B/PB2 16
MOSI/OC2A/PB3 17
MISO/OC2A/PB4 18
SCK/PB5 19
ADC0/PC0 23
ADC1/PC1 24
ADC2/PC2 25
ADC3/PC3 26
ADC4/SDA/PC4 27
ADC5/SCL/PC5 28
PD3/INT1/OC2B
5
5
5
4
4
3
3
2
2
1
1
D D
C C
B B
A A
12.5pF
ESR=50K XTAL1=18pF
XTAL2=8pF
SDA
EXB2
EXB1
EXD6
EXD5
EXC3
EXC2
EXC1
EXC0EXD4
IRDA
EXD7
EXB0
EXD1
EXD0
EXB1
EXD7
EXD6
EXD5
EXB0
EXB2
EXD4
IRDA
EXD1
EXD0
EXC3
EXC0
EXC1
EXC2
EX_MISO
EX_SCK
EX_MOSI
EX_DBG/RESETn
SDA
SCL
EX_SCK
EX_MOSI
EX_MISO
5V
VEXT
5V
5V
VEXT
5V
Title
Size Document Number Rev
Date: Sheet of
<Doc> 5.1
Gertduino Atmega
A4
3 4Saturday, September 28, 2013
Title
Size Document Number Rev
Date: Sheet of
<Doc> 5.1
Gertduino Atmega
A4
3 4Saturday, September 28, 2013
Title
Size Document Number Rev
Date: Sheet of
<Doc> 5.1
Gertduino Atmega
A4
3 4Saturday, September 28, 2013
C9
100nF_0603
J10
HEADER 10X2
1
3
5
7
9
11
13
15
17
19
2
4
6
8
10
12
14
16
18
20
C12
10pF_0402
U9
TSOP38238
Out 1
Gnd
2Vcc 3
C11
0pF_0402_DNF
C10
100nF_0603
X1
XTAL_SMD
12
3 4
C28
100nF_0603_DNF
C8
4u7F_0805
C7
100nF_0603
D4
TS4148RY
D25
BAT54C_SOT23
1
3
2
R18
22K_0603
B1 BAT_HOLD_CR1025
VCC 2
VCC 3
1
GND
R19
22K_0603
R40 330_0603
U10
ATMEGAx48PA_28MLF
PC6/Reset_n
25
PD0/RXD
26
PD1/TXD
27
PD2/INT0
28
PD4/XCK/T0
2
VCC 3
PB6/XTAL1
5
GND
4
PB7/XTAL2
6
PD5/OC0B/T1
7
PD6/OC0A/AIN0
8
PD7/AIN1
9
PB0/CLK0/ICP1
10
GND
18 AVCC 16
AREF 17
OC1A/PB1 11
SS_n/OC1B/PB2 12
MOSI/OC2A/PB3 13
MISO/OC2A/PB4 14
SCK/PB5 15
ADC0/PC0 19
ADC1/PC1 20
ADC2/PC2 21
ADC3/PC3 22
ADC4/SDA/PC4 23
ADC5/SCL/PC5 24
PD3/INT1/OC2B
1
PAD
29
5
5
4
4
3
3
2
2
1
1
D D
C C
B B
A A
74HC14D power connection.
SER_RX
SER_TX
LED0
LED1
LED2
LED3
LED4
LED5
BUT0
BUT1
SER_TX
SER_RX
5V
VCC5V 5V
Title
Size Document Number Rev
Date: Sheet of
<Doc> 5.1
Gertduino I/O
A4
4 4Saturday, September 28, 2013
Title
Size Document Number Rev
Date: Sheet of
<Doc> 5.1
Gertduino I/O
A4
4 4Saturday, September 28, 2013
Title
Size Document Number Rev
Date: Sheet of
<Doc> 5.1
Gertduino I/O
A4
4 4Saturday, September 28, 2013
D10
LED
C42
100nF_0603
U3D
M74HC14TTR
9 8
J12
HEADER 3X1
1
2
3
C41
100nF_0603
U3F
M74HC14TTR
13 12
RN1
1KX4_TC164
1
2
3
45
6
7
8
U3A
M74HC14TTR
1 2
R39
22K_0603
RN2
1KX4_TC164
1
2
3
45
6
7
8
C13
100nF_0603
C39
100nF_0603
D7
LED
C38
100nF_0603
D6
LED
S3
1 3
2 4
U3C
M74HC14TTR
5 6
U3B
M74HC14TTR
3 4
D8
LED
C40
100nF_0603
S2
1 3
2 4
U8
MAX3232CUE
C1+
1
C1-
3
C2+
4
C2-
5
V+ 2
V- 6
TX1_O 14
TX2_O 7
RX1_I 13
RX2_I 8
TX1_I
11
TX2_I
10
RX1_O
12
RX2_O
9
GND
15 VCC 16
U3E
M74HC14TTR
11 10
D9
LED
D5
LED