risc-v/esp32c3: Add ESP32-C3 (G)DMA driver and testing

This commit is contained in:
Dong Heng 2021-04-30 14:14:45 +08:00 committed by Gustavo Henrique Nihei
parent b8ed17b9da
commit bd8e37bb4b
6 changed files with 4697 additions and 0 deletions

View File

@ -237,6 +237,11 @@ config ESP32C3_SPI2
select ESP32C3_SPI
select SPI
config ESP32C3_DMA
bool "General DMA (GDMA)"
default n
select ARCH_DMA
config ESP32C3_MWDT0
bool "Main System Watchdog Timer (Group 0)"
default n
@ -542,4 +547,23 @@ config ESP32C3_PARTITION_MOUNT
endmenu # ESP32C3_PARTITION
menu "GDMA Configuration"
depends on ESP32C3_DMA
config ESP32C3_DMA_M2M_TEST
bool "Test GDMA M2M"
default n
config ESP32C3_DMA_M2M_TEST_LOOPS
int "Test GDMA M2M loops"
default 256
depends on ESP32C3_DMA_M2M_TEST
config ESP32C3_DMA_M2M_TEST_BUFSIZE
int "Test GDMA M2M buffer size"
default 16000
depends on ESP32C3_DMA_M2M_TEST
endmenu # GDMA Configuration
endif # ARCH_CHIP_ESP32C3

View File

@ -108,6 +108,10 @@ ifeq ($(CONFIG_ESP32C3_DISABLE_STDC_ATOMIC),)
CHIP_CSRCS += esp32c3_std_atomic.c
endif
ifeq ($(CONFIG_ESP32C3_DMA),y)
CHIP_CSRCS += esp32c3_dma.c
endif
ifeq ($(CONFIG_ESP32C3_WIRELESS),y)
WIRELESS_DRV_UNPACK = esp-wireless-drivers-3rdparty
WIRELESS_DRV_ID = 2b53111

View File

@ -0,0 +1,516 @@
/****************************************************************************
* arch/risc-v/src/esp32c3/esp32c3_dma.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 <sys/types.h>
#include <stdint.h>
#include <string.h>
#include <assert.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/irq.h>
#include <nuttx/kmalloc.h>
#include <arch/irq.h>
#include "riscv_arch.h"
#include "esp32c3_dma.h"
#include "hardware/esp32c3_dma.h"
#include "hardware/esp32c3_soc.h"
#include "hardware/esp32c3_system.h"
/****************************************************************************
* Pre-processor Macros
****************************************************************************/
#define REG_OFF (DMA_OUT_CONF0_CH1_REG - DMA_OUT_CONF0_CH0_REG)
#define SET_REG(_r, _ch, _v) putreg32((_v), (_r) + (_ch) * REG_OFF)
#define GET_REG(_r, _ch, _v) getreg32((_r) + (_ch) * REG_OFF)
#define SET_BITS(_r, _ch, _b) modifyreg32((_r) + (_ch) * REG_OFF, 0, (_b))
#define CLR_BITS(_r, _ch, _b) modifyreg32((_r) + (_ch) * REG_OFF, (_b), 0)
#ifndef MIN
# define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
#ifndef ALIGN_UP
# define ALIGN_UP(num, align) (((num) + ((align) - 1)) & ~((align) - 1))
#endif
/****************************************************************************
* Private Data
****************************************************************************/
static bool g_dma_chan_used[ESP32C3_DMA_CHAN_MAX];
static sem_t g_dma_exc_sem = SEM_INITIALIZER(1);
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: esp32c3_dma_request
*
* Description:
* Request DMA channel and config it with given parameters.
*
* Input Parameters:
* periph - Peripheral for which the DMA channel request was made
* tx_prio - Interrupt priority
* rx_prio - Interrupt flags
*
* Returned Value:
* DMA channel number (>=0) if success or -1 if fail.
*
****************************************************************************/
int32_t esp32c3_dma_request(enum esp32c3_dma_periph_e periph,
uint32_t tx_prio,
uint32_t rx_prio)
{
int chan;
DEBUGASSERT((periph < ESP32C3_DMA_PERIPH_NUM) &&
(periph != ESP32C3_DMA_PERIPH_RD0) &&
(periph != ESP32C3_DMA_PERIPH_RD1));
DEBUGASSERT(tx_prio <= ESP32C3_DMA_TX_PRIO_MAX);
DEBUGASSERT(rx_prio <= ESP32C3_DMA_RX_PRIO_MAX);
dmainfo("periph=%" PRIu32 " tx_prio=%" PRIu32 " rx_prio=%" PRIu32 "\n",
(uint32_t)periph, tx_prio, rx_prio);
nxsem_wait_uninterruptible(&g_dma_exc_sem);
for (chan = 0; chan < ESP32C3_DMA_CHAN_MAX; chan++)
{
if (!g_dma_chan_used[chan])
{
g_dma_chan_used[chan] = true;
break;
}
}
if (chan == ESP32C3_DMA_CHAN_MAX)
{
dmaerr("No available GDMA channel for allocation\n");
nxsem_post(&g_dma_exc_sem);
return ERROR;
}
dmainfo("Allocated channel=%d\n", chan);
if (periph == ESP32C3_DMA_PERIPH_MEM)
{
/* Enable DMA channel M2M mode */
SET_BITS(DMA_IN_CONF0_CH0_REG, chan, DMA_MEM_TRANS_EN_CH0_M);
/* Just setting a valid value to the register */
SET_REG(DMA_OUT_PERI_SEL_CH0_REG, chan, 0);
SET_REG(DMA_IN_PERI_SEL_CH0_REG, chan, 0);
}
else
{
/* Disable DMA channel M2M mode */
CLR_BITS(DMA_IN_CONF0_CH0_REG, chan, DMA_MEM_TRANS_EN_CH0_M);
/* Connect DMA TX/RX channel to a given peripheral */
SET_REG(DMA_OUT_PERI_SEL_CH0_REG, chan, periph);
SET_REG(DMA_IN_PERI_SEL_CH0_REG, chan, periph);
}
SET_REG(DMA_OUT_PRI_CH0_REG, chan, tx_prio);
SET_REG(DMA_IN_PRI_CH0_REG, chan, rx_prio);
nxsem_post(&g_dma_exc_sem);
return chan;
}
/****************************************************************************
* Name: esp32c3_dma_setup
*
* Description:
* Set up DMA descriptor with given parameters.
*
* Input Parameters:
* chan - DMA channel
* tx - true: TX mode; false: RX mode
* dmadesc - DMA descriptor pointer
* num - DMA descriptor number
* pbuf - Buffer pointer
* len - Buffer length by byte
*
* Returned Value:
* Bind pbuf data bytes.
*
****************************************************************************/
uint32_t esp32c3_dma_setup(int chan, bool tx,
struct esp32c3_dmadesc_s *dmadesc, uint32_t num,
uint8_t *pbuf, uint32_t len)
{
int i;
uint32_t regval;
uint32_t bytes = len;
uint8_t *pdata = pbuf;
uint32_t data_len;
uint32_t buf_len;
DEBUGASSERT(chan >= 0);
DEBUGASSERT(dmadesc != NULL);
DEBUGASSERT(num > 0);
DEBUGASSERT(pbuf != NULL);
DEBUGASSERT(len > 0);
for (i = 0; i < num; i++)
{
data_len = MIN(bytes, ESP32C3_DMA_BUFLEN_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 << ESP32C3_DMA_CTRL_DATALEN_S) |
(buf_len << ESP32C3_DMA_CTRL_BUFLEN_S) |
ESP32C3_DMA_CTRL_OWN;
dmadesc[i].pbuf = pdata;
dmadesc[i].next = &dmadesc[i + 1];
bytes -= data_len;
if (bytes == 0)
{
break;
}
pdata += data_len;
}
dmadesc[i].ctrl |= ESP32C3_DMA_CTRL_EOF;
dmadesc[i].next = NULL;
if (tx)
{
/* Reset DMA TX channel FSM and FIFO pointer */
SET_BITS(DMA_OUT_CONF0_CH0_REG, chan, DMA_OUT_RST_CH0_M);
CLR_BITS(DMA_OUT_CONF0_CH0_REG, chan, DMA_OUT_RST_CH0_M);
/* Set the descriptor link base address for TX channel */
regval = (uint32_t)dmadesc & DMA_OUTLINK_ADDR_CH0;
SET_BITS(DMA_OUT_LINK_CH0_REG, chan, regval);
}
else
{
/* Reset DMA RX channel FSM and FIFO pointer */
SET_BITS(DMA_IN_CONF0_CH0_REG, chan, DMA_IN_RST_CH0_M);
CLR_BITS(DMA_IN_CONF0_CH0_REG, chan, DMA_IN_RST_CH0_M);
/* Set the descriptor link base address for RX channel */
regval = (uint32_t)dmadesc & DMA_INLINK_ADDR_CH0;
SET_BITS(DMA_IN_LINK_CH0_REG, chan, regval);
}
return len - bytes;
}
/****************************************************************************
* Name: esp32c3_dma_enable
*
* Description:
* Enable DMA channel transmission.
*
* Input Parameters:
* chan - DMA channel
* tx - true: TX mode; false: RX mode
*
* Returned Value:
* None.
*
****************************************************************************/
void esp32c3_dma_enable(int chan, bool tx)
{
if (tx)
{
SET_BITS(DMA_OUT_LINK_CH0_REG, chan, DMA_OUTLINK_START_CH0_M);
}
else
{
SET_BITS(DMA_IN_LINK_CH0_REG, chan, DMA_INLINK_START_CH0_M);
}
}
/****************************************************************************
* Name: esp32c3_dma_disable
*
* Description:
* Disable DMA channel transmission.
*
* Input Parameters:
* chan - DMA channel
* tx - true: TX mode; false: RX mode
*
* Returned Value:
* None.
*
****************************************************************************/
void esp32c3_dma_disable(int chan, bool tx)
{
if (tx)
{
SET_BITS(DMA_OUT_LINK_CH0_REG, chan, DMA_OUTLINK_STOP_CH0_M);
}
else
{
SET_BITS(DMA_IN_LINK_CH0_REG, chan, DMA_INLINK_STOP_CH0_M);
}
}
/****************************************************************************
* Name: esp32c3_dma_wait_idle
*
* Description:
* Wait until transmission ends.
*
* Input Parameters:
* chan - DMA channel
* tx - true: TX mode; false: RX mode
*
* Returned Value:
* None.
*
****************************************************************************/
void esp32c3_dma_wait_idle(int chan, bool tx)
{
uint32_t regval;
uint32_t regaddr;
uint32_t waitbits;
if (tx)
{
regaddr = DMA_OUT_LINK_CH0_REG + chan * REG_OFF;
waitbits = DMA_OUTLINK_PARK_CH0;
}
else
{
regaddr = DMA_IN_LINK_CH0_REG + chan * REG_OFF;
waitbits = DMA_INLINK_PARK_CH0;
}
do
{
regval = getreg32(regaddr);
}
while ((waitbits & regval) == 0);
}
/****************************************************************************
* Name: esp32c3_dma_init
*
* Description:
* Initialize DMA driver.
*
* Input Parameters:
* None
*
* Returned Value:
* None.
*
****************************************************************************/
void esp32c3_dma_init(void)
{
modifyreg32(SYSTEM_PERIP_CLK_EN1_REG, 0, SYSTEM_DMA_CLK_EN_M);
modifyreg32(SYSTEM_PERIP_RST_EN1_REG, SYSTEM_DMA_RST_M, 0);
modifyreg32(DMA_MISC_CONF_REG, 0, DMA_CLK_EN_M);
}
/****************************************************************************
* Name: esp32c3_dma_main
*
* Description:
* ESP32-C3 DMA testing example.
*
****************************************************************************/
#ifdef CONFIG_ESP32C3_DMA_M2M_TEST
void esp32c3_dma_main(int argc, char *argv[])
{
int chan;
struct esp32c3_dmadesc_s *rx_dmadesc;
struct esp32c3_dmadesc_s *tx_dmadesc;
uint8_t *rxbuf;
uint8_t *txbuf;
bool success = true;
const size_t bufsize = CONFIG_ESP32C3_DMA_M2M_TEST_BUFSIZE;
#if (CONFIG_ESP32C3_DMA_M2M_TEST_BUFSIZE % ESP32C3_DMA_BUFLEN_MAX) > 0
const size_t dmadesc_num = bufsize / ESP32C3_DMA_BUFLEN_MAX + 1;
#else
const size_t dmadesc_num = bufsize / ESP32C3_DMA_BUFLEN_MAX;
#endif
syslog(LOG_INFO, "----- BEGIN TEST -----\n");
rxbuf = kmm_malloc(bufsize);
if (rxbuf == NULL)
{
syslog(LOG_ERR, "Failed to malloc RX buffer\n");
success = false;
goto test_end;
}
txbuf = kmm_malloc(bufsize);
if (txbuf == NULL)
{
syslog(LOG_ERR, "Failed to malloc TX buffer\n");
kmm_free(rxbuf);
success = false;
goto test_end;
}
rx_dmadesc = kmm_malloc(sizeof(struct esp32c3_dmadesc_s) * dmadesc_num);
if (rx_dmadesc == NULL)
{
syslog(LOG_ERR, "Failed to malloc RX DMA descriptor\n");
kmm_free(txbuf);
kmm_free(rxbuf);
success = false;
goto test_end;
}
tx_dmadesc = kmm_malloc(sizeof(struct esp32c3_dmadesc_s) * dmadesc_num);
if (txbuf == NULL)
{
syslog(LOG_ERR, "Failed to malloc TX DMA descriptor\n");
kmm_free(rx_dmadesc);
kmm_free(txbuf);
kmm_free(rxbuf);
success = false;
goto test_end;
}
esp32c3_dma_init();
chan = esp32c3_dma_request(ESP32C3_DMA_PERIPH_MEM, 1, 1);
if (chan < 0)
{
syslog(LOG_ERR, "Request DMA channel error\n");
success = false;
goto test_end_cleanup;
}
for (int i = 1; i <= CONFIG_ESP32C3_DMA_M2M_TEST_LOOPS; ++i)
{
const uint8_t watermark = i & UINT8_MAX;
size_t j = 0;
/* Prepare buffers for DMA transfer */
memset(rxbuf, 0, bufsize);
memset(txbuf, watermark, bufsize);
/* Setup DMA descriptors.
* Intentionally ignore the last byte for TX.
*/
esp32c3_dma_setup(chan, false, rx_dmadesc, dmadesc_num,
rxbuf, bufsize);
esp32c3_dma_setup(chan, true, tx_dmadesc, dmadesc_num,
txbuf, bufsize - 1);
/* Start DMA transfer */
esp32c3_dma_enable(chan, false);
esp32c3_dma_enable(chan, true);
/* Wait for DMA transfer to complete */
esp32c3_dma_wait_idle(chan, true);
esp32c3_dma_wait_idle(chan, false);
/* Verify if last byte on RX buffer is unchanged */
if (rxbuf[bufsize - 1] != 0)
{
success = false;
goto test_end_cleanup;
}
/* Verify if RX buffer contains expected values */
for (j = 0; j < bufsize - 1; ++j)
{
if (rxbuf[j] != watermark)
{
syslog(LOG_ERR,
"DMA-M2M-TEST loop %d fail buf[%zu]=%" PRIu8 "\n",
i, j, rxbuf[j]);
success = false;
goto test_end_cleanup;
}
}
syslog(LOG_INFO, "DMA-M2M-TEST loop %d OK\n", i);
}
test_end_cleanup:
kmm_free(tx_dmadesc);
kmm_free(rx_dmadesc);
kmm_free(txbuf);
kmm_free(rxbuf);
test_end:
syslog(LOG_INFO, "----- END TEST -----\n");
syslog(LOG_INFO, "\n");
syslog(LOG_INFO, "----- RESULT: %s -----\n",
success ? "SUCCESS" : "FAILED");
}
#endif

