• AVR Freaks

Hot!(1402) a pointer to eeprom cannot also point to other data types

Author
AlanMcR
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2018/07/26 10:42:22
  • Location: 0
  • Status: offline
2019/05/03 17:06:05 (permalink)
0

(1402) a pointer to eeprom cannot also point to other data types

I'm attempting to do something that seems like it should be easy:
Use both EEPROM and program memory to store values.  My relatively simple code seems to trip up the compiler in a way that I don't understand.
 
#define _XTAL_FREQ 16000000 // Frequency 16MHz = (32MHz/2)

#include <xc.h>
__eeprom long int testy2 = {0x1234};
extern const char LogIndex[];

unsigned short int FLASH_ReadWord(unsigned short int flashAddr) {
    unsigned char GIEBitValue = INTCONbits.GIE; // Save interrupt enable
    INTCONbits.GIE = 0; // Disable interrupts
    NVMADRL = (flashAddr & 0x00FF);
    NVMADRH = ((flashAddr & 0xFF00) >> 8);
    NVMCON1bits.NVMREGS = 0; // Deselect Configuration space
    NVMCON1bits.RD = 1; // Initiate Read
    NOP();
    NOP();
    INTCONbits.GIE = GIEBitValue; // Restore interrupt enable
    return ((unsigned short int)((NVMDATH << 8) | NVMDATL));
}

void main() {
    volatile short int retrievedEeprom, retrievedProgram;
    unsigned short int fixedValue = 555;
    retrievedEeprom = testy2;
    retrievedProgram = FLASH_ReadWord((unsigned int)fixedValue);
    retrievedProgram = FLASH_ReadWord((unsigned int)LogIndex);
}

And, just for good measure, this is the table being stuffed into program memory:
PSECT mathTables,class=CODE,local,noexec,pure,optim=,delta=2
GLOBAL _LogIndex
_LogIndex
  DW 0x0000 ; If=0.05 (Would be DW 0x0006, but we use 0 to terminate the table)
  DW 0x000c ; If=0.1
  

If I include the last line in main [retrievedP ... LogIndex);] the compile fails with the error:
error: (1402) a pointer to eeprom cannot also point to other data types

The previous line [retrievedP ... fixedValue);] does not cause an error.  This has to be something about the way the compiler is trying to work with LogIndex.  I don't understand what is bothering it.  In theory, both addresses should be converted to an ordinary integer and then the function works with that.  In practice they are treated differently. 
 
Any help greatly appreciated.
 
post edited by AlanMcR - 2019/05/04 11:41:33
#1

8 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: (1402) a pointer to eeprom cannot also point to other data types 2019/05/03 17:20:24 (permalink)
    +1 (1)
    You don't mention which PIC family you are working with. Is it PIC16 or PIC18?
    Which version of XC8, and which mode are you in? (C90/C99)
    Pointers are a complex subject in XC8, as it tries to use the optimal size (8/16/24 bits) for how the pointer is being used.
     
    There is a compiler option to tell it to always use the widest (=most universal) pointer size.
    Have a look at "5.4.5.3 DATA POINTERS" in the C90 manual, or
    "4.4.6.3 DATA POINTERS" in the C99 manual.
     

    Nearly there...
    #2
    1and0
    Access is Denied
    • Total Posts : 9607
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: (1402) a pointer to eeprom cannot also point to other data types 2019/05/03 18:08:49 (permalink)
    0
    My crystal ball says it's a PIC16. ;)
     
    #3
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: (1402) a pointer to eeprom cannot also point to other data types 2019/05/03 18:34:30 (permalink)
    +1 (1)
    That was my guess, as the whole point of the exercise seems to be to access all 14 bits of code memory.

    Nearly there...
    #4
    1and0
    Access is Denied
    • Total Posts : 9607
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: (1402) a pointer to eeprom cannot also point to other data types 2019/05/03 18:59:22 (permalink)
    0
    I tried OP's code with XC8 v2.05 in C99 mode with a PIC16F18324. The error message points to this line
    NVMADRL = (flashAddr & 0x00FF);

    which is weird; comment out this line and that error disappears -- go figure!
     
    Replace this

        NVMADRL = (flashAddr & 0x00FF);
        NVMADRH = ((flashAddr & 0xFF00) >> 8);

    with this

        NVMADR = flashAddr;

    and no error.  Compiler bug?
    #5
    AlanMcR
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2018/07/26 10:42:22
    • Location: 0
    • Status: offline
    Re: (1402) a pointer to eeprom cannot also point to other data types 2019/05/03 19:44:40 (permalink)
    0
    Yes, PIC16F8325/XC8, with optimization set to "smaller", I forget the switch.
     
    1and0, that is an interesting observation! I am far from my development system right now, but should I expect that your replacement code would accomplish the same intended result?
     
    One other thing that I failed to mention is that (when it compiles) the correct values are retrieved from the correct places.  So, if I replace (unsigned int)LogIndex with the correct hardcoded constant, I get the right value. 
    #6
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: (1402) a pointer to eeprom cannot also point to other data types 2019/05/03 20:31:26 (permalink)
    +1 (1)
    AlanMcR
    Yes, PIC16F8325/XC8, with optimization set to "smaller", I forget the switch.

    That only answers my first question.
    Which version? (1.xx or 2.xx) and which mode (C90 or C99, only available in 2.xx)
     

    1and0, that is an interesting observation! I am far from my development system right now, but should I expect that your replacement code would accomplish the same intended result?

    Yes, it will write all 16 bits for you.
     

    One other thing that I failed to mention is that (when it compiles) the correct values are retrieved from the correct places.  So, if I replace (unsigned int)LogIndex with the correct hardcoded constant, I get the right value.

    You still need to read the user guide to see why pointers are a complex subject, as you have three independant busses (RAM/FLASH/EEPROM) each with their own address spaces).

    Nearly there...
    #7
    AlanMcR
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2018/07/26 10:42:22
    • Location: 0
    • Status: offline
    Re: (1402) a pointer to eeprom cannot also point to other data types 2019/05/03 21:32:13 (permalink)
    0
    I do understand how access the various buses.  The code snippet demonstrates access to all three.  The last paragraph was merely pointing out that program memory works correctly when the compiler isn't throwing this code..
    #8
    1and0
    Access is Denied
    • Total Posts : 9607
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: (1402) a pointer to eeprom cannot also point to other data types 2019/05/04 01:08:08 (permalink)
    +2 (2)
    Something is weird going on with that program. Commenting out this line
        retrievedEeprom = testy2;

    in Post #1 code also makes the error message disappear. So, I suggest you to submit a support ticket to Microchip.
     
    Anyway, there is no need to disable GIE to read the flash memory.
    #include <stdint.h>
     
    uint16_t FLASH_ReadWord(uint16_t flashAddr)

    {
        NVMADR = flashAddr;
        NVMCON1bits.NVMREGS = 0; // select program flash memory
        NVMCON1bits.RD = 1;
        NOP();    // datasheet has no NOP here, but Simulator fails without it
        return NVMDAT;
    }
    post edited by 1and0 - 2019/05/04 01:09:59
    #9
    Jump to:
    © 2019 APG vNext Commercial Version 4.5