2020-03-11 18:13:06 -04:00
|
|
|
/*
|
|
|
|
* 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/PulseOut.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#include "py/mpconfig.h"
|
|
|
|
#include "shared-bindings/pulseio/PulseOut.h"
|
2020-08-18 16:08:33 -04:00
|
|
|
#include "shared-bindings/pwmio/PWMOut.h"
|
2020-03-11 18:13:06 -04:00
|
|
|
|
2020-04-24 09:44:51 -04:00
|
|
|
#include STM32_HAL_H
|
2020-07-01 10:07:45 -04:00
|
|
|
#include "timers.h"
|
2020-03-11 18:13:06 -04:00
|
|
|
|
|
|
|
// A single timer is shared amongst all PulseOut objects under the assumption that
|
|
|
|
// the code is single threaded.
|
|
|
|
STATIC uint8_t refcount = 0;
|
|
|
|
STATIC uint16_t *pulse_array = NULL;
|
|
|
|
STATIC volatile uint16_t pulse_array_index = 0;
|
|
|
|
STATIC uint16_t pulse_array_length;
|
2021-03-15 09:57:36 -04:00
|
|
|
// Timer is shared, must be accessible by interrupt
|
2020-07-01 10:07:45 -04:00
|
|
|
STATIC TIM_HandleTypeDef tim_handle;
|
2021-03-05 05:52:13 -05:00
|
|
|
STATIC pulseio_pulseout_obj_t *curr_pulseout = NULL;
|
2020-03-11 18:13:06 -04:00
|
|
|
|
2020-07-01 10:07:45 -04:00
|
|
|
|
2020-03-11 18:13:06 -04:00
|
|
|
STATIC void turn_on(pulseio_pulseout_obj_t *pulseout) {
|
|
|
|
// Turn on PWM
|
2021-07-21 19:27:09 -04:00
|
|
|
HAL_TIM_PWM_Start(&(pulseout->pwmout.handle), pulseout->pwmout.channel);
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void turn_off(pulseio_pulseout_obj_t *pulseout) {
|
|
|
|
// Turn off PWM
|
2021-07-21 19:27:09 -04:00
|
|
|
HAL_TIM_PWM_Stop(&(pulseout->pwmout.handle), pulseout->pwmout.channel);
|
2020-03-11 18:13:06 -04:00
|
|
|
// Make sure pin is low.
|
2021-07-21 19:27:09 -04:00
|
|
|
HAL_GPIO_WritePin(pin_port(pulseout->pwmout.tim->pin->port),
|
|
|
|
pin_mask(pulseout->pwmout.tim->pin->number), 0);
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void start_timer(void) {
|
|
|
|
// Set the new period
|
2020-07-01 10:07:45 -04:00
|
|
|
tim_handle.Init.Period = pulse_array[pulse_array_index] - 1;
|
|
|
|
HAL_TIM_Base_Init(&tim_handle);
|
2020-03-11 18:13:06 -04:00
|
|
|
|
|
|
|
// TIM7 has limited HAL support, set registers manually
|
2020-07-01 10:07:45 -04:00
|
|
|
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-11 18:13:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void pulseout_event_handler(void) {
|
2020-07-01 10:07:45 -04:00
|
|
|
// Detect TIM Update event
|
2021-03-15 09:57:36 -04:00
|
|
|
if (__HAL_TIM_GET_FLAG(&tim_handle, TIM_FLAG_UPDATE) != RESET) {
|
|
|
|
if (__HAL_TIM_GET_IT_SOURCE(&tim_handle, TIM_IT_UPDATE) != RESET) {
|
2020-07-01 10:07:45 -04:00
|
|
|
__HAL_TIM_CLEAR_IT(&tim_handle, TIM_IT_UPDATE);
|
2021-07-21 19:27:09 -04:00
|
|
|
if (common_hal_pulseio_pulseout_deinited(curr_pulseout)) {
|
2021-03-15 09:57:36 -04:00
|
|
|
return; // invalid interrupt
|
2020-07-01 10:07:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
pulse_array_index++;
|
|
|
|
|
|
|
|
// No more pulses. Turn off output and don't restart.
|
|
|
|
if (pulse_array_index >= pulse_array_length) {
|
|
|
|
turn_off(curr_pulseout);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alternate on and off, starting with on.
|
|
|
|
if (pulse_array_index % 2 == 0) {
|
|
|
|
turn_on(curr_pulseout);
|
|
|
|
} else {
|
|
|
|
turn_off(curr_pulseout);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count up to the next given value.
|
|
|
|
start_timer();
|
|
|
|
}
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void pulseout_reset() {
|
2020-07-01 10:07:45 -04:00
|
|
|
stm_peripherals_timer_free(tim_handle.Instance);
|
2020-03-11 18:13:06 -04:00
|
|
|
refcount = 0;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
|
2021-07-21 19:27:09 -04:00
|
|
|
const mcu_pin_obj_t *pin, uint32_t frequency, uint16_t duty_cycle) {
|
|
|
|
pwmout_result_t result = common_hal_pwmio_pwmout_construct(
|
|
|
|
&self->pwmout, pin, duty_cycle, frequency, false);
|
|
|
|
|
|
|
|
// This will raise an exception and not return if needed.
|
|
|
|
common_hal_pwmio_pwmout_raise_error(result);
|
2020-08-14 16:33:24 -04:00
|
|
|
|
2020-03-11 18:13:06 -04:00
|
|
|
// Add to active PulseOuts
|
|
|
|
refcount++;
|
2021-03-15 09:57:36 -04:00
|
|
|
TIM_TypeDef *tim_instance = stm_peripherals_find_timer();
|
2020-07-01 10:07:45 -04:00
|
|
|
stm_peripherals_timer_reserve(tim_instance);
|
2020-03-11 18:13:06 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
// calculate a 1ms period
|
2020-07-01 10:07:45 -04:00
|
|
|
uint32_t source = stm_peripherals_timer_get_source_freq(tim_instance);
|
2021-03-15 09:57:36 -04:00
|
|
|
uint32_t prescaler = source / 1000000; // 1us intervals
|
2020-03-11 18:13:06 -04:00
|
|
|
|
2020-07-01 10:07:45 -04:00
|
|
|
stm_peripherals_timer_preinit(tim_instance, 4, pulseout_event_handler);
|
|
|
|
tim_handle.Instance = tim_instance;
|
2021-03-15 09:57:36 -04:00
|
|
|
tim_handle.Init.Period = 100; // immediately replaced.
|
2020-07-01 10:07:45 -04:00
|
|
|
tim_handle.Init.Prescaler = prescaler - 1;
|
|
|
|
tim_handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
|
|
|
|
tim_handle.Init.CounterMode = TIM_COUNTERMODE_UP;
|
|
|
|
tim_handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
|
2020-03-11 18:13:06 -04:00
|
|
|
|
2020-07-01 10:07:45 -04:00
|
|
|
HAL_TIM_Base_Init(&tim_handle);
|
|
|
|
tim_handle.Instance->SR = 0;
|
2020-03-11 18:13:06 -04:00
|
|
|
|
2021-07-21 19:27:09 -04:00
|
|
|
|
2020-03-11 18:13:06 -04:00
|
|
|
turn_off(self);
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t *self) {
|
2021-07-21 19:27:09 -04:00
|
|
|
return common_hal_pwmio_pwmout_deinited(&self->pwmout);
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t *self) {
|
2020-03-11 18:13:06 -04:00
|
|
|
if (common_hal_pulseio_pulseout_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
turn_on(self);
|
2021-07-21 19:27:09 -04:00
|
|
|
common_hal_pwmio_pwmout_deinit(&self->pwmout);
|
2020-03-11 18:13:06 -04:00
|
|
|
|
|
|
|
refcount--;
|
|
|
|
if (refcount == 0) {
|
2020-07-22 13:58:57 -04:00
|
|
|
stm_peripherals_timer_free(tim_handle.Instance);
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pulses, uint16_t length) {
|
2020-03-11 18:13:06 -04:00
|
|
|
pulse_array = pulses;
|
|
|
|
pulse_array_index = 0;
|
|
|
|
pulse_array_length = length;
|
|
|
|
curr_pulseout = self;
|
|
|
|
|
|
|
|
turn_on(self);
|
|
|
|
|
|
|
|
// Count up to the next given value.
|
|
|
|
start_timer();
|
|
|
|
|
|
|
|
// Use when debugging, or issues are irrecoverable
|
|
|
|
// uint32_t starttime = supervisor_ticks_ms64();
|
|
|
|
// uint32_t timeout = 10000;
|
2021-03-15 09:57:36 -04:00
|
|
|
while (pulse_array_index < length) {
|
2020-03-11 18:13:06 -04:00
|
|
|
// Do other things while we wait. The interrupts will handle sending the
|
|
|
|
// signal.
|
|
|
|
RUN_BACKGROUND_TASKS;
|
|
|
|
|
2020-07-01 10:07:45 -04:00
|
|
|
// // Use when debugging, or issues are irrecoverable
|
2020-03-11 18:13:06 -04:00
|
|
|
// if ((supervisor_ticks_ms64() - starttime ) > timeout ) {
|
|
|
|
// mp_raise_RuntimeError(translate("Error: Send Timeout"));
|
|
|
|
// }
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
// turn off timer counter.
|
2020-07-01 10:07:45 -04:00
|
|
|
tim_handle.Instance->CR1 &= ~TIM_CR1_CEN;
|
2020-03-11 18:13:06 -04:00
|
|
|
}
|