From 12c835c2f29d74705c7823d348434cdd5ae30351 Mon Sep 17 00:00:00 2001 From: pnb Date: Sat, 31 Oct 2015 15:33:32 +0100 Subject: [PATCH 01/16] avoid warning unsused function when GPIO_TIMxCHxOUT declared in board and CONFIG_STM32_TIMx not enable but an other CONFIG_STM32_TIMx is enabled --- arch/arm/src/stm32/stm32_tim.c | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/arch/arm/src/stm32/stm32_tim.c b/arch/arm/src/stm32/stm32_tim.c index 1f9d5375714..2aeccd86a3e 100644 --- a/arch/arm/src/stm32/stm32_tim.c +++ b/arch/arm/src/stm32/stm32_tim.c @@ -145,6 +145,49 @@ # undef CONFIG_STM32_TIM14 #endif +#if defined(CONFIG_STM32_TIM1) +# if defined(GPIO_TIM1_CH1OUT) ||defined(GPIO_TIM1_CH2OUT)||\ + defined(GPIO_TIM1_CH3OUT) ||defined(GPIO_TIM1_CH4OUT) +# define HAVE_TIM1_PWM_OUTPUT 1 +#endif +#endif + +#if defined(CONFIG_STM32_TIM2) +# if defined(GPIO_TIM2_CH1OUT) ||defined(GPIO_TIM2_CH2OUT)||\ + defined(GPIO_TIM2_CH3OUT) ||defined(GPIO_TIM2_CH4OUT) +# define HAVE_TIM2_PWM_OUTPUT 1 +#endif +#endif + +#if defined(CONFIG_STM32_TIM3) +# if defined(GPIO_TIM3_CH1OUT) ||defined(GPIO_TIM3_CH2OUT)||\ + defined(GPIO_TIM3_CH3OUT) ||defined(GPIO_TIM3_CH4OUT) +# define HAVE_TIM3_PWM_OUTPUT 1 +#endif +#endif + +#if defined(CONFIG_STM32_TIM4) +# if defined(GPIO_TIM4_CH1OUT) ||defined(GPIO_TIM4_CH2OUT)||\ + defined(GPIO_TIM4_CH3OUT) ||defined(GPIO_TIM4_CH4OUT) +# define HAVE_TIM4_PWM_OUTPUT 1 +#endif +#endif + +#if defined(CONFIG_STM32_TIM5) +# if defined(GPIO_TIM5_CH1OUT) ||defined(GPIO_TIM5_CH2OUT)||\ + defined(GPIO_TIM5_CH3OUT) ||defined(GPIO_TIM5_CH4OUT) +# define HAVE_TIM5_PWM_OUTPUT 1 +#endif +#endif + +#if defined(CONFIG_STM32_TIM8) +# if defined(GPIO_TIM8_CH1OUT) ||defined(GPIO_TIM8_CH2OUT)||\ + defined(GPIO_TIM8_CH3OUT) ||defined(GPIO_TIM8_CH4OUT) +# define HAVE_TIM8_PWM_OUTPUT 1 +#endif +#endif + + /* This module then only compiles if there are enabled timers that are not intended for * some other purpose. */ @@ -245,6 +288,9 @@ static void stm32_tim_reset(FAR struct stm32_tim_dev_s *dev) stm32_tim_disable(dev); } +#if defined(HAVE_TIM1_PWM_OUTPUT)||defined(HAVE_TIM2_PWM_OUTPUT)||\ + defined(HAVE_TIM3_PWM_OUTPUT)||defined(HAVE_TIM4_PWM_OUTPUT)||\ + defined(HAVE_TIM5_PWM_OUTPUT)||defined(HAVE_TIM8_PWM_OUTPUT) static void stm32_tim_gpioconfig(uint32_t cfg, stm32_tim_channel_t mode) { /* TODO: Add support for input capture and bipolar dual outputs for TIM8 */ @@ -258,6 +304,7 @@ static void stm32_tim_gpioconfig(uint32_t cfg, stm32_tim_channel_t mode) stm32_unconfiggpio(cfg); } } +#endif /************************************************************************************ * Basic Functions From fa278723d5587f1e1faf0f77d9ad97ee17b608b5 Mon Sep 17 00:00:00 2001 From: pnb Date: Sat, 31 Oct 2015 15:59:20 +0100 Subject: [PATCH 02/16] Change name of HAVE_TIMx_PWM_OUTPUT into HAVE_TIMx_GPIOCONFIG --- arch/arm/src/stm32/stm32_tim.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/arch/arm/src/stm32/stm32_tim.c b/arch/arm/src/stm32/stm32_tim.c index 2aeccd86a3e..c7d3b8350ce 100644 --- a/arch/arm/src/stm32/stm32_tim.c +++ b/arch/arm/src/stm32/stm32_tim.c @@ -148,42 +148,42 @@ #if defined(CONFIG_STM32_TIM1) # if defined(GPIO_TIM1_CH1OUT) ||defined(GPIO_TIM1_CH2OUT)||\ defined(GPIO_TIM1_CH3OUT) ||defined(GPIO_TIM1_CH4OUT) -# define HAVE_TIM1_PWM_OUTPUT 1 +# define HAVE_TIM1_GPIOCONFIG 1 #endif #endif #if defined(CONFIG_STM32_TIM2) # if defined(GPIO_TIM2_CH1OUT) ||defined(GPIO_TIM2_CH2OUT)||\ defined(GPIO_TIM2_CH3OUT) ||defined(GPIO_TIM2_CH4OUT) -# define HAVE_TIM2_PWM_OUTPUT 1 +# define HAVE_TIM2_GPIOCONFIG 1 #endif #endif #if defined(CONFIG_STM32_TIM3) # if defined(GPIO_TIM3_CH1OUT) ||defined(GPIO_TIM3_CH2OUT)||\ defined(GPIO_TIM3_CH3OUT) ||defined(GPIO_TIM3_CH4OUT) -# define HAVE_TIM3_PWM_OUTPUT 1 +# define HAVE_TIM3_GPIOCONFIG 1 #endif #endif #if defined(CONFIG_STM32_TIM4) # if defined(GPIO_TIM4_CH1OUT) ||defined(GPIO_TIM4_CH2OUT)||\ defined(GPIO_TIM4_CH3OUT) ||defined(GPIO_TIM4_CH4OUT) -# define HAVE_TIM4_PWM_OUTPUT 1 +# define HAVE_TIM4_GPIOCONFIG 1 #endif #endif #if defined(CONFIG_STM32_TIM5) # if defined(GPIO_TIM5_CH1OUT) ||defined(GPIO_TIM5_CH2OUT)||\ defined(GPIO_TIM5_CH3OUT) ||defined(GPIO_TIM5_CH4OUT) -# define HAVE_TIM5_PWM_OUTPUT 1 +# define HAVE_TIM5_GPIOCONFIG 1 #endif #endif #if defined(CONFIG_STM32_TIM8) # if defined(GPIO_TIM8_CH1OUT) ||defined(GPIO_TIM8_CH2OUT)||\ defined(GPIO_TIM8_CH3OUT) ||defined(GPIO_TIM8_CH4OUT) -# define HAVE_TIM8_PWM_OUTPUT 1 +# define HAVE_TIM8_GPIOCONFIG 1 #endif #endif @@ -288,9 +288,9 @@ static void stm32_tim_reset(FAR struct stm32_tim_dev_s *dev) stm32_tim_disable(dev); } -#if defined(HAVE_TIM1_PWM_OUTPUT)||defined(HAVE_TIM2_PWM_OUTPUT)||\ - defined(HAVE_TIM3_PWM_OUTPUT)||defined(HAVE_TIM4_PWM_OUTPUT)||\ - defined(HAVE_TIM5_PWM_OUTPUT)||defined(HAVE_TIM8_PWM_OUTPUT) +#if defined(HAVE_TIM1_GPIOCONFIG)||defined(HAVE_TIM2_GPIOCONFIG)||\ + defined(HAVE_TIM3_GPIOCONFIG)||defined(HAVE_TIM4_GPIOCONFIG)||\ + defined(HAVE_TIM5_GPIOCONFIG)||defined(HAVE_TIM8_GPIOCONFIG) static void stm32_tim_gpioconfig(uint32_t cfg, stm32_tim_channel_t mode) { /* TODO: Add support for input capture and bipolar dual outputs for TIM8 */ From f3c0e5db5eac51558af12e81e4fcc9e0088232aa Mon Sep 17 00:00:00 2001 From: pnb Date: Sun, 1 Nov 2015 14:40:43 +0100 Subject: [PATCH 03/16] pwm duty can by zero --- arch/arm/src/stm32/stm32_pwm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/arm/src/stm32/stm32_pwm.c b/arch/arm/src/stm32/stm32_pwm.c index a3a16456889..a5cafce7974 100644 --- a/arch/arm/src/stm32/stm32_pwm.c +++ b/arch/arm/src/stm32/stm32_pwm.c @@ -582,7 +582,7 @@ static int pwm_timer(FAR struct stm32_pwmtimer_s *priv, pwmvdbg("TIM%d channel: %d frequency: %d duty: %08x\n", priv->timid, priv->channel, info->frequency, info->duty); #endif - DEBUGASSERT(info->frequency > 0 && info->duty > 0 && + DEBUGASSERT(info->frequency > 0 && info->duty >= 0 && info->duty < uitoub16(100)); /* Disable all interrupts and DMA requests, clear all pending status */ From 701c386299070c2304ec0247013c54e3828d66fe Mon Sep 17 00:00:00 2001 From: pnb Date: Wed, 16 Dec 2015 21:39:51 +0100 Subject: [PATCH 04/16] add timer capture STM32 --- arch/arm/src/stm32/stm32_tim.c | 562 +++++++++++++++++++-------------- arch/arm/src/stm32/stm32_tim.h | 10 +- 2 files changed, 340 insertions(+), 232 deletions(-) diff --git a/arch/arm/src/stm32/stm32_tim.c b/arch/arm/src/stm32/stm32_tim.c index c7d3b8350ce..ebf967946cb 100644 --- a/arch/arm/src/stm32/stm32_tim.c +++ b/arch/arm/src/stm32/stm32_tim.c @@ -145,47 +145,6 @@ # undef CONFIG_STM32_TIM14 #endif -#if defined(CONFIG_STM32_TIM1) -# if defined(GPIO_TIM1_CH1OUT) ||defined(GPIO_TIM1_CH2OUT)||\ - defined(GPIO_TIM1_CH3OUT) ||defined(GPIO_TIM1_CH4OUT) -# define HAVE_TIM1_GPIOCONFIG 1 -#endif -#endif - -#if defined(CONFIG_STM32_TIM2) -# if defined(GPIO_TIM2_CH1OUT) ||defined(GPIO_TIM2_CH2OUT)||\ - defined(GPIO_TIM2_CH3OUT) ||defined(GPIO_TIM2_CH4OUT) -# define HAVE_TIM2_GPIOCONFIG 1 -#endif -#endif - -#if defined(CONFIG_STM32_TIM3) -# if defined(GPIO_TIM3_CH1OUT) ||defined(GPIO_TIM3_CH2OUT)||\ - defined(GPIO_TIM3_CH3OUT) ||defined(GPIO_TIM3_CH4OUT) -# define HAVE_TIM3_GPIOCONFIG 1 -#endif -#endif - -#if defined(CONFIG_STM32_TIM4) -# if defined(GPIO_TIM4_CH1OUT) ||defined(GPIO_TIM4_CH2OUT)||\ - defined(GPIO_TIM4_CH3OUT) ||defined(GPIO_TIM4_CH4OUT) -# define HAVE_TIM4_GPIOCONFIG 1 -#endif -#endif - -#if defined(CONFIG_STM32_TIM5) -# if defined(GPIO_TIM5_CH1OUT) ||defined(GPIO_TIM5_CH2OUT)||\ - defined(GPIO_TIM5_CH3OUT) ||defined(GPIO_TIM5_CH4OUT) -# define HAVE_TIM5_GPIOCONFIG 1 -#endif -#endif - -#if defined(CONFIG_STM32_TIM8) -# if defined(GPIO_TIM8_CH1OUT) ||defined(GPIO_TIM8_CH2OUT)||\ - defined(GPIO_TIM8_CH3OUT) ||defined(GPIO_TIM8_CH4OUT) -# define HAVE_TIM8_GPIOCONFIG 1 -#endif -#endif /* This module then only compiles if there are enabled timers that are not intended for @@ -288,24 +247,6 @@ static void stm32_tim_reset(FAR struct stm32_tim_dev_s *dev) stm32_tim_disable(dev); } -#if defined(HAVE_TIM1_GPIOCONFIG)||defined(HAVE_TIM2_GPIOCONFIG)||\ - defined(HAVE_TIM3_GPIOCONFIG)||defined(HAVE_TIM4_GPIOCONFIG)||\ - defined(HAVE_TIM5_GPIOCONFIG)||defined(HAVE_TIM8_GPIOCONFIG) -static void stm32_tim_gpioconfig(uint32_t cfg, stm32_tim_channel_t mode) -{ - /* TODO: Add support for input capture and bipolar dual outputs for TIM8 */ - - if (mode & STM32_TIM_CH_MODE_MASK) - { - stm32_configgpio(cfg); - } - else - { - stm32_unconfiggpio(cfg); - } -} -#endif - /************************************************************************************ * Basic Functions ************************************************************************************/ @@ -535,9 +476,64 @@ static int stm32_tim_setmode(FAR struct stm32_tim_dev_s *dev, stm32_tim_mode_t m return OK; } +static int stm32_tim_setcapturecfg(FAR struct stm32_tim_dev_s *dev, + uint8_t channel, uint8_t capt_filter, + uint8_t capt_prescaler) +{ + uint16_t ccmr_orig = 0; + uint16_t ccmr_val = 0; + uint16_t ccmr_mask = 0xff; + uint8_t ccmr_offset = STM32_GTIM_CCMR1_OFFSET; + + ASSERT(dev); + + /* Further we use range as 0..3; if channel=0 it will also overflow here */ + + if (--channel > 4) return ERROR; + +#if STM32_NBTIM > 0 + if (((struct stm32_tim_priv_s *)dev)->base == STM32_TIM6_BASE +#endif +#if STM32_NBTIM > 1 + || ((struct stm32_tim_priv_s *)dev)->base == STM32_TIM7_BASE +#endif +#if STM32_NBTIM > 0 + ) + { + return ERROR; + } +#endif + + ccmr_val |= ( capt_filter << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( capt_prescaler << ATIM_CCMR1_IC1PSC_SHIFT ); + + /* Define its position (shift) and get register offset */ + + if (channel & 1) + { + ccmr_val <<= 8; + ccmr_mask <<= 8; + } + + if (channel > 1) + { + ccmr_offset = STM32_GTIM_CCMR2_OFFSET; + } + + ccmr_orig = stm32_getreg16(dev, ccmr_offset); + ccmr_orig &= ~ccmr_mask; + ccmr_orig |= ccmr_val; + stm32_putreg16(dev, ccmr_offset, ccmr_orig); + + return OK; + +} + static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel, stm32_tim_channel_t mode) { + uint32_t gpio_in_cfg = 0; + uint32_t gpio_out_cfg= 0; uint16_t ccmr_orig = 0; uint16_t ccmr_val = 0; uint16_t ccmr_mask = 0xff; @@ -571,6 +567,259 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel } #endif + switch (((struct stm32_tim_priv_s *)dev)->base) + { +#ifdef CONFIG_STM32_TIM2 + case STM32_TIM2_BASE: + switch (channel) + { + case 0: +#if defined(GPIO_TIM2_CH1OUT) + gpio_out_cfg = GPIO_TIM2_CH1OUT; +#endif +#if defined(GPIO_TIM2_CH1IN) + gpio_in_cfg = GPIO_TIM2_CH1IN; +#endif + break; + case 1: +#if defined(GPIO_TIM2_CH2OUT) + gpio_out_cfg = GPIO_TIM2_CH2OUT; +#endif +#if defined(GPIO_TIM2_CH2IN) + gpio_in_cfg = GPIO_TIM2_CH2IN; +#endif + break; + case 2: +#if defined(GPIO_TIM2_CH3OUT) + gpio_out_cfg = GPIO_TIM2_CH3OUT; +#endif +#if defined(GPIO_TIM2_CH3IN) + gpio_in_cfg = GPIO_TIM2_CH3IN; +#endif + break; + case 3: +#if defined(GPIO_TIM2_CH4OUT) + gpio_out_cfg = GPIO_TIM2_CH4OUT; +#endif +#if defined(GPIO_TIM2_CH4IN) + gpio_in_cfg = GPIO_TIM2_CH4IN; +#endif + break; + default: + return ERROR; + } + break; +#endif +#ifdef CONFIG_STM32_TIM3 + case STM32_TIM3_BASE: + switch (channel) + { + case 0: +#if defined(GPIO_TIM3_CH1OUT) + gpio_out_cfg = GPIO_TIM3_CH1OUT; +#endif +#if defined(GPIO_TIM3_CH1IN) + gpio_in_cfg = GPIO_TIM3_CH1IN; +#endif + break; + case 1: +#if defined(GPIO_TIM3_CH2OUT) + gpio_out_cfg = GPIO_TIM3_CH2OUT; +#endif +#if defined(GPIO_TIM3_CH2IN) + gpio_in_cfg = GPIO_TIM3_CH2IN; +#endif + break; + case 2: +#if defined(GPIO_TIM3_CH3OUT) + gpio_out_cfg = GPIO_TIM3_CH3OUT; +#endif +#if defined(GPIO_TIM3_CH3IN) + gpio_in_cfg = GPIO_TIM3_CH3IN; +#endif + break; + case 3: +#if defined(GPIO_TIM3_CH4OUT) + gpio_out_cfg = GPIO_TIM3_CH4OUT; +#endif +#if defined(GPIO_TIM3_CH4IN) + gpio_in_cfg = GPIO_TIM3_CH4IN; +#endif + break; + default: + return ERROR; + } + break; +#endif +#ifdef CONFIG_STM32_TIM4 + case STM32_TIM4_BASE: + switch (channel) + { + case 0: +#if defined(GPIO_TIM4_CH1OUT) + gpio_out_cfg = GPIO_TIM4_CH1OUT; +#endif +#if defined(GPIO_TIM4_CH1IN) + gpio_in_cfg = GPIO_TIM4_CH1IN; +#endif + break; + case 1: +#if defined(GPIO_TIM4_CH2OUT) + gpio_out_cfg = GPIO_TIM4_CH2OUT; +#endif +#if defined(GPIO_TIM4_CH2IN) + gpio_in_cfg = GPIO_TIM4_CH2IN; +#endif + break; + case 2: +#if defined(GPIO_TIM4_CH3OUT) + gpio_out_cfg = GPIO_TIM4_CH3OUT; +#endif +#if defined(GPIO_TIM4_CH3IN) + gpio_in_cfg = GPIO_TIM4_CH3IN; +#endif + break; + case 3: +#if defined(GPIO_TIM4_CH4OUT) + gpio_out_cfg = GPIO_TIM4_CH4OUT; +#endif +#if defined(GPIO_TIM4_CH4IN) + gpio_in_cfg = GPIO_TIM4_CH4IN; +#endif + break; + default: return ERROR; + } + break; +#endif +#ifdef CONFIG_STM32_TIM5 + case STM32_TIM5_BASE: + switch (channel) + { + case 0: +#if defined(GPIO_TIM5_CH1OUT) + gpio_out_cfg = GPIO_TIM5_CH1OUT; +#endif +#if defined(GPIO_TIM5_CH1IN) + gpio_in_cfg = GPIO_TIM5_CH1IN; +#endif + break; + case 1: +#if defined(GPIO_TIM5_CH2OUT) + gpio_out_cfg = GPIO_TIM5_CH2OUT; +#endif +#if defined(GPIO_TIM5_CH2IN) + gpio_in_cfg = GPIO_TIM5_CH2IN; +#endif + break; + case 2: +#if defined(GPIO_TIM5_CH3OUT) + gpio_out_cfg = GPIO_TIM5_CH3OUT; +#endif +#if defined(GPIO_TIM5_CH3IN) + gpio_in_cfg = GPIO_TIM5_CH3IN; +#endif + break; + case 3: +#if defined(GPIO_TIM5_CH4OUT) + gpio_out_cfg = GPIO_TIM5_CH4OUT; +#endif +#if defined(GPIO_TIM5_CH4IN) + gpio_in_cfg = GPIO_TIM5_CH4IN; +#endif + break; + default: return ERROR; + } + break; +#endif + +#if STM32_NATIM > 0 +#ifdef CONFIG_STM32_TIM1 + case STM32_TIM1_BASE: + switch (channel) + { + case 0: +#if defined(GPIO_TIM1_CH1OUT) + gpio_out_cfg = GPIO_TIM1_CH1OUT; +#endif +#if defined(GPIO_TIM1_CH1IN) + gpio_in_cfg = GPIO_TIM1_CH1IN; +#endif + break; + case 1: +#if defined(GPIO_TIM1_CH2OUT) + gpio_out_cfg = GPIO_TIM1_CH2OUT; +#endif +#if defined(GPIO_TIM1_CH2IN) + gpio_in_cfg = GPIO_TIM1_CH2IN; +#endif + break; + case 2: +#if defined(GPIO_TIM1_CH3OUT) + gpio_out_cfg = GPIO_TIM1_CH3OUT; +#endif +#if defined(GPIO_TIM1_CH3IN) + gpio_in_cfg = GPIO_TIM1_CH3IN; +#endif + break; + case 3: +#if defined(GPIO_TIM1_CH4OUT) + gpio_out_cfg = GPIO_TIM1_CH4OUT; +#endif +#if defined(GPIO_TIM1_CH4IN) + gpio_in_cfg = GPIO_TIM1_CH4IN; +#endif + break; + default: + return ERROR; + } + break; +#endif +#ifdef CONFIG_STM32_TIM8 + case STM32_TIM8_BASE: + switch (channel) + { + case 0: +#if defined(GPIO_TIM8_CH1OUT) + gpio_out_cfg = GPIO_TIM8_CH1OUT, ; +#endif +#if defined(GPIO_TIM8_CH1IN) + gpio_in_cfg = GPIO_TIM8_CH1OUIN ; +#endif + break; + case 1: +#if defined(GPIO_TIM8_CH2OUT) + gpio_out_cfg = GPIO_TIM8_CH2OUT, ; +#endif +#if defined(GPIO_TIM8_CH2IN) + gpio_in_cfg = GPIO_TIM8_CH2OUIN ; +#endif + break; + case 2: +#if defined(GPIO_TIM8_CH3OUT) + gpio_out_cfg = GPIO_TIM8_CH3OUT, ; +#endif +#if defined(GPIO_TIM8_CH3IN) + gpio_in_cfg = GPIO_TIM8_CH3OUIN ; +#endif + break; + case 3: +#if defined(GPIO_TIM8_CH4OUT) + gpio_out_cfg = GPIO_TIM8_CH4OUT, ; +#endif +#if defined(GPIO_TIM8_CH4IN) + gpio_in_cfg = GPIO_TIM8_CH4OUIN ; +#endif + break; + default: + return ERROR; + } + break; +#endif +#endif + default: + return ERROR; + } + /* Decode configuration */ switch (mode & STM32_TIM_CH_MODE_MASK) @@ -578,9 +827,18 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel case STM32_TIM_CH_DISABLED: break; + case STM32_TIM_CH_INCAPTURE: + ccmr_val = (ATIM_CCMR_CCS_CCIN1 << ATIM_CCMR1_CC1S_SHIFT); + ccer_val |= ATIM_CCER_CC1E << (channel << 2); + if ( gpio_in_cfg == 0 ) + return ERROR; + break; + case STM32_TIM_CH_OUTPWM: ccmr_val = (ATIM_CCMR_MODE_PWM1 << ATIM_CCMR1_OC1M_SHIFT) + ATIM_CCMR1_OC1PE; ccer_val |= ATIM_CCER_CC1E << (channel << 2); + if ( gpio_out_cfg == 0 ) + return ERROR; break; default: @@ -614,177 +872,20 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel stm32_putreg16(dev, STM32_GTIM_CCER_OFFSET, ccer_val); /* set GPIO */ - - switch (((struct stm32_tim_priv_s *)dev)->base) + + if ( gpio_in_cfg ) { -#ifdef CONFIG_STM32_TIM2 - case STM32_TIM2_BASE: - switch (channel) - { -#if defined(GPIO_TIM2_CH1OUT) - case 0: - stm32_tim_gpioconfig(GPIO_TIM2_CH1OUT, mode); - break; -#endif -#if defined(GPIO_TIM2_CH2OUT) - case 1: - stm32_tim_gpioconfig(GPIO_TIM2_CH2OUT, mode); - break; -#endif -#if defined(GPIO_TIM2_CH3OUT) - case 2: - stm32_tim_gpioconfig(GPIO_TIM2_CH3OUT, mode); - break; -#endif -#if defined(GPIO_TIM2_CH4OUT) - case 3: - stm32_tim_gpioconfig(GPIO_TIM2_CH4OUT, mode); - break; -#endif - default: - return ERROR; - } - break; -#endif -#ifdef CONFIG_STM32_TIM3 - case STM32_TIM3_BASE: - switch (channel) - { -#if defined(GPIO_TIM3_CH1OUT) - case 0: - stm32_tim_gpioconfig(GPIO_TIM3_CH1OUT, mode); - break; -#endif -#if defined(GPIO_TIM3_CH2OUT) - case 1: - stm32_tim_gpioconfig(GPIO_TIM3_CH2OUT, mode); - break; -#endif -#if defined(GPIO_TIM3_CH3OUT) - case 2: - stm32_tim_gpioconfig(GPIO_TIM3_CH3OUT, mode); - break; -#endif -#if defined(GPIO_TIM3_CH4OUT) - case 3: - stm32_tim_gpioconfig(GPIO_TIM3_CH4OUT, mode); - break; -#endif - default: - return ERROR; - } - break; -#endif -#ifdef CONFIG_STM32_TIM4 - case STM32_TIM4_BASE: - switch (channel) - { -#if defined(GPIO_TIM4_CH1OUT) - case 0: - stm32_tim_gpioconfig(GPIO_TIM4_CH1OUT, mode); - break; -#endif -#if defined(GPIO_TIM4_CH2OUT) - case 1: - stm32_tim_gpioconfig(GPIO_TIM4_CH2OUT, mode); - break; -#endif -#if defined(GPIO_TIM4_CH3OUT) - case 2: - stm32_tim_gpioconfig(GPIO_TIM4_CH3OUT, mode); - break; -#endif -#if defined(GPIO_TIM4_CH4OUT) - case 3: - stm32_tim_gpioconfig(GPIO_TIM4_CH4OUT, mode); - break; -#endif - default: return ERROR; - } - break; -#endif -#ifdef CONFIG_STM32_TIM5 - case STM32_TIM5_BASE: - switch (channel) - { -#if defined(GPIO_TIM5_CH1OUT) - case 0: - stm32_tim_gpioconfig(GPIO_TIM5_CH1OUT, mode); - break; -#endif -#if defined(GPIO_TIM5_CH2OUT) - case 1: - stm32_tim_gpioconfig(GPIO_TIM5_CH2OUT, mode); - break; -#endif -#if defined(GPIO_TIM5_CH3OUT) - case 2: - stm32_tim_gpioconfig(GPIO_TIM5_CH3OUT, mode); - break; -#endif -#if defined(GPIO_TIM5_CH4OUT) - case 3: - stm32_tim_gpioconfig(GPIO_TIM5_CH4OUT, mode); - break; -#endif - default: return ERROR; - } - break; -#endif - -#if STM32_NATIM > 0 -#ifdef CONFIG_STM32_TIM1 - case STM32_TIM1_BASE: - switch (channel) - { -#if defined(GPIO_TIM1_CH1OUT) - case 0: - stm32_tim_gpioconfig(GPIO_TIM1_CH1OUT, mode); break; -#endif -#if defined(GPIO_TIM1_CH2OUT) - case 1: - stm32_tim_gpioconfig(GPIO_TIM1_CH2OUT, mode); break; -#endif -#if defined(GPIO_TIM1_CH3OUT) - case 2: - stm32_tim_gpioconfig(GPIO_TIM1_CH3OUT, mode); break; -#endif -#if defined(GPIO_TIM1_CH4OUT) - case 3: - stm32_tim_gpioconfig(GPIO_TIM1_CH4OUT, mode); break; -#endif - default: return ERROR; - } - break; -#endif -#ifdef CONFIG_STM32_TIM8 - case STM32_TIM8_BASE: - switch (channel) - { -#if defined(GPIO_TIM8_CH1OUT) - case 0: - stm32_tim_gpioconfig(GPIO_TIM8_CH1OUT, mode); break; -#endif -#if defined(GPIO_TIM8_CH2OUT) - case 1: - stm32_tim_gpioconfig(GPIO_TIM8_CH2OUT, mode); break; -#endif -#if defined(GPIO_TIM8_CH3OUT) - case 2: - stm32_tim_gpioconfig(GPIO_TIM8_CH3OUT, mode); break; -#endif -#if defined(GPIO_TIM8_CH4OUT) - case 3: - stm32_tim_gpioconfig(GPIO_TIM8_CH4OUT, mode); break; -#endif - default: - return ERROR; - } - break; -#endif -#endif - default: - return ERROR; + if ( mode & STM32_TIM_CH_MODE_IN_MASK ) + stm32_configgpio(gpio_in_cfg); + else + stm32_unconfiggpio(gpio_in_cfg); + } + if ( gpio_out_cfg ) + { + if ( mode & STM32_TIM_CH_MODE_OUT_MASK ) + stm32_configgpio(gpio_in_cfg); + else + stm32_unconfiggpio(gpio_out_cfg); } return OK; @@ -855,7 +956,8 @@ struct stm32_tim_ops_s stm32_tim_ops = .setisr = &stm32_tim_setisr, .enableint = &stm32_tim_enableint, .disableint = &stm32_tim_disableint, - .ackint = &stm32_tim_ackint + .ackint = &stm32_tim_ackint, + .setcapturecfg = &stm32_tim_setcapturecfg }; #ifdef CONFIG_STM32_TIM2 diff --git a/arch/arm/src/stm32/stm32_tim.h b/arch/arm/src/stm32/stm32_tim.h index 2d3b01a683b..56db9605dda 100644 --- a/arch/arm/src/stm32/stm32_tim.h +++ b/arch/arm/src/stm32/stm32_tim.h @@ -122,6 +122,7 @@ typedef enum } stm32_tim_mode_t; + /* TIM Channel Modes */ typedef enum @@ -135,15 +136,19 @@ typedef enum /* MODES: */ - STM32_TIM_CH_MODE_MASK = 0x06, + STM32_TIM_CH_MODE_OUT_MASK = 0x06, + STM32_TIM_CH_MODE_IN_MASK = 0x10, + STM32_TIM_CH_MODE_MASK = 0x16, /* Output Compare Modes */ STM32_TIM_CH_OUTPWM = 0x04, /** Enable standard PWM mode, active high when counter < compare */ //STM32_TIM_CH_OUTCOMPARE = 0x06, + /* Input Compare Modes */ + STM32_TIM_CH_INCAPTURE = 0x10, + // TODO other modes ... as PWM capture, ENCODER and Hall Sensor -//STM32_TIM_CH_INCAPTURE = 0x10, //STM32_TIM_CH_INPWM = 0x20 //STM32_TIM_CH_DRIVE_OC -- open collector mode @@ -171,6 +176,7 @@ struct stm32_tim_ops_s void (*enableint)(FAR struct stm32_tim_dev_s *dev, int source); void (*disableint)(FAR struct stm32_tim_dev_s *dev, int source); void (*ackint)(FAR struct stm32_tim_dev_s *dev, int source); + int (*setcapturecfg)(FAR struct stm32_tim_dev_s *dev, uint8_t channel, uint8_t capt_filter, uint8_t capt_prescaler); }; /************************************************************************************ From e25bc48d010cb550f5aebdb875acb7010ef8915e Mon Sep 17 00:00:00 2001 From: pnb Date: Wed, 16 Dec 2015 23:42:43 +0100 Subject: [PATCH 05/16] used DEFINE instead of setcapturecfg function to set filter and prescaler of input capture --- arch/arm/src/stm32/stm32_tim.c | 107 ++++++++++++++++----------------- arch/arm/src/stm32/stm32_tim.h | 21 +++---- 2 files changed, 61 insertions(+), 67 deletions(-) diff --git a/arch/arm/src/stm32/stm32_tim.c b/arch/arm/src/stm32/stm32_tim.c index ebf967946cb..f72d411718c 100644 --- a/arch/arm/src/stm32/stm32_tim.c +++ b/arch/arm/src/stm32/stm32_tim.c @@ -476,59 +476,6 @@ static int stm32_tim_setmode(FAR struct stm32_tim_dev_s *dev, stm32_tim_mode_t m return OK; } -static int stm32_tim_setcapturecfg(FAR struct stm32_tim_dev_s *dev, - uint8_t channel, uint8_t capt_filter, - uint8_t capt_prescaler) -{ - uint16_t ccmr_orig = 0; - uint16_t ccmr_val = 0; - uint16_t ccmr_mask = 0xff; - uint8_t ccmr_offset = STM32_GTIM_CCMR1_OFFSET; - - ASSERT(dev); - - /* Further we use range as 0..3; if channel=0 it will also overflow here */ - - if (--channel > 4) return ERROR; - -#if STM32_NBTIM > 0 - if (((struct stm32_tim_priv_s *)dev)->base == STM32_TIM6_BASE -#endif -#if STM32_NBTIM > 1 - || ((struct stm32_tim_priv_s *)dev)->base == STM32_TIM7_BASE -#endif -#if STM32_NBTIM > 0 - ) - { - return ERROR; - } -#endif - - ccmr_val |= ( capt_filter << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( capt_prescaler << ATIM_CCMR1_IC1PSC_SHIFT ); - - /* Define its position (shift) and get register offset */ - - if (channel & 1) - { - ccmr_val <<= 8; - ccmr_mask <<= 8; - } - - if (channel > 1) - { - ccmr_offset = STM32_GTIM_CCMR2_OFFSET; - } - - ccmr_orig = stm32_getreg16(dev, ccmr_offset); - ccmr_orig &= ~ccmr_mask; - ccmr_orig |= ccmr_val; - stm32_putreg16(dev, ccmr_offset, ccmr_orig); - - return OK; - -} - static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel, stm32_tim_channel_t mode) { @@ -579,6 +526,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM2_CH1IN) gpio_in_cfg = GPIO_TIM2_CH1IN; + ccmr_val |= ( GPIO_TIM2_CH1_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM2_CH1_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 1: @@ -587,6 +536,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM2_CH2IN) gpio_in_cfg = GPIO_TIM2_CH2IN; + ccmr_val |= ( GPIO_TIM2_CH2_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM2_CH2_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 2: @@ -595,6 +546,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM2_CH3IN) gpio_in_cfg = GPIO_TIM2_CH3IN; + ccmr_val |= ( GPIO_TIM2_CH3_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM2_CH3_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 3: @@ -603,6 +556,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM2_CH4IN) gpio_in_cfg = GPIO_TIM2_CH4IN; + ccmr_val |= ( GPIO_TIM2_CH4_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM2_CH4_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; default: @@ -620,6 +575,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM3_CH1IN) gpio_in_cfg = GPIO_TIM3_CH1IN; + ccmr_val |= ( GPIO_TIM3_CH1_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM3_CH1_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 1: @@ -628,6 +585,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM3_CH2IN) gpio_in_cfg = GPIO_TIM3_CH2IN; + ccmr_val |= ( GPIO_TIM3_CH2_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM3_CH2_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 2: @@ -636,6 +595,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM3_CH3IN) gpio_in_cfg = GPIO_TIM3_CH3IN; + ccmr_val |= ( GPIO_TIM3_CH3_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM3_CH3_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 3: @@ -644,6 +605,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM3_CH4IN) gpio_in_cfg = GPIO_TIM3_CH4IN; + ccmr_val |= ( GPIO_TIM3_CH4_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM3_CH4_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; default: @@ -661,6 +624,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM4_CH1IN) gpio_in_cfg = GPIO_TIM4_CH1IN; + ccmr_val |= ( GPIO_TIM4_CH1_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM4_CH1_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 1: @@ -669,6 +634,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM4_CH2IN) gpio_in_cfg = GPIO_TIM4_CH2IN; + ccmr_val |= ( GPIO_TIM4_CH2_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM4_CH2_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 2: @@ -677,6 +644,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM4_CH3IN) gpio_in_cfg = GPIO_TIM4_CH3IN; + ccmr_val |= ( GPIO_TIM4_CH3_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM4_CH3_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 3: @@ -685,6 +654,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM4_CH4IN) gpio_in_cfg = GPIO_TIM4_CH4IN; + ccmr_val |= ( GPIO_TIM4_CH4_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM4_CH4_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; default: return ERROR; @@ -701,6 +672,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM5_CH1IN) gpio_in_cfg = GPIO_TIM5_CH1IN; + ccmr_val |= ( GPIO_TIM5_CH1_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM5_CH1_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 1: @@ -709,6 +682,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM5_CH2IN) gpio_in_cfg = GPIO_TIM5_CH2IN; + ccmr_val |= ( GPIO_TIM5_CH2_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM5_CH2_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 2: @@ -717,6 +692,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM5_CH3IN) gpio_in_cfg = GPIO_TIM5_CH3IN; + ccmr_val |= ( GPIO_TIM5_CH3_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM5_CH3_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 3: @@ -725,6 +702,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM5_CH4IN) gpio_in_cfg = GPIO_TIM5_CH4IN; + ccmr_val |= ( GPIO_TIM5_CH4_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM5_CH4_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; default: return ERROR; @@ -743,6 +722,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM1_CH1IN) gpio_in_cfg = GPIO_TIM1_CH1IN; + ccmr_val |= ( GPIO_TIM1_CH1_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM1_CH1_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 1: @@ -751,6 +732,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM1_CH2IN) gpio_in_cfg = GPIO_TIM1_CH2IN; + ccmr_val |= ( GPIO_TIM1_CH2_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM1_CH2_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 2: @@ -759,6 +742,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM1_CH3IN) gpio_in_cfg = GPIO_TIM1_CH3IN; + ccmr_val |= ( GPIO_TIM1_CH3_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM1_CH3_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 3: @@ -767,6 +752,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM1_CH4IN) gpio_in_cfg = GPIO_TIM1_CH4IN; + ccmr_val |= ( GPIO_TIM1_CH4_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM1_CH4_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; default: @@ -784,6 +771,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM8_CH1IN) gpio_in_cfg = GPIO_TIM8_CH1OUIN ; + ccmr_val |= ( GPIO_TIM8_CH1_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM8_CH1_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 1: @@ -792,6 +781,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM8_CH2IN) gpio_in_cfg = GPIO_TIM8_CH2OUIN ; + ccmr_val |= ( GPIO_TIM8_CH2_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM8_CH2_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 2: @@ -800,6 +791,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM8_CH3IN) gpio_in_cfg = GPIO_TIM8_CH3OUIN ; + ccmr_val |= ( GPIO_TIM8_CH3_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM8_CH3_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 3: @@ -808,6 +801,8 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM8_CH4IN) gpio_in_cfg = GPIO_TIM8_CH4OUIN ; + ccmr_val |= ( GPIO_TIM8_CH4_ICF << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( GPIO_TIM8_CH4_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; default: @@ -825,10 +820,11 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel switch (mode & STM32_TIM_CH_MODE_MASK) { case STM32_TIM_CH_DISABLED: + ccmr_val = 0; break; case STM32_TIM_CH_INCAPTURE: - ccmr_val = (ATIM_CCMR_CCS_CCIN1 << ATIM_CCMR1_CC1S_SHIFT); + ccmr_val |= (ATIM_CCMR_CCS_CCIN1 << ATIM_CCMR1_CC1S_SHIFT); ccer_val |= ATIM_CCER_CC1E << (channel << 2); if ( gpio_in_cfg == 0 ) return ERROR; @@ -956,8 +952,7 @@ struct stm32_tim_ops_s stm32_tim_ops = .setisr = &stm32_tim_setisr, .enableint = &stm32_tim_enableint, .disableint = &stm32_tim_disableint, - .ackint = &stm32_tim_ackint, - .setcapturecfg = &stm32_tim_setcapturecfg + .ackint = &stm32_tim_ackint }; #ifdef CONFIG_STM32_TIM2 diff --git a/arch/arm/src/stm32/stm32_tim.h b/arch/arm/src/stm32/stm32_tim.h index 56db9605dda..3efee9eec6c 100644 --- a/arch/arm/src/stm32/stm32_tim.h +++ b/arch/arm/src/stm32/stm32_tim.h @@ -55,16 +55,16 @@ ************************************************************************************/ /* Helpers **************************************************************************/ -#define STM32_TIM_SETMODE(d,mode) ((d)->ops->setmode(d,mode)) -#define STM32_TIM_SETCLOCK(d,freq) ((d)->ops->setclock(d,freq)) -#define STM32_TIM_SETPERIOD(d,period) ((d)->ops->setperiod(d,period)) -#define STM32_TIM_SETCHANNEL(d,ch,mode) ((d)->ops->setchannel(d,ch,mode)) -#define STM32_TIM_SETCOMPARE(d,ch,comp) ((d)->ops->setcompare(d,ch,comp)) -#define STM32_TIM_GETCAPTURE(d,ch) ((d)->ops->getcapture(d,ch)) -#define STM32_TIM_SETISR(d,hnd,s) ((d)->ops->setisr(d,hnd,s)) -#define STM32_TIM_ENABLEINT(d,s) ((d)->ops->enableint(d,s)) -#define STM32_TIM_DISABLEINT(d,s) ((d)->ops->disableint(d,s)) -#define STM32_TIM_ACKINT(d,s) ((d)->ops->ackint(d,s)) +#define STM32_TIM_SETMODE(d,mode) ((d)->ops->setmode(d,mode)) +#define STM32_TIM_SETCLOCK(d,freq) ((d)->ops->setclock(d,freq)) +#define STM32_TIM_SETPERIOD(d,period) ((d)->ops->setperiod(d,period)) +#define STM32_TIM_SETCHANNEL(d,ch,mode) ((d)->ops->setchannel(d,ch,mode)) +#define STM32_TIM_SETCOMPARE(d,ch,comp) ((d)->ops->setcompare(d,ch,comp)) +#define STM32_TIM_GETCAPTURE(d,ch) ((d)->ops->getcapture(d,ch)) +#define STM32_TIM_SETISR(d,hnd,s) ((d)->ops->setisr(d,hnd,s)) +#define STM32_TIM_ENABLEINT(d,s) ((d)->ops->enableint(d,s)) +#define STM32_TIM_DISABLEINT(d,s) ((d)->ops->disableint(d,s)) +#define STM32_TIM_ACKINT(d,s) ((d)->ops->ackint(d,s)) /************************************************************************************ * Public Types @@ -176,7 +176,6 @@ struct stm32_tim_ops_s void (*enableint)(FAR struct stm32_tim_dev_s *dev, int source); void (*disableint)(FAR struct stm32_tim_dev_s *dev, int source); void (*ackint)(FAR struct stm32_tim_dev_s *dev, int source); - int (*setcapturecfg)(FAR struct stm32_tim_dev_s *dev, uint8_t channel, uint8_t capt_filter, uint8_t capt_prescaler); }; /************************************************************************************ From 2f78c0c1bc85083dae33e1bf599750d07e4baff3 Mon Sep 17 00:00:00 2001 From: pnb Date: Sun, 20 Dec 2015 12:54:21 +0100 Subject: [PATCH 06/16] continue capture integration --- arch/arm/src/stm32/stm32_tim.c | 177 ++++++++++++++++++++++++++++++++- arch/arm/src/stm32/stm32_tim.h | 14 +++ 2 files changed, 187 insertions(+), 4 deletions(-) diff --git a/arch/arm/src/stm32/stm32_tim.c b/arch/arm/src/stm32/stm32_tim.c index f72d411718c..de9c1aaecb6 100644 --- a/arch/arm/src/stm32/stm32_tim.c +++ b/arch/arm/src/stm32/stm32_tim.c @@ -145,7 +145,28 @@ # undef CONFIG_STM32_TIM14 #endif +#if defined(GPIO_TIM1_CH1IN) || defined(GPIO_TIM2_CH1IN) || defined(GPIO_TIM3_CH1IN) || \ + defined(GPIO_TIM4_CH1IN) || defined(GPIO_TIM5_CH1IN) || defined(GPIO_TIM8_CH1IN) || \ + defined(GPIO_TIM9_CH1IN) || defined(GPIO_TIM10_CH1IN) || defined(GPIO_TIM11_CH1IN) || \ + defined(GPIO_TIM12_CH1IN) || defined(GPIO_TIM13_CH1IN) || defined(GPIO_TIM14_CH1IN) +# define HAVE_CH1IN 1 +#endif +#if defined(GPIO_TIM1_CH2IN) || defined(GPIO_TIM2_CH2IN) || defined(GPIO_TIM3_CH2IN) || \ + defined(GPIO_TIM4_CH2IN) || defined(GPIO_TIM5_CH2IN) || defined(GPIO_TIM8_CH2IN) || \ + defined(GPIO_TIM9_CH2IN) || defined(GPIO_TIM12_CH2IN) +# define HAVE_CH2IN 1 +#endif + +#if defined(GPIO_TIM1_CH3IN) || defined(GPIO_TIM2_CH3IN) || defined(GPIO_TIM3_CH3IN) || \ + defined(GPIO_TIM4_CH3IN) || defined(GPIO_TIM5_CH3IN) || defined(GPIO_TIM8_CH3IN) +# define HAVE_CH3IN 1 +#endif + +#if defined(GPIO_TIM1_CH4IN) || defined(GPIO_TIM2_CH4IN) || defined(GPIO_TIM3_CH4IN) || \ + defined(GPIO_TIM4_CH4IN) || defined(GPIO_TIM5_CH4IN) || defined(GPIO_TIM8_CH4IN) +# define HAVE_CH4IN 1 +#endif /* This module then only compiles if there are enabled timers that are not intended for * some other purpose. @@ -390,21 +411,117 @@ static int stm32_tim_setisr(FAR struct stm32_tim_dev_s *dev, return OK; } + static void stm32_tim_enableint(FAR struct stm32_tim_dev_s *dev, int source) { + uint16_t mask; ASSERT(dev); - stm32_modifyreg16(dev, STM32_BTIM_DIER_OFFSET, 0, ATIM_DIER_UIE); + + if (source & STM32_TIM_INT_SRC_OVERFLOW) + regval |= ATIM_DIER_UIE; +#ifdef HAVE_CH1IN + if (source & STM32_TIM_INT_SRC_CAPTURE_1) + regval |= ATIM_DIER_CC1IE; +#endif +#ifdef HAVE_CH2IN + if (source & STM32_TIM_INT_SRC_CAPTURE_2) + regval |= ATIM_DIER_CC1IE; +#endif +#ifdef HAVE_CH3IN + if (source & STM32_TIM_INT_SRC_CAPTURE_3) + regval |= ATIM_DIER_CC1IE; +#endif +#ifdef HAVE_CH4IN + if (source & STM32_TIM_INT_SRC_CAPTURE_4) + regval |= ATIM_DIER_CC1IE; +#endif + + stm32_modifyreg16(dev, STM32_BTIM_DIER_OFFSET,0,mask); + } static void stm32_tim_disableint(FAR struct stm32_tim_dev_s *dev, int source) { + uint16_t mask; ASSERT(dev); - stm32_modifyreg16(dev, STM32_BTIM_DIER_OFFSET, ATIM_DIER_UIE, 0); + + if (source & STM32_TIM_INT_SRC_OVERFLOW) + regval |= ATIM_DIER_UIE; +#ifdef HAVE_CH1IN + if (source & STM32_TIM_INT_SRC_CAPTURE_1) + regval |= ATIM_DIER_CC1IE; +#endif +#ifdef HAVE_CH2IN + if (source & STM32_TIM_INT_SRC_CAPTURE_2) + regval |= ATIM_DIER_CC1IE; +#endif +#ifdef HAVE_CH3IN + if (source & STM32_TIM_INT_SRC_CAPTURE_3) + regval |= ATIM_DIER_CC1IE; +#endif +#ifdef HAVE_CH4IN + if (source & STM32_TIM_INT_SRC_CAPTURE_4) + regval |= ATIM_DIER_CC1IE; +#endif + stm32_modifyreg16(dev, STM32_BTIM_DIER_OFFSET,mask, 0); + } static void stm32_tim_ackint(FAR struct stm32_tim_dev_s *dev, int source) { - stm32_putreg16(dev, STM32_BTIM_SR_OFFSET, ~ATIM_SR_UIF); + uint16_t mask = 0; + + if (source & STM32_TIM_INT_SRC_OVERFLOW) + regval |= ATIM_SR_UIF; +#ifdef HAVE_CH1IN + if (source & STM32_TIM_INT_SRC_CAPTURE_1) + regval |= ATIM_SR_CC1IF; +#endif +#ifdef HAVE_CH2IN + if (source & STM32_TIM_INT_SRC_CAPTURE_2) + regval |= ATIM_SR_CC2IF; +#endif +#ifdef HAVE_CH3IN + if (source & STM32_TIM_INT_SRC_CAPTURE_3) + regval |= ATIM_SR_CC3IF; +#endif +#ifdef HAVE_CH4IN + if (source & STM32_TIM_INT_SRC_CAPTURE_4) + regval |= ATIM_SR_CC4IF; +#endif + + stm32_putreg16(dev, STM32_BTIM_SR_OFFSET, ~mask); + +} + +static int stm32_tim_getintsrc(FAR struct stm32_tim_dev_s *dev, int source) +{ + uint16_t regval = 0; + int source = 0; + + regval = stm32_getreg16(dev, STM32_BTIM_SR_OFFSET); + + if (regval & ATIM_SR_UIF) + source |= STM32_TIM_INT_SRC_OVERFLOW; +#ifdef HAVE_CH1IN + if (regval & ATIM_SR_CC1IF) + source |= STM32_TIM_INT_SRC_CAPTURE_1; +#endif +#ifdef HAVE_CH2IN + if (regval & ATIM_SR_CC2IF) + source |= STM32_TIM_INT_SRC_CAPTURE_2; +#endif +#ifdef HAVE_CH3IN + if (regval & ATIM_SR_CC3IF) + source |= STM32_TIM_INT_SRC_CAPTURE_3; +#endif +#ifdef HAVE_CH4IN + if (regval & ATIM_SR_CC4IF) + source |= STM32_TIM_INT_SRC_CAPTURE_4; +#endif + + return source; + } /************************************************************************************ @@ -918,19 +1035,69 @@ static int stm32_tim_getcapture(FAR struct stm32_tim_dev_s *dev, uint8_t channel switch (channel) { +#ifdef HAVE_CH1IN case 1: return stm32_getreg32(dev, STM32_GTIM_CCR1_OFFSET); +#endif +#ifdef HAVE_CH1IN case 2: return stm32_getreg32(dev, STM32_GTIM_CCR2_OFFSET); +#endif +#ifdef HAVE_CH1IN case 3: return stm32_getreg32(dev, STM32_GTIM_CCR3_OFFSET); +#endif +#ifdef HAVE_CH1IN case 4: return stm32_getreg32(dev, STM32_GTIM_CCR4_OFFSET); +#endif } return ERROR; } +static bool stm32_tim_captureoverflow(FAR struct stm32_tim_dev_s *dev, int channel, + bool clear) +{ + uint16_t regval; + uint16_t mask; + ASSERT(dev); + + switch (channel) + { +#ifdef HAVE_CH1IN + case 1: + mask = GTIM_SR_CC1OF; + break; +#endif +#ifdef HAVE_CH2IN + case 2: + mask = GTIM_SR_CC2IF; + break; +#endif +#ifdef HAVE_CH3IN + case 3: + mask = GTIM_SR_CC3IF; + break; +#endif +#ifdef HAVE_CH4IN + case 4: + mask = GTIM_SR_CC4IF; + break; +#endif + default: + return ERROR; + } + regval = stm32_getreg16(dev, STM32_GTIM_SR_OFFSET); + if (regval & mask) + { + if (clear) + stm32_putreg16(dev, STM32_GTIM_SR_OFFSET, ~mask); + return 1; + } + return 0; +} + /************************************************************************************ * Advanced Functions ************************************************************************************/ @@ -949,10 +1116,12 @@ struct stm32_tim_ops_s stm32_tim_ops = .setchannel = &stm32_tim_setchannel, .setcompare = &stm32_tim_setcompare, .getcapture = &stm32_tim_getcapture, + .captureoverflow= &stm32_tim_captureoverflow, .setisr = &stm32_tim_setisr, .enableint = &stm32_tim_enableint, .disableint = &stm32_tim_disableint, - .ackint = &stm32_tim_ackint + .ackint = &stm32_tim_ackint, + .getintsrc = &stm32_tim_getintsrc }; #ifdef CONFIG_STM32_TIM2 diff --git a/arch/arm/src/stm32/stm32_tim.h b/arch/arm/src/stm32/stm32_tim.h index 3efee9eec6c..2c3c9962819 100644 --- a/arch/arm/src/stm32/stm32_tim.h +++ b/arch/arm/src/stm32/stm32_tim.h @@ -122,6 +122,20 @@ typedef enum } stm32_tim_mode_t; +/* TIM Sources */ + +typedef enum +{ + /* One of the following */ + + STM32_TIM_INT_SRC_OVERFLOW = 0x0001, + STM32_TIM_INT_SRC_CAPTURE_1 = 0x0002, + STM32_TIM_INT_SRC_CAPTURE_2 = 0x0004, + STM32_TIM_INT_SRC_CAPTURE_3 = 0x0008, + STM32_TIM_INT_SRC_CAPTURE_4 = 0x0010 + +} stm32_tim_source_t; + /* TIM Channel Modes */ From f88e62c8e9b0a0e74c84439fa7227e75c1a77cf0 Mon Sep 17 00:00:00 2001 From: pnb Date: Tue, 22 Dec 2015 12:21:03 +0100 Subject: [PATCH 07/16] continue capture --- arch/arm/src/stm32/Kconfig | 130 +++++ arch/arm/src/stm32/stm32_capture.c | 692 +++++++++++++++++++++++++ arch/arm/src/stm32/stm32_capture.h | 779 +++++++++++++++++++++++++++++ 3 files changed, 1601 insertions(+) create mode 100644 arch/arm/src/stm32/stm32_capture.c create mode 100644 arch/arm/src/stm32/stm32_capture.h diff --git a/arch/arm/src/stm32/Kconfig b/arch/arm/src/stm32/Kconfig index 11a6ca3ff92..07218af0655 100644 --- a/arch/arm/src/stm32/Kconfig +++ b/arch/arm/src/stm32/Kconfig @@ -5120,6 +5120,136 @@ config STM32_TIM14_DAC2 endchoice +config STM32_TIM1_CAP + bool "TIM1 Capture + default n + depends on STM32_HAVE_TIM1 + ---help--- + Reserve timer 1 for use by Capture + + Timer devices may be used for different purposes. One special purpose is + to capture input. + +config STM32_TIM1_CAP + bool "TIM1 Capture + default n + depends on STM32_HAVE_TIM1 + ---help--- + Reserve timer 1 for use by Capture + + Timer devices may be used for different purposes. One special purpose is + to capture input. + +config STM32_TIM2_CAP + bool "TIM2 Capture + default n + depends on STM32_HAVE_TIM2 + ---help--- + Reserve timer 2 for use by Capture + + Timer devices may be used for different purposes. One special purpose is + to capture input. + +config STM32_TIM3_CAP + bool "TIM3 Capture + default n + depends on STM32_HAVE_TIM3 + ---help--- + Reserve timer 3 for use by Capture + + Timer devices may be used for different purposes. One special purpose is + to capture input. + +config STM32_TIM4_CAP + bool "TIM4 Capture + default n + depends on STM32_HAVE_TIM4 + ---help--- + Reserve timer 4 for use by Capture + + Timer devices may be used for different purposes. One special purpose is + to capture input. + +config STM32_TIM5_CAP + bool "TIM5 Capture + default n + depends on STM32_HAVE_TIM5 + ---help--- + Reserve timer 5 for use by Capture + + Timer devices may be used for different purposes. One special purpose is + to capture input. + +config STM32_TIM8_CAP + bool "TIM8 Capture + default n + depends on STM32_HAVE_TIM8 + ---help--- + Reserve timer 8 for use by Capture + + Timer devices may be used for different purposes. One special purpose is + to capture input. + +config STM32_TIM9_CAP + bool "TIM9 Capture + default n + depends on STM32_HAVE_TIM9 + ---help--- + Reserve timer 9 for use by Capture + + Timer devices may be used for different purposes. One special purpose is + to capture input. + +config STM32_TIM10_CAP + bool "TIM10 Capture + default n + depends on STM32_HAVE_TIM10 + ---help--- + Reserve timer 10 for use by Capture + + Timer devices may be used for different purposes. One special purpose is + to capture input. + +config STM32_TIM11_CAP + bool "TIM11 Capture + default n + depends on STM32_HAVE_TIM11 + ---help--- + Reserve timer 11 for use by Capture + + Timer devices may be used for different purposes. One special purpose is + to capture input. + +config STM32_TIM12_CAP + bool "TIM12 Capture + default n + depends on STM32_HAVE_TIM12 + ---help--- + Reserve timer 12 for use by Capture + + Timer devices may be used for different purposes. One special purpose is + to capture input. + +config STM32_TIM13_CAP + bool "TIM13 Capture + default n + depends on STM32_HAVE_TIM13 + ---help--- + Reserve timer 13 for use by Capture + + Timer devices may be used for different purposes. One special purpose is + to capture input. + +config STM32_TIM14_CAP + bool "TIM14 Capture + default n + depends on STM32_HAVE_TIM14 + ---help--- + Reserve timer 14 for use by Capture + + Timer devices may be used for different purposes. One special purpose is + to capture input. + menu "ADC Configuration" depends on STM32_ADC diff --git a/arch/arm/src/stm32/stm32_capture.c b/arch/arm/src/stm32/stm32_capture.c new file mode 100644 index 00000000000..fd85d2d3fab --- /dev/null +++ b/arch/arm/src/stm32/stm32_capture.c @@ -0,0 +1,692 @@ +/************************************************************************************ + * arm/arm/src/stm32/stm32_capture.c + * + * Copyright (C) 2015 Bouteville Pierre-Noel. All rights reserved. + * Author: Bouteville Pierre-Noel + * + * 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 NuttX 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 OWNER 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. + * + ************************************************************************************/ + +/************************************************************************************ + * Included Files + ************************************************************************************/ + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + +#include "chip.h" +#include "up_internal.h" +#include "up_arch.h" + +#include "stm32.h" +#include "stm32_gpio.h" +#include "stm32_capture.h" + +/************************************************************************************ + * Private Types + ************************************************************************************/ +/* Configuration ********************************************************************/ + +#if defined(GPIO_TIM1_CH1IN) || defined(GPIO_TIM2_CH1IN) || defined(GPIO_TIM3_CH1IN) || \ + defined(GPIO_TIM4_CH1IN) || defined(GPIO_TIM5_CH1IN) || defined(GPIO_TIM8_CH1IN) || \ + defined(GPIO_TIM9_CH1IN) || defined(GPIO_TIM10_CH1IN) || defined(GPIO_TIM11_CH1IN) || \ + defined(GPIO_TIM12_CH1IN) || defined(GPIO_TIM13_CH1IN) || defined(GPIO_TIM14_CH1IN) +# define HAVE_CH1IN 1 +#endif + +#if defined(GPIO_TIM1_CH2IN) || defined(GPIO_TIM2_CH2IN) || defined(GPIO_TIM3_CH2IN) || \ + defined(GPIO_TIM4_CH2IN) || defined(GPIO_TIM5_CH2IN) || defined(GPIO_TIM8_CH2IN) || \ + defined(GPIO_TIM9_CH2IN) || defined(GPIO_TIM12_CH2IN) +# define HAVE_CH2IN 1 +#endif + +#if defined(GPIO_TIM1_CH3IN) || defined(GPIO_TIM2_CH3IN) || defined(GPIO_TIM3_CH3IN) || \ + defined(GPIO_TIM4_CH3IN) || defined(GPIO_TIM5_CH3IN) || defined(GPIO_TIM8_CH3IN) +# define HAVE_CH3IN 1 +#endif + +#if defined(GPIO_TIM1_CH4IN) || defined(GPIO_TIM2_CH4IN) || defined(GPIO_TIM3_CH4IN) || \ + defined(GPIO_TIM4_CH4IN) || defined(GPIO_TIM5_CH4IN) || defined(GPIO_TIM8_CH4IN) +# define HAVE_CH4IN 1 +#endif + +#if defined(CONFIG_STM32_TIM1_CAP) || defined(CONFIG_STM32_TIM1_CAP) +#define HAVE_ADANCED_TIM 1 +#endif + +/* This module then only compiles if there are enabled timers that are not intended for + * some other purpose. + */ + +#if defined(CONFIG_STM32_TIM1_CAP) || defined(CONFIG_STM32_TIM2_CAP) || defined(CONFIG_STM32_TIM3_CAP) || \ + defined(CONFIG_STM32_TIM4_CAP) || defined(CONFIG_STM32_TIM5_CAP) || defined(CONFIG_STM32_TIM8_CAP) || \ + defined(CONFIG_STM32_TIM9_CAP) || defined(CONFIG_STM32_TIM10_CAP) || defined(CONFIG_STM32_TIM11_CAP) || \ + defined(CONFIG_STM32_TIM12_CAP) || defined(CONFIG_STM32_TIM13_CAP) || defined(CONFIG_STM32_TIM14_CAP) + +/************************************************************************************ + * Private Types + ************************************************************************************/ + +struct stm32_cap_channel_s +{ + uint8_t ch_id; + uint16_t ccmr; + uint32_t gpio; +} + +/* TIM Device Structure */ + +struct stm32_cap_priv_s +{ + const struct stm32_cap_ops_s *ops; + const uint32_t base; /* TIMn base address */ + const int irq; /* irq vector */ +#define HAVE_ADANCED_TIM 1 + const int irq_of; /* irq timer overflow is deferent in advanced timer */ +#endif + const stm32_cap_channel_s channels[CAP_NCHANNELS]; +}; + +/************************************************************************************ + * Private Functions + ************************************************************************************/ + +/* Get a 16-bit register value by offset */ + +static inline uint16_t stm32_getreg16(FAR struct stm32_cap_priv_s *priv, + uint8_t offset) +{ + return getreg16(priv->base + offset); +} + +/* Put a 16-bit register value by offset */ + +static inline void stm32_putreg16(FAR struct stm32_cap_priv_s *priv, uint8_t offset, + uint16_t value) +{ + putreg16(value, priv->base + offset); +} + +/* Modify a 16-bit register value by offset */ + +static inline void stm32_modifyreg16(FAR struct stm32_cap_priv_s *priv, + uint8_t offset, uint16_t clearbits, + uint16_t setbits) +{ + modifyreg16(priv->base + offset, clearbits, setbits); +} + +/* Get a 32-bit register value by offset. This applies only for the STM32 F4 + * 32-bit registers (CNT, ARR, CRR1-4) in the 32-bit timers TIM2-5. + */ + +static inline uint32_t stm32_getreg32(FAR struct stm32_cap_priv_s *priv, + uint8_t offset) +{ + return getreg32(priv->base + offset); +} + +/* Put a 32-bit register value by offset. This applies only for the STM32 F4 + * 32-bit registers (CNT, ARR, CRR1-4) in the 32-bit timers TIM2-5. + */ + +static inline void stm32_putreg32(FAR struct stm32_cap_priv_s *priv, uint8_t offset, + uint32_t value) +{ + putreg32(value, priv->base + offset); +} + +/************************************************************************************ + * Basic Functions + ************************************************************************************/ + +static int stm32_cap_setclock(FAR struct stm32_cap_dev_s *dev, stm32_cap_clk_src_t clk_src, + uint32_t prescaler) +{ + struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; + uint16_t regval = 0; + + if (prescaler == 0) + { + //disable Timer + stm32_modifyreg16(priv, STM32_BTIM_CR1_OFFSET,ATIM_CR1_CEN,0); + return 0; + } + + /* We need to decrement value for '1', but only, if we are allowed to + * not to cause underflow. Check for overflow. + */ + + if (prescaler > 0) + prescaler--; + + if (prescaler > 0xffff) + prescaler = 0xffff; + + + switch(clk_src) + { + case STM32_CAP_CLK_INT: + regval = GTIM_SMCR_DISAB; + break; + + case STM32_CAP_CLK_EXT: + regval = GTIM_SMCR_EXTCLK1 + break; + + /* TODO: Add other case */ + + default: + return ERROR; + } + + stm32_modifyreg16(priv, STM32_BTIM_EGR_OFFSET, GTIM_SMCR_SMS_MASK, regval ); + + // Set Maximum + stm32_putreg32(priv, STM32_BTIM_ARR_OFFSET, period); + + // Set prescaler + stm32_putreg16(priv, STM32_BTIM_PSC_OFFSET, prescaler); + + //reset counter timer + stm32_modifyreg16(priv, STM32_BTIM_EGR_OFFSET,0,BTIM_EGR_UG); + + //enable timer + stm32_modifyreg16(priv, STM32_BTIM_CR1_OFFSET,0,BTIM_CR1_CEN); + +#ifdef HAVE_ADANCED_TIM + /* Advanced registers require Main Output Enable */ + if ((priv->base == STM32_TIM1_BASE) || (priv->base == STM32_TIM8_BASE)) + { + stm32_modifyreg16(priv, STM32_ATIM_BDTR_OFFSET, 0, ATIM_BDTR_MOE); + } +#endif + + return prescaler; +} + +static int stm32_cap_setisr(FAR struct stm32_cap_dev_s *dev, xcpt_t handler); +{ + struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; + int irq; +#ifdef HAVE_ADANCED_TIM + int irq_of; +#endif + + ASSERT(dev); + + irq = priv->irq; +#ifdef HAVE_ADANCED_TIM + irq_of = priv->irq_of; +#endif + + /* Disable interrupt when callback is removed */ + + if (!handler) + { + up_disable_irq(irq); + irq_detach(irq); +#ifdef HAVE_ADANCED_TIM + if (priv->irq_of) + { + up_disable_irq(irq_of); + irq_detach(irq_of); + } +#endif + return OK; + } + + /* Otherwise set callback and enable interrupt */ + + irq_attach(irq, handler); + up_enable_irq(irq); + +#ifdef HAVE_ADANCED_TIM + if (priv->irq_of) + { + irq_attach(priv->irq_of, handler); + up_enable_irq(priv->irq_of); + } +#endif + +#ifdef CONFIG_ARCH_IRQPRIO + /* Set the interrupt priority */ + + up_prioritize_irq(irq, NVIC_SYSH_PRIORITY_DEFAULT); + +# ifdef HAVE_ADANCED_TIM + if (priv->irq_of) + { + up_prioritize_irq(irq_of, NVIC_SYSH_PRIORITY_DEFAULT); + } +# endif + +#endif + + return OK; +} + + +static void stm32_cap_enableint(FAR struct stm32_cap_dev_s *dev, + stm32_cap_flags_t src, bool on) +{ + struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; + uint16_t mask; + ASSERT(dev); + + if (src & STM32_TIM_FLAG_IRQ_OVERFLOW) + regval |= ATIM_DIER_UIE; + if (src & STM32_TIM_FLAG_IRQ_CAPTURE_1) + regval |= ATIM_DIER_CC1IE; + if (src & STM32_TIM_FLAG_IRQ_CAPTURE_2) + regval |= ATIM_DIER_CC1IE; + if (src & STM32_TIM_FLAG_IRQ_CAPTURE_3) + regval |= ATIM_DIER_CC1IE; + if (src & STM32_TIM_FLAG_IRQ_CAPTURE_4) + regval |= ATIM_DIER_CC1IE; + + /* Not IRQ on channel overflow */ + + if (on) + stm32_modifyreg16(priv, STM32_BTIM_DIER_OFFSET,0,mask); + else + stm32_modifyreg16(priv, STM32_BTIM_DIER_OFFSET,mask,0); + +} + +static void stm32_cap_ackflags(FAR struct stm32_cap_dev_s *dev, int src) +{ + struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; + uint16_t mask = 0; + + if (src & STM32_TIM_FLAG_IRQ_OVERFLOW) + regval |= ATIM_SR_UIF; + + if (src & STM32_TIM_FLAG_IRQ_CAPTURE_1) + regval |= ATIM_SR_CC1IF; + if (src & STM32_TIM_FLAG_IRQ_CAPTURE_2) + regval |= ATIM_SR_CC2IF; + if (src & STM32_TIM_FLAG_IRQ_CAPTURE_3) + regval |= ATIM_SR_CC3IF; + if (src & STM32_TIM_FLAG_IRQ_CAPTURE_4) + regval |= ATIM_SR_CC4IF; + + if (src & STM32_TIM_FLAG_OF_CAPTURE_1) + regval |= ATIM_SR_CC1OF; + if (src & STM32_TIM_FLAG_OF_CAPTURE_2) + regval |= ATIM_SR_CC2OF; + if (src & STM32_TIM_FLAG_OF_CAPTURE_3) + regval |= ATIM_SR_CC3OF; + if (src & STM32_TIM_FLAG_OF_CAPTURE_4) + regval |= ATIM_SR_CC4OF; + + stm32_putreg16(priv, STM32_BTIM_SR_OFFSET, ~mask); + +} + +static stm32_cap_flags_t stm32_cap_getint(FAR struct stm32_cap_dev_s *dev) +{ + struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; + uint16_t regval = 0; + stm32_cap_flags_t src = 0; + + regval = stm32_getreg16(priv, STM32_BTIM_SR_OFFSET); + + if (regval & ATIM_SR_UIF) + src |= STM32_TIM_FLAG_IRQ_OVERFLOW; + + if (regval & ATIM_SR_CC1IF) + src |= STM32_TIM_FLAG_IRQ_CAPTURE_1; + if (regval & ATIM_SR_CC2IF) + src |= STM32_TIM_FLAG_IRQ_CAPTURE_2; + if (regval & ATIM_SR_CC3IF) + src |= STM32_TIM_FLAG_IRQ_CAPTURE_3; + if (regval & ATIM_SR_CC4IF) + src |= STM32_TIM_FLAG_IRQ_CAPTURE_4; + + if (regval & ATIM_SR_CC1OF) + src |= STM32_TIM_FLAG_OF_CAPTURE_1; + if (regval & ATIM_SR_CC2OF) + src |= STM32_TIM_FLAG_OF_CAPTURE_2; + if (regval & ATIM_SR_CC3OF) + src |= STM32_TIM_FLAG_OF_CAPTURE_3; + if (regval & ATIM_SR_CC4OF) + src |= STM32_TIM_FLAG_OF_CAPTURE_4; + + return src; + +} + +/************************************************************************************ + * General Functions + ************************************************************************************/ + +static int stm32_cap_setchannel(FAR struct stm32_cap_dev_s *dev, uint8_t channel, + stm32_cap_ch_cfg_t edge) +{ + int i; + struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; + uint32_t gpio = 0; + uint16_t mask; + uint16_t regval; + + ASSERT(dev); + + i = CAP_NCHANNELS; + while(i--) + { + if ( priv->channels[i].ch_id == channel ) + { + gpio = priv->channels[i].gpio; + break; + } + } + + if ( gpio == 0 ) + return ERROR; + + /* change to zero base index */ + channel--; + + + /* Set ccer */ + switch (cfg & STM32_CAP_EDGE_MASK) + { + case STM32_CAP_EDGE_DISABLED: + regval = 0; + break; + case STM32_CAP_EDGE_RISING: + regval = GTIM_CCER_CC1E; + break; + case STM32_CAP_EDGE_FALLING: + regval = GTIM_CCER_CC1E | GTIM_CCER_CC1P; + break; + case STM32_CAP_EDGE_BOTH: + regval = GTIM_CCER_CC1E | GTIM_CCER_CC1P | GTIM_CCER_CC1NP; + break; + default: + return ERROR; + } + + mask = (GTIM_CCER_CC1E | GTIM_CCER_CC1P | GTIM_CCER_CC1NP) + mask <<= (channel << 2); + regval <<= (channel << 2); + stm32_modifyreg16(priv,mask,regval); + + /* Set ccmr */ + + ccmr_val = cfg; + + if ( (ccmr_val & GTIM_CCMR1_CC1S_MASK ) == 0 ) + return ERROR; /* configured as output */ + + if ( (ccmr_val & GTIM_CCMR1_CC1S_MASK ) == 3 ) + return ERROR; /* Not implemented */ + + /* Define its position (shift) and get register offset */ + + mask = (GTIM_CCMR1_IC1F_MASK | GTIM_CCMR1_IC1PSC_MASK | GTIM_CCMR1_CC1S_MASK) + ccmr_val &= mask + + if (channel & 1) + { + ccmr_val <<= 8; + ccmr_mask <<= 8; + } + + if (channel < 2) + stm32_modifyreg16(priv,STM32_GTIM_CCMR1_OFFSET,ccmr_mask,ccmr_val); + else + stm32_modifyreg16(priv,STM32_GTIM_CCMR2_OFFSET,ccmr_mask,ccmr_val); + + /* set GPIO */ + + if ( (cfg & STM32_CAP_EDGE_MASK) == STM32_CAP_EDGE_DISABLED) + stm32_unconfiggpio(gpio); + else + stm32_configgpio(gpio); + + return OK; +} + + +static int stm32_cap_getcapture(FAR struct stm32_cap_dev_s *dev, uint8_t channel) +{ + struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; + ASSERT(dev); + + switch (channel) + { +#ifdef HAVE_CH1IN + case 1: + return stm32_getreg32(dev, STM32_GTIM_CCR1_OFFSET); +#endif +#ifdef HAVE_CH1IN + case 2: + return stm32_getreg32(dev, STM32_GTIM_CCR2_OFFSET); +#endif +#ifdef HAVE_CH1IN + case 3: + return stm32_getreg32(dev, STM32_GTIM_CCR3_OFFSET); +#endif +#ifdef HAVE_CH1IN + case 4: + return stm32_getreg32(dev, STM32_GTIM_CCR4_OFFSET); +#endif + } + + return ERROR; +} + + +/************************************************************************************ + * Advanced Functions + ************************************************************************************/ + +/* TODO: Advanced functions for the STM32_ATIM */ + +/************************************************************************************ + * Device Structures, Instantiation + ************************************************************************************/ + +struct stm32_cap_ops_s stm32_cap_ops = +{ + .setclock = &stm32_cap_setclock, + .setchannel = &stm32_cap_setchannel, + .getcapture = &stm32_cap_getcapture, + .setisr = &stm32_cap_setisr, + .enableint = &stm32_cap_enableint, + .ackflags = &stm32_cap_ackflags, + .getflags = &stm32_cap_getflags +}; + +#ifdef CONFIG_STM32_TIM2_CAP +const struct stm32_cap_priv_s stm32_tim2_priv = +{ + .ops = &stm32_cap_ops, + .base = STM32_TIM2_BASE, + .irg = STM32_IRQ_TIM2, +#define HAVE_ADANCED_TIM 1 + .irg_of = -1, +#endif + .gpio_clk = GPIO_TIM2_CLKIN; + .channels = { +#if defined(GPIO_TIM2_CH1IN) + .gpio = GPIO_TIM2_CH1IN; +#endif +#if defined(GPIO_TIM2_CH2IN) + .gpio = GPIO_TIM2_CH2IN; + .ccmr = ( GPIO_TIM2_CH2_ICF << ATIM_CCMR1_IC1F_SHIFT )|\ + ( GPIO_TIM2_CH2_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); +#endif +#if defined(GPIO_TIM2_CH3IN) + .gpio = GPIO_TIM2_CH3IN; + .ccmr = ( GPIO_TIM2_CH3_ICF << ATIM_CCMR1_IC1F_SHIFT )|\ + ( GPIO_TIM2_CH3_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); +#endif +#if defined(GPIO_TIM2_CH4IN) + .gpio = GPIO_TIM2_CH4IN; + .ccmr = ( GPIO_TIM2_CH4_ICF << ATIM_CCMR1_IC1F_SHIFT )|\ + ( GPIO_TIM2_CH4_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); +#endif + } +}; +#endif + + +/************************************************************************************ + * Public Function - Initialization + ************************************************************************************/ + +FAR struct stm32_cap_dev_s *stm32_cap_init(int timer) +{ + struct stm32_cap_priv_s *priv = NULL; + + /* Get structure and enable power */ + + switch (timer) + { +#ifdef CONFIG_STM32_TIM1_CAP + case 1: + priv = &stm32_tim1_priv; + modifyreg32(STM32_RCC_APB2ENR, 0, RCC_APB2ENR_TIM1EN); + break; +#endif +#ifdef CONFIG_STM32_TIM2_CAP + case 2: + priv = &stm32_tim2_priv; + modifyreg32(STM32_RCC_APB1ENR, 0, RCC_APB1ENR_TIM2EN); + break; +#endif +#ifdef CONFIG_STM32_TIM3_CAP + case 3: + priv = &stm32_tim3_priv; + modifyreg32(STM32_RCC_APB1ENR, 0, RCC_APB1ENR_TIM3EN); + break; +#endif +#ifdef CONFIG_STM32_TIM4_CAP + case 4: + priv = &stm32_tim4_priv; + modifyreg32(STM32_RCC_APB1ENR, 0, RCC_APB1ENR_TIM4EN); + break; +#endif +#ifdef CONFIG_STM32_TIM5_CAP + case 5: + priv = &stm32_tim5_priv; + modifyreg32(STM32_RCC_APB1ENR, 0, RCC_APB1ENR_TIM5EN); + break; +#endif +/* TIM6 and TIM7 cannot be used in capture */ +#ifdef CONFIG_STM32_TIM8_CAP + case 8: + priv = &stm32_tim8_priv; + modifyreg32(STM32_RCC_APB2ENR, 0, RCC_APB2ENR_TIM8EN); + break; +#endif +#ifdef CONFIG_STM32_TIM9_CAP + case 9: + priv = &stm32_tim9_priv; + modifyreg32(STM32_RCC_APB2ENR, 0, RCC_APB2ENR_TIM8EN); + break; +#endif + default: + return NULL; + } + + if (priv->gpio_clk) + stm32_configgpio(priv->gpio_clk) + + // disable timer while is not configured + stm32_modifyreg16(priv, STM32_BTIM_CR1_OFFSET, ATIM_CR1_CEN, 0); + + return (struct stm32_cap_dev_s *)priv; +} + + +int stm32_cap_deinit(FAR struct stm32_cap_dev_s * dev) +{ + struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; + ASSERT(dev); + + // disable timer while is not configured + stm32_modifyreg16(dev, STM32_BTIM_CR1_OFFSET, ATIM_CR1_CEN, 0); + + if (priv->gpio_clk) + stm32_unconfiggpio(priv->gpio_clk) + + switch (priv->base) + { +#ifdef CONFIG_STM32_TIM2_CAP + case STM32_TIM2_BASE: + modifyreg32(STM32_RCC_APB1ENR, RCC_APB1ENR_TIM2EN, 0); + break; +#endif +#ifdef CONFIG_STM32_TIM3_CAP + case STM32_TIM3_BASE: + modifyreg32(STM32_RCC_APB1ENR, RCC_APB1ENR_TIM3EN, 0); + break; +#endif +#ifdef CONFIG_STM32_TIM4_CAP + case STM32_TIM4_BASE: + modifyreg32(STM32_RCC_APB1ENR, RCC_APB1ENR_TIM4EN, 0); + break; +#endif +#ifdef CONFIG_STM32_TIM5_CAP + case STM32_TIM5_BASE: + modifyreg32(STM32_RCC_APB1ENR, RCC_APB1ENR_TIM5EN, 0); + break; +#endif +#ifdef CONFIG_STM32_TIM1_CAP + case STM32_TIM1_BASE: + modifyreg32(STM32_RCC_APB2ENR, RCC_APB2ENR_TIM1EN, 0); + break; +#endif +#ifdef CONFIG_STM32_TIM8_CAP + case STM32_TIM8_BASE: + modifyreg32(STM32_RCC_APB2ENR, RCC_APB2ENR_TIM8EN, 0); + break; +#endif + default: + return ERROR; + } + + return OK; +} + +#endif /* defined(CONFIG_STM32_TIM1 || ... || TIM8) */ diff --git a/arch/arm/src/stm32/stm32_capture.h b/arch/arm/src/stm32/stm32_capture.h new file mode 100644 index 00000000000..e3eb1f388ec --- /dev/null +++ b/arch/arm/src/stm32/stm32_capture.h @@ -0,0 +1,779 @@ +/************************************************************************************ + * arch/arm/src/stm32/stm32_capture.h + * + * Copyright (C) 2015 Bouteville Pierre-Noel. All rights reserved. + * Author: Bouteville Pierre-Noel + * + * 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 NuttX 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 OWNER 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 __ARCH_ARM_SRC_STM32_STM32_CAPTURE_H +#define __ARCH_ARM_SRC_STM32_STM32_CAPTURE_H + +/************************************************************************************ + * Included Files + ************************************************************************************/ + +#include + + +#include "chip.h" +#include +#include "chip/stm32_tim.h" + + +#ifdef CONFIG_STM32_TIM1_CHANNEL1 +# ifdef CONFIG_STM32_TIM1_CH1OUT +# define CAP_TIM1_CH1CFG GPIO_TIM1_CH1OUT +# else +# define CAP_TIM1_CH1CFG 0 +# endif +# define CAP_TIM1_CHANNEL1 1 +#else +# define CAP_TIM1_CHANNEL1 0 +#endif +#ifdef CONFIG_STM32_TIM1_CHANNEL2 +# ifdef CONFIG_STM32_TIM1_CH2OUT +# define CAP_TIM1_CH2CFG GPIO_TIM1_CH2OUT +# else +# define CAP_TIM1_CH2CFG 0 +# endif +# define CAP_TIM1_CHANNEL2 1 +#else +# define CAP_TIM1_CHANNEL2 0 +#endif +#ifdef CONFIG_STM32_TIM1_CHANNEL3 +# ifdef CONFIG_STM32_TIM1_CH3OUT +# define CAP_TIM1_CH3CFG GPIO_TIM1_CH3OUT +# else +# define CAP_TIM1_CH3CFG 0 +# endif +# define CAP_TIM1_CHANNEL3 1 +#else +# define CAP_TIM1_CHANNEL3 0 +#endif +#ifdef CONFIG_STM32_TIM1_CHANNEL4 +# ifdef CONFIG_STM32_TIM1_CH4OUT +# define CAP_TIM1_CH4CFG GPIO_TIM1_CH4OUT +# else +# define CAP_TIM1_CH4CFG 0 +# endif +# define CAP_TIM1_CHANNEL4 1 +#else +# define CAP_TIM1_CHANNEL4 0 +#endif +#define CAP_TIM1_NCHANNELS (CAP_TIM1_CHANNEL1 + CAP_TIM1_CHANNEL2 + \ + CAP_TIM1_CHANNEL3 + CAP_TIM1_CHANNEL4) + +#ifdef CONFIG_STM32_TIM2_CHANNEL1 +# ifdef CONFIG_STM32_TIM2_CH1OUT +# define CAP_TIM2_CH1CFG GPIO_TIM2_CH1OUT +# else +# define CAP_TIM2_CH1CFG 0 +# endif +# define CAP_TIM2_CHANNEL1 1 +#else +# define CAP_TIM2_CHANNEL1 0 +#endif +#ifdef CONFIG_STM32_TIM2_CHANNEL2 +# ifdef CONFIG_STM32_TIM2_CH2OUT +# define CAP_TIM2_CH2CFG GPIO_TIM2_CH2OUT +# else +# define CAP_TIM2_CH2CFG 0 +# endif +# define CAP_TIM2_CHANNEL2 1 +#else +# define CAP_TIM2_CHANNEL2 0 +#endif +#ifdef CONFIG_STM32_TIM2_CHANNEL3 +# ifdef CONFIG_STM32_TIM2_CH3OUT +# define CAP_TIM2_CH3CFG GPIO_TIM2_CH3OUT +# else +# define CAP_TIM2_CH3CFG 0 +# endif +# define CAP_TIM2_CHANNEL3 1 +#else +# define CAP_TIM2_CHANNEL3 0 +#endif +#ifdef CONFIG_STM32_TIM2_CHANNEL4 +# ifdef CONFIG_STM32_TIM2_CH4OUT +# define CAP_TIM2_CH4CFG GPIO_TIM2_CH4OUT +# else +# define CAP_TIM2_CH4CFG 0 +# endif +# define CAP_TIM2_CHANNEL4 1 +#else +# define CAP_TIM2_CHANNEL4 0 +#endif +#define CAP_TIM2_NCHANNELS (CAP_TIM2_CHANNEL1 + CAP_TIM2_CHANNEL2 + \ + CAP_TIM2_CHANNEL3 + CAP_TIM2_CHANNEL4) + +#ifdef CONFIG_STM32_TIM3_CHANNEL1 +# ifdef CONFIG_STM32_TIM3_CH1OUT +# define CAP_TIM3_CH1CFG GPIO_TIM3_CH1OUT +# else +# define CAP_TIM3_CH1CFG 0 +# endif +# define CAP_TIM3_CHANNEL1 1 +#else +# define CAP_TIM3_CHANNEL1 0 +#endif +#ifdef CONFIG_STM32_TIM3_CHANNEL2 +# ifdef CONFIG_STM32_TIM3_CH2OUT +# define CAP_TIM3_CH2CFG GPIO_TIM3_CH2OUT +# else +# define CAP_TIM3_CH2CFG 0 +# endif +# define CAP_TIM3_CHANNEL2 1 +#else +# define CAP_TIM3_CHANNEL2 0 +#endif +#ifdef CONFIG_STM32_TIM3_CHANNEL3 +# ifdef CONFIG_STM32_TIM3_CH3OUT +# define CAP_TIM3_CH3CFG GPIO_TIM3_CH3OUT +# else +# define CAP_TIM3_CH3CFG 0 +# endif +# define CAP_TIM3_CHANNEL3 1 +#else +# define CAP_TIM3_CHANNEL3 0 +#endif +#ifdef CONFIG_STM32_TIM3_CHANNEL4 +# ifdef CONFIG_STM32_TIM3_CH4OUT +# define CAP_TIM3_CH4CFG GPIO_TIM3_CH4OUT +# else +# define CAP_TIM3_CH4CFG 0 +# endif +# define CAP_TIM3_CHANNEL4 1 +#else +# define CAP_TIM3_CHANNEL4 0 +#endif +#define CAP_TIM3_NCHANNELS (CAP_TIM3_CHANNEL1 + CAP_TIM3_CHANNEL2 + \ + CAP_TIM3_CHANNEL3 + CAP_TIM3_CHANNEL4) + +#ifdef CONFIG_STM32_TIM4_CHANNEL1 +# ifdef CONFIG_STM32_TIM4_CH1OUT +# define CAP_TIM4_CH1CFG GPIO_TIM4_CH1OUT +# else +# define CAP_TIM4_CH1CFG 0 +# endif +# define CAP_TIM4_CHANNEL1 1 +#else +# define CAP_TIM4_CHANNEL1 0 +#endif +#ifdef CONFIG_STM32_TIM4_CHANNEL2 +# ifdef CONFIG_STM32_TIM4_CH2OUT +# define CAP_TIM4_CH2CFG GPIO_TIM4_CH2OUT +# else +# define CAP_TIM4_CH2CFG 0 +# endif +# define CAP_TIM4_CHANNEL2 1 +#else +# define CAP_TIM4_CHANNEL2 0 +#endif +#ifdef CONFIG_STM32_TIM4_CHANNEL3 +# ifdef CONFIG_STM32_TIM4_CH3OUT +# define CAP_TIM4_CH3CFG GPIO_TIM4_CH3OUT +# else +# define CAP_TIM4_CH3CFG 0 +# endif +# define CAP_TIM4_CHANNEL3 1 +#else +# define CAP_TIM4_CHANNEL3 0 +#endif +#ifdef CONFIG_STM32_TIM4_CHANNEL4 +# ifdef CONFIG_STM32_TIM4_CH4OUT +# define CAP_TIM4_CH4CFG GPIO_TIM4_CH4OUT +# else +# define CAP_TIM4_CH4CFG 0 +# endif +# define CAP_TIM4_CHANNEL4 1 +#else +# define CAP_TIM4_CHANNEL4 0 +#endif +#define CAP_TIM4_NCHANNELS (CAP_TIM4_CHANNEL1 + CAP_TIM4_CHANNEL2 + \ + CAP_TIM4_CHANNEL3 + CAP_TIM4_CHANNEL4) + +#ifdef CONFIG_STM32_TIM5_CHANNEL1 +# ifdef CONFIG_STM32_TIM5_CH1OUT +# define CAP_TIM5_CH1CFG GPIO_TIM5_CH1OUT +# else +# define CAP_TIM5_CH1CFG 0 +# endif +# define CAP_TIM5_CHANNEL1 1 +#else +# define CAP_TIM5_CHANNEL1 0 +#endif +#ifdef CONFIG_STM32_TIM5_CHANNEL2 +# ifdef CONFIG_STM32_TIM5_CH2OUT +# define CAP_TIM5_CH2CFG GPIO_TIM5_CH2OUT +# else +# define CAP_TIM5_CH2CFG 0 +# endif +# define CAP_TIM5_CHANNEL2 1 +#else +# define CAP_TIM5_CHANNEL2 0 +#endif +#ifdef CONFIG_STM32_TIM5_CHANNEL3 +# ifdef CONFIG_STM32_TIM5_CH3OUT +# define CAP_TIM5_CH3CFG GPIO_TIM5_CH3OUT +# else +# define CAP_TIM5_CH3CFG 0 +# endif +# define CAP_TIM5_CHANNEL3 1 +#else +# define CAP_TIM5_CHANNEL3 0 +#endif +#ifdef CONFIG_STM32_TIM5_CHANNEL4 +# ifdef CONFIG_STM32_TIM5_CH4OUT +# define CAP_TIM5_CH4CFG GPIO_TIM5_CH4OUT +# else +# define CAP_TIM5_CH4CFG 0 +# endif +# define CAP_TIM5_CHANNEL4 1 +#else +# define CAP_TIM5_CHANNEL4 0 +#endif +#define CAP_TIM5_NCHANNELS (CAP_TIM5_CHANNEL1 + CAP_TIM5_CHANNEL2 + \ + CAP_TIM5_CHANNEL3 + CAP_TIM5_CHANNEL4) + +#ifdef CONFIG_STM32_TIM8_CHANNEL1 +# ifdef CONFIG_STM32_TIM8_CH1OUT +# define CAP_TIM8_CH1CFG GPIO_TIM8_CH1OUT +# else +# define CAP_TIM8_CH1CFG 0 +# endif +# define CAP_TIM8_CHANNEL1 1 +#else +# define CAP_TIM8_CHANNEL1 0 +#endif +#ifdef CONFIG_STM32_TIM8_CHANNEL2 +# ifdef CONFIG_STM32_TIM8_CH2OUT +# define CAP_TIM8_CH2CFG GPIO_TIM8_CH2OUT +# else +# define CAP_TIM8_CH2CFG 0 +# endif +# define CAP_TIM8_CHANNEL2 1 +#else +# define CAP_TIM8_CHANNEL2 0 +#endif +#ifdef CONFIG_STM32_TIM8_CHANNEL3 +# ifdef CONFIG_STM32_TIM8_CH3OUT +# define CAP_TIM8_CH3CFG GPIO_TIM8_CH3OUT +# else +# define CAP_TIM8_CH3CFG 0 +# endif +# define CAP_TIM8_CHANNEL3 1 +#else +# define CAP_TIM8_CHANNEL3 0 +#endif +#ifdef CONFIG_STM32_TIM8_CHANNEL4 +# ifdef CONFIG_STM32_TIM8_CH4OUT +# define CAP_TIM8_CH4CFG GPIO_TIM8_CH4OUT +# else +# define CAP_TIM8_CH4CFG 0 +# endif +# define CAP_TIM8_CHANNEL4 1 +#else +# define CAP_TIM8_CHANNEL4 0 +#endif +#define CAP_TIM8_NCHANNELS (CAP_TIM8_CHANNEL1 + CAP_TIM8_CHANNEL2 + \ + CAP_TIM8_CHANNEL3 + CAP_TIM8_CHANNEL4) + +#ifdef CONFIG_STM32_TIM9_CHANNEL1 +# ifdef CONFIG_STM32_TIM9_CH1OUT +# define CAP_TIM9_CH1CFG GPIO_TIM9_CH1OUT +# else +# define CAP_TIM9_CH1CFG 0 +# endif +# define CAP_TIM9_CHANNEL1 1 +#else +# define CAP_TIM9_CHANNEL1 0 +#endif +#ifdef CONFIG_STM32_TIM9_CHANNEL2 +# ifdef CONFIG_STM32_TIM9_CH2OUT +# define CAP_TIM9_CH2CFG GPIO_TIM9_CH2OUT +# else +# define CAP_TIM9_CH2CFG 0 +# endif +# define CAP_TIM9_CHANNEL2 1 +#else +# define CAP_TIM9_CHANNEL2 0 +#endif +#ifdef CONFIG_STM32_TIM9_CHANNEL3 +# ifdef CONFIG_STM32_TIM9_CH3OUT +# define CAP_TIM9_CH3CFG GPIO_TIM9_CH3OUT +# else +# define CAP_TIM9_CH3CFG 0 +# endif +# define CAP_TIM9_CHANNEL3 1 +#else +# define CAP_TIM9_CHANNEL3 0 +#endif +#ifdef CONFIG_STM32_TIM9_CHANNEL4 +# ifdef CONFIG_STM32_TIM9_CH4OUT +# define CAP_TIM9_CH4CFG GPIO_TIM9_CH4OUT +# else +# define CAP_TIM9_CH4CFG 0 +# endif +# define CAP_TIM9_CHANNEL4 1 +#else +# define CAP_TIM9_CHANNEL4 0 +#endif +#define CAP_TIM9_NCHANNELS (CAP_TIM9_CHANNEL1 + CAP_TIM9_CHANNEL2 + \ + CAP_TIM9_CHANNEL3 + CAP_TIM9_CHANNEL4) + +#ifdef CONFIG_STM32_TIM10_CHANNEL1 +# ifdef CONFIG_STM32_TIM10_CH1OUT +# define CAP_TIM10_CH1CFG GPIO_TIM10_CH1OUT +# else +# define CAP_TIM10_CH1CFG 0 +# endif +# define CAP_TIM10_CHANNEL1 1 +#else +# define CAP_TIM10_CHANNEL1 0 +#endif +#ifdef CONFIG_STM32_TIM10_CHANNEL2 +# ifdef CONFIG_STM32_TIM10_CH2OUT +# define CAP_TIM10_CH2CFG GPIO_TIM10_CH2OUT +# else +# define CAP_TIM10_CH2CFG 0 +# endif +# define CAP_TIM10_CHANNEL2 1 +#else +# define CAP_TIM10_CHANNEL2 0 +#endif +#ifdef CONFIG_STM32_TIM10_CHANNEL3 +# ifdef CONFIG_STM32_TIM10_CH3OUT +# define CAP_TIM10_CH3CFG GPIO_TIM10_CH3OUT +# else +# define CAP_TIM10_CH3CFG 0 +# endif +# define CAP_TIM10_CHANNEL3 1 +#else +# define CAP_TIM10_CHANNEL3 0 +#endif +#ifdef CONFIG_STM32_TIM10_CHANNEL4 +# ifdef CONFIG_STM32_TIM10_CH4OUT +# define CAP_TIM10_CH4CFG GPIO_TIM10_CH4OUT +# else +# define CAP_TIM10_CH4CFG 0 +# endif +# define CAP_TIM10_CHANNEL4 1 +#else +# define CAP_TIM10_CHANNEL4 0 +#endif +#define CAP_TIM10_NCHANNELS (CAP_TIM10_CHANNEL1 + CAP_TIM10_CHANNEL2 + \ + CAP_TIM10_CHANNEL3 + CAP_TIM10_CHANNEL4) + +#ifdef CONFIG_STM32_TIM11_CHANNEL1 +# ifdef CONFIG_STM32_TIM11_CH1OUT +# define CAP_TIM11_CH1CFG GPIO_TIM11_CH1OUT +# else +# define CAP_TIM11_CH1CFG 0 +# endif +# define CAP_TIM11_CHANNEL1 1 +#else +# define CAP_TIM11_CHANNEL1 0 +#endif +#ifdef CONFIG_STM32_TIM11_CHANNEL2 +# ifdef CONFIG_STM32_TIM11_CH2OUT +# define CAP_TIM11_CH2CFG GPIO_TIM11_CH2OUT +# else +# define CAP_TIM11_CH2CFG 0 +# endif +# define CAP_TIM11_CHANNEL2 1 +#else +# define CAP_TIM11_CHANNEL2 0 +#endif +#ifdef CONFIG_STM32_TIM11_CHANNEL3 +# ifdef CONFIG_STM32_TIM11_CH3OUT +# define CAP_TIM11_CH3CFG GPIO_TIM11_CH3OUT +# else +# define CAP_TIM11_CH3CFG 0 +# endif +# define CAP_TIM11_CHANNEL3 1 +#else +# define CAP_TIM11_CHANNEL3 0 +#endif +#ifdef CONFIG_STM32_TIM11_CHANNEL4 +# ifdef CONFIG_STM32_TIM11_CH4OUT +# define CAP_TIM11_CH4CFG GPIO_TIM11_CH4OUT +# else +# define CAP_TIM11_CH4CFG 0 +# endif +# define CAP_TIM11_CHANNEL4 1 +#else +# define CAP_TIM11_CHANNEL4 0 +#endif +#define CAP_TIM11_NCHANNELS (CAP_TIM11_CHANNEL1 + CAP_TIM11_CHANNEL2 + \ + CAP_TIM11_CHANNEL3 + CAP_TIM11_CHANNEL4) + +#ifdef CONFIG_STM32_TIM12_CHANNEL1 +# ifdef CONFIG_STM32_TIM12_CH1OUT +# define CAP_TIM12_CH1CFG GPIO_TIM12_CH1OUT +# else +# define CAP_TIM12_CH1CFG 0 +# endif +# define CAP_TIM12_CHANNEL1 1 +#else +# define CAP_TIM12_CHANNEL1 0 +#endif +#ifdef CONFIG_STM32_TIM12_CHANNEL2 +# ifdef CONFIG_STM32_TIM12_CH2OUT +# define CAP_TIM12_CH2CFG GPIO_TIM12_CH2OUT +# else +# define CAP_TIM12_CH2CFG 0 +# endif +# define CAP_TIM12_CHANNEL2 1 +#else +# define CAP_TIM12_CHANNEL2 0 +#endif +#ifdef CONFIG_STM32_TIM12_CHANNEL3 +# ifdef CONFIG_STM32_TIM12_CH3OUT +# define CAP_TIM12_CH3CFG GPIO_TIM12_CH3OUT +# else +# define CAP_TIM12_CH3CFG 0 +# endif +# define CAP_TIM12_CHANNEL3 1 +#else +# define CAP_TIM12_CHANNEL3 0 +#endif +#ifdef CONFIG_STM32_TIM12_CHANNEL4 +# ifdef CONFIG_STM32_TIM12_CH4OUT +# define CAP_TIM12_CH4CFG GPIO_TIM12_CH4OUT +# else +# define CAP_TIM12_CH4CFG 0 +# endif +# define CAP_TIM12_CHANNEL4 1 +#else +# define CAP_TIM12_CHANNEL4 0 +#endif +#define CAP_TIM12_NCHANNELS (CAP_TIM12_CHANNEL1 + CAP_TIM12_CHANNEL2 + \ + CAP_TIM12_CHANNEL3 + CAP_TIM12_CHANNEL4) + +#ifdef CONFIG_STM32_TIM13_CHANNEL1 +# ifdef CONFIG_STM32_TIM13_CH1OUT +# define CAP_TIM13_CH1CFG GPIO_TIM13_CH1OUT +# else +# define CAP_TIM13_CH1CFG 0 +# endif +# define CAP_TIM13_CHANNEL1 1 +#else +# define CAP_TIM13_CHANNEL1 0 +#endif +#ifdef CONFIG_STM32_TIM13_CHANNEL2 +# ifdef CONFIG_STM32_TIM13_CH2OUT +# define CAP_TIM13_CH2CFG GPIO_TIM13_CH2OUT +# else +# define CAP_TIM13_CH2CFG 0 +# endif +# define CAP_TIM13_CHANNEL2 1 +#else +# define CAP_TIM13_CHANNEL2 0 +#endif +#ifdef CONFIG_STM32_TIM13_CHANNEL3 +# ifdef CONFIG_STM32_TIM13_CH3OUT +# define CAP_TIM13_CH3CFG GPIO_TIM13_CH3OUT +# else +# define CAP_TIM13_CH3CFG 0 +# endif +# define CAP_TIM13_CHANNEL3 1 +#else +# define CAP_TIM13_CHANNEL3 0 +#endif +#ifdef CONFIG_STM32_TIM13_CHANNEL4 +# ifdef CONFIG_STM32_TIM13_CH4OUT +# define CAP_TIM13_CH4CFG GPIO_TIM13_CH4OUT +# else +# define CAP_TIM13_CH4CFG 0 +# endif +# define CAP_TIM13_CHANNEL4 1 +#else +# define CAP_TIM13_CHANNEL4 0 +#endif +#define CAP_TIM13_NCHANNELS (CAP_TIM13_CHANNEL1 + CAP_TIM13_CHANNEL2 + \ + CAP_TIM13_CHANNEL3 + CAP_TIM13_CHANNEL4) + +#ifdef CONFIG_STM32_TIM14_CHANNEL1 +# ifdef CONFIG_STM32_TIM14_CH1OUT +# define CAP_TIM14_CH1CFG GPIO_TIM14_CH1OUT +# else +# define CAP_TIM14_CH1CFG 0 +# endif +# define CAP_TIM14_CHANNEL1 1 +#else +# define CAP_TIM14_CHANNEL1 0 +#endif +#ifdef CONFIG_STM32_TIM14_CHANNEL2 +# ifdef CONFIG_STM32_TIM14_CH2OUT +# define CAP_TIM14_CH2CFG GPIO_TIM14_CH2OUT +# else +# define CAP_TIM14_CH2CFG 0 +# endif +# define CAP_TIM14_CHANNEL2 1 +#else +# define CAP_TIM14_CHANNEL2 0 +#endif +#ifdef CONFIG_STM32_TIM14_CHANNEL3 +# ifdef CONFIG_STM32_TIM14_CH3OUT +# define CAP_TIM14_CH3CFG GPIO_TIM14_CH3OUT +# else +# define CAP_TIM14_CH3CFG 0 +# endif +# define CAP_TIM14_CHANNEL3 1 +#else +# define CAP_TIM14_CHANNEL3 0 +#endif +#ifdef CONFIG_STM32_TIM14_CHANNEL4 +# ifdef CONFIG_STM32_TIM14_CH4OUT +# define CAP_TIM14_CH4CFG GPIO_TIM14_CH4OUT +# else +# define CAP_TIM14_CH4CFG 0 +# endif +# define CAP_TIM14_CHANNEL4 1 +#else +# define CAP_TIM14_CHANNEL4 0 +#endif +#define CAP_TIM14_NCHANNELS (CAP_TIM14_CHANNEL1 + CAP_TIM14_CHANNEL2 + \ + CAP_TIM14_CHANNEL3 + CAP_TIM14_CHANNEL4) + +#ifdef CONFIG_STM32_TIM15_CHANNEL1 +# ifdef CONFIG_STM32_TIM15_CH1OUT +# define CAP_TIM15_CH1CFG GPIO_TIM15_CH1OUT +# else +# define CAP_TIM15_CH1CFG 0 +# endif +# define CAP_TIM15_CHANNEL1 1 +#else +# define CAP_TIM15_CHANNEL1 0 +#endif +#ifdef CONFIG_STM32_TIM15_CHANNEL2 +# ifdef CONFIG_STM32_TIM15_CH2OUT +# define CAP_TIM15_CH2CFG GPIO_TIM15_CH2OUT +# else +# define CAP_TIM15_CH2CFG 0 +# endif +# define CAP_TIM15_CHANNEL2 1 +#else +# define CAP_TIM15_CHANNEL2 0 +#endif +#define CAP_TIM15_NCHANNELS (CAP_TIM15_CHANNEL1 + CAP_TIM15_CHANNEL2) + +#ifdef CONFIG_STM32_TIM16_CHANNEL1 +# ifdef CONFIG_STM32_TIM16_CH1OUT +# define CAP_TIM16_CH1CFG GPIO_TIM16_CH1OUT +# else +# define CAP_TIM16_CH1CFG 0 +# endif +# define CAP_TIM16_CHANNEL1 1 +#else +# define CAP_TIM16_CHANNEL1 0 +#endif +#define CAP_TIM16_NCHANNELS CAP_TIM16_CHANNEL1 + +#ifdef CONFIG_STM32_TIM17_CHANNEL1 +# ifdef CONFIG_STM32_TIM17_CH1OUT +# define CAP_TIM17_CH1CFG GPIO_TIM17_CH1OUT +# else +# define CAP_TIM17_CH1CFG 0 +# endif +# define CAP_TIM17_CHANNEL1 1 +#else +# define CAP_TIM17_CHANNEL1 0 +#endif +#define CAP_TIM17_NCHANNELS CAP_TIM17_CHANNEL1 + +#define CAP_MAX(a, b) ((a) > (b) ? (a) : (b)) + +#define CAP_NCHANNELS CAP_MAX(CAP_TIM1_NCHANNELS, \ + CAP_MAX(CAP_TIM2_NCHANNELS, \ + CAP_MAX(CAP_TIM3_NCHANNELS, \ + CAP_MAX(CAP_TIM4_NCHANNELS, \ + CAP_MAX(CAP_TIM5_NCHANNELS, \ + CAP_MAX(CAP_TIM8_NCHANNELS, \ + CAP_MAX(CAP_TIM9_NCHANNELS, \ + CAP_MAX(CAP_TIM10_NCHANNELS, \ + CAP_MAX(CAP_TIM11_NCHANNELS, \ + CAP_MAX(CAP_TIM12_NCHANNELS, \ + CAP_MAX(CAP_TIM13_NCHANNELS, \ + CAP_MAX(CAP_TIM14_NCHANNELS, \ + CAP_MAX(CAP_TIM15_NCHANNELS, \ + CAP_MAX(CAP_TIM16_NCHANNELS, \ + CAP_TIM17_NCHANNELS)))))))))))))) + +#endif + +/************************************************************************************ + * Pre-processor Definitions + ************************************************************************************/ +/* Helpers **************************************************************************/ + +#define STM32_TIM_SETCLOCK(d,freq) ((d)->ops->setclock(d,freq)) +#define STM32_TIM_SETCHANNEL(d,ch,mode) ((d)->ops->setchannel(d,ch,mode)) +#define STM32_TIM_GETCAPTURE(d,ch) ((d)->ops->getcapture(d,ch)) +#define STM32_TIM_SETISR(d,hnd,s) ((d)->ops->setisr(d,hnd,s)) +#define STM32_TIM_ENABLEINT(d,s,on) ((d)->ops->enableint(d,s,on)) +#define STM32_TIM_ACKINT(d,s) ((d)->ops->ackint(d,s)) +#define STM32_TIM_GETINT(d) ((d)->ops->getint(d)) + +/************************************************************************************ + * Public Types + ************************************************************************************/ + +#ifndef __ASSEMBLY__ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/* Capture Device Structure */ + +struct stm32_cap_dev_s +{ + struct stm32_cap_ops_s *ops; +}; + +/* Capture input EDGE sources */ + +typedef enum +{ + + /* Mapped */ + STM32_CAP_MAPPED_MASK = (GTIM_CCMR1_CC1S_MASK), + STM32_CAP_MAPPED_TI1 = (1< Date: Tue, 22 Dec 2015 13:26:22 +0100 Subject: [PATCH 08/16] continue capture --- arch/arm/src/stm32/stm32_capture.c | 148 ++++++- arch/arm/src/stm32/stm32_capture.h | 604 +---------------------------- 2 files changed, 141 insertions(+), 611 deletions(-) diff --git a/arch/arm/src/stm32/stm32_capture.c b/arch/arm/src/stm32/stm32_capture.c index fd85d2d3fab..360c9101c08 100644 --- a/arch/arm/src/stm32/stm32_capture.c +++ b/arch/arm/src/stm32/stm32_capture.c @@ -99,17 +99,11 @@ defined(CONFIG_STM32_TIM9_CAP) || defined(CONFIG_STM32_TIM10_CAP) || defined(CONFIG_STM32_TIM11_CAP) || \ defined(CONFIG_STM32_TIM12_CAP) || defined(CONFIG_STM32_TIM13_CAP) || defined(CONFIG_STM32_TIM14_CAP) + /************************************************************************************ * Private Types ************************************************************************************/ -struct stm32_cap_channel_s -{ - uint8_t ch_id; - uint16_t ccmr; - uint32_t gpio; -} - /* TIM Device Structure */ struct stm32_cap_priv_s @@ -120,7 +114,6 @@ struct stm32_cap_priv_s #define HAVE_ADANCED_TIM 1 const int irq_of; /* irq timer overflow is deferent in advanced timer */ #endif - const stm32_cap_channel_s channels[CAP_NCHANNELS]; }; /************************************************************************************ @@ -172,6 +165,134 @@ static inline void stm32_putreg32(FAR struct stm32_cap_priv_s *priv, uint8_t off putreg32(value, priv->base + offset); } + +/************************************************************************************ + * gpio Functions + ************************************************************************************/ + +static inline uint32_t stm32_cap_gpio(FAR struct stm32_cap_priv_s *priv, int channel) +{ + + switch(priv->base) + { +#ifdef CONFIG_STM32_TIM1 + case STM32_TIM1_BASE: + switch (channel) + { +#if defined(GPIO_TIM1_CH1IN) + case 0: return GPIO_TIM1_CH1IN; +#endif +#if defined(GPIO_TIM1_CH2IN) + case 1: return GPIO_TIM1_CH2IN; +#endif +#if defined(GPIO_TIM1_CH3IN) + case 2: return GPIO_TIM1_CH3IN; +#endif +#if defined(GPIO_TIM1_CH4IN) + case 3: return GPIO_TIM1_CH4IN; +#endif + } + break; +#endif +#ifdef CONFIG_STM32_TIM2 + case STM32_TIM2_BASE: + switch (channel) + { +#if defined(GPIO_TIM2_CH1IN) + case 0: return GPIO_TIM2_CH1IN; +#endif +#if defined(GPIO_TIM2_CH2IN) + case 1: return GPIO_TIM2_CH2IN; +#endif +#if defined(GPIO_TIM2_CH3IN) + case 2: return GPIO_TIM2_CH3IN; +#endif +#if defined(GPIO_TIM2_CH4IN) + case 3: return GPIO_TIM2_CH4IN; +#endif + } + break; +#endif +#ifdef CONFIG_STM32_TIM3 + case STM32_TIM3_BASE: + switch (channel) + { +#if defined(GPIO_TIM3_CH1IN) + case 0: return GPIO_TIM3_CH1IN; +#endif +#if defined(GPIO_TIM3_CH2IN) + case 1: return GPIO_TIM3_CH2IN; +#endif +#if defined(GPIO_TIM3_CH3IN) + case 2: return GPIO_TIM3_CH3IN; +#endif +#if defined(GPIO_TIM3_CH4IN) + case 3: return GPIO_TIM3_CH4IN; +#endif + } + break; +#endif +#ifdef CONFIG_STM32_TIM4 + case STM32_TIM4_BASE: + switch (channel) + { +#if defined(GPIO_TIM4_CH1IN) + case 0: return GPIO_TIM4_CH1IN; +#endif +#if defined(GPIO_TIM4_CH2IN) + case 1: return GPIO_TIM4_CH2IN; +#endif +#if defined(GPIO_TIM4_CH3IN) + case 2: return GPIO_TIM4_CH3IN; +#endif +#if defined(GPIO_TIM4_CH4IN) + case 3: return GPIO_TIM4_CH4IN; +#endif + } + break; +#endif +#ifdef CONFIG_STM32_TIM5 + case STM32_TIM5_BASE: + switch (channel) + { +#if defined(GPIO_TIM5_CH1IN) + case 0: return GPIO_TIM5_CH1IN; +#endif +#if defined(GPIO_TIM5_CH2IN) + case 1: return GPIO_TIM5_CH2IN; +#endif +#if defined(GPIO_TIM5_CH3IN) + case 2: return GPIO_TIM5_CH3IN; +#endif +#if defined(GPIO_TIM5_CH4IN) + case 3: return GPIO_TIM5_CH4IN; +#endif + } + break; +#endif + +#ifdef CONFIG_STM32_TIM8 + case STM32_TIM8_BASE: + switch (channel) + { +#if defined(GPIO_TIM8_CH1IN) + case 0: return GPIO_TIM8_CH1OUIN ; +#endif +#if defined(GPIO_TIM8_CH2IN) + case 1: return GPIO_TIM8_CH2OUIN ; +#endif +#if defined(GPIO_TIM8_CH3IN) + case 2: return GPIO_TIM8_CH3OUIN ; +#endif +#if defined(GPIO_TIM8_CH4IN) + case 3: return GPIO_TIM8_CH4OUIN ; +#endif + } + break; +#endif + } + return gpio; +} /************************************************************************************ * Basic Functions ************************************************************************************/ @@ -408,15 +529,7 @@ static int stm32_cap_setchannel(FAR struct stm32_cap_dev_s *dev, uint8_t channel ASSERT(dev); - i = CAP_NCHANNELS; - while(i--) - { - if ( priv->channels[i].ch_id == channel ) - { - gpio = priv->channels[i].gpio; - break; - } - } + gpio = stm32_cap_gpio(priv,channel); if ( gpio == 0 ) return ERROR; @@ -424,7 +537,6 @@ static int stm32_cap_setchannel(FAR struct stm32_cap_dev_s *dev, uint8_t channel /* change to zero base index */ channel--; - /* Set ccer */ switch (cfg & STM32_CAP_EDGE_MASK) { diff --git a/arch/arm/src/stm32/stm32_capture.h b/arch/arm/src/stm32/stm32_capture.h index e3eb1f388ec..744b6852690 100644 --- a/arch/arm/src/stm32/stm32_capture.h +++ b/arch/arm/src/stm32/stm32_capture.h @@ -48,588 +48,6 @@ #include "chip/stm32_tim.h" -#ifdef CONFIG_STM32_TIM1_CHANNEL1 -# ifdef CONFIG_STM32_TIM1_CH1OUT -# define CAP_TIM1_CH1CFG GPIO_TIM1_CH1OUT -# else -# define CAP_TIM1_CH1CFG 0 -# endif -# define CAP_TIM1_CHANNEL1 1 -#else -# define CAP_TIM1_CHANNEL1 0 -#endif -#ifdef CONFIG_STM32_TIM1_CHANNEL2 -# ifdef CONFIG_STM32_TIM1_CH2OUT -# define CAP_TIM1_CH2CFG GPIO_TIM1_CH2OUT -# else -# define CAP_TIM1_CH2CFG 0 -# endif -# define CAP_TIM1_CHANNEL2 1 -#else -# define CAP_TIM1_CHANNEL2 0 -#endif -#ifdef CONFIG_STM32_TIM1_CHANNEL3 -# ifdef CONFIG_STM32_TIM1_CH3OUT -# define CAP_TIM1_CH3CFG GPIO_TIM1_CH3OUT -# else -# define CAP_TIM1_CH3CFG 0 -# endif -# define CAP_TIM1_CHANNEL3 1 -#else -# define CAP_TIM1_CHANNEL3 0 -#endif -#ifdef CONFIG_STM32_TIM1_CHANNEL4 -# ifdef CONFIG_STM32_TIM1_CH4OUT -# define CAP_TIM1_CH4CFG GPIO_TIM1_CH4OUT -# else -# define CAP_TIM1_CH4CFG 0 -# endif -# define CAP_TIM1_CHANNEL4 1 -#else -# define CAP_TIM1_CHANNEL4 0 -#endif -#define CAP_TIM1_NCHANNELS (CAP_TIM1_CHANNEL1 + CAP_TIM1_CHANNEL2 + \ - CAP_TIM1_CHANNEL3 + CAP_TIM1_CHANNEL4) - -#ifdef CONFIG_STM32_TIM2_CHANNEL1 -# ifdef CONFIG_STM32_TIM2_CH1OUT -# define CAP_TIM2_CH1CFG GPIO_TIM2_CH1OUT -# else -# define CAP_TIM2_CH1CFG 0 -# endif -# define CAP_TIM2_CHANNEL1 1 -#else -# define CAP_TIM2_CHANNEL1 0 -#endif -#ifdef CONFIG_STM32_TIM2_CHANNEL2 -# ifdef CONFIG_STM32_TIM2_CH2OUT -# define CAP_TIM2_CH2CFG GPIO_TIM2_CH2OUT -# else -# define CAP_TIM2_CH2CFG 0 -# endif -# define CAP_TIM2_CHANNEL2 1 -#else -# define CAP_TIM2_CHANNEL2 0 -#endif -#ifdef CONFIG_STM32_TIM2_CHANNEL3 -# ifdef CONFIG_STM32_TIM2_CH3OUT -# define CAP_TIM2_CH3CFG GPIO_TIM2_CH3OUT -# else -# define CAP_TIM2_CH3CFG 0 -# endif -# define CAP_TIM2_CHANNEL3 1 -#else -# define CAP_TIM2_CHANNEL3 0 -#endif -#ifdef CONFIG_STM32_TIM2_CHANNEL4 -# ifdef CONFIG_STM32_TIM2_CH4OUT -# define CAP_TIM2_CH4CFG GPIO_TIM2_CH4OUT -# else -# define CAP_TIM2_CH4CFG 0 -# endif -# define CAP_TIM2_CHANNEL4 1 -#else -# define CAP_TIM2_CHANNEL4 0 -#endif -#define CAP_TIM2_NCHANNELS (CAP_TIM2_CHANNEL1 + CAP_TIM2_CHANNEL2 + \ - CAP_TIM2_CHANNEL3 + CAP_TIM2_CHANNEL4) - -#ifdef CONFIG_STM32_TIM3_CHANNEL1 -# ifdef CONFIG_STM32_TIM3_CH1OUT -# define CAP_TIM3_CH1CFG GPIO_TIM3_CH1OUT -# else -# define CAP_TIM3_CH1CFG 0 -# endif -# define CAP_TIM3_CHANNEL1 1 -#else -# define CAP_TIM3_CHANNEL1 0 -#endif -#ifdef CONFIG_STM32_TIM3_CHANNEL2 -# ifdef CONFIG_STM32_TIM3_CH2OUT -# define CAP_TIM3_CH2CFG GPIO_TIM3_CH2OUT -# else -# define CAP_TIM3_CH2CFG 0 -# endif -# define CAP_TIM3_CHANNEL2 1 -#else -# define CAP_TIM3_CHANNEL2 0 -#endif -#ifdef CONFIG_STM32_TIM3_CHANNEL3 -# ifdef CONFIG_STM32_TIM3_CH3OUT -# define CAP_TIM3_CH3CFG GPIO_TIM3_CH3OUT -# else -# define CAP_TIM3_CH3CFG 0 -# endif -# define CAP_TIM3_CHANNEL3 1 -#else -# define CAP_TIM3_CHANNEL3 0 -#endif -#ifdef CONFIG_STM32_TIM3_CHANNEL4 -# ifdef CONFIG_STM32_TIM3_CH4OUT -# define CAP_TIM3_CH4CFG GPIO_TIM3_CH4OUT -# else -# define CAP_TIM3_CH4CFG 0 -# endif -# define CAP_TIM3_CHANNEL4 1 -#else -# define CAP_TIM3_CHANNEL4 0 -#endif -#define CAP_TIM3_NCHANNELS (CAP_TIM3_CHANNEL1 + CAP_TIM3_CHANNEL2 + \ - CAP_TIM3_CHANNEL3 + CAP_TIM3_CHANNEL4) - -#ifdef CONFIG_STM32_TIM4_CHANNEL1 -# ifdef CONFIG_STM32_TIM4_CH1OUT -# define CAP_TIM4_CH1CFG GPIO_TIM4_CH1OUT -# else -# define CAP_TIM4_CH1CFG 0 -# endif -# define CAP_TIM4_CHANNEL1 1 -#else -# define CAP_TIM4_CHANNEL1 0 -#endif -#ifdef CONFIG_STM32_TIM4_CHANNEL2 -# ifdef CONFIG_STM32_TIM4_CH2OUT -# define CAP_TIM4_CH2CFG GPIO_TIM4_CH2OUT -# else -# define CAP_TIM4_CH2CFG 0 -# endif -# define CAP_TIM4_CHANNEL2 1 -#else -# define CAP_TIM4_CHANNEL2 0 -#endif -#ifdef CONFIG_STM32_TIM4_CHANNEL3 -# ifdef CONFIG_STM32_TIM4_CH3OUT -# define CAP_TIM4_CH3CFG GPIO_TIM4_CH3OUT -# else -# define CAP_TIM4_CH3CFG 0 -# endif -# define CAP_TIM4_CHANNEL3 1 -#else -# define CAP_TIM4_CHANNEL3 0 -#endif -#ifdef CONFIG_STM32_TIM4_CHANNEL4 -# ifdef CONFIG_STM32_TIM4_CH4OUT -# define CAP_TIM4_CH4CFG GPIO_TIM4_CH4OUT -# else -# define CAP_TIM4_CH4CFG 0 -# endif -# define CAP_TIM4_CHANNEL4 1 -#else -# define CAP_TIM4_CHANNEL4 0 -#endif -#define CAP_TIM4_NCHANNELS (CAP_TIM4_CHANNEL1 + CAP_TIM4_CHANNEL2 + \ - CAP_TIM4_CHANNEL3 + CAP_TIM4_CHANNEL4) - -#ifdef CONFIG_STM32_TIM5_CHANNEL1 -# ifdef CONFIG_STM32_TIM5_CH1OUT -# define CAP_TIM5_CH1CFG GPIO_TIM5_CH1OUT -# else -# define CAP_TIM5_CH1CFG 0 -# endif -# define CAP_TIM5_CHANNEL1 1 -#else -# define CAP_TIM5_CHANNEL1 0 -#endif -#ifdef CONFIG_STM32_TIM5_CHANNEL2 -# ifdef CONFIG_STM32_TIM5_CH2OUT -# define CAP_TIM5_CH2CFG GPIO_TIM5_CH2OUT -# else -# define CAP_TIM5_CH2CFG 0 -# endif -# define CAP_TIM5_CHANNEL2 1 -#else -# define CAP_TIM5_CHANNEL2 0 -#endif -#ifdef CONFIG_STM32_TIM5_CHANNEL3 -# ifdef CONFIG_STM32_TIM5_CH3OUT -# define CAP_TIM5_CH3CFG GPIO_TIM5_CH3OUT -# else -# define CAP_TIM5_CH3CFG 0 -# endif -# define CAP_TIM5_CHANNEL3 1 -#else -# define CAP_TIM5_CHANNEL3 0 -#endif -#ifdef CONFIG_STM32_TIM5_CHANNEL4 -# ifdef CONFIG_STM32_TIM5_CH4OUT -# define CAP_TIM5_CH4CFG GPIO_TIM5_CH4OUT -# else -# define CAP_TIM5_CH4CFG 0 -# endif -# define CAP_TIM5_CHANNEL4 1 -#else -# define CAP_TIM5_CHANNEL4 0 -#endif -#define CAP_TIM5_NCHANNELS (CAP_TIM5_CHANNEL1 + CAP_TIM5_CHANNEL2 + \ - CAP_TIM5_CHANNEL3 + CAP_TIM5_CHANNEL4) - -#ifdef CONFIG_STM32_TIM8_CHANNEL1 -# ifdef CONFIG_STM32_TIM8_CH1OUT -# define CAP_TIM8_CH1CFG GPIO_TIM8_CH1OUT -# else -# define CAP_TIM8_CH1CFG 0 -# endif -# define CAP_TIM8_CHANNEL1 1 -#else -# define CAP_TIM8_CHANNEL1 0 -#endif -#ifdef CONFIG_STM32_TIM8_CHANNEL2 -# ifdef CONFIG_STM32_TIM8_CH2OUT -# define CAP_TIM8_CH2CFG GPIO_TIM8_CH2OUT -# else -# define CAP_TIM8_CH2CFG 0 -# endif -# define CAP_TIM8_CHANNEL2 1 -#else -# define CAP_TIM8_CHANNEL2 0 -#endif -#ifdef CONFIG_STM32_TIM8_CHANNEL3 -# ifdef CONFIG_STM32_TIM8_CH3OUT -# define CAP_TIM8_CH3CFG GPIO_TIM8_CH3OUT -# else -# define CAP_TIM8_CH3CFG 0 -# endif -# define CAP_TIM8_CHANNEL3 1 -#else -# define CAP_TIM8_CHANNEL3 0 -#endif -#ifdef CONFIG_STM32_TIM8_CHANNEL4 -# ifdef CONFIG_STM32_TIM8_CH4OUT -# define CAP_TIM8_CH4CFG GPIO_TIM8_CH4OUT -# else -# define CAP_TIM8_CH4CFG 0 -# endif -# define CAP_TIM8_CHANNEL4 1 -#else -# define CAP_TIM8_CHANNEL4 0 -#endif -#define CAP_TIM8_NCHANNELS (CAP_TIM8_CHANNEL1 + CAP_TIM8_CHANNEL2 + \ - CAP_TIM8_CHANNEL3 + CAP_TIM8_CHANNEL4) - -#ifdef CONFIG_STM32_TIM9_CHANNEL1 -# ifdef CONFIG_STM32_TIM9_CH1OUT -# define CAP_TIM9_CH1CFG GPIO_TIM9_CH1OUT -# else -# define CAP_TIM9_CH1CFG 0 -# endif -# define CAP_TIM9_CHANNEL1 1 -#else -# define CAP_TIM9_CHANNEL1 0 -#endif -#ifdef CONFIG_STM32_TIM9_CHANNEL2 -# ifdef CONFIG_STM32_TIM9_CH2OUT -# define CAP_TIM9_CH2CFG GPIO_TIM9_CH2OUT -# else -# define CAP_TIM9_CH2CFG 0 -# endif -# define CAP_TIM9_CHANNEL2 1 -#else -# define CAP_TIM9_CHANNEL2 0 -#endif -#ifdef CONFIG_STM32_TIM9_CHANNEL3 -# ifdef CONFIG_STM32_TIM9_CH3OUT -# define CAP_TIM9_CH3CFG GPIO_TIM9_CH3OUT -# else -# define CAP_TIM9_CH3CFG 0 -# endif -# define CAP_TIM9_CHANNEL3 1 -#else -# define CAP_TIM9_CHANNEL3 0 -#endif -#ifdef CONFIG_STM32_TIM9_CHANNEL4 -# ifdef CONFIG_STM32_TIM9_CH4OUT -# define CAP_TIM9_CH4CFG GPIO_TIM9_CH4OUT -# else -# define CAP_TIM9_CH4CFG 0 -# endif -# define CAP_TIM9_CHANNEL4 1 -#else -# define CAP_TIM9_CHANNEL4 0 -#endif -#define CAP_TIM9_NCHANNELS (CAP_TIM9_CHANNEL1 + CAP_TIM9_CHANNEL2 + \ - CAP_TIM9_CHANNEL3 + CAP_TIM9_CHANNEL4) - -#ifdef CONFIG_STM32_TIM10_CHANNEL1 -# ifdef CONFIG_STM32_TIM10_CH1OUT -# define CAP_TIM10_CH1CFG GPIO_TIM10_CH1OUT -# else -# define CAP_TIM10_CH1CFG 0 -# endif -# define CAP_TIM10_CHANNEL1 1 -#else -# define CAP_TIM10_CHANNEL1 0 -#endif -#ifdef CONFIG_STM32_TIM10_CHANNEL2 -# ifdef CONFIG_STM32_TIM10_CH2OUT -# define CAP_TIM10_CH2CFG GPIO_TIM10_CH2OUT -# else -# define CAP_TIM10_CH2CFG 0 -# endif -# define CAP_TIM10_CHANNEL2 1 -#else -# define CAP_TIM10_CHANNEL2 0 -#endif -#ifdef CONFIG_STM32_TIM10_CHANNEL3 -# ifdef CONFIG_STM32_TIM10_CH3OUT -# define CAP_TIM10_CH3CFG GPIO_TIM10_CH3OUT -# else -# define CAP_TIM10_CH3CFG 0 -# endif -# define CAP_TIM10_CHANNEL3 1 -#else -# define CAP_TIM10_CHANNEL3 0 -#endif -#ifdef CONFIG_STM32_TIM10_CHANNEL4 -# ifdef CONFIG_STM32_TIM10_CH4OUT -# define CAP_TIM10_CH4CFG GPIO_TIM10_CH4OUT -# else -# define CAP_TIM10_CH4CFG 0 -# endif -# define CAP_TIM10_CHANNEL4 1 -#else -# define CAP_TIM10_CHANNEL4 0 -#endif -#define CAP_TIM10_NCHANNELS (CAP_TIM10_CHANNEL1 + CAP_TIM10_CHANNEL2 + \ - CAP_TIM10_CHANNEL3 + CAP_TIM10_CHANNEL4) - -#ifdef CONFIG_STM32_TIM11_CHANNEL1 -# ifdef CONFIG_STM32_TIM11_CH1OUT -# define CAP_TIM11_CH1CFG GPIO_TIM11_CH1OUT -# else -# define CAP_TIM11_CH1CFG 0 -# endif -# define CAP_TIM11_CHANNEL1 1 -#else -# define CAP_TIM11_CHANNEL1 0 -#endif -#ifdef CONFIG_STM32_TIM11_CHANNEL2 -# ifdef CONFIG_STM32_TIM11_CH2OUT -# define CAP_TIM11_CH2CFG GPIO_TIM11_CH2OUT -# else -# define CAP_TIM11_CH2CFG 0 -# endif -# define CAP_TIM11_CHANNEL2 1 -#else -# define CAP_TIM11_CHANNEL2 0 -#endif -#ifdef CONFIG_STM32_TIM11_CHANNEL3 -# ifdef CONFIG_STM32_TIM11_CH3OUT -# define CAP_TIM11_CH3CFG GPIO_TIM11_CH3OUT -# else -# define CAP_TIM11_CH3CFG 0 -# endif -# define CAP_TIM11_CHANNEL3 1 -#else -# define CAP_TIM11_CHANNEL3 0 -#endif -#ifdef CONFIG_STM32_TIM11_CHANNEL4 -# ifdef CONFIG_STM32_TIM11_CH4OUT -# define CAP_TIM11_CH4CFG GPIO_TIM11_CH4OUT -# else -# define CAP_TIM11_CH4CFG 0 -# endif -# define CAP_TIM11_CHANNEL4 1 -#else -# define CAP_TIM11_CHANNEL4 0 -#endif -#define CAP_TIM11_NCHANNELS (CAP_TIM11_CHANNEL1 + CAP_TIM11_CHANNEL2 + \ - CAP_TIM11_CHANNEL3 + CAP_TIM11_CHANNEL4) - -#ifdef CONFIG_STM32_TIM12_CHANNEL1 -# ifdef CONFIG_STM32_TIM12_CH1OUT -# define CAP_TIM12_CH1CFG GPIO_TIM12_CH1OUT -# else -# define CAP_TIM12_CH1CFG 0 -# endif -# define CAP_TIM12_CHANNEL1 1 -#else -# define CAP_TIM12_CHANNEL1 0 -#endif -#ifdef CONFIG_STM32_TIM12_CHANNEL2 -# ifdef CONFIG_STM32_TIM12_CH2OUT -# define CAP_TIM12_CH2CFG GPIO_TIM12_CH2OUT -# else -# define CAP_TIM12_CH2CFG 0 -# endif -# define CAP_TIM12_CHANNEL2 1 -#else -# define CAP_TIM12_CHANNEL2 0 -#endif -#ifdef CONFIG_STM32_TIM12_CHANNEL3 -# ifdef CONFIG_STM32_TIM12_CH3OUT -# define CAP_TIM12_CH3CFG GPIO_TIM12_CH3OUT -# else -# define CAP_TIM12_CH3CFG 0 -# endif -# define CAP_TIM12_CHANNEL3 1 -#else -# define CAP_TIM12_CHANNEL3 0 -#endif -#ifdef CONFIG_STM32_TIM12_CHANNEL4 -# ifdef CONFIG_STM32_TIM12_CH4OUT -# define CAP_TIM12_CH4CFG GPIO_TIM12_CH4OUT -# else -# define CAP_TIM12_CH4CFG 0 -# endif -# define CAP_TIM12_CHANNEL4 1 -#else -# define CAP_TIM12_CHANNEL4 0 -#endif -#define CAP_TIM12_NCHANNELS (CAP_TIM12_CHANNEL1 + CAP_TIM12_CHANNEL2 + \ - CAP_TIM12_CHANNEL3 + CAP_TIM12_CHANNEL4) - -#ifdef CONFIG_STM32_TIM13_CHANNEL1 -# ifdef CONFIG_STM32_TIM13_CH1OUT -# define CAP_TIM13_CH1CFG GPIO_TIM13_CH1OUT -# else -# define CAP_TIM13_CH1CFG 0 -# endif -# define CAP_TIM13_CHANNEL1 1 -#else -# define CAP_TIM13_CHANNEL1 0 -#endif -#ifdef CONFIG_STM32_TIM13_CHANNEL2 -# ifdef CONFIG_STM32_TIM13_CH2OUT -# define CAP_TIM13_CH2CFG GPIO_TIM13_CH2OUT -# else -# define CAP_TIM13_CH2CFG 0 -# endif -# define CAP_TIM13_CHANNEL2 1 -#else -# define CAP_TIM13_CHANNEL2 0 -#endif -#ifdef CONFIG_STM32_TIM13_CHANNEL3 -# ifdef CONFIG_STM32_TIM13_CH3OUT -# define CAP_TIM13_CH3CFG GPIO_TIM13_CH3OUT -# else -# define CAP_TIM13_CH3CFG 0 -# endif -# define CAP_TIM13_CHANNEL3 1 -#else -# define CAP_TIM13_CHANNEL3 0 -#endif -#ifdef CONFIG_STM32_TIM13_CHANNEL4 -# ifdef CONFIG_STM32_TIM13_CH4OUT -# define CAP_TIM13_CH4CFG GPIO_TIM13_CH4OUT -# else -# define CAP_TIM13_CH4CFG 0 -# endif -# define CAP_TIM13_CHANNEL4 1 -#else -# define CAP_TIM13_CHANNEL4 0 -#endif -#define CAP_TIM13_NCHANNELS (CAP_TIM13_CHANNEL1 + CAP_TIM13_CHANNEL2 + \ - CAP_TIM13_CHANNEL3 + CAP_TIM13_CHANNEL4) - -#ifdef CONFIG_STM32_TIM14_CHANNEL1 -# ifdef CONFIG_STM32_TIM14_CH1OUT -# define CAP_TIM14_CH1CFG GPIO_TIM14_CH1OUT -# else -# define CAP_TIM14_CH1CFG 0 -# endif -# define CAP_TIM14_CHANNEL1 1 -#else -# define CAP_TIM14_CHANNEL1 0 -#endif -#ifdef CONFIG_STM32_TIM14_CHANNEL2 -# ifdef CONFIG_STM32_TIM14_CH2OUT -# define CAP_TIM14_CH2CFG GPIO_TIM14_CH2OUT -# else -# define CAP_TIM14_CH2CFG 0 -# endif -# define CAP_TIM14_CHANNEL2 1 -#else -# define CAP_TIM14_CHANNEL2 0 -#endif -#ifdef CONFIG_STM32_TIM14_CHANNEL3 -# ifdef CONFIG_STM32_TIM14_CH3OUT -# define CAP_TIM14_CH3CFG GPIO_TIM14_CH3OUT -# else -# define CAP_TIM14_CH3CFG 0 -# endif -# define CAP_TIM14_CHANNEL3 1 -#else -# define CAP_TIM14_CHANNEL3 0 -#endif -#ifdef CONFIG_STM32_TIM14_CHANNEL4 -# ifdef CONFIG_STM32_TIM14_CH4OUT -# define CAP_TIM14_CH4CFG GPIO_TIM14_CH4OUT -# else -# define CAP_TIM14_CH4CFG 0 -# endif -# define CAP_TIM14_CHANNEL4 1 -#else -# define CAP_TIM14_CHANNEL4 0 -#endif -#define CAP_TIM14_NCHANNELS (CAP_TIM14_CHANNEL1 + CAP_TIM14_CHANNEL2 + \ - CAP_TIM14_CHANNEL3 + CAP_TIM14_CHANNEL4) - -#ifdef CONFIG_STM32_TIM15_CHANNEL1 -# ifdef CONFIG_STM32_TIM15_CH1OUT -# define CAP_TIM15_CH1CFG GPIO_TIM15_CH1OUT -# else -# define CAP_TIM15_CH1CFG 0 -# endif -# define CAP_TIM15_CHANNEL1 1 -#else -# define CAP_TIM15_CHANNEL1 0 -#endif -#ifdef CONFIG_STM32_TIM15_CHANNEL2 -# ifdef CONFIG_STM32_TIM15_CH2OUT -# define CAP_TIM15_CH2CFG GPIO_TIM15_CH2OUT -# else -# define CAP_TIM15_CH2CFG 0 -# endif -# define CAP_TIM15_CHANNEL2 1 -#else -# define CAP_TIM15_CHANNEL2 0 -#endif -#define CAP_TIM15_NCHANNELS (CAP_TIM15_CHANNEL1 + CAP_TIM15_CHANNEL2) - -#ifdef CONFIG_STM32_TIM16_CHANNEL1 -# ifdef CONFIG_STM32_TIM16_CH1OUT -# define CAP_TIM16_CH1CFG GPIO_TIM16_CH1OUT -# else -# define CAP_TIM16_CH1CFG 0 -# endif -# define CAP_TIM16_CHANNEL1 1 -#else -# define CAP_TIM16_CHANNEL1 0 -#endif -#define CAP_TIM16_NCHANNELS CAP_TIM16_CHANNEL1 - -#ifdef CONFIG_STM32_TIM17_CHANNEL1 -# ifdef CONFIG_STM32_TIM17_CH1OUT -# define CAP_TIM17_CH1CFG GPIO_TIM17_CH1OUT -# else -# define CAP_TIM17_CH1CFG 0 -# endif -# define CAP_TIM17_CHANNEL1 1 -#else -# define CAP_TIM17_CHANNEL1 0 -#endif -#define CAP_TIM17_NCHANNELS CAP_TIM17_CHANNEL1 - -#define CAP_MAX(a, b) ((a) > (b) ? (a) : (b)) - -#define CAP_NCHANNELS CAP_MAX(CAP_TIM1_NCHANNELS, \ - CAP_MAX(CAP_TIM2_NCHANNELS, \ - CAP_MAX(CAP_TIM3_NCHANNELS, \ - CAP_MAX(CAP_TIM4_NCHANNELS, \ - CAP_MAX(CAP_TIM5_NCHANNELS, \ - CAP_MAX(CAP_TIM8_NCHANNELS, \ - CAP_MAX(CAP_TIM9_NCHANNELS, \ - CAP_MAX(CAP_TIM10_NCHANNELS, \ - CAP_MAX(CAP_TIM11_NCHANNELS, \ - CAP_MAX(CAP_TIM12_NCHANNELS, \ - CAP_MAX(CAP_TIM13_NCHANNELS, \ - CAP_MAX(CAP_TIM14_NCHANNELS, \ - CAP_MAX(CAP_TIM15_NCHANNELS, \ - CAP_MAX(CAP_TIM16_NCHANNELS, \ - CAP_TIM17_NCHANNELS)))))))))))))) - -#endif - /************************************************************************************ * Pre-processor Definitions ************************************************************************************/ @@ -640,8 +58,8 @@ #define STM32_TIM_GETCAPTURE(d,ch) ((d)->ops->getcapture(d,ch)) #define STM32_TIM_SETISR(d,hnd,s) ((d)->ops->setisr(d,hnd,s)) #define STM32_TIM_ENABLEINT(d,s,on) ((d)->ops->enableint(d,s,on)) -#define STM32_TIM_ACKINT(d,s) ((d)->ops->ackint(d,s)) -#define STM32_TIM_GETINT(d) ((d)->ops->getint(d)) +#define STM32_TIM_ACKFLAGS(d,s) ((d)->ops->ackflags(d,s)) +#define STM32_TIM_GETFLAGS(d) ((d)->ops->getflags(d)) /************************************************************************************ * Public Types @@ -731,17 +149,17 @@ typedef enum { /* One of the following */ - STM32_CAP_FLAG_IRQ_TIMER = (1<< 0), + STM32_CAP_FLAG_IRQ_TIMER = (GTIM_SR_UIF), - STM32_CAP_FLAG_IRQ_CH_1 = (1<< 1), - STM32_CAP_FLAG_IRQ_CH_2 = (1<< 2), - STM32_CAP_FLAG_IRQ_CH_3 = (1<< 3), - STM32_CAP_FLAG_IRQ_CH_4 = (1<< 4), + STM32_CAP_FLAG_IRQ_CH_1 = (GTIM_SR_CC1IF), + STM32_CAP_FLAG_IRQ_CH_2 = (GTIM_SR_CC2IF), + STM32_CAP_FLAG_IRQ_CH_3 = (GTIM_SR_CC3IF), + STM32_CAP_FLAG_IRQ_CH_4 = (GTIM_SR_CC4IF), - STM32_CAP_FLAG_OF_CH_1 = (1<< 9), - STM32_CAP_FLAG_OF_CH_2 = (1<<10), - STM32_CAP_FLAG_OF_CH_3 = (1<<11), - STM32_CAP_FLAG_OF_CH_4 = (1<<12) + STM32_CAP_FLAG_OF_CH_1 = (GTIM_SR_CC1OF), + STM32_CAP_FLAG_OF_CH_2 = (GTIM_SR_CC2OF), + STM32_CAP_FLAG_OF_CH_3 = (GTIM_SR_CC3OF), + STM32_CAP_FLAG_OF_CH_4 = (GTIM_SR_CC4OF) } stm32_cap_flags_t; From 573462e355690d9b051b7baf5e54cc95c98ead27 Mon Sep 17 00:00:00 2001 From: pnb Date: Tue, 22 Dec 2015 13:27:00 +0100 Subject: [PATCH 09/16] Revert "continue capture integration" This reverts commit c0b6ba73d59dc973d6984bce12ff4a20d870de2a. --- arch/arm/src/stm32/stm32_tim.c | 177 +-------------------------------- arch/arm/src/stm32/stm32_tim.h | 14 --- 2 files changed, 4 insertions(+), 187 deletions(-) diff --git a/arch/arm/src/stm32/stm32_tim.c b/arch/arm/src/stm32/stm32_tim.c index de9c1aaecb6..f72d411718c 100644 --- a/arch/arm/src/stm32/stm32_tim.c +++ b/arch/arm/src/stm32/stm32_tim.c @@ -145,28 +145,7 @@ # undef CONFIG_STM32_TIM14 #endif -#if defined(GPIO_TIM1_CH1IN) || defined(GPIO_TIM2_CH1IN) || defined(GPIO_TIM3_CH1IN) || \ - defined(GPIO_TIM4_CH1IN) || defined(GPIO_TIM5_CH1IN) || defined(GPIO_TIM8_CH1IN) || \ - defined(GPIO_TIM9_CH1IN) || defined(GPIO_TIM10_CH1IN) || defined(GPIO_TIM11_CH1IN) || \ - defined(GPIO_TIM12_CH1IN) || defined(GPIO_TIM13_CH1IN) || defined(GPIO_TIM14_CH1IN) -# define HAVE_CH1IN 1 -#endif -#if defined(GPIO_TIM1_CH2IN) || defined(GPIO_TIM2_CH2IN) || defined(GPIO_TIM3_CH2IN) || \ - defined(GPIO_TIM4_CH2IN) || defined(GPIO_TIM5_CH2IN) || defined(GPIO_TIM8_CH2IN) || \ - defined(GPIO_TIM9_CH2IN) || defined(GPIO_TIM12_CH2IN) -# define HAVE_CH2IN 1 -#endif - -#if defined(GPIO_TIM1_CH3IN) || defined(GPIO_TIM2_CH3IN) || defined(GPIO_TIM3_CH3IN) || \ - defined(GPIO_TIM4_CH3IN) || defined(GPIO_TIM5_CH3IN) || defined(GPIO_TIM8_CH3IN) -# define HAVE_CH3IN 1 -#endif - -#if defined(GPIO_TIM1_CH4IN) || defined(GPIO_TIM2_CH4IN) || defined(GPIO_TIM3_CH4IN) || \ - defined(GPIO_TIM4_CH4IN) || defined(GPIO_TIM5_CH4IN) || defined(GPIO_TIM8_CH4IN) -# define HAVE_CH4IN 1 -#endif /* This module then only compiles if there are enabled timers that are not intended for * some other purpose. @@ -411,117 +390,21 @@ static int stm32_tim_setisr(FAR struct stm32_tim_dev_s *dev, return OK; } - static void stm32_tim_enableint(FAR struct stm32_tim_dev_s *dev, int source) { - uint16_t mask; ASSERT(dev); - - if (source & STM32_TIM_INT_SRC_OVERFLOW) - regval |= ATIM_DIER_UIE; -#ifdef HAVE_CH1IN - if (source & STM32_TIM_INT_SRC_CAPTURE_1) - regval |= ATIM_DIER_CC1IE; -#endif -#ifdef HAVE_CH2IN - if (source & STM32_TIM_INT_SRC_CAPTURE_2) - regval |= ATIM_DIER_CC1IE; -#endif -#ifdef HAVE_CH3IN - if (source & STM32_TIM_INT_SRC_CAPTURE_3) - regval |= ATIM_DIER_CC1IE; -#endif -#ifdef HAVE_CH4IN - if (source & STM32_TIM_INT_SRC_CAPTURE_4) - regval |= ATIM_DIER_CC1IE; -#endif - - stm32_modifyreg16(dev, STM32_BTIM_DIER_OFFSET,0,mask); - + stm32_modifyreg16(dev, STM32_BTIM_DIER_OFFSET, 0, ATIM_DIER_UIE); } static void stm32_tim_disableint(FAR struct stm32_tim_dev_s *dev, int source) { - uint16_t mask; ASSERT(dev); - - if (source & STM32_TIM_INT_SRC_OVERFLOW) - regval |= ATIM_DIER_UIE; -#ifdef HAVE_CH1IN - if (source & STM32_TIM_INT_SRC_CAPTURE_1) - regval |= ATIM_DIER_CC1IE; -#endif -#ifdef HAVE_CH2IN - if (source & STM32_TIM_INT_SRC_CAPTURE_2) - regval |= ATIM_DIER_CC1IE; -#endif -#ifdef HAVE_CH3IN - if (source & STM32_TIM_INT_SRC_CAPTURE_3) - regval |= ATIM_DIER_CC1IE; -#endif -#ifdef HAVE_CH4IN - if (source & STM32_TIM_INT_SRC_CAPTURE_4) - regval |= ATIM_DIER_CC1IE; -#endif - stm32_modifyreg16(dev, STM32_BTIM_DIER_OFFSET,mask, 0); - + stm32_modifyreg16(dev, STM32_BTIM_DIER_OFFSET, ATIM_DIER_UIE, 0); } static void stm32_tim_ackint(FAR struct stm32_tim_dev_s *dev, int source) { - uint16_t mask = 0; - - if (source & STM32_TIM_INT_SRC_OVERFLOW) - regval |= ATIM_SR_UIF; -#ifdef HAVE_CH1IN - if (source & STM32_TIM_INT_SRC_CAPTURE_1) - regval |= ATIM_SR_CC1IF; -#endif -#ifdef HAVE_CH2IN - if (source & STM32_TIM_INT_SRC_CAPTURE_2) - regval |= ATIM_SR_CC2IF; -#endif -#ifdef HAVE_CH3IN - if (source & STM32_TIM_INT_SRC_CAPTURE_3) - regval |= ATIM_SR_CC3IF; -#endif -#ifdef HAVE_CH4IN - if (source & STM32_TIM_INT_SRC_CAPTURE_4) - regval |= ATIM_SR_CC4IF; -#endif - - stm32_putreg16(dev, STM32_BTIM_SR_OFFSET, ~mask); - -} - -static int stm32_tim_getintsrc(FAR struct stm32_tim_dev_s *dev, int source) -{ - uint16_t regval = 0; - int source = 0; - - regval = stm32_getreg16(dev, STM32_BTIM_SR_OFFSET); - - if (regval & ATIM_SR_UIF) - source |= STM32_TIM_INT_SRC_OVERFLOW; -#ifdef HAVE_CH1IN - if (regval & ATIM_SR_CC1IF) - source |= STM32_TIM_INT_SRC_CAPTURE_1; -#endif -#ifdef HAVE_CH2IN - if (regval & ATIM_SR_CC2IF) - source |= STM32_TIM_INT_SRC_CAPTURE_2; -#endif -#ifdef HAVE_CH3IN - if (regval & ATIM_SR_CC3IF) - source |= STM32_TIM_INT_SRC_CAPTURE_3; -#endif -#ifdef HAVE_CH4IN - if (regval & ATIM_SR_CC4IF) - source |= STM32_TIM_INT_SRC_CAPTURE_4; -#endif - - return source; - + stm32_putreg16(dev, STM32_BTIM_SR_OFFSET, ~ATIM_SR_UIF); } /************************************************************************************ @@ -1035,69 +918,19 @@ static int stm32_tim_getcapture(FAR struct stm32_tim_dev_s *dev, uint8_t channel switch (channel) { -#ifdef HAVE_CH1IN case 1: return stm32_getreg32(dev, STM32_GTIM_CCR1_OFFSET); -#endif -#ifdef HAVE_CH1IN case 2: return stm32_getreg32(dev, STM32_GTIM_CCR2_OFFSET); -#endif -#ifdef HAVE_CH1IN case 3: return stm32_getreg32(dev, STM32_GTIM_CCR3_OFFSET); -#endif -#ifdef HAVE_CH1IN case 4: return stm32_getreg32(dev, STM32_GTIM_CCR4_OFFSET); -#endif } return ERROR; } -static bool stm32_tim_captureoverflow(FAR struct stm32_tim_dev_s *dev, int channel, - bool clear) -{ - uint16_t regval; - uint16_t mask; - ASSERT(dev); - - switch (channel) - { -#ifdef HAVE_CH1IN - case 1: - mask = GTIM_SR_CC1OF; - break; -#endif -#ifdef HAVE_CH2IN - case 2: - mask = GTIM_SR_CC2IF; - break; -#endif -#ifdef HAVE_CH3IN - case 3: - mask = GTIM_SR_CC3IF; - break; -#endif -#ifdef HAVE_CH4IN - case 4: - mask = GTIM_SR_CC4IF; - break; -#endif - default: - return ERROR; - } - regval = stm32_getreg16(dev, STM32_GTIM_SR_OFFSET); - if (regval & mask) - { - if (clear) - stm32_putreg16(dev, STM32_GTIM_SR_OFFSET, ~mask); - return 1; - } - return 0; -} - /************************************************************************************ * Advanced Functions ************************************************************************************/ @@ -1116,12 +949,10 @@ struct stm32_tim_ops_s stm32_tim_ops = .setchannel = &stm32_tim_setchannel, .setcompare = &stm32_tim_setcompare, .getcapture = &stm32_tim_getcapture, - .captureoverflow= &stm32_tim_captureoverflow, .setisr = &stm32_tim_setisr, .enableint = &stm32_tim_enableint, .disableint = &stm32_tim_disableint, - .ackint = &stm32_tim_ackint, - .getintsrc = &stm32_tim_getintsrc + .ackint = &stm32_tim_ackint }; #ifdef CONFIG_STM32_TIM2 diff --git a/arch/arm/src/stm32/stm32_tim.h b/arch/arm/src/stm32/stm32_tim.h index 2c3c9962819..3efee9eec6c 100644 --- a/arch/arm/src/stm32/stm32_tim.h +++ b/arch/arm/src/stm32/stm32_tim.h @@ -122,20 +122,6 @@ typedef enum } stm32_tim_mode_t; -/* TIM Sources */ - -typedef enum -{ - /* One of the following */ - - STM32_TIM_INT_SRC_OVERFLOW = 0x0001, - STM32_TIM_INT_SRC_CAPTURE_1 = 0x0002, - STM32_TIM_INT_SRC_CAPTURE_2 = 0x0004, - STM32_TIM_INT_SRC_CAPTURE_3 = 0x0008, - STM32_TIM_INT_SRC_CAPTURE_4 = 0x0010 - -} stm32_tim_source_t; - /* TIM Channel Modes */ From 764c9ea887fdbe406a6287ba142043c6f7c33709 Mon Sep 17 00:00:00 2001 From: pnb Date: Tue, 22 Dec 2015 13:27:35 +0100 Subject: [PATCH 10/16] Revert "used DEFINE instead of setcapturecfg function to set filter and prescaler of input capture" This reverts commit 08600d05eb12cb4f62280766b7f780e8eaa2284b. --- arch/arm/src/stm32/stm32_tim.c | 107 +++++++++++++++++---------------- arch/arm/src/stm32/stm32_tim.h | 21 ++++--- 2 files changed, 67 insertions(+), 61 deletions(-) diff --git a/arch/arm/src/stm32/stm32_tim.c b/arch/arm/src/stm32/stm32_tim.c index f72d411718c..ebf967946cb 100644 --- a/arch/arm/src/stm32/stm32_tim.c +++ b/arch/arm/src/stm32/stm32_tim.c @@ -476,6 +476,59 @@ static int stm32_tim_setmode(FAR struct stm32_tim_dev_s *dev, stm32_tim_mode_t m return OK; } +static int stm32_tim_setcapturecfg(FAR struct stm32_tim_dev_s *dev, + uint8_t channel, uint8_t capt_filter, + uint8_t capt_prescaler) +{ + uint16_t ccmr_orig = 0; + uint16_t ccmr_val = 0; + uint16_t ccmr_mask = 0xff; + uint8_t ccmr_offset = STM32_GTIM_CCMR1_OFFSET; + + ASSERT(dev); + + /* Further we use range as 0..3; if channel=0 it will also overflow here */ + + if (--channel > 4) return ERROR; + +#if STM32_NBTIM > 0 + if (((struct stm32_tim_priv_s *)dev)->base == STM32_TIM6_BASE +#endif +#if STM32_NBTIM > 1 + || ((struct stm32_tim_priv_s *)dev)->base == STM32_TIM7_BASE +#endif +#if STM32_NBTIM > 0 + ) + { + return ERROR; + } +#endif + + ccmr_val |= ( capt_filter << ATIM_CCMR1_IC1F_SHIFT ); + ccmr_val |= ( capt_prescaler << ATIM_CCMR1_IC1PSC_SHIFT ); + + /* Define its position (shift) and get register offset */ + + if (channel & 1) + { + ccmr_val <<= 8; + ccmr_mask <<= 8; + } + + if (channel > 1) + { + ccmr_offset = STM32_GTIM_CCMR2_OFFSET; + } + + ccmr_orig = stm32_getreg16(dev, ccmr_offset); + ccmr_orig &= ~ccmr_mask; + ccmr_orig |= ccmr_val; + stm32_putreg16(dev, ccmr_offset, ccmr_orig); + + return OK; + +} + static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel, stm32_tim_channel_t mode) { @@ -526,8 +579,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM2_CH1IN) gpio_in_cfg = GPIO_TIM2_CH1IN; - ccmr_val |= ( GPIO_TIM2_CH1_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM2_CH1_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 1: @@ -536,8 +587,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM2_CH2IN) gpio_in_cfg = GPIO_TIM2_CH2IN; - ccmr_val |= ( GPIO_TIM2_CH2_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM2_CH2_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 2: @@ -546,8 +595,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM2_CH3IN) gpio_in_cfg = GPIO_TIM2_CH3IN; - ccmr_val |= ( GPIO_TIM2_CH3_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM2_CH3_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 3: @@ -556,8 +603,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM2_CH4IN) gpio_in_cfg = GPIO_TIM2_CH4IN; - ccmr_val |= ( GPIO_TIM2_CH4_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM2_CH4_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; default: @@ -575,8 +620,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM3_CH1IN) gpio_in_cfg = GPIO_TIM3_CH1IN; - ccmr_val |= ( GPIO_TIM3_CH1_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM3_CH1_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 1: @@ -585,8 +628,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM3_CH2IN) gpio_in_cfg = GPIO_TIM3_CH2IN; - ccmr_val |= ( GPIO_TIM3_CH2_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM3_CH2_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 2: @@ -595,8 +636,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM3_CH3IN) gpio_in_cfg = GPIO_TIM3_CH3IN; - ccmr_val |= ( GPIO_TIM3_CH3_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM3_CH3_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 3: @@ -605,8 +644,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM3_CH4IN) gpio_in_cfg = GPIO_TIM3_CH4IN; - ccmr_val |= ( GPIO_TIM3_CH4_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM3_CH4_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; default: @@ -624,8 +661,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM4_CH1IN) gpio_in_cfg = GPIO_TIM4_CH1IN; - ccmr_val |= ( GPIO_TIM4_CH1_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM4_CH1_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 1: @@ -634,8 +669,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM4_CH2IN) gpio_in_cfg = GPIO_TIM4_CH2IN; - ccmr_val |= ( GPIO_TIM4_CH2_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM4_CH2_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 2: @@ -644,8 +677,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM4_CH3IN) gpio_in_cfg = GPIO_TIM4_CH3IN; - ccmr_val |= ( GPIO_TIM4_CH3_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM4_CH3_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 3: @@ -654,8 +685,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM4_CH4IN) gpio_in_cfg = GPIO_TIM4_CH4IN; - ccmr_val |= ( GPIO_TIM4_CH4_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM4_CH4_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; default: return ERROR; @@ -672,8 +701,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM5_CH1IN) gpio_in_cfg = GPIO_TIM5_CH1IN; - ccmr_val |= ( GPIO_TIM5_CH1_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM5_CH1_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 1: @@ -682,8 +709,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM5_CH2IN) gpio_in_cfg = GPIO_TIM5_CH2IN; - ccmr_val |= ( GPIO_TIM5_CH2_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM5_CH2_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 2: @@ -692,8 +717,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM5_CH3IN) gpio_in_cfg = GPIO_TIM5_CH3IN; - ccmr_val |= ( GPIO_TIM5_CH3_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM5_CH3_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 3: @@ -702,8 +725,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM5_CH4IN) gpio_in_cfg = GPIO_TIM5_CH4IN; - ccmr_val |= ( GPIO_TIM5_CH4_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM5_CH4_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; default: return ERROR; @@ -722,8 +743,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM1_CH1IN) gpio_in_cfg = GPIO_TIM1_CH1IN; - ccmr_val |= ( GPIO_TIM1_CH1_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM1_CH1_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 1: @@ -732,8 +751,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM1_CH2IN) gpio_in_cfg = GPIO_TIM1_CH2IN; - ccmr_val |= ( GPIO_TIM1_CH2_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM1_CH2_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 2: @@ -742,8 +759,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM1_CH3IN) gpio_in_cfg = GPIO_TIM1_CH3IN; - ccmr_val |= ( GPIO_TIM1_CH3_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM1_CH3_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 3: @@ -752,8 +767,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM1_CH4IN) gpio_in_cfg = GPIO_TIM1_CH4IN; - ccmr_val |= ( GPIO_TIM1_CH4_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM1_CH4_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; default: @@ -771,8 +784,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM8_CH1IN) gpio_in_cfg = GPIO_TIM8_CH1OUIN ; - ccmr_val |= ( GPIO_TIM8_CH1_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM8_CH1_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 1: @@ -781,8 +792,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM8_CH2IN) gpio_in_cfg = GPIO_TIM8_CH2OUIN ; - ccmr_val |= ( GPIO_TIM8_CH2_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM8_CH2_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 2: @@ -791,8 +800,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM8_CH3IN) gpio_in_cfg = GPIO_TIM8_CH3OUIN ; - ccmr_val |= ( GPIO_TIM8_CH3_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM8_CH3_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; case 3: @@ -801,8 +808,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel #endif #if defined(GPIO_TIM8_CH4IN) gpio_in_cfg = GPIO_TIM8_CH4OUIN ; - ccmr_val |= ( GPIO_TIM8_CH4_ICF << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( GPIO_TIM8_CH4_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); #endif break; default: @@ -820,11 +825,10 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel switch (mode & STM32_TIM_CH_MODE_MASK) { case STM32_TIM_CH_DISABLED: - ccmr_val = 0; break; case STM32_TIM_CH_INCAPTURE: - ccmr_val |= (ATIM_CCMR_CCS_CCIN1 << ATIM_CCMR1_CC1S_SHIFT); + ccmr_val = (ATIM_CCMR_CCS_CCIN1 << ATIM_CCMR1_CC1S_SHIFT); ccer_val |= ATIM_CCER_CC1E << (channel << 2); if ( gpio_in_cfg == 0 ) return ERROR; @@ -952,7 +956,8 @@ struct stm32_tim_ops_s stm32_tim_ops = .setisr = &stm32_tim_setisr, .enableint = &stm32_tim_enableint, .disableint = &stm32_tim_disableint, - .ackint = &stm32_tim_ackint + .ackint = &stm32_tim_ackint, + .setcapturecfg = &stm32_tim_setcapturecfg }; #ifdef CONFIG_STM32_TIM2 diff --git a/arch/arm/src/stm32/stm32_tim.h b/arch/arm/src/stm32/stm32_tim.h index 3efee9eec6c..56db9605dda 100644 --- a/arch/arm/src/stm32/stm32_tim.h +++ b/arch/arm/src/stm32/stm32_tim.h @@ -55,16 +55,16 @@ ************************************************************************************/ /* Helpers **************************************************************************/ -#define STM32_TIM_SETMODE(d,mode) ((d)->ops->setmode(d,mode)) -#define STM32_TIM_SETCLOCK(d,freq) ((d)->ops->setclock(d,freq)) -#define STM32_TIM_SETPERIOD(d,period) ((d)->ops->setperiod(d,period)) -#define STM32_TIM_SETCHANNEL(d,ch,mode) ((d)->ops->setchannel(d,ch,mode)) -#define STM32_TIM_SETCOMPARE(d,ch,comp) ((d)->ops->setcompare(d,ch,comp)) -#define STM32_TIM_GETCAPTURE(d,ch) ((d)->ops->getcapture(d,ch)) -#define STM32_TIM_SETISR(d,hnd,s) ((d)->ops->setisr(d,hnd,s)) -#define STM32_TIM_ENABLEINT(d,s) ((d)->ops->enableint(d,s)) -#define STM32_TIM_DISABLEINT(d,s) ((d)->ops->disableint(d,s)) -#define STM32_TIM_ACKINT(d,s) ((d)->ops->ackint(d,s)) +#define STM32_TIM_SETMODE(d,mode) ((d)->ops->setmode(d,mode)) +#define STM32_TIM_SETCLOCK(d,freq) ((d)->ops->setclock(d,freq)) +#define STM32_TIM_SETPERIOD(d,period) ((d)->ops->setperiod(d,period)) +#define STM32_TIM_SETCHANNEL(d,ch,mode) ((d)->ops->setchannel(d,ch,mode)) +#define STM32_TIM_SETCOMPARE(d,ch,comp) ((d)->ops->setcompare(d,ch,comp)) +#define STM32_TIM_GETCAPTURE(d,ch) ((d)->ops->getcapture(d,ch)) +#define STM32_TIM_SETISR(d,hnd,s) ((d)->ops->setisr(d,hnd,s)) +#define STM32_TIM_ENABLEINT(d,s) ((d)->ops->enableint(d,s)) +#define STM32_TIM_DISABLEINT(d,s) ((d)->ops->disableint(d,s)) +#define STM32_TIM_ACKINT(d,s) ((d)->ops->ackint(d,s)) /************************************************************************************ * Public Types @@ -176,6 +176,7 @@ struct stm32_tim_ops_s void (*enableint)(FAR struct stm32_tim_dev_s *dev, int source); void (*disableint)(FAR struct stm32_tim_dev_s *dev, int source); void (*ackint)(FAR struct stm32_tim_dev_s *dev, int source); + int (*setcapturecfg)(FAR struct stm32_tim_dev_s *dev, uint8_t channel, uint8_t capt_filter, uint8_t capt_prescaler); }; /************************************************************************************ From 7d244d4d04457ac8d3811119ca962fb9a6f60428 Mon Sep 17 00:00:00 2001 From: pnb Date: Tue, 22 Dec 2015 13:27:42 +0100 Subject: [PATCH 11/16] Revert "add timer capture STM32" This reverts commit 7a484f4d7665c533998d6893c21b69340038b5fc. --- arch/arm/src/stm32/stm32_tim.c | 562 ++++++++++++++------------------- arch/arm/src/stm32/stm32_tim.h | 10 +- 2 files changed, 232 insertions(+), 340 deletions(-) diff --git a/arch/arm/src/stm32/stm32_tim.c b/arch/arm/src/stm32/stm32_tim.c index ebf967946cb..c7d3b8350ce 100644 --- a/arch/arm/src/stm32/stm32_tim.c +++ b/arch/arm/src/stm32/stm32_tim.c @@ -145,6 +145,47 @@ # undef CONFIG_STM32_TIM14 #endif +#if defined(CONFIG_STM32_TIM1) +# if defined(GPIO_TIM1_CH1OUT) ||defined(GPIO_TIM1_CH2OUT)||\ + defined(GPIO_TIM1_CH3OUT) ||defined(GPIO_TIM1_CH4OUT) +# define HAVE_TIM1_GPIOCONFIG 1 +#endif +#endif + +#if defined(CONFIG_STM32_TIM2) +# if defined(GPIO_TIM2_CH1OUT) ||defined(GPIO_TIM2_CH2OUT)||\ + defined(GPIO_TIM2_CH3OUT) ||defined(GPIO_TIM2_CH4OUT) +# define HAVE_TIM2_GPIOCONFIG 1 +#endif +#endif + +#if defined(CONFIG_STM32_TIM3) +# if defined(GPIO_TIM3_CH1OUT) ||defined(GPIO_TIM3_CH2OUT)||\ + defined(GPIO_TIM3_CH3OUT) ||defined(GPIO_TIM3_CH4OUT) +# define HAVE_TIM3_GPIOCONFIG 1 +#endif +#endif + +#if defined(CONFIG_STM32_TIM4) +# if defined(GPIO_TIM4_CH1OUT) ||defined(GPIO_TIM4_CH2OUT)||\ + defined(GPIO_TIM4_CH3OUT) ||defined(GPIO_TIM4_CH4OUT) +# define HAVE_TIM4_GPIOCONFIG 1 +#endif +#endif + +#if defined(CONFIG_STM32_TIM5) +# if defined(GPIO_TIM5_CH1OUT) ||defined(GPIO_TIM5_CH2OUT)||\ + defined(GPIO_TIM5_CH3OUT) ||defined(GPIO_TIM5_CH4OUT) +# define HAVE_TIM5_GPIOCONFIG 1 +#endif +#endif + +#if defined(CONFIG_STM32_TIM8) +# if defined(GPIO_TIM8_CH1OUT) ||defined(GPIO_TIM8_CH2OUT)||\ + defined(GPIO_TIM8_CH3OUT) ||defined(GPIO_TIM8_CH4OUT) +# define HAVE_TIM8_GPIOCONFIG 1 +#endif +#endif /* This module then only compiles if there are enabled timers that are not intended for @@ -247,6 +288,24 @@ static void stm32_tim_reset(FAR struct stm32_tim_dev_s *dev) stm32_tim_disable(dev); } +#if defined(HAVE_TIM1_GPIOCONFIG)||defined(HAVE_TIM2_GPIOCONFIG)||\ + defined(HAVE_TIM3_GPIOCONFIG)||defined(HAVE_TIM4_GPIOCONFIG)||\ + defined(HAVE_TIM5_GPIOCONFIG)||defined(HAVE_TIM8_GPIOCONFIG) +static void stm32_tim_gpioconfig(uint32_t cfg, stm32_tim_channel_t mode) +{ + /* TODO: Add support for input capture and bipolar dual outputs for TIM8 */ + + if (mode & STM32_TIM_CH_MODE_MASK) + { + stm32_configgpio(cfg); + } + else + { + stm32_unconfiggpio(cfg); + } +} +#endif + /************************************************************************************ * Basic Functions ************************************************************************************/ @@ -476,64 +535,9 @@ static int stm32_tim_setmode(FAR struct stm32_tim_dev_s *dev, stm32_tim_mode_t m return OK; } -static int stm32_tim_setcapturecfg(FAR struct stm32_tim_dev_s *dev, - uint8_t channel, uint8_t capt_filter, - uint8_t capt_prescaler) -{ - uint16_t ccmr_orig = 0; - uint16_t ccmr_val = 0; - uint16_t ccmr_mask = 0xff; - uint8_t ccmr_offset = STM32_GTIM_CCMR1_OFFSET; - - ASSERT(dev); - - /* Further we use range as 0..3; if channel=0 it will also overflow here */ - - if (--channel > 4) return ERROR; - -#if STM32_NBTIM > 0 - if (((struct stm32_tim_priv_s *)dev)->base == STM32_TIM6_BASE -#endif -#if STM32_NBTIM > 1 - || ((struct stm32_tim_priv_s *)dev)->base == STM32_TIM7_BASE -#endif -#if STM32_NBTIM > 0 - ) - { - return ERROR; - } -#endif - - ccmr_val |= ( capt_filter << ATIM_CCMR1_IC1F_SHIFT ); - ccmr_val |= ( capt_prescaler << ATIM_CCMR1_IC1PSC_SHIFT ); - - /* Define its position (shift) and get register offset */ - - if (channel & 1) - { - ccmr_val <<= 8; - ccmr_mask <<= 8; - } - - if (channel > 1) - { - ccmr_offset = STM32_GTIM_CCMR2_OFFSET; - } - - ccmr_orig = stm32_getreg16(dev, ccmr_offset); - ccmr_orig &= ~ccmr_mask; - ccmr_orig |= ccmr_val; - stm32_putreg16(dev, ccmr_offset, ccmr_orig); - - return OK; - -} - static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel, stm32_tim_channel_t mode) { - uint32_t gpio_in_cfg = 0; - uint32_t gpio_out_cfg= 0; uint16_t ccmr_orig = 0; uint16_t ccmr_val = 0; uint16_t ccmr_mask = 0xff; @@ -567,259 +571,6 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel } #endif - switch (((struct stm32_tim_priv_s *)dev)->base) - { -#ifdef CONFIG_STM32_TIM2 - case STM32_TIM2_BASE: - switch (channel) - { - case 0: -#if defined(GPIO_TIM2_CH1OUT) - gpio_out_cfg = GPIO_TIM2_CH1OUT; -#endif -#if defined(GPIO_TIM2_CH1IN) - gpio_in_cfg = GPIO_TIM2_CH1IN; -#endif - break; - case 1: -#if defined(GPIO_TIM2_CH2OUT) - gpio_out_cfg = GPIO_TIM2_CH2OUT; -#endif -#if defined(GPIO_TIM2_CH2IN) - gpio_in_cfg = GPIO_TIM2_CH2IN; -#endif - break; - case 2: -#if defined(GPIO_TIM2_CH3OUT) - gpio_out_cfg = GPIO_TIM2_CH3OUT; -#endif -#if defined(GPIO_TIM2_CH3IN) - gpio_in_cfg = GPIO_TIM2_CH3IN; -#endif - break; - case 3: -#if defined(GPIO_TIM2_CH4OUT) - gpio_out_cfg = GPIO_TIM2_CH4OUT; -#endif -#if defined(GPIO_TIM2_CH4IN) - gpio_in_cfg = GPIO_TIM2_CH4IN; -#endif - break; - default: - return ERROR; - } - break; -#endif -#ifdef CONFIG_STM32_TIM3 - case STM32_TIM3_BASE: - switch (channel) - { - case 0: -#if defined(GPIO_TIM3_CH1OUT) - gpio_out_cfg = GPIO_TIM3_CH1OUT; -#endif -#if defined(GPIO_TIM3_CH1IN) - gpio_in_cfg = GPIO_TIM3_CH1IN; -#endif - break; - case 1: -#if defined(GPIO_TIM3_CH2OUT) - gpio_out_cfg = GPIO_TIM3_CH2OUT; -#endif -#if defined(GPIO_TIM3_CH2IN) - gpio_in_cfg = GPIO_TIM3_CH2IN; -#endif - break; - case 2: -#if defined(GPIO_TIM3_CH3OUT) - gpio_out_cfg = GPIO_TIM3_CH3OUT; -#endif -#if defined(GPIO_TIM3_CH3IN) - gpio_in_cfg = GPIO_TIM3_CH3IN; -#endif - break; - case 3: -#if defined(GPIO_TIM3_CH4OUT) - gpio_out_cfg = GPIO_TIM3_CH4OUT; -#endif -#if defined(GPIO_TIM3_CH4IN) - gpio_in_cfg = GPIO_TIM3_CH4IN; -#endif - break; - default: - return ERROR; - } - break; -#endif -#ifdef CONFIG_STM32_TIM4 - case STM32_TIM4_BASE: - switch (channel) - { - case 0: -#if defined(GPIO_TIM4_CH1OUT) - gpio_out_cfg = GPIO_TIM4_CH1OUT; -#endif -#if defined(GPIO_TIM4_CH1IN) - gpio_in_cfg = GPIO_TIM4_CH1IN; -#endif - break; - case 1: -#if defined(GPIO_TIM4_CH2OUT) - gpio_out_cfg = GPIO_TIM4_CH2OUT; -#endif -#if defined(GPIO_TIM4_CH2IN) - gpio_in_cfg = GPIO_TIM4_CH2IN; -#endif - break; - case 2: -#if defined(GPIO_TIM4_CH3OUT) - gpio_out_cfg = GPIO_TIM4_CH3OUT; -#endif -#if defined(GPIO_TIM4_CH3IN) - gpio_in_cfg = GPIO_TIM4_CH3IN; -#endif - break; - case 3: -#if defined(GPIO_TIM4_CH4OUT) - gpio_out_cfg = GPIO_TIM4_CH4OUT; -#endif -#if defined(GPIO_TIM4_CH4IN) - gpio_in_cfg = GPIO_TIM4_CH4IN; -#endif - break; - default: return ERROR; - } - break; -#endif -#ifdef CONFIG_STM32_TIM5 - case STM32_TIM5_BASE: - switch (channel) - { - case 0: -#if defined(GPIO_TIM5_CH1OUT) - gpio_out_cfg = GPIO_TIM5_CH1OUT; -#endif -#if defined(GPIO_TIM5_CH1IN) - gpio_in_cfg = GPIO_TIM5_CH1IN; -#endif - break; - case 1: -#if defined(GPIO_TIM5_CH2OUT) - gpio_out_cfg = GPIO_TIM5_CH2OUT; -#endif -#if defined(GPIO_TIM5_CH2IN) - gpio_in_cfg = GPIO_TIM5_CH2IN; -#endif - break; - case 2: -#if defined(GPIO_TIM5_CH3OUT) - gpio_out_cfg = GPIO_TIM5_CH3OUT; -#endif -#if defined(GPIO_TIM5_CH3IN) - gpio_in_cfg = GPIO_TIM5_CH3IN; -#endif - break; - case 3: -#if defined(GPIO_TIM5_CH4OUT) - gpio_out_cfg = GPIO_TIM5_CH4OUT; -#endif -#if defined(GPIO_TIM5_CH4IN) - gpio_in_cfg = GPIO_TIM5_CH4IN; -#endif - break; - default: return ERROR; - } - break; -#endif - -#if STM32_NATIM > 0 -#ifdef CONFIG_STM32_TIM1 - case STM32_TIM1_BASE: - switch (channel) - { - case 0: -#if defined(GPIO_TIM1_CH1OUT) - gpio_out_cfg = GPIO_TIM1_CH1OUT; -#endif -#if defined(GPIO_TIM1_CH1IN) - gpio_in_cfg = GPIO_TIM1_CH1IN; -#endif - break; - case 1: -#if defined(GPIO_TIM1_CH2OUT) - gpio_out_cfg = GPIO_TIM1_CH2OUT; -#endif -#if defined(GPIO_TIM1_CH2IN) - gpio_in_cfg = GPIO_TIM1_CH2IN; -#endif - break; - case 2: -#if defined(GPIO_TIM1_CH3OUT) - gpio_out_cfg = GPIO_TIM1_CH3OUT; -#endif -#if defined(GPIO_TIM1_CH3IN) - gpio_in_cfg = GPIO_TIM1_CH3IN; -#endif - break; - case 3: -#if defined(GPIO_TIM1_CH4OUT) - gpio_out_cfg = GPIO_TIM1_CH4OUT; -#endif -#if defined(GPIO_TIM1_CH4IN) - gpio_in_cfg = GPIO_TIM1_CH4IN; -#endif - break; - default: - return ERROR; - } - break; -#endif -#ifdef CONFIG_STM32_TIM8 - case STM32_TIM8_BASE: - switch (channel) - { - case 0: -#if defined(GPIO_TIM8_CH1OUT) - gpio_out_cfg = GPIO_TIM8_CH1OUT, ; -#endif -#if defined(GPIO_TIM8_CH1IN) - gpio_in_cfg = GPIO_TIM8_CH1OUIN ; -#endif - break; - case 1: -#if defined(GPIO_TIM8_CH2OUT) - gpio_out_cfg = GPIO_TIM8_CH2OUT, ; -#endif -#if defined(GPIO_TIM8_CH2IN) - gpio_in_cfg = GPIO_TIM8_CH2OUIN ; -#endif - break; - case 2: -#if defined(GPIO_TIM8_CH3OUT) - gpio_out_cfg = GPIO_TIM8_CH3OUT, ; -#endif -#if defined(GPIO_TIM8_CH3IN) - gpio_in_cfg = GPIO_TIM8_CH3OUIN ; -#endif - break; - case 3: -#if defined(GPIO_TIM8_CH4OUT) - gpio_out_cfg = GPIO_TIM8_CH4OUT, ; -#endif -#if defined(GPIO_TIM8_CH4IN) - gpio_in_cfg = GPIO_TIM8_CH4OUIN ; -#endif - break; - default: - return ERROR; - } - break; -#endif -#endif - default: - return ERROR; - } - /* Decode configuration */ switch (mode & STM32_TIM_CH_MODE_MASK) @@ -827,18 +578,9 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel case STM32_TIM_CH_DISABLED: break; - case STM32_TIM_CH_INCAPTURE: - ccmr_val = (ATIM_CCMR_CCS_CCIN1 << ATIM_CCMR1_CC1S_SHIFT); - ccer_val |= ATIM_CCER_CC1E << (channel << 2); - if ( gpio_in_cfg == 0 ) - return ERROR; - break; - case STM32_TIM_CH_OUTPWM: ccmr_val = (ATIM_CCMR_MODE_PWM1 << ATIM_CCMR1_OC1M_SHIFT) + ATIM_CCMR1_OC1PE; ccer_val |= ATIM_CCER_CC1E << (channel << 2); - if ( gpio_out_cfg == 0 ) - return ERROR; break; default: @@ -872,20 +614,177 @@ static int stm32_tim_setchannel(FAR struct stm32_tim_dev_s *dev, uint8_t channel stm32_putreg16(dev, STM32_GTIM_CCER_OFFSET, ccer_val); /* set GPIO */ - - if ( gpio_in_cfg ) + + switch (((struct stm32_tim_priv_s *)dev)->base) { - if ( mode & STM32_TIM_CH_MODE_IN_MASK ) - stm32_configgpio(gpio_in_cfg); - else - stm32_unconfiggpio(gpio_in_cfg); - } - if ( gpio_out_cfg ) - { - if ( mode & STM32_TIM_CH_MODE_OUT_MASK ) - stm32_configgpio(gpio_in_cfg); - else - stm32_unconfiggpio(gpio_out_cfg); +#ifdef CONFIG_STM32_TIM2 + case STM32_TIM2_BASE: + switch (channel) + { +#if defined(GPIO_TIM2_CH1OUT) + case 0: + stm32_tim_gpioconfig(GPIO_TIM2_CH1OUT, mode); + break; +#endif +#if defined(GPIO_TIM2_CH2OUT) + case 1: + stm32_tim_gpioconfig(GPIO_TIM2_CH2OUT, mode); + break; +#endif +#if defined(GPIO_TIM2_CH3OUT) + case 2: + stm32_tim_gpioconfig(GPIO_TIM2_CH3OUT, mode); + break; +#endif +#if defined(GPIO_TIM2_CH4OUT) + case 3: + stm32_tim_gpioconfig(GPIO_TIM2_CH4OUT, mode); + break; +#endif + default: + return ERROR; + } + break; +#endif +#ifdef CONFIG_STM32_TIM3 + case STM32_TIM3_BASE: + switch (channel) + { +#if defined(GPIO_TIM3_CH1OUT) + case 0: + stm32_tim_gpioconfig(GPIO_TIM3_CH1OUT, mode); + break; +#endif +#if defined(GPIO_TIM3_CH2OUT) + case 1: + stm32_tim_gpioconfig(GPIO_TIM3_CH2OUT, mode); + break; +#endif +#if defined(GPIO_TIM3_CH3OUT) + case 2: + stm32_tim_gpioconfig(GPIO_TIM3_CH3OUT, mode); + break; +#endif +#if defined(GPIO_TIM3_CH4OUT) + case 3: + stm32_tim_gpioconfig(GPIO_TIM3_CH4OUT, mode); + break; +#endif + default: + return ERROR; + } + break; +#endif +#ifdef CONFIG_STM32_TIM4 + case STM32_TIM4_BASE: + switch (channel) + { +#if defined(GPIO_TIM4_CH1OUT) + case 0: + stm32_tim_gpioconfig(GPIO_TIM4_CH1OUT, mode); + break; +#endif +#if defined(GPIO_TIM4_CH2OUT) + case 1: + stm32_tim_gpioconfig(GPIO_TIM4_CH2OUT, mode); + break; +#endif +#if defined(GPIO_TIM4_CH3OUT) + case 2: + stm32_tim_gpioconfig(GPIO_TIM4_CH3OUT, mode); + break; +#endif +#if defined(GPIO_TIM4_CH4OUT) + case 3: + stm32_tim_gpioconfig(GPIO_TIM4_CH4OUT, mode); + break; +#endif + default: return ERROR; + } + break; +#endif +#ifdef CONFIG_STM32_TIM5 + case STM32_TIM5_BASE: + switch (channel) + { +#if defined(GPIO_TIM5_CH1OUT) + case 0: + stm32_tim_gpioconfig(GPIO_TIM5_CH1OUT, mode); + break; +#endif +#if defined(GPIO_TIM5_CH2OUT) + case 1: + stm32_tim_gpioconfig(GPIO_TIM5_CH2OUT, mode); + break; +#endif +#if defined(GPIO_TIM5_CH3OUT) + case 2: + stm32_tim_gpioconfig(GPIO_TIM5_CH3OUT, mode); + break; +#endif +#if defined(GPIO_TIM5_CH4OUT) + case 3: + stm32_tim_gpioconfig(GPIO_TIM5_CH4OUT, mode); + break; +#endif + default: return ERROR; + } + break; +#endif + +#if STM32_NATIM > 0 +#ifdef CONFIG_STM32_TIM1 + case STM32_TIM1_BASE: + switch (channel) + { +#if defined(GPIO_TIM1_CH1OUT) + case 0: + stm32_tim_gpioconfig(GPIO_TIM1_CH1OUT, mode); break; +#endif +#if defined(GPIO_TIM1_CH2OUT) + case 1: + stm32_tim_gpioconfig(GPIO_TIM1_CH2OUT, mode); break; +#endif +#if defined(GPIO_TIM1_CH3OUT) + case 2: + stm32_tim_gpioconfig(GPIO_TIM1_CH3OUT, mode); break; +#endif +#if defined(GPIO_TIM1_CH4OUT) + case 3: + stm32_tim_gpioconfig(GPIO_TIM1_CH4OUT, mode); break; +#endif + default: return ERROR; + } + break; +#endif +#ifdef CONFIG_STM32_TIM8 + case STM32_TIM8_BASE: + switch (channel) + { +#if defined(GPIO_TIM8_CH1OUT) + case 0: + stm32_tim_gpioconfig(GPIO_TIM8_CH1OUT, mode); break; +#endif +#if defined(GPIO_TIM8_CH2OUT) + case 1: + stm32_tim_gpioconfig(GPIO_TIM8_CH2OUT, mode); break; +#endif +#if defined(GPIO_TIM8_CH3OUT) + case 2: + stm32_tim_gpioconfig(GPIO_TIM8_CH3OUT, mode); break; +#endif +#if defined(GPIO_TIM8_CH4OUT) + case 3: + stm32_tim_gpioconfig(GPIO_TIM8_CH4OUT, mode); break; +#endif + default: + return ERROR; + } + break; +#endif +#endif + default: + return ERROR; } return OK; @@ -956,8 +855,7 @@ struct stm32_tim_ops_s stm32_tim_ops = .setisr = &stm32_tim_setisr, .enableint = &stm32_tim_enableint, .disableint = &stm32_tim_disableint, - .ackint = &stm32_tim_ackint, - .setcapturecfg = &stm32_tim_setcapturecfg + .ackint = &stm32_tim_ackint }; #ifdef CONFIG_STM32_TIM2 diff --git a/arch/arm/src/stm32/stm32_tim.h b/arch/arm/src/stm32/stm32_tim.h index 56db9605dda..2d3b01a683b 100644 --- a/arch/arm/src/stm32/stm32_tim.h +++ b/arch/arm/src/stm32/stm32_tim.h @@ -122,7 +122,6 @@ typedef enum } stm32_tim_mode_t; - /* TIM Channel Modes */ typedef enum @@ -136,19 +135,15 @@ typedef enum /* MODES: */ - STM32_TIM_CH_MODE_OUT_MASK = 0x06, - STM32_TIM_CH_MODE_IN_MASK = 0x10, - STM32_TIM_CH_MODE_MASK = 0x16, + STM32_TIM_CH_MODE_MASK = 0x06, /* Output Compare Modes */ STM32_TIM_CH_OUTPWM = 0x04, /** Enable standard PWM mode, active high when counter < compare */ //STM32_TIM_CH_OUTCOMPARE = 0x06, - /* Input Compare Modes */ - STM32_TIM_CH_INCAPTURE = 0x10, - // TODO other modes ... as PWM capture, ENCODER and Hall Sensor +//STM32_TIM_CH_INCAPTURE = 0x10, //STM32_TIM_CH_INPWM = 0x20 //STM32_TIM_CH_DRIVE_OC -- open collector mode @@ -176,7 +171,6 @@ struct stm32_tim_ops_s void (*enableint)(FAR struct stm32_tim_dev_s *dev, int source); void (*disableint)(FAR struct stm32_tim_dev_s *dev, int source); void (*ackint)(FAR struct stm32_tim_dev_s *dev, int source); - int (*setcapturecfg)(FAR struct stm32_tim_dev_s *dev, uint8_t channel, uint8_t capt_filter, uint8_t capt_prescaler); }; /************************************************************************************ From 9cc2119c4ef753cc559b97d4e487f3eb0fa4aa31 Mon Sep 17 00:00:00 2001 From: pnb Date: Tue, 22 Dec 2015 16:28:19 +0100 Subject: [PATCH 12/16] capture compile --- arch/arm/src/stm32/Kconfig | 39 +-- arch/arm/src/stm32/Make.defs | 2 + arch/arm/src/stm32/stm32_capture.c | 485 ++++++++++++++++------------- arch/arm/src/stm32/stm32_capture.h | 38 ++- 4 files changed, 313 insertions(+), 251 deletions(-) diff --git a/arch/arm/src/stm32/Kconfig b/arch/arm/src/stm32/Kconfig index 07218af0655..195e9af07ad 100644 --- a/arch/arm/src/stm32/Kconfig +++ b/arch/arm/src/stm32/Kconfig @@ -1433,6 +1433,7 @@ config STM32_STM32F407 select STM32_HAVE_UART5 select STM32_HAVE_USART6 select STM32_HAVE_TIM1 + select STM32_HAVE_TIM2 select STM32_HAVE_TIM5 select STM32_HAVE_TIM6 select STM32_HAVE_TIM7 @@ -1603,6 +1604,10 @@ config STM32_HAVE_TIM1 bool default n +config STM32_HAVE_TIM2 + bool + default n + config STM32_HAVE_TIM3 bool default n @@ -5121,17 +5126,7 @@ config STM32_TIM14_DAC2 endchoice config STM32_TIM1_CAP - bool "TIM1 Capture - default n - depends on STM32_HAVE_TIM1 - ---help--- - Reserve timer 1 for use by Capture - - Timer devices may be used for different purposes. One special purpose is - to capture input. - -config STM32_TIM1_CAP - bool "TIM1 Capture + bool "TIM1 Capture" default n depends on STM32_HAVE_TIM1 ---help--- @@ -5141,7 +5136,7 @@ config STM32_TIM1_CAP to capture input. config STM32_TIM2_CAP - bool "TIM2 Capture + bool "TIM2 Capture" default n depends on STM32_HAVE_TIM2 ---help--- @@ -5151,7 +5146,7 @@ config STM32_TIM2_CAP to capture input. config STM32_TIM3_CAP - bool "TIM3 Capture + bool "TIM3 Capture" default n depends on STM32_HAVE_TIM3 ---help--- @@ -5161,7 +5156,7 @@ config STM32_TIM3_CAP to capture input. config STM32_TIM4_CAP - bool "TIM4 Capture + bool "TIM4 Capture" default n depends on STM32_HAVE_TIM4 ---help--- @@ -5171,7 +5166,7 @@ config STM32_TIM4_CAP to capture input. config STM32_TIM5_CAP - bool "TIM5 Capture + bool "TIM5 Capture" default n depends on STM32_HAVE_TIM5 ---help--- @@ -5181,7 +5176,7 @@ config STM32_TIM5_CAP to capture input. config STM32_TIM8_CAP - bool "TIM8 Capture + bool "TIM8 Capture" default n depends on STM32_HAVE_TIM8 ---help--- @@ -5191,7 +5186,7 @@ config STM32_TIM8_CAP to capture input. config STM32_TIM9_CAP - bool "TIM9 Capture + bool "TIM9 Capture" default n depends on STM32_HAVE_TIM9 ---help--- @@ -5201,7 +5196,7 @@ config STM32_TIM9_CAP to capture input. config STM32_TIM10_CAP - bool "TIM10 Capture + bool "TIM10 Capture" default n depends on STM32_HAVE_TIM10 ---help--- @@ -5211,7 +5206,7 @@ config STM32_TIM10_CAP to capture input. config STM32_TIM11_CAP - bool "TIM11 Capture + bool "TIM11 Capture" default n depends on STM32_HAVE_TIM11 ---help--- @@ -5221,7 +5216,7 @@ config STM32_TIM11_CAP to capture input. config STM32_TIM12_CAP - bool "TIM12 Capture + bool "TIM12 Capture" default n depends on STM32_HAVE_TIM12 ---help--- @@ -5231,7 +5226,7 @@ config STM32_TIM12_CAP to capture input. config STM32_TIM13_CAP - bool "TIM13 Capture + bool "TIM13 Capture" default n depends on STM32_HAVE_TIM13 ---help--- @@ -5241,7 +5236,7 @@ config STM32_TIM13_CAP to capture input. config STM32_TIM14_CAP - bool "TIM14 Capture + bool "TIM14 Capture" default n depends on STM32_HAVE_TIM14 ---help--- diff --git a/arch/arm/src/stm32/Make.defs b/arch/arm/src/stm32/Make.defs index e1b4d6d9fd7..3bbef1c4bdb 100644 --- a/arch/arm/src/stm32/Make.defs +++ b/arch/arm/src/stm32/Make.defs @@ -110,6 +110,7 @@ CHIP_CSRCS = stm32_allocateheap.c stm32_start.c stm32_rcc.c stm32_lse.c CHIP_CSRCS += stm32_lsi.c stm32_gpio.c stm32_exti_gpio.c stm32_flash.c stm32_irq.c CHIP_CSRCS += stm32_dma.c stm32_lowputc.c stm32_serial.c stm32_spi.c CHIP_CSRCS += stm32_sdio.c stm32_tim.c stm32_waste.c stm32_ccm.c stm32_uid.c +CHIP_CSRCS += stm32_capture.c ifeq ($(CONFIG_TIMER),y) CHIP_CSRCS += stm32_tim_lowerhalf.c @@ -228,6 +229,7 @@ ifeq ($(CONFIG_QENCODER),y) CHIP_CSRCS += stm32_qencoder.c endif + ifeq ($(CONFIG_CAN),y) CHIP_CSRCS += stm32_can.c endif diff --git a/arch/arm/src/stm32/stm32_capture.c b/arch/arm/src/stm32/stm32_capture.c index 360c9101c08..d5a4f053456 100644 --- a/arch/arm/src/stm32/stm32_capture.c +++ b/arch/arm/src/stm32/stm32_capture.c @@ -87,7 +87,14 @@ #endif #if defined(CONFIG_STM32_TIM1_CAP) || defined(CONFIG_STM32_TIM1_CAP) -#define HAVE_ADANCED_TIM 1 +#define USE_ADVENCED_TIM 1 +#endif + +#if defined(GPIO_TIM1_EXT_CLK_IN) || defined(GPIO_TIM2_EXT_CLK_IN) || \ + defined(GPIO_TIM3_EXT_CLK_IN) || defined(GPIO_TIM4_EXT_CLK_IN) || \ + defined(GPIO_TIM5_EXT_CLK_IN) || defined(GPIO_TIM8_EXT_CLK_IN) || \ + defined(GPIO_TIM9_EXT_CLK_IN) || defined(GPIO_TIM12_EXT_CLK_IN) +# define USE_EXT_CLOCK 1 #endif /* This module then only compiles if there are enabled timers that are not intended for @@ -110,8 +117,11 @@ struct stm32_cap_priv_s { const struct stm32_cap_ops_s *ops; const uint32_t base; /* TIMn base address */ +#ifdef USE_EXT_CLOCK + const uint32_t gpio_clk; /* TIMn base address */ +#endif const int irq; /* irq vector */ -#define HAVE_ADANCED_TIM 1 +#ifdef USE_ADVENCED_TIM const int irq_of; /* irq timer overflow is deferent in advanced timer */ #endif }; @@ -122,7 +132,7 @@ struct stm32_cap_priv_s /* Get a 16-bit register value by offset */ -static inline uint16_t stm32_getreg16(FAR struct stm32_cap_priv_s *priv, +static inline uint16_t stm32_getreg16(FAR const struct stm32_cap_priv_s *priv, uint8_t offset) { return getreg16(priv->base + offset); @@ -130,7 +140,7 @@ static inline uint16_t stm32_getreg16(FAR struct stm32_cap_priv_s *priv, /* Put a 16-bit register value by offset */ -static inline void stm32_putreg16(FAR struct stm32_cap_priv_s *priv, uint8_t offset, +static inline void stm32_putreg16(FAR const struct stm32_cap_priv_s *priv, uint8_t offset, uint16_t value) { putreg16(value, priv->base + offset); @@ -138,7 +148,7 @@ static inline void stm32_putreg16(FAR struct stm32_cap_priv_s *priv, uint8_t off /* Modify a 16-bit register value by offset */ -static inline void stm32_modifyreg16(FAR struct stm32_cap_priv_s *priv, +static inline void stm32_modifyreg16(FAR const struct stm32_cap_priv_s *priv, uint8_t offset, uint16_t clearbits, uint16_t setbits) { @@ -149,7 +159,7 @@ static inline void stm32_modifyreg16(FAR struct stm32_cap_priv_s *priv, * 32-bit registers (CNT, ARR, CRR1-4) in the 32-bit timers TIM2-5. */ -static inline uint32_t stm32_getreg32(FAR struct stm32_cap_priv_s *priv, +static inline uint32_t stm32_getreg32(FAR const struct stm32_cap_priv_s *priv, uint8_t offset) { return getreg32(priv->base + offset); @@ -159,7 +169,7 @@ static inline uint32_t stm32_getreg32(FAR struct stm32_cap_priv_s *priv, * 32-bit registers (CNT, ARR, CRR1-4) in the 32-bit timers TIM2-5. */ -static inline void stm32_putreg32(FAR struct stm32_cap_priv_s *priv, uint8_t offset, +static inline void stm32_putreg32(FAR const struct stm32_cap_priv_s *priv, uint8_t offset, uint32_t value) { putreg32(value, priv->base + offset); @@ -169,8 +179,8 @@ static inline void stm32_putreg32(FAR struct stm32_cap_priv_s *priv, uint8_t off /************************************************************************************ * gpio Functions ************************************************************************************/ - -static inline uint32_t stm32_cap_gpio(FAR struct stm32_cap_priv_s *priv, int channel) +#define GPIO_CLK -1 +static inline uint32_t stm32_cap_gpio(FAR const struct stm32_cap_priv_s *priv, int channel) { switch(priv->base) @@ -179,6 +189,9 @@ static inline uint32_t stm32_cap_gpio(FAR struct stm32_cap_priv_s *priv, int cha case STM32_TIM1_BASE: switch (channel) { +#ifdef GPIO_TIM1_EXT_CLK_IN + case GPIO_CLK: return GPIO_TIM1_EXT_CLK_IN; +#endif #if defined(GPIO_TIM1_CH1IN) case 0: return GPIO_TIM1_CH1IN; #endif @@ -198,6 +211,9 @@ static inline uint32_t stm32_cap_gpio(FAR struct stm32_cap_priv_s *priv, int cha case STM32_TIM2_BASE: switch (channel) { +#ifdef GPIO_TIM2_EXT_CLK_IN + case GPIO_CLK: return GPIO_TIM2_EXT_CLK_IN; +#endif #if defined(GPIO_TIM2_CH1IN) case 0: return GPIO_TIM2_CH1IN; #endif @@ -217,6 +233,9 @@ static inline uint32_t stm32_cap_gpio(FAR struct stm32_cap_priv_s *priv, int cha case STM32_TIM3_BASE: switch (channel) { +#ifdef GPIO_TIM3_EXT_CLK_IN + case GPIO_CLK: return GPIO_TIM3_EXT_CLK_IN; +#endif #if defined(GPIO_TIM3_CH1IN) case 0: return GPIO_TIM3_CH1IN; #endif @@ -236,6 +255,9 @@ static inline uint32_t stm32_cap_gpio(FAR struct stm32_cap_priv_s *priv, int cha case STM32_TIM4_BASE: switch (channel) { +#ifdef GPIO_TIM4_EXT_CLK_IN + case GPIO_CLK: return GPIO_TIM4_EXT_CLK_IN; +#endif #if defined(GPIO_TIM4_CH1IN) case 0: return GPIO_TIM4_CH1IN; #endif @@ -255,6 +277,9 @@ static inline uint32_t stm32_cap_gpio(FAR struct stm32_cap_priv_s *priv, int cha case STM32_TIM5_BASE: switch (channel) { +#ifdef GPIO_TIM5_EXT_CLK_IN + case GPIO_CLK: return GPIO_TIM5_EXT_CLK_IN; +#endif #if defined(GPIO_TIM5_CH1IN) case 0: return GPIO_TIM5_CH1IN; #endif @@ -275,6 +300,9 @@ static inline uint32_t stm32_cap_gpio(FAR struct stm32_cap_priv_s *priv, int cha case STM32_TIM8_BASE: switch (channel) { +#ifdef GPIO_TIM8_EXT_CLK_IN + case GPIO_CLK: return GPIO_TIM8_EXT_CLK_IN; +#endif #if defined(GPIO_TIM8_CH1IN) case 0: return GPIO_TIM8_CH1OUIN ; #endif @@ -291,16 +319,110 @@ static inline uint32_t stm32_cap_gpio(FAR struct stm32_cap_priv_s *priv, int cha break; #endif } - return gpio; + return 0; +} + +static inline int stm32_cap_set_rcc(FAR const struct stm32_cap_priv_s *priv, + bool on) +{ + uint32_t offset = 0; + uint32_t mask = 0; + + switch (priv->base) + { +#ifdef CONFIG_STM32_TIM1_CAP + case 1: + offset = STM32_RCC_APB2ENR; + mask = RCC_APB2ENR_TIM1EN; + break; +#endif +#ifdef CONFIG_STM32_TIM2_CAP + case 2: + offset = STM32_RCC_APB1ENR; + mask = RCC_APB1ENR_TIM2EN; + break; +#endif +#ifdef CONFIG_STM32_TIM3_CAP + case 3: + offset = STM32_RCC_APB1ENR; + mask = RCC_APB1ENR_TIM3EN; + break; +#endif +#ifdef CONFIG_STM32_TIM4_CAP + case 4: + offset = STM32_RCC_APB1ENR; + mask = RCC_APB1ENR_TIM4EN; + break; +#endif +#ifdef CONFIG_STM32_TIM5_CAP + case 5: + offset = STM32_RCC_APB1ENR; + mask = RCC_APB1ENR_TIM5EN; + break; +#endif +/* TIM6 and TIM7 cannot be used in cap */ +#ifdef CONFIG_STM32_TIM8_CAP + case 8: + offset = STM32_RCC_APB2ENR; + mask = RCC_APB2ENR_TIM8EN; + break; +#endif +#ifdef CONFIG_STM32_TIM9_CAP + case 9: + offset = STM32_RCC_APB2ENR; + mask = RCC_APB2ENR_TIM9EN; + break; +#endif +#ifdef CONFIG_STM32_TIM10_CAP + case 10: + offset = STM32_RCC_APB2ENR; + mask = RCC_APB2ENR_TIM10EN; + break; +#endif +#ifdef CONFIG_STM32_TIM11_CAP + case 11: + offset = STM32_RCC_APB2ENR; + mask = RCC_APB2ENR_TIM11EN; + break; +#endif +#ifdef CONFIG_STM32_TIM12_CAP + case 12: + offset = STM32_RCC_APB1ENR; + mask = RCC_APB2ENR_TIM12EN; + break; +#endif +#ifdef CONFIG_STM32_TIM13_CAP + case 13: + offset = STM32_RCC_APB1ENR; + mask = RCC_APB2ENR_TIM13EN; + break; +#endif +#ifdef CONFIG_STM32_TIM14_CAP + case 14: + offset = STM32_RCC_APB1ENR; + mask = RCC_APB2ENR_TIM14EN; + break; +#endif + } + + if ( mask == 0 ) + return ERROR; + + if ( on ) + modifyreg32(offset, 0, mask); + else + modifyreg32(offset, mask, 0); + + return OK; } /************************************************************************************ * Basic Functions ************************************************************************************/ -static int stm32_cap_setclock(FAR struct stm32_cap_dev_s *dev, stm32_cap_clk_src_t clk_src, - uint32_t prescaler) +static int stm32_cap_setclock(FAR struct stm32_cap_dev_s *dev, stm32_cap_clk_t clk, + uint32_t prescaler,uint32_t max) { - struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; + const struct stm32_cap_priv_s *priv = (const struct stm32_cap_priv_s *)dev; uint16_t regval = 0; if (prescaler == 0) @@ -321,14 +443,14 @@ static int stm32_cap_setclock(FAR struct stm32_cap_dev_s *dev, stm32_cap_clk_src prescaler = 0xffff; - switch(clk_src) + switch(clk) { case STM32_CAP_CLK_INT: regval = GTIM_SMCR_DISAB; break; case STM32_CAP_CLK_EXT: - regval = GTIM_SMCR_EXTCLK1 + regval = GTIM_SMCR_EXTCLK1; break; /* TODO: Add other case */ @@ -340,7 +462,7 @@ static int stm32_cap_setclock(FAR struct stm32_cap_dev_s *dev, stm32_cap_clk_src stm32_modifyreg16(priv, STM32_BTIM_EGR_OFFSET, GTIM_SMCR_SMS_MASK, regval ); // Set Maximum - stm32_putreg32(priv, STM32_BTIM_ARR_OFFSET, period); + stm32_putreg32(priv, STM32_BTIM_ARR_OFFSET, max); // Set prescaler stm32_putreg16(priv, STM32_BTIM_PSC_OFFSET, prescaler); @@ -351,7 +473,7 @@ static int stm32_cap_setclock(FAR struct stm32_cap_dev_s *dev, stm32_cap_clk_src //enable timer stm32_modifyreg16(priv, STM32_BTIM_CR1_OFFSET,0,BTIM_CR1_CEN); -#ifdef HAVE_ADANCED_TIM +#ifdef USE_ADVENCED_TIM /* Advanced registers require Main Output Enable */ if ((priv->base == STM32_TIM1_BASE) || (priv->base == STM32_TIM8_BASE)) { @@ -362,18 +484,18 @@ static int stm32_cap_setclock(FAR struct stm32_cap_dev_s *dev, stm32_cap_clk_src return prescaler; } -static int stm32_cap_setisr(FAR struct stm32_cap_dev_s *dev, xcpt_t handler); +static int stm32_cap_setisr(FAR struct stm32_cap_dev_s *dev, xcpt_t handler) { - struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; + const struct stm32_cap_priv_s *priv = (const struct stm32_cap_priv_s *)dev; int irq; -#ifdef HAVE_ADANCED_TIM +#ifdef USE_ADVENCED_TIM int irq_of; #endif ASSERT(dev); irq = priv->irq; -#ifdef HAVE_ADANCED_TIM +#ifdef USE_ADVENCED_TIM irq_of = priv->irq_of; #endif @@ -383,7 +505,7 @@ static int stm32_cap_setisr(FAR struct stm32_cap_dev_s *dev, xcpt_t handler); { up_disable_irq(irq); irq_detach(irq); -#ifdef HAVE_ADANCED_TIM +#ifdef USE_ADVENCED_TIM if (priv->irq_of) { up_disable_irq(irq_of); @@ -398,7 +520,7 @@ static int stm32_cap_setisr(FAR struct stm32_cap_dev_s *dev, xcpt_t handler); irq_attach(irq, handler); up_enable_irq(irq); -#ifdef HAVE_ADANCED_TIM +#ifdef USE_ADVENCED_TIM if (priv->irq_of) { irq_attach(priv->irq_of, handler); @@ -411,7 +533,7 @@ static int stm32_cap_setisr(FAR struct stm32_cap_dev_s *dev, xcpt_t handler); up_prioritize_irq(irq, NVIC_SYSH_PRIORITY_DEFAULT); -# ifdef HAVE_ADANCED_TIM +# ifdef USE_ADVENCED_TIM if (priv->irq_of) { up_prioritize_irq(irq_of, NVIC_SYSH_PRIORITY_DEFAULT); @@ -427,20 +549,20 @@ static int stm32_cap_setisr(FAR struct stm32_cap_dev_s *dev, xcpt_t handler); static void stm32_cap_enableint(FAR struct stm32_cap_dev_s *dev, stm32_cap_flags_t src, bool on) { - struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; - uint16_t mask; + const struct stm32_cap_priv_s *priv = (const struct stm32_cap_priv_s *)dev; + uint16_t mask = 0; ASSERT(dev); - if (src & STM32_TIM_FLAG_IRQ_OVERFLOW) - regval |= ATIM_DIER_UIE; - if (src & STM32_TIM_FLAG_IRQ_CAPTURE_1) - regval |= ATIM_DIER_CC1IE; - if (src & STM32_TIM_FLAG_IRQ_CAPTURE_2) - regval |= ATIM_DIER_CC1IE; - if (src & STM32_TIM_FLAG_IRQ_CAPTURE_3) - regval |= ATIM_DIER_CC1IE; - if (src & STM32_TIM_FLAG_IRQ_CAPTURE_4) - regval |= ATIM_DIER_CC1IE; + if (src & STM32_CAP_FLAG_IRG_COUNTER) + mask |= ATIM_DIER_UIE; + if (src & STM32_CAP_FLAG_IRQ_CH_1) + mask |= ATIM_DIER_CC1IE; + if (src & STM32_CAP_FLAG_IRQ_CH_2) + mask |= ATIM_DIER_CC1IE; + if (src & STM32_CAP_FLAG_IRQ_CH_3) + mask |= ATIM_DIER_CC1IE; + if (src & STM32_CAP_FLAG_IRQ_CH_4) + mask |= ATIM_DIER_CC1IE; /* Not IRQ on channel overflow */ @@ -451,66 +573,66 @@ static void stm32_cap_enableint(FAR struct stm32_cap_dev_s *dev, } -static void stm32_cap_ackflags(FAR struct stm32_cap_dev_s *dev, int src) +static void stm32_cap_ackflags(FAR struct stm32_cap_dev_s *dev, int flags) { - struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; + const struct stm32_cap_priv_s *priv = (const struct stm32_cap_priv_s *)dev; uint16_t mask = 0; - if (src & STM32_TIM_FLAG_IRQ_OVERFLOW) - regval |= ATIM_SR_UIF; + if (flags & STM32_CAP_FLAG_IRG_COUNTER) + mask |= ATIM_SR_UIF; - if (src & STM32_TIM_FLAG_IRQ_CAPTURE_1) - regval |= ATIM_SR_CC1IF; - if (src & STM32_TIM_FLAG_IRQ_CAPTURE_2) - regval |= ATIM_SR_CC2IF; - if (src & STM32_TIM_FLAG_IRQ_CAPTURE_3) - regval |= ATIM_SR_CC3IF; - if (src & STM32_TIM_FLAG_IRQ_CAPTURE_4) - regval |= ATIM_SR_CC4IF; + if (flags & STM32_CAP_FLAG_IRQ_CH_1) + mask |= ATIM_SR_CC1IF; + if (flags & STM32_CAP_FLAG_IRQ_CH_2) + mask |= ATIM_SR_CC2IF; + if (flags & STM32_CAP_FLAG_IRQ_CH_3) + mask |= ATIM_SR_CC3IF; + if (flags & STM32_CAP_FLAG_IRQ_CH_4) + mask |= ATIM_SR_CC4IF; - if (src & STM32_TIM_FLAG_OF_CAPTURE_1) - regval |= ATIM_SR_CC1OF; - if (src & STM32_TIM_FLAG_OF_CAPTURE_2) - regval |= ATIM_SR_CC2OF; - if (src & STM32_TIM_FLAG_OF_CAPTURE_3) - regval |= ATIM_SR_CC3OF; - if (src & STM32_TIM_FLAG_OF_CAPTURE_4) - regval |= ATIM_SR_CC4OF; + if (flags & STM32_CAP_FLAG_OF_CH_1) + mask |= ATIM_SR_CC1OF; + if (flags & STM32_CAP_FLAG_OF_CH_2) + mask |= ATIM_SR_CC2OF; + if (flags & STM32_CAP_FLAG_OF_CH_3) + mask |= ATIM_SR_CC3OF; + if (flags & STM32_CAP_FLAG_OF_CH_4) + mask |= ATIM_SR_CC4OF; stm32_putreg16(priv, STM32_BTIM_SR_OFFSET, ~mask); } -static stm32_cap_flags_t stm32_cap_getint(FAR struct stm32_cap_dev_s *dev) +static stm32_cap_flags_t stm32_cap_getflags(FAR struct stm32_cap_dev_s *dev) { - struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; + const struct stm32_cap_priv_s *priv = (const struct stm32_cap_priv_s *)dev; uint16_t regval = 0; - stm32_cap_flags_t src = 0; + stm32_cap_flags_t flags = 0; regval = stm32_getreg16(priv, STM32_BTIM_SR_OFFSET); if (regval & ATIM_SR_UIF) - src |= STM32_TIM_FLAG_IRQ_OVERFLOW; + flags |= STM32_CAP_FLAG_IRG_COUNTER; if (regval & ATIM_SR_CC1IF) - src |= STM32_TIM_FLAG_IRQ_CAPTURE_1; + flags |= STM32_CAP_FLAG_IRQ_CH_1; if (regval & ATIM_SR_CC2IF) - src |= STM32_TIM_FLAG_IRQ_CAPTURE_2; + flags |= STM32_CAP_FLAG_IRQ_CH_2; if (regval & ATIM_SR_CC3IF) - src |= STM32_TIM_FLAG_IRQ_CAPTURE_3; + flags |= STM32_CAP_FLAG_IRQ_CH_3; if (regval & ATIM_SR_CC4IF) - src |= STM32_TIM_FLAG_IRQ_CAPTURE_4; + flags |= STM32_CAP_FLAG_IRQ_CH_4; if (regval & ATIM_SR_CC1OF) - src |= STM32_TIM_FLAG_OF_CAPTURE_1; + flags |= STM32_CAP_FLAG_OF_CH_1; if (regval & ATIM_SR_CC2OF) - src |= STM32_TIM_FLAG_OF_CAPTURE_2; + flags |= STM32_CAP_FLAG_OF_CH_2; if (regval & ATIM_SR_CC3OF) - src |= STM32_TIM_FLAG_OF_CAPTURE_3; + flags |= STM32_CAP_FLAG_OF_CH_3; if (regval & ATIM_SR_CC4OF) - src |= STM32_TIM_FLAG_OF_CAPTURE_4; + flags |= STM32_CAP_FLAG_OF_CH_4; - return src; + return flags; } @@ -519,10 +641,9 @@ static stm32_cap_flags_t stm32_cap_getint(FAR struct stm32_cap_dev_s *dev) ************************************************************************************/ static int stm32_cap_setchannel(FAR struct stm32_cap_dev_s *dev, uint8_t channel, - stm32_cap_ch_cfg_t edge) + stm32_cap_ch_cfg_t cfg) { - int i; - struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; + const struct stm32_cap_priv_s *priv = (const struct stm32_cap_priv_s *)dev; uint32_t gpio = 0; uint16_t mask; uint16_t regval; @@ -556,36 +677,27 @@ static int stm32_cap_setchannel(FAR struct stm32_cap_dev_s *dev, uint8_t channel return ERROR; } - mask = (GTIM_CCER_CC1E | GTIM_CCER_CC1P | GTIM_CCER_CC1NP) + mask = (GTIM_CCER_CC1E | GTIM_CCER_CC1P | GTIM_CCER_CC1NP); mask <<= (channel << 2); regval <<= (channel << 2); - stm32_modifyreg16(priv,mask,regval); + stm32_modifyreg16(priv,STM32_GTIM_CCER_OFFSET,mask,regval); /* Set ccmr */ - ccmr_val = cfg; - - if ( (ccmr_val & GTIM_CCMR1_CC1S_MASK ) == 0 ) - return ERROR; /* configured as output */ - - if ( (ccmr_val & GTIM_CCMR1_CC1S_MASK ) == 3 ) - return ERROR; /* Not implemented */ - - /* Define its position (shift) and get register offset */ - - mask = (GTIM_CCMR1_IC1F_MASK | GTIM_CCMR1_IC1PSC_MASK | GTIM_CCMR1_CC1S_MASK) - ccmr_val &= mask + regval = cfg; + mask = (GTIM_CCMR1_IC1F_MASK | GTIM_CCMR1_IC1PSC_MASK | GTIM_CCMR1_CC1S_MASK); + regval &= mask; if (channel & 1) { - ccmr_val <<= 8; - ccmr_mask <<= 8; + regval <<= 8; + mask <<= 8; } if (channel < 2) - stm32_modifyreg16(priv,STM32_GTIM_CCMR1_OFFSET,ccmr_mask,ccmr_val); + stm32_modifyreg16(priv,STM32_GTIM_CCMR1_OFFSET,mask,regval); else - stm32_modifyreg16(priv,STM32_GTIM_CCMR2_OFFSET,ccmr_mask,ccmr_val); + stm32_modifyreg16(priv,STM32_GTIM_CCMR2_OFFSET,mask,regval); /* set GPIO */ @@ -600,26 +712,26 @@ static int stm32_cap_setchannel(FAR struct stm32_cap_dev_s *dev, uint8_t channel static int stm32_cap_getcapture(FAR struct stm32_cap_dev_s *dev, uint8_t channel) { - struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; + const struct stm32_cap_priv_s *priv = (const struct stm32_cap_priv_s *)dev; ASSERT(dev); switch (channel) { #ifdef HAVE_CH1IN case 1: - return stm32_getreg32(dev, STM32_GTIM_CCR1_OFFSET); + return stm32_getreg32(priv, STM32_GTIM_CCR1_OFFSET); #endif #ifdef HAVE_CH1IN case 2: - return stm32_getreg32(dev, STM32_GTIM_CCR2_OFFSET); + return stm32_getreg32(priv, STM32_GTIM_CCR2_OFFSET); #endif #ifdef HAVE_CH1IN case 3: - return stm32_getreg32(dev, STM32_GTIM_CCR3_OFFSET); + return stm32_getreg32(priv, STM32_GTIM_CCR3_OFFSET); #endif #ifdef HAVE_CH1IN case 4: - return stm32_getreg32(dev, STM32_GTIM_CCR4_OFFSET); + return stm32_getreg32(priv, STM32_GTIM_CCR4_OFFSET); #endif } @@ -653,150 +765,99 @@ const struct stm32_cap_priv_s stm32_tim2_priv = { .ops = &stm32_cap_ops, .base = STM32_TIM2_BASE, - .irg = STM32_IRQ_TIM2, -#define HAVE_ADANCED_TIM 1 + .irq = STM32_IRQ_TIM2, +#ifdef USE_ADVENCED_TIM .irg_of = -1, #endif - .gpio_clk = GPIO_TIM2_CLKIN; - .channels = { -#if defined(GPIO_TIM2_CH1IN) - .gpio = GPIO_TIM2_CH1IN; -#endif -#if defined(GPIO_TIM2_CH2IN) - .gpio = GPIO_TIM2_CH2IN; - .ccmr = ( GPIO_TIM2_CH2_ICF << ATIM_CCMR1_IC1F_SHIFT )|\ - ( GPIO_TIM2_CH2_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); -#endif -#if defined(GPIO_TIM2_CH3IN) - .gpio = GPIO_TIM2_CH3IN; - .ccmr = ( GPIO_TIM2_CH3_ICF << ATIM_CCMR1_IC1F_SHIFT )|\ - ( GPIO_TIM2_CH3_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); -#endif -#if defined(GPIO_TIM2_CH4IN) - .gpio = GPIO_TIM2_CH4IN; - .ccmr = ( GPIO_TIM2_CH4_ICF << ATIM_CCMR1_IC1F_SHIFT )|\ - ( GPIO_TIM2_CH4_ICPSC << ATIM_CCMR1_IC1PSC_SHIFT ); -#endif - } }; #endif +static inline const struct stm32_cap_priv_s * stm32_cap_get_priv(int timer) +{ + switch (timer) + { +#ifdef CONFIG_STM32_TIM1_CAP + case 1: return &stm32_tim1_priv; +#endif +#ifdef CONFIG_STM32_TIM2_CAP + case 2: return &stm32_tim2_priv; +#endif +#ifdef CONFIG_STM32_TIM3_CAP + case 3: return &stm32_tim3_priv; +#endif +#ifdef CONFIG_STM32_TIM4_CAP + case 4: return &stm32_tim4_priv; +#endif +#ifdef CONFIG_STM32_TIM5_CAP + case 5: return &stm32_tim5_priv; +#endif +/* TIM6 and TIM7 cannot be used in capture */ +#ifdef CONFIG_STM32_TIM8_CAP + case 8: return &stm32_tim8_priv; +#endif +#ifdef CONFIG_STM32_TIM9_CAP + case 9: return &stm32_tim9_priv; +#endif +#ifdef CONFIG_STM32_TIM10_CAP + case 9: return &stm32_tim10_priv; +#endif +#ifdef CONFIG_STM32_TIM11_CAP + case 9: return &stm32_tim11_priv; +#endif +#ifdef CONFIG_STM32_TIM12_CAP + case 9: return &stm32_tim12_priv; +#endif +#ifdef CONFIG_STM32_TIM13_CAP + case 9: return &stm32_tim13_priv; +#endif +#ifdef CONFIG_STM32_TIM14_CAP + case 9: return &stm32_tim14_priv; +#endif + } + return NULL; +} + /************************************************************************************ * Public Function - Initialization ************************************************************************************/ FAR struct stm32_cap_dev_s *stm32_cap_init(int timer) { - struct stm32_cap_priv_s *priv = NULL; + const struct stm32_cap_priv_s *priv = stm32_cap_get_priv(timer); + uint32_t gpio; - /* Get structure and enable power */ - - switch (timer) + if ( priv ) { -#ifdef CONFIG_STM32_TIM1_CAP - case 1: - priv = &stm32_tim1_priv; - modifyreg32(STM32_RCC_APB2ENR, 0, RCC_APB2ENR_TIM1EN); - break; -#endif -#ifdef CONFIG_STM32_TIM2_CAP - case 2: - priv = &stm32_tim2_priv; - modifyreg32(STM32_RCC_APB1ENR, 0, RCC_APB1ENR_TIM2EN); - break; -#endif -#ifdef CONFIG_STM32_TIM3_CAP - case 3: - priv = &stm32_tim3_priv; - modifyreg32(STM32_RCC_APB1ENR, 0, RCC_APB1ENR_TIM3EN); - break; -#endif -#ifdef CONFIG_STM32_TIM4_CAP - case 4: - priv = &stm32_tim4_priv; - modifyreg32(STM32_RCC_APB1ENR, 0, RCC_APB1ENR_TIM4EN); - break; -#endif -#ifdef CONFIG_STM32_TIM5_CAP - case 5: - priv = &stm32_tim5_priv; - modifyreg32(STM32_RCC_APB1ENR, 0, RCC_APB1ENR_TIM5EN); - break; -#endif -/* TIM6 and TIM7 cannot be used in capture */ -#ifdef CONFIG_STM32_TIM8_CAP - case 8: - priv = &stm32_tim8_priv; - modifyreg32(STM32_RCC_APB2ENR, 0, RCC_APB2ENR_TIM8EN); - break; -#endif -#ifdef CONFIG_STM32_TIM9_CAP - case 9: - priv = &stm32_tim9_priv; - modifyreg32(STM32_RCC_APB2ENR, 0, RCC_APB2ENR_TIM8EN); - break; -#endif - default: - return NULL; + + stm32_cap_set_rcc(priv,true); + + gpio = stm32_cap_gpio(priv,GPIO_CLK); + if (gpio) + stm32_configgpio(gpio); + + // disable timer while is not configured + stm32_modifyreg16(priv, STM32_BTIM_CR1_OFFSET, ATIM_CR1_CEN, 0); } - if (priv->gpio_clk) - stm32_configgpio(priv->gpio_clk) - - // disable timer while is not configured - stm32_modifyreg16(priv, STM32_BTIM_CR1_OFFSET, ATIM_CR1_CEN, 0); - return (struct stm32_cap_dev_s *)priv; } int stm32_cap_deinit(FAR struct stm32_cap_dev_s * dev) { - struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; + const struct stm32_cap_priv_s *priv = (struct stm32_cap_priv_s *)dev; + uint32_t gpio; ASSERT(dev); // disable timer while is not configured - stm32_modifyreg16(dev, STM32_BTIM_CR1_OFFSET, ATIM_CR1_CEN, 0); + stm32_modifyreg16(priv, STM32_BTIM_CR1_OFFSET, ATIM_CR1_CEN, 0); - if (priv->gpio_clk) - stm32_unconfiggpio(priv->gpio_clk) + gpio = stm32_cap_gpio(priv,GPIO_CLK); + if (gpio) + stm32_unconfiggpio(gpio); - switch (priv->base) - { -#ifdef CONFIG_STM32_TIM2_CAP - case STM32_TIM2_BASE: - modifyreg32(STM32_RCC_APB1ENR, RCC_APB1ENR_TIM2EN, 0); - break; -#endif -#ifdef CONFIG_STM32_TIM3_CAP - case STM32_TIM3_BASE: - modifyreg32(STM32_RCC_APB1ENR, RCC_APB1ENR_TIM3EN, 0); - break; -#endif -#ifdef CONFIG_STM32_TIM4_CAP - case STM32_TIM4_BASE: - modifyreg32(STM32_RCC_APB1ENR, RCC_APB1ENR_TIM4EN, 0); - break; -#endif -#ifdef CONFIG_STM32_TIM5_CAP - case STM32_TIM5_BASE: - modifyreg32(STM32_RCC_APB1ENR, RCC_APB1ENR_TIM5EN, 0); - break; -#endif -#ifdef CONFIG_STM32_TIM1_CAP - case STM32_TIM1_BASE: - modifyreg32(STM32_RCC_APB2ENR, RCC_APB2ENR_TIM1EN, 0); - break; -#endif -#ifdef CONFIG_STM32_TIM8_CAP - case STM32_TIM8_BASE: - modifyreg32(STM32_RCC_APB2ENR, RCC_APB2ENR_TIM8EN, 0); - break; -#endif - default: - return ERROR; - } + stm32_cap_set_rcc(priv,false); return OK; } diff --git a/arch/arm/src/stm32/stm32_capture.h b/arch/arm/src/stm32/stm32_capture.h index 744b6852690..d37c27ca310 100644 --- a/arch/arm/src/stm32/stm32_capture.h +++ b/arch/arm/src/stm32/stm32_capture.h @@ -53,13 +53,13 @@ ************************************************************************************/ /* Helpers **************************************************************************/ -#define STM32_TIM_SETCLOCK(d,freq) ((d)->ops->setclock(d,freq)) -#define STM32_TIM_SETCHANNEL(d,ch,mode) ((d)->ops->setchannel(d,ch,mode)) -#define STM32_TIM_GETCAPTURE(d,ch) ((d)->ops->getcapture(d,ch)) -#define STM32_TIM_SETISR(d,hnd,s) ((d)->ops->setisr(d,hnd,s)) -#define STM32_TIM_ENABLEINT(d,s,on) ((d)->ops->enableint(d,s,on)) -#define STM32_TIM_ACKFLAGS(d,s) ((d)->ops->ackflags(d,s)) -#define STM32_TIM_GETFLAGS(d) ((d)->ops->getflags(d)) +#define STM32_CAP_SETCLOCK(d,clk_src,psc,max) ((d)->ops->setclock(d,clk_src,psc,max)) +#define STM32_CAP_SETCHANNEL(d,ch,cfg) ((d)->ops->setchannel(d,ch,cfg)) +#define STM32_CAP_GETCAPTURE(d,ch) ((d)->ops->getcapture(d,ch)) +#define STM32_CAP_SETISR(d,hnd) ((d)->ops->setisr(d,hnd)) +#define STM32_CAP_ENABLEINT(d,s,on) ((d)->ops->enableint(d,s,on)) +#define STM32_CAP_ACKFLAGS(d,f) ((d)->ops->ackflags(d,f)) +#define STM32_CAP_GETFLAGS(d) ((d)->ops->getflags(d)) /************************************************************************************ * Public Types @@ -131,7 +131,7 @@ typedef enum } stm32_cap_ch_cfg_t; -/* TIM clock sources */ +/* Capture clock sources */ typedef enum { @@ -143,13 +143,13 @@ typedef enum } stm32_cap_clk_t; -/* TIM Sources */ +/* Capture flags */ typedef enum { /* One of the following */ - STM32_CAP_FLAG_IRQ_TIMER = (GTIM_SR_UIF), + STM32_CAP_FLAG_IRG_COUNTER = (GTIM_SR_UIF), STM32_CAP_FLAG_IRQ_CH_1 = (GTIM_SR_CC1IF), STM32_CAP_FLAG_IRQ_CH_2 = (GTIM_SR_CC2IF), @@ -163,16 +163,20 @@ typedef enum } stm32_cap_flags_t; +#define STM32_CAP_FLAG_IRQ_CH(ch) (GTIM_SR_CC1IF<<((ch)-1)) +#define STM32_CAP_FLAG_OF_CH(ch) (GTIM_SR_CC1OF<<((ch)-1)) -/* TIM Operations */ +/* Capture Operations */ struct stm32_cap_ops_s { - int (*setclock)(FAR struct stm32_cap_dev_s *dev, stm32_cap_clk_src_t clk_src, - uint32_t prescaler); - int (*setchannel)(FAR struct stm32_cap_dev_s *dev, uint8_t channel); - int (*setisr)(FAR struct stm32_tim_dev_s *dev,xcpt_t handler); - void (*enableint)(FAR struct stm32_tim_dev_s *dev, stm32_cap_flags_t src, bool on ); + int (*setclock)( FAR struct stm32_cap_dev_s *dev, stm32_cap_clk_t clk, uint32_t prescaler, uint32_t max); + int (*setchannel)(FAR struct stm32_cap_dev_s *dev, uint8_t channel, stm32_cap_ch_cfg_t cfg); + int (*getcapture)(FAR struct stm32_cap_dev_s *dev, uint8_t channel); + int (*setisr)( FAR struct stm32_cap_dev_s *dev, xcpt_t handler); + void (*enableint)( FAR struct stm32_cap_dev_s *dev, stm32_cap_flags_t src, bool on ); + void (*ackflags)( FAR struct stm32_cap_dev_s *dev, int flags); + stm32_cap_flags_t (*getflags)(FAR struct stm32_cap_dev_s *dev); }; /************************************************************************************ @@ -194,4 +198,4 @@ int stm32_cap_deinit(FAR struct stm32_cap_dev_s * dev); #endif #endif /* __ASSEMBLY__ */ -#endif /* __ARCH_ARM_SRC_STM32_STM32_TIM_H */ +#endif /* __ARCH_ARM_SRC_STM32_STM32_CAPTURE_H */ From cfe9555e24821889359f78c6cb0368a4e857d350 Mon Sep 17 00:00:00 2001 From: pnb Date: Wed, 23 Dec 2015 19:07:45 +0100 Subject: [PATCH 13/16] capture start to works need test it more --- arch/arm/src/stm32/stm32_capture.c | 188 +++++++++++++++++------------ arch/arm/src/stm32/stm32_capture.h | 3 +- 2 files changed, 113 insertions(+), 78 deletions(-) diff --git a/arch/arm/src/stm32/stm32_capture.c b/arch/arm/src/stm32/stm32_capture.c index d5a4f053456..b951e01b462 100644 --- a/arch/arm/src/stm32/stm32_capture.c +++ b/arch/arm/src/stm32/stm32_capture.c @@ -179,7 +179,6 @@ static inline void stm32_putreg32(FAR const struct stm32_cap_priv_s *priv, uint8 /************************************************************************************ * gpio Functions ************************************************************************************/ -#define GPIO_CLK -1 static inline uint32_t stm32_cap_gpio(FAR const struct stm32_cap_priv_s *priv, int channel) { @@ -190,19 +189,19 @@ static inline uint32_t stm32_cap_gpio(FAR const struct stm32_cap_priv_s *priv, i switch (channel) { #ifdef GPIO_TIM1_EXT_CLK_IN - case GPIO_CLK: return GPIO_TIM1_EXT_CLK_IN; + case STM32_CAP_CHANNEL_COUNTER: return GPIO_TIM1_EXT_CLK_IN; #endif -#if defined(GPIO_TIM1_CH1IN) - case 0: return GPIO_TIM1_CH1IN; +#ifdef GPIO_TIM1_CH1IN + case 1: return GPIO_TIM1_CH1IN; #endif -#if defined(GPIO_TIM1_CH2IN) - case 1: return GPIO_TIM1_CH2IN; +#ifdef GPIO_TIM1_CH2IN + case 2: return GPIO_TIM1_CH2IN; #endif -#if defined(GPIO_TIM1_CH3IN) - case 2: return GPIO_TIM1_CH3IN; +#ifdef GPIO_TIM1_CH3IN + case 3: return GPIO_TIM1_CH3IN; #endif -#if defined(GPIO_TIM1_CH4IN) - case 3: return GPIO_TIM1_CH4IN; +#ifdef GPIO_TIM1_CH4IN + case 4: return GPIO_TIM1_CH4IN; #endif } break; @@ -212,19 +211,19 @@ static inline uint32_t stm32_cap_gpio(FAR const struct stm32_cap_priv_s *priv, i switch (channel) { #ifdef GPIO_TIM2_EXT_CLK_IN - case GPIO_CLK: return GPIO_TIM2_EXT_CLK_IN; + case STM32_CAP_CHANNEL_COUNTER: return GPIO_TIM2_EXT_CLK_IN; #endif -#if defined(GPIO_TIM2_CH1IN) - case 0: return GPIO_TIM2_CH1IN; +#ifdef GPIO_TIM2_CH1IN + case 1: return GPIO_TIM2_CH1IN; #endif -#if defined(GPIO_TIM2_CH2IN) - case 1: return GPIO_TIM2_CH2IN; +#ifdef GPIO_TIM2_CH2IN + case 2: return GPIO_TIM2_CH2IN; #endif -#if defined(GPIO_TIM2_CH3IN) - case 2: return GPIO_TIM2_CH3IN; +#ifdef GPIO_TIM2_CH3IN + case 3: return GPIO_TIM2_CH3IN; #endif -#if defined(GPIO_TIM2_CH4IN) - case 3: return GPIO_TIM2_CH4IN; +#ifdef GPIO_TIM2_CH4IN + case 4: return GPIO_TIM2_CH4IN; #endif } break; @@ -234,19 +233,19 @@ static inline uint32_t stm32_cap_gpio(FAR const struct stm32_cap_priv_s *priv, i switch (channel) { #ifdef GPIO_TIM3_EXT_CLK_IN - case GPIO_CLK: return GPIO_TIM3_EXT_CLK_IN; + case STM32_CAP_CHANNEL_COUNTER: return GPIO_TIM3_EXT_CLK_IN; #endif -#if defined(GPIO_TIM3_CH1IN) - case 0: return GPIO_TIM3_CH1IN; +#ifdef GPIO_TIM3_CH1IN + case 1: return GPIO_TIM3_CH1IN; #endif -#if defined(GPIO_TIM3_CH2IN) - case 1: return GPIO_TIM3_CH2IN; +#ifdef GPIO_TIM3_CH2IN + case 2: return GPIO_TIM3_CH2IN; #endif -#if defined(GPIO_TIM3_CH3IN) - case 2: return GPIO_TIM3_CH3IN; +#ifdef GPIO_TIM3_CH3IN + case 3: return GPIO_TIM3_CH3IN; #endif -#if defined(GPIO_TIM3_CH4IN) - case 3: return GPIO_TIM3_CH4IN; +#ifdef GPIO_TIM3_CH4IN + case 4: return GPIO_TIM3_CH4IN; #endif } break; @@ -256,19 +255,19 @@ static inline uint32_t stm32_cap_gpio(FAR const struct stm32_cap_priv_s *priv, i switch (channel) { #ifdef GPIO_TIM4_EXT_CLK_IN - case GPIO_CLK: return GPIO_TIM4_EXT_CLK_IN; + case STM32_CAP_CHANNEL_COUNTER: return GPIO_TIM4_EXT_CLK_IN; #endif -#if defined(GPIO_TIM4_CH1IN) - case 0: return GPIO_TIM4_CH1IN; +#ifdef GPIO_TIM4_CH1IN + case 1: return GPIO_TIM4_CH1IN; #endif -#if defined(GPIO_TIM4_CH2IN) - case 1: return GPIO_TIM4_CH2IN; +#ifdef GPIO_TIM4_CH2IN + case 2: return GPIO_TIM4_CH2IN; #endif -#if defined(GPIO_TIM4_CH3IN) - case 2: return GPIO_TIM4_CH3IN; +#ifdef GPIO_TIM4_CH3IN + case 3: return GPIO_TIM4_CH3IN; #endif -#if defined(GPIO_TIM4_CH4IN) - case 3: return GPIO_TIM4_CH4IN; +#ifdef GPIO_TIM4_CH4IN + case 4: return GPIO_TIM4_CH4IN; #endif } break; @@ -278,19 +277,19 @@ static inline uint32_t stm32_cap_gpio(FAR const struct stm32_cap_priv_s *priv, i switch (channel) { #ifdef GPIO_TIM5_EXT_CLK_IN - case GPIO_CLK: return GPIO_TIM5_EXT_CLK_IN; + case STM32_CAP_CHANNEL_COUNTER: return GPIO_TIM5_EXT_CLK_IN; #endif -#if defined(GPIO_TIM5_CH1IN) - case 0: return GPIO_TIM5_CH1IN; +#ifdef GPIO_TIM5_CH1IN + case 1: return GPIO_TIM5_CH1IN; #endif -#if defined(GPIO_TIM5_CH2IN) - case 1: return GPIO_TIM5_CH2IN; +#ifdef GPIO_TIM5_CH2IN + case 2: return GPIO_TIM5_CH2IN; #endif -#if defined(GPIO_TIM5_CH3IN) - case 2: return GPIO_TIM5_CH3IN; +#ifdef GPIO_TIM5_CH3IN + case 3: return GPIO_TIM5_CH3IN; #endif -#if defined(GPIO_TIM5_CH4IN) - case 3: return GPIO_TIM5_CH4IN; +#ifdef GPIO_TIM5_CH4IN + case 4: return GPIO_TIM5_CH4IN; #endif } break; @@ -301,19 +300,19 @@ static inline uint32_t stm32_cap_gpio(FAR const struct stm32_cap_priv_s *priv, i switch (channel) { #ifdef GPIO_TIM8_EXT_CLK_IN - case GPIO_CLK: return GPIO_TIM8_EXT_CLK_IN; + case STM32_CAP_CHANNEL_COUNTER: return GPIO_TIM8_EXT_CLK_IN; #endif -#if defined(GPIO_TIM8_CH1IN) - case 0: return GPIO_TIM8_CH1OUIN ; +#ifdef GPIO_TIM8_CH1IN + case 1: return GPIO_TIM8_CH1IN ; #endif -#if defined(GPIO_TIM8_CH2IN) - case 1: return GPIO_TIM8_CH2OUIN ; +#ifdef GPIO_TIM8_CH2IN + case 2: return GPIO_TIM8_CH2IN ; #endif -#if defined(GPIO_TIM8_CH3IN) - case 2: return GPIO_TIM8_CH3OUIN ; +#ifdef GPIO_TIM8_CH3IN + case 3: return GPIO_TIM8_CH3IN ; #endif -#if defined(GPIO_TIM8_CH4IN) - case 3: return GPIO_TIM8_CH4OUIN ; +#ifdef GPIO_TIM8_CH4IN + case 4: return GPIO_TIM8_CH4IN ; #endif } break; @@ -553,16 +552,16 @@ static void stm32_cap_enableint(FAR struct stm32_cap_dev_s *dev, uint16_t mask = 0; ASSERT(dev); - if (src & STM32_CAP_FLAG_IRG_COUNTER) + if (src & STM32_CAP_FLAG_IRQ_COUNTER) mask |= ATIM_DIER_UIE; if (src & STM32_CAP_FLAG_IRQ_CH_1) mask |= ATIM_DIER_CC1IE; if (src & STM32_CAP_FLAG_IRQ_CH_2) - mask |= ATIM_DIER_CC1IE; + mask |= ATIM_DIER_CC2IE; if (src & STM32_CAP_FLAG_IRQ_CH_3) - mask |= ATIM_DIER_CC1IE; + mask |= ATIM_DIER_CC3IE; if (src & STM32_CAP_FLAG_IRQ_CH_4) - mask |= ATIM_DIER_CC1IE; + mask |= ATIM_DIER_CC4IE; /* Not IRQ on channel overflow */ @@ -578,7 +577,7 @@ static void stm32_cap_ackflags(FAR struct stm32_cap_dev_s *dev, int flags) const struct stm32_cap_priv_s *priv = (const struct stm32_cap_priv_s *)dev; uint16_t mask = 0; - if (flags & STM32_CAP_FLAG_IRG_COUNTER) + if (flags & STM32_CAP_FLAG_IRQ_COUNTER) mask |= ATIM_SR_UIF; if (flags & STM32_CAP_FLAG_IRQ_CH_1) @@ -612,7 +611,7 @@ static stm32_cap_flags_t stm32_cap_getflags(FAR struct stm32_cap_dev_s *dev) regval = stm32_getreg16(priv, STM32_BTIM_SR_OFFSET); if (regval & ATIM_SR_UIF) - flags |= STM32_CAP_FLAG_IRG_COUNTER; + flags |= STM32_CAP_FLAG_IRQ_COUNTER; if (regval & ATIM_SR_CC1IF) flags |= STM32_CAP_FLAG_IRQ_CH_1; @@ -647,6 +646,7 @@ static int stm32_cap_setchannel(FAR struct stm32_cap_dev_s *dev, uint8_t channel uint32_t gpio = 0; uint16_t mask; uint16_t regval; + uint16_t ccer_en_bit; ASSERT(dev); @@ -655,31 +655,47 @@ static int stm32_cap_setchannel(FAR struct stm32_cap_dev_s *dev, uint8_t channel if ( gpio == 0 ) return ERROR; + if ((cfg & STM32_CAP_MAPPED_MASK ) == 0) + return ERROR; /* MAPPED not selected */ + /* change to zero base index */ channel--; - /* Set ccer */ + /* Set ccer : + * + * GTIM_CCER_CCxE Is written latter to allow writing CCxS bits. + * + */ + switch (cfg & STM32_CAP_EDGE_MASK) { case STM32_CAP_EDGE_DISABLED: regval = 0; + ccer_en_bit = 0; break; case STM32_CAP_EDGE_RISING: - regval = GTIM_CCER_CC1E; + ccer_en_bit = GTIM_CCER_CC1E; + regval = 0; break; case STM32_CAP_EDGE_FALLING: - regval = GTIM_CCER_CC1E | GTIM_CCER_CC1P; + ccer_en_bit = GTIM_CCER_CC1E; + regval = GTIM_CCER_CC1P; break; case STM32_CAP_EDGE_BOTH: - regval = GTIM_CCER_CC1E | GTIM_CCER_CC1P | GTIM_CCER_CC1NP; + ccer_en_bit = GTIM_CCER_CC1E; + regval = GTIM_CCER_CC1P | GTIM_CCER_CC1NP; break; default: return ERROR; } + /* Shift all CCER bits to corresponding channel */ + mask = (GTIM_CCER_CC1E | GTIM_CCER_CC1P | GTIM_CCER_CC1NP); - mask <<= (channel << 2); - regval <<= (channel << 2); + mask <<= (channel << 2); + regval <<= (channel << 2); + ccer_en_bit <<= (channel << 2); + stm32_modifyreg16(priv,STM32_GTIM_CCER_OFFSET,mask,regval); /* Set ccmr */ @@ -694,6 +710,7 @@ static int stm32_cap_setchannel(FAR struct stm32_cap_dev_s *dev, uint8_t channel mask <<= 8; } + if (channel < 2) stm32_modifyreg16(priv,STM32_GTIM_CCMR1_OFFSET,mask,regval); else @@ -706,6 +723,10 @@ static int stm32_cap_setchannel(FAR struct stm32_cap_dev_s *dev, uint8_t channel else stm32_configgpio(gpio); + /* Enable this channel timer */ + + stm32_modifyreg16(priv, STM32_GTIM_CCER_OFFSET, 0, ccer_en_bit); + return OK; } @@ -713,29 +734,42 @@ static int stm32_cap_setchannel(FAR struct stm32_cap_dev_s *dev, uint8_t channel static int stm32_cap_getcapture(FAR struct stm32_cap_dev_s *dev, uint8_t channel) { const struct stm32_cap_priv_s *priv = (const struct stm32_cap_priv_s *)dev; + uint32_t offset; ASSERT(dev); switch (channel) { + case STM32_CAP_CHANNEL_COUNTER: + offset = STM32_GTIM_CNT_OFFSET; #ifdef HAVE_CH1IN case 1: - return stm32_getreg32(priv, STM32_GTIM_CCR1_OFFSET); + offset = STM32_GTIM_CCR1_OFFSET; + break; #endif -#ifdef HAVE_CH1IN +#ifdef HAVE_CH2IN case 2: - return stm32_getreg32(priv, STM32_GTIM_CCR2_OFFSET); + offset = STM32_GTIM_CCR2_OFFSET; + break; #endif -#ifdef HAVE_CH1IN +#ifdef HAVE_CH3IN case 3: - return stm32_getreg32(priv, STM32_GTIM_CCR3_OFFSET); + offset = STM32_GTIM_CCR3_OFFSET; + break; #endif -#ifdef HAVE_CH1IN +#ifdef HAVE_CH4IN case 4: - return stm32_getreg32(priv, STM32_GTIM_CCR4_OFFSET); + offset = STM32_GTIM_CCR4_OFFSET; + break; #endif + default: + return ERROR; } - return ERROR; + if ((priv->base == STM32_TIM2_BASE)||(priv->base == STM32_TIM2_BASE)) + return stm32_getreg32(priv,offset); + + return stm32_getreg16(priv,offset); + } @@ -832,7 +866,7 @@ FAR struct stm32_cap_dev_s *stm32_cap_init(int timer) stm32_cap_set_rcc(priv,true); - gpio = stm32_cap_gpio(priv,GPIO_CLK); + gpio = stm32_cap_gpio(priv,STM32_CAP_CHANNEL_COUNTER); if (gpio) stm32_configgpio(gpio); @@ -853,7 +887,7 @@ int stm32_cap_deinit(FAR struct stm32_cap_dev_s * dev) // disable timer while is not configured stm32_modifyreg16(priv, STM32_BTIM_CR1_OFFSET, ATIM_CR1_CEN, 0); - gpio = stm32_cap_gpio(priv,GPIO_CLK); + gpio = stm32_cap_gpio(priv,STM32_CAP_CHANNEL_COUNTER); if (gpio) stm32_unconfiggpio(gpio); diff --git a/arch/arm/src/stm32/stm32_capture.h b/arch/arm/src/stm32/stm32_capture.h index d37c27ca310..8b857c53a76 100644 --- a/arch/arm/src/stm32/stm32_capture.h +++ b/arch/arm/src/stm32/stm32_capture.h @@ -149,7 +149,7 @@ typedef enum { /* One of the following */ - STM32_CAP_FLAG_IRG_COUNTER = (GTIM_SR_UIF), + STM32_CAP_FLAG_IRQ_COUNTER = (GTIM_SR_UIF), STM32_CAP_FLAG_IRQ_CH_1 = (GTIM_SR_CC1IF), STM32_CAP_FLAG_IRQ_CH_2 = (GTIM_SR_CC2IF), @@ -165,6 +165,7 @@ typedef enum #define STM32_CAP_FLAG_IRQ_CH(ch) (GTIM_SR_CC1IF<<((ch)-1)) #define STM32_CAP_FLAG_OF_CH(ch) (GTIM_SR_CC1OF<<((ch)-1)) +#define STM32_CAP_CHANNEL_COUNTER 0 /* Capture Operations */ From e22d8aea5d550e781ed7b9d1caf54f30fb96fe2e Mon Sep 17 00:00:00 2001 From: pnb Date: Thu, 24 Dec 2015 09:42:33 +0100 Subject: [PATCH 14/16] add oter timer --- arch/arm/src/stm32/stm32_capture.c | 280 ++++++++++++++++++++++++++++- 1 file changed, 277 insertions(+), 3 deletions(-) diff --git a/arch/arm/src/stm32/stm32_capture.c b/arch/arm/src/stm32/stm32_capture.c index b951e01b462..9f476b5dba9 100644 --- a/arch/arm/src/stm32/stm32_capture.c +++ b/arch/arm/src/stm32/stm32_capture.c @@ -295,6 +295,8 @@ static inline uint32_t stm32_cap_gpio(FAR const struct stm32_cap_priv_s *priv, i break; #endif +/* TIM6 and TIM7 cannot be used in capture */ + #ifdef CONFIG_STM32_TIM8 case STM32_TIM8_BASE: switch (channel) @@ -317,6 +319,144 @@ static inline uint32_t stm32_cap_gpio(FAR const struct stm32_cap_priv_s *priv, i } break; #endif + +#ifdef CONFIG_STM32_TIM9 + case STM32_TIM9_BASE: + switch (channel) + { +#ifdef GPIO_TIM9_EXT_CLK_IN + case STM32_CAP_CHANNEL_COUNTER: return GPIO_TIM9_EXT_CLK_IN; +#endif +#ifdef GPIO_TIM9_CH1IN + case 1: return GPIO_TIM9_CH1IN ; +#endif +#ifdef GPIO_TIM9_CH2IN + case 2: return GPIO_TIM9_CH2IN ; +#endif +#ifdef GPIO_TIM9_CH3IN + case 3: return GPIO_TIM9_CH3IN ; +#endif +#ifdef GPIO_TIM9_CH4IN + case 4: return GPIO_TIM9_CH4IN ; +#endif + } + break; +#endif + +#ifdef CONFIG_STM32_TIM10 + case STM32_TIM10_BASE: + switch (channel) + { +#ifdef GPIO_TIM10_EXT_CLK_IN + case STM32_CAP_CHANNEL_COUNTER: return GPIO_TIM10_EXT_CLK_IN; +#endif +#ifdef GPIO_TIM10_CH1IN + case 1: return GPIO_TIM10_CH1IN ; +#endif +#ifdef GPIO_TIM10_CH2IN + case 2: return GPIO_TIM10_CH2IN ; +#endif +#ifdef GPIO_TIM10_CH4IN + case 3: return GPIO_TIM10_CH4IN ; +#endif +#ifdef GPIO_TIM10_CH5IN + case 4: return GPIO_TIM10_CH5IN ; +#endif + } + break; +#endif + +#ifdef CONFIG_STM32_TIM11 + case STM32_TIM11_BASE: + switch (channel) + { +#ifdef GPIO_TIM11_EXT_CLK_IN + case STM32_CAP_CHANNEL_COUNTER: return GPIO_TIM11_EXT_CLK_IN; +#endif +#ifdef GPIO_TIM11_CH1IN + case 1: return GPIO_TIM11_CH1IN ; +#endif +#ifdef GPIO_TIM11_CH2IN + case 2: return GPIO_TIM11_CH2IN ; +#endif +#ifdef GPIO_TIM11_CH4IN + case 3: return GPIO_TIM11_CH4IN ; +#endif +#ifdef GPIO_TIM11_CH5IN + case 4: return GPIO_TIM11_CH5IN ; +#endif + } + break; +#endif + +#ifdef CONFIG_STM32_TIM12 + case STM32_TIM12_BASE: + switch (channel) + { +#ifdef GPIO_TIM12_EXT_CLK_IN + case STM32_CAP_CHANNEL_COUNTER: return GPIO_TIM12_EXT_CLK_IN; +#endif +#ifdef GPIO_TIM12_CH1IN + case 1: return GPIO_TIM12_CH1IN ; +#endif +#ifdef GPIO_TIM12_CH2IN + case 2: return GPIO_TIM12_CH2IN ; +#endif +#ifdef GPIO_TIM12_CH4IN + case 3: return GPIO_TIM12_CH4IN ; +#endif +#ifdef GPIO_TIM12_CH5IN + case 4: return GPIO_TIM12_CH5IN ; +#endif + } + break; +#endif + +#ifdef CONFIG_STM32_TIM13 + case STM32_TIM13_BASE: + switch (channel) + { +#ifdef GPIO_TIM13_EXT_CLK_IN + case STM32_CAP_CHANNEL_COUNTER: return GPIO_TIM13_EXT_CLK_IN; +#endif +#ifdef GPIO_TIM13_CH1IN + case 1: return GPIO_TIM13_CH1IN ; +#endif +#ifdef GPIO_TIM13_CH2IN + case 2: return GPIO_TIM13_CH2IN ; +#endif +#ifdef GPIO_TIM13_CH4IN + case 3: return GPIO_TIM13_CH4IN ; +#endif +#ifdef GPIO_TIM13_CH5IN + case 4: return GPIO_TIM13_CH5IN ; +#endif + } + break; +#endif + +#ifdef CONFIG_STM32_TIM14 + case STM32_TIM14_BASE: + switch (channel) + { +#ifdef GPIO_TIM14_EXT_CLK_IN + case STM32_CAP_CHANNEL_COUNTER: return GPIO_TIM14_EXT_CLK_IN; +#endif +#ifdef GPIO_TIM14_CH1IN + case 1: return GPIO_TIM14_CH1IN ; +#endif +#ifdef GPIO_TIM14_CH2IN + case 2: return GPIO_TIM14_CH2IN ; +#endif +#ifdef GPIO_TIM14_CH4IN + case 3: return GPIO_TIM14_CH4IN ; +#endif +#ifdef GPIO_TIM14_CH5IN + case 4: return GPIO_TIM14_CH5IN ; +#endif + } + break; +#endif } return 0; } @@ -359,7 +499,7 @@ static inline int stm32_cap_set_rcc(FAR const struct stm32_cap_priv_s *priv, mask = RCC_APB1ENR_TIM5EN; break; #endif -/* TIM6 and TIM7 cannot be used in cap */ +/* TIM6 and TIM7 cannot be used in capture */ #ifdef CONFIG_STM32_TIM8_CAP case 8: offset = STM32_RCC_APB2ENR; @@ -794,6 +934,18 @@ struct stm32_cap_ops_s stm32_cap_ops = .getflags = &stm32_cap_getflags }; +#ifdef CONFIG_STM32_TIM1_CAP +const struct stm32_cap_priv_s stm32_tim1_priv = +{ + .ops = &stm32_cap_ops, + .base = STM32_TIM1_BASE, + .irq = STM32_IRQ_TIM1, +#ifdef USE_ADVENCED_TIM + .irg_of = STM32_IRQ_TIM1UP, +#endif +}; +#endif + #ifdef CONFIG_STM32_TIM2_CAP const struct stm32_cap_priv_s stm32_tim2_priv = { @@ -801,7 +953,129 @@ const struct stm32_cap_priv_s stm32_tim2_priv = .base = STM32_TIM2_BASE, .irq = STM32_IRQ_TIM2, #ifdef USE_ADVENCED_TIM - .irg_of = -1, + .irg_of = 0, +#endif +}; +#endif + +#ifdef CONFIG_STM32_TIM3_CAP +const struct stm32_cap_priv_s stm32_tim3_priv = +{ + .ops = &stm32_cap_ops, + .base = STM32_TIM3_BASE, + .irq = STM32_IRQ_TIM3, +#ifdef USE_ADVENCED_TIM + .irg_of = 0, +#endif +}; +#endif + +#ifdef CONFIG_STM32_TIM4_CAP +const struct stm32_cap_priv_s stm32_tim4_priv = +{ + .ops = &stm32_cap_ops, + .base = STM32_TIM4_BASE, + .irq = STM32_IRQ_TIM4, +#ifdef USE_ADVENCED_TIM + .irg_of = 0, +#endif +}; +#endif + +#ifdef CONFIG_STM32_TIM5_CAP +const struct stm32_cap_priv_s stm32_tim5_priv = +{ + .ops = &stm32_cap_ops, + .base = STM32_TIM5_BASE, + .irq = STM32_IRQ_TIM5, +#ifdef USE_ADVENCED_TIM + .irg_of = 0, +#endif +}; +#endif + +/* TIM6 and TIM7 cannot be used in capture */ + +#ifdef CONFIG_STM32_TIM8_CAP +const struct stm32_cap_priv_s stm32_tim8_priv = +{ + .ops = &stm32_cap_ops, + .base = STM32_TIM8_BASE, + .irq = STM32_IRQ_TIM8, +#ifdef USE_ADVENCED_TIM + .irg_of = STM32_IRQ_TIM8UP, +#endif +}; +#endif + +#ifdef CONFIG_STM32_TIM9_CAP +const struct stm32_cap_priv_s stm32_tim9_priv = +{ + .ops = &stm32_cap_ops, + .base = STM32_TIM9_BASE, + .irq = STM32_IRQ_TIM9, +#ifdef USE_ADVENCED_TIM + .irg_of = 0, +#endif +}; +#endif + +#ifdef CONFIG_STM32_TIM10_CAP +const struct stm32_cap_priv_s stm32_tim10_priv = +{ + .ops = &stm32_cap_ops, + .base = STM32_TIM10_BASE, + .irq = STM32_IRQ_TIM10, +#ifdef USE_ADVENCED_TIM + .irg_of = 0, +#endif +}; +#endif + +#ifdef CONFIG_STM32_TIM11_CAP +const struct stm32_cap_priv_s stm32_tim11_priv = +{ + .ops = &stm32_cap_ops, + .base = STM32_TIM11_BASE, + .irq = STM32_IRQ_TIM11, +#ifdef USE_ADVENCED_TIM + .irg_of = 0, +#endif +}; +#endif + +#ifdef CONFIG_STM32_TIM12_CAP +const struct stm32_cap_priv_s stm32_tim12_priv = +{ + .ops = &stm32_cap_ops, + .base = STM32_TIM12_BASE, + .irq = STM32_IRQ_TIM12, +#ifdef USE_ADVENCED_TIM + .irg_of = 0, +#endif +}; +#endif + +#ifdef CONFIG_STM32_TIM13_CAP +const struct stm32_cap_priv_s stm32_tim13_priv = +{ + .ops = &stm32_cap_ops, + .base = STM32_TIM13_BASE, + .irq = STM32_IRQ_TIM13, +#ifdef USE_ADVENCED_TIM + .irg_of = 0, +#endif +}; +#endif + +#ifdef CONFIG_STM32_TIM14_CAP +const struct stm32_cap_priv_s stm32_tim14_priv = +{ + .ops = &stm32_cap_ops, + .base = STM32_TIM14_BASE, + .irq = STM32_IRQ_TIM14, +#ifdef USE_ADVENCED_TIM + .irg_of = 0, #endif }; #endif @@ -896,4 +1170,4 @@ int stm32_cap_deinit(FAR struct stm32_cap_dev_s * dev) return OK; } -#endif /* defined(CONFIG_STM32_TIM1 || ... || TIM8) */ +#endif /* defined(CONFIG_STM32_TIM1 || ... || TIM14) */ From 709c257a43f72302acb6701e2bb62d02ef151363 Mon Sep 17 00:00:00 2001 From: pnb Date: Thu, 24 Dec 2015 09:45:02 +0100 Subject: [PATCH 15/16] cosmetic --- arch/arm/src/stm32/Kconfig | 24 ++++++++++++------------ arch/arm/src/stm32/Make.defs | 1 - 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/arch/arm/src/stm32/Kconfig b/arch/arm/src/stm32/Kconfig index 2a524e4b0f5..d5b106fc942 100644 --- a/arch/arm/src/stm32/Kconfig +++ b/arch/arm/src/stm32/Kconfig @@ -5130,7 +5130,7 @@ config STM32_TIM1_CAP default n depends on STM32_HAVE_TIM1 ---help--- - Reserve timer 1 for use by Capture + Reserve timer 1 for use by Capture Timer devices may be used for different purposes. One special purpose is to capture input. @@ -5140,7 +5140,7 @@ config STM32_TIM2_CAP default n depends on STM32_HAVE_TIM2 ---help--- - Reserve timer 2 for use by Capture + Reserve timer 2 for use by Capture Timer devices may be used for different purposes. One special purpose is to capture input. @@ -5150,7 +5150,7 @@ config STM32_TIM3_CAP default n depends on STM32_HAVE_TIM3 ---help--- - Reserve timer 3 for use by Capture + Reserve timer 3 for use by Capture Timer devices may be used for different purposes. One special purpose is to capture input. @@ -5160,7 +5160,7 @@ config STM32_TIM4_CAP default n depends on STM32_HAVE_TIM4 ---help--- - Reserve timer 4 for use by Capture + Reserve timer 4 for use by Capture Timer devices may be used for different purposes. One special purpose is to capture input. @@ -5170,7 +5170,7 @@ config STM32_TIM5_CAP default n depends on STM32_HAVE_TIM5 ---help--- - Reserve timer 5 for use by Capture + Reserve timer 5 for use by Capture Timer devices may be used for different purposes. One special purpose is to capture input. @@ -5180,7 +5180,7 @@ config STM32_TIM8_CAP default n depends on STM32_HAVE_TIM8 ---help--- - Reserve timer 8 for use by Capture + Reserve timer 8 for use by Capture Timer devices may be used for different purposes. One special purpose is to capture input. @@ -5190,7 +5190,7 @@ config STM32_TIM9_CAP default n depends on STM32_HAVE_TIM9 ---help--- - Reserve timer 9 for use by Capture + Reserve timer 9 for use by Capture Timer devices may be used for different purposes. One special purpose is to capture input. @@ -5200,7 +5200,7 @@ config STM32_TIM10_CAP default n depends on STM32_HAVE_TIM10 ---help--- - Reserve timer 10 for use by Capture + Reserve timer 10 for use by Capture Timer devices may be used for different purposes. One special purpose is to capture input. @@ -5210,7 +5210,7 @@ config STM32_TIM11_CAP default n depends on STM32_HAVE_TIM11 ---help--- - Reserve timer 11 for use by Capture + Reserve timer 11 for use by Capture Timer devices may be used for different purposes. One special purpose is to capture input. @@ -5220,7 +5220,7 @@ config STM32_TIM12_CAP default n depends on STM32_HAVE_TIM12 ---help--- - Reserve timer 12 for use by Capture + Reserve timer 12 for use by Capture Timer devices may be used for different purposes. One special purpose is to capture input. @@ -5230,7 +5230,7 @@ config STM32_TIM13_CAP default n depends on STM32_HAVE_TIM13 ---help--- - Reserve timer 13 for use by Capture + Reserve timer 13 for use by Capture Timer devices may be used for different purposes. One special purpose is to capture input. @@ -5240,7 +5240,7 @@ config STM32_TIM14_CAP default n depends on STM32_HAVE_TIM14 ---help--- - Reserve timer 14 for use by Capture + Reserve timer 14 for use by Capture Timer devices may be used for different purposes. One special purpose is to capture input. diff --git a/arch/arm/src/stm32/Make.defs b/arch/arm/src/stm32/Make.defs index f3994dd2bd0..f0ef9c22b0e 100644 --- a/arch/arm/src/stm32/Make.defs +++ b/arch/arm/src/stm32/Make.defs @@ -231,7 +231,6 @@ ifeq ($(CONFIG_QENCODER),y) CHIP_CSRCS += stm32_qencoder.c endif - ifeq ($(CONFIG_CAN),y) CHIP_CSRCS += stm32_can.c endif From 400d06e9ecfc0d41fe68cf07c98867e5a7dc5121 Mon Sep 17 00:00:00 2001 From: pnb Date: Thu, 24 Dec 2015 12:26:39 +0100 Subject: [PATCH 16/16] fix capture bug --- arch/arm/src/stm32/stm32_capture.c | 3 ++- arch/arm/src/stm32/stm32_capture.h | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/arch/arm/src/stm32/stm32_capture.c b/arch/arm/src/stm32/stm32_capture.c index 9f476b5dba9..c4229f0f538 100644 --- a/arch/arm/src/stm32/stm32_capture.c +++ b/arch/arm/src/stm32/stm32_capture.c @@ -871,7 +871,7 @@ static int stm32_cap_setchannel(FAR struct stm32_cap_dev_s *dev, uint8_t channel } -static int stm32_cap_getcapture(FAR struct stm32_cap_dev_s *dev, uint8_t channel) +static uint32_t stm32_cap_getcapture(FAR struct stm32_cap_dev_s *dev, uint8_t channel) { const struct stm32_cap_priv_s *priv = (const struct stm32_cap_priv_s *)dev; uint32_t offset; @@ -881,6 +881,7 @@ static int stm32_cap_getcapture(FAR struct stm32_cap_dev_s *dev, uint8_t channel { case STM32_CAP_CHANNEL_COUNTER: offset = STM32_GTIM_CNT_OFFSET; + break; #ifdef HAVE_CH1IN case 1: offset = STM32_GTIM_CCR1_OFFSET; diff --git a/arch/arm/src/stm32/stm32_capture.h b/arch/arm/src/stm32/stm32_capture.h index 8b857c53a76..a9017719857 100644 --- a/arch/arm/src/stm32/stm32_capture.h +++ b/arch/arm/src/stm32/stm32_capture.h @@ -173,7 +173,7 @@ struct stm32_cap_ops_s { int (*setclock)( FAR struct stm32_cap_dev_s *dev, stm32_cap_clk_t clk, uint32_t prescaler, uint32_t max); int (*setchannel)(FAR struct stm32_cap_dev_s *dev, uint8_t channel, stm32_cap_ch_cfg_t cfg); - int (*getcapture)(FAR struct stm32_cap_dev_s *dev, uint8_t channel); + uint32_t (*getcapture)(FAR struct stm32_cap_dev_s *dev, uint8_t channel); int (*setisr)( FAR struct stm32_cap_dev_s *dev, xcpt_t handler); void (*enableint)( FAR struct stm32_cap_dev_s *dev, stm32_cap_flags_t src, bool on ); void (*ackflags)( FAR struct stm32_cap_dev_s *dev, int flags);