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 |
|
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 |
|
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 |
|
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
|
|
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 |
|
The autowrite command forces a save whenever I do a :make, ensuring that I’m compiling what was on my screen.