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

bootloader usb problem

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



Joined: 05 Oct 2016
Posts: 120

View user's profile Send private message

bootloader usb problem
PostPosted: Tue Jun 11, 2019 8:55 am     Reply with quote

Hello everyone,

I try to upload a hex via usb. I used the example bootloader code for this with minor edits(changed the led pins and usb sense pin). My problem is, when i add usb code to hex, the program stops working.

Code:
#include <18F4550.h>
#DEVICE ADC=10
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)

void main()
{
   delay_ms(50);
   int i=0;
   for(i=0;i<5;i++)
   {
      output_high(PIN_D5);
      output_high(PIN_D6);
      delay_ms(500);
      output_low(PIN_D5);
      output_low(PIN_D5);
      delay_ms(500);
   }
}


this works just fine. when i add this

Code:
#include <18F4550.h>
#DEVICE ADC=10
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)

#include "usb_cdc.h"
#define rp_getc usb_cdc_getc
#define rp_putc usb_cdc_putc

int adc_channel;
int16 var_u16[];
int16 adc_value;

void main()
{
   delay_ms(50);
   int i=0;
   for(i=0;i<5;i++)
   {
      output_high(PIN_D5);
      output_high(PIN_D6);
      delay_ms(500);
      output_low(PIN_D5);
      output_low(PIN_D5);
      delay_ms(500);
   }
}

void mydebug()
{
if(usb_attached()){
   if (usb_enumerated()){

   // Process analog inputs
      for (adc_channel=0;adc_channel<4;adc_channel++){
         set_adc_channel(adc_channel);
         delay_us(30);
         adc_value=read_adc();
         if(usb_cdc_carrier.dte_present)
            printf(rp_putc, "%lu;", adc_value);
      }     
      if(usb_cdc_carrier.dte_present){
      // Send word variables
         for (adc_channel = 0; adc_channel < 6; adc_channel++)
          printf(rp_putc, "%lu;", var_u16[adc_channel]);
         printf(rp_putc, "\r\n");
      }
   }
}
} // debug


it does not work correctly. even though i dont call the mydebug function. the leds dont blink at all. and i think it also breaks bootlader because after this hex, nothing i upload works(even the ones worked before).

Any ideas what is wrong?

My best wishes,

Doguhan
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Tue Jun 11, 2019 10:29 am     Reply with quote

There is no sign at all of this being prepared to be loaded by USB.

Look at the bootloader examples. Understand that the running application
code, needs to be compiled to be compatible with the bootloader. You can't
just write a program 'as normal' and load it with a bootloader.

Your run time code, needs to be loading the header "usb_bootloader.h".

In the bootloader itself, this same file is loaded, but with the define
_bootloader made. For the runtime code, you include this file without
this define. _Required_ or the code cannot be loaded and run.
doguhanpala



Joined: 05 Oct 2016
Posts: 120

View user's profile Send private message

PostPosted: Tue Jun 11, 2019 1:19 pm     Reply with quote

Ttelmah wrote:
There is no sign at all of this being prepared to be loaded by USB.

Look at the bootloader examples. Understand that the running application
code, needs to be compiled to be compatible with the bootloader. You can't
just write a program 'as normal' and load it with a bootloader.

Your run time code, needs to be loading the header "usb_bootloader.h".

In the bootloader itself, this same file is loaded, but with the define
_bootloader made. For the runtime code, you include this file without
this define. _Required_ or the code cannot be loaded and run.


thanks for answer. i just forgot to add it here. even with it problem still occurs.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 11, 2019 6:06 pm     Reply with quote

Where is your call to usb_task() in the main loop ?

Look at the main() section of this CCS example file. Look closely at
this CCS file, and try to copy what it's doing:
Quote:

ex_usb_serial.c
doguhanpala



Joined: 05 Oct 2016
Posts: 120

View user's profile Send private message

PostPosted: Wed Jun 12, 2019 6:09 am     Reply with quote

PCM programmer wrote:
Where is your call to usb_task() in the main loop ?

Look at the main() section of this CCS example file. Look closely at
this CCS file, and try to copy what it's doing:
Quote:

ex_usb_serial.c


hello pcm programmer

I tried a simpler code with usb task.

Code:
#if !defined(__PCH__)
 #error USB CDC Library requires PIC18
#endif

