Ardunio Nano V3 Development on Linux

•August 10, 2009 • 1 Comment

After I’ve read an interesting article about Arduino I obtained an “Arduino Nano V3″, which contains already the newer AVR Atmega 328p micro processor. The starter kit contained also a 3 axis accelerometer (ADXL330), a bread board, an LED, push button and so on.
See www.arduino.cc for more information about the arduino itself.

Arduino Starterkit

The nice thing about arduino nano is, that it comes preinstalled with a USB plug so you don’t need to solder yourself. It’s just plug & play and you even do not need a power supply, because it’s powered over USB (external power supply is possible too of course).

The seconds nice thing is that there exists an Open Source IDE, also called “Arduino” which make programming really easy also for people without great programming skills. This IDE is written in JAVA, and so is portable. Underneeth the GUI it uses the AVR-GCC toolchain to compile C and C++ programs and links against AVR-LIBC. The most complicated thing to get it running is building a working AVR toolchain, but Gentoo has this nice “crossdev” script which can generate complete toolchains including compiler, binutils and libc :-)

Installing the AVR Toolchain
To use crossdev you should add the line ‘PORTDIR_OVERLAY=”/usr/local/portage”‘ to your /etc/make.conf.

crossdev -t avr -s4 --without-headers
ln -s /usr/i686-pc-linux-gnu/avr/lib/ldscripts /usr/avr/lib/ldscripts
emerge avrdude -av

There first line executes the crossdev script which compiles the toolchain for AVR (See crossdev –help for more info). The second line creates a missing symlink due to bug 147155. The third line installs avrdude, a tool for downloading programs to the AVR.

If everything succeeded you should be able to run “avr-gcc –version” to check the compiler version. You need at least avr-libc 1.6.4 to support the Atmega328p, but this is already default meanwhile.

Now you can simply download the Arduino IDE from www.arduino.cc, unpack and run it. For compiling you should select the “Arduino Deumilanove w/ Atmega 328″ via the menu Tools->Board. The normal nano configuration has no Atmega 328.

arduino

For downloading you have to select your serial port via Tools->Serial Port. This is /dev/ttyUSB0 normally if it is the first connected tty device.
If you don’t see this device when you plug in the cable you probably don’t have the FTDI driver compiled into your kernel.

Configuring the FTDI USB serial converter
You can activate it via the kernel menu config (you must know how to compile a kernel for that):
Device Drivers -> USB support -> USB Serial Converter support -> USB FTDI Single Port Serial.

To get access to the tty device file you should be in the uucp group. Use “sudo usermod -a -G uucp ” to add the group to a user. This change requires you to relogin to get active.

MSDN website standard compliance

•July 21, 2009 • Leave a Comment

Before you ask, I did this just for fun ;-)

I’m annoyed that the MS sites doesn’t work probably in any browser but IE – what a wonder. I need the site sometimes because I’m developing portable software that also has to run on Windows, and so I often need to figure out the problems where the MS C++ compiler is not standard conform, but that’s another story…

So I asked myself how bad is that standard compliance really. So I put one of the API reference sites into W3C’s markup validator…

The result: 98 Errors, 10 warning(s)

You can recheck that your self using that link Recheck MSDN Site

We cannot loose anything so lets check also the CSS ;-)

The result: Sorry! We found the following errors (18)

Recheck MSDN Site

About the new kernel exploit

•July 19, 2009 • Leave a Comment

I’m reading so much at the moment about the new “exploitable” kernel bug that I need to clarify some things, because many things with this exploit are represented in strange ways that give a wrong impression.

1.) First of all it’s stupid NULL pointer deference. A stupid bug that no programmer should made, but shit happens. The good thing with such a bug is that it is easy to fix, and the bug was already patched on 6th of July by Mariusz Kozlowsk which shows how fast the community reacts on security issues.

diff --git a/drivers/net/tun.c b/drivers/net/tun.c
index a1b0697..bcbb25e 100644
--- a/drivers/net/tun.c
+++ b/drivers/net/tun.c
@@ -482,12 +482,14 @@ static unsigned int tun_chr_poll(struct file *file, poll_table * wait)
 {
        struct tun_file *tfile = file->private_data;
        struct tun_struct *tun = __tun_get(tfile);
-       struct sock *sk = tun->sk;
+       struct sock *sk;
        unsigned int mask = 0;

        if (!tun)
                return POLLERR;

+       sk = tun->sk;
+
        DBG(KERN_INFO "%s: tun_chr_poll\n", tun->dev->name);

        poll_wait(file, &tfile->read_wait, wait);

2.) The bug gets exploitable due to some compiler optimizations. But this is really not a problem in GCC. The optimization makes sense, and the problem occurs only because of this bug. Without this bug the optimization is no problem.

