CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug Reports on this forum. Send them to support@ccsinfo.com

18F4550 & 18F45K50 bootloader problem ? Compiler 5.078

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Cogitum



Joined: 22 Mar 2012
Posts: 59
Location: France (Paris)

View user's profile Send private message

18F4550 & 18F45K50 bootloader problem ? Compiler 5.078
PostPosted: Fri May 03, 2019 10:22 am     Reply with quote

Hello,
I am having some problems using 2 different PICs and a bootloader.
I have 2x different boards
1 X with 18F4550
1 x with 18F45K50

PROCESS :
I load the bootloader and unplug the programmer.
I plug the USB connection cable.
2 push buttons allow me to switch to BOOT mode (recognition state 0 for one and reset for the other).
I open serial monitor and I can read " BOOTLOADER READY"!
Then "file then download software".
The program downloaded is the blinking LED. LEDs FLASH!

Everything is perfect with the 18F4550.
I do the same thing with the bootloader adapted for the 18F45K50.
I open serial monitor and I can read " BOOTLOADER READY"!
The led program is also suitable for the 18F45K50.
unfortunately the LEDs do not blink ???

Any suggestions or advice are welcome.
Thanks in advance for your help.
Code:

#include <18F45K50.h>
#include <usb_bootloader.h>
 //#include <ex_usb_hid.c>

#use delay(crystal=16MHz)
//****************************************************************************
#fuses none   
//****************************************************************************
//#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)

// #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
void main(void) {
{
 while(TRUE)
 {
output_toggle(PIN_E0);//LED1
delay_ms(100);
output_toggle(PIN_E1);//LED2 RED
delay_ms(100);
 }   
}
}
Ttelmah



Joined: 11 Mar 2010
Posts: 14424

View user's profile Send private message

PostPosted: Fri May 03, 2019 12:19 pm     Reply with quote

Post the fuses your bootloader uses, and the compiler version.

My guess is that on the 45K50, PBADEN is being set, so port B5..0 all
wake up set as analog inputs.
Cogitum



Joined: 22 Mar 2012
Posts: 59
Location: France (Paris)

View user's profile Send private message

18F4550 & 18F45K50 bootloader problem ? Compiler 5.078
PostPosted: Fri May 03, 2019 1:32 pm     Reply with quote

HI Ttelmah,

Thank for your quick answer. Sorry Compiler version is on the subject line

V : 5.078 Very Happy .
Code:

 #fuses WDT_SW         //No Watch Dog Timer, enabled in Software
   #fuses CCP2C1         //CCP2 input/output multiplexed with RC1
   #fuses PUT            //Power Up Timer
   #fuses BROWNOUT_SW    //Brownout controlled by configuration bit in special file register
   #fuses BORV19         //Brownout reset at 1.9V
   #fuses PBADEN         //PORTB pins are configured as analog input channels on RESET
   #fuses T3CKC0         //T3 Clock In is on C0
   #fuses SDOB3          //SDO is on RB3
   #fuses MCLR           //Master Clear pin enabled
   #fuses NOLVP          //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
   #fuses NOSTVREN       //Stack full/underflow will not cause reset
   #fuses NOPROTECT      //Code not protected from reading
   #fuses NOCPB          //No Boot Block code protection
   #fuses NOCPD          //No EE protection
   #fuses NOWRT          //Program memory not write protected
   #fuses WRTC           //Configuration registers write protected
   #fuses NOWRTB         //Boot block not write protected
   #fuses NOWRTD         //Data EEPROM not write protected
   #fuses NOEBTR         //Memory not protected from table reads
   #fuses NOEBTRB        //Boot block not protected from table reads

Best regards
temtronic



Joined: 01 Jul 2010
Posts: 6699
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri May 03, 2019 3:31 pm     Reply with quote

from your post..
Quote:
#fuses PBADEN //PORTB pins are configured as analog input channels on RESET

so as Mr. T said........

you'll have to add NO infront of PBADEN

then it might work...
Jay
PCM programmer



Joined: 06 Sep 2003
Posts: 20630

View user's profile Send private message

PostPosted: Fri May 03, 2019 4:20 pm     Reply with quote

I don't think that's going to be it. Reason:
PCH vs. 5.078 has init code inserted by the compiler that sets
all i/o ports to digital. Example:
Code:

.................... void main() 
0004:  CLRF   TBLPTRU
0006:  BCF    RCON.IPEN
0008:  MOVLW  50
000A:  MOVWF  OSCCON
000C:  CLRF   OSCCON2
000E:  CLRF   OSCTUNE
0010:  MOVLB  F
0012:  CLRF   x5B  // ANSELA = 0
0014:  CLRF   x5C  // ANSELB = 0
0016:  CLRF   x5D  // ANSELC = 0
0018:  CLRF   x5E  // ANSELD = 0
001A:  CLRF   x5F  // ANSELE = 0
001C:  CLRF   CM2CON1
001E:  CLRF   CM2CON0
0020:  CLRF   CM1CON0

Test program:
Code:
#include<18F45K50.h>
#fuses NOWDT, PBADEN
#use delay(internal=4M)

