Shop OBEX P1 Docs P2 Docs Learn Events
SmartPins Duty Cycle measurement — Parallax Forums

SmartPins Duty Cycle measurement

The big pieces are looking good - Pins can measure Period, or can measure Hi time, which means two pin cells are needed paired to measure both Period and Hi time, to then extract Duty cycle in software.

There seem to be some small problems in the details :
Ideally, you want
a) Period and Hi time to apply to the same Pin Cycle in
b) A choice of how many Pin Cycles to measure over
and.or
c) Define of some measurement time, (rounds up to whole Pin cycles).

For b) the present time-only X Counter, could be clocked from the Pin, instead of SysCLK ?
That's a simple CLK source config ?

for a) some cross-linking seems to be needed, so pin-pair Start and Capture in Sync

With 2 pin cells, there are 2 X Counters, seems a pity to waste one ?

As well as possible define of N-Cycles to average over, another approach is to define some minimum time (TGM) - eg >= 100ms. (c)

With this setup, the Pair of cells total HI and Periods, until time is >= TGM.
Then, the next Period edge fires the capture-pair, and the cycle repeats.

This Capture-Pair on next edge >= TGM is also the feature needed for Reciprocal Counting (mentioned already in other thread).

It is very similar to existing Time-regulated capture, just a D-FF is added on >= flag, so actual capture is next-pin-edge based.
If TGM is too small, the SW side polls/waits for the Pin edge.
«1

