Dynamic Virtual Peripheral Library for SXB 2.0
Attached is my current dynamic vp library. This provides background
routines (so these run inside the interrupt) that are easily added to
sxb 2.0 applications. The demo file shows how libraries are imported.
There are 3 libraries.
The SXB_ConMac library contains general constants and macros.
My demo program uses INTERRUPT NOCODE so no __PARAMx variables
are saved during interrupt. The VP interrupt code does not use these
but additional isr code may. There are macros SaveALLparams and
SaveTASKparams (TASKS only use __PARAM1 and __PARAM2)
so when running TASKS as well, such a macro must be used.
These macro calls are included in the isr routine as comments.
The PortPins library contains constants for ports and pins and
subroutines and functions to manipulate ports and pins via variables.
The DynamicVP library contains the VP interrupt code (encoded in 3 macros)
plus subroutines and functions in SXB that you use to pass values to and from
the VP drivers.
VP's are defined by 15 bytes of CDATA. There can be up to 6 VP slots.
Each slot occupies a ram bank of 16 bytes and each VP slot adds 31 cycles
to the interrupt. With 6 slots the isr always takes 196 cycles.
You can run the demo through SxSim v2.08.06 (this is the most recent SxSim version)
and observe the isr cycle count.
Important to note: up to now this has been coded using SxSim only.
I have not yet programmed any sx chip with the demo. That is the next stage.
But I wanted·to share it now so this can be used as a showcase how to implement
libraries. Also, if people want special VP features I'd like to hear them while
doing real silicon testing so I might add these at this point as that seems more
easy than incorperating new features afterwards.
For those that want to experiment: there are conditional switches near the top of
the demo program that you can use to enable/disable certain VP types. The idea
is that an application only enables the VP types it wants to use.
If you do that you will need to comment out some code in· the demo program
that uses the VP type. No changes should be made to the libraries. Everything
is controlled from the demo program.
Just unzip the attachement in some folder and load the demo program in the IDE.
If you have any questions about the code, just ask. But note that I have no
hardware setup yet.
regards peter
routines (so these run inside the interrupt) that are easily added to
sxb 2.0 applications. The demo file shows how libraries are imported.
There are 3 libraries.
The SXB_ConMac library contains general constants and macros.
My demo program uses INTERRUPT NOCODE so no __PARAMx variables
are saved during interrupt. The VP interrupt code does not use these
but additional isr code may. There are macros SaveALLparams and
SaveTASKparams (TASKS only use __PARAM1 and __PARAM2)
so when running TASKS as well, such a macro must be used.
These macro calls are included in the isr routine as comments.
The PortPins library contains constants for ports and pins and
subroutines and functions to manipulate ports and pins via variables.
The DynamicVP library contains the VP interrupt code (encoded in 3 macros)
plus subroutines and functions in SXB that you use to pass values to and from
the VP drivers.
VP's are defined by 15 bytes of CDATA. There can be up to 6 VP slots.
Each slot occupies a ram bank of 16 bytes and each VP slot adds 31 cycles
to the interrupt. With 6 slots the isr always takes 196 cycles.
You can run the demo through SxSim v2.08.06 (this is the most recent SxSim version)
and observe the isr cycle count.
Important to note: up to now this has been coded using SxSim only.
I have not yet programmed any sx chip with the demo. That is the next stage.
But I wanted·to share it now so this can be used as a showcase how to implement
libraries. Also, if people want special VP features I'd like to hear them while
doing real silicon testing so I might add these at this point as that seems more
easy than incorperating new features afterwards.
For those that want to experiment: there are conditional switches near the top of
the demo program that you can use to enable/disable certain VP types. The idea
is that an application only enables the VP types it wants to use.
If you do that you will need to comment out some code in· the demo program
that uses the VP type. No changes should be made to the libraries. Everything
is controlled from the demo program.
Just unzip the attachement in some folder and load the demo program in the IDE.
If you have any questions about the code, just ask. But note that I have no
hardware setup yet.
regards peter
zip

