pwmout - K66F - add read methods for period and pulsewidth

pull/13492/head
talorion 2020-08-25 23:39:43 +02:00 committed by Gregor Mayramhof
parent 5763b3a136
commit 8c2c7bddb8
1 changed files with 30 additions and 11 deletions

View File

@ -27,7 +27,7 @@ static float pwm_clock_mhz;
/* Array of FTM peripheral base address. */ /* Array of FTM peripheral base address. */
static FTM_Type *const ftm_addrs[] = FTM_BASE_PTRS; static FTM_Type *const ftm_addrs[] = FTM_BASE_PTRS;
void pwmout_init(pwmout_t* obj, PinName pin) void pwmout_init(pwmout_t *obj, PinName pin)
{ {
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);
@ -73,12 +73,12 @@ void pwmout_init(pwmout_t* obj, PinName pin)
pinmap_pinout(pin, PinMap_PWM); pinmap_pinout(pin, PinMap_PWM);
} }
void pwmout_free(pwmout_t* obj) void pwmout_free(pwmout_t *obj)
{ {
FTM_Deinit(ftm_addrs[obj->pwm_name >> TPM_SHIFT]); FTM_Deinit(ftm_addrs[obj->pwm_name >> TPM_SHIFT]);
} }
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.0f; value = 0.0f;
@ -96,30 +96,31 @@ void pwmout_write(pwmout_t* obj, float value)
FTM_SetSoftwareTrigger(base, true); FTM_SetSoftwareTrigger(base, true);
} }
float pwmout_read(pwmout_t* obj) float pwmout_read(pwmout_t *obj)
{ {
FTM_Type *base = ftm_addrs[obj->pwm_name >> TPM_SHIFT]; FTM_Type *base = ftm_addrs[obj->pwm_name >> TPM_SHIFT];
uint16_t count = (base->CONTROLS[obj->pwm_name & 0xF].CnV) & FTM_CnV_VAL_MASK; uint16_t count = (base->CONTROLS[obj->pwm_name & 0xF].CnV) & FTM_CnV_VAL_MASK;
uint16_t mod = base->MOD & FTM_MOD_MOD_MASK; uint16_t mod = base->MOD & FTM_MOD_MOD_MASK;
if (mod == 0) if (mod == 0) {
return 0.0; return 0.0;
}
float v = (float)(count) / (float)(mod); float v = (float)(count) / (float)(mod);
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)
{ {
FTM_Type *base = ftm_addrs[obj->pwm_name >> TPM_SHIFT]; FTM_Type *base = ftm_addrs[obj->pwm_name >> TPM_SHIFT];
float dc = pwmout_read(obj); float dc = pwmout_read(obj);
@ -129,17 +130,26 @@ void pwmout_period_us(pwmout_t* obj, int us)
pwmout_write(obj, dc); pwmout_write(obj, dc);
} }
void pwmout_pulsewidth(pwmout_t* obj, float seconds) int pwmout_read_period_us(pwmout_t *obj)
{
uint32_t tmp = 0;
if (pwm_clock_mhz > 0) {
tmp = ((base->MOD) + 1) / pwm_clock_mhz;
}
return tmp;
}
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)
{ {
FTM_Type *base = ftm_addrs[obj->pwm_name >> TPM_SHIFT]; FTM_Type *base = ftm_addrs[obj->pwm_name >> TPM_SHIFT];
uint32_t value = (uint32_t)(pwm_clock_mhz * (float)us); uint32_t value = (uint32_t)(pwm_clock_mhz * (float)us);
@ -150,6 +160,15 @@ void pwmout_pulsewidth_us(pwmout_t* obj, int us)
FTM_SetSoftwareTrigger(base, true); FTM_SetSoftwareTrigger(base, true);
} }
int pwmout_read_pulsewidth_us(pwmout_t *obj)
{
uint32_t tmp = 0;
if (pwm_clock_mhz > 0) {
tmp = (base->CONTROLS[obj->pwm_name & 0xF].CnV) / pwm_clock_mhz;
}
return tmp;
}
const PinMap *pwmout_pinmap() const PinMap *pwmout_pinmap()
{ {
return PinMap_PWM; return PinMap_PWM;