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

[solved] USB Bootloader -> program loaded not reliable
Goto page Previous  1, 2, 3
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
spilz



Joined: 30 Jan 2012
Posts: 216

View user's profile Send private message

PostPosted: Fri Dec 09, 2016 3:49 pm     Reply with quote

I'm not sure to understand what do you refer about PCM_programmer method.

My modification is only like adding my own board to usb_common file. it's only to build the bootloader, and being as close as possible to the original CCS USB Bootloader to facilitate trade in the forum, and delete possible issue of a modify bootloader.

For my program I'm doing like PCM_programmer I guess, or I misunderstand what you said.

I learn lot of thing with your help Ttelmah, thank for that.
newguy



Joined: 24 Jun 2004
Posts: 1902

View user's profile Send private message

PostPosted: Fri Dec 09, 2016 7:37 pm     Reply with quote

Has anyone thought to try this?:
- load PIC using the two different methods, bootloader (faulty) and bootloader + program burned into PIC at same time
- use programmer to read contents of PIC to file for each method
- do a file compare
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 09, 2016 8:36 pm     Reply with quote

Yes I have done this. I compared two versions, one with
Code:

if(delta >= 7)

and the other with:
Code:

if(delta == 7)

There are only two lines different between them.
if(delta >= 7) gives this:
Code:

SUBLW 0x6                             
BC 0x212a     

if(delta == 7) gives this:
Code:
SUBLW 0x7                             
BNZ 0x212a
Ttelmah



Joined: 11 Mar 2010
Posts: 19225

View user's profile Send private message

PostPosted: Sat Dec 10, 2016 3:06 am     Reply with quote

spilz wrote:
I'm not sure to understand what do you refer about PCM_programmer method.

My modification is only like adding my own board to usb_common file. it's only to build the bootloader, and being as close as possible to the original CCS USB Bootloader to facilitate trade in the forum, and delete possible issue of a modify bootloader.

For my program I'm doing like PCM_programmer I guess, or I misunderstand what you said.

I learn lot of thing with your help Ttelmah, thank for that.


The point is that PCM_programmer's example, can be loaded and compiled 'as it stands'. No modification of files needed.

You are misunderstanding slightly what the files actually 'are'. There are drivers and code like the bootloader, and then there are 'examples'. The examples, are setup to run with specific hardware, to demonstrate how to use things. Now the problem is that you are taking an 'example' file (this is what the 'ex' means), and then tweaking this for your hardware, and not to run the example code. Now there are things in this that are specific to the example (the LED's for instance, are setup to drive the LED's on the example board). It means that we have to guess what hardware you actually have, and try to match the effect of your tweaks.
The 'example' files are designed for specific hardware. To run something different on your hardware, you should have either your own equivalent header file, or just include the lines into your code.

One of the old 'suggestions' here, has always been that when posting code, you should make it so we can load the code 'as posted', and test what happens. PCM_programmer's code is like this. You can compile it _as posted_ no tweaking of other files. That way we know that what we are compiling matches what he is compiling.

It's also 'pointless'. By trying to include your chip's settings into a CCS file, you increase the risk of something unexpected happening.

In this case, this is not the problem (since PCM_programmer has now posted simplified code showing the fault), but you are creating extra complexity, that makes it harder for us, and for _you_ to actually diagnose what is happening....
spilz



Joined: 30 Jan 2012
Posts: 216

View user's profile Send private message

PostPosted: Sat Dec 10, 2016 3:16 am     Reply with quote

Oh I see,

I was thinking that this small modification was simpler than giving all the code. My idea was : if you have the demo board, you don't need to modify the file, and can try directly.

I understand now, i will be careful about that next time I post a code.
Thanks
Ttelmah



Joined: 11 Mar 2010
Posts: 19225

View user's profile Send private message

PostPosted: Sat Dec 10, 2016 4:21 am     Reply with quote

I suspect it is related to RAM bank switching.

I notice that the interrupt code always places it's reserved variables in the low memory.
With the array declared, the timer variables are sitting in bank 4.
When the array is smaller than about 220, the timer variables are in bank 0.

