Skip to content

Fix pwmout period #2103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Jul 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,7 @@ struct i2c_s {
I2CName i2c;
};

struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t period;
uint32_t pulse;
};

#include "common_objects.h"
#include "gpio_object.h"

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,7 @@ struct i2c_s {
I2CName i2c;
};

struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t period;
uint32_t pulse;
};

#include "common_objects.h"
#include "gpio_object.h"

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,7 @@ struct i2c_s {
I2CName i2c;
};

struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t period;
uint32_t pulse;
};

#include "common_objects.h"
#include "gpio_object.h"

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,12 @@ struct i2c_s {
I2CName i2c;
};

struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t period;
uint32_t pulse;
};

struct can_s {
CANName can;
int index;
};

#include "common_objects.h"
#include "gpio_object.h"

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,7 @@ struct i2c_s {
I2CName i2c;
};

struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t period;
uint32_t pulse;
};

#include "common_objects.h"
#include "gpio_object.h"

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,12 @@ struct i2c_s {
I2CName i2c;
};

struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t period;
uint32_t pulse;
};

struct can_s {
CANName can;
int index;
};

#include "common_objects.h"
#include "gpio_object.h"

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,18 +93,12 @@ struct i2c_s {
I2CName i2c;
};

struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t period;
uint32_t pulse;
};

struct can_s {
CANName can;
int index;
};

#include "common_objects.h"
#include "gpio_object.h"

#ifdef __cplusplus
Expand Down
57 changes: 57 additions & 0 deletions hal/targets/hal/TARGET_STM/TARGET_STM32F0/common_objects.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* mbed Microcontroller Library
*******************************************************************************
* Copyright (c) 2016, STMicroelectronics
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************
*/
#ifndef MBED_COMMON_OBJECTS_H
#define MBED_COMMON_OBJECTS_H

#include "cmsis.h"
#include "PortNames.h"
#include "PeripheralNames.h"
#include "PinNames.h"

#ifdef __cplusplus
extern "C" {
#endif

struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t prescaler;
uint32_t period;
uint32_t pulse;
};

#include "gpio_object.h"

#ifdef __cplusplus
}
#endif

#endif

34 changes: 28 additions & 6 deletions hal/targets/hal/TARGET_STM/TARGET_STM32F0/pwmout_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void pwmout_init(pwmout_t* obj, PinName pin) {
obj->pin = pin;
obj->period = 0;
obj->pulse = 0;
obj->prescaler = 1;

pwmout_period_us(obj, 20000); // 20 ms per default
}
Expand All @@ -101,7 +102,7 @@ void pwmout_write(pwmout_t* obj, float value) {

// Configure channels
sConfig.OCMode = TIM_OCMODE_PWM1;
sConfig.Pulse = obj->pulse;
sConfig.Pulse = obj->pulse / obj->prescaler;
sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
sConfig.OCFastMode = TIM_OCFAST_DISABLE;
Expand Down Expand Up @@ -265,18 +266,39 @@ void pwmout_period_us(pwmout_t* obj, int us) {
// Update the SystemCoreClock variable
SystemCoreClockUpdate();

TimHandle.Init.Period = us - 1;
TimHandle.Init.Prescaler = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 �s tick
/* To make it simple, we use to possible prescaler values which lead to:
* pwm unit = 1us, period/pulse can be from 1us to 65535us
* or
* pwm unit = 500us, period/pulse can be from 500us to ~32.76sec
* Be careful that all the channels of a PWM shares the same prescaler
*/
if (us > 0xFFFF) {
obj->prescaler = 500;
} else {
obj->prescaler = 1;
}
TimHandle.Init.Prescaler = ((SystemCoreClock / 1000000) * obj->prescaler) - 1;

if (TimHandle.Init.Prescaler > 0xFFFF)
error("PWM: out of range prescaler");

TimHandle.Init.Period = (us - 1) / obj->prescaler;
if (TimHandle.Init.Period > 0xFFFF)
error("PWM: out of range period");

TimHandle.Init.ClockDivision = 0;
TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
HAL_TIM_PWM_Init(&TimHandle);

// Set duty cycle again
pwmout_write(obj, dc);
if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) {
error("Cannot initialize PWM");
}

// Save for future use
obj->period = us;

// Set duty cycle again
pwmout_write(obj, dc);

__HAL_TIM_ENABLE(&TimHandle);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,19 +89,13 @@ struct i2c_s {
uint32_t slave;
};

struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t period;
uint32_t pulse;
};

struct can_s {
CANName can;
int index;
};

#include "gpio_object.h"
#include "common_objects.h"

#ifdef __cplusplus
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,7 @@ struct i2c_s {
uint32_t slave;
};

struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t period;
uint32_t pulse;
};

#include "common_objects.h"
#include "gpio_object.h"

#ifdef __cplusplus
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,12 @@ struct i2c_s {
uint32_t slave;
};

struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t period;
uint32_t pulse;
};

struct can_s {
CANName can;
int index;
};

#include "common_objects.h"
#include "gpio_object.h"

#ifdef __cplusplus
Expand Down
57 changes: 57 additions & 0 deletions hal/targets/hal/TARGET_STM/TARGET_STM32F1/common_objects.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* mbed Microcontroller Library
*******************************************************************************
* Copyright (c) 2016, STMicroelectronics
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*******************************************************************************
*/
#ifndef MBED_COMMON_OBJECTS_H
#define MBED_COMMON_OBJECTS_H

#include "cmsis.h"
#include "PortNames.h"
#include "PeripheralNames.h"
#include "PinNames.h"

#ifdef __cplusplus
extern "C" {
#endif

struct pwmout_s {
PWMName pwm;
PinName pin;
uint32_t prescaler;
uint32_t period;
uint32_t pulse;
};

#include "gpio_object.h"

#ifdef __cplusplus
}
#endif

#endif

Loading