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 support@ccsinfo.com

spi_xfer with 7 bytes

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



Joined: 22 May 2008
Posts: 63
Location: Madison, TN

View user's profile Send private message Visit poster's website

spi_xfer with 7 bytes
PostPosted: Wed Mar 24, 2010 10:15 am     Reply with quote

I need to send 7 bytes via spi_xfer.

I can do this (pseudo code)
define array of 7 bytes
define stream without enable and 8 bits max transfer
define enable
set enable
for next 7 bytes from array
unset enable

That works but is not elegant.

Question
Can I define the stream with enable and 56 bits max transfer and then
spi_xfer(stream, array);
e.g. is an array considered data within the context of an spi_xfer?
MikeW



Joined: 15 Sep 2003
Posts: 184
Location: Warrington UK

View user's profile Send private message

PostPosted: Wed Mar 24, 2010 10:34 am     Reply with quote

I am pretty sure you can do it with the software SPI, not sure about the hardware SPI though..

I use
Code:

#use spi(Master, Mode=0, DI=Touch_Screen_SPI_MISO_IN, DO=Touch_Screen_SPI_MOSI_OUT, CLK=Touch_Screen_SPI_SCLK, ENABLE=Touch_Screen_SPI_CS, ENABLE_ACTIVE=0,BITS=24, MSB_FIRST)

to transfer 24 bits at a time

then
Code:

void  Check_Touchscreen()
{
int32 spi_buf[3];


   spi_buf[0] = spi_xfer(TC_CR_Y);
   spi_buf[1] = spi_xfer(TC_CR_X);
 

etc
etc
Ttelmah



Joined: 11 Mar 2010
Posts: 19389

View user's profile Send private message

PostPosted: Wed Mar 24, 2010 10:46 am     Reply with quote

However there is nothing 'non elegant' about the original solution. Just generate your own wrapper. So something like:
Code:

send_spi(int8 * data, int8 num_bytes) {
   int8 ctr=0;
   output_low(CS); //change to suit your enable polarity and pin
   do {
      spi_read(data[ctr++]);
   } while (ctr<num_bytes);
   output_high(CS); //again change
}

Using spi_read, ensures that the byte has _finished_ clocking, before the function returns. On some of the later chips, the write returns as soon as the byte is transferred to the output buffer.

This is after all, exactly what any internal 'CCS' function is going to have to do. You would obviously need to add stream identifiers if these are being used, and I have generated the function, so it can send any number of bytes (up to 255).

Best Wishes
SuperDave



Joined: 22 May 2008
Posts: 63
Location: Madison, TN

View user's profile Send private message Visit poster's website

PostPosted: Thu Mar 25, 2010 5:52 am     Reply with quote

Code:
int32 spi_buf[3];
   spi_buf[0] = spi_xfer(TC_CR_Y);
   spi_buf[1] = spi_xfer(TC_CR_X);

This is what I do elsewhere when I need 24 bits using an int32. But it has the problem that the enable line drops between spi_xfers, meaning that since there is no int64 it won't work for 56 bits. (Persons reading this at some future time should note that since the transfer is MSB first, the least significant byte is undefined, never sent and never read.)

Code:
do {
      spi_read(data[ctr++]);
   } while (ctr<num_bytes);

Indeed works but is 'inelegant' in the sense that spi is being done differently it two areas of the same code. Not fatal, just thinking of the guy in the future that has to maintain this.

It would be nicer if spi_xfer(stream,DATA) could be interpreted as being valid to mean spi_xfer(stream,ARRAY). That's really the central question. I was hoping there was someone out there who had made that work successfully. Looks like I'll have to experiment.

Thanks for the help.
Dave
Ttelmah



Joined: 11 Mar 2010
Posts: 19389

View user's profile Send private message

PostPosted: Thu Mar 25, 2010 6:22 am     Reply with quote

Again wrapper it yourself.
Remember that the compiler now supports function overloading, so just make the wrapper function, apply if spi_xfer, is called with a pointer.
Use the sizeof operator to get the size of the object, and "Robert's your mother's brother".
Serioulsy, the more that is done 'under the hood' by CCS, the more likely things are to have problems with different versions. Personally, I wish that CCS just made the compiler 'invisible', and all the standard functions were supplied in libraries, rather than hidden from the user.
As for using different ways of writing, this is what documentation is for. A simple comment on the line like:
//Writing, using the read function, to ensure data _has_ been clocked out.
tells a future devloper what is being done.

Best Wishes
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