BASIC INFO

We are going to describe how to build the bootloader for the PIC18f2550 used in our board PICCOLETTA. To do this job we will use the C compiler from Microchip (the lite version).

The bootloader is an application that enables the programming of the PIC without a programmer. At boot time the bootloader has two choiches:

  1. Wait for instructions to program the PIC.
  2. Launch the previous programmed firmware.

The memory of the PIC (without the interrupt vector table) is as follows:

Program Memory Start address 0X0000  

  bootloader

Application Start Address 0X1234  

  Application

Program Memory End Address 0XFFFF  

The Bootloader is stored into the microcontroller memory (in the example it is in the first 0×1234 bytes of the flash memory), while an application will be stored between the byte 0x1234 and the byte 0xffff.

At boot time (when the PIC is powered on) the PIC executes a jump to the address 0x0000, so the bootloader can run.

Now the bootloader can wait for commands or it can run an application starting from address 0x1235.

The bootloader can choose what to do by reading the value of a pin or when a timeout expires.

NEEDED TOOLS

  • A PC running Windows
  • A PIC programmer (we are using the Pickit2 by microchip).
  • The MPLAB IDE (it can be downloaded from the microchip site).
  • The C compiler mcc18 lite (it can be downloaded from the microchip site).
  • PICCOLETTA equipped with a pic18f2550.
  • The USB stack from microchip (it can be downloaded from the microchip site). We use Microchip Solutions v2010-10-19 ( Microchip Application Libraries v2010-10-19 Installer.exe) . The USB stack must be installed in "C:\" and both the compiler and the MPLAB must be installed in the default directories during their own installation.

The USB stack contains a bootloader for Non-J devices

c:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices

 

AT WORK

Open the the HID Bootloader PIC18 Non J.mcp project file.

On the MPLAB IDE let's select "Configure–>Select Device …."

Change set the device to pic18f2550. 

In the project window delete the linker file "BootModified.18f4550_g.lkr" and then add the file BootModified.18f2550_g.lkr

Last launch the build using the Build all command.

🙁

you should have obtained a lot of errors!!!

 

Release build of project `C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\HID Bootloader PIC18 Non J.mcp' started.

Language tool versions: mpasmwin.exe v5.38, mplink.exe v4.38, mcc18.exe v3.37, mplib.exe v4.38

Tue Apr 12 19:55:16 2011

———————————————————————-

Clean: Deleting intermediary and output files.

Clean: Deleted file "C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\hid.o".

Clean: Done.

Executing: "C:\Program Files\Microchip\mplabc18\v3.37\bin\mcc18.exe" -p=18F2550 /i"C:\MCC18\h" "hid.c" -fo="hid.o"

Executing: "C:\Program Files\Microchip\mplabc18\v3.37\bin\mcc18.exe" -p=18F2550 /i"C:\MCC18\h" "main.c" -fo="main.o"

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.c:376:Error [1105] symbol 'LATDbits' has not been defined

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.c:376:Error [1151] struct or union object designator expected

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.c:376:Error [1101] lvalue required

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.c:376:Error [1105] symbol 'LATDbits' has not been defined

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.c:376:Error [1151] struct or union object designator expected

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.c:376:Error [1101] lvalue required

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.c:382:Error [1105] symbol 'LATDbits' has not been defined

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.c:382:Error [1151] struct or union object designator expected

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.c:382:Error [1105] symbol 'LATDbits' has not been defined

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.c:382:Error [1151] struct or union object designator expected

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.c:382:Error [1101] lvalue required

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.c:383:Error [1105] symbol 'LATDbits' has not been defined

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.c:383:Error [1151] struct or union object designator expected

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.c:383:Error [1105] symbol 'LATDbits' has not been defined

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.c:383:Error [1151] struct or union object designator expected

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.c:383:Error [1101] lvalue required

Halting build on first failure as requested.

———————————————————————-

Release build of project `C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\HID Bootloader PIC18 Non J.mcp' failed.

Language tool versions: mpasmwin.exe v5.38, mplink.exe v4.38, mcc18.exe v3.37, mplib.exe v4.38

Tue Apr 12 19:55:17 2011

———————————————————————-

BUILD FAILED

  • Also the compiler doesn't know where to find the include files.
  • The microchip example uses the pic18f4550, this PIC has a D port with some leds but the pic18f2550 doesn't have this port.

Select "Project–>Build Options …" and then the "Project" item.

 


 

For the include files change the directory c:\MCC18\h with c:\Microchip Solutions v2010-10-19\Microchip\Include

In the library field insert C:\Program Files\Microchip\mplabc18\v3.37\lib

Delete every reference to the LEDs and to the D port.

Remove from the main.c file the BlinkUSBStatus function and all call to this function.

Comment out the following lines:

  • 186
  • 243
  • 362 to 386

In the BootPIC18NonJ.c file comment out the line 252.

