Author Topic: C-code; array of struct ? (Atmel Studio 6)  (Read 47923 times)

0 Members and 2 Guests are viewing this topic.

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #75 on: August 12, 2013, 11:15:51 pm »
Just changed the code to use arrays and get this error now!   |O

Code: [Select]
Error 1 unable to find a register to spill in class 'POINTER_REGS'
 

Offline mrflibble

  • Super Contributor
  • ***
  • Posts: 2024
  • Country: nl
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #76 on: August 12, 2013, 11:19:06 pm »
Waitaminute. You said you get no warnings. But if you used the static const char * , you should at the very least get a typecast warning since your struct says caption is a char *.

Maybe use -Wall compiler option somewhere?

And just noticed your update. I have no idea what that one means. :P Other than "ran out of registers on this here atmel so screw you hippie".
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #77 on: August 12, 2013, 11:34:41 pm »
This is getting weirder and weirder.  The "spill" error can be triggered by assigning a value to my structure properties.  But... it also depends on the exact value!  What the hell is going on the AS6 ?
 

Offline ve7xen

  • Frequent Contributor
  • **
  • Posts: 732
  • Country: ca
    • VE7XEN Blog
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #78 on: August 13, 2013, 12:16:34 am »
I'm getting lost as to what the actual problem is and what you're doing. Maybe post your entire code as it stands and we can see where there might be problems?

I would recommend, if you need to generate strings at all, anywhere in your code, that you never use string literals outside of strcpy and the like. It is extremely easy to accidentally mix that up and then you run around corrupting memory and causing weird failures.

It also looks like you might be running out of memory or having other resource allocation problems. Are you using a lot of stack space?
73 de VE7XEN
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 9668
  • Country: us
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #79 on: August 13, 2013, 01:52:25 am »
This is getting weirder and weirder.

When strange weirdness happens, it is often a sign that you have badly constructed code. C is designed to let experts write code to do exactly what they want with maximum efficiency. The corollary is that C is designed to let newcomers shoot themselves in the foot with the greatest of ease.

The direction of this thread suggests that maybe you are doing something horribly wrong? As others have said, maybe it is time to post your whole program and not just snippets of it.
I'm not an EE--what am I doing here?
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #80 on: August 13, 2013, 01:54:47 am »
It also looks like you might be running out of memory or having other resource allocation problems. Are you using a lot of stack space?

Where would I look to check this?
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #81 on: August 13, 2013, 02:23:27 am »
OK, I've rolled the code back to where it compiled this morning.  Maybe we should look at those warnings (to do with the font selection) before going any further.

The warning:
Code: [Select]
expected 'char *' but argument is of type 'const char *'
The function referred to:
Code: [Select]
static void LCD_SetFont(char *font)
{
cfont.font=font;
cfont.x_size=fontbyte(0);
cfont.y_size=fontbyte(1);
cfont.offset=fontbyte(2);
cfont.numchars=fontbyte(3);
}

The definition for the structure above:
Code: [Select]
typedef struct
{
char* font;
uint8_t x_size;
uint8_t y_size;
uint8_t offset;
uint8_t numchars;
}current_font;

current_font cfont;

Part of the font file:
Code: [Select]
const char SmallFont[1144] PROGMEM={         
0x08,0x0C,0x20,0x5F,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // <Space>
0x00,0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x00,0x20,0x00,0x00, // !
...
0x40,0xA4,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, // ~
}; 


The (4) calls that are causing the warnings:
Code: [Select]
void LCD_SelectFont(unsigned char f)
{
switch(f)
{
case 0:
LCD_SetFont(SmallFont); // causes a warning: expected 'char *' but argument is of type 'const char *'
break;
case 1:
LCD_SetFont(BigFont); // causes a warning: expected 'char *' but argument is of type 'const char *'
break;
case 2:
LCD_SetFont(SevenSegNumFont); // causes a warning: expected 'char *' but argument is of type 'const char *'
break;
default:
LCD_SetFont(BigFont); // causes a warning: expected 'char *' but argument is of type 'const char *'
}
}
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 9668
  • Country: us
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #82 on: August 13, 2013, 02:28:12 am »
Well that one is easy. Fix what the compiler is warning about:

