SmingHub / Sming

Sming - powerful open source framework simplifying the creation of embedded C++ applications.
https://sming.readthedocs.io
GNU Lesser General Public License v3.0
1.48k stars 348 forks source link

PWM unstable/flickering #70

Closed zigarrre closed 9 years ago

zigarrre commented 9 years ago

When driving a LED with PWM it flickers. This is probably caused by timing issues in the PWM implementation which cause a varying duty cycle. The lower the duty cycle is set the more drastic will be the flickering.

Minimum sample to reproduce the problem:

#include <user_config.h>
#include <SmingCore/SmingCore.h>

void init()
{
        analogWrite(2, 50);
}

LED connected between GPIO2 and GND.

tprochazka commented 9 years ago

Here is interesting blog post about PWM http://tech.scargill.net/esp8266-pwm-on-one-output/

tprochazka commented 9 years ago

Has ESP real HW PWM on some pin or everything is only emulated?

I tried it and flickering is minor problem, much bigger problem is that ESP is completely unresponsive during PWM.

zigarrre commented 9 years ago

The ESP has a hardware PWM peripheral (on pins 12-15, which the link you posted is using) but the PWM implementation in Sming is software PWM. The problem is the suboptimal implementation in Sming.

The software PWM implementation in NodeMCU (which i'm using now) works perfectly fine without any flickering, even at the lowest duty cycle.

tprochazka commented 9 years ago

According https://github.com/esp8266/Arduino/issues/424#issuecomment-111876468 ESP has no HW support for PWM

tprochazka commented 9 years ago

SDK 1.1.1 has some new driver for PWM http://bbs.espressif.com/viewtopic.php?f=5&t=554

AutomationD commented 9 years ago

@zigarrre can you please see if 1.1.1 fixes that? Thank you!

hreintke commented 9 years ago

@kireevco : I don't think sming uses the new PWM drivers yet. They are in libpwm.a and that is not used by sming

zigarrre commented 9 years ago

@kireevco @hreintke : hreintke is right, Sming uses it's own software PWM implementation (see https://github.com/anakod/Sming/blob/master/Sming/SmingCore/PWM.cpp).

anakod commented 9 years ago

Current PWM implementation needs update. Good example: https://github.com/sh4d0w12/esp8266_IRremote/pull/1

hrsavla commented 9 years ago

@anakod Sming's PWM.cpp is using ostimer. Bellow is written in latest SDK 1.2 documentation on Page 15.

"Please be noted that os_timer APIs listed below are software timer, they are executed in task, so timer callback may not be precisely executed at the right time, it depends on priority. If you need a precise timer, please use hardware timer which will be executed in hardware interrupt, refer to hw_timer.c. "

Also latest SDK PWM api seems to be using hw_timer. (bellow is from page18)

"APIs in hw_timer.c can not be called when PWM APIs are using, because they are using the same hardware timer."

So we should make change Sming PWM to use hw_timer. I think this will solve "Flicker" problem.

AutomationD commented 9 years ago

@hrsavla do you think you could add that change via PR?

crosofg commented 9 years ago

Is flickering problem solved?

hrsavla commented 9 years ago

Software PWM generated in Sming will generate flickers as timers are of not higher priority and some Network routines will interrupt it. Solution is to use ESP sdk pwm api or write whole new PWM class using hardware timer. I am able to make Esp sdk PWM to work with sming. I still need to format the code in Sming way Class files. I will do it and make a PR within 6 hours. (I will let you know).

crosofg commented 9 years ago

@hrsavla Did you remove your implementation of hw pwm?

hrsavla commented 9 years ago

I am resubmitting it in an hour as per git flow. On 6 Sep 2015 12:08, "crosofg" notifications@github.com wrote:

@hrsavla https://github.com/hrsavla Did you remove your implementation of hw pwm?

— Reply to this email directly or view it on GitHub https://github.com/anakod/Sming/issues/70#issuecomment-138047044.

crosofg commented 9 years ago

How can I combine two features? I want to implement rboot and Hw pwm. what changes needs to be done?

hreintke commented 9 years ago

In progress. See PR #375