Tuesday, March 17, 2015

Print the current tag OR commit in git

Here's the command to print the tag name of the current commit, or just the commit if there is no tag. It's useful for a script that builds or exports something and you always want to have some version reference..
# prints tag name or short revision
git name-rev --tags --name-only --no-undefined --always $(git rev-parse HEAD)

Monday, March 16, 2015

Multiple test programs, one makefile

I've been writing tests using glib's test framework. Some of them got quite large, so I split them up into multiple files with their own main()'s. Here's my makefile to build and test up to 10 test binaries in one go:
T:=$(patsubst %.c,%,$(filter tests-%,$(wildcard *.c)))  # make all test progs
#T=test1 test2  # or list them by hand
OBJECTS:=$(patsubst %.c,%.o,$(wildcard ../*.c))  # CUT is back one dir
#OBJECTS=mysource1.o mysource2.o  # you can also just list these too

CFLAGS=-Wall -Werror `pkg-config --cflags glib-2.0`
CFLAGS+=-D_GNU_SOURCE -D_BSD_SOURCE  # asprintf, strdup
LDLIBS=`pkg-config --libs glib-2.0`
CC=gcc -std=c99

TFLAGS=--quiet  # suppress per test binary output
#TFLAGS=--verbose  # report success per test case

.PHONY: all
all: $(T)

.PHONY: rebuild
rebuild: clean all

.PHONY: check
check: $(T)
 @$(TEST) $(TFLAGS) $(T) && echo "ALL OK"

.PHONY: clean
 rm -f $(OBJECTS) $(T)

$(T): $(OBJECTS)
Now it's just make check and you get a simple "ALL OK", or a big ugly error. By the way, make sure all the rule definitions start with actual tabs, make doesn't like spaces..

Friday, November 14, 2014

git lsd – For when you don't want to use gitk --all

If all you want is a quick look at your commit history with all the branches nicely laid out with their merges shown with pretty colored lines... Try this instead of one of the gui viewers:
git log --graph --boundary '--format=%C(yellow)%h%Creset%C(bold cyan)%d%Creset %s %C(dim normal)(%ar)%Creset' --all
This will output something like this (depending on your terminal color settings):
* 4b22a90 (HEAD, master) erases chip before programming (2 days ago)
*   ee107af Merge branch 'sysclk' (2 days ago)
| * e9df6f6 removes clk config to use defaults (2 days ago)
| * 66a1ea3 fixes clkout pin timing (2 days ago)
| * bcdda54 enables PLL @ 16MHz (internal 2MHz x 8) (2 days ago)
| * cd22bab enables the 32KHz rc for system clock (2 days ago)
| * cee8efd enables the internal 32MHz rc for sysclk (2 days ago)
| * aa10b4d enables 2MHz internal rc osc for sysclk (2 days ago)
| * 80cbade re-defines F_CPU (2 days ago)
*   0644c9d Merge branch 'diagnostics' (3 days ago)
| * b9e155d adds SYSCLK/10000 on pin A0 (3 days ago)
| * 57807ab adds blinky led (3 days ago)
* 5cfc996 basic skeleton (3 days ago)
Of course you can mess with the format and colors, but this is how I like it. Once you have it set the way you like it, alias it for later use!
git config --global alias.lsd "git log --graph --boundary '--format=%C(yellow)%h%Creset%C(bold cyan)%d%Creset %s %C(dim normal)(%ar)%Creset' --all"
Now you can just type git lsd and have a quick overview of your commits.

Wednesday, October 15, 2014

Xmega DFU Programming in Linux

I'm using elementary OS 0.2 and have an MT-DB-X4 development board from MattairTech with an ATxmega32A4U — so you may need to adjust the following commands to suit your particular setup.

Install dfu-programmer

First we need to install some prerequisites:

$> sudo aptitude install libusb-1.0-0 libusb-1.0-0-dev

Then, download and extract the source:

$> wget -qO-  "http://sourceforge.net/projects/dfu-programmer/files/dfu-programmer/0.7.0/dfu-programmer-0.7.0.tar.gz/download" | tar xvz

Now configure, make, make install:

cd dfu-programmer-0.7.0
./configure --prefix=$HOME/.local  # This will install to your home
make                               # Compile
make install                       # Install

Now we can test it out. Plug in your device and run the following (change the device to match your chip):

$> sudo ~/.local/bin/dfu-programmer atxmega32a4u get
Bootloader Version: 0x04 (4)

Finally, we need to add ~/.local/bin to our PATH so we can just type dfu-programmer:


# check to see if it's already in PATH
echo $PATH | grep -v "$HOME/\.local/bin" > /dev/null &&
cat << EOF >>~/.profile

# set PATH so it includes user's private .local/bin if it exists
if [ -d "\$HOME/.local/bin" ] ; then

Remember, you need to logout and back in before the changes to your groups and .profile take effect.

Setup udev rules and permissions

When you first plug the device in, udev will only allow access to the root user by default. To change this we need to let udev know that we want to give access to the device to users in a certain group — plugdev in this case.

To start, check to see if you're already in the plugdev group:

$> groups
If not, you'll need to add yourself:
$> sudo usermod -a -G plugdev $USER

Next, we need to add a rules file to tell udev which group to add the device to. To get the device info, run lsusp to get the vendor and product IDs:

$> lsusb | grep Atmel
This should output something like the following:
Bus 005 Device 003: ID 03eb:2fe4 Atmel Corp.
Note the first and second parts of the ID

Now that we have the IDs, we can make the udev rule file and restart udev:


sudo bash -c 'cat << EOF >/lib/udev/rules.d/12-atmel.rules
# atxmega32a4u with DFU Bootloader
SUBSYSTEM=="usb", ACTION=="add", ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="2fe4", GROUP="plugdev"
service udev restart'

Now just unplug and replug your device and you should be able to access it without root:

$> dfu-programmer atxmega32a4u get
Bootloader Version: 0x04 (4)

Tuesday, October 14, 2014

Installing HL2270-DW on Ubuntu / elementary Linux

Download and install these packages:


wget --output-document=cupswrapperHL2270DW-2.0.4-2.i386.deb 'http://goo.gl/mw87o6' && wget --output-document=hl2270dwlpr-2.1.0-1.i386.deb 'http://goo.gl/8ixmvt'

sudo dpkg -i cupswrapperHL2270DW-2.0.4-2.i386.deb hl2270dwlpr-2.1.0-1.i386.deb

Follow the instructions from the Archwiki Page:

  • Open a web browser to http://localhost:631/
  • Click the Administration tab then click the Add Printer button. The username is "root" and the password is the root password of the user's computer.
  • Select IPP from the list.
  • In the 'Connection' field, type ipp://THE_PRINTER_IP/ipp/port1
  • In the next form, give the printer a unique name (no spaces and the name be must unique from any identical printers), and select "Brother" from the printer make field.
  • Select "Brother HL2270DW for CUPS (en)" from the list of drivers (it is not in numerical order and is toward the bottom of the list).
  • Configure the default options on the next page to your liking

Monday, October 13, 2014

Adjusting the system time in a VirtualBox Guest

This is only useful in a couple situations, but very handy if you need it. The guest OS must be unlocked, so you'll need to shut it down before you do this.

# First get the name of the vm you need to adjust:
VBoxManage list vms

# Then, adjust the time in milliseconds (neg nums go back in time):
VBoxManage modifyvm $MY_VM --biossystemtimeoffset -32000000000  # about a year

# To get it back on current time, just set it to zero:
VBoxManage modifyvm $MY_VM --biossystemtimeoffset 0

Wednesday, October 1, 2014

Setting Up the AVR Toolchain for Linux for AVR Development

Since I'm running an old version of Ubuntu (12.04), the AVR development tools in the repository are too old to handle the new ATxmega chips. So here's how to get up and running super quick with the AVR dev toolchain without reinstalling linux or compiling them yourself...
  1. Download the Atmel AVR Toolchain 3.4.x for Linux
  2. Extract it to either /usr/local/avr or ~/local/avr
    mkdir -p ~/local/avr
    cd ~/local/avr
    tar zxvf ~/Downloads/avr8-gnu-toolchain-*.tar.gz
    mv avr8-gnu-toolchain-* avr
  3. Add the path to your ~/.profile:
    # add avr toolchain to path (search ~/local/avr then /usr/local/avr)
    if [ -d "$HOME/local/avr/bin" ] ; then
    if [ -d "/usr/local/avr/bin" ] ; then
    This will look for avr commands in your home first and then in /usr/local/avr, and only if they exist.
  4. Now logout, login and then check to see if its working:
    $ which avr-gcc
    This should output /home/$USER/local/avr/bin/avr-gcc, or wherever you put it.