driver/rmt: new features on esp32s2

1. add receive carrier remove function
2. add pingpong receive function
This commit is contained in:
chenjianqiang 2020-01-13 14:38:24 +08:00 committed by morris
parent c71445a9b9
commit ecbe0dad1b
6 changed files with 243 additions and 3 deletions

View File

@ -56,6 +56,13 @@ typedef struct {
uint16_t idle_threshold; /*!< RMT RX idle threshold */
uint8_t filter_ticks_thresh; /*!< RMT filter tick number */
bool filter_en; /*!< RMT receiver filter enable */
#ifdef RMT_SUPPORT_RX_DEMODULATION
bool rm_carrier; /*!< RMT receiver remove carrier enable */
uint16_t high_thres; /*!< The threshold of carrier high level tick number */
uint16_t low_thres; /*!< The threshold of carrier low level tick number */
rmt_carrier_level_t carrier_level; /*!< The level need to remove carrier */
#endif
} rmt_rx_config_t;
/**

View File

@ -79,6 +79,12 @@ typedef struct {
#endif
rmt_item32_t *tx_buf;
RingbufHandle_t rx_buf;
#ifdef RMT_SUPPORT_RX_PINGPONG_MODE
rmt_item32_t *rx_item_buf;
uint32_t rx_item_buf_size;
uint32_t rx_item_len;
uint32_t rx_item_start_idx;
#endif
sample_to_rmt_t sample_to_rmt;
size_t sample_size_remain;
const uint8_t *sample_cur;
@ -213,6 +219,17 @@ esp_err_t rmt_rx_start(rmt_channel_t channel, bool rx_idx_rst)
}
rmt_ll_clear_rx_end_interrupt(p_rmt_obj[channel]->hal.regs, channel);
rmt_ll_enable_rx_end_interrupt(p_rmt_obj[channel]->hal.regs, channel, true);
#ifdef RMT_SUPPORT_RX_PINGPONG_MODE
const uint32_t item_block_len = rmt_ll_get_mem_blocks(p_rmt_obj[channel]->hal.regs, channel) * RMT_MEM_ITEM_NUM;
rmt_ll_set_rx_limit(p_rmt_obj[channel]->hal.regs, channel, item_block_len / 2);
p_rmt_obj[channel]->rx_item_start_idx = 0;
p_rmt_obj[channel]->rx_item_len = 0;
if (rmt_ll_get_rx_pingpong_en(p_rmt_obj[channel]->hal.regs, channel)) {
rmt_ll_enable_rx_thres_interrupt(p_rmt_obj[channel]->hal.regs, channel, true);
}
#endif
rmt_ll_enable_rx(p_rmt_obj[channel]->hal.regs, channel, true);
RMT_EXIT_CRITICAL();
return ESP_OK;
@ -224,6 +241,9 @@ esp_err_t rmt_rx_stop(rmt_channel_t channel)
RMT_ENTER_CRITICAL();
rmt_ll_enable_rx(p_rmt_obj[channel]->hal.regs, channel, false);
rmt_ll_enable_rx_end_interrupt(p_rmt_obj[channel]->hal.regs, channel, false);
#ifdef RMT_SUPPORT_RX_PINGPONG_MODE
rmt_ll_enable_rx_thres_interrupt(p_rmt_obj[channel]->hal.regs, channel, false);
#endif
RMT_EXIT_CRITICAL();
return ESP_OK;
}
@ -358,6 +378,25 @@ esp_err_t rmt_set_rx_intr_en(rmt_channel_t channel, bool en)
return ESP_OK;
}
#ifdef RMT_SUPPORT_RX_PINGPONG_MODE
esp_err_t rmt_set_rx_thr_intr_en(rmt_channel_t channel, bool en, uint16_t evt_thresh)
{
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
if (en) {
RMT_CHECK(evt_thresh <= 256, "RMT EVT THRESH ERR", ESP_ERR_INVALID_ARG);
RMT_ENTER_CRITICAL();
rmt_ll_set_rx_limit(p_rmt_obj[channel]->hal.regs, channel, evt_thresh);
rmt_ll_enable_rx_thres_interrupt(p_rmt_obj[channel]->hal.regs, channel, true);
RMT_EXIT_CRITICAL();
} else {
RMT_ENTER_CRITICAL();
rmt_ll_enable_rx_thres_interrupt(p_rmt_obj[channel]->hal.regs, channel, false);
RMT_EXIT_CRITICAL();
}
return ESP_OK;
}
#endif
esp_err_t rmt_set_err_intr_en(rmt_channel_t channel, bool en)
{
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
@ -490,6 +529,17 @@ static esp_err_t rmt_internal_config(rmt_dev_t *dev, const rmt_config_t *rmt_par
/* Set RX filter */
rmt_ll_set_rx_filter_thres(dev, channel, filter_cnt);
rmt_ll_enable_rx_filter(dev, channel, rmt_param->rx_config.filter_en);
#ifdef RMT_SUPPORT_RX_PINGPONG_MODE
rmt_ll_enable_rx_pingpong(dev, channel, true);
#endif
#ifdef RMT_SUPPORT_RX_DEMODULATION
rmt_ll_enable_rx_carrier_rm(dev, channel, rmt_param->rx_config.rm_carrier);
rmt_ll_set_carrier_rm_high_thres_ticks(dev, channel, rmt_param->rx_config.high_thres);
rmt_ll_set_carrier_rm_low_thres_ticks(dev, channel, rmt_param->rx_config.low_thres);
rmt_ll_set_carrier_to_level(dev, channel, rmt_param->rx_config.carrier_level);
#endif
RMT_EXIT_CRITICAL();
ESP_LOGD(RMT_TAG, "Rmt Rx Channel %u|Gpio %u|Sclk_Hz %u|Div %u|Thresold %u|Filter %u",
@ -660,13 +710,28 @@ static void IRAM_ATTR rmt_driver_isr_default(void *arg)
rmt_ll_set_mem_owner(p_rmt_obj[channel]->hal.regs, channel, RMT_MEM_OWNER_SW);
if (p_rmt->rx_buf) {
addr = RMTMEM.chan[channel].data32;
#ifdef RMT_SUPPORT_RX_PINGPONG_MODE
if(item_len > p_rmt->rx_item_start_idx) {
item_len = item_len - p_rmt->rx_item_start_idx;
}
memcpy((void*)(p_rmt->rx_item_buf + p_rmt->rx_item_len), (void*)(addr + p_rmt->rx_item_start_idx), item_len * 4);
p_rmt->rx_item_len += item_len;
BaseType_t res = xRingbufferSendFromISR(p_rmt->rx_buf, (void *)(p_rmt->rx_item_buf), p_rmt->rx_item_len * 4, &HPTaskAwoken);
#else
BaseType_t res = xRingbufferSendFromISR(p_rmt->rx_buf, (void *)addr, item_len * 4, &HPTaskAwoken);
#endif
if (res == pdFALSE) {
ESP_EARLY_LOGE(RMT_TAG, "RMT RX BUFFER FULL");
}
} else {
ESP_EARLY_LOGE(RMT_TAG, "RMT RX BUFFER ERROR");
}
#ifdef RMT_SUPPORT_RX_PINGPONG_MODE
p_rmt->rx_item_start_idx = 0;
p_rmt->rx_item_len = 0;
memset((void*)p_rmt->rx_item_buf, 0, p_rmt->rx_item_buf_size);
#endif
rmt_ll_reset_rx_pointer(p_rmt_obj[channel]->hal.regs, channel);
rmt_ll_set_mem_owner(p_rmt_obj[channel]->hal.regs, channel, RMT_MEM_OWNER_HW);
rmt_ll_enable_rx(p_rmt_obj[channel]->hal.regs, channel, true);
@ -674,6 +739,30 @@ static void IRAM_ATTR rmt_driver_isr_default(void *arg)
rmt_ll_clear_rx_end_interrupt(hal->regs, channel);
}
#ifdef RMT_SUPPORT_RX_PINGPONG_MODE
// Rx thres interrupt
status = rmt_ll_get_rx_thres_interrupt_status(hal->regs);
while (status) {
channel = __builtin_ffs(status) - 1;
status &= ~(1 << channel);
rmt_obj_t *p_rmt = p_rmt_obj[channel];
int mem_item_size = rmt_ll_get_mem_blocks(p_rmt_obj[channel]->hal.regs, channel) * RMT_MEM_ITEM_NUM;
int rx_thres_lim = rmt_ll_get_rx_limit(p_rmt_obj[channel]->hal.regs, channel);
int item_len = (p_rmt->rx_item_start_idx == 0) ? rx_thres_lim : (mem_item_size - rx_thres_lim);
if((p_rmt->rx_item_len + item_len) < (p_rmt->rx_item_buf_size / 4)) {
memcpy((void*)(p_rmt->rx_item_buf + p_rmt->rx_item_len), (void*)(RMTMEM.chan[channel].data32 + p_rmt->rx_item_start_idx), item_len * 4);
p_rmt->rx_item_len += item_len;
p_rmt->rx_item_start_idx += item_len;
if (p_rmt->rx_item_start_idx >= mem_item_size) {
p_rmt->rx_item_start_idx = 0;
}
} else {
ESP_EARLY_LOGE(RMT_TAG, "---RMT RX BUFFER ERROR %d\n", sizeof(p_rmt->rx_item_buf));
}
rmt_ll_clear_rx_thres_interrupt(hal->regs, channel);
}
#endif
// Err interrupt
status = rmt_ll_get_err_interrupt_status(hal->regs);
while (status) {
@ -711,6 +800,9 @@ esp_err_t rmt_driver_uninstall(rmt_channel_t channel)
rmt_set_err_intr_en(channel, 0);
rmt_set_tx_intr_en(channel, 0);
rmt_set_tx_thr_intr_en(channel, 0, 0xffff);
#ifdef RMT_SUPPORT_RX_PINGPONG_MODE
rmt_set_rx_thr_intr_en(channel, 0, 0xffff);
#endif
_lock_acquire_recursive(&rmt_driver_isr_lock);
@ -743,6 +835,14 @@ esp_err_t rmt_driver_uninstall(rmt_channel_t channel)
p_rmt_obj[channel]->sample_to_rmt = NULL;
}
#ifdef RMT_SUPPORT_RX_PINGPONG_MODE
if (p_rmt_obj[channel]->rx_item_buf) {
free(p_rmt_obj[channel]->rx_item_buf);
p_rmt_obj[channel]->rx_item_buf = NULL;
p_rmt_obj[channel]->rx_item_buf_size = 0;
}
#endif
free(p_rmt_obj[channel]);
p_rmt_obj[channel] = NULL;
return ESP_OK;
@ -804,6 +904,26 @@ esp_err_t rmt_driver_install(rmt_channel_t channel, size_t rx_buf_size, int intr
if (p_rmt_obj[channel]->rx_buf == NULL && rx_buf_size > 0) {
p_rmt_obj[channel]->rx_buf = xRingbufferCreate(rx_buf_size, RINGBUF_TYPE_NOSPLIT);
}
#ifdef RMT_SUPPORT_RX_PINGPONG_MODE
if (p_rmt_obj[channel]->rx_item_buf == NULL && rx_buf_size > 0) {
#if !CONFIG_SPIRAM_USE_MALLOC
p_rmt_obj[channel]->rx_item_buf = (rmt_item32_t *)malloc(rx_buf_size);
#else
if (p_rmt_obj[channel]->intr_alloc_flags & ESP_INTR_FLAG_IRAM) {
p_rmt_obj[channel]->rx_item_buf = (rmt_item32_t *)malloc(rx_buf_size);
} else {
p_rmt_obj[channel]->rx_item_buf = (rmt_item32_t *)heap_caps_calloc(1, rx_buf_size, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
}
#endif
if (p_rmt_obj[channel]->rx_item_buf == NULL) {
ESP_LOGE(RMT_TAG, "RMT malloc fail");
return ESP_FAIL;
}
p_rmt_obj[channel]->rx_item_buf_size = rx_buf_size;
}
#endif
rmt_set_err_intr_en(channel, 1);
_lock_acquire_recursive(&rmt_driver_isr_lock);

View File

@ -312,3 +312,61 @@ TEST_CASE("RMT TX stop", "[rmt]")
TEST_ASSERT(num < count);
rmt_clean_testbench(tx_channel, rx_channel);
}
#ifdef RMT_SUPPORT_RX_DEMODULATION
/**
* @brief RMT demoudulation receiver initialization
*/
static void rx_demoudulation_init(void)
{
rmt_rx_config_t rx_cfg = {
.filter_en = true,
.filter_ticks_thresh = 100,
.idle_threshold = RMT_ITEM32_TIMEOUT_US / 10 * (RMT_TICK_10_US),
.rm_carrier = true,
.high_thres = 20,
.low_thres = 20,
.carrier_level = RMT_CARRIER_LEVEL_HIGH,
};
rmt_config_t rmt_rx = {
.channel = RMT_RX_CHANNEL,
.gpio_num = RMT_RX_GPIO_NUM,
.clk_div = RMT_CLK_DIV,
.mem_block_num = 1,
.rmt_mode = RMT_MODE_RX,
.rx_config = rx_cfg,
};
rmt_config(&rmt_rx);
rmt_driver_install(rmt_rx.channel, (sizeof(rmt_item32_t) * DATA_ITEM_NUM * (RMT_TX_DATA_NUM + 6)), 0);
}
TEST_CASE("RMT carrier TX and RX", "[rmt][test_env=UT_T1_RMT]")
{
rx_demoudulation_init();
RingbufHandle_t rb = NULL;
rmt_get_ringbuf_handle(RMT_RX_CHANNEL, &rb);
rmt_rx_start(RMT_RX_CHANNEL, 1);
ESP_LOGI(TAG, "Star receiving RMT data...");
tx_init();
rmt_set_tx_carrier(RMT_TX_CHANNEL, true, 1052, 1052, RMT_CARRIER_LEVEL_HIGH);
uint16_t cmd = 0x0;
uint16_t addr = 0x11;
int num_items = DATA_ITEM_NUM * RMT_TX_DATA_NUM;
rmt_item32_t *items = calloc(num_items + 1, sizeof(rmt_item32_t));
vTaskDelay(pdMS_TO_TICKS(2000));
ESP_LOGI(TAG, "Sending RMT data...");
// send data
set_tx_data(RMT_TX_CHANNEL, cmd, addr, num_items, items, 0);
// wait until tx done
rmt_write_items(RMT_TX_CHANNEL, items, num_items, 1);
free(items);
// receive data
uint16_t tmp = get_rx_data(rb);
TEST_ASSERT(tmp == RMT_TX_DATA_NUM);
TEST_ESP_OK(rmt_driver_uninstall(RMT_TX_CHANNEL));
TEST_ESP_OK(rmt_driver_uninstall(RMT_RX_CHANNEL));
}
#endif

View File

@ -20,6 +20,8 @@ extern "C" {
#define RMT_CHANNEL_MEM_WORDS (64) /*!< Each channel owns 64 words memory */
#define RMT_CHANNELS_NUM (4) /*!< Total 4 channels */
#define RMT_SUPPORT_RX_PINGPONG_MODE (1)
#define RMT_SUPPORT_RX_DEMODULATION (1)
#ifdef __cplusplus
}

View File

@ -59,12 +59,12 @@ static inline void rmt_ll_enable_rx(rmt_dev_t *dev, uint32_t channel, bool enabl
static inline void rmt_ll_power_down_mem(rmt_dev_t *dev, uint32_t channel, bool enable)
{
dev->conf_ch[channel].conf0.mem_pd = enable;
dev->conf_ch[0].conf0.mem_pd = enable; // Only conf0 register of channel0 has `mem_pd`
}
static inline bool rmt_ll_is_mem_power_down(rmt_dev_t *dev, uint32_t channel)
{
return dev->conf_ch[channel].conf0.mem_pd;
return dev->conf_ch[0].conf0.mem_pd; // Only conf0 register of channel0 has `mem_pd`
}
static inline void rmt_ll_set_mem_blocks(rmt_dev_t *dev, uint32_t channel, uint8_t block_num)

View File

@ -55,12 +55,13 @@ static inline void rmt_ll_enable_rx(rmt_dev_t *dev, uint32_t channel, bool enabl
static inline void rmt_ll_power_down_mem(rmt_dev_t *dev, uint32_t channel, bool enable)
{
dev->apb_conf.mem_force_pu = enable;
dev->apb_conf.mem_force_pd = enable;
}
static inline bool rmt_ll_is_mem_power_down(rmt_dev_t *dev, uint32_t channel)
{
return dev->apb_conf.mem_force_pd;
return (dev->apb_conf.mem_force_pu) && (dev->apb_conf.mem_force_pd);
}
static inline void rmt_ll_set_mem_blocks(rmt_dev_t *dev, uint32_t channel, uint8_t block_num)
@ -173,6 +174,16 @@ static inline void rmt_ll_set_tx_limit(rmt_dev_t *dev, uint32_t channel, uint32_
dev->tx_lim_ch[channel].limit = limit;
}
static inline void rmt_ll_set_rx_limit(rmt_dev_t *dev, uint32_t channel, uint32_t limit)
{
dev->tx_lim_ch[channel].rx_lim = limit;
}
static inline uint32_t rmt_ll_get_rx_limit(rmt_dev_t *dev, uint32_t channel)
{
return dev->tx_lim_ch[channel].rx_lim;
}
static inline void rmt_ll_enable_tx_end_interrupt(rmt_dev_t *dev, uint32_t channel, bool enable)
{
dev->int_ena.val &= ~(1 << (channel * 3));
@ -197,6 +208,12 @@ static inline void rmt_ll_enable_tx_thres_interrupt(rmt_dev_t *dev, uint32_t cha
dev->int_ena.val |= (enable << (channel + 12));
}
static inline void rmt_ll_enable_rx_thres_interrupt(rmt_dev_t *dev, uint32_t channel, bool enable)
{
dev->int_ena.val &= ~(1 << (channel + 20));
dev->int_ena.val |= (enable << (channel + 20));
}
static inline void rmt_ll_clear_tx_end_interrupt(rmt_dev_t *dev, uint32_t channel)
{
dev->int_clr.val = (1 << (channel * 3));
@ -217,6 +234,11 @@ static inline void rmt_ll_clear_tx_thres_interrupt(rmt_dev_t *dev, uint32_t chan
dev->int_clr.val = (1 << (channel + 12));
}
static inline void rmt_ll_clear_rx_thres_interrupt(rmt_dev_t *dev, uint32_t channel)
{
dev->int_clr.val = (1 << (channel + 20));
}
static inline uint32_t rmt_ll_get_tx_end_interrupt_status(rmt_dev_t *dev)
{
uint32_t status = dev->int_st.val;
@ -241,6 +263,12 @@ static inline uint32_t rmt_ll_get_tx_thres_interrupt_status(rmt_dev_t *dev)
return (status & 0xF000) >> 12;
}
static inline uint32_t rmt_ll_get_rx_thres_interrupt_status(rmt_dev_t *dev)
{
uint32_t status = dev->int_st.val;
return (status & 0xF00000) >> 20;
}
static inline void rmt_ll_set_carrier_high_low_ticks(rmt_dev_t *dev, uint32_t channel, uint32_t high_ticks, uint32_t low_ticks)
{
dev->carrier_duty_ch[channel].high = high_ticks;
@ -271,6 +299,31 @@ static inline void rmt_ll_write_memory(rmt_mem_t *mem, uint32_t channel, const r
}
}
static inline void rmt_ll_enable_rx_pingpong(rmt_dev_t *dev, uint32_t channel, bool enable)
{
dev->conf_ch[channel].conf1.chk_rx_carrier_en = enable;
}
static inline bool rmt_ll_get_rx_pingpong_en(rmt_dev_t *dev, uint32_t channel)
{
return dev->conf_ch[channel].conf1.chk_rx_carrier_en;
}
static inline void rmt_ll_enable_rx_carrier_rm(rmt_dev_t *dev, uint32_t channel, bool enable)
{
dev->conf_ch[channel].conf0.carrier_en = enable;
}
static inline void rmt_ll_set_carrier_rm_high_thres_ticks(rmt_dev_t *dev, uint32_t channel, uint16_t high_ticks)
{
dev->ch_rx_carrier_rm[channel].carrier_high_thres_ch = high_ticks;
}
static inline void rmt_ll_set_carrier_rm_low_thres_ticks(rmt_dev_t *dev, uint32_t channel, uint16_t low_ticks)
{
dev->ch_rx_carrier_rm[channel].carrier_low_thres_ch = low_ticks;
}
/************************************************************************************************
* Following Low Level APIs only used for backward compatible, will be deprecated in the future!
***********************************************************************************************/