#include <18F4550.h>
#DEVICE ADC=10
#fuses HSPLL,NOWDT,NOMCLR,PROTECT,NOLVP,NODEBUG,NOBROWNOUT,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#include "usb_cdc.h"

#define rp_getc usb_cdc_getc
#define rp_putc usb_cdc_putc

int   value;

void init_usb()
{
   delay_ms(500);
   value = 0;
}

void main()
{
  usb_cdc_init();
  usb_init();

  while (true)
   {
      usb_task();
      value = rp_getc();
      delay_ms(20);
      if(value=='a')
      {
         output_high(PIN_D5);
         rp_putc('k');
      }

   }
}


this code works just fine. i use pickit to upload it. when i add the line

Code:
#include <usb_bootloader.h>


and upload it with bootloader i cannot open serial comm via putty.

there is something i miss when i use usb with bootloader. i try communicate with bluetooth and comms work fine.
doguhanpala



Joined: 05 Oct 2016
Posts: 120

View user's profile Send private message

PostPosted: Tue Jun 18, 2019 1:59 am     Reply with quote

Is there a code line that should be used in bootloader when you have to use usb comm in application hex.

I tried led blink application hex and upload them with bootloader. Seems to work fine. When i add usb, things get broken.

Code:
#include <18F4550.h>
#DEVICE ADC=10
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#include <usb_bootloader.h>

#include "usb_cdc.h"
#define rp_getc usb_cdc_getc
#define rp_putc usb_cdc_putc

void mydebug();

int adc_channel;
int16 var_u16[];
int16 adc_value;

void main()
{
   delay_ms(50);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_adc_ports(AN0);
   usb_init_cs();
   usb_cdc_init();
   delay_ms(50);
   while(true)
   {
      usb_task();
      delay_ms(5);
      mydebug();
   }
}


void mydebug()
{
if(usb_attached()){
   if (usb_enumerated()){

   // Process analog inputs
      for (adc_channel=0;adc_channel<4;adc_channel++){
         set_adc_channel(adc_channel);
         delay_us(30);
         adc_value=read_adc();
         if(usb_cdc_carrier.dte_present)
            printf(rp_putc, "%lu;", adc_value);
      }     
      if(usb_cdc_carrier.dte_present){
      // Send word variables
         for (adc_channel = 0; adc_channel < 6; adc_channel++)
          printf(rp_putc, "%lu;", var_u16[adc_channel]);
         printf(rp_putc, "\r\n");
      }
   }
}
} // debug


when i delete the

Code:
include <usb_bootloader.h>


and upload the hex with pickit, it works fine. I add the line and try to upload it with bootloader. Putty does not open and computer does not recognize the usb. I have checked the fuse settings and they match with the application hex. I also call the usb_task() in while loop all the time. I have no idea what am i missing. Any ideas?
temtronic



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

View user's profile Send private message

PostPosted: Tue Jun 18, 2019 4:45 am     Reply with quote

Though not 'USB' related these lines of code are wrong...

setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(AN0);

With most PICs, including this one, all clock speeds over 1MHz should NOT ne using the internal ADC clock. Microchip's ADC section has 3-4 page describing what is a vaild clock including a couple of charts.
You've only selected 1 ADC port, yet your program accesses 4 ( in the loop).
I assume you used a 'wizard' to help you code, problem is the 'wizard' is wrong.

Jay
doguhanpala



Joined: 05 Oct 2016
Posts: 120

View user's profile Send private message

PostPosted: Tue Jun 18, 2019 4:52 am     Reply with quote

temtronic wrote:
Though not 'USB' related these lines of code are wrong...

setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(AN0);

With most PICs, including this one, all clock speeds over 1MHz should NOT ne using the internal ADC clock. Microchip's ADC section has 3-4 page describing what is a vaild clock including a couple of charts.
You've only selected 1 ADC port, yet your program accesses 4 ( in the loop).
I assume you used a 'wizard' to help you code, problem is the 'wizard' is wrong.

Jay


Thank you for your answer Jay.

I understand your answer but i don't think the problem is about the adc. I shared other code that only gets a char sends another char. The code works by itself but does not work with bootloader. That example does not include an adc.

This is the code:
Code:
#if !defined(__PCH__)
 #error USB CDC Library requires PIC18
#endif