//=================================
void main()
{

while(TRUE);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 14424

View user's profile Send private message

PostPosted: Sat May 04, 2019 1:31 am     Reply with quote

I think if the fuse if actually set to PBADEN, the compiler boot code
won't set the ports to digital. That is the 'default', but not when you explicitly
set the port to analog....
Cogitum



Joined: 22 Mar 2012
Posts: 59
Location: France (Paris)

View user's profile Send private message

18F4550 & 18F45K50 bootloader problem ? COMPILER 5.078
PostPosted: Sat May 04, 2019 3:30 am     Reply with quote

Hello,
First of all I want to thank you all 3 for your help!
NOPBADEN does not change anything ... the LEDs do not blink.
SIOW display BOOTLOADER READY 18F45K50 !

After loading the software "BLINK LED" SIOW display nothing "

No more BOOTLOADER READY 18F45K50 display is black !
The same process with 18F4550 works.


Best regards
Ttelmah



Joined: 11 Mar 2010
Posts: 14424

View user's profile Send private message

PostPosted: Sat May 04, 2019 7:42 am     Reply with quote

Show your clock configuration, and tell us what the clock source is.
How is the bootloader triggered?. Input pin?. What pin?.
How big is the bootloader?. (compile it an look at the lst file).
One thought going through my head is that the size being reserved by
'usb_bootloader.h', is not actually large enough for your bootloader.
in the K50.
Cogitum



Joined: 22 Mar 2012
Posts: 59
Location: France (Paris)

View user's profile Send private message

PostPosted: Sat May 04, 2019 7:52 am     Reply with quote

HI Ttelmah,
You will find below the files I use:

///////////////////////////////////////////////////////////////////////////
//// EX_USB_BOOTLOADER.C ////
//// ////
//// This program is an example stand alone USB bootloader. It uses ////
//// the communication device class (CDC) that creates a virtual COM ////
//// port on your PC. The bootloader contains it's own set of USB ////
//// and bootloader code in low memory, the high memory will contain ////
//// the application (and if you are developing a USB application, ////
//// this will hold it's own USB code). ////
//// ////
//// This program must be loaded into a target chip using a device ////
//// programmer. Afterwards this program may be used to load new ////
//// versions of the application program. There is a specific ////
//// protocol used by the bootloader, SIOW.EXE uses this protocol ////
//// if you use the 'Download Software' option. ////
//// ////
//// Compiling this program gives you a HEX file of just the loader. ////
//// To load the application you have two choices: a.) use the loader ////
//// to load the application, or b.) merge the application HEX and ////
//// loader HEX into one production HEX. The #import() CCS command ////
//// will help you perfrom the latter. Be aware that if you do the ////
//// former the PIC will get it's configuration bit settings (fuses) ////
//// from the loader and not the application (this bootloader does ////
//// not change the configuration bits) ////
//// ////
//// To create an application that is compatible with this loader ////
//// simply #include usb_bootloader.h into your application. This ////
//// loader does support interrupts in your application. ////
//// ////
//// This bootloader is designed to detect pin A4 low on reset (this ////
//// is the push-button on the CCS USB Development kit). If A4 is ////
//// low it starts the USB CDC stack and checks for bootload ////
//// information over USB. Otherwise the application program is ////
//// started. ////
//// ////
//// If SEND_BOOTLOADER_IDLE_MESSAGE is #defined, then every 3s ////
//// it will send a "Bootloader Ready" message over USB. It will ////
//// stop sending this once it receives a character over USB. ////
//// ////
//// This is a port of ex_bootloader.c, which is a serial bootloader. ////
//// Since CDC makes it easy to port legacy applications, the ////
//// previous example makes the bulk of this code. ////
//// ////
//// This file is part of CCS's PIC USB driver code. See USB.H ////
//// for more documentation and a list of examples. ////
//// ////
///////////////////////////////////////////////////////////////////////////
//// ////
//// VERSION HISTORY ////
//// ////
//// January 26th, 2017: ////
//// Added support for PCM. ////
//// Added SEND_BOOTLOADER_IDLE_MESSAGE option. ////
//// ////
//// July 9th, 2012: ////
//// Added support for PCD. ////
//// No longer uses interrupts (polling). This simplifies the ////
//// interrupt support in your application, because they don't ////
//// need to be shared. ////
//// ////
//// Oct 15th, 2010: ////
//// Potential bug in CDC that causes lost PIC->PC chars resolved? ////
//// Added initial 18F47J53 family support. ////
//// Added USB_ISR_POLLING support. Define this and interrupts ////
//// will not be used. usb_task() must be called periodically ////
//// in your main loop. If it is not called faster than once ////
//// per millisecond, USB may not work (PIC18 and PIC24 only). ////
//// ////
//// April 28th, 2010: ////
//// Added ex_usb_common.h. ////
//// Initial support for CCS PIC24USB board. ////
//// ////
//// March 5th, 2009: ////
//// Cleanup for Wizard. ////
//// PIC24 Initial release. ////
//// ////
///////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2009 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS ////
//// C compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, ////
//// reproduction or distribution is permitted without written ////
//// permission. Derivative programs created using this software ////
//// in object code form are not restricted in any way. ////
///////////////////////////////////////////////////////////////////////////

// option - usually enabled.
// see documentation above.
#define SEND_BOOTLOADER_IDLE_MESSAGE

// option - usually not enabled.
// see documentation in usb_bootloader.h
//#define USB_BOOTLOADER_CONFIG_BITS_WRITABLE

///////////////////////

#include <ex_usb_common.h>

/*
Configure, then load the bootloader definitions
*/

#define _bootloader
#include "usb_bootloader.h"
//#include "loader.c"
// Includes all USB code and interrupts, as well as the CDC API
#include <usb_cdc.h>

#if defined(USB_BOOTLOADER_CONFIG_BITS_WRITABLE)
#define ROM_WRITE_CAN_MODIFY_CONFIG_BITS
#endif

#include <rom_write.c>

#define BUFFER_LEN_LOD 64

#define ACKLOD 0x06
#define XON 0x11
#define XOFF 0x13

// Convert two hex characters to a int8
unsigned int8 atoi_b16(char *s)
{
char c;
unsigned int8 result = 0;
int i;

for (i=0; i<2; i++,s++)
{
c = *s;
if (c >= 'A')
result = 16*result + c - 'A' + 10;
else
result = 16*result + c - '0';
}

return(result);
}

#if defined(SEND_BOOTLOADER_IDLE_MESSAGE)
#include <tick.c>
#endif

void load_program(void)
{
int1 do_ACKLOD, done=FALSE;
unsigned int8 checksum, line_type;
unsigned int16 l_addr,h_addr=0;
unsigned int8 to;
unsigned int32 addr;
unsigned int8 dataidx, i, count;
unsigned int8 data[32];
unsigned int8 buffidx;
char buffer[BUFFER_LEN_LOD];
#if defined(SEND_BOOTLOADER_IDLE_MESSAGE)
TICK t;
int1 doSend;

t = TickGet();
doSend = 1;
#endif

while (!done) // Loop until the entire program is downloaded
{
usb_task();

#if defined(SEND_BOOTLOADER_IDLE_MESSAGE)
if
(
doSend &&
TickIsExpired(t, TICKS_PER_SECOND * 3)
)
{
t = TickGet();
printf(usb_cdc_putc_fast, "BOOTLOADER READY 18F45K50 \r\n");
//! printf(usb_cdc_putc_fast, " 18F45K50 !\r\n");
}
#endif

if (!usb_cdc_kbhit())
continue;

#if defined(SEND_BOOTLOADER_IDLE_MESSAGE)
doSend = 0;
#endif

buffidx = 0; // Read into the buffer until 0x0D ('\r') is received or the buffer is full
to = 250; //250 milliseconds

do
{
if (!usb_cdc_kbhit())
{
delay_ms(1);
usb_task();
to--;
if (!to)
break;
}
else
{
to = 250;
i = usb_cdc_getc();
buffer[buffidx++] = i;
if ((i == 0x0D) || (i == 0x0A))
break;
}
} while (buffidx <= BUFFER_LEN_LOD);

if (!to)
continue;

usb_cdc_putc(XOFF); // Suspend sender
usb_task();

do_ACKLOD = TRUE;

// Only process data blocks that start with ':'
if (buffer[0] == ':')
{
checksum = 0; // Sum the bytes to find the check sum value

for (i=1; i<(buffidx-3); i+=2)
checksum += atoi_b16 (&buffer[i]);
checksum = 0xFF - checksum + 1;

if (checksum != atoi_b16 (&buffer[buffidx-3]))
do_ACKLOD = FALSE;
else
{
count = atoi_b16(&buffer[1]); // Get the number of bytes from the buffer

// Get the lower 16 bits of address
l_addr = make16(atoi_b16(&buffer[3]),atoi_b16(&buffer[5]));

line_type = atoi_b16 (&buffer[7]);

addr = make32(h_addr,l_addr) / (__ADDRESS__)_LOADER_BPA;

// If the line type is 1, then data is done being sent
if (line_type == 1)
done = TRUE;
else if (line_type == 4)
h_addr = make16(atoi_b16(&buffer[9]), atoi_b16(&buffer[11]));
else if ((line_type == 0) && (addr >= (int32)APPLICATION_START))
{
// Loops through all of the data and stores it in data
// The last 2 bytes are the check sum, hence buffidx-3
for (i = 9,dataidx=0; i < buffidx-3; i += 2)
data[dataidx++]=atoi_b16(&buffer[i]);

rom_modify(addr, data, count);
}
}
}
if(do_ACKLOD)
usb_cdc_putc(ACKLOD);

usb_cdc_putc(XON);
}

rom_flush();

usb_cdc_putc(ACKLOD);
usb_cdc_putc(XON);

while(!usb_cdc_put_buffer_free())
{
usb_task();
}

to = 250;
while(to) //give time for packet to flush
{
delay_ms(1);
usb_task();
to--;
}

reset_cpu();
}

#if !defined(LED3) && defined(LED1)
#define LED3 LED1
#endif

void main(void)
{
HW_INIT();

LEDS_OFF();

//we use a button as an event to determine if we should start the USB CDC
//bootloader. if button is not pressed then goto the application, else if
//button is pressed then do the bootloader.
if(BUTTON_PRESSED())
{
usb_init_cs();

while(!usb_enumerated())
{
// necessary for polling function to work
#ifdef USB_ISR_POLLING
usb_task();
#endif
}

#if defined(LED3)
LED_ON(LED3);
#endif

load_program();
}
while(TRUE)
{
//!output_toggle(PIN_E0);//hard OK
//!delay_ms(100);
}
goto_address(APPLICATION_START);
}

#if defined(__PCD__)
#int_default
#else
#int_global
#endif
void isr(void)
{
jump_to_isr(APPLICATION_ISR);
}

//=======================================//
/////////////////////////////////////////////////////////////////////////
//// ////
//// ex_usb_common.h ////
//// ////
//// Common hardware definitions and configuration for all of CCS's ////
//// USB example programs (ex_usb_*.c) ////
//// ////
//// One of the following configuration macros must be #defined: ////
//// USB_HW_CCS_PIC18F4550 CCS PIC18F4550 USB Development kit ////
//// USB_HW_CCS_PIC18F45K50 CCS PIC18F45K50 USB Development kit ////
//// USB_HW_CCS_PIC24F CCS 24FJ256GB206 USB Development kit. ////
//// USB_HW_CCS_E3 CCS E3 Mini development kit. ////
//// USB_HW_CCS_16F1459 CCS Rapid USB Development kit. ////
//// PIC16F1459 using internal oscillator. ////
//// USB_HW_MCHP_16F1459 Microchip low-pin count USB ////
//// development kit with a PIC16F1459. ////
//// NOTE: the ICD pins on the low pin ////
//// count development kit are not ////
//// compatible with this chip! ////
//// USB_HW_CCS_USBN9604 CCS National USBN9604 USB development ////
//// kit (external USB peripheral). ////
//// USB_HW_MCHP_18F14K50 Microchip low-pin count USB ////
//// development kit (PIC18F14K50). ////
//// USB_HW_MCHP_18F46J50 Microchip USB PIM Demo Board ////
//// (PIC18F46J50). ////
//// USB_HW_GENERIC_18F67J50 Generic 18F67J50 example. ////
//// USB_HW_GENERIC_18F27J53 Generic 18F27J53 example. ////
//// USB_HW_GENERIC_18F45K50 Generic 18F45K50 example. ////
//// USB_HW_GENERIC_18F67J94 Generic 18F67J94 example. ////
//// USB_HW_MCHP_EXPLORER16_24F Microchip Explorer16 with USB ////
//// OTG PICTail Plus. ////
//// USB_HW_MCHP_EXPLORER16_24E Microchip Explorer16 with USB ////
//// OTG PICTail Plus. ////
//// USB_HW_CCS_24FJ64GC006 CCS PIC24USB development kit, ////
//// PIC replaced with PIC24FJ64GC006 and grounding ////
//// pin 21 (SVss). ////
//// USB_HW_CCS_24FJ1024GB606 CCS PIC24USB development kit, ////
//// PIC replaced with PIC24FJ1024GB606. ////
//// USB_HW_CCS_18F4523 The PIC16F1459 on the CCS 18F4523 ////
//// development kit. ////
//// ////
//// This file is part of CCS's PIC USB driver code. See USB.H ////
//// for more documentation and a list of examples. ////
//// ////
/////////////////////////////////////////////////////////////////////////
//// ////
//// VERSION HISTORY ////
//// ////
//// May 13th, 2016: ////
//// Addded USB_HW_CCS_24FJ1024GB606. ////
//// ////
//// April 6th, 2016: ////
//// Replaced USB_HW_GENERIC_24FJ128GC006 with ////
//// USB_HW_CCS_24FJ64GC006. ////
//// ////
//// December 15th, 2015: ////
//// Added USB_HW_CCS_E3. But this kit comes with a USB CDC ////
//// bootloader, so in most cases you may want to use the e3.h ////
//// include file that comes with the compiler instead of ////
//// using this. ////
//// ////
//// June 30, 2015: ////
//// Added USB_HW_CCS_18F4523 ////
//// ////
//// Sep 18, 2014: ////
//// Added USB_HW_CCS_PIC18F45K50 ////
//// ////
//// Dec 17, 2013: ////
//// Added USB_HW_GENERIC_18F67J94 ////
//// Added USB_HW_GENERIC_24FJ128GC006 ////
//// ////
//// July 1, 2013: ////
//// Added USB_HW_CCS_16F1459 ////
//// ////
//// Feb 22, 2013: ////
//// Added USB_HW_GENERIC_18F45K50 ////
//// ////
//// Feb 15th, 2013: ////
//// Removed USB_HW_CCS_PIC16C765, added USB_HW_MCHP_16F1459 ////
//// ////
//// Mar 7th, 2012: ////
//// Renamed USB_HW_MCHP_EXPLORER16 to USB_HW_MCHP_EXPLORER16_24F. ////
//// Added USB_HW_MCHP_EXPLORER16_24E. ////
//// ////
//// Oct 15th, 2010: ////
//// Added initial 18F47J53 family support. ////
//// Added USB_ISR_POLLING support. Define this and interrupts ////
//// will not be used. usb_task() must be called periodically ////
//// in your main loop. If it is not called faster than once ////
//// per millisecond, USB may not work (PIC18 and PIC24 only). ////
//// ////
//// August 31st, 2010: ////
//// Added USB_HW_MCHP_18F46J50, USB_HW_MCHP_18F14K50 and ////
//// USB_HW_GENERIC_18F67J50 hardware. ////
//// ////
//// April 28th, 2010: ////
//// Added ex_usb_common.h. ////
//// Initial support for CCS PIC24USB board. ////
//// USB_CON_SENSE_PIN replaced with USB_CABLE_IS_ATTACHED() ////
//// macro. If USB_CON_SENSE_PIN is defined, it will create ////
//// USB_CABLE_IS_ATTACHED() macro for you (for backwards ////
//// compatibility). ////
//// ////
/////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2010 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS ////
//// C compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, ////
//// reproduction or distribution is permitted without written ////
//// permission. Derivative programs created using this software ////
//// in object code form are not restricted in any way. ////
/////////////////////////////////////////////////////////////////////////

////// Begin User Configuration

//#define USB_HW_CCS_PIC18F4550 //CCS PIC18F4550 USB Development kit
#define USB_HW_CCS_PIC18F45K50 //CCS PIC18F45K50 USB Development kit
//#define USB_HW_CCS_E3 //CCS E3 Mini development kit. 18F14K50 with 12Mhz crystal.
//#define USB_HW_CCS_16F1459 //CCS Rapid USB devleopment kit. 16F1459 with no oscillator
//#define USB_HW_CCS_PIC24F //CCS 24FJ256GB206 USB Development kit
//#define USB_HW_CCS_PIC33EP //CCS PIC24 USB Development kit, PIC replaced with a 33EP256MU806
//#define USB_HW_CCS_USBN9604 //CCS National USBN9604 USB development kit (external USB peripheral)
//#define USB_HW_MCHP_18F14K50 //Microchip low-pin count USB development kit (PIC18F14K50)
//#define USB_HW_MCHP_18F46J50 //Microchip USB PIM Demo Board (PIC18F46J50)
//#define USB_HW_GENERIC_18F67J50 //Generic 18F67J50 example
//#define USB_HW_GENERIC_18F27J53 //Generic 18F27J53 example
//#define USB_HW_MCHP_EXPLORER16_24F //Microchip Explorer16 with USB OTG PICTail+ & 24FJ256GB110
//#define USB_HW_MCHP_EXPLORER16_24E //Microchip Explorer16 with USB OTG PICTail+
//#define USB_HW_MCHP_16F1459 //Microchip low-pin count USB development kit with a 16F1459, using internal oscillator with active clock tuning NOTE: the ICD pins on the low pin count development kit are not compatible with this chip!
//#define USB_HW_GENERIC_18F45K50 //Generic 18F45K50 example. You can get this by taking a CCS 4550 board and replacing the PIC with a 45K50 and the crystal wiht a 12Mhz
//#define USB_HW_GENERIC_18F67J94 //Generic 18F67J94 example.
//#define USB_HW_CCS_24FJ64GC006 //CCS PIC24USB development kit, PIC replaced with PIC24FJ64GC006 and grounding pin 21 (SVss)
//#define USB_HW_CCS_24FJ1024GB606 //CCS PIC24USB development kit, PIC replaced with PIC24FJ1024GB606
//#define USB_HW_MCHP_24FJ1024GB610 //Microchip Explorer 16 with 24FJ1024GB610 PIM and USB OTG PICtail+
//#define USB_HW_CCS_18F4523 //The PIC16F1459 on the 18F4523 development kit

// Optional configuration.
// Defining USB_ISR_POLLING will have USB library not use ISRs. Instead you
// must periodically call usb_task().
//#define USB_ISR_POLLING

////// End User Configuration

#ifndef __EX_USB_COMMON_H__
#define __EX_USB_COMMON_H__

#if defined(USB_HW_CCS_PIC18F4550)
#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)

//leds ordered from bottom to top
#DEFINE LED1 PIN_A5 //green
#define LED2 PIN_B5 //yellow OK HARD
#define LED3 PIN_A1 //red B5 ?
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3)
#define BUTTON_PRESSED() !input(PIN_B4)// initial A4 OK HARD

