Pwm.write with positivie npulses not blocking

Hi all,
I am using pwm.write. with npulses=1 in a loop to write a number of pulses to an output pin all with different periods. For testing purposes the values I’m writing add up to 8 seconds (so I can see what’s happening in human time). According to the documentation pwm.write with positive npulses is blocking, so according to my understanding it should pause the program for 8 seconds. However it doesn’t seem to do this. What I get is this.


The loop completed in 21 milliseconds, not the 8000 I was expecting.

Here’s the code.

# test something
# Created at 2020-03-21 01:09:13.603035

###########
import timers
t = timers.timer()
t.start()

import pwm
import streams
streams.serial()
print(" here we go...")
PPMout = D15.PWM
PPMtrig = D14
# D14 is physically connected to D15 with a connecting wire
pinMode(PPMout, OUTPUT_PUSHPULL)
pinMode(PPMtrig, INPUT_PULLUP)
pinMode(LED0, OUTPUT)

chan = [100, 200, 300, 400, 500, 600, 700, 800]

def PPMgen():
    print("PPMgen started at", t.get())
    syncPulse = 0
    for val in chan:
        syncPulse += val + 300
        pwm.write(PPMout, val+300, 300, MILLIS, 1)
        print("chan val", val)
    pwm.write(PPMout, 8000-syncPulse, 300, MILLIS, 1)
    print("syncPulse", 8000-syncPulse)
    print("Leaving PPMgen at", t.get())

while True:
    print ("entering loop at", t.get())
    PPMgen()
    digitalWrite(LED0, digitalRead(PPMout))
    sleep(100)
    print ("back in main loop after PPMgen at", t.get())

All advice and guidance gratefully received.
:wink:
PS I have this working fine using a Timer, but unfortunately timers only work in milliseconds, and a I need microseconds.

Since npulses are implemented for St nuclleo F401, It should be blocking given the write amount of time.
Let me check it and get back to you.

In the meantime, If you’re looking for Timers in MICROS, There is a workaround to have polling timer in Microseconds until the MICROS get implemented for you board.

you could implement a polling sleep by means of the vosTicks() function which returns a pointer to the number of clock ticks.
In a c file, Make function with a while code that loops until the right amount of vosTicks pass.
And call this function from a python function.

uint32_t start_ticks = *vosTicks();
while (*vosTicks() - start_ticks < (ticks_for_micros * micros_to_sleep));

But you should make an evaluation of the right ticks_for_micros value

Let me know what you think

Thanks @karimhamdy1. For this application each “frame” (sequence of pulses plus a “sync” pulse) has a fixed length, 22 milliseconds, so I am using a standard timer to trigger the pwm.write. Or trying to. I want to run the pwm.write in a separate thread (to get around the blocking issue…) and trying to use the “Event()” function of threading to make it all work. Still experimenting with that however. I haven’t been able to find any examples of Event() though, nor Locking, Semaphore or Condition, so there’s a bit of trial and error involved. :wink:

BTW Stumbled across a function, sleep_micros(), in hwtimers. Seems to work. Looks like thre is one_shot function hidden in there too, but I haven’t tried it.
:wink: