mirror of
https://github.com/espressif/esp-idf.git
synced 2025-09-09 16:38:10 +00:00
Merge branch 'feature/add_rmt_hal' into 'master'
rmt: add hal layer and new examples Closes IDF-841, IDF-844, and IDF-857 See merge request espressif/esp-idf!5649
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
||||
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed 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
|
||||
@@ -12,98 +12,38 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef _DRIVER_RMT_CTRL_H_
|
||||
#define _DRIVER_RMT_CTRL_H_
|
||||
#include "esp_err.h"
|
||||
#include "soc/rmt_periph.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "freertos/xtensa_api.h"
|
||||
#include "freertos/ringbuf.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/periph_ctrl.h"
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define RMT_MEM_BLOCK_BYTE_NUM (256)
|
||||
#define RMT_MEM_ITEM_NUM (RMT_MEM_BLOCK_BYTE_NUM/4)
|
||||
|
||||
typedef enum {
|
||||
RMT_CHANNEL_0 = 0, /*!< RMT Channel 0 */
|
||||
RMT_CHANNEL_1, /*!< RMT Channel 1 */
|
||||
RMT_CHANNEL_2, /*!< RMT Channel 2 */
|
||||
RMT_CHANNEL_3, /*!< RMT Channel 3 */
|
||||
//ESP32-S2 only have 4 channel
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32
|
||||
RMT_CHANNEL_4, /*!< RMT Channel 4 */
|
||||
RMT_CHANNEL_5, /*!< RMT Channel 5 */
|
||||
RMT_CHANNEL_6, /*!< RMT Channel 6 */
|
||||
RMT_CHANNEL_7, /*!< RMT Channel 7 */
|
||||
#endif
|
||||
RMT_CHANNEL_MAX
|
||||
} rmt_channel_t;
|
||||
|
||||
typedef enum {
|
||||
RMT_MEM_OWNER_TX = 0, /*!< RMT TX mode, RMT transmitter owns the memory block*/
|
||||
RMT_MEM_OWNER_RX = 1, /*!< RMT RX mode, RMT receiver owns the memory block*/
|
||||
RMT_MEM_OWNER_MAX,
|
||||
}rmt_mem_owner_t;
|
||||
|
||||
typedef enum {
|
||||
RMT_BASECLK_REF = 0, /*!< RMT source clock system reference tick, 1MHz by default (not supported in this version) */
|
||||
RMT_BASECLK_APB, /*!< RMT source clock is APB CLK, 80Mhz by default */
|
||||
RMT_BASECLK_MAX,
|
||||
} rmt_source_clk_t;
|
||||
|
||||
typedef enum {
|
||||
RMT_DATA_MODE_FIFO = 0, /*<! RMT memory access in FIFO mode */
|
||||
RMT_DATA_MODE_MEM = 1, /*<! RMT memory access in memory mode */
|
||||
RMT_DATA_MODE_MAX,
|
||||
} rmt_data_mode_t;
|
||||
|
||||
typedef enum {
|
||||
RMT_MODE_TX = 0, /*!< RMT TX mode */
|
||||
RMT_MODE_RX, /*!< RMT RX mode */
|
||||
RMT_MODE_MAX
|
||||
} rmt_mode_t;
|
||||
|
||||
typedef enum {
|
||||
RMT_IDLE_LEVEL_LOW = 0, /*!< RMT TX idle level: low Level */
|
||||
RMT_IDLE_LEVEL_HIGH, /*!< RMT TX idle level: high Level */
|
||||
RMT_IDLE_LEVEL_MAX,
|
||||
} rmt_idle_level_t;
|
||||
|
||||
typedef enum {
|
||||
RMT_CARRIER_LEVEL_LOW = 0, /*!< RMT carrier wave is modulated for low Level output */
|
||||
RMT_CARRIER_LEVEL_HIGH, /*!< RMT carrier wave is modulated for high Level output */
|
||||
RMT_CARRIER_LEVEL_MAX
|
||||
} rmt_carrier_level_t;
|
||||
|
||||
typedef enum {
|
||||
RMT_CHANNEL_UNINIT = 0, /*!< RMT channel uninitialized */
|
||||
RMT_CHANNEL_IDLE = 1, /*!< RMT channel status idle */
|
||||
RMT_CHANNEL_BUSY = 2, /*!< RMT channel status busy */
|
||||
} rmt_channel_status_t;
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include "esp_err.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/ringbuf.h"
|
||||
#include "soc/rmt_caps.h"
|
||||
#include "soc/rmt_struct.h"
|
||||
#include "hal/rmt_types.h"
|
||||
|
||||
/**
|
||||
* @brief Data struct of RMT channel status
|
||||
* @brief Define memory space of each RMT channel (in words = 4 bytes)
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
rmt_channel_status_t status[RMT_CHANNEL_MAX]; /*!< Store the current status of each channel */
|
||||
} rmt_channel_status_result_t;
|
||||
#define RMT_MEM_ITEM_NUM RMT_CHANNEL_MEM_WORDS
|
||||
|
||||
/**
|
||||
* @brief Data struct of RMT TX configure parameters
|
||||
*/
|
||||
typedef struct {
|
||||
bool loop_en; /*!< Enable sending RMT items in a loop */
|
||||
uint32_t carrier_freq_hz; /*!< RMT carrier frequency */
|
||||
uint8_t carrier_duty_percent; /*!< RMT carrier duty (%) */
|
||||
rmt_carrier_level_t carrier_level; /*!< Level of the RMT output, when the carrier is applied */
|
||||
bool carrier_en; /*!< RMT carrier enable */
|
||||
rmt_idle_level_t idle_level; /*!< RMT idle level */
|
||||
uint8_t carrier_duty_percent; /*!< RMT carrier duty (%) */
|
||||
bool carrier_en; /*!< RMT carrier enable */
|
||||
bool loop_en; /*!< Enable sending RMT items in a loop */
|
||||
bool idle_output_en; /*!< RMT idle level output enable */
|
||||
} rmt_tx_config_t;
|
||||
|
||||
@@ -111,9 +51,9 @@ typedef struct {
|
||||
* @brief Data struct of RMT RX configure parameters
|
||||
*/
|
||||
typedef struct {
|
||||
bool filter_en; /*!< RMT receiver filter enable */
|
||||
uint8_t filter_ticks_thresh; /*!< RMT filter tick number */
|
||||
uint16_t idle_threshold; /*!< RMT RX idle threshold */
|
||||
uint8_t filter_ticks_thresh; /*!< RMT filter tick number */
|
||||
bool filter_en; /*!< RMT receiver filter enable */
|
||||
} rmt_rx_config_t;
|
||||
|
||||
/**
|
||||
@@ -122,8 +62,8 @@ typedef struct {
|
||||
typedef struct {
|
||||
rmt_mode_t rmt_mode; /*!< RMT mode: transmitter or receiver */
|
||||
rmt_channel_t channel; /*!< RMT channel */
|
||||
uint8_t clk_div; /*!< RMT channel counter divider */
|
||||
gpio_num_t gpio_num; /*!< RMT GPIO number */
|
||||
uint8_t clk_div; /*!< RMT channel counter divider */
|
||||
uint8_t mem_block_num; /*!< RMT memory block number */
|
||||
union {
|
||||
rmt_tx_config_t tx_config; /*!< RMT TX parameter */
|
||||
@@ -131,8 +71,56 @@ typedef struct {
|
||||
};
|
||||
} rmt_config_t;
|
||||
|
||||
/**
|
||||
* @brief Default configuration for Tx channel
|
||||
*
|
||||
*/
|
||||
#define RMT_DEFAULT_CONFIG_TX(gpio, channel_id) \
|
||||
{ \
|
||||
.rmt_mode = RMT_MODE_TX, \
|
||||
.channel = channel_id, \
|
||||
.gpio_num = gpio, \
|
||||
.clk_div = 80, \
|
||||
.mem_block_num = 1, \
|
||||
.tx_config = { \
|
||||
.carrier_freq_hz = 38000, \
|
||||
.carrier_level = RMT_CARRIER_LEVEL_HIGH, \
|
||||
.idle_level = RMT_IDLE_LEVEL_LOW, \
|
||||
.carrier_duty_percent = 33, \
|
||||
.carrier_en = false, \
|
||||
.loop_en = false, \
|
||||
.idle_output_en = true, \
|
||||
} \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Default configuration for RX channel
|
||||
*
|
||||
*/
|
||||
#define RMT_DEFAULT_CONFIG_RX(gpio, channel_id) \
|
||||
{ \
|
||||
.rmt_mode = RMT_MODE_RX, \
|
||||
.channel = channel_id, \
|
||||
.gpio_num = gpio, \
|
||||
.clk_div = 80, \
|
||||
.mem_block_num = 1, \
|
||||
.rx_config = { \
|
||||
.idle_threshold = 12000, \
|
||||
.filter_ticks_thresh = 100, \
|
||||
.filter_en = true, \
|
||||
} \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief RMT interrupt handle
|
||||
*
|
||||
*/
|
||||
typedef intr_handle_t rmt_isr_handle_t;
|
||||
|
||||
/**
|
||||
* @brief Type of RMT Tx End callback function
|
||||
*
|
||||
*/
|
||||
typedef void (*rmt_tx_end_fn_t)(rmt_channel_t channel, void *arg);
|
||||
|
||||
/**
|
||||
@@ -154,8 +142,9 @@ typedef struct {
|
||||
* @param wanted_num The number of rmt format data that wanted to get.
|
||||
* @param[out] translated_size The size of the raw data that has been converted to rmt format,
|
||||
* it should return 0 if no data is converted in user callback.
|
||||
* @param[out] item_num The number of the rmt format data that actually converted to, it can be less than wanted_num if there is not enough raw data,
|
||||
* but cannot exceed wanted_num. it should return 0 if no data was converted.
|
||||
* @param[out] item_num The number of the rmt format data that actually converted to,
|
||||
* it can be less than wanted_num if there is not enough raw data, but cannot exceed wanted_num.
|
||||
* it should return 0 if no data was converted.
|
||||
*
|
||||
* @note
|
||||
* In fact, item_num should be a multiple of translated_size, e.g. :
|
||||
@@ -167,7 +156,7 @@ typedef void (*sample_to_rmt_t)(const void* src, rmt_item32_t* dest, size_t src_
|
||||
/**
|
||||
* @brief Set RMT clock divider, channel clock is divided from source clock.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param div_cnt RMT counter clock divider
|
||||
*
|
||||
* @return
|
||||
@@ -179,7 +168,7 @@ esp_err_t rmt_set_clk_div(rmt_channel_t channel, uint8_t div_cnt);
|
||||
/**
|
||||
* @brief Get RMT clock divider, channel clock is divided from source clock.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param div_cnt pointer to accept RMT counter divider
|
||||
*
|
||||
* @return
|
||||
@@ -195,7 +184,7 @@ esp_err_t rmt_get_clk_div(rmt_channel_t channel, uint8_t* div_cnt);
|
||||
* for longer than idle_thres channel clock cycles,
|
||||
* the receive process is finished.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param thresh RMT RX idle threshold
|
||||
*
|
||||
* @return
|
||||
@@ -211,7 +200,7 @@ esp_err_t rmt_set_rx_idle_thresh(rmt_channel_t channel, uint16_t thresh);
|
||||
* for longer than idle_thres channel clock cycles,
|
||||
* the receive process is finished.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param thresh pointer to accept RMT RX idle threshold value
|
||||
*
|
||||
* @return
|
||||
@@ -238,7 +227,7 @@ esp_err_t rmt_get_rx_idle_thresh(rmt_channel_t channel, uint16_t *thresh);
|
||||
* block of the next channel.
|
||||
* Channel 0 can use at most 8 blocks of memory, accordingly channel 7 can only use one memory block.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param rmt_mem_num RMT RX memory block number, one block has 64 * 32 bits.
|
||||
*
|
||||
* @return
|
||||
@@ -250,7 +239,7 @@ esp_err_t rmt_set_mem_block_num(rmt_channel_t channel, uint8_t rmt_mem_num);
|
||||
/**
|
||||
* @brief Get RMT memory block number
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param rmt_mem_num Pointer to accept RMT RX memory block number
|
||||
*
|
||||
* @return
|
||||
@@ -265,11 +254,11 @@ esp_err_t rmt_get_mem_block_num(rmt_channel_t channel, uint8_t* rmt_mem_num);
|
||||
* Set different values for carrier_high and carrier_low to set different frequency of carrier.
|
||||
* The unit of carrier_high/low is the source clock tick, not the divided channel counter clock.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param carrier_en Whether to enable output carrier.
|
||||
* @param high_level High level duration of carrier
|
||||
* @param low_level Low level duration of carrier.
|
||||
* @param carrier_level Configure the way carrier wave is modulated for channel 0-7.
|
||||
* @param carrier_level Configure the way carrier wave is modulated for channel.
|
||||
* - 1'b1:transmit on low output level
|
||||
* - 1'b0:transmit on high output level
|
||||
*
|
||||
@@ -284,7 +273,7 @@ esp_err_t rmt_set_tx_carrier(rmt_channel_t channel, bool carrier_en, uint16_t hi
|
||||
*
|
||||
* Reduce power consumed by memory. 1:memory is in low power state.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param pd_en RMT memory low power enable.
|
||||
*
|
||||
* @return
|
||||
@@ -296,7 +285,7 @@ esp_err_t rmt_set_mem_pd(rmt_channel_t channel, bool pd_en);
|
||||
/**
|
||||
* @brief Get RMT memory low power mode.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param pd_en Pointer to accept RMT memory low power mode.
|
||||
*
|
||||
* @return
|
||||
@@ -308,7 +297,7 @@ esp_err_t rmt_get_mem_pd(rmt_channel_t channel, bool* pd_en);
|
||||
/**
|
||||
* @brief Set RMT start sending data from memory.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param tx_idx_rst Set true to reset memory index for TX.
|
||||
* Otherwise, transmitter will continue sending from the last index in memory.
|
||||
*
|
||||
@@ -321,7 +310,7 @@ esp_err_t rmt_tx_start(rmt_channel_t channel, bool tx_idx_rst);
|
||||
/**
|
||||
* @brief Set RMT stop sending.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
*
|
||||
* @return
|
||||
* - ESP_ERR_INVALID_ARG Parameter error
|
||||
@@ -332,7 +321,7 @@ esp_err_t rmt_tx_stop(rmt_channel_t channel);
|
||||
/**
|
||||
* @brief Set RMT start receiving data.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param rx_idx_rst Set true to reset memory index for receiver.
|
||||
* Otherwise, receiver will continue receiving data to the last index in memory.
|
||||
*
|
||||
@@ -345,7 +334,7 @@ esp_err_t rmt_rx_start(rmt_channel_t channel, bool rx_idx_rst);
|
||||
/**
|
||||
* @brief Set RMT stop receiving data.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
*
|
||||
* @return
|
||||
* - ESP_ERR_INVALID_ARG Parameter error
|
||||
@@ -356,7 +345,7 @@ esp_err_t rmt_rx_stop(rmt_channel_t channel);
|
||||
/**
|
||||
* @brief Reset RMT TX/RX memory index.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
*
|
||||
* @return
|
||||
* - ESP_ERR_INVALID_ARG Parameter error
|
||||
@@ -367,7 +356,7 @@ esp_err_t rmt_memory_rw_rst(rmt_channel_t channel);
|
||||
/**
|
||||
* @brief Set RMT memory owner.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param owner To set when the transmitter or receiver can process the memory of channel.
|
||||
*
|
||||
* @return
|
||||
@@ -379,7 +368,7 @@ esp_err_t rmt_set_memory_owner(rmt_channel_t channel, rmt_mem_owner_t owner);
|
||||
/**
|
||||
* @brief Get RMT memory owner.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param owner Pointer to get memory owner.
|
||||
*
|
||||
* @return
|
||||
@@ -391,10 +380,10 @@ esp_err_t rmt_get_memory_owner(rmt_channel_t channel, rmt_mem_owner_t* owner);
|
||||
/**
|
||||
* @brief Set RMT tx loop mode.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param loop_en Enable RMT transmitter loop sending mode.
|
||||
* If set true, transmitter will continue sending from the first data
|
||||
* to the last data in channel 0-7 over and over again in a loop.
|
||||
* to the last data in channel over and over again in a loop.
|
||||
*
|
||||
* @return
|
||||
* - ESP_ERR_INVALID_ARG Parameter error
|
||||
@@ -405,7 +394,7 @@ esp_err_t rmt_set_tx_loop_mode(rmt_channel_t channel, bool loop_en);
|
||||
/**
|
||||
* @brief Get RMT tx loop mode.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param loop_en Pointer to accept RMT transmitter loop sending mode.
|
||||
*
|
||||
* @return
|
||||
@@ -417,10 +406,10 @@ esp_err_t rmt_get_tx_loop_mode(rmt_channel_t channel, bool* loop_en);
|
||||
/**
|
||||
* @brief Set RMT RX filter.
|
||||
*
|
||||
* In receive mode, channel 0-7 will ignore input pulse when the pulse width is smaller than threshold.
|
||||
* In receive mode, channel will ignore input pulse when the pulse width is smaller than threshold.
|
||||
* Counted in source clock, not divided counter clock.
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param rx_filter_en To enable RMT receiver filter.
|
||||
* @param thresh Threshold of pulse width for receiver.
|
||||
*
|
||||
@@ -437,7 +426,7 @@ esp_err_t rmt_set_rx_filter(rmt_channel_t channel, bool rx_filter_en, uint8_t th
|
||||
* 1. APB clock which is 80Mhz
|
||||
* 2. REF tick clock, which would be 1Mhz (not supported in this version).
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param base_clk To choose source clock for RMT module.
|
||||
*
|
||||
* @return
|
||||
@@ -453,7 +442,7 @@ esp_err_t rmt_set_source_clk(rmt_channel_t channel, rmt_source_clk_t base_clk);
|
||||
* 1. APB clock which is 80Mhz
|
||||
* 2. REF tick clock, which would be 1Mhz (not supported in this version).
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param src_clk Pointer to accept source clock for RMT module.
|
||||
*
|
||||
* @return
|
||||
@@ -465,9 +454,9 @@ esp_err_t rmt_get_source_clk(rmt_channel_t channel, rmt_source_clk_t* src_clk);
|
||||
/**
|
||||
* @brief Set RMT idle output level for transmitter
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param idle_out_en To enable idle level output.
|
||||
* @param level To set the output signal's level for channel 0-7 in idle state.
|
||||
* @param level To set the output signal's level for channel in idle state.
|
||||
*
|
||||
* @return
|
||||
* - ESP_ERR_INVALID_ARG Parameter error
|
||||
@@ -478,7 +467,7 @@ esp_err_t rmt_set_idle_level(rmt_channel_t channel, bool idle_out_en, rmt_idle_l
|
||||
/**
|
||||
* @brief Get RMT idle output level for transmitter
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param idle_out_en Pointer to accept value of enable idle.
|
||||
* @param level Pointer to accept value of output signal's level in idle state for specified channel.
|
||||
*
|
||||
@@ -491,7 +480,7 @@ esp_err_t rmt_get_idle_level(rmt_channel_t channel, bool* idle_out_en, rmt_idle_
|
||||
/**
|
||||
* @brief Get RMT status
|
||||
*
|
||||
* @param channel RMT channel (0-7)
|
||||
* @param channel RMT channel
|
||||
* @param status Pointer to accept channel status.
|
||||
* Please refer to RMT_CHnSTATUS_REG(n=0~7) in `rmt_reg.h` for more details of each field.
|
||||
*
|
||||
@@ -520,7 +509,7 @@ void rmt_clr_intr_enable_mask(uint32_t mask);
|
||||
/**
|
||||
* @brief Set RMT RX interrupt enable
|
||||
*
|
||||
* @param channel RMT channel (0 - 7)
|
||||
* @param channel RMT channel
|
||||
* @param en enable or disable RX interrupt.
|
||||
*
|
||||
* @return
|
||||
@@ -532,7 +521,7 @@ esp_err_t rmt_set_rx_intr_en(rmt_channel_t channel, bool en);
|
||||
/**
|
||||
* @brief Set RMT RX error interrupt enable
|
||||
*
|
||||
* @param channel RMT channel (0 - 7)
|
||||
* @param channel RMT channel
|
||||
* @param en enable or disable RX err interrupt.
|
||||
*
|
||||
* @return
|
||||
@@ -544,7 +533,7 @@ esp_err_t rmt_set_err_intr_en(rmt_channel_t channel, bool en);
|
||||
/**
|
||||
* @brief Set RMT TX interrupt enable
|
||||
*
|
||||
* @param channel RMT channel (0 - 7)
|
||||
* @param channel RMT channel
|
||||
* @param en enable or disable TX interrupt.
|
||||
*
|
||||
* @return
|
||||
@@ -558,7 +547,7 @@ esp_err_t rmt_set_tx_intr_en(rmt_channel_t channel, bool en);
|
||||
*
|
||||
* An interrupt will be triggered when the number of transmitted items reaches the threshold value
|
||||
*
|
||||
* @param channel RMT channel (0 - 7)
|
||||
* @param channel RMT channel
|
||||
* @param en enable or disable TX event interrupt.
|
||||
* @param evt_thresh RMT event interrupt threshold value
|
||||
*
|
||||
@@ -571,7 +560,7 @@ esp_err_t rmt_set_tx_thr_intr_en(rmt_channel_t channel, bool en, uint16_t evt_th
|
||||
/**
|
||||
* @brief Set RMT pin
|
||||
*
|
||||
* @param channel RMT channel (0 - 7)
|
||||
* @param channel RMT channel
|
||||
* @param mode TX or RX mode for RMT
|
||||
* @param gpio_num GPIO number to transmit or receive the signal.
|
||||
*
|
||||
@@ -627,7 +616,7 @@ esp_err_t rmt_isr_deregister(rmt_isr_handle_t handle);
|
||||
/**
|
||||
* @brief Fill memory data of channel with given RMT items.
|
||||
*
|
||||
* @param channel RMT channel (0 - 7)
|
||||
* @param channel RMT channel
|
||||
* @param item Pointer of items.
|
||||
* @param item_num RMT sending items number.
|
||||
* @param mem_offset Index offset of memory.
|
||||
@@ -641,7 +630,7 @@ esp_err_t rmt_fill_tx_items(rmt_channel_t channel, const rmt_item32_t* item, uin
|
||||
/**
|
||||
* @brief Initialize RMT driver
|
||||
*
|
||||
* @param channel RMT channel (0 - 7)
|
||||
* @param channel RMT channel
|
||||
* @param rx_buf_size Size of RMT RX ringbuffer. Can be 0 if the RX ringbuffer is not used.
|
||||
* @param intr_alloc_flags Flags for the RMT driver interrupt handler. Pass 0 for default flags. See esp_intr_alloc.h for details.
|
||||
* If ESP_INTR_FLAG_IRAM is used, please do not use the memory allocated from psram when calling rmt_write_items.
|
||||
@@ -657,7 +646,7 @@ esp_err_t rmt_driver_install(rmt_channel_t channel, size_t rx_buf_size, int intr
|
||||
/**
|
||||
* @brief Uninstall RMT driver.
|
||||
*
|
||||
* @param channel RMT channel (0 - 7)
|
||||
* @param channel RMT channel
|
||||
*
|
||||
* @return
|
||||
* - ESP_ERR_INVALID_ARG Parameter error
|
||||
@@ -678,12 +667,24 @@ esp_err_t rmt_driver_uninstall(rmt_channel_t channel);
|
||||
*/
|
||||
esp_err_t rmt_get_channel_status(rmt_channel_status_result_t *channel_status);
|
||||
|
||||
/**
|
||||
* @brief Get speed of channel's internal counter clock.
|
||||
*
|
||||
* @param channel RMT channel
|
||||
* @param[out] clock_hz counter clock speed, in hz
|
||||
*
|
||||
* @return
|
||||
* - ESP_ERR_INVALID_ARG Parameter is NULL
|
||||
* - ESP_OK Success
|
||||
*/
|
||||
esp_err_t rmt_get_counter_clock(rmt_channel_t channel, uint32_t *clock_hz);
|
||||
|
||||
/**
|
||||
* @brief RMT send waveform from rmt_item array.
|
||||
*
|
||||
* This API allows user to send waveform with any length.
|
||||
*
|
||||
* @param channel RMT channel (0 - 7)
|
||||
* @param channel RMT channel
|
||||
* @param rmt_item head point of RMT items array.
|
||||
* If ESP_INTR_FLAG_IRAM is used, please do not use the memory allocated from psram when calling rmt_write_items.
|
||||
* @param item_num RMT data item number.
|
||||
@@ -709,7 +710,7 @@ esp_err_t rmt_write_items(rmt_channel_t channel, const rmt_item32_t* rmt_item, i
|
||||
/**
|
||||
* @brief Wait RMT TX finished.
|
||||
*
|
||||
* @param channel RMT channel (0 - 7)
|
||||
* @param channel RMT channel
|
||||
* @param wait_time Maximum time in ticks to wait for transmission to be complete. If set 0, return immediately with ESP_ERR_TIMEOUT if TX is busy (polling).
|
||||
*
|
||||
* @return
|
||||
@@ -725,7 +726,7 @@ esp_err_t rmt_wait_tx_done(rmt_channel_t channel, TickType_t wait_time);
|
||||
*
|
||||
* Users can get the RMT RX ringbuffer handle, and process the RX data.
|
||||
*
|
||||
* @param channel RMT channel (0 - 7)
|
||||
* @param channel RMT channel
|
||||
* @param buf_handle Pointer to buffer handle to accept RX ringbuffer handle.
|
||||
*
|
||||
* @return
|
||||
@@ -739,7 +740,7 @@ esp_err_t rmt_get_ringbuf_handle(rmt_channel_t channel, RingbufHandle_t* buf_han
|
||||
* The callback will convert the raw data that needs to be sent to rmt format.
|
||||
* If a channel is initialized more than once, tha user callback will be replaced by the later.
|
||||
*
|
||||
* @param channel RMT channel (0 - 7).
|
||||
* @param channel RMT channel .
|
||||
* @param fn Point to the data conversion function.
|
||||
*
|
||||
* @return
|
||||
@@ -752,7 +753,7 @@ esp_err_t rmt_translator_init(rmt_channel_t channel, sample_to_rmt_t fn);
|
||||
* @brief Translate uint8_t type of data into rmt format and send it out.
|
||||
* Requires rmt_translator_init to init the translator first.
|
||||
*
|
||||
* @param channel RMT channel (0 - 7).
|
||||
* @param channel RMT channel .
|
||||
* @param src Pointer to the raw data.
|
||||
* @param src_size The size of the raw data.
|
||||
* @param wait_tx_done Set true to wait all data send done.
|
||||
@@ -777,40 +778,6 @@ esp_err_t rmt_write_sample(rmt_channel_t channel, const uint8_t *src, size_t src
|
||||
*/
|
||||
rmt_tx_end_callback_t rmt_register_tx_end_callback(rmt_tx_end_fn_t function, void *arg);
|
||||
|
||||
|
||||
/*
|
||||
* ----------------EXAMPLE OF RMT INTERRUPT ------------------
|
||||
* @code{c}
|
||||
*
|
||||
* rmt_isr_register(rmt_isr, NULL, 0); //hook the ISR handler for RMT interrupt
|
||||
* @endcode
|
||||
* @note
|
||||
* 0. If you have called rmt_driver_install, you don't need to set ISR handler any more.
|
||||
*
|
||||
* ----------------EXAMPLE OF INTERRUPT HANDLER ---------------
|
||||
* @code{c}
|
||||
* #include "esp_attr.h"
|
||||
* void IRAM_ATTR rmt_isr_handler(void* arg)
|
||||
* {
|
||||
* //read RMT interrupt status.
|
||||
* uint32_t intr_st = RMT.int_st.val;
|
||||
*
|
||||
* //you will find which channels have triggered an interrupt here,
|
||||
* //then, you can post some event to RTOS queue to process the event.
|
||||
* //later we will add a queue in the driver code.
|
||||
*
|
||||
* //clear RMT interrupt status.
|
||||
* RMT.int_clr.val = intr_st;
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
*--------------------------END OF EXAMPLE --------------------------
|
||||
*/
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* _DRIVER_RMT_CTRL_H_ */
|
||||
|
@@ -3,7 +3,7 @@
|
||||
// Licensed 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
|
||||
@@ -11,26 +11,26 @@
|
||||
// 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.
|
||||
#include <esp_types.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "freertos/xtensa_api.h"
|
||||
#include "freertos/ringbuf.h"
|
||||
#include <string.h>
|
||||
#include <sys/lock.h>
|
||||
#include "esp_intr_alloc.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_err.h"
|
||||
#include "soc/gpio_periph.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "driver/periph_ctrl.h"
|
||||
#include "driver/rmt.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "freertos/ringbuf.h"
|
||||
#include "soc/soc_memory_layout.h"
|
||||
|
||||
#include <sys/lock.h>
|
||||
#include "hal/rmt_hal.h"
|
||||
#include "hal/rmt_ll.h"
|
||||
|
||||
#define RMT_SOUCCE_CLK_APB (APB_CLK_FREQ) /*!< RMT source clock is APB_CLK */
|
||||
#define RMT_SOURCE_CLK_REF (1 * 1000000) /*!< not used yet */
|
||||
#define RMT_SOURCE_CLK(select) ((select == RMT_BASECLK_REF) ? (RMT_SOURCE_CLK_REF) : (RMT_SOUCCE_CLK_APB)) /*! RMT source clock frequency */
|
||||
|
||||
#define RMT_SOURCE_CLK(select) ((select == RMT_BASECLK_REF) ? (RMT_SOURCE_CLK_REF) : (RMT_SOUCCE_CLK_APB))
|
||||
|
||||
#define RMT_CHANNEL_ERROR_STR "RMT CHANNEL ERR"
|
||||
#define RMT_ADDR_ERROR_STR "RMT ADDRESS ERR"
|
||||
@@ -50,22 +50,26 @@
|
||||
#define RMT_PARAM_ERR_STR "RMT param error"
|
||||
|
||||
static const char *RMT_TAG = "rmt";
|
||||
static uint8_t s_rmt_driver_channels; // Bitmask (bits 0-7) of installed drivers' channels
|
||||
static rmt_isr_handle_t s_rmt_driver_intr_handle;
|
||||
|
||||
#define RMT_CHECK(a, str, ret_val) \
|
||||
if (!(a)) { \
|
||||
if (!(a)) \
|
||||
{ \
|
||||
ESP_LOGE(RMT_TAG, "%s(%d): %s", __FUNCTION__, __LINE__, str); \
|
||||
return (ret_val); \
|
||||
}
|
||||
|
||||
static uint8_t s_rmt_driver_channels; // Bitmask of installed drivers' channels
|
||||
|
||||
// Spinlock for protecting concurrent register-level access only
|
||||
static portMUX_TYPE rmt_spinlock = portMUX_INITIALIZER_UNLOCKED;
|
||||
#define RMT_ENTER_CRITICAL() portENTER_CRITICAL_SAFE(&rmt_spinlock)
|
||||
#define RMT_EXIT_CRITICAL() portEXIT_CRITICAL_SAFE(&rmt_spinlock)
|
||||
|
||||
// Mutex lock for protecting concurrent register/unregister of RMT channels' ISR
|
||||
static _lock_t rmt_driver_isr_lock;
|
||||
static rmt_isr_handle_t s_rmt_driver_intr_handle;
|
||||
|
||||
typedef struct {
|
||||
rmt_hal_context_t hal;
|
||||
size_t tx_offset;
|
||||
size_t tx_len_rem;
|
||||
size_t tx_sub_len;
|
||||
@@ -90,24 +94,12 @@ rmt_obj_t* p_rmt_obj[RMT_CHANNEL_MAX] = {0};
|
||||
// Event called when transmission is ended
|
||||
static rmt_tx_end_callback_t rmt_tx_end_callback;
|
||||
|
||||
static void rmt_set_tx_wrap_en(bool en)
|
||||
{
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
RMT.apb_conf.mem_tx_wrap_en = en;
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
}
|
||||
|
||||
static void rmt_set_data_mode(rmt_data_mode_t data_mode)
|
||||
{
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
RMT.apb_conf.fifo_mask = data_mode;
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
}
|
||||
|
||||
esp_err_t rmt_set_clk_div(rmt_channel_t channel, uint8_t div_cnt)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT.conf_ch[channel].conf0.div_cnt = div_cnt;
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_set_counter_clock_div(p_rmt_obj[channel]->hal.regs, channel, div_cnt);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -115,14 +107,18 @@ esp_err_t rmt_get_clk_div(rmt_channel_t channel, uint8_t* div_cnt)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK(div_cnt != NULL, RMT_ADDR_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
*div_cnt = RMT.conf_ch[channel].conf0.div_cnt;
|
||||
RMT_ENTER_CRITICAL();
|
||||
*div_cnt = (uint8_t)rmt_ll_get_counter_clock_div(p_rmt_obj[channel]->hal.regs, channel);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_set_rx_idle_thresh(rmt_channel_t channel, uint16_t thresh)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT.conf_ch[channel].conf0.idle_thres = thresh;
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_set_rx_idle_thres(p_rmt_obj[channel]->hal.regs, channel, thresh);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -130,7 +126,9 @@ esp_err_t rmt_get_rx_idle_thresh(rmt_channel_t channel, uint16_t *thresh)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK(thresh != NULL, RMT_ADDR_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
*thresh = RMT.conf_ch[channel].conf0.idle_thres;
|
||||
RMT_ENTER_CRITICAL();
|
||||
*thresh = (uint16_t)rmt_ll_get_rx_idle_thres(p_rmt_obj[channel]->hal.regs, channel);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -138,7 +136,9 @@ esp_err_t rmt_set_mem_block_num(rmt_channel_t channel, uint8_t rmt_mem_num)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK(rmt_mem_num <= RMT_CHANNEL_MAX - channel, RMT_MEM_CNT_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT.conf_ch[channel].conf0.mem_size = rmt_mem_num;
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_set_mem_blocks(p_rmt_obj[channel]->hal.regs, channel, rmt_mem_num);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -146,7 +146,9 @@ esp_err_t rmt_get_mem_block_num(rmt_channel_t channel, uint8_t* rmt_mem_num)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK(rmt_mem_num != NULL, RMT_ADDR_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
*rmt_mem_num = RMT.conf_ch[channel].conf0.mem_size;
|
||||
RMT_ENTER_CRITICAL();
|
||||
*rmt_mem_num = (uint8_t)rmt_ll_get_mem_blocks(p_rmt_obj[channel]->hal.regs, channel);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -155,86 +157,88 @@ esp_err_t rmt_set_tx_carrier(rmt_channel_t channel, bool carrier_en, uint16_t hi
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK(carrier_level < RMT_CARRIER_LEVEL_MAX, RMT_CARRIER_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT.carrier_duty_ch[channel].high = high_level;
|
||||
RMT.carrier_duty_ch[channel].low = low_level;
|
||||
RMT.conf_ch[channel].conf0.carrier_out_lv = carrier_level;
|
||||
RMT.conf_ch[channel].conf0.carrier_en = carrier_en;
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_set_carrier_high_low_ticks(p_rmt_obj[channel]->hal.regs, channel, high_level, low_level);
|
||||
rmt_ll_set_carrier_to_level(p_rmt_obj[channel]->hal.regs, channel, carrier_level);
|
||||
rmt_ll_enable_tx_carrier(p_rmt_obj[channel]->hal.regs, channel, carrier_en);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_set_mem_pd(rmt_channel_t channel, bool pd_en)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT.conf_ch[channel].conf0.mem_pd = pd_en;
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_power_down_mem(p_rmt_obj[channel]->hal.regs, channel, pd_en);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_get_mem_pd(rmt_channel_t channel, bool *pd_en)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
*pd_en = (bool) RMT.conf_ch[channel].conf0.mem_pd;
|
||||
RMT_ENTER_CRITICAL();
|
||||
*pd_en = rmt_ll_is_mem_power_down(p_rmt_obj[channel]->hal.regs, channel);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_tx_start(rmt_channel_t channel, bool tx_idx_rst)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
RMT_ENTER_CRITICAL();
|
||||
if (tx_idx_rst) {
|
||||
RMT.conf_ch[channel].conf1.mem_rd_rst = 1;
|
||||
rmt_ll_reset_tx_pointer(p_rmt_obj[channel]->hal.regs, channel);
|
||||
}
|
||||
RMT.conf_ch[channel].conf1.mem_owner = RMT_MEM_OWNER_TX;
|
||||
RMT.conf_ch[channel].conf1.tx_start = 1;
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
rmt_ll_clear_tx_end_interrupt(p_rmt_obj[channel]->hal.regs, channel);
|
||||
rmt_ll_enable_tx_end_interrupt(p_rmt_obj[channel]->hal.regs, channel, true);
|
||||
rmt_ll_start_tx(p_rmt_obj[channel]->hal.regs, channel);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_tx_stop(rmt_channel_t channel)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32
|
||||
RMTMEM.chan[channel].data32[0].val = 0;
|
||||
RMT.conf_ch[channel].conf1.tx_start = 0;
|
||||
#elif defined CONFIG_IDF_TARGET_ESP32S2BETA
|
||||
RMT.conf_ch[channel].conf1.tx_stop = 1;
|
||||
#endif
|
||||
RMT.conf_ch[channel].conf1.mem_rd_rst = 1;
|
||||
RMT.conf_ch[channel].conf1.mem_rd_rst = 0;
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_stop_tx(p_rmt_obj[channel]->hal.regs, channel);
|
||||
rmt_ll_reset_tx_pointer(p_rmt_obj[channel]->hal.regs, channel);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_rx_start(rmt_channel_t channel, bool rx_idx_rst)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_enable_rx(p_rmt_obj[channel]->hal.regs, channel, false);
|
||||
if (rx_idx_rst) {
|
||||
RMT.conf_ch[channel].conf1.mem_wr_rst = 1;
|
||||
rmt_ll_reset_rx_pointer(p_rmt_obj[channel]->hal.regs, channel);
|
||||
}
|
||||
RMT.conf_ch[channel].conf1.rx_en = 0;
|
||||
RMT.conf_ch[channel].conf1.mem_owner = RMT_MEM_OWNER_RX;
|
||||
RMT.conf_ch[channel].conf1.rx_en = 1;
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
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);
|
||||
rmt_ll_enable_rx(p_rmt_obj[channel]->hal.regs, channel, true);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_rx_stop(rmt_channel_t channel)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
RMT.conf_ch[channel].conf1.rx_en = 0;
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
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);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_memory_rw_rst(rmt_channel_t channel)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
RMT.conf_ch[channel].conf1.mem_rd_rst = 1;
|
||||
RMT.conf_ch[channel].conf1.mem_wr_rst = 1;
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_reset_tx_pointer(p_rmt_obj[channel]->hal.regs, channel);
|
||||
rmt_ll_reset_rx_pointer(p_rmt_obj[channel]->hal.regs, channel);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -242,9 +246,9 @@ esp_err_t rmt_set_memory_owner(rmt_channel_t channel, rmt_mem_owner_t owner)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK(owner < RMT_MEM_OWNER_MAX, RMT_MEM_OWNER_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
RMT.conf_ch[channel].conf1.mem_owner = owner;
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_set_mem_owner(p_rmt_obj[channel]->hal.regs, channel, owner);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -252,33 +256,37 @@ esp_err_t rmt_get_memory_owner(rmt_channel_t channel, rmt_mem_owner_t* owner)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK(owner != NULL, RMT_MEM_OWNER_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
*owner = (rmt_mem_owner_t) RMT.conf_ch[channel].conf1.mem_owner;
|
||||
RMT_ENTER_CRITICAL();
|
||||
*owner = (rmt_mem_owner_t)rmt_ll_get_mem_owner(p_rmt_obj[channel]->hal.regs, channel);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_set_tx_loop_mode(rmt_channel_t channel, bool loop_en)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
RMT.conf_ch[channel].conf1.tx_conti_mode = loop_en;
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_enable_tx_cyclic(p_rmt_obj[channel]->hal.regs, channel, loop_en);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_get_tx_loop_mode(rmt_channel_t channel, bool *loop_en)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
*loop_en = (bool) RMT.conf_ch[channel].conf1.tx_conti_mode;
|
||||
RMT_ENTER_CRITICAL();
|
||||
*loop_en = rmt_ll_is_tx_cyclic_enabled(p_rmt_obj[channel]->hal.regs, channel);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_set_rx_filter(rmt_channel_t channel, bool rx_filter_en, uint8_t thresh)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
RMT.conf_ch[channel].conf1.rx_filter_en = rx_filter_en;
|
||||
RMT.conf_ch[channel].conf1.rx_filter_thres = thresh;
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_enable_rx_filter(p_rmt_obj[channel]->hal.regs, channel, rx_filter_en);
|
||||
rmt_ll_set_rx_filter_thres(p_rmt_obj[channel]->hal.regs, channel, thresh);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -286,16 +294,18 @@ esp_err_t rmt_set_source_clk(rmt_channel_t channel, rmt_source_clk_t base_clk)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK(base_clk < RMT_BASECLK_MAX, RMT_BASECLK_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
RMT.conf_ch[channel].conf1.ref_always_on = base_clk;
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_set_counter_clock_src(p_rmt_obj[channel]->hal.regs, channel, base_clk);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_get_source_clk(rmt_channel_t channel, rmt_source_clk_t *src_clk)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
*src_clk = (rmt_source_clk_t) (RMT.conf_ch[channel].conf1.ref_always_on);
|
||||
RMT_ENTER_CRITICAL();
|
||||
*src_clk = (rmt_source_clk_t)rmt_ll_get_counter_clock_src(p_rmt_obj[channel]->hal.regs, channel);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -303,81 +313,70 @@ esp_err_t rmt_set_idle_level(rmt_channel_t channel, bool idle_out_en, rmt_idle_l
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK(level < RMT_IDLE_LEVEL_MAX, "RMT IDLE LEVEL ERR", ESP_ERR_INVALID_ARG);
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
RMT.conf_ch[channel].conf1.idle_out_en = idle_out_en;
|
||||
RMT.conf_ch[channel].conf1.idle_out_lv = level;
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_enable_tx_idle(p_rmt_obj[channel]->hal.regs, channel, idle_out_en);
|
||||
rmt_ll_set_tx_idle_level(p_rmt_obj[channel]->hal.regs, channel, level);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_get_idle_level(rmt_channel_t channel, bool *idle_out_en, rmt_idle_level_t *level)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
*idle_out_en = (bool) (RMT.conf_ch[channel].conf1.idle_out_en);
|
||||
*level = (rmt_idle_level_t) (RMT.conf_ch[channel].conf1.idle_out_lv);
|
||||
RMT_ENTER_CRITICAL();
|
||||
*idle_out_en = rmt_ll_is_tx_idle_enabled(p_rmt_obj[channel]->hal.regs, channel);
|
||||
*level = rmt_ll_get_tx_idle_level(p_rmt_obj[channel]->hal.regs, channel);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_get_status(rmt_channel_t channel, uint32_t *status)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32
|
||||
*status = RMT.status_ch[channel];
|
||||
#elif defined CONFIG_IDF_TARGET_ESP32S2BETA
|
||||
*status = RMT.status_ch[channel].val;
|
||||
#endif
|
||||
RMT_ENTER_CRITICAL();
|
||||
*status = rmt_ll_get_channel_status(p_rmt_obj[channel]->hal.regs, channel);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
rmt_data_mode_t rmt_get_data_mode(void)
|
||||
{
|
||||
return (rmt_data_mode_t) (RMT.apb_conf.fifo_mask);
|
||||
}
|
||||
|
||||
void rmt_set_intr_enable_mask(uint32_t mask)
|
||||
{
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
RMT.int_ena.val |= mask;
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_set_intr_enable_mask(mask);
|
||||
RMT_EXIT_CRITICAL();
|
||||
}
|
||||
|
||||
void rmt_clr_intr_enable_mask(uint32_t mask)
|
||||
{
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
RMT.int_ena.val &= (~mask);
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_clr_intr_enable_mask(mask);
|
||||
RMT_EXIT_CRITICAL();
|
||||
}
|
||||
|
||||
esp_err_t rmt_set_rx_intr_en(rmt_channel_t channel, bool en)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
if(en) {
|
||||
rmt_set_intr_enable_mask(BIT(channel * 3 + 1));
|
||||
} else {
|
||||
rmt_clr_intr_enable_mask(BIT(channel * 3 + 1));
|
||||
}
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_enable_rx_end_interrupt(p_rmt_obj[channel]->hal.regs, channel, en);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
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);
|
||||
if(en) {
|
||||
rmt_set_intr_enable_mask(BIT(channel * 3 + 2));
|
||||
} else {
|
||||
rmt_clr_intr_enable_mask(BIT(channel * 3 + 2));
|
||||
}
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_enable_err_interrupt(p_rmt_obj[channel]->hal.regs, channel, en);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_set_tx_intr_en(rmt_channel_t channel, bool en)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
if(en) {
|
||||
rmt_set_intr_enable_mask(BIT(channel * 3));
|
||||
} else {
|
||||
rmt_clr_intr_enable_mask(BIT(channel * 3));
|
||||
}
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_enable_tx_end_interrupt(p_rmt_obj[channel]->hal.regs, channel, en);
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
@@ -386,19 +385,15 @@ esp_err_t rmt_set_tx_thr_intr_en(rmt_channel_t channel, bool en, uint16_t evt_th
|
||||
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);
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
RMT.tx_lim_ch[channel].limit = evt_thresh;
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
rmt_set_tx_wrap_en(true);
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32
|
||||
rmt_set_intr_enable_mask(BIT(channel + 24));
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_set_tx_limit(p_rmt_obj[channel]->hal.regs, channel, evt_thresh);
|
||||
rmt_ll_enable_tx_pingpong(p_rmt_obj[channel]->hal.regs, true);
|
||||
rmt_ll_enable_tx_thres_interrupt(p_rmt_obj[channel]->hal.regs, channel, true);
|
||||
RMT_EXIT_CRITICAL();
|
||||
} else {
|
||||
rmt_clr_intr_enable_mask(BIT(channel + 24));
|
||||
#elif defined CONFIG_IDF_TARGET_ESP32S2BETA
|
||||
rmt_set_intr_enable_mask(BIT(channel + 12));
|
||||
} else {
|
||||
rmt_clr_intr_enable_mask(BIT(channel + 12));
|
||||
#endif
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_enable_tx_thres_interrupt(p_rmt_obj[channel]->hal.regs, channel, false);
|
||||
RMT_EXIT_CRITICAL();
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
@@ -407,7 +402,8 @@ esp_err_t rmt_set_pin(rmt_channel_t channel, rmt_mode_t mode, gpio_num_t gpio_nu
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK(mode < RMT_MODE_MAX, RMT_MODE_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK(((GPIO_IS_VALID_GPIO(gpio_num) && (mode == RMT_MODE_RX)) || (GPIO_IS_VALID_OUTPUT_GPIO(gpio_num) && (mode == RMT_MODE_TX))),
|
||||
RMT_CHECK(((GPIO_IS_VALID_GPIO(gpio_num) && (mode == RMT_MODE_RX)) ||
|
||||
(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num) && (mode == RMT_MODE_TX))),
|
||||
RMT_GPIO_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
|
||||
PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio_num], PIN_FUNC_GPIO);
|
||||
@@ -421,111 +417,108 @@ esp_err_t rmt_set_pin(rmt_channel_t channel, rmt_mode_t mode, gpio_num_t gpio_nu
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_config(const rmt_config_t* rmt_param)
|
||||
static esp_err_t rmt_internal_config(rmt_dev_t *dev, const rmt_config_t *rmt_param)
|
||||
{
|
||||
uint8_t mode = rmt_param->rmt_mode;
|
||||
uint8_t channel = rmt_param->channel;
|
||||
uint8_t gpio_num = rmt_param->gpio_num;
|
||||
uint8_t mem_cnt = rmt_param->mem_block_num;
|
||||
int clk_div = rmt_param->clk_div;
|
||||
uint8_t clk_div = rmt_param->clk_div;
|
||||
uint32_t carrier_freq_hz = rmt_param->tx_config.carrier_freq_hz;
|
||||
bool carrier_en = rmt_param->tx_config.carrier_en;
|
||||
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK(GPIO_IS_VALID_GPIO(gpio_num), RMT_GPIO_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK((mem_cnt + channel <= 8 && mem_cnt > 0), RMT_MEM_CNT_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK((clk_div > 0), RMT_CLK_DIV_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
|
||||
if (mode == RMT_MODE_TX) {
|
||||
RMT_CHECK((!carrier_en || carrier_freq_hz > 0), "RMT carrier frequency can't be zero", ESP_ERR_INVALID_ARG);
|
||||
}
|
||||
|
||||
static bool rmt_enable = false;
|
||||
if (rmt_enable == false) {
|
||||
periph_module_reset(PERIPH_RMT_MODULE);
|
||||
rmt_enable = true;
|
||||
}
|
||||
periph_module_enable(PERIPH_RMT_MODULE);
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_set_counter_clock_div(dev, channel, clk_div);
|
||||
rmt_ll_enable_mem_access(dev, true);
|
||||
rmt_ll_reset_tx_pointer(dev, channel);
|
||||
rmt_ll_reset_rx_pointer(dev, channel);
|
||||
rmt_ll_set_counter_clock_src(dev, channel, RMT_BASECLK_APB); // only support APB clock for now
|
||||
rmt_ll_set_mem_blocks(dev, channel, mem_cnt);
|
||||
rmt_ll_set_mem_owner(dev, channel, RMT_MEM_OWNER_HW);
|
||||
RMT_EXIT_CRITICAL();
|
||||
|
||||
RMT.conf_ch[channel].conf0.div_cnt = clk_div;
|
||||
/*Visit data use memory not FIFO*/
|
||||
rmt_set_data_mode(RMT_DATA_MODE_MEM);
|
||||
/*Reset tx/rx memory index */
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
RMT.conf_ch[channel].conf1.mem_rd_rst = 1;
|
||||
RMT.conf_ch[channel].conf1.mem_wr_rst = 1;
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
uint32_t rmt_source_clk_hz = RMT_SOURCE_CLK(RMT_BASECLK_APB);
|
||||
|
||||
if (mode == RMT_MODE_TX) {
|
||||
uint32_t rmt_source_clk_hz = 0;
|
||||
uint16_t carrier_duty_percent = rmt_param->tx_config.carrier_duty_percent;
|
||||
uint8_t carrier_level = rmt_param->tx_config.carrier_level;
|
||||
uint8_t idle_level = rmt_param->tx_config.idle_level;
|
||||
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
RMT.conf_ch[channel].conf1.tx_conti_mode = rmt_param->tx_config.loop_en;
|
||||
/*Memory set block number*/
|
||||
RMT.conf_ch[channel].conf0.mem_size = mem_cnt;
|
||||
RMT.conf_ch[channel].conf1.mem_owner = RMT_MEM_OWNER_TX;
|
||||
/*We use APB clock in this version, which is 80Mhz, later we will release system reference clock*/
|
||||
RMT.conf_ch[channel].conf1.ref_always_on = RMT_BASECLK_APB;
|
||||
rmt_source_clk_hz = RMT_SOURCE_CLK(RMT_BASECLK_APB);
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_enable_tx_cyclic(dev, channel, rmt_param->tx_config.loop_en);
|
||||
/*Set idle level */
|
||||
RMT.conf_ch[channel].conf1.idle_out_en = rmt_param->tx_config.idle_output_en;
|
||||
RMT.conf_ch[channel].conf1.idle_out_lv = idle_level;
|
||||
rmt_ll_enable_tx_idle(dev, channel, rmt_param->tx_config.idle_output_en);
|
||||
rmt_ll_set_tx_idle_level(dev, channel, idle_level);
|
||||
/*Set carrier*/
|
||||
RMT.conf_ch[channel].conf0.carrier_en = carrier_en;
|
||||
rmt_ll_enable_tx_carrier(dev, channel, carrier_en);
|
||||
if (carrier_en) {
|
||||
uint32_t duty_div, duty_h, duty_l;
|
||||
duty_div = rmt_source_clk_hz / carrier_freq_hz;
|
||||
duty_h = duty_div * carrier_duty_percent / 100;
|
||||
duty_l = duty_div - duty_h;
|
||||
RMT.conf_ch[channel].conf0.carrier_out_lv = carrier_level;
|
||||
RMT.carrier_duty_ch[channel].high = duty_h;
|
||||
RMT.carrier_duty_ch[channel].low = duty_l;
|
||||
rmt_ll_set_carrier_to_level(dev, channel, carrier_level);
|
||||
rmt_ll_set_carrier_high_low_ticks(dev, channel, duty_h, duty_l);
|
||||
} else {
|
||||
RMT.conf_ch[channel].conf0.carrier_out_lv = 0;
|
||||
RMT.carrier_duty_ch[channel].high = 0;
|
||||
RMT.carrier_duty_ch[channel].low = 0;
|
||||
rmt_ll_set_carrier_to_level(dev, channel, 0);
|
||||
rmt_ll_set_carrier_high_low_ticks(dev, channel, 0, 0);
|
||||
}
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
RMT_EXIT_CRITICAL();
|
||||
|
||||
ESP_LOGD(RMT_TAG, "Rmt Tx Channel %u|Gpio %u|Sclk_Hz %u|Div %u|Carrier_Hz %u|Duty %u",
|
||||
channel, gpio_num, rmt_source_clk_hz, clk_div, carrier_freq_hz, carrier_duty_percent);
|
||||
|
||||
}
|
||||
else if(RMT_MODE_RX == mode) {
|
||||
} else if (RMT_MODE_RX == mode) {
|
||||
uint8_t filter_cnt = rmt_param->rx_config.filter_ticks_thresh;
|
||||
uint16_t threshold = rmt_param->rx_config.idle_threshold;
|
||||
|
||||
portENTER_CRITICAL(&rmt_spinlock);
|
||||
/*clock init*/
|
||||
RMT.conf_ch[channel].conf1.ref_always_on = RMT_BASECLK_APB;
|
||||
uint32_t rmt_source_clk_hz = RMT_SOURCE_CLK(RMT_BASECLK_APB);
|
||||
/*memory set block number and owner*/
|
||||
RMT.conf_ch[channel].conf0.mem_size = mem_cnt;
|
||||
RMT.conf_ch[channel].conf1.mem_owner = RMT_MEM_OWNER_RX;
|
||||
RMT_ENTER_CRITICAL();
|
||||
/*Set idle threshold*/
|
||||
RMT.conf_ch[channel].conf0.idle_thres = threshold;
|
||||
rmt_ll_set_rx_idle_thres(dev, channel, threshold);
|
||||
/* Set RX filter */
|
||||
RMT.conf_ch[channel].conf1.rx_filter_thres = filter_cnt;
|
||||
RMT.conf_ch[channel].conf1.rx_filter_en = rmt_param->rx_config.filter_en;
|
||||
portEXIT_CRITICAL(&rmt_spinlock);
|
||||
rmt_ll_set_rx_filter_thres(dev, channel, filter_cnt);
|
||||
rmt_ll_enable_rx_filter(dev, channel, rmt_param->rx_config.filter_en);
|
||||
RMT_EXIT_CRITICAL();
|
||||
|
||||
ESP_LOGD(RMT_TAG, "Rmt Rx Channel %u|Gpio %u|Sclk_Hz %u|Div %u|Thresold %u|Filter %u",
|
||||
channel, gpio_num, rmt_source_clk_hz, clk_div, threshold, filter_cnt);
|
||||
}
|
||||
rmt_set_pin(channel, mode, gpio_num);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void IRAM_ATTR rmt_fill_memory(rmt_channel_t channel, const rmt_item32_t* item, uint16_t item_num, uint16_t mem_offset)
|
||||
esp_err_t rmt_config(const rmt_config_t *rmt_param)
|
||||
{
|
||||
portENTER_CRITICAL_SAFE(&rmt_spinlock);
|
||||
RMT.apb_conf.fifo_mask = RMT_DATA_MODE_MEM;
|
||||
portEXIT_CRITICAL_SAFE(&rmt_spinlock);
|
||||
int i;
|
||||
for(i = 0; i < item_num; i++) {
|
||||
RMTMEM.chan[channel].data32[i + mem_offset].val = item[i].val;
|
||||
// reset the RMT module at the first time initialize RMT driver
|
||||
static bool rmt_module_enabled = false;
|
||||
if (rmt_module_enabled == false) {
|
||||
periph_module_reset(PERIPH_RMT_MODULE);
|
||||
rmt_module_enabled = true;
|
||||
}
|
||||
periph_module_enable(PERIPH_RMT_MODULE);
|
||||
|
||||
RMT_CHECK(rmt_set_pin(rmt_param->channel, rmt_param->rmt_mode, rmt_param->gpio_num) == ESP_OK,
|
||||
"set gpio for RMT driver failed", ESP_ERR_INVALID_ARG);
|
||||
|
||||
RMT_CHECK(rmt_internal_config(&RMT, rmt_param) == ESP_OK,
|
||||
"initialize RMT driver failed", ESP_ERR_INVALID_ARG);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static void IRAM_ATTR rmt_fill_memory(rmt_channel_t channel, const rmt_item32_t *item,
|
||||
uint16_t item_num, uint16_t mem_offset)
|
||||
{
|
||||
RMT_ENTER_CRITICAL();
|
||||
rmt_ll_set_mem_owner(p_rmt_obj[channel]->hal.regs, channel, RMT_MEM_OWNER_SW);
|
||||
rmt_ll_write_memory(p_rmt_obj[channel]->hal.mem, channel, item, item_num, mem_offset);
|
||||
rmt_ll_set_mem_owner(p_rmt_obj[channel]->hal.regs, channel, RMT_MEM_OWNER_HW);
|
||||
RMT_EXIT_CRITICAL();
|
||||
}
|
||||
|
||||
esp_err_t rmt_fill_tx_items(rmt_channel_t channel, const rmt_item32_t *item, uint16_t item_num, uint16_t mem_offset)
|
||||
@@ -535,7 +528,7 @@ esp_err_t rmt_fill_tx_items(rmt_channel_t channel, const rmt_item32_t* item, uin
|
||||
RMT_CHECK((item_num > 0), RMT_DRIVER_LENGTH_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
|
||||
/*Each block has 64 x 32 bits of data*/
|
||||
uint8_t mem_cnt = RMT.conf_ch[channel].conf0.mem_size;
|
||||
uint8_t mem_cnt = rmt_ll_get_mem_blocks(p_rmt_obj[channel]->hal.regs, channel);
|
||||
RMT_CHECK((mem_cnt * RMT_MEM_ITEM_NUM >= item_num), RMT_WR_MEM_OVF_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
rmt_fill_memory(channel, item, item_num, mem_offset);
|
||||
return ESP_OK;
|
||||
@@ -549,7 +542,6 @@ esp_err_t rmt_isr_register(void (*fn)(void*), void * arg, int intr_alloc_flags,
|
||||
return esp_intr_alloc(ETS_RMT_INTR_SOURCE, intr_alloc_flags, fn, arg, handle);
|
||||
}
|
||||
|
||||
|
||||
esp_err_t rmt_isr_deregister(rmt_isr_handle_t handle)
|
||||
{
|
||||
return esp_intr_free(handle);
|
||||
@@ -557,9 +549,9 @@ esp_err_t rmt_isr_deregister(rmt_isr_handle_t handle)
|
||||
|
||||
static int IRAM_ATTR rmt_get_mem_len(rmt_channel_t channel)
|
||||
{
|
||||
int block_num = RMT.conf_ch[channel].conf0.mem_size;
|
||||
int block_num = rmt_ll_get_mem_blocks(p_rmt_obj[channel]->hal.regs, channel);
|
||||
int item_block_len = block_num * RMT_MEM_ITEM_NUM;
|
||||
volatile rmt_item32_t* data = RMTMEM.chan[channel].data32;
|
||||
volatile rmt_item32_t *data = (rmt_item32_t *)RMTMEM.chan[channel].data32;
|
||||
int idx;
|
||||
for (idx = 0; idx < item_block_len; idx++) {
|
||||
if (data[idx].duration0 == 0) {
|
||||
@@ -573,30 +565,21 @@ static int IRAM_ATTR rmt_get_mem_len(rmt_channel_t channel)
|
||||
|
||||
static void IRAM_ATTR rmt_driver_isr_default(void *arg)
|
||||
{
|
||||
const uint32_t intr_st = RMT.int_st.val;
|
||||
uint32_t status = intr_st;
|
||||
uint8_t channel;
|
||||
portBASE_TYPE HPTaskAwoken = 0;
|
||||
uint32_t status = 0;
|
||||
rmt_item32_t volatile *addr = NULL;
|
||||
uint8_t channel = 0;
|
||||
rmt_hal_context_t *hal = (rmt_hal_context_t *)arg;
|
||||
portBASE_TYPE HPTaskAwoken = pdFALSE;
|
||||
|
||||
// Tx end interrupt
|
||||
status = rmt_ll_get_tx_end_interrupt_status(hal->regs);
|
||||
while (status) {
|
||||
int i = __builtin_ffs(status) - 1;
|
||||
status &= ~(1 << i);
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32
|
||||
if(i < 24) {
|
||||
#elif defined CONFIG_IDF_TARGET_ESP32S2BETA
|
||||
if(i >= 15) {
|
||||
} else if(i < 12) {
|
||||
#endif
|
||||
channel = i / 3;
|
||||
channel = __builtin_ffs(status) - 1;
|
||||
status &= ~(1 << channel);
|
||||
rmt_obj_t *p_rmt = p_rmt_obj[channel];
|
||||
if(NULL == p_rmt) {
|
||||
continue;
|
||||
}
|
||||
switch(i % 3) {
|
||||
//TX END
|
||||
case 0:
|
||||
if (p_rmt) {
|
||||
xSemaphoreGiveFromISR(p_rmt->tx_sem, &HPTaskAwoken);
|
||||
RMT.conf_ch[channel].conf1.mem_rd_rst = 1;
|
||||
RMT.conf_ch[channel].conf1.mem_rd_rst = 0;
|
||||
rmt_ll_reset_tx_pointer(p_rmt_obj[channel]->hal.regs, channel);
|
||||
p_rmt->tx_data = NULL;
|
||||
p_rmt->tx_len_rem = 0;
|
||||
p_rmt->tx_offset = 0;
|
||||
@@ -606,48 +589,17 @@ static void IRAM_ATTR rmt_driver_isr_default(void* arg)
|
||||
if (rmt_tx_end_callback.function != NULL) {
|
||||
rmt_tx_end_callback.function(channel, rmt_tx_end_callback.arg);
|
||||
}
|
||||
break;
|
||||
//RX_END
|
||||
case 1:
|
||||
RMT.conf_ch[channel].conf1.rx_en = 0;
|
||||
int item_len = rmt_get_mem_len(channel);
|
||||
//change memory owner to protect data.
|
||||
RMT.conf_ch[channel].conf1.mem_owner = RMT_MEM_OWNER_TX;
|
||||
if(p_rmt->rx_buf) {
|
||||
BaseType_t res = xRingbufferSendFromISR(p_rmt->rx_buf, (void*) RMTMEM.chan[channel].data32, item_len * 4, &HPTaskAwoken);
|
||||
if(res == pdFALSE) {
|
||||
ESP_EARLY_LOGE(RMT_TAG, "RMT RX BUFFER FULL");
|
||||
} else {
|
||||
}
|
||||
rmt_ll_clear_tx_end_interrupt(hal->regs, channel);
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
ESP_EARLY_LOGE(RMT_TAG, "RMT RX BUFFER ERROR\n");
|
||||
}
|
||||
RMT.conf_ch[channel].conf1.mem_wr_rst = 1;
|
||||
RMT.conf_ch[channel].conf1.mem_owner = RMT_MEM_OWNER_RX;
|
||||
RMT.conf_ch[channel].conf1.rx_en = 1;
|
||||
break;
|
||||
//ERR
|
||||
case 2:
|
||||
ESP_EARLY_LOGE(RMT_TAG, "RMT[%d] ERR", channel);
|
||||
ESP_EARLY_LOGE(RMT_TAG, "status: 0x%08x", RMT.status_ch[channel]);
|
||||
RMT.int_ena.val &= (~(BIT(i)));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
#ifdef CONFIG_IDF_TARGET_ESP32
|
||||
} else {
|
||||
channel = i - 24;
|
||||
#elif defined CONFIG_IDF_TARGET_ESP32S2BETA
|
||||
} else if(i >= 12 && i < 16) {
|
||||
channel = i - 12;
|
||||
#endif
|
||||
// Tx thres interrupt
|
||||
status = rmt_ll_get_tx_thres_interrupt_status(hal->regs);
|
||||
while (status) {
|
||||
channel = __builtin_ffs(status) - 1;
|
||||
status &= ~(1 << channel);
|
||||
rmt_obj_t *p_rmt = p_rmt_obj[channel];
|
||||
|
||||
if(p_rmt->tx_data == NULL) {
|
||||
//skip
|
||||
} else {
|
||||
if (p_rmt) {
|
||||
if (p_rmt->translator) {
|
||||
if (p_rmt->sample_size_remain > 0) {
|
||||
size_t translated_size = 0;
|
||||
@@ -656,8 +608,7 @@ static void IRAM_ATTR rmt_driver_isr_default(void* arg)
|
||||
p_rmt->sample_size_remain,
|
||||
p_rmt->tx_sub_len,
|
||||
&translated_size,
|
||||
&p_rmt->tx_len_rem
|
||||
);
|
||||
&p_rmt->tx_len_rem);
|
||||
p_rmt->sample_size_remain -= translated_size;
|
||||
p_rmt->sample_cur += translated_size;
|
||||
p_rmt->tx_data = p_rmt->tx_buf;
|
||||
@@ -673,10 +624,12 @@ static void IRAM_ATTR rmt_driver_isr_default(void* arg)
|
||||
p_rmt->tx_data += p_rmt->tx_sub_len;
|
||||
p_rmt->tx_len_rem -= p_rmt->tx_sub_len;
|
||||
} else if (len_rem == 0) {
|
||||
RMTMEM.chan[channel].data32[p_rmt->tx_offset].val = 0;
|
||||
rmt_item32_t stop_data = {0};
|
||||
rmt_ll_write_memory(p_rmt_obj[channel]->hal.mem, channel, &stop_data, 1, p_rmt->tx_offset);
|
||||
} else {
|
||||
rmt_fill_memory(channel, pdata, len_rem, p_rmt->tx_offset);
|
||||
RMTMEM.chan[channel].data32[p_rmt->tx_offset + len_rem].val = 0;
|
||||
rmt_item32_t stop_data = {0};
|
||||
rmt_ll_write_memory(p_rmt_obj[channel]->hal.mem, channel, &stop_data, 1, p_rmt->tx_offset + len_rem);
|
||||
p_rmt->tx_data += len_rem;
|
||||
p_rmt->tx_len_rem -= len_rem;
|
||||
}
|
||||
@@ -686,9 +639,48 @@ static void IRAM_ATTR rmt_driver_isr_default(void* arg)
|
||||
p_rmt->tx_offset = 0;
|
||||
}
|
||||
}
|
||||
rmt_ll_clear_tx_thres_interrupt(hal->regs, channel);
|
||||
}
|
||||
|
||||
// Rx end interrupt
|
||||
status = rmt_ll_get_rx_end_interrupt_status(hal->regs);
|
||||
while (status) {
|
||||
channel = __builtin_ffs(status) - 1;
|
||||
status &= ~(1 << channel);
|
||||
rmt_obj_t *p_rmt = p_rmt_obj[channel];
|
||||
if (p_rmt) {
|
||||
rmt_ll_enable_rx(p_rmt_obj[channel]->hal.regs, channel, false);
|
||||
int item_len = rmt_get_mem_len(channel);
|
||||
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;
|
||||
BaseType_t res = xRingbufferSendFromISR(p_rmt->rx_buf, (void *)addr, item_len * 4, &HPTaskAwoken);
|
||||
if (res == pdFALSE) {
|
||||
ESP_EARLY_LOGE(RMT_TAG, "RMT RX BUFFER FULL");
|
||||
}
|
||||
RMT.int_clr.val = intr_st;
|
||||
} else {
|
||||
ESP_EARLY_LOGE(RMT_TAG, "RMT RX BUFFER ERROR");
|
||||
}
|
||||
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);
|
||||
}
|
||||
rmt_ll_clear_rx_end_interrupt(hal->regs, channel);
|
||||
}
|
||||
|
||||
// Err interrupt
|
||||
status = rmt_ll_get_err_interrupt_status(hal->regs);
|
||||
while (status) {
|
||||
channel = __builtin_ffs(status) - 1;
|
||||
status &= ~(1 << channel);
|
||||
rmt_obj_t *p_rmt = p_rmt_obj[channel];
|
||||
if (p_rmt) {
|
||||
ESP_EARLY_LOGD(RMT_TAG, "RMT[%d] ERR", channel);
|
||||
ESP_EARLY_LOGD(RMT_TAG, "status: 0x%08x", rmt_ll_get_channel_status(p_rmt_obj[channel]->hal.regs, channel));
|
||||
}
|
||||
rmt_ll_clear_err_interrupt(hal->regs, channel);
|
||||
}
|
||||
|
||||
if (HPTaskAwoken == pdTRUE) {
|
||||
portYIELD_FROM_ISR();
|
||||
}
|
||||
@@ -714,7 +706,8 @@ esp_err_t rmt_driver_uninstall(rmt_channel_t channel)
|
||||
_lock_acquire_recursive(&rmt_driver_isr_lock);
|
||||
|
||||
s_rmt_driver_channels &= ~BIT(channel);
|
||||
if (s_rmt_driver_channels == 0) { // all channels have driver disabled
|
||||
if (s_rmt_driver_channels == 0) {
|
||||
// all channels have driver disabled
|
||||
err = rmt_isr_deregister(s_rmt_driver_intr_handle);
|
||||
s_rmt_driver_intr_handle = NULL;
|
||||
}
|
||||
@@ -749,7 +742,8 @@ esp_err_t rmt_driver_uninstall(rmt_channel_t channel)
|
||||
esp_err_t rmt_driver_install(rmt_channel_t channel, size_t rx_buf_size, int intr_alloc_flags)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK((s_rmt_driver_channels & BIT(channel)) == 0, "RMT driver already installed for channel", ESP_ERR_INVALID_STATE);
|
||||
RMT_CHECK((s_rmt_driver_channels & BIT(channel)) == 0,
|
||||
"RMT driver already installed for channel", ESP_ERR_INVALID_STATE);
|
||||
|
||||
esp_err_t err = ESP_OK;
|
||||
|
||||
@@ -774,6 +768,9 @@ esp_err_t rmt_driver_install(rmt_channel_t channel, size_t rx_buf_size, int intr
|
||||
}
|
||||
memset(p_rmt_obj[channel], 0, sizeof(rmt_obj_t));
|
||||
|
||||
rmt_hal_init(&p_rmt_obj[channel]->hal);
|
||||
rmt_hal_channel_reset(&p_rmt_obj[channel]->hal, channel);
|
||||
|
||||
p_rmt_obj[channel]->tx_len_rem = 0;
|
||||
p_rmt_obj[channel]->tx_data = NULL;
|
||||
p_rmt_obj[channel]->channel = channel;
|
||||
@@ -797,20 +794,17 @@ 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);
|
||||
rmt_set_rx_intr_en(channel, 1);
|
||||
rmt_set_err_intr_en(channel, 1);
|
||||
}
|
||||
|
||||
rmt_set_err_intr_en(channel, 1);
|
||||
_lock_acquire_recursive(&rmt_driver_isr_lock);
|
||||
|
||||
if(s_rmt_driver_channels == 0) { // first RMT channel using driver
|
||||
err = rmt_isr_register(rmt_driver_isr_default, NULL, intr_alloc_flags, &s_rmt_driver_intr_handle);
|
||||
if (s_rmt_driver_channels == 0) {
|
||||
// first RMT channel using driver
|
||||
err = rmt_isr_register(rmt_driver_isr_default, &p_rmt_obj[channel]->hal, intr_alloc_flags, &s_rmt_driver_intr_handle);
|
||||
}
|
||||
if (err == ESP_OK) {
|
||||
s_rmt_driver_channels |= BIT(channel);
|
||||
rmt_set_tx_intr_en(channel, 1);
|
||||
}
|
||||
|
||||
_lock_release_recursive(&rmt_driver_isr_lock);
|
||||
|
||||
return err;
|
||||
@@ -831,7 +825,7 @@ esp_err_t rmt_write_items(rmt_channel_t channel, const rmt_item32_t* rmt_item, i
|
||||
}
|
||||
#endif
|
||||
rmt_obj_t *p_rmt = p_rmt_obj[channel];
|
||||
int block_num = RMT.conf_ch[channel].conf0.mem_size;
|
||||
int block_num = rmt_ll_get_mem_blocks(p_rmt_obj[channel]->hal.regs, channel);
|
||||
int item_block_len = block_num * RMT_MEM_ITEM_NUM;
|
||||
int item_sub_len = block_num * RMT_MEM_ITEM_NUM / 2;
|
||||
int len_rem = item_num;
|
||||
@@ -848,7 +842,8 @@ esp_err_t rmt_write_items(rmt_channel_t channel, const rmt_item32_t* rmt_item, i
|
||||
p_rmt->tx_sub_len = item_sub_len;
|
||||
} else {
|
||||
rmt_fill_memory(channel, rmt_item, len_rem, 0);
|
||||
RMTMEM.chan[channel].data32[len_rem].val = 0;
|
||||
rmt_item32_t stop_data = {0};
|
||||
rmt_ll_write_memory(p_rmt_obj[channel]->hal.mem, channel, &stop_data, 1, len_rem);
|
||||
p_rmt->tx_len_rem = 0;
|
||||
}
|
||||
rmt_tx_start(channel, true);
|
||||
@@ -868,9 +863,9 @@ esp_err_t rmt_wait_tx_done(rmt_channel_t channel, TickType_t wait_time)
|
||||
p_rmt_obj[channel]->wait_done = false;
|
||||
xSemaphoreGive(p_rmt_obj[channel]->tx_sem);
|
||||
return ESP_OK;
|
||||
}
|
||||
else {
|
||||
if (wait_time != 0) { // Don't emit error message if just polling.
|
||||
} else {
|
||||
if (wait_time != 0) {
|
||||
// Don't emit error message if just polling.
|
||||
ESP_LOGE(RMT_TAG, "Timeout on wait_tx_done");
|
||||
}
|
||||
return ESP_ERR_TIMEOUT;
|
||||
@@ -899,7 +894,8 @@ esp_err_t rmt_translator_init(rmt_channel_t channel, sample_to_rmt_t fn)
|
||||
RMT_CHECK(fn != NULL, RMT_TRANSLATOR_NULL_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK(p_rmt_obj[channel] != NULL, RMT_DRIVER_ERROR_STR, ESP_FAIL);
|
||||
const uint32_t block_size = RMT.conf_ch[channel].conf0.mem_size * RMT_MEM_ITEM_NUM * sizeof(rmt_item32_t);
|
||||
const uint32_t block_size = rmt_ll_get_mem_blocks(p_rmt_obj[channel]->hal.regs, channel) *
|
||||
RMT_MEM_ITEM_NUM * sizeof(rmt_item32_t);
|
||||
if (p_rmt_obj[channel]->tx_buf == NULL) {
|
||||
#if !CONFIG_SPIRAM_USE_MALLOC
|
||||
p_rmt_obj[channel]->tx_buf = (rmt_item32_t *)malloc(block_size);
|
||||
@@ -938,7 +934,7 @@ esp_err_t rmt_write_sample(rmt_channel_t channel, const uint8_t *src, size_t src
|
||||
size_t item_num = 0;
|
||||
size_t translated_size = 0;
|
||||
rmt_obj_t *p_rmt = p_rmt_obj[channel];
|
||||
const uint32_t item_block_len = RMT.conf_ch[channel].conf0.mem_size * RMT_MEM_ITEM_NUM;
|
||||
const uint32_t item_block_len = rmt_ll_get_mem_blocks(p_rmt_obj[channel]->hal.regs, channel) * RMT_MEM_ITEM_NUM;
|
||||
const uint32_t item_sub_len = item_block_len / 2;
|
||||
xSemaphoreTake(p_rmt->tx_sem, portMAX_DELAY);
|
||||
p_rmt->sample_to_rmt((void *)src, p_rmt->tx_buf, src_size, item_block_len, &translated_size, &item_num);
|
||||
@@ -952,7 +948,8 @@ esp_err_t rmt_write_sample(rmt_channel_t channel, const uint8_t *src, size_t src
|
||||
p_rmt->tx_sub_len = item_sub_len;
|
||||
p_rmt->translator = true;
|
||||
} else {
|
||||
RMTMEM.chan[channel].data32[item_num].val = 0;
|
||||
rmt_item32_t stop_data = {0};
|
||||
rmt_ll_write_memory(p_rmt_obj[channel]->hal.mem, channel, &stop_data, 1, item_num);
|
||||
p_rmt->tx_len_rem = 0;
|
||||
p_rmt->sample_cur = NULL;
|
||||
p_rmt->translator = false;
|
||||
@@ -984,3 +981,13 @@ esp_err_t rmt_get_channel_status(rmt_channel_status_result_t *channel_status)
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t rmt_get_counter_clock(rmt_channel_t channel, uint32_t *clock_hz)
|
||||
{
|
||||
RMT_CHECK(channel < RMT_CHANNEL_MAX, RMT_CHANNEL_ERROR_STR, ESP_ERR_INVALID_ARG);
|
||||
RMT_CHECK(clock_hz, "parameter clock_hz can't be null", ESP_ERR_INVALID_ARG);
|
||||
RMT_ENTER_CRITICAL();
|
||||
*clock_hz = rmt_hal_get_counter_clock(&p_rmt_obj[channel]->hal, channel, RMT_SOURCE_CLK(RMT_BASECLK_APB));
|
||||
RMT_EXIT_CRITICAL();
|
||||
return ESP_OK;
|
||||
}
|
||||
|
@@ -1,22 +1,24 @@
|
||||
/**
|
||||
* please connect GPIO18 to GPIO19
|
||||
* @brief To run this unit test, MAKE SURE GPIO18(TX) is connected to GPIO19(RX)!
|
||||
*
|
||||
*/
|
||||
#include "stdio.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "driver/rmt.h"
|
||||
#include "sdkconfig.h"
|
||||
#include "unity.h"
|
||||
#include "test_utils.h"
|
||||
#include "driver/rmt.h"
|
||||
#include "driver/periph_ctrl.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/periph_ctrl.h"
|
||||
#include "soc/soc.h"
|
||||
#include "soc/rmt_periph.h"
|
||||
|
||||
static const char* TAG = "RMT";
|
||||
|
||||
static const char *TAG = "RMT.test";
|
||||
|
||||
#define RMT_RX_ACTIVE_LEVEL 1 /*!< Data bit is active high for self test mode */
|
||||
#define RMT_TX_CARRIER_EN 0 /*!< Disable carrier for self test mode */
|
||||
@@ -26,8 +28,9 @@ static const char* TAG = "RMT";
|
||||
#define RMT_RX_CHANNEL 0 /*!< RMT channel for receiver */
|
||||
#define RMT_RX_GPIO_NUM 19 /*!< GPIO number for receiver */
|
||||
#define RMT_CLK_DIV 100 /*!< RMT counter clock divider */
|
||||
#define RMT_TICK_10_US (80000000/RMT_CLK_DIV/100000) /*!< RMT counter value for 10 us.(Source clock is APB clock) */
|
||||
#define RMT_TICK_10_US (APB_CLK_FREQ / RMT_CLK_DIV / 100000) /*!< RMT counter value for 10 us */
|
||||
|
||||
// NEC protocol related parameters
|
||||
#define HEADER_HIGH_US 9000 /*!< NEC protocol header: positive 9ms */
|
||||
#define HEADER_LOW_US 4500 /*!< NEC protocol header: negative 4.5ms*/
|
||||
#define BIT_ONE_HIGH_US 560 /*!< NEC protocol data bit 1: positive 0.56ms */
|
||||
@@ -35,11 +38,11 @@ static const char* TAG = "RMT";
|
||||
#define BIT_ZERO_HIGH_US 560 /*!< NEC protocol data bit 0: positive 0.56ms */
|
||||
#define BIT_ZERO_LOW_US (1120 - BIT_ZERO_HIGH_US) /*!< NEC protocol data bit 0: negative 0.56ms */
|
||||
#define BIT_END 560 /*!< NEC protocol end: positive 0.56ms */
|
||||
#define BIT_MARGIN 20 /*!< NEC parse margin time */
|
||||
#define BIT_MARGIN 160 /*!< NEC parse margin time */
|
||||
|
||||
#define ITEM_DURATION(d) ((d & 0x7fff) * 10 / RMT_TICK_10_US) /*!< Parse duration time from memory register value */
|
||||
#define DATA_ITEM_NUM 34 /*!< NEC code item number: header + 32bit data + end */
|
||||
#define RMT_TX_DATA_NUM 100 /*!< NEC tx test data number */
|
||||
#define RMT_TX_DATA_NUM 50 /*!< NEC tx test data number */
|
||||
#define RMT_ITEM32_TIMEOUT_US 9500 /*!< RMT receiver timeout value(us) */
|
||||
|
||||
/**
|
||||
@@ -90,8 +93,8 @@ static void fill_item_end(rmt_item32_t* item)
|
||||
*/
|
||||
static inline bool check_in_range(int duration_ticks, int target_us, int margin_us)
|
||||
{
|
||||
if(( ITEM_DURATION(duration_ticks) < (target_us + margin_us))
|
||||
&& ( ITEM_DURATION(duration_ticks) > (target_us - margin_us))) {
|
||||
if ((ITEM_DURATION(duration_ticks) < (target_us + margin_us)) &&
|
||||
(ITEM_DURATION(duration_ticks) > (target_us - margin_us))) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
@@ -103,9 +106,9 @@ static inline bool check_in_range(int duration_ticks, int target_us, int margin_
|
||||
*/
|
||||
static bool header_if(rmt_item32_t *item)
|
||||
{
|
||||
if((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL)
|
||||
&& check_in_range(item->duration0, HEADER_HIGH_US, BIT_MARGIN)
|
||||
&& check_in_range(item->duration1, HEADER_LOW_US, BIT_MARGIN)) {
|
||||
if ((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL) &&
|
||||
check_in_range(item->duration0, HEADER_HIGH_US, BIT_MARGIN) &&
|
||||
check_in_range(item->duration1, HEADER_LOW_US, BIT_MARGIN)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -116,9 +119,9 @@ static bool header_if(rmt_item32_t* item)
|
||||
*/
|
||||
static bool bit_one_if(rmt_item32_t *item)
|
||||
{
|
||||
if((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL)
|
||||
&& check_in_range(item->duration0, BIT_ONE_HIGH_US, BIT_MARGIN)
|
||||
&& check_in_range(item->duration1, BIT_ONE_LOW_US, BIT_MARGIN)) {
|
||||
if ((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL) &&
|
||||
check_in_range(item->duration0, BIT_ONE_HIGH_US, BIT_MARGIN) &&
|
||||
check_in_range(item->duration1, BIT_ONE_LOW_US, BIT_MARGIN)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
@@ -129,15 +132,14 @@ static bool bit_one_if(rmt_item32_t* item)
|
||||
*/
|
||||
static bool bit_zero_if(rmt_item32_t *item)
|
||||
{
|
||||
if((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL)
|
||||
&& check_in_range(item->duration0, BIT_ZERO_HIGH_US, BIT_MARGIN)
|
||||
&& check_in_range(item->duration1, BIT_ZERO_LOW_US, BIT_MARGIN)) {
|
||||
if ((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL) &&
|
||||
check_in_range(item->duration0, BIT_ZERO_HIGH_US, BIT_MARGIN) &&
|
||||
check_in_range(item->duration1, BIT_ZERO_LOW_US, BIT_MARGIN)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Parse NEC 32 bit waveform to address and command.
|
||||
*/
|
||||
@@ -220,7 +222,6 @@ static void set_tx_data(int tx_channel, uint16_t cmd, uint16_t addr, int item_nu
|
||||
{
|
||||
while (1) {
|
||||
int i = build_items(tx_channel, item + offset, item_num - offset, ((~addr) << 8) | addr, cmd);
|
||||
printf("cmd :%d\n", cmd);
|
||||
if (i < 0) {
|
||||
break;
|
||||
}
|
||||
@@ -244,7 +245,7 @@ static int get_rx_data(RingbufHandle_t rb)
|
||||
int res = parse_items(rx_item + rx_offset, rx_size / 4 - rx_offset, &rmt_addr, &rmt_cmd);
|
||||
if (res > 0) {
|
||||
rx_offset += res + 1;
|
||||
ESP_LOGI(TAG, "RMT RCV --- addr: 0x%04x cmd: 0x%04x", rmt_addr, rmt_cmd);
|
||||
ESP_LOGI(TAG, "receive cmd %d from addr %d", rmt_cmd, rmt_addr & 0xFF);
|
||||
TEST_ASSERT(rmt_cmd == tmp);
|
||||
tmp++;
|
||||
} else {
|
||||
@@ -376,12 +377,12 @@ TEST_CASE("RMT init config", "[rmt][test_env=UT_T1_RMT]")
|
||||
TEST_CASE("RMT init set function", "[rmt][test_env=UT_T1_RMT]")
|
||||
{
|
||||
rmt_channel_t channel = 7;
|
||||
TEST_ESP_OK(rmt_driver_install(channel, 0, 0));
|
||||
TEST_ESP_OK(rmt_set_pin(channel, RMT_MODE_RX, RMT_RX_GPIO_NUM));
|
||||
TEST_ESP_OK(rmt_set_clk_div(channel, RMT_CLK_DIV * 2));
|
||||
TEST_ESP_OK(rmt_set_mem_block_num(channel, 1));
|
||||
TEST_ESP_OK(rmt_set_rx_filter(channel, 1, 100));
|
||||
TEST_ESP_OK(rmt_set_rx_idle_thresh(channel, RMT_ITEM32_TIMEOUT_US / 10 * (RMT_TICK_10_US) * 2));
|
||||
TEST_ESP_OK(rmt_driver_install(channel, 0, 0));
|
||||
TEST_ESP_OK(rmt_driver_uninstall(channel));
|
||||
}
|
||||
|
||||
@@ -405,8 +406,8 @@ TEST_CASE("RMT clock devider, clock source set(logic analyzer)", "[rmt][ignore]"
|
||||
rmt_tx.rmt_mode = RMT_MODE_TX;
|
||||
|
||||
TEST_ESP_OK(rmt_config(&rmt_tx));
|
||||
TEST_ESP_OK(rmt_get_clk_div(RMT_TX_CHANNEL, &div_cnt));
|
||||
TEST_ESP_OK(rmt_driver_install(rmt_tx.channel, 0, 0));
|
||||
TEST_ESP_OK(rmt_get_clk_div(RMT_TX_CHANNEL, &div_cnt));
|
||||
TEST_ASSERT_EQUAL_UINT8(div_cnt, RMT_CLK_DIV);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
|
||||
@@ -435,14 +436,11 @@ TEST_CASE("RMT rx set and get properties", "[rmt][test_env=UT_T1_RMT]")
|
||||
TEST_ESP_OK(rmt_get_clk_div(channel, &div_cnt));
|
||||
TEST_ESP_OK(rmt_get_mem_block_num(channel, &memNum));
|
||||
TEST_ESP_OK(rmt_get_rx_idle_thresh(channel, &idleThreshold));
|
||||
TEST_ESP_OK(rmt_get_memory_owner(channel, &owner));
|
||||
|
||||
TEST_ASSERT_EQUAL_UINT8(div_cnt, RMT_CLK_DIV);
|
||||
TEST_ASSERT_EQUAL_UINT8(memNum, 1);
|
||||
TEST_ASSERT_EQUAL_UINT16(idleThreshold, RMT_ITEM32_TIMEOUT_US / 10 * (RMT_TICK_10_US));
|
||||
TEST_ASSERT_EQUAL_INT(owner, RMT_MEM_OWNER_RX);
|
||||
|
||||
//eRR
|
||||
TEST_ESP_OK(rmt_set_pin(channel, RMT_MODE_RX, 22));
|
||||
TEST_ESP_OK(rmt_set_clk_div(channel, RMT_CLK_DIV * 2));
|
||||
TEST_ESP_OK(rmt_set_mem_block_num(channel, 2));
|
||||
@@ -475,12 +473,10 @@ TEST_CASE("RMT tx set and get properties", "[rmt][test_env=UT_T1_RMT]")
|
||||
TEST_ESP_OK(rmt_get_clk_div(channel, &div_cnt));
|
||||
TEST_ESP_OK(rmt_get_mem_block_num(channel, &memNum));
|
||||
TEST_ESP_OK(rmt_get_tx_loop_mode(channel, &loop_en));
|
||||
TEST_ESP_OK(rmt_get_memory_owner(channel, &owner));
|
||||
|
||||
TEST_ASSERT_EQUAL_INT8(loop_en, 0);
|
||||
TEST_ASSERT_EQUAL_UINT8(div_cnt, RMT_CLK_DIV);
|
||||
TEST_ASSERT_EQUAL_UINT8(memNum, 1);
|
||||
TEST_ASSERT_EQUAL_INT(owner, RMT_MEM_OWNER_TX);
|
||||
|
||||
//reset by "set"
|
||||
TEST_ESP_OK(rmt_set_pin(channel, RMT_MODE_TX, RMT_TX_GPIO_NUM));
|
||||
@@ -501,13 +497,13 @@ TEST_CASE("RMT tx set and get properties", "[rmt][test_env=UT_T1_RMT]")
|
||||
TEST_ASSERT_EQUAL_UINT8(memNum, 2);
|
||||
TEST_ASSERT_EQUAL_INT(owner, RMT_MEM_OWNER_TX);
|
||||
|
||||
rmt_item32_t items[1];
|
||||
items[0].duration0 = 300 / 10 * RMT_TICK_10_US; //300us
|
||||
items[0].level0 = 1;
|
||||
items[0].duration1 = 0;
|
||||
items[0].level1 = 0;
|
||||
rmt_item32_t item;
|
||||
item.duration0 = 300 / 10 * RMT_TICK_10_US; //300us
|
||||
item.level0 = 1;
|
||||
item.duration1 = 0;
|
||||
item.level1 = 0;
|
||||
for (int i = 0; i < 100; i++) {
|
||||
TEST_ESP_OK(rmt_write_items(RMT_TX_CHANNEL, items,
|
||||
TEST_ESP_OK(rmt_write_items(RMT_TX_CHANNEL, &item,
|
||||
1, /* Number of items */
|
||||
1 /* wait till done */));
|
||||
vTaskDelay(10 / portTICK_PERIOD_MS); //every 10ms to write the item
|
||||
@@ -515,6 +511,44 @@ TEST_CASE("RMT tx set and get properties", "[rmt][test_env=UT_T1_RMT]")
|
||||
TEST_ESP_OK(rmt_driver_uninstall(channel));
|
||||
}
|
||||
|
||||
TEST_CASE("RMT use multi channel", "[rmt][test_env=UT_T1_RMT]")
|
||||
{
|
||||
rmt_tx_config_t tx_cfg = {
|
||||
.loop_en = true, // set it as true
|
||||
.carrier_duty_percent = 50,
|
||||
.carrier_freq_hz = 38000,
|
||||
.carrier_level = 1,
|
||||
.carrier_en = RMT_TX_CARRIER_EN,
|
||||
.idle_level = 0,
|
||||
.idle_output_en = true,
|
||||
};
|
||||
rmt_config_t rmt_tx1 = {
|
||||
.channel = RMT_TX_CHANNEL,
|
||||
.gpio_num = RMT_TX_GPIO_NUM,
|
||||
.mem_block_num = 4,
|
||||
.clk_div = RMT_CLK_DIV,
|
||||
.tx_config = tx_cfg,
|
||||
.rmt_mode = 0,
|
||||
};
|
||||
rmt_config(&rmt_tx1);
|
||||
rmt_driver_install(rmt_tx1.channel, 0, 0);
|
||||
|
||||
rmt_config_t rmt_tx2 = rmt_tx1;
|
||||
rmt_tx2.channel = 2;
|
||||
rmt_config(&rmt_tx2);
|
||||
rmt_driver_install(rmt_tx2.channel, 0, 0);
|
||||
|
||||
rmt_config_t rmt_tx3 = rmt_tx1;
|
||||
rmt_tx3.channel = 7;
|
||||
rmt_tx3.mem_block_num = 1;
|
||||
rmt_config(&rmt_tx3);
|
||||
rmt_driver_install(rmt_tx3.channel, 0, 0);
|
||||
|
||||
TEST_ESP_OK(rmt_driver_uninstall(RMT_TX_CHANNEL));
|
||||
TEST_ESP_OK(rmt_driver_uninstall(2));
|
||||
TEST_ESP_OK(rmt_driver_uninstall(7));
|
||||
}
|
||||
|
||||
TEST_CASE("RMT memory test", "[rmt][test_env=UT_T1_RMT]")
|
||||
{
|
||||
rmt_config_t rmt_rx;
|
||||
@@ -580,131 +614,92 @@ TEST_CASE("RMT send waveform(logic analyzer)", "[rmt][test_env=UT_T1_RMT][ignore
|
||||
|
||||
TEST_CASE("RMT basic TX and RX", "[rmt][test_env=UT_T1_RMT]")
|
||||
{
|
||||
tx_init();
|
||||
int tx_channel = RMT_TX_CHANNEL;
|
||||
uint16_t cmd = 0x0;
|
||||
uint16_t addr = 0x11;
|
||||
int tx_num = RMT_TX_DATA_NUM;
|
||||
ESP_LOGI(TAG, "RMT TX DATA");
|
||||
size_t size = (sizeof(rmt_item32_t) * DATA_ITEM_NUM * tx_num);
|
||||
rmt_item32_t* item = (rmt_item32_t*) malloc(size);
|
||||
int item_num = DATA_ITEM_NUM * tx_num;
|
||||
memset((void*) item, 0, size);
|
||||
int offset = 0;
|
||||
|
||||
int rx_channel = RMT_RX_CHANNEL;
|
||||
rx_init();
|
||||
RingbufHandle_t rb = NULL;
|
||||
rmt_get_ringbuf_handle(rx_channel, &rb);
|
||||
rmt_rx_start(rx_channel, 1);
|
||||
// send data
|
||||
set_tx_data(tx_channel, cmd, addr, item_num, item, offset);
|
||||
rmt_write_items(tx_channel, item, item_num, 1);
|
||||
free(item);
|
||||
// receive data
|
||||
uint16_t tmp = get_rx_data(rb);
|
||||
TEST_ASSERT(tmp == 100);
|
||||
TEST_ESP_OK(rmt_driver_uninstall(RMT_TX_CHANNEL));
|
||||
TEST_ESP_OK(rmt_driver_uninstall(RMT_RX_CHANNEL));
|
||||
}
|
||||
rmt_get_ringbuf_handle(RMT_RX_CHANNEL, &rb);
|
||||
rmt_rx_start(RMT_RX_CHANNEL, 1);
|
||||
ESP_LOGI(TAG, "Star receiving RMT data...");
|
||||
|
||||
TEST_CASE("RMT TX write item not wait", "[rmt][test_env=UT_T1_RMT]")
|
||||
{
|
||||
tx_init();
|
||||
int tx_channel = RMT_TX_CHANNEL;
|
||||
uint16_t cmd = 0x0;
|
||||
uint16_t addr = 0x11;
|
||||
int tx_num = RMT_TX_DATA_NUM;
|
||||
ESP_LOGI(TAG, "RMT TX DATA");
|
||||
size_t size = (sizeof(rmt_item32_t) * DATA_ITEM_NUM * tx_num);
|
||||
rmt_item32_t* item = (rmt_item32_t*) malloc(size);
|
||||
int item_num = DATA_ITEM_NUM * tx_num;
|
||||
memset((void*) item, 0, size);
|
||||
int offset = 0;
|
||||
int num_items = DATA_ITEM_NUM * RMT_TX_DATA_NUM;
|
||||
rmt_item32_t *items = calloc(num_items + 1, sizeof(rmt_item32_t));
|
||||
|
||||
int rx_channel = RMT_RX_CHANNEL;
|
||||
rx_init();
|
||||
RingbufHandle_t rb = NULL;
|
||||
rmt_get_ringbuf_handle(rx_channel, &rb);
|
||||
rmt_rx_start(rx_channel, 1);
|
||||
vTaskDelay(pdMS_TO_TICKS(2000));
|
||||
|
||||
ESP_LOGI(TAG, "Sending RMT data...");
|
||||
// send data
|
||||
set_tx_data(tx_channel, cmd, addr, item_num, item, offset);
|
||||
rmt_write_items(tx_channel, item, item_num, 0);
|
||||
free(item);
|
||||
|
||||
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 < 100);
|
||||
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));
|
||||
}
|
||||
|
||||
TEST_CASE("RMT TX write item wait some ticks", "[rmt][test_env=UT_T1_RMT]")
|
||||
{
|
||||
tx_init();
|
||||
int tx_channel = RMT_TX_CHANNEL;
|
||||
uint16_t cmd = 0x0;
|
||||
uint16_t addr = 0x11;
|
||||
int tx_num = RMT_TX_DATA_NUM;
|
||||
ESP_LOGI(TAG, "RMT TX DATA");
|
||||
size_t size = (sizeof(rmt_item32_t) * DATA_ITEM_NUM * tx_num);
|
||||
rmt_item32_t* item = (rmt_item32_t*) malloc(size);
|
||||
int item_num = DATA_ITEM_NUM * tx_num;
|
||||
memset((void*) item, 0, size);
|
||||
int offset = 0;
|
||||
|
||||
int rx_channel = RMT_RX_CHANNEL;
|
||||
rx_init();
|
||||
RingbufHandle_t rb = NULL;
|
||||
rmt_get_ringbuf_handle(rx_channel, &rb);
|
||||
rmt_rx_start(rx_channel, 1);
|
||||
rmt_get_ringbuf_handle(RMT_RX_CHANNEL, &rb);
|
||||
rmt_rx_start(RMT_RX_CHANNEL, 1);
|
||||
ESP_LOGI(TAG, "Star receiving RMT data...");
|
||||
|
||||
tx_init();
|
||||
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(tx_channel, cmd, addr, item_num, item, offset);
|
||||
rmt_write_items(tx_channel, item, item_num, 0);
|
||||
rmt_wait_tx_done(tx_channel, portMAX_DELAY);
|
||||
free(item);
|
||||
set_tx_data(RMT_TX_CHANNEL, cmd, addr, num_items, items, 0);
|
||||
rmt_write_items(RMT_TX_CHANNEL, items, num_items, 0);
|
||||
rmt_wait_tx_done(RMT_TX_CHANNEL, portMAX_DELAY);
|
||||
free(items);
|
||||
|
||||
// receive data
|
||||
uint16_t tmp = get_rx_data(rb);
|
||||
TEST_ASSERT(tmp == 100);
|
||||
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));
|
||||
}
|
||||
|
||||
TEST_CASE("RMT TX stop test", "[rmt][test_env=UT_T1_RMT]")
|
||||
{
|
||||
int rx_channel = RMT_RX_CHANNEL;
|
||||
rx_init();
|
||||
RingbufHandle_t rb = NULL;
|
||||
rmt_get_ringbuf_handle(rx_channel, &rb);
|
||||
rmt_rx_start(rx_channel, 1);
|
||||
rmt_get_ringbuf_handle(RMT_RX_CHANNEL, &rb);
|
||||
rmt_rx_start(RMT_RX_CHANNEL, 1);
|
||||
ESP_LOGI(TAG, "Star receiving RMT data...");
|
||||
|
||||
vTaskDelay(10);
|
||||
tx_init();
|
||||
int tx_channel = RMT_TX_CHANNEL;
|
||||
int tx_num = RMT_TX_DATA_NUM;
|
||||
|
||||
ESP_LOGI(TAG, "RMT TX DATA");
|
||||
size_t size = (sizeof(rmt_item32_t) * DATA_ITEM_NUM * tx_num);
|
||||
rmt_item32_t* item = (rmt_item32_t*) malloc(size);
|
||||
int item_num = DATA_ITEM_NUM * tx_num;
|
||||
memset((void*) item, 0, size);
|
||||
int offset = 0;
|
||||
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(tx_channel, cmd, addr, item_num, item, offset);
|
||||
rmt_write_items(tx_channel, item, item_num, 0);
|
||||
set_tx_data(RMT_TX_CHANNEL, cmd, addr, num_items, items, 0);
|
||||
rmt_write_items(RMT_TX_CHANNEL, items, num_items, 0);
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
rmt_tx_stop(tx_channel);
|
||||
free(item);
|
||||
rmt_tx_stop(RMT_TX_CHANNEL);
|
||||
free(items);
|
||||
|
||||
// receive data
|
||||
uint16_t tmp = get_rx_data(rb);
|
||||
TEST_ASSERT(tmp < 100);
|
||||
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));
|
||||
@@ -761,47 +756,8 @@ TEST_CASE("RMT loop_en test", "[rmt][test_env=UT_T1_RMT][ignore]")
|
||||
|
||||
// receive data
|
||||
uint16_t tmp = get_rx_data(rb);
|
||||
TEST_ASSERT(tmp < 100);
|
||||
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));
|
||||
}
|
||||
|
||||
TEST_CASE("RMT use multi channel", "[rmt][test_env=UT_T1_RMT]")
|
||||
{
|
||||
rmt_tx_config_t tx_cfg = {
|
||||
.loop_en = true, // set it as true
|
||||
.carrier_duty_percent = 50,
|
||||
.carrier_freq_hz = 38000,
|
||||
.carrier_level = 1,
|
||||
.carrier_en = RMT_TX_CARRIER_EN,
|
||||
.idle_level = 0,
|
||||
.idle_output_en = true,
|
||||
};
|
||||
rmt_config_t rmt_tx1 = {
|
||||
.channel = RMT_TX_CHANNEL,
|
||||
.gpio_num = RMT_TX_GPIO_NUM,
|
||||
.mem_block_num = 4,
|
||||
.clk_div = RMT_CLK_DIV,
|
||||
.tx_config = tx_cfg,
|
||||
.rmt_mode = 0,
|
||||
};
|
||||
rmt_config(&rmt_tx1);
|
||||
rmt_driver_install(rmt_tx1.channel, 0, 0);
|
||||
|
||||
rmt_config_t rmt_tx2 = rmt_tx1;
|
||||
rmt_tx2.channel = 2;
|
||||
rmt_config(&rmt_tx2);
|
||||
rmt_driver_install(rmt_tx2.channel, 0, 0);
|
||||
|
||||
rmt_config_t rmt_tx3 = rmt_tx1;
|
||||
rmt_tx3.channel = 7;
|
||||
rmt_tx3.mem_block_num = 1;
|
||||
rmt_config(&rmt_tx3);
|
||||
rmt_driver_install(rmt_tx3.channel, 0, 0);
|
||||
|
||||
TEST_ESP_OK(rmt_driver_uninstall(RMT_TX_CHANNEL));
|
||||
TEST_ESP_OK(rmt_driver_uninstall(2));
|
||||
TEST_ESP_OK(rmt_driver_uninstall(7));
|
||||
}
|
||||
|
||||
|
@@ -12,6 +12,7 @@ list(APPEND include_dirs include)
|
||||
list(APPEND srcs
|
||||
"src/memory_layout_utils.c"
|
||||
"src/lldesc.c"
|
||||
"src/hal/rmt_hal.c"
|
||||
"src/hal/spi_hal.c"
|
||||
"src/hal/spi_hal_iram.c"
|
||||
"src/hal/spi_slave_hal.c"
|
||||
|
297
components/soc/esp32/include/hal/rmt_ll.h
Normal file
297
components/soc/esp32/include/hal/rmt_ll.h
Normal file
@@ -0,0 +1,297 @@
|
||||
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed 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.
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "soc/rmt_struct.h"
|
||||
#include "soc/rmt_caps.h"
|
||||
|
||||
static inline void rmt_ll_reset_counter_clock_div(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.ref_cnt_rst = 1;
|
||||
dev->conf_ch[channel].conf1.ref_cnt_rst = 0;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_reset_tx_pointer(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.mem_rd_rst = 1;
|
||||
dev->conf_ch[channel].conf1.mem_rd_rst = 0;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_reset_rx_pointer(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.mem_wr_rst = 1;
|
||||
dev->conf_ch[channel].conf1.mem_wr_rst = 0;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_start_tx(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.tx_start = 1;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_stop_tx(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
RMTMEM.chan[channel].data32[0].val = 0;
|
||||
dev->conf_ch[channel].conf1.tx_start = 0;
|
||||
dev->conf_ch[channel].conf1.mem_rd_rst = 1;
|
||||
dev->conf_ch[channel].conf1.mem_rd_rst = 0;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_rx(rmt_dev_t *dev, uint32_t channel, bool enable)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.rx_en = enable;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static inline bool rmt_ll_is_mem_power_down(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf0.mem_pd;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_mem_blocks(rmt_dev_t *dev, uint32_t channel, uint8_t block_num)
|
||||
{
|
||||
dev->conf_ch[channel].conf0.mem_size = block_num;
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_mem_blocks(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf0.mem_size;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_counter_clock_div(rmt_dev_t *dev, uint32_t channel, uint32_t div)
|
||||
{
|
||||
dev->conf_ch[channel].conf0.div_cnt = div;
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_counter_clock_div(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf0.div_cnt;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_tx_pingpong(rmt_dev_t *dev, bool enable)
|
||||
{
|
||||
dev->apb_conf.mem_tx_wrap_en = enable;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_mem_access(rmt_dev_t *dev, bool enable)
|
||||
{
|
||||
dev->apb_conf.fifo_mask = enable;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_rx_idle_thres(rmt_dev_t *dev, uint32_t channel, uint32_t thres)
|
||||
{
|
||||
dev->conf_ch[channel].conf0.idle_thres = thres;
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_rx_idle_thres(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf0.idle_thres;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_mem_owner(rmt_dev_t *dev, uint32_t channel, uint8_t owner)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.mem_owner = owner;
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_mem_owner(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf1.mem_owner;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_tx_cyclic(rmt_dev_t *dev, uint32_t channel, bool enable)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.tx_conti_mode = enable;
|
||||
}
|
||||
|
||||
static inline bool rmt_ll_is_tx_cyclic_enabled(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf1.tx_conti_mode;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_rx_filter(rmt_dev_t *dev, uint32_t channel, bool enable)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.rx_filter_en = enable;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_rx_filter_thres(rmt_dev_t *dev, uint32_t channel, uint32_t thres)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.rx_filter_thres = thres;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_counter_clock_src(rmt_dev_t *dev, uint32_t channel, uint8_t src)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.ref_always_on = src;
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_counter_clock_src(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf1.ref_always_on;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_tx_idle(rmt_dev_t *dev, uint32_t channel, bool enable)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.idle_out_en = enable;
|
||||
}
|
||||
|
||||
static inline bool rmt_ll_is_tx_idle_enabled(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf1.idle_out_en;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_tx_idle_level(rmt_dev_t *dev, uint32_t channel, uint8_t level)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.idle_out_lv = level;
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_tx_idle_level(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf1.idle_out_lv;
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_channel_status(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->status_ch[channel];
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_tx_limit(rmt_dev_t *dev, uint32_t channel, uint32_t limit)
|
||||
{
|
||||
dev->tx_lim_ch[channel].limit = limit;
|
||||
}
|
||||
|
||||
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));
|
||||
dev->int_ena.val |= (enable << (channel * 3));
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_rx_end_interrupt(rmt_dev_t *dev, uint32_t channel, bool enable)
|
||||
{
|
||||
dev->int_ena.val &= ~(1 << (channel * 3 + 1));
|
||||
dev->int_ena.val |= (enable << (channel * 3 + 1));
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_err_interrupt(rmt_dev_t *dev, uint32_t channel, bool enable)
|
||||
{
|
||||
dev->int_ena.val &= ~(1 << (channel * 3 + 2));
|
||||
dev->int_ena.val |= (enable << (channel * 3 + 2));
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_tx_thres_interrupt(rmt_dev_t *dev, uint32_t channel, bool enable)
|
||||
{
|
||||
dev->int_ena.val &= ~(1 << (channel + 24));
|
||||
dev->int_ena.val |= (enable << (channel + 24));
|
||||
}
|
||||
|
||||
static inline void rmt_ll_clear_tx_end_interrupt(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
dev->int_clr.val = (1 << (channel * 3));
|
||||
}
|
||||
|
||||
static inline void rmt_ll_clear_rx_end_interrupt(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
dev->int_clr.val = (1 << (channel * 3 + 1));
|
||||
}
|
||||
|
||||
static inline void rmt_ll_clear_err_interrupt(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
dev->int_clr.val = (1 << (channel * 3 + 2));
|
||||
}
|
||||
|
||||
static inline void rmt_ll_clear_tx_thres_interrupt(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
dev->int_clr.val = (1 << (channel + 24));
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_tx_end_interrupt_status(rmt_dev_t *dev)
|
||||
{
|
||||
uint32_t status = dev->int_st.val;
|
||||
return ((status & 0x01) >> 0) | ((status & 0x08) >> 2) | ((status & 0x40) >> 4) | ((status & 0x200) >> 6) |
|
||||
((status & 0x1000) >> 8) | ((status & 0x8000) >> 10) | ((status & 40000) >> 12) | ((status & 0x200000) >> 14);
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_rx_end_interrupt_status(rmt_dev_t *dev)
|
||||
{
|
||||
uint32_t status = dev->int_st.val;
|
||||
return ((status & 0x02) >> 1) | ((status & 0x10) >> 3) | ((status & 0x80) >> 5) | ((status & 0x400) >> 7) |
|
||||
((status & 0x2000) >> 9) | ((status & 0x10000) >> 11) | ((status & 80000) >> 13) | ((status & 0x400000) >> 15);
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_err_interrupt_status(rmt_dev_t *dev)
|
||||
{
|
||||
uint32_t status = dev->int_st.val;
|
||||
return ((status & 0x04) >> 2) | ((status & 0x20) >> 4) | ((status & 0x100) >> 6) | ((status & 0x800) >> 8) |
|
||||
((status & 0x4000) >> 10) | ((status & 0x20000) >> 12) | ((status & 100000) >> 14) | ((status & 0x800000) >> 16);
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_tx_thres_interrupt_status(rmt_dev_t *dev)
|
||||
{
|
||||
uint32_t status = dev->int_st.val;
|
||||
return (status & 0xFF000000) >> 24;
|
||||
}
|
||||
|
||||
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;
|
||||
dev->carrier_duty_ch[channel].low = low_ticks;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_get_carrier_high_low_ticks(rmt_dev_t *dev, uint32_t channel, uint32_t *high_ticks, uint32_t *low_ticks)
|
||||
{
|
||||
*high_ticks = dev->carrier_duty_ch[channel].high;
|
||||
*low_ticks = dev->carrier_duty_ch[channel].low;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_tx_carrier(rmt_dev_t *dev, uint32_t channel, bool enable)
|
||||
{
|
||||
dev->conf_ch[channel].conf0.carrier_en = enable;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_carrier_to_level(rmt_dev_t *dev, uint32_t channel, uint8_t level)
|
||||
{
|
||||
dev->conf_ch[channel].conf0.carrier_out_lv = level;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_write_memory(rmt_mem_t *mem, uint32_t channel, const rmt_item32_t *data, uint32_t length, uint32_t off)
|
||||
{
|
||||
length = (off + length) > RMT_CHANNEL_MEM_WORDS ? (RMT_CHANNEL_MEM_WORDS - off) : length;
|
||||
for (uint32_t i = 0; i < length; i++) {
|
||||
mem->chan[channel].data32[i + off].val = data[i].val;
|
||||
}
|
||||
}
|
||||
|
||||
/************************************************************************************************
|
||||
* Following Low Level APIs only used for backward compatible, will be deprecated in the future!
|
||||
***********************************************************************************************/
|
||||
|
||||
static inline void rmt_ll_set_intr_enable_mask(uint32_t mask)
|
||||
{
|
||||
RMT.int_ena.val |= mask;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_clr_intr_enable_mask(uint32_t mask)
|
||||
{
|
||||
RMT.int_ena.val &= (~mask);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
43
components/soc/esp32/include/soc/rmt_caps.h
Normal file
43
components/soc/esp32/include/soc/rmt_caps.h
Normal file
@@ -0,0 +1,43 @@
|
||||
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define RMT_CHANNEL_MEM_WORDS (64) /*!< Each channel owns 64 words memory */
|
||||
|
||||
/**
|
||||
* @brief RMT channel ID
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
RMT_CHANNEL_0, /*!< RMT channel number 0 */
|
||||
RMT_CHANNEL_1, /*!< RMT channel number 1 */
|
||||
RMT_CHANNEL_2, /*!< RMT channel number 2 */
|
||||
RMT_CHANNEL_3, /*!< RMT channel number 3 */
|
||||
RMT_CHANNEL_4, /*!< RMT channel number 4 */
|
||||
RMT_CHANNEL_5, /*!< RMT channel number 5 */
|
||||
RMT_CHANNEL_6, /*!< RMT channel number 6 */
|
||||
RMT_CHANNEL_7, /*!< RMT channel number 7 */
|
||||
RMT_CHANNEL_MAX /*!< Number of RMT channels */
|
||||
} rmt_channel_id_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
290
components/soc/esp32s2beta/include/hal/rmt_ll.h
Normal file
290
components/soc/esp32s2beta/include/hal/rmt_ll.h
Normal file
@@ -0,0 +1,290 @@
|
||||
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed 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.
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdbool.h>
|
||||
#include "soc/rmt_struct.h"
|
||||
#include "soc/rmt_caps.h"
|
||||
|
||||
static inline void rmt_ll_reset_counter_clock_div(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
static inline void rmt_ll_reset_tx_pointer(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.mem_rd_rst = 1;
|
||||
dev->conf_ch[channel].conf1.mem_rd_rst = 0;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_reset_rx_pointer(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.mem_wr_rst = 1;
|
||||
dev->conf_ch[channel].conf1.mem_wr_rst = 0;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_start_tx(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.tx_start = 1;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_stop_tx(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.tx_stop = 1;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_rx(rmt_dev_t *dev, uint32_t channel, bool enable)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.rx_en = enable;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
static inline bool rmt_ll_is_mem_power_down(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf0.mem_pd;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_mem_blocks(rmt_dev_t *dev, uint32_t channel, uint8_t block_num)
|
||||
{
|
||||
dev->conf_ch[channel].conf0.mem_size = block_num;
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_mem_blocks(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf0.mem_size;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_counter_clock_div(rmt_dev_t *dev, uint32_t channel, uint32_t div)
|
||||
{
|
||||
dev->conf_ch[channel].conf0.div_cnt = div;
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_counter_clock_div(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf0.div_cnt;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_tx_pingpong(rmt_dev_t *dev, bool enable)
|
||||
{
|
||||
dev->apb_conf.mem_tx_wrap_en = enable;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_mem_access(rmt_dev_t *dev, bool enable)
|
||||
{
|
||||
dev->apb_conf.fifo_mask = enable;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_rx_idle_thres(rmt_dev_t *dev, uint32_t channel, uint32_t thres)
|
||||
{
|
||||
dev->conf_ch[channel].conf0.idle_thres = thres;
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_rx_idle_thres(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf0.idle_thres;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_mem_owner(rmt_dev_t *dev, uint32_t channel, uint8_t owner)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.mem_owner = owner;
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_mem_owner(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf1.mem_owner;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_tx_cyclic(rmt_dev_t *dev, uint32_t channel, bool enable)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.tx_conti_mode = enable;
|
||||
}
|
||||
|
||||
static inline bool rmt_ll_is_tx_cyclic_enabled(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf1.tx_conti_mode;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_rx_filter(rmt_dev_t *dev, uint32_t channel, bool enable)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.rx_filter_en = enable;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_rx_filter_thres(rmt_dev_t *dev, uint32_t channel, uint32_t thres)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.rx_filter_thres = thres;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_counter_clock_src(rmt_dev_t *dev, uint32_t channel, uint8_t src)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.ref_always_on = src;
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_counter_clock_src(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf1.ref_always_on;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_tx_idle(rmt_dev_t *dev, uint32_t channel, bool enable)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.idle_out_en = enable;
|
||||
}
|
||||
|
||||
static inline bool rmt_ll_is_tx_idle_enabled(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf1.idle_out_en;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_tx_idle_level(rmt_dev_t *dev, uint32_t channel, uint8_t level)
|
||||
{
|
||||
dev->conf_ch[channel].conf1.idle_out_lv = level;
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_tx_idle_level(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->conf_ch[channel].conf1.idle_out_lv;
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_channel_status(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
return dev->status_ch[channel].val;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_tx_limit(rmt_dev_t *dev, uint32_t channel, uint32_t limit)
|
||||
{
|
||||
dev->tx_lim_ch[channel].limit = limit;
|
||||
}
|
||||
|
||||
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));
|
||||
dev->int_ena.val |= (enable << (channel * 3));
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_rx_end_interrupt(rmt_dev_t *dev, uint32_t channel, bool enable)
|
||||
{
|
||||
dev->int_ena.val &= ~(1 << (channel * 3 + 1));
|
||||
dev->int_ena.val |= (enable << (channel * 3 + 1));
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_err_interrupt(rmt_dev_t *dev, uint32_t channel, bool enable)
|
||||
{
|
||||
dev->int_ena.val &= ~(1 << (channel * 3 + 2));
|
||||
dev->int_ena.val |= (enable << (channel * 3 + 2));
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_tx_thres_interrupt(rmt_dev_t *dev, uint32_t channel, bool enable)
|
||||
{
|
||||
dev->int_ena.val &= ~(1 << (channel + 12));
|
||||
dev->int_ena.val |= (enable << (channel + 12));
|
||||
}
|
||||
|
||||
static inline void rmt_ll_clear_tx_end_interrupt(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
dev->int_clr.val = (1 << (channel * 3));
|
||||
}
|
||||
|
||||
static inline void rmt_ll_clear_rx_end_interrupt(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
dev->int_clr.val = (1 << (channel * 3 + 1));
|
||||
}
|
||||
|
||||
static inline void rmt_ll_clear_err_interrupt(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
dev->int_clr.val = (1 << (channel * 3 + 2));
|
||||
}
|
||||
|
||||
static inline void rmt_ll_clear_tx_thres_interrupt(rmt_dev_t *dev, uint32_t channel)
|
||||
{
|
||||
dev->int_clr.val = (1 << (channel + 12));
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_tx_end_interrupt_status(rmt_dev_t *dev)
|
||||
{
|
||||
uint32_t status = dev->int_st.val;
|
||||
return ((status & 0x01) >> 0) | ((status & 0x08) >> 2) | ((status & 0x40) >> 4) | ((status & 0x200) >> 6);
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_rx_end_interrupt_status(rmt_dev_t *dev)
|
||||
{
|
||||
uint32_t status = dev->int_st.val;
|
||||
return ((status & 0x02) >> 1) | ((status & 0x10) >> 3) | ((status & 0x80) >> 5) | ((status & 0x400) >> 7);
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_err_interrupt_status(rmt_dev_t *dev)
|
||||
{
|
||||
uint32_t status = dev->int_st.val;
|
||||
return ((status & 0x04) >> 2) | ((status & 0x20) >> 4) | ((status & 0x100) >> 6) | ((status & 0x800) >> 8);;
|
||||
}
|
||||
|
||||
static inline uint32_t rmt_ll_get_tx_thres_interrupt_status(rmt_dev_t *dev)
|
||||
{
|
||||
uint32_t status = dev->int_st.val;
|
||||
return (status & 0xF000) >> 12;
|
||||
}
|
||||
|
||||
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;
|
||||
dev->carrier_duty_ch[channel].low = low_ticks;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_get_carrier_high_low_ticks(rmt_dev_t *dev, uint32_t channel, uint32_t *high_ticks, uint32_t *low_ticks)
|
||||
{
|
||||
*high_ticks = dev->carrier_duty_ch[channel].high;
|
||||
*low_ticks = dev->carrier_duty_ch[channel].low;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_enable_tx_carrier(rmt_dev_t *dev, uint32_t channel, bool enable)
|
||||
{
|
||||
dev->conf_ch[channel].conf0.carrier_en = enable;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_set_carrier_to_level(rmt_dev_t *dev, uint32_t channel, uint8_t level)
|
||||
{
|
||||
dev->conf_ch[channel].conf0.carrier_out_lv = level;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_write_memory(rmt_mem_t *mem, uint32_t channel, const rmt_item32_t *data, uint32_t length, uint32_t off)
|
||||
{
|
||||
length = (off + length) > RMT_CHANNEL_MEM_WORDS ? (RMT_CHANNEL_MEM_WORDS - off) : length;
|
||||
for (uint32_t i = 0; i < length; i++) {
|
||||
mem->chan[channel].data32[i + off].val = data[i].val;
|
||||
}
|
||||
}
|
||||
|
||||
/************************************************************************************************
|
||||
* Following Low Level APIs only used for backward compatible, will be deprecated in the future!
|
||||
***********************************************************************************************/
|
||||
|
||||
static inline void rmt_ll_set_intr_enable_mask(uint32_t mask)
|
||||
{
|
||||
RMT.int_ena.val |= mask;
|
||||
}
|
||||
|
||||
static inline void rmt_ll_clr_intr_enable_mask(uint32_t mask)
|
||||
{
|
||||
RMT.int_ena.val &= (~mask);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
39
components/soc/esp32s2beta/include/soc/rmt_caps.h
Normal file
39
components/soc/esp32s2beta/include/soc/rmt_caps.h
Normal file
@@ -0,0 +1,39 @@
|
||||
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define RMT_CHANNEL_MEM_WORDS (64) /*!< Each channel owns 64 words memory */
|
||||
|
||||
/**
|
||||
* @brief RMT channel ID
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
RMT_CHANNEL_0, /*!< RMT channel number 0 */
|
||||
RMT_CHANNEL_1, /*!< RMT channel number 1 */
|
||||
RMT_CHANNEL_2, /*!< RMT channel number 2 */
|
||||
RMT_CHANNEL_3, /*!< RMT channel number 3 */
|
||||
RMT_CHANNEL_MAX /*!< Number of RMT channels */
|
||||
} rmt_channel_id_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@@ -13,6 +13,9 @@
|
||||
// limitations under the License.
|
||||
#ifndef _SOC_RMT_STRUCT_H_
|
||||
#define _SOC_RMT_STRUCT_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
142
components/soc/include/hal/rmt_hal.h
Normal file
142
components/soc/include/hal/rmt_hal.h
Normal file
@@ -0,0 +1,142 @@
|
||||
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed 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.
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "soc/rmt_struct.h"
|
||||
#include "soc/rmt_caps.h"
|
||||
|
||||
/**
|
||||
* @brief HAL context type of RMT driver
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
rmt_dev_t *regs; /*!< RMT Register base address */
|
||||
rmt_mem_t *mem; /*!< RMT Memory base address */
|
||||
} rmt_hal_context_t;
|
||||
|
||||
#define RMT_MEM_OWNER_SW (0) /*!< RMT Memory ownership belongs to software side */
|
||||
#define RMT_MEM_OWNER_HW (1) /*!< RMT Memory ownership belongs to hardware side */
|
||||
|
||||
/**
|
||||
* @brief Initialize the RMT HAL driver
|
||||
*
|
||||
* @param hal: RMT HAL context
|
||||
*/
|
||||
void rmt_hal_init(rmt_hal_context_t *hal);
|
||||
|
||||
/**
|
||||
* @brief Reset RMT HAL driver
|
||||
*
|
||||
* @param hal: RMT HAL context
|
||||
*/
|
||||
void rmt_hal_reset(rmt_hal_context_t *hal);
|
||||
|
||||
/**
|
||||
* @brief Reset RMT Channel specific HAL driver
|
||||
*
|
||||
* @param hal: RMT HAL context
|
||||
* @param channel: RMT channel number
|
||||
*/
|
||||
void rmt_hal_channel_reset(rmt_hal_context_t *hal, uint32_t channel);
|
||||
|
||||
/**
|
||||
* @brief Set counter clock for RMT channel
|
||||
*
|
||||
* @param hal: RMT HAL context
|
||||
* @param channel: RMT channel number
|
||||
* @param base_clk_hz: base clock for RMT internal channel (counter clock will divide from it)
|
||||
* @param counter_clk_hz: target counter clock
|
||||
*/
|
||||
void rmt_hal_set_counter_clock(rmt_hal_context_t *hal, uint32_t channel, uint32_t base_clk_hz, uint32_t counter_clk_hz);
|
||||
|
||||
/**
|
||||
* @brief Get counter clock for RMT channel
|
||||
*
|
||||
* @param hal: RMT HAL context
|
||||
* @param channel: RMT channel number
|
||||
* @param base_clk_hz: base clock for RMT internal channel (counter clock will divide from it)
|
||||
* @return counter clock in Hz
|
||||
*/
|
||||
uint32_t rmt_hal_get_counter_clock(rmt_hal_context_t *hal, uint32_t channel, uint32_t base_clk_hz);
|
||||
|
||||
/**
|
||||
* @brief Set carrier clock for RMT channel
|
||||
*
|
||||
* @param hal: RMT HAL context
|
||||
* @param channel: RMT channel number
|
||||
* @param base_clk_hz: base clock for RMT carrier generation (carrier clock will divide from it)
|
||||
* @param carrier_clk_hz: target carrier clock
|
||||
* @param carrier_clk_duty: duty ratio of carrier clock
|
||||
*/
|
||||
void rmt_hal_set_carrier_clock(rmt_hal_context_t *hal, uint32_t channel, uint32_t base_clk_hz, uint32_t carrier_clk_hz, float carrier_clk_duty);
|
||||
|
||||
/**
|
||||
* @brief Get carrier clock for RMT channel
|
||||
*
|
||||
* @param hal: RMT HAL context
|
||||
* @param channel: RMT channel number
|
||||
* @param base_clk_hz: base clock for RMT carrier generation
|
||||
* @param carrier_clk_hz: target carrier clock
|
||||
* @param carrier_clk_duty: duty ratio of carrier clock
|
||||
*/
|
||||
void rmt_hal_get_carrier_clock(rmt_hal_context_t *hal, uint32_t channel, uint32_t base_clk_hz, uint32_t *carrier_clk_hz, float *carrier_clk_duty);
|
||||
|
||||
/**
|
||||
* @brief Set filter threshold for RMT Receive channel
|
||||
*
|
||||
* @param hal: RMT HAL context
|
||||
* @param channel: RMT channel number
|
||||
* @param base_clk_hz: base clock for RMT receive filter
|
||||
* @param thres_us: threshold of RMT receive filter, in us
|
||||
*/
|
||||
void rmt_hal_set_rx_filter_thres(rmt_hal_context_t *hal, uint32_t channel, uint32_t base_clk_hz, uint32_t thres_us);
|
||||
|
||||
/**
|
||||
* @brief Set idle threshold for RMT Receive channel
|
||||
*
|
||||
* @param hal: RMT HAL context
|
||||
* @param channel: RMT channel number
|
||||
* @param base_clk_hz: base clock for RMT receive channel
|
||||
* @param thres_us: IDLE threshold for RMT receive channel
|
||||
*/
|
||||
void rmt_hal_set_rx_idle_thres(rmt_hal_context_t *hal, uint32_t channel, uint32_t base_clk_hz, uint32_t thres_us);
|
||||
|
||||
/**
|
||||
* @brief Receive a frame from RMT channel
|
||||
*
|
||||
* @param hal: RMT HAL context
|
||||
* @param channel: RMT channel number
|
||||
* @param buf: buffer to store received RMT frame
|
||||
* @return number of items that get received
|
||||
*/
|
||||
uint32_t rmt_hal_receive(rmt_hal_context_t *hal, uint32_t channel, rmt_item32_t *buf);
|
||||
|
||||
/**
|
||||
* @brief Transmit a from by RMT
|
||||
*
|
||||
* @param hal: RMT HAL context
|
||||
* @param channel: RMT channel number
|
||||
* @param src: RMT items to transmit
|
||||
* @param length: length of RMT items to transmit
|
||||
* @param offset: offset of RMT internal memory to store the items
|
||||
*/
|
||||
void rmt_hal_transmit(rmt_hal_context_t *hal, uint32_t channel, const rmt_item32_t *src, uint32_t length, uint32_t offset);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
108
components/soc/include/hal/rmt_types.h
Normal file
108
components/soc/include/hal/rmt_types.h
Normal file
@@ -0,0 +1,108 @@
|
||||
// Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief RMT Channel Type
|
||||
*
|
||||
*/
|
||||
typedef rmt_channel_id_t rmt_channel_t;
|
||||
|
||||
/**
|
||||
* @brief RMT Internal Memory Owner
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
RMT_MEM_OWNER_TX, /*!< RMT RX mode, RMT transmitter owns the memory block*/
|
||||
RMT_MEM_OWNER_RX, /*!< RMT RX mode, RMT receiver owns the memory block*/
|
||||
RMT_MEM_OWNER_MAX,
|
||||
} rmt_mem_owner_t;
|
||||
|
||||
/**
|
||||
* @brief Clock Source of RMT Channel
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
RMT_BASECLK_REF, /*!< RMT source clock system reference tick, 1MHz by default (not supported in this version) */
|
||||
RMT_BASECLK_APB, /*!< RMT source clock is APB CLK, 80Mhz by default */
|
||||
RMT_BASECLK_MAX,
|
||||
} rmt_source_clk_t;
|
||||
|
||||
/**
|
||||
* @brief RMT Data Mode
|
||||
*
|
||||
* @note We highly recommended to use MEM mode not FIFO mode since there will be some gotcha in FIFO mode.
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
RMT_DATA_MODE_FIFO, /*<! RMT memory access in FIFO mode */
|
||||
RMT_DATA_MODE_MEM, /*<! RMT memory access in memory mode */
|
||||
RMT_DATA_MODE_MAX,
|
||||
} rmt_data_mode_t;
|
||||
|
||||
/**
|
||||
* @brief RMT Channel Working Mode (TX or RX)
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
RMT_MODE_TX, /*!< RMT TX mode */
|
||||
RMT_MODE_RX, /*!< RMT RX mode */
|
||||
RMT_MODE_MAX
|
||||
} rmt_mode_t;
|
||||
|
||||
/**
|
||||
* @brief RMT Idle Level
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
RMT_IDLE_LEVEL_LOW, /*!< RMT TX idle level: low Level */
|
||||
RMT_IDLE_LEVEL_HIGH, /*!< RMT TX idle level: high Level */
|
||||
RMT_IDLE_LEVEL_MAX,
|
||||
} rmt_idle_level_t;
|
||||
|
||||
/**
|
||||
* @brief RMT Carrier Level
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
RMT_CARRIER_LEVEL_LOW, /*!< RMT carrier wave is modulated for low Level output */
|
||||
RMT_CARRIER_LEVEL_HIGH, /*!< RMT carrier wave is modulated for high Level output */
|
||||
RMT_CARRIER_LEVEL_MAX
|
||||
} rmt_carrier_level_t;
|
||||
|
||||
/**
|
||||
* @brief RMT Channel Status
|
||||
*
|
||||
*/
|
||||
typedef enum {
|
||||
RMT_CHANNEL_UNINIT, /*!< RMT channel uninitialized */
|
||||
RMT_CHANNEL_IDLE, /*!< RMT channel status idle */
|
||||
RMT_CHANNEL_BUSY, /*!< RMT channel status busy */
|
||||
} rmt_channel_status_t;
|
||||
|
||||
/**
|
||||
* @brief Data struct of RMT channel status
|
||||
*/
|
||||
typedef struct {
|
||||
rmt_channel_status_t status[RMT_CHANNEL_MAX]; /*!< Store the current status of each channel */
|
||||
} rmt_channel_status_result_t;
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
106
components/soc/src/hal/rmt_hal.c
Normal file
106
components/soc/src/hal/rmt_hal.c
Normal file
@@ -0,0 +1,106 @@
|
||||
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed 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.
|
||||
#include "hal/rmt_hal.h"
|
||||
#include "hal/rmt_ll.h"
|
||||
|
||||
void rmt_hal_init(rmt_hal_context_t *hal)
|
||||
{
|
||||
hal->regs = &RMT;
|
||||
hal->mem = &RMTMEM;
|
||||
}
|
||||
|
||||
void rmt_hal_reset(rmt_hal_context_t *hal)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void rmt_hal_channel_reset(rmt_hal_context_t *hal, uint32_t channel)
|
||||
{
|
||||
rmt_ll_reset_tx_pointer(hal->regs, channel);
|
||||
rmt_ll_reset_rx_pointer(hal->regs, channel);
|
||||
rmt_ll_enable_err_interrupt(hal->regs, channel, false);
|
||||
rmt_ll_enable_tx_end_interrupt(hal->regs, channel, false);
|
||||
rmt_ll_enable_tx_thres_interrupt(hal->regs, channel, false);
|
||||
rmt_ll_enable_rx_end_interrupt(hal->regs, channel, false);
|
||||
rmt_ll_clear_err_interrupt(hal->regs, channel);
|
||||
rmt_ll_clear_tx_end_interrupt(hal->regs, channel);
|
||||
rmt_ll_clear_tx_thres_interrupt(hal->regs, channel);
|
||||
rmt_ll_clear_rx_end_interrupt(hal->regs, channel);
|
||||
}
|
||||
|
||||
void rmt_hal_set_counter_clock(rmt_hal_context_t *hal, uint32_t channel, uint32_t base_clk_hz, uint32_t counter_clk_hz)
|
||||
{
|
||||
rmt_ll_reset_counter_clock_div(hal->regs, channel);
|
||||
uint32_t counter_div = (base_clk_hz + counter_clk_hz / 2) / counter_clk_hz;
|
||||
rmt_ll_set_counter_clock_div(hal->regs, channel, counter_div);
|
||||
}
|
||||
|
||||
uint32_t rmt_hal_get_counter_clock(rmt_hal_context_t *hal, uint32_t channel, uint32_t base_clk_hz)
|
||||
{
|
||||
return base_clk_hz / rmt_ll_get_counter_clock_div(hal->regs, channel);
|
||||
}
|
||||
|
||||
void rmt_hal_set_carrier_clock(rmt_hal_context_t *hal, uint32_t channel, uint32_t base_clk_hz, uint32_t carrier_clk_hz, float carrier_clk_duty)
|
||||
{
|
||||
uint32_t carrier_div = (base_clk_hz + carrier_clk_hz / 2) / carrier_clk_hz;
|
||||
uint32_t div_high = (uint32_t)(carrier_div * carrier_clk_duty);
|
||||
uint32_t div_low = carrier_div - div_high;
|
||||
rmt_ll_set_carrier_high_low_ticks(hal->regs, channel, div_high, div_low);
|
||||
}
|
||||
|
||||
void rmt_hal_get_carrier_clock(rmt_hal_context_t *hal, uint32_t channel, uint32_t base_clk_hz, uint32_t *carrier_clk_hz, float *carrier_clk_duty)
|
||||
{
|
||||
uint32_t div_high = 0;
|
||||
uint32_t div_low = 0;
|
||||
rmt_ll_get_carrier_high_low_ticks(hal->regs, channel, &div_high, &div_low);
|
||||
*carrier_clk_hz = base_clk_hz / (div_high + div_low);
|
||||
*carrier_clk_duty = (float)div_high / (div_high + div_low);
|
||||
}
|
||||
|
||||
void rmt_hal_set_rx_filter_thres(rmt_hal_context_t *hal, uint32_t channel, uint32_t base_clk_hz, uint32_t thres_us)
|
||||
{
|
||||
uint32_t thres = (uint32_t)(base_clk_hz / 1e6 * thres_us);
|
||||
rmt_ll_set_rx_filter_thres(hal->regs, channel, thres);
|
||||
}
|
||||
|
||||
void rmt_hal_set_rx_idle_thres(rmt_hal_context_t *hal, uint32_t channel, uint32_t base_clk_hz, uint32_t thres_us)
|
||||
{
|
||||
uint32_t thres = (uint32_t)(base_clk_hz / 1e6 * thres_us);
|
||||
rmt_ll_set_rx_idle_thres(hal->regs, channel, thres);
|
||||
}
|
||||
|
||||
uint32_t rmt_hal_receive(rmt_hal_context_t *hal, uint32_t channel, rmt_item32_t *buf)
|
||||
{
|
||||
uint32_t len = 0;
|
||||
rmt_ll_set_mem_owner(hal->regs, channel, RMT_MEM_OWNER_SW);
|
||||
for (len = 0; len < RMT_CHANNEL_MEM_WORDS; len++) {
|
||||
buf[len].val = hal->mem->chan[channel].data32[len].val;
|
||||
if (!(buf[len].val & 0x7FFF)) {
|
||||
break;
|
||||
} else if (!(buf[len].val & 0x7FFF0000)) {
|
||||
len++;
|
||||
break;
|
||||
}
|
||||
}
|
||||
rmt_ll_set_mem_owner(hal->regs, channel, RMT_MEM_OWNER_HW);
|
||||
rmt_ll_reset_rx_pointer(hal->regs, channel);
|
||||
return len;
|
||||
}
|
||||
|
||||
void rmt_hal_transmit(rmt_hal_context_t *hal, uint32_t channel, const rmt_item32_t *src, uint32_t length, uint32_t offset)
|
||||
{
|
||||
rmt_ll_set_mem_owner(hal->regs, channel, RMT_MEM_OWNER_SW);
|
||||
rmt_ll_write_memory(hal->mem, channel, src, length, offset);
|
||||
rmt_ll_set_mem_owner(hal->regs, channel, RMT_MEM_OWNER_HW);
|
||||
}
|
@@ -103,6 +103,7 @@ INPUT = \
|
||||
../../components/driver/include/driver/touch_pad.h \
|
||||
../../components/driver/include/driver/uart.h \
|
||||
../../components/esp_adc_cal/include/esp_adc_cal.h \
|
||||
../../components/soc/include/hal/rmt_types.h \
|
||||
../../components/soc/include/hal/spi_types.h \
|
||||
../../components/soc/include/hal/pcnt_types.h \
|
||||
../../components/soc/include/hal/i2s_types.h \
|
||||
|
@@ -153,7 +153,7 @@ Now, depending on how the channel is configured, we are ready to either `Transmi
|
||||
Transmit Data
|
||||
-------------
|
||||
|
||||
Before being able to transmit some RMT pulses, we need to define the pulse pattern. The minimum pattern recognized by the RMT controller, later called an 'item', is provided in a structure :cpp:type:`rmt_item32_t`, see :component_file:`soc/esp32/include/soc/rmt_struct.h`. Each item consists of two pairs of two values. The first value in a pair describes the signal duration in ticks and is 15 bits long, the second provides the signal level (high or low) and is contained in a single bit. A block of couple of items and the structure of an item is presented below.
|
||||
Before being able to transmit some RMT pulses, we need to define the pulse pattern. The minimum pattern recognized by the RMT controller, later called an 'item', is provided in a structure :cpp:type:`rmt_item32_t`, see :component_file:`soc/esp32/include/soc/rmt_caps.h`. Each item consists of two pairs of two values. The first value in a pair describes the signal duration in ticks and is 15 bits long, the second provides the signal level (high or low) and is contained in a single bit. A block of couple of items and the structure of an item is presented below.
|
||||
|
||||
.. packetdiag::
|
||||
:caption: Structure of RMT items (L - signal level)
|
||||
@@ -176,7 +176,7 @@ Before being able to transmit some RMT pulses, we need to define the pulse patte
|
||||
127: L
|
||||
}
|
||||
|
||||
For a simple example how to define a block of items see :example:`peripherals/rmt_tx`.
|
||||
For a simple example how to define a block of items see :example:`peripherals/rmt/morse_code`.
|
||||
|
||||
The items are provided to the RMT controller by calling function :cpp:func:`rmt_write_items`. This function also automatically triggers start of transmission. It may be called to wait for transmission completion or exit just after transmission start. In such case you can wait for the transmission end by calling :cpp:func:`rmt_wait_tx_done`. This function does not limit the number of data items to transmit. It is using an interrupt to successively copy the new data chunks to RMT's internal memory as previously provided data are sent out.
|
||||
|
||||
@@ -188,7 +188,7 @@ Receive Data
|
||||
|
||||
Before starting the receiver we need some storage for incoming items. The RMT controller has 512 x 32-bits of internal RAM shared between all eight channels. In typical scenarios it is not enough as an ultimate storage for all incoming (and outgoing) items. Therefore this API supports retrieval of incoming items on the fly to save them in a ring buffer of a size defined by the user. The size is provided when calling :cpp:func:`rmt_driver_install` discussed above. To get a handle to this buffer call :cpp:func:`rmt_get_ringbuf_handle`.
|
||||
|
||||
With the above steps complete we can start the receiver by calling :cpp:func:`rmt_rx_start` and then move to checking what's inside the buffer. To do so, you can use common FreeRTOS functions that interact with the ring buffer. Please see an example how to do it in :example:`peripherals/rmt_nec_tx_rx`.
|
||||
With the above steps complete we can start the receiver by calling :cpp:func:`rmt_rx_start` and then move to checking what's inside the buffer. To do so, you can use common FreeRTOS functions that interact with the ring buffer. Please see an example how to do it in :example:`peripherals/rmt/ir_protocols`.
|
||||
|
||||
To stop the receiver, call :cpp:func:`rmt_rx_stop`.
|
||||
|
||||
@@ -256,8 +256,9 @@ If the RMT driver has been installed with :cpp:func:`rmt_driver_install` for som
|
||||
Application Examples
|
||||
--------------------
|
||||
|
||||
* A simple RMT TX example: :example:`peripherals/rmt_tx`.
|
||||
* NEC remote control TX and RX example: :example:`peripherals/rmt_nec_tx_rx`.
|
||||
* A simple RMT TX example: :example:`peripherals/rmt/morse_code`.
|
||||
* Another RMT TX example, specific to drive a common RGB LED strip: :example:`peripherals/rmt/led_strip`.
|
||||
* NEC remote control TX and RX example: :example:`peripherals/rmt/ir_protocols`.
|
||||
|
||||
|
||||
API Reference
|
||||
|
@@ -3,4 +3,4 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(infrared_nec)
|
||||
project(ir_protocols)
|
@@ -3,7 +3,7 @@
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := infrared_nec
|
||||
PROJECT_NAME := ir_protocols
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
72
examples/peripherals/rmt/ir_protocols/README.md
Normal file
72
examples/peripherals/rmt/ir_protocols/README.md
Normal file
@@ -0,0 +1,72 @@
|
||||
# IR Protocol Example
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
This example illustrates how to encode and decode RMT signals with/to common IR protocols (e.g. NEC and RC5).
|
||||
|
||||
[NEC](https://www.sbprojects.net/knowledge/ir/nec.php) and [RC5](https://www.sbprojects.net/knowledge/ir/rc5.php) have different encoding rules, but both can be compatible to RMT data format.
|
||||
|
||||
The example supports building and parsing both normal and extended NEC/RC5 protocol. And also supports `repeat code` which would be sent out if one remote key got pressed for a specific long time.
|
||||
|
||||
## How to Use Example
|
||||
|
||||
### Hardware Required
|
||||
|
||||
* A development board with ESP32 SoC (e.g. ESP32-DevKitC or ESP-WROVER-KIT)
|
||||
* An USB cable for power supply and programming
|
||||
* A 5mm infrared LED (e.g. IR333C) used to transmit encoded IR signals
|
||||
* An infrared receiver module (e.g. IRM-3638T), which integrates a demodulator and AGC circuit.
|
||||
|
||||
Example connection :
|
||||
|
||||
| ESP32 | IR333C | IRM-3638T |
|
||||
| -------- | ------ | --------- |
|
||||
| GPIO18 | Tx | × |
|
||||
| GPIO19 | × | Rx |
|
||||
| VCC 5V | √ | × |
|
||||
| VCC 3.3V | × | √ |
|
||||
| GND | GND | GND |
|
||||
|
||||
|
||||
### Configure the Project
|
||||
|
||||
Open the project configuration menu (`idf.py menuconfig`).
|
||||
|
||||
In the `Example Connection Configuration` menu:
|
||||
|
||||
* Select the infrared protocol used in the example under `Infrared Protocol` option.
|
||||
* Set the GPIO number used for transmitting the IR signal under `RMT TX GPIO` option.
|
||||
* Set the GPIO number used for receiving the demodulated IR signal under `RMT RX GPIO` option.
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Run `idf.py -p PORT flash monitor` to build, flash and monitor the project.
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
Run this example, you will see the following output log (for NEC protocol):
|
||||
```
|
||||
I (2000) example: Send command 0x20 to address 0x10
|
||||
I (2070) example: Scan Code --- addr: 0x0010 cmd: 0x0020
|
||||
I (2220) example: Scan Code (repeat) --- addr: 0x0010 cmd: 0x0020
|
||||
I (4240) example: Send command 0x21 to address 0x10
|
||||
I (4310) example: Scan Code --- addr: 0x0010 cmd: 0x0021
|
||||
I (4460) example: Scan Code (repeat) --- addr: 0x0010 cmd: 0x0021
|
||||
I (6480) example: Send command 0x22 to address 0x10
|
||||
I (6550) example: Scan Code --- addr: 0x0010 cmd: 0x0022
|
||||
I (6700) example: Scan Code (repeat) --- addr: 0x0010 cmd: 0x0022
|
||||
I (8720) example: Send command 0x23 to address 0x10
|
||||
I (8790) example: Scan Code --- addr: 0x0010 cmd: 0x0023
|
||||
I (8940) example: Scan Code (repeat) --- addr: 0x0010 cmd: 0x0023
|
||||
I (10960) example: Send command 0x24 to address 0x10
|
||||
I (11030) example: Scan Code --- addr: 0x0010 cmd: 0x0024
|
||||
I (11180) example: Scan Code (repeat) --- addr: 0x0010 cmd: 0x0024
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
For any technical queries, please open an [issue] (https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.
|
@@ -0,0 +1,11 @@
|
||||
set(component_srcs "src/ir_builder_rmt_nec.c"
|
||||
"src/ir_builder_rmt_rc5.c"
|
||||
"src/ir_parser_rmt_nec.c"
|
||||
"src/ir_parser_rmt_rc5.c")
|
||||
|
||||
idf_component_register(SRCS "${component_srcs}"
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_INCLUDE_DIRS ""
|
||||
PRIV_REQUIRES "driver"
|
||||
REQUIRES "")
|
||||
|
@@ -0,0 +1,3 @@
|
||||
COMPONENT_ADD_INCLUDEDIRS := include
|
||||
|
||||
COMPONENT_SRCDIRS := src
|
@@ -0,0 +1,43 @@
|
||||
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Timings for NEC protocol
|
||||
*
|
||||
*/
|
||||
#define NEC_LEADING_CODE_HIGH_US (9000)
|
||||
#define NEC_LEADING_CODE_LOW_US (4500)
|
||||
#define NEC_PAYLOAD_ONE_HIGH_US (560)
|
||||
#define NEC_PAYLOAD_ONE_LOW_US (1690)
|
||||
#define NEC_PAYLOAD_ZERO_HIGH_US (560)
|
||||
#define NEC_PAYLOAD_ZERO_LOW_US (560)
|
||||
#define NEC_REPEAT_CODE_HIGH_US (9000)
|
||||
#define NEC_REPEAT_CODE_LOW_US (2250)
|
||||
#define NEC_ENDING_CODE_HIGH_US (560)
|
||||
|
||||
/**
|
||||
* @brief Timings for RC5 protocol
|
||||
*
|
||||
*/
|
||||
#define RC5_PULSE_DURATION_US (889)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@@ -0,0 +1,272 @@
|
||||
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed 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.
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
#define IR_TOOLS_FLAGS_PROTO_EXT (1 << 0) /*!< Enable Extended IR protocol */
|
||||
#define IR_TOOLS_FLAGS_INVERSE (1 << 1) /*!< Inverse the IR signal, i.e. take high level as low, and vice versa */
|
||||
|
||||
/**
|
||||
* @brief IR device type
|
||||
*
|
||||
*/
|
||||
typedef void *ir_dev_t;
|
||||
|
||||
/**
|
||||
* @brief IR builder type
|
||||
*
|
||||
*/
|
||||
typedef struct ir_builder_s ir_builder_t;
|
||||
|
||||
/**
|
||||
* @brief IR parser type
|
||||
*
|
||||
*/
|
||||
typedef struct ir_parser_s ir_parser_t;
|
||||
|
||||
/**
|
||||
* @brief Type definition of IR builder
|
||||
*
|
||||
*/
|
||||
struct ir_builder_s {
|
||||
/**
|
||||
* @brief Period time of sending repeat code
|
||||
*
|
||||
*/
|
||||
uint32_t repeat_period_ms;
|
||||
|
||||
/**
|
||||
* @brief Build frame header
|
||||
*
|
||||
* @param[in] builder: Handle of IR builder
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Build frame header successfully
|
||||
* - ESP_FAIL: Build frame header failed because some error occurred
|
||||
*/
|
||||
esp_err_t (*make_head)(ir_builder_t *builder);
|
||||
|
||||
/**
|
||||
* @brief Build logic bit zero
|
||||
*
|
||||
* @param[in] builder: Handle of IR builder
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Build logic bit zero successfully
|
||||
* - ESP_FAIL: Build logic bit zero failed because some error occurred
|
||||
*/
|
||||
esp_err_t (*make_logic0)(ir_builder_t *builder);
|
||||
|
||||
/**
|
||||
* @brief Build logic bit one
|
||||
*
|
||||
* @param[in] builder: Handle of IR builder
|
||||
*
|
||||
* @return
|
||||
* ESP_OK: Build logic bit one successfully
|
||||
* ESP_FAIL: Build logic bit one failed because some error occurred
|
||||
*/
|
||||
esp_err_t (*make_logic1)(ir_builder_t *builder);
|
||||
|
||||
/**
|
||||
* @brief Build frame tail
|
||||
*
|
||||
* @param[in] builder: Handle of IR builder
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Build frame tail successfully
|
||||
* - ESP_FAIL: Build frame tail failed because some error occurred
|
||||
*/
|
||||
esp_err_t (*make_end)(ir_builder_t *builder);
|
||||
|
||||
/**
|
||||
* @brief Build a complete frame
|
||||
*
|
||||
* @param[in] builder: Handle of IR builder
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Build a complete frame successfully
|
||||
* - ESP_FAIL: Build a complete frame failed because some error occurred
|
||||
*/
|
||||
esp_err_t (*build_frame)(ir_builder_t *builder, uint32_t address, uint32_t command);
|
||||
|
||||
/**
|
||||
* @brief Build a repeat frame
|
||||
*
|
||||
* @param[in] builder: Handle of IR builder
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Build a repeat frame successfully
|
||||
* - ESP_FAIL: Build a repeat frame failed because some error occurred
|
||||
*/
|
||||
esp_err_t (*build_repeat_frame)(ir_builder_t *builder);
|
||||
|
||||
/**
|
||||
* @brief Get the result frame after a series of building steps
|
||||
*
|
||||
* @param[in] builder: Handle of IR builder
|
||||
* @param[out] result: Result of frame building, which contains all of the raw data that could be send directly
|
||||
* @param[out] length: Length of result data
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Get result data successfully
|
||||
* - ESP_ERR_INVALID_ARG: Get result data failed because of invalid arguments
|
||||
* - ESP_FAIL: Get result data failed because some other errors occurred
|
||||
*/
|
||||
esp_err_t (*get_result)(ir_builder_t *builder, void *result, uint32_t *length);
|
||||
|
||||
/**
|
||||
* @brief Free resources used by IR builder
|
||||
*
|
||||
* @param[in] builder: Handle of IR builder
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Free resources successfully
|
||||
* - ESP_FAIL: Free resources failed because some error occurred
|
||||
*/
|
||||
esp_err_t (*del)(ir_builder_t *builder);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Type definition of IR parser
|
||||
*
|
||||
*/
|
||||
struct ir_parser_s {
|
||||
/**
|
||||
* @brief Input raw data to IR parser
|
||||
*
|
||||
* @param[in] parser: Handle of IR parser
|
||||
* @param[in] raw_data: Raw data which need decoding by IR parser
|
||||
* @param[in] length: Length of raw data
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Input raw data successfully
|
||||
* - ESP_ERR_INVALID_ARG: Input raw data failed because of invalid argument
|
||||
* - ESP_FAIL: Input raw data failed because some other error occurred
|
||||
*/
|
||||
esp_err_t (*input)(ir_parser_t *parser, void *raw_data, uint32_t length);
|
||||
|
||||
/**
|
||||
* @brief Get the scan code after decoding of raw data
|
||||
*
|
||||
* @param[in] parser: Handle of IR parser
|
||||
* @param[out] address: Address of the scan code
|
||||
* @param[out] command: Command of the scan code
|
||||
* @param[out] repeat: Indicate if it's a repeat code
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Get scan code successfully
|
||||
* - ESP_ERR_INVALID_ARG: Get scan code failed because of invalid arguments
|
||||
* - ESP_FAIL: Get scan code failed because some error occurred
|
||||
*/
|
||||
esp_err_t (*get_scan_code)(ir_parser_t *parser, uint32_t *address, uint32_t *command, bool *repeat);
|
||||
|
||||
/**
|
||||
* @brief Free resources used by IR parser
|
||||
*
|
||||
* @param[in] parser: Handle of IR parser
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Free resource successfully
|
||||
* - ESP_FAIL: Free resources fail failed because some error occurred
|
||||
*/
|
||||
esp_err_t (*del)(ir_parser_t *parser);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Configuration type of IR builder
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t buffer_size; /*!< Size of the internal buffer used by IR builder */
|
||||
ir_dev_t dev_hdl; /*!< IR device handle */
|
||||
uint32_t flags; /*!< Flags for IR builder, different flags will enable different features */
|
||||
} ir_builder_config_t;
|
||||
|
||||
/**
|
||||
* @brief Configuration type of IR parser
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
ir_dev_t dev_hdl; /*!< IR device handle */
|
||||
uint32_t flags; /*!< Flags for IR parser, different flags will enable different features */
|
||||
uint32_t margin_us; /*!< Timing parameter, indicating the tolerance to environment noise */
|
||||
} ir_parser_config_t;
|
||||
|
||||
/**
|
||||
* @brief Default configuration for IR builder
|
||||
*
|
||||
*/
|
||||
#define IR_BUILDER_DEFAULT_CONFIG(dev) \
|
||||
{ \
|
||||
.buffer_size = 64, \
|
||||
.dev_hdl = dev, \
|
||||
.flags = 0, \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Default configuration for IR parser
|
||||
*
|
||||
*/
|
||||
#define IR_PARSER_DEFAULT_CONFIG(dev) \
|
||||
{ \
|
||||
.dev_hdl = dev, \
|
||||
.flags = 0, \
|
||||
.margin_us = 200, \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Creat a NEC protocol builder
|
||||
*
|
||||
* @param config: configuration of NEC builder
|
||||
* @return
|
||||
* Handle of NEC builder or NULL
|
||||
*/
|
||||
ir_builder_t *ir_builder_rmt_new_nec(const ir_builder_config_t *config);
|
||||
|
||||
/**
|
||||
* @brief Creat a RC5 protocol builder
|
||||
*
|
||||
* @param config: configuration of RC5 builder
|
||||
* @return
|
||||
* Handle of RC5 builder or NULL
|
||||
*/
|
||||
ir_builder_t *ir_builder_rmt_new_rc5(const ir_builder_config_t *config);
|
||||
|
||||
/**
|
||||
* @brief Creat a NEC protocol parser
|
||||
*
|
||||
* @param config: configuration of NEC parser
|
||||
* @return
|
||||
* Handle of NEC parser or NULL
|
||||
*/
|
||||
ir_parser_t *ir_parser_rmt_new_nec(const ir_parser_config_t *config);
|
||||
|
||||
/**
|
||||
* @brief Creat a RC5 protocol parser
|
||||
*
|
||||
* @param config: configuration of RC5 parser
|
||||
* @return
|
||||
* Handle of RC5 parser or NULL
|
||||
*/
|
||||
ir_parser_t *ir_parser_rmt_new_rc5(const ir_parser_config_t *config);
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@@ -0,0 +1,206 @@
|
||||
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed 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.#include <stdlib.h>
|
||||
#include <sys/cdefs.h>
|
||||
#include "esp_log.h"
|
||||
#include "ir_tools.h"
|
||||
#include "ir_timings.h"
|
||||
#include "driver/rmt.h"
|
||||
|
||||
static const char *TAG = "nec_builder";
|
||||
#define NEC_CHECK(a, str, goto_tag, ret_value, ...) \
|
||||
do \
|
||||
{ \
|
||||
if (!(a)) \
|
||||
{ \
|
||||
ESP_LOGE(TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
|
||||
ret = ret_value; \
|
||||
goto goto_tag; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
typedef struct {
|
||||
ir_builder_t parent;
|
||||
uint32_t buffer_size;
|
||||
uint32_t cursor;
|
||||
uint32_t flags;
|
||||
uint32_t leading_code_high_ticks;
|
||||
uint32_t leading_code_low_ticks;
|
||||
uint32_t repeat_code_high_ticks;
|
||||
uint32_t repeat_code_low_ticks;
|
||||
uint32_t payload_logic0_high_ticks;
|
||||
uint32_t payload_logic0_low_ticks;
|
||||
uint32_t payload_logic1_high_ticks;
|
||||
uint32_t payload_logic1_low_ticks;
|
||||
uint32_t ending_code_high_ticks;
|
||||
uint32_t ending_code_low_ticks;
|
||||
bool inverse;
|
||||
rmt_item32_t buffer[0];
|
||||
} nec_builder_t;
|
||||
|
||||
static esp_err_t nec_builder_make_head(ir_builder_t *builder)
|
||||
{
|
||||
nec_builder_t *nec_builder = __containerof(builder, nec_builder_t, parent);
|
||||
nec_builder->cursor = 0;
|
||||
nec_builder->buffer[nec_builder->cursor].level0 = !nec_builder->inverse;
|
||||
nec_builder->buffer[nec_builder->cursor].duration0 = nec_builder->leading_code_high_ticks;
|
||||
nec_builder->buffer[nec_builder->cursor].level1 = nec_builder->inverse;
|
||||
nec_builder->buffer[nec_builder->cursor].duration1 = nec_builder->leading_code_low_ticks;
|
||||
nec_builder->cursor += 1;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t nec_builder_make_logic0(ir_builder_t *builder)
|
||||
{
|
||||
nec_builder_t *nec_builder = __containerof(builder, nec_builder_t, parent);
|
||||
nec_builder->buffer[nec_builder->cursor].level0 = !nec_builder->inverse;
|
||||
nec_builder->buffer[nec_builder->cursor].duration0 = nec_builder->payload_logic0_high_ticks;
|
||||
nec_builder->buffer[nec_builder->cursor].level1 = nec_builder->inverse;
|
||||
nec_builder->buffer[nec_builder->cursor].duration1 = nec_builder->payload_logic0_low_ticks;
|
||||
nec_builder->cursor += 1;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t nec_builder_make_logic1(ir_builder_t *builder)
|
||||
{
|
||||
nec_builder_t *nec_builder = __containerof(builder, nec_builder_t, parent);
|
||||
nec_builder->buffer[nec_builder->cursor].level0 = !nec_builder->inverse;
|
||||
nec_builder->buffer[nec_builder->cursor].duration0 = nec_builder->payload_logic1_high_ticks;
|
||||
nec_builder->buffer[nec_builder->cursor].level1 = nec_builder->inverse;
|
||||
nec_builder->buffer[nec_builder->cursor].duration1 = nec_builder->payload_logic1_low_ticks;
|
||||
nec_builder->cursor += 1;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t nec_builder_make_end(ir_builder_t *builder)
|
||||
{
|
||||
nec_builder_t *nec_builder = __containerof(builder, nec_builder_t, parent);
|
||||
nec_builder->buffer[nec_builder->cursor].level0 = !nec_builder->inverse;
|
||||
nec_builder->buffer[nec_builder->cursor].duration0 = nec_builder->ending_code_high_ticks;
|
||||
nec_builder->buffer[nec_builder->cursor].level1 = nec_builder->inverse;
|
||||
nec_builder->buffer[nec_builder->cursor].duration1 = nec_builder->ending_code_low_ticks;
|
||||
nec_builder->cursor += 1;
|
||||
nec_builder->buffer[nec_builder->cursor].val = 0;
|
||||
nec_builder->cursor += 1;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t nec_build_frame(ir_builder_t *builder, uint32_t address, uint32_t command)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
nec_builder_t *nec_builder = __containerof(builder, nec_builder_t, parent);
|
||||
if (!nec_builder->flags & IR_TOOLS_FLAGS_PROTO_EXT) {
|
||||
uint8_t low_byte = address & 0xFF;
|
||||
uint8_t high_byte = (address >> 8) & 0xFF;
|
||||
NEC_CHECK(low_byte == ~high_byte, "address not match standard NEC protocol", err, ESP_ERR_INVALID_ARG);
|
||||
low_byte = command & 0xFF;
|
||||
high_byte = (command >> 8) & 0xFF;
|
||||
NEC_CHECK(low_byte == ~high_byte, "command not match standard NEC protocol", err, ESP_ERR_INVALID_ARG);
|
||||
}
|
||||
builder->make_head(builder);
|
||||
// LSB -> MSB
|
||||
for (int i = 0; i < 16; i++) {
|
||||
if (address & (1 << i)) {
|
||||
builder->make_logic1(builder);
|
||||
} else {
|
||||
builder->make_logic0(builder);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 16; i++) {
|
||||
if (command & (1 << i)) {
|
||||
builder->make_logic1(builder);
|
||||
} else {
|
||||
builder->make_logic0(builder);
|
||||
}
|
||||
}
|
||||
builder->make_end(builder);
|
||||
return ESP_OK;
|
||||
err:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t nec_build_repeat_frame(ir_builder_t *builder)
|
||||
{
|
||||
nec_builder_t *nec_builder = __containerof(builder, nec_builder_t, parent);
|
||||
nec_builder->cursor = 0;
|
||||
nec_builder->buffer[nec_builder->cursor].level0 = !nec_builder->inverse;
|
||||
nec_builder->buffer[nec_builder->cursor].duration0 = nec_builder->repeat_code_high_ticks;
|
||||
nec_builder->buffer[nec_builder->cursor].level1 = nec_builder->inverse;
|
||||
nec_builder->buffer[nec_builder->cursor].duration1 = nec_builder->repeat_code_low_ticks;
|
||||
nec_builder->cursor += 1;
|
||||
nec_builder_make_end(builder);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t nec_builder_get_result(ir_builder_t *builder, void *result, uint32_t *length)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
nec_builder_t *nec_builder = __containerof(builder, nec_builder_t, parent);
|
||||
NEC_CHECK(result && length, "result and length can't be null", err, ESP_ERR_INVALID_ARG);
|
||||
*(rmt_item32_t **)result = nec_builder->buffer;
|
||||
*length = nec_builder->cursor;
|
||||
return ESP_OK;
|
||||
err:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t nec_builder_del(ir_builder_t *builder)
|
||||
{
|
||||
nec_builder_t *nec_builder = __containerof(builder, nec_builder_t, parent);
|
||||
free(nec_builder);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
ir_builder_t *ir_builder_rmt_new_nec(const ir_builder_config_t *config)
|
||||
{
|
||||
ir_builder_t *ret = NULL;
|
||||
NEC_CHECK(config, "nec configuration can't be null", err, NULL);
|
||||
NEC_CHECK(config->buffer_size, "buffer size can't be zero", err, NULL);
|
||||
|
||||
uint32_t builder_size = sizeof(nec_builder_t) + config->buffer_size * sizeof(rmt_item32_t);
|
||||
nec_builder_t *nec_builder = calloc(1, builder_size);
|
||||
NEC_CHECK(nec_builder, "request memory for nec_builder failed", err, NULL);
|
||||
|
||||
nec_builder->buffer_size = config->buffer_size;
|
||||
nec_builder->flags = config->flags;
|
||||
if (config->flags & IR_TOOLS_FLAGS_INVERSE) {
|
||||
nec_builder->inverse = true;
|
||||
}
|
||||
|
||||
uint32_t counter_clk_hz = 0;
|
||||
NEC_CHECK(rmt_get_counter_clock((rmt_channel_t)config->dev_hdl, &counter_clk_hz) == ESP_OK,
|
||||
"get rmt counter clock failed", err, NULL);
|
||||
float ratio = (float)counter_clk_hz / 1e6;
|
||||
nec_builder->leading_code_high_ticks = (uint32_t)(ratio * NEC_LEADING_CODE_HIGH_US);
|
||||
nec_builder->leading_code_low_ticks = (uint32_t)(ratio * NEC_LEADING_CODE_LOW_US);
|
||||
nec_builder->repeat_code_high_ticks = (uint32_t)(ratio * NEC_REPEAT_CODE_HIGH_US);
|
||||
nec_builder->repeat_code_low_ticks = (uint32_t)(ratio * NEC_REPEAT_CODE_LOW_US);
|
||||
nec_builder->payload_logic0_high_ticks = (uint32_t)(ratio * NEC_PAYLOAD_ZERO_HIGH_US);
|
||||
nec_builder->payload_logic0_low_ticks = (uint32_t)(ratio * NEC_PAYLOAD_ZERO_LOW_US);
|
||||
nec_builder->payload_logic1_high_ticks = (uint32_t)(ratio * NEC_PAYLOAD_ONE_HIGH_US);
|
||||
nec_builder->payload_logic1_low_ticks = (uint32_t)(ratio * NEC_PAYLOAD_ONE_LOW_US);
|
||||
nec_builder->ending_code_high_ticks = (uint32_t)(ratio * NEC_ENDING_CODE_HIGH_US);
|
||||
nec_builder->ending_code_low_ticks = 0x7FFF;
|
||||
nec_builder->parent.make_head = nec_builder_make_head;
|
||||
nec_builder->parent.make_logic0 = nec_builder_make_logic0;
|
||||
nec_builder->parent.make_logic1 = nec_builder_make_logic1;
|
||||
nec_builder->parent.make_end = nec_builder_make_end;
|
||||
nec_builder->parent.build_frame = nec_build_frame;
|
||||
nec_builder->parent.build_repeat_frame = nec_build_repeat_frame;
|
||||
nec_builder->parent.get_result = nec_builder_get_result;
|
||||
nec_builder->parent.del = nec_builder_del;
|
||||
nec_builder->parent.repeat_period_ms = 110;
|
||||
return &nec_builder->parent;
|
||||
err:
|
||||
return ret;
|
||||
}
|
@@ -0,0 +1,190 @@
|
||||
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed 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.
|
||||
#include <stdlib.h>
|
||||
#include <sys/cdefs.h>
|
||||
#include "esp_log.h"
|
||||
#include "ir_tools.h"
|
||||
#include "ir_timings.h"
|
||||
#include "driver/rmt.h"
|
||||
|
||||
static const char *TAG = "rc5_builder";
|
||||
#define RC5_CHECK(a, str, goto_tag, ret_value, ...) \
|
||||
do \
|
||||
{ \
|
||||
if (!(a)) \
|
||||
{ \
|
||||
ESP_LOGE(TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
|
||||
ret = ret_value; \
|
||||
goto goto_tag; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
typedef struct {
|
||||
ir_builder_t parent;
|
||||
uint32_t buffer_size;
|
||||
uint32_t cursor;
|
||||
uint32_t pulse_duration_ticks;
|
||||
uint32_t flags;
|
||||
bool toggle;
|
||||
bool s2_bit;
|
||||
bool inverse;
|
||||
rmt_item32_t buffer[0];
|
||||
} rc5_builder_t;
|
||||
|
||||
static esp_err_t rc5_builder_make_head(ir_builder_t *builder)
|
||||
{
|
||||
rc5_builder_t *rc5_builder = __containerof(builder, rc5_builder_t, parent);
|
||||
rc5_builder->cursor = 0;
|
||||
rc5_builder->toggle = !rc5_builder->toggle;
|
||||
// S1 default (not inverse) is 0
|
||||
rc5_builder->buffer[rc5_builder->cursor].level0 = rc5_builder->inverse;
|
||||
rc5_builder->buffer[rc5_builder->cursor].duration0 = rc5_builder->pulse_duration_ticks;
|
||||
rc5_builder->buffer[rc5_builder->cursor].level1 = !rc5_builder->inverse;
|
||||
rc5_builder->buffer[rc5_builder->cursor].duration1 = rc5_builder->pulse_duration_ticks;
|
||||
rc5_builder->cursor += 1;
|
||||
// S2 default (not inverse) is depend on whether use extended protocol
|
||||
rc5_builder->buffer[rc5_builder->cursor].level0 = rc5_builder->s2_bit ^ rc5_builder->inverse;
|
||||
rc5_builder->buffer[rc5_builder->cursor].duration0 = rc5_builder->pulse_duration_ticks;
|
||||
rc5_builder->buffer[rc5_builder->cursor].level1 = !(rc5_builder->s2_bit ^ rc5_builder->inverse);
|
||||
rc5_builder->buffer[rc5_builder->cursor].duration1 = rc5_builder->pulse_duration_ticks;
|
||||
rc5_builder->cursor += 1;
|
||||
// T
|
||||
rc5_builder->buffer[rc5_builder->cursor].level0 = rc5_builder->toggle;
|
||||
rc5_builder->buffer[rc5_builder->cursor].duration0 = rc5_builder->pulse_duration_ticks;
|
||||
rc5_builder->buffer[rc5_builder->cursor].level1 = !rc5_builder->toggle;
|
||||
rc5_builder->buffer[rc5_builder->cursor].duration1 = rc5_builder->pulse_duration_ticks;
|
||||
rc5_builder->cursor += 1;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t rc5_builder_make_logic0(ir_builder_t *builder)
|
||||
{
|
||||
rc5_builder_t *rc5_builder = __containerof(builder, rc5_builder_t, parent);
|
||||
rc5_builder->buffer[rc5_builder->cursor].level0 = !rc5_builder->inverse;
|
||||
rc5_builder->buffer[rc5_builder->cursor].duration0 = rc5_builder->pulse_duration_ticks;
|
||||
rc5_builder->buffer[rc5_builder->cursor].level1 = rc5_builder->inverse;
|
||||
rc5_builder->buffer[rc5_builder->cursor].duration1 = rc5_builder->pulse_duration_ticks;
|
||||
rc5_builder->cursor += 1;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t rc5_builder_make_logic1(ir_builder_t *builder)
|
||||
{
|
||||
rc5_builder_t *rc5_builder = __containerof(builder, rc5_builder_t, parent);
|
||||
rc5_builder->buffer[rc5_builder->cursor].level0 = rc5_builder->inverse;
|
||||
rc5_builder->buffer[rc5_builder->cursor].duration0 = rc5_builder->pulse_duration_ticks;
|
||||
rc5_builder->buffer[rc5_builder->cursor].level1 = !rc5_builder->inverse;
|
||||
rc5_builder->buffer[rc5_builder->cursor].duration1 = rc5_builder->pulse_duration_ticks;
|
||||
rc5_builder->cursor += 1;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t rc5_builder_make_end(ir_builder_t *builder)
|
||||
{
|
||||
rc5_builder_t *rc5_builder = __containerof(builder, rc5_builder_t, parent);
|
||||
rc5_builder->buffer[rc5_builder->cursor].val = 0;
|
||||
rc5_builder->cursor += 1;
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t rc5_build_frame(ir_builder_t *builder, uint32_t address, uint32_t command)
|
||||
{
|
||||
rc5_builder_t *rc5_builder = __containerof(builder, rc5_builder_t, parent);
|
||||
if (rc5_builder->flags & IR_TOOLS_FLAGS_PROTO_EXT) {
|
||||
// RC5-extended protocol uses S2 bit as a 7th command bit (MSB of a command)
|
||||
if (command > 63) {
|
||||
rc5_builder->s2_bit = true;
|
||||
} else {
|
||||
rc5_builder->s2_bit = false;
|
||||
}
|
||||
}
|
||||
builder->make_head(builder);
|
||||
// MSB -> LSB
|
||||
for (int i = 4; i >= 0; i--) {
|
||||
if (address & (1 << i)) {
|
||||
builder->make_logic1(builder);
|
||||
} else {
|
||||
builder->make_logic0(builder);
|
||||
}
|
||||
}
|
||||
for (int i = 5; i >= 0; i--) {
|
||||
if (command & (1 << i)) {
|
||||
builder->make_logic1(builder);
|
||||
} else {
|
||||
builder->make_logic0(builder);
|
||||
}
|
||||
}
|
||||
builder->make_end(builder);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t rc5_build_repeat_frame(ir_builder_t *builder)
|
||||
{
|
||||
// repeat frame is just the latest build frame, so do nothing here
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t rc5_builder_get_result(ir_builder_t *builder, void *result, uint32_t *length)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
rc5_builder_t *rc5_builder = __containerof(builder, rc5_builder_t, parent);
|
||||
RC5_CHECK(result && length, "result and length can't be null", err, ESP_ERR_INVALID_ARG);
|
||||
*(rmt_item32_t **)result = rc5_builder->buffer;
|
||||
*length = rc5_builder->cursor;
|
||||
return ESP_OK;
|
||||
err:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t rc5_builder_del(ir_builder_t *builder)
|
||||
{
|
||||
rc5_builder_t *rc5_builder = __containerof(builder, rc5_builder_t, parent);
|
||||
free(rc5_builder);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
ir_builder_t *ir_builder_rmt_new_rc5(const ir_builder_config_t *config)
|
||||
{
|
||||
ir_builder_t *ret = NULL;
|
||||
RC5_CHECK(config, "rc5 configuration can't be null", err, NULL);
|
||||
RC5_CHECK(config->buffer_size, "buffer size can't be zero", err, NULL);
|
||||
|
||||
uint32_t builder_size = sizeof(rc5_builder_t) + config->buffer_size * sizeof(rmt_item32_t);
|
||||
rc5_builder_t *rc5_builder = calloc(1, builder_size);
|
||||
RC5_CHECK(rc5_builder, "request memory for rc5_builder failed", err, NULL);
|
||||
|
||||
rc5_builder->buffer_size = config->buffer_size;
|
||||
rc5_builder->flags = config->flags;
|
||||
if (config->flags & IR_TOOLS_FLAGS_INVERSE) {
|
||||
rc5_builder->inverse = true;
|
||||
}
|
||||
|
||||
uint32_t counter_clk_hz = 0;
|
||||
RC5_CHECK(rmt_get_counter_clock((rmt_channel_t)config->dev_hdl, &counter_clk_hz) == ESP_OK,
|
||||
"get rmt counter clock failed", err, NULL);
|
||||
float ratio = (float)counter_clk_hz / 1e6;
|
||||
rc5_builder->pulse_duration_ticks = (uint32_t)(ratio * RC5_PULSE_DURATION_US);
|
||||
rc5_builder->parent.make_head = rc5_builder_make_head;
|
||||
rc5_builder->parent.make_logic0 = rc5_builder_make_logic0;
|
||||
rc5_builder->parent.make_logic1 = rc5_builder_make_logic1;
|
||||
rc5_builder->parent.make_end = rc5_builder_make_end;
|
||||
rc5_builder->parent.build_frame = rc5_build_frame;
|
||||
rc5_builder->parent.build_repeat_frame = rc5_build_repeat_frame;
|
||||
rc5_builder->parent.get_result = rc5_builder_get_result;
|
||||
rc5_builder->parent.del = rc5_builder_del;
|
||||
rc5_builder->parent.repeat_period_ms = 114;
|
||||
return &rc5_builder->parent;
|
||||
err:
|
||||
return ret;
|
||||
}
|
@@ -0,0 +1,218 @@
|
||||
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed 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.
|
||||
#include <stdlib.h>
|
||||
#include <sys/cdefs.h>
|
||||
#include "esp_log.h"
|
||||
#include "ir_tools.h"
|
||||
#include "ir_timings.h"
|
||||
#include "driver/rmt.h"
|
||||
|
||||
static const char *TAG = "nec_parser";
|
||||
#define NEC_CHECK(a, str, goto_tag, ret_value, ...) \
|
||||
do \
|
||||
{ \
|
||||
if (!(a)) \
|
||||
{ \
|
||||
ESP_LOGE(TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
|
||||
ret = ret_value; \
|
||||
goto goto_tag; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define NEC_DATA_FRAME_RMT_WORDS (34)
|
||||
#define NEC_REPEAT_FRAME_RMT_WORDS (2)
|
||||
|
||||
typedef struct {
|
||||
ir_parser_t parent;
|
||||
uint32_t flags;
|
||||
uint32_t leading_code_high_ticks;
|
||||
uint32_t leading_code_low_ticks;
|
||||
uint32_t repeat_code_high_ticks;
|
||||
uint32_t repeat_code_low_ticks;
|
||||
uint32_t payload_logic0_high_ticks;
|
||||
uint32_t payload_logic0_low_ticks;
|
||||
uint32_t payload_logic1_high_ticks;
|
||||
uint32_t payload_logic1_low_ticks;
|
||||
uint32_t margin_ticks;
|
||||
rmt_item32_t *buffer;
|
||||
uint32_t cursor;
|
||||
uint32_t last_address;
|
||||
uint32_t last_command;
|
||||
bool repeat;
|
||||
bool inverse;
|
||||
} nec_parser_t;
|
||||
|
||||
static inline bool nec_check_in_range(uint32_t raw_ticks, uint32_t target_ticks, uint32_t margin_ticks)
|
||||
{
|
||||
return (raw_ticks < (target_ticks + margin_ticks)) && (raw_ticks > (target_ticks - margin_ticks));
|
||||
}
|
||||
|
||||
static bool nec_parse_head(nec_parser_t *nec_parser)
|
||||
{
|
||||
nec_parser->cursor = 0;
|
||||
rmt_item32_t item = nec_parser->buffer[nec_parser->cursor];
|
||||
bool ret = (item.level0 == nec_parser->inverse) && (item.level1 != nec_parser->inverse) &&
|
||||
nec_check_in_range(item.duration0, nec_parser->leading_code_high_ticks, nec_parser->margin_ticks) &&
|
||||
nec_check_in_range(item.duration1, nec_parser->leading_code_low_ticks, nec_parser->margin_ticks);
|
||||
nec_parser->cursor += 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool nec_parse_logic0(nec_parser_t *nec_parser)
|
||||
{
|
||||
rmt_item32_t item = nec_parser->buffer[nec_parser->cursor];
|
||||
bool ret = (item.level0 == nec_parser->inverse) && (item.level1 != nec_parser->inverse) &&
|
||||
nec_check_in_range(item.duration0, nec_parser->payload_logic0_high_ticks, nec_parser->margin_ticks) &&
|
||||
nec_check_in_range(item.duration1, nec_parser->payload_logic0_low_ticks, nec_parser->margin_ticks);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool nec_parse_logic1(nec_parser_t *nec_parser)
|
||||
{
|
||||
rmt_item32_t item = nec_parser->buffer[nec_parser->cursor];
|
||||
bool ret = (item.level0 == nec_parser->inverse) && (item.level1 != nec_parser->inverse) &&
|
||||
nec_check_in_range(item.duration0, nec_parser->payload_logic1_high_ticks, nec_parser->margin_ticks) &&
|
||||
nec_check_in_range(item.duration1, nec_parser->payload_logic1_low_ticks, nec_parser->margin_ticks);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t nec_parse_logic(ir_parser_t *parser, bool *logic)
|
||||
{
|
||||
esp_err_t ret = ESP_FAIL;
|
||||
bool logic_value = false;
|
||||
nec_parser_t *nec_parser = __containerof(parser, nec_parser_t, parent);
|
||||
if (nec_parse_logic0(nec_parser)) {
|
||||
logic_value = false;
|
||||
ret = ESP_OK;
|
||||
} else if (nec_parse_logic1(nec_parser)) {
|
||||
logic_value = true;
|
||||
ret = ESP_OK;
|
||||
}
|
||||
if (ret == ESP_OK) {
|
||||
*logic = logic_value;
|
||||
}
|
||||
nec_parser->cursor += 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static bool nec_parse_repeat_frame(nec_parser_t *nec_parser)
|
||||
{
|
||||
nec_parser->cursor = 0;
|
||||
rmt_item32_t item = nec_parser->buffer[nec_parser->cursor];
|
||||
bool ret = (item.level0 == nec_parser->inverse) && (item.level1 != nec_parser->inverse) &&
|
||||
nec_check_in_range(item.duration0, nec_parser->repeat_code_high_ticks, nec_parser->margin_ticks) &&
|
||||
nec_check_in_range(item.duration1, nec_parser->repeat_code_low_ticks, nec_parser->margin_ticks);
|
||||
nec_parser->cursor += 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t nec_parser_input(ir_parser_t *parser, void *raw_data, uint32_t length)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
nec_parser_t *nec_parser = __containerof(parser, nec_parser_t, parent);
|
||||
NEC_CHECK(raw_data, "input data can't be null", err, ESP_ERR_INVALID_ARG);
|
||||
nec_parser->buffer = raw_data;
|
||||
// Data Frame costs 34 items and Repeat Frame costs 2 items
|
||||
if (length == NEC_DATA_FRAME_RMT_WORDS) {
|
||||
nec_parser->repeat = false;
|
||||
} else if (length == NEC_REPEAT_FRAME_RMT_WORDS) {
|
||||
nec_parser->repeat = true;
|
||||
} else {
|
||||
ret = ESP_FAIL;
|
||||
}
|
||||
return ret;
|
||||
err:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t nec_parser_get_scan_code(ir_parser_t *parser, uint32_t *address, uint32_t *command, bool *repeat)
|
||||
{
|
||||
esp_err_t ret = ESP_FAIL;
|
||||
uint32_t addr = 0;
|
||||
uint32_t cmd = 0;
|
||||
bool logic_value = false;
|
||||
nec_parser_t *nec_parser = __containerof(parser, nec_parser_t, parent);
|
||||
NEC_CHECK(address && command && repeat, "address, command and repeat can't be null", out, ESP_ERR_INVALID_ARG);
|
||||
if (nec_parser->repeat) {
|
||||
if (nec_parse_repeat_frame(nec_parser)) {
|
||||
*address = nec_parser->last_address;
|
||||
*command = nec_parser->last_command;
|
||||
*repeat = true;
|
||||
ret = ESP_OK;
|
||||
}
|
||||
} else {
|
||||
if (nec_parse_head(nec_parser)) {
|
||||
for (int i = 0; i < 16; i++) {
|
||||
if (nec_parse_logic(parser, &logic_value) == ESP_OK) {
|
||||
addr |= (logic_value << i);
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < 16; i++) {
|
||||
if (nec_parse_logic(parser, &logic_value) == ESP_OK) {
|
||||
cmd |= (logic_value << i);
|
||||
}
|
||||
}
|
||||
*address = addr;
|
||||
*command = cmd;
|
||||
*repeat = false;
|
||||
// keep it as potential repeat code
|
||||
nec_parser->last_address = addr;
|
||||
nec_parser->last_command = cmd;
|
||||
ret = ESP_OK;
|
||||
}
|
||||
}
|
||||
out:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t nec_parser_del(ir_parser_t *parser)
|
||||
{
|
||||
nec_parser_t *nec_parser = __containerof(parser, nec_parser_t, parent);
|
||||
free(nec_parser);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
ir_parser_t *ir_parser_rmt_new_nec(const ir_parser_config_t *config)
|
||||
{
|
||||
ir_parser_t *ret = NULL;
|
||||
NEC_CHECK(config, "nec configuration can't be null", err, NULL);
|
||||
|
||||
nec_parser_t *nec_parser = calloc(1, sizeof(nec_parser_t));
|
||||
NEC_CHECK(nec_parser, "request memory for nec_parser failed", err, NULL);
|
||||
|
||||
nec_parser->flags = config->flags;
|
||||
if (config->flags & IR_TOOLS_FLAGS_INVERSE) {
|
||||
nec_parser->inverse = true;
|
||||
}
|
||||
|
||||
uint32_t counter_clk_hz = 0;
|
||||
NEC_CHECK(rmt_get_counter_clock((rmt_channel_t)config->dev_hdl, &counter_clk_hz) == ESP_OK,
|
||||
"get rmt counter clock failed", err, NULL);
|
||||
float ratio = (float)counter_clk_hz / 1e6;
|
||||
nec_parser->leading_code_high_ticks = (uint32_t)(ratio * NEC_LEADING_CODE_HIGH_US);
|
||||
nec_parser->leading_code_low_ticks = (uint32_t)(ratio * NEC_LEADING_CODE_LOW_US);
|
||||
nec_parser->repeat_code_high_ticks = (uint32_t)(ratio * NEC_REPEAT_CODE_HIGH_US);
|
||||
nec_parser->repeat_code_low_ticks = (uint32_t)(ratio * NEC_REPEAT_CODE_LOW_US);
|
||||
nec_parser->payload_logic0_high_ticks = (uint32_t)(ratio * NEC_PAYLOAD_ZERO_HIGH_US);
|
||||
nec_parser->payload_logic0_low_ticks = (uint32_t)(ratio * NEC_PAYLOAD_ZERO_LOW_US);
|
||||
nec_parser->payload_logic1_high_ticks = (uint32_t)(ratio * NEC_PAYLOAD_ONE_HIGH_US);
|
||||
nec_parser->payload_logic1_low_ticks = (uint32_t)(ratio * NEC_PAYLOAD_ONE_LOW_US);
|
||||
nec_parser->margin_ticks = (uint32_t)(ratio * config->margin_us);
|
||||
nec_parser->parent.input = nec_parser_input;
|
||||
nec_parser->parent.get_scan_code = nec_parser_get_scan_code;
|
||||
nec_parser->parent.del = nec_parser_del;
|
||||
return &nec_parser->parent;
|
||||
err:
|
||||
return ret;
|
||||
}
|
@@ -0,0 +1,165 @@
|
||||
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed 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.
|
||||
#include <stdlib.h>
|
||||
#include <sys/cdefs.h>
|
||||
#include "esp_log.h"
|
||||
#include "ir_tools.h"
|
||||
#include "ir_timings.h"
|
||||
#include "driver/rmt.h"
|
||||
|
||||
static const char *TAG = "rc5_parser";
|
||||
#define RC5_CHECK(a, str, goto_tag, ret_value, ...) \
|
||||
do \
|
||||
{ \
|
||||
if (!(a)) \
|
||||
{ \
|
||||
ESP_LOGE(TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
|
||||
ret = ret_value; \
|
||||
goto goto_tag; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define RC5_MAX_FRAME_RMT_WORDS (14) // S1+S2+T+ADDR(5)+CMD(6)
|
||||
|
||||
typedef struct {
|
||||
ir_parser_t parent;
|
||||
uint32_t flags;
|
||||
uint32_t pulse_duration_ticks;
|
||||
uint32_t margin_ticks;
|
||||
rmt_item32_t *buffer;
|
||||
uint32_t buffer_len;
|
||||
uint32_t last_command;
|
||||
uint32_t last_address;
|
||||
bool last_t_bit;
|
||||
} rc5_parser_t;
|
||||
|
||||
static inline bool rc5_check_in_range(uint32_t raw_ticks, uint32_t target_ticks, uint32_t margin_ticks)
|
||||
{
|
||||
return (raw_ticks < (target_ticks + margin_ticks)) && (raw_ticks > (target_ticks - margin_ticks));
|
||||
}
|
||||
|
||||
static esp_err_t rc5_parser_input(ir_parser_t *parser, void *raw_data, uint32_t length)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
rc5_parser_t *rc5_parser = __containerof(parser, rc5_parser_t, parent);
|
||||
rc5_parser->buffer = raw_data;
|
||||
rc5_parser->buffer_len = length;
|
||||
if (length > RC5_MAX_FRAME_RMT_WORDS) {
|
||||
ret = ESP_FAIL;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
static inline bool rc5_duration_one_unit(rc5_parser_t *rc5_parser, uint32_t duration)
|
||||
{
|
||||
return (duration < (rc5_parser->pulse_duration_ticks + rc5_parser->margin_ticks)) &&
|
||||
(duration > (rc5_parser->pulse_duration_ticks - rc5_parser->margin_ticks));
|
||||
}
|
||||
|
||||
static inline bool rc5_duration_two_unit(rc5_parser_t *rc5_parser, uint32_t duration)
|
||||
{
|
||||
return (duration < (rc5_parser->pulse_duration_ticks * 2 + rc5_parser->margin_ticks)) &&
|
||||
(duration > (rc5_parser->pulse_duration_ticks * 2 - rc5_parser->margin_ticks));
|
||||
}
|
||||
|
||||
static esp_err_t rc5_parser_get_scan_code(ir_parser_t *parser, uint32_t *address, uint32_t *command, bool *repeat)
|
||||
{
|
||||
esp_err_t ret = ESP_FAIL;
|
||||
uint32_t parse_result = 0; // 32 bit is enough to hold the parse result of one RC5 frame
|
||||
uint32_t addr = 0;
|
||||
uint32_t cmd = 0;
|
||||
bool s1 = true;
|
||||
bool s2 = true;
|
||||
bool t = false;
|
||||
bool exchange = false;
|
||||
rc5_parser_t *rc5_parser = __containerof(parser, rc5_parser_t, parent);
|
||||
RC5_CHECK(address && command && repeat, "address, command and repeat can't be null", out, ESP_ERR_INVALID_ARG);
|
||||
for (int i = 0; i < rc5_parser->buffer_len; i++) {
|
||||
if (rc5_duration_one_unit(rc5_parser, rc5_parser->buffer[i].duration0)) {
|
||||
parse_result <<= 1;
|
||||
parse_result |= exchange;
|
||||
if (rc5_duration_two_unit(rc5_parser, rc5_parser->buffer[i].duration1)) {
|
||||
exchange = !exchange;
|
||||
}
|
||||
} else if (rc5_duration_two_unit(rc5_parser, rc5_parser->buffer[i].duration0)) {
|
||||
parse_result <<= 1;
|
||||
parse_result |= rc5_parser->buffer[i].level0;
|
||||
parse_result <<= 1;
|
||||
parse_result |= !rc5_parser->buffer[i].level0;
|
||||
if (rc5_duration_one_unit(rc5_parser, rc5_parser->buffer[i].duration1)) {
|
||||
exchange = !exchange;
|
||||
}
|
||||
} else {
|
||||
goto out;
|
||||
}
|
||||
}
|
||||
if (!(rc5_parser->flags & IR_TOOLS_FLAGS_INVERSE)) {
|
||||
parse_result = ~parse_result;
|
||||
}
|
||||
s1 = ((parse_result & 0x2000) >> 13) & 0x01;
|
||||
s2 = ((parse_result & 0x1000) >> 12) & 0x01;
|
||||
t = ((parse_result & 0x800) >> 11) & 0x01;
|
||||
// Check S1, must be 1
|
||||
if (s1) {
|
||||
if (!(rc5_parser->flags & IR_TOOLS_FLAGS_PROTO_EXT) && !s2) {
|
||||
// Not standard RC5 protocol, but S2 is 0
|
||||
goto out;
|
||||
}
|
||||
addr = (parse_result & 0x7C0) >> 6;
|
||||
cmd = (parse_result & 0x3F);
|
||||
if (!s2) {
|
||||
cmd |= 1 << 6;
|
||||
}
|
||||
*repeat = (t == rc5_parser->last_t_bit && addr == rc5_parser->last_address && cmd == rc5_parser->last_command);
|
||||
*address = addr;
|
||||
*command = cmd;
|
||||
rc5_parser->last_address = addr;
|
||||
rc5_parser->last_command = cmd;
|
||||
rc5_parser->last_t_bit = t;
|
||||
ret = ESP_OK;
|
||||
}
|
||||
out:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t rc5_parser_del(ir_parser_t *parser)
|
||||
{
|
||||
rc5_parser_t *rc5_parser = __containerof(parser, rc5_parser_t, parent);
|
||||
free(rc5_parser);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
ir_parser_t *ir_parser_rmt_new_rc5(const ir_parser_config_t *config)
|
||||
{
|
||||
ir_parser_t *ret = NULL;
|
||||
RC5_CHECK(config, "rc5 configuration can't be null", err, NULL);
|
||||
|
||||
rc5_parser_t *rc5_parser = calloc(1, sizeof(rc5_parser_t));
|
||||
RC5_CHECK(rc5_parser, "request memory for rc5_parser failed", err, NULL);
|
||||
|
||||
rc5_parser->flags = config->flags;
|
||||
|
||||
uint32_t counter_clk_hz = 0;
|
||||
RC5_CHECK(rmt_get_counter_clock((rmt_channel_t)config->dev_hdl, &counter_clk_hz) == ESP_OK,
|
||||
"get rmt counter clock failed", err, NULL);
|
||||
float ratio = (float)counter_clk_hz / 1e6;
|
||||
rc5_parser->pulse_duration_ticks = (uint32_t)(ratio * RC5_PULSE_DURATION_US);
|
||||
rc5_parser->margin_ticks = (uint32_t)(ratio * config->margin_us);
|
||||
rc5_parser->parent.input = rc5_parser_input;
|
||||
rc5_parser->parent.get_scan_code = rc5_parser_get_scan_code;
|
||||
rc5_parser->parent.del = rc5_parser_del;
|
||||
return &rc5_parser->parent;
|
||||
err:
|
||||
return ret;
|
||||
}
|
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "ir_protocols_main.c"
|
||||
INCLUDE_DIRS ".")
|
33
examples/peripherals/rmt/ir_protocols/main/Kconfig.projbuild
Normal file
33
examples/peripherals/rmt/ir_protocols/main/Kconfig.projbuild
Normal file
@@ -0,0 +1,33 @@
|
||||
menu "Example Configuration"
|
||||
choice EXAMPLE_IR_PROTOCOL
|
||||
prompt "Infrared Protocol"
|
||||
default EXAMPLE_IR_PROTOCOL_NEC
|
||||
help
|
||||
Choose the IR protocol used in the example.
|
||||
|
||||
config EXAMPLE_IR_PROTOCOL_NEC
|
||||
bool "NEC"
|
||||
help
|
||||
NEC is a kind of Pulse Distance Protocol.
|
||||
It uses ASK modulation and pulse distance encoding with a carrier frequency of 38 kHz.
|
||||
|
||||
config EXAMPLE_IR_PROTOCOL_RC5
|
||||
bool "RC5"
|
||||
help
|
||||
The RC5 protocol was introduced by Philips.
|
||||
It uses ASK modulation and Manchester encoding with carrier frequency fixed at 36 kHz.
|
||||
|
||||
endchoice
|
||||
|
||||
config EXAMPLE_RMT_TX_GPIO
|
||||
int "RMT TX GPIO"
|
||||
default 18
|
||||
help
|
||||
Set the GPIO number used for transmitting the RMT signal.
|
||||
|
||||
config EXAMPLE_RMT_RX_GPIO
|
||||
int "RMT RX GPIO"
|
||||
default 19
|
||||
help
|
||||
Set the GPIO number used for receiving the RMT signal.
|
||||
endmenu
|
119
examples/peripherals/rmt/ir_protocols/main/ir_protocols_main.c
Normal file
119
examples/peripherals/rmt/ir_protocols/main/ir_protocols_main.c
Normal file
@@ -0,0 +1,119 @@
|
||||
/* IR protocols example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "sdkconfig.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/rmt.h"
|
||||
#include "ir_tools.h"
|
||||
|
||||
static const char *TAG = "example";
|
||||
|
||||
static rmt_channel_t example_tx_channel = RMT_CHANNEL_0;
|
||||
static rmt_channel_t example_rx_channel = RMT_CHANNEL_1;
|
||||
|
||||
/**
|
||||
* @brief RMT Receive Task
|
||||
*
|
||||
*/
|
||||
static void example_ir_rx_task(void *arg)
|
||||
{
|
||||
uint32_t addr = 0;
|
||||
uint32_t cmd = 0;
|
||||
uint32_t length = 0;
|
||||
bool repeat = false;
|
||||
RingbufHandle_t rb = NULL;
|
||||
rmt_item32_t *items = NULL;
|
||||
|
||||
rmt_config_t rmt_rx_config = RMT_DEFAULT_CONFIG_RX(CONFIG_EXAMPLE_RMT_RX_GPIO, example_rx_channel);
|
||||
rmt_config(&rmt_rx_config);
|
||||
rmt_driver_install(example_rx_channel, 1000, 0);
|
||||
ir_parser_config_t ir_parser_config = IR_PARSER_DEFAULT_CONFIG((ir_dev_t)example_rx_channel);
|
||||
ir_parser_config.flags |= IR_TOOLS_FLAGS_PROTO_EXT; // Using extended IR protocols (both NEC and RC5 have extended version)
|
||||
ir_parser_t *ir_parser = NULL;
|
||||
#if CONFIG_EXAMPLE_IR_PROTOCOL_NEC
|
||||
ir_parser = ir_parser_rmt_new_nec(&ir_parser_config);
|
||||
#elif CONFIG_EXAMPLE_IR_PROTOCOL_RC5
|
||||
ir_parser = ir_parser_rmt_new_rc5(&ir_parser_config);
|
||||
#endif
|
||||
|
||||
//get RMT RX ringbuffer
|
||||
rmt_get_ringbuf_handle(example_rx_channel, &rb);
|
||||
// Start receive
|
||||
rmt_rx_start(example_rx_channel, true);
|
||||
while (rb) {
|
||||
items = (rmt_item32_t *) xRingbufferReceive(rb, &length, 1000);
|
||||
if (items) {
|
||||
length /= 4; // one RMT = 4 Bytes
|
||||
if (ir_parser->input(ir_parser, items, length) == ESP_OK) {
|
||||
if (ir_parser->get_scan_code(ir_parser, &addr, &cmd, &repeat) == ESP_OK) {
|
||||
ESP_LOGI(TAG, "Scan Code %s --- addr: 0x%04x cmd: 0x%04x", repeat ? "(repeat)" : "", addr, cmd);
|
||||
}
|
||||
}
|
||||
//after parsing the data, return spaces to ringbuffer.
|
||||
vRingbufferReturnItem(rb, (void *) items);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
ir_parser->del(ir_parser);
|
||||
rmt_driver_uninstall(example_rx_channel);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief RMT Transmit Task
|
||||
*
|
||||
*/
|
||||
static void example_ir_tx_task(void *arg)
|
||||
{
|
||||
uint32_t addr = 0x10;
|
||||
uint32_t cmd = 0x20;
|
||||
rmt_item32_t *items = NULL;
|
||||
uint32_t length = 0;
|
||||
ir_builder_t *ir_builder = NULL;
|
||||
|
||||
rmt_config_t rmt_tx_config = RMT_DEFAULT_CONFIG_TX(CONFIG_EXAMPLE_RMT_TX_GPIO, example_tx_channel);
|
||||
rmt_tx_config.tx_config.carrier_en = true;
|
||||
rmt_config(&rmt_tx_config);
|
||||
rmt_driver_install(example_tx_channel, 0, 0);
|
||||
ir_builder_config_t ir_builder_config = IR_BUILDER_DEFAULT_CONFIG((ir_dev_t)example_tx_channel);
|
||||
ir_builder_config.flags |= IR_TOOLS_FLAGS_PROTO_EXT; // Using extended IR protocols (both NEC and RC5 have extended version)
|
||||
#if CONFIG_EXAMPLE_IR_PROTOCOL_NEC
|
||||
ir_builder = ir_builder_rmt_new_nec(&ir_builder_config);
|
||||
#elif CONFIG_EXAMPLE_IR_PROTOCOL_RC5
|
||||
ir_builder = ir_builder_rmt_new_rc5(&ir_builder_config);
|
||||
#endif
|
||||
while (1) {
|
||||
vTaskDelay(pdMS_TO_TICKS(2000));
|
||||
ESP_LOGI(TAG, "Send command 0x%x to address 0x%x", cmd, addr);
|
||||
// Send new key code
|
||||
ESP_ERROR_CHECK(ir_builder->build_frame(ir_builder, addr, cmd));
|
||||
ESP_ERROR_CHECK(ir_builder->get_result(ir_builder, &items, &length));
|
||||
//To send data according to the waveform items.
|
||||
rmt_write_items(example_tx_channel, items, length, true);
|
||||
// Send repeat code
|
||||
vTaskDelay(pdMS_TO_TICKS(ir_builder->repeat_period_ms));
|
||||
ESP_ERROR_CHECK(ir_builder->build_repeat_frame(ir_builder));
|
||||
ESP_ERROR_CHECK(ir_builder->get_result(ir_builder, &items, &length));
|
||||
rmt_write_items(example_tx_channel, items, length, true);
|
||||
cmd++;
|
||||
}
|
||||
ir_builder->del(ir_builder);
|
||||
rmt_driver_uninstall(example_tx_channel);
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
xTaskCreate(example_ir_rx_task, "ir_rx_task", 2048, NULL, 10, NULL);
|
||||
xTaskCreate(example_ir_tx_task, "ir_tx_task", 2048, NULL, 10, NULL);
|
||||
}
|
@@ -3,4 +3,4 @@
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(rmt_tx)
|
||||
project(led_strip)
|
@@ -3,7 +3,6 @@
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := rmt_tx
|
||||
PROJECT_NAME := led_strip
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
52
examples/peripherals/rmt/led_strip/README.md
Normal file
52
examples/peripherals/rmt/led_strip/README.md
Normal file
@@ -0,0 +1,52 @@
|
||||
# RMT Transmit Example -- LED Strip
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
Although RMT peripheral is mainly designed for infrared remote applications, it can also support other generic protocols thanks to its flexible data format. [WS2812](http://www.world-semi.com/Certifications/WS2812B.html) is a digital RGB LED which integrates a driver circuit and a single control wire. The protocol data format defined in WS2812 is compatible to that in RMT peripheral. This example will illustrate how to drive an WS2812 LED strip based on the RMT driver.
|
||||
|
||||
## How to Use Example
|
||||
|
||||
### Hardware Required
|
||||
|
||||
* A development board with ESP32 SoC (e.g., ESP32-DevKitC, ESP-WROVER-KIT, etc.)
|
||||
* A USB cable for Power supply and programming
|
||||
* A WS2812 LED strip
|
||||
|
||||
Connection :
|
||||
|
||||
```
|
||||
--- 5V
|
||||
|
|
||||
+
|
||||
GPIO18 +-----------------+---|>| (WS2812)
|
||||
DI +
|
||||
|
|
||||
--- GND
|
||||
```
|
||||
|
||||
### Configure the Project
|
||||
|
||||
Open the project configuration menu (`idf.py menuconfig`).
|
||||
|
||||
In the `Example Connection Configuration` menu:
|
||||
|
||||
* Set the GPIO number used for transmitting the IR signal under `RMT TX GPIO` optin.
|
||||
* Set the number of LEDs in a strip under `Number of LEDS in a strip` option.
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Run `idf.py -p PORT flash monitor` to build, flash and monitor the project.
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
Connect the `DI` signal of WS2812 LED strip to the GPIO you set in menuconfig.
|
||||
|
||||
Run the example, you will see a rainbow chasing demonstration effect. To change the chasing speed, you can update the `EXAMPLE_CHASE_SPEED_MS` value in `led_strip_main.c` file.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
For any technical queries, please open an [issue] (https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.
|
@@ -0,0 +1,8 @@
|
||||
set(component_srcs "src/led_strip_rmt_ws2812.c")
|
||||
|
||||
idf_component_register(SRCS "${component_srcs}"
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_INCLUDE_DIRS ""
|
||||
PRIV_REQUIRES "driver"
|
||||
REQUIRES "")
|
||||
|
@@ -0,0 +1,3 @@
|
||||
COMPONENT_ADD_INCLUDEDIRS := include
|
||||
|
||||
COMPONENT_SRCDIRS := src
|
@@ -0,0 +1,126 @@
|
||||
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed 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.
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "esp_err.h"
|
||||
|
||||
/**
|
||||
* @brief LED Strip Type
|
||||
*
|
||||
*/
|
||||
typedef struct led_strip_s led_strip_t;
|
||||
|
||||
/**
|
||||
* @brief LED Strip Device Type
|
||||
*
|
||||
*/
|
||||
typedef void *led_strip_dev_t;
|
||||
|
||||
/**
|
||||
* @brief Declare of LED Strip Type
|
||||
*
|
||||
*/
|
||||
struct led_strip_s {
|
||||
/**
|
||||
* @brief Set RGB for a specific pixel
|
||||
*
|
||||
* @param strip: LED strip
|
||||
* @param index: index of pixel to set
|
||||
* @param red: red part of color
|
||||
* @param green: green part of color
|
||||
* @param blue: blue part of color
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Set RGB for a specific pixel successfully
|
||||
* - ESP_ERR_INVALID_ARG: Set RGB for a specific pixel failed because of invalid parameters
|
||||
* - ESP_FAIL: Set RGB for a specific pixel failed because other error occurred
|
||||
*/
|
||||
esp_err_t (*set_pixel)(led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue);
|
||||
|
||||
/**
|
||||
* @brief Refresh memory colors to LEDs
|
||||
*
|
||||
* @param strip: LED strip
|
||||
* @param timeout_ms: timeout value for refreshing task
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Refresh successfully
|
||||
* - ESP_ERR_TIMEOUT: Refresh failed because of timeout
|
||||
* - ESP_FAIL: Refresh failed because some other error occurred
|
||||
*
|
||||
* @note:
|
||||
* After updating the LED colors in the memory, a following invocation of this API is needed to flush colors to strip.
|
||||
*/
|
||||
esp_err_t (*refresh)(led_strip_t *strip, uint32_t timeout_ms);
|
||||
|
||||
/**
|
||||
* @brief Clear LED strip (turn off all LEDs)
|
||||
*
|
||||
* @param strip: LED strip
|
||||
* @param timeout_ms: timeout value for clearing task
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Clear LEDs successfully
|
||||
* - ESP_ERR_TIMEOUT: Clear LEDs failed because of timeout
|
||||
* - ESP_FAIL: Clear LEDs failed because some other error occurred
|
||||
*/
|
||||
esp_err_t (*clear)(led_strip_t *strip, uint32_t timeout_ms);
|
||||
|
||||
/**
|
||||
* @brief Free LED strip resources
|
||||
*
|
||||
* @param strip: LED strip
|
||||
*
|
||||
* @return
|
||||
* - ESP_OK: Free resources successfully
|
||||
* - ESP_FAIL: Free resources failed because error occurred
|
||||
*/
|
||||
esp_err_t (*del)(led_strip_t *strip);
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief LED Strip Configuration Type
|
||||
*
|
||||
*/
|
||||
typedef struct {
|
||||
uint32_t max_leds; /*!< Maximum LEDs in a single strip */
|
||||
led_strip_dev_t dev; /*!< LED strip device (e.g. RMT channel, PWM channel, etc) */
|
||||
} led_strip_config_t;
|
||||
|
||||
/**
|
||||
* @brief Default configuration for LED strip
|
||||
*
|
||||
*/
|
||||
#define LED_STRIP_DEFAULT_CONFIG(number, dev_hdl) \
|
||||
{ \
|
||||
.max_leds = number, \
|
||||
.dev = dev_hdl, \
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Install a new ws2812 driver (based on RMT peripheral)
|
||||
*
|
||||
* @param config: LED strip configuration
|
||||
* @return
|
||||
* LED strip instance or NULL
|
||||
*/
|
||||
led_strip_t *led_strip_new_rmt_ws2812(const led_strip_config_t *config);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
@@ -0,0 +1,171 @@
|
||||
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed 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.
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/cdefs.h>
|
||||
#include "esp_log.h"
|
||||
#include "esp_attr.h"
|
||||
#include "led_strip.h"
|
||||
#include "driver/rmt.h"
|
||||
|
||||
static const char *TAG = "ws2812";
|
||||
#define STRIP_CHECK(a, str, goto_tag, ret_value, ...) \
|
||||
do \
|
||||
{ \
|
||||
if (!(a)) \
|
||||
{ \
|
||||
ESP_LOGE(TAG, "%s(%d): " str, __FUNCTION__, __LINE__, ##__VA_ARGS__); \
|
||||
ret = ret_value; \
|
||||
goto goto_tag; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define WS2812_T0H_NS (350)
|
||||
#define WS2812_T0L_NS (1000)
|
||||
#define WS2812_T1H_NS (1000)
|
||||
#define WS2812_T1L_NS (350)
|
||||
#define WS2812_RESET_US (280)
|
||||
|
||||
static uint32_t ws2812_t0h_ticks = 0;
|
||||
static uint32_t ws2812_t1h_ticks = 0;
|
||||
static uint32_t ws2812_t0l_ticks = 0;
|
||||
static uint32_t ws2812_t1l_ticks = 0;
|
||||
|
||||
typedef struct {
|
||||
led_strip_t parent;
|
||||
rmt_channel_t rmt_channel;
|
||||
uint32_t strip_len;
|
||||
uint8_t buffer[0];
|
||||
} ws2812_t;
|
||||
|
||||
/**
|
||||
* @brief Conver RGB data to RMT format.
|
||||
*
|
||||
* @note For WS2812, R,G,B each contains 256 different choices (i.e. uint8_t)
|
||||
*
|
||||
* @param[in] src: source data, to converted to RMT format
|
||||
* @param[in] dest: place where to store the convert result
|
||||
* @param[in] src_size: size of source data
|
||||
* @param[in] wanted_num: number of RMT items that want to get
|
||||
* @param[out] translated_size: number of source data that got converted
|
||||
* @param[out] item_num: number of RMT items which are converted from source data
|
||||
*/
|
||||
static void IRAM_ATTR ws2812_rmt_adapter(const void *src, rmt_item32_t *dest, size_t src_size,
|
||||
size_t wanted_num, size_t *translated_size, size_t *item_num)
|
||||
{
|
||||
if (src == NULL || dest == NULL) {
|
||||
*translated_size = 0;
|
||||
*item_num = 0;
|
||||
return;
|
||||
}
|
||||
const rmt_item32_t bit0 = {{{ ws2812_t0h_ticks, 1, ws2812_t0l_ticks, 0 }}}; //Logical 0
|
||||
const rmt_item32_t bit1 = {{{ ws2812_t1h_ticks, 1, ws2812_t1l_ticks, 0 }}}; //Logical 1
|
||||
size_t size = 0;
|
||||
size_t num = 0;
|
||||
uint8_t *psrc = (uint8_t *)src;
|
||||
rmt_item32_t *pdest = dest;
|
||||
while (size < src_size && num < wanted_num) {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
// MSB first
|
||||
if (*psrc & (1 << (7 - i))) {
|
||||
pdest->val = bit1.val;
|
||||
} else {
|
||||
pdest->val = bit0.val;
|
||||
}
|
||||
num++;
|
||||
pdest++;
|
||||
}
|
||||
size++;
|
||||
psrc++;
|
||||
}
|
||||
*translated_size = size;
|
||||
*item_num = num;
|
||||
}
|
||||
|
||||
static esp_err_t ws2812_set_pixel(led_strip_t *strip, uint32_t index, uint32_t red, uint32_t green, uint32_t blue)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
ws2812_t *ws2812 = __containerof(strip, ws2812_t, parent);
|
||||
STRIP_CHECK(index < ws2812->strip_len, "index out of the maximum number of leds", err, ESP_ERR_INVALID_ARG);
|
||||
uint32_t start = index * 3;
|
||||
// In thr order of GRB
|
||||
ws2812->buffer[start + 0] = green & 0xFF;
|
||||
ws2812->buffer[start + 1] = red & 0xFF;
|
||||
ws2812->buffer[start + 2] = blue & 0xFF;
|
||||
return ESP_OK;
|
||||
err:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t ws2812_refresh(led_strip_t *strip, uint32_t timeout_ms)
|
||||
{
|
||||
esp_err_t ret = ESP_OK;
|
||||
ws2812_t *ws2812 = __containerof(strip, ws2812_t, parent);
|
||||
STRIP_CHECK(rmt_write_sample(ws2812->rmt_channel, ws2812->buffer, ws2812->strip_len * 3, true) == ESP_OK,
|
||||
"transmit RMT samples failed", err, ESP_FAIL);
|
||||
return rmt_wait_tx_done(ws2812->rmt_channel, pdMS_TO_TICKS(timeout_ms));
|
||||
err:
|
||||
return ret;
|
||||
}
|
||||
|
||||
static esp_err_t ws2812_clear(led_strip_t *strip, uint32_t timeout_ms)
|
||||
{
|
||||
ws2812_t *ws2812 = __containerof(strip, ws2812_t, parent);
|
||||
// Write zero to turn off all leds
|
||||
memset(ws2812->buffer, 0, ws2812->strip_len * 3);
|
||||
return ws2812_refresh(strip, timeout_ms);
|
||||
}
|
||||
|
||||
static esp_err_t ws2812_del(led_strip_t *strip)
|
||||
{
|
||||
ws2812_t *ws2812 = __containerof(strip, ws2812_t, parent);
|
||||
free(ws2812);
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
led_strip_t *led_strip_new_rmt_ws2812(const led_strip_config_t *config)
|
||||
{
|
||||
led_strip_t *ret = NULL;
|
||||
STRIP_CHECK(config, "configuration can't be null", err, NULL);
|
||||
|
||||
// 24 bits per led
|
||||
uint32_t ws2812_size = sizeof(ws2812_t) + config->max_leds * 3;
|
||||
ws2812_t *ws2812 = calloc(1, ws2812_size);
|
||||
STRIP_CHECK(ws2812, "request memory for ws2812 failed", err, NULL);
|
||||
|
||||
uint32_t counter_clk_hz = 0;
|
||||
STRIP_CHECK(rmt_get_counter_clock((rmt_channel_t)config->dev, &counter_clk_hz) == ESP_OK,
|
||||
"get rmt counter clock failed", err, NULL);
|
||||
// ns -> ticks
|
||||
float ratio = (float)counter_clk_hz / 1e9;
|
||||
ws2812_t0h_ticks = (uint32_t)(ratio * WS2812_T0H_NS);
|
||||
ws2812_t0l_ticks = (uint32_t)(ratio * WS2812_T0L_NS);
|
||||
ws2812_t1h_ticks = (uint32_t)(ratio * WS2812_T1H_NS);
|
||||
ws2812_t1l_ticks = (uint32_t)(ratio * WS2812_T1L_NS);
|
||||
|
||||
// set ws2812 to rmt adapter
|
||||
rmt_translator_init((rmt_channel_t)config->dev, ws2812_rmt_adapter);
|
||||
|
||||
ws2812->rmt_channel = (rmt_channel_t)config->dev;
|
||||
ws2812->strip_len = config->max_leds;
|
||||
|
||||
ws2812->parent.set_pixel = ws2812_set_pixel;
|
||||
ws2812->parent.refresh = ws2812_refresh;
|
||||
ws2812->parent.clear = ws2812_clear;
|
||||
ws2812->parent.del = ws2812_del;
|
||||
|
||||
return &ws2812->parent;
|
||||
err:
|
||||
return ret;
|
||||
}
|
2
examples/peripherals/rmt/led_strip/main/CMakeLists.txt
Normal file
2
examples/peripherals/rmt/led_strip/main/CMakeLists.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "led_strip_main.c"
|
||||
INCLUDE_DIRS ".")
|
13
examples/peripherals/rmt/led_strip/main/Kconfig.projbuild
Normal file
13
examples/peripherals/rmt/led_strip/main/Kconfig.projbuild
Normal file
@@ -0,0 +1,13 @@
|
||||
menu "Example Configuration"
|
||||
config EXAMPLE_RMT_TX_GPIO
|
||||
int "RMT TX GPIO"
|
||||
default 18
|
||||
help
|
||||
Set the GPIO number used for transmitting the RMT signal.
|
||||
|
||||
config EXAMPLE_STRIP_LED_NUMBER
|
||||
int "Number of LEDS in a strip"
|
||||
default 24
|
||||
help
|
||||
A single RGB strip contains several LEDs.
|
||||
endmenu
|
116
examples/peripherals/rmt/led_strip/main/led_strip_main.c
Normal file
116
examples/peripherals/rmt/led_strip/main/led_strip_main.c
Normal file
@@ -0,0 +1,116 @@
|
||||
/* RMT example -- RGB LED Strip
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include "sdkconfig.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/rmt.h"
|
||||
#include "led_strip.h"
|
||||
|
||||
static const char *TAG = "example";
|
||||
|
||||
#define RMT_TX_CHANNEL RMT_CHANNEL_0
|
||||
|
||||
#define EXAMPLE_CHASE_SPEED_MS (10)
|
||||
|
||||
/**
|
||||
* @brief Simple helper function, converting HSV color space to RGB color space
|
||||
*
|
||||
* Wiki: https://en.wikipedia.org/wiki/HSL_and_HSV
|
||||
*
|
||||
*/
|
||||
void led_strip_hsv2rgb(uint32_t h, uint32_t s, uint32_t v, uint32_t *r, uint32_t *g, uint32_t *b)
|
||||
{
|
||||
h %= 360; // h -> [0,360]
|
||||
uint32_t rgb_max = v * 2.55f;
|
||||
uint32_t rgb_min = rgb_max * (100 - s) / 100.0f;
|
||||
|
||||
uint32_t i = h / 60;
|
||||
uint32_t diff = h % 60;
|
||||
|
||||
// RGB adjustment amount by hue
|
||||
uint32_t rgb_adj = (rgb_max - rgb_min) * diff / 60;
|
||||
|
||||
switch (i) {
|
||||
case 0:
|
||||
*r = rgb_max;
|
||||
*g = rgb_min + rgb_adj;
|
||||
*b = rgb_min;
|
||||
break;
|
||||
case 1:
|
||||
*r = rgb_max - rgb_adj;
|
||||
*g = rgb_max;
|
||||
*b = rgb_min;
|
||||
break;
|
||||
case 2:
|
||||
*r = rgb_min;
|
||||
*g = rgb_max;
|
||||
*b = rgb_min + rgb_adj;
|
||||
break;
|
||||
case 3:
|
||||
*r = rgb_min;
|
||||
*g = rgb_max - rgb_adj;
|
||||
*b = rgb_max;
|
||||
break;
|
||||
case 4:
|
||||
*r = rgb_min + rgb_adj;
|
||||
*g = rgb_min;
|
||||
*b = rgb_max;
|
||||
break;
|
||||
default:
|
||||
*r = rgb_max;
|
||||
*g = rgb_min;
|
||||
*b = rgb_max - rgb_adj;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
uint32_t red = 0;
|
||||
uint32_t green = 0;
|
||||
uint32_t blue = 0;
|
||||
uint16_t hue = 0;
|
||||
uint16_t start_rgb = 0;
|
||||
|
||||
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(CONFIG_EXAMPLE_RMT_TX_GPIO, RMT_TX_CHANNEL);
|
||||
// set counter clock to 40MHz
|
||||
config.clk_div = 2;
|
||||
|
||||
ESP_ERROR_CHECK(rmt_config(&config));
|
||||
ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));
|
||||
|
||||
// install ws2812 driver
|
||||
led_strip_config_t strip_config = LED_STRIP_DEFAULT_CONFIG(CONFIG_EXAMPLE_STRIP_LED_NUMBER, (led_strip_dev_t)config.channel);
|
||||
led_strip_t *strip = led_strip_new_rmt_ws2812(&strip_config);
|
||||
if (!strip) {
|
||||
ESP_LOGE(TAG, "install WS2812 driver failed");
|
||||
}
|
||||
// Clear LED strip (turn off all LEDs)
|
||||
ESP_ERROR_CHECK(strip->clear(strip, 100));
|
||||
// Show simple rainbow chasing pattern
|
||||
ESP_LOGI(TAG, "LED Rainbow Chase Start");
|
||||
while (true) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
for (int j = i; j < CONFIG_EXAMPLE_STRIP_LED_NUMBER; j += 3) {
|
||||
// Build RGB values
|
||||
hue = j * 360 / CONFIG_EXAMPLE_STRIP_LED_NUMBER + start_rgb;
|
||||
led_strip_hsv2rgb(hue, 100, 100, &red, &green, &blue);
|
||||
// Write RGB values to strip driver
|
||||
ESP_ERROR_CHECK(strip->set_pixel(strip, j, red, green, blue));
|
||||
}
|
||||
// Flush RGB values to LEDs
|
||||
ESP_ERROR_CHECK(strip->refresh(strip, 100));
|
||||
vTaskDelay(pdMS_TO_TICKS(EXAMPLE_CHASE_SPEED_MS));
|
||||
strip->clear(strip, 50);
|
||||
vTaskDelay(pdMS_TO_TICKS(EXAMPLE_CHASE_SPEED_MS));
|
||||
}
|
||||
start_rgb += 60;
|
||||
}
|
||||
}
|
6
examples/peripherals/rmt/morse_code/CMakeLists.txt
Normal file
6
examples/peripherals/rmt/morse_code/CMakeLists.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
# The following lines of boilerplate have to be in your project's CMakeLists
|
||||
# in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(morse_code)
|
9
examples/peripherals/rmt/morse_code/Makefile
Normal file
9
examples/peripherals/rmt/morse_code/Makefile
Normal file
@@ -0,0 +1,9 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := morse_code
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
@@ -1,8 +1,8 @@
|
||||
# _RMT Transmit Example_
|
||||
# RMT Transmit Example -- Morse Code
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
This example will shows how to configure and operate the remote control (RMT) peripheral to transmit a sample message in the [Morse code](https://en.wikipedia.org/wiki/Morse_code), it also shows how to transmit custom format of data.
|
||||
This example mainly illustrates how to transmit the [Morse code](https://en.wikipedia.org/wiki/Morse_code) using the RMT driver.
|
||||
|
||||
## How to Use Example
|
||||
|
||||
@@ -29,39 +29,34 @@ GPIO18 +----/\/\/\----+------|>|-----+ GND
|
||||
|
||||
### Configure the Project
|
||||
|
||||
```
|
||||
idf.py menuconfig
|
||||
```
|
||||
Open the project configuration menu (`idf.py menuconfig`).
|
||||
|
||||
* Set serial port under Serial Flasher Options.
|
||||
In the `Example Connection Configuration` menu:
|
||||
|
||||
* Set the GPIO number used for transmitting the IR signal under `RMT TX GPIO` optin.
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
|
||||
```
|
||||
idf.py -p PORT flash monitor
|
||||
```
|
||||
Run `idf.py -p PORT flash monitor` to build, flash and monitor the project.
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the [Getting Started Guide](https://docs.espressif.com/projects/esp-idf/en/latest/get-started/index.html) for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
|
||||
## Example Output
|
||||
|
||||
To be able to see and hear the message output by the RMT, connect a LED and a speaker or an earphone (be careful - it may be loud) to the GPIO18(the pin can be changed by modify the definition of `RMT_TX_GPIO` in `main/rmt_tx_main.c`).
|
||||
To be able to see and hear the message output by the RMT, connect an LED and a speaker or an earphone (be careful it might make a large noise) to the GPIO you set in the menuconfig.
|
||||
|
||||
Run this example, you will see the following output log:
|
||||
```
|
||||
RMT Tx: Transmission complete
|
||||
RMT Tx: Sample transmission complete
|
||||
Run the example, you will see the following output log:
|
||||
|
||||
``` bash
|
||||
...
|
||||
I (304) example: Configuring transmitter
|
||||
I (2814) example: Transmission complete
|
||||
...
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
* Programming fail
|
||||
|
||||
* Hardware connection is not correct: run `idf.py -p PORT monitor`, and reboot your board to see if there is any output logs.
|
||||
* The baud rate for downloading is too high: lower your baud rate in the `menuconfig` menu, and try again.
|
||||
|
||||
For any technical queries, please open an [issue] (https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.
|
2
examples/peripherals/rmt/morse_code/main/CMakeLists.txt
Normal file
2
examples/peripherals/rmt/morse_code/main/CMakeLists.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
idf_component_register(SRCS "morse_code_main.c"
|
||||
INCLUDE_DIRS ".")
|
@@ -0,0 +1,7 @@
|
||||
menu "Example Configuration"
|
||||
config EXAMPLE_RMT_TX_GPIO
|
||||
int "RMT TX GPIO"
|
||||
default 18
|
||||
help
|
||||
Set the GPIO number used for transmitting the RMT signal.
|
||||
endmenu
|
4
examples/peripherals/rmt/morse_code/main/component.mk
Normal file
4
examples/peripherals/rmt/morse_code/main/component.mk
Normal file
@@ -0,0 +1,4 @@
|
||||
#
|
||||
# Main Makefile. This is basically the same as a component makefile.
|
||||
#
|
||||
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
|
80
examples/peripherals/rmt/morse_code/main/morse_code_main.c
Normal file
80
examples/peripherals/rmt/morse_code/main/morse_code_main.c
Normal file
@@ -0,0 +1,80 @@
|
||||
/* RMT example -- Morse Code
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include "sdkconfig.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/rmt.h"
|
||||
|
||||
static const char *TAG = "example";
|
||||
|
||||
#define RMT_TX_CHANNEL RMT_CHANNEL_0
|
||||
|
||||
/*
|
||||
* Prepare a raw table with a message in the Morse code
|
||||
*
|
||||
* The message is "ESP" : . ... .--.
|
||||
*
|
||||
* The table structure represents the RMT item structure:
|
||||
* {duration, level, duration, level}
|
||||
*
|
||||
*/
|
||||
static const rmt_item32_t morse_esp[] = {
|
||||
// E : dot
|
||||
{{{ 32767, 1, 32767, 0 }}}, // dot
|
||||
{{{ 32767, 0, 32767, 0 }}}, // SPACE
|
||||
// S : dot, dot, dot
|
||||
{{{ 32767, 1, 32767, 0 }}}, // dot
|
||||
{{{ 32767, 1, 32767, 0 }}}, // dot
|
||||
{{{ 32767, 1, 32767, 0 }}}, // dot
|
||||
{{{ 32767, 0, 32767, 0 }}}, // SPACE
|
||||
// P : dot, dash, dash, dot
|
||||
{{{ 32767, 1, 32767, 0 }}}, // dot
|
||||
{{{ 32767, 1, 32767, 1 }}},
|
||||
{{{ 32767, 1, 32767, 0 }}}, // dash
|
||||
{{{ 32767, 1, 32767, 1 }}},
|
||||
{{{ 32767, 1, 32767, 0 }}}, // dash
|
||||
{{{ 32767, 1, 32767, 0 }}}, // dot
|
||||
// RMT end marker
|
||||
{{{ 0, 1, 0, 0 }}}
|
||||
};
|
||||
|
||||
/*
|
||||
* Initialize the RMT Tx channel
|
||||
*/
|
||||
static void rmt_tx_init(void)
|
||||
{
|
||||
rmt_config_t config = RMT_DEFAULT_CONFIG_TX(CONFIG_EXAMPLE_RMT_TX_GPIO, RMT_TX_CHANNEL);
|
||||
// enable the carrier to be able to hear the Morse sound
|
||||
// if the RMT_TX_GPIO is connected to a speaker
|
||||
config.tx_config.carrier_en = true;
|
||||
config.tx_config.carrier_duty_percent = 50;
|
||||
// set audible career frequency of 611 Hz
|
||||
// actually 611 Hz is the minimum, that can be set
|
||||
// with current implementation of the RMT API
|
||||
config.tx_config.carrier_freq_hz = 611;
|
||||
// set the maximum clock divider to be able to output
|
||||
// RMT pulses in range of about one hundred milliseconds
|
||||
config.clk_div = 255;
|
||||
|
||||
ESP_ERROR_CHECK(rmt_config(&config));
|
||||
ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));
|
||||
}
|
||||
|
||||
void app_main(void *ignore)
|
||||
{
|
||||
ESP_LOGI(TAG, "Configuring transmitter");
|
||||
rmt_tx_init();
|
||||
|
||||
while (1) {
|
||||
ESP_ERROR_CHECK(rmt_write_items(RMT_TX_CHANNEL, morse_esp, sizeof(morse_esp) / sizeof(morse_esp[0]), true));
|
||||
ESP_LOGI(TAG, "Transmission complete");
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
@@ -1,80 +0,0 @@
|
||||
# _RMT NEC_TX_RX Example_
|
||||
|
||||
(See the README.md file in the upper level 'examples' directory for more information about examples.)
|
||||
|
||||
This example uses the remote control (RMT) peripheral to transmit and receive codes for the NEC infrared remote protocol.
|
||||
|
||||
## How to Use Example
|
||||
|
||||
### Hardware Required
|
||||
|
||||
* A development board with ESP32 SoC (e.g., ESP32-DevKitC, ESP-WROVER-KIT, etc.)
|
||||
* A USB cable for Power supply and programming
|
||||
|
||||
By default, this example runs a self test which assumes the TX pin (GPIO18) and RX pin (GPIO19) are _connected together_.
|
||||
|
||||
To disable self-test mode, comment out RMT_RX_SELF_TEST in infrared_nec_main.c, after which, you need to connect a IR transmitter and a receiver to GPIO18 and GPIO19.
|
||||
|
||||
The TX pin and RX pin can be modified in top of the main/infrared_nec_main.c file.
|
||||
|
||||
```
|
||||
#define RMT_TX_GPIO_NUM 18 /*!< GPIO number for transmitter signal */
|
||||
#define RMT_RX_GPIO_NUM 19 /*!< GPIO number for receiver */
|
||||
```
|
||||
|
||||
### Configure the Project
|
||||
|
||||
```
|
||||
idf.py menuconfig
|
||||
```
|
||||
|
||||
* Set serial port under Serial Flasher Options.
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
|
||||
```
|
||||
idf.py -p PORT flash monitor
|
||||
```
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
Run this example, you will see the following output log:
|
||||
```
|
||||
NEC: RMT TX DATA
|
||||
NEC: RMT RCV --- addr: 0xee11 cmd: 0xff00
|
||||
NEC: RMT RCV --- addr: 0xed12 cmd: 0xfe01
|
||||
NEC: RMT RCV --- addr: 0xec13 cmd: 0xfd02
|
||||
NEC: RMT RCV --- addr: 0xeb14 cmd: 0xfc03
|
||||
NEC: RMT RCV --- addr: 0xea15 cmd: 0xfb04
|
||||
NEC: RMT RCV --- addr: 0xe916 cmd: 0xfa05
|
||||
NEC: RMT RCV --- addr: 0xe817 cmd: 0xf906
|
||||
NEC: RMT RCV --- addr: 0xe718 cmd: 0xf807
|
||||
NEC: RMT RCV --- addr: 0xe619 cmd: 0xf708
|
||||
NEC: RMT RCV --- addr: 0xe51a cmd: 0xf609
|
||||
NEC: RMT RCV --- addr: 0xe41b cmd: 0xf50a
|
||||
NEC: RMT RCV --- addr: 0xe31c cmd: 0xf40b
|
||||
NEC: RMT RCV --- addr: 0xe21d cmd: 0xf30c
|
||||
NEC: RMT RCV --- addr: 0xe11e cmd: 0xf20d
|
||||
NEC: RMT RCV --- addr: 0xe01f cmd: 0xf10e
|
||||
NEC: RMT RCV --- addr: 0xdf20 cmd: 0xf00f
|
||||
NEC: RMT RCV --- addr: 0xde21 cmd: 0xef10
|
||||
NEC: RMT RCV --- addr: 0xdd22 cmd: 0xee11
|
||||
NEC: RMT RCV --- addr: 0xdc23 cmd: 0xed12
|
||||
NEC: RMT RCV --- addr: 0xdb24 cmd: 0xec13
|
||||
NEC: RMT RCV --- addr: 0xda25 cmd: 0xeb14
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
* Programming fail
|
||||
|
||||
* Hardware connection is not correct: run `idf.py -p PORT monitor`, and reboot your board to see if there is any output logs.
|
||||
* The baud rate for downloading is too high: lower your baud rate in the `menuconfig` menu, and try again.
|
||||
|
||||
For any technical queries, please open an [issue] (https://github.com/espressif/esp-idf/issues) on GitHub. We will get back to you soon.
|
@@ -1,2 +0,0 @@
|
||||
idf_component_register(SRCS "infrared_nec_main.c"
|
||||
INCLUDE_DIRS ".")
|
@@ -1,362 +0,0 @@
|
||||
/* NEC remote infrared RMT example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/rmt.h"
|
||||
|
||||
static const char* NEC_TAG = "NEC";
|
||||
|
||||
//CHOOSE SELF TEST OR NORMAL TEST
|
||||
#define RMT_RX_SELF_TEST 1
|
||||
|
||||
/******************************************************/
|
||||
/***** SELF TEST: *****/
|
||||
/*Connect RMT_TX_GPIO_NUM with RMT_RX_GPIO_NUM */
|
||||
/*TX task will send NEC data with carrier disabled */
|
||||
/*RX task will print NEC data it receives. */
|
||||
/******************************************************/
|
||||
#if RMT_RX_SELF_TEST
|
||||
#define RMT_RX_ACTIVE_LEVEL 1 /*!< Data bit is active high for self test mode */
|
||||
#define RMT_TX_CARRIER_EN 0 /*!< Disable carrier for self test mode */
|
||||
#else
|
||||
//Test with infrared LED, we have to enable carrier for transmitter
|
||||
//When testing via IR led, the receiver waveform is usually active-low.
|
||||
#define RMT_RX_ACTIVE_LEVEL 0 /*!< If we connect with a IR receiver, the data is active low */
|
||||
#define RMT_TX_CARRIER_EN 1 /*!< Enable carrier for IR transmitter test with IR led */
|
||||
#endif
|
||||
|
||||
#define RMT_TX_CHANNEL 1 /*!< RMT channel for transmitter */
|
||||
#define RMT_TX_GPIO_NUM 18 /*!< GPIO number for transmitter signal */
|
||||
#define RMT_RX_CHANNEL 0 /*!< RMT channel for receiver */
|
||||
#define RMT_RX_GPIO_NUM 19 /*!< GPIO number for receiver */
|
||||
#define RMT_CLK_DIV 100 /*!< RMT counter clock divider */
|
||||
#define RMT_TICK_10_US (80000000/RMT_CLK_DIV/100000) /*!< RMT counter value for 10 us.(Source clock is APB clock) */
|
||||
|
||||
#define NEC_HEADER_HIGH_US 9000 /*!< NEC protocol header: positive 9ms */
|
||||
#define NEC_HEADER_LOW_US 4500 /*!< NEC protocol header: negative 4.5ms*/
|
||||
#define NEC_BIT_ONE_HIGH_US 560 /*!< NEC protocol data bit 1: positive 0.56ms */
|
||||
#define NEC_BIT_ONE_LOW_US (2250-NEC_BIT_ONE_HIGH_US) /*!< NEC protocol data bit 1: negative 1.69ms */
|
||||
#define NEC_BIT_ZERO_HIGH_US 560 /*!< NEC protocol data bit 0: positive 0.56ms */
|
||||
#define NEC_BIT_ZERO_LOW_US (1120-NEC_BIT_ZERO_HIGH_US) /*!< NEC protocol data bit 0: negative 0.56ms */
|
||||
#define NEC_BIT_END 560 /*!< NEC protocol end: positive 0.56ms */
|
||||
#define NEC_BIT_MARGIN 20 /*!< NEC parse margin time */
|
||||
|
||||
#define NEC_ITEM_DURATION(d) ((d & 0x7fff)*10/RMT_TICK_10_US) /*!< Parse duration time from memory register value */
|
||||
#define NEC_DATA_ITEM_NUM 34 /*!< NEC code item number: header + 32bit data + end */
|
||||
#define RMT_TX_DATA_NUM 100 /*!< NEC tx test data number */
|
||||
#define rmt_item32_tIMEOUT_US 9500 /*!< RMT receiver timeout value(us) */
|
||||
|
||||
/*
|
||||
* @brief Build register value of waveform for NEC one data bit
|
||||
*/
|
||||
static inline void nec_fill_item_level(rmt_item32_t* item, int high_us, int low_us)
|
||||
{
|
||||
item->level0 = 1;
|
||||
item->duration0 = (high_us) / 10 * RMT_TICK_10_US;
|
||||
item->level1 = 0;
|
||||
item->duration1 = (low_us) / 10 * RMT_TICK_10_US;
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief Generate NEC header value: active 9ms + negative 4.5ms
|
||||
*/
|
||||
static void nec_fill_item_header(rmt_item32_t* item)
|
||||
{
|
||||
nec_fill_item_level(item, NEC_HEADER_HIGH_US, NEC_HEADER_LOW_US);
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief Generate NEC data bit 1: positive 0.56ms + negative 1.69ms
|
||||
*/
|
||||
static void nec_fill_item_bit_one(rmt_item32_t* item)
|
||||
{
|
||||
nec_fill_item_level(item, NEC_BIT_ONE_HIGH_US, NEC_BIT_ONE_LOW_US);
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief Generate NEC data bit 0: positive 0.56ms + negative 0.56ms
|
||||
*/
|
||||
static void nec_fill_item_bit_zero(rmt_item32_t* item)
|
||||
{
|
||||
nec_fill_item_level(item, NEC_BIT_ZERO_HIGH_US, NEC_BIT_ZERO_LOW_US);
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief Generate NEC end signal: positive 0.56ms
|
||||
*/
|
||||
static void nec_fill_item_end(rmt_item32_t* item)
|
||||
{
|
||||
nec_fill_item_level(item, NEC_BIT_END, 0x7fff);
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief Check whether duration is around target_us
|
||||
*/
|
||||
inline bool nec_check_in_range(int duration_ticks, int target_us, int margin_us)
|
||||
{
|
||||
if(( NEC_ITEM_DURATION(duration_ticks) < (target_us + margin_us))
|
||||
&& ( NEC_ITEM_DURATION(duration_ticks) > (target_us - margin_us))) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief Check whether this value represents an NEC header
|
||||
*/
|
||||
static bool nec_header_if(rmt_item32_t* item)
|
||||
{
|
||||
if((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL)
|
||||
&& nec_check_in_range(item->duration0, NEC_HEADER_HIGH_US, NEC_BIT_MARGIN)
|
||||
&& nec_check_in_range(item->duration1, NEC_HEADER_LOW_US, NEC_BIT_MARGIN)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief Check whether this value represents an NEC data bit 1
|
||||
*/
|
||||
static bool nec_bit_one_if(rmt_item32_t* item)
|
||||
{
|
||||
if((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL)
|
||||
&& nec_check_in_range(item->duration0, NEC_BIT_ONE_HIGH_US, NEC_BIT_MARGIN)
|
||||
&& nec_check_in_range(item->duration1, NEC_BIT_ONE_LOW_US, NEC_BIT_MARGIN)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief Check whether this value represents an NEC data bit 0
|
||||
*/
|
||||
static bool nec_bit_zero_if(rmt_item32_t* item)
|
||||
{
|
||||
if((item->level0 == RMT_RX_ACTIVE_LEVEL && item->level1 != RMT_RX_ACTIVE_LEVEL)
|
||||
&& nec_check_in_range(item->duration0, NEC_BIT_ZERO_HIGH_US, NEC_BIT_MARGIN)
|
||||
&& nec_check_in_range(item->duration1, NEC_BIT_ZERO_LOW_US, NEC_BIT_MARGIN)) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* @brief Parse NEC 32 bit waveform to address and command.
|
||||
*/
|
||||
static int nec_parse_items(rmt_item32_t* item, int item_num, uint16_t* addr, uint16_t* data)
|
||||
{
|
||||
int w_len = item_num;
|
||||
if(w_len < NEC_DATA_ITEM_NUM) {
|
||||
return -1;
|
||||
}
|
||||
int i = 0, j = 0;
|
||||
if(!nec_header_if(item++)) {
|
||||
return -1;
|
||||
}
|
||||
uint16_t addr_t = 0;
|
||||
for(j = 0; j < 16; j++) {
|
||||
if(nec_bit_one_if(item)) {
|
||||
addr_t |= (1 << j);
|
||||
} else if(nec_bit_zero_if(item)) {
|
||||
addr_t |= (0 << j);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
item++;
|
||||
i++;
|
||||
}
|
||||
uint16_t data_t = 0;
|
||||
for(j = 0; j < 16; j++) {
|
||||
if(nec_bit_one_if(item)) {
|
||||
data_t |= (1 << j);
|
||||
} else if(nec_bit_zero_if(item)) {
|
||||
data_t |= (0 << j);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
item++;
|
||||
i++;
|
||||
}
|
||||
*addr = addr_t;
|
||||
*data = data_t;
|
||||
return i;
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief Build NEC 32bit waveform.
|
||||
*/
|
||||
static int nec_build_items(int channel, rmt_item32_t* item, int item_num, uint16_t addr, uint16_t cmd_data)
|
||||
{
|
||||
int i = 0, j = 0;
|
||||
if(item_num < NEC_DATA_ITEM_NUM) {
|
||||
return -1;
|
||||
}
|
||||
nec_fill_item_header(item++);
|
||||
i++;
|
||||
for(j = 0; j < 16; j++) {
|
||||
if(addr & 0x1) {
|
||||
nec_fill_item_bit_one(item);
|
||||
} else {
|
||||
nec_fill_item_bit_zero(item);
|
||||
}
|
||||
item++;
|
||||
i++;
|
||||
addr >>= 1;
|
||||
}
|
||||
for(j = 0; j < 16; j++) {
|
||||
if(cmd_data & 0x1) {
|
||||
nec_fill_item_bit_one(item);
|
||||
} else {
|
||||
nec_fill_item_bit_zero(item);
|
||||
}
|
||||
item++;
|
||||
i++;
|
||||
cmd_data >>= 1;
|
||||
}
|
||||
nec_fill_item_end(item);
|
||||
i++;
|
||||
return i;
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief RMT transmitter initialization
|
||||
*/
|
||||
static void nec_tx_init(void)
|
||||
{
|
||||
rmt_config_t rmt_tx;
|
||||
rmt_tx.channel = RMT_TX_CHANNEL;
|
||||
rmt_tx.gpio_num = RMT_TX_GPIO_NUM;
|
||||
rmt_tx.mem_block_num = 1;
|
||||
rmt_tx.clk_div = RMT_CLK_DIV;
|
||||
rmt_tx.tx_config.loop_en = false;
|
||||
rmt_tx.tx_config.carrier_duty_percent = 50;
|
||||
rmt_tx.tx_config.carrier_freq_hz = 38000;
|
||||
rmt_tx.tx_config.carrier_level = 1;
|
||||
rmt_tx.tx_config.carrier_en = RMT_TX_CARRIER_EN;
|
||||
rmt_tx.tx_config.idle_level = 0;
|
||||
rmt_tx.tx_config.idle_output_en = true;
|
||||
rmt_tx.rmt_mode = 0;
|
||||
rmt_config(&rmt_tx);
|
||||
rmt_driver_install(rmt_tx.channel, 0, 0);
|
||||
}
|
||||
|
||||
/*
|
||||
* @brief RMT receiver initialization
|
||||
*/
|
||||
static void nec_rx_init(void)
|
||||
{
|
||||
rmt_config_t rmt_rx;
|
||||
rmt_rx.channel = RMT_RX_CHANNEL;
|
||||
rmt_rx.gpio_num = RMT_RX_GPIO_NUM;
|
||||
rmt_rx.clk_div = RMT_CLK_DIV;
|
||||
rmt_rx.mem_block_num = 1;
|
||||
rmt_rx.rmt_mode = RMT_MODE_RX;
|
||||
rmt_rx.rx_config.filter_en = true;
|
||||
rmt_rx.rx_config.filter_ticks_thresh = 100;
|
||||
rmt_rx.rx_config.idle_threshold = rmt_item32_tIMEOUT_US / 10 * (RMT_TICK_10_US);
|
||||
rmt_config(&rmt_rx);
|
||||
rmt_driver_install(rmt_rx.channel, 1000, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief RMT receiver demo, this task will print each received NEC data.
|
||||
*
|
||||
*/
|
||||
static void rmt_example_nec_rx_task(void *arg)
|
||||
{
|
||||
int channel = RMT_RX_CHANNEL;
|
||||
nec_rx_init();
|
||||
RingbufHandle_t rb = NULL;
|
||||
//get RMT RX ringbuffer
|
||||
rmt_get_ringbuf_handle(channel, &rb);
|
||||
rmt_rx_start(channel, 1);
|
||||
while(rb) {
|
||||
size_t rx_size = 0;
|
||||
//try to receive data from ringbuffer.
|
||||
//RMT driver will push all the data it receives to its ringbuffer.
|
||||
//We just need to parse the value and return the spaces of ringbuffer.
|
||||
rmt_item32_t* item = (rmt_item32_t*) xRingbufferReceive(rb, &rx_size, 1000);
|
||||
if(item) {
|
||||
uint16_t rmt_addr;
|
||||
uint16_t rmt_cmd;
|
||||
int offset = 0;
|
||||
while(1) {
|
||||
//parse data value from ringbuffer.
|
||||
int res = nec_parse_items(item + offset, rx_size / 4 - offset, &rmt_addr, &rmt_cmd);
|
||||
if(res > 0) {
|
||||
offset += res + 1;
|
||||
ESP_LOGI(NEC_TAG, "RMT RCV --- addr: 0x%04x cmd: 0x%04x", rmt_addr, rmt_cmd);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
//after parsing the data, return spaces to ringbuffer.
|
||||
vRingbufferReturnItem(rb, (void*) item);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief RMT transmitter demo, this task will periodically send NEC data. (100 * 32 bits each time.)
|
||||
*
|
||||
*/
|
||||
static void rmt_example_nec_tx_task(void *arg)
|
||||
{
|
||||
vTaskDelay(10);
|
||||
nec_tx_init();
|
||||
esp_log_level_set(NEC_TAG, ESP_LOG_INFO);
|
||||
int channel = RMT_TX_CHANNEL;
|
||||
uint16_t cmd = 0x0;
|
||||
uint16_t addr = 0x11;
|
||||
int nec_tx_num = RMT_TX_DATA_NUM;
|
||||
for(;;) {
|
||||
ESP_LOGI(NEC_TAG, "RMT TX DATA");
|
||||
size_t size = (sizeof(rmt_item32_t) * NEC_DATA_ITEM_NUM * nec_tx_num);
|
||||
//each item represent a cycle of waveform.
|
||||
rmt_item32_t* item = (rmt_item32_t*) malloc(size);
|
||||
int item_num = NEC_DATA_ITEM_NUM * nec_tx_num;
|
||||
memset((void*) item, 0, size);
|
||||
int i, offset = 0;
|
||||
while(1) {
|
||||
//To build a series of waveforms.
|
||||
i = nec_build_items(channel, item + offset, item_num - offset, ((~addr) << 8) | addr, ((~cmd) << 8) | cmd);
|
||||
if(i < 0) {
|
||||
break;
|
||||
}
|
||||
cmd++;
|
||||
addr++;
|
||||
offset += i;
|
||||
}
|
||||
//To send data according to the waveform items.
|
||||
rmt_write_items(channel, item, item_num, true);
|
||||
//Wait until sending is done.
|
||||
rmt_wait_tx_done(channel, portMAX_DELAY);
|
||||
//before we free the data, make sure sending is already done.
|
||||
free(item);
|
||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
xTaskCreate(rmt_example_nec_rx_task, "rmt_nec_rx_task", 2048, NULL, 10, NULL);
|
||||
xTaskCreate(rmt_example_nec_tx_task, "rmt_nec_tx_task", 2048, NULL, 10, NULL);
|
||||
}
|
@@ -1,2 +0,0 @@
|
||||
idf_component_register(SRCS "rmt_tx_main.c"
|
||||
INCLUDE_DIRS ".")
|
@@ -1,131 +0,0 @@
|
||||
/* RMT transmit example
|
||||
|
||||
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||
|
||||
Unless required by applicable law or agreed to in writing, this
|
||||
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "esp_log.h"
|
||||
#include "driver/rmt.h"
|
||||
|
||||
static const char *RMT_TX_TAG = "RMT Tx";
|
||||
|
||||
#define RMT_TX_CHANNEL RMT_CHANNEL_0
|
||||
#define RMT_TX_GPIO 18
|
||||
#define SAMPLE_CNT (10)
|
||||
|
||||
/*
|
||||
* Prepare a raw table with a message in the Morse code
|
||||
*
|
||||
* The message is "ESP" : . ... .--.
|
||||
*
|
||||
* The table structure represents the RMT item structure:
|
||||
* {duration, level, duration, level}
|
||||
*
|
||||
*/
|
||||
rmt_item32_t items[] = {
|
||||
// E : dot
|
||||
{{{ 32767, 1, 32767, 0 }}}, // dot
|
||||
//
|
||||
{{{ 32767, 0, 32767, 0 }}}, // SPACE
|
||||
// S : dot, dot, dot
|
||||
{{{ 32767, 1, 32767, 0 }}}, // dot
|
||||
{{{ 32767, 1, 32767, 0 }}}, // dot
|
||||
{{{ 32767, 1, 32767, 0 }}}, // dot
|
||||
//
|
||||
{{{ 32767, 0, 32767, 0 }}}, // SPACE
|
||||
// P : dot, dash, dash, dot
|
||||
{{{ 32767, 1, 32767, 0 }}}, // dot
|
||||
{{{ 32767, 1, 32767, 1 }}},
|
||||
{{{ 32767, 1, 32767, 0 }}}, // dash
|
||||
{{{ 32767, 1, 32767, 1 }}},
|
||||
{{{ 32767, 1, 32767, 0 }}}, // dash
|
||||
{{{ 32767, 1, 32767, 0 }}}, // dot
|
||||
|
||||
// RMT end marker
|
||||
{{{ 0, 1, 0, 0 }}}
|
||||
};
|
||||
|
||||
//Convert uint8_t type of data to rmt format data.
|
||||
static void IRAM_ATTR u8_to_rmt(const void* src, rmt_item32_t* dest, size_t src_size,
|
||||
size_t wanted_num, size_t* translated_size, size_t* item_num)
|
||||
{
|
||||
if(src == NULL || dest == NULL) {
|
||||
*translated_size = 0;
|
||||
*item_num = 0;
|
||||
return;
|
||||
}
|
||||
const rmt_item32_t bit0 = {{{ 32767, 1, 15000, 0 }}}; //Logical 0
|
||||
const rmt_item32_t bit1 = {{{ 32767, 1, 32767, 0 }}}; //Logical 1
|
||||
size_t size = 0;
|
||||
size_t num = 0;
|
||||
uint8_t *psrc = (uint8_t *)src;
|
||||
rmt_item32_t* pdest = dest;
|
||||
while (size < src_size && num < wanted_num) {
|
||||
for(int i = 0; i < 8; i++) {
|
||||
if(*psrc & (0x1 << i)) {
|
||||
pdest->val = bit1.val;
|
||||
} else {
|
||||
pdest->val = bit0.val;
|
||||
}
|
||||
num++;
|
||||
pdest++;
|
||||
}
|
||||
size++;
|
||||
psrc++;
|
||||
}
|
||||
*translated_size = size;
|
||||
*item_num = num;
|
||||
}
|
||||
|
||||
/*
|
||||
* Initialize the RMT Tx channel
|
||||
*/
|
||||
static void rmt_tx_int(void)
|
||||
{
|
||||
rmt_config_t config;
|
||||
config.rmt_mode = RMT_MODE_TX;
|
||||
config.channel = RMT_TX_CHANNEL;
|
||||
config.gpio_num = RMT_TX_GPIO;
|
||||
config.mem_block_num = 1;
|
||||
config.tx_config.loop_en = 0;
|
||||
// enable the carrier to be able to hear the Morse sound
|
||||
// if the RMT_TX_GPIO is connected to a speaker
|
||||
config.tx_config.carrier_en = 1;
|
||||
config.tx_config.idle_output_en = 1;
|
||||
config.tx_config.idle_level = 0;
|
||||
config.tx_config.carrier_duty_percent = 50;
|
||||
// set audible career frequency of 611 Hz
|
||||
// actually 611 Hz is the minimum, that can be set
|
||||
// with current implementation of the RMT API
|
||||
config.tx_config.carrier_freq_hz = 611;
|
||||
config.tx_config.carrier_level = 1;
|
||||
// set the maximum clock divider to be able to output
|
||||
// RMT pulses in range of about one hundred milliseconds
|
||||
config.clk_div = 255;
|
||||
|
||||
ESP_ERROR_CHECK(rmt_config(&config));
|
||||
ESP_ERROR_CHECK(rmt_driver_install(config.channel, 0, 0));
|
||||
ESP_ERROR_CHECK(rmt_translator_init(config.channel, u8_to_rmt));
|
||||
}
|
||||
|
||||
void app_main(void *ignore)
|
||||
{
|
||||
ESP_LOGI(RMT_TX_TAG, "Configuring transmitter");
|
||||
rmt_tx_int();
|
||||
int number_of_items = sizeof(items) / sizeof(items[0]);
|
||||
const uint8_t sample[SAMPLE_CNT] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||
|
||||
while (1) {
|
||||
ESP_ERROR_CHECK(rmt_write_items(RMT_TX_CHANNEL, items, number_of_items, true));
|
||||
ESP_LOGI(RMT_TX_TAG, "Transmission complete");
|
||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
||||
ESP_ERROR_CHECK(rmt_write_sample(RMT_TX_CHANNEL, sample, SAMPLE_CNT, true));
|
||||
ESP_LOGI(RMT_TX_TAG, "Sample transmission complete");
|
||||
vTaskDelay(2000 / portTICK_PERIOD_MS);
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
}
|
Reference in New Issue
Block a user