arch/arm/src/stm32l4: Add getstatus and getperiod method to the timer driver (includes coding standard changes made prior to commit)

This commit is contained in:
Goden Freemans
2018-09-05 08:48:24 -06:00
committed by Gregory Nutt
parent b7d1f13a54
commit 9348f600ee
3 changed files with 368 additions and 50 deletions
File diff suppressed because it is too large Load Diff
+35 -22
View File
@@ -6,7 +6,7 @@
*
* With modifications and updates by:
*
* Copyright (C) 2011-2012, 2016 Gregory Nutt. All rights reserved.
* Copyright (C) 2011-2012, 2016, 2018 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
* dev@ziggurat29.com
*
@@ -54,11 +54,14 @@
/************************************************************************************
* Pre-processor Definitions
************************************************************************************/
/* Helpers **************************************************************************/
#define STM32L4_TIM_SETMODE(d,mode) ((d)->ops->setmode(d,mode))
#define STM32L4_TIM_SETCLOCK(d,freq) ((d)->ops->setclock(d,freq))
#define STM32L4_TIM_GETCLOCK(d) ((d)->ops->getclock(d))
#define STM32L4_TIM_SETPERIOD(d,period) ((d)->ops->setperiod(d,period))
#define STM32L4_TIM_GETPERIOD(d) ((d)->ops->getperiod(d))
#define STM32L4_TIM_GETCOUNTER(d) ((d)->ops->getcounter(d))
#define STM32L4_TIM_SETCHANNEL(d,ch,mode) ((d)->ops->setchannel(d,ch,mode))
#define STM32L4_TIM_SETCOMPARE(d,ch,comp) ((d)->ops->setcompare(d,ch,comp))
@@ -93,7 +96,7 @@ struct stm32l4_tim_dev_s
/* TIM Modes of Operation */
typedef enum
enum stm32l4_tim_mode_e
{
STM32L4_TIM_MODE_UNUSED = -1,
@@ -109,25 +112,28 @@ typedef enum
/* One of the following */
STM32L4_TIM_MODE_CK_INT = 0x0000,
//STM32L4_TIM_MODE_CK_INT_TRIG = 0x0400,
//STM32L4_TIM_MODE_CK_EXT = 0x0800,
//STM32L4_TIM_MODE_CK_EXT_TRIG = 0x0C00,
#if 0
STM32L4_TIM_MODE_CK_INT_TRIG = 0x0400,
STM32L4_TIM_MODE_CK_EXT = 0x0800,
STM32L4_TIM_MODE_CK_EXT_TRIG = 0x0C00,
#endif
/* Clock sources, OR'ed with CK_EXT */
//STM32L4_TIM_MODE_CK_CHINVALID = 0x0000,
//STM32L4_TIM_MODE_CK_CH1 = 0x0001,
//STM32L4_TIM_MODE_CK_CH2 = 0x0002,
//STM32L4_TIM_MODE_CK_CH3 = 0x0003,
//STM32L4_TIM_MODE_CK_CH4 = 0x0004
#if 0
STM32L4_TIM_MODE_CK_CHINVALID = 0x0000,
STM32L4_TIM_MODE_CK_CH1 = 0x0001,
STM32L4_TIM_MODE_CK_CH2 = 0x0002,
STM32L4_TIM_MODE_CK_CH3 = 0x0003,
STM32L4_TIM_MODE_CK_CH4 = 0x0004
#endif
/* Todo: external trigger block */
} stm32l4_tim_mode_t;
};
/* TIM Channel Modes */
typedef enum
enum stm32l4_tim_channel_e
{
STM32L4_TIM_CH_DISABLED = 0x00,
@@ -142,15 +148,19 @@ typedef enum
/* Output Compare Modes */
STM32L4_TIM_CH_OUTPWM = 0x04, /** Enable standard PWM mode, active high when counter < compare */
//STM32L4_TIM_CH_OUTCOMPARE = 0x06,
STM32L4_TIM_CH_OUTPWM = 0x04, /* Enable standard PWM mode, active high when counter < compare */
#if 0
STM32L4_TIM_CH_OUTCOMPARE = 0x06,
#endif
// TODO other modes ... as PWM capture, ENCODER and Hall Sensor
//STM32L4_TIM_CH_INCAPTURE = 0x10,
//STM32L4_TIM_CH_INPWM = 0x20
//STM32L4_TIM_CH_DRIVE_OC -- open collector mode
/* TODO other modes ... as PWM capture, ENCODER and Hall Sensor */
} stm32l4_tim_channel_t;
#if 0
STM32L4_TIM_CH_INCAPTURE = 0x10,
STM32L4_TIM_CH_INPWM = 0x20
STM32L4_TIM_CH_DRIVE_OC -- open collector mode
#endif
};
/* TIM Operations */
@@ -158,15 +168,18 @@ struct stm32l4_tim_ops_s
{
/* Basic Timers */
int (*setmode)(FAR struct stm32l4_tim_dev_s *dev, stm32l4_tim_mode_t mode);
int (*setmode)(FAR struct stm32l4_tim_dev_s *dev,
enum stm32l4_tim_mode_e mode);
int (*setclock)(FAR struct stm32l4_tim_dev_s *dev, uint32_t freq);
uint32_t (*getclock)(FAR struct stm32l4_tim_dev_s *dev);
void (*setperiod)(FAR struct stm32l4_tim_dev_s *dev, uint32_t period);
uint32_t (*getperiod)(FAR struct stm32l4_tim_dev_s *dev);
uint32_t (*getcounter)(FAR struct stm32l4_tim_dev_s *dev);
/* General and Advanced Timers Adds */
int (*setchannel)(FAR struct stm32l4_tim_dev_s *dev, uint8_t channel,
stm32l4_tim_channel_t mode);
enum stm32l4_tim_channel_e mode);
int (*setcompare)(FAR struct stm32l4_tim_dev_s *dev, uint8_t channel,
uint32_t compare);
int (*getcapture)(FAR struct stm32l4_tim_dev_s *dev, uint8_t channel);
+80 -1
View File
@@ -84,6 +84,7 @@
/****************************************************************************
* Private Types
****************************************************************************/
/* This structure provides the private representation of the "lower-half"
* driver state structure. This structure must be cast-compatible with the
* timer_lowerhalf_s structure.
@@ -111,6 +112,8 @@ static int stm32l4_timer_handler(int irq, void *context, void *arg);
static int stm32l4_start(FAR struct timer_lowerhalf_s *lower);
static int stm32l4_stop(FAR struct timer_lowerhalf_s *lower);
static int stm32l4_getstatus(FAR struct timer_lowerhalf_s *lower,
FAR struct timer_status_s *status);
static int stm32l4_settimeout(FAR struct timer_lowerhalf_s *lower,
uint32_t timeout);
static void stm32l4_setcallback(FAR struct timer_lowerhalf_s *lower,
@@ -119,13 +122,14 @@ static void stm32l4_setcallback(FAR struct timer_lowerhalf_s *lower,
/****************************************************************************
* Private Data
****************************************************************************/
/* "Lower half" driver methods */
static const struct timer_ops_s g_timer_ops =
{
.start = stm32l4_start,
.stop = stm32l4_stop,
.getstatus = NULL,
.getstatus = stm32l4_getstatus,
.settimeout = stm32l4_settimeout,
.setcallback = stm32l4_setcallback,
.ioctl = NULL,
@@ -328,6 +332,71 @@ static int stm32l4_stop(FAR struct timer_lowerhalf_s *lower)
return -ENODEV;
}
/****************************************************************************
* Name: stm32l4_getstatus
*
* Description:
* get timer status
*
* Input Parameters:
* lower - A pointer the publicly visible representation of the "lower-
* half" driver state structure.
* status - The location to return the status information.
*
* Returned Value:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
static int stm32l4_getstatus(FAR struct timer_lowerhalf_s *lower,
FAR struct timer_status_s *status)
{
FAR struct stm32l4_lowerhalf_s *priv = (FAR struct stm32l4_lowerhalf_s *)lower;
uint64_t maxtimeout;
uint32_t timeout;
uint32_t clock;
uint32_t period;
uint32_t clock_factor;
DEBUGASSERT(priv);
/* Return the status bit */
status->flags = 0;
if (priv->started)
{
status->flags |= TCFLAGS_ACTIVE;
}
if (priv->callback)
{
status->flags |= TCFLAGS_HANDLER;
}
/* Get timeout */
maxtimeout = (1 << priv->resolution) - 1;
clock = STM32L4_TIM_GETCLOCK(priv->tim);
period = STM32L4_TIM_GETPERIOD(priv->tim);
if (clock == 1000000)
{
timeout = period;
}
else
{
timeout = (maxtimeout * 1000000) / clock;
}
status->timeout = timeout;
/* Get the time remaining until the timer expires (in microseconds) */
clock_factor = (clock == 1000000)? 1: (clock / 1000000);
status->timeleft = (timeout - STM32L4_TIM_GETCOUNTER(priv->tim)) * clock_factor;
return OK;
}
/****************************************************************************
* Name: stm32l4_settimeout
*
@@ -449,21 +518,25 @@ int stm32l4_timer_initialize(FAR const char *devpath, int timer)
lower = &g_tim1_lowerhalf;
break;
#endif
#ifdef CONFIG_STM32L4_TIM2
case 2:
lower = &g_tim2_lowerhalf;
break;
#endif
#ifdef CONFIG_STM32L4_TIM3
case 3:
lower = &g_tim3_lowerhalf;
break;
#endif
#ifdef CONFIG_STM32L4_TIM4
case 4:
lower = &g_tim4_lowerhalf;
break;
#endif
#ifdef CONFIG_STM32L4_TIM5
case 5:
lower = &g_tim5_lowerhalf;
@@ -474,31 +547,37 @@ int stm32l4_timer_initialize(FAR const char *devpath, int timer)
lower = &g_tim6_lowerhalf;
break;
#endif
#ifdef CONFIG_STM32L4_TIM7
case 7:
lower = &g_tim7_lowerhalf;
break;
#endif
#ifdef CONFIG_STM32L4_TIM8
case 8:
lower = &g_tim8_lowerhalf;
break;
#endif
#ifdef CONFIG_STM32L4_TIM15
case 15:
lower = &g_tim15_lowerhalf;
break;
#endif
#ifdef CONFIG_STM32L4_TIM16
case 16:
lower = &g_tim16_lowerhalf;
break;
#endif
#ifdef CONFIG_STM32L4_TIM17
case 17:
lower = &g_tim17_lowerhalf;
break;
#endif
default:
return -ENODEV;
}