Tag Archives: programming

ATmega168 documentation uses word addressing, avrdude uses byte addressing

I was programming USBaspBootloader into a metaboard with an Atmel AVR ATmega168 microcontroller. I always got this error:

usb 6-2: device descriptor read/64, error -71
...
usb 6-2: device not accepting address 21, error -71

1. USBaspBootloader’s Makefile specifies atmega168 and BOOTLOADER_ADDRESS=3800 by default. This is in hex addressing a byte, i.e. 0x3800. avrdude uses byte-addressing.

ATmega168 documentation refers to 0x1C00 as the bootloader address. This is in hex addressing a word. 0x3800 / 2 = 0x1C00.

So, when programming a bootloader using avrdude, USBaspLoader’s Makefile should have 3800 (the default), not 1C00.

2. bootloaderconfig.h has features enabled that make the bootloader too big (> 2K). I had to turm them off:

80 #define HAVE_EEPROM_PAGED_ACCESS 0
...
87 #define HAVE_EEPROM_BYTE_ACCESS 0
...
93 #define BOOTLOADER_CAN_EXIT 0

All of this was, of course, done under Linux.

Common Lisp: how to split a program (project) into many files

This here is good if you move from other languages and wish to know fast.

Lousy jailed-lighttpd scripts

Now, jailed has its own automated scripts to generate its own proper fully-contained jails (as far as “jails” on GNU/Linux go, that is; they’re chroots, actually).

The following scripts are leftovers from me trying to do the same, before finding out that the utilities are already there. They have been lying around for quite some time. I’m just copying them here so I can delete them from the home dir.

=====

jailed-lighttpd-install
—–

#!/bin/sh
# Create a jail for lighttpd

jail=/jail/lighttpd

mkdir -p $jail
cd $jail

mkdir tmp
chmod 1777 tmp

mkdir -p        var/log/lighttpd var/run/lighttpd home/http
chown http:http var/log/lighttpd var/run/lighttpd home/http
chmod a+rx      var/log/lighttpd var/run/lighttpd home/http

mkdir etc
cp -avr /etc/php etc/
cp -v /etc/hosts /etc/nsswitch.conf /etc/resolv.conf /etc/services /etc/localtime etc/
cp -v /etc/group /etc/host.conf /etc/passwd /etc/protocols etc/

mkdir -p usr/bin
cp /usr/bin/php /usr/bin/php-cgi usr/bin/

# read l2chroot before use
./l2chroot /usr/bin/php
./l2chroot /usr/bin/php-cgi

# just in case some permissions were forgotten...
chmod a+rx $jail

=====

jailed-lighttpd.rc
—–

#!/bin/bash

# general config
. /etc/rc.conf
. /etc/rc.d/functions

jailroot=/jail/lighttpd
function jailcmd () {
su http -c "$*"
}

PID=`pidof -o %PPID /usr/sbin/lighttpd`

case "$1" in
start)
stat_busy "Starting jailed lighttpd Daemon"
[ -z "$PID" ] && jk_chrootlaunch -j $jailroot -x /usr/sbin/lighttpd -- -f /etc/lighttpd/lighttpd.conf 2>&1
if [ $? -gt 0 ]; then
stat_fail
else
add_daemon lighttpd
stat_done
fi
;;
stop)
stat_busy "Stopping jailed lighttpd Daemon"
[ ! -z "$PID" ] && jailcmd kill $PID &>/dev/null
if [ $? -gt 0 ]; then
stat_fail
else
rm_daemon lighttpd
rm -f $jailroot/var/run/lighttpd/lighttpd.pid
stat_done
fi
;;
restart)
$0 stop
sleep 1
$0 start
;;
*)
echo "usage: $0 {start|stop|restart}"
esac

=====

l2chroot
—–

#!/bin/bash
# Use this script to copy shared (libs) files to Apache/Lighttpd chrooted
# jail server.
# ----------------------------------------------------------------------------
# Written by nixCraft <http://www.cyberciti.biz/tips/>
# (c) 2006 nixCraft under GNU GPL v2.0+
# + Added ld-linux support
# + Added error checking support
# ------------------------------------------------------------------------------
# See url for usage:
# http://www.cyberciti.biz/tips/howto-setup-lighttpd-php-mysql-chrooted-jail.html
# -------------------------------------------------------------------------------
# Set CHROOT directory name
BASE="/jail/lighttpd"

if [ $# -eq 0 ]; then
echo "Syntax : $0 /path/to/executable"
echo "Example: $0 /usr/bin/php5-cgi"
exit 1
fi

[ ! $BASE ] && mkdir -p $BASE || :

# iggy ld-linux* file as it is not shared one
FILES="$(ldd $1 | awk '{ print $3 }' |egrep -v ^'\(')"

echo "Copying shared files/libs to $BASE..."
for i in $FILES
do
d="$(dirname $i)"
[ ! -d $BASE$d ] && mkdir -p $BASE$d || :
/bin/cp $i $BASE$d
done

# copy /lib/ld-linux* or /lib64/ld-linux* to $BASE/$sldlsubdir
# get ld-linux full file location
sldl="$(ldd $1 | grep 'ld-linux' | awk '{ print $1}')"
# now get sub-dir
sldlsubdir="$(dirname $sldl)"

if [ ! -f $BASE$sldl ];
then
echo "Copying $sldl $BASE$sldlsubdir..."
/bin/cp $sldl $BASE$sldlsubdir
else
:
fi

=====

There. Sorry for the tabbing, it went AWOL.

Python and SWIG: local variable ‘_mod’ referenced before assignment

I was doing some stuff that requires importing C++ libraries to use in Python, and that error came up.

I’m using Stackless, so I first thought that was the case. Eventually it turned out to be related to the build configuration of the program I was compiling, FIFE.

The thing here is, SWIG generated a wrapper for that library that had a bad. It was at the very top of the wrapper file (fife.py):

8  from sys import version_info
9  if version_info >= (2,6,0):
10     def swig_import_helper():
11         from os.path import dirname
12         import imp
13         fp = None
14         try:
15             fp, pathname, description = imp.find_module('_fife', [dirname(__file__)])
16         except ImportError:
17             import _fife
18             return _fife
19         if fp is not None:
20             try:
21                 _mod = imp.load_module('_fife', fp, pathname, description)
22             finally:
23                 fp.close()
24                 return _mod
25     _fife = swig_import_helper()
26     del swig_import_helper
27 else:
28     import _fife
29 del version_info

The bad thing here is that there’s no “except:” where it should be. When I added one on line 22, it caught an exception. Technically, the exception should be passed to a higher-level “try:” (on line 14). However:

1. “finally:” is executed in all cases, exception or no exception.
2. The upper-level “try:” doesn’t have a default exception handler.

I didn’t dig deeper into this, because I have no time at the moment.

Simple script to see file sizes

#!/usr/bin/python
# directory size - show it

import os, sys

if len(sys.argv) < 2:
    os.system("du -sh *")
    print "-----"
    os.system("du -sh .")
else:
    for arg in sys.argv[1:]:
        cmd = "du -sh \"%s\"" % arg
        os.system(cmd)