//see section below labeled USB_CABLE_IS_ATTACHED
#define PIN_USB_SENSE PIN_B3 // ok hard

#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL
#define HW_ADC_CHANNEL 0
#define HW_ADC_PORTS AN0

#define HW_INIT() setup_adc_ports(HW_ADC_PORTS)
#endif

#if defined(USB_HW_CCS_PIC18F45K50)
#include <18F45K50.h>

#if 1
// use the external crystal on the board
#use delay(crystal=16MHz, clock=48MHz, USB_FULL)
#else
// use the internal oscillator with active clock tuning
#use delay(int, clock=48MHz, USB_FULL, act=USB)
#endif

#fuses WDT_SW //No Watch Dog Timer, enabled in Software
#fuses CCP2C1 //CCP2 input/output multiplexed with RC1
#fuses PUT //Power Up Timer
#fuses BROWNOUT_SW //Brownout controlled by configuration bit in special file register
#fuses BORV19 //Brownout reset at 1.9V
#fuses NOPBADEN //PORTB pins are configured as analog input channels on RESET
#fuses T3CKC0 //T3 Clock In is on C0
#fuses SDOB3 //SDO is on RB3
#fuses MCLR //Master Clear pin enabled
#fuses NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#fuses NOSTVREN //Stack full/underflow will not cause reset
#fuses NOPROTECT //Code not protected from reading
#fuses NOCPB //No Boot Block code protection
#fuses NOCPD //No EE protection
#fuses NOWRT //Program memory not write protected
#fuses WRTC //Configuration registers write protected
#fuses NOWRTB //Boot block not write protected
#fuses NOWRTD //Data EEPROM not write protected
#fuses NOEBTR //Memory not protected from table reads
#fuses NOEBTRB //Boot block not protected from table reads

