From 65efd29a8759169477c2211f5ba1b56b2a8fc7b6 Mon Sep 17 00:00:00 2001 From: Malacalypse Date: Sat, 16 Jul 2022 22:10:38 +0000 Subject: [PATCH] Change buffers to int32_t to accomodate signed data from I2S --- .gitignore | 4 ++++ CMakeLists.txt | 30 +++++++++++++++++++++--------- i2s.h | 8 ++++---- i2s_example.c | 4 ++-- 4 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9fe80ba --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +.idea +cmake-* +build/ diff --git a/CMakeLists.txt b/CMakeLists.txt index c3bd7ed..dc183d4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,20 +22,32 @@ cmake_minimum_required(VERSION 3.13) set(CMAKE_C_STANDARD 11) set(CMAKE_CXX_STANDARD 17) -# Initialise pico_sdk from installed location -# (note this can come from environment, CMake cache etc) -set(PICO_SDK_PATH "/Users/studiodc/Development/raspi/pico/sdk") + +# Set standard CMake build type options +## Default C compiler flags +set(CMAKE_C_FLAGS_DEBUG_INIT "-g3 -Og -Wall -Wextra -DDEBUG") +set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG_INIT}" CACHE STRING "" FORCE) +set(CMAKE_C_FLAGS_RELEASE_INIT "-O3 -Wall") +set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE_INIT}" CACHE STRING "" FORCE) +set(CMAKE_C_FLAGS_MINSIZEREL_INIT "-Os -Wall") +set(CMAKE_C_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL_INIT}" CACHE STRING "" FORCE) +set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "-O2 -g -Wall") +set(CMAKE_C_FLAGS_RELWITHDEBINFO "${CMAKE_ASM_FLAGS_RELWITHDEBINFO_INIT}" CACHE STRING "" FORCE) +## Default C++ compiler flags +set(CMAKE_CXX_FLAGS_DEBUG_INIT "-g3 -Og -Wall -Wextra -DDEBUG") +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG_INIT}" CACHE STRING "" FORCE) +set(CMAKE_CXX_FLAGS_RELEASE_INIT "-O3 -Wall") +set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE_INIT}" CACHE STRING "" FORCE) +set(CMAKE_CXX_FLAGS_MINSIZEREL_INIT "-Os -Wall") +set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_C_FLAGS_MINSIZEREL_INIT}" CACHE STRING "" FORCE) +set(CMAKE_CXX_FLAGS_RELWITHDEBINFO_INIT "-O2 -g -Wall") +set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_ASM_FLAGS_RELWITHDEBINFO_INIT}" CACHE STRING "" FORCE) # Pull in Raspberry Pi Pico SDK (must be before project) include(pico_sdk_import.cmake) - project(i2s_example C CXX ASM) - -# Initialise the Raspberry Pi Pico SDK pico_sdk_init() -# Add executable. Default name is the project name, version 0.1 - add_executable(i2s_example i2s.c) pico_generate_pio_header(i2s_example ${CMAKE_CURRENT_LIST_DIR}/i2s.pio) @@ -43,7 +55,7 @@ pico_generate_pio_header(i2s_example ${CMAKE_CURRENT_LIST_DIR}/i2s.pio) target_sources(i2s_example PRIVATE i2s_example.c) pico_set_program_name(i2s_example "i2s_test") -pico_set_program_version(i2s_example "0.1") +pico_set_program_version(i2s_example "1.0.1") # no_flash means the target is to run from RAM # pico_set_binary_type(i2s_test no_flash) diff --git a/i2s.h b/i2s.h index d9f0405..059e318 100644 --- a/i2s.h +++ b/i2s.h @@ -63,10 +63,10 @@ typedef struct pio_i2s { uint dma_ch_in_data; uint dma_ch_out_ctrl; uint dma_ch_out_data; - uint32_t* in_ctrl_blocks[2]; // Control blocks MUST have 8-byte alignment. - uint32_t* out_ctrl_blocks[2]; - uint32_t input_buffer[STEREO_BUFFER_SIZE * 2]; - uint32_t output_buffer[STEREO_BUFFER_SIZE * 2]; + int32_t* in_ctrl_blocks[2]; // Control blocks MUST have 8-byte alignment. + int32_t* out_ctrl_blocks[2]; + int32_t input_buffer[STEREO_BUFFER_SIZE * 2]; + int32_t output_buffer[STEREO_BUFFER_SIZE * 2]; i2s_config config; } pio_i2s; diff --git a/i2s_example.c b/i2s_example.c index 1b35ae6..3b9505c 100644 --- a/i2s_example.c +++ b/i2s_example.c @@ -47,7 +47,7 @@ const uint LED_PIN = PICO_DEFAULT_LED_PIN; static __attribute__((aligned(8))) pio_i2s i2s; -static void process_audio(const uint32_t* input, uint32_t* output, size_t num_frames) { +static void process_audio(const int32_t* input, int32_t* output, size_t num_frames) { // Just copy the input to the output for (size_t i = 0; i < num_frames * 2; i++) { output[i] = input[i]; @@ -59,7 +59,7 @@ static void dma_i2s_in_handler(void) { * DMA is currently reading from, we can identify which buffer it has just * finished reading (the completion of which has triggered this interrupt). */ - if (*(uint32_t**)dma_hw->ch[i2s.dma_ch_in_ctrl].read_addr == i2s.input_buffer) { + if (*(int32_t**)dma_hw->ch[i2s.dma_ch_in_ctrl].read_addr == i2s.input_buffer) { // It is inputting to the second buffer so we can overwrite the first process_audio(i2s.input_buffer, i2s.output_buffer, AUDIO_BUFFER_FRAMES); } else {