diff --git a/cores/nRF5/HardwarePWM.cpp b/cores/nRF5/HardwarePWM.cpp index 488309b1..01da6980 100644 --- a/cores/nRF5/HardwarePWM.cpp +++ b/cores/nRF5/HardwarePWM.cpp @@ -51,6 +51,7 @@ HardwarePWM::HardwarePWM(NRF_PWM_Type* pwm) arrclr(_seq0); _resolution = 8; + _clock_div = PWM_PRESCALER_PRESCALER_DIV_1; // 16 Mhz } #if 0 @@ -90,8 +91,8 @@ void HardwarePWM::setResolution(uint8_t bitnum) void HardwarePWM::setClockDiv(uint8_t div) { - div = min8(div, PWM_PRESCALER_PRESCALER_DIV_128); - _pwm->PRESCALER = div; + _clock_div = min8(div, PWM_PRESCALER_PRESCALER_DIV_128); + _pwm->PRESCALER = _clock_div; } /** @@ -137,7 +138,7 @@ void HardwarePWM::begin(void) // Initialize Registers _pwm->MODE = PWM_MODE_UPDOWN_Up; _pwm->COUNTERTOP = bit(_resolution) - 1; // default is 8 bit, can be configured before begin() - _pwm->PRESCALER = PWM_PRESCALER_PRESCALER_DIV_1; // 16MHz + _pwm->PRESCALER = _clock_div; _pwm->DECODER = PWM_DECODER_LOAD_Individual; _pwm->LOOP = 0; diff --git a/cores/nRF5/HardwarePWM.h b/cores/nRF5/HardwarePWM.h index 9e7b9467..434a8540 100644 --- a/cores/nRF5/HardwarePWM.h +++ b/cores/nRF5/HardwarePWM.h @@ -49,6 +49,7 @@ class HardwarePWM uint16_t _seq0[MAX_CHANNELS]; uint8_t _resolution; + uint8_t _clock_div; void _start(void); diff --git a/cores/nRF5/rtos.cpp b/cores/nRF5/rtos.cpp index d33569af..dbfecee1 100644 --- a/cores/nRF5/rtos.cpp +++ b/cores/nRF5/rtos.cpp @@ -88,7 +88,12 @@ extern "C" void vApplicationIdleHook( void ) { - waitForEvent(); + // Enter low-power mode will turn of hardware PWM + // Only go there if no PWM device is active. + if ( !(PWM0.begun() || PWM1.begun() || PWM2.begun() ) ) + { + waitForEvent(); + } } } diff --git a/cores/nRF5/wiring_analog.cpp b/cores/nRF5/wiring_analog.cpp index 172af117..ec6646b4 100644 --- a/cores/nRF5/wiring_analog.cpp +++ b/cores/nRF5/wiring_analog.cpp @@ -69,9 +69,6 @@ void analogWrite( uint32_t pin, uint32_t value ) // If pin is added (already) in this group if ( PWMx[i]->addPin(pin) ) { - // begin if not previously - if ( !PWMx[i]->begun() ) PWMx[i]->begin(); - PWMx[i]->writePin(pin, value); return; }