xtensa/esp32: add initial support for I2S

Add initial support for the I2S peripheral on ESP32.
Add I2S character driver and generic I2S audio driver.
Include i2schar defconfig for ESP32-DevKitC board.
This commit is contained in:
Tiago Medicci Serrano 2022-09-28 15:01:53 -03:00 committed by Alan Carvalho de Assis
parent d906c2ff3b
commit 18c715ba92
12 changed files with 4368 additions and 29 deletions

View File

@ -276,20 +276,6 @@ config ESP32_TWAI
default n default n
select CAN select CAN
config ESP32_I2S0
bool "I2S 0"
default n
depends on EXPERIMENTAL
---help---
No yet implemented
config ESP32_I2S1
bool "I2S 2"
default n
depends on EXPERIMENTAL
---help---
No yet implemented
config ESP32_LEDC config ESP32_LEDC
bool "LEDC (PWM)" bool "LEDC (PWM)"
default n default n
@ -333,6 +319,283 @@ config ESP32_SDMMC
---help--- ---help---
No yet implemented No yet implemented
config ESP32_I2S
bool "I2S"
select I2S
---help---
See the Board Selection menu to configure the pins used by I2S.
if ESP32_I2S
config ESP32_I2S0
bool "I2S 0"
default n
select ARCH_DMA
select ESP32_GPIO_IRQ
if ESP32_I2S0
config ESP32_I2S0_RX
bool "Enable I2S receiver"
default y
---help---
Enable I2S receive logic
config ESP32_I2S0_TX
bool "Enable I2S transmitter"
default y
---help---
Enable I2S transmit logic
choice
prompt "I2S0 role"
default ESP32_I2S0_ROLE_MASTER
---help---
Selects the operation role of the I2S0.
config ESP32_I2S0_ROLE_MASTER
bool "Master"
config ESP32_I2S0_ROLE_SLAVE
bool "Slave"
endchoice
choice
prompt "Bit width"
---help---
Selects the valid data bits per sample.
Note that this option may be overwritten by the audio
according to the bit width of the file being played
config ESP32_I2S0_DATA_BIT_WIDTH_8BIT
bool "8 bits"
config ESP32_I2S0_DATA_BIT_WIDTH_16BIT
bool "16 bits"
config ESP32_I2S0_DATA_BIT_WIDTH_24BIT
bool "24 bits"
config ESP32_I2S0_DATA_BIT_WIDTH_32BIT
bool "32 bits"
endchoice
config ESP32_I2S0_DATA_BIT_WIDTH
int
default 8 if ESP32_I2S0_DATA_BIT_WIDTH_8BIT
default 16 if ESP32_I2S0_DATA_BIT_WIDTH_16BIT
default 24 if ESP32_I2S0_DATA_BIT_WIDTH_24BIT
default 32 if ESP32_I2S0_DATA_BIT_WIDTH_32BIT
config ESP32_I2S0_SAMPLE_RATE
int "I2S0 sample rate"
default 44100
range 8000 48000
---help---
Selects the sample rate.
Note that this option may be overwritten by the audio
according to the bit width of the file being played
config ESP32_I2S0_BCLKPIN
int "I2S0 BCLK pin"
default 4
range 0 33 if ESP32_I2S0_ROLE_MASTER
range 0 39 if ESP32_I2S0_ROLE_SLAVE
config ESP32_I2S0_WSPIN
int "I2S0 WS pin"
default 5
range 0 33 if ESP32_I2S0_ROLE_MASTER
range 0 39 if ESP32_I2S0_ROLE_SLAVE
config ESP32_I2S0_DINPIN
int "I2S0 DOUT pin"
depends on ESP32_I2S0_RX
default 12
range 0 39
config ESP32_I2S0_DOUTPIN
int "I2S0 DOUT pin"
depends on ESP32_I2S0_TX
default 18
range 0 33
config ESP32_I2S0_MCLK
bool "Enable I2S Master Clock"
depends on ESP32_I2S0_ROLE_MASTER
default n
---help---
Enable I2S master clock
choice
prompt "I2S0 MCLK pin"
depends on ESP32_I2S0_MCLK
default ESP32_I2S0_MCLKPIN_GPIO0
---help---
Selects the pin to output master clock.
config ESP32_I2S0_MCLKPIN_GPIO0
bool "GPIO0"
config ESP32_I2S0_MCLKPIN_GPIO1
bool "GPIO1 (U0TXD)"
config ESP32_I2S0_MCLKPIN_GPIO3
bool "GPIO3 [U0RXD]"
endchoice
config ESP32_I2S0_MCLKPIN
int
default 0
default 0 if ESP32_I2S0_MCLKPIN_GPIO0
default 1 if ESP32_I2S0_MCLKPIN_GPIO1
default 3 if ESP32_I2S0_MCLKPIN_GPIO3
endif #ESP32_I2S0
config ESP32_I2S1
bool "I2S 1"
default n
select ARCH_DMA
select ESP32_GPIO_IRQ
if ESP32_I2S1
config ESP32_I2S1_RX
bool "Enable I2S receiver"
default y
---help---
Enable I2S receive logic
config ESP32_I2S1_TX
bool "Enable I2S transmitter"
default y
---help---
Enable I2S transmit logic
choice
prompt "I2S1 role"
default ESP32_I2S1_ROLE_MASTER
---help---
Selects the operation role of the I2S1.
config ESP32_I2S1_ROLE_MASTER
bool "Master"
config ESP32_I2S1_ROLE_SLAVE
bool "Slave"
endchoice
choice
prompt "Bit width"
---help---
Selects the valid data bits per sample.
Note that this option may be overwritten by the audio
according to the bit width of the file being played
config ESP32_I2S1_DATA_BIT_WIDTH_8BIT
bool "8 bits"
config ESP32_I2S1_DATA_BIT_WIDTH_16BIT
bool "16 bits"
config ESP32_I2S1_DATA_BIT_WIDTH_24BIT
bool "24 bits"
config ESP32_I2S1_DATA_BIT_WIDTH_32BIT
bool "32 bits"
endchoice
config ESP32_I2S1_DATA_BIT_WIDTH
int
default 16
default 8 if ESP32_I2S1_DATA_BIT_WIDTH_8BIT
default 16 if ESP32_I2S1_DATA_BIT_WIDTH_16BIT
default 24 if ESP32_I2S1_DATA_BIT_WIDTH_24BIT
default 32 if ESP32_I2S1_DATA_BIT_WIDTH_32BIT
config ESP32_I2S1_SAMPLE_RATE
int "I2S1 sample rate"
default 44100
range 8000 48000
---help---
Selects the sample rate.
Note that this option may be overwritten by the audio
according to the bit width of the file being played
config ESP32_I2S1_BCLKPIN
int "I2S1 BCLK pin"
default 19
range 0 33 if ESP32_I2S1_ROLE_MASTER
range 0 39 if ESP32_I2S1_ROLE_SLAVE
config ESP32_I2S1_WSPIN
int "I2S1 WS pin"
default 18
range 0 33 if ESP32_I2S1_ROLE_MASTER
range 0 39 if ESP32_I2S1_ROLE_SLAVE
config ESP32_I2S1_DOUTPIN
int "I2S1 DOUT pin"
depends on ESP32_I2S1_RX
default 17
range 0 39
config ESP32_I2S1_DOUTPIN
int "I2S1 DOUT pin"
depends on ESP32_I2S1_TX
default 16
range 0 33
config ESP32_I2S1_MCLK
bool "Enable I2S Master Clock"
depends on ESP32_I2S1_ROLE_MASTER
default n
---help---
Enable I2S master clock
choice
prompt "I2S1 MCLK pin"
depends on ESP32_I2S1_MCLK
default ESP32_I2S1_MCLKPIN_GPIO0
---help---
Selects the pin to output master clock.
config ESP32_I2S1_MCLKPIN_GPIO0
bool "GPIO0"
config ESP32_I2S1_MCLKPIN_GPIO1
bool "GPIO1 (U0TXD)"
config ESP32_I2S1_MCLKPIN_GPIO3
bool "GPIO3 [U0RXD]"
endchoice
config ESP32_I2S1_MCLKPIN
int
default 0
default 0 if ESP32_I2S1_MCLKPIN_GPIO0
default 1 if ESP32_I2S1_MCLKPIN_GPIO1
default 3 if ESP32_I2S1_MCLKPIN_GPIO3
endif #ESP32_I2S1
config I2S_DMADESC_NUM
int "I2S DMA maximum number of descriptors"
default 2
---help---
Configure the maximum number of out-link/in-link descriptors to
be chained for a I2S DMA transfer.
endif #ESP32_I2S
config ESP32_SPI config ESP32_SPI
bool bool
default n default n