View File

@ -0,0 +1,223 @@
/****************************************************************************
* arch/risc-v/src/esp32c3/esp32c3_dma.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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#ifndef __ARCH_RISCV_SRC_ESP32C3_ESP32C3_DMA_H
#define __ARCH_RISCV_SRC_ESP32C3_ESP32C3_DMA_H
#include <nuttx/config.h>
#include <stdint.h>
#ifndef __ASSEMBLY__
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Pre-processor Macros
****************************************************************************/
/* DMA max data length */
#define ESP32C3_DMA_DATALEN_MAX (0x1000 - 4)
/* DMA max buffer length */
#define ESP32C3_DMA_BUFLEN_MAX ESP32C3_DMA_DATALEN_MAX
/* DMA channel number */
#define ESP32C3_DMA_CHAN_MAX (3)
/* DMA RX MAX priority */
#define ESP32C3_DMA_RX_PRIO_MAX (15)
/* DMA TX MAX priority */
#define ESP32C3_DMA_TX_PRIO_MAX (15)
/* DMA descriptor */
#define ESP32C3_DMA_CTRL_OWN (1 << 31) /* Owned by DMA */
#define ESP32C3_DMA_CTRL_EOF (1 << 30) /* End of frame */
#define ESP32C3_DMA_CTRL_ERREOF (1 << 28) /* Received data error */
#define ESP32C3_DMA_CTRL_DATALEN_S (12) /* Data length shift */
#define ESP32C3_DMA_CTRL_DATALEN_V (0xfff) /* Data length value */
#define ESP32C3_DMA_CTRL_BUFLEN_S (0) /* Buffer length shift */
#define ESP32C3_DMA_CTRL_BUFLEN_V (0xfff) /* Buffer length value */
/****************************************************************************
* Public Types
****************************************************************************/
enum esp32c3_dma_periph_e
{
ESP32C3_DMA_PERIPH_MEM = -1,
ESP32C3_DMA_PERIPH_SPI = 0,
ESP32C3_DMA_PERIPH_UCHI0 = 2,
ESP32C3_DMA_PERIPH_I2S = 3,
ESP32C3_DMA_PERIPH_RD0 = 4,
ESP32C3_DMA_PERIPH_RD1 = 5,
ESP32C3_DMA_PERIPH_AES = 6,
ESP32C3_DMA_PERIPH_SHA = 7,
ESP32C3_DMA_PERIPH_ADC = 8,
ESP32C3_DMA_PERIPH_NUM,
};
/* DMA descriptor type */
struct esp32c3_dmadesc_s
{
uint32_t ctrl; /* DMA control block */
uint8_t *pbuf; /* DMA TX/RX buffer address */
struct esp32c3_dmadesc_s *next; /* Next DMA descriptor address */
};
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: esp32c3_dma_request
*
* Description:
* Request DMA channel and config it with given parameters.
*
* Input Parameters:
* periph - Peripheral for which the DMA channel request was made
* tx_prio - Interrupt priority
* rx_prio - Interrupt flags
*
* Returned Value:
* DMA channel number (>=0) if success or -1 if fail.
*
****************************************************************************/
int32_t esp32c3_dma_request(enum esp32c3_dma_periph_e periph,
uint32_t tx_prio,
uint32_t rx_prio);
/****************************************************************************
* Name: esp32c3_dma_setup
*
* Description:
* Set up DMA descriptor with given parameters.
*
* Input Parameters:
* chan - DMA channel
* tx - true: TX mode; false: RX mode
* dmadesc - DMA descriptor pointer
* num - DMA descriptor number
* pbuf - Buffer pointer
* len - Buffer length by byte
*
* Returned Value:
* Bind pbuf data bytes.
*
****************************************************************************/
uint32_t esp32c3_dma_setup(int chan, bool tx,
struct esp32c3_dmadesc_s *dmadesc, uint32_t num,
uint8_t *pbuf, uint32_t len);
/****************************************************************************
* Name: esp32c3_dma_enable
*
* Description:
* Enable DMA channel transmission.
*
* Input Parameters:
* chan - DMA channel
* tx - true: TX mode; false: RX mode
*
* Returned Value:
* None.
*
****************************************************************************/
void esp32c3_dma_enable(int chan, bool tx);
/****************************************************************************
* Name: esp32c3_dma_disable
*
* Description:
* Disable DMA channel transmission.
*
* Input Parameters:
* chan - DMA channel
* tx - true: TX mode; false: RX mode
*
* Returned Value:
* None.
*
****************************************************************************/
void esp32c3_dma_disable(int chan, bool tx);
/****************************************************************************
* Name: esp32c3_dma_wait_idle
*
* Description:
* Wait until transmission ends.
*
* Input Parameters:
* chan - DMA channel
* tx - true: TX mode; false: RX mode
*
* Returned Value:
* None.
*
****************************************************************************/
void esp32c3_dma_wait_idle(int chan, bool tx);
/****************************************************************************
* Name: esp32c3_dma_init
*
* Description:
* Initialize DMA driver.
*
* Input Parameters:
* None
*
* Returned Value:
* None.
*
****************************************************************************/
void esp32c3_dma_init(void);
#ifdef __cplusplus
}
#endif
#undef EXTERN
#endif /* __ASSEMBLY__ */
#endif /* __ARCH_RISCV_SRC_ESP32C3_ESP32C3_DMA_H */

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,32 @@
#
# 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="risc-v"
CONFIG_ARCH_BOARD="esp32c3-devkit"
CONFIG_ARCH_BOARD_ESP32C3_DEVKIT=y
CONFIG_ARCH_CHIP="esp32c3"
CONFIG_ARCH_CHIP_ESP32C3=y
CONFIG_ARCH_CHIP_ESP32C3WROOM02=y
CONFIG_ARCH_INTERRUPTSTACK=1536
CONFIG_ARCH_RISCV=y
CONFIG_ARCH_STACKDUMP=y
CONFIG_BOARD_LOOPSPERMSEC=15000
CONFIG_BUILTIN=y
CONFIG_ESP32C3_DMA=y
CONFIG_ESP32C3_DMA_M2M_TEST=y
CONFIG_IDLETHREAD_STACKSIZE=2048
CONFIG_INTELHEX_BINARY=y
CONFIG_LIBC_PERROR_STDOUT=y
CONFIG_LIBC_STRERROR=y
CONFIG_MAX_TASKS=8
CONFIG_PREALLOC_TIMERS=0
CONFIG_RAW_BINARY=y
CONFIG_START_DAY=29
CONFIG_START_MONTH=11
CONFIG_START_YEAR=2019
CONFIG_UART0_SERIAL_CONSOLE=y
CONFIG_USER_ENTRYPOINT="esp32c3_dma_main"