From 8e217b028718fb6f0196e8a42ccff026a82c3215 Mon Sep 17 00:00:00 2001 From: David Sidrane Date: Tue, 4 Apr 2017 06:06:18 -1000 Subject: [PATCH] bugfix:drv_io_timer properly initialize non-contiguous timer channels This fixes the issue with initializing channels 5,6 without channels 1-4. The code assumed all timers actions were in order to be initialized. This is not the case. This commit fixes that bad assumption by not stopping the configuration on the first action entry with a base == 0, but processing all entries with non-zero base. --- src/drivers/stm32/drv_io_timer.c | 53 +++++++++++++++++--------------- 1 file changed, 28 insertions(+), 25 deletions(-) diff --git a/src/drivers/stm32/drv_io_timer.c b/src/drivers/stm32/drv_io_timer.c index 1830ad4dd41..49f977ba0bb 100644 --- a/src/drivers/stm32/drv_io_timer.c +++ b/src/drivers/stm32/drv_io_timer.c @@ -796,39 +796,42 @@ int io_timer_set_enable(bool state, io_timer_channel_mode_t mode, io_timer_chann irqstate_t flags = px4_enter_critical_section(); - for (unsigned actions = 0; actions < arraySize(action_cache) && action_cache[actions].base != 0 ; actions++) { - uint32_t rvalue = _REG32(action_cache[actions].base, STM32_GTIM_CCER_OFFSET); - rvalue &= ~action_cache[actions].ccer_clearbits; - rvalue |= action_cache[actions].ccer_setbits; - _REG32(action_cache[actions].base, STM32_GTIM_CCER_OFFSET) = rvalue; - uint32_t after = rvalue & (GTIM_CCER_CC1E | GTIM_CCER_CC2E | GTIM_CCER_CC3E | GTIM_CCER_CC4E); - rvalue = _REG32(action_cache[actions].base, STM32_GTIM_DIER_OFFSET); - rvalue &= ~action_cache[actions].dier_clearbits; - rvalue |= action_cache[actions].dier_setbits; - _REG32(action_cache[actions].base, STM32_GTIM_DIER_OFFSET) = rvalue; + for (unsigned actions = 0; actions < arraySize(action_cache); actions++) { + if (action_cache[actions].base != 0) { + uint32_t rvalue = _REG32(action_cache[actions].base, STM32_GTIM_CCER_OFFSET); + rvalue &= ~action_cache[actions].ccer_clearbits; + rvalue |= action_cache[actions].ccer_setbits; + _REG32(action_cache[actions].base, STM32_GTIM_CCER_OFFSET) = rvalue; + uint32_t after = rvalue & (GTIM_CCER_CC1E | GTIM_CCER_CC2E | GTIM_CCER_CC3E | GTIM_CCER_CC4E); + + rvalue = _REG32(action_cache[actions].base, STM32_GTIM_DIER_OFFSET); + rvalue &= ~action_cache[actions].dier_clearbits; + rvalue |= action_cache[actions].dier_setbits; + _REG32(action_cache[actions].base, STM32_GTIM_DIER_OFFSET) = rvalue; - /* Any On ?*/ + /* Any On ?*/ - if (after != 0) { + if (after != 0) { - /* force an update to preload all registers */ - rEGR(actions) = GTIM_EGR_UG; + /* force an update to preload all registers */ + rEGR(actions) = GTIM_EGR_UG; - for (unsigned chan = 0; chan < arraySize(action_cache[actions].gpio); chan++) { - if (action_cache[actions].gpio[chan]) { - px4_arch_configgpio(action_cache[actions].gpio[chan]); - action_cache[actions].gpio[chan] = 0; + for (unsigned chan = 0; chan < arraySize(action_cache[actions].gpio); chan++) { + if (action_cache[actions].gpio[chan]) { + px4_arch_configgpio(action_cache[actions].gpio[chan]); + action_cache[actions].gpio[chan] = 0; + } } + + /* arm requires the timer be enabled */ + rCR1(actions) |= GTIM_CR1_CEN | GTIM_CR1_ARPE; + + } else { + + rCR1(actions) = 0; } - - /* arm requires the timer be enabled */ - rCR1(actions) |= GTIM_CR1_CEN | GTIM_CR1_ARPE; - - } else { - - rCR1(actions) = 0; } }