View File

@ -70,6 +70,10 @@ ifeq ($(CONFIG_ESP32_I2C),y)
CHIP_CSRCS += esp32_i2c.c CHIP_CSRCS += esp32_i2c.c
endif endif
ifeq ($(CONFIG_ESP32_I2S),y)
CHIP_CSRCS += esp32_i2s.c
endif
ifeq ($(CONFIG_ESP32_TWAI),y) ifeq ($(CONFIG_ESP32_TWAI),y)
CHIP_CSRCS += esp32_twai.c CHIP_CSRCS += esp32_twai.c
endif endif

View File

@ -105,3 +105,82 @@ uint32_t esp32_dma_init(struct esp32_dmadesc_s *dmadesc, uint32_t num,
return len - bytes; return len - bytes;
} }
/****************************************************************************
* Name: esp32_dma_init_with_padding
*
* Description:
* Initialize DMA outlink descriptors and bind the target buffer to
* these DMA descriptors. If len is not word-aligned, add a new descriptor
* containing a 4-byte variable to make the outlink data world-aligned.
*
* Input Parameters:
* dmadesc - DMA descriptions pointer
* num - DMA descriptions number
* pbuf - RX/TX buffer pointer
* len - RX/TX buffer length
* stuff - Value to be padded with the buffer
*
* Returned Value:
* Binded pbuf data bytes
*
****************************************************************************/
uint32_t esp32_dma_init_with_padding(struct esp32_dmadesc_s *dmadesc,
uint32_t num,
uint8_t *pbuf,
uint32_t len,
uint32_t *stuff)
{
int i;
uint32_t bytes = len;
uint8_t *pdata = pbuf;
uint32_t data_len = 0;
uint32_t buf_len = 0;
DEBUGASSERT(dmadesc != NULL);
DEBUGASSERT(pbuf != NULL);
DEBUGASSERT(len > 0);
for (i = 0; i < num - 1; i++)
{
data_len = MIN(bytes, ESP32_DMA_DATALEN_MAX);
/* Buffer length must be rounded to next 32-bit boundary. */
buf_len = ALIGN_UP(data_len, sizeof(uintptr_t));
dmadesc[i].ctrl = (data_len << DMA_CTRL_DATALEN_S) |
(buf_len << DMA_CTRL_BUFLEN_S) |
DMA_CTRL_OWN;
dmadesc[i].pbuf = pdata;
dmadesc[i].next = &dmadesc[i + 1];
bytes -= data_len;
if (bytes == 0)
{
break;
}
pdata += data_len;
}
/* Check if the data_len of the last descriptor is different from buf_len.
* If so, it's necessary to add the padding bytes to a new descriptor on
* outlink.
*/
if (data_len != buf_len)
{
i++;
dmadesc[i].ctrl = ((buf_len - data_len) << DMA_CTRL_DATALEN_S) |
(4 << DMA_CTRL_BUFLEN_S) |
DMA_CTRL_OWN;
dmadesc[i].pbuf = (uint8_t *)stuff;
}
dmadesc[i].ctrl |= DMA_CTRL_EOF;
dmadesc[i].next = NULL;
return len - bytes;
}

