2017-03-10 13:17:54 -05:00
|
|
|
/*
|
2017-08-27 15:02:50 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2017-03-10 13:17:54 -05:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2020-06-03 18:40:05 -04:00
|
|
|
* SPDX-FileCopyrightText: Copyright (c) 2016 Damien P. George
|
2017-03-10 13:17:54 -05: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.
|
|
|
|
*/
|
|
|
|
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "common-hal/pulseio/PulseOut.h"
|
2017-03-10 13:17:54 -05:00
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2018-02-09 03:22:29 -05:00
|
|
|
#include "hal/include/hal_gpio.h"
|
2017-03-10 13:17:54 -05:00
|
|
|
|
|
|
|
#include "mpconfigport.h"
|
2018-06-15 19:16:21 -04:00
|
|
|
#include "samd/pins.h"
|
|
|
|
#include "samd/timers.h"
|
2017-04-21 17:43:03 -04:00
|
|
|
#include "py/gc.h"
|
2017-03-10 13:17:54 -05:00
|
|
|
#include "py/runtime.h"
|
2017-04-10 16:32:19 -04:00
|
|
|
#include "shared-bindings/pulseio/PulseOut.h"
|
2022-05-27 15:59:54 -04:00
|
|
|
#include "supervisor/shared/translate/translate.h"
|
2019-02-16 20:54:16 -05:00
|
|
|
#include "timer_handler.h"
|
2017-03-10 13:17:54 -05:00
|
|
|
|
|
|
|
// This timer is shared amongst all PulseOut objects under the assumption that
|
2018-02-09 03:22:29 -05:00
|
|
|
// the code is single threaded.
|
2017-03-10 13:17:54 -05:00
|
|
|
static uint8_t refcount = 0;
|
|
|
|
|
2018-02-13 21:17:20 -05:00
|
|
|
static uint8_t pulseout_tc_index = 0xff;
|
2018-02-09 03:22:29 -05:00
|
|
|
|
2017-03-10 13:17:54 -05:00
|
|
|
static __IO PORT_PINCFG_Type *active_pincfg = NULL;
|
|
|
|
static uint16_t *pulse_buffer = NULL;
|
|
|
|
static volatile uint16_t pulse_index = 0;
|
|
|
|
static uint16_t pulse_length;
|
|
|
|
static volatile uint32_t current_compare = 0;
|
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
static void turn_on(__IO PORT_PINCFG_Type *pincfg) {
|
2017-03-10 13:17:54 -05:00
|
|
|
pincfg->reg = PORT_PINCFG_PMUXEN;
|
|
|
|
}
|
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
static void turn_off(__IO PORT_PINCFG_Type *pincfg) {
|
2017-03-10 13:17:54 -05:00
|
|
|
pincfg->reg = PORT_PINCFG_RESETVALUE;
|
|
|
|
}
|
|
|
|
|
2021-11-10 09:42:21 -05:00
|
|
|
STATIC void pulse_finish(void) {
|
2017-03-10 13:17:54 -05:00
|
|
|
pulse_index++;
|
|
|
|
|
|
|
|
if (active_pincfg == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Always turn it off.
|
|
|
|
turn_off(active_pincfg);
|
|
|
|
if (pulse_index >= pulse_length) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
current_compare = (current_compare + pulse_buffer[pulse_index] * 3 / 4) & 0xffff;
|
2021-04-30 11:47:37 -04:00
|
|
|
Tc *tc = tc_insts[pulseout_tc_index];
|
2018-02-13 21:17:20 -05:00
|
|
|
tc->COUNT16.CC[0].reg = current_compare;
|
2017-03-10 13:17:54 -05:00
|
|
|
if (pulse_index % 2 == 0) {
|
|
|
|
turn_on(active_pincfg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 21:17:20 -05:00
|
|
|
void pulseout_interrupt_handler(uint8_t index) {
|
2021-04-30 11:47:37 -04:00
|
|
|
if (index != pulseout_tc_index) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
Tc *tc = tc_insts[index];
|
|
|
|
if (!tc->COUNT16.INTFLAG.bit.MC0) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-13 21:17:20 -05:00
|
|
|
|
|
|
|
pulse_finish();
|
|
|
|
|
|
|
|
// Clear the interrupt bit.
|
|
|
|
tc->COUNT16.INTFLAG.reg = TC_INTFLAG_MC0;
|
|
|
|
}
|
|
|
|
|
2017-03-10 13:17:54 -05:00
|
|
|
void pulseout_reset() {
|
|
|
|
refcount = 0;
|
2018-02-13 21:17:20 -05:00
|
|
|
pulseout_tc_index = 0xff;
|
2017-03-10 13:17:54 -05:00
|
|
|
active_pincfg = NULL;
|
2021-04-30 11:47:37 -04:00
|
|
|
#ifdef SAMD21
|
2021-04-20 16:21:05 -04:00
|
|
|
rtc_end_pulse();
|
2021-04-30 11:47:37 -04:00
|
|
|
#endif
|
2017-03-10 13:17:54 -05:00
|
|
|
}
|
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self,
|
|
|
|
const mcu_pin_obj_t *pin,
|
|
|
|
uint32_t frequency,
|
|
|
|
uint16_t duty_cycle) {
|
2021-07-21 19:27:09 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-03-10 13:17:54 -05:00
|
|
|
if (refcount == 0) {
|
|
|
|
// Find a spare timer.
|
2018-02-13 21:17:20 -05:00
|
|
|
Tc *tc = NULL;
|
2018-02-16 13:05:28 -05:00
|
|
|
int8_t index = TC_INST_NUM - 1;
|
2018-02-13 21:17:20 -05:00
|
|
|
for (; index >= 0; index--) {
|
|
|
|
if (tc_insts[index]->COUNT16.CTRLA.bit.ENABLE == 0) {
|
|
|
|
tc = tc_insts[index];
|
2017-03-10 13:17:54 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-02-13 21:17:20 -05:00
|
|
|
if (tc == NULL) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_RuntimeError(translate("All timers in use"));
|
2017-03-10 13:17:54 -05:00
|
|
|
}
|
|
|
|
|
2018-02-13 21:17:20 -05:00
|
|
|
pulseout_tc_index = index;
|
|
|
|
|
2019-02-19 21:18:21 -05:00
|
|
|
set_timer_handler(true, index, TC_HANDLER_PULSEOUT);
|
2018-02-13 21:17:20 -05:00
|
|
|
// We use GCLK0 for SAMD21 and GCLK1 for SAMD51 because they both run at 48mhz making our
|
|
|
|
// math the same across the boards.
|
|
|
|
#ifdef SAMD21
|
2019-02-19 21:18:21 -05:00
|
|
|
turn_on_clocks(true, index, 0);
|
2018-02-13 21:17:20 -05:00
|
|
|
#endif
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2019-02-19 21:18:21 -05:00
|
|
|
turn_on_clocks(true, index, 1);
|
2018-02-13 21:17:20 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef SAMD21
|
|
|
|
tc->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16 |
|
2021-04-30 11:47:37 -04:00
|
|
|
TC_CTRLA_PRESCALER_DIV64 |
|
|
|
|
TC_CTRLA_WAVEGEN_NFRQ;
|
2018-02-13 21:17:20 -05:00
|
|
|
#endif
|
2020-06-18 15:13:59 -04:00
|
|
|
#ifdef SAM_D5X_E5X
|
2018-02-13 21:17:20 -05:00
|
|
|
tc_reset(tc);
|
|
|
|
tc_set_enable(tc, false);
|
|
|
|
tc->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16 | TC_CTRLA_PRESCALER_DIV64;
|
|
|
|
tc->COUNT16.WAVE.reg = TC_WAVE_WAVEGEN_NFRQ;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tc_set_enable(tc, true);
|
|
|
|
tc->COUNT16.CTRLBSET.reg = TC_CTRLBSET_CMD_STOP;
|
2017-03-10 13:17:54 -05:00
|
|
|
}
|
|
|
|
refcount++;
|
|
|
|
|
2021-07-21 19:27:09 -04:00
|
|
|
self->pin = pin->number;
|
2017-03-10 13:17:54 -05:00
|
|
|
|
2018-02-09 19:37:18 -05:00
|
|
|
PortGroup *const port_base = &PORT->Group[GPIO_PORT(self->pin)];
|
2017-03-10 13:17:54 -05:00
|
|
|
self->pincfg = &port_base->PINCFG[self->pin % 32];
|
|
|
|
|
|
|
|
// Set the port to output a zero.
|
|
|
|
port_base->OUTCLR.reg = 1 << (self->pin % 32);
|
|
|
|
port_base->DIRSET.reg = 1 << (self->pin % 32);
|
|
|
|
|
|
|
|
// Turn off the pinmux which should connect the port output.
|
|
|
|
turn_off(self->pincfg);
|
2021-04-30 11:47:37 -04:00
|
|
|
#ifdef SAMD21
|
2021-04-20 16:21:05 -04:00
|
|
|
rtc_start_pulse();
|
2021-04-30 11:47:37 -04:00
|
|
|
#endif
|
2021-04-20 16:21:05 -04:00
|
|
|
|
2017-03-10 13:17:54 -05:00
|
|
|
}
|
|
|
|
|
2021-04-30 11:47:37 -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);
|
2017-10-02 20:49:40 -04:00
|
|
|
}
|
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t *self) {
|
2017-10-02 20:49:40 -04:00
|
|
|
if (common_hal_pulseio_pulseout_deinited(self)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-02-09 19:37:18 -05:00
|
|
|
PortGroup *const port_base = &PORT->Group[GPIO_PORT(self->pin)];
|
2017-03-10 13:17:54 -05:00
|
|
|
port_base->DIRCLR.reg = 1 << (self->pin % 32);
|
|
|
|
|
|
|
|
turn_on(self->pincfg);
|
|
|
|
|
|
|
|
refcount--;
|
|
|
|
if (refcount == 0) {
|
2018-02-13 21:17:20 -05:00
|
|
|
tc_reset(tc_insts[pulseout_tc_index]);
|
|
|
|
pulseout_tc_index = 0xff;
|
2017-03-10 13:17:54 -05:00
|
|
|
}
|
2017-10-02 20:49:40 -04:00
|
|
|
self->pin = NO_PIN;
|
2021-07-21 19:27:09 -04:00
|
|
|
common_hal_pwmio_pwmout_deinit(&self->pwmout);
|
2021-04-30 11:47:37 -04:00
|
|
|
#ifdef SAMD21
|
2021-04-20 16:21:05 -04:00
|
|
|
rtc_end_pulse();
|
2021-04-30 11:47:37 -04:00
|
|
|
#endif
|
2017-03-10 13:17:54 -05:00
|
|
|
}
|
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t *self, uint16_t *pulses, uint16_t length) {
|
2017-03-10 13:17:54 -05:00
|
|
|
if (active_pincfg != NULL) {
|
2018-07-31 19:53:54 -04:00
|
|
|
mp_raise_RuntimeError(translate("Another send is already active"));
|
2017-03-10 13:17:54 -05:00
|
|
|
}
|
|
|
|
active_pincfg = self->pincfg;
|
|
|
|
pulse_buffer = pulses;
|
|
|
|
pulse_index = 0;
|
|
|
|
pulse_length = length;
|
|
|
|
|
|
|
|
current_compare = pulses[0] * 3 / 4;
|
2021-04-30 11:47:37 -04:00
|
|
|
Tc *tc = tc_insts[pulseout_tc_index];
|
2018-02-13 21:17:20 -05:00
|
|
|
tc->COUNT16.CC[0].reg = current_compare;
|
2017-03-10 13:17:54 -05:00
|
|
|
|
2018-02-14 14:38:52 -05:00
|
|
|
// Clear our interrupt in case it was set earlier
|
|
|
|
tc->COUNT16.INTFLAG.reg = TC_INTFLAG_MC0;
|
2018-02-13 21:17:20 -05:00
|
|
|
tc->COUNT16.INTENSET.reg = TC_INTENSET_MC0;
|
|
|
|
tc_enable_interrupts(pulseout_tc_index);
|
2017-03-10 13:17:54 -05:00
|
|
|
turn_on(active_pincfg);
|
2018-02-13 21:17:20 -05:00
|
|
|
tc->COUNT16.CTRLBSET.reg = TC_CTRLBSET_CMD_RETRIGGER;
|
2017-03-10 13:17:54 -05:00
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
while (pulse_index < length) {
|
2017-03-10 13:17:54 -05:00
|
|
|
// Do other things while we wait. The interrupts will handle sending the
|
|
|
|
// signal.
|
2019-08-11 09:38:59 -04:00
|
|
|
RUN_BACKGROUND_TASKS;
|
2017-03-10 13:17:54 -05:00
|
|
|
}
|
|
|
|
|
2018-02-13 21:17:20 -05:00
|
|
|
tc->COUNT16.CTRLBSET.reg = TC_CTRLBSET_CMD_STOP;
|
|
|
|
tc->COUNT16.INTENCLR.reg = TC_INTENCLR_MC0;
|
|
|
|
tc_disable_interrupts(pulseout_tc_index);
|
2017-03-10 13:17:54 -05:00
|
|
|
active_pincfg = NULL;
|
|
|
|
}
|