Now the code seems to have the correct bank switches present, so possibly something in the interrupt handler (looking now). #locating the array, means the timer variables can again be located in bank 0. Declaring the array after the timer variables has the same effect and also fixes the problem.
Accessing 'Seconds_flag', forces a bank switch.
The bank switching code is identical in chips compiled for address 0 builds, or 0x2000. I'm suspecting something in the bootloader interrupt relocation.

A look shows what is wrong.

At some point, the interrupt relocation in the USB bootloader, has been incorrectly modified.

It should read:
Code:

#int_global
void isr(void)
{
   jump_to_isr(APPLICATION_ISR);
}


It's been set to 'int_default'.....

Now this means that it is not correctly being programmed to relocate the interrupt handler. Won't show if your code doesn't use interrupts, and my own USB bootloader, had my own relocation code that handles this correctly.

So try this one change. Smile

What causes the problem is that there is a double save and then 'half restore', this clears the BSR, as well as wasting time...

Ugh.

I have already posted details to CCS.
spilz



Joined: 30 Jan 2012
Posts: 216

View user's profile Send private message

PostPosted: Sat Dec 10, 2016 9:40 am     Reply with quote

That seems to solve lot of thing !!
It seems the usb bootloader has an error.

Good job !!!

I'm not sure to understand all, does it change/solve bank stuff ?

Let see if PCM_programmer find something else.

Thanks again both for time spend on my question.
Ttelmah



Joined: 11 Mar 2010
Posts: 19225

View user's profile Send private message

PostPosted: Sat Dec 10, 2016 10:09 am     Reply with quote

What happens is a bit complex.

#int_global, makes the code become the 'master' handler, without any save/restore.
#int_default, makes the code save all the registers then be called.

Now remember that the code already placed in the upper bank, saves and restores everything.

With #int_global, the hardware saves the W reg, BSR and status, then the code immediately jumps to the code in the upper bank, this then saves everything else, executes the handler, then restores everything and returns, and the return restores the W, BSR & status. What is wanted.

With #int_default, the code in the low memory saves everything, then _calls_ this as the interrupt handler, which then jumps to the upper handler. This call also updates the 'shadow registers, then saves the other registers again. Then jumps to the handler. Problem then is that the upper handler then uses RETFIE1 to return to the low handler. This restores the W, BSR & status back to the value from the low handler, and returns to the handler in the low memory. This calls RETFIE1 _again_. Since the shadow registers were updated by the call from #int_default, the W, BSR and status may have then been damaged. Problem....
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Dec 11, 2016 12:02 am     Reply with quote

Thank you for solving this, Ttelmah.
Ttelmah



Joined: 11 Mar 2010
Posts: 19225

View user's profile Send private message

PostPosted: Sun Dec 11, 2016 2:10 am     Reply with quote

Your simplified version was what allowed me to track what was happening. Smile
I felt right at the start that it sounded like a BSR problem. This was why much earlier on in the thread, I asked what happened if the array was declared after the timer variables, but this was not tested.
With your version, the first thing was that the code was small enough, and 'complete', allowing me to take the time and make sure that it was correctly being 'relocated' at 2000. Compiled it both at address zero, and relocated, then compared the assembler and checked everything was OK. It was. This then pointed to the problem being in the bootloader, and (of course), except for a slight delay at initialisation, and some registers being changed, the only thing that entered the equation when the bootloader was used, was the interrupt relocation. A look at this code gave the 'ah ha' moment!... I then made a 'combined' file of bootloader and code, and (since USB is not used in the 'normal boot' path), could run this up in the simulator and see what actually happened. Team 'CCS users'. Smile
Ttelmah



Joined: 11 Mar 2010
Posts: 19225

View user's profile Send private message

PostPosted: Thu Dec 22, 2016 4:00 pm     Reply with quote

Just for information, this has now been fixed in compiler version 5.066. Smile
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2, 3
Page 3 of 3

 
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