Sean’s Obsessions

Sean Walberg’s blog

Another Reason VI Rocks

I’ve recently started playing with PIC microcontrollers at home, and have found some more of vi’s features saving me time. I can’t believe I haven’t used this before, the ability to run makefiles from within an editor session, and have vi interpret the output. Like programming in C, Makefiles can be used to automate the building of a project. An .asm file is written by the programmer, which is compiled by an assembler into a .hex file. The .hex file contains the opcodes and memory locations to be burned to the PIC by the programmer. So, the makefile I wrote is

1
2
3
4
5
6
rs232.hex:      rs232.asm
    gpasm -a inhx8m rs232.asm
burn:
    picprog --pic-serial-port=/dev/ttyS1 --burn  -i rs232.hex
clean:
    rm *.hex *.lst *.hx? *.cod

The first line says that the .hex file depends on the .asm file, so that if the latter is newer, use gpasm to build the .hex. The burn target doesn’t depend on anything, so it has to be called manually to use picprog to copy the .hex file to the pic. Finally, I have a clean target the gets rid of some of the temporary files.

The process then, to get my code to the PIC is:

1
2
make
make burn

On to the vi part… vi has a build in command to execute a Makefile in the current directory. Surprisingly enough, type :make and it will be build. Similarily, :make burn can be used to burn the PIC without leaving your vi window. The real value comes in when you get vi to interpret the output. If there is an error in my code, gpasm says something like:

1
2
3
gpasm -a inhx8m rs232.asm
rs232.asm:114:Error [157] Unknown opcode "abcf"
make: *** [rs232.hex] Error 1

The 114 is the line number the problem is on. Wouldn’t it be nice if vi would keep track of the errors, and jump to the appropriate line number? Quickfix to the rescue. By default, vi is set up to understand gcc’s output, which is different from gpasm’s. gpasm can be understood with:

1
setlocal errorformat=%f:%l:Error\ %m,%f:%l:Message\ %m

You can put that in your .vimrc or create a compiler file, mine is /usr/share/vim/vim61/compiler/gpasm.vim. Then, :compiler gpasm causes it to be loaded. When you run a :make from within vi, it learns the line numbers that have errors or warnings. Lots of commands are available, read the Quickfix docs for the complete list, but the common ones are: :cn - go to the next error :cp - go to the previous error :cc N - go to error # N :clist - show all the errors So, with the gpasm.vim file in place, I put the following in ~/.vimrc

1
2
:set autowrite
:compiler gpasm

The autowrite command forces a save whenever I do a :make, ensuring that I’m compiling what was on my screen.

Comments

I’m trying something new here. Talk to me on Twitter with the button above, please.