summaryrefslogtreecommitdiff
path: root/ports/stm/common-hal/pulseio/PulseIn.c
blob: a667cf01020a239bc74b0d1b49502edd3ecccbae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * This file is part of the MicroPython project, http://micropython.org/
 *
 * The MIT License (MIT)
 *
 * Copyright (c) 2019 Lucian Copeland for Adafruit Industries
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "common-hal/pulseio/PulseIn.h"
#include <stdint.h>
#include <string.h>
#include "py/mpconfig.h"
#include "py/gc.h"
#include "py/runtime.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"
#include "shared-bindings/pulseio/PulseIn.h"
#include "timers.h"

#include STM32_HAL_H

#define STM32_GPIO_PORT_SIZE 16
static pulseio_pulsein_obj_t* _objs[STM32_GPIO_PORT_SIZE];

STATIC TIM_HandleTypeDef tim_handle;
static uint32_t overflow_count = 0;
STATIC uint8_t refcount = 0;

static void assign_EXTI_Interrupt(pulseio_pulsein_obj_t* self, uint8_t num);

void pulsein_timer_event_handler(void)
{
    // Detect TIM Update event
    if (__HAL_TIM_GET_FLAG(&tim_handle, TIM_FLAG_UPDATE) != RESET)
    {
        if (__HAL_TIM_GET_IT_SOURCE(&tim_handle, TIM_IT_UPDATE) != RESET)
        {
            __HAL_TIM_CLEAR_IT(&tim_handle, TIM_IT_UPDATE);
            overflow_count++;
        }
    }
}

static void pulsein_exti_event_handler(uint8_t num) {
    // Grab the current time first.
    uint32_t current_overflow = overflow_count;
    uint32_t current_count = tim_handle.Instance->CNT;

    // Interrupt register must be cleared manually
    EXTI->PR = 1 << num;

    pulseio_pulsein_obj_t* self = _objs[num];
    if ( !self ) return;

    if (self->first_edge) {
        // first pulse is opposite state from idle
        bool state = HAL_GPIO_ReadPin(pin_port(self->pin->port), pin_mask(self->pin->number));
        if ( self->idle_state != state ) {
            self->first_edge = false;
        }
    } else {
        uint32_t total_diff = current_count + 0x10000 * (current_overflow - self->last_overflow) - self->last_count;
        // Cap duration at 16 bits.
        uint16_t duration = MIN(0xffff, total_diff);

        uint16_t i = (self->start + self->len) % self->maxlen;
        self->buffer[i] = duration;
        if (self->len < self->maxlen) {
            self->len++;
        } else {
            self->start++;
        }
    }

    self->last_count = current_count;
    self->last_overflow = current_overflow;
}

void pulsein_reset(void) {
    // Disable all active interrupts and clear array
    for (uint i = 0; i < STM32_GPIO_PORT_SIZE; i++) {
        if (_objs[i] != NULL) {
            HAL_NVIC_DisableIRQ(_objs[i]->irq);
        }
    }
    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;
}

void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu_pin_obj_t* pin,
                                             uint16_t maxlen, bool idle_state) {
    // STM32 has one shared EXTI for each pin number, 0-15
    uint8_t p_num = pin->number;
    if(_objs[p_num]) {
        mp_raise_ValueError(translate("Pin number already reserved by EXTI"));
    }
    _objs[p_num] = self;

    // Allocate pulse buffer
    self->buffer = (uint16_t *) m_malloc(maxlen * sizeof(uint16_t), false);
    if (self->buffer == NULL) {
        mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"),
                          maxlen * sizeof(uint16_t));
    }

    // Set internal variables
    self->pin = pin;
    self->maxlen = maxlen;
    self->idle_state = idle_state;
    self->start = 0;
    self->len = 0;
    self->first_edge = true;
    self->paused = false;
    self->last_count = 0;
    self->last_overflow = 0;

    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);

        // Set ticks to 1us
        uint32_t source = stm_peripherals_timer_get_source_freq(tim_instance);
        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 - 1;
        tim_handle.Init.Period = 0x10000 - 1; //65 ms period (maximum)
        HAL_TIM_Base_Init(&tim_handle);

        // Set registers manually
        tim_handle.Instance->SR = 0; // Prevent the SR from triggering an interrupt
        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);

        overflow_count = 0;
    }
    // Add to active PulseIns
    refcount++;

    // EXTI pins can also be read as an input
    GPIO_InitTypeDef GPIO_InitStruct = {0};
    GPIO_InitStruct.Pin = pin_mask(pin->number);
    GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(pin_port(pin->port), &GPIO_InitStruct);

    // Interrupt starts immediately
    assign_EXTI_Interrupt(self, pin->number);
    HAL_NVIC_EnableIRQ(self->irq);
    common_hal_mcu_pin_claim(pin);
}

bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {
    return (self->pin == NULL);
}

void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
    if (common_hal_pulseio_pulsein_deinited(self)) {
        return;
    }
    //Remove pulsein slot from shared array
    _objs[self->pin->number] = NULL;
    reset_pin_number(self->pin->port, self->pin->number);
    self->pin = NULL;

    refcount--;
    if (refcount == 0) {
        stm_peripherals_timer_free(tim_handle.Instance);
    }
}

void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) {
    HAL_NVIC_DisableIRQ(self->irq);
    self->paused = true;
}

void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration) {
    // Make sure we're paused.
    if ( !self->paused ) {
        common_hal_pulseio_pulsein_pause(self);
    }

    // Send the trigger pulse.
    if (trigger_duration > 0) {
        GPIO_InitTypeDef GPIO_InitStruct = {0};
        GPIO_InitStruct.Pin = pin_mask(self->pin->number);
        GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
        HAL_GPIO_Init(pin_port(self->pin->port), &GPIO_InitStruct);

        HAL_GPIO_WritePin(pin_port(self->pin->port), pin_mask(self->pin->number), !self->idle_state);
        common_hal_mcu_delay_us((uint32_t)trigger_duration);
        HAL_GPIO_WritePin(pin_port(self->pin->port), pin_mask(self->pin->number), self->idle_state);

        GPIO_InitStruct.Pin = pin_mask(self->pin->number);
        GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        HAL_GPIO_Init(pin_port(self->pin->port), &GPIO_InitStruct);
    }

    self->first_edge = true;
    self->paused = false;
    self->last_count = 0;
    self->last_overflow = 0;

    HAL_NVIC_EnableIRQ(self->irq);
}

void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) {
    HAL_NVIC_DisableIRQ(self->irq);
    self->start = 0;
    self->len = 0;
    HAL_NVIC_EnableIRQ(self->irq);
}

uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_t index) {
    HAL_NVIC_DisableIRQ(self->irq);
    if (index < 0) {
        index += self->len;
    }
    if (index < 0 || index >= self->len) {
        HAL_NVIC_EnableIRQ(self->irq);
        mp_raise_IndexError_varg(translate("%q index out of range"), MP_QSTR_PulseIn);
    }
    uint16_t value = self->buffer[(self->start + index) % self->maxlen];
    HAL_NVIC_EnableIRQ(self->irq);
    return value;
}

uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
    if (self->len == 0) {
        mp_raise_IndexError_varg(translate("pop from empty %s"), "PulseIn");
    }
    HAL_NVIC_DisableIRQ(self->irq);
    uint16_t value = self->buffer[self->start];
    self->start = (self->start + 1) % self->maxlen;
    self->len--;
    HAL_NVIC_EnableIRQ(self->irq);

    return value;
}

uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
    return self->maxlen;
}

bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t* self) {
    return self->paused;
}

uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) {
    return self->len;
}

static void assign_EXTI_Interrupt(pulseio_pulsein_obj_t* self, uint8_t num) {
    if (num == 0) {
        self->irq = EXTI0_IRQn;
    } else if (num == 1) {
        self->irq = EXTI1_IRQn;
    } else if (num == 2) {
        self->irq = EXTI2_IRQn;
    } else if (num == 3) {
        self->irq = EXTI3_IRQn;
    } else if (num == 4) {
        self->irq = EXTI4_IRQn;
    } else if (num >= 5 && num <= 9 ) {
        self->irq = EXTI9_5_IRQn;
    } else if (num >= 10 && num <= 15) {
        self->irq = EXTI15_10_IRQn;
    }
}

void EXTI0_IRQHandler(void)
{
    pulsein_exti_event_handler(0);
}
void EXTI1_IRQHandler(void)
{
    pulsein_exti_event_handler(1);
}
void EXTI2_IRQHandler(void)
{
    pulsein_exti_event_handler(2);
}
void EXTI3_IRQHandler(void)
{
    pulsein_exti_event_handler(3);
}
void EXTI4_IRQHandler(void)
{
    pulsein_exti_event_handler(4);
}

void EXTI9_5_IRQHandler(void)
{
    uint32_t pending = EXTI->PR;
    for (uint i = 5; i <= 9; i++) {
        if(pending & (1 << i)) {
            pulsein_exti_event_handler(i);
        }
    }
}

void EXTI15_10_IRQHandler(void)
{
    uint32_t pending = EXTI->PR;
    for (uint i = 10; i <= 15; i++) {
        if(pending & (1 << i)) {
            pulsein_exti_event_handler(i);
        }
    }
}