Creating a dynamic Array for SPI in C with Xpresso

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Creating a dynamic Array for SPI in C with Xpresso

1,072 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Poenie on Wed Mar 09 01:02:39 MST 2011
I created a struct:

[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055][LEFT]struct[/LEFT]
[/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2]nodeType
{
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]info[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]struct[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] nodeType *[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]link[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]count[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
} ;

 
and directly below I declare the pointers:[/SIZE][/LEFT]
[SIZE=2]
nodeType [/SIZE][SIZE=2]*first, *newNode, *last, *current;[/SIZE]

[SIZE=2]The compiler kept complaining about "expected '=', ',' , ';' 'asm' or '__attribute__' before '*'"[/SIZE]

[SIZE=2]So I changed it to :[/SIZE]

nodeType [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]__attribute__[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ((__packed__))*first, *newNode, *last, *current;[/SIZE]

[SIZE=2]Now I get:[/SIZE]
[SIZE=2]"expected ',' or ';' before '*'"[/SIZE]

[SIZE=2]Not sure what [COLOR=#7f0055][B]__attribute__ [/B][/COLOR][SIZE=3][COLOR=#000000]does yet, but will do more reading. [/COLOR][/SIZE][/SIZE]

I tested the code in Visual Studio C++, where it worked well. It looks correct for C though..?

What I want to accomplish is to create a dynamic array to accept data packets from a master via SPI. Because I do not know the size untill actually receiving the 3rd byte, it should be dynamic, thus a linked list.
0 Kudos
Reply
4 Replies

1,057 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by Poenie on Fri Mar 11 02:30:43 MST 2011

Quote: gbm
Your solution doesn't make too much sense. Simply declare an array capable of storing the longest sequence of data you want to receive from SPI and store as many bytes as you need in it.



I was trying to save on memmory. Although in this case I do not think it is necessary, as there is not that much data. So I think this will be a pretty good solution. Create a buffer of Max size to save a full data packet, and from there use the commands and data as required.

Thanks for all the replies..
0 Kudos
Reply

1,057 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by ecw on Wed Mar 09 07:45:54 MST 2011
It's correct c++, struct names are implictly typedefed. They aren't in 'C' though.

replace,
nodeType *first, *newNode, *last, *current;
with
struct nodeType *first, *newNode, *last, *current;
0 Kudos
Reply

1,057 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by larryvc on Wed Mar 09 01:34:39 MST 2011
Try this:

typedef struct
{
     int info;
     struct nodeType *link;
     int count;
} nodeType;

nodeType *first, *newNode, *last, *current;


Using typedef to define a structure type allows us to not have to use struct before defining variables of that type.

For example like the previous post implied:

struct nodeType *first, *newNode, *last, *current;

Becomes:

nodeType *first, *newNode, *last, *current;

Or you could also use this:

struct nodeType
{
      int info;
      struct nodeType *link;
      int count;
} *first, *newNode, *last, *current;


The reply in the previous post is completely correct and you can use any method you prefer.:)
0 Kudos
Reply

1,057 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by gbm on Wed Mar 09 01:16:13 MST 2011
your declaratioin should start with:

struct nodeType *first, ...

But...

Your solution doesn't make too much sense. Simply declare an array capable of storing the longest sequence of data you want to receive from SPI and store as many bytes as you need in it, recording the number of bytes stored in some counter variable.
0 Kudos
Reply