Page 1 of 1

Pulse Generation

Posted: Mon Sep 01, 2014 9:04 pm
by nelfata
Hi,
is there any way to generate microsecond pulses on a GPIO line using MP where the pulse width could be configurable in multiples of micro seconds?
The minimum is one pulse.

I have an idea to use the timer callback to trigger a GPIO line but I am not sure if the processing is fast enough to do that.

Any thoughts?

Re: Pulse Generation

Posted: Mon Sep 01, 2014 9:38 pm
by dhylands
What are you trying to do?

Do you need a series of pulses of varying widths?

I've got some code for working with the HW timers here: Using the code on PR #815 https://github.com/micropython/micropython/pull/815 but this is really designed for sending same-width pulses.
I've got the basics working on teensy, and when I get IC, PWM, and OC all working, I'll be trying to get the whole thing pushed.

Re: Pulse Generation

Posted: Mon Sep 01, 2014 11:48 pm
by nelfata
Interesting, I think your implementation could work.
I am using the AD8555 chip and part of its configuration requires to have short pulses for 0 (50ns to 10usec), long pulses for 1 (longer than 10usec) with at least 10usec between pulses.
It may work but I have to see in more detail what you've done.

Re: Pulse Generation

Posted: Tue Sep 02, 2014 4:02 am
by dhylands
So for that type of application, I'd be inclined to use SPI and set it up so that you're using a 1.5 MHz clock. Then a single 1 bit will be about 6.7 usec, and 2 1 bits will be about 13.3 usec wide.

Re: Pulse Generation

Posted: Tue Sep 02, 2014 11:51 am
by nelfata
The SPI pin I need is already in use on the board, but thank you for the idea, a good one.
I will have to bit bang it in C then.
Is there a usec delay routine within the MP C code?

Re: Pulse Generation

Posted: Tue Sep 02, 2014 3:31 pm
by dhylands
There is a pyb_udelay in stmhal/modpyb.c (but it's currently only exposed to python)

And there is sys_tick_get_microseconds which could easily be wrapped into a delay.

From python, you have pyb.micros() (which queries the microsecond counter) and pyb.udelay (which delays for a given number of microseconds)

The problem will be that you need to disable interrupts in order to guarantee a short pulse.
If your long pulses and the time between pulses can be arbitrarily long then you'd only have to disable interrupts while generating the short pulse.

50 nsec is approx 9 CPU instructions (at 168 MHz), so you don't need to delay at all when creating a low pulse, just make the pin high and immediately make it low.

Re: Pulse Generation

Posted: Tue Sep 02, 2014 4:58 pm
by blmorris
Would it make sense to use assembler to create pulses where you can count exactly how many instruction cycles the pulse lasts?

Re: Pulse Generation

Posted: Tue Sep 02, 2014 5:29 pm
by nelfata
Disabling interrupts is not a big problem due to the fact that the duration is less than 10usec.
Assembly is the most optimal but no longer portable.