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 CCS Technical Support

USB Bootloader problem

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



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

USB Bootloader problem
PostPosted: Fri Jun 23, 2017 2:28 am     Reply with quote

Hi,

I have a problem with bootloader.
When i connect to usb, the Port will become " CCS USB to UART(COM2)".
I push reset buton and followed by button B4. Then I release button reset without release button B4.

It should change from " CCS USB to UART(COM2)" to CDC.....
but it did not change. What is wrong with my code ? Kindly please help me.

Code:

#include <18F4550.h>
#fuses none
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>
#include <usb_bootloader.h> 


void main(    )   
  {
   char c;

   usb_init_cs();

   while (TRUE)
         {
         usb_task();

            if (usb_cdc_kbhit())
               {
                 c=usb_cdc_getc();
                 if (c=='\n') {putc('\r'); putc('\n');}
                 if (c=='\r') {putc('\r'); putc('\n');}

                 while(true)
                     {

                     }
               }
         }
   }


the ex_usb common.h
Code:

//#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_CCS_PIC18F4550     //CCS PIC18F4550 USB Development kit
//#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_CCS_PIC24F           //CCS 24FJ256GB206 USB Development kit
//#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

// 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_B4  //yellow
 //  #define LED3 PIN_B5  //red
   #define BUTTON_PRESSED() !input(PIN_B4)

   //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    AN0
#endif





The USB Bootloader.H

Code:

///////////////////////////////////////////////////////////////////////////
////                                                                   ////
////                     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.           ////
////                                                                   ////
////  For more documentation see ex_usb_bootloader.c                   ////
////                                                                   ////
///////////////////////////////////////////////////////////////////////////
////                                                                   ////
//// VERSION HISTORY                                                   ////
////                                                                   ////
//// 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)
#define LOADER_SIZE        (0x2000)

#if defined(__USB_87J50__)
 #define APPLICATION_END    (getenv("PROGRAM_MEMORY")-9) //configuration bits
#elif defined(__PCD__)
 #define APPLICATION_END    (getenv("PROGRAM_MEMORY")-2)
#else
 #define APPLICATION_END    (getenv("PROGRAM_MEMORY")-1)
#endif

#if defined(__PCM__)
   #define LOADER_END      (LOADER_SIZE+0x40-1)
#elif defined(__PCH__)
   #define FLASH_SIZE getenv("FLASH_ERASE_SIZE")
   #if (((LOADER_SIZE) % FLASH_SIZE) == 0 )       //IF LOADER_SIZE is even flash boundary
      #define LOADER_END   (LOADER_SIZE-1)
   #else                                  //ELSE, goto next even boundary
      #define LOADER_END   (((LOADER_SIZE)+FLASH_SIZE-((LOADER_SIZE)%FLASH_SIZE))-1)
   #endif
#elif defined(__PCD__)
   #define FLASH_SIZE getenv("FLASH_ERASE_SIZE")/2
   #if (((LOADER_START+LOADER_SIZE) % FLASH_SIZE) == 0)
      #define LOADER_END (LOADER_START+LOADER_SIZE-1)
   #else
      #define LOADER_END (LOADER_START+(LOADER_SIZE+FLASH_SIZE-((LOADER_SIZE)%FLASH_SIZE))-1)
   #endif
#else
 #error PCM, PCH, and PCD only supported
#endif

#define APPLICATION_START  (LOADER_END+1)

#if defined(__PCH__)
 #define APPLICATION_ISR (APPLICATION_START+8)
#elif defined(__PCM__)
 #define APPLICATION_ISR (APPLICATION_START+4)
#elif defined(__PCD__)
 #define APPLICATION_ISR (APPLICATION_START+4)
#endif

//// --- end configuration --- ////////////////////////////////////////////