//leds ordered from bottom to top
#DEFINE LED1 PIN_A5 //green
#define LED2 PIN_A4 //yellow D4
#define LED3 PIN_B5 //red
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3)
#define BUTTON_PRESSED() !input(PIN_B4)// initial B4

//see section below labeled USB_CABLE_IS_ATTACHED
#define PIN_USB_SENSE PIN_D4 // B2

#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL
#define HW_ADC_CHANNEL 0 //PIN_A0
#define HW_ADC_PORTS sAN0

#define HW_INIT() setup_adc_ports(HW_ADC_PORTS)
#endif

#if defined(USB_HW_CCS_16F1459)
#include <16F1459.h>

#use delay(int=8MHz, clock=48MHz, USB_FULL, act=USB)

#fuses MCLR
#fuses PUT
#fuses WDT_SW //No Watch Dog Timer, enabled in Software
#fuses NOPROTECT //Code not protected from reading
#fuses NOBROWNOUT //No brownout reset
#fuses NOWRT //Program memory not write protected, otherwise bootloader won't work
#fuses NOSTVREN //Stack full/underflow will not cause reset
#fuses NOLPBOR //Low-Power Brownout reset is disabled
#fuses NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O

//leds are ordered left to right
#define LED_DIR1 PIN_A5
#define LED_DIR2 PIN_C6
#define LED_ON(x) delay_cycles(1)
#define LED_OFF(x) delay_cycles(1)
#define LEDS_OFF() output_low(LED_DIR1); output_low(LED_DIR2)
#define LEDS_GREEN() LEDS_OFF(); output_high(LED_DIR1)
#define LEDS_RED() LEDS_OFF(); output_high(LED_DIR2)
#define LEDS_TOGGLE() output_toggle(LED_DIR1); output_toggle(LED_DIR2)

#define BUTTON_PRESSED() !input(PIN_C1)

#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL
#define HW_ADC_CHANNEL 11 //PIN_B5
#define HW_ADC_PORTS sAN11

#define HW_INIT() setup_adc_ports(HW_ADC_PORTS); setup_comparator(NC_NC_NC_NC)

#define __NO_UART__

//#define UART_IS_ICD

#if defined(UART_IS_ICD)
#warning Can't use button and ICSP streaming because they share the same pin!
#define UART_ICD_SETTINGS xmit=PIN_C0, rcv=PIN_C1, baud=57600
#endif
#endif

#if defined(USB_HW_CCS_E3)
#include <18F14K50.h>

#use delay(crystal=12MHz, clock=48MHz, USB_FULL)

#fuses NOWDT

#define LED1 PIN_C6 //green
#define LED2 PIN_C4 //yellow
#define LED3 PIN_C3 //red
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3)
#define LED_ON(x) output_high(x)
#define LED_OFF(x) output_low(x)
#define BUTTON_PRESSED() !input(PIN_C1)

#define HW_ADC_PORTS sAN4
#define HW_ADC_CHANNEL 4
#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL | ADC_TAD_MUL_2

//hardware UART pins for this pic is PIN_B5 and PIN_B7
#endif

#if defined(USB_HW_CCS_PIC24F)
#include <24FJ256GB206.h>
#build(stack=1024)
#device ICSP=2
#device ADC=8
#fuses HS,PR_PLL,NOWDT,PLL5,SOSC_DIG
#use delay(clock=32M)

