• AVR Freaks

Hot!Read from flash memory seems to access wrong address in simulator for PIC16F15313

Author
hgl
New Member
  • Total Posts : 4
  • Reward points : 0
  • Joined: 2019/07/01 06:36:35
  • Location: 0
  • Status: offline
2019/07/17 04:04:22 (permalink)
0

Read from flash memory seems to access wrong address in simulator for PIC16F15313

Hi,
I am working on some example code to access flash memory of PIC16F15313. The complete reproducer project is attached. My problem is that although writing to program memory works as expected (verified with the simulator's Program Memory View), reading back the same data does not work as expected.
 
I have the following data in my program memory (section reserved for user data):
address 0x780: content 0x3F02
address 0x781: content 0x3F7B
address 0x782: content 0x3F7C
 
My code now reads the lowest data bytes from these 3 addresses and gets the following:
read from address 0x780: content 0xFF       // expected: 0x02
read from address 0x781: content 0x02      // expected: 0x7B
read from address 0x782: content 0x7B      // expected: 0x7C
 
This seems as if a wrong address is used for reading the data. However, I can't find a mistake in my code (see attached project).
Can anyone help me? I am using MPLAB X IDE v5.20.
 
Best regards, Hagen

Attachment(s)

Attachments are not available: Download requirements not met
#1

19 Replies Related Threads

    ric
    Super Member
    • Total Posts : 23185
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/17 06:28:21 (permalink)
    +1 (1)
    Have you tried this on a real chip?
    I've wasted too many hours chasing bugs in the simulator to bother testing obscure features with it.
     
    You don't need to use the NVM registers at all to read the lower 8 bits of FLASH memory.
    PIC16F1xxxx chips can read it directly via the FSRx/INDx registers, and XC8 allows you to place variables at absolute addresses, and knows about 24 bit integer variables. ("short long")

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    GeorgePauley
    Moderator
    • Total Posts : 1138
    • Reward points : 0
    • Joined: 2009/12/01 13:59:30
    • Location: Chandler AZ
    • Status: offline
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/17 12:08:41 (permalink)
    +1 (1)
    I'd be really curious to know if this works on a real device.  It is possible it might.  Earlier devices in this family required the following 4 instructions in sequence with no deviation
     

     
       MOVLW  55h
     
       MOVWF  NVMCON2
     
       MOVLW  AAh
     
       MOVWF  NVMCON2
     

     
    But because you are doing this in C code, you end up with
     

     
       !    NVMCON2 = 0x55;
       0x7E8: MOVLW 0x55
       0x7E9: MOVLB 0x10
       0x7EA: MOVWF 0x1F
       !    NVMCON2 = 0xAA;
       0x7EB: MOVLW 0xAA
       0x7EC: MOVWF 0x1F
     

     
    Notice the extra MOVLB instruction.  So the simulator isn't recognizing the expected unlock sequence.  Specifically we expect the 0xAA to happen exactly 2 instruction cycles after the 0x55.  I remember coding this up several years ago and thinking "no way the device is that sensitive".  After discussing with design engineers we decided, yes the device IS that sensitive. 

    But it is possible the 16F15313 behaves differently than the old 16F877?  The verbiage in the datasheet leaves some room for interpretation.  I will say that code generated by MCC seems to meet the simulator's expectations, which is probably a clue.
    #3
    davekw7x
    Entropy++
    • Total Posts : 1789
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Left Coast, USA
    • Status: offline
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/17 12:26:05 (permalink)
    0
    Ignore premature posting.  I pulled the trigger before testing was complete
    post edited by davekw7x - 2019/07/17 12:47:35

    Sometimes I just can't help myself...
    #4
    1and0
    Access is Denied
    • Total Posts : 9484
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/17 12:55:11 (permalink)
    +3 (3)
    GeorgePauley
    I'd be really curious to know if this works on a real device.  It is possible it might.  Earlier devices in this family required the following 4 instructions in sequence with no deviation

       MOVLW  55h
       MOVWF  NVMCON2
       MOVLW  AAh
       MOVWF  NVMCON2

    But because you are doing this in C code, you end up with

       !    NVMCON2 = 0x55;
       0x7E8: MOVLW 0x55
       0x7E9: MOVLB 0x10
       0x7EA: MOVWF 0x1F
       !    NVMCON2 = 0xAA;
       0x7EB: MOVLW 0xAA
       0x7EC: MOVWF 0x1F

    Notice the extra MOVLB instruction.  So the simulator isn't recognizing the expected unlock sequence.  Specifically we expect the 0xAA to happen exactly 2 instruction cycles after the 0x55.  I remember coding this up several years ago and thinking "no way the device is that sensitive".  After discussing with design engineers we decided, yes the device IS that sensitive.  

    You might want to have another discussion with those design engineers. ;)
     
    Because the required 4-cycle "magic" sequence is:

       MOVWF  NVMCON2  ; load 0x55
       MOVLW  AAh
       MOVWF  NVMCON2
       BSF        NVMCON1,WR

    #5
    GeorgePauley
    Moderator
    • Total Posts : 1138
    • Reward points : 0
    • Joined: 2009/12/01 13:59:30
    • Location: Chandler AZ
    • Status: offline
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/17 14:52:54 (permalink)
    0
    Yeah yeah, the BSF NVMCON1,WR went without saying... mr green: mr green
    #6
    ric
    Super Member
    • Total Posts : 23185
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/17 15:03:31 (permalink)
    +2 (2)
    I think 1and0's main point was, the "magic" sequence does not start until the first write to NVMCON2, so the MOVLB in your example should not have mattered.
    Now if there was another MOVLB before the second write to NVMCON2, that would matter!

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    GeorgePauley
    Moderator
    • Total Posts : 1138
    • Reward points : 0
    • Joined: 2009/12/01 13:59:30
    • Location: Chandler AZ
    • Status: offline
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/17 15:14:03 (permalink)
    0
    ric
    I think 1and0's main point was, the "magic" sequence does not start until the first write to NVMCON2, so the MOVLB in your example should not have mattered.
    Now if there was another MOVLB before the second write to NVMCON2, that would matter!


    Thanks ric, I hadn't considered 1and0's comment this way.  I thought he was just commenting on the missing MOVLB.  Sorry for missing the obvious 1and0.

    Sometimes being old (and forgetful) sucks.  I'm pretty sure this issue with the magic sequence inside C code has come up before, and folks were told to use asm("") directives to get around the compiler not doing it correctly.  But I've slept a few times since the last time this came up and don't remember all the details.

    In any case, hgl can work around this problem by replacing the the C code with the appropriate asm("") directives (see message #3).  This will allow him to move forward for now.


    #8
    NKurzman
    A Guy on the Net
    • Total Posts : 17599
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/17 15:59:48 (permalink)
    0
    This is has been an issue in the Past with the Free version of XC8.  Or Pro at low optimization levels.
    SO the Question is What version XC8 and what mode C90 or C99?
    And is it actually doing the unlock incorrectly.
    #9
    ric
    Super Member
    • Total Posts : 23185
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/17 16:02:03 (permalink)
    +2 (2)
    We seem to have strayed from the point that the OP reports the WRITES are working ok.
    It is the READS that seem to be offset by one address in the simulator. That has nothing to do with the unlock sequence.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #10
    hgl
    New Member
    • Total Posts : 4
    • Reward points : 0
    • Joined: 2019/07/01 06:36:35
    • Location: 0
    • Status: offline
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/17 23:05:58 (permalink)
    0
    Thanks for all your answers!
     
    Exactly, the writes are OK; I only have problems with the reads.
     
    However, it seems to make sense to implement certain stuff in assembler. I have never written any line of assembler code nor embedded it into C, but I think that'll be no problem after reading TFM.
     
    I'll also give a try to read the program memory via FSR/INDF registers. => Good idea!
     
    I am using the free version of XC8. The linker option "link in C library" is "C99". I compile without optimizations.
    #11
    ric
    Super Member
    • Total Posts : 23185
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/17 23:16:35 (permalink)
    +1 (1)
    hgl
    I'll also give a try to read the program memory via FSR/INDF registers. => Good idea!

    Note, when you are accessing these registers directly, Bit 15 is the RAM/ROM switch.
    Bit 15 low accesses RAM
    Bit 15 high accesses ROM
    Note "4.1.1.2 Indirect Read with FSR" in the datasheet.
    Also have a look at "TABLE 13-2: NVM ORGANIZATION AND ACCESS INFORMATION" to see what you can and cannot access via FSR/INDF.
    Also note the very useful instruction:
    MOVIW FSR0++
    which simultaneously reads a location and does a true 16 bit increment of the address in FSR.
    Unfortunately the syntax of that instruction differs between MPASM, and XC8 inline assembly.
    See "5.2.1.5 MOVIW/MOVWI INSTRUCTIONS" in the C99 version of the XC8 manual, or "6.2.1.5 MOVIW/MOVWI INSTRUCTIONS" in the old version.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #12
    GeorgePauley
    Moderator
    • Total Posts : 1138
    • Reward points : 0
    • Joined: 2009/12/01 13:59:30
    • Location: Chandler AZ
    • Status: offline
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/18 09:15:45 (permalink)
    0
    ric
    We seem to have strayed from the point that the OP reports the WRITES are working ok.
    It is the READS that seem to be offset by one address in the simulator. That has nothing to do with the unlock sequence.



    Sorry, when stepping through simulator code with hgl's project I observed the simulator getting "confused" during the magic sequence for the write.  It never really recovers from this confusion so subsequent operations like the read don't have a chance.
    #13
    1and0
    Access is Denied
    • Total Posts : 9484
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/18 09:31:33 (permalink)
    0
    GeorgePauley
     
    Sorry, when stepping through simulator code with hgl's project I observed the simulator getting "confused" during the magic sequence for the write.  It never really recovers from this confusion so subsequent operations like the read don't have a chance.

    George, when I commented out the write
    //    writeLifetimeCounterToFlashMemory(counter);

    and edited flash memory locations as
     
    address 0x780: content 0x3F12
    address 0x781: content 0x3F34
    address 0x782: content 0x3F56
     
    counter = readCounterFromFlashMemory();

    yields counter = 0x00001234 in the simulator, which is still off by one.  So it cannot be the magic sequence confusion.?
    #14
    1and0
    Access is Denied
    • Total Posts : 9484
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/18 09:44:01 (permalink)
    0
    Interesting, on subsequent reads it yields counter = 0x00561234 in the simulator. Hope this will help you fix the simulator. ;)
    #15
    davekw7x
    Entropy++
    • Total Posts : 1789
    • Reward points : 0
    • Joined: 2012/01/16 12:01:07
    • Location: Left Coast, USA
    • Status: offline
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/18 10:09:14 (permalink)
    0
    ric
    I've wasted too many hours chasing bugs in the simulator...

     +1 for that.
     
    ric
    You don't need to use the NVM registers at all ...

    A couple of observations:

    1.
    You can let XC8 generate the sequence to read from Program Memory.

    For example try the following in main():

        volatile unsigned long counter3;
        volatile unsigned char chu;
        volatile unsigned char chh;
        volatile unsigned char chl;
        .
        .
        .
        // Let the compiler generate the PFM read sequence
        chu = placeholder00;
        chh = placeholder01;
        chl = placeholder02;
        NOP(); // Maybe put breakpoint here

        // Combine the bytes
        counter3 = placeholder00;
        counter3 = (counter3 << 8) | placeholder01;
        counter3 = (counter3 << 8) | placeholder02;
        NOP(); // Put breakpoint here

    As a matter of habit i put nop instructions where I expect to put a breakpoint.

    Which brings me to my other observation:
    2.
    When I put a nop right after setting the RD bit, the simulator gave expected results.

        // enable read operation
        NVMCON1bits.RD = 1;
        
        // CPU operation is suspended until the read is complete, therefore
        // we don't need any NOPs here.
        NOP(); // But this makes it work in the simulator // Maybe put breakpoint here
        return NVMDATL; // only lower 8 bits are of interest (HEF in last 128 words)

    Bottom line: A real chip should work without the nop, but how much time have you (we) spent on the simulator that still leaves us not knowing for sure?

    Regards,

    Dave



     
     
    post edited by davekw7x - 2019/07/18 10:14:51

    Sometimes I just can't help myself...
    #16
    1and0
    Access is Denied
    • Total Posts : 9484
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/18 10:27:25 (permalink)
    0
    davekw7x
    When I put a nop right after setting the RD bit, the simulator gave expected results.

        // enable read operation
        NVMCON1bits.RD = 1;
        
        // CPU operation is suspended until the read is complete, therefore
        // we don't need any NOPs here.
        NOP(); // But this makes it work in the simulator // Maybe put breakpoint here
        return NVMDATL; // only lower 8 bits are of interest (HEF in last 128 words)

    Bottom line: A real chip should work without the nop, but how much time have you (we) spent on the simulator that still leaves us not knowing for sure?

    Good job, Dave!  I seems to remember now that this came up before, but I cannot find that thread now. :(
     
    #17
    1and0
    Access is Denied
    • Total Posts : 9484
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/18 10:33:47 (permalink)
    0
    Found it now!  See Post #9 here: https://www.microchip.com/forums/FindPost/1096828
    #18
    GeorgePauley
    Moderator
    • Total Posts : 1138
    • Reward points : 0
    • Joined: 2009/12/01 13:59:30
    • Location: Chandler AZ
    • Status: offline
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/19 16:15:22 (permalink)
    0
    Sigh... There is a 3.5 year old fix-it discussing the extra NOP issue.  On older devices it took 2 cycles to read flash.  On newer devices it takes 0.  The reason the fix-it is still in not fixed is because the underlying device data files do not give us timing specs on flash reads (we get timing for writes and erases, but not for reads) so we don't know which devices are which.  3 years ago there were more (current-active) devices that used 2 cycles than used 0.  That situation has likely changed now.

    So, we plan to change the simulator so that all 16F devices will have 0 cycle reads.  This likely "breaks" the timing on the older devices.  But likely more users need 0 cycles than 2 cycles at this point.
    #19
    ric
    Super Member
    • Total Posts : 23185
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Read from flash memory seems to access wrong address in simulator for PIC16F15313 2019/07/19 21:56:56 (permalink)
    +2 (2)
    GeorgePauley
    ... the underlying device data files do not give us timing specs on flash reads (we get timing for writes and erases, but not for reads) so we don't know which devices are which. 

    If the simulator is supposed to be an accurate reflection of actual device behaviour, then this would sound like a strong argument for adding that information to the device data files.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #20
    Jump to:
    © 2019 APG vNext Commercial Version 4.5