Comments

  • rjo__rjo__ Posts: 2,114
    I'm very confused... which is a normal state these days...
    Are you assuming the case of a continuously varying duty cycle and period?

  • jmgjmg Posts: 15,175
    rjo__ wrote: »
    I'm very confused... which is a normal state these days...
    Are you assuming the case of a continuously varying duty cycle and period?

    Yes, that is the general case that needs to be covered.
    Some sensors have simple low-spec RC oscillators, and that means the period has poor tolerance, and can drift.
    The duty cycle should be more precise, as that is set by a timer.
    Other systems may encode information in the Period, but keep the duty cycle fixed % for every Period.


  • Shouldn't the COG be managing that?
  • rjo__rjo__ Posts: 2,114
    Tater

    I think in terms of frequency response that would be correct... and if you are going to build it into the pin, then don't you have to warn everybody that the frequency limit is Nyquist/something substantial, because you are going to be limited by the actual bandwidth of the signal, no? At lower frequencies, you have cog instruction time available... which can be parallelized.

    In summary, I don't know:)
  • potatoheadpotatohead Posts: 10,261
    edited 2016-02-12 23:32
    Right. We have some testing and thinking to do. The optimal division of labor isn't obvious.

    I don't know either.
  • jmgjmg Posts: 15,175
    potatohead wrote: »
    Shouldn't the COG be managing that?

    Short answer : It can't.

    To measure both Period and Pulse on the same cycle, requires (quite simple) hardware sync, not software.

    The COG of course, manages the Divide to calculate Duty cycle, given the numbers by the Smart pins (paired)/

  • rjo__rjo__ Posts: 2,114
    I was not trying to be difficult... promise:)
  • rjo__rjo__ Posts: 2,114
    This is related and I don't want to bother Chip. So, you seem like the next best option.

    When we are talking about generating a duty cycle... where are the duty@frequency limits?
    For example... as it stands now, can I generate a 48MHz frequency with a 40% duty cycle?
  • Can't we use one unit to measure pulse, and another to measure duty?

    Seems like that use case is covered by the ability to assign multiple units (POGS) to the task?

  • AribaAriba Posts: 2,690
    jmg wrote: »
    potatohead wrote: »
    Shouldn't the COG be managing that?

    Short answer : It can't.

    To measure both Period and Pulse on the same cycle, requires (quite simple) hardware sync, not software.

    The COG of course, manages the Divide to calculate Duty cycle, given the numbers by the Smart pins (paired)/

    A Cog can do it for example with Mode %10001 = Time A­input high and low states that gives the high and the low time with two consecutive reads. It can be repeated several times to get an average value over some time.

    Andy
  • jmgjmg Posts: 15,175
    edited 2016-02-13 04:03
    Ariba wrote: »
    jmg wrote: »
    potatohead wrote: »
    Shouldn't the COG be managing that?

    Short answer : It can't.

    To measure both Period and Pulse on the same cycle, requires (quite simple) hardware sync, not software.

    The COG of course, manages the Divide to calculate Duty cycle, given the numbers by the Smart pins (paired)/

    A Cog can do it for example with Mode %10001 = Time A­input high and low states that gives the high and the low time with two consecutive reads. It can be repeated several times to get an average value over some time.

    Andy
    Key element there is two consecutive reads, so you are not measuring the same cycle, and so lose precision.
    - but you are right, if you can tolerate alternate reading and accept any drift between readings, then you can multiplex the two readings.

    This rapid mode change approach would be worth testing, as there is a chance the enable part way through a Hi, needs to be discarded.
    Rapid alternate also does not address multiple Pin Cycle readings, to get better precision.

  • jmgjmg Posts: 15,175
    potatohead wrote: »
    Can't we use one unit to measure pulse, and another to measure duty?

    Seems like that use case is covered by the ability to assign multiple units (POGS) to the task?
    Yes, that is the point - you use two Pin cells (your POGS) - one measures Period, and the other measures HI (or LO) Time.

    Duty, which is a ratio, is then derived from those two readings, by maths in the COG
    (you cannot measure duty directly)

    The detail I am trying to get across here, is when those two pin cells capture, they should do so on the same cycle.

    I think Multiple Pin cells can trigger from the COG now (Chip mentions sync'd starts) , but I do not (yet) see where multiple Pin cells can trigger from a Pin Signal.

  • jmgjmg Posts: 15,175
    rjo__ wrote: »
    This is related and I don't want to bother Chip. So, you seem like the next best option.

    When we are talking about generating a duty cycle... where are the duty@frequency limits?
    For example... as it stands now, can I generate a 48MHz frequency with a 40% duty cycle?

    You can control Frequency and Duty, in a classic (non P1 duty) manner.

    Your example is pushing things a little.

    The new PWM modes have Period and Compare setpoints defined to 1 cycle granularity.

    48MHz at 40% requires
    0.4/48M = 8.333 ns
    0.6/48M = 12.5ns

    That's a difference of 4.167ns, which would be possible with a 240MHz Clock.

  • AribaAriba Posts: 2,690
    jmg wrote: »
    ...
    Key element there is two consecutive reads, so you are not measuring the same cycle, and so lose precision.
    - but you are right, if you can tolerate alternate reading and accept any drift between readings, then you can multiplex the two readings.

    This rapid mode change approach would be worth testing, as there is a chance the enable part way through a Hi, needs to be discarded.
    Rapid alternate also does not address multiple Pin Cycle readings, to get better precision.

    In this special mode the smartpin logic switches between high and low state measuring on every read, so you get the two times from the same cycle. No mode switching from the cog required.
    0% and 100% PWM will be a problem though.

    Andy


  • rjo__rjo__ Posts: 2,114
    Andy,

    Teach a man to fish... and he will eventually get tired of eating fish.

    Not yet though.

    Thanks


  • jmgjmg Posts: 15,175
    Ariba wrote: »
    In this special mode the smartpin logic switches between high and low state measuring on every read, so you get the two times from the same cycle. No mode switching from the cog required.
    0% and 100% PWM will be a problem though.
    Not quite how I read the docs, but let's assume you are correct, & the %10001 does Alternate Capture nicely.

    DOCs also say
    "If states change faster than the cog is able to retrieve measurements, the measurements will effectively be lost, as old ones will be overwritten with new ones."
    so that places some strict caveats on Reading rate, limits more than just 0% and 100%, and also pretty much limits to just ONE channel per COG.
    I see no mention of reading-clobber error flags, so failures will be hard to sense.

    DOCs also suggest using two pin cells as a workaround (now the same as in post #1), but omits any mention of sync of those two cells, which is really what this thread is about.

    Smart pins can be controlled many from one COG in most modes, which is one of their significant features.
    Best to get the details right, so that a single COG can use many-pins with no gotchas, & the best dynamic range.


  • rjo__rjo__ Posts: 2,114
    jmg...I am using a small screen. Thanks for the answer. In all of my scrolling around I thought Andy had provided the answer.

    sorry
  • AribaAriba Posts: 2,690
    The MSB of the result holds the state that was measured, so you can detect a failure if the MSBs of two reads are the same.
    I think you just need to reverse the polarity in this case, so that the first state measures the shorter pulsewidth.

    You can sync the two smart pins by setting the corresponding DIRx bits in the same instruction to start them.

    Andy
  • jmgjmg Posts: 15,175
    Ariba wrote: »
    You can sync the two smart pins by setting the corresponding DIRx bits in the same instruction to start them.
    That is a SysCLK launch sync, which is not quite the same as a Capture Sync.
    The Smart pins have good SysCLK domain Sync and Counts operation, but they are (currently) less well covered in Pin-Signal Sync and Count.

  • cgraceycgracey Posts: 14,209
    jmg wrote: »
    Ariba wrote: »
    You can sync the two smart pins by setting the corresponding DIRx bits in the same instruction to start them.
    That is a SysCLK launch sync, which is not quite the same as a Capture Sync.
    The Smart pins have good SysCLK domain Sync and Counts operation, but they are (currently) less well covered in Pin-Signal Sync and Count.

    So, maybe we need some pin-event option to bring them out of reset.
  • jmgjmg Posts: 15,175
    cgracey wrote: »
    jmg wrote: »
    Ariba wrote: »
    You can sync the two smart pins by setting the corresponding DIRx bits in the same instruction to start them.
    That is a SysCLK launch sync, which is not quite the same as a Capture Sync.
    The Smart pins have good SysCLK domain Sync and Counts operation, but they are (currently) less well covered in Pin-Signal Sync and Count.
    So, maybe we need some pin-event option to bring them out of reset.

    Yes, the Pin-Event changes I would suggest are
    a) Allow Period Counter to be SyCLK or Pin Event clocked. (or Pin-Event Clock Enabled )
    This allows all those Periodic modes, to be configured for some user-set number of Pin-Events.

    b) Add Pin-event trigger, that can apply to more than one pin at a time for sync,


    I think this can be relatively simple - along the lines of a single Series D-FF that has a choice of Clock-Enable

    i) Always clocked (this for SysCLK trigger)
    or
    ii) Pin-Event Clocked. This now triggers on first Pin-Event after the COG 'arms' the Pin cell.

  • cgraceycgracey Posts: 14,209
    edited 2016-04-21 09:22
    I'm redoing the smart pin measuring modes to accommodate N-cycle measurements, not just single-cycle measurements. PINSETX is used to set the number of cycles/periods to measure and Z will accumulate all clock cycles needed to account for the input cycles/periods. There's one mode which accumulates highs during this measurement process, to give you the duty-cycle info. Two modes (%10010, %10011) can be started simultaneously on two different smart pins, focusing on the same input pin, in order to give you total number of clocks in X input cycles, as well as total number of highs. This should realize what JMG has been talking about for so long (the reciprocal counter).
    pad
    MMMMM	Description				OUT	Inputs		Pattern
    --------------------------------------------------------------------------------------------------------
    00000	OUT (default)				OUT	A,B		<none>
    
    00001 *	DAC noise				OUT	A,B		output-update-repeat
    00010 *	DAC 16-bit dither, noise		OUT	A,B		output-update-repeat
    00011 *	DAC 16-bit dither, PWM			OUT	A,B		output-update-repeat
    
    00100 *	pulses				drive	-		wait-output-repeat
    00101 *	edges				drive	-		wait-output-repeat
    00110 *	NCO freq				drive	-		output
    00111 *	NCO duty				drive	-		output
    
    01000 *	PWM triangle 16:16			drive	-		output-update-repeat
    01001 *	PWM sawtooth 16:16			drive	-		output-update-repeat
    01010 *	PWM SMPS, A=V, B=I			drive	A,B		output-update-repeat
    01011 *	A-B quadrature encoder			OUT	A,B		measure-report-loop
    
    01100 *	A-high inc				OUT	A,B		measure-report-loop
    01101 *	A-rise inc				OUT	A,B		measure-report-loop
    01110 *	A-high inc, B-high dec			OUT	A,B		measure-report-loop
    01111 *	A-rise inc, B-rise dec			OUT	A,B		measure-report-loop
    
    10000 *	time A states				OUT	A,B		time-report-loop (!sign = state)
    10001 *	time A high				OUT	A,B		time-report-loop
    10010 *	time X periods A-rise to A-rise		OUT	A,B		time-report-loop
    10011 *	duty X periods A-rise to A-rise		OUT	A,B		time-report-loop
    
    10100 *	time X periods A-rise to B-rise		OUT	A,B		time-report-loop
    10101 *	time X periods A-rise to B-edge		OUT	A,B		time-report-loop
    10110 *	time X periods A-edge to B-rise		OUT	A,B		time-report-loop
    10111 *	time X periods A-edge to B-edge		OUT	A,B		time-report-loop
    
    11000 *	USB host, low-speed			drive	A,B		receive-repeat
    11001 *	USB host, full-speed			drive	A,B		receive-repeat
    11010 *	USB device, low-speed			drive	A,B		receive-repeat
    11011 *	USB device, full-speed			drive	A,B		receive-repeat
    
    11100 *	B-clk tx				drive	B		transmit-repeat
    11101 *	B-clk rx				OUT	A,B		receive-repeat
    11110 *	async tx				drive	-		transmit-repeat
    11111 *	async rx				OUT	A,B		receive-repeat
    

    I got rid of the V-only PWM SMPS mode, since it was redundant, considering that the B-input can be set to the pin's OUT bit, which can just be kept low.
  • jmgjmg Posts: 15,175
    edited 2016-04-21 09:42
    cgracey wrote: »
    I'm redoing the smart pin measuring modes to accommodate N-cycle measurements, not just single-cycle measurements. PINSETX is used to set the number of cycles/periods to measure and Z will accumulate all clock cycles needed to account for the input cycles/periods.

    Cool, so that means the A-Field sets the Clock source (if external), and B-field can set the Capture Source ?

    cgracey wrote: »
    There's one mode which accumulates highs during this measurement process, to give you the duty-cycle info.

    These two modes can be started simultaneously on two different smart pins, focusing on the same input pin, in order to give you total number of clocks in X input cycles, as well as total number of highs.

    This should realize what JMG has been talking about for so long (reciprocal counter):

    I like the sound of that :)

    Two pins (or more) co-operating on a measurement is very useful.

    N-Cycle is easy to define, when you know you have a narrow range of interest.
    However, that is not always the case, and for wide Dynamic Range, (and that includes general libraries) you need a variant means to Capture from Two Pins at the same time, and hold that, until they are both read.
    ie some simple housekeeping logic.

    This choice of 2 operations could also be called 'Hold Until Read' or '(re)Capture Overwrite' modes.
    In Hold Until Read, two(or more) pins are Same-edge Enabled for Capture, and the NEXT external event captures both.
    Any more external events that may arrive, before read, are ignored.

    Once read is done, some time later the SW re-enables Multiple pins, waits for NEXT external event capture, and repeats.
    This feature gives 2 results, from 2 pins that you know are same SysCLK edge based.

    Most small MCUs operate only 'Capture Overwrite' (tho some have small FIFOs) and that gives serious race/aperture problems with higher frequency signals, where you cannot be in two places at once in SW, so what you read, is not same SysCLK derived.
  • cgraceycgracey Posts: 14,209
    jmg wrote: »
    cgracey wrote: »
    I'm redoing the smart pin measuring modes to accommodate N-cycle measurements, not just single-cycle measurements. PINSETX is used to set the number of cycles/periods to measure and Z will accumulate all clock cycles needed to account for the input cycles/periods.

    Cool, so that means the A-Field sets the Clock source (if external), and B-field can set the Capture Source ?

    cgracey wrote: »
    There's one mode which accumulates highs during this measurement process, to give you the duty-cycle info.

    These two modes can be started simultaneously on two different smart pins, focusing on the same input pin, in order to give you total number of clocks in X input cycles, as well as total number of highs.

    This should realize what JMG has been talking about for so long (reciprocal counter):

    I like the sound of that :)

    Two pins (or more) co-operating on a measurement is very useful.

    N-Cycle is easy to define, when you know you have a narrow range of interest.
    However, that is not always the case, and for wide Dynamic Range, (and that includes general libraries) you need a variant means to Capture from Two Pins at the same time, and hold that, until they are both read.
    ie some simple housekeeping logic.

    This choice of 2 operations could also be called 'Hold Until Read' or '(re)Capture Overwrite' modes.
    In Hold Until Read, two(or more) pins are Same-edge Enabled for Capture, and the NEXT external event captures both.
    Any more external events that may arrive, before read, are ignored.

    Once read is done, some time later the SW re-enables Multiple pins, waits for NEXT external event capture, and repeats.
    This feature gives 2 results, from 2 pins that you know are same SysCLK edge based.

    Most small MCUs operate only 'Capture Overwrite' (tho some have small FIFOs) and that gives serious race/aperture problems with higher frequency signals, where you cannot be in two places at once in SW, so what you read, is not same SysCLK derived.

    I can imagine a much richer implementation, in which you give some number of clocks, as a limit of measurement time, and the circuit measures individual periods and duties (highs) for each cycle, adding them into separate period and duty accumulators and incrementing the cycle count, until the measurement time limit is reached, at which point any unfinished period and duty is thrown away and the accumulated periods, duties, and cycle count are returned. We don't have enough stuff to do all that, though. So, we make you pick a number of cycles to measure.
  • jmgjmg Posts: 15,175

    To Summarise the Pin Pair modes,

    This Pair for Duty Cycle measurement, which can be over N(HW preset) or dC (SW read) whole Pinin Cycles.
    MMMMM	Description				OUT	Inputs		Pattern
    --------------------------------------------------------------------------------------------------------
    01100 *	A-high inc				OUT	A,B		measure-report-loop PinCell R
    10010 *	time X periods A-rise to A-rise		OUT	A,B		time-report-loop  PinCell S
    

    and this pairing for Reciprocal Counting, which can be over N(HW preset) or dC (SW read) whole Pinin Cycles.
    MMMMM	Description				OUT	Inputs		Pattern
    --------------------------------------------------------------------------------------------------------
    01101 *	A-rise inc				OUT	A,B		measure-report-loop  PinCell R
    10010 *	time X periods A-rise to A-rise		OUT	A,B		time-report-loop  PinCell S
    


    PinCell R Z reg holds PinIn Cycles (dC) over the Capture time, and PinCell S Z holds the SysCLKs (dT)
    Usually, R & S map to the same PinIn. The P2 limit would be 32 Channels of high precision Frequency Counter, and groups of up to 16 could be sync'd

    Frequency is K*dC/dT, and precision is essentially independent of the Frequency In.
    100MHz resolves to a (10 parts per billion)(second) product.
    1s gives 10 ppb, 10s gives 1ppb, 100ms is 0.1ppm etc

    If dC/dT are lossless (as they are here) you can run multiple time bases, to the numeric limit.


    I see that not mentioned in either 'measure', is the implicit capture that occurs, on A-rise.
    Most MCUs mention Capture, so it would be good to include that wording in the P2.
  • evanhevanh Posts: 16,042
    edited 2016-04-21 13:08
    Chip,
    What's the difference between the new mode 10011 and the similar sounding mode 10001? It seems to me combining mode 10010 with 10001 should achieve what you are proposing.

    EDIT: Ah, comparing to earlier docs I now see the added "X periods" applied to six of the modes. That detail didn't stand out for me.

    Rereading I think I get it now. Although the old modes 10010 + 10001 would've been completely accurate, they also required a Cog to fetch every modulated sample one by one.

    With the new version, one can leave the two Smartpins to keep accumulating whole samples. The two Z's can presumably be examined arbitrarily before the X expiration and contain valid whole running totals.
  • evanhevanh Posts: 16,042
    Mode 10001 might be redundant.
  • jmgjmg Posts: 15,175
    cgracey wrote: »
    jmg wrote: »
    In Hold Until Read, two(or more) pins are Same-edge Enabled for Capture, and the NEXT external event captures both.
    Any more external events that may arrive, before read, are ignored.

    I can imagine a much richer implementation, in which you give some number of clocks, as a limit of measurement time, and the circuit measures individual periods and duties (highs) for each cycle, adding them into separate period and duty accumulators and incrementing the cycle count, until the measurement time limit is reached, at which point any unfinished period and duty is thrown away and the accumulated periods, duties, and cycle count are returned. We don't have enough stuff to do all that, though. So, we make you pick a number of cycles to measure.

    That would need another timer, and I think it is simpler if the Software is used to choose the nominal measurement time.
    Remember, this always snaps to a whole PinIn cycle, so any 'number of clocks' is a minimum.
    The actual SysCLKs taken is read from the Pin Pairs.

    Are there updated & more complete docs on this yet ?
    When X=0, does that mean user has Software Control, as in other modes ?

  • jmgjmg Posts: 15,175
    edited 2016-04-21 20:57
    evanh wrote: »
    With the new version, one can leave the two Smartpins to keep accumulating whole samples.
    Hopefully, yes.
    evanh wrote: »
    The two Z's can presumably be examined arbitrarily before the X expiration and contain valid whole running totals.
    That would be good, and is very close to the Software paced Capture I am seeking, and that I hope Chip has included, in the fine print.

    There are lurking problems with that, 'two Z's can presumably be examined arbitrarily ' and more details are needed than Chip has given so far.

    With two captures, as we have here, you need to be certain both captures are related.
    If software reads one, then possibly the other changes, inside that reading aperture, then you have bad data.

    That's why I've suggested the Hold Until Read action, that allows many-pin (re) init, and then you can read the captured values from many pins.

    I think this needs a single Flag, per PinCell plus a means to set that flag across many PinCells on one SysCLK, and then they clear as each Pin is read, and then SW resets the group, and you repeat. Another flag signals Capture_Done.

    This way you know all readings are good data, and this is simpler than needing some cross-ties between pin-cells.

    Chip has said this "Two modes (%10010, %10011) can be started simultaneously on two different smart pins, focusing on the same input pin", so I think the first part is now implemented, hopefully, the second minor detail is there too ?
  • evanhevanh Posts: 16,042
    jmg wrote: »
    If software reads one, then possibly the other changes, inside that reading aperture, then you have bad data.
    Standard trick is four quick reads instead of two: Read the "frequency" Z accumulator first then the "duty" Z accumulator (First pair of readings), and reread both in same order (Second pair of readings). Compare the two frequency readings. If they're the same then first pair is valid, if they're different then second pair is valid.
    That's why I've suggested the Hold Until Read action, that allows many-pin (re) init, and then you can read the captured values from many pins.
    The results will be held indefinitely at end of expiry. Nothing extra needed.
Sign In or Register to comment.