mirror of https://github.com/ARMmbed/mbed-os.git
pwmout - LPC176X - add read methods for period and pulsewidth
parent
7270f296d3
commit
ea6e806a31
|
@ -29,12 +29,12 @@ static const PinMap PinMap_PWM[] = {
|
||||||
{P1_23, PWM_4, 2},
|
{P1_23, PWM_4, 2},
|
||||||
{P1_24, PWM_5, 2},
|
{P1_24, PWM_5, 2},
|
||||||
{P1_26, PWM_6, 2},
|
{P1_26, PWM_6, 2},
|
||||||
{P2_0 , PWM_1, 1},
|
{P2_0, PWM_1, 1},
|
||||||
{P2_1 , PWM_2, 1},
|
{P2_1, PWM_2, 1},
|
||||||
{P2_2 , PWM_3, 1},
|
{P2_2, PWM_3, 1},
|
||||||
{P2_3 , PWM_4, 1},
|
{P2_3, PWM_4, 1},
|
||||||
{P2_4 , PWM_5, 1},
|
{P2_4, PWM_5, 1},
|
||||||
{P2_5 , PWM_6, 1},
|
{P2_5, PWM_6, 1},
|
||||||
{P3_25, PWM_2, 3},
|
{P3_25, PWM_2, 3},
|
||||||
{P3_26, PWM_3, 3},
|
{P3_26, PWM_3, 3},
|
||||||
{NC, NC, 0}
|
{NC, NC, 0}
|
||||||
|
@ -54,77 +54,84 @@ __IO uint32_t *PWM_MATCH[] = {
|
||||||
|
|
||||||
static unsigned int pwm_clock_mhz;
|
static unsigned int pwm_clock_mhz;
|
||||||
|
|
||||||
void pwmout_init(pwmout_t* obj, PinName pin) {
|
void pwmout_init(pwmout_t *obj, PinName pin)
|
||||||
|
{
|
||||||
// determine the channel
|
// determine the channel
|
||||||
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
|
PWMName pwm = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
|
||||||
MBED_ASSERT(pwm != (PWMName)NC);
|
MBED_ASSERT(pwm != (PWMName)NC);
|
||||||
|
|
||||||
obj->pwm = pwm;
|
obj->pwm = pwm;
|
||||||
obj->MR = PWM_MATCH[pwm];
|
obj->MR = PWM_MATCH[pwm];
|
||||||
|
|
||||||
// ensure the power is on
|
// ensure the power is on
|
||||||
LPC_SC->PCONP |= 1 << 6;
|
LPC_SC->PCONP |= 1 << 6;
|
||||||
|
|
||||||
// ensure clock to /4
|
// ensure clock to /4
|
||||||
LPC_SC->PCLKSEL0 &= ~(0x3 << 12); // pclk = /4
|
LPC_SC->PCLKSEL0 &= ~(0x3 << 12); // pclk = /4
|
||||||
LPC_PWM1->PR = 0; // no pre-scale
|
LPC_PWM1->PR = 0; // no pre-scale
|
||||||
|
|
||||||
// ensure single PWM mode
|
// ensure single PWM mode
|
||||||
LPC_PWM1->MCR = 1 << 1; // reset TC on match 0
|
LPC_PWM1->MCR = 1 << 1; // reset TC on match 0
|
||||||
|
|
||||||
// enable the specific PWM output
|
// enable the specific PWM output
|
||||||
LPC_PWM1->PCR |= 1 << (8 + pwm);
|
LPC_PWM1->PCR |= 1 << (8 + pwm);
|
||||||
|
|
||||||
pwm_clock_mhz = SystemCoreClock / 4000000;
|
pwm_clock_mhz = SystemCoreClock / 4000000;
|
||||||
|
|
||||||
// default to 20ms: standard for servos, and fine for e.g. brightness control
|
// default to 20ms: standard for servos, and fine for e.g. brightness control
|
||||||
pwmout_period_ms(obj, 20);
|
pwmout_period_ms(obj, 20);
|
||||||
pwmout_write (obj, 0);
|
pwmout_write(obj, 0);
|
||||||
|
|
||||||
// Wire pinout
|
// Wire pinout
|
||||||
pinmap_pinout(pin, PinMap_PWM);
|
pinmap_pinout(pin, PinMap_PWM);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pwmout_free(pwmout_t* obj) {
|
void pwmout_free(pwmout_t *obj)
|
||||||
|
{
|
||||||
// [TODO]
|
// [TODO]
|
||||||
}
|
}
|
||||||
|
|
||||||
void pwmout_write(pwmout_t* obj, float value) {
|
void pwmout_write(pwmout_t *obj, float value)
|
||||||
|
{
|
||||||
if (value < 0.0f) {
|
if (value < 0.0f) {
|
||||||
value = 0.0;
|
value = 0.0;
|
||||||
} else if (value > 1.0f) {
|
} else if (value > 1.0f) {
|
||||||
value = 1.0;
|
value = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// set channel match to percentage
|
// set channel match to percentage
|
||||||
uint32_t v = (uint32_t)((float)(LPC_PWM1->MR0) * value);
|
uint32_t v = (uint32_t)((float)(LPC_PWM1->MR0) * value);
|
||||||
|
|
||||||
// workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout
|
// workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout
|
||||||
if (v == LPC_PWM1->MR0) {
|
if (v == LPC_PWM1->MR0) {
|
||||||
v++;
|
v++;
|
||||||
}
|
}
|
||||||
|
|
||||||
*obj->MR = v;
|
*obj->MR = v;
|
||||||
|
|
||||||
// accept on next period start
|
// accept on next period start
|
||||||
LPC_PWM1->LER |= 1 << obj->pwm;
|
LPC_PWM1->LER |= 1 << obj->pwm;
|
||||||
}
|
}
|
||||||
|
|
||||||
float pwmout_read(pwmout_t* obj) {
|
float pwmout_read(pwmout_t *obj)
|
||||||
|
{
|
||||||
float v = (float)(*obj->MR) / (float)(LPC_PWM1->MR0);
|
float v = (float)(*obj->MR) / (float)(LPC_PWM1->MR0);
|
||||||
return (v > 1.0f) ? (1.0f) : (v);
|
return (v > 1.0f) ? (1.0f) : (v);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pwmout_period(pwmout_t* obj, float seconds) {
|
void pwmout_period(pwmout_t *obj, float seconds)
|
||||||
|
{
|
||||||
pwmout_period_us(obj, seconds * 1000000.0f);
|
pwmout_period_us(obj, seconds * 1000000.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pwmout_period_ms(pwmout_t* obj, int ms) {
|
void pwmout_period_ms(pwmout_t *obj, int ms)
|
||||||
|
{
|
||||||
pwmout_period_us(obj, ms * 1000);
|
pwmout_period_us(obj, ms * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the PWM period, keeping the duty cycle the same.
|
// Set the PWM period, keeping the duty cycle the same.
|
||||||
void pwmout_period_us(pwmout_t* obj, int us) {
|
void pwmout_period_us(pwmout_t *obj, int us)
|
||||||
|
{
|
||||||
// calculate number of ticks
|
// calculate number of ticks
|
||||||
uint32_t ticks = pwm_clock_mhz * us;
|
uint32_t ticks = pwm_clock_mhz * us;
|
||||||
|
|
||||||
|
@ -146,30 +153,42 @@ void pwmout_period_us(pwmout_t* obj, int us) {
|
||||||
LPC_PWM1->TCR = TCR_CNT_EN | TCR_PWM_EN;
|
LPC_PWM1->TCR = TCR_CNT_EN | TCR_PWM_EN;
|
||||||
}
|
}
|
||||||
|
|
||||||
void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
|
int pwmout_read_period_us(pwmout_t *obj)
|
||||||
|
{
|
||||||
|
return (float)(LPC_PWM1->MR0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void pwmout_pulsewidth(pwmout_t *obj, float seconds)
|
||||||
|
{
|
||||||
pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
|
pwmout_pulsewidth_us(obj, seconds * 1000000.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pwmout_pulsewidth_ms(pwmout_t* obj, int ms) {
|
void pwmout_pulsewidth_ms(pwmout_t *obj, int ms)
|
||||||
|
{
|
||||||
pwmout_pulsewidth_us(obj, ms * 1000);
|
pwmout_pulsewidth_us(obj, ms * 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
|
void pwmout_pulsewidth_us(pwmout_t *obj, int us)
|
||||||
|
{
|
||||||
// calculate number of ticks
|
// calculate number of ticks
|
||||||
uint32_t v = pwm_clock_mhz * us;
|
uint32_t v = pwm_clock_mhz * us;
|
||||||
|
|
||||||
// workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout
|
// workaround for PWM1[1] - Never make it equal MR0, else we get 1 cycle dropout
|
||||||
if (v == LPC_PWM1->MR0) {
|
if (v == LPC_PWM1->MR0) {
|
||||||
v++;
|
v++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// set the match register value
|
// set the match register value
|
||||||
*obj->MR = v;
|
*obj->MR = v;
|
||||||
|
|
||||||
// set the channel latch to update value at next period start
|
// set the channel latch to update value at next period start
|
||||||
LPC_PWM1->LER |= 1 << obj->pwm;
|
LPC_PWM1->LER |= 1 << obj->pwm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int pwmout_read_pulsewidth_us(pwmout_t *obj {
|
||||||
|
return (timer->MR3 - timer->MR[tid.mr]);
|
||||||
|
}
|
||||||
|
|
||||||
const PinMap *pwmout_pinmap()
|
const PinMap *pwmout_pinmap()
|
||||||
{
|
{
|
||||||
return PinMap_PWM;
|
return PinMap_PWM;
|
||||||
|
|
Loading…
Reference in New Issue