This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

const arrays in flash



Hello

To save RAM I defined an array of struct as const, so the array will not be copied from flash into RAM.

struct stMyStruct = {unsigned short usElement1[6],
                                   unsigned short usElement2[6],
                                   ...};   (28 Elements)
const struct stMyStruct MyStruct[2] = {{{1, 2, 3, 4, 5, 6},
                                                                    {1, 2, 3, 4, 5, 6},
                                                                    {...}},
                                                                   {{1, 2, 3, 4, 5, 6},
                                                                    {1, 2, 3, 4, 5, 6},
                                                                    {...}}};

When I read data from this struct array, i always get the address of the chosen element, instead of the data.
Is it not possible to read data directly from flash?

I also tried to resolve this "pointer", but without success.

Temp = *(unsigned short *) MyStruct[0].usElement1[0];

Greets
Phil

 

  • Phil,

    How are you normally trying to read the struct?

    MyStruct[0].usElement[0] should be all you need.  If you want to get a pointer to the element you would do something like &(MyStruct[0].usElement[0]).

     

    If you still can't get it working, send me a copy of the exact struct you are trying to access and the way you are accessing it and I'll plug it in here to my copy of CCS.

    Trey

  • Hi

    This is my struct and its initialisation.

    struct stDeviceParameter
    {
        unsigned short Achse1_Stromregler_FU_P_GL[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_I_down_GL[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_I_up_GL[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_P_fast_GL[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_I_fast_GL[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_D_GL[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_D_fast_GL[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_P_GL[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_I_down_GL[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_I_up_GL[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_P_fast_GL[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_I_fast_GL[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_D_GL[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_D_fast_GL[NUM_OF_DATASETS];

        unsigned short Achse1_Stromregler_FU_P_ML[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_I_down_ML[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_I_up_ML[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_P_fast_ML[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_I_fast_ML[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_D_ML[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_D_fast_ML[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_P_ML[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_I_down_ML[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_I_up_ML[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_P_fast_ML[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_I_fast_ML[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_D_ML[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_D_fast_ML[NUM_OF_DATASETS];
    };

    const struct stDeviceParameter DevParam[2] = 
      {{{   0,     0,     0,     0,     0,     0},  // Achse1_Stromregler_FU_P_GL[NUM_OF_DATASETS];
        {  10,    10,    20,    10,    20,    10},  // Achse1_Stromregler_FU_I_down_GL[NUM_OF_DATASETS];
        {  30,    30,    30,    30,    30,    20},  // Achse1_Stromregler_FU_I_up_GL[NUM_OF_DATASETS];
        { 300,   300,   300,   300,   300,   300},  // Achse1_Stromregler_FU_P_fast_GL[NUM_OF_DATASETS];
        { 350,   350,   500,   350,   500,   500},  // Achse1_Stromregler_FU_I_fast_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse1_Stromregler_FU_D_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse1_Stromregler_FU_D_fast_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse2_Stromregler_FU_P_GL[NUM_OF_DATASETS];
        {  10,    10,    20,    10,    20,    10},  // Achse2_Stromregler_FU_I_down_GL[NUM_OF_DATASETS];
        {  30,    30,    30,    30,    30,    20},  // Achse2_Stromregler_FU_I_up_GL[NUM_OF_DATASETS];
        { 300,   300,   300,   300,   300,   300},  // Achse2_Stromregler_FU_P_fast_GL[NUM_OF_DATASETS];
        { 350,   350,   500,   350,   500,   500},  // Achse2_Stromregler_FU_I_fast_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse2_Stromregler_FU_D_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse2_Stromregler_FU_D_fast_GL[NUM_OF_DATASETS];

        {   0,     0,     0,     0,     0,     0},  // Achse1_Stromregler_FU_P_ML[NUM_OF_DATASETS];
        {  10,    10,    10,    10,    10,    10},  // Achse1_Stromregler_FU_I_down_ML[NUM_OF_DATASETS];
        {  20,    20,    20,    40,    20,    15},  // Achse1_Stromregler_FU_I_up_ML[NUM_OF_DATASETS];
        { 150,   150,   150,   250,   150,   100},  // Achse1_Stromregler_FU_P_fast_ML[NUM_OF_DATASETS];
        { 250,   250,   250,   500,   350,  1000},  // Achse1_Stromregler_FU_I_fast_ML[NUM_OF_DATASETS];
        {1000,  1000,  1000,  1000,  1000,     0},  // Achse1_Stromregler_FU_D_ML[NUM_OF_DATASETS];
        {1000,  1000,  1000,  1000,  1000,     0},  // Achse1_Stromregler_FU_D_fast_ML[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse2_Stromregler_FU_P_ML[NUM_OF_DATASETS];
        {  10,    10,    10,    10,    10,    10},  // Achse2_Stromregler_FU_I_down_ML[NUM_OF_DATASETS];
        {  20,    20,    20,    40,    20,    15},  // Achse2_Stromregler_FU_I_up_ML[NUM_OF_DATASETS];
        { 150,   150,   150,   250,   150,   100},  // Achse2_Stromregler_FU_P_fast_ML[NUM_OF_DATASETS];
        { 250,   250,   250,   500,   350,  1000},  // Achse2_Stromregler_FU_I_fast_ML[NUM_OF_DATASETS];
        {2000,  2000,  1000,  1000,  1000,     0},  // Achse2_Stromregler_FU_D_ML[NUM_OF_DATASETS];
        {2000,  2000,  1000,  1000,  1000,     0}}, // Achse2_Stromregler_FU_D_fast_ML[NUM_OF_DATASETS];

       {{   0,     0,     0,     0,     0,     0},  // Achse1_Stromregler_FU_P_GL[NUM_OF_DATASETS];
        {  10,    10,    20,    10,    20,    10},  // Achse1_Stromregler_FU_I_down_GL[NUM_OF_DATASETS];
        {  30,    30,    30,    30,    30,    20},  // Achse1_Stromregler_FU_I_up_GL[NUM_OF_DATASETS];
        { 300,   300,   300,   300,   300,   300},  // Achse1_Stromregler_FU_P_fast_GL[NUM_OF_DATASETS];
        { 350,   350,   500,   350,   500,   500},  // Achse1_Stromregler_FU_I_fast_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse1_Stromregler_FU_D_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse1_Stromregler_FU_D_fast_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse2_Stromregler_FU_P_GL[NUM_OF_DATASETS];
        {  10,    10,    20,    10,    20,    10},  // Achse2_Stromregler_FU_I_down_GL[NUM_OF_DATASETS];
        {  30,    30,    30,    30,    30,    20},  // Achse2_Stromregler_FU_I_up_GL[NUM_OF_DATASETS];
        { 300,   300,   300,   300,   300,   300},  // Achse2_Stromregler_FU_P_fast_GL[NUM_OF_DATASETS];
        { 350,   350,   500,   350,   500,   500},  // Achse2_Stromregler_FU_I_fast_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse2_Stromregler_FU_D_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse2_Stromregler_FU_D_fast_GL[NUM_OF_DATASETS];

        {   0,     0,     0,     0,     0,     0},  // Achse1_Stromregler_FU_P_ML[NUM_OF_DATASETS];
        {  10,    10,    10,    10,    10,    10},  // Achse1_Stromregler_FU_I_down_ML[NUM_OF_DATASETS];
        {  20,    20,    20,    40,    20,    15},  // Achse1_Stromregler_FU_I_up_ML[NUM_OF_DATASETS];
        { 150,   150,   150,   250,   150,   100},  // Achse1_Stromregler_FU_P_fast_ML[NUM_OF_DATASETS];
        { 250,   250,   250,   500,   350,  1000},  // Achse1_Stromregler_FU_I_fast_ML[NUM_OF_DATASETS];
        {1000,  1000,  1000,  1000,  1000,     0},  // Achse1_Stromregler_FU_D_ML[NUM_OF_DATASETS];
        {1000,  1000,  1000,  1000,  1000,     0},  // Achse1_Stromregler_FU_D_fast_ML[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse2_Stromregler_FU_P_ML[NUM_OF_DATASETS];
        {  10,    10,    10,    10,    10,    10},  // Achse2_Stromregler_FU_I_down_ML[NUM_OF_DATASETS];
        {  20,    20,    20,    40,    20,    15},  // Achse2_Stromregler_FU_I_up_ML[NUM_OF_DATASETS];
        { 150,   150,   150,   250,   150,   100},  // Achse2_Stromregler_FU_P_fast_ML[NUM_OF_DATASETS];
        { 250,   250,   250,   500,   350,  1000},  // Achse2_Stromregler_FU_I_fast_ML[NUM_OF_DATASETS];
        {2000,  2000,  1000,  1000,  1000,     0},  // Achse2_Stromregler_FU_D_ML[NUM_OF_DATASETS];
        {2000,  2000,  1000,  1000,  1000,     0}}};// Achse2_Stromregler_FU_D_fast_ML[NUM_OF_DATASETS];

    I tried to read like this: Temp = DevParam[0].Achse1_Stromregler_FU_P_GL[0];
    But I always got the address instead of the data.

    Then I tried to read like this: Temp = *(unsigned short *)DevParam[0].Achse1_Stromregler_FU_P_GL[0];
    But I also got the address.

    What's wrong?

    Greets
    Phil

  • Hi Phil,

    I tossed your code into my compiler and the first access (data) you have listed above works just fine on my machine.

    The second access you do where you are trying to get the address of the value is incorrect though.  You are telling the compiler to interpret the value at DevParam[0]... as an address and to go get the value at that address.  Instead you should have used the & like this:

        Temp = (unsigned long) &(DevParam[0].Achse1_Stromregler_FU_P_GL[0]);

    That will give you the address of that value and allow you to store it into an unsigned long.

    I've attached my test code in case you want to try it.  Just copy it into a blank project, run it, and it should work.  Let me know if you are still having trouble.

    #define NUM_OF_DATASETS 6
    
    struct stDeviceParameter
    {
        unsigned short Achse1_Stromregler_FU_P_GL[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_I_down_GL[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_I_up_GL[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_P_fast_GL[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_I_fast_GL[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_D_GL[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_D_fast_GL[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_P_GL[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_I_down_GL[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_I_up_GL[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_P_fast_GL[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_I_fast_GL[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_D_GL[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_D_fast_GL[NUM_OF_DATASETS];
    
        unsigned short Achse1_Stromregler_FU_P_ML[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_I_down_ML[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_I_up_ML[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_P_fast_ML[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_I_fast_ML[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_D_ML[NUM_OF_DATASETS];
        unsigned short Achse1_Stromregler_FU_D_fast_ML[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_P_ML[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_I_down_ML[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_I_up_ML[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_P_fast_ML[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_I_fast_ML[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_D_ML[NUM_OF_DATASETS];
        unsigned short Achse2_Stromregler_FU_D_fast_ML[NUM_OF_DATASETS];
    };
    
    const struct stDeviceParameter DevParam[2] = 
      {{{   0,     0,     0,     0,     0,     0},  // Achse1_Stromregler_FU_P_GL[NUM_OF_DATASETS];
        {  10,    10,    20,    10,    20,    10},  // Achse1_Stromregler_FU_I_down_GL[NUM_OF_DATASETS];
        {  30,    30,    30,    30,    30,    20},  // Achse1_Stromregler_FU_I_up_GL[NUM_OF_DATASETS];
        { 300,   300,   300,   300,   300,   300},  // Achse1_Stromregler_FU_P_fast_GL[NUM_OF_DATASETS];
        { 350,   350,   500,   350,   500,   500},  // Achse1_Stromregler_FU_I_fast_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse1_Stromregler_FU_D_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse1_Stromregler_FU_D_fast_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse2_Stromregler_FU_P_GL[NUM_OF_DATASETS];
        {  10,    10,    20,    10,    20,    10},  // Achse2_Stromregler_FU_I_down_GL[NUM_OF_DATASETS];
        {  30,    30,    30,    30,    30,    20},  // Achse2_Stromregler_FU_I_up_GL[NUM_OF_DATASETS];
        { 300,   300,   300,   300,   300,   300},  // Achse2_Stromregler_FU_P_fast_GL[NUM_OF_DATASETS];
        { 350,   350,   500,   350,   500,   500},  // Achse2_Stromregler_FU_I_fast_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse2_Stromregler_FU_D_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse2_Stromregler_FU_D_fast_GL[NUM_OF_DATASETS];
    
        {   0,     0,     0,     0,     0,     0},  // Achse1_Stromregler_FU_P_ML[NUM_OF_DATASETS];
        {  10,    10,    10,    10,    10,    10},  // Achse1_Stromregler_FU_I_down_ML[NUM_OF_DATASETS];
        {  20,    20,    20,    40,    20,    15},  // Achse1_Stromregler_FU_I_up_ML[NUM_OF_DATASETS];
        { 150,   150,   150,   250,   150,   100},  // Achse1_Stromregler_FU_P_fast_ML[NUM_OF_DATASETS];
        { 250,   250,   250,   500,   350,  1000},  // Achse1_Stromregler_FU_I_fast_ML[NUM_OF_DATASETS];
        {1000,  1000,  1000,  1000,  1000,     0},  // Achse1_Stromregler_FU_D_ML[NUM_OF_DATASETS];
        {1000,  1000,  1000,  1000,  1000,     0},  // Achse1_Stromregler_FU_D_fast_ML[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse2_Stromregler_FU_P_ML[NUM_OF_DATASETS];
        {  10,    10,    10,    10,    10,    10},  // Achse2_Stromregler_FU_I_down_ML[NUM_OF_DATASETS];
        {  20,    20,    20,    40,    20,    15},  // Achse2_Stromregler_FU_I_up_ML[NUM_OF_DATASETS];
        { 150,   150,   150,   250,   150,   100},  // Achse2_Stromregler_FU_P_fast_ML[NUM_OF_DATASETS];
        { 250,   250,   250,   500,   350,  1000},  // Achse2_Stromregler_FU_I_fast_ML[NUM_OF_DATASETS];
        {2000,  2000,  1000,  1000,  1000,     0},  // Achse2_Stromregler_FU_D_ML[NUM_OF_DATASETS];
        {2000,  2000,  1000,  1000,  1000,     0}}, // Achse2_Stromregler_FU_D_fast_ML[NUM_OF_DATASETS];
    
       {{   0,     0,     0,     0,     0,     0},  // Achse1_Stromregler_FU_P_GL[NUM_OF_DATASETS];
        {  10,    10,    20,    10,    20,    10},  // Achse1_Stromregler_FU_I_down_GL[NUM_OF_DATASETS];
        {  30,    30,    30,    30,    30,    20},  // Achse1_Stromregler_FU_I_up_GL[NUM_OF_DATASETS];
        { 300,   300,   300,   300,   300,   300},  // Achse1_Stromregler_FU_P_fast_GL[NUM_OF_DATASETS];
        { 350,   350,   500,   350,   500,   500},  // Achse1_Stromregler_FU_I_fast_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse1_Stromregler_FU_D_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse1_Stromregler_FU_D_fast_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse2_Stromregler_FU_P_GL[NUM_OF_DATASETS];
        {  10,    10,    20,    10,    20,    10},  // Achse2_Stromregler_FU_I_down_GL[NUM_OF_DATASETS];
        {  30,    30,    30,    30,    30,    20},  // Achse2_Stromregler_FU_I_up_GL[NUM_OF_DATASETS];
        { 300,   300,   300,   300,   300,   300},  // Achse2_Stromregler_FU_P_fast_GL[NUM_OF_DATASETS];
        { 350,   350,   500,   350,   500,   500},  // Achse2_Stromregler_FU_I_fast_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse2_Stromregler_FU_D_GL[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse2_Stromregler_FU_D_fast_GL[NUM_OF_DATASETS];
    
        {   0,     0,     0,     0,     0,     0},  // Achse1_Stromregler_FU_P_ML[NUM_OF_DATASETS];
        {  10,    10,    10,    10,    10,    10},  // Achse1_Stromregler_FU_I_down_ML[NUM_OF_DATASETS];
        {  20,    20,    20,    40,    20,    15},  // Achse1_Stromregler_FU_I_up_ML[NUM_OF_DATASETS];
        { 150,   150,   150,   250,   150,   100},  // Achse1_Stromregler_FU_P_fast_ML[NUM_OF_DATASETS];
        { 250,   250,   250,   500,   350,  1000},  // Achse1_Stromregler_FU_I_fast_ML[NUM_OF_DATASETS];
        {1000,  1000,  1000,  1000,  1000,     0},  // Achse1_Stromregler_FU_D_ML[NUM_OF_DATASETS];
        {1000,  1000,  1000,  1000,  1000,     0},  // Achse1_Stromregler_FU_D_fast_ML[NUM_OF_DATASETS];
        {   0,     0,     0,     0,     0,     0},  // Achse2_Stromregler_FU_P_ML[NUM_OF_DATASETS];
        {  10,    10,    10,    10,    10,    10},  // Achse2_Stromregler_FU_I_down_ML[NUM_OF_DATASETS];
        {  20,    20,    20,    40,    20,    15},  // Achse2_Stromregler_FU_I_up_ML[NUM_OF_DATASETS];
        { 150,   150,   150,   250,   150,   100},  // Achse2_Stromregler_FU_P_fast_ML[NUM_OF_DATASETS];
        { 250,   250,   250,   500,   350,  1000},  // Achse2_Stromregler_FU_I_fast_ML[NUM_OF_DATASETS];
        {2000,  2000,  1000,  1000,  1000,     0},  // Achse2_Stromregler_FU_D_ML[NUM_OF_DATASETS];
        {2000,  2000,  1000,  1000,  1000,     0}}};// Achse2_Stromregler_FU_D_fast_ML[NUM_OF_DATASETS];
        
        
    void main(void)
    {
    	volatile unsigned long Temp;
    	//Data
    	Temp = DevParam[0].Achse1_Stromregler_FU_P_GL[0];
    	//Address of data
    	Temp = (unsigned long) &(DevParam[0].Achse1_Stromregler_FU_P_GL[0]);
    	
    }

     

    Trey