monfold.sh: Use tabs instead of mixed tabs/spaces for indentation
[blog.git] / posts / AVR.mdwn
1 I've been wanting to get into microcontroller programming for a while
2 now, and last week I broke down and ordered components for a
3 [breadboard Arduino][breadboard-Arduino] from [Mouser].  There's a
4 fair amount of buzz about the [Arduino][] platform, but I find the
5 whole [sketch infrastucture][sketch] confusing.  I'm a big fan of
6 command line tools in general, so the whole IDE thing was a bit of a
7 turn off.
8
9 Because the [ATMega328][] doesn't have a USB controller, I also bought
10 a [Teensy 2.0][Teensy] from [PJRC][].  The Teensy is just an
11 [ATMega32u4][] on a board with supporting hardware (clock, reset
12 switch, LED, etc).  I've packed the Teensy programmer and HID listener
13 in my [[Gentoo overlay]], to make it easier to install them and stay
14 up to date.
15
16 Arduinos (and a number of similar projects) are based on [AVR][]
17 microcontrollers like the ATMegas.  Writing code for an AVR processor
18 is the similar to writing code for any other processor.  [GCC][] will
19 cross-compile your code once you've setup a cross-compiling toolchain.
20 There's a good intro to the whole embedded approach in the [Gentoo
21 Embedded Handbook][handbook].
22
23 For all the AVR-specific features you can use [AVR-libc][], an open
24 source C library for AVR processors.  It's hard to imagine doing
25 anything interesting without using this library, so you should at
26 least skim through the manual.  They also have a few interesting
27 [demos][] to get you going.
28
29 AVR-libc sorts chip-support code into AVR architecture subdirectories.
30 For example, object code specific to my [ATMega32u4][] is installed at
31 `/usr/avr/lib/avr5/crtm32u4.o`.  `avr5` is the [AVR architecture
32 version][arch] of this chip.
33
34 Crossdev
35 --------
36
37 Since you will probably not want to build a version of GCC that runs
38 on your AVR chip, you'll be building a cross comiling toolchain.  The
39 toolchain will allow you to use your development box to compile
40 programs for your AVR chip.  On Gentoo, the recommended approach is to
41 use [crossdev][] to build the toolchain (although [crossdev's AVR
42 support can be flaky][bug147155]).  They suggest you install it in a
43 [stage3 chroot to protect your native toolchain][chroot], but I think
44 it's easier to just make [[btrfs]] snapshots of my hard drive before
45 doing something crazy.  I didn't have any trouble skipping the chroot
46 on my sytem, but your mileage may vary.
47
48     # emerge -av crossdev
49
50 Because it has per-arch libraries (like `avr5`), AVR-libc needs to be
51 built with [multilib][] support.  If you (like me) have avoided
52 multilib like the plague so far, you'll need to patch crossdev to turn
53 on multilib for the AVR tools.  Do this by applying Jess'
54 [patch][multilib-patch] from [bug 377039][bug377039].
55
56     # wget -O crossdev-avr-multilib.patch 'https://bugs.gentoo.org/attachment.cgi?id=304037'
57     # patch /usr/bin/crossdev < crossdev-avr-multilib.patch
58
59 If you're using a profile where multilib is masked
60 (e.g. `default/linux/x86/10.0/desktop`) you should use Niklas'
61 [extended version of the patch][niklas-patch] from the duplicate [bug
62 378387][bug378387].
63
64 Despite claiming to use the last overlay in `PORTDIR_OVERLAY`,
65 crossdev currently [uses the first][bug428420], so if you use
66 [layman][] to manage your overlays (like [[mine|Gentoo_overlay]]),
67 you'll want to tweak your `make.conf` to look like:
68
69     source /var/lib/layman/make.conf
70     PORTDIR_OVERLAY="/usr/local/portage ${PORTDIR_OVERLAY}"
71
72 Now you can install your toolchain following the [Crossdev
73 wiki][crossdev-avr].  First install a minimal GCC (stage 1) using
74
75     # USE="-cxx -openmp" crossdev --binutils 9999 -s1 --without-headers --target avr
76
77 Then install a full featured GCC (stage 4) using
78
79     # USE="cxx -nocxx" crossdev --binutils 9999 -s4 --target avr
80
81 I use `binutils-9999` to install live from the [git mirror][git],
82 which avoids [a segfault bug in binutils 2.22][bug12161].
83
84 After the install, I was getting bit by [bug 147155][bug147155]:
85
86     cannot open linker script file ldscripts/avr5.x
87
88 Which I work around with:
89
90     # ln -s /usr/x86_64-pc-linux-gnu/avr/lib/ldscripts /usr/avr/lib/ldscripts
91
92 Now you're ready.  Go forth and build!
93
94 Cross compiler construction
95 ---------------------------
96
97 Why do several stages of GCC need to be built anyway?  From `crossdev
98 --help`, here are the stages:
99
100 0. Build just binutils
101 1. Also build a bare C compiler (no C library/C++/shared GCC libs/C++
102    exceptions/etc…)
103 2. Also build kernel headers
104 3. Also build the C library
105 4. Also build a full compiler
106
107
108 [breadboard-Arduino]: http://arduino.cc/en/Main/Standalone
109 [Mouser]: http://www.mouser.com/
110 [Arduino]: http://arduino.cc/
111 [sketch]: http://arduino.cc/en/Guide/Environment
112 [ATMega328]: http://www.atmel.com/devices/atmega328.aspx
113 [Teensy]: http://pjrc.com/teensy/
114 [PJRC]: http://pjrc.com/
115 [ATMega32u4]: http://www.atmel.com/devices/atmega32u4.aspx
116 [AVR]: http://en.wikipedia.org/wiki/Atmel_AVR
117 [GCC]: http://gcc.gnu.org/
118 [handbook]: http://www.gentoo.org/proj/en/base/embedded/handbook/
119 [AVR-libc]: http://www.nongnu.org/avr-libc/
120 [demos]: http://www.nongnu.org/avr-libc/user-manual/group__demos.html
121 [arch]: http://www.nongnu.org/avr-libc/user-manual/using_tools.html#using_avr_gcc_mach_opt
122 [crossdev]: http://en.gentoo-wiki.com/wiki/Crossdev
123 [bug147155]: https://bugs.gentoo.org/show_bug.cgi?id=147155
124 [chroot]: http://www.gentoo.org/proj/en/base/embedded/handbook/?part=1&chap=2#doc_chap1
125 [multilib]: http://www.gentoo.org/doc/en/gentoo-amd64-faq.xml#multilib
126 [multilib-patch]: https://bugs.gentoo.org/attachment.cgi?id=304037
127 [bug377039]: https://bugs.gentoo.org/show_bug.cgi?id=377039
128 [bug428420]: https://bugs.gentoo.org/show_bug.cgi?id=428420
129 [layman]: http://layman.sourceforge.net/
130 [crossdev-avr]: http://en.gentoo-wiki.com/wiki/Crossdev#AVR_Architecture
131 [git]: http://sourceware.org/git/?p=binutils.git
132 [niklas-patch]: https://bugs.gentoo.org/attachment.cgi?id=285901
133 [bug378387]: https://bugs.gentoo.org/show_bug.cgi?id=378387
134 [bug12161]: http://sourceware.org/bugzilla/show_bug.cgi?id=12161
135 [link]: http://en.gentoo-wiki.com/wiki/Paludis/AVR_Crossdev#Post-install
136 [bug147155]: https://bugs.gentoo.org/show_bug.cgi?id=147155
137
138 [[!tag tags/C]]
139 [[!tag tags/hardware]]
140 [[!tag tags/linux]]
141 [[!tag tags/programming]]