2019-10-22 11:09:46 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2019-11-03 16:33:01 -05:00
|
|
|
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
|
2019-10-22 11:09:46 -04:00
|
|
|
*
|
|
|
|
* 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/pulseio/PulseIn.h"
|
2020-07-01 10:07:45 -04:00
|
|
|
#include "timers.h"
|
2019-10-22 11:09:46 -04:00
|
|
|
|
2020-04-24 09:44:51 -04:00
|
|
|
#include STM32_HAL_H
|
2020-03-10 17:16:31 -04:00
|
|
|
|
|
|
|
#define STM32_GPIO_PORT_SIZE 16
|
|
|
|
static pulseio_pulsein_obj_t* _objs[STM32_GPIO_PORT_SIZE];
|
|
|
|
|
2020-07-01 10:07:45 -04:00
|
|
|
STATIC TIM_HandleTypeDef tim_handle;
|
2020-03-23 17:46:25 -04:00
|
|
|
static uint32_t overflow_count = 0;
|
|
|
|
STATIC uint8_t refcount = 0;
|
|
|
|
|
2020-03-10 17:16:31 -04:00
|
|
|
static void assign_EXTI_Interrupt(pulseio_pulsein_obj_t* self, uint8_t num);
|
|
|
|
|
2020-07-01 10:07:45 -04:00
|
|
|
void pulsein_timer_event_handler(void)
|
2020-03-23 17:46:25 -04:00
|
|
|
{
|
|
|
|
// Detect TIM Update event
|
2020-07-01 10:07:45 -04:00
|
|
|
if (__HAL_TIM_GET_FLAG(&tim_handle, TIM_FLAG_UPDATE) != RESET)
|
2020-03-23 17:46:25 -04:00
|
|
|
{
|
2020-07-01 10:07:45 -04:00
|
|
|
if (__HAL_TIM_GET_IT_SOURCE(&tim_handle, TIM_IT_UPDATE) != RESET)
|
2020-03-23 17:46:25 -04:00
|
|
|
{
|
2020-07-01 10:07:45 -04:00
|
|
|
__HAL_TIM_CLEAR_IT(&tim_handle, TIM_IT_UPDATE);
|
2020-03-23 17:46:25 -04:00
|
|
|
overflow_count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-12 15:03:21 -04:00
|
|
|
|
2020-07-01 10:07:45 -04:00
|
|
|
static void pulsein_exti_event_handler(uint8_t num) {
|
2020-03-10 17:16:31 -04:00
|
|
|
// Grab the current time first.
|
2020-03-23 17:46:25 -04:00
|
|
|
uint32_t current_overflow = overflow_count;
|
2020-07-01 10:07:45 -04:00
|
|
|
uint32_t current_count = tim_handle.Instance->CNT;
|
2020-03-10 17:16:31 -04:00
|
|
|
|
2020-03-23 17:46:25 -04:00
|
|
|
// Interrupt register must be cleared manually
|
|
|
|
EXTI->PR = 1 << num;
|
2020-03-10 17:16:31 -04:00
|
|
|
|
|
|
|
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 {
|
2020-04-14 20:09:48 -04:00
|
|
|
uint32_t total_diff = current_count + 0x10000 * (current_overflow - self->last_overflow) - self->last_count;
|
2020-03-23 17:46:25 -04:00
|
|
|
// Cap duration at 16 bits.
|
2020-04-14 20:09:48 -04:00
|
|
|
uint16_t duration = MIN(0xffff, total_diff);
|
2020-03-10 17:16:31 -04:00
|
|
|
|
|
|
|
uint16_t i = (self->start + self->len) % self->maxlen;
|
|
|
|
self->buffer[i] = duration;
|
|
|
|
if (self->len < self->maxlen) {
|
|
|
|
self->len++;
|
|
|
|
} else {
|
|
|
|
self->start++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-23 17:46:25 -04:00
|
|
|
self->last_count = current_count;
|
|
|
|
self->last_overflow = current_overflow;
|
2020-03-10 17:16:31 -04:00
|
|
|
}
|
|
|
|
|
2019-10-22 11:09:46 -04:00
|
|
|
void pulsein_reset(void) {
|
2020-03-10 17:16:31 -04:00
|
|
|
// 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));
|
2020-03-23 17:46:25 -04:00
|
|
|
|
2020-07-16 15:45:18 -04:00
|
|
|
HAL_TIM_Base_DeInit(&tim_handle);
|
2020-07-01 10:07:45 -04:00
|
|
|
tim_clock_disable(stm_peripherals_timer_get_index(tim_handle.Instance));
|
2020-07-16 15:45:18 -04:00
|
|
|
memset(&tim_handle, 0, sizeof(tim_handle));
|
2020-03-23 17:46:25 -04:00
|
|
|
refcount = 0;
|
2019-10-22 11:09:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu_pin_obj_t* pin,
|
|
|
|
uint16_t maxlen, bool idle_state) {
|
2020-03-10 17:16:31 -04:00
|
|
|
// 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) {
|
2020-03-20 15:58:34 -04:00
|
|
|
mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"),
|
2020-03-10 17:16:31 -04:00
|
|
|
maxlen * sizeof(uint16_t));
|
|
|
|
}
|
2020-03-20 15:58:34 -04:00
|
|
|
|
2020-03-10 17:16:31 -04:00
|
|
|
// 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;
|
2020-03-23 17:46:25 -04:00
|
|
|
self->last_count = 0;
|
|
|
|
self->last_overflow = 0;
|
|
|
|
|
2020-07-16 15:45:18 -04:00
|
|
|
if (HAL_TIM_Base_GetState(&tim_handle) == HAL_TIM_STATE_RESET) {
|
2020-07-01 10:07:45 -04:00
|
|
|
// Find a suitable timer
|
|
|
|
TIM_TypeDef * tim_instance = stm_peripherals_find_timer();
|
|
|
|
stm_peripherals_timer_reserve(tim_instance);
|
|
|
|
|
2020-07-16 15:45:18 -04:00
|
|
|
// Set ticks to 1us
|
2020-07-01 10:07:45 -04:00
|
|
|
uint32_t source = stm_peripherals_timer_get_source_freq(tim_instance);
|
2020-07-16 15:45:18 -04:00
|
|
|
uint32_t prescaler = source/1000000;
|
2020-07-01 10:07:45 -04:00
|
|
|
|
2020-07-16 15:45:18 -04:00
|
|
|
// Enable clocks and IRQ, set callback
|
2020-07-01 10:07:45 -04:00
|
|
|
stm_peripherals_timer_preinit(tim_instance, 4, pulsein_timer_event_handler);
|
|
|
|
|
|
|
|
// Set the new period
|
|
|
|
tim_handle.Instance = tim_instance;
|
2020-07-16 15:45:18 -04:00
|
|
|
tim_handle.Init.Prescaler = prescaler - 1;
|
|
|
|
tim_handle.Init.Period = 0x10000 - 1; //65 ms period (maximum)
|
2020-07-01 10:07:45 -04:00
|
|
|
HAL_TIM_Base_Init(&tim_handle);
|
2020-03-23 17:46:25 -04:00
|
|
|
|
2020-07-01 10:07:45 -04:00
|
|
|
// 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);
|
2020-03-23 17:46:25 -04:00
|
|
|
|
|
|
|
overflow_count = 0;
|
|
|
|
}
|
|
|
|
// Add to active PulseIns
|
|
|
|
refcount++;
|
2020-03-10 17:16:31 -04:00
|
|
|
|
|
|
|
// 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);
|
|
|
|
claim_pin(pin);
|
2019-10-22 11:09:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {
|
2020-03-10 17:16:31 -04:00
|
|
|
return (self->pin == NULL);
|
2019-10-22 11:09:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
|
2020-03-10 17:16:31 -04:00
|
|
|
if (common_hal_pulseio_pulsein_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-03-12 11:21:32 -04:00
|
|
|
//Remove pulsein slot from shared array
|
2020-03-10 17:16:31 -04:00
|
|
|
HAL_NVIC_DisableIRQ(self->irq);
|
|
|
|
_objs[self->pin->number] = NULL;
|
|
|
|
reset_pin_number(self->pin->port, self->pin->number);
|
|
|
|
self->pin = NULL;
|
2020-03-23 17:46:25 -04:00
|
|
|
|
|
|
|
refcount--;
|
|
|
|
if (refcount == 0) {
|
2020-07-01 10:07:45 -04:00
|
|
|
tim_clock_disable(1<< stm_peripherals_timer_get_index(tim_handle.Instance));
|
2020-03-23 17:46:25 -04:00
|
|
|
}
|
2019-10-22 11:09:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) {
|
2020-03-10 17:16:31 -04:00
|
|
|
HAL_NVIC_DisableIRQ(self->irq);
|
|
|
|
self->paused = true;
|
2019-10-22 11:09:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration) {
|
2020-03-10 17:16:31 -04:00
|
|
|
// 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;
|
2020-03-23 17:46:25 -04:00
|
|
|
self->last_count = 0;
|
|
|
|
self->last_overflow = 0;
|
2020-03-10 17:16:31 -04:00
|
|
|
|
|
|
|
HAL_NVIC_EnableIRQ(self->irq);
|
2019-10-22 11:09:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) {
|
2020-03-10 17:16:31 -04:00
|
|
|
HAL_NVIC_DisableIRQ(self->irq);
|
|
|
|
self->start = 0;
|
|
|
|
self->len = 0;
|
|
|
|
HAL_NVIC_EnableIRQ(self->irq);
|
2019-10-22 11:09:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_t index) {
|
2020-03-10 17:16:31 -04:00
|
|
|
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(translate("index out of range"));
|
|
|
|
}
|
|
|
|
uint16_t value = self->buffer[(self->start + index) % self->maxlen];
|
|
|
|
HAL_NVIC_EnableIRQ(self->irq);
|
|
|
|
return value;
|
2019-10-22 11:09:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) {
|
2020-03-10 17:16:31 -04:00
|
|
|
if (self->len == 0) {
|
|
|
|
mp_raise_IndexError(translate("pop from an empty 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;
|
2019-10-22 11:09:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) {
|
2020-03-10 17:16:31 -04:00
|
|
|
return self->maxlen;
|
2019-10-22 11:09:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t* self) {
|
2020-03-10 17:16:31 -04:00
|
|
|
return self->paused;
|
2019-10-22 11:09:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) {
|
2020-03-10 17:16:31 -04:00
|
|
|
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)
|
|
|
|
{
|
2020-07-01 10:07:45 -04:00
|
|
|
pulsein_exti_event_handler(0);
|
2020-03-10 17:16:31 -04:00
|
|
|
}
|
|
|
|
void EXTI1_IRQHandler(void)
|
|
|
|
{
|
2020-07-01 10:07:45 -04:00
|
|
|
pulsein_exti_event_handler(1);
|
2020-03-10 17:16:31 -04:00
|
|
|
}
|
|
|
|
void EXTI2_IRQHandler(void)
|
|
|
|
{
|
2020-07-01 10:07:45 -04:00
|
|
|
pulsein_exti_event_handler(2);
|
2020-03-10 17:16:31 -04:00
|
|
|
}
|
|
|
|
void EXTI3_IRQHandler(void)
|
|
|
|
{
|
2020-07-01 10:07:45 -04:00
|
|
|
pulsein_exti_event_handler(3);
|
2020-03-10 17:16:31 -04:00
|
|
|
}
|
|
|
|
void EXTI4_IRQHandler(void)
|
|
|
|
{
|
2020-07-01 10:07:45 -04:00
|
|
|
pulsein_exti_event_handler(4);
|
2020-03-10 17:16:31 -04:00
|
|
|
}
|
2020-03-12 15:03:21 -04:00
|
|
|
|
2020-03-10 17:16:31 -04:00
|
|
|
void EXTI9_5_IRQHandler(void)
|
|
|
|
{
|
|
|
|
uint32_t pending = EXTI->PR;
|
2020-03-12 15:03:21 -04:00
|
|
|
for (uint i = 5; i <= 9; i++) {
|
|
|
|
if(pending & (1 << i)) {
|
2020-07-01 10:07:45 -04:00
|
|
|
pulsein_exti_event_handler(i);
|
2020-03-12 15:03:21 -04:00
|
|
|
}
|
2020-03-10 17:16:31 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void EXTI15_10_IRQHandler(void)
|
|
|
|
{
|
|
|
|
uint32_t pending = EXTI->PR;
|
2020-03-12 15:03:21 -04:00
|
|
|
for (uint i = 10; i <= 15; i++) {
|
|
|
|
if(pending & (1 << i)) {
|
2020-07-01 10:07:45 -04:00
|
|
|
pulsein_exti_event_handler(i);
|
2020-03-12 15:03:21 -04:00
|
|
|
}
|
2020-03-10 17:16:31 -04:00
|
|
|
}
|
2019-10-22 11:09:46 -04:00
|
|
|
}
|