View previous topic :: View next topic |
Author |
Message |
doguhanpala
Joined: 05 Oct 2016 Posts: 120
|
usb connection lost on bootloader |
Posted: Mon Jun 24, 2019 4:43 am |
|
|
Hello everyone,
I have a usb code that receieves a char 'a' and sends 'k'. The code works just fine.
However when i upload the code with bootloader windows loses connection. I use the ccs example usb bootloader.
I checked the bootloader hex and it works. I have uploaded a simple led blink with bootloader and there were no problems. When i add usb, things get complicated and after a while i lose usb connection.
This is the code that works on normal but gets stuck on bootloader. Any ideas?
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> //this line exist only in bootloader version
#include "usb_cdc.h"
#define rp_getc usb_cdc_getc
#define rp_putc usb_cdc_putc
int value;
void main()
{
usb_init_cs();
usb_cdc_init();
while (true)
{
usb_task();
value = rp_getc();
delay_ms(20);
if(value=='a')
{
rp_putc('k');
}
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Mon Jun 24, 2019 6:24 am |
|
|
No...
The file "usb_bootloader.h"
Needs to be included both in the bootloader, _and_ in the code you are going
to load _with_ the bootloader. The difference is that in the bootloader
it needs the #define _bootloader before it is loaded.
Without this loaded, the code will not be compiled located suitably to
use the bootloader.
Hopefully your comment that it only exists means you are loading it?.
Then next question, are your fuses the same in the bootloader code?.
They need to be. The best way to handle the fuses is to take the bootloader
fuses and include then as a 'comment' at the top of your code, and then
use #FUSES NONE. The main code cannot change the bootloader fuses
and this ensures you know what is happening!...
Last edited by Ttelmah on Mon Jun 24, 2019 6:32 am; edited 1 time in total |
|
|
doguhanpala
Joined: 05 Oct 2016 Posts: 120
|
|
Posted: Mon Jun 24, 2019 6:32 am |
|
|
Ttelmah wrote: | No...
The file "usb_bootloader.h"
Needs to be included both in the bootloader, _and_ in the code you are going
to load _with_ the bootloader. The difference is that in the bootloader
it needs the #define _bootloader before it is loaded.
Without this loaded, the code will not be compiled located suitably to
use the bootloader. |
Thank you for answer Ttelmah,
"usb_bootloader.h" is included in bootloader too. I've checked the example code and it also has the #define _bootloader line.
the problem still continues. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Mon Jun 24, 2019 6:35 am |
|
|
Are you sure the same code is reliable if loaded without the bootloader?.
Classic things that can give 'long term' USB problems are a poor capacitor
on the Vusb line (this should be a 0.47uF polyester ideally).
Also the long delay could cause an issue. If multiple characters arrive
only one is being accepted every 20mSec. Very easy to cause overflow
problems handling like this.
There shouldn't really be a delay at all. The code will only send when
a character is received, so 'why delay'?. |
|
|
doguhanpala
Joined: 05 Oct 2016 Posts: 120
|
|
Posted: Mon Jun 24, 2019 7:17 am |
|
|
Hello Ttelmah,
I have checked and you are right. The first one is not stable. I have shared the wrong verision of code. This is the right version. This works without bootloader. I have checked for a few minutes and there were no connection loss at all. When i add bootloader to it, Putty cant create a serial connection to board.
The difference is
Code: | usb_cdc_init();
usb_init(); |
lines.
I have checked the example code. It has the usb_init_cs() line. I dont know what that does but could one of the usb function overlap another?
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 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');
}
}
} |
thank you for your answer. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Mon Jun 24, 2019 7:59 am |
|
|
usb_init_cs checks for the 'connection sense' line being pulled high before
initialising the USB. usb_init initialises it 'without question'.
Connection sense is actually _required_ by USB (if you want to meet the
'specs'), if the device is not actually powered by the USB bus. The reason
is that on modern kit, the CPU can switch off power to the USB bus
to save power, and requires the stuff attached to detect this and reset
when the power comes back on. |
|
|
doguhanpala
Joined: 05 Oct 2016 Posts: 120
|
|
Posted: Mon Jun 24, 2019 8:11 am |
|
|
Ttelmah wrote: | usb_init_cs checks for the 'connection sense' line being pulled high before
initialising the USB. usb_init initialises it 'without question'.
Connection sense is actually _required_ by USB (if you want to meet the
'specs'), if the device is not actually powered by the USB bus. The reason
is that on modern kit, the CPU can switch off power to the USB bus
to save power, and requires the stuff attached to detect this and reset
when the power comes back on. |
Thank you so much. I still could not figure out why Putty can't create a serial connection on bootloader. Assuming some codes are overlapping.
In bootloader there are some usb connection lines like usb_isr polling and usb_init_cs(). There are usb related codes on my own hex. Could the codes i use might be overlapping or conflict with each other?
And lastly, any ideas why the working code gets broken on bootloader?
I am stuck at this for a few days. Sorry for my dumb questions. Thank you so much! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19496
|
|
Posted: Mon Jun 24, 2019 1:19 pm |
|
|
No, they don't 'overlap'.
Unless your bootloader is larger that the standard one.
The bootloader does assume you have CS implemented. |
|
|
doguhanpala
Joined: 05 Oct 2016 Posts: 120
|
|
Posted: Wed Jun 26, 2019 6:54 am |
|
|
Ttelmah wrote: |
The bootloader does assume you have CS implemented. |
So should i use usb_init_cs() instead of usb_init() ?
I tried that but still no luck. |
|
|
|