Changes from Review of last PR adding Tiva PWM driver

This commit is contained in:
Gregory Nutt 2016-08-05 07:17:42 -06:00
parent ce0dfda923
commit f5ae207516
4 changed files with 311 additions and 298 deletions

View File

@ -1,49 +1,52 @@
/************************************************************************************ /************************************************************************************
* arch/arm/src/tiva/tiva_pwm.c * arch/arm/src/tiva/tiva_pwm.c
* *
* Copyright (C) 2016 Young Mu. All rights reserved. * Copyright (C) 2016 Young Mu. All rights reserved.
* Author: Young Mu <young.mu@aliyun.com> * Author: Young Mu <young.mu@aliyun.com>
* *
* The basic structure of this driver derives in spirit (if nothing more) from the * The basic structure of this driver derives in spirit (if nothing more) from the
* NuttX SAM PWM driver which has: * NuttX SAM PWM driver which has:
* *
* Copyright (C) 2013 Gregory Nutt. All rights reserved. * Copyright (C) 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org> * Author: Gregory Nutt <gnutt@nuttx.org>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
* are met: * are met:
* *
* 1. Redistributions of source code must retain the above copyright * 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer. * notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright * 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in * notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the * the documentation and/or other materials provided with the
* distribution. * distribution.
* 3. Neither the name NuttX nor the names of its contributors may be * 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software * used to endorse or promote products derived from this software
* without specific prior written permission. * without specific prior written permission.
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE. * POSSIBILITY OF SUCH DAMAGE.
* *
************************************************************************************/ ************************************************************************************/
/************************************************************************************ /************************************************************************************
* Included Files * Included Files
************************************************************************************/ ************************************************************************************/
#include <nuttx/config.h>
#include <stdio.h> #include <stdio.h>
#include <errno.h>
#include <debug.h> #include <debug.h>
#include <nuttx/drivers/pwm.h> #include <nuttx/drivers/pwm.h>
@ -59,60 +62,39 @@
#include "chip/tm4c_memorymap.h" #include "chip/tm4c_memorymap.h"
/************************************************************************************ /************************************************************************************
* Pre-processor Definitions * Private Types
************************************************************************************/ ************************************************************************************/
#define pwmerr(fmt, args...) printf("%s(%d): " fmt, __FUNCTION__, __LINE__, ##args);
#ifndef CONFIG_DEBUG
# undef CONFIG_DEBUG_PWM
#endif
#ifdef CONFIG_DEBUG_PWM
# define pwmdbg dbg
# ifdef CONFIG_DEBUG_VERBOSE
# define pwmvdbg vdbg
# else
# define pwmvdbg(x...)
# endif
#else
# define pwmdbg(x...)
# define pwmvdbg(x...)
#endif
/************************************************************************************
* Private Types
************************************************************************************/
uint32_t g_pwm_pinset[] = uint32_t g_pwm_pinset[] =
{ {
GPIO_M0_PWM0, GPIO_M0_PWM0,
GPIO_M0_PWM1, GPIO_M0_PWM1,
GPIO_M0_PWM2, GPIO_M0_PWM2,
GPIO_M0_PWM3, GPIO_M0_PWM3,
GPIO_M0_PWM4, GPIO_M0_PWM4,
GPIO_M0_PWM5, GPIO_M0_PWM5,
GPIO_M0_PWM6, GPIO_M0_PWM6,
GPIO_M0_PWM7, GPIO_M0_PWM7,
}; };
struct tiva_pwm_chan_s struct tiva_pwm_chan_s
{ {
const struct pwm_ops_s *ops; const struct pwm_ops_s *ops;
uint8_t controller_id; uint8_t controller_id;
uintptr_t controller_base; uintptr_t controller_base;
uint8_t generator_id; uint8_t generator_id;
uintptr_t generator_base; uintptr_t generator_base;
uint8_t channel_id; uint8_t channel_id;
}; };
/************************************************************************************ /************************************************************************************
* Private Function Prototypes * Private Function Prototypes
************************************************************************************/ ************************************************************************************/
static inline void tiva_pwm_putreg(struct tiva_pwm_chan_s *chan, unsigned int offset, uint32_t regval); static inline void tiva_pwm_putreg(struct tiva_pwm_chan_s *chan,
static inline uint32_t tiva_pwm_getreg(struct tiva_pwm_chan_s *chan, unsigned int offset); unsigned int offset, uint32_t regval);
static inline uint32_t tiva_pwm_getreg(struct tiva_pwm_chan_s *chan,
unsigned int offset);
static int tiva_pwm_setup(FAR struct pwm_lowerhalf_s *dev); static int tiva_pwm_setup(FAR struct pwm_lowerhalf_s *dev);
static int tiva_pwm_shutdown(FAR struct pwm_lowerhalf_s *dev); static int tiva_pwm_shutdown(FAR struct pwm_lowerhalf_s *dev);
@ -123,236 +105,246 @@ static int tiva_pwm_ioctl(FAR struct pwm_lowerhalf_s *dev,
int cmd, unsigned long arg); int cmd, unsigned long arg);
/************************************************************************************ /************************************************************************************
* Private Data * Private Data
************************************************************************************/ ************************************************************************************/
uint32_t g_pwm_freq = 15000000; static uint32_t g_pwm_freq = 15000000;
uint32_t g_pwm_counter = (1 << 16); static uint32_t g_pwm_counter = (1 << 16);
static const struct pwm_ops_s g_pwm_ops = static const struct pwm_ops_s g_pwm_ops =
{ {
.setup = tiva_pwm_setup, .setup = tiva_pwm_setup,
.shutdown = tiva_pwm_shutdown, .shutdown = tiva_pwm_shutdown,
.start = tiva_pwm_start, .start = tiva_pwm_start,
.stop = tiva_pwm_stop, .stop = tiva_pwm_stop,
.ioctl = tiva_pwm_ioctl, .ioctl = tiva_pwm_ioctl,
}; };
#ifdef CONFIG_TIVA_PWM0_CHAN0 #ifdef CONFIG_TIVA_PWM0_CHAN0
static struct tiva_pwm_chan_s g_pwm_chan0 = static struct tiva_pwm_chan_s g_pwm_chan0 =
{ {
.ops = &g_pwm_ops, .ops = &g_pwm_ops,
.controller_id = 0, .controller_id = 0,
.controller_base = TIVA_PWM0_BASE, .controller_base = TIVA_PWM0_BASE,
.generator_id = 0, .generator_id = 0,
.generator_base = TIVA_PWM0_BASE + TIVA_PWMn_BASE + TIVA_PWMn_INTERVAL * 0, .generator_base = TIVA_PWM0_BASE + TIVA_PWMn_BASE + TIVA_PWMn_INTERVAL * 0,
.channel_id = 0, .channel_id = 0,
}; };
#endif #endif
#ifdef CONFIG_TIVA_PWM0_CHAN1 #ifdef CONFIG_TIVA_PWM0_CHAN1
static struct tiva_pwm_chan_s g_pwm_chan1 = static struct tiva_pwm_chan_s g_pwm_chan1 =
{ {
.ops = &g_pwm_ops, .ops = &g_pwm_ops,
.controller_id = 0, .controller_id = 0,
.controller_base = TIVA_PWM0_BASE, .controller_base = TIVA_PWM0_BASE,
.generator_id = 0, .generator_id = 0,
.generator_base = TIVA_PWM0_BASE + TIVA_PWMn_BASE + TIVA_PWMn_INTERVAL * 0, .generator_base = TIVA_PWM0_BASE + TIVA_PWMn_BASE + TIVA_PWMn_INTERVAL * 0,
.channel_id = 1, .channel_id = 1,
}; };
#endif #endif
#ifdef CONFIG_TIVA_PWM0_CHAN2 #ifdef CONFIG_TIVA_PWM0_CHAN2
static struct tiva_pwm_chan_s g_pwm_chan2 = static struct tiva_pwm_chan_s g_pwm_chan2 =
{ {
.ops = &g_pwm_ops, .ops = &g_pwm_ops,
.controller_id = 0, .controller_id = 0,
.controller_base = TIVA_PWM0_BASE, .controller_base = TIVA_PWM0_BASE,
.generator_id = 1, .generator_id = 1,
.generator_base = TIVA_PWM0_BASE + TIVA_PWMn_BASE + TIVA_PWMn_INTERVAL * 1, .generator_base = TIVA_PWM0_BASE + TIVA_PWMn_BASE + TIVA_PWMn_INTERVAL * 1,
.channel_id = 2, .channel_id = 2,
}; };
#endif #endif
#ifdef CONFIG_TIVA_PWM0_CHAN3 #ifdef CONFIG_TIVA_PWM0_CHAN3
static struct tiva_pwm_chan_s g_pwm_chan3 = static struct tiva_pwm_chan_s g_pwm_chan3 =
{ {
.ops = &g_pwm_ops, .ops = &g_pwm_ops,
.controller_id = 0, .controller_id = 0,
.controller_base = TIVA_PWM0_BASE, .controller_base = TIVA_PWM0_BASE,
.generator_id = 1, .generator_id = 1,
.generator_base = TIVA_PWM0_BASE + TIVA_PWMn_BASE + TIVA_PWMn_INTERVAL * 1, .generator_base = TIVA_PWM0_BASE + TIVA_PWMn_BASE + TIVA_PWMn_INTERVAL * 1,
.channel_id = 3, .channel_id = 3,
}; };
#endif #endif
#ifdef CONFIG_TIVA_PWM0_CHAN4 #ifdef CONFIG_TIVA_PWM0_CHAN4
static struct tiva_pwm_chan_s g_pwm_chan4 = static struct tiva_pwm_chan_s g_pwm_chan4 =
{ {
.ops = &g_pwm_ops, .ops = &g_pwm_ops,
.controller_id = 0, .controller_id = 0,
.controller_base = TIVA_PWM0_BASE, .controller_base = TIVA_PWM0_BASE,
.generator_id = 2, .generator_id = 2,
.generator_base = TIVA_PWM0_BASE + TIVA_PWMn_BASE + TIVA_PWMn_INTERVAL * 2, .generator_base = TIVA_PWM0_BASE + TIVA_PWMn_BASE + TIVA_PWMn_INTERVAL * 2,
.channel_id = 4, .channel_id = 4,
}; };
#endif #endif
#ifdef CONFIG_TIVA_PWM0_CHAN5 #ifdef CONFIG_TIVA_PWM0_CHAN5
static struct tiva_pwm_chan_s g_pwm_chan5 = static struct tiva_pwm_chan_s g_pwm_chan5 =
{ {
.ops = &g_pwm_ops, .ops = &g_pwm_ops,
.controller_id = 0, .controller_id = 0,
.controller_base = TIVA_PWM0_BASE, .controller_base = TIVA_PWM0_BASE,
.generator_id = 2, .generator_id = 2,
.generator_base = TIVA_PWM0_BASE + TIVA_PWMn_BASE + TIVA_PWMn_INTERVAL * 2, .generator_base = TIVA_PWM0_BASE + TIVA_PWMn_BASE + TIVA_PWMn_INTERVAL * 2,
.channel_id = 5, .channel_id = 5,
}; };
#endif #endif
#ifdef CONFIG_TIVA_PWM0_CHAN6 #ifdef CONFIG_TIVA_PWM0_CHAN6
static struct tiva_pwm_chan_s g_pwm_chan6 = static struct tiva_pwm_chan_s g_pwm_chan6 =
{ {
.ops = &g_pwm_ops, .ops = &g_pwm_ops,
.controller_id = 0, .controller_id = 0,
.controller_base = TIVA_PWM0_BASE, .controller_base = TIVA_PWM0_BASE,
.generator_id = 3, .generator_id = 3,
.generator_base = TIVA_PWM0_BASE + TIVA_PWMn_BASE + TIVA_PWMn_INTERVAL * 3, .generator_base = TIVA_PWM0_BASE + TIVA_PWMn_BASE + TIVA_PWMn_INTERVAL * 3,
.channel_id = 6, .channel_id = 6,
}; };
#endif #endif
#ifdef CONFIG_TIVA_PWM0_CHAN7 #ifdef CONFIG_TIVA_PWM0_CHAN7
static struct tiva_pwm_chan_s g_pwm_chan7 = static struct tiva_pwm_chan_s g_pwm_chan7 =
{ {
.ops = &g_pwm_ops, .ops = &g_pwm_ops,
.controller_id = 0, .controller_id = 0,
.controller_base = TIVA_PWM0_BASE, .controller_base = TIVA_PWM0_BASE,
.generator_id = 3, .generator_id = 3,
.generator_base = TIVA_PWM0_BASE + TIVA_PWMn_BASE + TIVA_PWMn_INTERVAL * 3, .generator_base = TIVA_PWM0_BASE + TIVA_PWMn_BASE + TIVA_PWMn_INTERVAL * 3,
.channel_id = 7, .channel_id = 7,
}; };
#endif #endif
/************************************************************************************ /************************************************************************************
* Private Functions * Private Functions
************************************************************************************/ ************************************************************************************/
/************************************************************************************ /************************************************************************************
* Name: tiva_pwm_getreg * Name: tiva_pwm_getreg
* *
* Description: * Description:
* Get a 32-bit register value by offset * Get a 32-bit register value by offset
* *
************************************************************************************/ ************************************************************************************/
static inline uint32_t tiva_pwm_getreg(struct tiva_pwm_chan_s *chan, unsigned int offset) static inline uint32_t tiva_pwm_getreg(struct tiva_pwm_chan_s *chan,
unsigned int offset)
{ {
uintptr_t regaddr = chan->generator_base + offset; uintptr_t regaddr = chan->generator_base + offset;
return getreg32(regaddr); return getreg32(regaddr);
} }
/************************************************************************************ /************************************************************************************
* Name: tiva_pwm_putreg * Name: tiva_pwm_putreg
* *
* Description: * Description:
* Put a 32-bit register value by offset * Put a 32-bit register value by offset
* *
************************************************************************************/ ************************************************************************************/
static inline void tiva_pwm_putreg(struct tiva_pwm_chan_s *chan, unsigned int offset, uint32_t regval) static inline void tiva_pwm_putreg(struct tiva_pwm_chan_s *chan,
unsigned int offset, uint32_t regval)
{ {
uintptr_t regaddr = chan->generator_base + offset; uintptr_t regaddr = chan->generator_base + offset;
putreg32(regval, regaddr); putreg32(regval, regaddr);
} }
/**************************************************************************** /****************************************************************************
* Name: pwm_setup * Name: pwm_setup
* *
* Description: * Description:
* This method is called when the driver is opened. The lower half driver * This method is called when the driver is opened. The lower half driver
* will be configured and initialized the device so that it is ready for * will be configured and initialized the device so that it is ready for
* use. It will not, however, output pulses until the start method is * use. It will not, however, output pulses until the start method is
* called. * called.
* *
* Input parameters: * Input parameters:
* dev - A reference to the lower half PWM driver state structure * dev - A reference to the lower half PWM driver state structure
* *
* Returned Value: * Returned Value:
* Zero on success; a negated errno value on failure * Zero on success; a negated errno value on failure
* *
****************************************************************************/ ****************************************************************************/
static int tiva_pwm_setup(FAR struct pwm_lowerhalf_s *dev) static int tiva_pwm_setup(FAR struct pwm_lowerhalf_s *dev)
{ {
FAR struct tiva_pwm_chan_s *chan = (FAR struct tiva_pwm_chan_s *)dev; FAR struct tiva_pwm_chan_s *chan = (FAR struct tiva_pwm_chan_s *)dev;
pwmdbg("setup PWM for channel %d\n", chan->channel_id); pwminfo("setup PWM for channel %d\n", chan->channel_id);
/* Enable GPIO port, GPIO pin type and GPIO alternate function (refer to
* TM4C1294NC 23.4.2-4)
*/
/* Enable GPIO port, GPIO pin type and GPIO alternate function (refer to TM4C1294NC 23.4.2-4) */
int ret = tiva_configgpio(g_pwm_pinset[chan->channel_id]); int ret = tiva_configgpio(g_pwm_pinset[chan->channel_id]);
if (ret < 0) if (ret < 0)
{ {
pwmerr("tiva_configgpio failed (%x)\n", g_pwm_pinset[chan->channel_id]); pwmerr("ERROR: tiva_configgpio failed (%x)\n",
return -1; g_pwm_pinset[chan->channel_id]);
return ret;
} }
return OK; return OK;
} }
/**************************************************************************** /****************************************************************************
* Name: pwm_shutdown * Name: pwm_shutdown
* *
* Description: * Description:
* This method is called when the driver is closed. The lower half driver * This method is called when the driver is closed. The lower half driver
* stop pulsed output, free any resources, disable the timer hardware, and * stop pulsed output, free any resources, disable the timer hardware, and
* put the system into the lowest possible power usage state * put the system into the lowest possible power usage state
* *
* Input parameters: * Input parameters:
* dev - A reference to the lower half PWM driver state structure * dev - A reference to the lower half PWM driver state structure
* *
* Returned Value: * Returned Value:
* Zero on success; a negated errno value on failure * Zero on success; a negated errno value on failure
* *
****************************************************************************/ ****************************************************************************/
static int tiva_pwm_shutdown(FAR struct pwm_lowerhalf_s *dev) static int tiva_pwm_shutdown(FAR struct pwm_lowerhalf_s *dev)
{ {
FAR struct tiva_pwm_chan_s *chan = (FAR struct tiva_pwm_chan_s *)dev; FAR struct tiva_pwm_chan_s *chan = (FAR struct tiva_pwm_chan_s *)dev;
pwmdbg("shutdown PWM for channel %d\n", chan->channel_id); pwminfo("shutdown PWM for channel %d\n", chan->channel_id);
/* Remove unused-variable warning */ /* Remove unused-variable warning */
(void)chan; (void)chan;
/* Ensure the PWM channel has been stopped */ /* Ensure the PWM channel has been stopped */
tiva_pwm_stop(dev); tiva_pwm_stop(dev);
return OK; return OK;
} }
/**************************************************************************** /****************************************************************************
* Name: pwm_start * Name: pwm_start
* *
* Description: * Description:
* (Re-)initialize the timer resources and start the pulsed output * (Re-)initialize the timer resources and start the pulsed output
* *
* Input parameters: * Input parameters:
* dev - A reference to the lower half PWM driver state structure * dev - A reference to the lower half PWM driver state structure
* info - A reference to the characteristics of the pulsed output * info - A reference to the characteristics of the pulsed output
* *
* Returned Value: * Returned Value:
* Zero on success; a negated errno value on failure * Zero on success; a negated errno value on failure
* *
****************************************************************************/ ****************************************************************************/
static int tiva_pwm_start(FAR struct pwm_lowerhalf_s *dev, FAR const struct pwm_info_s *info) static int tiva_pwm_start(FAR struct pwm_lowerhalf_s *dev,
FAR const struct pwm_info_s *info)
{ {
FAR struct tiva_pwm_chan_s *chan = (FAR struct tiva_pwm_chan_s *)dev; FAR struct tiva_pwm_chan_s *chan = (FAR struct tiva_pwm_chan_s *)dev;
pwmdbg("start PWM for channel %d\n", chan->channel_id); pwminfo("start PWM for channel %d\n", chan->channel_id);
uint16_t duty = info->duty; uint16_t duty = info->duty;
uint32_t frequency = info->frequency; uint32_t frequency = info->frequency;
/* Configure PWM countdown mode (refer to TM4C1294NC 23.4.6) */ /* Configure PWM countdown mode (refer to TM4C1294NC 23.4.6) */
tiva_pwm_putreg(chan, TIVA_PWMn_CTL_OFFSET, 0); tiva_pwm_putreg(chan, TIVA_PWMn_CTL_OFFSET, 0);
if (chan->channel_id % 2 == 0) if (chan->channel_id % 2 == 0)
{ {
@ -366,20 +358,27 @@ static int tiva_pwm_start(FAR struct pwm_lowerhalf_s *dev, FAR const struct pwm_
} }
/* Set the PWM period (refer to TM4C1294NC 23.4.7) */ /* Set the PWM period (refer to TM4C1294NC 23.4.7) */
uint32_t pwm_min_freq = (uint32_t)(g_pwm_freq / g_pwm_counter) + 1; uint32_t pwm_min_freq = (uint32_t)(g_pwm_freq / g_pwm_counter) + 1;
uint32_t pwm_max_freq = g_pwm_freq; uint32_t pwm_max_freq = g_pwm_freq;
uint32_t load = (uint32_t)(g_pwm_freq / frequency); uint32_t load = (uint32_t)(g_pwm_freq / frequency);
pwmdbg("channel %d: load = %u (%08x)\n", chan->channel_id, load, load);
pwminfo("channel %d: load = %u (%08x)\n", chan->channel_id, load, load);
if (load >= g_pwm_counter || load < 1) if (load >= g_pwm_counter || load < 1)
{ {
pwmerr("frequency should be in [%d, %d] Hz\n", pwm_min_freq, pwm_max_freq); pwmerr("ERROR: frequency should be in [%d, %d] Hz\n",
return -1; pwm_min_freq, pwm_max_freq);
return -ERANGE;
} }
tiva_pwm_putreg(chan, TIVA_PWMn_LOAD_OFFSET, load - 1); tiva_pwm_putreg(chan, TIVA_PWMn_LOAD_OFFSET, load - 1);
/* Configure PWM duty (refer to TM4C1294NC 23.4.8-9) */ /* Configure PWM duty (refer to TM4C1294NC 23.4.8-9) */
uint32_t comp = (uint32_t)((1 - (float)duty / g_pwm_counter) * load); uint32_t comp = (uint32_t)((1 - (float)duty / g_pwm_counter) * load);
pwmdbg("channel %d: comp = %u (%08x)\n", chan->channel_id, comp, comp); pwminfo("channel %d: comp = %u (%08x)\n", chan->channel_id, comp, comp);
if (chan->channel_id % 2 == 0) if (chan->channel_id % 2 == 0)
{ {
tiva_pwm_putreg(chan, TIVA_PWMn_CMPA_OFFSET, comp - 1); tiva_pwm_putreg(chan, TIVA_PWMn_CMPA_OFFSET, comp - 1);
@ -390,39 +389,41 @@ static int tiva_pwm_start(FAR struct pwm_lowerhalf_s *dev, FAR const struct pwm_
} }
/* Enable the PWM generator (refer to TM4C1294NC 23.4.10) */ /* Enable the PWM generator (refer to TM4C1294NC 23.4.10) */
tiva_pwm_putreg(chan, TIVA_PWMn_CTL_OFFSET, CTL_ENABLE << TIVA_PWMn_CTL_ENABLE); tiva_pwm_putreg(chan, TIVA_PWMn_CTL_OFFSET, CTL_ENABLE << TIVA_PWMn_CTL_ENABLE);
/* Enable PWM channel (refer to TM4C1294NC 23.4.11) */ /* Enable PWM channel (refer to TM4C1294NC 23.4.11) */
putreg32((1 << chan->channel_id), chan->controller_base + TIVA_PWM_ENABLE_OFFSET);
putreg32((1 << chan->channel_id), chan->controller_base + TIVA_PWM_ENABLE_OFFSET);
return OK; return OK;
} }
/**************************************************************************** /****************************************************************************
* Name: pwm_stop * Name: pwm_stop
* *
* Description: * Description:
* Stop the pulsed output and reset the timer resources * Stop the pulsed output and reset the timer resources
* *
* Input parameters: * Input parameters:
* dev - A reference to the lower half PWM driver state structure * dev - A reference to the lower half PWM driver state structure
* *
* Returned Value: * Returned Value:
* Zero on success; a negated errno value on failure * Zero on success; a negated errno value on failure
* *
* Assumptions: * Assumptions:
* This function is called to stop the pulsed output at anytime. This * This function is called to stop the pulsed output at anytime. This
* method is also called from the timer interrupt handler when a repetition * method is also called from the timer interrupt handler when a repetition
* count expires... automatically stopping the timer. * count expires... automatically stopping the timer.
* *
****************************************************************************/ ****************************************************************************/
static int tiva_pwm_stop(FAR struct pwm_lowerhalf_s *dev) static int tiva_pwm_stop(FAR struct pwm_lowerhalf_s *dev)
{ {
FAR struct tiva_pwm_chan_s *chan = (FAR struct tiva_pwm_chan_s *)dev; FAR struct tiva_pwm_chan_s *chan = (FAR struct tiva_pwm_chan_s *)dev;
pwmdbg("stop PWM for channel %d\n", chan->channel_id); pwminfo("stop PWM for channel %d\n", chan->channel_id);
/* Disable PWM channel */ /* Disable PWM channel */
uint32_t value = getreg32(chan->controller_base + TIVA_PWM_ENABLE_OFFSET); uint32_t value = getreg32(chan->controller_base + TIVA_PWM_ENABLE_OFFSET);
value &= ~(1 << chan->channel_id); value &= ~(1 << chan->channel_id);
putreg32(value, chan->controller_base + TIVA_PWM_ENABLE_OFFSET); putreg32(value, chan->controller_base + TIVA_PWM_ENABLE_OFFSET);
@ -431,52 +432,54 @@ static int tiva_pwm_stop(FAR struct pwm_lowerhalf_s *dev)
} }
/**************************************************************************** /****************************************************************************
* Name: pwm_ioctl * Name: pwm_ioctl
* *
* Description: * Description:
* Lower-half logic may support platform-specific ioctl commands * Lower-half logic may support platform-specific ioctl commands
* *
* Input parameters: * Input parameters:
* dev - A reference to the lower half PWM driver state structure * dev - A reference to the lower half PWM driver state structure
* cmd - The ioctl command * cmd - The ioctl command
* arg - The argument accompanying the ioctl command * arg - The argument accompanying the ioctl command
* *
* Returned Value: * Returned Value:
* Zero on success; a negated errno value on failure * Zero on success; a negated errno value on failure
* *
****************************************************************************/ ****************************************************************************/
static int tiva_pwm_ioctl(FAR struct pwm_lowerhalf_s *dev, int cmd, unsigned long arg) static int tiva_pwm_ioctl(FAR struct pwm_lowerhalf_s *dev, int cmd,
unsigned long arg)
{ {
FAR struct tiva_pwm_chan_s *chan = (FAR struct tiva_pwm_chan_s *)dev; FAR struct tiva_pwm_chan_s *chan = (FAR struct tiva_pwm_chan_s *)dev;
pwmdbg("ioctl PWM for channel %d\n", chan->channel_id); pwminfo("ioctl PWM for channel %d\n", chan->channel_id);
/* Remove unused-variable warning */ /* Remove unused-variable warning */
(void)chan; (void)chan;
/* There are no platform-specific ioctl commands */ /* There are no platform-specific ioctl commands */
return -1; return -ENOTTY;
} }
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************
* Name: tiva_pwm_initialize * Name: tiva_pwm_initialize
* *
* Description: * Description:
* Initialize one PWM channel for use with the upper_level PWM driver. * Initialize one PWM channel for use with the upper_level PWM driver.
* *
* Input Parameters: * Input Parameters:
* channel - A number identifying the PWM channel use. * channel - A number identifying the PWM channel use.
* *
* Returned Value: * Returned Value:
* On success, a pointer to the SAMA5 lower half PWM driver is returned. * On success, a pointer to the SAMA5 lower half PWM driver is returned.
* NULL is returned on any failure. * NULL is returned on any failure.
* *
****************************************************************************/ ****************************************************************************/
FAR struct pwm_lowerhalf_s *tiva_pwm_initialize(int channel) FAR struct pwm_lowerhalf_s *tiva_pwm_initialize(int channel)
{ {
@ -538,39 +541,28 @@ FAR struct pwm_lowerhalf_s *tiva_pwm_initialize(int channel)
return NULL; return NULL;
} }
pwmvdbg("channel %d: channel_id=%d, ", channel, chan->channel_id); pwminfo("channel %d: channel_id=%d, ", channel, chan->channel_id);
pwmvdbg("controller_id=%d, controller_base=%08x, ", chan->controller_id, chan->controller_base); pwminfo("controller_id=%d, controller_base=%08x, ",
pwmvdbg("generator_id=%d, generator_base=%08x\n", chan->generator_id, chan->generator_base); chan->controller_id, chan->controller_base);
pwminfo("generator_id=%d, generator_base=%08x\n",
chan->generator_id, chan->generator_base);
/* Enable PWM controller (refer to TM4C1294NC 23.4.1) */ /* Enable PWM controller (refer to TM4C1294NC 23.4.1) */
assert(chan->controller_id == 0); assert(chan->controller_id == 0);
tiva_pwm_enablepwr(chan->controller_id); tiva_pwm_enablepwr(chan->controller_id);
tiva_pwm_enableclk(chan->controller_id); tiva_pwm_enableclk(chan->controller_id);
/* Configure PWM Clock Configuration (refer to TM4C1294NC 23.4.5) */ /* Configure PWM Clock Configuration (refer to TM4C1294NC 23.4.5)
/* on TM4C1294NC, configure the PWM clock source as 15MHz (the system clock 120MHz divided by 8) */ *
/* TODO: need an algorithm to choose the best divider and load value combo */ * On TM4C1294NC, configure the PWM clock source as 15MHz (the system
* clock 120MHz divided by 8)
*
* TODO: need an algorithm to choose the best divider and load value combo.
*/
putreg32(CC_USEPWM << TIVA_PWM_CC_USEPWM | CC_PWMDIV_8 << TIVA_PWM_CC_PWMDIV, putreg32(CC_USEPWM << TIVA_PWM_CC_USEPWM | CC_PWMDIV_8 << TIVA_PWM_CC_PWMDIV,
chan->controller_base + TIVA_PWM_CC); chan->controller_base + TIVA_PWM_CC);
return (FAR struct pwm_lowerhalf_s *)chan; return (FAR struct pwm_lowerhalf_s *)chan;
} }
/****************************************************************************
* Name: board_pwm_setup
*
* Description:
* No implementation for now, it's called by PWM tool.
*
* Input Parameters:
* None.
*
* Returned Value:
* Zero on Success.
*
****************************************************************************/
int board_pwm_setup(void)
{
return OK;
}

