pwmout - LPC11U6X - add read methods for period and pulsewidth

pull/13492/head
talorion 2020-08-25 23:44:59 +02:00 committed by Gregor Mayramhof
parent a6f9c5b84f
commit fabd906534
1 changed files with 56 additions and 35 deletions

View File

@ -25,36 +25,39 @@
static const PinMap PinMap_PWM[] = {
{P1_19, SCT0_0, 2},
{P2_2 , SCT0_1, 3},
{P2_7 , SCT0_2, 2},
{P2_2, SCT0_1, 3},
{P2_7, SCT0_2, 2},
{P1_13, SCT0_3, 2},
{P2_16, SCT1_0, 1},
{P2_17, SCT1_1, 1},
{P2_18, SCT1_2, 1},
{P2_19, SCT1_3, 1},
{NC , NC ,0}
{NC, NC, 0}
};
static LPC_SCT0_Type *SCTs[SCT_CHANNELS] = {
(LPC_SCT0_Type*)LPC_SCT0,
(LPC_SCT0_Type*)LPC_SCT1,
(LPC_SCT0_Type *)LPC_SCT0,
(LPC_SCT0_Type *)LPC_SCT1,
};
// bit flags for used SCTs
static unsigned char sct_used = 0;
static int get_available_sct(void) {
static int get_available_sct(void)
{
int i;
for (i=0; i<SCT_CHANNELS; i++) {
if ((sct_used & (1 << i)) == 0)
for (i = 0; i < SCT_CHANNELS; i++) {
if ((sct_used & (1 << i)) == 0) {
return i;
}
}
return -1;
}
void pwmout_init(pwmout_t* obj, PinName pin) {
void pwmout_init(pwmout_t *obj, PinName pin)
{
// determine the SPI to use
PWMName pwm_mapped = (PWMName)pinmap_peripheral(pin, PinMap_PWM);
if (pwm_mapped == (PWMName)NC) {
@ -73,17 +76,17 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
LPC_SYSCON->SYSAHBCLKCTRL |= (1UL << 31);
// Clear peripheral reset the SCT:
LPC_SYSCON->PRESETCTRL |= (1 << (obj->pwm_ch + 9));
LPC_SYSCON->PRESETCTRL |= (1 << (obj->pwm_ch + 9));
pinmap_pinout(pin, PinMap_PWM);
LPC_SCT0_Type* pwm = obj->pwm;
LPC_SCT0_Type *pwm = obj->pwm;
// Unified 32-bit counter, autolimit
pwm->CONFIG |= ((0x3 << 17) | 0x01);
// halt and clear the counter
pwm->CTRL |= (1 << 2) | (1 << 3);
switch(pwm_mapped) {
switch (pwm_mapped) {
case SCT0_0:
case SCT1_0:
pwm->OUT0_SET = (1 << 0); // event 0
@ -116,10 +119,11 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
// default to 20ms: standard for servos, and fine for e.g. brightness control
pwmout_period_ms(obj, 20);
pwmout_write (obj, 0);
pwmout_write(obj, 0);
}
void pwmout_free(pwmout_t* obj) {
void pwmout_free(pwmout_t *obj)
{
sct_used &= ~(1 << obj->pwm_ch);
if (sct_used == 0) {
// Disable the SCT clock
@ -127,8 +131,9 @@ void pwmout_free(pwmout_t* obj) {
}
}
void pwmout_write(pwmout_t* obj, float value) {
LPC_SCT0_Type* pwm = obj->pwm;
void pwmout_write(pwmout_t *obj, float value)
{
LPC_SCT0_Type *pwm = obj->pwm;
if (value < 0.0f) {
value = 0.0;
} else if (value > 1.0f) {
@ -137,7 +142,7 @@ void pwmout_write(pwmout_t* obj, float value) {
uint32_t t_on = (uint32_t)((float)(pwm->MATCHREL0 + 1) * value);
if (t_on > 0) {
pwm->MATCHREL1 = t_on - 1;
// Un-halt the timer and ensure the new pulse-width takes immediate effect if necessary
if (pwm->CTRL & (1 << 2)) {
pwm->MATCH1 = pwm->MATCHREL1;
@ -150,34 +155,38 @@ void pwmout_write(pwmout_t* obj, float value) {
}
}
float pwmout_read(pwmout_t* obj) {
LPC_SCT0_Type* pwm = obj->pwm;
float pwmout_read(pwmout_t *obj)
{
LPC_SCT0_Type *pwm = obj->pwm;
uint32_t t_off = pwm->MATCHREL0 + 1;
uint32_t t_on = (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
float v = (float)t_on/(float)t_off;
float v = (float)t_on / (float)t_off;
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);
}
void pwmout_period_ms(pwmout_t* obj, int ms) {
void pwmout_period_ms(pwmout_t *obj, int ms)
{
pwmout_period_us(obj, ms * 1000);
}
// Set the PWM period, keeping the duty cycle the same.
void pwmout_period_us(pwmout_t* obj, int us) {
LPC_SCT0_Type* pwm = obj->pwm;
void pwmout_period_us(pwmout_t *obj, int us)
{
LPC_SCT0_Type *pwm = obj->pwm;
uint32_t t_off = pwm->MATCHREL0 + 1;
uint32_t t_on = (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
float v = (float)t_on/(float)t_off;
float v = (float)t_on / (float)t_off;
uint32_t period_ticks = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000);
uint32_t pulsewidth_ticks = period_ticks * v;
pwm->MATCHREL0 = period_ticks - 1;
if (pulsewidth_ticks > 0) {
pwm->MATCHREL1 = pulsewidth_ticks - 1;
// Un-halt the timer and ensure the new period & pulse-width take immediate effect if necessary
if (pwm->CTRL & (1 << 2)) {
pwm->MATCH0 = pwm->MATCHREL0;
@ -188,25 +197,33 @@ void pwmout_period_us(pwmout_t* obj, int us) {
// Halt the timer and force the output low
pwm->CTRL |= (1 << 2) | (1 << 3);
pwm->OUTPUT = 0x00000000;
// Ensure the new period will take immediate effect when the timer is un-halted
pwm->MATCH0 = pwm->MATCHREL0;
}
}
void pwmout_pulsewidth(pwmout_t* obj, float seconds) {
int pwmout_read_period_us(pwmout_t *obj)
{
return pwm->MATCHREL0 + 1;
}
void pwmout_pulsewidth(pwmout_t *obj, float seconds)
{
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);
}
void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
LPC_SCT0_Type* pwm = obj->pwm;
void pwmout_pulsewidth_us(pwmout_t *obj, int us)
{
LPC_SCT0_Type *pwm = obj->pwm;
if (us > 0) {
pwm->MATCHREL1 = (uint32_t)(((uint64_t)SystemCoreClock * (uint64_t)us) / (uint64_t)1000000) - 1;
// Un-halt the timer and ensure the new pulse-width takes immediate effect if necessary
if (pwm->CTRL & (1 << 2)) {
pwm->MATCH1 = pwm->MATCHREL1;
@ -219,6 +236,10 @@ void pwmout_pulsewidth_us(pwmout_t* obj, int us) {
}
}
int pwmout_read_pulsewidth_us(pwmout_t *obj {
return (!(pwm->CTRL & (1 << 2))) ? pwm->MATCHREL1 + 1 : 0;
}
const PinMap *pwmout_pinmap()
{
return PinMap_PWM;