Pulse Generation

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

Pulse Generation

Post by nelfata » Mon Sep 01, 2014 9:04 pm

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?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Pulse Generation

Post by dhylands » Mon Sep 01, 2014 9:38 pm

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.

nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

Re: Pulse Generation

Post by nelfata » Mon Sep 01, 2014 11:48 pm

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.

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Pulse Generation

Post by dhylands » Tue Sep 02, 2014 4:02 am

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.

nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

Re: Pulse Generation

Post by nelfata » Tue Sep 02, 2014 11:51 am

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?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: Pulse Generation

Post by dhylands » Tue Sep 02, 2014 3:31 pm

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.

blmorris
Posts: 348
Joined: Fri May 02, 2014 3:43 pm
Location: Massachusetts, USA

Re: Pulse Generation

Post by blmorris » Tue Sep 02, 2014 4:58 pm

Would it make sense to use assembler to create pulses where you can count exactly how many instruction cycles the pulse lasts?

nelfata
Posts: 74
Joined: Wed Apr 30, 2014 10:50 pm

Re: Pulse Generation

Post by nelfata » Tue Sep 02, 2014 5:29 pm

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.

Post Reply