Skip to content

Commit

Permalink
RP2: Add Audio support for Arduino Nano RP2040.
Browse files Browse the repository at this point in the history
  • Loading branch information
iabdalkader committed Sep 19, 2021
1 parent 9facc18 commit 8c1c590
Show file tree
Hide file tree
Showing 8 changed files with 1,501 additions and 0 deletions.
484 changes: 484 additions & 0 deletions ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/audio/modaudio.c

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/audio/pdm.pio
@@ -0,0 +1,54 @@
; This file is part of the MicroPython project, http://micropython.org/
;
; The MIT License (MIT)
;
; Copyright (c) 2013-2021 Ibrahim Abdelkader <iabdalkader@openmv.io>
;
; 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.

.program pdm_pio
.side_set 1
.wrap_target
push iffull noblock side 1
in pins, 1 side 0
.wrap

% c-sdk {
#include "hardware/gpio.h"

static inline void pdm_pio_program_init(PIO pio, uint sm, uint offset, uint clk, uint din, float div) {
pio_sm_config c = pdm_pio_program_get_default_config(offset);

sm_config_set_sideset(&c, 1, false, false);
sm_config_set_in_shift(&c, false, false, 8);

sm_config_set_in_pins(&c, din);
sm_config_set_sideset_pins(&c, clk);
sm_config_set_clkdiv(&c, div);

pio_sm_set_consecutive_pindirs(pio, sm, din, 1, false);
pio_sm_set_consecutive_pindirs(pio, sm, clk, 1, true);
pio_sm_set_pins_with_mask(pio, sm, 0, (1u << clk) );
pio_gpio_init(pio, clk);

pio_sm_init(pio, sm, offset, &c);
pio_sm_set_enabled(pio, sm, true);
}

%}
26 changes: 26 additions & 0 deletions ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/micropython.cmake
@@ -0,0 +1,26 @@
# Create an INTERFACE library for our C module.
add_library(nano_modules INTERFACE)

target_compile_definitions(nano_modules INTERFACE
MICROPY_PY_AUDIO=1
USE_LUT
)

# Add the current directory as an include directory.
target_include_directories(nano_modules INTERFACE
${CMAKE_CURRENT_LIST_DIR}/openpdm
)

# Add our source files to the lib
target_sources(nano_modules INTERFACE
${CMAKE_CURRENT_LIST_DIR}/audio/modaudio.c
${CMAKE_CURRENT_LIST_DIR}/openpdm/OpenPDMFilter.c
)

# PDM PIO program
pico_generate_pio_header(nano_modules
${CMAKE_CURRENT_LIST_DIR}/audio/pdm.pio
)

# Link our INTERFACE library to the usermod target.
target_link_libraries(usermod INTERFACE nano_modules)
Expand Up @@ -3,4 +3,5 @@ set(MICROPY_PY_BLUETOOTH 1)
set(MICROPY_BLUETOOTH_NIMBLE 1)
set(MICROPY_PY_NETWORK_NINAW10 1)
set(MICROPY_HW_ENABLE_DOUBLE_TAP 1)
set(USER_C_MODULES ${MICROPY_BOARD_DIR}/micropython.cmake)
set(MICROPY_FROZEN_MANIFEST ${MICROPY_BOARD_DIR}/manifest.py)
11 changes: 11 additions & 0 deletions ports/rp2/boards/ARDUINO_NANO_RP2040_CONNECT/mpconfigboard.h
Expand Up @@ -37,3 +37,14 @@
#define MICROPY_HW_NINA_GPIO0 (2)
#define MICROPY_HW_NINA_GPIO1 (9)
#define MICROPY_HW_NINA_ACK (10)

// AUDIO config.
#define MICROPY_HW_PDM_PIO (pio1)
#define MICROPY_HW_PDM_SM (0)
#define MICROPY_HW_PDM_DMA (1)
#define MICROPY_HW_PDM_DMA_IRQ (DMA_IRQ_1)

#define MICROPY_HW_PDM_CLK_PIN (23)
#define MICROPY_HW_PDM_DIN_PIN (22)

#define MICROPY_BOARD_ROOT_POINTERS struct _audio_data_t *audio_data;

0 comments on commit 8c1c590

Please sign in to comment.