Now launch again the Build All command, you'll get a strange error:

:(

———————————————————————-

Release build of project `C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\HID Bootloader PIC18 Non J.mcp' started.

Language tool versions: mpasmwin.exe v5.38, mplink.exe v4.38, mcc18.exe v3.37, mplib.exe v4.38

Tue Apr 12 20:11:34 2011

———————————————————————-

Clean: Deleting intermediary and output files.

Clean: Deleted file "C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\hid.o".

Clean: Deleted file "C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\main.o".

Clean: Deleted file "C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\usb9.o".

Clean: Deleted file "C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\usbctrltrf.o".

Clean: Deleted file "C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\usbdrv.o".

Clean: Deleted file "C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\usbdsc.o".

Clean: Deleted file "C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\usbmmap.o".

Clean: Done.

Executing: "C:\Program Files\Microchip\mplabc18\v3.37\bin\mcc18.exe" -p=18F2550 /i"C:\Microchip Solutions v2010-10-19\Microchip\Include" "hid.c" -fo="hid.o"

Executing: "C:\Program Files\Microchip\mplabc18\v3.37\bin\mcc18.exe" -p=18F2550 /i"C:\Microchip Solutions v2010-10-19\Microchip\Include" "main.c" -fo="main.o"

Executing: "C:\Program Files\Microchip\mplabc18\v3.37\bin\mcc18.exe" -p=18F2550 /i"C:\Microchip Solutions v2010-10-19\Microchip\Include" "usb9.c" -fo="usb9.o"

Executing: "C:\Program Files\Microchip\mplabc18\v3.37\bin\mcc18.exe" -p=18F2550 /i"C:\Microchip Solutions v2010-10-19\Microchip\Include" "usbctrltrf.c" -fo="usbctrltrf.o"

Executing: "C:\Program Files\Microchip\mplabc18\v3.37\bin\mcc18.exe" -p=18F2550 /i"C:\Microchip Solutions v2010-10-19\Microchip\Include" "usbdrv.c" -fo="usbdrv.o"

C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\usbdrv.c:104:Warning [2056] expression is always true

Executing: "C:\Program Files\Microchip\mplabc18\v3.37\bin\mcc18.exe" -p=18F2550 /i"C:\Microchip Solutions v2010-10-19\Microchip\Include" "usbdsc.c" -fo="usbdsc.o"

Executing: "C:\Program Files\Microchip\mplabc18\v3.37\bin\mcc18.exe" -p=18F2550 /i"C:\Microchip Solutions v2010-10-19\Microchip\Include" "usbmmap.c" -fo="usbmmap.o"

Executing: "C:\Program Files\Microchip\mplabc18\v3.37\bin\mcc18.exe" -p=18F2550 /i"C:\Microchip Solutions v2010-10-19\Microchip\Include" "BootPIC18NonJ.c" -fo="BootPIC18NonJ.o"

Executing: "C:\Program Files\Microchip\mplabc18\v3.37\bin\mplink.exe" /p18F2550 /l"C:\Program Files\Microchip\mplabc18\v3.37\lib" "BootModified.18f2550_g.lkr" "hid.o" "main.o" "usb9.o" "usbctrltrf.o" "usbdrv.o" "usbdsc.o" "usbmmap.o" "BootPIC18NonJ.o" /u_CRUNTIME /z__MPLAB_BUILD=1 /m"HID Bootloader PIC18 Non J.map" /w /o"HID Bootloader PIC18 Non J.cof"

MPLINK 4.38, Linker

Copyright (c) 1998-2010 Microchip Technology Inc.

Error – section '_cinit_scn' can not fit the section. Section '_cinit_scn' length=0x0000009e

Errors : 1


Link step failed.

———————————————————————-

Release build of project `C:\Microchip Solutions v2010-10-19\USB Device – Bootloaders\HID – Bootloader\HID Bootloader – Firmware for PIC18 Non-J Devices\HID Bootloader PIC18 Non J.mcp' failed.

Language tool versions: mpasmwin.exe v5.38, mplink.exe v4.38, mcc18.exe v3.37, mplib.exe v4.38

Tue Apr 12 20:11:39 2011

———————————————————————-

BUILD FAILED


This means that the application is too large for a boot loader.

The reserved space for the bootloader is defined into the file BootModified.18f2550_g.lkr.

Search for: CODEPAGE NAME=BootPage START=0×20 END=0xFFF

 

Change the linker file.

Open, in the MPLAB editor, the BootModified.18f2550_g.lkr file.

Change the prevoius line with the following:

CODEPAGE NAME=BootPage START=0×20 END=0x10FF

Now I'm increasing the dimension of the space reserved for the bootloader.

Remove line 28 and write the following:

CODEPAGE NAME=page START=0×1100 END=_CODEEND PROTECTED

Remove line 31 and write the following:

CODEPAGE NAME=page START=0×1100 END=0x7FFF PROTECTED

The memory used by the bootloader to store the application will start at address 0×1100.

Save all changes and run again the "Build All" command.

The job goes well, but we need to perform some other actions.

Slect View->Memory usage Gauge, there are 2113 words of program memory and 512KB of RAM.

In the sources you have to define a MACRO, this MACRO means that you are defining a new BOARD (we call PICCOLETTA_BOARD this new BOARD)

Open the main.c file, go to line 138 and insert the following:

#elif defined(PICCOLETTA) // Configuration bits for PICCOLETTA

#pragma config PLLDIV = 2 // (9 MHz crystal)

#pragma config CPUDIV = OSC1_PLL2

#pragma config USBDIV = 2 // Clock source from 96MHz PLL/2

#pragma config FOSC = HSPLL_HS

#pragma config FCMEN = OFF

#pragma config IESO = OFF

#pragma config PWRT = OFF

#pragma config BOR = ON

#pragma config BORV = 3

#pragma config VREGEN = ON //USB Voltage Regulator

#pragma config WDT = OFF

#pragma config WDTPS = 32768

#pragma config MCLRE = ON

#pragma config LPT1OSC = OFF

#pragma config PBADEN = OFF

// #pragma config CCP2MX = ON

#pragma config STVREN = ON

#pragma config LVP = OFF

// #pragma config ICPRT = OFF // Dedicated In-Circuit Debug/Programming

#pragma config XINST = OFF // Extended Instruction Set

#pragma config CP0 = OFF

#pragma config CP1 = OFF

// #pragma config CP2 = OFF

// #pragma config CP3 = OFF

#pragma config CPB = OFF

// #pragma config CPD = OFF

#pragma config WRT0 = OFF

#pragma config WRT1 = OFF

// #pragma config WRT2 = OFF

// #pragma config WRT3 = OFF

#pragma config WRTB = OFF // Boot Block Write Protection

#pragma config WRTC = OFF

// #pragma config WRTD = OFF

#pragma config EBTR0 = OFF

#pragma config EBTR1 = OFF

// #pragma config EBTR2 = OFF

// #pragma config EBTR3 = OFF

#pragma config EBTRB = OFF


Now you have to remap the interrupt vectors (otherwise they are allocated at the end of the program memory space reserved to the bootloader)

Go to line 233 and change it with the following:

_asm goto 0×1108 _endasm

Go to line 238 and change it with the following:

_asm goto 0×1118 _endasm

Go to line 270 and change it with the following:

goto 0×1100 //If the user is not trying to enter the bootloader, go straight to the main application remapped "reset" vector.

The last change remaps the start point for the application.

Close the main.c file.


Open the uscfg.h file and comment out lines starting from 49 to 54

move to line 56 and add the following:

#define PICCOLETTA

move to line 67 and add the following:

#elif defined(PICCOLETTA)

Close the uscfg.h file.

Open the io_cfg.h file.

Go to line 192 and add:

#elif defined(PICCOLETTA)

/** SELF POWER *****************************************************/

#define tris_usb_bus_sense

#define usb_bus_sense 1

#define tris_self_power

#define self_power 1


/** LED ************************************************************/

#define mInitAllLEDs()


#define mLED_1

#define mLED_2

#define mLED_3

#define mLED_4


#define mLED_1_On()

#define mLED_2_On()

#define mLED_3_On()

#define mLED_4_On()


#define mLED_1_Off()

#define mLED_2_Off()

#define mLED_3_Off()

#define mLED_4_Off()


#define mLED_1_Toggle()

#define mLED_2_Toggle()

#define mLED_3_Toggle()

#define mLED_4_Toggle()


/** SWITCH *********************************************************/

#define mInitSwitch2() TRISCbits.TRISC0=1


#define mInitSwitch3()

// PICCOLETTA has a jumper on the fourth pin of portb for the bootloader

#define sw2 PORTBbits.RB4

#define sw3

#define mInitAllSwitches() mInitSwitch2();


/** POT ************************************************************/

#define mInitPOT()


Close the  io_cfg.h file

Open the BootPIC18NonJ.c file.


Change the line 46 with the following:

#define ProgramMemStart 0×001100 //Beginning of application program memory (not occupied by bootloader). **THIS VALUE MUST BE ALIGNED WITH 64 BYTE BLOCK BOUNDRY** Also, in order to work correctly, make sure the StartPageToErase is set to erase this section.


change the line 49 with the following:

#define StartPageToErase 68 //The 4096 byte section from 0×000-0x10FF contains the bootloader and will not be erased


Close the BootPIC18NonJ.c file

Run the Build All command.

Use the programmer to flash your new bootloader, and test it.


Yust a little complex, but it can be done!!!

 

If you want to give us support or if you want the assembled and tested board, you can go to ebay at the following adress:

http://shop.ebay.it/giovannitto/m.html?_dmd=1&_ipg=50&_sop=12&_rdc=1

 

Gg1