AMSA84
Advanced Member level 2
- Joined
- Aug 24, 2010
- Messages
- 577
- Helped
- 8
- Reputation
- 16
- Reaction score
- 8
- Trophy points
- 1,298
- Location
- Iberian Peninsula
- Activity points
- 6,178
Yes that's correct. I want to use the USB feature of the PIC18F4550 (which was the one I choose to start with, because of the demonstration board) along with the PICDEM FSUSB software to upload the .hex to program my device.
C:\Microchip\MCHPUSB2.7a
USB Device - MCHPUSB - MCHPUSB Bootloader - C18 - PICDEM FSUSB.hex
picdemfsusb.hex
USB Device - HID - HID Bootloader - C18 - PIC18F4550.hex
USB Device - MCHPUSB - Generic Driver - C18 - PICDEM FSUSB.hex
#include <xc.h>
void main (void)
{
TRISD = 0x00; //set PortB.RB0 as output
while (1) //infinite loop
{
LATDbits.LATD3 = 1;
}
}
If I use the code in the MCHPFSUSB\fw\Demo folder from the MCHPFSUSB_Setup.exe which I think it is the very first project in USB communication from microchip I can get what I want. I just change the code inside the main function and doing all the procedure that I described above I manage to program the PIC through USB using the PICDEM tool.
Don't know why...;
#include <xc.h>
void main (void)
{
TRISD = 0x00; //set PortB.RB0 as output
while (1) //infinite loop
{
LATDbits.LATD3 = 1;
}
}
All bootloader occupy a specific portion of the device Flash (ROM) memory, in the case of the MCHPUSB bootloader, this is the range from 0x000 to 0x7FF, this ensures upon reset, either power up/startup or pressing the S1 (MCLR) button, the bootloader firmware is immediately ran. Also, typically bootloader, as is the case with the MCHPUSB bootloader, write protect the section/block of Flash (ROM) they inhabit, this prevents downloaded code from inadvertently writing over the bootloader.
Upon startup/reset, in the case of the PIC18F family,execution of code begins at Flash (ROM) address 0x000, the bootloader is executed which then checks the status of the S2 button (RB4 line), to determine if it should remain in bootloader mode or branch to Flash (ROM) address 0x800, which is the intend start location of either the demo app or your custom app.
Therefore, you must indicate to the compiler, to which you are using to developing code, that the compiled code must be relocated/offset from starting address 0x000 to 0x800. Of course, as the boot section/block of Flash (ROM) which contains the bootloader is write protected, if the HEX file containing your code does not indicate a starting address of 0x800 or higher, as was the case with you test app:
/** V E C T O R R E M A P P I N G *******************************************/
extern void _startup (void); // See c018i.c in your C18 compiler dir
#pragma code _RESET_INTERRUPT_VECTOR = 0x000800
void _reset (void)
{
_asm goto _startup _endasm
}
/*********************************************************************
*
* Microchip USB C18 Firmware Version 1.0
*
*********************************************************************
/** I N C L U D E S **********************************************************/
#include <p18cxxx.h>
//#include "system\typedefs.h" // Required
//#include "system\usb\usb.h" // Required
//#include "io_cfg.h" // Required
//#include "system\usb\usb_compile_time_validation.h" // Optional
//#include "user\user.h" // Modifiable
//#include <portb.h> // For interruptions
//#include "def_ports.h" // Port definitions
//#include "def_interruptions.h" // Interruption definitions
//void int_routine(void);
/** V E C T O R R E M A P P I N G *******************************************/
extern void _startup (void); // See c018i.c in your C18 compiler dir
#pragma code _RESET_INTERRUPT_VECTOR = 0x000800
void _reset (void)
{
_asm goto _startup _endasm
}
/*#pragma code
#pragma code _LOW_INTERRUPT_VECTOR = 0x000818
void _low_ISR (void)
{
;
}*/
// HIGH PRIORITY INTERRUPTS VECTOR
/*#pragma code high_interrupt_vector = 0x808
void high_interrupt (void)
{
_asm goto int_routine _endasm
}*/
//#pragma code
//#pragma interrupt int_routine
/*void int_routine (void)
{
// Lights LED D3 and D4, which is connected to bit 2 and 3 of port D (PD2 and PD3),
// only when both switches 2 and 3, connected bits 4 and 5 of port B (PB4 and PB5) are pressed.
if(!PB4 && !PB5 ) {
PD2 = 1;
PD3 = 1;
}
else {
PD2 = 0;
PD3 = 0;
}
RBIF = 0; // Clear the interrupt flag for port B.
}*/
/******************************************************************************
* Function: void main(void)
* Overview: Main program entry point.
******************************************************************************/
void main(void)
{
TRISDbits.RD2 = 0;
while(1)
{
LATDbits.LATD2 = 1;
}
} //end main
Most of the example demo use the C18 legacy compiler, it appears you are using the XC8 compiler is that true?
EDIT: I was thinking on another thing. In my project I don't have any linker file attached into the Projects -> Linker Files. From what I read if there is no linker file, MPLAB X will use the default linker filer. This might be influencing this code offset thing?
EDIT for the first paragraph in bold:
NOTE: But this was done separately from the "MCHPUSB bootloader". This "MCHPUSB bootloader" is compiled with the C18 compiler and my code is compiled using the XC8. The code offset is done in the MPLAB X feature using the XC8 compiler.
Can this mean that the "MCHPUSB bootloader" must be written in XC8 so that he can understand the code offset implemented through the MPLAB X code offset feature?
Depending on the compiler this can be accomplished with either a linker script or #pragma preprocessor directives, in the case of the XC8/MPLABX toolset, I believe this is handled with #pragma preprocessor directives.
the source code, compiler and IDE be properly configured to offset both the starting address and any vector addresses which are relevant to the underlying microcontroller hardware.
When you say that at bold, means that I should tell the MPLAB X to offset the code through the project setting, that is, the code offset feature, and at same time write in my program the #pragma preprocessor directives? I must do both?
----------------------------------------------------------------------
NOTE FOR BUILDING APPLICATION FIRMWARE PROJECTS WITH THE XC8 COMPILER:
----------------------------------------------------------------------
When building the application project that is meant to be programmed by this
bootloader, you must use different XC8 linker settings as this project.
For application projects, two linker settings are required:
ROM ranges: default,-0-FFF,-1006-1007,-1016-1017
Codeoffset: 0x1000
When the above settings are implemented, the application firmware will no longer work
without the bootloader present. Therefore, it is recommended to add the bootloader
firmware output (from this bootloader project) .hex file as a "Loadable" into
the application firmware project. This will allow the "HEXMATE" tool to run
after building the application firmware project, which will merge the application
output .hex file contents with the bootloader output .hex file contents (which was
added as a loadable file).
However, in some cases you may encounter build errors during the hex merge operation.
This will occur if there are any overlapping regions in the bootloader firmware
.hex file, with the application firmware .hex file, when the contents of these
overlapping regions are not 100% exact matches to each other. Normally, only the
configuration bit settings will be overlapping between the two projects.
Therefore, to prevent build errors, the configuration bit settings between the
bootloader firmware project and the application firmware project must be set to
100% exactly identical values (or they must only be set in one of the projects,
to eliminate the overlapping region altogether).
So what you suggest? Use Microchip C18 compiler?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?