View File

@ -89,6 +89,32 @@ struct esp32_dmadesc_s
uint32_t esp32_dma_init(struct esp32_dmadesc_s *dmadesc, uint32_t num, uint32_t esp32_dma_init(struct esp32_dmadesc_s *dmadesc, uint32_t num,
uint8_t *pbuf, uint32_t len); uint8_t *pbuf, uint32_t len);
/****************************************************************************
* Name: esp32_dma_init_with_padding
*
* Description:
* Initialize DMA outlink descriptors and bind the target buffer to
* these DMA descriptors. If len is not word-aligned, add a new descriptor
* containing a 4-byte variable to make the outlink data world-aligned.
*
* Input Parameters:
* dmadesc - DMA descriptions pointer
* num - DMA descriptions number
* pbuf - RX/TX buffer pointer
* len - RX/TX buffer length
* stuff - Value to be padded with the buffer
*
* Returned Value:
* Binded pbuf data bytes
*
****************************************************************************/
uint32_t esp32_dma_init_with_padding(struct esp32_dmadesc_s *dmadesc,
uint32_t num,
uint8_t *pbuf,
uint32_t len,
uint32_t *stuff);
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,80 @@
/****************************************************************************
* arch/xtensa/src/esp32/esp32_i2s.h
*
* 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.
*
****************************************************************************/
#ifndef __ARCH_XTENSA_SRC_ESP32_ESP32_I2S_H
#define __ARCH_XTENSA_SRC_ESP32_ESP32_I2S_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/audio/i2s.h>
#ifndef __ASSEMBLY__
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
#ifdef CONFIG_ESP32_I2S
#ifdef CONFIG_ESP32_I2S0
#define ESP32_I2S0 0
#endif /* CONFIG_ESP32_I2S0 */
#ifdef CONFIG_ESP32_I2S1
#define ESP32_I2S1 1
#endif /* CONFIG_ESP32_I2S1 */
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: esp32_i2sbus_initialize
*
* Description:
* Initialize the selected I2S port
*
* Input Parameters:
* Port number (for hardware that has multiple I2S interfaces)
*
* Returned Value:
* Valid I2S device structure reference on success; a NULL on failure
*
****************************************************************************/
struct i2s_dev_s *esp32_i2sbus_initialize(int port);
#endif /* CONFIG_ESP32_I2S */
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* __ASSEMBLY__ */
#endif /* __ARCH_XTENSA_SRC_ESP32_ESP32_I2S_H */