3.) How dangerous is the exploit? Despite some articles I’ve read it’s really hard to use this exploit in reality.

  • First of all you need to have the affected kernel installed with the tun code activated. I don’t even have this tun code activated in my kernel.
  • To execute the exploit the attacker must have physical access to your computer.
  • And the attacker must be able to execute a SETUID program that loads some special kernel modules. For installing such an application and making it SETUID the attacker would already need to have root privileges.

So this is exploit is interesting from an academic point of view, because two different things, a programming bug and a compiler optimization lead to an exploit, but it’s really minimum danger for all us normal Linux users.

GIT bash cheats

•July 9, 2009 • Leave a Comment

When you install GIT you should also install the GIT bash completion with it. (On gentoo you just need to set the bash-completion USE flag when emerging git). This makes your life much easier. This can complete git commands like “git stat[TAB]” to “git status”. Additionally it offers the GIT bash function __get_ps1 for your command prompt. You can integrate this into your “PS1″ variable to show the current branch of your working dir on your command prompt. That’s just great, isn’t it?

Add this to your ~/.bashrc to activate the GIT bash completion and the new command prompt.

# load git bash completion
source /usr/share/bash-completion/git
# Cool trick to show current git branch in the command prompt in gentoo colors.
export PS1='\[33[01;32m\]\u@\h\[33[01;34m\] \w$(__git_ps1 " (%s)") \$\[33[00m\] '

If you enter your working dir the prompt will look like this.

gergap@lt_gergap ~/work/helisim (master) $

If you are wondering what this “\[33...]” is for, this is just to set some nice colors for the prompt. You can remove that if you don’t like colors.

VIM Wild Menu

•July 8, 2009 • Leave a Comment

I was annoyed by the file name completion in VIM. It completes the whole name of the first match, even if there are a lot of possibilities. That feels like DOS :-( so that you need a lot of <TAB> to iterate to the correct file.
I wanted to behave like the BASH to complete only the longest possible part and then show a list of possible completions with the second <TAB>.
No problem in high configurable VIM. I found out that this two settings do what I want.

set wildmode=longest:full
set wildmenu

Does KDE4 on Windows make sense?

•June 9, 2009 • Leave a Comment

I expect there are a lot of opinions to this questions ;-)

KDE4 is another Open Source success story. So why should a user be interested in running it on Windows, when there are free alternatives like Linux? Is it not one step back? By only supporting free operating systems this would be another reason for Windows users, that want to use the cool KDE apps, to switch to free operating systems like Linux. So why are Open Source developers are interested in porting it to Windows?

A lot of good questions, and in my opinion all this is right, but there is this KDE4 Windows port and a lot of Windows users are already using it. So for some users it seems to make sense. One reason I heard is that some users are forced to use Windows in their job and so have no choice. With KDE4 for Windows they can now use all the cool KDE applications, that they are used to use from home and are not limited to the applications that come with Windows.

Open Source means to be free. Let you do with it whatever you want, as long as the result of your work offers the same freedom to all other users. Thus it allows you also to port it to Windows and let it run on a proprietary operating systems.
So at the end – if people like it or not – KDE4 on Windows is also part of the freedom of Open Source.

Howto recursively replace file headers of source files

•June 3, 2009 • 2 Comments

This scripts can replace file headers from C/C++/JAVA header and source files. Often it’s necessary to replace the license conditions in multiple source files, update the copyright year, or simply make all file headers consistent. This can be a lot of work, so that’s why I wrote this scripts.

