2015-07-08 11:39:24 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
2017-03-12 03:10:51 -04:00
|
|
|
* Copyright (C) 2016 Furrtek
|
2015-07-08 11:39:24 -04:00
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
* any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; see the file COPYING. If not, write to
|
|
|
|
* the Free Software Foundation, Inc., 51 Franklin Street,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "audio_dma.hpp"
|
|
|
|
|
|
|
|
#include <cstdint>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <array>
|
2024-03-19 05:53:55 -04:00
|
|
|
#include <cstring>
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
#include "hal.h"
|
|
|
|
#include "gpdma.hpp"
|
2024-03-19 05:53:55 -04:00
|
|
|
#include "tone_gen.hpp"
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
using namespace lpc43xx;
|
|
|
|
|
|
|
|
#include "portapack_dma.hpp"
|
|
|
|
|
|
|
|
namespace audio {
|
|
|
|
namespace dma {
|
|
|
|
|
2024-03-19 05:53:55 -04:00
|
|
|
ToneGen tone_gen{};
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
constexpr uint32_t gpdma_ahb_master_peripheral = 1;
|
|
|
|
constexpr uint32_t gpdma_ahb_master_memory = 0;
|
|
|
|
constexpr uint32_t gpdma_ahb_master_lli_fetch = 0;
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
constexpr uint32_t gpdma_rx_peripheral = 0x9; /* I2S0 DMA request 1 */
|
2015-07-08 11:39:24 -04:00
|
|
|
constexpr uint32_t gpdma_rx_src_peripheral = gpdma_rx_peripheral;
|
|
|
|
constexpr uint32_t gpdma_rx_dest_peripheral = gpdma_rx_peripheral;
|
|
|
|
|
2023-05-18 16:16:05 -04:00
|
|
|
constexpr uint32_t gpdma_tx_peripheral = 0xa; /* I2S0 DMA request 2 */
|
2015-07-08 11:39:24 -04:00
|
|
|
constexpr uint32_t gpdma_tx_src_peripheral = gpdma_tx_peripheral;
|
|
|
|
constexpr uint32_t gpdma_tx_dest_peripheral = gpdma_tx_peripheral;
|
|
|
|
|
|
|
|
constexpr gpdma::channel::LLIPointer lli_pointer(const void* lli) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return {
|
|
|
|
.lm = gpdma_ahb_master_lli_fetch,
|
|
|
|
.r = 0,
|
|
|
|
.lli = reinterpret_cast<uint32_t>(lli),
|
|
|
|
};
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr gpdma::channel::Control control_tx(const size_t transfer_bytes) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return {
|
|
|
|
.transfersize = gpdma::buffer_words(transfer_bytes, 4),
|
|
|
|
.sbsize = 4, /* Burst size: 32 */
|
|
|
|
.dbsize = 4, /* Burst size: 32 */
|
|
|
|
.swidth = 2, /* Source transfer width: word (32 bits) */
|
|
|
|
.dwidth = 2, /* Destination transfer width: word (32 bits) */
|
|
|
|
.s = gpdma_ahb_master_memory,
|
|
|
|
.d = gpdma_ahb_master_peripheral,
|
|
|
|
.si = 1,
|
|
|
|
.di = 0,
|
|
|
|
.prot1 = 0,
|
|
|
|
.prot2 = 0,
|
|
|
|
.prot3 = 0,
|
|
|
|
.i = 1,
|
|
|
|
};
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr gpdma::channel::Config config_tx() {
|
2023-05-18 16:16:05 -04:00
|
|
|
return {
|
|
|
|
.e = 0,
|
|
|
|
.srcperipheral = gpdma_tx_src_peripheral,
|
|
|
|
.destperipheral = gpdma_tx_dest_peripheral,
|
|
|
|
.flowcntrl = gpdma::FlowControl::MemoryToPeripheral_DMAControl,
|
|
|
|
.ie = 1,
|
|
|
|
.itc = 1,
|
|
|
|
.l = 0,
|
|
|
|
.a = 0,
|
|
|
|
.h = 0,
|
|
|
|
};
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr gpdma::channel::Control control_rx(const size_t transfer_bytes) {
|
2023-05-18 16:16:05 -04:00
|
|
|
return {
|
|
|
|
.transfersize = gpdma::buffer_words(transfer_bytes, 4),
|
|
|
|
.sbsize = 4, /* Burst size: 32 */
|
|
|
|
.dbsize = 4, /* Burst size: 32 */
|
|
|
|
.swidth = 2, /* Source transfer width: word (32 bits) */
|
|
|
|
.dwidth = 2, /* Destination transfer width: word (32 bits) */
|
|
|
|
.s = gpdma_ahb_master_peripheral,
|
|
|
|
.d = gpdma_ahb_master_memory,
|
|
|
|
.si = 0,
|
|
|
|
.di = 1,
|
|
|
|
.prot1 = 0,
|
|
|
|
.prot2 = 0,
|
|
|
|
.prot3 = 0,
|
|
|
|
.i = 1,
|
|
|
|
};
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
constexpr gpdma::channel::Config config_rx() {
|
2023-05-18 16:16:05 -04:00
|
|
|
return {
|
|
|
|
.e = 0,
|
|
|
|
.srcperipheral = gpdma_rx_src_peripheral,
|
|
|
|
.destperipheral = gpdma_rx_dest_peripheral,
|
|
|
|
.flowcntrl = gpdma::FlowControl::PeripheralToMemory_DMAControl,
|
|
|
|
.ie = 1,
|
|
|
|
.itc = 1,
|
|
|
|
.l = 0,
|
|
|
|
.a = 0,
|
|
|
|
.h = 0,
|
|
|
|
};
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Clean up terminology around "buffer", "transfer", "samples" */
|
|
|
|
|
|
|
|
constexpr size_t buffer_samples_log2n = 7;
|
2024-01-09 14:50:45 -05:00
|
|
|
constexpr size_t buffer_samples = (1 << buffer_samples_log2n); // 2^7 = 128 byte circular DMA buffer
|
2015-07-08 11:39:24 -04:00
|
|
|
constexpr size_t transfers_per_buffer_log2n = 2;
|
2024-01-09 14:50:45 -05:00
|
|
|
constexpr size_t transfers_per_buffer = (1 << transfers_per_buffer_log2n); // 2^2 = 4 transfer buffers in the circular buffer
|
|
|
|
constexpr size_t transfer_samples = buffer_samples / transfers_per_buffer; // 128/4 = 32 samples in each transfer buffer
|
2015-07-08 11:39:24 -04:00
|
|
|
constexpr size_t transfers_mask = transfers_per_buffer - 1;
|
|
|
|
|
|
|
|
constexpr size_t buffer_bytes = buffer_samples * sizeof(sample_t);
|
|
|
|
constexpr size_t transfer_bytes = transfer_samples * sizeof(sample_t);
|
|
|
|
|
|
|
|
static std::array<sample_t, buffer_samples> buffer_tx;
|
|
|
|
static std::array<sample_t, buffer_samples> buffer_rx;
|
|
|
|
|
|
|
|
static std::array<gpdma::channel::LLI, transfers_per_buffer> lli_tx_loop;
|
|
|
|
static std::array<gpdma::channel::LLI, transfers_per_buffer> lli_rx_loop;
|
|
|
|
|
|
|
|
static constexpr auto& gpdma_channel_i2s0_tx = gpdma::channels[portapack::i2s0_tx_gpdma_channel_number];
|
|
|
|
static constexpr auto& gpdma_channel_i2s0_rx = gpdma::channels[portapack::i2s0_rx_gpdma_channel_number];
|
|
|
|
|
|
|
|
static volatile const gpdma::channel::LLI* tx_next_lli = nullptr;
|
|
|
|
static volatile const gpdma::channel::LLI* rx_next_lli = nullptr;
|
|
|
|
|
2024-01-09 14:50:45 -05:00
|
|
|
static bool single_tx_buffer = false;
|
2024-03-19 13:23:06 -04:00
|
|
|
static uint32_t beep_duration_downcounter = 0;
|
2024-01-09 14:50:45 -05:00
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
static void tx_transfer_complete() {
|
2023-05-18 16:16:05 -04:00
|
|
|
tx_next_lli = gpdma_channel_i2s0_tx.next_lli();
|
2024-03-19 13:23:06 -04:00
|
|
|
|
|
|
|
if (beep_duration_downcounter != 0)
|
|
|
|
if (--beep_duration_downcounter == 0)
|
|
|
|
beep_stop();
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void tx_error() {
|
2023-05-18 16:16:05 -04:00
|
|
|
disable();
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rx_transfer_complete() {
|
2023-05-18 16:16:05 -04:00
|
|
|
rx_next_lli = gpdma_channel_i2s0_rx.next_lli();
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void rx_error() {
|
2023-05-18 16:16:05 -04:00
|
|
|
disable();
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void configure_tx() {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto peripheral = reinterpret_cast<uint32_t>(&LPC_I2S0->TXFIFO);
|
|
|
|
const auto control_value = control_tx(transfer_bytes);
|
|
|
|
for (size_t i = 0; i < lli_tx_loop.size(); i++) {
|
|
|
|
const auto memory = reinterpret_cast<uint32_t>(&buffer_tx[i * transfer_samples]);
|
|
|
|
lli_tx_loop[i].srcaddr = memory;
|
|
|
|
lli_tx_loop[i].destaddr = peripheral;
|
|
|
|
lli_tx_loop[i].lli = lli_pointer(&lli_tx_loop[(i + 1) % lli_tx_loop.size()]);
|
|
|
|
lli_tx_loop[i].control = control_value;
|
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void configure_rx() {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto peripheral = reinterpret_cast<uint32_t>(&LPC_I2S0->RXFIFO);
|
|
|
|
const auto control_value = control_rx(transfer_bytes);
|
|
|
|
for (size_t i = 0; i < lli_rx_loop.size(); i++) {
|
|
|
|
const auto memory = reinterpret_cast<uint32_t>(&buffer_rx[i * transfer_samples]);
|
|
|
|
lli_rx_loop[i].srcaddr = peripheral;
|
|
|
|
lli_rx_loop[i].destaddr = memory;
|
|
|
|
lli_rx_loop[i].lli = lli_pointer(&lli_rx_loop[(i + 1) % lli_rx_loop.size()]);
|
|
|
|
lli_rx_loop[i].control = control_value;
|
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2024-03-13 11:07:44 -04:00
|
|
|
static void enable_tx() {
|
|
|
|
const auto gpdma_config_tx = config_tx();
|
|
|
|
gpdma_channel_i2s0_tx.configure(lli_tx_loop[0], gpdma_config_tx);
|
|
|
|
gpdma_channel_i2s0_tx.enable();
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2024-03-13 11:07:44 -04:00
|
|
|
static void enable_rx() {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto gpdma_config_rx = config_rx();
|
|
|
|
gpdma_channel_i2s0_rx.configure(lli_rx_loop[0], gpdma_config_rx);
|
|
|
|
gpdma_channel_i2s0_rx.enable();
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2024-03-13 11:07:44 -04:00
|
|
|
void init_audio_out() {
|
|
|
|
gpdma_channel_i2s0_tx.set_handlers(tx_transfer_complete, tx_error);
|
|
|
|
// LPC_GPDMA->SYNC |= (1 << gpdma_tx_peripheral);
|
|
|
|
configure_tx();
|
|
|
|
enable_tx();
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_audio_in() {
|
|
|
|
gpdma_channel_i2s0_rx.set_handlers(rx_transfer_complete, rx_error);
|
|
|
|
// LPC_GPDMA->SYNC |= (1 << gpdma_rx_peripheral);
|
|
|
|
configure_rx();
|
|
|
|
enable_rx();
|
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
void disable() {
|
2023-05-18 16:16:05 -04:00
|
|
|
gpdma_channel_i2s0_tx.disable();
|
|
|
|
gpdma_channel_i2s0_rx.disable();
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2024-01-23 14:53:04 -05:00
|
|
|
void shrink_tx_buffer(bool shrink) {
|
|
|
|
single_tx_buffer = shrink;
|
2024-03-13 11:07:44 -04:00
|
|
|
|
|
|
|
if (transfers_per_buffer == 1)
|
|
|
|
return;
|
|
|
|
|
2024-01-23 14:53:04 -05:00
|
|
|
if (single_tx_buffer)
|
|
|
|
lli_tx_loop[0].lli = lli_pointer(&lli_tx_loop[0]);
|
|
|
|
else
|
|
|
|
lli_tx_loop[0].lli = lli_pointer(&lli_tx_loop[1]);
|
2024-01-09 14:50:45 -05:00
|
|
|
}
|
|
|
|
|
2024-03-19 13:23:06 -04:00
|
|
|
void beep_start(uint32_t freq, uint32_t sample_rate, uint32_t beep_duration_ms) {
|
2024-03-19 16:30:05 -04:00
|
|
|
// Prevent divide-by-0
|
|
|
|
if (freq == 0 || sample_rate == 0)
|
|
|
|
return;
|
2024-03-19 05:53:55 -04:00
|
|
|
|
2024-03-19 16:30:05 -04:00
|
|
|
// Fill entire buffer with sine waves
|
|
|
|
tone_gen.configure_beep(freq, sample_rate);
|
2024-03-19 05:53:55 -04:00
|
|
|
for (size_t i = 0; i < buffer_samples; i++)
|
|
|
|
buffer_tx[i].left = buffer_tx[i].right = tone_gen.process_beep();
|
2024-03-19 13:23:06 -04:00
|
|
|
|
2024-03-19 16:30:05 -04:00
|
|
|
// Try to adjust DMA transfer count to align with full sine waves for a better tone
|
|
|
|
float samples_per_sine_wave = float(sample_rate) / freq;
|
|
|
|
uint32_t sine_waves_per_buffer = buffer_samples / samples_per_sine_wave;
|
|
|
|
size_t sample_count = (sine_waves_per_buffer == 0) ? buffer_samples : sine_waves_per_buffer * samples_per_sine_wave + 0.5;
|
|
|
|
|
|
|
|
// Use single larger transfer buffer with sample count determined above
|
|
|
|
lli_tx_loop[0].lli = lli_pointer(&lli_tx_loop[0]);
|
|
|
|
lli_tx_loop[0].control = control_tx(sample_count * sizeof(sample_t));
|
|
|
|
|
|
|
|
// Convert duration ms to number of buffers to send before stopping
|
|
|
|
// NB: beep_duration_ms==0 means beep continuously until stopped
|
|
|
|
uint32_t beep_interrupt_count = beep_duration_ms * sample_rate / (1000 * sample_count);
|
2024-03-19 13:23:06 -04:00
|
|
|
if ((beep_duration_ms != 0) && (beep_interrupt_count == 0))
|
|
|
|
beep_interrupt_count = 1;
|
|
|
|
beep_duration_downcounter = beep_interrupt_count;
|
2024-03-19 05:53:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void beep_stop() {
|
2024-03-19 16:30:05 -04:00
|
|
|
// Clear audio DMA buffer
|
2024-03-19 05:53:55 -04:00
|
|
|
memset(&buffer_tx, 0, buffer_bytes);
|
2024-03-19 16:30:05 -04:00
|
|
|
|
|
|
|
// Restore DMA linked list to use multiple smaller buffers
|
|
|
|
lli_tx_loop[0].control = control_tx(transfer_bytes);
|
|
|
|
if (!single_tx_buffer && (transfers_per_buffer > 1)) {
|
|
|
|
lli_tx_loop[0].lli = lli_pointer(&lli_tx_loop[1]);
|
|
|
|
}
|
2024-03-19 05:53:55 -04:00
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
buffer_t tx_empty_buffer() {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto next_lli = tx_next_lli;
|
|
|
|
if (next_lli) {
|
|
|
|
const size_t next_index = next_lli - &lli_tx_loop[0];
|
2024-01-09 14:50:45 -05:00
|
|
|
const size_t free_index = (single_tx_buffer) ? 0 : (next_index + transfers_per_buffer - 2) & transfers_mask;
|
2023-05-18 16:16:05 -04:00
|
|
|
return {reinterpret_cast<sample_t*>(lli_tx_loop[free_index].srcaddr), transfer_samples};
|
|
|
|
} else {
|
|
|
|
return {nullptr, 0};
|
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer_t rx_empty_buffer() {
|
2023-05-18 16:16:05 -04:00
|
|
|
const auto next_lli = rx_next_lli;
|
|
|
|
if (next_lli) {
|
|
|
|
const size_t next_index = next_lli - &lli_rx_loop[0];
|
|
|
|
const size_t free_index = (next_index + transfers_per_buffer - 2) & transfers_mask;
|
|
|
|
return {reinterpret_cast<sample_t*>(lli_rx_loop[free_index].destaddr), transfer_samples};
|
|
|
|
} else {
|
|
|
|
return {nullptr, 0};
|
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace dma */
|
|
|
|
} /* namespace audio */
|