Information arrives in bits

\(H(X) = - \sum\limits_{i=1}^n P(x_i) \log P(x_i)\)

1. Makefiles FTW

WinAVR Portable setup has been updated and placed in a separate page for easier reference.

1.1. Basic make tutorial

# anatomy of a "rule":
targets: prerequisites
<Tab>command
<Tab>command

# Super important that the command is indented
# using an actual Tab character `\t` instead of spaces!

make reads the file named Makefile (the default name) and collects all of the rules.

make either starts with the named target on the command line, or uses the default target (defined as the first one in the Makefile).

It looks at the file modification times of the target and its prerequisites. Each prereq may itself have prerequisites as described in the Makefile.

If any prerequisite is newer (according to the filesystem’s modification time), then make runs the command(s) for each rule to re-generate the target.

Here is an example form the Makefile that helps to generate these pages:

# get all files matching the pattern
SOURCE=$(wildcard *.adoc)

# New variable with values created by replacing each foo.adoc file
# name with foo.html
HTMLS=$(SOURCE:.adoc=.html)

# ruby bundler manages the program version
ASCIIDOCTOR=bundle exec asciidoctor

# appending to a variable
ASCIIDOCTOR_OPTS=
ASCIIDOCTOR_OPTS+=-a imagesdir@=fig
ASCIIDOCTOR_OPTS+=-a scriptsdir@=js
ASCIIDOCTOR_OPTS+=-a stem@=latexmath
ASCIIDOCTOR_OPTS+=-a stylesdir@=css
ASCIIDOCTOR_OPTS+=-a toc@=left


# First target, so it's the default by *position*
default: html


# Phony target that auto-renders the source files if they change
# and starts a webserver to show them in a browser.
.PHONY: dev
dev:
	(ls *.adoc *.md | entr make & python -m http.server)


# Phony target depends on all the .html files to be generated,
# plus files included by the pages.
.PHONY: html
html: $(HTMLS) $(RESOURCES)


# How to make a single .html from the original .adoc source file
%.html: %.adoc
	SOURCE_DATE_EPOCH=$(shell git log -1 --pretty=%ct) \  # last-modified date
	    $(ASCIIDOCTOR) \  # the tool itself
	    $(ASCIIDOCTOR_OPTS) \  # options
	    $<

# The .html files can be regenerated at-will, so they don't need to be saved.
clean:
	rm $(SOURCE:.adoc=.html)

Makefile Tutorial ← this is quite good!

GCC’s -M options for helping to automatically extract dependencies.

1.2. Your Task

Create a Makefile for managing the build (.c to .hex) and upload (avrdude command) process for code for an ATtiny85.

day13 shows all of the commands the Arduino IDE uses to compile, assemble, and link source code, and to generate a .hex file for programming with avrdude.

Below is the Makefile for the solution to the blinkfast task from Lab 1. This was programmed in assembly (only), assembled by gavrasm, the command-line assembler that is included inside avr_sim.

Makefile for the project blink-fastest
SOURCE=main.asm
INCLUDES=manytoggles.asm
HEX=$(SOURCE:.asm=.hex)

AVRDUDE_OPTS=
AVRDUDE_OPTS+=-V
AVRDUDE_OPTS+=-c stk500v1
AVRDUDE_OPTS+=-p attiny85
AVRDUDE_OPTS+=-b 19200
AVRDUDE_OPTS+=-U flash:w:main.hex
#AVRDUDE_OPTS+=-P COM3
AVRDUDE_OPTS+=-P /dev/ttyUSB1

default: hex


hex: $(HEX)

# howto do specific actions

# assemble
%.hex: $(SOURCE) $(INCLUDES)
	gavrasm -W -B -E $< 

# upload to flash
flash: hex
	avrdude $(AVRDUDE_OPTS)

2. Porting ShortSquawker to C99

This is the task of Lab 3.

  • Use avr-gcc option -std=c99 to use the 1999 C standard, which is common in the embedded systems industry.

  • Turn on all warnings, and trigger none with your code:

    • -Wall -Wextra -Werror

    • -Wpedantic — some extra warnings required by the C standard

    • -fanalyzer to enable static-analysis warnings?

  • Create a Makefile to manage all repetitive tasks.

  • Use WinAVR Portable

  • Rewrite function void debug_value(uint16_t) in assembly to ensure that each bit interval is exactly the same.

3. avr-gcc references

Information about the tools to compile, assemble, and manipulate code for the AVR architecture is provided across several locations.

WinAVR Portable setup has been updated and placed in a separate page for easier reference.

First is information for the specific devices and the instructions available.


The compiler is separate from the standard libraries.