How it works: The BASH script replace_header.sh is a simple wrapper that executes the AWK script remove_header.awk which implements a basic state machine for recognizing C/C++/JAVA comments (“// …”, “/* … */). All comments at the beginning of the file – also when spanning over multiple lines – are ignored until the first line without comment is reached. The AWK file ignores all this comments, and prints the rest of the file unchanged. The BASH script creates a new file with your new header.template and adds your code without the old file header to the newly created file. Afterwards it copies the new file over the old one.

Example:

find . -name "*.h" -exec ~/rh/replace_header.sh {} \;

Download replace_header_scripts.tar.gz

VIM paste text with line numbers

•June 1, 2009 • 1 Comment

I normally put my code from VIM with syntax highlighting and line numbers turned on on my blog. This is good for readability, but bad if you want to copy paste it from the web site. With vim it’s easy to remove these line numbers from pasted code. Here is how to do that.

  1. Select the code on the website (no CTRL-C/CTRL-V is needed on Linux, selecting does already the copy)
  2. Start up vim and enter :set paste. This turns off the automatic indentation, to allow keeping the indentation of the pasted code.
  3. Press “i” to change to insert mode.
  4. Now click with the middle mouse button into the vim window. That inserts the code.
  5. Press “Esc” to change back to command mode.
  6. Now use vim’s regexp based substitute and replace command to remove the line numbers: :%s/^[0-9 ]\d \=//

New VIM color scheme wombat256

•June 1, 2009 • Leave a Comment

I found this nice GVIM color scheme from Lars H. Nielsen called “wombat”. I wanted to use this also in normal vim, because I prefer working on the console over the graphical vim. For that I needed to convert the scheme to work in 256 color mode of xterm or KDE’s konsole. I found this site very useful to understand why and how to convert the color schemes.
Here are a screenshot and the scheme “wombat256″. To include it store the wombat256 scheme in ~/.vim/colors and add this lines into your .vimrc.

" turn syntax highlighting on
set t_Co=256
syntax on
colorscheme wombat256

VIM Wombat256
Wombat256:


 1 " Author: Gerhard Gappmeier <gerhard.gappmeier@ascolab.com>
 2 " Description: 256C Scheme for console based VIM
 3 "   Based in wombat GVIM scheme from Lars H. Nielsen (dengmao@gmail.com)
 4 " Last Change:  2009-06-01
 5
 6 set background=dark
 7
 8 hi clear
 9
10 if exists("syntax_on")
11   syntax reset
12 endif
13
14 let colors_name = "wombat256"
15
16 " Vim >= 7.0 specific colors
17 if version >= 700
18   hi CursorLine ctermbg=236 
19   hi CursorColumn ctermbg=236  
20   hi MatchParen ctermfg=7    ctermbg=243   cterm=bold
21   hi Pmenu      ctermfg=7   ctermbg=238 
22   hi PmenuSel   ctermfg=0     ctermbg=186  
23 endif
24
25 " General colors
26 hi Cursor       ctermfg=0    ctermbg=241     cterm=none
27 hi Normal       ctermfg=7    ctermbg=235   cterm=none
28 hi NonText      ctermfg=244     ctermbg=236    cterm=none
29 hi LineNr       ctermfg=243  ctermbg=0       cterm=none
30 hi StatusLine   ctermfg=7     ctermbg=238    cterm=none
31 hi StatusLineNC ctermfg=243  ctermbg=238     cterm=none
32 hi VertSplit    ctermfg=238    ctermbg=238   cterm=none
33 hi Folded       ctermbg=238  ctermfg=248     cterm=none
34 hi Title        ctermfg=7  ctermbg=0       cterm=bold
35 hi Visual       ctermfg=7     ctermbg=238    cterm=none
36 hi SpecialKey   ctermfg=244    ctermbg=236   cterm=none
37 hi DiffAdd      ctermfg=0    ctermbg=113     cterm=none
38 hi DiffChange   ctermfg=0    ctermbg=175     cterm=none
39 hi DiffDelete   ctermfg=0    ctermbg=17      cterm=none
40 hi SpellBad     ctermfg=0    ctermbg=161     cterm=none
41
42 " Syntax highlighting
43 hi Comment      ctermfg=246     ctermbg=235
44 hi Todo         ctermfg=245    cterm=italic
45 hi Constant     ctermfg=173     cterm=none
46 hi String       ctermfg=113  cterm=none
47 hi Identifier   ctermfg=186   cterm=none
48 hi Function     ctermfg=186     cterm=none
49 hi Type         ctermfg=186    cterm=none
50 hi Statement    ctermfg=117    cterm=none
51 hi Keyword      ctermfg=117  cterm=none
52 hi PreProc      ctermfg=173     cterm=none
53 hi Number       ctermfg=173   cterm=none
54 hi Special      ctermfg=194  cterm=none
55
56

Minimal .vimrc for C/C++ developers

•May 29, 2009 • 1 Comment

VIM is a very powerful editor, and a standard developing tool for Linux users. Meanwhile VIM is also available for Windows. The good thing is, it is very customizable and can do almost everything. The bad thing is, by default most features are turned of, because it depends on your use case of the editor, what features should be turned on or off.

I’m a C/C++ developer, so I configured it for developing. Just store this file in your home directory or add it to your existig .vimrc. VIM will read this file at startup and configure it for C/C++ developing.

What this configuration offers:

  1. Visual: Syntax-highlighting and line numbers
  2. Editor Configuration: tab expanding to space, tabwidth 4, linewrap at 120 characters, C indentation, simple save file using [F2] (works in insert mode and normal mode)
  3. Navigation: Goto-Definition with [F12]. The word under the cursor is looked up using the ctags index file.
  4. Autocompletion: Therefore omnicppcomplete has to be installed additionally. See comments in .vimrc.
  5. [F5] automatically generates the tags file for the current folder.
  6. Intelligent Comments: Just type /* Text. The next * will be inserted automatically. / closes the comment automatically.
  7. Doxygen Comments: Creating doxygen comments for function under cursor using [F6]. Therefor DoxygenToolkit needs to be installed. See comments in .vimrc.
  8. Spell Checking using vim-spell: Use Alt-Down/up to jump from own spelling error to the next/previous. Use zg to add a good word, z= to get suggestion.

Another thing that works out of the box: Press SHIFT-K to lookup the function under cursor in the manpage – that’s the integrated help :-)

Vim with OmniCppComplete

Vim with OmniCppComplete


 1 " VIM Configuration File
 2 " Description: Optimized for C/C++ development, but useful also for other things.
 3 " Author: Gerhard Gappmeier
 4 "
 5
 6 " set UTF-8 encoding
 7 set enc=utf-8
 8 set fenc=utf-8
 9 set termencoding=utf-8
10 " disable vi compatibility (emulation of old bugs)
11 set nocompatible
12 " use indentation of previous line
13 set autoindent
14 " use intelligent indentation for C
15 set smartindent
16 " configure tabwidth and insert spaces instead of tabs
17 set tabstop=4        " tab width is 4 spaces
18 set shiftwidth=4     " indent also with 4 spaces
19 set expandtab        " expand tabs to spaces
20 " wrap lines at 120 chars. 80 is somewaht antiquated with nowadays displays.
21 set textwidth=120
22 " turn syntax highlighting on
23 set t_Co=256
24 syntax on
25 colorscheme wombat256
26 " turn line numbers on
27 set number
28 " highlight matching braces
29 set showmatch
30 " intelligent comments
31 set comments=sl:/*,mb:\ *,elx:\ */
32
33 " Install OmniCppComplete like described on http://vim.wikia.com/wiki/C++_code_completion
34 " This offers intelligent C++ completion when typing ‘.’ ‘->’ or <C-o>
35 " Load standard tag files
36 set tags+=~/.vim/tags/cpp
37 set tags+=~/.vim/tags/gl
38 set tags+=~/.vim/tags/sdl
39 set tags+=~/.vim/tags/qt4
40
41 " Install DoxygenToolkit from http://www.vim.org/scripts/script.php?script_id=987
42 let g:DoxygenToolkit_authorName="Gerhard Gappmeier <gerhard.gappmeier@ascolab.com>" 
43
44 " Enhanced keyboard mappings
45 "
46 " in normal mode F2 will save the file
47 nmap <F2> :w<CR>
48 " in insert mode F2 will exit insert, save, enters insert again
49 imap <F2> <ESC>:w<CR>i
50 " switch between header/source with F4
51 map <F4> :e %:p:s,.h$,.X123X,:s,.cpp$,.h,:s,.X123X$,.cpp,<CR>
52 " recreate tags file with F5
53 map <F5> :!ctags -R –c++-kinds=+p –fields=+iaS –extra=+q .<CR>
54 " create doxygen comment
55 map <F6> :Dox<CR>
56 " build using makeprg with <F7>
57 map <F7> :make<CR>
58 " build using makeprg with <S-F7>
59 map <S-F7> :make clean all<CR>
60 " goto definition with F12
61 map <F12> <C-]>
62 " in diff mode we use the spell check keys for merging
63 if &diff
64   " diff settings
65   map <M-Down> ]c
66   map <M-Up> [c
67   map <M-Left> do
68   map <M-Right> dp
69   map <F9> :new<CR>:read !svn diff<CR>:set syntax=diff buftype=nofile<CR>gg
70 else
71   " spell settings
72   :setlocal spell spelllang=en
73   " set the spellfile - folders must exist
74   set spellfile=~/.vim/spellfile.add
75   map <M-Down> ]s
76   map <M-Up> [s
77 endif
78