Skip to content

Commit 6a49766

Browse files
committed
Fix pulseIO reset and frequency issues, remove IRQ conflict
1 parent cdfc3a2 commit 6a49766

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

ports/stm/common-hal/pulseio/PulseIn.c

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ void pulsein_reset(void) {
102102
}
103103
memset(_objs, 0, sizeof(_objs));
104104

105+
HAL_TIM_Base_DeInit(&tim_handle);
105106
tim_clock_disable(stm_peripherals_timer_get_index(tim_handle.Instance));
107+
memset(&tim_handle, 0, sizeof(tim_handle));
106108
refcount = 0;
107109
}
108110

@@ -133,21 +135,22 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu
133135
self->last_count = 0;
134136
self->last_overflow = 0;
135137

136-
if (HAL_TIM_Base_GetState(&tim_handle) == HAL_TIM_STATE_RESET) { //TODO: there's no state yet
138+
if (HAL_TIM_Base_GetState(&tim_handle) == HAL_TIM_STATE_RESET) {
137139
// Find a suitable timer
138140
TIM_TypeDef * tim_instance = stm_peripherals_find_timer();
139141
stm_peripherals_timer_reserve(tim_instance);
140142

141-
// Calculate a 1ms period
143+
// Set ticks to 1us
142144
uint32_t source = stm_peripherals_timer_get_source_freq(tim_instance);
143-
uint32_t prescaler = source/1000000; // 1us intervals
145+
uint32_t prescaler = source/1000000;
144146

147+
// Enable clocks and IRQ, set callback
145148
stm_peripherals_timer_preinit(tim_instance, 4, pulsein_timer_event_handler);
146149

147150
// Set the new period
148151
tim_handle.Instance = tim_instance;
149-
tim_handle.Init.Prescaler = prescaler; // divide down to 1mhz
150-
tim_handle.Init.Period = 0xffff;
152+
tim_handle.Init.Prescaler = prescaler - 1;
153+
tim_handle.Init.Period = 0x10000 - 1; //65 ms period (maximum)
151154
HAL_TIM_Base_Init(&tim_handle);
152155

153156
// Set registers manually

ports/stm/common-hal/pulseio/PulseOut.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ STATIC void start_timer(void) {
7373
tim_handle.Instance->CR1 |= TIM_CR1_CEN; // Resume timer
7474
tim_handle.Instance->CR1 |= TIM_CR1_URS; // Disable non-overflow interrupts
7575
__HAL_TIM_ENABLE_IT(&tim_handle, TIM_IT_UPDATE);
76-
7776
}
7877

7978
STATIC void pulseout_event_handler(void) {

ports/stm/peripherals/timers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static size_t irq_map[] = {
6767
NULL_IRQ,
6868
#endif
6969
#ifdef TIM6
70-
#if !defined(DAC_BASE) || !defined(DAC1_BASE)
70+
#if !defined(DAC_BASE) && !defined(DAC1_BASE)
7171
TIM6_IRQn,
7272
#else
7373
TIM6_DAC_IRQn,

0 commit comments

Comments
 (0)