View File

@ -47,8 +47,6 @@
****************************************************************************/ ****************************************************************************/
FAR struct pwm_lowerhalf_s *tiva_pwm_initialize(int channel); FAR struct pwm_lowerhalf_s *tiva_pwm_initialize(int channel);
void tm4c_pwm_register(int channel); void tm4c_pwm_register(int channel);
int board_pwm_setup(void);
#endif /* __ARCH_ARM_SRC_TIVA_TIVA_PWM_H */ #endif /* __ARCH_ARM_SRC_TIVA_TIVA_PWM_H */

View File

@ -701,7 +701,7 @@ static ioe_pinset_t sim_int_update(FAR struct sim_dev_s *priv)
} }
else /* if (SIM_LEVEL_SENSITIVE(priv, pin)) */ else /* if (SIM_LEVEL_SENSITIVE(priv, pin)) */
{ {
/* Level triggered. Set intstat if imatch in level type. */ /* Level triggered. Set intstat if match in level type. */
if ((pinval && SIM_LEVEL_HIGH(priv, pin)) || if ((pinval && SIM_LEVEL_HIGH(priv, pin)) ||
(!pinval && SIM_LEVEL_LOW(priv, pin))) (!pinval && SIM_LEVEL_LOW(priv, pin)))

View File

@ -37,13 +37,13 @@
* Included Files * Included Files
****************************************************************************/ ****************************************************************************/
#include <stdio.h>
#include <stdint.h>
#include <nuttx/config.h> #include <nuttx/config.h>
#include <stdio.h>
#include <stdint.h>
#include <debug.h> #include <debug.h>
#include <nuttx/board.h>
#include <nuttx/i2c/i2c_master.h> #include <nuttx/i2c/i2c_master.h>
#include <arch/board/board.h> #include <arch/board/board.h>
@ -54,7 +54,6 @@
#define PWM_PATH_FMT "/dev/pwm%d" #define PWM_PATH_FMT "/dev/pwm%d"
#define PWM_PATH_FMTLEN (10) #define PWM_PATH_FMTLEN (10)
/**************************************************************************** /****************************************************************************
* Pre-processor Definitions * Pre-processor Definitions
****************************************************************************/ ****************************************************************************/
@ -171,7 +170,7 @@ void tm4c_pwm_register(int channel)
dev = tiva_pwm_initialize(channel); dev = tiva_pwm_initialize(channel);
if (dev == NULL) if (dev == NULL)
{ {
dbg("ERROR: Failed to get PWM%d interface\n", channel); pwmerr("ERROR: Failed to get PWM%d interface\n", channel);
} }
else else
{ {
@ -179,7 +178,8 @@ void tm4c_pwm_register(int channel)
ret = pwm_register(pwm_path, dev); ret = pwm_register(pwm_path, dev);
if (ret < 0) if (ret < 0)
{ {
dbg("ERROR: Failed to register PWM%d driver: %d\n", channel, ret); pwmerr("ERROR: Failed to register PWM%d driver: %d\n",
channel, ret);
} }
} }
} }
@ -193,7 +193,6 @@ void tm4c_pwm_register(int channel)
****************************************************************************/ ****************************************************************************/
#ifdef HAVE_PWM #ifdef HAVE_PWM
static void tm4c_pwm(void) static void tm4c_pwm(void)
{ {
#ifdef CONFIG_TIVA_PWM0_CHAN0 #ifdef CONFIG_TIVA_PWM0_CHAN0
@ -221,8 +220,7 @@ static void tm4c_pwm(void)
tm4c_pwm_register(7); tm4c_pwm_register(7);
#endif #endif
} }
#endif # HAVE_PWM
#endif
/**************************************************************************** /****************************************************************************
* Public Functions * Public Functions
@ -246,9 +244,11 @@ int tm4c_bringup(void)
tm4c_i2ctool(); tm4c_i2ctool();
#ifdef HAVE_PWM
/* Register PWM drivers */ /* Register PWM drivers */
tm4c_pwm(); tm4c_pwm();
#endif
#ifdef HAVE_TIMER #ifdef HAVE_TIMER
/* Initialize the timer driver */ /* Initialize the timer driver */
@ -262,3 +262,26 @@ int tm4c_bringup(void)
return OK; return OK;
} }
/****************************************************************************
* Name: board_pwm_setup
*
* Description:
* No implementation for now, it's called by PWM tool via boardctl.h.
* See include/nuttx/board.h
*
* Input Parameters:
* None.
*
* Returned Value:
* Zero on Success.
*
****************************************************************************/
#ifdef CONFIG_BOARDCTL_PWMTEST
int board_pwm_setup(void)
{
return OK;
}
#endif