Code: [Select]
static void LCD_SetFont(const char *font)
Code: [Select]
typedef struct
{
const char *font;
I'm not an EE--what am I doing here?
 

Offline HackedFridgeMagnet

  • Super Contributor
  • ***
  • Posts: 1974
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #83 on: August 13, 2013, 02:32:37 am »
I appreciate this thread and the other C threads as I am just getting back up to speed on C.

Quote
Where would I look to check this?
Stack pointer register and know what is allocated to the stack, and whether it is the main stack of just a thread's stack.

Can you attach a debugger?
I would probably approach it by working out if it is the ' ' char or the '*' char or if it is the position of the assignments in the compiled code.
Look at the values in the debugger and what is getting assigned to what.
Check the dissassembly if it makes any sense to see what the compiler is doing.
Check the memory to see if the values are being assigned where you expect.

Warnings are good to watch, if you can reduce them to under 100. I can't I am using Yagarto.

Can you change LCD_SetFont to (const char* font)?  Beaten by IanB



 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #84 on: August 13, 2013, 02:33:54 am »
I did that after my last post, but now I have just one (different) warning:

Code: [Select]
assignment discards 'const' qualifier from pointer target type [enabled by default]
The warning refers to

Code: [Select]
static void LCD_SetFont(const char *font)
{
cfont.font=font; // causes warning: assignment discards 'const' qualifier from pointer target type [enabled by default]
cfont.x_size=fontbyte(0);
cfont.y_size=fontbyte(1);
cfont.offset=fontbyte(2);
cfont.numchars=fontbyte(3);
}
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #85 on: August 13, 2013, 02:35:53 am »
As for the other issue (wrong caption text), it happens to other buttons too.   Only when the caption is set to "" or "[space]" though.
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 9668
  • Country: us
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #86 on: August 13, 2013, 02:38:47 am »
I did that after my last post, but now I have just one (different) warning:

Code: [Select]
assignment discards 'const' qualifier from pointer target type [enabled by default]
The warning refers to

Code: [Select]
static void LCD_SetFont(const char *font)
{
cfont.font=font; // causes warning: assignment discards 'const' qualifier from pointer target type [enabled by default]
cfont.x_size=fontbyte(0);
cfont.y_size=fontbyte(1);
cfont.offset=fontbyte(2);
cfont.numchars=fontbyte(3);
}

You did this, right?

Code: [Select]
typedef struct
{
const char *font;
uint8_t x_size;
uint8_t y_size;
uint8_t offset;
uint8_t numchars;
}current_font;
I'm not an EE--what am I doing here?
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #87 on: August 13, 2013, 02:42:03 am »
No, I missed adding the other const.  Thanks for the reality check.  That warning has now gone. :-+
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #88 on: August 13, 2013, 02:48:12 am »
OK, back to these captions.  As part of the boot-up, I assign values to the structures like so:

Code: [Select]
Buttons[0].top = BtnRow1Top;
Buttons[0].left = BtnCol1Left;
Buttons[0].height = BtnHeight;
Buttons[0].width = BtnWidth;
Buttons[0].BgColour = BtnColourLabel;
Buttons[0].CaptionColour = VGA_BLACK;
Buttons[0].caption = "";
Buttons[0].GlyphIndex = 0;

This is done for about 30 buttons.  I have the definitions above in other files:

Code: [Select]
#define BtnCol1Left 32
#define BtnWidth 82
#define BtnHSpace 13
#define BtnVSpace 15
...
#define VGA_BLACK 0x000000
#define VGA_WHITE 0xFFFFFF
#define VGA_RED 0xFF0000
...

I haven't got around to doing this more elegantly yet (with a function that takes parameters).

Is this a bad way of doing things in embedded C ?
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7805
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #89 on: August 13, 2013, 03:00:43 am »
You can do a full struct assignment just like they're regular data, which the compiler might optimize a bit better. (Though GCC with full optimizations might be smart enough not to need to do this. I think it looks a bit nicer, though.)

Sorry, I'm tired as all hell and can't be bothered to go look up all your type names, you'll have to adapt this:

Code: [Select]
Buttons[0] = (struct foo) {one, two, three, four};

Or you can initialize them all like this:

Code: [Select]
const struct foo foo_initializer = {one, two, three, four};
size_t i;
for (i = 0; i < ABOUT30; ++i)
    Buttons[i] = foo_initializer;

Or you could of course do:
Code: [Select]
#define FOO_INITIALIZER {one, two, three, four};
Buttons[ABOUT30] = {FOO_INITIALIZER, FOO_INITIALIZER, .........on and on....., FOO_INITIALIZER};

If you're using C99 features (GCC supports them; compile with c99 command instead of gcc, but your code will be much less portable to older compilers):
Code: [Select]
Buttons[ABOUT30] = {[0 ... ABOUT29] = {one, two, three, four}};

The last two will be the most efficient. The same feature can abbreviate struct literals:

Code: [Select]
Buttons[ABOUT30] = {[0 ... ABOUT29] = {.height = BtnHeight, .width=BtnWidth}};

and all other fields are left as zero.

As an aside..... C99... as in 1999... 14 years ago... why is this still "new" and "non-portable"?? |O Come on, compiler writers, get your shit together!
« Last Edit: August 13, 2013, 03:07:10 am by c4757p »
No longer active here - try the IRC channel if you just can't be without me :)
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 9668
  • Country: us
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #90 on: August 13, 2013, 03:07:21 am »
Code: [Select]
Buttons[0] = (struct foo) {one, two, three, four};

I'd be surprised if this worked? Assignment is not the same as initialization.
I'm not an EE--what am I doing here?
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7805
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #91 on: August 13, 2013, 03:09:31 am »
You're correct. I tried to optimize what he had one step at a time, and that one just replaced assigning the fields one at a time with assigning them in bulk. Obviously it's more efficient to set them all in an initializer, if they are supposed to be default values like he said.

It will, of course, work, even if it won't work well.

(I don't trust my coding ability when I'm half falling asleep, so I did test all these code snippets before posting them :-+)
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #92 on: August 13, 2013, 03:17:39 am »
Since most of the button properties don't change during program execution, is there a better way?

At present, the only ones that do need to change are the caption and possibly it's colour.
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7805
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #93 on: August 13, 2013, 03:25:27 am »
Hmm.... it's a bit tricky to optimize all that out. You could make some settings global instead of per-button:

Code: [Select]
#define BUTTONWIDTH 20

One way you could perhaps do it is with macros:

Code: [Select]
#define buttonWidth(btn) (20)
#define buttonCaption(btn) ((btn).caption)
...

and then omit the fields you don't need. Then if you need to make a field mutable, you can add it to the struct and change the macro to access that. Bit ugly, though.

You can define individual struct fields const. I'm not sure if that extends to __flash and PROGMEM, and if the compiler would bother yanking them from the struct, but it might be worth investigating. Then they're still per-button, but don't reside in the more dear RAM. (I can say with certainly that PIC's xc8 compiler, at least the free version, doesn't bother, and I would suspect most others do not as well.)

If you have plenty of memory I'd keep it, and just keep it at the top of a list of things to cut back if you run out.
« Last Edit: August 13, 2013, 03:27:56 am by c4757p »
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #94 on: August 13, 2013, 03:43:55 am »
Looks like I have plenty of room yet:

Code: [Select]
Program Memory Usage : 27892 bytes   20.0 % Full
Data Memory Usage : 1053 bytes   12.9 % Full
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7805
  • Country: us
  • adieu
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #95 on: August 13, 2013, 03:47:42 am »
Pfffft... you could practically install Windows on that!

"Premature optimization is the root of all evil" - Knuth

That space exists for the entire purpose of being used by your program. There's no reason to muck about trying to conserve it when you've hardly touched it in the first place!
No longer active here - try the IRC channel if you just can't be without me :)
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 9668
  • Country: us
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #96 on: August 13, 2013, 04:08:17 am »
Since most of the button properties don't change during program execution, is there a better way?

You could do things very simply using one large initializer, like this:

Code: [Select]
struct Button Buttons[32] =
    { BtnRow1Top, BtnCol1Left, BtnHeight, BtnWidth, BtnColourLabel, VGA_BLACK, "Caption 1",
      BtnRow1Top, BtnCol2Left, BtnHeight, BtnWidth, BtnColourLabel, VGA_BLACK, "Caption 2",
      BtnRow1Top, BtnCol3Left, BtnHeight, BtnWidth, BtnColourLabel, VGA_BLACK, "Caption 3",
...
      BtnRow4Top, BtnCol8Left, BtnHeight, BtnWidth, BtnColourLabel, VGA_BLACK, "Caption 32" };
I'm not an EE--what am I doing here?
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #97 on: August 13, 2013, 04:09:08 am »
That space exists for the entire purpose of being used by your program. There's no reason to muck about trying to conserve it when you've hardly touched it in the first place!

Hence the confusion about that weird error I got when trying to change the caption to an array.  I can find a few references to it on the 'net, but nothing that really explains why I'm getting it.   :(
 

Offline David_AVD

  • Super Contributor
  • ***
  • Posts: 2607
  • Country: au
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #98 on: August 13, 2013, 04:12:53 am »
Could one issue be that the initialisation is convoluted in some cases?

Code: [Select]
Buttons[28].left = BtnCol8Left;
The above would expand out to:

Code: [Select]
Buttons[28].left = 32 + (7 * (82 + 13));
Can I (should I) change those #define to constants with a calculated (at compile time) value?
 

Online IanB

  • Super Contributor
  • ***
  • Posts: 9668
  • Country: us
Re: C-code; array of struct ? (Atmel Studio 6)
« Reply #99 on: August 13, 2013, 04:23:54 am »
It will, of course, work, even if it won't work well.

(I don't trust my coding ability when I'm half falling asleep, so I did test all these code snippets before posting them :-+)

I don't find it to work. For instance:

Code: [Select]
#include <stdio.h>

struct Thing {
    int n;
    double v;
    char *label;
};

int main()
{
    struct Thing foo;

    foo = (struct Thing){2, 3.14, "hello"};
   
    return 0;
}

Code: [Select]
1>------ Build started: Project: CStructAssignment, Configuration: Debug Win32 ------
1>  main.c
1>c:\users\ian\documents\visual studio 2010\projects\cstructassignment\main.c(13): error C2059: syntax error : '{'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

On the other hand, this does work:

Code: [Select]
#include <stdio.h>

struct Thing {
    int n;
    double v;
    char *label;
};

int main()
{
    struct Thing foo = {2, 3.14, "hello"};
   
    return 0;
}
I'm not an EE--what am I doing here?
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf