IDE bug and flash programming problem

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
pk7639
Posts: 24
Joined: 24 Jun 2011 05:07

IDE bug and flash programming problem

#1 Post by pk7639 » 20 Oct 2016 03:42

So the "Brownout reset" config setting for an 18F67J94 in MikroC V6.6.2 has it's logic inverted, ie "enabled" actually disables BOR.

It was an easy mistake to make, CONFIG1L<0> == 0 to enable is a bit counter intuitive, and I've logged a ticket.

So now I have a bunch of product in the wild that I'd really like to turn BOR on for. We have a bootloader which (mercifully) leaves the top 512Bytes of flash clear so I just need to:

Read the config1l from flash and check if the bit is set.......working
Erase the top 512bytes of flash (this is the smallest erase block for this device)......working
Write the config registers back.....not working

I mean, what could possibly go wrong right?

My routine to write a word is based on

7.5.2 FLASH PROGRAM MEMORY WRITE
SEQUENCE (WORD PROGRAMMING)

from the data sheet

And it looks like this:

Code: Select all

void writeWord(unsigned long addr, unsigned char lsb, unsigned char msb)
{
   GIE_bit=0;
   TBLPTRU=(addr & 0xFF0000)>>16;
   TBLPTRH=(addr & 0xFF00)>>8;
   TBLPTRL=addr & 0xFF;
   TABLAT=lsb;  //config1L
   asm TBLWT*+
   TABLAT=msb;  //config1H
   asm TBLWT*

   asm {
   MOVLB  0x0F
   BSF EECON1, WWPROG
   BSF EECON1, WREN
   MOVLW 0x55
   MOVWF EECON2
   MOVLW 0xAA
   MOVWF EECON2
   BSF EECON1, WR
   BSF INTCON, GIE
   BCF EECON1, WWPROG
   BCF EECON1, WREN
   }
   GIE_bit=1;
}
it gets called like this:

Code: Select all

  writeWord(0x1FFFF0,0xA0,0xF6);  //Config 1
   writeWord(0x1FFFF2,0xAA,0xFF);  //Config 2
   writeWord(0x1FFFF4,0x02,0xF0);  //Config 3
   writeWord(0x1FFFF6,0xFF,0xF7);  //Config 4
   writeWord(0x1FFFF8,0x03,0xFE);  //Config 5
   writeWord(0x1FFFFA,0xFF,0xFE);  //Config 6
   writeWord(0x1FFFFC,0x1D,0xF0);  //Config 7
   writeWord(0x1FFFFE,0xF8,0xF3);  //Config 8
But I still only get 0xFF's when I read back with a programmer.
I'm missing something stupid I know.....
Anyone got any pointers?

PK

pk7639
Posts: 24
Joined: 24 Jun 2011 05:07

Re: IDE bug and flash programming problem

#2 Post by pk7639 » 20 Oct 2016 04:04

So I make some progress by getting the address' correct

Code: Select all

 writeWord(0x1FFFF0,0xA0,0xF6);  //Config 1
becomes

Code: Select all

 writeWord(0x1FFF0,0xA0,0xF6);  //Config 1
Now it works once. ie the config1 is programmed, then it hangs....
Hmm, maybe I need to do a page write after all..

PK

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: IDE bug and flash programming problem

#3 Post by Aleksandar.Mitrovic » 20 Oct 2016 16:21

Hi,

Yes, I agree with you.

Just one note, Brown-out Reset Enable bit is in CONFIG1H<0> ,page 555:
http://www.mouser.com/ds/2/268/30575A-243037.pdf

Maybe your problem is because that CONFIG1L<0> is U-1 (not writable bit).

Kind regards,
Aleksandar

pk7639
Posts: 24
Joined: 24 Jun 2011 05:07

Re: IDE bug and flash programming problem

#4 Post by pk7639 » 21 Oct 2016 01:12

Aleksandar.Mitrovic wrote:Hi,

Yes, I agree with you.

Just one note, Brown-out Reset Enable bit is in CONFIG1H<0> ,page 555:
http://www.mouser.com/ds/2/268/30575A-243037.pdf

Maybe your problem is because that CONFIG1L<0> is U-1 (not writable bit).

Kind regards,
Aleksandar
Yes, posting problems on the forums is like going to therapy. The process of explaining it to someone else makes the problem obvious..

I have that code working now. My main problem was that I was including the MOVLB 0x0F in the assembly block which meant that the compiler lost track of the current RAM page and this caused some weirdness.
Adding
EECON1.WWPROG=1;

before the ASM block made sure we were pointing to the correct page and that the compiler knew which page that was.

Just in case anyone else needs to change config on the fly, (and let's be clear here, there are risks in doing this)

Code: Select all

void writeWord(unsigned long addr, unsigned char lsb, unsigned char msb)
{

   //write the config words back
   TBLPTRU=(addr & 0xFF0000)>>16;
   TBLPTRH=(addr & 0xFF00)>>8;
   TBLPTRL=addr & 0xFF;
   TABLAT=lsb;  //config1L
   asm TBLWT*+
   TABLAT=msb;  //config1H
   asm TBLWT*
   EECON1.WWPROG=1;
   asm {

   BSF EECON1, WWPROG
   BSF EECON1, WREN
   MOVLW 0x55
   MOVWF EECON2
   MOVLW 0xAA
   MOVWF EECON2
   BSF EECON1, WR
   BCF EECON1, WWPROG
   BCF EECON1, WREN
   }
}

void writeConfig()
{
   GIE_bit=0;
   SWDTEN_bit=0;
   //Erase the last page
   TBLPTRU=0x1;
   TBLPTRH=0xFF;
   TBLPTRL=0xF1;
   EECON1.WREN=1;
    asm {

    BSF EECON1, WREN ; enable write to memory
    BSF EECON1, FREE ; enable Row Erase operation
    MOVLW 0x55
    MOVWF EECON2 ; write 55h
    MOVLW 0xAA
    MOVWF EECON2 ; write 0AAh
    BSF EECON1, WR ; start erase (CPU stall)
    BCF EECON1, FREE
    BCF EECON1, WREN
    }
    
   //write the config words back
   writeWord(0x1FFF0,0xA0,0xF6);  //Config 1
   writeWord(0x1FFF2,0xAA,0xFF);  //Config 2
   writeWord(0x1FFF4,0x02,0xF0);  //Config 3
   writeWord(0x1FFF6,0xFF,0xF7);  //Config 4
   writeWord(0x1FFF8,0x03,0xFE);  //Config 5
   writeWord(0x1FFFA,0xFF,0xFE);  //Config 6
   writeWord(0x1FFFC,0x1D,0xF0);  //Config 7
   writeWord(0x1FFFE,0xF8,0xF3);  //Config 8
   GIE_bit=1;

}

It'd be nice to see that IDE bug fixed, I have a bad feeling that it's going to catch us out again...

PK

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: IDE bug and flash programming problem

#5 Post by Aleksandar.Mitrovic » 21 Oct 2016 15:41

Hi,

Thank you for this explanation.
pk7639 wrote:Just in case anyone else needs to change config on the fly, (and let's be clear here, there are risks in doing this)
I agree with this.

Kind regards,
Aleksandar

Post Reply

Return to “mikroC PRO for PIC General”