File diff suppressed because it is too large Load Diff

View File

@ -32,6 +32,10 @@ ifeq ($(CONFIG_I2C_DRIVER),y)
CSRCS += esp32_board_i2c.c CSRCS += esp32_board_i2c.c
endif endif
ifeq ($(CONFIG_ESP32_I2S),y)
CSRCS += esp32_board_i2sdev.c
endif
ifeq ($(CONFIG_I2CMULTIPLEXER_TCA9548A),y) ifeq ($(CONFIG_I2CMULTIPLEXER_TCA9548A),y)
CSRCS += esp32_tca9548a.c CSRCS += esp32_tca9548a.c
endif endif

View File

@ -0,0 +1,109 @@
/****************************************************************************
* boards/xtensa/esp32/common/src/esp32_board_i2sdev.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 <stdio.h>
#include <debug.h>
#include <errno.h>
#include <nuttx/audio/audio.h>
#include <nuttx/audio/audio_i2s.h>
#include <nuttx/audio/i2s.h>
#include <nuttx/audio/pcm.h>
#include <arch/board/board.h>
#include "esp32_i2s.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: board_i2sdev_initialize
*
* Description:
* This function is called by platform-specific, setup logic to configure
* and register the generic I2S audio driver. 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 board_i2sdev_initialize(int port)
{
struct audio_lowerhalf_s *audio_i2s;
struct audio_lowerhalf_s *pcm;
struct i2s_dev_s *i2s;
char devname[12];
int ret;
ainfo("Initializing I2S\n");
i2s = esp32_i2sbus_initialize(port);
#ifdef CONFIG_AUDIO_I2SCHAR
ret = i2schar_register(i2s, 0);
if (ret < 0)
{
aerr("ERROR: i2schar_register failed: %d\n", ret);
return ret;
}
#endif
audio_i2s = audio_i2s_initialize(i2s, true);
if (!audio_i2s)
{
auderr("ERROR: Failed to initialize I2S\n");
return -ENODEV;
}
pcm = pcm_decode_initialize(audio_i2s);
if (!pcm)
{
auderr("ERROR: Failed create the PCM decoder\n");
return -ENODEV;
}
snprintf(devname, 12, "pcm%d", port);
ret = audio_register(devname, pcm);
if (ret < 0)
{
auderr("ERROR: Failed to register /dev/%s device: %d\n", devname, ret);
}
return ret;
}

View File

@ -0,0 +1,62 @@
#
# 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_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_STACKDUMP=y
CONFIG_ARCH_XTENSA=y
CONFIG_AUDIO=y
CONFIG_AUDIO_I2S=y
CONFIG_AUDIO_I2SCHAR=y
CONFIG_BOARD_LOOPSPERMSEC=16717
CONFIG_BUILTIN=y
CONFIG_DEBUG_SYMBOLS=y
CONFIG_DRIVERS_AUDIO=y
CONFIG_ESP32_I2S0=y
CONFIG_ESP32_I2S0_MCLK=y
CONFIG_ESP32_I2S=y
CONFIG_ESP32_UART0=y
CONFIG_EXAMPLES_I2SCHAR=y
CONFIG_EXAMPLES_I2SCHAR_TX=y
CONFIG_EXAMPLES_I2SCHAR_TXBUFFERS=2
CONFIG_EXAMPLES_I2SCHAR_TXSTACKSIZE=4096
CONFIG_FS_PROCFS=y
CONFIG_HAVE_CXX=y
CONFIG_HAVE_CXXINITIALIZE=y
CONFIG_IDLETHREAD_STACKSIZE=3072
CONFIG_INIT_ENTRYPOINT="nsh_main"
CONFIG_INIT_STACKSIZE=4096
CONFIG_INTELHEX_BINARY=y
CONFIG_MM_REGIONS=3
CONFIG_NSH_ARCHINIT=y
CONFIG_NSH_BUILTIN_APPS=y
CONFIG_NSH_FILEIOSIZE=512
CONFIG_NSH_LINELEN=64
CONFIG_NSH_READLINE=y
CONFIG_PREALLOC_TIMERS=4
CONFIG_RAM_SIZE=114688
CONFIG_RAM_START=0x20000000
CONFIG_RR_INTERVAL=200
CONFIG_SCHED_HPWORK=y
CONFIG_SCHED_LPWORK=y
CONFIG_SCHED_WAITPID=y
CONFIG_START_DAY=6
CONFIG_START_MONTH=12
CONFIG_START_YEAR=2011
CONFIG_SYSTEM_NSH=y
CONFIG_UART0_SERIAL_CONSOLE=y

View File

@ -151,5 +151,27 @@ int board_spidev_initialize(int bus);
int esp32_twai_setup(void); int esp32_twai_setup(void);
#endif #endif
/****************************************************************************
* Name: board_i2sdev_initialize
*
* Description:
* This function is called by platform-specific, setup logic to configure
* and register the generic I2S audio driver. 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.
*
****************************************************************************/
#if defined CONFIG_ESP32_I2S0 || defined CONFIG_ESP32_I2S1
int board_i2sdev_initialize(int port);
#endif
#endif /* __ASSEMBLY__ */ #endif /* __ASSEMBLY__ */
#endif /* __BOARDS_XTENSA_ESP32_ESP32_DEVKITC_SRC_ESP32_DEVKITC_H */ #endif /* __BOARDS_XTENSA_ESP32_ESP32_DEVKITC_SRC_ESP32_DEVKITC_H */

