close #105 pwm remove pin

refactor hwpwm sketch
This commit is contained in:
hathach committed 2019-01-10 16:13:04 +07:00
1 parent 36f707a98a
commit 04c45e13d9
3 files changed
+62 -71

No files matched your search

+35 -14
View File
@@ -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; i<MAX_CHANNELS; i++)
{
//Pin not already configured, but this HardwarePWM is full
return false;
if ( _pwm->PSEL.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));