starting smart pins from p2spin
@JonTitus
@JonnyMac
@evanh
Or anybody.
I have been following the document by @JonTitus regarding the smart pins. Unlocked the word doc to fix the blocked out verbage because of the pdf maker shifted some stuff and making notes to my self. Getting them to work and beginning t understand them much better.
Been looking at the Spin2 documentation and have some confusion as well as sifting through the forums for something simple.
Example code attached.
I am looking of how in Spin2 to simply call the pins from a Spin2 pub(). Bascally start it from spin2 versus directly as the code is written.
Any simple examples and suggestions would be appreciated as well as some links to code that calls stuff from spin2 in pasm as well as passing parameters.
Thank you in advance.
Martin
Comments
The Spin2 provided function is called
pinstart()
. Not a lot is said about it in the docs. Description is simplyPINSTART(PinField, Mode, Xval, Yval)
PINCLEAR(PinField)
is its partner function for returning it back to a plain pin.These are listed under section "Built-In Methods" in the official Spin2 manual. I've attached a PDF of the current version 44.
If you've got an oscilloscope, here's a demo you can watch using it:
The smartpin mode is one that pulses the pin output for a specified number of steps (transitions) and then holds the output there. I've setup a loop that tells it to make 5 steps, waits half a second, then repeats.
I learned to code by studying working examples. Start with a few simple things that might be useful to what you're doing. It takes a minute to understand the smart pins, but after some time experimenting, it will start to become clear. I'm with Evan: Connect a 'scope or logic analyzer; this will help you make sense of things.
I've attached three simple objects that use smart pins -- I use these objects frequently.
@evanh your example helps thanks. What is the P_OE stand for?
@JonnyMac do you have anything a bit less complicated at this time for me to build from. I need to start at a lower level and build up. Thanks for the help to both.
Martin
@JonTitus
Your document is now making sense.
I will look at the email you sent shortly.
Martin
When a smartpin is configured, with PINSTART() for example, it diverts its associated DIR bit to become its reset control. The regular DIR pin drive is then done via one bit in PINSTART's mode word - Bit6. The symbol name for setting of bit6 is
P_OE
. OE is short for Output Enable. Any smartpin mode that has an output to its pin would be expected to set this P_OE bit, otherwise the physical pin stays as just an input.There is a full list of these symbols in the Spin2 Doc under the section name "Built-In Symbols for Smart Pin Configuration"
@evanh
Thanks for the definition. Easy. I will look at the built in symbols list.
Btw, quesiton. I am assuming the the smart pins are fire and forget like the counters on the P1. Is this the basic case?
My first two mini projects is running servos and also the X-band radar module.
Any simple examples of passing parameters from pasm2 to spin2?
Thanks again.
Martin
You cannot (practically) call spin2 functions from pasm. Why are you trying to use pasm anyway? For someone just starting out, spin2 is vastly easier. If you find spin2 is not fast enough, then you can use inline pasm within spin2 to optimize critical functions, or you can use flexspin to compile the whole program to pasm.
In the P2, basic servo control is accomplished with PWM smart pin mode. The attached P2 servo object works with analog (50Hz) and digital (300Hz) servos. If you want to do servo ramping (i.e., speed control of movement), that requires an extra cog or running in a timed loop that allows you to refresh the servo position to intermediate positions moving to the desired end point (I have an 8-channel servo controller with movement speed control for the P2 that uses a cog, but doesn't use PWM mode, it uses PULSE mode).
The attached demo uses PWM to generate a 1kHz PWM output on one pin which is fed into another (jumper wire). The second pin is setup to count positive edges using a smart pin mode. The code shows how to setup the pin to count edges, how to read the count, and how to reset the count.
Not that -- as has been mentioned in this thread -- when in smart pin mode the dir bit of the pin is used to enable/reset the smart pin function. The pinfloat command sets the pin to an input, clearing the dir bit to 0 which resets/disables the smart pin mode. The pinlow command makes the pin an output which sets the dir bit to 1 which re-enables the smart pin. The use of the dir bit for on/off control of the smart pin is why you will see p_oe in some smart pin modes (e.g., PWM); this enables smart pin output.
When running on my computer, the output looks like this.
It's no different than in the P1: You have to establish a shared location in the hub that the Spin2 and PASM2 cogs know about. In the P2 the initial pointer (par in P1, ptra in P2) can be used with offsets which can make data exchange between PASM2 and Spin2 nicer.
For example:
I use offsets with ptra in jm_fullduplexserial.spin2 (in the attached archive).
The nature of hardware is it does tend to do its own thing, yes. Both wheels on a bike rotate as needed without having to alternately micro-manage them one by one.
@JonnyMac
Thanks for the help I am making progress.