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

"load" option for hardware SPI?

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



Joined: 02 Feb 2010
Posts: 73

View user's profile Send private message

"load" option for hardware SPI?
PostPosted: Fri Sep 21, 2012 5:03 am     Reply with quote

About to start my first experiment with SPI, and need some advice. Actually, the target device uses a not-quite-SPI interface; there is no addressing etc., it simply uses bytes of data received serially to determine which outputs to turn on/off, with only those devices on the line. After receiving a byte (or two bytes, for 16pin output models), a "latch enable" pin needs to be toggled to "lock in the data". This is where I need help.

The devices are the Texas Instruments TLC5917 (8 outputs, one byte) or the TLC5926 (16 outputs, two bytes).

I would like to use the remappable hardware SPI of the PIC24FJ64. Is there a way to setup a "load" pin to associate with hardware SPI, which auto-toggles after a byte is sent? #USE SPI has this, but isn't setup_spi() the newer better method for hardware? Or am I getting things mixed up?

I have two TLC5926's only on this SPI line, and no other SPI devices being used. Overflow data from the first 5926 becomes input for the second one.

PIC24FJ64, 20MHz OSC, 10MHz clock. CCS 4.120
From how I read it, TLC5917/26 works optimally with a clock pulse of 20ns, and the latch pulse should also be 20ns, which gives a clock divider of... ?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Sep 21, 2012 5:58 am     Reply with quote

Several remarks:
1) The TLC5917 doesn't use SPI or a derivation there of. It is just a serial-in parallel-out shift register that needs to be clocked. A lot like the good old TTL chip 74595 and derivatives (74LS595, 74HC595, etc). Check the CCS drivers directory on your harddisk for an implementation example (74595.c)

2) Yes, it should be possible to use the SPI hardware to drive these chips but there is very little to gain from that. Transfers will be slightly faster compared to software driver but you'll waste a lot of time in figuring out the right configuration settings.

3) #use SPI is the new CCS driver for SPI and setup_spi() is older. #use SPI is more versatile but less well tested (i.e. more bugs) and more difficult to configure because of undocumented features and strange default settings.

4) the 20ns timings are minimum values. No maximum is specified so if you want to take hours before toggling the next bit that is fine to the hardware. BTW, with a clock divisor of 1, a 20ns clock equals 50MHz. Your PIC is much slower, so even at the fastest clock settings you will always meet this minimum timing.
RoGuE_StreaK



Joined: 02 Feb 2010
Posts: 73

View user's profile Send private message

PostPosted: Fri Sep 21, 2012 4:30 pm     Reply with quote

Thanks ckielstra, I'll check out the samples when I get a chance (on a different PC at the moment).
My desire for hardware use may be misguided; I'll be using a pseudo state machine for other operations, driven by an interrupt of 16kHz, and thought that, like hardware PWM, hardware SPI would take less overhead in code, ie. just say "do this" and the hardware goes off and takes care of the guts of what it has to do, rather than a software operation that cuts into other operation times?

Does setup_spi() have a feature to auto-toggle a pin after transfer, like #use SPI does? I couldn't find anything in the docs or forum, but didn't know if I was using the right terms. That's probably my main problem, trying to figure out how to get the timing right for the "Latch Enable" toggle, without using any delays etc.

RE: clock, so any old divisor will suffice? Keep it at 1 for fastest possible, but well within specs for the 5917/26?
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Sep 22, 2012 2:40 am     Reply with quote

Hardware SPI can be appropriate for a simple serial IO port extension if you are intending it to be quite fast. In this case, you should look for those SPI features that are actually implemented in PIC hardware. I presume that the "load=pin" option of #use SPI is a pure software feature and you don't get any speed adavantage compared to controlling the pin in your software directly.

PIC24 offers a hardware sync output in framed SPI mode, that isn't specifically supported by CCS C. If you come to the conclusion that framed SPI mode meets your requirements, you'll need to set some SPI special function registers in your code directly. This is always an option if built-in functions don't provide what you want.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sat Sep 22, 2012 7:27 am     Reply with quote

