데이터 포인터 및 코드 포인터

기술노트 84583

아키텍처:

AVR

컴포넌트:

compiler

업데이트:

2018-02-28 오전 1:46

Introduction

This Technical Note discusses inconsistent use of a DATA pointer to point into CODE memory, (which the IAR AVR C/C++ compiler detects).

Discussion

The below source code...

void fun (void)
{
;
}
void *p = (void*)fun;
int main( void )
{
return 0;
}

...will give this error message:

Error[Pe028]: expression must have a constant value ...\main.c

Background to the message

The message is produced because the pointer "void *" is a DATA pointer. This pointer type is very different from a FUNCTION pointer which points into CODE memory. The reported error occurs when the data pointer is smaller than the code pointer. Using the option -mt the default data pointer is 8 bits and with the -ms the default data pointer is 16 bits.

Solution

  • The IAR AVR C/C++ compiler version 6.10 (and nwere) is able to detect these kinds of problems. The correct way of re-writing the source is:
  • void fun (void)
    {
    ;
    }
    typedef void (*f_p)(void);
    f_p p = fun;
  • To avoid the problem use the compiler options -v3 -ms or -v6 -mh to get the same size for the data pointer and the code pointer.
  • If using an xmega device, for example ATxmega128D4, that is an -v5 device use the options -v5 -ms but place the function in 'near memory' with the keyword __nearfunc.
    __nearfunc void fun (void)
    {
    ;
    }
    void *p = (void*)fun;
    or use the pragma type_attribute:
    #pragma type_attribute=__nearfunc

    void fun (void)
    {
    ;
    }

    void *p = (void*)fun;

 

All product names are trademarks or registered trademarks of their respective owners.

죄송하지만, 당사 사이트에서는 Internet Explorer를 지원하지 않습니다.보다 편안한 사이트를 위해 Chrome, Edge, Firefox 등과 같은 최신 브라우저를 사용해 주시길 부탁드립니다.