#pin_select U1TX = PIN_D5
#pin_select U1RX = PIN_D4

#pin_select SDI2 = PIN_F5
#pin_select SDO2 = PIN_B15
#pin_select SCK2OUT = PIN_F4

#define LED1 PIN_B9 //green
#define LED2 PIN_B10 //yellow
#define LED3 PIN_B11 //red
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3)
#define LED_ON(x) output_high(x)
#define LED_OFF(x) output_low(x)
#define BUTTON_PRESSED() !input(PIN_F0)

#define HW_ADC_PORTS sAN0|sAN2 //AN0/PIN_B0=POT, AN2/PIN_B2=VBus
#define HW_ADC_CHANNEL 0
#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL | ADC_TAD_MUL_31

#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_DATA4 PIN_E6
#define LCD_DATA5 PIN_E7
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#endif

#if defined(USB_HW_MCHP_24FJ1024GB610)
#include <24FJ1024GB610.h>
//#device ICSP=2 //??
#device ADC=8

#use delay(crystal=8MHz, clock=32MHz, USB_FULL)
#fuses NOWDT

#pin_select U1TX = PIN_F5
#pin_select U1RX = PIN_F4
#define PIN_UART1_RTS PIN_F13 //out to PC
#define PIN_UART1_CTS PIN_F12 //in to PIC

// led's ordered from left to right
#define LED1 PIN_A7
#define LED2 PIN_A6
#define LED3 PIN_A5
#define LED4 PIN_A4
#define LED5 PIN_A3
#define LED6 PIN_A2
#define LED7 PIN_A1
#define LED8 PIN_A0
#define LED_ON(x) output_high(x)
#define LED_OFF(x) output_low(x)
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3); LED_OFF(LED4); LED_OFF(LED5); LED_OFF(LED6); LED_OFF(LED7); LED_OFF(LED8)
#define BUTTON_PRESSED() !input(PIN_D6)

#define HW_ADC_PORTS sAN5
#define HW_ADC_CHANNEL 5
#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL | ADC_TAD_MUL_31
#endif

#if defined(USB_HW_CCS_24FJ1024GB606)
#include <24FJ1024GB606.h>
#device ICSP=2
#device ADC=8

#use delay(crystal=20MHz, clock=32MHz, USB_FULL)
#fuses NOWDT

#pin_select U1TX = PIN_D5
#pin_select U1RX = PIN_D4

#pin_select SDI2 = PIN_F5
#pin_select SDO2 = PIN_B15
#pin_select SCK2OUT = PIN_F4

#define LED1 PIN_B9 //green
#define LED2 PIN_B10 //yellow
#define LED3 PIN_B11 //red
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3)
#define LED_ON(x) output_high(x)
#define LED_OFF(x) output_low(x)
#define BUTTON_PRESSED() !input(PIN_F0)

#define HW_ADC_PORTS sAN0|sAN2 //AN0/PIN_B0=POT, AN2/PIN_B2=VBus
#define HW_ADC_CHANNEL 0
#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL | ADC_TAD_MUL_31

#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_DATA4 PIN_E6
#define LCD_DATA5 PIN_E7
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7

#define HW_INIT() setup_adc_ports(HW_ADC_PORTS)
#endif

#if defined(USB_HW_CCS_PIC33EP)
#include <33EP256MU806.h>
#build(stack=1024)
#device ADC=8
//20MHz clock is scaled to 48mhz for usb clock.
//20mhz clock is scaled to 120mhz for cpu operation.
#use delay(crystal=20Mhz, clock=120Mhz, AUX:clock=48Mhz)
#fuses NOWDT

#pin_select U1TX = PIN_D5
#pin_select U1RX = PIN_D4

#define LED1 PIN_B9 //green
#define LED2 PIN_B10 //yellow
#define LED3 PIN_B11 //red
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3)
#define LED_ON(x) output_high(x)
#define LED_OFF(x) output_low(x)
#define BUTTON_PRESSED() !input(PIN_F0)

#define HW_ADC_PORTS sAN0|sAN2 //0=pot, 2=vbus
#define HW_ADC_CHANNEL 0
#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL | ADC_TAD_MUL_31

#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_DATA4 PIN_E6
#define LCD_DATA5 PIN_E7
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#endif

#if defined(USB_HW_GENERIC_18F45K50)
#include <18F45K50.h>

#fuses NOWDT

//#use delay(crystal=12Mhz, clock=48Mhz, USB_FULL)
#fuses NOCPUDIV, PLLEN, PLL4X, HSH, PRIMARY, NOIESO, NOFCMEN //12mhz crystal, 48mhz clock for pic and usb
#use delay(clock=48MHz)

//leds ordered from bottom to top
#DEFINE LED1 PIN_A5 //green
#define LED2 PIN_D4 //yellow
#define LED3 PIN_B5 //red
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3)
#define BUTTON_PRESSED() !input(PIN_A4)

//see section below labeled USB_CABLE_IS_ATTACHED
#define PIN_USB_SENSE PIN_B2

#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL
#define HW_ADC_CHANNEL 0
#define HW_ADC_PORTS sAN0
#endif

#if defined(USB_HW_MCHP_EXPLORER16_24E)
#include <24EP512GU810.h>
#build(stack=1024)
#device ADC=8

#fuses NOWDT, NOIESO

//8MHz clock is scaled to 48mhz for usb clock.
//8mhz clock is scaled to 120mhz for cpu operation.
#use delay(crystal=8Mhz, clock=120Mhz, AUX:clock=48Mhz)

// no pin select compatible.
#pin_select U1TX = PIN_F5
#pin_select U1RX = PIN_F4
#define PIN_UART1_RTS PIN_F13 //out to PC
#define PIN_UART1_CTS PIN_F12 //in to PIC

// led's ordered from right to left
#define LED1 PIN_A0 //D3
#define LED2 PIN_A1 //D4
#define LED3 PIN_A2 //D5
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3)
#define LED_ON(x) output_high(x)
#define LED_OFF(x) output_low(x)

#define BUTTON_PRESSED() !input(PIN_D6) //s3

#define HW_ADC_PORTS sAN5
#define HW_ADC_CHANNEL 5
#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL | ADC_TAD_MUL_31

/*
#byte ACLKCON3=getenv("SFR:ACLKCON3")
#byte ACLKDIV3=getenv("SFR:ACLKDIV3")
#bit ACLKCON3_APLLCK=ACLKCON3.14
#bit ACLKCON3_ENAPLL=ACLKCON3.15
//#define HW_INIT() while(!ACLKCON3_APLLCK)
#define HW_INIT() APllInit()
void ApllInit(void)
{
ACLKCON3 = 0x24C1;
ACLKDIV3 = 0x7;
ACLKCON3_ENAPLL = 1;
while(!ACLKCON3_APLLCK);
}
*/
#endif

#if defined(USB_HW_MCHP_EXPLORER16_24F)
#include <24FJ256GB110.h>
#build(stack=1024)
#device ADC=8
#fuses HS,PR_PLL,NOWDT,DISUVREG,ICSP2
#use delay(clock=32M) //8MHz clock is 4x because of PLL
#fuses PLL2 //Divide 8MHz by 2 to get the 4MHz required for USB

#pin_select U1TX = PIN_F5
#pin_select U1RX = PIN_F4
#define PIN_UART1_RTS PIN_F13 //out to PC
#define PIN_UART1_CTS PIN_F12 //in to PIC

