HID Bootloader for a PIC18F2550

Discussion in 'Embedded Systems and Microcontrollers' started by KansaiRobot, May 20, 2010.

  1. KansaiRobot

    Thread Starter Active Member

    Jan 15, 2010
    318
    5
    Hello everybody and thank you always for your help.
    As stated in my previous posts, I have managed to make the USB examples from Microchip work for my PIC18F2550. I am using the HID Bootloader.

    Now i want something simpler I have this program that turns on and off a LED from a pin in the micro. it works well. of course I have to use a programmer to upload it. Now I want the same program but written through microchip HID bootloader. I cant manage to do this. Can anybody help me or tell me what am I doing wrong??? Many thanks in advance

    Kansai

    --------------------
    Code

    First I have this program
    Code ( (Unknown Language)):
    1. #include <p18f2550.h>
    2. #include <delays.h>
    3.  
    4. #pragma config FOSC = INTOSCIO_EC //Internal oscillator, port function on RA6, EC used by USB
    5. #pragma config WDT = OFF //Disable watchdog timer
    6.  
    7. #define LEDPin  LATBbits.LATB6   //Define LEDPin as PORT B Pin 6
    8. #define LEDTris TRISBbits.TRISB6 //Define LEDTris as TRISB Pin 6
    9.  
    10. void main()
    11. {
    12.     LEDTris = 0;//Set LED Pin data direction to OUTPUT
    13.     LEDPin = 1;//Set LED Pin
    14.    
    15.     while(1)
    16.     {
    17.         LEDPin = ~LEDPin;//Toggle LED Pin
    18.         Delay10KTCYx(25);//Delay 250K cycles (1 second at 1MHz since each instruction takes 4 cycles)
    19.     }
    20.  
    21. }
    It works great.
    Now I want the same code but with the bootloader. We know that we have to modify somehow the addresses for reset when using the bootloader so I did

    Code ( (Unknown Language)):
    1. #include <p18f2550.h>
    2. #include <delays.h>
    3. #include "configuration.h"
    4.  
    5. //#pragma config FOSC = INTOSCIO_EC //Internal oscillator, port function on RA6, EC used by USB
    6. //#pragma config WDT = OFF //Disable watchdog timer
    7.  
    8. #define LEDPin  LATBbits.LATB6   //Define LEDPin as PORT B Pin 6
    9. #define LEDTris TRISBbits.TRISB6 //Define LEDTris as TRISB Pin 6
    10.  
    11. void main()
    12. {
    13.     LEDTris = 0;//Set LED Pin data direction to OUTPUT
    14.     LEDPin = 1;//Set LED Pin
    15.    
    16.     while(1)
    17.     {
    18.         LEDPin = ~LEDPin;//Toggle LED Pin
    19.         Delay10KTCYx(25);//Delay 250K cycles (1 second at 1MHz since each instruction takes 4 cycles)
    20.     }
    21.  
    22. }
    and include a file "configuration.h" that contains the following code (taken with some modifications from the microchip example)

    Code ( (Unknown Language)):
    1.  
    2. #ifndef _CONFIGURATION_H
    3. #define _CONFIGURATION_H
    4.  
    5. void YourHighPriorityISRCode();
    6. void YourLowPriorityISRCode();
    7.  
    8. // Configuring the PIC18F2550 Microprocessor
    9.  
    10.         #pragma config PLLDIV   = 5         // (20 MHz crystal on PICDEM FS USB board)
    11.         #pragma config CPUDIV   = OSC1_PLL2  
    12.         #pragma config USBDIV   = 2         // Clock source from 96MHz PLL/2
    13.        // #pragma config FOSC     = HSPLL_HS
    14.         #pragma config FOSC = INTOSCIO_EC
    15.         #pragma config FCMEN    = OFF
    16.         #pragma config IESO     = OFF
    17.         #pragma config PWRT     = OFF
    18.         #pragma config BOR      = ON
    19.         #pragma config BORV     = 3
    20.         #pragma config VREGEN   = ON      //USB Voltage Regulator
    21.         #pragma config WDT      = OFF
    22.         #pragma config WDTPS    = 32768
    23.         #pragma config MCLRE    = ON
    24.         #pragma config LPT1OSC  = OFF
    25.         #pragma config PBADEN   = OFF
    26. //      #pragma config CCP2MX   = ON
    27.         #pragma config STVREN   = ON
    28.         #pragma config LVP      = OFF
    29. //      #pragma config ICPRT    = OFF       // Dedicated In-Circuit Debug/Programming
    30.         #pragma config XINST    = OFF       // Extended Instruction Set
    31.         #pragma config CP0      = OFF
    32.         #pragma config CP1      = OFF
    33. //      #pragma config CP2      = OFF
    34. //      #pragma config CP3      = OFF
    35.         #pragma config CPB      = OFF
    36. //      #pragma config CPD      = OFF
    37.         #pragma config WRT0     = OFF
    38.         #pragma config WRT1     = OFF
    39. //      #pragma config WRT2     = OFF
    40. //      #pragma config WRT3     = OFF
    41.         #pragma config WRTB     = OFF       // Boot Block Write Protection
    42.         #pragma config WRTC     = OFF
    43. //      #pragma config WRTD     = OFF
    44.         #pragma config EBTR0    = OFF
    45.         #pragma config EBTR1    = OFF
    46. //      #pragma config EBTR2    = OFF
    47. //      #pragma config EBTR3    = OFF
    48.         #pragma config EBTRB    = OFF
    49.  
    50.  
    51. // Using the HID Bootloader
    52.  
    53. //On PIC18 devices, addresses 0x00, 0x08, and 0x18 are used for
    54. //the reset, high priority interrupt, and low priority interrupt
    55. //vectors.  However, the current Microchip USB bootloader
    56. //examples are intended to occupy addresses 0x00-0x7FF or
    57. //0x00-0xFFF depending on which bootloader is used.  Therefore,
    58. //the bootloader code remaps these vectors to new locations
    59. //as indicated below.  This remapping is only necessary if you
    60. //wish to program the hex file generated from this project with
    61. //the USB bootloader.  If no bootloader is used, comment out
    62. //the following define
    63.  
    64. #define PROGRAMMABLE_WITH_USB_HID_BOOTLOADER
    65.  
    66. #if defined(PROGRAMMABLE_WITH_USB_HID_BOOTLOADER)
    67.         #define REMAPPED_RESET_VECTOR_ADDRESS           0x1000
    68.         #define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS  0x1008
    69.         #define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS   0x1018
    70.     #elif defined(PROGRAMMABLE_WITH_USB_MCHPUSB_BOOTLOADER)
    71.         #define REMAPPED_RESET_VECTOR_ADDRESS           0x800
    72.         #define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS  0x808
    73.         #define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS   0x818
    74.     #else  
    75.         #define REMAPPED_RESET_VECTOR_ADDRESS           0x00
    76.         #define REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS  0x08
    77.         #define REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS   0x18
    78.     #endif
    79.  
    80. #if defined(PROGRAMMABLE_WITH_USB_HID_BOOTLOADER)||defined(PROGRAMMABLE_WITH_USB_MCHPUSB_BOOTLOADER)
    81.     extern void _startup (void);        // See c018i.c in your C18 compiler dir
    82.     #pragma code REMAPPED_RESET_VECTOR = REMAPPED_RESET_VECTOR_ADDRESS
    83.     void _reset (void)
    84.     {
    85.         _asm goto _startup _endasm
    86.     }
    87.     #endif
    88.     #pragma code REMAPPED_HIGH_INTERRUPT_VECTOR = REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS
    89.     void Remapped_High_ISR (void)
    90.     {
    91.          _asm goto YourHighPriorityISRCode _endasm
    92.     }
    93.     #pragma code REMAPPED_LOW_INTERRUPT_VECTOR = REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS
    94.     void Remapped_Low_ISR (void)
    95.     {
    96.          _asm goto YourLowPriorityISRCode _endasm
    97.     }
    98.    
    99.  
    100. #if defined(PROGRAMMABLE_WITH_USB_HID_BOOTLOADER)||defined(PROGRAMMABLE_WITH_USB_MCHPUSB_BOOTLOADER)
    101.     //Note: If this project is built while one of the bootloaders has
    102.     //been defined, but then the output hex file is not programmed with
    103.     //the bootloader, addresses 0x08 and 0x18 would end up programmed with 0xFFFF.
    104.     //As a result, if an actual interrupt was enabled and occured, the PC would jump
    105.     //to 0x08 (or 0x18) and would begin executing "0xFFFF" (unprogrammed space).  This
    106.     //executes as nop instructions, but the PC would eventually reach the REMAPPED_RESET_VECTOR_ADDRESS
    107.     //(0x1000 or 0x800, depending upon bootloader), and would execute the "goto _startup".  This
    108.     //would effective reset the application.
    109.    
    110.     //To fix this situation, we should always deliberately place a
    111.     //"goto REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS" at address 0x08, and a
    112.     //"goto REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS" at address 0x18.  When the output
    113.     //hex file of this project is programmed with the bootloader, these sections do not
    114.     //get bootloaded (as they overlap the bootloader space).  If the output hex file is not
    115.     //programmed using the bootloader, then the below goto instructions do get programmed,
    116.     //and the hex file still works like normal.  The below section is only required to fix this
    117.     //scenario.
    118.     #pragma code HIGH_INTERRUPT_VECTOR = 0x08
    119.     void High_ISR (void)
    120.     {
    121.          _asm goto REMAPPED_HIGH_INTERRUPT_VECTOR_ADDRESS _endasm
    122.     }
    123.     #pragma code LOW_INTERRUPT_VECTOR = 0x18
    124.     void Low_ISR (void)
    125.     {
    126.          _asm goto REMAPPED_LOW_INTERRUPT_VECTOR_ADDRESS _endasm
    127.     }
    128.     #endif  //end of "#if defined(PROGRAMMABLE_WITH_USB_HID_BOOTLOADER)||defined(PROGRAMMABLE_WITH_USB_LEGACY_CUSTOM_CLASS_BOOTLOADER)"
    129.  
    130.     #pragma code
    131.    
    132.    
    133.     //These are your actual interrupt handling routines.
    134.     #pragma interrupt YourHighPriorityISRCode
    135.     void YourHighPriorityISRCode()
    136.     {
    137.         //Check which interrupt flag caused the interrupt.
    138.         //Service the interrupt
    139.         //Clear the interrupt flag
    140.         //Etc.
    141.        // #if defined(USB_INTERRUPT)
    142.         //    USBDeviceTasks();
    143.         //#endif
    144.    
    145.     }   //This return will be a "retfie fast", since this is in a #pragma interrupt section
    146.     #pragma interruptlow YourLowPriorityISRCode
    147.     void YourLowPriorityISRCode()
    148.     {
    149.         //Check which interrupt flag caused the interrupt.
    150.         //Service the interrupt
    151.         //Clear the interrupt flag
    152.         //Etc.
    153.    
    154.     }   //This return will be a "retfie", since this is in a #pragma interruptlow section
    155.    
    156.    
    157. #endif
    it doesnt work. What am I doing wrong???:(

    many thanks :)
     
  2. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    Why did you modify the bootloader code?? It was designed specifically for the C18's.

    You need a way to put the pic in bootloader mode, then run the USBHIDBootloader.exe on the PC to download your hex file to the pic using the bootloader....

    Here is a site (http://www.schmalzhaus.com/UBW32/doc/UBW32BootloaderDocumentation.html)where you can get the latest version of the bootloader (although it has some modifications to work with the UBW32, you can just edit the Hardwareprofile.h to reconfigure the I/O's to work with your setup....

    you can also download the HidBootloader.exe from there too....

    B. Morse
     
Loading...