From 2d451991af7b3a0b1f3b2e683bfd53d1c94e1ceb Mon Sep 17 00:00:00 2001 From: David Sidrane Date: Tue, 1 Nov 2016 10:30:22 -1000 Subject: [PATCH] PX4 System change to Remove #ifdefs from the IO timers Update the comment, to explain how to achive a different perescale value. Added PX4_IO_TIMER_ALTERNATE_RATE one board agnostic ifdef PX4_IO_TIMER_ALTERNATE_RATE that is non board specific. N.B. I would like to eliminate PX4_IO_TIMER_ALTERNATE_RATE as well, but I need crazyflie HW to validate that the startup script can just set the rate on the PWM to 3921 fast enough to not effect the motors. --- src/drivers/stm32/drv_io_timer.c | 21 +++++++++++---------- src/drivers/stm32/drv_led_pwm.cpp | 4 ++-- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/drivers/stm32/drv_io_timer.c b/src/drivers/stm32/drv_io_timer.c index 16b662a3c1..1844c04f94 100644 --- a/src/drivers/stm32/drv_io_timer.c +++ b/src/drivers/stm32/drv_io_timer.c @@ -379,11 +379,15 @@ static int allocate_channel(unsigned channel, io_timer_channel_mode_t mode) static int timer_set_rate(unsigned timer, unsigned rate) { -#if defined(CONFIG_ARCH_BOARD_CRAZYFLIE) - /* configure the timer to update at 328.125 kHz (recommended) */ - rARR(timer) = 255; +#if defined(PX4_IO_TIMER_ALTERNATE_RATE) + + /* Override the rate to a constant that could be provided by the board */ + + rARR(timer) = PX4_IO_TIMER_ALTERNATE_RATE; #else + /* configure the timer to update at the desired rate */ + rARR(timer) = 1000000 / rate; #endif @@ -432,15 +436,12 @@ int io_timer_init_timer(unsigned timer) rBDTR(timer) = ATIM_BDTR_MOE; } -#if defined(CONFIG_ARCH_BOARD_CRAZYFLIE) - /* configure the timer to free-run at timer frequency */ - rPSC(timer) = 0; -#else - /* configure the timer to free-run at 1MHz */ + /* If the timer clock source provided as clock_freq is the STM32_APBx_TIMx_CLKIN + * then configure the timer to free-run at 1MHz. + * Otherwize, other frequencies are attainable by adjusting .clock_freq accordingly. + */ rPSC(timer) = (io_timers[timer].clock_freq / 1000000) - 1; -#endif - /* * Note we do the Standard PWM Out init here diff --git a/src/drivers/stm32/drv_led_pwm.cpp b/src/drivers/stm32/drv_led_pwm.cpp index 32a365e9d2..ea3dff17b2 100644 --- a/src/drivers/stm32/drv_led_pwm.cpp +++ b/src/drivers/stm32/drv_led_pwm.cpp @@ -37,8 +37,9 @@ * */ -#include +#include +#include #include #include @@ -60,7 +61,6 @@ #include -#include #if defined(BOARD_HAS_LED_PWM) #define REG(_tmr, _reg) (*(volatile uint32_t *)(led_pwm_timers[_tmr].base + _reg))