2019-07-15 20:56:21 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2019 Jeff Epler 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 <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "extmod/vfs_fat.h"
|
|
|
|
#include "py/gc.h"
|
|
|
|
#include "py/mperrno.h"
|
|
|
|
#include "py/runtime.h"
|
2019-07-29 18:39:00 -04:00
|
|
|
#include "common-hal/audiopwmio/PWMAudioOut.h"
|
2020-08-18 16:08:33 -04:00
|
|
|
#include "common-hal/pwmio/PWMOut.h"
|
2019-07-29 18:39:00 -04:00
|
|
|
#include "shared-bindings/audiopwmio/PWMAudioOut.h"
|
2019-07-15 20:56:21 -04:00
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
|
|
|
#include "shared-bindings/microcontroller/Pin.h"
|
2020-05-13 11:39:40 -04:00
|
|
|
#include "supervisor/shared/tick.h"
|
2019-07-15 20:56:21 -04:00
|
|
|
#include "supervisor/shared/translate.h"
|
|
|
|
|
|
|
|
// TODO: This should be the same size as PWMOut.c:pwms[], but there's no trivial way to accomplish that
|
2021-03-15 09:57:36 -04:00
|
|
|
STATIC audiopwmio_pwmaudioout_obj_t *active_audio[4];
|
2019-07-15 20:56:21 -04:00
|
|
|
|
|
|
|
#define F_TARGET (62500)
|
|
|
|
#define F_PWM (16000000)
|
|
|
|
// return the REFRESH value, store the TOP value in an out-parameter
|
|
|
|
// Tested for key values (worst relative error = 0.224% = 3.84 cents)
|
|
|
|
// 8000: top = 250 refresh = 7 [ 8000.0]
|
|
|
|
// 22050: top = 242 refresh = 2 [22038.5]
|
|
|
|
// 24000: top = 222 refresh = 2 [24024.0]
|
|
|
|
// 44100: top = 181 refresh = 1 [44198.8]
|
|
|
|
// 48000: top = 167 refresh = 1 [47904.1]
|
|
|
|
STATIC uint32_t calculate_pwm_parameters(uint32_t sample_rate, uint32_t *top_out) {
|
|
|
|
// the desired frequency is the closest integer multiple of sample_rate not less than F_TARGET
|
|
|
|
uint32_t desired_frequency = (F_TARGET + sample_rate - 1) / sample_rate * sample_rate;
|
|
|
|
// The top value is the PWM frequency divided by the desired frequency (round to nearest)
|
2021-03-15 09:57:36 -04:00
|
|
|
uint32_t top = (F_PWM + desired_frequency / 2) / desired_frequency;
|
2019-07-15 20:56:21 -04:00
|
|
|
// The actual frequency is the PWM frequency divided by the top value (round to nearest)
|
2021-03-15 09:57:36 -04:00
|
|
|
uint32_t actual_frequency = (F_PWM + top / 2) / top;
|
2019-07-15 20:56:21 -04:00
|
|
|
// The multiplier is the actual frequency divided by the sample rate (round to nearest)
|
2021-03-15 09:57:36 -04:00
|
|
|
uint32_t multiplier = (actual_frequency + sample_rate / 2) / sample_rate;
|
2019-07-15 20:56:21 -04:00
|
|
|
*top_out = top;
|
|
|
|
return multiplier - 1;
|
|
|
|
}
|
|
|
|
|
2019-07-29 18:39:00 -04:00
|
|
|
STATIC void activate_audiopwmout_obj(audiopwmio_pwmaudioout_obj_t *self) {
|
2021-03-15 09:57:36 -04:00
|
|
|
for (size_t i = 0; i < MP_ARRAY_SIZE(active_audio); i++) {
|
2019-10-12 00:57:08 -04:00
|
|
|
if (!active_audio[i]) {
|
2019-07-15 20:56:21 -04:00
|
|
|
active_audio[i] = self;
|
2020-05-13 11:39:40 -04:00
|
|
|
supervisor_enable_tick();
|
2019-07-15 20:56:21 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-07-29 18:39:00 -04:00
|
|
|
STATIC void deactivate_audiopwmout_obj(audiopwmio_pwmaudioout_obj_t *self) {
|
2020-03-10 18:05:42 -04:00
|
|
|
// Turn off the interrupts to the CPU.
|
|
|
|
self->pwm->INTENCLR = PWM_INTENSET_SEQSTARTED0_Msk | PWM_INTENSET_SEQSTARTED1_Msk;
|
2021-03-15 09:57:36 -04:00
|
|
|
for (size_t i = 0; i < MP_ARRAY_SIZE(active_audio); i++) {
|
2019-10-12 00:57:08 -04:00
|
|
|
if (active_audio[i] == self) {
|
2019-07-15 20:56:21 -04:00
|
|
|
active_audio[i] = NULL;
|
2020-05-13 11:39:40 -04:00
|
|
|
supervisor_disable_tick();
|
2019-10-12 00:57:08 -04:00
|
|
|
}
|
2019-07-15 20:56:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void audiopwmout_reset() {
|
2021-03-15 09:57:36 -04:00
|
|
|
for (size_t i = 0; i < MP_ARRAY_SIZE(active_audio); i++) {
|
2020-05-13 11:39:40 -04:00
|
|
|
if (active_audio[i]) {
|
|
|
|
supervisor_disable_tick();
|
|
|
|
}
|
2019-07-15 20:56:21 -04:00
|
|
|
active_audio[i] = NULL;
|
2019-10-12 00:57:08 -04:00
|
|
|
}
|
2019-07-15 20:56:21 -04:00
|
|
|
}
|
|
|
|
|
2019-07-29 18:39:00 -04:00
|
|
|
STATIC void fill_buffers(audiopwmio_pwmaudioout_obj_t *self, int buf) {
|
2019-07-15 20:56:21 -04:00
|
|
|
uint16_t *dev_buffer = self->buffers[buf];
|
|
|
|
uint8_t *buffer;
|
|
|
|
uint32_t buffer_length;
|
|
|
|
audioio_get_buffer_result_t get_buffer_result =
|
|
|
|
audiosample_get_buffer(self->sample, false, 0,
|
2021-03-15 09:57:36 -04:00
|
|
|
&buffer, &buffer_length);
|
2019-07-15 20:56:21 -04:00
|
|
|
if (get_buffer_result == GET_BUFFER_ERROR) {
|
2019-07-29 18:39:00 -04:00
|
|
|
common_hal_audiopwmio_pwmaudioout_stop(self);
|
2019-07-15 20:56:21 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-12-09 11:57:55 -05:00
|
|
|
uint32_t num_samples = buffer_length / self->bytes_per_sample / self->sample_channel_count;
|
|
|
|
uint16_t *end_dev_buffer = dev_buffer + 2 * num_samples;
|
2019-07-15 20:56:21 -04:00
|
|
|
|
2019-10-12 00:57:08 -04:00
|
|
|
if (self->bytes_per_sample == 1) {
|
2019-07-15 20:56:21 -04:00
|
|
|
uint8_t offset = self->signed_to_unsigned ? 0x80 : 0;
|
|
|
|
uint16_t scale = self->scale;
|
2019-12-09 11:57:55 -05:00
|
|
|
while (dev_buffer < end_dev_buffer) {
|
2019-07-15 20:56:21 -04:00
|
|
|
uint8_t rawval = (*buffer++ + offset);
|
|
|
|
uint16_t val = (uint16_t)(((uint32_t)rawval * (uint32_t)scale) >> 8);
|
|
|
|
*dev_buffer++ = val;
|
2021-03-15 09:57:36 -04:00
|
|
|
if (self->sample_channel_count == 1) {
|
2019-07-15 20:56:21 -04:00
|
|
|
*dev_buffer++ = val;
|
2021-03-15 09:57:36 -04:00
|
|
|
}
|
2019-07-15 20:56:21 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
uint16_t offset = self->signed_to_unsigned ? 0x8000 : 0;
|
|
|
|
uint16_t scale = self->scale;
|
2021-03-15 09:57:36 -04:00
|
|
|
uint16_t *buffer16 = (uint16_t *)buffer;
|
2019-12-09 11:57:55 -05:00
|
|
|
while (dev_buffer < end_dev_buffer) {
|
2019-07-15 20:56:21 -04:00
|
|
|
uint16_t rawval = (*buffer16++ + offset);
|
|
|
|
uint16_t val = (uint16_t)((rawval * (uint32_t)scale) >> 16);
|
|
|
|
*dev_buffer++ = val;
|
2021-03-15 09:57:36 -04:00
|
|
|
if (self->sample_channel_count == 1) {
|
2019-07-15 20:56:21 -04:00
|
|
|
*dev_buffer++ = val;
|
2021-03-15 09:57:36 -04:00
|
|
|
}
|
2019-07-15 20:56:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
self->pwm->SEQ[buf].PTR = (intptr_t)self->buffers[buf];
|
2021-03-15 09:57:36 -04:00
|
|
|
self->pwm->SEQ[buf].CNT = num_samples * 2;
|
2019-07-15 20:56:21 -04:00
|
|
|
|
|
|
|
if (self->loop && get_buffer_result == GET_BUFFER_DONE) {
|
|
|
|
audiosample_reset_buffer(self->sample, false, 0);
|
2019-10-12 00:57:08 -04:00
|
|
|
} else if (get_buffer_result == GET_BUFFER_DONE) {
|
2019-07-31 21:02:56 -04:00
|
|
|
self->pwm->SHORTS = NRF_PWM_SHORT_SEQEND0_STOP_MASK | NRF_PWM_SHORT_SEQEND1_STOP_MASK;
|
2019-07-15 20:56:21 -04:00
|
|
|
self->stopping = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 18:39:00 -04:00
|
|
|
STATIC void audiopwmout_background_obj(audiopwmio_pwmaudioout_obj_t *self) {
|
2021-03-15 09:57:36 -04:00
|
|
|
if (!common_hal_audiopwmio_pwmaudioout_get_playing(self)) {
|
2019-07-15 20:56:21 -04:00
|
|
|
return;
|
2021-03-15 09:57:36 -04:00
|
|
|
}
|
2019-10-12 00:57:08 -04:00
|
|
|
if (self->stopping) {
|
2019-07-15 20:56:21 -04:00
|
|
|
bool stopped =
|
|
|
|
(self->pwm->EVENTS_SEQEND[0] || !self->pwm->EVENTS_SEQSTARTED[0]) &&
|
|
|
|
(self->pwm->EVENTS_SEQEND[1] || !self->pwm->EVENTS_SEQSTARTED[1]);
|
2021-03-15 09:57:36 -04:00
|
|
|
if (stopped) {
|
2019-07-15 20:56:21 -04:00
|
|
|
self->pwm->TASKS_STOP = 1;
|
2021-03-15 09:57:36 -04:00
|
|
|
}
|
2019-10-12 00:57:08 -04:00
|
|
|
} else if (!self->paused && !self->single_buffer) {
|
2020-03-10 19:57:41 -04:00
|
|
|
if (self->pwm->EVENTS_SEQSTARTED[0]) {
|
|
|
|
fill_buffers(self, 1);
|
|
|
|
self->pwm->EVENTS_SEQSTARTED[0] = 0;
|
|
|
|
}
|
|
|
|
if (self->pwm->EVENTS_SEQSTARTED[1]) {
|
|
|
|
fill_buffers(self, 0);
|
|
|
|
self->pwm->EVENTS_SEQSTARTED[1] = 0;
|
|
|
|
}
|
|
|
|
NVIC_ClearPendingIRQ(self->pwm_irq);
|
2019-07-15 20:56:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void audiopwmout_background() {
|
2020-03-10 18:05:42 -04:00
|
|
|
// Check the NVIC first because it is part of the CPU and fast to read.
|
|
|
|
if (!NVIC_GetPendingIRQ(PWM0_IRQn) &&
|
|
|
|
!NVIC_GetPendingIRQ(PWM1_IRQn) &&
|
|
|
|
!NVIC_GetPendingIRQ(PWM2_IRQn) &&
|
|
|
|
!NVIC_GetPendingIRQ(PWM3_IRQn)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Check our objects because the PWM could be active for some other reason.
|
2021-03-15 09:57:36 -04:00
|
|
|
for (size_t i = 0; i < MP_ARRAY_SIZE(active_audio); i++) {
|
|
|
|
if (!active_audio[i]) {
|
|
|
|
continue;
|
|
|
|
}
|
2019-07-15 20:56:21 -04:00
|
|
|
audiopwmout_background_obj(active_audio[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-29 15:37:32 -05:00
|
|
|
// Caller validates that pins are free.
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_audiopwmio_pwmaudioout_construct(audiopwmio_pwmaudioout_obj_t *self,
|
|
|
|
const mcu_pin_obj_t *left_channel, const mcu_pin_obj_t *right_channel, uint16_t quiescent_value) {
|
2020-03-10 19:57:41 -04:00
|
|
|
self->pwm = pwmout_allocate(256, PWM_PRESCALER_PRESCALER_DIV_1, true, NULL, NULL,
|
2021-03-15 09:57:36 -04:00
|
|
|
&self->pwm_irq);
|
2019-10-12 00:57:08 -04:00
|
|
|
if (!self->pwm) {
|
2019-07-15 20:56:21 -04:00
|
|
|
mp_raise_RuntimeError(translate("All timers in use"));
|
|
|
|
}
|
|
|
|
|
|
|
|
self->pwm->PRESCALER = PWM_PRESCALER_PRESCALER_DIV_1;
|
|
|
|
// two uint16_t values per sample when Grouped
|
|
|
|
// n.b. SEQ[#].CNT "counts" are 2 per sample (left and right channels)
|
|
|
|
self->pwm->DECODER = PWM_DECODER_LOAD_Grouped;
|
|
|
|
|
|
|
|
// we use channels 0 and 2 because these are GROUPED; it lets us save half
|
|
|
|
// the space for sample data (no additional optimization is possible for
|
|
|
|
// single channel)
|
|
|
|
self->pwm->PSEL.OUT[0] = self->left_channel_number = left_channel->number;
|
|
|
|
claim_pin(left_channel);
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
if (right_channel) {
|
2019-07-15 20:56:21 -04:00
|
|
|
self->pwm->PSEL.OUT[2] = self->right_channel_number = right_channel->number;
|
|
|
|
claim_pin(right_channel);
|
|
|
|
}
|
|
|
|
|
|
|
|
self->quiescent_value = quiescent_value >> 8;
|
|
|
|
|
|
|
|
self->pwm->ENABLE = 1;
|
|
|
|
// TODO: Ramp from 0 to quiescent value
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_audiopwmio_pwmaudioout_deinited(audiopwmio_pwmaudioout_obj_t *self) {
|
2019-07-15 20:56:21 -04:00
|
|
|
return !self->pwm;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_audiopwmio_pwmaudioout_deinit(audiopwmio_pwmaudioout_obj_t *self) {
|
2019-07-29 18:39:00 -04:00
|
|
|
if (common_hal_audiopwmio_pwmaudioout_deinited(self)) {
|
2019-07-15 20:56:21 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-10-12 01:00:04 -04:00
|
|
|
deactivate_audiopwmout_obj(self);
|
|
|
|
|
2019-07-15 20:56:21 -04:00
|
|
|
// TODO: ramp the pwm down from quiescent value to 0
|
|
|
|
self->pwm->ENABLE = 0;
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
if (self->left_channel_number) {
|
2019-07-15 20:56:21 -04:00
|
|
|
reset_pin_number(self->left_channel_number);
|
2021-03-15 09:57:36 -04:00
|
|
|
}
|
|
|
|
if (self->right_channel_number) {
|
2019-07-15 20:56:21 -04:00
|
|
|
reset_pin_number(self->right_channel_number);
|
2021-03-15 09:57:36 -04:00
|
|
|
}
|
2019-07-15 20:56:21 -04:00
|
|
|
|
|
|
|
pwmout_free_channel(self->pwm, 0);
|
|
|
|
pwmout_free_channel(self->pwm, 2);
|
|
|
|
|
|
|
|
self->pwm = NULL;
|
|
|
|
|
|
|
|
m_free(self->buffers[0]);
|
|
|
|
self->buffers[0] = NULL;
|
|
|
|
|
|
|
|
m_free(self->buffers[1]);
|
|
|
|
self->buffers[1] = NULL;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_audiopwmio_pwmaudioout_play(audiopwmio_pwmaudioout_obj_t *self, mp_obj_t sample, bool loop) {
|
2019-07-29 18:39:00 -04:00
|
|
|
if (common_hal_audiopwmio_pwmaudioout_get_playing(self)) {
|
|
|
|
common_hal_audiopwmio_pwmaudioout_stop(self);
|
2019-07-15 20:56:21 -04:00
|
|
|
}
|
|
|
|
self->sample = sample;
|
|
|
|
self->loop = loop;
|
|
|
|
|
|
|
|
uint32_t sample_rate = audiosample_sample_rate(sample);
|
|
|
|
self->bytes_per_sample = audiosample_bits_per_sample(sample) / 8;
|
|
|
|
|
|
|
|
uint32_t max_buffer_length;
|
2019-12-09 11:57:55 -05:00
|
|
|
uint8_t spacing;
|
2019-07-15 20:56:21 -04:00
|
|
|
audiosample_get_buffer_structure(sample, /* single channel */ false,
|
|
|
|
&self->single_buffer, &self->signed_to_unsigned, &max_buffer_length,
|
2019-12-09 11:57:55 -05:00
|
|
|
&spacing);
|
|
|
|
self->sample_channel_count = audiosample_channel_count(sample);
|
|
|
|
|
2022-05-13 15:33:43 -04:00
|
|
|
mp_arg_validate_length_max(max_buffer_length, UINT16_MAX, MP_QSTR_buffer);
|
|
|
|
|
2021-03-05 06:00:40 -05:00
|
|
|
uint16_t buffer_length = (uint16_t)max_buffer_length;
|
|
|
|
self->buffers[0] = m_malloc(buffer_length * 2 * sizeof(uint16_t), false);
|
2021-03-15 09:57:36 -04:00
|
|
|
if (!self->single_buffer) {
|
2021-03-05 06:00:40 -05:00
|
|
|
self->buffers[1] = m_malloc(buffer_length * 2 * sizeof(uint16_t), false);
|
2021-03-15 09:57:36 -04:00
|
|
|
}
|
2019-07-15 20:56:21 -04:00
|
|
|
|
|
|
|
|
|
|
|
uint32_t top;
|
|
|
|
self->pwm->SEQ[0].REFRESH = self->pwm->SEQ[1].REFRESH = calculate_pwm_parameters(sample_rate, &top);
|
2021-03-15 09:57:36 -04:00
|
|
|
self->scale = top - 1;
|
2019-07-15 20:56:21 -04:00
|
|
|
self->pwm->COUNTERTOP = top;
|
|
|
|
|
2019-07-31 21:02:56 -04:00
|
|
|
self->pwm->LOOP = 1;
|
2019-07-15 20:56:21 -04:00
|
|
|
audiosample_reset_buffer(self->sample, false, 0);
|
|
|
|
activate_audiopwmout_obj(self);
|
2019-12-17 09:59:08 -05:00
|
|
|
self->stopping = false;
|
|
|
|
self->pwm->SHORTS = NRF_PWM_SHORT_LOOPSDONE_SEQSTART0_MASK;
|
2019-07-15 20:56:21 -04:00
|
|
|
fill_buffers(self, 0);
|
|
|
|
self->pwm->SEQ[1].PTR = self->pwm->SEQ[0].PTR;
|
|
|
|
self->pwm->SEQ[1].CNT = self->pwm->SEQ[0].CNT;
|
|
|
|
self->pwm->EVENTS_SEQSTARTED[0] = 0;
|
|
|
|
self->pwm->EVENTS_SEQSTARTED[1] = 0;
|
2019-12-17 09:59:08 -05:00
|
|
|
self->pwm->EVENTS_SEQEND[0] = 0;
|
|
|
|
self->pwm->EVENTS_SEQEND[1] = 0;
|
2019-07-15 20:56:21 -04:00
|
|
|
self->pwm->EVENTS_STOPPED = 0;
|
2020-03-10 18:05:42 -04:00
|
|
|
// Enable the SEQSTARTED interrupts so that they wake the CPU and keep it awake until serviced.
|
|
|
|
// We don't enable them in the NVIC because we don't actually want an interrupt routine to run.
|
|
|
|
self->pwm->INTENSET = PWM_INTENSET_SEQSTARTED0_Msk | PWM_INTENSET_SEQSTARTED1_Msk;
|
2019-07-31 21:02:56 -04:00
|
|
|
self->pwm->TASKS_SEQSTART[0] = 1;
|
2019-07-15 20:56:21 -04:00
|
|
|
self->playing = true;
|
|
|
|
self->paused = false;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_audiopwmio_pwmaudioout_stop(audiopwmio_pwmaudioout_obj_t *self) {
|
2019-07-15 20:56:21 -04:00
|
|
|
deactivate_audiopwmout_obj(self);
|
|
|
|
self->pwm->TASKS_STOP = 1;
|
|
|
|
self->stopping = false;
|
|
|
|
self->paused = false;
|
|
|
|
|
|
|
|
m_free(self->buffers[0]);
|
|
|
|
self->buffers[0] = NULL;
|
|
|
|
|
|
|
|
m_free(self->buffers[1]);
|
|
|
|
self->buffers[1] = NULL;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_audiopwmio_pwmaudioout_get_playing(audiopwmio_pwmaudioout_obj_t *self) {
|
2019-10-12 00:57:08 -04:00
|
|
|
if (!self->paused && self->pwm->EVENTS_STOPPED) {
|
2019-07-15 20:56:21 -04:00
|
|
|
self->playing = false;
|
|
|
|
self->pwm->EVENTS_STOPPED = 0;
|
|
|
|
}
|
|
|
|
return self->playing;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* pause/resume present difficulties for the NRF PWM audio module.
|
|
|
|
*
|
|
|
|
* A PWM sequence can be stopped in its tracks by sending a TASKS_STOP event,
|
|
|
|
* but there's no way to pick up the sequence where it was stopped; you could
|
|
|
|
* start at the start of one of the two sequences, but especially for "single buffer"
|
|
|
|
* sample, this seems undesirable.
|
|
|
|
*
|
|
|
|
* Or, you can stop at the end of a sequence so that you don't duplicate anything
|
|
|
|
* when restarting, but again this is unsatisfactory for a "single buffer" sample.
|
|
|
|
*
|
|
|
|
* For now, I've taken the coward's way and left these methods unimplemented.
|
|
|
|
* Perhaps the way forward is to divide even "single buffer" samples into tasks of
|
|
|
|
* only a few ms long, so that they can be stopped/restarted quickly enough that it
|
|
|
|
* feels instant. (This also saves on memory, for long in-memory "single buffer"
|
2019-08-03 09:19:25 -04:00
|
|
|
* samples, since we have to locally take a resampled copy!)
|
2019-07-15 20:56:21 -04:00
|
|
|
*/
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_audiopwmio_pwmaudioout_pause(audiopwmio_pwmaudioout_obj_t *self) {
|
2019-07-15 20:56:21 -04:00
|
|
|
self->paused = true;
|
2019-07-31 21:02:56 -04:00
|
|
|
self->pwm->SHORTS = NRF_PWM_SHORT_SEQEND1_STOP_MASK;
|
2019-07-15 20:56:21 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void common_hal_audiopwmio_pwmaudioout_resume(audiopwmio_pwmaudioout_obj_t *self) {
|
2019-07-15 20:56:21 -04:00
|
|
|
self->paused = false;
|
2019-07-31 21:02:56 -04:00
|
|
|
self->pwm->SHORTS = NRF_PWM_SHORT_LOOPSDONE_SEQSTART0_MASK;
|
2019-08-03 09:19:25 -04:00
|
|
|
if (self->pwm->EVENTS_STOPPED) {
|
|
|
|
self->pwm->EVENTS_STOPPED = 0;
|
|
|
|
self->pwm->TASKS_SEQSTART[0] = 1;
|
|
|
|
}
|
2019-07-15 20:56:21 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
bool common_hal_audiopwmio_pwmaudioout_get_paused(audiopwmio_pwmaudioout_obj_t *self) {
|
2019-07-15 20:56:21 -04:00
|
|
|
return self->paused;
|
|
|
|
}
|