add readPin() and readChannel() to hw pwm
ported Servo library (not tested)
This commit is contained in:
1 parent
f5e23fdaba
commit
739f98b683
3 files changed
+71
-14
No files matched your search
@@ -166,3 +166,18 @@ bool HardwarePWM::writePin(uint8_t pin, uint16_t value, bool inverted)
|
||||
|
||||
return writeChannel(ch, value, inverted);
|
||||
}
|
||||
|
||||
uint16_t HardwarePWM::readPin(uint8_t pin)
|
||||
{
|
||||
int ch = pin2channel(pin);
|
||||
VERIFY( ch >= 0, 0);
|
||||
|
||||
return readChannel(ch);
|
||||
}
|
||||
|
||||
uint16_t HardwarePWM::readChannel(uint8_t ch)
|
||||
{
|
||||
// remove inverted bit
|
||||
return (_seq0[ch] & 0x7FFF);
|
||||
}
|
||||
|
||||
@@ -39,6 +39,8 @@
|
||||
#include "common_inc.h"
|
||||
#include "nrf.h"
|
||||
|
||||
#define HWPWM_MODULE_NUM 3
|
||||
|
||||
class HardwarePWM
|
||||
{
|
||||
private:
|
||||
@@ -82,10 +84,12 @@ class HardwarePWM
|
||||
void stop (void);
|
||||
|
||||
// Generate PWM
|
||||
bool writePin (uint8_t pin, uint16_t value, bool inverted = false);
|
||||
bool writeChannel(uint8_t ch , uint16_t value, bool inverted = false);
|
||||
bool writePin (uint8_t pin, uint16_t value, bool inverted = false);
|
||||
bool writeChannel(uint8_t ch , uint16_t value, bool inverted = false);
|
||||
|
||||
// void printInfo(void);
|
||||
// Read current set value
|
||||
uint16_t readPin (uint8_t pin);
|
||||
uint16_t readChannel (uint8_t ch);
|
||||
};
|
||||
|
||||
extern HardwarePWM HwPWM0;
|
||||
|
||||
@@ -28,14 +28,14 @@ uint8_t ServoCount = 0; // the total number
|
||||
|
||||
|
||||
|
||||
uint32_t group_pins[3][NRF_PWM_CHANNEL_COUNT]={{NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED}, {NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED}, {NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED}};
|
||||
static uint16_t seq_values[3][NRF_PWM_CHANNEL_COUNT]={{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
|
||||
//uint32_t group_pins[3][NRF_PWM_CHANNEL_COUNT]={{NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED}, {NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED}, {NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED, NRF_PWM_PIN_NOT_CONNECTED}};
|
||||
//static uint16_t seq_values[3][NRF_PWM_CHANNEL_COUNT]={{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
|
||||
|
||||
Servo::Servo()
|
||||
{
|
||||
if (ServoCount < MAX_SERVOS) {
|
||||
this->servoIndex = ServoCount++; // assign a servo index to this instance
|
||||
} else {
|
||||
this->servoIndex = ServoCount++; // assign a servo index to this instance
|
||||
} else {
|
||||
this->servoIndex = INVALID_SERVO; // too many servos
|
||||
}
|
||||
|
||||
@@ -55,13 +55,26 @@ uint8_t Servo::attach(int pin, int min, int max)
|
||||
pinMode(pin, OUTPUT); // set servo pin to output
|
||||
servos[this->servoIndex].Pin.nbr = pin;
|
||||
|
||||
if(min < servo_min) min = servo_min;
|
||||
if (max > servo_max) max = servo_max;
|
||||
this->min = min;
|
||||
if(min < servo_min) min = servo_min;
|
||||
if (max > servo_max) max = servo_max;
|
||||
this->min = min;
|
||||
this->max = max;
|
||||
|
||||
servos[this->servoIndex].Pin.isActive = true;
|
||||
|
||||
|
||||
servos[this->servoIndex].Pin.isActive = true;
|
||||
|
||||
// Adafruit, add pin to 1 of available Hw PWM
|
||||
for(int i=0; i<HWPWM_MODULE_NUM; i++)
|
||||
{
|
||||
if ( HwPWMx[i]->addPin(pin) )
|
||||
{
|
||||
// 20ms - 50Hz
|
||||
HwPWMx[i]->setMaxValue(2500);
|
||||
HwPWMx[i]->setClockDiv(PWM_PRESCALER_PRESCALER_DIV_128);
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
return this->servoIndex;
|
||||
}
|
||||
@@ -69,6 +82,8 @@ uint8_t Servo::attach(int pin, int min, int max)
|
||||
void Servo::detach()
|
||||
{
|
||||
servos[this->servoIndex].Pin.isActive = false;
|
||||
|
||||
// TODO Adafruit remove pin from HW PWM
|
||||
}
|
||||
|
||||
|
||||
@@ -86,6 +101,7 @@ void Servo::write(int value)
|
||||
|
||||
void Servo::writeMicroseconds(int value)
|
||||
{
|
||||
#if 0
|
||||
uint8_t channel, instance;
|
||||
uint8_t pin = servos[this->servoIndex].Pin.nbr;
|
||||
//instance of pwm module is MSB - look at VWariant.h
|
||||
@@ -109,6 +125,14 @@ void Servo::writeMicroseconds(int value)
|
||||
nrf_pwm_sequence_set(PWMInstance, 0, &seq);
|
||||
nrf_pwm_loop_set(PWMInstance, 0UL);
|
||||
nrf_pwm_task_trigger(PWMInstance, NRF_PWM_TASK_SEQSTART0);
|
||||
#else
|
||||
uint8_t pin = servos[this->servoIndex].Pin.nbr;
|
||||
|
||||
for(int i=0; i<HWPWM_MODULE_NUM; i++)
|
||||
{
|
||||
if ( HwPWMx[i]->writePin(pin, value) ) break;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
int Servo::read() // return the value as degrees
|
||||
@@ -118,12 +142,26 @@ int Servo::read() // return the value as degrees
|
||||
|
||||
int Servo::readMicroseconds()
|
||||
{
|
||||
#if 0
|
||||
uint8_t channel, instance;
|
||||
uint8_t pin=servos[this->servoIndex].Pin.nbr;
|
||||
instance=(g_APinDescription[pin].ulPWMChannel & 0xF0)/16;
|
||||
channel=g_APinDescription[pin].ulPWMChannel & 0x0F;
|
||||
// remove the 16th bit we added before
|
||||
return seq_values[instance][channel] & 0x7FFF;
|
||||
#else
|
||||
uint8_t pin=servos[this->servoIndex].Pin.nbr;
|
||||
|
||||
for(int i=0; i<HWPWM_MODULE_NUM; i++)
|
||||
{
|
||||
if ( HwPWMx[i]->checkPin(pin) )
|
||||
{
|
||||
return HwPWMx[i]->readPin(pin);
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
bool Servo::attached()
|
||||
@@ -131,4 +169,4 @@ bool Servo::attached()
|
||||
return servos[this->servoIndex].Pin.isActive;
|
||||
}
|
||||
|
||||
#endif // ARDUINO_ARCH_NRF52
|
||||
#endif // ARDUINO_ARCH_NRF52
|
||||
Reference in new issue
Block a user