• AVR Freaks

Hot!XC16 v1.50 "assignment discards qualifiers from pointer target type"

Author
Shuaah
Starting Member
  • Total Posts : 19
  • Reward points : 0
  • Joined: 2018/11/16 06:07:14
  • Location: 0
  • Status: offline
2020/12/01 09:08:23 (permalink)
4 (1)

XC16 v1.50 "assignment discards qualifiers from pointer target type"

Hello,
I have the following structure defined in my code to implement a circular queue:

typedef struct{
PWM_BUFFER_ENTRY *head;
PWM_BUFFER_ENTRY *tail;
PWM_BUFFER_ENTRY buffer[PWM_BUFFER_SIZE];
bool enabled;
bool empty;
}PWM_BUFFER;

I have a global PWM_BUFFER declared as static and volatile like so:

static volatile PWM_BUFFER mybuffer;

I need to have it declared as volatile since it will be accessed within an ISR.
 
And in my initialization routine, I have the following lines of code where I'm getting the compiler warning "warning: assignment discards qualifiers from pointer target type":

mybuffer.head = mybuffer.buffer;
mybuffer.tail = mybuffer.buffer;

 
I think I understand why I'm getting the warning. Since the struct is declared volatile, the head and tail pointers are also volatile. However, the pointer generated by the array name (mybuffer.buffer) is not volatile; only the data in the array is. So the compiler sees that I'm assigning a volatile pointer to a non-volatile address and isn't happy.
 
I get the same warnings when I "loop" the pointers back in my store and remove routines.
 
My question is: what exactly is the "right" way to do this? I've tried casting mybuffer.buffer as a volatile pointer but that didn't seem to help. I assume there has to be a way to implement this kind of thing without causing the compiler to throw warnings all the time.
 
Thanks,
Shuaah
 
#1

5 Replies Related Threads

    crosland
    Super Member
    • Total Posts : 2145
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: XC16 v1.50 "assignment discards qualifiers from pointer target type" 2020/12/01 10:33:13 (permalink)
    -1 (1)
    You haven't shown us how PWM_BUFFER_ENTRY is defined.
     
    Shuaah
    I've tried casting mybuffer.buffer as a volatile pointer but that didn't seem to help. 


    Volatile pointer to what?
     
    It always best to show ALL the code that is relevant.
    #2
    Shuaah
    Starting Member
    • Total Posts : 19
    • Reward points : 0
    • Joined: 2018/11/16 06:07:14
    • Location: 0
    • Status: offline
    Re: XC16 v1.50 "assignment discards qualifiers from pointer target type" 2020/12/01 10:59:47 (permalink)
    0
    Here are the definitions for all the relevant structs that I have defined:

    #define PWM_BUFFER_SIZE 21
    typedef enum{
    PASS = 0,
    FAIL = 1,
    INVALID = 2,
    }PWM_LIMIT_STATUS;
    typedef struct{
    uint32_t hightime;
    uint32_t period;
    }PWM_CYCLE_DATA;
    typedef struct{
    uint32_t risingval;
    uint32_t fallingval;
    }PWM_EDGE_DATA;
    typedef struct{
    uint32_t hightime_upper;
    uint32_t hightime_lower;
    uint32_t period_upper;
    uint32_t period_lower;
    }PWM_LIMITS;
    typedef struct{
    PWM_CYCLE_DATA data;
    uint16_t timestamp;
    PWM_LIMIT_STATUS status;
    }PWM_BUFFER_ENTRY;
    typedef struct{
    PWM_BUFFER_ENTRY *head;
    PWM_BUFFER_ENTRY *tail;
    PWM_BUFFER_ENTRY buffer[PWM_BUFFER_SIZE];
    bool enabled;
    bool empty;
    }PWM_BUFFER;

     
    The cast I tried using was as follows:

    mybuffer.head = (volatile PWM_BUFFER_ENTRY *) mybuffer.buffer;

     
    Please let me know if there is more information I can provide.
    #3
    andersm
    Super Member
    • Total Posts : 2868
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: XC16 v1.50 "assignment discards qualifiers from pointer target type" 2020/12/01 13:19:57 (permalink)
    +2 (2)
    Add the volatile qualifier to your pointers. You want a pointer-to-volatile, not a volatile pointer.
     
    typedef struct {
      volatile PWM_BUFFER_ENTRY *head;
      volatile PWM_BUFFER_ENTRY *tail;
      PWM_BUFFER_ENTRY buffer[PWM_BUFFER_SIZE];
      bool enabled;
      bool empty;
    } PWM_BUFFER;

    #4
    Shuaah
    Starting Member
    • Total Posts : 19
    • Reward points : 0
    • Joined: 2018/11/16 06:07:14
    • Location: 0
    • Status: offline
    Re: XC16 v1.50 "assignment discards qualifiers from pointer target type" 2020/12/02 07:31:39 (permalink)
    0
    andersm
    Add the volatile qualifier to your pointers. You want a pointer-to-volatile, not a volatile pointer.

    Are you sure? Naturally, the pointer values themselves are getting updated in the ISR as data is inserted, so it seems like they need to be volatile pointers.
     
    If I do need to add the volatile qualifier to the pointers, do I still need to declare mybuffer as volatile?
    #5
    Shuaah
    Starting Member
    • Total Posts : 19
    • Reward points : 0
    • Joined: 2018/11/16 06:07:14
    • Location: 0
    • Status: offline
    Re: XC16 v1.50 "assignment discards qualifiers from pointer target type" 2020/12/03 08:41:48 (permalink)
    0
    OK I've tried andersm's solution of adding the volatile qualifier to the pointers, but kept the volatile qualifier on the struct declaration so that the pointers themselves are still volatile. This gets rid of the warning, and the code seems to function.
     
    Thanks!
    #6
    Jump to:
    © 2021 APG vNext Commercial Version 4.5