PWM Capture
Moderators: RoccoMarco, lbednarz, tfAteba
PWM Capture
Hi,
I want to capture PWM Input. Is there any example code for that? I am able to generate PWM output on F4 Discovery Board.
Sapan
I want to capture PWM Input. Is there any example code for that? I am able to generate PWM output on F4 Discovery Board.
Sapan
- Giovanni
- Site Admin
- Posts: 14892
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
Re: PWM Capture
Hi,
The ICU driver does that, see the PWM_ICU demo under ./testhal, it captures a self generated PWM.
Giovanni
The ICU driver does that, see the PWM_ICU demo under ./testhal, it captures a self generated PWM.
Giovanni
Re: PWM Capture
Hi,
I am trying to compile code for ICU. I am using PC8 TIM8_CH3 for ICU purpose. Following is initialize part
Before this I have enabled ICU in mcuconf.h and halconf.h
While trying to compile this code I am getting following errors:
Compiling ../../os/hal/platforms/STM32/i2c_lld.c
Compiling ../../os/hal/platforms/STM32/icu_lld.c
../../os/hal/platforms/STM32/icu_lld.c: In function 'icu_lld_init':
../../os/hal/platforms/STM32/icu_lld.c:279:3: error: 'ICUD5' undeclared (first use in this function)
../../os/hal/platforms/STM32/icu_lld.c:279:3: note: each undeclared identifier is reported only once for each function it appears in
../../os/ports/GCC/ARMCMx/rules.mk:152: recipe for target `build/obj/icu_lld.o' failed
make: *** [build/obj/icu_lld.o] Error 1
I am trying to compile code for ICU. I am using PC8 TIM8_CH3 for ICU purpose. Following is initialize part
Code: Select all
icuStart(&ICUD8, &icucfg);
palSetPadMode(GPIOC, 8, PAL_MODE_ALTERNATE(3));
icuEnable(&ICUD8);
Before this I have enabled ICU in mcuconf.h and halconf.h
Code: Select all
#define STM32_ICU_USE_TIM8 TRUE
Code: Select all
#if !defined(HAL_USE_ICU) || defined(__DOXYGEN__)
#define HAL_USE_ICU TRUE
#endif
While trying to compile this code I am getting following errors:
Compiling ../../os/hal/platforms/STM32/i2c_lld.c
Compiling ../../os/hal/platforms/STM32/icu_lld.c
../../os/hal/platforms/STM32/icu_lld.c: In function 'icu_lld_init':
../../os/hal/platforms/STM32/icu_lld.c:279:3: error: 'ICUD5' undeclared (first use in this function)
../../os/hal/platforms/STM32/icu_lld.c:279:3: note: each undeclared identifier is reported only once for each function it appears in
../../os/ports/GCC/ARMCMx/rules.mk:152: recipe for target `build/obj/icu_lld.o' failed
make: *** [build/obj/icu_lld.o] Error 1
- Giovanni
- Site Admin
- Posts: 14892
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
Re: PWM Capture
Got it. I have recompiled the code using ICU3. I am using PC7 for PWM capture. PWM output is available on PC8 - TIM8 CH3. PWM width is 1.6 ms and Period is 20 ms.
Reading PWM Capture value
But I am getting output as 1, 1 only....
...what have I missed?
Code: Select all
icucnt_t last_width, last_period;
static void icuwidthcb(ICUDriver *icup) {
last_width = icuGetWidthI(icup);
}
static void icuperiodcb(ICUDriver *icup) {
last_period = icuGetPeriodI(icup);
}
static ICUConfig icucfg = {
ICU_INPUT_ACTIVE_HIGH,
1000000, /* 1MHz ICU clock frequency. */
icuwidthcb,
icuperiodcb
};
Code: Select all
icuStart(&ICUD3, &icucfg);
palSetPadMode(GPIOC, 7, PAL_MODE_ALTERNATE(2));
icuEnable(&ICUD3);
Reading PWM Capture value
Code: Select all
while (TRUE) {
icuwidthcb(&ICUD3);
icuperiodcb(&ICUD3);
chprintf((BaseChannel *)&SD1,"Input Capture 3 Value is %u, %u\r\n", last_width, last_period);
chThdSleepMilliseconds(500);
}
But I am getting output as 1, 1 only....
- Giovanni
- Site Admin
- Posts: 14892
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
Re: PWM Capture
Probably because you are calling the callbacks from your loop. It is the driver supposed to call them.
Giovanni
Giovanni
Re: PWM Capture
How to do that? I am not sure about it. Can you point me to some example? That would be very helpful.
Thanks.
Thanks.
- Giovanni
- Site Admin
- Posts: 14892
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 996 times
Re: PWM Capture
The demo is in ./testhal/STM32F4xx/PWM-ICU. It uses ICUD3 and captures on PC6, you can run it on the discovery.
Giovanni
Giovanni
Re: PWM Capture
Hi Giovanni,
I am confused about example given in /testhal/STM32F4xx/PWM-ICU. You said drivers are supposed to invoke callback functions.
In example code
Here PWM output and PWM input is initialized first. Then PWM is being generated at different different duty cycles and finally before main loop PWM and ICU driver is being disabled. I want to know from where this call back is invoked. I want to output of ICU on UART1.
Sapan
I am confused about example given in /testhal/STM32F4xx/PWM-ICU. You said drivers are supposed to invoke callback functions.
In example code
Code: Select all
/*
ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
2011,2012 Giovanni Di Sirio.
This file is part of ChibiOS/RT.
ChibiOS/RT is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
ChibiOS/RT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
---
A special exception to the GPL can be applied should you wish to distribute
a combined work that includes ChibiOS/RT, without being obliged to provide
the source code for any proprietary components. See the file exception.txt
for full details of how and when the exception can be applied.
*/
#include "ch.h"
#include "hal.h"
static void pwmpcb(PWMDriver *pwmp) {
(void)pwmp;
palClearPad(GPIOD, GPIOD_LED5);
}
static void pwmc1cb(PWMDriver *pwmp) {
(void)pwmp;
palSetPad(GPIOD, GPIOD_LED5);
}
static PWMConfig pwmcfg = {
10000, /* 10KHz PWM clock frequency. */
10000, /* Initial PWM period 1S. */
pwmpcb,
{
{PWM_OUTPUT_ACTIVE_HIGH, pwmc1cb},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL},
{PWM_OUTPUT_DISABLED, NULL}
},
0,
};
icucnt_t last_width, last_period;
static void icuwidthcb(ICUDriver *icup) {
palSetPad(GPIOD, GPIOD_LED4);
last_width = icuGetWidthI(icup);
}
static void icuperiodcb(ICUDriver *icup) {
palClearPad(GPIOD, GPIOD_LED4);
last_period = icuGetPeriodI(icup);
}
static ICUConfig icucfg = {
ICU_INPUT_ACTIVE_HIGH,
10000, /* 10KHz ICU clock frequency. */
icuwidthcb,
icuperiodcb
};
/*
* Application entry point.
*/
int main(void) {
/*
* System initializations.
* - HAL initialization, this also initializes the configured device drivers
* and performs the board-specific initializations.
* - Kernel initialization, the main() function becomes a thread and the
* RTOS is active.
*/
halInit();
chSysInit();
/*
* Initializes the PWM driver 2 and ICU driver 3.
* GPIOA15 is the PWM output.
* GPIOC6 is the ICU input.
* The two pins have to be externally connected together.
*/
pwmStart(&PWMD2, &pwmcfg);
palSetPadMode(GPIOA, 15, PAL_MODE_ALTERNATE(1));
icuStart(&ICUD3, &icucfg);
palSetPadMode(GPIOC, 6, PAL_MODE_ALTERNATE(2));
icuEnable(&ICUD3);
chThdSleepMilliseconds(2000);
/*
* Starts the PWM channel 0 using 75% duty cycle.
*/
pwmEnableChannel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 7500));
chThdSleepMilliseconds(5000);
/*
* Changes the PWM channel 0 to 50% duty cycle.
*/
pwmEnableChannel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 5000));
chThdSleepMilliseconds(5000);
/*
* Changes the PWM channel 0 to 25% duty cycle.
*/
pwmEnableChannel(&PWMD2, 0, PWM_PERCENTAGE_TO_WIDTH(&PWMD2, 2500));
chThdSleepMilliseconds(5000);
/*
* Changes PWM period to half second the duty cycle becomes 50%
* implicitly.
*/
pwmChangePeriod(&PWMD2, 5000);
chThdSleepMilliseconds(5000);
/*
* Disables channel 0 and stops the drivers.
*/
pwmDisableChannel(&PWMD2, 0);
pwmStop(&PWMD2);
icuDisable(&ICUD3);
icuStop(&ICUD3);
palClearPad(GPIOD, GPIOD_LED4);
palClearPad(GPIOD, GPIOD_LED5);
/*
* Normal main() thread activity, in this demo it does nothing.
*/
while (TRUE) {
chThdSleepMilliseconds(500);
}
return 0;
}
Here PWM output and PWM input is initialized first. Then PWM is being generated at different different duty cycles and finally before main loop PWM and ICU driver is being disabled. I want to know from where this call back is invoked. I want to output of ICU on UART1.
Sapan