#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+0xF
 void BlankApplication(void)
 {
   while(TRUE);
 }

 //we need to prevent the loader from using application space
 #if (APPLICATION_END > 0x10000)
   #org APPLICATION_START+0x10, 0xFFFF {}
   #if (APPLICATION_END > 0x20000)
      #org 0x10000, 0x1FFFF {}
      #org 0x20000, APPLICATION_END {}
   #else
      #org 0x10000, APPLICATION_END {}
   #endif
 #else
   #org APPLICATION_START+0x10, APPLICATION_END {}
 #endif

   #define USB_STRINGS_OVERWRITTEN
   char USB_STRING_DESC_OFFSET[]={0,4,12};
   
   #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 {}

#endif
Ttelmah



Joined: 11 Mar 2010
Posts: 19508

View user's profile Send private message

PostPosted: Fri Jun 23, 2017 3:02 am     Reply with quote

You really need to start the unit 'from cold' holding the button.

Problem is the device is already enumerated, and the hardware doesn't switch off, so the port doesn't re-enumerate.

You could add boot code to switch the USB peripheral off and then restart it, but it's extra code/space.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Fri Jun 23, 2017 9:37 am     Reply with quote

Hi Ttelmah,

Could you please show me the additional boot code to solve this problem.
Ttelmah



Joined: 11 Mar 2010
Posts: 19508

View user's profile Send private message

PostPosted: Fri Jun 23, 2017 2:13 pm     Reply with quote

You would need to disable the USB peripheral, wait a short time, and then call usb_init. So something like:

Code:

#bit USBEN=getenv("BIT:USBEN")

//The bootloader
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())
   {
      USBEN=FALSE; //disable USB
      delay_ms(100); //ensure Windows sees the device has disconnected.
      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();
   }
   
   goto_address(APPLICATION_START);
}

//Your main code should have the botloader _before_ the other includes:
#include <18F4550.h>
#fuses none
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#include <usb_bootloader.h>
#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
#include <usb_cdc.h>

void main(    )   
  {
   char c;

//etc..


The bootloader file must always load first to ensure routines are correctly relocated to clear the bootloader.

Windows takes a short time to see a device has disconnected. With your 'reboot', this is being missed.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Sat Jun 24, 2017 9:41 pm     Reply with quote

Dear Ttelmah,

Thank you for the code, i will inform you the result once i test it.
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Thu Jun 29, 2017 9:18 am     Reply with quote

Hai Ttelmah,

I've just compiled the code, but it needs a #DEVICE at the top of the code.
So I've modified the code as shown below:
Code:

#include <ex_usb_common.h>
#include <usb_cdc.h>
#bit USBEN=getenv("BIT:USBEN")

//The bootloader
void art(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())
   {
      USBEN=FALSE; //disable USB
      delay_ms(100); //ensure Windows sees the device has disconnected.
      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();
   }
   
   goto_address(40);
}



//#include <18F4550.h>
#fuses none
#use delay(clock=48000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)


#include <usb_bootloader.h>
#include <string.h>
#include <input.c>
#include <stdio.h>
#include <stdlib.h>
//#include <usb_cdc.h>   


void main(    )   
  {
   char c;

   usb_init_cs();

   while (TRUE)
         {
         usb_task();

            if (usb_cdc_kbhit())
               {
                 c=usb_cdc_getc();
                 if (c=='\n') {putc('\r'); putc('\n');}
                 if (c=='\r') {putc('\r'); putc('\n');}

                 while(true)
                     {

                     }
               }
         }
   }

I've compiled again, but this time it shows an error,
1)undefine identifier-- load_program

I don't have any ideas to solve this problem, please help me.
Ttelmah



Joined: 11 Mar 2010
Posts: 19508

View user's profile Send private message

PostPosted: Thu Jun 29, 2017 2:01 pm     Reply with quote

Of course it does.
I only posted the sections that need to be added or modified in your original code. You need everything else as before.
It needs the whole bootloader. What you have posted is not going to do much good...
art



Joined: 21 May 2015
Posts: 181

View user's profile Send private message

PostPosted: Thu Jun 29, 2017 5:07 pm     Reply with quote

Hai Ttelmah,

From your explanation, I think better I just burn in the whole example of "ex_usb_bootloader.c" to the PIC first.

hope it can solve the problem.
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