About SPI_Spin.spin
caskaz
Posts: 957
Hi everyone.
I found out SPI_Spin.spin inside Propeller Tool's folder.
I read this and I have question.
There is code below:
At first I think that 4296 is overhead's time to call PostClock(_Cpin)[or PreClock].
But it seems different.
How do 4296&381 calculate?
I found out SPI_Spin.spin inside Propeller Tool's folder.
I read this and I have question.
There is code below:
ClockDelay := ((clkfreq/100000 * _ClockDelay) - 4296) #> 381
At first I think that 4296 is overhead's time to call PostClock(_Cpin)[or PreClock].
But it seems different.
How do 4296&381 calculate?
Comments
I'll have to look at this closer when I get a moment. I think the idea is that 4296 introduces an initial overhead offset as you suggested, where the 381 limits the minimum time allowed for the waitcnt instruction that would prevent a counter roll-over. That said, the below 'test' code should produce a clock pulse with a period of 20us (<- two clock Delays), but instead it's about 119us. Way off!! ... this might however be an artifact of Spin with this particular example. I originally wrote the code in PAsm and it might work fine there. The Spin version between instructions doesn't have the resolution of PAsm so it might actually take 119us with Spin.
But clkfreq/100000 (that's one hundred thousand) for 80MHz is 800 clock cycles (10 usec) times 10, gives 8000 clock cycles, less 4296, so Delay is set to 3704.
Since there are two Delays per period, you are delaying about 7400 clock cycles, which is more than 92 usec.
It looks like you want the expression clkfreq/1000000 (one milloin) to get clock cycles per usec, so that Delay will be about 10 usec, which is 800 clocks, but subtracting 4296 gives a negative delay (or a very large positive delay). Probably not what you want, either.
The timing is wrong for the SPI_Spin object. The SPI_Asm object has the correct timing based on this simple formula noted within the SPI_Asm object.
To correct the timing for the SPI_Spin code, the line that reads ...
... should read ...
... here is a better detailed descriptiion ...
I checked waitcnt by Demo Board.
System counter roll over on waitcnt(cnt + 556)
557 is OK.
This vary on each boards?
I checked below;
count is 4240.
4240/2=2120 2120*0.0125usec=26.5usec
80*27(usec) - 557 = 1603
ClockDelay := ((clkfreq / 1000000 * _ClockDelay) - 1603) #> 557
I measured Low-time by using osiloscope.
time wass aproximately 20usec.
I think spin cannot be 1u-resolusion.
My idea is wrong?
I think we are speaking apples to apples here ... The 557 will vary depending on the waitcnt loop that you are using but will be consistent from propeller to propeller.
See my post just prior to yours. For the SPI_Spin object the magic number is 541 ... and I too was getting about 20us (18.2us to be more precise.)
I don't understand well English. What does apples mean?
Sorry, I perfectly cannot understand your post.
When I checked code, I found strange.
Code is below;
Low-pulse is approximately 18usec.
I replace PUB PostClock(_Cpin) below; It's same above code. But No pulse.
When I changed waitcnt(cnt+557), there are pulses.
I have no idea.
Why?
"I don't understand well English. What does apples mean?" - It just means we are talking about the same thing.