mirror of https://github.com/ARMmbed/mbed-os.git
pwmout - MAX32600 - add read methods for period and pulsewidth
parent
13f57feaef
commit
b27b8698b4
|
@ -40,7 +40,7 @@
|
||||||
#include "PeripheralPins.h"
|
#include "PeripheralPins.h"
|
||||||
|
|
||||||
//******************************************************************************
|
//******************************************************************************
|
||||||
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
|
// Make sure the pin is free for GPIO use
|
||||||
unsigned int port = (unsigned int)pin >> PORT_SHIFT;
|
unsigned int port = (unsigned int)pin >> PORT_SHIFT;
|
||||||
|
@ -51,20 +51,20 @@ void pwmout_init(pwmout_t* obj, PinName pin)
|
||||||
PinMap pwm = PinMap_PWM[0];
|
PinMap pwm = PinMap_PWM[0];
|
||||||
|
|
||||||
// Check if there is a pulse train already active on this port
|
// Check if there is a pulse train already active on this port
|
||||||
int pin_func = (MXC_GPIO->func_sel[port] & (0xF << (port_pin*4))) >> (port_pin*4);
|
int pin_func = (MXC_GPIO->func_sel[port] & (0xF << (port_pin * 4))) >> (port_pin * 4);
|
||||||
MBED_ASSERT((pin_func < 1) || (pin_func > 3));
|
MBED_ASSERT((pin_func < 1) || (pin_func > 3));
|
||||||
|
|
||||||
// Search through PinMap_PWM to find the pin
|
// Search through PinMap_PWM to find the pin
|
||||||
while(pwm.pin != pin) {
|
while (pwm.pin != pin) {
|
||||||
pwm = PinMap_PWM[++i];
|
pwm = PinMap_PWM[++i];
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find a free PT instance on this pin
|
// Find a free PT instance on this pin
|
||||||
while(pwm.pin == pin) {
|
while (pwm.pin == pin) {
|
||||||
|
|
||||||
// Check to see if this PT instance is free
|
// Check to see if this PT instance is free
|
||||||
if((((mxc_pt_regs_t*)pwm.peripheral)->rate_length &
|
if ((((mxc_pt_regs_t *)pwm.peripheral)->rate_length &
|
||||||
MXC_F_PT_RATE_LENGTH_MODE)) {
|
MXC_F_PT_RATE_LENGTH_MODE)) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,10 +78,10 @@ void pwmout_init(pwmout_t* obj, PinName pin)
|
||||||
MXC_CLKMAN->clk_ctrl_2_pt = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
|
MXC_CLKMAN->clk_ctrl_2_pt = MXC_E_CLKMAN_CLK_SCALE_ENABLED;
|
||||||
|
|
||||||
// Set the obj pointer to the propper PWM instance
|
// 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
|
// Initialize object period and pulse width
|
||||||
obj->period = -1;
|
obj->period = -1;
|
||||||
obj->pulse_width = -1;
|
obj->pulse_width = -1;
|
||||||
|
|
||||||
// Disable the output
|
// Disable the output
|
||||||
|
@ -94,17 +94,17 @@ void pwmout_init(pwmout_t* obj, PinName pin)
|
||||||
|
|
||||||
// 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_us(obj, 20000);
|
pwmout_period_us(obj, 20000);
|
||||||
pwmout_write (obj, 0);
|
pwmout_write(obj, 0);
|
||||||
|
|
||||||
// Set the drive mode to normal
|
// Set the drive mode to normal
|
||||||
MXC_SET_FIELD(&MXC_GPIO->out_mode[port], (0x7 << (port_pin*4)), (MXC_V_GPIO_OUT_MODE_NORMAL_DRIVE << (port_pin*4)));
|
MXC_SET_FIELD(&MXC_GPIO->out_mode[port], (0x7 << (port_pin * 4)), (MXC_V_GPIO_OUT_MODE_NORMAL_DRIVE << (port_pin * 4)));
|
||||||
|
|
||||||
// Enable the global pwm
|
// Enable the global pwm
|
||||||
MXC_PTG->ctrl = MXC_F_PT_CTRL_ENABLE_ALL;
|
MXC_PTG->ctrl = MXC_F_PT_CTRL_ENABLE_ALL;
|
||||||
}
|
}
|
||||||
|
|
||||||
//******************************************************************************
|
//******************************************************************************
|
||||||
void pwmout_free(pwmout_t* obj)
|
void pwmout_free(pwmout_t *obj)
|
||||||
{
|
{
|
||||||
// Set the registers to the reset value
|
// Set the registers to the reset value
|
||||||
obj->pwm->train = 0;
|
obj->pwm->train = 0;
|
||||||
|
@ -112,39 +112,39 @@ void pwmout_free(pwmout_t* obj)
|
||||||
}
|
}
|
||||||
|
|
||||||
//******************************************************************************
|
//******************************************************************************
|
||||||
static void pwmout_update(pwmout_t* obj)
|
static void pwmout_update(pwmout_t *obj)
|
||||||
{
|
{
|
||||||
// Calculate and set the divider ratio
|
// Calculate and set the divider ratio
|
||||||
int div = (obj->period * (SystemCoreClock/1000000))/32;
|
int div = (obj->period * (SystemCoreClock / 1000000)) / 32;
|
||||||
if (div < 2){
|
if (div < 2) {
|
||||||
div = 2;
|
div = 2;
|
||||||
}
|
}
|
||||||
MXC_SET_FIELD(&obj->pwm->rate_length, MXC_F_PT_RATE_LENGTH_RATE_CONTROL, div);
|
MXC_SET_FIELD(&obj->pwm->rate_length, MXC_F_PT_RATE_LENGTH_RATE_CONTROL, div);
|
||||||
|
|
||||||
// Change the duty cycle to adjust the pulse width
|
// Change the duty cycle to adjust the pulse width
|
||||||
obj->pwm->train = (0xFFFFFFFF << (32-((32*obj->pulse_width)/obj->period)));
|
obj->pwm->train = (0xFFFFFFFF << (32 - ((32 * obj->pulse_width) / obj->period)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//******************************************************************************
|
//******************************************************************************
|
||||||
void pwmout_write(pwmout_t* obj, float percent)
|
void pwmout_write(pwmout_t *obj, float percent)
|
||||||
{
|
{
|
||||||
// Saturate percent if outside of range
|
// Saturate percent if outside of range
|
||||||
if(percent < 0.0) {
|
if (percent < 0.0) {
|
||||||
percent = 0.0;
|
percent = 0.0;
|
||||||
} else if(percent > 1.0) {
|
} else if (percent > 1.0) {
|
||||||
percent = 1.0;
|
percent = 1.0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resize the pulse width to set the duty cycle
|
// 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
|
// 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;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -153,26 +153,26 @@ 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.0));
|
pwmout_period_us(obj, (int)(seconds * 1000000.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
//******************************************************************************
|
//******************************************************************************
|
||||||
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
//******************************************************************************
|
//******************************************************************************
|
||||||
void pwmout_period_us(pwmout_t* obj, int us)
|
void pwmout_period_us(pwmout_t *obj, int us)
|
||||||
{
|
{
|
||||||
// Check the range of the period
|
// Check the range of the period
|
||||||
MBED_ASSERT((us >= 0) && (us <= (int)(SystemCoreClock/32)));
|
MBED_ASSERT((us >= 0) && (us <= (int)(SystemCoreClock / 32)));
|
||||||
|
|
||||||
// Set pulse width to half the period if uninitialized
|
// Set pulse width to half the period if uninitialized
|
||||||
if(obj->pulse_width == -1){
|
if (obj->pulse_width == -1) {
|
||||||
obj->pulse_width = us/2;
|
obj->pulse_width = us / 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the period
|
// Save the period
|
||||||
|
@ -183,26 +183,32 @@ 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.0));
|
pwmout_pulsewidth_us(obj, (int)(seconds * 1000000.0));
|
||||||
}
|
}
|
||||||
|
|
||||||
//******************************************************************************
|
//******************************************************************************
|
||||||
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)
|
||||||
{
|
{
|
||||||
// Check the range of the pulsewidth
|
// Check the range of the pulsewidth
|
||||||
MBED_ASSERT((us >= 0) && (us <= (int)(SystemCoreClock/32)));
|
MBED_ASSERT((us >= 0) && (us <= (int)(SystemCoreClock / 32)));
|
||||||
|
|
||||||
// Initialize period to double the pulsewidth if uninitialized
|
// Initialize period to double the pulsewidth if uninitialized
|
||||||
if(obj->period == -1){
|
if (obj->period == -1) {
|
||||||
obj->period = 2*us;
|
obj->period = 2 * us;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the pulsewidth
|
// Save the pulsewidth
|
||||||
|
@ -212,6 +218,12 @@ void pwmout_pulsewidth_us(pwmout_t* obj, int us)
|
||||||
pwmout_update(obj);
|
pwmout_update(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//******************************************************************************
|
||||||
|
int pwmout_read_pulsewidth_us(pwmout_t *obj)
|
||||||
|
{
|
||||||
|
return obj->pulse_width;
|
||||||
|
}
|
||||||
|
|
||||||
const PinMap *pwmout_pinmap()
|
const PinMap *pwmout_pinmap()
|
||||||
{
|
{
|
||||||
return PinMap_PWM;
|
return PinMap_PWM;
|
||||||
|
|
Loading…
Reference in New Issue