RoGuE_StreaK wrote:
My desire for hardware use may be misguided; I'll be using a pseudo state machine for other operations, driven by an interrupt of 16kHz, and thought that, like hardware PWM, hardware SPI would take less overhead in code, ie. just say "do this" and the hardware goes off and takes care of the guts of what it has to do, rather than a software operation that cuts into other operation times?
True, while the hardware is busy your interrupt can become active and do it's things. True multitasking.
But what you are doing now is looking for the most optimal solution. In my favourite approach I do it like: Get it working - Get it right - Get it optimized.

In general, most optimization is achieved by touching only about 5% of the code base, i.e. the time processing bottlenecks are located in only a few of the program functions.

I'm sure it can be done with hardware SPI, but you'll have to wait for all data to be transmitted before you can trigger the output latch. So while the hardware is busy, your processor is idling, whether you do the SPI in hardware or software. Only advantage is that hardware will be slightly faster and a possible interrupt can be handled simultaneously. But I doubt the extra work is worth the effort. My suggestion would be to get it working using the software driver for the 74595 but choose your hardware connections so that you can upgrade to SPI if need arises.

Quote:
RE: clock, so any old divisor will suffice? Keep it at 1 for fastest possible, but well within specs for the 5917/26?
Yes.

Quote:
a pseudo state machine for other operations, driven by an interrupt of 16kHz
I have no experience with PIC24, but in every other system I know this would be a very high interrupt rate for a state machine.
In a PIC24FJ at 20MHz you get 10 MIPS. With 16kHz interrupt rate that is only 625 instructions before the next interrupt occurs. Doable, but the interrupt overhead will become noticeable. Much better to reduce the interrupt rate to 1kHz.
RoGuE_StreaK



Joined: 02 Feb 2010
Posts: 73

View user's profile Send private message

PostPosted: Sat Sep 22, 2012 11:15 pm     Reply with quote

ckielstra wrote:
In my favourite approach I do it like: Get it working - Get it right - Get it optimized.
True; I'm trying to get basic operations working separately before trying to tie them together (eg. debouncing, interrupts etc) but will try starting with a soft implementation, just to see if the hardware interfaces actually work.

ckielstra wrote:
but you'll have to wait for all data to be transmitted before you can trigger the output latch. So while the hardware is busy, your processor is idling
I'm not sure I quite follow you there; I was under the (possibly misguided) impression that if you used the hard SPI, like hard PWM, you could just call the function to write a byte, then continue on with the next separate opertions? Output-latch-wise, that's why I was asking if there was a hardware implementation, so I didn't have to monitor whether the byte was sent yet or not, and then toggle a pin high-low for a long enough period. Then again, I don't need to change the settings on these TLC chips all that often, say 60 times a second, so could maybe just send the bytes via hard SPI, then use a soft toggle every hundred or so interrupts, to lock it in.

ckielstra wrote:
I have no experience with PIC24, but in every other system I know this would be a very high interrupt rate for a state machine.
In a PIC24FJ at 20MHz you get 10 MIPS. With 16kHz interrupt rate that is only 625 instructions before the next interrupt occurs. Doable, but the interrupt overhead will become noticeable. Much better to reduce the interrupt rate to 1kHz.
This is for PCM sound playback, which I've proven to work on an 18F2620 @20MHz, 5MIPS. Essentially on interrupt it would set the PWM output to the current sample value, and set a flag to start doing the calcs for the next sample. The main loop is free to continue as fast as it can go, and if it sees an unattended flag goes into a subroutine to check what the next sample should be, be it a simple incrementation of a table lookup, a switch to a new table, or a blend of data from several tables.
On a much less time-sensitive basis is the likes of button debouncing, LED indicator updates, sensor reading, etc. But they can be staggered so if necessary they each can have a few Main cycles for processing.
So as you can see, if I can get hardware to take care of the communications etc., it theoretically should free up some processing time for other stuff.

Looks like my LDO reg isn't working for some reason, so will have to do testing just with the limited current of a USB connection via PICKIT2; not the ideal way of testing whether the TLC chips can be switched to sink about 4Amps!
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Sep 23, 2012 2:35 am     Reply with quote

Quote:
I'm not sure I quite follow you there; I was under the (possibly misguided) impression that if you used the hard SPI, like hard PWM, you could just call the function to write a byte, then continue on with the next separate opertions?

PIC24 can do it, as said.
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