// led's ordered from left to right
#define LED1 PIN_A7
#define LED2 PIN_A6
#define LED3 PIN_A5
#define LED4 PIN_A4
#define LED5 PIN_A3
#define LED6 PIN_A2
#define LED7 PIN_A1
#define LED8 PIN_A0
#define LED_ON(x) output_high(x)
#define LED_OFF(x) output_low(x)
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3); LED_OFF(LED4); LED_OFF(LED5); LED_OFF(LED6); LED_OFF(LED7); LED_OFF(LED8)
#define BUTTON_PRESSED() !input(PIN_D6)

#define HW_ADC_PORTS sAN5
#define HW_ADC_CHANNEL 5
#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL | ADC_TAD_MUL_31
#endif

#if defined(USB_HW_CCS_USBN9604)
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

#define __USB_PIC_PERIF__ 0

#DEFINE LED1 PIN_B3
#define LED2 PIN_D4
#define LED3 PIN_B5
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3)
#define BUTTON_PRESSED() !input(PIN_A4)

#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL
#define HW_ADC_CHANNEL 0
#define HW_ADC_PORTS AN0
#endif

#if defined(USB_HW_MCHP_16F1459)
//NOTE: the ICD pins on the low pin count development kit are not compatible with this chip!
#include <16F1459.h>

#use delay(int=8MHz, clock=48MHz, USB_FULL, act=USB)

#fuses NOMCLR
#fuses NOWDT

//leds are ordered left to right
#define LED1 PIN_C3
#define LED2 PIN_C2
#define LED3 PIN_C1
#define LED4 PIN_C0
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3); LED_OFF(LED4)
#define LED_ON(x) output_high(x)
#define LED_OFF(x) output_low(x)

#if getenv("FUSE_SET:NOMCLR")
#define BUTTON_PRESSED() !input(PIN_A3)
#else
#define BUTTON_PRESSED() FALSE
#endif

#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL
#define HW_ADC_CHANNEL 10
#define HW_ADC_PORTS sAN10
#endif

#if defined(USB_HW_MCHP_18F14K50)
#include <18F14K50.h>

#fuses NOIESO
#fuses NOFCMEN
#fuses PCLKEN
#fuses NOPUT
#fuses NOBROWNOUT
#fuses NOWDT
#fuses NOMCLR
#fuses NOHFOFST
#fuses NOLVP
#fuses NOXINST

// configure 12MHz clock for USB operation and 48Mhz CPU operation
#fuses HS
#fuses PLLEN //enable pll, we can now use CPUDIVx fuses
#fuses CPUDIV1 //CPU clock set to 48MHz
#fuses USBDIV2 //when using low speed USB, need to divide 12MHz by 2. this fuse has no effect when using full speed.

#use delay(clock=48000000)

//leds are ordered left to right
#define LED1 PIN_C3
#define LED2 PIN_C2
#define LED3 PIN_C1
#define LED4 PIN_C0
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3); LED_OFF(LED4)
#define LED_ON(x) output_high(x)
#define LED_OFF(x) output_low(x)
#define BUTTON_PRESSED() !input(PIN_A3)

#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL
#define HW_ADC_CHANNEL 10
#define HW_ADC_PORTS sAN10
#endif

#if defined(USB_HW_MCHP_18F46J50)
#include <18F46J50.h>

#fuses NOWDT
#fuses NOXINST
#fuses NODEBUG
#fuses NOPROTECT

#fuses HSPLL
#fuses PLL3 //set usb pll clock to 4MHz (from 12Mhz input)
#fuses NOCPUDIV //set cpu clock to 48MHz
#use delay(clock=48MHz)

#DEFINE LED1 PIN_E0
#define LED2 PIN_E1
#define LED3 PIN_D7 //requires PIC18 Explorer
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3)
#define LED_ON(x) output_high(x)
#define LED_OFF(x) output_low(x)
#define BUTTON_PRESSED() !input(PIN_B2)

// pot is on PIC18 Explorer
#define HW_ADC_PORTS sAN0
#define HW_ADC_CHANNEL 0
#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL

// uart/rs232 requires PIC18 Explorer

//#define PIN_USB_SENSE PIN_C2 //Jumper JP2 needs to short 'R' to 'U' position for this to be valid.

#define HW_INIT() setup_oscillator(OSC_PLL_ON); delay_ms(50)
#endif

#if defined(USB_HW_GENERIC_18F67J50)
#include <18F67J50.h>

#fuses NOWDT
#fuses NOXINST

//8Mhz clock configured for USB operation and 48MHz CPU operation
#fuses HSPLL
#fuses PLL2
#fuses NOCPUDIV
#use delay(clock=48M)

#DEFINE LED1 PIN_E4
#define LED2 PIN_E5
#define LED3 PIN_E6
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3)
#define BUTTON_PRESSED() FALSE //no push button on this hardware

#define HW_ADC_PORTS sAN0
#define HW_ADC_CHANNEL 0
#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL

#define PIN_USB_SENSE PIN_B2

#byte OSCTUNE = 0xF9B
#bit PLLEN=OSCTUNE.6
#define HW_INIT() PLLEN=TRUE; delay_ms(50)
#endif

#if defined(USB_HW_GENERIC_18F27J53)
#include <18F27J53.h>

#fuses NOWDT
#fuses NOXINST

//12Mhz clock configured for USB operation and 48MHz CPU operation
#fuses HSPLL
#fuses PLLEN
#fuses PLL3
#fuses NOCPUDIV
#use delay(clock=48M)

#DEFINE LED1 PIN_B5
#define LED2 PIN_D4
#define LED3 PIN_C7
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3)
#define BUTTON_PRESSED() (!input(PIN_B0))

#define PIN_USB_SENSE PIN_A3

#define __NO_UART__

#define HW_ADC_PORTS sAN0|sAN1|sAN2
#define HW_ADC_CHANNEL 0
#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL

#byte OSCTUNE = 0xF9B
#bit PLLEN=OSCTUNE.6

#define HW_INIT() output_low(PIN_C0); output_low(PIN_C1); output_high(PIN_C2); output_high(PIN_B2); PLLEN=TRUE; delay_ms(50)
#endif

#if defined(USB_HW_GENERIC_18F67J94)
#include <18F67J94.h>

#use delay(int=4MHz, clock=64MHz, USB_FULL, act=USB) //internal oscillator with active clock tuning
#fuses NOIESO //Internal External Switch Over mode disabled
#fuses NOCKSNOFSM //Clock Switching is disabled, fail Safe clock monitor is disabled

#define __NO_UART__

#define LED1 PIN_G0
#define LED2 PIN_G1
#define LED3 PIN_G2
#define LEDS_OFF() LED_OFF(LED1); LED_OFF(LED2); LED_OFF(LED3)

//wait for PLL to get stable
#define HW_INIT() delay_ms(72)

#define PIN_USB_SENSE PIN_F7
#endif

#if defined(USB_HW_CCS_24FJ64GC006)
#include <24fj64gc006.h>
#device ICSP=2
#device ADC=8
#fuses NOWDT

//#use delay(crystal=20MHz, clock=32MHz, USB_FULL) //external 20mhz crystal
#use delay(int=8MHz, clock=32MHz, USB_FULL, act=USB) //internal oscillator with active clock tuning

#pin_select U1TX = PIN_D5
#pin_select U1RX = PIN_D4

#pin_select SDI2 = PIN_F5
#pin_select SDO2 = PIN_B15
#pin_select SCK2OUT = PIN_F4

