Skip to content

Commit

Permalink
pwmout - MAX32630 - add read methods for period and pulsewidth
Browse files Browse the repository at this point in the history
  • Loading branch information
talorion committed Sep 2, 2020
1 parent f996216 commit 3db5d7b
Showing 1 changed file with 31 additions and 19 deletions.
50 changes: 31 additions & 19 deletions targets/TARGET_Maxim/TARGET_MAX32630/pwmout_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@


//******************************************************************************
void pwmout_init(pwmout_t* obj, PinName pin)
void pwmout_init(pwmout_t *obj, PinName pin)
{
// Make sure the pin is free for GPIO use
unsigned int port = (unsigned int)pin >> PORT_SHIFT;
Expand All @@ -69,7 +69,7 @@ void pwmout_init(pwmout_t* obj, PinName pin)
while (pwm.pin == pin) {

// Check to see if this PT instance is free
if (((mxc_pt_regs_t*)pwm.peripheral)->rate_length & MXC_F_PT_RATE_LENGTH_MODE) {
if (((mxc_pt_regs_t *)pwm.peripheral)->rate_length & MXC_F_PT_RATE_LENGTH_MODE) {
break;
}

Expand All @@ -83,7 +83,7 @@ void pwmout_init(pwmout_t* obj, PinName pin)
MXC_CLKMAN->sys_clk_ctrl_7_pt = MXC_S_CLKMAN_CLK_SCALE_DIV_1;

// Set the obj pointer to the propper PWM instance
obj->pwm = (mxc_pt_regs_t*)pwm.peripheral;
obj->pwm = (mxc_pt_regs_t *)pwm.peripheral;

// Initialize object period and pulse width
obj->period = -1;
Expand All @@ -99,7 +99,7 @@ void pwmout_init(pwmout_t* obj, PinName pin)

// default to 20ms: standard for servos, and fine for e.g. brightness control
pwmout_period_us(obj, 20000);
pwmout_write (obj, 0);
pwmout_write(obj, 0);

// Set the drive mode to normal
MXC_SET_FIELD(&MXC_GPIO->out_mode[port],
Expand All @@ -111,18 +111,18 @@ void pwmout_init(pwmout_t* obj, PinName pin)
}

//******************************************************************************
void pwmout_free(pwmout_t* obj)
void pwmout_free(pwmout_t *obj)
{
// Set the registers to the reset value
obj->pwm->train = 0;
obj->pwm->rate_length = 0x08000000;
}

//******************************************************************************
static void pwmout_update(pwmout_t* obj)
static void pwmout_update(pwmout_t *obj)
{
// Calculate and set the divider ratio
int div = (obj->period * (SystemCoreClock / 1000000))/32;
int div = (obj->period * (SystemCoreClock / 1000000)) / 32;
if (div < 2) {
div = 2;
}
Expand All @@ -134,24 +134,24 @@ static void pwmout_update(pwmout_t* obj)


//******************************************************************************
void pwmout_write(pwmout_t* obj, float percent)
void pwmout_write(pwmout_t *obj, float percent)
{
// Saturate percent if outside of range
if(percent < 0.0f) {
if (percent < 0.0f) {
percent = 0.0f;
} else if(percent > 1.0f) {
} else if (percent > 1.0f) {
percent = 1.0f;
}

// Resize the pulse width to set the duty cycle
pwmout_pulsewidth_us(obj, (int)(percent*obj->period));
pwmout_pulsewidth_us(obj, (int)(percent * obj->period));
}

//******************************************************************************
float pwmout_read(pwmout_t* obj)
float pwmout_read(pwmout_t *obj)
{
// Check for when pulsewidth or period equals 0
if((obj->pulse_width == 0) || (obj->period == 0)) {
if ((obj->pulse_width == 0) || (obj->period == 0)) {
return 0;
}

Expand All @@ -160,19 +160,19 @@ float pwmout_read(pwmout_t* obj)
}

//******************************************************************************
void pwmout_period(pwmout_t* obj, float seconds)
void pwmout_period(pwmout_t *obj, float seconds)
{
pwmout_period_us(obj, (int)(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);
}

//******************************************************************************
void pwmout_period_us(pwmout_t* obj, int us)
void pwmout_period_us(pwmout_t *obj, int us)
{
// Check the range of the period
MBED_ASSERT((us >= 0) && (us <= (int)(SystemCoreClock / 32)));
Expand All @@ -190,19 +190,25 @@ void pwmout_period_us(pwmout_t* obj, int us)
}

//******************************************************************************
void pwmout_pulsewidth(pwmout_t* obj, float seconds)
int pwmout_read_period_us(pwmout_t *obj)
{
return obj->period;
}

//******************************************************************************
void pwmout_pulsewidth(pwmout_t *obj, float seconds)
{
pwmout_pulsewidth_us(obj, (int)(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)
void pwmout_pulsewidth_us(pwmout_t *obj, int us)
{
// Check the range of the pulsewidth
MBED_ASSERT((us >= 0) && (us <= (int)(SystemCoreClock / 32)));
Expand All @@ -219,6 +225,12 @@ void pwmout_pulsewidth_us(pwmout_t* obj, int us)
pwmout_update(obj);
}

//******************************************************************************
int pwmout_read_pulsewidth_us(pwmout_t *obj)
{
return obj->pulse_width;
}

const PinMap *pwmout_pinmap()
{
return PinMap_PWM;
Expand Down

0 comments on commit 3db5d7b

Please sign in to comment.