We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi, I have this code working OK in IAR 5 kickstart and GNU toolchain:
typedef struct { uint8_t r; uint8_t g; uint8_t b; } color;
void LCD_fill(uint16_t x, uint16_t y, color fcolor) { [...]
LCD_fill(640,480,(color){0xff,0,0});
However, MDK414 gives the following errors: error: #119: cast to type "color" is not allowed error: #29: expected an expression
were the last one stays when I don't cast.
I see two issues here: 1- the impossibility to cast to the 'color' type, can be removed by adding an indirection level (but I don't want to do that for performance reasons) 2- the impossibility to use a constant structure as the parameter (I also tried defining and casting as 'const' with no success).
However, everything works OK if I define
color RED={0xff,0,0}; // or const color, btw...
and do:
LCD_fill(640,480,RED);
This looks like RealView is playing nasty on me. Any clues ?
"the impossibility to cast to the 'color' type, can be removed by adding an indirection level (but I don't want to do that for performance reasons)"
When performance is important, you should normally avoid call-by-value with a struct. In this special case, the compiler may figure out that the struct is the same size as a 32-bit processor register but the general case when sending a struct as parameter is that the compiler have to create a temporary object, and perform a copy of the data to this temporary object.
Sending pointers to structures is normally very efficient, since the ARM have many registers that can be used to store pointers and it can perform indirect addressing based on this pointer at the receiving end.
But if you look at existing code that plays with 24-bit or 32-bit colors (normal bitmap libraries etc) you'll notice that they normally makes use of a MAKE_COLOR(r,g,b) macro or similar to create a 32-bit integer value.
Thx for your comments.
I'm actually seeing the code in the simulator and (al least IAR) is packing the structure in a register and extracting the bytes from the stack one by one. However, writing 32-bit code for color is slightly faster and the compiler inlines it better; so I will switch to it.
Thanks a lot