// LEDs on PIC24USB development kit, when changed to a GC006, are
// connected to the SVss and CHx pins and thus we can't use the LEDs.
#define LEDS_OFF()
#define LED_ON(x)
#define LED_OFF(x)

#define BUTTON_PRESSED() !input(PIN_F0)

#define HW_ADC_PORTS sAN0|sAN2 //AN0/PIN_B0=POT, AN2/PIN_B2=VBus
#define HW_ADC_CHANNEL 0
#define HW_ADC_CONFIG ADC_CLOCK_INTERNAL | ADC_TAD_MUL_31

#define LCD_ENABLE_PIN PIN_D0
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_DATA4 PIN_E6
#define LCD_DATA5 PIN_E7
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#endif

#if defined(USB_HW_CCS_18F4523)
#include <16F1459.h>

#use delay(int=8MHz, clock=48MHz, USB_FULL, act=USB)

#fuses MCLR
#fuses PUT
#fuses WDT

#define LED_ON output_high
#define LED_OFF output_low

#define LED1 PIN_C7

#define LEDS_OFF() LED_OFF(LED1)

#define PIN_USB_SENSE PIN_C2

// no user UART. there is a UART tied to the 18F4523.
#define __NO_UART__
#endif

#if defined(__NO_UART__)
#define uart_putc(c)
#define uart_getc() (0)
#define uart_kbhit() (FALSE)
#define uart_printf(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z) { }
#define uart_task()
#else
#ifndef __UART_BAUD__
#define __UART_BAUD__ 9600
#endif
#if defined(PIN_UART_TX)
#use rs232(baud=__UART_BAUD__, xmit=PIN_UART_TX, rcv=PIN_UART_RX, errors)
#elif defined(UART_IS_ICD)
#if defined(UART_ICD_SETTINGS)
#use rs232(icd, UART_ICD_SETTINGS)
#else
#use rs232(icd) //pgd=tx, pgc=rx
#endif
#else
#use rs232(baud=__UART_BAUD__, UART1, errors)
#endif

#define uart_getc getc
#define uart_kbhit kbhit
#define uart_printf printf

//#define __DO_DEBUG_USB
//void debug_putc(char c);

#if defined(UART_USE_TX_BUFFER)
char tbeBuffer[UART_USE_TX_BUFFER];
#if UART_USE_TX_BUFFER>=0x100
unsigned int16 tbeIn=0, tbeOut=0, tbeCount=0;
#else
unsigned int8 tbeIn=0, tbeOut=0, tbeCount=0;
#endif

void uart_putc(char c)
{
if (tbeCount < sizeof(tbeBuffer))
{
tbeCount++;
tbeBuffer[tbeIn++] = c;
if (tbeIn >= sizeof(tbeBuffer))
tbeIn = 0;
}
}

void uart_task(void)
{
char c;
if (tbeCount)
{
tbeCount--;
c = tbeBuffer[tbeOut++];
if (tbeOut >= sizeof(tbeBuffer))
tbeOut = 0;
putc(c);
}
}
#else
void uart_putc(char c) {putc(c);}
#define uart_task()
#endif
#endif

#ifndef LEDS_OFF
#define LEDS_OFF()
#endif

/////////////////////////////////////////////////////////////////////////////
//
// Required macro: USB_CABLE_IS_ATTACHED()
//
// This macro provides configuration to the library to detect if a
// USB cable is attached or not. This is only relevant if the PIC is acting
// as a slave device.
//
// If you are using a USB connection sense method or pin, define this
// macro here. If you are not using connection sense, comment out this line.
// Without connection sense you will not know if the device gets disconnected.
//
// If you are using a PIC24/33 with the internal USB peripheral, you can connect
// the Vbus (5V) line from the USB to the Vbus pin on the PIC - you
// can then look at the SFR bit U1OTGSTAT.SESVD to determine if USB is
// connected.
//
// If you are not using the internal USB peripheral of a PIC24, you can connect
// Vbus to a GPIO to detect connection sense.
//
// For both methods (connecting to Vbus pin or GPIO pin), a pull-down resistor
// (around 150K) and capacitor (1uF) should also be placed on the Vbus pin.
//
/////////////////////////////////////////////////////////////////////////////
#if defined(PIN_USB_SENSE)
#define USB_CABLE_IS_ATTACHED() input(PIN_USB_SENSE)
#elif defined(__PCD__)
#bit U1OTGSTAT_SESVD=getenv("BIT:SESVD")
#define USB_CABLE_IS_ATTACHED() (U1OTGSTAT_SESVD)
#endif

#ifndef LED_ON
#define LED_ON(x) output_low(x)
#endif

#ifndef LED_OFF
#define LED_OFF(x) output_high(x)
#endif

#ifndef HW_INIT
#define HW_INIT()
#endif

#ifndef __USB_PIC_PERIF__
#define __USB_PIC_PERIF__ 1
#endif

#endif
//====================================//
///////////////////////////////////////////////////////////////////////////
//// ////
//// USB_BOOTLOADER.H ////
//// ////
//// This include file must be included by any application loaded ////
//// by the example USB bootloader (ex_usb_bootloader ////
//// ////
//// The directives in this file relocate the reset and interrupt ////
//// vectors as well as reserving space for the bootloader. ////
//// ////
//// Normally the bootloader will not try to the write to the flash ////
//// page that contains the config bits. This page will also be ////
//// blocked from use by the application, since this page is usually ////
//// write protected. If you want to allow the bootloader to write ////
//// to this page and let the application use this page, then ////
//// #define USB_BOOTLOADER_CONFIG_BITS_WRITABLE to enable this ////
//// option. Caution should be taken when using this option. ////
//// Code prior to 2017 was using this option, but starting in 2017 ////
//// the default was to turn this option off. This option has no ////
//// effect on PICs that have config bits stored in a special ////
//// location and not the last flash page. ////
//// ////
//// For more documentation see ex_usb_bootloader.c ////
//// ////
///////////////////////////////////////////////////////////////////////////
//// ////
//// VERSION HISTORY ////
//// ////
//// January 26th, 2017: ////
//// Added PCM support (see ex_usb_bootloader.c). ////
//// Added USB_BOOTLOADER_CONFIG_BITS_WRITABLE option. ////
//// ////
//// July 9th, 2012: ////
//// Added support for PCD (see ex_usb_bootloader.c). ////
//// ////
//// March 5th, 2009: ////
//// Cleanup for Wizard. ////
//// PIC24 Initial release. ////
//// ////
///////////////////////////////////////////////////////////////////////////
//// (C) Copyright 1996,2009 Custom Computer Services ////
//// This source code may only be used by licensed users of the CCS ////
//// C compiler. This source code may only be distributed to other ////
//// licensed users of the CCS C compiler. No other use, ////
//// reproduction or distribution is permitted without written ////
//// permission. Derivative programs created using this software ////
//// in object code form are not restricted in any way. ////
///////////////////////////////////////////////////////////////////////////

#define LOADER_START (0)

#if defined(__PCM__)
#define LOADER_SIZE (0x1000)
#elif defined(__PCH__)
#define LOADER_SIZE (0x2000)
#elif defined(__PCD__)
#define LOADER_SIZE (0x2000)
#else
#error not supported
#endif

#define LOADER_END (LOADER_SIZE-1)

#define APPLICATION_START LOADER_SIZE

#define APPLICATION_ISR (APPLICATION_START+8)

