2017-12-21 07:49:14 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
2018-10-16 11:05:02 -04:00
|
|
|
* Copyright (c) 2018 Dan Halbert for Adafruit Industries
|
2017-12-21 07:49:14 -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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "common-hal/pulseio/PulseOut.h"
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
#include "py/mpconfig.h"
|
2018-10-16 11:05:02 -04:00
|
|
|
#include "nrf/pins.h"
|
|
|
|
#include "nrf/timers.h"
|
2017-12-21 07:49:14 -05:00
|
|
|
#include "py/gc.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "shared-bindings/pulseio/PulseOut.h"
|
2020-08-18 16:08:33 -04:00
|
|
|
#include "shared-bindings/pwmio/PWMOut.h"
|
2018-10-16 11:05:02 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-10-16 11:05:02 -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;
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-10-16 11:05:02 -04:00
|
|
|
static nrfx_timer_t *timer = NULL;
|
|
|
|
|
|
|
|
static uint16_t *pulse_array = NULL;
|
|
|
|
static volatile uint16_t pulse_array_index = 0;
|
|
|
|
static uint16_t pulse_array_length;
|
|
|
|
|
|
|
|
static void turn_on(pulseio_pulseout_obj_t *pulseout) {
|
2021-07-21 19:27:09 -04:00
|
|
|
pulseout->pwmout.pwm->PSEL.OUT[0] = pulseout->pwmout.pin->number;
|
2018-10-16 11:05:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void turn_off(pulseio_pulseout_obj_t *pulseout) {
|
|
|
|
// Disconnect pin from PWM.
|
2021-07-21 19:27:09 -04:00
|
|
|
pulseout->pwmout.pwm->PSEL.OUT[0] = 0xffffffff;
|
2018-10-16 11:05:02 -04:00
|
|
|
// Make sure pin is low.
|
2021-07-21 19:27:09 -04:00
|
|
|
nrf_gpio_pin_clear(pulseout->pwmout.pin->number);
|
2018-10-16 11:05:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void start_timer(void) {
|
|
|
|
nrfx_timer_clear(timer);
|
|
|
|
// true enables interrupt.
|
|
|
|
nrfx_timer_compare(timer, NRF_TIMER_CC_CHANNEL0, pulse_array[pulse_array_index], true);
|
|
|
|
nrfx_timer_resume(timer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void pulseout_event_handler(nrf_timer_event_t event_type, void *p_context) {
|
2021-03-15 09:57:36 -04:00
|
|
|
pulseio_pulseout_obj_t *pulseout = (pulseio_pulseout_obj_t *)p_context;
|
2018-10-16 11:05:02 -04:00
|
|
|
if (event_type != NRF_TIMER_EVENT_COMPARE0) {
|
|
|
|
// Spurious event.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nrfx_timer_pause(timer);
|
|
|
|
|
|
|
|
pulse_array_index++;
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-10-16 11:05:02 -04:00
|
|
|
// No more pulses. Turn off output and don't restart.
|
|
|
|
if (pulse_array_index >= pulse_array_length) {
|
|
|
|
turn_off(pulseout);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Alternate on and off, starting with on.
|
|
|
|
if (pulse_array_index % 2 == 0) {
|
|
|
|
turn_on(pulseout);
|
|
|
|
} else {
|
|
|
|
turn_off(pulseout);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Count up to the next given value.
|
|
|
|
start_timer();
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
2018-10-16 11:05:02 -04:00
|
|
|
void pulseout_reset() {
|
|
|
|
if (timer != NULL) {
|
|
|
|
nrf_peripherals_free_timer(timer);
|
|
|
|
}
|
|
|
|
refcount = 0;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -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
|
|
|
|
2018-10-16 11:05:02 -04:00
|
|
|
if (refcount == 0) {
|
2020-04-01 12:57:52 -04:00
|
|
|
timer = nrf_peripherals_allocate_timer_or_throw();
|
2018-10-16 11:05:02 -04:00
|
|
|
}
|
|
|
|
refcount++;
|
|
|
|
|
|
|
|
nrfx_timer_config_t timer_config = {
|
|
|
|
// PulseOut durations are in microseconds, so this is convenient.
|
|
|
|
.frequency = NRF_TIMER_FREQ_1MHz,
|
|
|
|
.mode = NRF_TIMER_MODE_TIMER,
|
|
|
|
.bit_width = NRF_TIMER_BIT_WIDTH_32,
|
|
|
|
.interrupt_priority = NRFX_TIMER_DEFAULT_CONFIG_IRQ_PRIORITY,
|
|
|
|
.p_context = self,
|
|
|
|
};
|
|
|
|
|
|
|
|
nrfx_timer_init(timer, &timer_config, &pulseout_event_handler);
|
|
|
|
turn_off(self);
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
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);
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t *self) {
|
2018-10-16 11:05:02 -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);
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-10-16 11:05:02 -04:00
|
|
|
refcount--;
|
|
|
|
if (refcount == 0) {
|
|
|
|
nrf_peripherals_free_timer(timer);
|
|
|
|
}
|
2017-12-21 07:49:14 -05: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) {
|
2018-10-16 11:05:02 -04:00
|
|
|
pulse_array = pulses;
|
|
|
|
pulse_array_index = 0;
|
|
|
|
pulse_array_length = length;
|
|
|
|
|
|
|
|
nrfx_timer_enable(timer);
|
|
|
|
|
|
|
|
turn_on(self);
|
|
|
|
// Count up to the next given value.
|
|
|
|
start_timer();
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
while (pulse_array_index < length) {
|
2018-10-16 11:05:02 -04:00
|
|
|
// Do other things while we wait. The interrupts will handle sending the
|
|
|
|
// signal.
|
2019-08-11 09:39:50 -04:00
|
|
|
RUN_BACKGROUND_TASKS;
|
2018-10-16 11:05:02 -04:00
|
|
|
}
|
2017-12-21 07:49:14 -05:00
|
|
|
|
2018-10-16 11:05:02 -04:00
|
|
|
nrfx_timer_disable(timer);
|
2017-12-21 07:49:14 -05:00
|
|
|
}
|