├── .gitignore ├── Makefile ├── NOTES.asciidoc ├── README.md ├── doc ├── a-look-at-the-raspberry-pi.asciidoc ├── blink-button.asciidoc ├── blink-button.fzz ├── blink-button.svg ├── blink-pin-type.asciidoc ├── blink.asciidoc ├── blink.fzz ├── blink.svg ├── book.asciidoc ├── box-ownership-err.compile-output ├── compile-the-compiler.asciidoc ├── dev-environment.asciidoc ├── export-gpio-err.compile-output ├── file-ownership-err.compile-output ├── hello-world.asciidoc ├── introduction.asciidoc ├── macros.asciidoc ├── parts-bin.asciidoc ├── pi-top.asciidoc ├── pi-top.jpg ├── pi.fzz ├── pi.svg └── rust-whats-the-big-idea.asciidoc ├── originals ├── pi-photo-orig.jpg └── pi-photo.xcf ├── src ├── blink-button-epoll.rs ├── blink-button-tasks.rs ├── blink-button.rs ├── blink.rs ├── box-ownership-err.rs ├── box-ownership-ok.rs ├── eventfd-example.rs ├── export-gpio-err.rs ├── file-ownership-err.rs ├── file-ownership-ok.rs ├── hello.rs ├── i2c-example.rs ├── pi │ ├── epoll.rs │ ├── eventfd.rs │ ├── gpio.rs │ ├── i2c.rs │ ├── mod.rs │ └── unixio.rs ├── raw-blink.rs └── signals.rs └── tools └── gpio-cleanup /.gitignore: -------------------------------------------------------------------------------- 1 | out/ 2 | *~ 3 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # define showerrs on the command-line to see what errors are generated by 2 | # the xxx-err.rs programs 3 | 4 | pi?=192.168.1.35 5 | 6 | rust_exe_src:=$(wildcard src/*.rs) 7 | rust_lib_src:=$(shell find src/ -path 'src/*/*.rs') 8 | rust_src:=$(rust_exe_src) $(rust_lib_src) 9 | 10 | rust_exes:=$(patsubst src/%.rs,out/%,$(filter-out %-err.rs,$(rust_exe_src))) 11 | rust_compile_errors:=$(patsubst src/%-err.rs,out/compile-output/%-err.compile-output,$(rust_exe_src)) 12 | 13 | book_src:=$(wildcard doc/*.asciidoc) 14 | book_images:=$(wildcard doc/*.svg doc/*.jpg doc/*.png) 15 | 16 | version:=$(shell git describe --tags --always --dirty=-local --match='v*' | sed -e 's/^v//') 17 | rust_version:=$(shell rustc --version | cut -c 7-) 18 | 19 | asciidoc_icondir=/usr/share/asciidoc/icons 20 | asciidoc_icons:=$(shell find $(asciidoc_icondir) -type f -name '*.*') 21 | 22 | linker=../tools/arm-bcm2708/arm-bcm2708hardfp-linux-gnueabi/bin/arm-bcm2708hardfp-linux-gnueabi-g++ 23 | rustflags=-L . --target arm-unknown-linux-gnueabihf -C linker=$(linker) 24 | 25 | all: pdf exes 26 | exes: $(rust_exes) 27 | pdf: out/pdf/book.pdf 28 | 29 | out/pdf/book.pdf: out/docbook/book.xml 30 | @mkdir -p $(dir $@) 31 | dblatex -o $@ --fig-path=doc -P latex.encoding=utf8 $< 32 | 33 | out/docbook/book.xml: $(book_src) $(rust_src) $(rust_compile_errors) 34 | @mkdir -p $(dir $@) 35 | asciidoc \ 36 | -a icons \ 37 | -a version="$(version)" \ 38 | -a rust_version="$(rust_version)" \ 39 | -b docbook45 \ 40 | -o $@ doc/book.asciidoc 41 | 42 | out/%: src/%.rs $(rust_lib_src) 43 | @mkdir -p $(dir $@) 44 | rustc $(rustflags) -o $@ $< 45 | 46 | out/compile-output/%.compile-output: src/%.rs 47 | @mkdir -p $(dir $@) 48 | ifdef showerrs 49 | -rustc $(rustflags) -o $(dir $@)/$* $< 2>&1 | tee $@ 50 | else 51 | -rustc $(rustflags) -o $(dir $@)/$* $< > $@ 2>&1 52 | endif 53 | diff $@ doc/$*.compile-output 54 | 55 | 56 | deployed: $(rust_exes) 57 | rsync $^ $(pi): 58 | 59 | clean: 60 | rm -rf out/ 61 | 62 | again: clean deployed 63 | 64 | .PHONY: all pdf exes deployed clean again tmp 65 | -------------------------------------------------------------------------------- /NOTES.asciidoc: -------------------------------------------------------------------------------- 1 | 2 | * Need to explicitly set the state of the GPIO pin on creation of the Pin struct -- unexporting does not reset it. 3 | 4 | * To enable i2c you need to remove (or comment out) the line 'blacklist i2c-bcm2708' from /etc/modprobe.d/raspi-blacklist.conf, and add the lines 'i2c-bcm2708' and 'i2c-dev' to /etc/modules. Then reboot the Pi. 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Hardware Control on the Raspberry Pi with the Rust Language 2 | =========================================================== 3 | 4 | A work in progress... 5 | 6 | Current plan... 7 | 8 | 9 | 1. Getting Started: intro to the Raspberry Pi, building a cross-compiler and setting up the development environment, parts for hardware tinkering. Rust learning: program entry point, the prelude 10 | 2. Getting Physical: simple hardware control blinking LEDs and reacting to button presses via GPIO. Rust learning: I/O, error handling, enums, structs, traits, splitting a program into cooperating tasks, separating I/O from coordination 11 | 3. Getting Fast: map GPIO device registers into memory for direct access, bit-bang a simple radio protocol. Rust learning: memory mapping, unsafe blocks, use Rust's type system to ensure safe access. 12 | 4. Getting Connected: using the I2C bus to read from analogue-to-digital converters. Rust learning: using C functions and data structures. Native vs green runtimes. 13 | 5. Getting Mobile: motor control or control of an RC vehicle. 14 | 15 | This plan may change... 16 | -------------------------------------------------------------------------------- /doc/a-look-at-the-raspberry-pi.asciidoc: -------------------------------------------------------------------------------- 1 | == A Look At The Raspberry Pi 2 | 3 | === The Raspberry Pi 4 | 5 | The Raspberry Pi is a credit-card-sized single-board computer developed 6 | by the Raspberry Pi Foundation to promote the teaching of basic computer 7 | science. It has become very popular for teaching children in schools 8 | and code clubs and also among adult hobbyists, makers and hardware hackers. 9 | 10 | include::pi-top.asciidoc[] 11 | 12 | The Pi is built around a 32-bit ARMv6 system-on-a-chip (SoC) 13 | footnote:[the Broadcom BCM2835], has 512MB of RAM, 3D accelerated 14 | graphics, a USB port for keyboard and mouse, and can output HDMI video 15 | to a TV or monitor. It uses an SD card instead of a 16 | hard-drive. Although small, the Raspberry Pi is powerful enough to run 17 | a desktop operating system, full motion video at 1080p resolution, and 18 | 3D games like Minecraft. 19 | 20 | One way in which the Raspberry Pi differs from consumer PCs is that it 21 | does not come with a pre-installed operating system. You have to 22 | download an operating system image and copy it onto an SD card so that 23 | you can boot up the Pi and start using it. You have a choice of 24 | operating systems and, for Linux, a choice of distributions. The 25 | Raspberry Pi Foundation recommends Raspbian, a variant of Debian 26 | Linux, and most software for the Pi is written for 27 | Raspbian. Therefore, I'm using Raspbian in this book. If you're used 28 | to a Debian, or a Debian-based Linux distribution like Ubuntu, 29 | Raspbian will be very familiar. If you prefer a different Linux 30 | distribution, I assume you know enough translate any Raspbian-specific 31 | commands to your preferred environment. The Rust programs should 32 | behave the same on any Linux distribution. 33 | 34 | === Interfaces 35 | 36 | As well as the usual USB, Ethernet and video ports, the Raspberry Pi 37 | has a 24-pin header at its top, left-hand corner that exposes useful 38 | interfaces for controlling hardware: a UART serial interface, eight 39 | GPIO pins for general purpose digital IO, two I2C buses and an SPI 40 | bus, that can be used to control a variety of hardware devices, 41 | pulse-width modulation output, as well as 3v3 and 5v0 power and a 42 | couple of ground pins. 43 | 44 | Unlike the Arduino and many other single-board computers, the 45 | Raspberry Pi has a _male_ header. This means that to connect the 46 | header to a breadboard, you have to make sure you buy female-to-male 47 | jumpers, which have a socket an one end to connect to a header pin and 48 | a pin at the other to plug into a hole on the breadboard. These are 49 | less common than male-to-male jumpers, which have a pin at both ends. 50 | 51 | The other difficulty you'll encounter when you start connecting your 52 | Raspberry Pi to your circuits is that the header pins are not 53 | labelled. In fact, the function of the pins can be remapped by 54 | software. However, for this book we will use the default pin 55 | mappings. The circuit diagrams in this book clearly identify the 56 | header pins of the Raspberry Pi, as shown in <>, and the 57 | text will refer to the pins by the same names. 58 | 59 | [[pi-fritzing]] 60 | .Raspberry Pi as displayed in a circuit diagrams 61 | image::pi.svg[] 62 | 63 | Because of these difficulties, some people like to use a "breakout 64 | board" to connect their Pi to hardware. A breakout board plugs into 65 | the header and routes the pins to female headers that are organised in 66 | logical groups and clearly labelled. They only cost a few pounds and 67 | can be very convenient. 68 | 69 | An ingenious, cheap alternative to a breakout board is to print the 70 | header pin identifiers on a small piece of paper that you push over 71 | the pins, so that the pins stick through the paper next to their 72 | identifiers. You can download a printable PDF by Simon Monk from 73 | http://www.doctormonk.com/2013/02/raspberry-pi-and-breadboard-raspberry.html. 74 | 75 | === Protecting Your Pi 76 | 77 | TODO: no protection on the header, be careful not to burn out the Pi, 78 | some breakout boards protect the pi with voltage regulators and diodes, 79 | beware of static electricity. 80 | 81 | === Compiling Programs for the Raspberry Pi 82 | 83 | Although the Raspberry Pi can be used as a Linux PC with a graphical 84 | desktop interface, I find it a bit slow for software development 85 | compared to a laptop or desktop PC. When using my Pi to control 86 | hardware, I prefer to write software on my laptop, compile it for the 87 | Pi with a cross-compiler footnote:[a cross-compiler runs on one 88 | architecture, such as my laptop running Ubuntu on an Intel x86, and 89 | outputs machine code for another architecture, such as the Raspberry Pi 90 | running Raspbian on an ARM], and deploy the compiled program onto the 91 | Pi when I want to test it against real hardware. 92 | 93 | So, there are two things to sort out before writing Rust programs for 94 | the Pi: 95 | 96 | 1. <> 97 | 2. <> 98 | -------------------------------------------------------------------------------- /doc/blink-button.asciidoc: -------------------------------------------------------------------------------- 1 | == Clicky Buttons 2 | 3 | Adding GPIO input to the blink program. 4 | 5 | 6 | === The Circuit 7 | 8 | .A push-button added to the blink circuit 9 | image::blink-button.svg[] 10 | 11 | 12 | === The Code 13 | 14 | 15 | --------------------------------------------- 16 | include::../src/blink-button.rs[] 17 | --------------------------------------------- 18 | -------------------------------------------------------------------------------- /doc/blink-button.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/npryce/rusty-pi/3aa638c06c6b4dd519c540b583a94d6c419ad12f/doc/blink-button.fzz -------------------------------------------------------------------------------- /doc/blink-pin-type.asciidoc: -------------------------------------------------------------------------------- 1 | == Defining a GPIO Pin Type 2 | 3 | Abstracting the I/O for GPIO into a convenient module to clarify our code. Make the Pin type usable from other programs. 4 | 5 | Introduces structs and traits, destructors, defining new modules and organising a program into multiple files. 6 | -------------------------------------------------------------------------------- /doc/blink.asciidoc: -------------------------------------------------------------------------------- 1 | == Blinking Lights 2 | 3 | Blinking an LED via GPIO is the Hello World of physical computing -- a 4 | simple program that will cause some visible effect in the connected 5 | hardware. It will also demonstrate some more Rust language concepts, 6 | including modules, control flow, ownership and lifetimes, I/O and 7 | error handling. 8 | 9 | == What is GPIO? 10 | 11 | GPIO, which stands for general-purpose input/output, is the simplest 12 | digital I/O you can do with a computer. A GPIO pin can be controlled 13 | by software to either set or read the logical voltage level at the 14 | pin. A GPIO output pin can turn an LED or buzzer on and off, for 15 | example. An input pin can receive input from a button or sensor. 16 | 17 | Eight of the pins on the Raspberry Pi's header are dedicated to 18 | GPIO. The Raspbian operating system makes the GPIO pins available to 19 | user-space programs as files in the /sys filesystem. 20 | 21 | Getting to grips with GPIO programming can be pretty confusing at 22 | first because of the multitude of ways in which GPIO pins are 23 | identified in the Raspberry Pi documentation. The pins on the header 24 | connect to the pins of the system-on-a-chip (SoC) at the heart of the 25 | Raspberry Pi. Each SoC pin has a number. Twenty six of the SoC pins 26 | are routed to the header, but those twenty six pins do not have 27 | consecutive numbers. The Raspberry Pi documentation fives the header 28 | pins numbers 1 to 26 and also uses the names GP0 to GP7 for the eight 29 | header pins dedicated to GPIO, but neither the header pin number nor 30 | the GP0 to GP7 names has any relation to the number used by the SoC 31 | for that pin. 32 | 33 | The operating system's programming interface for GPIO addresses the 34 | pins _only_ by their SoC pin numbers. When programming GPIO, we can 35 | ignore the header pin number and the GP0 to GP7 names used for the 36 | GPIO pins. We only need to know the header pin number when connecting 37 | physical wires to physical pins. 38 | 39 | To avoid confusion this book only refers to GPIO pins by their SoC 40 | numbers and does not use the names GP0 to GP7. The circuit diagrams 41 | show how the SoC pin numbers relate to the physical header pins. 42 | 43 | TIP: If you buy a breakout board, choose one that labels the GPIO pins 44 | with their the SoC numbers, not GP0...GP7. 45 | 46 | Let's have a look at the circuit we need to build to blink an LED with 47 | GPIO. 48 | 49 | 50 | === The Circuit 51 | 52 | You will need: 53 | 54 | * a breadboard 55 | * one LED 56 | * one 220{ohm} resistor 57 | * three long female-to-male jumpers 58 | 59 | .An LED connected to GPIO pin 18 60 | image::blink.svg[] 61 | 62 | 63 | === Controlling the GPIO Pin from the Command Line 64 | 65 | The Raspbian operating system exposes each GPIO pin to user space 66 | processes as a directory of text files in the /sys filesystem. Because 67 | the pins are controlled by reading and writing text files, we can 68 | easily use GPIO from any language, even shell scripts. 69 | 70 | The GPIO pins are not initially visible in the /sys filesystem. To 71 | start using a pin, a program must "export" it to user space by writing 72 | the number of the pin it wants to use into the control file 73 | /sys/class/gpio/export. This requires root privileges. 74 | 75 | For example, the following shell command will export pin 18 to user 76 | space, using the sudo to run `tee` command with elevated privileges so 77 | that it can write to the /sys/class/gpio/export file. 78 | 79 | ---------------------------------------------- 80 | % echo 18 | sudo tee /sys/class/gpio/export 81 | 18 82 | ---------------------------------------------- 83 | 84 | If that succeeds, the kernel will make the pin's control files 85 | available in a subdirectory of /sys/class/gpio that is named after the 86 | pin number, in this case /sys/class/gpio/gpio18/. 87 | 88 | ---------------------------------------------- 89 | % ls /sys/class/gpio/gpio18/ 90 | active_low direction edge power subsystem uevent value 91 | ---------------------------------------------- 92 | 93 | The most commonly used files are direction and value. The `direction` 94 | file reports (when read) and controls (when written) whether the pin 95 | is used for input or output. Reading from the `value` file reports the 96 | voltage level at the pin. A high voltage is represented as the text 97 | "1" and a low voltage as "0". If the pin has been configured for 98 | output, writing to the value file will set the voltage level of the 99 | pin. 100 | 101 | With this knowledge we can blink the LED connected to pin 18 from the 102 | shell. 103 | 104 | First, set the pin to output mode: 105 | 106 | ---------------------------------------------- 107 | % echo out | sudo tee /sys/class/gpio/gpio18/direction 108 | out 109 | ---------------------------------------------- 110 | 111 | Then write 1 into the pin's value file. The LED should light up. 112 | 113 | ---------------------------------------------- 114 | % echo 1 | sudo tee /sys/class/gpio/gpio18/value 115 | 1 116 | ---------------------------------------------- 117 | 118 | Write 0 into the pin's value file to turn the LED off. 119 | 120 | ---------------------------------------------- 121 | % echo 0 | sudo tee /sys/class/gpio/gpio18/value 122 | 0 123 | ---------------------------------------------- 124 | 125 | Do that in a loop with sleep to blink the LED until you press 126 | Control-C. 127 | 128 | ---------------------------------------------- 129 | % while true 130 | > do 131 | > echo 1 | sudo tee /sys/class/gpio/gpio18/value 132 | > sleep 1 133 | > echo 0 | sudo tee /sys/class/gpio/gpio18/value 134 | > sleep 1 135 | > done 136 | 0 137 | 1 138 | 0 139 | 1 140 | ... 141 | ---------------------------------------------- 142 | 143 | When we've finished using the pin we should unexport it, which removes 144 | its control files from the filesystem. 145 | 146 | ---------------------------------------------- 147 | % echo 18 | sudo tee /sys/class/gpio/unexport 148 | 18 149 | ---------------------------------------------- 150 | 151 | .The Importance of Error Handling 152 | [TIP] 153 | ======================================================================= 154 | It's best to export a GPIO pin just before we want to use it and 155 | unexport the pin as soon as we've finished with it. This helps avoid 156 | interference between processes trying to use the same pin. When a pin 157 | has been exported to user space, further writes to the export file 158 | will fail. 159 | 160 | ---------------------------------- 161 | % echo 18 | sudo tee /sys/class/gpio/export 162 | 18 163 | tee: /sys/class/gpio/export: Device or resource busy 164 | % echo $? 165 | 1 166 | ---------------------------------- 167 | 168 | A process will not be able to disrupt ongoing GPIO, as long as it 169 | checks for I/O errors when exporting its pins. In shell scripts and C 170 | it's too easy to ignore I/O failures and allow erroneous behaviour to 171 | occur. Rust does not let us be so lazy, as we will see. 172 | ======================================================================= 173 | 174 | Now we understand the basics of GPIO in Rasbian, let's control the 175 | GPIO pi from Rust. We'll go step by step, building up the 176 | functionality and explaining Rust's features as we go. If you're 177 | impatient to see some blinking lights, feel free to jump ahead, 178 | compile <> and run it onto your Pi before 179 | coming back here to examine into the Rust features that are used in 180 | the program. 181 | 182 | === Export and Unexport the Pin 183 | 184 | TBD... 185 | 186 | === Set the Pin's Direction 187 | 188 | TBD... 189 | 190 | === Set the Voltage Level 191 | 192 | TBD... 193 | 194 | === Blink the LED 195 | 196 | TBD... 197 | 198 | === Putting it All Together 199 | 200 | [[raw-blink]] 201 | --------------------------------------------- 202 | include::../src/raw-blink.rs[] 203 | --------------------------------------------- 204 | -------------------------------------------------------------------------------- /doc/blink.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/npryce/rusty-pi/3aa638c06c6b4dd519c540b583a94d6c419ad12f/doc/blink.fzz -------------------------------------------------------------------------------- /doc/book.asciidoc: -------------------------------------------------------------------------------- 1 | Exploring Rust through Physical Computing with the Raspberry Pi 2 | =============================================================== 3 | Nat Pryce 4 | {version} 5 | :doctype: book 6 | :ascii-ids: 7 | 8 | include::macros.asciidoc[] 9 | 10 | include::introduction.asciidoc[] 11 | 12 | include::rust-whats-the-big-idea.asciidoc[] 13 | 14 | 15 | = Getting Started 16 | 17 | include::a-look-at-the-raspberry-pi.asciidoc[] 18 | 19 | include::compile-the-compiler.asciidoc[] 20 | 21 | include::dev-environment.asciidoc[] 22 | 23 | include::hello-world.asciidoc[] 24 | 25 | = Getting Physical 26 | 27 | include::blink.asciidoc[] 28 | 29 | include::blink-pin-type.asciidoc[] 30 | 31 | include::blink-button.asciidoc[] 32 | 33 | -------------------------------------------------------------------------------- /doc/box-ownership-err.compile-output: -------------------------------------------------------------------------------- 1 | src/box-ownership-err.rs:5:20: 5:24 error: use of moved value: `ibox` 2 | src/box-ownership-err.rs:5 println!("{}", ibox); 3 | ^~~~ 4 | note: in expansion of format_args! 5 | :2:23: 2:77 note: expansion site 6 | :1:1: 3:2 note: in expansion of println! 7 | src/box-ownership-err.rs:5:5: 5:26 note: expansion site 8 | src/box-ownership-err.rs:4:21: 4:25 note: `ibox` moved here because it has type `Box`, which is non-copyable (perhaps you meant to use clone()?) 9 | src/box-ownership-err.rs:4 use_boxed_value(ibox); 10 | ^~~~ 11 | error: aborting due to previous error 12 | -------------------------------------------------------------------------------- /doc/compile-the-compiler.asciidoc: -------------------------------------------------------------------------------- 1 | [[compile-cross-compiler]] 2 | == Compiling the Compiler 3 | 4 | 5 | The Rust compiler is not (yet?) distributed as a cross-compiler for 6 | the Raspberry Pi. However, it can be compiled as a cross-compiler 7 | from source code with the Raspberry Pi development tools. 8 | 9 | That sounds like hard work, but don't worry -- the Rust build process 10 | is completely automated. It does take a long time to run, but won't 11 | require any effort on your part. 12 | 13 | 14 | === Install Git 15 | 16 | You'll need Git to download the Raspberry Pi tools and the source code 17 | of the Rust compiler. Git is available in the package managers for most 18 | Linux distributions. For example, on Ubuntu or Debian you can install it 19 | with the command: 20 | 21 | [source,sh] 22 | ----------------------------------- 23 | % sudo apt-get install git 24 | ----------------------------------- 25 | 26 | If Git is not available in your package manager, you can download it 27 | from http://git-scm.com/download/. 28 | 29 | 30 | === Downloading the Raspberry Pi Tools 31 | 32 | When Git is installed, you can download the Raspberry Pi development 33 | tools. The following command downloads the tools into a directory 34 | named pi-tools in your home directory: 35 | 36 | [source,sh] 37 | --------------------------------------------------------------- 38 | % git clone https://github.com/raspberrypi/tools.git ~/pi-tools 39 | --------------------------------------------------------------- 40 | 41 | 42 | === Compiling the Rust Cross-Compiler 43 | 44 | Now, download the Rust compiler source code and jump into cloned directory: 45 | 46 | [source,sh] 47 | --------------------------------------------------------------- 48 | % git clone https://github.com/rust-lang/rust.git rust && cd $_ 49 | --------------------------------------------------------------- 50 | 51 | Add the Raspberry Pi tools to your shell's command search path: 52 | 53 | [source,sh] 54 | --------------------------------------------------------------- 55 | % export PATH=~/pi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin:$PATH 56 | --------------------------------------------------------------- 57 | 58 | Then build Rust as a cross-compiler. The following command will 59 | build the cross compiler and install it into a directory called 60 | pi-rust in your home directory. 61 | 62 | [source,sh] 63 | ------------------------------------------------------------------- 64 | % ./configure --target=arm-unknown-linux-gnueabihf --prefix=$HOME/pi-rust && make && make install 65 | ------------------------------------------------------------------- 66 | 67 | The build will take a long time to complete. When it's finished, you 68 | can test it by compiling a simple Rust program and running it on the 69 | Pi. But until then, you've got more than enough time to go have a cup 70 | of tea. Or two. 71 | 72 | 73 | === Testing the Cross-Compiler 74 | 75 | To test the cross-compiler and deployment of compiled programs onto 76 | the Raspberry Pi, we'll compile a traditional Hello World program. 77 | Here's Hello World in Rust: 78 | 79 | .Hello World 80 | ------------------------------------------------------------------- 81 | include::../src/hello.rs[] 82 | ------------------------------------------------------------------- 83 | 84 | Save this code into a file called hello.rs. 85 | 86 | To compile it for the Pi, make sure that the Raspberry Pi tools are 87 | on your path (as shown above), then run the command: 88 | 89 | [source,sh] 90 | ------------------------------------------------------------------- 91 | % ~/pi-rust/bin/rustc --target=arm-unknown-linux-gnueabihf -C linker=arm-linux-gnueabihf-g++ hello.rs 92 | ------------------------------------------------------------------- 93 | 94 | This will produce an executable program called `hello`. You won't be 95 | able to run it on your PC. You have to deploy it onto the Pi and run 96 | it there. 97 | 98 | We'll look at how to do that in <>. 99 | -------------------------------------------------------------------------------- /doc/dev-environment.asciidoc: -------------------------------------------------------------------------------- 1 | [[dev-environment]] 2 | == Deploying onto the Raspberry Pi 3 | 4 | === Deploying with Rsync and SSH 5 | 6 | TODO 7 | 8 | === Deploying with a Shared Folder 9 | 10 | If you’re running Linux on your PC you can mount files from your PC’s 11 | filesystem on the Pi or vice versa. You can then cross-compile 12 | programs on your PC and run them on the Pi without having to 13 | explicitly copy them across. 14 | 15 | If you mount a directory on the Pi into your PC’s filesystem, the 16 | files remain on the Pi when you unmount the directory from your PC, so 17 | you can edit programs on the Pi with your PC and leave them running on 18 | the Pi when you finish working. 19 | 20 | If you mount a directory on your PC into the Pi’s filesystem, you can 21 | continue working on the program on your PC even when you’ve unmounted 22 | it from the Pi – handy if you want to work on the move – and then 23 | remount it to run your program on the Pi. 24 | 25 | My favourite tool for doing this is sshfs. It uses ssh and sftp as the 26 | transport and so there’s no need to edit system-global configuration 27 | files, as you do to export NFS directories, and you don’t need to be 28 | root to export or mount a filesystem. 29 | 30 | You’ll need to install sshfs on your PC and your Raspebrry Pi. On each 31 | machine, install it with apt-get: 32 | 33 | % sudo apt-get install sshfs 34 | 35 | You need to add yourself to the fuse group to mount FUSE filesystems 36 | like sshfs. On each machine, run 37 | 38 | % sudo adduser $USER fuse 39 | 40 | Now log out and back in again and you’ll be able to mount remote 41 | directories into the filesystem on the Pi: 42 | 43 | % mkdir rusty-pi 44 | % sshfs -o idmap=user -o gid=`id --group` nat@192.168.1.4:rusty-pi rusty-pi 45 | 46 | (Replace "192.168.1.4" with the IP address or hostname of your PC and 47 | "nat" with your user id on your PC) 48 | 49 | Now you can edit the files on your PC, and run them on the Pi 50 | immediately. 51 | 52 | When you’ve finished, unmount the directory using fusermount -u: 53 | 54 | % fusermount -u rusty-pi 55 | 56 | 57 | === Automating logon to the Pi 58 | 59 | Whether you use rsync and scp or a sshfs and a shared folder, you’ll 60 | get asked to enter your password each time you want to connect. This 61 | can get pretty tedious. We can simplify deployment by creating an SSH 62 | key-pair that is authorised to connect to the Pi so that you do not 63 | have to enter the password every time you connect. 64 | 65 | If you need to generate a key pair on your PC. run 66 | 67 | % ssh-keygen -t rsa 68 | 69 | Accept the default location. 70 | 71 | You will be prompted for a passphrase, and asked to confirm it. If you 72 | enter an empty string, you can use your key to log on to the Pi 73 | without further interaction. 74 | 75 | [WARNING] 76 | So can anyone else who gains access to your PC account, so use this option with care! 77 | 78 | OpenSSH will create a pair of files in your .ssh directory – a private 79 | key which you must keep secret, and a public key which you can 80 | transfer to other computers. 81 | 82 | To transfer your public key to the the RPi, on your PC run: 83 | 84 | % ssh-copy-id nat@ 85 | 86 | On a Mac you'll have to run: 87 | 88 | % cat ~/.ssh/id_rsa.pub | ssh nat@192.168.1.4 "umask 077; mkdir -p .ssh ; cat >> .ssh/authorized_keys" 89 | 90 | After one final prompt you won’t be prompted for your Pi password when 91 | using ssh or sshfs. 92 | 93 | -------------------------------------------------------------------------------- /doc/export-gpio-err.compile-output: -------------------------------------------------------------------------------- 1 | src/export-gpio-err.rs:7:31: 7:34 error: cannot determine a type for this bounded type parameter: cannot determine the type of this integer; add a suffix to specify the type explicitly 2 | src/export-gpio-err.rs:7 write!(export_file, "{}", pin); 3 | ^~~ 4 | note: in expansion of format_args_method! 5 | :3:9: 4:6 note: expansion site 6 | :1:1: 5:2 note: in expansion of write! 7 | src/export-gpio-err.rs:7:5: 7:36 note: expansion site 8 | -------------------------------------------------------------------------------- /doc/file-ownership-err.compile-output: -------------------------------------------------------------------------------- 1 | src/file-ownership-err.rs:9:5: 9:6 error: use of moved value: `f` 2 | src/file-ownership-err.rs:9 f.flush() 3 | ^ 4 | src/file-ownership-err.rs:7:14: 7:15 note: `f` moved here because it has type `std::io::fs::File`, which is non-copyable (perhaps you meant to use clone()?) 5 | src/file-ownership-err.rs:7 use_file(f) 6 | ^ 7 | src/file-ownership-err.rs:14:5: 14:6 error: cannot borrow immutable argument `f` as mutable 8 | src/file-ownership-err.rs:14 f.write_str("hello") 9 | ^ 10 | error: aborting due to 2 previous errors 11 | -------------------------------------------------------------------------------- /doc/hello-world.asciidoc: -------------------------------------------------------------------------------- 1 | == What Can We Learn From Hello World? 2 | 3 | Let's have another look at the Hello World program we wrote to test 4 | our build toolchain. Although short, it does demonstrate some notable 5 | features of Rust that we will explore in more depth as we go through 6 | the book. 7 | 8 | .The Hello World Program 9 | ----------------------------------------------- 10 | include::../src/hello.rs[] 11 | ----------------------------------------------- 12 | 13 | The program defines a single function called `main`, which is the 14 | program's entry point. To be pedantic, `main` is the entry point of 15 | the program's _main task_. A Rust program is organised as a tree of 16 | concurrent tasks that communicate by sending messages to one 17 | another. The program starts by running the main task. The main task 18 | spawns other tasks and sets up communication channels between them. 19 | 20 | The syntax for defining functions is slightly different from that of 21 | C-like languages, but still quite recognisable, especially if you've 22 | used to Python, Ruby or JavaScript. Functions definitions start with 23 | the `fn` keyword, followed by the name of the function. Parameters 24 | are defined between parentheses. In this program, we define a 25 | function called `main` that has no parameters. The main function does 26 | not return a value, and so, unlike in C, we do not need to specify its 27 | result type. 28 | 29 | If you are used to C, Java, or many other languages, you may be 30 | wondering about the lack of import or #include statements. Where does 31 | the definition of `println!` come from? Is it built in to the 32 | language? Like C, Rust is a small language with few built-in 33 | constructs. Println is therefore defined in the standard library. 34 | However, commonly used features defined in the various modules of the 35 | standard library are also reexported from a module called the 36 | "prelude". The contents of the prelude are implicitly imported into 37 | every module, making the most common standard types and functions 38 | available without boilerplate. This lets Rust remain a a small 39 | language with as much functionality as possible defined in libraries, 40 | while also giving the programmer the convenience of a rich language 41 | can be used without lots of boilerplate to import functionality from 42 | libraries. 43 | 44 | TBD... 45 | 46 | macros -- not like C 47 | 48 | println -- is type safe 49 | -------------------------------------------------------------------------------- /doc/introduction.asciidoc: -------------------------------------------------------------------------------- 1 | == Introduction 2 | 3 | This is a book about Rust, a new programming language being developed 4 | by Mozilla. 5 | 6 | We are currently enjoying a renaissance of programming language 7 | design, at least for application development. New languages like 8 | Scala, Clojure, F# and Swift are displacing Java, C#, Ruby and 9 | Objective-C. Haskell is bubbling under. Financial institutions have 10 | embraced ML and array processing languages like K and Q. 11 | 12 | But for _system_ programming, in which precise control over memory and 13 | hardware are necessary, C and $$C++$$ still rule the roost. 14 | Unfortunately C and $$C++$$ are error prone -- it is so very easy for 15 | programmers to make mistakes that corrupt memory, crash the running 16 | program or, worse, allow attackers to bypass security and steal data 17 | from or take control of remote computers. 18 | 19 | I'm excited about Rust because it applies the latest research in type 20 | theory to a system programming language to catch many of the errors 21 | that are common in C and C++ programming. It provides: 22 | 23 | * type safety - use generic types instead of casting to/from void 24 | pointers. 25 | 26 | * memory safety - the compiler will not allow the program to 27 | defererence null pointers or dereference pointers to memory before 28 | it has been initialised or after it has been deallocated. 29 | 30 | * automatic memory management - Rust automatically frees dynamically 31 | allocated memory, avoiding memory leaks. 32 | 33 | * concurrency - Rust organises programs into concurrent tasks that 34 | communicate by message sending. The compiler will not allow data 35 | races, in which different tasks use the same area of memory without 36 | synchronisation. 37 | 38 | 39 | However, Rust is a very new language. The Rust team are still tying up 40 | loose ends to get to version 1.0 of the language. If you're used to 41 | mainstream languages you'll find the experience of programming in Rust 42 | a bit spartan. There are no powerful IDEs with autocompletion, code 43 | transformation & documentation at your fingertips -- you have to write 44 | your programs in a plain ol' text editor like Vi, Emacs or Sublime 45 | Text. There are few libraries to build applications upon and (at the 46 | time of writing) no standard package manager with which Rust users can 47 | share libraries with one another footnote:[the Rust team are working 48 | on a package manager called Cargo. Hopefully it will have been 49 | released by the time you are reading this.]. 50 | 51 | With a few notable exceptions footnote:[for example, C# and Swift were 52 | announced after years of development in secret and strongly marketed 53 | by a huge vendor, Microsoft and Apple respectively, as the new way to 54 | develop for their platform], all new languages have to overcome a 55 | chicken-and-egg problem. Programmers are unwilling to use a language 56 | that has few libraries and tools, but unwilling to write libraries and 57 | tools for a language that has few users. Languages can break out of 58 | this vicious circle by finding a niche in which they excel and then 59 | expanding from that niche into other application areas. 60 | 61 | For Rust, physical computing could be one such niche. Applications 62 | that control physical devices need to: 63 | 64 | * run on bare metal or with high operating-system privileges, that 65 | allows bugs to corrupt system state and crash the entire machine 66 | 67 | * read and write to directly to system memory locations, such as 68 | memory-mapped device registers, that are usually made inaccessible 69 | to application programs by the operating system and virtual memory 70 | hardware. 71 | 72 | * coordinate events from multiple hardware devices at the same time 73 | 74 | Rust's type system that disallows invalid use of memory and 75 | message-passing concurrency sound like a great help! 76 | 77 | In its current state, Rust is not suitable for use on small 8-bit 78 | microcontroller boards like the link:http://arduino.cc[Arduino 79 | Uno]. The Rust compiler creates programs that are far too large and 80 | the language runtime relies on an operating system for multithreading, 81 | memory management and I/O. But this is not a problem on more capable 82 | single-board systems like the 83 | link:http://www.raspberrypi.org/[Raspberry Pi], which I use in this 84 | book, link:http://arduino.cc[Arduino Tre] and 85 | link:http://beagleboard.org/Products/BeagleBone%20Black[BeagleBone]. These 86 | computers are built around a 32-bit ARM system-on-a-chip, can run 87 | Linux, and have a variety of pinouts for interfacing with the outside 88 | world. Even better - they are relatively cheap. A Raspberry Pi costs 89 | about £25 (35 USD), although you must add on the cost of an SD card, 90 | power supply and Ethernet cable, and keyboard and HDMI connector if 91 | you want to use it like a desktop PC. 92 | 93 | This book is not an in-depth tutorial about Rust, nor is it an in 94 | depth introduction to electronic engineering. I'm going to use 95 | physical computing to demonstrate Rust's language features in action 96 | and show how and why they are useful. I hope that the programs in 97 | this book will inspire you to learn more about Rust. I hope you choose 98 | to use Rust in your own projects. I hope you enthuse about Rust to 99 | other programmers. I hope that, eventually, Rust will become a 100 | popular, widely supported language. 101 | 102 | More information about Rust can be found at the following sites. 103 | 104 | * http://www.rust-lang.org The Rust Website 105 | 106 | * http://doc.rust-lang.org/tutorial.html The Rust Tutorial 107 | 108 | * http://doc.rust-lang.org/rust.html The Rust Reference Manual 109 | 110 | * http://www.rustbyexample.com/ Rust by Example, an interactive 111 | tutorial that lets you write and run Rust code in your web browser. 112 | 113 | * http://www.rustforrubyists.com Rust for Rubyists by Steve 114 | Klabnik, a book about Rust aimed primarily at Ruby programmers. 115 | 116 | 117 | === Which Version of Rust? 118 | 119 | The code in this book is written for Rust version {rust_version}. 120 | 121 | -------------------------------------------------------------------------------- /doc/macros.asciidoc: -------------------------------------------------------------------------------- 1 | :ohm: Ω 2 | :kohm: k{ohm} 3 | :ehat: ê 4 | -------------------------------------------------------------------------------- /doc/parts-bin.asciidoc: -------------------------------------------------------------------------------- 1 | == Filling Your Parts Bin 2 | 3 | What to buy. No Rust learning in this chapter. 4 | 5 | You will need some components build the circuits in this book. 6 | 7 | === A Breadboard 8 | 9 | image::breadboard.jpg[] 10 | 11 | A half-size breadboards are about the same size as the Raspberry Pi 12 | and will be plenty big enough for the circuits we will build in this 13 | book. 14 | 15 | 16 | === Jumper Wires 17 | 18 | image::jumper-cables.jpg[] 19 | 20 | You'll need jumper wires to connect your Raspberry Pi's header to the 21 | breadboard and create connections between components on the 22 | breadboard. You'll need female-to-male jumpers to connect the 23 | Rasperry Pi's header to the breadboard. Other connections will need 24 | male-to-male jumpers. Get a selection of different colours, so you 25 | can easily distinguish power, ground and control connections, and a 26 | selection of different lengths -- long ones that let you easily reach 27 | the breadboard from the Pi and short ones so that connections on the 28 | breadboard don't become a tangled mess. 29 | 30 | === LEDs 31 | 32 | image::leds.jpg[] 33 | 34 | Coloured light-emitting diodes -- for basic output. 35 | 36 | 37 | === Push Buttons 38 | 39 | image::pushbuttons.jpg[] 40 | 41 | Push buttons for user input. 42 | 43 | === Resistors 44 | 45 | image::resistors.jpg[] 46 | 47 | 220{ohm} resistors to reduce current flow through the Raspberry Pi so that 48 | you don't burn out it's delicate circuits. 49 | 50 | 100{kohm} resistors to pull floating input voltages down (or up). 51 | 52 | 53 | === Other fun components 54 | 55 | Don't stick with our choices. Experiment with other components: 56 | buzzers, RGB LEDs, passive infrared detectors, tilt sensors, ... 57 | -------------------------------------------------------------------------------- /doc/pi-top.asciidoc: -------------------------------------------------------------------------------- 1 | [[pi-top]] 2 | .Top view of the Raspberry Pi model B 3 | [image_credit="Gijsbert Peijs"] 4 | [image_copyright="14-09-2012"] 5 | [image_license="CC BY 2.0"] 6 | [image_source="https://www.flickr.com/photos/gijsbertpeijs/7988256711"] 7 | [image_modification="cropped, colour balance adjusted"] 8 | image::pi-top.jpg[width="320px"] 9 | 10 | -------------------------------------------------------------------------------- /doc/pi-top.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/npryce/rusty-pi/3aa638c06c6b4dd519c540b583a94d6c419ad12f/doc/pi-top.jpg -------------------------------------------------------------------------------- /doc/pi.fzz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/npryce/rusty-pi/3aa638c06c6b4dd519c540b583a94d6c419ad12f/doc/pi.fzz -------------------------------------------------------------------------------- /doc/pi.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | Raspberry Pi 15 | 16 | 17 | Model B (R2) 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | h 29 | 30 | 31 | t 32 | 33 | 34 | t 35 | 36 | 37 | p 38 | 39 | 40 | : 41 | 42 | 43 | / 44 | 45 | 46 | / 47 | 48 | 49 | w 50 | 51 | 52 | w 53 | 54 | 55 | w 56 | 57 | 58 | . 59 | 60 | 61 | r 62 | 63 | 64 | a 65 | 66 | 67 | s 68 | 69 | 70 | p 71 | 72 | 73 | b 74 | 75 | 76 | e 77 | 78 | 79 | r 80 | 81 | 82 | r 83 | 84 | 85 | y 86 | 87 | 88 | p 89 | 90 | 91 | i 92 | 93 | 94 | . 95 | 96 | 97 | o 98 | 99 | 100 | r 101 | 102 | 103 | g 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | OK 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | PWR 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | FDX 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | LNK 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 10M 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | Power 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | HDMI 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | Audio 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | Video-Out 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | USB 2x 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | ETHERNET 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | DSI (DISPLAY) 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | CSI (CAMERA) 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 1 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | 529 | 530 | 531 | 532 | 533 | 534 | 535 | 536 | 537 | 538 | 539 | 540 | 541 | 542 | 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | 554 | 555 | 556 | 557 | 558 | 559 | 560 | 561 | 562 | 563 | 564 | 565 | 566 | 567 | 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | 685 | 686 | 687 | 688 | 689 | 690 | 691 | 692 | 693 | 694 | 695 | 696 | 697 | 698 | 699 | 700 | 701 | 702 | 703 | 704 | 705 | 706 | 707 | 708 | 709 | 710 | 711 | 712 | 713 | 714 | 715 | 716 | 717 | 718 | 719 | 720 | 721 | 722 | 723 | 724 | 725 | 726 | 727 | 728 | 729 | 730 | 731 | 732 | 733 | 734 | 735 | 736 | 737 | 738 | 739 | 740 | 741 | 742 | 743 | 744 | 745 | 746 | 747 | 748 | 749 | 750 | 751 | 752 | 753 | 754 | 755 | 756 | 757 | 758 | 759 | 760 | 761 | 762 | 763 | 764 | 765 | 766 | 767 | 768 | 769 | 770 | 771 | 772 | 773 | 774 | 775 | 776 | 777 | 778 | 779 | 780 | 781 | 782 | 783 | 784 | 785 | 786 | 787 | 788 | 789 | 790 | 791 | 792 | 793 | 794 | 795 | 796 | 797 | 798 | 799 | 800 | 801 | 802 | 803 | 804 | 805 | 806 | 807 | 808 | 809 | 810 | 811 | 812 | 813 | 814 | 815 | 816 | 817 | 818 | 819 | 820 | 821 | 822 | 823 | 824 | 825 | 826 | 827 | 828 | 829 | 830 | 831 | 832 | 833 | 834 | 835 | 836 | 837 | 838 | 839 | 840 | 841 | 842 | 843 | 844 | 845 | 846 | 847 | 848 | 849 | 850 | 851 | 852 | 853 | 854 | 855 | 856 | 857 | 858 | 859 | 860 | 861 | 862 | 863 | 864 | 865 | 866 | 867 | 868 | 869 | 870 | 871 | 872 | 873 | 874 | 875 | 876 | 877 | 878 | 879 | 880 | 881 | 882 | 883 | 884 | 885 | 886 | 887 | 888 | 889 | 890 | 891 | 892 | 893 | 894 | 895 | 896 | 897 | 898 | 899 | 900 | 901 | 902 | 903 | 904 | 905 | 906 | 907 | 908 | 909 | 910 | 911 | 912 | 913 | 914 | 915 | 916 | 917 | 918 | 919 | 920 | 921 | 922 | 923 | 924 | 925 | 926 | 927 | 928 | 929 | 930 | 931 | 932 | 933 | 934 | 935 | 936 | 937 | 938 | 939 | 940 | 941 | 942 | 943 | 944 | 945 | 946 | 947 | 948 | 949 | 950 | 951 | 952 | 953 | 954 | 955 | 956 | 957 | 958 | 959 | 960 | 961 | 962 | 963 | 964 | 965 | 966 | 967 | 968 | 969 | 970 | 971 | 972 | 973 | 974 | 975 | 976 | 977 | 978 | 979 | 980 | 981 | 982 | 983 | 984 | 985 | 986 | 987 | 988 | 989 | 990 | 991 | 992 | 993 | 994 | 995 | 996 | 997 | 998 | 999 | 1000 | 1001 | 1002 | 1003 | 1004 | 1005 | 1006 | 1007 | 1008 | 1009 | 1010 | 1011 | 1012 | 1013 | 1014 | 1015 | 1016 | 1017 | 1018 | 1019 | 1020 | 1021 | 1022 | 1023 | 1024 | 1025 | 1026 | 1027 | 1028 | 1029 | 1030 | 1031 | 1032 | 1033 | 1034 | 1035 | 1036 | 1037 | 1038 | 1039 | 1040 | 1041 | 1042 | 1043 | 1044 | 1045 | 1046 | 1047 | 1048 | 1049 | 1050 | 1051 | 1052 | 1053 | 1054 | 1055 | 1056 | 1057 | 1058 | 1059 | 1060 | 1061 | 1062 | 1063 | 1064 | 1065 | 1066 | 1067 | 1068 | 1069 | 1070 | 1071 | 1072 | 1073 | 1074 | 1075 | 1076 | 1077 | 1078 | 1079 | 1080 | 1081 | 1082 | 1083 | 1084 | 1085 | 1086 | 1087 | 1088 | 1089 | 1090 | 1091 | 1092 | 1093 | 1094 | 1095 | 1096 | 1097 | 1098 | 1099 | 1100 | 1101 | 1102 | 1103 | 1104 | 1105 | 1106 | 1107 | 1108 | 1109 | 1110 | 1111 | 1112 | 1113 | 1114 | 1115 | 1116 | 1117 | 1118 | 1119 | 1120 | 1121 | 1122 | 1123 | 1124 | 1125 | 1126 | 1127 | 1128 | 1129 | 1130 | 1131 | 1132 | 1133 | 1134 | 1135 | 1136 | 1137 | 1138 | 1139 | 1140 | 1141 | 1142 | 1143 | 1144 | 1145 | 1146 | 1147 | 1148 | 1149 | 1150 | 1151 | 1152 | 1153 | 1154 | 1155 | 1156 | 1157 | 1158 | 1159 | 1160 | 1161 | 1162 | 1163 | 1164 | 1165 | 1166 | 1167 | 1168 | 1169 | 1170 | 1171 | 1172 | 1173 | 1174 | 1175 | 1176 | 1177 | 1178 | 1179 | 1180 | 1181 | 1182 | 1183 | 1184 | 1185 | 1186 | 1187 | 1188 | 1189 | 1190 | 1191 | 1192 | 1193 | 1194 | 1195 | 1196 | 1197 | 1198 | 1199 | 1200 | 1201 | 1202 | 1203 | 1204 | 1205 | 1206 | 1207 | 1208 | 1209 | 1210 | 1211 | 1212 | 1213 | 1214 | 1215 | 1216 | 1217 | 1218 | 1219 | 1220 | 1221 | 1222 | 1223 | 1224 | 1225 | 1226 | 1227 | 1228 | 1229 | 1230 | 1231 | 1232 | 1233 | 1234 | 1235 | 1236 | 1237 | 1238 | 1239 | 1240 | 1241 | 1242 | 1243 | 1244 | 1245 | 1246 | 1247 | 1248 | 1249 | 1250 | 1251 | 1252 | 1253 | 1254 | 1255 | 1256 | 1257 | 1258 | 1259 | 1260 | 1261 | 1262 | 1263 | 1264 | 1265 | 1266 | 1267 | 1268 | 1269 | 1270 | 1271 | 1272 | 1273 | 1274 | 1275 | 1276 | 1277 | 1278 | 1279 | 1280 | 1281 | 1282 | 1283 | 1284 | 1285 | 1286 | 1287 | 1288 | 1289 | 1290 | 1291 | 1292 | 1293 | 1294 | 1295 | 1296 | 1297 | 1298 | 1299 | 1300 | 1301 | 1302 | 1303 | 1304 | 1305 | 1306 | 1307 | 1308 | 1309 | 1310 | 1311 | 1312 | 1313 | 1314 | 1315 | 1316 | 1317 | 1318 | 1319 | 1320 | 1321 | 1322 | 1323 | 1324 | 1325 | 1326 | 1327 | 1328 | 1329 | 1330 | 1331 | 1332 | 1333 | 1334 | 1335 | 1336 | 1337 | 1338 | 1339 | 1340 | 1341 | 1342 | 1343 | 1344 | 1345 | 1346 | 1347 | 1348 | 1349 | 1350 | 1351 | 1352 | 1353 | 1354 | 1355 | 1356 | 1357 | 1358 | 1359 | 1360 | 1361 | 1362 | 1363 | 1364 | 1365 | 1366 | 1367 | 1368 | 1369 | 1370 | 1371 | 1372 | 1373 | 1374 | 1375 | 1376 | 1377 | 1378 | 1379 | 1380 | 1381 | 1382 | 1383 | 1384 | 1385 | 1386 | 1387 | 1388 | 1389 | 1390 | 1391 | 1392 | 1393 | 1394 | 1395 | 1396 | 1397 | 1398 | 1399 | 1400 | 1401 | 1402 | 1403 | 1404 | 1405 | 1406 | 1407 | 1408 | 1409 | 1410 | 1411 | 1412 | 1413 | 1414 | 1415 | 1416 | 1417 | 1418 | 1419 | 1420 | 1421 | 1422 | 1423 | 1424 | 1425 | 1426 | 1427 | 1428 | 1429 | 1430 | 1431 | 1432 | 1433 | 1434 | 1435 | 1436 | 1437 | 1438 | 1439 | 1440 | 1441 | 1442 | 1443 | 1444 | 1445 | 1446 | 1447 | 1448 | 1449 | 1450 | 1451 | 1452 | 1453 | 1454 | 1455 | 1456 | 1457 | 1458 | 1459 | 1460 | 1461 | 1462 | 1463 | 1464 | 1465 | 1466 | 1467 | 1468 | 1469 | 1470 | 1471 | 1472 | 1473 | 1474 | 1475 | 1476 | 1477 | 1478 | 1479 | 1480 | 1481 | 1482 | 1483 | 1484 | 1485 | 1486 | 1487 | 1488 | 1489 | 1490 | 1491 | 1492 | 1493 | 1494 | 1495 | 1496 | 1497 | 1498 | 1499 | 1500 | 1501 | 1502 | 1503 | 1504 | 1505 | 1506 | 1507 | 1508 | 1509 | 1510 | 1511 | 1512 | 1513 | 1514 | 1515 | 1516 | 1517 | 1518 | 1519 | 1520 | 1521 | 1522 | 1523 | 1524 | 1525 | 1526 | 1527 | 1528 | 1529 | 1530 | 1531 | 1532 | 1533 | 1534 | 1535 | 1536 | 1537 | 1538 | 1539 | 1540 | 1541 | 1542 | 1543 | 1544 | 1545 | 1546 | 1547 | 1548 | 1549 | 1550 | 1551 | 1552 | 1553 | 1554 | 1555 | 1556 | 1557 | 1558 | 1559 | 1560 | 1561 | 1562 | 1563 | 1564 | 1565 | 1566 | 1567 | 1568 | 1569 | 1570 | 1571 | 1572 | 1573 | 1574 | 1575 | 1576 | 1577 | 1578 | 1579 | 1580 | 1581 | 1582 | 1583 | 1584 | 1585 | 1586 | 1587 | 1588 | 1589 | 1590 | 1591 | 1592 | 1593 | 1594 | 1595 | 1596 | 1597 | 1598 | 1599 | 1600 | 1601 | 1602 | 1603 | 1604 | 1605 | 1606 | 1607 | 1608 | 1609 | 1610 | 1611 | 1612 | 1613 | 1614 | 1615 | 1616 | 1617 | 1618 | 1619 | 1620 | 1621 | 1622 | 1623 | 1624 | 1625 | 1626 | 1627 | 1628 | 1629 | 1630 | 1631 | 1632 | 1633 | 1634 | 1635 | 1636 | 1637 | 1638 | 1639 | 1640 | 1641 | 1642 | 1643 | 1644 | 1645 | 1646 | 1647 | 1648 | 1649 | 1650 | 1651 | 1652 | 1653 | 1654 | 1655 | 1656 | 1657 | 1658 | 1659 | 1660 | 1661 | 1662 | 1663 | 1664 | 1665 | 1666 | 1667 | 1668 | 1669 | 1670 | 1671 | 1672 | 1673 | 1674 | 1675 | 1676 | 1677 | 1678 | 1679 | 1680 | 1681 | 1682 | 1683 | 1684 | 1685 | 1686 | 1687 | 1688 | 1689 | 1690 | 1691 | 1692 | 1693 | 1694 | 1695 | 1696 | 1697 | 1698 | 1699 | 1700 | 1701 | 1702 | 1703 | 1704 | 1705 | 1706 | 1707 | 1708 | 1709 | 1710 | 1711 | 1712 | 1713 | 1714 | 1715 | 1716 | 1717 | 1718 | 1719 | 1720 | 1721 | 1722 | 1723 | 1724 | 1725 | 1726 | 1727 | 1728 | 1729 | 1730 | 1731 | 1732 | 1733 | 1734 | 1735 | 1736 | 1737 | 1738 | 1739 | 1740 | 1741 | 1742 | 1743 | 1744 | 1745 | 1746 | 1747 | 1748 | 1749 | 1750 | 1751 | 1752 | 1753 | 1754 | 1755 | 1756 | 1757 | 1758 | 1759 | 1760 | 1761 | 1762 | GPIO 1763 | 1764 | 1765 | 1766 | 1767 | 1768 | 1769 | 1770 | 1771 | 1772 | 1773 | 1774 | 1775 | 1776 | 1777 | 1778 | 1779 | 1780 | 1781 | 1782 | 1783 | 1784 | 1785 | 1786 | 1787 | 1788 | 1789 | 1790 | 1791 | 1792 | 1793 | 1794 | 1795 | 1796 | 1797 | 1798 | 1799 | 1800 | 1801 | 1802 | 1803 | 1804 | 1805 | 1806 | 1807 | 1808 | 1809 | 1810 | 1811 | 1812 | 1813 | 1814 | 1815 | 1816 | 1817 | 1818 | 1819 | 1820 | 1821 | 1822 | 1823 | 1824 | 1825 | 1826 | 1827 | 1828 | 1829 | 1830 | 1831 | 1832 | 1833 | 1834 | 1835 | 1836 | 1837 | 1838 | 1839 | 1840 | 1841 | 1842 | 1843 | 1844 | 1845 | 1846 | 1847 | 1848 | 1849 | 1850 | 1851 | 1852 | 1853 | 1854 | 1855 | 1856 | 1857 | 1858 | 1859 | 1860 | 1861 | 1862 | 1863 | 1864 | 1865 | 1866 | 1867 | 1868 | 1869 | 1870 | 1871 | 1872 | 1873 | 1874 | 1875 | 1876 | 1877 | 1878 | 1879 | 1880 | 1881 | 1882 | 1883 | 1884 | 1885 | 1886 | 1887 | 1888 | 1889 | 1890 | 1891 | 1892 | 1893 | 1894 | 1895 | 1896 | 1897 | 1898 | 1899 | 1900 | 1901 | 1902 | 1903 | 1904 | 1905 | 1906 | 1907 | 1908 | 1909 | 1910 | 1911 | 1912 | 1913 | 1914 | 1915 | 1916 | 1917 | 1918 | 1919 | 1920 | 1921 | 1922 | 1923 | 1924 | 1925 | 1926 | 1927 | 1928 | 1929 | 1930 | 1931 | 1932 | 1933 | 1934 | 1935 | 1936 | 1937 | 1938 | 1939 | 1940 | 1941 | 1942 | 1943 | 1944 | 1945 | 1946 | 1947 | 1948 | 1949 | 1950 | 1951 | 1952 | 1953 | 1954 | 1955 | 1956 | 1957 | 1958 | 1959 | 1960 | 1961 | 1962 | 1963 | 1964 | 1965 | 1966 | 1967 | 1968 | 1969 | 1970 | 1971 | 1972 | 1973 | 1974 | 1975 | 1976 | 1977 | 1978 | 1979 | 1980 | 1981 | 1982 | 1983 | 1984 | 1985 | 1986 | 1987 | 1988 | 1989 | 1990 | 1991 | 1992 | 1993 | 1994 | 1995 | 1996 | 1997 | 1998 | 1999 | 2000 | 2001 | 2002 | 2003 | 2004 | 2005 | 2006 | 2007 | 2008 | 2009 | 2010 | 2011 | 2012 | 2013 | 2014 | 2015 | 2016 | 2017 | 2018 | 2019 | 2020 | 2021 | 2022 | 2023 | 2024 | 2025 | 2026 | 2027 | 2028 | 2029 | 2030 | 2031 | 2032 | 2033 | 2034 | 2035 | 2036 | 2037 | 2038 | 2039 | 2040 | 2041 | 2042 | 2043 | 2044 | 2045 | 2046 | 2047 | 2048 | 2049 | 2050 | 2051 | 2052 | 2053 | 2054 | 2055 | 2056 | 2057 | 2058 | 2059 | 2060 | 2061 | 2062 | 2063 | 2064 | 2065 | 2066 | 2067 | 3V3 2068 | 2069 | 2070 | 2071 | 2072 | 2073 | 2074 | 2075 | 2076 | 2077 | 2078 | 5V 2079 | 2080 | 2081 | 2082 | 2083 | 2084 | 2085 | 2086 | 2087 | 2088 | 2089 | 5V 2090 | 2091 | 2092 | 2093 | 2094 | 2095 | 2096 | 2097 | 2098 | 2099 | 2100 | GND 2101 | 2102 | 2103 | 2104 | 2105 | 2106 | 2107 | 2108 | 2109 | 2110 | 2111 | 4 2112 | 2113 | 2114 | 2115 | 2116 | 2117 | 2118 | 2119 | 2120 | 2121 | 2122 | GND 2123 | 2124 | 2125 | 2126 | 2127 | 2128 | 2129 | 2130 | 2131 | 2132 | 2133 | 17 2134 | 2135 | 2136 | 2137 | 2138 | 2139 | 2140 | 2141 | 2142 | 2143 | 2144 | 18 2145 | 2146 | 2147 | 2148 | 2149 | 2150 | 2151 | 2152 | 2153 | 2154 | 2155 | 27 2156 | 2157 | 2158 | 2159 | 2160 | 2161 | 2162 | 2163 | 2164 | 2165 | 2166 | GND 2167 | 2168 | 2169 | 2170 | 2171 | 2172 | 2173 | 2174 | 2175 | 2176 | 2177 | 22 2178 | 2179 | 2180 | 2181 | 2182 | 2183 | 2184 | 2185 | 2186 | 2187 | 2188 | 23 2189 | 2190 | 2191 | 2192 | 2193 | 2194 | 2195 | 2196 | 2197 | 2198 | 2199 | 3V3 2200 | 2201 | 2202 | 2203 | 2204 | 2205 | 2206 | 2207 | 2208 | 2209 | 2210 | 24 2211 | 2212 | 2213 | 2214 | 2215 | 2216 | 2217 | 2218 | 2219 | 2220 | 2221 | GND 2222 | 2223 | 2224 | 2225 | 2226 | 2227 | 2228 | 2229 | 2230 | 2231 | 2232 | 25 2233 | 2234 | 2235 | 2236 | 2237 | 2238 | 2239 | 2240 | 2241 | 2242 | 2243 | GND 2244 | 2245 | 2246 | 2247 | 2248 | 2249 | 2250 | 2251 | 2252 | 2253 | 2254 | 2255 | 2256 | 2257 | 2258 | 2259 | 2260 | 10 2261 | 2262 | 2263 | 2264 | 2265 | 2266 | 2267 | MOSI 2268 | 2269 | 2270 | 2271 | 2272 | 2273 | 2274 | 2 2275 | 2276 | 2277 | 2278 | 2279 | 2280 | 2281 | SDA 2282 | 2283 | 2284 | 2285 | 2286 | 2287 | 2288 | 3 2289 | 2290 | 2291 | 2292 | 2293 | 2294 | 2295 | SCL 2296 | 2297 | 2298 | 2299 | 2300 | 2301 | 2302 | 9 2303 | 2304 | 2305 | 2306 | 2307 | 2308 | 2309 | MOSI 2310 | 2311 | 2312 | 2313 | 2314 | 2315 | 2316 | 11 2317 | 2318 | 2319 | 2320 | 2321 | 2322 | 2323 | SCLK 2324 | 2325 | 2326 | 2327 | 2328 | 2329 | 2330 | 14 2331 | 2332 | 2333 | 2334 | 2335 | 2336 | 2337 | TXD 2338 | 2339 | 2340 | 2341 | 2342 | 2343 | 2344 | 15 2345 | 2346 | 2347 | 2348 | 2349 | 2350 | 2351 | RXD 2352 | 2353 | 2354 | 2355 | 2356 | 2357 | 2358 | 8 2359 | 2360 | 2361 | 2362 | 2363 | 2364 | 2365 | CS0 2366 | 2367 | 2368 | 2369 | 2370 | 2371 | 2372 | 7 2373 | 2374 | 2375 | 2376 | 2377 | 2378 | 2379 | CS1 2380 | 2381 | 2382 | 2383 | -------------------------------------------------------------------------------- /doc/rust-whats-the-big-idea.asciidoc: -------------------------------------------------------------------------------- 1 | == Rust - What's the Big Idea? 2 | 3 | [quote, Alan Perlis] 4 | A language that doesn't affect the way you think about programming, is not worth knowing. 5 | 6 | Rust's big idea is _ownership_ and, in particular, being explicit 7 | about transfer of ownership. The Rust compiler checks ownership and 8 | lifetimes at compile time and rejects a program that attempts to use 9 | values beyond their lifetimes. Rust's principal goal as a systems 10 | programming language is memory safety without runtime overhead. 11 | Although Rust programs manually manage memory, the Rust compiler 12 | rejects programs that try to dereference null pointers or dangling 13 | pointers, double-free heap-allocated memory, use uninitialised memory, 14 | overrun buffers or create data races between threads. Rust's 15 | combination of a powerful type system and control structures adopted 16 | from modern functional programming languages gives the programmer the 17 | expressiveness of a high-level language and the precise control over 18 | memory and performance of a low-level language. 19 | 20 | 21 | === Ownership, Transferring Ownership, and Borrowing 22 | 23 | Rust's novel feature -- the feature that makes it a "Perlis language" in my book -- is that, by default, values are _moved_, not copied. 24 | 25 | ... more to come... 26 | 27 | Example. This program does not compile. 28 | 29 | ------------------------------------------------------- 30 | include::../src/file-ownership-err.rs[] 31 | ------------------------------------------------------- 32 | 33 | Error: 34 | 35 | ------------------------------------------------------- 36 | include::file-ownership-err.compile-output[] 37 | ------------------------------------------------------- 38 | 39 | 40 | === Zero-Overhead Abstractions 41 | 42 | TBD 43 | 44 | 45 | === Message-Passing Concurrency 46 | 47 | 48 | Rust's ownership model extends to concurrency. Disallows data races. 49 | 50 | 51 | -------------------------------------------------------------------------------- /originals/pi-photo-orig.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/npryce/rusty-pi/3aa638c06c6b4dd519c540b583a94d6c419ad12f/originals/pi-photo-orig.jpg -------------------------------------------------------------------------------- /originals/pi-photo.xcf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/npryce/rusty-pi/3aa638c06c6b4dd519c540b583a94d6c419ad12f/originals/pi-photo.xcf -------------------------------------------------------------------------------- /src/blink-button-epoll.rs: -------------------------------------------------------------------------------- 1 | 2 | use std::io::timer::Timer; 3 | use pi::gpio::{open_pin,Pin,In,Out,BothEdges}; 4 | use pi::epoll::{IoSelector,IN,ERR,ET}; 5 | 6 | mod pi; 7 | 8 | 9 | fn poll_changes(mut button_pin : Pin, out: SyncSender) { 10 | button_pin.set_interrupt(BothEdges).unwrap(); 11 | 12 | let mut selector = IoSelector::create().unwrap(); 13 | selector.add(&button_pin, IN|ERR|ET, 1).unwrap(); 14 | 15 | out.send(button_pin.get_value().unwrap()); 16 | loop { 17 | selector.wait().unwrap(); 18 | out.send(button_pin.get_value().unwrap()); 19 | } 20 | } 21 | 22 | 23 | fn blink(button: Receiver, mut led_pin : Pin) { 24 | let mut timer = Timer::new().unwrap(); 25 | 26 | loop { 27 | loop { 28 | let mut button_state = button.recv(); 29 | 30 | if button_state == 1 { 31 | let mut led_state = 1u; 32 | let blink_timeout = timer.periodic(1000); 33 | 34 | led_pin.set_value(led_state).unwrap(); 35 | 36 | while button_state == 1 { 37 | select! { 38 | b = button.recv() => {button_state = b;}, 39 | _ = blink_timeout.recv() => {led_state = 1 - led_state;} 40 | } 41 | 42 | led_pin.set_value(led_state*button_state).unwrap(); 43 | } 44 | } 45 | } 46 | } 47 | } 48 | 49 | 50 | fn main() { 51 | let button_pin = open_pin(23,In).unwrap(); 52 | let led_pin = open_pin(18,Out).unwrap(); 53 | let (send, recv) = sync_channel(0); 54 | 55 | spawn(proc() { poll_changes(button_pin, send); }); 56 | spawn(proc() { blink(recv, led_pin); }); 57 | } 58 | -------------------------------------------------------------------------------- /src/blink-button-tasks.rs: -------------------------------------------------------------------------------- 1 | 2 | use std::io::timer::{Timer,sleep}; 3 | use pi::gpio::{open_pin,Pin,In,Out}; 4 | 5 | mod pi; 6 | 7 | 8 | fn poll_changes(mut button_pin : Pin, out: SyncSender) { 9 | let mut last_button_state = button_pin.get_value().unwrap(); 10 | 11 | out.send(last_button_state); 12 | 13 | loop { 14 | let button_state = button_pin.get_value().unwrap(); 15 | if button_state != last_button_state { 16 | out.send(button_state); 17 | last_button_state = button_state; 18 | } 19 | 20 | sleep(100); 21 | } 22 | } 23 | 24 | 25 | fn blink(button: Receiver, mut led_pin : Pin) { 26 | let mut timer = Timer::new().unwrap(); 27 | 28 | loop { 29 | loop { 30 | let mut button_state = button.recv(); 31 | 32 | if button_state == 1 { 33 | let mut led_state = 1u; 34 | let blink_timeout = timer.periodic(1000); 35 | 36 | led_pin.set_value(led_state).unwrap(); 37 | 38 | while button_state == 1 { 39 | select! { 40 | b = button.recv() => {button_state = b;}, 41 | _ = blink_timeout.recv() => {led_state = 1 - led_state;} 42 | } 43 | 44 | led_pin.set_value(led_state*button_state).unwrap(); 45 | } 46 | } 47 | } 48 | } 49 | } 50 | 51 | 52 | fn main() { 53 | let button_pin = open_pin(23,In).unwrap(); 54 | let led_pin = open_pin(18,Out).unwrap(); 55 | let (send, recv) = sync_channel(0); 56 | 57 | spawn(proc() { poll_changes(button_pin, send); }); 58 | spawn(proc() { blink(recv, led_pin); }); 59 | } 60 | -------------------------------------------------------------------------------- /src/blink-button.rs: -------------------------------------------------------------------------------- 1 | 2 | use std::io::timer::sleep; 3 | use pi::gpio::{open_pin,In,Out}; 4 | 5 | mod pi; 6 | 7 | 8 | fn main() { 9 | let mut led_pin = open_pin(18,Out).unwrap(); 10 | let mut button_pin = open_pin(23,In).unwrap(); 11 | 12 | let mut led_value = 0u; 13 | 14 | for i in range(0u,10) { 15 | println!("{} / {}", i+1, 10u); 16 | 17 | let button_value = button_pin.get_value().unwrap(); 18 | 19 | led_pin.set_value(led_value * button_value).unwrap(); 20 | 21 | led_value = 1 - led_value; 22 | sleep(1000); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /src/blink.rs: -------------------------------------------------------------------------------- 1 | 2 | use std::io::timer::sleep; 3 | use pi::gpio::{open_pin,Out}; 4 | 5 | mod pi; 6 | 7 | 8 | fn main() { 9 | let mut pin = open_pin(18,Out).unwrap(); 10 | let mut value = 1; 11 | 12 | for i in range(0u,10) { 13 | println!("{} / {}", i+1, 10u); 14 | 15 | pin.set_value(value).unwrap(); 16 | value = 1 - value; 17 | sleep(1000); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/box-ownership-err.rs: -------------------------------------------------------------------------------- 1 | 2 | fn main() { 3 | let mut ibox = box 10; 4 | use_boxed_value(ibox); 5 | println!("{}", ibox); 6 | } 7 | 8 | fn use_boxed_value(mut ibox: Box) { 9 | *ibox = 20; 10 | } 11 | -------------------------------------------------------------------------------- /src/box-ownership-ok.rs: -------------------------------------------------------------------------------- 1 | 2 | fn main() { 3 | let ibox : Box = box 10; 4 | use_boxed_value(&*ibox); 5 | println!("finished with {}", ibox); 6 | } 7 | 8 | fn use_boxed_value(iref: &int) { 9 | println!("borrowing {}", iref); 10 | } 11 | -------------------------------------------------------------------------------- /src/eventfd-example.rs: -------------------------------------------------------------------------------- 1 | 2 | use pi::eventfd::semaphore; 3 | use pi::epoll::{IoSelector,IN}; 4 | use std::io::timer::sleep; 5 | 6 | mod pi; 7 | 8 | fn main() { 9 | let (send, recv) = semaphore(0).unwrap(); 10 | 11 | spawn(proc() { 12 | println!("SENDER: sleeping..."); 13 | sleep(1000); 14 | println!("SENDER: awake - sending signal"); 15 | send.signal().unwrap(); 16 | }); 17 | 18 | spawn(proc() { 19 | let mut s = IoSelector::create().unwrap(); 20 | s.add(&recv, IN, 0).unwrap(); 21 | 22 | println!("RECEIVER: waiting for signal..."); 23 | s.wait().unwrap(); 24 | 25 | println!("RECEIVER: receiving signal"); 26 | recv.recv().unwrap(); 27 | println!("RECEIVER: signal received"); 28 | }); 29 | } 30 | -------------------------------------------------------------------------------- /src/export-gpio-err.rs: -------------------------------------------------------------------------------- 1 | use std::io::File; 2 | 3 | fn main() { 4 | let pin = 18; 5 | 6 | let mut export_file = File::create(&Path::new("/sys/class/gpio/export")); 7 | write!(export_file, "{}", pin); 8 | } 9 | -------------------------------------------------------------------------------- /src/file-ownership-err.rs: -------------------------------------------------------------------------------- 1 | 2 | use std::io::{File,IoResult}; 3 | 4 | fn main() { 5 | let mut f = File::open(&Path::new("hello.txt")) 6 | .ok().expect("could not open file"); 7 | use_file(f) 8 | .ok().expect("could not use file"); 9 | f.flush() 10 | .ok().expect("could not flush file"); 11 | } 12 | 13 | fn use_file(f: File) -> IoResult<()> { 14 | f.write_str("hello") 15 | } 16 | -------------------------------------------------------------------------------- /src/file-ownership-ok.rs: -------------------------------------------------------------------------------- 1 | 2 | use std::io::{File,IoResult}; 3 | 4 | fn main() { 5 | let mut f = File::open(&Path::new("hello.txt")) 6 | .ok().expect("could not open file"); 7 | use_file(&mut f) 8 | .ok().expect("could not use file"); 9 | f.flush() 10 | .ok().expect("could not flush file"); 11 | } 12 | 13 | fn use_file(f: &mut File) -> IoResult<()> { 14 | f.write_str("hello") 15 | } 16 | -------------------------------------------------------------------------------- /src/hello.rs: -------------------------------------------------------------------------------- 1 | fn main() { 2 | println!("hello, world."); 3 | } 4 | 5 | -------------------------------------------------------------------------------- /src/i2c-example.rs: -------------------------------------------------------------------------------- 1 | 2 | 3 | use std::io::timer::sleep; 4 | use pi::i2c::{Master,Read,Write}; 5 | mod pi; 6 | 7 | fn main() { 8 | let bus = Master::open(1).unwrap(); 9 | 10 | let mut rd_buf = [0u8, ..2]; 11 | 12 | loop { 13 | bus.transaction(0x48, [ 14 | Write([0x00]), // select channel 0 15 | Read(rd_buf.as_mut_slice())]).unwrap(); 16 | 17 | println!("{}", rd_buf[1]); 18 | 19 | sleep(500); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/pi/epoll.rs: -------------------------------------------------------------------------------- 1 | 2 | #![allow(dead_code)] 3 | 4 | extern crate native; 5 | extern crate libc; 6 | 7 | use self::libc::c_int; 8 | use std::io::IoResult; 9 | use super::unixio::{Fd,check_syscall,check_syscall_action}; 10 | 11 | pub static CLOEXEC : u32 = 02000000; 12 | 13 | pub static IN: u32 = 0x01; 14 | pub static PRI: u32 = 0x02; 15 | pub static OUT: u32 = 0x04; 16 | pub static ERR: u32 = 0x08; 17 | pub static HUP: u32 = 0x10; 18 | pub static ONESHOT: u32 = 0x40000000; 19 | pub static ET: u32 = 0x80000000; 20 | 21 | static ADD: c_int = 1; 22 | static DEL: c_int = 2; 23 | static MOD: c_int = 3; 24 | 25 | #[packed] 26 | struct EpollEvent { 27 | events: u32, 28 | data: u64 29 | } 30 | 31 | extern { 32 | fn epoll_create1(flags: c_int) -> c_int; 33 | fn epoll_ctl(epfd: c_int, op: c_int, fd: c_int, event: *mut EpollEvent) -> c_int; 34 | fn epoll_wait(epfd: c_int, events: *mut EpollEvent, maxevents: c_int, timeout: c_int) -> c_int; 35 | } 36 | 37 | pub trait IoEventSource { 38 | fn fd(&self) -> c_int; 39 | } 40 | 41 | #[deriving(Copy,Show)] 42 | pub struct Event { 43 | id: uint, 44 | events: u32 45 | } 46 | 47 | pub struct IoSelector { 48 | fd: Fd 49 | } 50 | 51 | impl IoSelector { 52 | pub fn create() -> IoResult { 53 | IoSelector::create1(0) 54 | } 55 | 56 | pub fn create1(flags: int) -> IoResult { 57 | check_syscall(unsafe {epoll_create1(flags as c_int)}, 58 | |fd| { IoSelector{fd: Fd::own(fd)} }) 59 | } 60 | 61 | pub fn add<'a, T:IoEventSource>(&'a mut self, event_source: &'a T, events: u32, id: uint) -> IoResult<()> { 62 | self.ctl(ADD, event_source, events, id) 63 | } 64 | 65 | pub fn update<'a, T:IoEventSource>(&'a mut self, event_source: &'a T, events: u32, id: uint) -> IoResult<()> { 66 | self.ctl(MOD, event_source, events, id) 67 | } 68 | 69 | pub fn remove<'a, T:IoEventSource>(&'a mut self, event_source: &'a T) -> IoResult<()> { 70 | self.ctl(DEL, event_source, 0, 0) 71 | } 72 | 73 | fn ctl(&mut self, op: c_int, event_source: &T, events: u32, id: uint) -> IoResult<()> { 74 | let mut ev = EpollEvent{events:events, data:id as u64}; 75 | check_syscall_action(unsafe {epoll_ctl(self.fd.native, op, event_source.fd(), &mut ev)}) 76 | } 77 | 78 | pub fn wait(&mut self) -> IoResult { 79 | let mut ev = EpollEvent{events:0, data:0}; 80 | 81 | check_syscall(unsafe {epoll_wait(self.fd.native, &mut ev, 1 as c_int, -1 as c_int)}, 82 | |_| Event{id: ev.data as uint, events: ev.events}) 83 | } 84 | } 85 | -------------------------------------------------------------------------------- /src/pi/eventfd.rs: -------------------------------------------------------------------------------- 1 | 2 | #![allow(dead_code)] 3 | 4 | extern crate libc; 5 | 6 | use self::libc::{c_int,c_uint}; 7 | use std::io::IoResult; 8 | use std::sync::Arc; 9 | use super::unixio::{Fd,check_syscall,check_syscall_action}; 10 | use super::epoll::IoEventSource; 11 | 12 | pub static SEMAPHORE : c_int = 1; 13 | pub static CLOEXEC : c_int = 02000000; 14 | pub static NONBLOCK : c_int = 04000; 15 | 16 | pub struct Eventfd { 17 | fd: Fd 18 | } 19 | 20 | pub type EventCount = u64; 21 | 22 | extern { 23 | fn eventfd(initval: c_uint, flags: c_int) -> c_int; 24 | fn eventfd_read(fd: c_int, value: *mut EventCount) -> c_int; 25 | fn eventfd_write(fd: c_int, value: EventCount) -> c_int; 26 | } 27 | 28 | impl Eventfd { 29 | pub fn create(flags: c_int) -> IoResult { 30 | check_syscall(unsafe {eventfd(0, flags)}, |fd| Eventfd{fd: Fd::own(fd)}) 31 | } 32 | 33 | pub fn write(&self, n: EventCount) -> IoResult<()> { 34 | check_syscall_action(unsafe {eventfd_write(self.fd.native, n)}) 35 | } 36 | 37 | pub fn read(&self) -> IoResult { 38 | let mut count : EventCount = 0; 39 | 40 | check_syscall(unsafe {eventfd_read(self.fd.native, &mut count)}, 41 | |_| count) 42 | } 43 | } 44 | 45 | pub struct Semaphore { 46 | fdref: Arc 47 | } 48 | 49 | pub struct SemaphoreSender { 50 | fdref: Arc 51 | } 52 | 53 | pub fn semaphore(flags: c_int) -> IoResult<(SemaphoreSender, Semaphore)> { 54 | Eventfd::create(SEMAPHORE|flags) 55 | .map(Arc::new) 56 | .map({|r| (SemaphoreSender{fdref:r.clone()}, Semaphore{fdref:r})}) 57 | } 58 | 59 | impl Semaphore { 60 | pub fn recv(&self) -> IoResult<()> { 61 | (*self.fdref).read().and(Ok(())) 62 | } 63 | } 64 | 65 | impl IoEventSource for Semaphore { 66 | fn fd(&self) -> c_int { 67 | self.fdref.fd.native 68 | } 69 | } 70 | 71 | impl SemaphoreSender { 72 | pub fn signal(&self) -> IoResult<()> { 73 | (*self.fdref).write(1) 74 | } 75 | } 76 | 77 | impl Clone for SemaphoreSender { 78 | fn clone(&self) -> SemaphoreSender { 79 | SemaphoreSender{fdref:self.fdref.clone()} 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /src/pi/gpio.rs: -------------------------------------------------------------------------------- 1 | 2 | #![allow(dead_code)] 3 | 4 | extern crate native; 5 | extern crate libc; 6 | 7 | use self::libc::c_int; 8 | use std::fmt::Show; 9 | use std::io; 10 | use std::io::{File,IoResult,IoError,TimedOut,ShortWrite,OtherIoError}; 11 | use std::rt::rtio; 12 | use std::rt::rtio::{SeekSet,RtioFileStream}; 13 | use self::native::io::FileDesc; 14 | use self::native::io::file::open; 15 | use super::epoll::IoEventSource; 16 | 17 | #[deriving(Copy,Show)] 18 | pub enum Direction {In, Out} 19 | 20 | #[deriving(Copy,Show)] 21 | pub enum Edge {NoInterrupt, RisingEdge, FallingEdge, BothEdges} 22 | 23 | fn write_value_to(path: &str, value: T) -> IoResult<()> { 24 | let mut f = try!(File::open_mode(&Path::new(path), io::Open, io::Write)); 25 | write!(f, "{}", value) 26 | } 27 | 28 | // Had to copy & paste from std::io module because it's private 29 | fn error_rtio_to_io(err: rtio::IoError) -> IoError { 30 | let rtio::IoError { code, extra, detail } = err; 31 | let mut ioerr = IoError::from_errno(code, false); 32 | ioerr.detail = detail; 33 | ioerr.kind = match ioerr.kind { 34 | TimedOut if extra > 0 => ShortWrite(extra), 35 | k => k, 36 | }; 37 | return ioerr; 38 | } 39 | 40 | 41 | pub struct Pin { 42 | port : uint, 43 | fd : FileDesc 44 | } 45 | 46 | impl Pin { 47 | fn write_to_device_file(&mut self, filename: &str, value: T) -> IoResult<()> { 48 | write_value_to(format!("/sys/devices/virtual/gpio/gpio{:u}/{:s}", 49 | self.port, filename).as_slice(), value) 50 | } 51 | 52 | pub fn set_direction(&mut self, direction : Direction) -> IoResult<()> { 53 | self.write_to_device_file("direction", match direction { 54 | In => "in", 55 | Out => "out" 56 | }) 57 | } 58 | 59 | pub fn set_interrupt(&mut self, edge : Edge) -> IoResult<()> { 60 | self.write_to_device_file("edge", match edge { 61 | NoInterrupt => "none", 62 | RisingEdge => "rising", 63 | FallingEdge => "falling", 64 | BothEdges => "both" 65 | }) 66 | } 67 | 68 | pub fn get_value(&mut self) -> IoResult { 69 | try!(self.fd.seek(0, SeekSet).map_err(error_rtio_to_io)); 70 | 71 | let mut buf = [0u8]; 72 | let amount = try!(self.fd.read(buf).map_err(error_rtio_to_io)); 73 | 74 | if amount == 0 { 75 | Err(IoError { 76 | kind: OtherIoError, 77 | desc: "no value read from GPIO file", 78 | detail: None 79 | }) 80 | } 81 | else if buf[0] == b'0' { 82 | Ok(0) 83 | } 84 | else if buf[0] == b'1' { 85 | Ok(1) 86 | } 87 | else { 88 | Err(IoError { 89 | kind: OtherIoError, 90 | desc: "unexpected value read from GPIO file", 91 | detail: Some(format!("{:u}", buf[0])) 92 | }) 93 | } 94 | } 95 | 96 | pub fn set_value(&mut self, value : uint) -> IoResult<()> { 97 | let buf = if value == 0 { b"0" } else { b"1" }; 98 | 99 | self.fd.write(buf).map_err(error_rtio_to_io) 100 | } 101 | } 102 | 103 | impl IoEventSource for Pin { 104 | fn fd(&self) -> c_int { 105 | self.fd.fd() 106 | } 107 | } 108 | 109 | impl Drop for Pin { 110 | fn drop(&mut self) { 111 | drop(write_value_to("/sys/class/gpio/unexport", self.port)); 112 | } 113 | } 114 | 115 | pub fn open_pin(port: uint, direction: Direction) -> IoResult { 116 | try!(write_value_to("/sys/class/gpio/export", port)); 117 | 118 | let pin_path = format!("/sys/class/gpio/gpio{:u}/value", port); 119 | let pin_fd = try!(open(&pin_path.to_c_str(), rtio::Open, rtio::ReadWrite).map_err(error_rtio_to_io)); 120 | 121 | let mut pin = Pin{port:port, fd:pin_fd}; 122 | 123 | try!(pin.set_direction(direction)); 124 | 125 | Ok(pin) 126 | } 127 | -------------------------------------------------------------------------------- /src/pi/i2c.rs: -------------------------------------------------------------------------------- 1 | 2 | #![allow(dead_code)] 3 | #![allow(non_camel_case_types)] 4 | 5 | extern crate libc; 6 | 7 | use std::mem::transmute; 8 | use std::io::IoResult; 9 | use self::libc::{c_int, c_ushort, open, O_RDWR}; 10 | use super::unixio::{Fd, check_syscall, check_syscall_action}; 11 | 12 | 13 | static M_TEN : c_ushort = 0x0010; // we have a ten bit chip address 14 | static M_RD : c_ushort = 0x0001; 15 | static M_NOSTART : c_ushort = 0x4000; 16 | static REV_DIR_ADDR : c_ushort = 0x2000; 17 | static IGNORE_NAK : c_ushort = 0x1000; 18 | static NO_RD_ACK : c_ushort = 0x0800; 19 | 20 | static IOCTL_RETRIES : c_int = 0x0701; // number of times a device address should be polled when not acknowledging 21 | static IOCTL_TIMEOUT : c_int = 0x0702; // set timeout in units of 10 ms 22 | // NOTE: Slave address is 7 or 10 bits, but 10-bit addresses are NOT supported! (due to code brokenness) 23 | static IOCTL_SLAVE : c_int = 0x0703; //Use this slave address 24 | static IOCTL_SLAVE_FORCE : c_int = 0x0706; // Use this slave address, even if it is already in use by a driver! 25 | static IOCTL_FUNCS : c_int = 0x0705; // Get the adapter functionality mask 26 | static IOCTL_RDWR : c_int = 0x0707; // Combined R/W transfer (one STOP only) 27 | static IOCTL_PEC : c_int = 0x0708; // != 0 to use PEC with SMBus 28 | static IOCTL_SMBUS : c_int = 0x0720; // SMBus transfer 29 | 30 | pub type SlaveAddress = u16; 31 | 32 | struct i2c_msg<'a> { 33 | addr : SlaveAddress, 34 | flags: u16, 35 | len: u16, // msg length 36 | buf: *mut u8 // pointer to msg data 37 | } 38 | 39 | struct i2c_rdwr_ioctl_data<'a> { 40 | msgs : *mut i2c_msg<'a>, // pointers to i2c_msgs 41 | nmsgs : u32 42 | } 43 | 44 | extern { 45 | fn ioctl(fd: c_int, req: c_int, ...) -> c_int; 46 | } 47 | 48 | 49 | #[deriving(Copy)] 50 | pub enum Message<'a> { 51 | Read(&'a mut[u8]), 52 | Write(&'a [u8]) 53 | } 54 | 55 | pub struct Master { 56 | fd: Fd 57 | } 58 | 59 | impl Master { 60 | pub fn open(bus_index: uint) -> IoResult { 61 | check_syscall(unsafe {open(format!("/dev/i2c-{:u}", bus_index).to_c_str().as_ptr(), O_RDWR, 0)}, 62 | |fd| { Master{fd: Fd::own(fd)} }) 63 | } 64 | 65 | pub fn transaction<'a>(&self, addr: SlaveAddress, messages: &'a[Message]) -> IoResult<()> { 66 | unsafe { 67 | let mut i2c_msgs = Vec::from_fn(messages.len(), |i| { 68 | match messages[i] { 69 | Read(ref buf) => 70 | i2c_msg{addr: addr, flags: M_RD, 71 | buf: transmute(buf.as_ptr()), 72 | len: buf.len() as u16}, 73 | Write(ref buf) => 74 | i2c_msg{addr: addr, flags: 0, 75 | buf: transmute(buf.as_ptr()), 76 | len: buf.len() as u16} 77 | } 78 | }); 79 | 80 | check_syscall_action( 81 | ioctl(self.fd.native, IOCTL_RDWR, 82 | &i2c_rdwr_ioctl_data{msgs: i2c_msgs.as_mut_ptr(), nmsgs: i2c_msgs.len() as u32})) 83 | } 84 | } 85 | } 86 | 87 | pub struct Slave<'a> { 88 | master: &'a Master, 89 | addr: SlaveAddress 90 | } 91 | 92 | impl <'a> Slave<'a> { 93 | pub fn transaction(&'a self, messages: &[Message]) -> IoResult<()> { 94 | self.master.transaction(self.addr, messages) 95 | } 96 | } 97 | -------------------------------------------------------------------------------- /src/pi/mod.rs: -------------------------------------------------------------------------------- 1 | 2 | mod unixio; 3 | 4 | pub mod epoll; 5 | pub mod eventfd; 6 | pub mod gpio; 7 | pub mod i2c; 8 | -------------------------------------------------------------------------------- /src/pi/unixio.rs: -------------------------------------------------------------------------------- 1 | 2 | #![allow(dead_code)] 3 | 4 | extern crate native; 5 | extern crate libc; 6 | 7 | use self::libc::{c_int,close}; 8 | use std::io::{IoResult,IoError}; 9 | 10 | pub struct Fd { 11 | pub native: c_int 12 | } 13 | 14 | impl Fd { 15 | pub fn own(native_fd: c_int) -> Fd { 16 | Fd{ native: native_fd } 17 | } 18 | } 19 | 20 | impl Drop for Fd { 21 | fn drop(&mut self) { 22 | unsafe { 23 | close(self.native); 24 | } 25 | } 26 | } 27 | 28 | pub fn check_syscall(status: c_int, result_fn: |c_int|-> T) -> IoResult { 29 | if status < 0 { 30 | Err(IoError::last_error()) 31 | } 32 | else { 33 | Ok(result_fn(status)) 34 | } 35 | } 36 | 37 | pub fn check_syscall_action(status: c_int) -> IoResult<()> { 38 | check_syscall(status, |_|()) 39 | } 40 | -------------------------------------------------------------------------------- /src/raw-blink.rs: -------------------------------------------------------------------------------- 1 | use std::fmt::Show; 2 | use std::io::{File, IoResult}; 3 | use std::io::timer::sleep; 4 | 5 | fn write_to(path: &str, value: T) -> IoResult<()> { 6 | let mut f = try!(File::create(&Path::new(path))); 7 | write!(f, "{}", value) 8 | } 9 | 10 | fn main() { 11 | write_to("/sys/class/gpio/export", 18u) 12 | .unwrap(); 13 | 14 | write_to("/sys/class/gpio/gpio18/direction", "out") 15 | .unwrap(); 16 | 17 | for i in range(1u, 21) { 18 | write_to("/sys/class/gpio/gpio18/value", i%2) 19 | .unwrap(); 20 | sleep(500); 21 | } 22 | 23 | write_to("/sys/class/gpio/unexport", 18u) 24 | .unwrap(); 25 | } 26 | -------------------------------------------------------------------------------- /src/signals.rs: -------------------------------------------------------------------------------- 1 | 2 | extern crate green; 3 | extern crate rustuv; 4 | 5 | use std::io::signal::{Listener, Interrupt}; 6 | 7 | #[start] 8 | fn start(argc: int, argv: *const *const u8) -> int { 9 | green::start(argc, argv, rustuv::event_loop, main) 10 | } 11 | 12 | fn main() { 13 | let mut listener = Listener::new(); 14 | 15 | match listener.register(Interrupt) { 16 | Err(e) => { 17 | println!("failed to register for interrupt signal: {}", e); 18 | fail!(); 19 | }, 20 | _ => () 21 | } 22 | 23 | loop { 24 | match listener.rx.recv() { 25 | Interrupt => { 26 | fail!("interrupted"); 27 | }, 28 | _ => () 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /tools/gpio-cleanup: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -e 4 | 5 | for f in $(find /sys/class/gpio/ -name 'gpio[0-9]*') 6 | do 7 | echo $(basename $f | cut -c 5-) | tee /sys/class/gpio/unexport 8 | done 9 | --------------------------------------------------------------------------------