#include <18F4550.h>
#DEVICE ADC=10
#fuses HSPLL,NOWDT,NOMCLR,PROTECT,NOLVP,NODEBUG,NOBROWNOUT,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#include <usb_bootloader.h>
#include "usb_cdc.h"

#define rp_getc usb_cdc_getc
#define rp_putc usb_cdc_putc

int   value;

void init_usb()
{
   delay_ms(500);
   value = 0;
}

void main()
{
  usb_init_cs();
  usb_cdc_init();
  usb_init();

  while (true)
   {
      usb_task();
      value = rp_getc();
      delay_ms(20);
      if(value=='a')
      {
         output_high(PIN_D5);
         rp_putc('k');
      }

   }
}
temtronic



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

View user's profile Send private message

PostPosted: Tue Jun 18, 2019 6:22 am     Reply with quote

Yes, the ADC code has nothing to do with your USB problem. I just mention it, so that when USB link is good, you'll understand why the ADC results aren't what you expect.

I no longer use any PIC with internal USB since it was cheaper, easier, safer to use a $2 TTL<>USB adapter !
Jay
doguhanpala



Joined: 05 Oct 2016
Posts: 120

View user's profile Send private message

PostPosted: Tue Jun 18, 2019 10:43 am     Reply with quote

temtronic wrote:
Yes, the ADC code has nothing to do with your USB problem. I just mention it, so that when USB link is good, you'll understand why the ADC results aren't what you expect.

I no longer use any PIC with internal USB since it was cheaper, easier, safer to use a $2 TTL<>USB adapter !
Jay


Thank you for your answer!

unfortunately it is not possible to use any other component. my hardware is ready. i am trying to add the bootloader feature to it. so i have to figure out the problem itself.

my best wishes,

Doguhan
doguhanpala



Joined: 05 Oct 2016
Posts: 120

View user's profile Send private message

PostPosted: Tue Jun 18, 2019 11:13 am     Reply with quote

Another interesting problem is when i upload a hex which contains usb, the comm stops after a while and i get usb not recognized error. not on start but after a long time(like 30 seconds). Any ideas?

Code:
#include <18F4550.h>
#DEVICE ADC=10
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#include <usb_bootloader.h>

#include "usb_cdc.h"
#define rp_getc usb_cdc_getc
#define rp_putc usb_cdc_putc

void mydebug();

int adc_channel;
int16 var_u16[];
int16 adc_value;

void main()
{
   delay_ms(50);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_adc_ports(AN0_TO_AN3);
   usb_init_cs();
   usb_cdc_init();
   delay_ms(50);
   output_high(PIN_D5);         
         while(true)
         {           
            output_high(PIN_D5);
            output_high(PIN_D6);
            delay_ms(100);
            usb_task();
            mydebug();
            output_low(PIN_D5);
            output_low(PIN_D5);
            delay_ms(100);
         }
         
}
//!
void mydebug()
{
if(usb_attached()){
   if (usb_enumerated()){

   // Process analog inputs
      for (adc_channel=0;adc_channel<4;adc_channel++){
         set_adc_channel(adc_channel);
         delay_us(30);
         adc_value=read_adc();
         if(usb_cdc_carrier.dte_present)
            printf(rp_putc, "%lu;", adc_value);
      }     
      if(usb_cdc_carrier.dte_present){
      // Send word variables
         for (adc_channel = 0; adc_channel < 6; adc_channel++)
          printf(rp_putc, "%lu;", var_u16[adc_channel]);
         printf(rp_putc, "\r\n");
      }
   }
}
} // debug
temtronic



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

View user's profile Send private message

PostPosted: Wed Jun 19, 2019 4:57 am     Reply with quote

re:
after a while and i get usb not recognized error.

I'm assuming this is Windows reporting the message. If so there are a few possibilties.
1) your program has several delays in it,so Windows USB driver or ap or whatever it's called could be 'timing out' as it doesn't see any USB activity on the port liked to the PIC.

2) If your PC is NOT dedicated to ONLY PIC programming, then any number of other aps or programs could be using up time and not allowing Windows to 'see' the PIC. The more modern the OS , the more time is spent doing 'background' stuff and of course people tend to have internet, email, virus pgms and auto updates ALL running 24/7. While today's PC are fast they still can only do ONE thing at a time

I suspect #1 is the problem,but since I use 'TTL<>USB' modules I've never run into the 'timeout' error. Others who do use the 4550 may be able to help.
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