Skip to content

Commit

Permalink
Change buffers to int32_t to accomodate signed data from I2S
Browse files Browse the repository at this point in the history
  • Loading branch information
malacalypse committed Jul 16, 2022
1 parent f52db6d commit 65efd29
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.DS_Store
.idea
cmake-*
build/
30 changes: 21 additions & 9 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,40 @@ 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)

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)
Expand Down
8 changes: 4 additions & 4 deletions i2s.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions i2s_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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 {
Expand Down

0 comments on commit 65efd29

Please sign in to comment.