You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by ac...@apache.org on 2022/09/30 20:23:25 UTC
[incubator-nuttx] 02/03: xtensa/esp32: add support for the CS4344 audio codec
This is an automated email from the ASF dual-hosted git repository.
acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git
commit 06bc2220b90224714a02787e1de91493bdb796a6
Author: Tiago Medicci Serrano <ti...@espressif.com>
AuthorDate: Wed Sep 28 12:20:45 2022 -0300
xtensa/esp32: add support for the CS4344 audio codec
Include i2schar and audio drivers defconfigs for ESP32-DevKitC board.
---
boards/xtensa/esp32/common/src/Make.defs | 4 +
.../xtensa/esp32/common/src/esp32_board_i2sdev.c | 5 +
boards/xtensa/esp32/common/src/esp32_cs4344.c | 166 +++++++++++++++++++++
.../esp32/esp32-devkitc/configs/audio/defconfig | 128 ++++++++++++++++
.../xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h | 24 ++-
.../xtensa/esp32/esp32-devkitc/src/esp32_bringup.c | 14 ++
6 files changed, 340 insertions(+), 1 deletion(-)
diff --git a/boards/xtensa/esp32/common/src/Make.defs b/boards/xtensa/esp32/common/src/Make.defs
index fdfde999db..84b6e11c97 100644
--- a/boards/xtensa/esp32/common/src/Make.defs
+++ b/boards/xtensa/esp32/common/src/Make.defs
@@ -36,6 +36,10 @@ ifeq ($(CONFIG_ESP32_I2S),y)
CSRCS += esp32_board_i2sdev.c
endif
+ifeq ($(CONFIG_AUDIO_CS4344),y)
+ CSRCS += esp32_cs4344.c
+endif
+
ifeq ($(CONFIG_I2CMULTIPLEXER_TCA9548A),y)
CSRCS += esp32_tca9548a.c
endif
diff --git a/boards/xtensa/esp32/common/src/esp32_board_i2sdev.c b/boards/xtensa/esp32/common/src/esp32_board_i2sdev.c
index 4ac9555a9b..321255af4c 100644
--- a/boards/xtensa/esp32/common/src/esp32_board_i2sdev.c
+++ b/boards/xtensa/esp32/common/src/esp32_board_i2sdev.c
@@ -37,6 +37,9 @@
#include "esp32_i2s.h"
+#if defined CONFIG_ESP32_I2S0 && !defined CONFIG_AUDIO_CS4344 || \
+ defined CONFIG_ESP32_I2S1
+
/****************************************************************************
* Public Functions
****************************************************************************/
@@ -107,3 +110,5 @@ int board_i2sdev_initialize(int port)
return ret;
}
+
+#endif /* CONFIG_ESP32_I2S0 && !CONFIG_AUDIO_CS4344 || CONFIG_ESP32_I2S1 */
diff --git a/boards/xtensa/esp32/common/src/esp32_cs4344.c b/boards/xtensa/esp32/common/src/esp32_cs4344.c
new file mode 100644
index 0000000000..76487cafe2
--- /dev/null
+++ b/boards/xtensa/esp32/common/src/esp32_cs4344.c
@@ -0,0 +1,166 @@
+/****************************************************************************
+ * boards/xtensa/esp32/common/src/esp32_cs4344.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership. The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <stdbool.h>
+#include <stdio.h>
+#include <debug.h>
+#include <assert.h>
+#include <errno.h>
+
+#include <nuttx/irq.h>
+#include <nuttx/audio/i2s.h>
+#include <nuttx/audio/pcm.h>
+#include <nuttx/audio/cs4344.h>
+
+#include <arch/board/board.h>
+
+#include "esp32_i2s.h"
+
+#if defined CONFIG_ESP32_I2S && defined CONFIG_AUDIO_CS4344
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: esp32_cs4344_initialize
+ *
+ * Description:
+ * This function is called by platform-specific, setup logic to configure
+ * and register the CS4344 device. This function will register the driver
+ * as /dev/audio/pcm[x] where x is determined by the I2S port number.
+ *
+ * Input Parameters:
+ * port - The I2S port used for the device
+ *
+ * Returned Value:
+ * Zero is returned on success. Otherwise, a negated errno value is
+ * returned to indicate the nature of the failure.
+ *
+ ****************************************************************************/
+
+int esp32_cs4344_initialize(int port)
+{
+ struct audio_lowerhalf_s *cs4344;
+ struct audio_lowerhalf_s *pcm;
+ struct i2s_dev_s *i2s;
+ static bool initialized = false;
+ char devname[12];
+ int ret;
+
+ audinfo("port %d\n", port);
+ DEBUGASSERT(port >= 0 && port <= 1);
+
+ /* Have we already initialized? Since we never uninitialize we must
+ * prevent multiple initializations. This is necessary, for example,
+ * when the touchscreen example is used as a built-in application in
+ * NSH and can be called numerous time. It will attempt to initialize
+ * each time.
+ */
+
+ if (!initialized)
+ {
+ /* Get an instance of the I2S interface for the CS4344 data channel */
+
+ i2s = esp32_i2sbus_initialize(port);
+ if (!i2s)
+ {
+ auderr("ERROR: Failed to initialize I2S%d\n", port);
+ ret = -ENODEV;
+ goto errout;
+ }
+
+ /* Check wheter to enable a simple character driver that supports I2S
+ * transfers via a read() and write(). The intent of this driver is to
+ * support I2S testing. It is not an audio driver but does conform to
+ * some of the buffer management heuristics of an audio driver. It is
+ * not suitable for use in any real driver application in its current
+ * form. The i2schar driver will be initialized at /dev/i2schar0
+ */
+
+#ifdef CONFIG_AUDIO_I2SCHAR
+ ret = i2schar_register(i2s, 0);
+ if (ret < 0)
+ {
+ auderr("ERROR: i2schar_register failed: %d\n", ret);
+ goto errout;
+ }
+#endif
+
+ /* Now we can use this I2S interface to initialize the CS4344 which
+ * will return an audio interface.
+ */
+
+ cs4344 = cs4344_initialize(i2s);
+ if (!cs4344)
+ {
+ auderr("ERROR: Failed to initialize the CS4344\n");
+ ret = -ENODEV;
+ goto errout;
+ }
+
+ /* No we can embed the CS4344/I2S conglomerate into a PCM decoder
+ * instance so that we will have a PCM front end for the the CS4344
+ * driver.
+ */
+
+ pcm = pcm_decode_initialize(cs4344);
+ if (!pcm)
+ {
+ auderr("ERROR: Failed create the PCM decoder\n");
+ ret = -ENODEV;
+ goto errout;
+ }
+
+ /* Create a device name */
+
+ snprintf(devname, 12, "pcm%d", port);
+
+ /* Finally, we can register the PCM/CS4344/I2S audio device.
+ *
+ * Is anyone young enough to remember Rube Goldberg?
+ */
+
+ ret = audio_register(devname, pcm);
+ if (ret < 0)
+ {
+ auderr("ERROR: Failed to register /dev/%s device: %d\n",
+ devname, ret);
+ goto errout;
+ }
+
+ /* Now we are initialized */
+
+ initialized = true;
+ }
+
+ return OK;
+
+errout:
+ return ret;
+}
+
+#endif /* CONFIG_ESP32_I2S && CONFIG_AUDIO_CS4344 */
diff --git a/boards/xtensa/esp32/esp32-devkitc/configs/audio/defconfig b/boards/xtensa/esp32/esp32-devkitc/configs/audio/defconfig
new file mode 100644
index 0000000000..309735832b
--- /dev/null
+++ b/boards/xtensa/esp32/esp32-devkitc/configs/audio/defconfig
@@ -0,0 +1,128 @@
+#
+# This file is autogenerated: PLEASE DO NOT EDIT IT.
+#
+# You can use "make menuconfig" to make any modifications to the installed .config file.
+# You can then do "make savedefconfig" to generate a new defconfig file that includes your
+# modifications.
+#
+# CONFIG_ARCH_LEDS is not set
+# CONFIG_ESP32_I2S0_RX is not set
+# CONFIG_NDEBUG is not set
+# CONFIG_NSH_ARGCAT is not set
+# CONFIG_NSH_CMDOPT_HEXDUMP is not set
+# CONFIG_NSH_CMDPARMS is not set
+CONFIG_ALLOW_BSD_COMPONENTS=y
+CONFIG_ARCH="xtensa"
+CONFIG_ARCH_BOARD="esp32-devkitc"
+CONFIG_ARCH_BOARD_COMMON=y
+CONFIG_ARCH_BOARD_ESP32_DEVKITC=y
+CONFIG_ARCH_CHIP="esp32"
+CONFIG_ARCH_CHIP_ESP32=y
+CONFIG_ARCH_CHIP_ESP32WROVER=y
+CONFIG_ARCH_INTERRUPTSTACK=4096
+CONFIG_ARCH_STACKDUMP=y
+CONFIG_ARCH_XTENSA=y
+CONFIG_AUDIO=y
+CONFIG_AUDIOUTILS_MMLPARSER_LIB=y
+CONFIG_AUDIO_CS4344=y
+CONFIG_AUDIO_EXCLUDE_BALANCE=y
+CONFIG_AUDIO_EXCLUDE_FFORWARD=y
+CONFIG_AUDIO_EXCLUDE_TONE=y
+CONFIG_AUDIO_EXCLUDE_VOLUME=y
+CONFIG_AUDIO_I2S=y
+CONFIG_AUDIO_I2SCHAR=y
+CONFIG_AUDIO_NUM_BUFFERS=4
+CONFIG_BOARDCTL_ROMDISK=y
+CONFIG_BOARD_LOOPSPERMSEC=16717
+CONFIG_BUILTIN=y
+CONFIG_CS4344_BUFFER_SIZE=2048
+CONFIG_CS4344_INFLIGHT=4
+CONFIG_CS4344_NUM_BUFFERS=2
+CONFIG_CS4344_WORKER_STACKSIZE=4096
+CONFIG_DEBUG_NOOPT=y
+CONFIG_DEFAULT_TASK_STACKSIZE=4096
+CONFIG_DEV_URANDOM=y
+CONFIG_DRIVERS_AUDIO=y
+CONFIG_DRIVERS_IEEE80211=y
+CONFIG_DRIVERS_WIRELESS=y
+CONFIG_ESP32_I2S0=y
+CONFIG_ESP32_I2S0_DATA_BIT_WIDTH_16BIT=y
+CONFIG_ESP32_I2S0_MCLK=y
+CONFIG_ESP32_I2S=y
+CONFIG_ESP32_SPIFLASH=y
+CONFIG_ESP32_SPIFLASH_SPIFFS=y
+CONFIG_ESP32_STORAGE_MTD_SIZE=0x80000
+CONFIG_ESP32_UART0=y
+CONFIG_ESP32_WIFI=y
+CONFIG_ESP32_WIFI_SAVE_PARAM=y
+CONFIG_EXAMPLES_I2SCHAR=y
+CONFIG_EXAMPLES_I2SCHAR_TX=y
+CONFIG_EXAMPLES_I2SCHAR_TXBUFFERS=2
+CONFIG_EXAMPLES_I2SCHAR_TXSTACKSIZE=2048
+CONFIG_EXAMPLES_ROMFS=y
+CONFIG_FS_PROCFS=y
+CONFIG_FS_ROMFS=y
+CONFIG_HAVE_CXX=y
+CONFIG_HAVE_CXXINITIALIZE=y
+CONFIG_I2S_DMADESC_NUM=4
+CONFIG_IDLETHREAD_STACKSIZE=3072
+CONFIG_INIT_ENTRYPOINT="nsh_main"
+CONFIG_INTELHEX_BINARY=y
+CONFIG_IOB_NBUFFERS=24
+CONFIG_IOB_THROTTLE=0
+CONFIG_MM_CIRCBUF=y
+CONFIG_MM_REGIONS=3
+CONFIG_NAME_MAX=48
+CONFIG_NETDB_DNSCLIENT=y
+CONFIG_NETDB_DNSCLIENT_NAMESIZE=64
+CONFIG_NETDEVICES=y
+CONFIG_NETDEV_LATEINIT=y
+CONFIG_NETDEV_PHY_IOCTL=y
+CONFIG_NETDEV_WIRELESS_IOCTL=y
+CONFIG_NETINIT_WAPI_ALG=1
+CONFIG_NETUTILS_IPERF=y
+CONFIG_NETUTILS_TELNETD=y
+CONFIG_NET_BROADCAST=y
+CONFIG_NET_ETH_PKTSIZE=1518
+CONFIG_NET_ICMP=y
+CONFIG_NET_ICMP_SOCKET=y
+CONFIG_NET_NACTIVESOCKETS=32
+CONFIG_NET_STATISTICS=y
+CONFIG_NET_TCP=y
+CONFIG_NET_TCP_DELAYED_ACK=y
+CONFIG_NET_TCP_WRITE_BUFFERS=y
+CONFIG_NET_UDP=y
+CONFIG_NSH_ARCHINIT=y
+CONFIG_NSH_BUILTIN_APPS=y
+CONFIG_NSH_FILEIOSIZE=512
+CONFIG_NSH_LINELEN=300
+CONFIG_NSH_READLINE=y
+CONFIG_NXPLAYER_HTTP_STREAMING_SUPPORT=y
+CONFIG_NXPLAYER_PLAYTHREAD_STACKSIZE=4096
+CONFIG_PREALLOC_TIMERS=4
+CONFIG_PTHREAD_MUTEX_TYPES=y
+CONFIG_PTHREAD_STACK_DEFAULT=2048
+CONFIG_RAM_SIZE=114688
+CONFIG_RAM_START=0x20000000
+CONFIG_RR_INTERVAL=200
+CONFIG_SCHED_HPWORK=y
+CONFIG_SCHED_HPWORKSTACKSIZE=2048
+CONFIG_SCHED_LPWORK=y
+CONFIG_SCHED_WAITPID=y
+CONFIG_SIG_DEFAULT=y
+CONFIG_SPI=y
+CONFIG_SPIFFS_NAME_MAX=48
+CONFIG_START_DAY=6
+CONFIG_START_MONTH=12
+CONFIG_START_YEAR=2011
+CONFIG_SYSTEM_DHCPC_RENEW=y
+CONFIG_SYSTEM_NSH=y
+CONFIG_SYSTEM_NXPLAYER=y
+CONFIG_SYSTEM_PING=y
+CONFIG_TASK_SPAWN_DEFAULT_STACKSIZE=2048
+CONFIG_TELNET_CHARACTER_MODE=y
+CONFIG_TLS_TASK_NELEM=4
+CONFIG_UART0_SERIAL_CONSOLE=y
+CONFIG_WIRELESS=y
+CONFIG_WIRELESS_WAPI=y
+CONFIG_WIRELESS_WAPI_CMDTOOL=y
diff --git a/boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h b/boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h
index 6f64f0d3d5..d173179f5a 100644
--- a/boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h
+++ b/boards/xtensa/esp32/esp32-devkitc/src/esp32-devkitc.h
@@ -169,9 +169,31 @@ int esp32_twai_setup(void);
*
****************************************************************************/
-#if defined CONFIG_ESP32_I2S0 || defined CONFIG_ESP32_I2S1
+#if defined CONFIG_ESP32_I2S0 && !defined CONFIG_AUDIO_CS4344 || \
+ defined CONFIG_ESP32_I2S1
int board_i2sdev_initialize(int port);
#endif
+/****************************************************************************
+ * Name: esp32_cs4344_initialize
+ *
+ * Description:
+ * This function is called by platform-specific, setup logic to configure
+ * and register the CS4344 device. This function will register the driver
+ * as /dev/audio/pcm[x] where x is determined by the I2S port number.
+ *
+ * Input Parameters:
+ * port - The I2S port used for the device
+ *
+ * Returned Value:
+ * Zero is returned on success. Otherwise, a negated errno value is
+ * returned to indicate the nature of the failure.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_AUDIO_CS4344
+int esp32_cs4344_initialize(int port);
+#endif
+
#endif /* __ASSEMBLY__ */
#endif /* __BOARDS_XTENSA_ESP32_ESP32_DEVKITC_SRC_ESP32_DEVKITC_H */
diff --git a/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c b/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c
index a5849946b4..2566b5af0e 100644
--- a/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c
+++ b/boards/xtensa/esp32/esp32-devkitc/src/esp32_bringup.c
@@ -461,6 +461,19 @@ int esp32_bringup(void)
#ifdef CONFIG_ESP32_I2S0
+ /* Configure I2S0 */
+
+#ifdef CONFIG_AUDIO_CS4344
+
+ /* Configure CS4344 audio on I2S0 */
+
+ ret = esp32_cs4344_initialize(ESP32_I2S0);
+ if (ret != OK)
+ {
+ syslog(LOG_ERR, "Failed to initialize CS4344 audio: %d\n", ret);
+ }
+#else
+
/* Configure I2S generic audio on I2S0 */
ret = board_i2sdev_initialize(ESP32_I2S0);
@@ -469,6 +482,7 @@ int esp32_bringup(void)
syslog(LOG_ERR, "Failed to initialize I2S%d driver: %d\n",
CONFIG_ESP32_I2S0, ret);
}
+#endif /* CONFIG_AUDIO_CS4344 */
#endif /* CONFIG_ESP32_I2S0 */