Bus Pirate v3 PIC 24FJ blinking LED programming tutorial

From DP

(Redirected from BPv3 blink MODE LED demo)
Jump to: navigation , search

Bpv3-dev-board.jpg

Contents

Overview

The Bus Pirate is a handy debugging tool, but it's also a 16 bit PIC 24FJ64GA002 development board. You don't need a programmer to play, just upload the firmware over USB using the Bus Pirate's nearly-impossible-to-brick bootloader. It's easy to go back to the original Bus Pirate firmware at any time.

This demo is step one. It shows how to setup the chip to run from the internal clock, and blink the MODE LED. The code is broken up and explained section by section.

Stuff to get

You'll need Microchip's free (but not open) IDE and C30 compiler to get started.

The MPLAB IDE is available in two versions. The old IDE which is Windows only, and the new Microchip MPLABX IDE which is cross platform (Windows, Mac, Linux). Download one and install it.

There are also two versions of the compilers. XC16 compiler is made for MPLABX, but is currently unavailable. The MPLAB C Compiler for PIC24 MCUs and dsPIC DSCs works on both IDEs, download and install it.

Don't forget to download the source for this demo:

Be sure to download the fine PIC 24FJ64GA002 datasheet, it has answers to most of your hardware questions.

The source

MainCmplab82.jpg

After installing MPLAB and a compiler, open the blink MODE LED demo project. Look for a list of files included in the project and click main.c to open it.

Basic Configuration

#include <p24fxxxx.h>

The very first line of main.c includes the C30 compiler's basic definitions for the PIC hardware.

_CONFIG1(    JTAGEN_OFF &     //JTAG port is disabled
            GCP_OFF &         //GSP Memory Code Protection OFF
            GWRP_OFF &         //GCC Flash Write Protection OFF
            COE_OFF &         //
            FWDTEN_OFF &     //Watchdog Timer OFF
            ICS_PGx1)         //debug over PGD1 and PGC1

Various default settings are used when a PIC is first powered. The initial setup is defined by the configuration words, sometimes also called config fuses. Here are the most meaningful options in CONFIG1:

  • JTAG debug mode is off - this is a secondary debugging mode we don't use that wastes pins
  • Watchdog Timer is off - this is an alarm that resets the PIC if there are problems. We won't use it in this demo
  • Debugging is done with the PGD1 and PGC1 pins, the default for the Bus Pirate hardware. If you're debugging custom hardware using a different set of pins then change this setting
CONFIG2(    FNOSC_FRCPLL &     //Internal FRC with PLL
            OSCIOFNC_ON &    //RA3 is clk out (fosc/2)
            POSCMOD_NONE &     //Primary oscillator disabled
            I2C1SEL_PRI)    //Use default SCL1/SDA1 pins

CONFIG2 settings determine what clock the PIC uses at startup. We need to configure it to use the internal oscillator or our code will never run.

  • FRC selects the an 8MHz internal oscillator, this is multiplied by 4 using an internal PLL option
  • The Bus Pirate has no external crystal so we disable the primary external oscillator circuit and score a few extra IO pins

Do some setup

#pragma code

This tells the compiler that the next section is code, as opposed to configuration. This is not strictly required.

int main(void)
{
   unsigned long i;         //a 32 bit variable to use as a timer
 
   CLKDIVbits.RCDIV0=0;     //clock divider to 0
   AD1PCFG = 0xFFFF;        // Default all pins to digital
   OSCCONbits.SOSCEN=0;     //Disables the secondary oscilator

This is the main function. The processor will start processing user code from here.

First we declare variable i, a 32 bit number that we'll use in a delay function later.

Next we change a few default startup settings.

  • [name of register]bits.[name of bit]

C30 bit variables are accessed with the above syntax. The names are the same as in the PIC 24FJ64GA002 datasheet.

Initially the PIC starts with a slow clock. To get the full 16 MIPs we need to set the clock divider to 0. This is done by setting the RCDIV0 bit of the CLKDIV register to 0.

The PIC starts with all pins in analog mode to save power. We make all the pins digital by setting the AD1PCFG register to all 1s.

We disable the secondary oscillator by clearing the SOSCEN bit in the OSCCON register. This gives us the pins on the real-time clock oscillator for digital IO.

   TRISAbits.TRISA1 = 0;    //sets the Mode LED pin RA1 as output
   LATAbits.LATA1 = 0;      //turns LED off

Check the Bus Pirate schematic and note that the MODE LED is connected to PORTA pin #1, or A1.

First we set the direction of the pin to output. The TRIS register controls the pin direction. 1 is input, 0 is output. To control the LED we need an output pin, so we write 0 to the TRISA1 bit of the TRISA register.

LATA sets the pin output to high (1) or low (0). Clear the LATA1 bit in the LATA register to start with the MODE LED off.

Blink the MODE LED

    while(1)
    {
      LATAbits.LATA1 = ~LATAbits.LATA1;  //toggles the MODE LED
      i =0xFFFFF;                       //sets i to 1048575
      while (i--);                      //delay function
    }                                   //decrements i until 0   
}


