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

Regarding using TWO software SPI

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



Joined: 10 Aug 2016
Posts: 9

View user's profile Send private message Send e-mail

Regarding using TWO software SPI
PostPosted: Tue Aug 30, 2016 6:48 am     Reply with quote

Hi, I am using PIC24HJ256GP210A and i want to implement TWO SOFTWARE SPI and TWO HARDWARE SPI.
For HARDWARE SPI in order to write we have SPI_WRITE()-For SPI1 and SPI_WRITE2()- For SPI2 builtin fuction its working fine.
but using software SPI i used #USE SPI() and mention all required pins for DI, DO, CLK .
How to use SPI_XFER() funtion for TWO Sofware SPI ? What is the argument i need to provide in spi_xfer() funtion to differentiate between TWO software SPI ?? Rolling Eyes
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Tue Aug 30, 2016 8:13 am     Reply with quote

Honestly use spi_xfer for all of the SPI's.

spi_write and spi_read, are the 'old' CCS way of handling SPI's.

Basically with #use spi, you set up a hardware SPI with:
Code:

#USE SPI(MASTER, SPI1, baud=100000, bits=8, STREAM=SPI_HARD1, MODE=0)


or software with:
Code:

#USE SPI(MASTER, DI=PIN_xx, DO=PIN_yy, CLK=PIN_zz, baud=100000, bits=8, STREAM=SPI_SOFT, MODE=0)


Now the nice thing is that you can just specify 'mode' numbers, and just saying 'SPI1' means it will use hardware SPI1.
This is the way you have to work on later chips with #PIN_SELECT, where the compiler will then still correctly handle the TRIS. Using spi_read and spi_write with these, you often have to setup the TRIS yourself.

Then with a SPI ports setup (soft or hard), you just talk to it with spi_xfer. So:
Code:

value_received = spi_xfer(SPI_HARD1, val_to_send);

value_soft = spi_xfer(SPI_SOFT, val_to_send);


sends 'val_to_send' out the first the hardware, and then the software ports shown.

You can choose stream names to help remind you what is connected etc.. So (for instance), I have one called 'SD', for the connection to an SD card, and one called 'ACCEL' for a connection to an accelerometer.

You can have a dozen ports all configured if you want. Just need different names.

Other 'neat' things about spi_xfer, are features like allowing you to specify (say) a word length of 32bits in the setup, and then sending (and receiving) an entire 32bit word as one transaction (can be very useful for chips that are based around longer transaction sizes).

You can (for instance) setup the transaction size as 32bits, and then do 'short' transactions for setup, by limiting the size in the transaction:

Code:

value_received = spi_xfer(SPI_SOFT, val_to_send_to_soft, 8);


Sends just 8bits out the software port.

A lot of things like Texas chips use larger transactions, making this a much nicer way to work.
GAGAN



Joined: 10 Aug 2016
Posts: 9

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 30, 2016 10:14 pm     Reply with quote

Thank you for your reply
and can it be possible to send 32 bit data in one transaction like spi_xfer(0xFFFF,software_SPI1,32) ?
So what argument should i follow to set #use spi, Setup_SPI() and spi_xfer() function?? to send 32 bit data in one transaction?
GAGAN



Joined: 10 Aug 2016
Posts: 9

View user's profile Send private message Send e-mail

PostPosted: Tue Aug 30, 2016 10:26 pm     Reply with quote

and What is the significance of SETUP_SPI(options) ? In which case i should use ?? Do i have to make use of #USE SPI () or SETUP_SPI() in order to setup my SPI??
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Wed Aug 31, 2016 1:00 am     Reply with quote

If you read the manual, you will find that the stuff is 'linked'.

So if you look at setup_spi, it cross links to spi_read and spi_write.
If you look at #use spi, it links to spi_xfer. They do not link to the other way of working.

You do not mix and match. They are two separate ways of handling spi. So #use, with spi_xfer, _or_, setup, with read & write.
If you use '#USE', you do not use setup_spi, spi_read or spi_write.

Now to use 32bit transfers, you have to set the maximum transfer length in the #use at 32bits (it defaults to this if you don't have a bits= setting). Then you need to specify the length in your transfers. So to send a single byte:

spi_xfer(STREAM_NAME, value_to_send, 8);

sends/receives an 8bit value, while

spi_xfer(STREAM_NAME, value_to_send, 16);

sends/receives 16bits, and

spi_xfer(STREAM_NAME, value_to_send, 32);

sends/receives 32bits.

Setup, read and write, were the 'old' way of handling spi. Only supported hardware, and relatively limited functionality. The #use capability was added five or more years ago (mid V4 compilers), with the stream names, and the ability to do software or hardware handling (like #USE RS232). Problem is that a lot of the examples are 'old' and show the older system, and people will try to mix the two methods (which can give problems...).
GAGAN



Joined: 10 Aug 2016
Posts: 9

View user's profile Send private message Send e-mail

PostPosted: Wed Aug 31, 2016 5:01 am     Reply with quote

thanks for your Suggestion,,, Smile
GAGAN



Joined: 10 Aug 2016
Posts: 9

View user's profile Send private message Send e-mail

PostPosted: Wed Aug 31, 2016 7:08 am     Reply with quote

Hi,
I am trying to transfer 32 data in one transaction so i mentioned #use spi(MASTER, SPI1, BAUD=10000, BITS=32, Stream=HW_SPI1, MODE=0) and using this function to transfer spi_xfer(HW_SPI1, 0X00120357, 32);
but when i check the output in scope after every 8 bit transfer its generating some delay.
Please let me know how to adjust this delay after between every 8 bit transfer?
Also please let me know what parameter i should take care in order to get all the 32 bit data without generating delay in between after each 8 bit is transferred.
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Wed Aug 31, 2016 7:37 am     Reply with quote

There is going to be some, but it should be tiny.

Remember the hardware only supports 8bit transmissions, so the code has to wait for the status flag to say 'register is empty', and only then load the next byte.

However should only be a handful of instruction times. Doesn't matter, since as SPI is synchronous, the clock will stop, and the slave will wait for the next bit.
GAGAN



Joined: 10 Aug 2016
Posts: 9

View user's profile Send private message Send e-mail

PostPosted: Wed Aug 31, 2016 10:42 am     Reply with quote

Thank you for your feedback,,,
1-How do I set the baud rate in #use spi ( ) so that I can run my spi @ a clock rate of <50 MHz ?
2-by using software spi, when I check my output the data is delayed by almost one spi clock pulse, so in this case my last data is outputted after the end of last clock, as I am trying to transfer 32 bit data in one transaction.
Could you please provide the information about the factor affecting this issue?
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Aug 31, 2016 12:37 pm     Reply with quote

Helpful hint:
While your project is open, press F11. The CCS manual will 'magically' appear ! You can minimize it, so it's always easy to access.

Look for the pre processor directives, locate use spi, click on it, on the right side of the screen 'how to use' it appears.....

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Wed Aug 31, 2016 2:18 pm     Reply with quote

and, as already said, having pauses in the data/clock, doesn't matter at all. That is the whole point of synchronous transactions.
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