Are you comfortable with Spin?
info
Posts: 31
What do you do when some things are hard to get done in Spin? Do you beat the dead horse until you get around limitation, or you resort to another programming language?
I'm trying to find a way to repeat block of code - retry on failure. In assembly language I would use conditional jump to label. Spin doesn't have labels or jumps.
I want to check for device busy or device present in the first place (hardware dependent) and restart i2c code if I don't get ACK from i2c device, but I don't want to get stuck in never-ending loop.
-set loop counter to 2
Start here (label)
-send start bit
-send device code byte
-IF counter = 0 then log error code, give up and return to main code
-IF ACK not received, wait a little, decrement counter and start all over again at label
-reset counter
-send register address byte
-IF counter = 0 then log error code, give up and return to main code
-IF ACK not received, wait a little, decrement counter and start all over again at label
-reset counter
-send data byte
-IF counter = 0 then log error code, give up and return to main code
-IF ACK not received, wait a little, decrement counter and start all over again at label
....continue here to do another i2c command or more data bytes
It is easy to do it in assembly, basic, etc.
Is there a way to do that in Spin?
There probably is, I just can't find it.
I'm trying to find a way to repeat block of code - retry on failure. In assembly language I would use conditional jump to label. Spin doesn't have labels or jumps.
I want to check for device busy or device present in the first place (hardware dependent) and restart i2c code if I don't get ACK from i2c device, but I don't want to get stuck in never-ending loop.
-set loop counter to 2
Start here (label)
-send start bit
-send device code byte
-IF counter = 0 then log error code, give up and return to main code
-IF ACK not received, wait a little, decrement counter and start all over again at label
-reset counter
-send register address byte
-IF counter = 0 then log error code, give up and return to main code
-IF ACK not received, wait a little, decrement counter and start all over again at label
-reset counter
-send data byte
-IF counter = 0 then log error code, give up and return to main code
-IF ACK not received, wait a little, decrement counter and start all over again at label
....continue here to do another i2c command or more data bytes
It is easy to do it in assembly, basic, etc.
Is there a way to do that in Spin?
There probably is, I just can't find it.
Comments
You can end a loop with "quit", or you can use "while" to continue a loop while a condition persists or you can use "until" to end the loop when a condition occurs. You can also skip to the next loop in the repeat with "next".
You can also end the method with "return" and you can end a method and return several methods up with an "abort" and an abort trap "\".
I don't see how this is any harder in Spin than in C or many other languages.
And yes, I am very comfortable with Spin. Thanks for asking.
This code above will check for the device to respond 3 times, but if it gets a reply it will get out of the loop and go to whatever next in the code.
I was beating the dead horse. The "quit" did the job in one loop without calls from main program. Maybe my code is little crude, but it tested ok.
x := 1 'simulate ACK=0 NAK<>0 to test the code
repeat i from 2 to 0 'loop counter
uarts.str(0,string(" Loop x="))
uarts.dec(0,x)
uarts.str(0,string(" i="))
uarts.dec(0,i)
'i2c.Start(SCLk) 'send start bit
'x := i2c.Write(SCLk, %0011_1110) 'device code, device address
if x == 0 'ACK ????
quit 'if ACK=0 skip the rest of code and do next i2c byte
waitcnt(clkfreq/2 + cnt) 'wait if device busy
uarts.str(0,string(" Wait x="))
uarts.dec(0,x)
uarts.str(0,string(" i="))
uarts.dec(0,i)
uarts.tx(0,13)
if i == 0
errcode01 := %1
quit
uarts.str(0,string(" ErrorCode01="))
uarts.dec(0,errcode01)
uarts.tx(0,13)
'do next i2c byte
Now I need to learn how to attach code here. The snippet above looks terrible.
I struggle even with things everybody takes for granted. I'm already used to that. The leprechon says that even bad luck is a luck, so I'm the luckiest person.
[noparse] [/noparse]
I will try next time.
-Phil
My limitation is that I am a real amateur in regard to programming languages. I know Basic (including PBasic), Spin, and that's it folks.
I am the one that tries all variations of code before checking out other's coding examples to include into mine, and even then I usually modify it quite a bit.
You can snoop on code formatting the same way.
You can also edit earlier posts to fix the code formatting.
A: Yes. I don't know everything. The Stack ambiguity bugs me, but I've never done SO much that that was a critical consideration. I just don't know why it's not part of the IDE to work that out.
Clearly such an analyser could see how many parameters a method has and how many local variables. From that it knows the stack required to call the thing. Then the analyser would have to recursively inspect all the methods that are called from the top method. One of those "branches" of analysis will get you the maximum stack usage.
Except it falls down some times:
1) Perhaps it's impossible to tell from the source code which paths are taken at run time and hence which calls are made.
2) When you have recursive methods.
Still perhaps a best effort analysis would be better than none.
Or you can use the code in post #13 and watch the stack get used as the program runs (assuming the buffer dump doesn't get in the way of displaying the other data in your program). I personally thought it was pretty cool to watch the stack space change as the various options in a method were exercised.
I was trying to think of an example of a method which couldn't have it's stack size calculated at compile time and the only example was some sort of recursive method (as Heater suggested). All the other examples I could think of seemed like they should have predictable stack sizes. If you look through Kye's code, he lists the stack size of each method. Unless Kye has some sort of psychic ability which allows him to do this, I'd think there should be a way for the rest of us, or even the compiler, to accurately calculate the stack size without the need to actually run the code.
There's some discussion of what types of things add to the stack in the above linked to thread.
I use forth. Writing drivers is a breeze, and writing apps is fun. Stuff either works, or it don't. rarely are there things that LOOK like they work, only to later have bugs, hidden by the compiler etc.
But I'm very lazy, and only do this for fun.
Spin and spinning is a lot of fun!
(Jump ahead to thirty seconds, to see robot moving Spinning.)
I have lots of fun programming in Spin.
I'm trying to remember if I've ever had fun programming the Propeller in Forth. . . Nope, I haven't. (Hey, he started it.)
Not to disparage spin, or your opinions, or your great works, but one size might not fit all.
The original poster DID ask, and clearly is having frustrations and is looking for alternatives.
We might acknowledge that while some of us love spin, others love C, still others no longer love C for what ever reason, and other excellent alternatives exist, in case they are needed.
If one does not not find benefit from a given tool set or envirnoment, it may be suitable to investigate other options. I happen to do well with one tool, this does not denigrate the others.