diff options
| author | Lucian Copeland <hierophect@gmail.com> | 2020-07-16 15:45:18 -0400 |
|---|---|---|
| committer | Lucian Copeland <hierophect@gmail.com> | 2020-07-16 15:45:18 -0400 |
| commit | 6a49766df04ba23d2c5f0bdb24f826ff1355340d (patch) | |
| tree | 796c69eb0c7ccf6ce2670f1af66a541a6587ed32 | |
| parent | cdfc3a2d37771b67f82ab810ebfefcbb9cd6a14e (diff) | |
Fix pulseIO reset and frequency issues, remove IRQ conflict
| -rw-r--r-- | ports/stm/common-hal/pulseio/PulseIn.c | 13 | ||||
| -rw-r--r-- | ports/stm/common-hal/pulseio/PulseOut.c | 1 | ||||
| -rw-r--r-- | ports/stm/peripherals/timers.c | 2 |
3 files changed, 9 insertions, 7 deletions
diff --git a/ports/stm/common-hal/pulseio/PulseIn.c b/ports/stm/common-hal/pulseio/PulseIn.c index 478077eda..db01968c4 100644 --- a/ports/stm/common-hal/pulseio/PulseIn.c +++ b/ports/stm/common-hal/pulseio/PulseIn.c @@ -102,7 +102,9 @@ void pulsein_reset(void) { } memset(_objs, 0, sizeof(_objs)); + HAL_TIM_Base_DeInit(&tim_handle); tim_clock_disable(stm_peripherals_timer_get_index(tim_handle.Instance)); + memset(&tim_handle, 0, sizeof(tim_handle)); refcount = 0; } @@ -133,21 +135,22 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu self->last_count = 0; self->last_overflow = 0; - if (HAL_TIM_Base_GetState(&tim_handle) == HAL_TIM_STATE_RESET) { //TODO: there's no state yet + if (HAL_TIM_Base_GetState(&tim_handle) == HAL_TIM_STATE_RESET) { // Find a suitable timer TIM_TypeDef * tim_instance = stm_peripherals_find_timer(); stm_peripherals_timer_reserve(tim_instance); - // Calculate a 1ms period + // Set ticks to 1us uint32_t source = stm_peripherals_timer_get_source_freq(tim_instance); - uint32_t prescaler = source/1000000; // 1us intervals + uint32_t prescaler = source/1000000; + // Enable clocks and IRQ, set callback stm_peripherals_timer_preinit(tim_instance, 4, pulsein_timer_event_handler); // Set the new period tim_handle.Instance = tim_instance; - tim_handle.Init.Prescaler = prescaler; // divide down to 1mhz - tim_handle.Init.Period = 0xffff; + tim_handle.Init.Prescaler = prescaler - 1; + tim_handle.Init.Period = 0x10000 - 1; //65 ms period (maximum) HAL_TIM_Base_Init(&tim_handle); // Set registers manually diff --git a/ports/stm/common-hal/pulseio/PulseOut.c b/ports/stm/common-hal/pulseio/PulseOut.c index ba817ae26..865b41dcf 100644 --- a/ports/stm/common-hal/pulseio/PulseOut.c +++ b/ports/stm/common-hal/pulseio/PulseOut.c @@ -73,7 +73,6 @@ STATIC void start_timer(void) { tim_handle.Instance->CR1 |= TIM_CR1_CEN; // Resume timer tim_handle.Instance->CR1 |= TIM_CR1_URS; // Disable non-overflow interrupts __HAL_TIM_ENABLE_IT(&tim_handle, TIM_IT_UPDATE); - } STATIC void pulseout_event_handler(void) { diff --git a/ports/stm/peripherals/timers.c b/ports/stm/peripherals/timers.c index 54b1fd18a..aa189da84 100644 --- a/ports/stm/peripherals/timers.c +++ b/ports/stm/peripherals/timers.c @@ -67,7 +67,7 @@ static size_t irq_map[] = { NULL_IRQ, #endif #ifdef TIM6 - #if !defined(DAC_BASE) || !defined(DAC1_BASE) + #if !defined(DAC_BASE) && !defined(DAC1_BASE) TIM6_IRQn, #else TIM6_DAC_IRQn, |
