PDA

View Full Version : Spin - TRUE, FALSE and BITWISE operators



lanternfish
11-10-2011, 07:08 AM
My preference is to use PASM but learn SPIN I must. So I need to better understanding of BITWISE operators as used by SPIN so that I can integrate a number of objects for my current project. I have been (re)reading the manual but still can't quite get my head around the following:

From the Propeller GPS Module firmware (VPN1513GPSReceiverFirmware_v1.0.spin):



OBJ
'FS : "FloatString"
NMEA : "GPS_Str_NMEA_lite_lawkmH"
Trans : "simple_serial"


PUB Init | OK1, OK2, OK3

dira[raw]:=0
dira[_RX_FM_GPS]:=0

OK1:=Trans.init(sio,sio,9600) ' returns if COG started.
OK2:=NMEA.StartCOGs(_RX_FM_GPS,_GPS_SER_BAUD) ' returns if COG started.
(more code follows)


This line


OK1:=Trans.init(sio,sio,9600)


starts the Simple_Serial object, where rxPin and txPin = 17 and baud = 9600



PUB init(rxPin, txPin, baud): Okay

rxOkay := rxPin > -1 ' receiving? rxOkay = -1 if rxPin > -1
txOkay := txPin > -1 ' transmitting? txOkay = -1 if txPin > -1

sin := rxPin & $1F ' set rx pin
sout := txPin & $1F ' set tx pin

inverted := baud < 0 ' set inverted flag
bitTime := clkfreq / ||baud ' calculate serial bit time

return rxOkay | txOkay


which returns a value which is rxOkay OR'd with txOkay.

Q1. What should the return value be? and why.

This line:



OK2:=NMEA.StartCOGs(_RX_FM_GPS,_GPS_SER_BAUD)


starts GPS_Str_NMEA_Lite_lawKmH object, where _RX_FM_GPS = 18 and _GPS_SER_BAUD = 9600.



cog1 := GPS_UART.INIT(rX_FR_GPS,rX_FR_GPS,nmea_Baud)
'Start a SPIN interpreter in separate COG to execute the tokens of the
'"Concurent_NMEA_Receiver" SPIN procedure parallely with the other COGs
cog2 := COGNEW(Concurent_NMEA_Receiver, @nmea_Rx_Stack) + 1

oKay := cog2


This starts another Simple_Serial cog and the next line starts the NMEA receiver/parser in yet another cog.

Q2. What will the value of oKay be?



OK3:=cognew(Detect_led,@stack)

waitcnt(clkfreq+cnt) ' Wait

IF NOT (OK1 AND OK2) 'Some error occurred
NMEA.StopCOGs
OUTA[LED]:=0 'Turn on
REPEAT 'Until Power Off or Reset



What values will the line


IF NOT (OK1 AND OK2)

produce.


Q4. How many cogs are running? I count 4.

Thanks in advance for your answers.

MagIO2
11-10-2011, 08:26 AM
Q1:
It's a simple check that the function has been called with at least one pin set, either rxPin or txPin or both. So, it will return -1 (which is equal to true) in case if one or more pins have been specified and it will return 0 (=false) if both rxPin and txPin are set to -1

MagIO2
11-10-2011, 09:38 AM
sorry ... got interrupted by work ;o)

Q2:
oKay can be 0 (equal to false) which means that the cognew for cog2:=... did not work (which usually only occurs if you run out of COGs) or it's 1-8 which represents the cogId + 1 and can be seen as "true" in boolean expressions.

Q3:
NOT( OK1 AND OK2 ) means that the code in the if block will be executed if one of both initializations failed. So it's some kind of error-handling, which in this case is a endless loop to "shutdown" the program execution because it makes no sense to continue without those parts.

Q4:
You have the initial COG.
Simple serial does NOT need an extra driver COG as far as I remember, as it runs in the initial COG.
One Nema-COG
One GPS-UART COG
One Concurent_NMEA_Receiver COG
One detect_LED COG

So I count 5 COGs in total.

lanternfish
11-10-2011, 10:13 PM
Q1:
It's a simple check that the function has been called with at least one pin set, either rxPin or txPin or both. So, it will return -1 (which is equal to true) in case if one or more pins have been specified and it will return 0 (=false) if both rxPin and txPin are set to -1

Thanks for the reply. I am still a bit (no pun intended) uncertain as I would have thought that a false (0) would be returned if no pins specified given that rxOkay is OR'd with txOkay.

lanternfish
11-10-2011, 10:26 PM
sorry ... got interrupted by work ;o)

That's ok as I got interrupted by my wife (it was pm our time)



Q2:
oKay can be 0 (equal to false) which means that the cognew for cog2:=... did not work (which usually only occurs if you run out of COGs) or it's 1-8 which represents the cogId + 1 and can be seen as "true" in boolean expressions.


Just as I thought. So all good there. This leads me to thinking that with a QuickStart board, using the onboard LED's, one could display which COGs are active as a learning aid for beginners. Thinks ... "will start another thread on this idea".



Q3:
NOT( OK1 AND OK2 ) means that the code in the if block will be executed if one of both initializations failed. So it's some kind of error-handling, which in this case is a endless loop to "shutdown" the program execution because it makes no sense to continue without those parts.


Of course! If say OK1 = -1 (true) and OK2 = 0 (false) then (OK1 AND OK2) = 0 and so NOT (OK1 AND OK2) = -1 (true) and code executes.



Q4:
You have the initial COG.
Simple serial does NOT need an extra driver COG as far as I remember, as it runs in the initial COG.
One Nema-COG
One GPS-UART COG
One Concurent_NMEA_Receiver COG
One detect_LED COG

So I count 5 COGs in total.

Thanks for your replies.