Shop OBEX P1 Docs P2 Docs Learn Events
Using a counter to serialize reading bits? — Parallax Forums

Using a counter to serialize reading bits?

lonesocklonesock Posts: 917
edited 2009-06-02 03:53 in Propeller 1
Hi, All.

I need some feedback on this idea:

OK, say I want to have a really fast SPI interface: I know I can set up a counter to act as my clock, then to read in a byte, do 8 of the following:
test mask, ina wc
rcl data, #1




Now, I'm greedy, so I want to go twice as fast. Here's the plan:
1) set up CTRB to act as my clock, output on the SCK pin (don't start it yet)
2) set up CTRA in POS detector mode, where pin A is my SDI pin, and set FRQA to 1<<7, and PHSA to 0 (don't start it yet)
then start CTRA, then CTRB, then do the following 8 times:
shr FRQA, #1



Now shut down both counters, and my desired value should be in the low 8 bits of PHSA.

Would this work? I'm not sure about the order I should start Counters A and B, or the phase of the clock line relative to the actual read (the POS detector mode specifies that it reads the APIN delayed by 1 clock). Any feedback from the local gurus?

thanks,
Jonathan

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
lonesock
Piranha are people too.

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-06-01 17:27
    Jonathan,

    Hee hee! That's pretty clever! One problem is that you'll get four counts for each "1" bit (four clocks per instruction). So you'll have to shift the value from PHSB right by two after you read it. (You can't shift PHSB itself.) If you can't get the two counters to synchronize just right, you can always externally AND the SCK and SDA lines and count edges with CTRA instead of levels.

    -Phil
  • lonesocklonesock Posts: 917
    edited 2009-06-01 17:32
    Phil Pilgrim (PhiPi) said...
    Jonathan,

    Hee hee! That's pretty clever! One problem is that you'll get four counts for each "1" bit (four clocks per instruction). So you'll have to shift the value from PHSB right by two after you read it. (You can't shift PHSB itself.) If you can't get the two counters to synchronize just right, you can always externally AND the SCK and SDA lines and count edges with CTRA instead of levels.

    -Phil
    Thanks, Phil, for the feedback. Can I just set the PLLDIV to %101 to get a clockrate/4? Or am I reading that wrong?

    edit: Great point about just correcting it via dividing by four after the fact!
    edit2: I see, the PLLDIV only works in a PLL counter mode...oops

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    lonesock
    Piranha are people too.

    Post Edited (lonesock) : 6/1/2009 5:40:51 PM GMT
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-06-01 17:42
    lonesock said...
    Can I just set the PLLDIV to %101 to get a clockrate/4?
    No, that won't help. Your shr FRQA, #1 occurs every four processor clocks. So you will want the output from ctrb to be cklfreq/4. ctra will still count up at the clkfreq rate whenever its ina bit is high, which means four counts per instruction.

    My feeling, on second thought, is that this would be unreliable, since it depends on a precise relationship between the edges of SDA and when ina is sampled by ctra. I'd be much more inclined to use the external logic and count edges.

    -Phil
  • lonesocklonesock Posts: 917
    edited 2009-06-01 17:53
    Phil Pilgrim (PhiPi) said...
    My feeling, on second thought, is that this would be unreliable, since it depends on a precise relationship between the edges of SDA and when ina is sampled by ctra. I'd be much more inclined to use the external logic and count edges.

    -Phil
    Could I use the 'LOGIC A & !B' mode, or similar, setting BPIN to be my SCLK line, to ignore any counting when the SCLK line is high. I think this would change 2 things:
    1) it means I only have to divide by 2 at the end
    2) I'm not reading the SDI pin while the value is changing (depending on the SPI mode, of course)

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    lonesock
    Piranha are people too.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-06-01 18:10
    If you're sure that the transitions on SDI completely straddle the SCK pulse, that should work fine.

    -Phil
  • kuronekokuroneko Posts: 3,623
    edited 2009-06-01 23:55
    See here, I used counters for a ~20Mbit serial link, meaning in principle it should cover your case.
  • lonesocklonesock Posts: 917
    edited 2009-06-02 00:02
    kuroneko said...
    See here, I used counters for a ~20Mbit serial link, meaning in principle it should cover your case.
    Hey, you beat me to it! Great job!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    lonesock
    Piranha are people too.
  • PhilldapillPhilldapill Posts: 1,283
    edited 2009-06-02 03:53
    This is great! I was hoping to do the same thing with an ADC of mine. Doing it this way should increase my sample rate by a factor of 4! I'm giddy.
Sign In or Register to comment.