Linux Guide to using SDCC compilers for PIC microcontroller

SDCC or Small device C compiler is an amazing open source compiler. This guide covers the necessary steps needed to setup the SDCC compiler for PIC microcontroller. SDCC has several benefits over the commercial licensed compilers. Firstly, It is free. Secondly, SDCC is retargetable compiler. That is, a wide range of controllers can be compiled using the same compiler. So, once you get comfortable with this compiler it becomes easier to switch between controllers. 

The only downside of SDCC is that there is a slight learning curve in order to set the compiler. But worry not. This guide will also solve this issue for you. This guide is for PIC controller. Compilers for other controllers like Intel can be setup using similar steps.

This blog takes you through the steps needed to install the Compiler in a Linux environment. It then explains how to write a program for the controller. Finally, files associated with the compilation steps are described.

SDCC Compiler setup

In order to setup, there are two steps. First, we shall install the tools like the assembler, linker and librarian. SDCC compiler uses these tools to compile the C code. Secondly, we have to install the SDCC compiler itself. 

Installing GPUTILS

GPUTILS is a group of free software whose main contents include an assembler, linker and a librarian. This shall form the compiler tool chain components for SDCC. SDCC utilizes these utilities to compile the software written in C. Instead of using the GPUTILS, SDCC can be set up to use another assembler and linker from Microchip, such as MPASM and MPLINK.

GPUTILS installs a set of Linker files for each PIC that it supports. The linker file has information about the memory regions in a device. It is used by the linker to put the code at the proper location in the PIC. It also includes a set of .inc files. These files have the EQU statements for each of the special function registers. These facilitate in programming in assembly language as one does not have to remember the SFR addresses and instead can refer to the locations using names. These are equivalent to #define in C language.

As this guide is for setting up the free compiler we shall be using GPUTILS instead of MPASM.

  1. Download the latest copy of GPUTILS can be going to this link. Click on the Download Snapshot button on the top right. 
  2. This should download a zip folder by the name gputils-code-<branchname>.zip. 
  3. Uncompress the folder.
  4. Using a terminal navigate to gputils folder in the extracted folder.
  5. Run the following commands in the terminal
sh ./configure
make
make install

SDCC Installation:

SDCC or Small Device C Compiler compiles the code. In this section we shall configure SDCC to use the GPUtils installed in the previous steps.

  1. Go to the link here.
  2. Choose the latest version available.
  3. Download the version which does not have -rc- with it.
  4. Extract the folder.
  5. Using a terminal go to the folder

During one of the steps on installation of SDCC it requires the location of GPUTILS. It uses this location to look for the assembler, linker and other tools. Along with all these tools it also generates include files for the devices. The include files are generated using the .inc files in the GPUTILS. These include files faciliate C programming the same way .inc helps in assembly. Along with the include files it also generates libraries for each device. The libraries have a declaration of symbols of SFRs which are defined in the header file. This libraries will be used during the linking process and helps the programs to find the necessary symbols. These libraries and header files will be saved in a folder at …/share/sdcc/non-free/lib.

Along with these libraries SDCC installation generates another set of libraries and header files. These are located at …/share/sdcc/lib. These libraries are include the startup codes that the compiler inserts by default with every program or libraries to add functionalities like float handing or multiplication which are not present in the controller by default.

PIC primer:

PICs internally have two kinds of memory.

  1. Data memory: Also called the RAM. This part of the memory is used to store data temporarily during computation. These are volatile memory and a part of the RAM address is also called Special Function Register. The SFRs are not used for storing random data like the rest of the RAM but it stores data which alter the functionality of the controller itself. Some of the interesting ones include the registers to control the timers and to control the PORTS of the controller. Program Counter is one very important Register which keeps track of which instruction address the controller is currently executing.
  2. Program memory: Also called as the code memory. This is the flash ROM which resides inside the controller and this is where the program that is executed by the controller resides. These have some important regions itself. It has some addresses called the reset vector and the interrupt vector. These are the locations where the Program Counter (PC) points in the events of Resets or Power on or Interrupt and hence appropriate code should be present there.

Some other memories

  1. Configuration Registers: These are implemented similar to the flash memory and has functions similar to SFR in controlling the device. The difference between SFR and these being that these can have programmed values during poweron and SFR values need to be set in the program as it resides in the RAM.
  2. EEPROM: A few bytes of data is provided in EEPROM. This can be used to storing data which can be used to remember data across power ons.
  3. Stack: There is a 31 level stack not directly accessible. It is used by PIC for storing Program Counter during function calls.

Writing Programs:

This part is fairly simple for most of the part. It involves writing lines of codes using C. Most of the times one would be interested to use SFRs. This can be simplified by including the appropriate header file for the pic. This basically means using #include <pic18f2550.h>. This will bring all the SFR variables into the workspace.

There are instances when one might need some extra features. There are few of such features which are already available with SDCC. Some of the functions like delay routines and math libraries are provided with SDCC. The header files are present in …/share/sdcc/include/pic16. It has to be linked with the appropriate library otherwise the compiler will complain of missing symbols. Inorder to link one can specify the path to the libraries in IDEs linker path.

Apart from the basic features there are some additional features.

  1. Storage specifiers: These keywords can be added before the datatypes. The modify the location where the data is storage. They are __near, __far, __code, __sfr
  2. Absolute addressing: This keyword is used to save the data at a particular address location. Keyword __at is used for this.
  3. Volatile: This keyword tells the compiler not to remove the reference to the variable.
  4. __reentrant: This makes the function reentrant. ie. Calling the function again before the function finishes execution still keeps the function execution valid.
  5. #pragma nooverlay: This tells the compiler not to overlay the variables used by this function. This can be used in one of the two cases
    1. If ISR calls a non reentrant function
    2. Inline assembler code calls a C function
  6. __critical: This is added next to a function definition and it tells the compiler to disable all interrupts on entering the function or the section
  7. __naked: This when added to the definition of the function tells the compiler not to add prologue or epilogue. This means that the function will not be responsible to save the Status, WREG and other important registers.

Linking with libraries:

During the linking the symbols in the libraries are linked with the main code. It also links with crt0i.o object file. This contains the startup code. The startup code is used by the compiler to create and maintain stacks and other sections.

Important files created during the compilation process:

  1. .asm: Assembly file created from the c code.
  2. .lst: This is the list file and it shows the linking with the other symbols along with the assembly code.
  3. .map: This file is important and gives a summary of location of the codes in the memory.
  4. .o/.rel: This are the object files. These are linked to create the final hex file.
  5. .hex: This is the final hex file and the product of compilation process.




0



0

Share with others:

Read Similar Tags:

Like what you read?

Consider providing a review for the post or the website by clickling Submit Review below.

Comments

Leave a Reply

Only people in my network can comment.
Strong Testimonials form submission spinner.

Required

What is your full name?
What do you think about the Blog?
Write the Title with the current blog name when writing a testimonial for this blog instead of the entire website
What is your email address?
Do you have a profile page you want to link to?