CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

How to send a structure over spi

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

How to send a structure over spi
PostPosted: Wed Feb 04, 2015 3:19 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 04, 2015 9:27 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Feb 04, 2015 10:59 am     Reply with quote

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.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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