// bytes per address
#if defined(__PCH__)
#define _LOADER_BPA 1
#else //PCM and PCD
#define _LOADER_BPA 2
#endif

#define _LOADER_FIND_PAGE_START(address) ((__ADDRESS__)address & ~(((__ADDRESS__)getenv("FLASH_ERASE_SIZE")/_LOADER_BPA)-(__ADDRESS__)1))

// total size of program memory of this controller, including config bits. this value is in instructions (NOT BYTES)
#if (_LOADER_FIND_PAGE_START(getenv("PROGRAM_MEMORY")) != getenv("PROGRAM_MEMORY"))
#define _LOADER_CONFIG_BITS_IN_ROM 1

#define _LOADER_CONFIG_BITS_PAGE_ADDRESS _LOADER_FIND_PAGE_START(getenv("PROGRAM_MEMORY"))
#else
#define _LOADER_CONFIG_BITS_IN_ROM 0
#endif

#ifdef _bootloader
// bootloader won't use interrupts, instead it will poll USB IF
#define USB_ISR_POLLING

/*
Provide an empty application, so if you load this .HEX file into the pic
without an application this will prevent the pic from executing unknown code.
*/
#org APPLICATION_START,APPLICATION_START+0x3F
void BlankApplication(void)
{
while(TRUE);
}

#define APPLICATION_END (getenv("PROGRAM_MEMORY")-1)

#org APPLICATION_START+0x40, APPLICATION_END {}

#define USB_STRINGS_OVERWRITTEN

#define USB_CONFIG_PID 0x000c

// Here is where the "CCS" Manufacturer string and "SERIAL DEMO" are stored.
// Strings are saved as unicode.
// These strings are mostly only displayed during the add hardware wizard.
// Once the operating system drivers have been installed it will usually display
// the name from the drivers .INF.
char const USB_STRING_DESC[]={
//string 0
4, //length of string index
0x03, //USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING)
0x09,0x04, //Microsoft Defined for US-English
//string 1 - manufacturer
8, //length of string index
0x03, //USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING)
'C',0,
'C',0,
'S',0,
//string 2 - product
38, //length of string index
0x03, //USB_DESC_STRING_TYPE, //descriptor type 0x03 (STRING)
'C',0,
'C',0,
'S',0,
' ',0,
'C',0,
'D',0,
'C',0,
' ',0,
'B',0,
'o',0,
'o',0,
't',0,
'l',0,
'o',0,
'a',0,
'd',0,
'e',0,
'r',0
};
#endif //_bootloader

#ifndef _bootloader
//in the application, this moves the reset and isr vector out of the bootload
//space. it then reserves the loader space from being used by the application.
#build(reset=APPLICATION_START, interrupt=APPLICATION_ISR)

#org 0, LOADER_END {}

#if ((_LOADER_CONFIG_BITS_IN_ROM==1) && !defined(USB_BOOTLOADER_CONFIG_BITS_WRITABLE))
// the page containing the config bits is usually write protected.
// so prevent the application from using this page.
#org _LOADER_CONFIG_BITS_PAGE_ADDRESS,getenv("PROGRAM_MEMORY")-1 {}
#endif
#endif
PCM programmer



Joined: 06 Sep 2003
Posts: 20630

View user's profile Send private message

Re: 18F4550 & 18F45K50 bootloader problem ? Compiler 5.
PostPosted: Sat May 04, 2019 7:57 am     Reply with quote

Cogitum wrote:

#include <18F45K50.h>
#include <usb_bootloader.h>
//#include <ex_usb_hid.c>

#use delay(crystal=16MHz)
//******************************************************
#fuses none
//*********************************************************
//#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)

// #use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
void main(void) {
{
while(TRUE)
{
output_toggle(PIN_E0);//LED1
delay_ms(100);
output_toggle(PIN_E1);//LED2 RED
delay_ms(100);
}
}
}

You have two #use delay() statements. Why ? Use only one.

Also, read this thread, where Ttelmah explains the #fuses must be the
same.
http://www.ccsinfo.com/forum/viewtopic.php?p=165610

Also, read about using: #fuses none
https://www.ccsinfo.com/forum/viewtopic.php?t=56155
Cogitum



Joined: 22 Mar 2012
Posts: 59
Location: France (Paris)

View user's profile Send private message

18F4550 & 18F45K50 bootloader problem ? COMPILER 5.078
PostPosted: Sat May 04, 2019 8:03 am     Reply with quote

Next to Ttelmah

About clock source is CRYSTAL 16 MHZ pins are RA6 & RA7.
Trigger pin is RB4
Cable detect : RD4

Size of bootloader :

CCS PCH C Compiler, Version 5.078, xxxxx 04-mai-19 15:53

Filename: D:\ex_usb_bootloader.lst

ROM used: 5514 bytes (17%)
Largest free fragment is 24512
RAM used: 399 (19%) at main() level
541 (26%) worst case
Stack used: 9 locations (8 in main + 1 for interrupts)
Stack size: 31

Best Regards
Cogitum



Joined: 22 Mar 2012
Posts: 59
Location: France (Paris)

View user's profile Send private message

18F4550 & 18F45K50 bootloader problem ? COMPILER 5.078
PostPosted: Sun May 05, 2019 12:01 am     Reply with quote

HI PCM Programmer,

Thanks for your support.
About your remark concerning two #delay().
I'm agree with you but ... i make different test
delete one of them >> I can compile my program.
Either is equal.
If i delete both compiler generate one error because I'm using for the blink led
delay_ms(100) ... I have make another test >> deleted this line ;delay_ms
I can compile but the final problem is the same with bootloader.

Additional test : hardware link between 45K50 and external HYPERTERMINAL.
result >> alone (without bootloader =RUN)
Using bootloader only this message BOOTLOADER READY 18F45K50
same message with SIOW !

However when I try to download my LED Blink program i can read this
message : Lines transferred 1 Total Lines 12 !
The green bargraph is stopped !!
Another small windows indicate Error #229 Timeout while downloading

This problem is not easy to solve (with 18F4550 everything work fine).
Unfortunately i need to use 18F45K50.

Best Regards
Ttelmah



Joined: 11 Mar 2010
Posts: 14424

View user's profile Send private message

PostPosted: Sun May 05, 2019 10:49 am     Reply with quote

Unfortunately, what you have posted would involve hours of time for us
to decode.

Post just your code.
The code you have modified to be the bootloader for this chip.
Use the code buttons, so it is readable.
Your code should be setting the defines it needs to work.
You should never need to modify the CCS files since they allow
you to disable stuff that doesn't apply using defines. So don't post
these, just the settings that are used. Remember these need to be done
before the files are included.
Now your approach seems instead to have been to modify the CCS files.
This has led to errors. For instance, it looks as if PIN_USB_SENSE is
actually set to PIN_B2, when this is compiled for the 1845K50, while
it has been changed for the 4550. Might well explain why there are
issues here....
Cogitum



Joined: 22 Mar 2012
Posts: 59
Location: France (Paris)

View user's profile Send private message

18F4550 & 18F45K50 bootloader problem ? SOLVED :grin:
PostPosted: Sun May 05, 2019 11:35 am     Reply with quote

Thank you Ttelmah for your valuable advice that I have taken into account.
I made the hardware changes for the sense and for the push button.
MY 18F45K50 CARD WORKS PERFECTLY NOW.
VERY RESPECTFULLY
Cogitum Very Happy Very Happy
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group