mercoledì 13 giugno 2012

How to setup a PIC development environment in FreeBSD

Abstract

Setting up a Microchip's PIC microcontroller development environment in FreeBSD is very easy because most of the packages we need are already in the ports. In this post I'll show you which packages you should install in order to develop, compile and write your code into your microcontroller. Then I'll show you how to configure your IDE (Piklab) to compile applications and flash your PIC.
This isn't a C tutorial, if you are looking for a guide to learn how to program in C you may search on Google.

Warning

First of all, the use of pk2cmd in FreeBSD is "very experimental" (as you can read on ReadmeMakefile.txt file). Some features, such as multi-programmers support or firmware updates, are not supported or require a different OS . So please use it carefully.

Packages

The packeges required for setting up the environment are:
  • sdcc, a small c compiler for microcontrollers, is available in the ports
  • pk2cmd, the official Microchip programmer, can be downloaded here
  • Piklab, an IDE written in QT, is also present in the ports tree
In addition, there are some "hardware" requirements:
  • a PIC programmer, I used the Microchip's Pickit II
  • a development board or an ICSP circuit 
  • a PIC microcontroller (go figure!), in my case I used a PIC16F877A

Installation

The install phase requires root privileges in order to write files in system folders. By typing:
# cd /usr/ports/lang/sdcc && make install clean 
the port will install the SDCC compiler.
So, to fetch and install the Piklab type:
# cd /usr/ports/devel/piklab && make install clean 
pk2cmd isn't in the ports tree, so I had install it from a tarball. In the download directory, run:
tar xvf pk2cmdv1.20LinuxMacSource.tar.gz 
The next step will show you how to compile the source:
cd pk2cmdv1.20LinuxMacSource/ && gmake freebsd 
and to install, it run (root privileges required):
# gmake install

Configure

Now let's see how to configure the environment ,so that a non-root user can easily access to the PIC programmer. First of all, we had to configure devfs for read and write permissions in the USB devices.
As root, add the user to the operator group, typing:
# pw groupmod operator -m $USERNAME 
Edit the /etc/devfs.rules file (create it if it doesn't exist) and add these lines:
[system=10]
add path 'usb/*' mode 0660 group operator
To load the rules at the startup, add this line at /etc/rc.conf file
devfs_system_roleset = "system" 
Then restart devfs by running
# /etc/rc.d/devfs restart

Piklab is a complete IDE for microcontroller developing, but out-of-the-box it doesn't support Pickit II. Next step I'll show how to configure a custom programmer to use pk2cmd.
Run piklab and open Setting -> Configure Programmer, select  Custom Programmer and add these lines:
read pk2cmd -PPIC%DEVICE -B/usr/share/pk2/ -GF %O
erase pk2cmd -PPIC%DEVICE -B/usr/share/pk2/ -E
program pk2cmd -PPIC%DEVICE -B/usr/share/pk2/ -F %O -M
verify pk2cmd -PPIC%DEVICE -B/usr/share/pk2/ -F %O -Y
blank check pk2cmd -PPIC%DEVICE -B/usr/share/pk2/ -C
run pk2cmd -PPIC%DEVICE -B/sr/share/pk2/ -T
stop pk2cmd -PPIC%DEVICE -B/usr/share/pk2/ -R
save by clicking OK.
Create a new project (Project->New Project) as you can see in Fig.1
Fig.1 - New Project form
Now clicking on the project Piklab will open the project setting window, then select "Toolchain" tab. In "Compiler" add a new include folder writing:
/usr/local/share/sdcc/non-free/include/ 
in "Include directories", then click "Add" (Fig. 2)
Fig.2 - Compiler Tab
Select "Linker" tab and in "Custom options" write:
-L/usr/local/share/sdcc/non-free/lib/pic14 
Pay attention to the last folder's name, it is the PIC16F877A's folder. With other PICs it may be different.
Then save by pressing OK.
Fig.3 - Linker Tab

And that's all falks! Now Piklab is able to compile and store your programs on the microcontroller.

Conclusions

In this post I showed you how to set up a complete PIC development environment in FreeBSD. Piklab has some features that simplify your work. For example its "Config generator", available in "Tools" menu, generate the code to configure your PIC.

Nessun commento:

Posta un commento