-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 547aebe
Showing
16 changed files
with
2,975 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Auto detect text files and perform LF normalization | ||
* text=auto |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.idea | ||
.vscode | ||
_deps | ||
cmake-* | ||
build | ||
.DS_Store | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
cmake_minimum_required(VERSION 3.12) | ||
|
||
# Pull in PICO SDK (must be before project) | ||
include(pico_sdk_import.cmake) | ||
|
||
# We also need PICO EXTRAS | ||
include(pico_extras_import.cmake) | ||
|
||
project(pico_amp C CXX) | ||
set(CMAKE_C_STANDARD 11) | ||
set(CMAKE_CXX_STANDARD 17) | ||
|
||
# Initialize the Pico SDK | ||
pico_sdk_init() | ||
|
||
add_subdirectory(i2s) # ??? | ||
|
||
add_executable(pico_amp | ||
main.c | ||
) | ||
|
||
target_compile_definitions(pico_amp PRIVATE | ||
AUDIO_FREQ_MAX=48000 | ||
|
||
# ours are zero based, so say so | ||
PICO_USBDEV_USE_ZERO_BASED_INTERFACES=1 | ||
|
||
# need large descriptor | ||
PICO_USBDEV_MAX_DESCRIPTOR_SIZE=256 | ||
|
||
|
||
PICO_USBDEV_ISOCHRONOUS_BUFFER_STRIDE_TYPE=1 | ||
PICO_USBDEV_ENABLE_DEBUG_TRAgCE | ||
|
||
PICO_AUDIO_I2S_MONO_OUTPUT=0 | ||
PICO_AUDIO_I2S_MONO_INPUT=0 | ||
|
||
PICO_AUDIO_I2S_DATA_PIN=18 | ||
PICO_AUDIO_I2S_CLOCK_PIN_BASE=16 | ||
) | ||
|
||
target_link_libraries(pico_amp pico_stdlib usb_device pico_audio sctanf_i2s pico_multicore) # change the name so it doesnt try to use the original i2s stuffs | ||
pico_add_extra_outputs(pico_amp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
Copyright 2020 (c) 2020 Raspberry Pi (Trading) Ltd. | ||
|
||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the | ||
following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following | ||
disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following | ||
disclaimer in the documentation and/or other materials provided with the distribution. | ||
|
||
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products | ||
derived from this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, | ||
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | ||
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | ||
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
firmware for pico with usb audio, i2s, and dsp | ||
|
||
this is modified for lower latency, and has limited dsp functions (biquads and scuffed full wave integrator, not used) | ||
|
||
created for a project to power Valve Index speakers with a Pico/RP2040 and I2S amplifers (MAX98357/MAX98360) | ||
|
||
based off pico-playground usb_sound_card |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#ifndef SCTANFDSP_H | ||
#define SCTANFDSP_H | ||
|
||
#include <stdio.h> | ||
|
||
#include "pico/stdlib.h" | ||
|
||
typedef int32_t dspfx; | ||
|
||
#define fpformat 15 | ||
#define mulfx(a,b) ((dspfx)(((int64_t)(a)*(int64_t)(b))>>fpformat)) | ||
#define intfx(a) ((a)<<fpformat) | ||
#define fxint(a) ((a)>>fpformat) | ||
#define floatfx(a) ((dspfx)((a)*(1<<fpformat))) | ||
#define fxfloat(a) ((float)(a)/(1<<fpformat)) | ||
#define fxabs(a) ((a)<0?-(a):(a)) | ||
|
||
#define fpformat2 30 | ||
#define mulfx2(a,b) ((dspfx)(((int64_t)(a)*(int64_t)(b))>>fpformat2)) | ||
#define intfx2(a) ((a)<<fpformat2) | ||
#define fxint2(a) ((a)>>fpformat2) | ||
#define floatfx2(a) ((dspfx)((a)*(1<<fpformat2))) | ||
#define fxfloat2(a) ((float)(a)/(1<<fpformat2)) | ||
#define fxabs2(a) ((a)<0?-(a):(a)) | ||
|
||
#define fpformat3 28 | ||
#define mulfx0(a,b) ((int64_t)(a)*(int64_t)(b)) | ||
#define fxint3(a) ((a)>>fpformat3) | ||
#define mulshift(a) ((dspfx)fxint3(a)) | ||
#define floatfx3(a) ((int64_t)((a)*(1<<fpformat3))) | ||
|
||
typedef struct { | ||
const dspfx k; | ||
dspfx y1, z1, u1; | ||
dspfx y1r, z1r, u1r; | ||
} fwi; | ||
|
||
typedef struct { | ||
dspfx a1z, a2z, b1z, b2z; | ||
dspfx a1zr, a2zr, b1zr, b2zr; | ||
} biquad; | ||
|
||
#define fwi(a,_k) fwi a = {.k=floatfx(_k),.y1=0,.z1=0,.u1=0,.y1r=0,.z1r=0,.u1r=0,}; | ||
#define biquad(a) biquad a = {.a1z=0,.a2z=0,.b1z=0,.b2z=0,.a1zr=0,.a2zr=0,.b1zr=0,.b2zr=0}; | ||
#define biquadconstfx(a,b,c,d,e) floatfx3(a),floatfx3(b),floatfx3(c),floatfx3(d),floatfx3(e) | ||
#define biquadconstsfx(a) biquadconstfx(a) | ||
|
||
static inline void process_fwi(fwi *const filter, int16_t iters, int32_t *in, int32_t *out) { | ||
int16_t iters2 = iters * 2; | ||
out[0] = filter->z1 + mulfx(filter->k, filter->u1); | ||
out[1] = filter->z1r + mulfx(filter->k, filter->u1r); | ||
for (int i = 2; i < iters2; i++) { | ||
out[i] = ((in[i] > 0) && (in[i - 2] <= 0)) ? 0 : out[i - 2] + mulfx(filter->k, fxabs(in[i - 2])); | ||
} | ||
filter->z1 = out[iters2 - 2]; | ||
filter->u1 = fxabs(in[iters2 - 2]); | ||
filter->z1r = out[iters2 - 1]; | ||
filter->u1r = fxabs(in[iters2 - 1]); | ||
} | ||
|
||
static inline void process_biquad(biquad *const filter, int64_t a0, int64_t a1, int64_t a2, int64_t b1, int64_t b2, int16_t iters, int32_t *in, int32_t *out) { | ||
int16_t iters2 = iters * 2; | ||
out[0] = mulshift(mulfx0(a0, in[0]) + mulfx0(a1, filter->a1z) + mulfx0(a2, filter->a2z) - mulfx0(b1, filter->b1z) - mulfx0(b2, filter->b2z)); | ||
out[2] = mulshift(mulfx0(a0, in[2]) + mulfx0(a1, in[0]) + mulfx0(a2, filter->a1z) - mulfx0(b1, out[0]) - mulfx0(b2, filter->b1z)); | ||
out[1] = mulshift(mulfx0(a0, in[1]) + mulfx0(a1, filter->a1zr) + mulfx0(a2, filter->a2zr) - mulfx0(b1, filter->b1zr) - mulfx0(b2, filter->b2zr)); | ||
out[3] = mulshift(mulfx0(a0, in[3]) + mulfx0(a1, in[1]) + mulfx0(a2, filter->a1zr) - mulfx0(b1, out[1]) - mulfx0(b2, filter->b1zr)); | ||
for (int i = 4; i < iters2; i++) { | ||
out[i] = mulshift(mulfx0(a0, in[i]) + mulfx0(a1, in[i - 2]) + mulfx0(a2, in[i - 4]) - mulfx0(b1, out[i - 2]) - mulfx0(b2, out[i - 4])); // takes up the most time by far.. | ||
} | ||
filter->a2z = in[iters2 - 4]; | ||
filter->b2z = out[iters2 - 4]; | ||
filter->a1z = in[iters2 - 2]; | ||
filter->b1z = out[iters2 - 2]; | ||
filter->a2zr = in[iters2 - 3]; | ||
filter->b2zr = out[iters2 - 3]; | ||
filter->a1zr = in[iters2 - 1]; | ||
filter->b1zr = out[iters2 - 1]; | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
if (NOT TARGET sctanf_i2s) # change the name so it doesnt try to use the original i2s stuffs | ||
add_library(sctanf_i2s INTERFACE) | ||
|
||
pico_generate_pio_header(sctanf_i2s ${CMAKE_CURRENT_LIST_DIR}/audio_i2s.pio) | ||
|
||
target_sources(sctanf_i2s INTERFACE | ||
${CMAKE_CURRENT_LIST_DIR}/audio_i2s.c | ||
) | ||
|
||
target_include_directories(sctanf_i2s INTERFACE ${CMAKE_CURRENT_LIST_DIR}/include) | ||
target_link_libraries(sctanf_i2s INTERFACE hardware_dma hardware_pio hardware_irq pico_audio) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
/* | ||
* Copyright (c) 2020 Raspberry Pi (Trading) Ltd. | ||
* | ||
* SPDX-License-Identifier: BSD-3-Clause | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
||
#include "pico/stdlib.h" | ||
#include "pico/audio_i2s.h" | ||
#include "audio_i2s.pio.h" | ||
#include "hardware/pio.h" | ||
#include "hardware/gpio.h" | ||
#include "hardware/dma.h" | ||
#include "hardware/irq.h" | ||
#include "hardware/clocks.h" | ||
|
||
|
||
CU_REGISTER_DEBUG_PINS(audio_timing) | ||
|
||
// ---- select at most one --- | ||
//CU_SELECT_DEBUG_PINS(audio_timing) | ||
|
||
|
||
#if PICO_AUDIO_I2S_MONO_OUTPUT | ||
#define i2s_dma_configure_size DMA_SIZE_16 | ||
#else | ||
#define i2s_dma_configure_size DMA_SIZE_32 | ||
#endif | ||
|
||
#define audio_pio __CONCAT(pio, PICO_AUDIO_I2S_PIO) | ||
#define GPIO_FUNC_PIOx __CONCAT(GPIO_FUNC_PIO, PICO_AUDIO_I2S_PIO) | ||
#define DREQ_PIOx_TX0 __CONCAT(__CONCAT(DREQ_PIO, PICO_AUDIO_I2S_PIO), _TX0) | ||
|
||
struct { | ||
audio_buffer_t *playing_buffer; | ||
uint32_t freq; | ||
uint8_t pio_sm; | ||
uint8_t dma_channel; | ||
} shared_state; | ||
|
||
bufring_t *bufring2; | ||
|
||
void audioi2sconstuff(bufring_t *bufring1) { | ||
bufring2=bufring1; | ||
} | ||
|
||
uint32_t buflends[8192]; | ||
void audioi2sconstuff2() { | ||
uint32_t divider = (270000000 * 2 / 48000) - ((bufring2->len - 16)/2); | ||
buflends[bufring2->index1] = divider; | ||
pio_sm_set_clkdiv_int_frac(audio_pio, shared_state.pio_sm, divider >> 8u, divider & 0xffu); | ||
} | ||
|
||
static void __isr __time_critical_func(audio_i2s_dma_irq_handler)(); | ||
|
||
const audio_format_t *audio_i2s_setup(const audio_format_t *intended_audio_format, | ||
const audio_i2s_config_t *config) { | ||
uint func = GPIO_FUNC_PIOx; | ||
gpio_set_function(config->data_pin, func); | ||
gpio_set_function(config->clock_pin_base, func); | ||
gpio_set_function(config->clock_pin_base + 1, func); | ||
|
||
uint8_t sm = shared_state.pio_sm = config->pio_sm; | ||
pio_sm_claim(audio_pio, sm); | ||
|
||
uint offset = pio_add_program(audio_pio, &audio_i2s_program); | ||
|
||
audio_i2s_program_init(audio_pio, sm, offset, config->data_pin, config->clock_pin_base); | ||
|
||
__mem_fence_release(); | ||
uint8_t dma_channel = config->dma_channel; | ||
dma_channel_claim(dma_channel); | ||
|
||
shared_state.dma_channel = dma_channel; | ||
|
||
dma_channel_config dma_config = dma_channel_get_default_config(dma_channel); | ||
|
||
channel_config_set_dreq(&dma_config, | ||
DREQ_PIOx_TX0 + sm | ||
); | ||
channel_config_set_transfer_data_size(&dma_config, i2s_dma_configure_size); | ||
dma_channel_configure(dma_channel, | ||
&dma_config, | ||
&audio_pio->txf[sm], // dest | ||
NULL, // src | ||
0, // count | ||
false // trigger | ||
); | ||
|
||
irq_add_shared_handler(DMA_IRQ_0 + PICO_AUDIO_I2S_DMA_IRQ, audio_i2s_dma_irq_handler, PICO_SHARED_IRQ_HANDLER_DEFAULT_ORDER_PRIORITY); | ||
dma_irqn_set_channel_enabled(PICO_AUDIO_I2S_DMA_IRQ, dma_channel, 1); | ||
return intended_audio_format; | ||
} | ||
|
||
static void update_pio_frequency(uint32_t sample_freq) { | ||
uint32_t system_clock_frequency = clock_get_hz(clk_sys); | ||
assert(system_clock_frequency < 0x40000000); | ||
uint32_t divider = system_clock_frequency * 2 / sample_freq; // avoid arithmetic overflow | ||
assert(divider < 0x1000000); | ||
pio_sm_set_clkdiv_int_frac(audio_pio, shared_state.pio_sm, divider >> 8u, divider & 0xffu); | ||
shared_state.freq = sample_freq; | ||
} | ||
|
||
static inline void audio_start_dma_transfer() { | ||
while(bufring2->corelock == 1) {tight_loop_contents();} | ||
bufring2->corelock = 2; | ||
if (bufring2->len < 2) { | ||
bufring2->corelock = 0; | ||
// just play some silence | ||
static uint32_t zero; | ||
dma_channel_config c = dma_get_channel_config(shared_state.dma_channel); | ||
channel_config_set_read_increment(&c, false); | ||
dma_channel_set_config(shared_state.dma_channel, &c, false); | ||
dma_channel_transfer_from_buffer_now(shared_state.dma_channel, &zero, 2); | ||
return; | ||
} | ||
dma_channel_config c = dma_get_channel_config(shared_state.dma_channel); | ||
channel_config_set_read_increment(&c, true); | ||
dma_channel_set_config(shared_state.dma_channel, &c, false); | ||
dma_channel_transfer_from_buffer_now(shared_state.dma_channel, bufring2->buf+bufring2->index1, 2); | ||
buflends[bufring2->index1+1] = bufring2->len; | ||
bufring2->len = bufring2->len - 2; | ||
bufring2->index1 = (bufring2->index1 + 2) % (1024-32); | ||
bufring2->corelock = 0; | ||
return; | ||
} | ||
|
||
// irq handler for DMA | ||
void __isr __time_critical_func(audio_i2s_dma_irq_handler)() { | ||
#if PICO_AUDIO_I2S_NOOP | ||
assert(false); | ||
#else | ||
uint dma_channel = shared_state.dma_channel; | ||
if (dma_irqn_get_channel_status(PICO_AUDIO_I2S_DMA_IRQ, dma_channel)) { | ||
dma_irqn_acknowledge_channel(PICO_AUDIO_I2S_DMA_IRQ, dma_channel); | ||
audio_start_dma_transfer(); | ||
} | ||
#endif | ||
} | ||
|
||
static bool audio_enabled; | ||
|
||
void audio_i2s_set_enabled(bool enabled) { | ||
if (enabled != audio_enabled) { | ||
#ifndef NDEBUG | ||
if (enabled) | ||
{ | ||
puts("Enabling PIO I2S audio\n"); | ||
printf("(on core %d\n", get_core_num()); | ||
update_pio_frequency(48000); | ||
} | ||
#endif | ||
irq_set_enabled(DMA_IRQ_0 + PICO_AUDIO_I2S_DMA_IRQ, enabled); | ||
|
||
if (enabled) { | ||
audio_start_dma_transfer(); | ||
} | ||
|
||
pio_sm_set_enabled(audio_pio, shared_state.pio_sm, enabled); | ||
|
||
audio_enabled = enabled; | ||
} | ||
} |
Oops, something went wrong.