2021-05-23 20:09:38 -04:00
|
|
|
/*
|
2023-06-21 07:00:59 -04:00
|
|
|
* SPDX-FileCopyrightText: 2020-2023 Espressif Systems (Shanghai) CO LTD
|
2021-05-23 20:09:38 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2020-12-09 07:29:26 -05:00
|
|
|
|
2023-07-11 04:32:54 -04:00
|
|
|
/**
|
|
|
|
* AHB-Bus --------+ +-------- AXI-Bus
|
|
|
|
* | |
|
|
|
|
* | |
|
|
|
|
* +-----------------------------------+--+ +--+-----------------------------------+
|
|
|
|
* | GDMA-Group-X | | | | GDMA-Group-Y |
|
|
|
|
* | +-------------+ +------------+ | | | | +-------------+ +------------+ |
|
|
|
|
* | | GDMA-Pair-0 |... |GDMA-Pair-N | | | | | | GDMA-Pair-0 |... |GDMA-Pair-N | |
|
|
|
|
* | | | | | | | | | | | | | |
|
|
|
|
* | | TX-Chan |... | TX-Chan | | | | | | TX-Chan |... | TX-Chan | |
|
|
|
|
* | | RX-Chan | | RX-Chan | | | | | | RX-Chan | | RX-Chan | |
|
|
|
|
* | +-------------+ +------------+ | | | | +-------------+ +------------+ |
|
|
|
|
* | | | | | |
|
|
|
|
* +-----------------------------------+--+ +--+-----------------------------------+
|
|
|
|
* | |
|
|
|
|
* | |
|
|
|
|
*
|
|
|
|
* - Channel is allocated when user calls `gdma_new_ahb/axi_channel`, its lifecycle is maintained by the user.
|
|
|
|
* - Pair and Group are all lazy allocated, their life cycles are maintained by this driver.
|
|
|
|
* - We're not using a global spin lock, instead, we created different spin locks at different level (group, pair).
|
|
|
|
*/
|
2020-12-09 07:29:26 -05:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
2023-06-02 07:48:24 -04:00
|
|
|
#include <string.h>
|
2020-12-09 07:29:26 -05:00
|
|
|
#include <sys/cdefs.h>
|
2021-11-07 21:26:52 -05:00
|
|
|
#include "sdkconfig.h"
|
2023-07-11 04:32:54 -04:00
|
|
|
#if CONFIG_GDMA_ENABLE_DEBUG_LOG
|
|
|
|
// The local log level must be defined before including esp_log.h
|
|
|
|
// Set the maximum log level for this source file
|
|
|
|
#define LOG_LOCAL_LEVEL ESP_LOG_DEBUG
|
|
|
|
#endif
|
2020-12-09 07:29:26 -05:00
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "soc/soc_caps.h"
|
|
|
|
#include "soc/periph_defs.h"
|
|
|
|
#include "esp_log.h"
|
2021-04-20 00:20:43 -04:00
|
|
|
#include "esp_check.h"
|
2022-03-31 03:07:51 -04:00
|
|
|
#include "esp_memory_utils.h"
|
2021-11-07 21:26:52 -05:00
|
|
|
#include "esp_private/periph_ctrl.h"
|
2022-09-13 06:47:08 -04:00
|
|
|
#include "gdma_priv.h"
|
2023-02-22 06:20:15 -05:00
|
|
|
#include "hal/cache_hal.h"
|
2020-12-09 07:29:26 -05:00
|
|
|
|
|
|
|
static const char *TAG = "gdma";
|
|
|
|
|
2023-08-24 04:09:24 -04:00
|
|
|
#if !SOC_RCC_IS_INDEPENDENT
|
|
|
|
// Reset and Clock Control registers are mixing with other peripherals, so we need to use a critical section
|
|
|
|
#define GDMA_RCC_ATOMIC() PERIPH_RCC_ATOMIC()
|
|
|
|
#else
|
|
|
|
#define GDMA_RCC_ATOMIC()
|
|
|
|
#endif
|
|
|
|
|
2020-12-09 07:29:26 -05:00
|
|
|
#define GDMA_INVALID_PERIPH_TRIG (0x3F)
|
|
|
|
#define SEARCH_REQUEST_RX_CHANNEL (1 << 0)
|
|
|
|
#define SEARCH_REQUEST_TX_CHANNEL (1 << 1)
|
|
|
|
|
2022-09-13 06:47:08 -04:00
|
|
|
typedef struct gdma_platform_t {
|
2023-07-11 04:32:54 -04:00
|
|
|
portMUX_TYPE spinlock; // platform level spinlock, protect the group handle slots and reference count of each group.
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_group_t *groups[SOC_GDMA_NUM_GROUPS_MAX]; // array of GDMA group instances
|
|
|
|
int group_ref_counts[SOC_GDMA_NUM_GROUPS_MAX]; // reference count used to protect group install/uninstall
|
2022-09-13 06:47:08 -04:00
|
|
|
} gdma_platform_t;
|
2020-12-09 07:29:26 -05:00
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
static gdma_group_t *gdma_acquire_group_handle(int group_id, void (*hal_init)(gdma_hal_context_t *hal, const gdma_hal_config_t *config));
|
2020-12-09 07:29:26 -05:00
|
|
|
static gdma_pair_t *gdma_acquire_pair_handle(gdma_group_t *group, int pair_id);
|
2021-12-13 21:08:26 -05:00
|
|
|
static void gdma_release_group_handle(gdma_group_t *group);
|
2020-12-09 07:29:26 -05:00
|
|
|
static void gdma_release_pair_handle(gdma_pair_t *pair);
|
|
|
|
static esp_err_t gdma_del_tx_channel(gdma_channel_t *dma_channel);
|
|
|
|
static esp_err_t gdma_del_rx_channel(gdma_channel_t *dma_channel);
|
2021-04-27 06:52:42 -04:00
|
|
|
static esp_err_t gdma_install_rx_interrupt(gdma_rx_channel_t *rx_chan);
|
|
|
|
static esp_err_t gdma_install_tx_interrupt(gdma_tx_channel_t *tx_chan);
|
2020-12-09 07:29:26 -05:00
|
|
|
|
|
|
|
// gdma driver platform
|
|
|
|
static gdma_platform_t s_platform = {
|
|
|
|
.spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED,
|
|
|
|
};
|
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
typedef struct {
|
|
|
|
int bus_id;
|
|
|
|
int start_group_id;
|
|
|
|
int end_group_id;
|
|
|
|
int pairs_per_group;
|
|
|
|
void (*hal_init)(gdma_hal_context_t *hal, const gdma_hal_config_t *config);
|
|
|
|
} gdma_channel_search_info_t;
|
|
|
|
|
|
|
|
static esp_err_t do_allocate_gdma_channel(const gdma_channel_search_info_t *search_info, const gdma_channel_alloc_config_t *config, gdma_channel_handle_t *ret_chan)
|
2020-12-09 07:29:26 -05:00
|
|
|
{
|
2023-07-11 04:32:54 -04:00
|
|
|
#if CONFIG_GDMA_ENABLE_DEBUG_LOG
|
|
|
|
esp_log_level_set(TAG, ESP_LOG_DEBUG);
|
|
|
|
#endif
|
2021-04-20 00:20:43 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2020-12-09 07:29:26 -05:00
|
|
|
gdma_tx_channel_t *alloc_tx_channel = NULL;
|
|
|
|
gdma_rx_channel_t *alloc_rx_channel = NULL;
|
|
|
|
int search_code = 0;
|
|
|
|
gdma_pair_t *pair = NULL;
|
|
|
|
gdma_group_t *group = NULL;
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_FALSE(config && ret_chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
2020-12-09 07:29:26 -05:00
|
|
|
|
|
|
|
if (config->flags.reserve_sibling) {
|
|
|
|
search_code = SEARCH_REQUEST_RX_CHANNEL | SEARCH_REQUEST_TX_CHANNEL; // search for a pair of channels
|
|
|
|
}
|
|
|
|
if (config->direction == GDMA_CHANNEL_DIRECTION_TX) {
|
|
|
|
search_code |= SEARCH_REQUEST_TX_CHANNEL; // search TX only
|
2021-11-07 21:26:52 -05:00
|
|
|
alloc_tx_channel = heap_caps_calloc(1, sizeof(gdma_tx_channel_t), GDMA_MEM_ALLOC_CAPS);
|
2021-04-20 00:20:43 -04:00
|
|
|
ESP_GOTO_ON_FALSE(alloc_tx_channel, ESP_ERR_NO_MEM, err, TAG, "no mem for gdma tx channel");
|
2020-12-09 07:29:26 -05:00
|
|
|
} else if (config->direction == GDMA_CHANNEL_DIRECTION_RX) {
|
|
|
|
search_code |= SEARCH_REQUEST_RX_CHANNEL; // search RX only
|
2021-11-07 21:26:52 -05:00
|
|
|
alloc_rx_channel = heap_caps_calloc(1, sizeof(gdma_rx_channel_t), GDMA_MEM_ALLOC_CAPS);
|
2021-04-20 00:20:43 -04:00
|
|
|
ESP_GOTO_ON_FALSE(alloc_rx_channel, ESP_ERR_NO_MEM, err, TAG, "no mem for gdma rx channel");
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (config->sibling_chan) {
|
|
|
|
pair = config->sibling_chan->pair;
|
2021-04-20 00:20:43 -04:00
|
|
|
ESP_GOTO_ON_FALSE(pair, ESP_ERR_INVALID_ARG, err, TAG, "invalid sibling channel");
|
|
|
|
ESP_GOTO_ON_FALSE(config->sibling_chan->direction != config->direction, ESP_ERR_INVALID_ARG, err, TAG, "sibling channel should have a different direction");
|
2020-12-09 07:29:26 -05:00
|
|
|
group = pair->group;
|
2021-02-07 22:55:49 -05:00
|
|
|
portENTER_CRITICAL(&group->spinlock);
|
|
|
|
group->pair_ref_counts[pair->pair_id]++; // channel obtains a reference to pair
|
|
|
|
portEXIT_CRITICAL(&group->spinlock);
|
2020-12-09 07:29:26 -05:00
|
|
|
goto search_done; // skip the search path below if user has specify a sibling channel
|
|
|
|
}
|
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
int start_group_id = search_info->start_group_id;
|
|
|
|
int end_group_id = search_info->end_group_id;
|
|
|
|
int pairs_per_group = search_info->pairs_per_group;
|
|
|
|
|
|
|
|
for (int i = start_group_id; i < end_group_id && search_code; i++) { // loop to search group
|
|
|
|
group = gdma_acquire_group_handle(i, search_info->hal_init);
|
|
|
|
group->bus_id = search_info->bus_id;
|
2021-12-13 21:08:26 -05:00
|
|
|
ESP_GOTO_ON_FALSE(group, ESP_ERR_NO_MEM, err, TAG, "no mem for group(%d)", i);
|
2023-06-21 07:00:59 -04:00
|
|
|
for (int j = 0; j < pairs_per_group && search_code; j++) { // loop to search pair
|
2020-12-09 07:29:26 -05:00
|
|
|
pair = gdma_acquire_pair_handle(group, j);
|
2021-12-13 21:08:26 -05:00
|
|
|
ESP_GOTO_ON_FALSE(pair, ESP_ERR_NO_MEM, err, TAG, "no mem for pair(%d,%d)", i, j);
|
|
|
|
portENTER_CRITICAL(&pair->spinlock);
|
|
|
|
if (!(search_code & pair->occupy_code)) { // pair has suitable position for acquired channel(s)
|
|
|
|
pair->occupy_code |= search_code;
|
|
|
|
search_code = 0; // exit search loop
|
|
|
|
}
|
|
|
|
portEXIT_CRITICAL(&pair->spinlock);
|
2023-07-11 04:32:54 -04:00
|
|
|
// found a pair that satisfies the search condition
|
|
|
|
if (search_code == 0) {
|
|
|
|
portENTER_CRITICAL(&group->spinlock);
|
|
|
|
group->pair_ref_counts[pair->pair_id]++; // channel obtains a reference to pair
|
|
|
|
portEXIT_CRITICAL(&group->spinlock);
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
2023-07-11 04:32:54 -04:00
|
|
|
gdma_release_pair_handle(pair);
|
2020-12-09 07:29:26 -05:00
|
|
|
} // loop used to search pair
|
2023-07-11 04:32:54 -04:00
|
|
|
gdma_release_group_handle(group);
|
|
|
|
// restore to initial state if no suitable channel slot is found
|
2021-12-13 21:08:26 -05:00
|
|
|
if (search_code) {
|
|
|
|
group = NULL;
|
2023-07-11 04:32:54 -04:00
|
|
|
pair = NULL;
|
2021-12-13 21:08:26 -05:00
|
|
|
}
|
2020-12-09 07:29:26 -05:00
|
|
|
} // loop used to search group
|
2021-04-20 00:20:43 -04:00
|
|
|
ESP_GOTO_ON_FALSE(search_code == 0, ESP_ERR_NOT_FOUND, err, TAG, "no free gdma channel, search code=%d", search_code);
|
2021-12-13 21:08:26 -05:00
|
|
|
assert(pair && group); // pair and group handle shouldn't be NULL
|
2020-12-09 07:29:26 -05:00
|
|
|
search_done:
|
|
|
|
// register TX channel
|
|
|
|
if (alloc_tx_channel) {
|
|
|
|
pair->tx_chan = alloc_tx_channel;
|
|
|
|
alloc_tx_channel->base.pair = pair;
|
|
|
|
alloc_tx_channel->base.direction = GDMA_CHANNEL_DIRECTION_TX;
|
|
|
|
alloc_tx_channel->base.periph_id = GDMA_INVALID_PERIPH_TRIG;
|
|
|
|
alloc_tx_channel->base.del = gdma_del_tx_channel; // set channel deletion function
|
|
|
|
*ret_chan = &alloc_tx_channel->base; // return the installed channel
|
|
|
|
}
|
|
|
|
|
|
|
|
// register RX channel
|
|
|
|
if (alloc_rx_channel) {
|
|
|
|
pair->rx_chan = alloc_rx_channel;
|
|
|
|
alloc_rx_channel->base.pair = pair;
|
|
|
|
alloc_rx_channel->base.direction = GDMA_CHANNEL_DIRECTION_RX;
|
|
|
|
alloc_rx_channel->base.periph_id = GDMA_INVALID_PERIPH_TRIG;
|
|
|
|
alloc_rx_channel->base.del = gdma_del_rx_channel; // set channel deletion function
|
|
|
|
*ret_chan = &alloc_rx_channel->base; // return the installed channel
|
|
|
|
}
|
|
|
|
|
2022-04-07 03:31:49 -04:00
|
|
|
(*ret_chan)->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
|
2020-12-09 07:29:26 -05:00
|
|
|
ESP_LOGD(TAG, "new %s channel (%d,%d) at %p", (config->direction == GDMA_CHANNEL_DIRECTION_TX) ? "tx" : "rx",
|
|
|
|
group->group_id, pair->pair_id, *ret_chan);
|
|
|
|
return ESP_OK;
|
|
|
|
|
|
|
|
err:
|
|
|
|
if (alloc_tx_channel) {
|
|
|
|
free(alloc_tx_channel);
|
|
|
|
}
|
|
|
|
if (alloc_rx_channel) {
|
|
|
|
free(alloc_rx_channel);
|
|
|
|
}
|
2021-12-13 21:08:26 -05:00
|
|
|
if (pair) {
|
|
|
|
gdma_release_pair_handle(pair);
|
|
|
|
}
|
|
|
|
if (group) {
|
|
|
|
gdma_release_group_handle(group);
|
|
|
|
}
|
2021-04-20 00:20:43 -04:00
|
|
|
return ret;
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
#if SOC_AHB_GDMA_SUPPORTED
|
|
|
|
esp_err_t gdma_new_ahb_channel(const gdma_channel_alloc_config_t *config, gdma_channel_handle_t *ret_chan)
|
|
|
|
{
|
|
|
|
gdma_channel_search_info_t search_info = {
|
|
|
|
.bus_id = SOC_GDMA_BUS_AHB,
|
|
|
|
.start_group_id = GDMA_LL_AHB_GROUP_START_ID,
|
|
|
|
.end_group_id = GDMA_LL_AHB_GROUP_START_ID + GDMA_LL_AHB_NUM_GROUPS,
|
|
|
|
.pairs_per_group = GDMA_LL_AHB_PAIRS_PER_GROUP,
|
|
|
|
.hal_init = gdma_ahb_hal_init,
|
|
|
|
};
|
|
|
|
return do_allocate_gdma_channel(&search_info, config, ret_chan);
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t gdma_new_channel(const gdma_channel_alloc_config_t *config, gdma_channel_handle_t *ret_chan)
|
|
|
|
__attribute__((alias("gdma_new_ahb_channel")));
|
|
|
|
#endif // SOC_AHB_GDMA_SUPPORTED
|
|
|
|
|
|
|
|
#if SOC_AXI_GDMA_SUPPORTED
|
|
|
|
esp_err_t gdma_new_axi_channel(const gdma_channel_alloc_config_t *config, gdma_channel_handle_t *ret_chan)
|
|
|
|
{
|
|
|
|
gdma_channel_search_info_t search_info = {
|
|
|
|
.bus_id = SOC_GDMA_BUS_AXI,
|
|
|
|
.start_group_id = GDMA_LL_AXI_GROUP_START_ID,
|
|
|
|
.end_group_id = GDMA_LL_AXI_GROUP_START_ID + GDMA_LL_AXI_NUM_GROUPS,
|
|
|
|
.pairs_per_group = GDMA_LL_AXI_PAIRS_PER_GROUP,
|
|
|
|
.hal_init = gdma_axi_hal_init,
|
|
|
|
};
|
|
|
|
return do_allocate_gdma_channel(&search_info, config, ret_chan);
|
|
|
|
}
|
|
|
|
#endif // SOC_AXI_GDMA_SUPPORTED
|
|
|
|
|
2020-12-09 07:29:26 -05:00
|
|
|
esp_err_t gdma_del_channel(gdma_channel_handle_t dma_chan)
|
|
|
|
{
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_FALSE(dma_chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
|
|
|
gdma_pair_t *pair = dma_chan->pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
2020-12-09 07:29:26 -05:00
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
// reset the channel priority to default
|
|
|
|
gdma_hal_set_priority(hal, pair->pair_id, dma_chan->direction, 0);
|
2020-12-09 07:29:26 -05:00
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
// call `gdma_del_tx_channel` or `gdma_del_rx_channel` under the hood
|
|
|
|
return dma_chan->del(dma_chan);
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t gdma_get_channel_id(gdma_channel_handle_t dma_chan, int *channel_id)
|
|
|
|
{
|
2021-04-20 00:20:43 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2020-12-09 07:29:26 -05:00
|
|
|
gdma_pair_t *pair = NULL;
|
2021-04-20 00:20:43 -04:00
|
|
|
ESP_GOTO_ON_FALSE(dma_chan, ESP_ERR_INVALID_ARG, err, TAG, "invalid argument");
|
2020-12-09 07:29:26 -05:00
|
|
|
pair = dma_chan->pair;
|
|
|
|
*channel_id = pair->pair_id;
|
|
|
|
err:
|
2021-04-20 00:20:43 -04:00
|
|
|
return ret;
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t gdma_connect(gdma_channel_handle_t dma_chan, gdma_trigger_t trig_periph)
|
|
|
|
{
|
2023-01-19 21:49:43 -05:00
|
|
|
ESP_RETURN_ON_FALSE(dma_chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
|
|
|
ESP_RETURN_ON_FALSE(dma_chan->periph_id == GDMA_INVALID_PERIPH_TRIG, ESP_ERR_INVALID_STATE, TAG, "channel is using by peripheral: %d", dma_chan->periph_id);
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_pair_t *pair = dma_chan->pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
2023-01-19 21:49:43 -05:00
|
|
|
bool periph_conflict = false;
|
2023-08-07 06:50:50 -04:00
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
if (trig_periph.bus_id != SOC_GDMA_BUS_ANY) {
|
|
|
|
ESP_RETURN_ON_FALSE(trig_periph.bus_id == group->bus_id, ESP_ERR_INVALID_ARG, TAG,
|
|
|
|
"peripheral and DMA system bus mismatch");
|
|
|
|
}
|
2020-12-09 07:29:26 -05:00
|
|
|
|
|
|
|
if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_TX) {
|
2023-01-19 21:49:43 -05:00
|
|
|
if (trig_periph.instance_id >= 0) {
|
|
|
|
portENTER_CRITICAL(&group->spinlock);
|
|
|
|
if (group->tx_periph_in_use_mask & (1 << trig_periph.instance_id)) {
|
|
|
|
periph_conflict = true;
|
|
|
|
} else {
|
|
|
|
group->tx_periph_in_use_mask |= (1 << trig_periph.instance_id);
|
|
|
|
}
|
|
|
|
portEXIT_CRITICAL(&group->spinlock);
|
|
|
|
}
|
2020-12-09 07:29:26 -05:00
|
|
|
} else {
|
2023-01-19 21:49:43 -05:00
|
|
|
if (trig_periph.instance_id >= 0) {
|
|
|
|
portENTER_CRITICAL(&group->spinlock);
|
|
|
|
if (group->rx_periph_in_use_mask & (1 << trig_periph.instance_id)) {
|
|
|
|
periph_conflict = true;
|
|
|
|
} else {
|
|
|
|
group->rx_periph_in_use_mask |= (1 << trig_periph.instance_id);
|
|
|
|
}
|
|
|
|
portEXIT_CRITICAL(&group->spinlock);
|
|
|
|
}
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
|
|
|
|
2023-01-19 21:49:43 -05:00
|
|
|
ESP_RETURN_ON_FALSE(!periph_conflict, ESP_ERR_INVALID_STATE, TAG, "peripheral %d is already used by another channel", trig_periph.instance_id);
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_connect_peri(hal, pair->pair_id, dma_chan->direction, trig_periph.periph, trig_periph.instance_id);
|
2023-01-19 21:49:43 -05:00
|
|
|
dma_chan->periph_id = trig_periph.instance_id;
|
|
|
|
return ESP_OK;
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t gdma_disconnect(gdma_channel_handle_t dma_chan)
|
|
|
|
{
|
2023-01-19 21:49:43 -05:00
|
|
|
ESP_RETURN_ON_FALSE(dma_chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
|
|
|
ESP_RETURN_ON_FALSE(dma_chan->periph_id != GDMA_INVALID_PERIPH_TRIG, ESP_ERR_INVALID_STATE, TAG, "no peripheral is connected to the channel");
|
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_pair_t *pair = dma_chan->pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
2023-01-19 21:49:43 -05:00
|
|
|
int save_periph_id = dma_chan->periph_id;
|
2020-12-09 07:29:26 -05:00
|
|
|
|
|
|
|
if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_TX) {
|
2023-01-19 21:49:43 -05:00
|
|
|
if (save_periph_id >= 0) {
|
|
|
|
portENTER_CRITICAL(&group->spinlock);
|
|
|
|
group->tx_periph_in_use_mask &= ~(1 << save_periph_id);
|
|
|
|
portEXIT_CRITICAL(&group->spinlock);
|
|
|
|
}
|
2020-12-09 07:29:26 -05:00
|
|
|
} else {
|
2023-01-19 21:49:43 -05:00
|
|
|
if (save_periph_id >= 0) {
|
|
|
|
portENTER_CRITICAL(&group->spinlock);
|
|
|
|
group->rx_periph_in_use_mask &= ~(1 << save_periph_id);
|
|
|
|
portEXIT_CRITICAL(&group->spinlock);
|
|
|
|
}
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_disconnect_peri(hal, pair->pair_id, dma_chan->direction);
|
|
|
|
|
2023-01-19 21:49:43 -05:00
|
|
|
dma_chan->periph_id = GDMA_INVALID_PERIPH_TRIG;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t gdma_get_free_m2m_trig_id_mask(gdma_channel_handle_t dma_chan, uint32_t *mask)
|
|
|
|
{
|
|
|
|
ESP_RETURN_ON_FALSE(dma_chan && mask, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_pair_t *pair = dma_chan->pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
|
|
|
uint32_t free_mask = group->hal.priv_data->m2m_free_periph_mask;
|
2023-01-19 21:49:43 -05:00
|
|
|
|
|
|
|
portENTER_CRITICAL(&group->spinlock);
|
|
|
|
free_mask &= ~(group->tx_periph_in_use_mask);
|
|
|
|
free_mask &= ~(group->rx_periph_in_use_mask);
|
|
|
|
portEXIT_CRITICAL(&group->spinlock);
|
|
|
|
|
|
|
|
*mask = free_mask;
|
|
|
|
return ESP_OK;
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
|
|
|
|
2021-06-23 02:10:07 -04:00
|
|
|
esp_err_t gdma_set_transfer_ability(gdma_channel_handle_t dma_chan, const gdma_transfer_ability_t *ability)
|
|
|
|
{
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_FALSE(dma_chan && ability, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
|
|
|
gdma_pair_t *pair = dma_chan->pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
|
|
|
|
2021-06-23 02:10:07 -04:00
|
|
|
size_t sram_alignment = ability->sram_trans_align;
|
|
|
|
size_t psram_alignment = ability->psram_trans_align;
|
|
|
|
// alignment should be 2^n
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_FALSE((sram_alignment & (sram_alignment - 1)) == 0, ESP_ERR_INVALID_ARG,
|
|
|
|
TAG, "invalid sram alignment: %zu", sram_alignment);
|
2021-06-23 02:10:07 -04:00
|
|
|
|
2023-02-22 06:20:15 -05:00
|
|
|
uint32_t data_cache_line_size = cache_hal_get_cache_line_size(CACHE_TYPE_DATA);
|
2023-06-21 07:00:59 -04:00
|
|
|
if (psram_alignment == 0) {
|
|
|
|
// fall back to use the same size of the psram data cache line size
|
|
|
|
psram_alignment = data_cache_line_size;
|
|
|
|
}
|
|
|
|
if (psram_alignment > data_cache_line_size) {
|
|
|
|
ESP_RETURN_ON_FALSE(((psram_alignment % data_cache_line_size) == 0), ESP_ERR_INVALID_ARG,
|
|
|
|
TAG, "psram_alignment(%d) should be multiple of the data_cache_line_size(%d)",
|
|
|
|
psram_alignment, data_cache_line_size);
|
|
|
|
}
|
2021-06-23 02:10:07 -04:00
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
// if the DMA can't access the PSRAM, this HAL function is no-op
|
|
|
|
gdma_hal_set_ext_mem_align(hal, pair->pair_id, dma_chan->direction, psram_alignment);
|
|
|
|
|
|
|
|
// TX channel can always enable burst mode, no matter data alignment
|
|
|
|
bool en_burst = true;
|
|
|
|
if (dma_chan->direction == GDMA_CHANNEL_DIRECTION_RX) {
|
2021-06-23 02:10:07 -04:00
|
|
|
// RX channel burst mode depends on specific data alignment
|
|
|
|
en_burst = sram_alignment >= 4;
|
|
|
|
}
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_enable_burst(hal, pair->pair_id, dma_chan->direction, en_burst, en_burst);
|
2021-06-23 02:10:07 -04:00
|
|
|
|
|
|
|
dma_chan->sram_alignment = sram_alignment;
|
|
|
|
dma_chan->psram_alignment = psram_alignment;
|
2023-05-04 23:18:09 -04:00
|
|
|
ESP_LOGD(TAG, "%s channel (%d,%d), (%u:%u) bytes aligned, burst %s", dma_chan->direction == GDMA_CHANNEL_DIRECTION_TX ? "tx" : "rx",
|
2021-06-23 02:10:07 -04:00
|
|
|
group->group_id, pair->pair_id, sram_alignment, psram_alignment, en_burst ? "enabled" : "disabled");
|
2023-06-21 07:00:59 -04:00
|
|
|
|
|
|
|
return ESP_OK;
|
2021-06-23 02:10:07 -04:00
|
|
|
}
|
|
|
|
|
2020-12-09 07:29:26 -05:00
|
|
|
esp_err_t gdma_apply_strategy(gdma_channel_handle_t dma_chan, const gdma_strategy_config_t *config)
|
|
|
|
{
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_FALSE(dma_chan && config, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
|
|
|
gdma_pair_t *pair = dma_chan->pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
2020-12-09 07:29:26 -05:00
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_set_strategy(hal, pair->pair_id, dma_chan->direction, config->owner_check, config->auto_update_desc);
|
2020-12-09 07:29:26 -05:00
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
return ESP_OK;
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
|
|
|
|
2023-03-08 22:42:06 -05:00
|
|
|
esp_err_t gdma_set_priority(gdma_channel_handle_t dma_chan, uint32_t priority)
|
|
|
|
{
|
|
|
|
ESP_RETURN_ON_FALSE(dma_chan && priority <= GDMA_LL_CHANNEL_MAX_PRIORITY, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_pair_t *pair = dma_chan->pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
2023-03-08 22:42:06 -05:00
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_set_priority(hal, pair->pair_id, dma_chan->direction, priority);
|
2023-03-08 22:42:06 -05:00
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2023-08-07 06:50:50 -04:00
|
|
|
#if SOC_GDMA_SUPPORT_CRC
|
|
|
|
esp_err_t gdma_config_crc_calculator(gdma_channel_handle_t dma_chan, const gdma_crc_calculator_config_t *config)
|
|
|
|
{
|
|
|
|
ESP_RETURN_ON_FALSE(dma_chan && config, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
|
|
|
gdma_pair_t *pair = dma_chan->pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
|
|
|
switch (group->bus_id) {
|
|
|
|
#if SOC_AHB_GDMA_SUPPORTED
|
|
|
|
case SOC_GDMA_BUS_AHB:
|
|
|
|
ESP_RETURN_ON_FALSE(config->crc_bit_width <= GDMA_LL_AHB_MAX_CRC_BIT_WIDTH, ESP_ERR_INVALID_ARG, TAG, "invalid crc bit width");
|
|
|
|
break;
|
|
|
|
#endif // SOC_AHB_GDMA_SUPPORTED
|
|
|
|
#if SOC_AXI_GDMA_SUPPORTED
|
|
|
|
case SOC_GDMA_BUS_AXI:
|
|
|
|
ESP_RETURN_ON_FALSE(config->crc_bit_width <= GDMA_LL_AXI_MAX_CRC_BIT_WIDTH, ESP_ERR_INVALID_ARG, TAG, "invalid crc bit width");
|
|
|
|
break;
|
|
|
|
#endif // SOC_AXI_GDMA_SUPPORTED
|
|
|
|
default:
|
|
|
|
ESP_LOGE(TAG, "invalid bus id: %d", group->bus_id);
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear the previous CRC result
|
|
|
|
gdma_hal_clear_crc(hal, pair->pair_id, dma_chan->direction);
|
|
|
|
|
|
|
|
// set polynomial and initial value
|
|
|
|
gdma_hal_crc_config_t hal_config = {
|
|
|
|
.crc_bit_width = config->crc_bit_width,
|
|
|
|
.poly_hex = config->poly_hex,
|
|
|
|
.init_value = config->init_value,
|
|
|
|
.reverse_data_mask = config->reverse_data_mask,
|
|
|
|
};
|
|
|
|
gdma_hal_set_crc_poly(hal, pair->pair_id, dma_chan->direction, &hal_config);
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t gdma_crc_get_result(gdma_channel_handle_t dma_chan, uint32_t *result)
|
|
|
|
{
|
|
|
|
ESP_RETURN_ON_FALSE(dma_chan && result, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
|
|
|
gdma_pair_t *pair = dma_chan->pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
|
|
|
|
|
|
|
*result = gdma_hal_get_crc_result(hal, pair->pair_id, dma_chan->direction);
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
#endif // SOC_GDMA_SUPPORT_CRC
|
|
|
|
|
2020-12-09 07:29:26 -05:00
|
|
|
esp_err_t gdma_register_tx_event_callbacks(gdma_channel_handle_t dma_chan, gdma_tx_event_callbacks_t *cbs, void *user_data)
|
|
|
|
{
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_FALSE(dma_chan && cbs && dma_chan->direction == GDMA_CHANNEL_DIRECTION_TX, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
|
|
|
gdma_pair_t *pair = dma_chan->pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
2020-12-09 07:29:26 -05:00
|
|
|
gdma_tx_channel_t *tx_chan = __containerof(dma_chan, gdma_tx_channel_t, base);
|
|
|
|
|
2021-11-07 21:26:52 -05:00
|
|
|
#if CONFIG_GDMA_ISR_IRAM_SAFE
|
|
|
|
if (cbs->on_trans_eof) {
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_FALSE(esp_ptr_in_iram(cbs->on_trans_eof), ESP_ERR_INVALID_ARG,
|
|
|
|
TAG, "on_trans_eof not in IRAM");
|
2021-11-07 21:26:52 -05:00
|
|
|
}
|
2023-05-25 04:08:50 -04:00
|
|
|
if (cbs->on_descr_err) {
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_FALSE(esp_ptr_in_iram(cbs->on_descr_err), ESP_ERR_INVALID_ARG,
|
|
|
|
TAG, "on_descr_err not in IRAM");
|
2023-05-25 04:08:50 -04:00
|
|
|
}
|
2021-11-07 21:26:52 -05:00
|
|
|
if (user_data) {
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_FALSE(esp_ptr_internal(user_data), ESP_ERR_INVALID_ARG,
|
|
|
|
TAG, "user context not in internal RAM");
|
2021-11-07 21:26:52 -05:00
|
|
|
}
|
|
|
|
#endif // CONFIG_GDMA_ISR_IRAM_SAFE
|
|
|
|
|
2020-12-09 07:29:26 -05:00
|
|
|
// lazy install interrupt service
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_ERROR(gdma_install_tx_interrupt(tx_chan), TAG, "install interrupt service failed");
|
2020-12-09 07:29:26 -05:00
|
|
|
|
|
|
|
// enable/disable GDMA interrupt events for TX channel
|
|
|
|
portENTER_CRITICAL(&pair->spinlock);
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_enable_intr(hal, pair->pair_id, GDMA_CHANNEL_DIRECTION_TX, GDMA_LL_EVENT_TX_EOF, cbs->on_trans_eof != NULL);
|
|
|
|
gdma_hal_enable_intr(hal, pair->pair_id, GDMA_CHANNEL_DIRECTION_TX, GDMA_LL_EVENT_TX_DESC_ERROR, cbs->on_descr_err != NULL);
|
2020-12-09 07:29:26 -05:00
|
|
|
portEXIT_CRITICAL(&pair->spinlock);
|
|
|
|
|
2023-06-02 07:48:24 -04:00
|
|
|
memcpy(&tx_chan->cbs, cbs, sizeof(gdma_tx_event_callbacks_t));
|
2020-12-09 07:29:26 -05:00
|
|
|
tx_chan->user_data = user_data;
|
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_ERROR(esp_intr_enable(dma_chan->intr), TAG, "enable interrupt failed");
|
2021-01-14 07:34:30 -05:00
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
return ESP_OK;
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t gdma_register_rx_event_callbacks(gdma_channel_handle_t dma_chan, gdma_rx_event_callbacks_t *cbs, void *user_data)
|
|
|
|
{
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_FALSE(dma_chan && cbs && dma_chan->direction == GDMA_CHANNEL_DIRECTION_RX, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
|
|
|
gdma_pair_t *pair = dma_chan->pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
2020-12-09 07:29:26 -05:00
|
|
|
gdma_rx_channel_t *rx_chan = __containerof(dma_chan, gdma_rx_channel_t, base);
|
|
|
|
|
2021-11-07 21:26:52 -05:00
|
|
|
#if CONFIG_GDMA_ISR_IRAM_SAFE
|
|
|
|
if (cbs->on_recv_eof) {
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_FALSE(esp_ptr_in_iram(cbs->on_recv_eof), ESP_ERR_INVALID_ARG,
|
|
|
|
TAG, "on_recv_eof not in IRAM");
|
2021-11-07 21:26:52 -05:00
|
|
|
}
|
2023-05-25 04:08:50 -04:00
|
|
|
if (cbs->on_descr_err) {
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_FALSE(esp_ptr_in_iram(cbs->on_descr_err), ESP_ERR_INVALID_ARG,
|
|
|
|
TAG, "on_descr_err not in IRAM");
|
2023-05-25 04:08:50 -04:00
|
|
|
}
|
2023-06-02 07:48:24 -04:00
|
|
|
if (cbs->on_recv_done) {
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_FALSE(esp_ptr_in_iram(cbs->on_recv_done), ESP_ERR_INVALID_ARG,
|
|
|
|
TAG, "on_recv_done not in IRAM");
|
2023-06-02 07:48:24 -04:00
|
|
|
}
|
2021-11-07 21:26:52 -05:00
|
|
|
if (user_data) {
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_FALSE(esp_ptr_internal(user_data), ESP_ERR_INVALID_ARG,
|
|
|
|
TAG, "user context not in internal RAM");
|
2021-11-07 21:26:52 -05:00
|
|
|
}
|
|
|
|
#endif // CONFIG_GDMA_ISR_IRAM_SAFE
|
|
|
|
|
2020-12-09 07:29:26 -05:00
|
|
|
// lazy install interrupt service
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_ERROR(gdma_install_rx_interrupt(rx_chan), TAG, "install interrupt service failed");
|
2020-12-09 07:29:26 -05:00
|
|
|
|
|
|
|
// enable/disable GDMA interrupt events for RX channel
|
|
|
|
portENTER_CRITICAL(&pair->spinlock);
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_enable_intr(hal, pair->pair_id, GDMA_CHANNEL_DIRECTION_RX, GDMA_LL_EVENT_RX_SUC_EOF, cbs->on_recv_eof != NULL);
|
|
|
|
gdma_hal_enable_intr(hal, pair->pair_id, GDMA_CHANNEL_DIRECTION_RX, GDMA_LL_EVENT_RX_DESC_ERROR, cbs->on_descr_err != NULL);
|
|
|
|
gdma_hal_enable_intr(hal, pair->pair_id, GDMA_CHANNEL_DIRECTION_RX, GDMA_LL_EVENT_RX_DONE, cbs->on_recv_done != NULL);
|
2020-12-09 07:29:26 -05:00
|
|
|
portEXIT_CRITICAL(&pair->spinlock);
|
|
|
|
|
2023-06-02 07:48:24 -04:00
|
|
|
memcpy(&rx_chan->cbs, cbs, sizeof(gdma_rx_event_callbacks_t));
|
2020-12-09 07:29:26 -05:00
|
|
|
rx_chan->user_data = user_data;
|
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_ERROR(esp_intr_enable(dma_chan->intr), TAG, "enable interrupt failed");
|
2021-01-14 07:34:30 -05:00
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
return ESP_OK;
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
|
|
|
|
2022-01-17 01:39:50 -05:00
|
|
|
esp_err_t gdma_start(gdma_channel_handle_t dma_chan, intptr_t desc_base_addr)
|
2020-12-09 07:29:26 -05:00
|
|
|
{
|
2023-04-03 03:55:00 -04:00
|
|
|
ESP_RETURN_ON_FALSE_ISR(dma_chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
|
|
|
ESP_RETURN_ON_FALSE_ISR(dma_chan->flags.start_stop_by_etm == false, ESP_ERR_INVALID_STATE, TAG, "channel is controlled by ETM");
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_pair_t *pair = dma_chan->pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
2020-12-09 07:29:26 -05:00
|
|
|
|
2022-04-07 03:31:49 -04:00
|
|
|
portENTER_CRITICAL_SAFE(&dma_chan->spinlock);
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_start_with_desc(hal, pair->pair_id, dma_chan->direction, desc_base_addr);
|
2022-04-07 03:31:49 -04:00
|
|
|
portEXIT_CRITICAL_SAFE(&dma_chan->spinlock);
|
2020-12-09 07:29:26 -05:00
|
|
|
|
2023-04-03 03:55:00 -04:00
|
|
|
return ESP_OK;
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
|
|
|
|
2022-01-17 01:39:50 -05:00
|
|
|
esp_err_t gdma_stop(gdma_channel_handle_t dma_chan)
|
2020-12-09 07:29:26 -05:00
|
|
|
{
|
2023-04-03 03:55:00 -04:00
|
|
|
ESP_RETURN_ON_FALSE_ISR(dma_chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
|
|
|
ESP_RETURN_ON_FALSE_ISR(dma_chan->flags.start_stop_by_etm == false, ESP_ERR_INVALID_STATE, TAG, "channel is controlled by ETM");
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_pair_t *pair = dma_chan->pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
2020-12-09 07:29:26 -05:00
|
|
|
|
2022-04-07 03:31:49 -04:00
|
|
|
portENTER_CRITICAL_SAFE(&dma_chan->spinlock);
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_stop(hal, pair->pair_id, dma_chan->direction);
|
2022-04-07 03:31:49 -04:00
|
|
|
portEXIT_CRITICAL_SAFE(&dma_chan->spinlock);
|
2020-12-09 07:29:26 -05:00
|
|
|
|
2023-04-03 03:55:00 -04:00
|
|
|
return ESP_OK;
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
|
|
|
|
2022-01-17 01:39:50 -05:00
|
|
|
esp_err_t gdma_append(gdma_channel_handle_t dma_chan)
|
2020-12-09 07:29:26 -05:00
|
|
|
{
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_FALSE_ISR(dma_chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
|
|
|
gdma_pair_t *pair = dma_chan->pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
2020-12-09 07:29:26 -05:00
|
|
|
|
2022-04-07 03:31:49 -04:00
|
|
|
portENTER_CRITICAL_SAFE(&dma_chan->spinlock);
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_append(hal, pair->pair_id, dma_chan->direction);
|
2022-04-07 03:31:49 -04:00
|
|
|
portEXIT_CRITICAL_SAFE(&dma_chan->spinlock);
|
2020-12-09 07:29:26 -05:00
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
return ESP_OK;
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
|
|
|
|
2022-01-17 01:39:50 -05:00
|
|
|
esp_err_t gdma_reset(gdma_channel_handle_t dma_chan)
|
2021-03-22 09:58:04 -04:00
|
|
|
{
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_RETURN_ON_FALSE_ISR(dma_chan, ESP_ERR_INVALID_ARG, TAG, "invalid argument");
|
|
|
|
gdma_pair_t *pair = dma_chan->pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
2021-03-22 09:58:04 -04:00
|
|
|
|
2022-04-07 03:31:49 -04:00
|
|
|
portENTER_CRITICAL_SAFE(&dma_chan->spinlock);
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_reset(hal, pair->pair_id, dma_chan->direction);
|
2022-04-07 03:31:49 -04:00
|
|
|
portEXIT_CRITICAL_SAFE(&dma_chan->spinlock);
|
2021-03-22 09:58:04 -04:00
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
return ESP_OK;
|
2021-03-22 09:58:04 -04:00
|
|
|
}
|
|
|
|
|
2021-12-13 21:08:26 -05:00
|
|
|
static void gdma_release_group_handle(gdma_group_t *group)
|
2020-12-09 07:29:26 -05:00
|
|
|
{
|
|
|
|
int group_id = group->group_id;
|
|
|
|
bool do_deinitialize = false;
|
|
|
|
|
2021-02-07 22:55:49 -05:00
|
|
|
portENTER_CRITICAL(&s_platform.spinlock);
|
|
|
|
s_platform.group_ref_counts[group_id]--;
|
|
|
|
if (s_platform.group_ref_counts[group_id] == 0) {
|
|
|
|
assert(s_platform.groups[group_id]);
|
|
|
|
do_deinitialize = true;
|
2023-06-21 07:00:59 -04:00
|
|
|
// deregister from the platform
|
|
|
|
s_platform.groups[group_id] = NULL;
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
2021-02-07 22:55:49 -05:00
|
|
|
portEXIT_CRITICAL(&s_platform.spinlock);
|
2020-12-09 07:29:26 -05:00
|
|
|
|
|
|
|
if (do_deinitialize) {
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_deinit(&group->hal);
|
2023-08-24 04:09:24 -04:00
|
|
|
GDMA_RCC_ATOMIC() {
|
|
|
|
gdma_ll_enable_bus_clock(group_id, false);
|
|
|
|
}
|
2020-12-09 07:29:26 -05:00
|
|
|
free(group);
|
|
|
|
ESP_LOGD(TAG, "del group %d", group_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
static gdma_group_t *gdma_acquire_group_handle(int group_id, void (*hal_init)(gdma_hal_context_t *hal, const gdma_hal_config_t *config))
|
2020-12-09 07:29:26 -05:00
|
|
|
{
|
|
|
|
bool new_group = false;
|
2021-01-14 07:34:30 -05:00
|
|
|
gdma_group_t *group = NULL;
|
2021-11-07 21:26:52 -05:00
|
|
|
gdma_group_t *pre_alloc_group = heap_caps_calloc(1, sizeof(gdma_group_t), GDMA_MEM_ALLOC_CAPS);
|
2021-01-14 07:34:30 -05:00
|
|
|
if (!pre_alloc_group) {
|
|
|
|
goto out;
|
|
|
|
}
|
2023-06-21 07:00:59 -04:00
|
|
|
|
2020-12-09 07:29:26 -05:00
|
|
|
portENTER_CRITICAL(&s_platform.spinlock);
|
|
|
|
if (!s_platform.groups[group_id]) {
|
2021-01-14 07:34:30 -05:00
|
|
|
new_group = true;
|
|
|
|
group = pre_alloc_group;
|
|
|
|
s_platform.groups[group_id] = group; // register to platform
|
2020-12-09 07:29:26 -05:00
|
|
|
} else {
|
|
|
|
group = s_platform.groups[group_id];
|
|
|
|
}
|
2021-01-14 07:34:30 -05:00
|
|
|
// someone acquired the group handle means we have a new object that refer to this group
|
2021-02-07 22:55:49 -05:00
|
|
|
s_platform.group_ref_counts[group_id]++;
|
2020-12-09 07:29:26 -05:00
|
|
|
portEXIT_CRITICAL(&s_platform.spinlock);
|
|
|
|
|
|
|
|
if (new_group) {
|
2023-06-21 07:00:59 -04:00
|
|
|
group->group_id = group_id;
|
|
|
|
group->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
// enable APB to access GDMA registers
|
2023-08-24 04:09:24 -04:00
|
|
|
GDMA_RCC_ATOMIC() {
|
|
|
|
gdma_ll_enable_bus_clock(group_id, true);
|
|
|
|
gdma_ll_reset_register(group_id);
|
|
|
|
}
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_config_t config = {
|
|
|
|
.group_id = group_id,
|
|
|
|
};
|
|
|
|
hal_init(&group->hal, &config);
|
|
|
|
ESP_LOGD(TAG, "new group (%d) at %p", group_id, group);
|
2021-01-14 07:34:30 -05:00
|
|
|
} else {
|
|
|
|
free(pre_alloc_group);
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
2021-01-14 07:34:30 -05:00
|
|
|
out:
|
2020-12-09 07:29:26 -05:00
|
|
|
return group;
|
|
|
|
}
|
|
|
|
|
2021-12-13 21:08:26 -05:00
|
|
|
static void gdma_release_pair_handle(gdma_pair_t *pair)
|
2020-12-09 07:29:26 -05:00
|
|
|
{
|
|
|
|
gdma_group_t *group = pair->group;
|
|
|
|
int pair_id = pair->pair_id;
|
|
|
|
bool do_deinitialize = false;
|
|
|
|
|
2021-02-07 22:55:49 -05:00
|
|
|
portENTER_CRITICAL(&group->spinlock);
|
|
|
|
group->pair_ref_counts[pair_id]--;
|
|
|
|
if (group->pair_ref_counts[pair_id] == 0) {
|
|
|
|
assert(group->pairs[pair_id]);
|
|
|
|
do_deinitialize = true;
|
|
|
|
group->pairs[pair_id] = NULL; // deregister from pair
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
2021-02-07 22:55:49 -05:00
|
|
|
portEXIT_CRITICAL(&group->spinlock);
|
|
|
|
|
2020-12-09 07:29:26 -05:00
|
|
|
if (do_deinitialize) {
|
|
|
|
free(pair);
|
|
|
|
ESP_LOGD(TAG, "del pair (%d,%d)", group->group_id, pair_id);
|
2021-12-13 21:08:26 -05:00
|
|
|
gdma_release_group_handle(group);
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static gdma_pair_t *gdma_acquire_pair_handle(gdma_group_t *group, int pair_id)
|
|
|
|
{
|
|
|
|
bool new_pair = false;
|
2021-01-14 07:34:30 -05:00
|
|
|
gdma_pair_t *pair = NULL;
|
2021-11-07 21:26:52 -05:00
|
|
|
gdma_pair_t *pre_alloc_pair = heap_caps_calloc(1, sizeof(gdma_pair_t), GDMA_MEM_ALLOC_CAPS);
|
2021-01-14 07:34:30 -05:00
|
|
|
if (!pre_alloc_pair) {
|
|
|
|
goto out;
|
|
|
|
}
|
2023-06-21 07:00:59 -04:00
|
|
|
|
2020-12-09 07:29:26 -05:00
|
|
|
portENTER_CRITICAL(&group->spinlock);
|
|
|
|
if (!group->pairs[pair_id]) {
|
2021-01-14 07:34:30 -05:00
|
|
|
new_pair = true;
|
|
|
|
pair = pre_alloc_pair;
|
2023-06-21 07:00:59 -04:00
|
|
|
// register the pair to the group
|
|
|
|
group->pairs[pair_id] = pair;
|
2020-12-09 07:29:26 -05:00
|
|
|
} else {
|
|
|
|
pair = group->pairs[pair_id];
|
|
|
|
}
|
2021-01-14 07:34:30 -05:00
|
|
|
// someone acquired the pair handle means we have a new object that refer to this pair
|
2021-02-07 22:55:49 -05:00
|
|
|
group->pair_ref_counts[pair_id]++;
|
2020-12-09 07:29:26 -05:00
|
|
|
portEXIT_CRITICAL(&group->spinlock);
|
|
|
|
|
|
|
|
if (new_pair) {
|
2023-06-21 07:00:59 -04:00
|
|
|
pair->group = group;
|
|
|
|
pair->pair_id = pair_id;
|
|
|
|
pair->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
|
2021-02-07 22:55:49 -05:00
|
|
|
portENTER_CRITICAL(&s_platform.spinlock);
|
2023-06-21 07:00:59 -04:00
|
|
|
// pair obtains a reference to group, so increase it
|
|
|
|
s_platform.group_ref_counts[group->group_id]++;
|
2021-02-07 22:55:49 -05:00
|
|
|
portEXIT_CRITICAL(&s_platform.spinlock);
|
2023-06-21 07:00:59 -04:00
|
|
|
|
|
|
|
ESP_LOGD(TAG, "new pair (%d,%d) at %p", group->group_id, pair_id, pair);
|
2021-01-14 07:34:30 -05:00
|
|
|
} else {
|
|
|
|
free(pre_alloc_pair);
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
2021-01-14 07:34:30 -05:00
|
|
|
out:
|
2020-12-09 07:29:26 -05:00
|
|
|
return pair;
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t gdma_del_tx_channel(gdma_channel_t *dma_channel)
|
|
|
|
{
|
|
|
|
gdma_pair_t *pair = dma_channel->pair;
|
2021-02-07 22:55:49 -05:00
|
|
|
gdma_group_t *group = pair->group;
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
2021-12-13 21:08:26 -05:00
|
|
|
int pair_id = pair->pair_id;
|
|
|
|
int group_id = group->group_id;
|
2020-12-09 07:29:26 -05:00
|
|
|
gdma_tx_channel_t *tx_chan = __containerof(dma_channel, gdma_tx_channel_t, base);
|
|
|
|
portENTER_CRITICAL(&pair->spinlock);
|
|
|
|
pair->tx_chan = NULL;
|
|
|
|
pair->occupy_code &= ~SEARCH_REQUEST_TX_CHANNEL;
|
|
|
|
portEXIT_CRITICAL(&pair->spinlock);
|
|
|
|
|
2021-04-27 06:52:42 -04:00
|
|
|
if (dma_channel->intr) {
|
|
|
|
esp_intr_free(dma_channel->intr);
|
|
|
|
portENTER_CRITICAL(&pair->spinlock);
|
2023-07-11 04:32:54 -04:00
|
|
|
gdma_hal_enable_intr(hal, pair_id, GDMA_CHANNEL_DIRECTION_TX, UINT32_MAX, false); // disable all interrupt events
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_clear_intr(hal, pair->pair_id, GDMA_CHANNEL_DIRECTION_TX, UINT32_MAX); // clear all pending events
|
2021-04-27 06:52:42 -04:00
|
|
|
portEXIT_CRITICAL(&pair->spinlock);
|
2021-12-13 21:08:26 -05:00
|
|
|
ESP_LOGD(TAG, "uninstall interrupt service for tx channel (%d,%d)", group_id, pair_id);
|
2021-04-27 06:52:42 -04:00
|
|
|
}
|
|
|
|
|
2020-12-09 07:29:26 -05:00
|
|
|
free(tx_chan);
|
2021-12-13 21:08:26 -05:00
|
|
|
ESP_LOGD(TAG, "del tx channel (%d,%d)", group_id, pair_id);
|
|
|
|
// channel has a reference on pair, release it now
|
|
|
|
gdma_release_pair_handle(pair);
|
2020-12-09 07:29:26 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t gdma_del_rx_channel(gdma_channel_t *dma_channel)
|
|
|
|
{
|
|
|
|
gdma_pair_t *pair = dma_channel->pair;
|
2021-02-07 22:55:49 -05:00
|
|
|
gdma_group_t *group = pair->group;
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
2021-12-13 21:08:26 -05:00
|
|
|
int pair_id = pair->pair_id;
|
|
|
|
int group_id = group->group_id;
|
2020-12-09 07:29:26 -05:00
|
|
|
gdma_rx_channel_t *rx_chan = __containerof(dma_channel, gdma_rx_channel_t, base);
|
|
|
|
portENTER_CRITICAL(&pair->spinlock);
|
|
|
|
pair->rx_chan = NULL;
|
|
|
|
pair->occupy_code &= ~SEARCH_REQUEST_RX_CHANNEL;
|
|
|
|
portEXIT_CRITICAL(&pair->spinlock);
|
|
|
|
|
2021-04-27 06:52:42 -04:00
|
|
|
if (dma_channel->intr) {
|
|
|
|
esp_intr_free(dma_channel->intr);
|
|
|
|
portENTER_CRITICAL(&pair->spinlock);
|
2023-07-11 04:32:54 -04:00
|
|
|
gdma_hal_enable_intr(hal, pair_id, GDMA_CHANNEL_DIRECTION_RX, UINT32_MAX, false); // disable all interrupt events
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_clear_intr(hal, pair->pair_id, GDMA_CHANNEL_DIRECTION_RX, UINT32_MAX); // clear all pending events
|
2021-04-27 06:52:42 -04:00
|
|
|
portEXIT_CRITICAL(&pair->spinlock);
|
2021-12-13 21:08:26 -05:00
|
|
|
ESP_LOGD(TAG, "uninstall interrupt service for rx channel (%d,%d)", group_id, pair_id);
|
2021-04-27 06:52:42 -04:00
|
|
|
}
|
|
|
|
|
2020-12-09 07:29:26 -05:00
|
|
|
free(rx_chan);
|
2021-12-13 21:08:26 -05:00
|
|
|
ESP_LOGD(TAG, "del rx channel (%d,%d)", group_id, pair_id);
|
|
|
|
gdma_release_pair_handle(pair);
|
2020-12-09 07:29:26 -05:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
void gdma_default_rx_isr(void *args)
|
2020-12-09 07:29:26 -05:00
|
|
|
{
|
2021-04-27 06:52:42 -04:00
|
|
|
gdma_rx_channel_t *rx_chan = (gdma_rx_channel_t *)args;
|
|
|
|
gdma_pair_t *pair = rx_chan->base.pair;
|
2020-12-09 07:29:26 -05:00
|
|
|
gdma_group_t *group = pair->group;
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
|
|
|
int pair_id = pair->pair_id;
|
2020-12-09 07:29:26 -05:00
|
|
|
bool need_yield = false;
|
|
|
|
// clear pending interrupt event
|
2023-06-21 07:00:59 -04:00
|
|
|
uint32_t intr_status = gdma_hal_read_intr_status(hal, pair_id, GDMA_CHANNEL_DIRECTION_RX);
|
|
|
|
gdma_hal_clear_intr(hal, pair_id, GDMA_CHANNEL_DIRECTION_RX, intr_status);
|
2020-12-09 07:29:26 -05:00
|
|
|
|
2023-07-03 04:43:12 -04:00
|
|
|
/* Call on_recv_done before eof callbacks to ensure a correct sequence */
|
2023-06-02 07:48:24 -04:00
|
|
|
if ((intr_status & GDMA_LL_EVENT_RX_DONE) && rx_chan->cbs.on_recv_done) {
|
|
|
|
/* Here we don't return an event data in this callback.
|
|
|
|
* Because we can't get a determinant descriptor address
|
|
|
|
* that just finished processing by DMA controller.
|
|
|
|
* When the `rx_done` interrupt triggers, the finished descriptor should ideally
|
|
|
|
* stored in `in_desc_bf1` register, however, as it takes a while to
|
|
|
|
* get the `in_desc_bf1` in software, `in_desc_bf1` might have already refreshed,
|
|
|
|
* Therefore, instead of returning an unreliable descriptor, we choose to return nothing.
|
|
|
|
*/
|
|
|
|
need_yield |= rx_chan->cbs.on_recv_done(&rx_chan->base, NULL, rx_chan->user_data);
|
2023-05-25 04:08:50 -04:00
|
|
|
}
|
2023-07-03 04:43:12 -04:00
|
|
|
if ((intr_status & GDMA_LL_EVENT_RX_DESC_ERROR) && rx_chan->cbs.on_descr_err) {
|
|
|
|
need_yield |= rx_chan->cbs.on_descr_err(&rx_chan->base, NULL, rx_chan->user_data);
|
|
|
|
}
|
|
|
|
if ((intr_status & GDMA_LL_EVENT_RX_SUC_EOF) && rx_chan->cbs.on_recv_eof) {
|
2023-07-24 00:22:31 -04:00
|
|
|
uint32_t eof_addr = gdma_hal_get_eof_desc_addr(&group->hal, pair->pair_id, GDMA_CHANNEL_DIRECTION_RX, true);
|
2023-07-03 04:43:12 -04:00
|
|
|
gdma_event_data_t suc_eof_data = {
|
|
|
|
.rx_eof_desc_addr = eof_addr,
|
|
|
|
};
|
|
|
|
need_yield |= rx_chan->cbs.on_recv_eof(&rx_chan->base, &suc_eof_data, rx_chan->user_data);
|
|
|
|
}
|
|
|
|
if ((intr_status & GDMA_LL_EVENT_RX_ERR_EOF) && rx_chan->cbs.on_recv_eof) {
|
2023-07-24 00:22:31 -04:00
|
|
|
uint32_t eof_addr = gdma_hal_get_eof_desc_addr(&group->hal, pair->pair_id, GDMA_CHANNEL_DIRECTION_RX, false);
|
2023-07-03 04:43:12 -04:00
|
|
|
gdma_event_data_t err_eof_data = {
|
|
|
|
.rx_eof_desc_addr = eof_addr,
|
|
|
|
.flags.abnormal_eof = true,
|
|
|
|
};
|
|
|
|
need_yield |= rx_chan->cbs.on_recv_eof(&rx_chan->base, &err_eof_data, rx_chan->user_data);
|
|
|
|
}
|
2020-12-09 07:29:26 -05:00
|
|
|
|
2021-04-27 06:52:42 -04:00
|
|
|
if (need_yield) {
|
|
|
|
portYIELD_FROM_ISR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-21 07:00:59 -04:00
|
|
|
void gdma_default_tx_isr(void *args)
|
2021-04-27 06:52:42 -04:00
|
|
|
{
|
|
|
|
gdma_tx_channel_t *tx_chan = (gdma_tx_channel_t *)args;
|
|
|
|
gdma_pair_t *pair = tx_chan->base.pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
|
|
|
int pair_id = pair->pair_id;
|
2021-04-27 06:52:42 -04:00
|
|
|
bool need_yield = false;
|
|
|
|
// clear pending interrupt event
|
2023-06-21 07:00:59 -04:00
|
|
|
uint32_t intr_status = gdma_hal_read_intr_status(hal, pair_id, GDMA_CHANNEL_DIRECTION_TX);
|
|
|
|
gdma_hal_clear_intr(hal, pair_id, GDMA_CHANNEL_DIRECTION_TX, intr_status);
|
2021-04-27 06:52:42 -04:00
|
|
|
|
2023-06-02 07:48:24 -04:00
|
|
|
if ((intr_status & GDMA_LL_EVENT_TX_EOF) && tx_chan->cbs.on_trans_eof) {
|
2023-07-24 00:22:31 -04:00
|
|
|
uint32_t eof_addr = gdma_hal_get_eof_desc_addr(hal, pair_id, GDMA_CHANNEL_DIRECTION_TX, true);
|
2023-06-02 07:48:24 -04:00
|
|
|
gdma_event_data_t edata = {
|
2023-07-03 04:43:12 -04:00
|
|
|
.tx_eof_desc_addr = eof_addr,
|
2023-06-02 07:48:24 -04:00
|
|
|
};
|
|
|
|
need_yield |= tx_chan->cbs.on_trans_eof(&tx_chan->base, &edata, tx_chan->user_data);
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|
2023-06-02 07:48:24 -04:00
|
|
|
if ((intr_status & GDMA_LL_EVENT_TX_DESC_ERROR) && tx_chan->cbs.on_descr_err) {
|
|
|
|
need_yield |= tx_chan->cbs.on_descr_err(&tx_chan->base, NULL, tx_chan->user_data);
|
2023-05-25 04:08:50 -04:00
|
|
|
}
|
2020-12-09 07:29:26 -05:00
|
|
|
if (need_yield) {
|
|
|
|
portYIELD_FROM_ISR();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-27 06:52:42 -04:00
|
|
|
static esp_err_t gdma_install_rx_interrupt(gdma_rx_channel_t *rx_chan)
|
2020-12-09 07:29:26 -05:00
|
|
|
{
|
2021-04-20 00:20:43 -04:00
|
|
|
esp_err_t ret = ESP_OK;
|
2021-04-27 06:52:42 -04:00
|
|
|
gdma_pair_t *pair = rx_chan->base.pair;
|
2020-12-09 07:29:26 -05:00
|
|
|
gdma_group_t *group = pair->group;
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
|
|
|
int pair_id = pair->pair_id;
|
2021-04-27 06:52:42 -04:00
|
|
|
// pre-alloc a interrupt handle, with handler disabled
|
2021-11-07 21:26:52 -05:00
|
|
|
int isr_flags = GDMA_INTR_ALLOC_FLAGS;
|
2023-06-21 07:00:59 -04:00
|
|
|
#if GDMA_LL_AHB_TX_RX_SHARE_INTERRUPT
|
2022-08-08 02:50:58 -04:00
|
|
|
isr_flags |= ESP_INTR_FLAG_SHARED | ESP_INTR_FLAG_LOWMED;
|
2021-04-27 06:52:42 -04:00
|
|
|
#endif
|
2021-01-14 07:34:30 -05:00
|
|
|
intr_handle_t intr = NULL;
|
2023-06-21 07:00:59 -04:00
|
|
|
ret = esp_intr_alloc_intrstatus(gdma_periph_signals.groups[group->group_id].pairs[pair_id].rx_irq_id, isr_flags,
|
|
|
|
gdma_hal_get_intr_status_reg(hal, pair_id, GDMA_CHANNEL_DIRECTION_RX), GDMA_LL_RX_EVENT_MASK,
|
2021-04-27 06:52:42 -04:00
|
|
|
gdma_default_rx_isr, rx_chan, &intr);
|
2021-04-20 00:20:43 -04:00
|
|
|
ESP_GOTO_ON_ERROR(ret, err, TAG, "alloc interrupt failed");
|
2021-04-27 06:52:42 -04:00
|
|
|
rx_chan->base.intr = intr;
|
2020-12-09 07:29:26 -05:00
|
|
|
|
2021-04-27 06:52:42 -04:00
|
|
|
portENTER_CRITICAL(&pair->spinlock);
|
2023-07-11 04:32:54 -04:00
|
|
|
gdma_hal_enable_intr(hal, pair_id, GDMA_CHANNEL_DIRECTION_RX, UINT32_MAX, false); // disable all interrupt events
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_clear_intr(hal, pair_id, GDMA_CHANNEL_DIRECTION_RX, UINT32_MAX); // clear all pending events
|
2021-04-27 06:52:42 -04:00
|
|
|
portEXIT_CRITICAL(&pair->spinlock);
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_LOGD(TAG, "install interrupt service for rx channel (%d,%d)", group->group_id, pair_id);
|
2021-04-27 06:52:42 -04:00
|
|
|
|
|
|
|
err:
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static esp_err_t gdma_install_tx_interrupt(gdma_tx_channel_t *tx_chan)
|
|
|
|
{
|
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
gdma_pair_t *pair = tx_chan->base.pair;
|
|
|
|
gdma_group_t *group = pair->group;
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_context_t *hal = &group->hal;
|
|
|
|
int pair_id = pair->pair_id;
|
2021-04-27 06:52:42 -04:00
|
|
|
// pre-alloc a interrupt handle, with handler disabled
|
2021-11-07 21:26:52 -05:00
|
|
|
int isr_flags = GDMA_INTR_ALLOC_FLAGS;
|
2023-06-21 07:00:59 -04:00
|
|
|
#if GDMA_LL_AHB_TX_RX_SHARE_INTERRUPT
|
2022-08-08 02:50:58 -04:00
|
|
|
isr_flags |= ESP_INTR_FLAG_SHARED | ESP_INTR_FLAG_LOWMED;
|
2021-04-27 06:52:42 -04:00
|
|
|
#endif
|
|
|
|
intr_handle_t intr = NULL;
|
2023-06-21 07:00:59 -04:00
|
|
|
ret = esp_intr_alloc_intrstatus(gdma_periph_signals.groups[group->group_id].pairs[pair_id].tx_irq_id, isr_flags,
|
|
|
|
gdma_hal_get_intr_status_reg(hal, pair_id, GDMA_CHANNEL_DIRECTION_TX), GDMA_LL_TX_EVENT_MASK,
|
2021-04-27 06:52:42 -04:00
|
|
|
gdma_default_tx_isr, tx_chan, &intr);
|
|
|
|
ESP_GOTO_ON_ERROR(ret, err, TAG, "alloc interrupt failed");
|
|
|
|
tx_chan->base.intr = intr;
|
|
|
|
|
|
|
|
portENTER_CRITICAL(&pair->spinlock);
|
2023-07-11 04:32:54 -04:00
|
|
|
gdma_hal_enable_intr(hal, pair_id, GDMA_CHANNEL_DIRECTION_TX, UINT32_MAX, false); // disable all interrupt events
|
2023-06-21 07:00:59 -04:00
|
|
|
gdma_hal_clear_intr(hal, pair_id, GDMA_CHANNEL_DIRECTION_TX, UINT32_MAX); // clear all pending events
|
2021-04-27 06:52:42 -04:00
|
|
|
portEXIT_CRITICAL(&pair->spinlock);
|
2023-06-21 07:00:59 -04:00
|
|
|
ESP_LOGD(TAG, "install interrupt service for tx channel (%d,%d)", group->group_id, pair_id);
|
2020-12-09 07:29:26 -05:00
|
|
|
|
|
|
|
err:
|
2021-04-20 00:20:43 -04:00
|
|
|
return ret;
|
2020-12-09 07:29:26 -05:00
|
|
|
}
|