tenbaht / sduino

An Arduino-like programming API for the STM8
http://tenbaht.github.io/sduino/
GNU Lesser General Public License v2.1
349 stars 213 forks source link

digitalWrite doesn't work after analogWrite #72

Closed Gei0r closed 5 years ago

Gei0r commented 5 years ago

I have an issue with analogWrite() on the stm8blue board. A call to analogWrite() with 255 should set the pin constantly to HIGH (disabling PWM). But that only works sometimes if there was another analog (PWM) value written there before:

analogWrite(2, 1);
analogWrite(2, 255);  // pin permanently stays off almost always

analogWrite(2, 64);
analogWrite(2, 255);  // works about 25 % of the time, pin stays off otherwise

analogWrite(2, 128);
analogWrite(2, 255);  // works about 50 % of the time, pin stays off otherwise

analogWrite(2, 192);
analogWrite(2, 255);  // works about 75 % of the time, pin stays off otherwise

analogWrite(2, 254);
analogWrite(2, 255);  // works almost 100 % of the time

Exchanging the second analogWrite() with digitalWrite() doesn't make a difference.

It seems that the pin gets "stuck at LOW" if the PWM is disabled while the pin is driven low by PWM.

Maybe turnOffPWM() in wiring_digital.c is insufficient?

static void turnOffPWM(uint8_t timer)
{
    *((unsigned char *) ccmrx[timer-1]) &= ~TIM1_CCMR_OCM;
}
tenbaht commented 5 years ago

Thank you for the report. I will have a look at this after new year's.

Gei0r commented 5 years ago

I took a look into the datasheet myself. As I suspected, the code in turnOffPwm() only "freezes" the timer comparator, but it does not disable the PWM output driver.

For that, CCiE / CCiNE in CCER need to be cleared. It's fixed in the referenced pull request.

datasheet