diff --git a/cores/nRF5/HardwarePWM.cpp b/cores/nRF5/HardwarePWM.cpp index 00668dff..881c054d 100644 --- a/cores/nRF5/HardwarePWM.cpp +++ b/cores/nRF5/HardwarePWM.cpp @@ -56,12 +56,13 @@ HardwarePWM* HwPWMx[] = HardwarePWM::HardwarePWM(NRF_PWM_Type* pwm) { _pwm = pwm; - - _count = 0; arrclr(_seq0); _max_value = 255; _clock_div = PWM_PRESCALER_PRESCALER_DIV_1; // 16 Mhz + + // FIXME workaround to fix bootloader 0.2.6 does not clean up PSEL[1] of PWM0 + _pwm->PSEL.OUT[1] = 0xFFFFFFFFUL; } void HardwarePWM::setResolution(uint8_t bitnum) @@ -88,19 +89,21 @@ void HardwarePWM::setClockDiv(uint8_t div) */ bool HardwarePWM::addPin(uint8_t pin) { - VERIFY( _count <= MAX_CHANNELS ); + // succeed if pin is already configured + if ( pin2channel(pin) >= 0 ) return true; - // Check if pin is already configured - for(uint8_t i=0; i<_count; i++) - { - if (_pwm->PSEL.OUT[i] == pin) return true; - } + int ch = -1; - if ((_count >= MAX_CHANNELS)) + // find free slot which is not connected + for(int i=0; iPSEL.OUT[i] & PWM_PSEL_OUT_CONNECT_Msk ) + { + ch = i; + break; + } } + VERIFY ( ch >= 0 ); pinMode(pin, OUTPUT); digitalWrite(pin, LOW); @@ -109,17 +112,35 @@ bool HardwarePWM::addPin(uint8_t pin) if ( enabled() ) { _pwm->ENABLE = 0; - _pwm->PSEL.OUT[_count++] = g_ADigitalPinMap[pin]; + _pwm->PSEL.OUT[ch] = g_ADigitalPinMap[pin]; _pwm->ENABLE = 1; _start(); }else { - _pwm->PSEL.OUT[_count++] = g_ADigitalPinMap[pin]; + _pwm->PSEL.OUT[ch] = g_ADigitalPinMap[pin]; } return true; } +bool HardwarePWM::removePin(uint8_t pin) +{ + int ch = pin2channel(pin); + VERIFY( ch >= 0 ); + + bool const en = enabled(); + + // Must disable before changing PSEL + if ( en ) _pwm->ENABLE = 0; + + _pwm->PSEL.OUT[ch] = 0xFFFFFFFFUL; + _seq0[ch] = 0; + + if ( en ) _pwm->ENABLE = 1; + + return true; +} + bool HardwarePWM::enabled (void) { return _pwm->ENABLE; @@ -163,7 +184,7 @@ void HardwarePWM::stop(void) bool HardwarePWM::writeChannel(uint8_t ch, uint16_t value, bool inverted ) { - VERIFY( ch < _count ); + VERIFY( ch < MAX_CHANNELS ); _seq0[ch] = value | (inverted ? 0 : bit(15)); diff --git a/cores/nRF5/HardwarePWM.h b/cores/nRF5/HardwarePWM.h index e58b3d24..bf2691e3 100644 --- a/cores/nRF5/HardwarePWM.h +++ b/cores/nRF5/HardwarePWM.h @@ -51,7 +51,6 @@ class HardwarePWM enum { MAX_CHANNELS = 4 }; // Max channel per group NRF_PWM_Type* _pwm; - uint8_t _count; uint16_t _seq0[MAX_CHANNELS]; uint16_t _max_value; @@ -69,9 +68,12 @@ class HardwarePWM void setClockDiv(uint8_t div); // value is PWM_PRESCALER_PRESCALER_DIV_x, DIV1 is 16Mhz bool addPin (uint8_t pin); + bool removePin (uint8_t pin); + int pin2channel(uint8_t pin) { - for(int i=0; i<_count; i++) + pin = g_ADigitalPinMap[pin]; + for(int i=0; iPSEL.OUT[i] == pin ) return i; } diff --git a/libraries/Bluefruit52Lib/examples/Hardware/hwpwm/hwpwm.ino b/libraries/Bluefruit52Lib/examples/Hardware/hwpwm/hwpwm.ino index 943eba19..93bf6ccd 100644 --- a/libraries/Bluefruit52Lib/examples/Hardware/hwpwm/hwpwm.ino +++ b/libraries/Bluefruit52Lib/examples/Hardware/hwpwm/hwpwm.ino @@ -13,56 +13,39 @@ *********************************************************************/ /* - * This sketch use all 12 channels of 3 Hardware PWM. - * Also change the resolution to max 15-bit. - * Each PWM Hardware group (4 pin) also run with different frequency + * This sketch use different Hardware PWMs for LED Blue and Red + * running with different frequency * - PWM0 : clock/1 ~ 16Mhz - * - PWM1 : clock/4 ~ 4Mhz - * - PMW2 : clock/16 ~ 1Mhz - * - * Since LED RED and BLUE are on different Hardware PWM, - * LED BLUE will blink (due to lower freq) while fading while LED RED - * looks more solid. Furthermore LED RED group write value (PWM1) is inverted + * - PWM1 : clock/16 ~ 1Mhz + * + * While LED RED looks solid, LED BLUE will blink while fading + * (due to its lower freq). Furthermore LED RED is inverted * compared to LED BLUE (PWM2) --> They fade in opposite direction. */ #include -// Maximum 12 pins can be used for 3 PWM modules ( 4 channel each ) -// Note: nRF52832 has 3 PWM modules, nRF52840 has 4 PWM modules -int pins[12] = -{ - A0 , A1 , A2 , A3, - A4 , A5 , A6 , LED_RED, /* avoid A7 (VBAT) */ - 27 , LED_BLUE, PIN_WIRE_SDA, PIN_WIRE_SCL -}; - /**************************************************************************/ /*! @brief The setup function runs once when reset the board */ /**************************************************************************/ void setup() -{ - // Add 4 pins into a group - // It is better to add Pin before call .begin() - for (int i=0; i<12; i++) - { - HwPWMx[i/4]->addPin( pins[i] ); - } +{ + // Add LED RED to PWM0 + HwPWM0.addPin( LED_RED ); - // Enable all 3 PWM modules with 15-bit resolutions(max) but different clock div + // Add LED BLUE to PWM1 + HwPWM1.addPin( LED_BLUE ); + + // Enable PWM modules with 15-bit resolutions(max) but different clock div HwPWM0.begin(); HwPWM0.setResolution(15); - HwPWM0.setClockDiv(PWM_PRESCALER_PRESCALER_DIV_1); // default : freq = 16Mhz + HwPWM0.setClockDiv(PWM_PRESCALER_PRESCALER_DIV_1); // freq = 16Mhz HwPWM1.begin(); HwPWM1.setResolution(15); - HwPWM1.setClockDiv(PWM_PRESCALER_PRESCALER_DIV_4); // default : freq = 4Mhz - - HwPWM2.begin(); - HwPWM2.setResolution(15); - HwPWM2.setClockDiv(PWM_PRESCALER_PRESCALER_DIV_16); // default : freq = 1Mhz + HwPWM1.setClockDiv(PWM_PRESCALER_PRESCALER_DIV_16); // freq = 1Mhz } /**************************************************************************/ @@ -73,39 +56,24 @@ void setup() void loop() { const int maxValue = bit(15) - 1; - bool inverted; // fade in from min to max - // inverted PWM0 (false), PWM1 (true), PWM2 (false) for (int fadeValue = 0 ; fadeValue <= maxValue; fadeValue += 1024) { - inverted = false; - - for (int i=0; i<12; i++) - { - // Inverted for each PWM group - if (i%4) inverted = !inverted; - - HwPWMx[i/4]->writePin( pins[i], fadeValue, inverted); - } - + // Write same value but inverted for Led Blue + HwPWM0.writePin(LED_RED, fadeValue, false); + HwPWM1.writePin(LED_BLUE, fadeValue, true); + // wait for 30 milliseconds to see the dimming effect delay(30); } // fade out from max to min - // inverted PWM0 (false), PWM1 (true), PWM2 (false) for (int fadeValue = maxValue ; fadeValue >= 0; fadeValue -= 1024) - { - inverted = false; - - for (int i=0; i<12; i++) - { - // Inverted for each PWM group - if (i%4) inverted = !inverted; - - HwPWMx[i/4]->writePin( pins[i], fadeValue, inverted); - } + { + // Write same value but inverted for Led Blue + HwPWM0.writePin(LED_RED, fadeValue, false); + HwPWM1.writePin(LED_BLUE, fadeValue, true); // wait for 30 milliseconds to see the dimming effect delay(30);