Now let's get to the good stuff, blinken LEDs.

while(1) creates a continuous loop of the code inside the {} brackets. Here's what happens:

  1. The MODE LED is flipped to the opposite of the current value using the =~ operator. On the first run the MODE LED on the Bus Pirate lights, on the second it turns off, and so on
  2. Variable i is now set to 1048575 (0xFFFFF)
  3. The while(i--) loop simply subtracts one until i hits 0. When i equals 0 the program returns to #1

This never ending loop blinks the LED.

About the linker script

Before compiling we need to add an extra file to the project. The linker script file (*.gld) tells the compiler where the bootloader lives so it doesn't put any code in that part of the PIC.

A linker script that avoids the Bus Pirate bootloader area is already included in the demo project. You can see p24FJ64GA002.gld here.

  • If you use a PIC programmer the linker isn't needed, but it will work fine with it too
  • If you bootload a firmware compiled without the linker there may be errors, but it won't hurt the Bus Pirate

Compile the code

ComplieBuildallMpalab8.jpg

Compile the code by pressing the compile button. You may need to modify the compiler location when prompted, but MPLAB is getting better about making these changes automatically.

The compiled .HEX file for the PIC is output to the project directory. If you're using a programmer burn this file, if you're using the bootloader continue to the extra step below.

Load the firmware

There are two ways to get code into the Bus Pirate.

This demo will work with either method.

If you're using a programmer stop here and program the firmware. Skip the bootloader steps below.

Export firmware for the bootloader

The Bus Pirate comes with a bootloader that can be used to program the demo firmware over the USB connection. First we have to export a limited chunk of the firmware that doesn't overlap the bootloader.

Mplab-export 001.png

After compiling is completed go to File/Export:

  1. Check the Program Memory box
  2. Uncheck the Configuration Bits box
  3. Enter the End memory address 0xa7fa
  4. Make sure INHX32 (Intel 32bit HEX) is selected on the File Format tab
  5. Hit OK
  6. Save the *.hex file where you can find it later

If you forget this step nothing bad will happen, but the bootloader may give an error message and fail. Just reset the Bus Pirate and restart the process.

More about exporting the firmware, including using MPLABX.

Bootload the firmware

Bpv3-upgrade-jumper.jpg

Follow your normal Bus Pirate upgrade process to load the firmware with the bootloader. This usually involves:

  1. Short the PGC and PGD pins of the ICSP header with a paper clip or wire
  2. Plug in the Bus Pirate, the MODE LED should light to indicate the bootloader is active
  3. Upload the firmware with Pirate-Loader or ds30 Loader
  4. Replug the Bus Pirate to reset and run the new firmware

Congratulations Bus Pirate hack master! The MODE LED should blink as the Bus Pirate runs the demo firmware.

Restore original Bus Pirate firmware

It's easy to get back to the original Bus Pirate firmware. Just use Pirate-Loader or ds30 Loader to upload the latest Bus Pirate firmware file.

Need help?

Don't fret, just give us a shout in the forum.

C30 Source Code

#include <p24fxxxx.h>
 
_CONFIG1(    JTAGEN_OFF &       //JTAG port is disabled
             GCP_OFF &          //GSP Memory Code Protection OFF
             GWRP_OFF &         //GCC Flash Write Protection OFF
             COE_OFF &          //
             FWDTEN_OFF &       //Watchdog Timer OFF
             ICS_PGx1)          //debug over PGD1 and PGC1
 
_CONFIG2(    FNOSC_FRCPLL &     //Internal FRC with PLL
             OSCIOFNC_ON &      //RA3 is clk out (fosc/2)
             POSCMOD_NONE &     //Primary oscillator disabled
             I2C1SEL_PRI)       //Use default SCL1/SDA1 pins
 
 
#pragma code
 
int main(void)
{
   unsigned long i;         //a 32 bit variable to use as a timer
 
   CLKDIVbits.RCDIV0=0;     //clock divider to 0
   AD1PCFG = 0xFFFF;        // Default all pins to digital
   OSCCONbits.SOSCEN=0;     //Disables the secondary oscilator
 
   TRISAbits.TRISA1 = 0;    //sets the Mode LED pin RA1 as output
   LATAbits.LATA1 = 0;      //turns LED off
 
//////////////////////////////////////////////////////////////////
///FOREVER LOOP///////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
    while(1)
    {
      LATAbits.LATA1 = ~LATAbits.LATA1; //toggles the MODE LED
      i =0xFFFFF;                       //sets i to 1048575
      while (i--);                      //delay function
    }                                   //decrements i until 0  
}