This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

xdata access by #define

I have to read a flash written by another program and am provided a file looking like this

// data locations
#define data1 0
#define data2 data1+0x100
...

I would like to use the file 'as is" but all my typecasting efforts have resulted in errors or wrong code.

In advance, thanks

Erik

Parents
  • Without knowing what errors or wrong code you were getting, I can only guess that the typecasting failures are due data2's expression definition lacking parentheses to avoid side effects. To use the include file unchanged, I would suggest you provide the parentheses explicitly or by xdata access macros as in:

    #define XDATA_BYTE_ACCESS(addr) (*(unsigned char xdata *)(addr))
    #define XDATA_WORD_ACCESS(addr) (*(unsigned int  xdata *)(addr))
    
    unsigned char b;
    unsigned int  w;
    
    void main(void)
    {
        b = XDATA_BYTE_ACCESS(data2);
        w = XDATA_WORD_ACCESS(data2);
    }
    

Reply
  • Without knowing what errors or wrong code you were getting, I can only guess that the typecasting failures are due data2's expression definition lacking parentheses to avoid side effects. To use the include file unchanged, I would suggest you provide the parentheses explicitly or by xdata access macros as in:

    #define XDATA_BYTE_ACCESS(addr) (*(unsigned char xdata *)(addr))
    #define XDATA_WORD_ACCESS(addr) (*(unsigned int  xdata *)(addr))
    
    unsigned char b;
    unsigned int  w;
    
    void main(void)
    {
        b = XDATA_BYTE_ACCESS(data2);
        w = XDATA_WORD_ACCESS(data2);
    }
    

Children