BS2 Library Testing
I've been playing around making a BS2 Library, so far with PULSOUT, PULSIN, FREQOUT, PAUSE and RCTime. Here's some sample code to use it. One cog is reading RCTime on a PhotoR and another Cog is using that data to sound a tone on a buzzer. They share a common global variable so they can both be doing their own thang simultaneously:
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Martin Hebel
Perform a Survey of Electronic Technology Employers - Click here!
Personal Links with plenty of BASIC Stamp info
and SelmaWare Solutions - StampPlot - Graphical Data Acquisition and Control
Post Edited (Martin Hebel) : 3/3/2006 3:57:24 PM GMT
{{ ____________________________________________________________________
Test of BS Function Library
This test reads RC Time of Cap & Photoresistor on A4 on one cog
and plays frequency of RCTime value on another cog from A0
for multiprocessor BS2. Data is passed using global variable.
___________________________________________________________________
}}
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
VAR
Long RC_Val, stack1[noparse][[/noparse]50], Stack2[noparse][[/noparse]50]
CON ' Set Pins
Buzzer = 0 ' Speaker
PhotoR = 4
OBJ
BS2 : "BS2_Functions" ' Create BS2 Object
PUB Start
BS2.start ' Initialize BS2 library
cognew(GetTime,@stack1) ' Start a cog to run RCTime routine
cognew(SoundTime,@stack2) ' Start a cog to run FREQOUT routine
'cognew(SoundTime2,@stack2) ' Start a cog to run FREQOUT_SET routine - Use this instead one for smoother tone
PUB GetTime '' Routine to read RCTime and store in global RC_Val
repeat
dira[noparse][[/noparse]PhotoR]~~ ' set to output
outa[noparse][[/noparse]PhotoR]:=1 ' set high
BS2.Pause(10) ' allow to charge for 10mS
RC_Val:= BS2.RCTime(PhotoR,1) ' read RCTime and store in RC_Val
BS2.Pause(500) ' Pause this routine for 1/2 second to prove other still runs
PUB SoundTime '' Routine to sound frequency
repeat
BS2.Freqout(Buzzer,100,RC_Val) ' Continually sound buzzer at RC_Val value
PUB SoundTime2 '' Uses modified FREQOUT_SET Command
'' Sounds tone continually, only updates when new value
Repeat
BS2.Freqout_SET(Buzzer,RC_Val)
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Martin Hebel
Perform a Survey of Electronic Technology Employers - Click here!
Personal Links with plenty of BASIC Stamp info
and SelmaWare Solutions - StampPlot - Graphical Data Acquisition and Control
Post Edited (Martin Hebel) : 3/3/2006 3:57:24 PM GMT

