|
|
View previous topic :: View next topic |
Author |
Message |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
How to send a structure over spi |
Posted: Wed Feb 04, 2015 3:19 am |
|
|
Hello. In my project I need to send a structure trough spi terminal. What is the general method to perform this task.
Let say I have the following structure:
Code: | struct date
{
int year[2];
int month;
int day;
int hour;
int minutes;
int seconds;
} time_set; |
I need to create a function that accepts the structure/structure pointer as argument and to transmit(over spi bus) the elements of the structure sequentially.
Thanks in advance! _________________ A person who never made a mistake never tried anything new. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Wed Feb 04, 2015 9:27 am |
|
|
The same way you send anything else that is not just a single byte....
Have a function, that accepts a pointer, and a size.
So something like:
Code: |
void send_block(int8 * data, int8 size)
{
int8 ctr, dummy;
//do your CS here
for (ctr=0;ctr<size;ctr++)
{
dummy=spi_xfer(SPI_STREAM_NAME,*(data++),8);
}
//release CS here
}
//Then call this with:
send_block(&time_set,sizeof(date));
//It'll also work for things like int32
send_block(&int_32_variable,sizeof(int32));
//etc...
|
Now, note the reading of 'dummy'. This ensures the spi_xfer does not exit, till the transfer completes. Hence CS can be raised after this, without going up before the transfer has taken place.
Obviously SPI_STREAM_NAME needs to match your #use SPI setup. |
|
|
RF_Developer
Joined: 07 Feb 2011 Posts: 839
|
|
Posted: Wed Feb 04, 2015 10:59 am |
|
|
Be aware that there is no standardisation on how structures are made up byte by byte. If you are sending PIC to PIC, both using the same version of CCS, and much the same PIC type, then there should not be much of a problem.
If there's any difference between the two ends, there may be alignment problems, byte order/endianness issues to name but two.
Transferring from, say, a PIC to a PC, or PIC to ARM, or 18 series PIC to 24 series PIC, or even, maybe, CCS V4.nnn to V5.mmm may not work as expected, or even at all.
Instead of relying on how the compiler happens to arrange the data in a structure, which is sometimes inconsistent and subject to unexpected change, its generally safer to format the data to make them more suitable and portable for sending. I do a lot of this, particularly with CAN - organising data in and out of CAN messages both PIC to PIC and PIC to PC. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|