19K
Comments
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
John J. Couture
San Diego Miramar College
And we must all thank Bean for implementing the conditional compile directives.
Those really make the difference (for me anyway) as it does allow to write reusable
code and filter out unused routines. And when you say clearer, I get your point.
The conditional compile directives are very much present and having the same color
as comments doesn't help. Thankfully indenting is allowed otherwise it would be
(almost) impossible·to read.
@PJMonthy,
Could the editor get an option to select a color for '{$···· } lines?
That should improve readability.
regards peter
In this thread I will keep you posted on the progress.
Attached is a test program (libraries included) for the Serial Transmit VP.
The hardware used is an SX28AC @20MHz in SX Tech board.
Propplug/USB2SER connected with its RX pin to sx pin RB0.
The main program simply sends an "A" every second.
I used the Basic Stamp Debug window to show received characters.
With the serial transmit working, the next step is to add an Serial Receive·VP,
and let the main program echo received characters.
If you want to test this for yourself, simply·unzip in a folder
and·load the DynamicVP_test_STX1.SXB file into the IDE and
program an SX28.
regards peter
the Serial Receive VP. There was only one small bug in the Serial Receive driver.
(a missing CLC at the start of·state1 of the serial receive driver).
With full duplex communication working, the next step is to
add a DAC VP. I will write a test program that receives a DAC value.
The DAC output pin should then reflect that·value.
The sx will transmit·the received DAC value, after first converting
the·received ASCII string to a binary·value, which is used to
update the DAC, then converting the value·to an ASCII string that is
transmitted back to the pc. These conversions will involve the Format library
that I posted a few days back.
Just unzip in a folder and load DynamicVP_test_SRX1.SXB
The hardware is still SX28 @20MHz on SX Tech board.
Propplug/USB2SER with its RX pin connected to sx pin RB0,
and its TX pin connected to sx pin RB2.
regards peter
All my libraries are involved now.
The hardware is the same as in the previous post plus one 1k resistor
and one 10uF electrolyt capacitor that make up a low pass filter for the DAC output.
RB.6 ---[noparse][[/noparse]1k]---+--- Udac
··············+|
··············=== 10uF
·············· |
·············· V GND
Here are the results of the DAC measurements
DACvalue· Udac (measured with 3.5digit DMM)
0········ 0.00V
64······· 1.23V
128·······2.45V
192·······3.69V
255······ 4.92V
That's quite well and as accurate as one can expect from an 8bit DAC.
The next step is to add the ADC vp. I will use the DAC vp to generate a voltage
that will be measured by the ADC. The measured value will then be transmitted
to the pc. I expect the measured ADC value to be slightly lower than the DAC value
because of the resistor divider formed by DAC output resistor and ADC input resistor.
Again, unzip in·a folder and load the DynamicVP_test_DAC1.SXB file.
regards peter
Additional hardware since· previous post:
RB.6 ---[noparse][[/noparse]1k]---+
+
··············+|············|
··············=== 10uF······|
·············· |···················· |
·············· V GND······· |
····························|
RB.5
+·········· |·
RB.4 ---[noparse][[/noparse]10k]---+---[noparse][[/noparse]10k]---+
··············· |
·············· === 0.1uF
··············· |
··············· V GND
ADC results:
Setting DAC to 255 gives ADC=236 (approx. (10/11)*255 )
Setting DAC to 0 gives ADC=10, which is due to charging the DAC cap via 10k.
For this test program I added a 4th VP bank which is the limit for SX28.
Next step is to test PWM. Instead of using DAC I will use PWM, which should lead
to identical results.
regards peter
The PWM driver uses 4 states, meaning the lowtime and hightime parameters
should each be at least 2.
pwm frequency = INTERRUPTRATE/(lowtime+hightime)
Values for lowtime and hightime are inputted via terminal
(I used basic stamp debug terminal) like lowtime,hightime<ENTER>
The inputted values are then displayed by the terminal as
lowtime:hightime
Unzip the attachement in a folder and load DynamicVP_test_PWM1.SXB
in the IDE. The pwm signal appears on pin RB.7
Next step is to test the 32bit Timer VP.
regards peter
serial transmit and receive test programs.
I added a format parameter to the serial routines.
You can now transmit and receive in 9 formats:
E71,E72,E81,O71,O72,O81,N72,N81,N82
Just set the appropiate field to the desired format.
Also added 2 functions, dvpSER_OddParity and dvpSER_EvenParity
that calculate the paritybit for an 8bit value. In case of a 7bit value
just clear b7.
The attached zip contains 2 test programs, DynamicVP_test_STX2.SXB
(transmit "A" every second using the specified format) and
DynamicVP_test_SRX2.SXB that echoes received bytes using the
specified format.
Note that received bytes are not checked for a valid paritybit.
The paritybit is simply cleared before returning the byte.
(Any error checking should be done on a higher level).
regards peter
to the libraries. VP declarations are now simple macro calls,
which makes them look like TASK declarations.
It also hides the implementation.
·
By including individual $uses for ALL used external subs/funcs in library
subs/funcs, the list of $uses is no longer required in the application file.
(except for those subs/funcs directly used by the application but not the libraries).
This leads to a cleaner application file.
Next step is to write a test program for the Timer VP.
After that, I will write a demo that features all VP's.
Edit:
Something was messed up. Also, when enabling all VP code. some driver entry points
moved into the upper half of their codepage, so I had to move around some code.
I have attached this new version that has all VP driver code enabled.
regards peter
Post Edited (Peter Verkaik) : 3/17/2009 6:15:16 PM GMT
I have been trying to combine the dynamic VP's with the TASK commands.
No luck for several reasons, mainly because of interrupt code overheaded
caused by using the TASK code.
So I cleaned up my optimized TASK implementation that I had offered before
(in the beta forum)·but that was rejected for unclear reasons.
My TASK implementation is located in file TaskSwitch_DEF.SXB
and consists only of macros (so there is no TaskSwitch_INC.SXB file).
'
' Library Description
'
' This library provides an alternative TASK implementation for the SXB 2.0
' builtin TASK commands. This implementation still uses the builtin TASK
' declarations, but one has to call \mTaskList that generates a list
' of tasks (used by the TaskScheduler), and \mTaskInit that initializes
' the task variables.
'
' The reason for this alternative implementation is that the builtin implementation
' uses interrupt code which size grows with the number of task slots, leaving
' less isr cycles for other code (like dynamic VP's). This alternative implementation
' has a fixed and smaller size interrupt code. The maximum number of task slots that can
' be used with this implementation is 5 for all SX devices. The builtin implementation
' allows 5 for sx18/20/28 and 8 for sx48/52.
'
' There are macros defined that replace the builtin TASKS commands:
' \mTaskDisable······················· replaces·TASKS DISABLE
' \mTaskEnable························ replaces·TASKS ENABLE
' \mTaskStop slot····················· replaces·TASKS STOP,slot
' \mTaskStart slot···················· replaces·TASKS START,slot
' \mTaskSuspend slot,ticks············ replaces·TASKS SUSPEND,slot,ticks
' \mTaskSet slot,name,interval,delay· ·replaces·TASKS SET,slot,name,interval,delay
' \mTaskIsr··························· replaces·TASKS RUN,tasktick[noparse][[/noparse],tasks] (inside isr)
'
' There is an additional macro for TASKS SUSPEND for use within a TASK routine.
' \mTaskReschedule ticks·replaces·TASKS SUSPEND,slot,ticks
' This is to let a task reschedule itself without the need to preserve __PARAM1 upon task entry.
The test program runs a serial transmit and serial receive VP, plus two tasks (116 isr cycles max).
You can comment out the '{$define USE_DynamicVP} at the start and the test program merely
runs the two tasks (54 isr cycles max). Adding more tasks has no effect on the isr.
The two tasks are LEDs flashing at different rates.
Just unzip in a folder and load DynamicVP_test_TASK1.SXB
Enjoy.
regards peter
Post Edited (Peter Verkaik) : 3/19/2009 8:37:18 PM GMT
Each Timer VP when installed gives you eight 8bit timers or four 16bit timers
or a combination of 8bit and 16bit timers.
The mainloop of the test program is the typical use of a timer.
Unzip attachement in a folder and load DynamicVP_test_TASK2.SXB into the IDE.
This concludes the testing of the VP code.
The zip holds all my libraries and these are all up to date.
regards peter
flowcontrol and higher resolution ADC and DAC.
Because of the added code I had to redefine the ppPortX values (but this has no effect
on application programs). The ppPortX values now equal the SXB port values
(eg. ppPortA var ra). The changed portvalues also required the fsr to read ports,
and so fsr needs to be saved. The VP interrupt code uses __param1 and __param2.
The end effect is that I even save one cycle per vpslot (now 30, was 31).
The ADC and DAC have an additional parameter that sets the resolution from 8 to 16 bits.
The attached picture shows the test program output using a 10bits DAC and 12bits ADC.
The fact that the ADC value is below 2048 for DAC value 512, is because of the circuitry.
I find the output value quite stable if you consider that the DAC is started and stopped as well.
The test program uses only 2 vpslots but 5 VP's, demonstrating starting and stopping
of VP's. Concurrently running are two TASKs using my alternative TASK implementation.
Just inzip in a folder and load DynamicVP_test_ADC3.SXB
It compiles for sx28 and sx48, but I have only tested it with a SX28.
(but I'm sure the program runs fine using a SX48).
Now I only need to complete its documentation although the documentation in
the DEF files should be sufficient.
regards peter
Post Edited (Peter Verkaik) : 4/9/2009 6:11:31 PM GMT
Just curious what terminal software you are using and where that might be found?
Cheers,
Peter (pjv)
I did a search on Google for the program and the search revealed it could be found on one of the Parallax forums.
Here is the link: http://forums.parallax.com/showthread.php?p=678480
Gary
I used the Basic Stamp Terminal which you can startup from
the Basic Stamp IDE. You can even startup multiple debug
windows.
@Sens-a-Dat,
Thanks for the link, but that is a stand-alone terminal.
It appears not to show the CTS state which the BS debug terminal
does show. In this case, for the included test program, that is
better as it shows that CTS is turned off while the SX transmits data
to the terminal (because the receive VP is stopped then).
regards peter
Post Edited (Peter Verkaik) : 4/9/2009 4:20:26 PM GMT
It was specified to return a byte but it should return a word.
I updated the zip in the above post.
Alternatively, you can·download the fixed attachement and replace the
zipped file.
Sorry about this, but it has no impact on the tesprogram as that
only used a bytevalue.
regards peter
pins that can be specified as either input or output. This allows
VP drivers that require two output pins for example.
These changes are·done via the·constants that were already defined,
they just got other· values.
My plan is to add serial VP code for rs485, which requires 2 output pins
for transmitting.
The test program is identical to the previous test program.
The zip contains all updated libraries.
regards peter
Post Edited (Peter Verkaik) : 4/18/2009 4:51:43 PM GMT