Comments
#1: PUB Start is a "key word" routine, or event?· If so, are there other "key" routines / events ?
#2: Reading the CogNew command, I can see you specify the PUB (or PRI?) routine for that cog to run, and it's stack space.· Can you pass paramiters?
#3: The VAR RC_Val, definded at the top of the file indicates the VAR is globally accessable;·
····· To be private, I would have declared them in the PUB or PRI routine?
#4: Seeing the PUB GetTime, it doesn't use any VARs, but it requires·VAR space ( ie: @Stack1 in the command ) ?
#5: What would be the difference in declaring PUB GetTime or PRI GetTime ?·
Oh, and one last one...
So, a PUB Routine_Name would indicate a Global level Routine, ... Ok... hang on a second... I think someone just turned on the lights...
Does the compiler looks at things like this:· ( this is a question...)
VAR Long Global_Var, Cog1Stack[noparse][[/noparse]50], Cog2Stack[noparse][[/noparse]50] PUB Start CogNew(Cog_1Code,@Cog1Stack) 'Start up first cog using cog 1 code CogNew(Cog_2Code,@Cog2Stack) 'Start up second cog using cog 2 code PUB Cog_1Code VAR Long Private_var 'THis would be a VAR that's only available in this routine, right? repeat 'Do until the end of time ... 'Some worth while code that ... 'actually does something GOSUB Cog_1_Routine 'Call a private routine within this cog's code... ... 'Some more code that ... 'Continues to do something GOSUB Global_Routine 'Call a routine that's globally accessable by all Cogs GOSUB Cog_1_Routine 'Call the pritate routine for this cog's code again... ... 'The rest of the code that ... 'finishes the something this cog does PRI Cog_1_Routine ... 'The Rountine called by Cog 1, and can only be called by cog 1 ... 'This does some thing and then RETURN 'returns to the caller (should be code in cog 1 code PUB Cog_2_Code REPEAT 'Again, do until the end of time ... 'This is Cog 2's code... GOSUB Global_Routine 'THat calls the global routine ... 'And does some nifty stuff. PUB Global_Routine ... 'This is a global routine to be used by any cog as needed. ... 'This does some stuff then RETURN 'returns to the callerIf this is getting too detailed then skip it, I'll wait for the docs to be published.·
THis code is very is easy·to read and understand.
Thanks for posting it!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Just tossing my two bits worth into the bit bucket
KK
·
PUB Routine_Name(arg0, arg1, arg2):return_value | local_variable1, local_variable2
A private function (a routine only called by routines within the object) is declared the same way but with PRI instead of PUB.
The arguments, return value and local (private) variables are all optional so you can get functions that look like:
PUB Routine1
PUB Routine2(pin1, pin2)
PUB Status_Check:OK
PRI Do_something | i, t
PRI Do_something_else:Status | h,k
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
#1: PUB Start is a "key word" routine, or event?· If so, are there other "key" routines / events ?
PUB is a public function/method/subroutine (or whatever you care to name it, haven't read the full docs yet to see conventions)· This means if the program you see was used as an object for another file, those functions would be accessible.· If they were PRI, they wouldn't be.
#2: Reading the CogNew command, I can see you specify the PUB (or PRI?) routine for that cog to run, and it's stack space.· Can you pass paramiters?
When the stack space is address is passed, both programs can have access to the same memory locations, meaning you can write in one and read in another.· When starting an Assembler COG, the pointer is passed also so it can be shared through the ASM code as well!· Potent, huh?
#3: The VAR RC_Val, definded at the top of the file indicates the VAR is globally accessable;·
····· To be private, I would have declared them in the PUB or PRI routine?
Yes, it is global to all the functions, not matter which cog it's in.· To be private and local, it would be declared in the function:
PUB· ReadMe (passed1, passed2) | mylocal1, mylocal2
#4: Seeing the PUB GetTime, it doesn't use any VARs, but it requires·VAR space ( ie: @Stack1 in the command ) ?
Yes, not an expert at this yet, still learning, but the functions that routine calls needs stack space for the variables it may use.· This part gets tricky on how much to allocate.
#5: What would be the difference in declaring PUB GetTime or PRI GetTime ?·
I think I answered this in #1
Oh, and one last one...
So, a PUB Routine_Name would indicate a Global level Routine, ... Ok... hang on a second... I think someone just turned on the lights...
The BS2_Functions spin file uses PUBs like RCTIme so they can be accessed as object function in the code you·see.· It may also have PRI functions that would NOT be accessible to the test code you see.
Maybe I should post my BS2_Functions code as well later.
I'll have to take a closer look at the code you wrote later in depth.· Duty calls.
Glad it helped light some lights [noparse]:)[/noparse]
-Martin
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Martin Hebel
Perform a Survey of Electronic Technology Employers - Click here!
Personal Links with plenty of BASIC Stamp info
and SelmaWare Solutions - StampPlot - Graphical Data Acquisition and Control
{{ ________________________________________________________________________ BS2 Function Library Functional Equivalents of many BS2 Functions Version 0.5 3/2/06 Contact: Martin Hebel [url=mailto:martin@selmaware.com]martin@selmaware.com[/url] To use include in code: OBJ BS2 : "BS2_Functions" Start Object with: BS2.Start Use Functions as: BS2.FREQOUT(pin,duration,frequency) or variable = BS2.PULSIN(pin,state) ________________________________________________________________________ }} OBJ sqWave : "Square Wave Object" ' Import square wave cog object from Andy Lindsey var long s, ms, us,Last_Freq PUB Start s := clkfreq ' Clock cycles for 1 us ms := clkfreq / 1_000 ' Clock cycles for 1 ms us := clkfreq / 1_000_000 ' Clock cycles for 1 s Last_Freq := 0 PUB PULSOUT(Pin,Duration) | clkcycles '' Accepts pin # and duration in 2uS increments '' Smallest value is 10 allowed is at clkfreq = 80Mhz, 20 at 40Mhz, etc ClkCycles := (Duration * us * 2 - 1250) ' 1200 is offset for processing time dira[noparse][[/noparse]pin]~~ ' set to output !outa[noparse][[/noparse]pin] ' NOT output waitcnt(clkcycles + cnt) ' Wait time !outa[noparse][[/noparse]pin] ' NOT output PUB PULSOUT_1uS(Pin,Duration) | ClkCycles '' Accepts pin # and duration in 1uS increments '' Smallest value allowed is 20 at clkfreq = 80Mhz, 40 at 40Mhz, etc ClkCycles := (Duration * us-1050) dira[noparse][[/noparse]pin]~~ ' set to output !outa[noparse][[/noparse]pin] ' NOT output waitcnt(cnt + clkCycles) ' Wait time !outa[noparse][[/noparse]pin] ' NOT output PUB PAUSE(Duration) | clkCycles '' Accepts time in mS '' Smallest value is 2 at clkfreq = 5Mhz, higher frequencies may use 1 waitcnt(cnt + (Duration * ms-2300)) PUB PULSIN (Pin, State) : Duration '' Reads duration of Pulse on pin defined for state, returns duration in 2uS resolution Duration := PULSIN_1us(Pin, State)/2 Return PUB PULSIN_1uS (Pin, State) : Duration | ClkStart, clkStop, timeout '' Reads duration of Pulse on pin defined for state, returns duration in 1uS resolution DIRA[noparse][[/noparse]pin]~ waitpne(State << pin, |< Pin, 0) ' Wait for opposite state ready waitpeq(State << pin, |< Pin, 0) ' Wait for state to start ClkStart := cnt ' Save counter waitpne(State << pin, |< Pin, 0) ' Wait for opposite state to end clkStop := cnt ' Save stop time Duration := ((clkStop - ClkStart) * 1000 / (clkfreq / 1000)) ' calculate in 1us resolution Return PUB FREQOUT(Pin, Duration, Frequency) '' Plays frequency defines on pin for duration in mS, does NOT support dual frequencies. dira[noparse][[/noparse]pin]~~ ' Set direction to output sqwave.Update(Pin,Frequency,0) ' start with Andy's freq gen library Pause(Duration) sqwave.Update(Pin,0,0) ' Stop freq Return PUB FREQOUT_SET(Pin, Frequency) '' Plays frequency defines on pin INDEFINATELY does NOT support dual frequencies. '' Use Frequency of 0 to stop If Frequency <> Last_Freq ' set freq if different dira[noparse][[/noparse]pin]~~ sqwave.Update(Pin,Frequency,0) Last_Freq := Frequency Return PUB RCTIME (Pin,State):Duration | ClkStart, ClkStop '' Reads RCTime on Pin starting at State, returns discharge time scaled to BS2 values DIRA[noparse][[/noparse]Pin]~ ClkStart := cnt ' Save counter waitpne(State << pin, |< Pin, 0) ' Wait for opposite state to end clkStop := cnt ' Save stop time Duration := ((clkStop - ClkStart) * 1000 / (clkfreq / 1000)) * 100/130 ' calculate in 2us resolution, scale for BS2 ReturnAlso note the documentation the Spin IDE makes from the code above: (Ok, still learning here, so may be better ways, but....)
________________________________________________________________________ BS2 Function Library Functional Equivalents of many BS2 Functions Version 0.5 3/2/06 Contact: Martin Hebel [url=mailto:martin@selmaware.com]martin@selmaware.com[/url] To use include in code: OBJ BS2 : "BS2_Functions" Start Object with: BS2.Start Use Functions as: BS2.FREQOUT(pin,duration,frequency) or variable = BS2.PULSIN(pin,state) ________________________________________________________________________ Object "bs2_functions" Interface: PUB Start PUB PULSOUT(Pin, Duration) PUB PULSOUT_1uS(Pin, Duration) PUB PAUSE(Duration) PUB PULSIN(Pin, State) : Duration PUB PULSIN_1uS(Pin, State) : Duration PUB FREQOUT(Pin, Duration, Frequency) PUB FREQOUT_SET(Pin, Frequency) PUB RCTIME(Pin, State) : Duration Program: 94 Longs Variable: 4 Longs __________ PUB Start ___________________________ PUB PULSOUT(Pin, Duration) Accepts pin # and duration in 2uS increments Smallest value is 10 allowed is at clkfreq = 80Mhz, 20 at 40Mhz, etc _______________________________ PUB PULSOUT_1uS(Pin, Duration) Accepts pin # and duration in 1uS increments Smallest value allowed is 20 at clkfreq = 80Mhz, 40 at 40Mhz, etc ____________________ PUB PAUSE(Duration) Accepts time in mS Smallest value is 2 at clkfreq = 5Mhz, higher frequencies may use 1 __________________________________ PUB PULSIN(Pin, State) : Duration Reads duration of Pulse on pin defined for state, returns duration in 2uS resolution ______________________________________ PUB PULSIN_1uS(Pin, State) : Duration Reads duration of Pulse on pin defined for state, returns duration in 1uS resolution ______________________________________ PUB FREQOUT(Pin, Duration, Frequency) Plays frequency defines on pin for duration in mS, does NOT support dual frequencies. ________________________________ PUB FREQOUT_SET(Pin, Frequency) Plays frequency defines on pin INDEFINATELY does NOT support dual frequencies. Use Frequency of 0 to stop __________________________________ PUB RCTIME(Pin, State) : Duration Reads RCTime on Pin starting at State, returns discharge time scaled to BS2 values▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Martin Hebel
Perform a Survey of Electronic Technology Employers - Click here!
Personal Links with plenty of BASIC Stamp info
and SelmaWare Solutions - StampPlot - Graphical Data Acquisition and Control
What about SERIN/SEROUT ?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thanks inaki,
Martin
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Martin Hebel
Perform a Survey of Electronic Technology Employers - Click here!
Personal Links with plenty of BASIC Stamp info
and SelmaWare Solutions - StampPlot - Graphical Data Acquisition and Control
Martin, so you are doing SHIFTIN/OUT in spin? I found spin·to woefully inadequate for high speeds, Ive tossed the spin version and am presently working on ASM to define them with spin wrapper functions. Im following the full_duplex model in it's operation. Though you may find a speedier way to do it in spin than my first attempt.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
Post Edited (Paul Baker) : 3/3/2006 5:34:28 PM GMT
-Martin
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Martin Hebel
Perform a Survey of Electronic Technology Employers - Click here!
Personal Links with plenty of BASIC Stamp info
and SelmaWare Solutions - StampPlot - Graphical Data Acquisition and Control
-Martin
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Martin Hebel
Perform a Survey of Electronic Technology Employers - Click here!
Personal Links with plenty of BASIC Stamp info
and SelmaWare Solutions - StampPlot - Graphical Data Acquisition and Control
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·1+1=10
This is resembling something like a 3G IDE... (Like the functionality of the vb 6 IDE)..
Martin, thanks a lot for explaining, and with your answers (and the code!), it makes a whole lot more sence.
I like the way the code 'reads'.... easy to follow and understand...
Very memory effencant (sp!)...
Ya, the lights are getting bright... I'll get my shades and continue on...
I cant wait...
I have been a VB programmer sence VB DOS (ver 1.0), and all the way up through .net 2005... so, what your building with the objects, and the modeling
is very much like class and oop in VB... very VERY simular... issues are... no GIU (Ok, I can live with that... make one out of LCD and some buttons...)
No "canned" events... (you would make events from watching pins...)
The way you are calling things objects are VERY simular to VB's classes... public and private functions and subs... delercations and returns...
Still, if you know VB classes, then the only thing to do here is learn the syntax of the language and to program within it's memory limits.
Still, not much of an issue...
Man, this is exciting. I can see dozens of usages...
I will add, your code examples are awesome to read. I can actually understand without know the language syntax, what your are doing.. (I guess your style helps !).
April isn't gonna get here fast enough!.
Questions about the IDE folks:
#1: Can it PLEASE compile without the propeller dev board attached?
#2: Does it have a object explorer ??
#3: Project based, not file based? (IE, A project can have a number of files with it.)
#4: Proper casing of declared vars. IE, if you delcare VAR Ddiksoak, type in ddiksoak, it will change it to Ddiksoak
#5: Printer support (for printing out code!)
Lastly, do you believe you will support the tokenizer so others can make custom IDE's ?
Ok, I know I'm ranting here, but I'm excited...
I.. I.. Ok, I'll park it...
THanks again for all the information, the code (espically the code) and the help!
...
One humbled kidd, Kaos Kidd that is
...
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Just tossing my two bits worth into the bit bucket
KK
·
·1+1=10
Yes, in color if you wish.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Beau Schwabe
IC Layout Engineer
Parallax, Inc.
Post Edited (Beau Schwabe (Parallax)) : 3/3/2006 10:27:43 PM GMT
THanks guys, really.
...
I can't wait!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Just tossing my two bits worth into the bit bucket
KK
·
Am I to understand that·a Spin function/procedure cannot have in-line assembly?
Does any assembly routine require its isolated COG to execute in?
I just reread the documents on the propeller product page, and apparently there no provision for in-line assembly.· For sume reason, I just assumed that there would be.
Should I assume that starting an assembly routine in a COG is transparent to the Spin programmer, for does the Spin programmer need to pay attention to the typical details of multi-processor/multi-task interactions?
Daniel
·
The Spin interpreter takes up the entire cog.· To have any assembly requires a different cog to run it in.· Also, unlike ASM, C or SX/B which are compiled, Spin is in an interpreted language using tokens (byte codes), so in-line assembly is not possible.
Now, if you have multiple jobs for assembly, data can be shared between a Spin cog and a pASM cog to trigger events or simply for data sharing.
Starting an assembly cog is just a matter of starting a new cog and declaring the assembly orgin to use (though I haven't gotten to playing with assembly programming yet on the Propeller).
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Martin Hebel
Perform an Employer's Survey of Electronic Technologies Graduates· - Click here!
Personal Links with plenty of BASIC Stamp info
and SelmaWare Solutions - StampPlot - Graphical Data Acquisition and Control
I have just posted a "Propeller Overview" section that I believe answers your questions.
Cheers,
Peter (pjv)