View File

@ -87,6 +87,10 @@
# include "esp32_board_i2c.h" # include "esp32_board_i2c.h"
#endif #endif
#ifdef CONFIG_ESP32_I2S
# include "esp32_i2s.h"
#endif
#ifdef CONFIG_I2CMULTIPLEXER_TCA9548A #ifdef CONFIG_I2CMULTIPLEXER_TCA9548A
# include "esp32_tca9548a.h" # include "esp32_tca9548a.h"
#endif #endif
@ -453,6 +457,36 @@ int esp32_bringup(void)
} }
#endif #endif
#ifdef CONFIG_ESP32_I2S
#ifdef CONFIG_ESP32_I2S0
/* Configure I2S generic audio on I2S0 */
ret = board_i2sdev_initialize(ESP32_I2S0);
if (ret < 0)
{
syslog(LOG_ERR, "Failed to initialize I2S%d driver: %d\n",
CONFIG_ESP32_I2S0, ret);
}
#endif /* CONFIG_ESP32_I2S0 */
#ifdef CONFIG_ESP32_I2S1
/* Configure I2S generic audio on I2S1 */
ret = board_i2sdev_initialize(ESP32_I2S1);
if (ret < 0)
{
syslog(LOG_ERR, "Failed to initialize I2S%d driver: %d\n",
CONFIG_ESP32_I2S0, ret);
}
#endif /* CONFIG_ESP32_I2S1 */
#endif /* CONFIG_ESP32_I2S */
#ifdef CONFIG_SENSORS_SHT3X #ifdef CONFIG_SENSORS_SHT3X
/* Try to register SHT3x device in I2C0 */ /* Try to register SHT3x device in I2C0 */