mirror of
https://github.com/espressif/esp-idf.git
synced 2025-09-30 19:19:21 +00:00
pcnt: added enable/disable functions
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
set(srcs "test_app_main.c"
|
||||
"test_pulse_cnt.c")
|
||||
"test_pulse_cnt_simulator.c"
|
||||
"test_pulse_cnt.c"
|
||||
"test_pulse_cnt_iram.c")
|
||||
|
||||
# In order for the cases defined by `TEST_CASE` to be linked into the final elf,
|
||||
# the component can be registered as WHOLE_ARCHIVE
|
||||
|
@@ -13,43 +13,7 @@
|
||||
#include "driver/gpio.h"
|
||||
#include "soc/soc_caps.h"
|
||||
#include "esp_attr.h"
|
||||
|
||||
#define TEST_PCNT_GPIO_A 0
|
||||
#define TEST_PCNT_GPIO_B 2
|
||||
|
||||
#if CONFIG_PCNT_ISR_IRAM_SAFE
|
||||
#define TEST_PCNT_CALLBACK_ATTR IRAM_ATTR
|
||||
#else
|
||||
#define TEST_PCNT_CALLBACK_ATTR
|
||||
#endif // CONFIG_PCNT_ISR_IRAM_SAFE
|
||||
|
||||
// helper function to simulate several rising edges on gpio
|
||||
static void test_gpio_simulate_rising_edge(int gpio_sig, size_t times)
|
||||
{
|
||||
while (times--) {
|
||||
TEST_ESP_OK(gpio_set_level(gpio_sig, 0));
|
||||
TEST_ESP_OK(gpio_set_level(gpio_sig, 1));
|
||||
}
|
||||
}
|
||||
|
||||
// helper function to simulate several groups of quadrature signals
|
||||
static void test_gpio_simulate_quadrature_signals(int gpio_sig_a, int gpio_sig_b, size_t times)
|
||||
{
|
||||
while (times--) {
|
||||
TEST_ESP_OK(gpio_set_level(gpio_sig_a, 1));
|
||||
TEST_ESP_OK(gpio_set_level(gpio_sig_b, 0));
|
||||
vTaskDelay(1);
|
||||
TEST_ESP_OK(gpio_set_level(gpio_sig_a, 0));
|
||||
TEST_ESP_OK(gpio_set_level(gpio_sig_b, 0));
|
||||
vTaskDelay(1);
|
||||
TEST_ESP_OK(gpio_set_level(gpio_sig_a, 0));
|
||||
TEST_ESP_OK(gpio_set_level(gpio_sig_b, 1));
|
||||
vTaskDelay(1);
|
||||
TEST_ESP_OK(gpio_set_level(gpio_sig_a, 1));
|
||||
TEST_ESP_OK(gpio_set_level(gpio_sig_b, 1));
|
||||
vTaskDelay(1);
|
||||
}
|
||||
}
|
||||
#include "test_pulse_cnt_board.h"
|
||||
|
||||
TEST_CASE("pcnt_unit_install_uninstall", "[pcnt]")
|
||||
{
|
||||
@@ -80,18 +44,28 @@ TEST_CASE("pcnt_unit_install_uninstall", "[pcnt]")
|
||||
filter_config.max_glitch_ns = 500000;
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, pcnt_unit_set_glitch_filter(units[0], &filter_config));
|
||||
|
||||
printf("enable pcnt units\r\n");
|
||||
for (int i = 0; i < SOC_PCNT_UNITS_PER_GROUP; i++) {
|
||||
TEST_ESP_OK(pcnt_unit_enable(units[i]));
|
||||
}
|
||||
|
||||
printf("start pcnt units\r\n");
|
||||
for (int i = 0; i < SOC_PCNT_UNITS_PER_GROUP; i++) {
|
||||
TEST_ESP_OK(pcnt_unit_start(units[i]));
|
||||
}
|
||||
// can't uninstall unit before stop it
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, pcnt_del_unit(units[0]));
|
||||
|
||||
printf("stop pcnt units\r\n");
|
||||
for (int i = 0; i < SOC_PCNT_UNITS_PER_GROUP; i++) {
|
||||
TEST_ESP_OK(pcnt_unit_stop(units[i]));
|
||||
}
|
||||
|
||||
// can't uninstall unit before disable it
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, pcnt_del_unit(units[0]));
|
||||
printf("disable pcnt units\r\n");
|
||||
for (int i = 0; i < SOC_PCNT_UNITS_PER_GROUP; i++) {
|
||||
TEST_ESP_OK(pcnt_unit_disable(units[i]));
|
||||
}
|
||||
|
||||
printf("uninstall pcnt units\r\n");
|
||||
for (int i = 0; i < SOC_PCNT_UNITS_PER_GROUP; i++) {
|
||||
TEST_ESP_OK(pcnt_del_unit(units[i]));
|
||||
@@ -125,6 +99,7 @@ TEST_CASE("pcnt_channel_install_uninstall", "[pcnt]")
|
||||
TEST_ESP_OK(pcnt_channel_set_level_action(chans[i][j], PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_KEEP));
|
||||
}
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_NOT_FOUND, pcnt_new_channel(units[i], &chan_config, &chans[i][0]));
|
||||
TEST_ESP_OK(pcnt_unit_enable(units[i]));
|
||||
}
|
||||
|
||||
printf("start units\r\n");
|
||||
@@ -174,6 +149,7 @@ TEST_CASE("pcnt_channel_install_uninstall", "[pcnt]")
|
||||
for (int i = 0; i < SOC_PCNT_UNITS_PER_GROUP; i++) {
|
||||
// stop unit
|
||||
TEST_ESP_OK(pcnt_unit_stop(units[i]));
|
||||
TEST_ESP_OK(pcnt_unit_disable(units[i]));
|
||||
// can't uninstall unit when channel is still alive
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, pcnt_del_unit(units[i]));
|
||||
for (int j = 0; j < SOC_PCNT_CHANNELS_PER_UNIT; j++) {
|
||||
@@ -257,6 +233,9 @@ TEST_CASE("pcnt_quadrature_decode_event", "[pcnt]")
|
||||
|
||||
// Clear internal counter, and make the watch points take effect
|
||||
TEST_ESP_OK(pcnt_unit_clear_count(unit));
|
||||
// start unit should fail if it's not enabled yet
|
||||
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, pcnt_unit_start(unit));
|
||||
TEST_ESP_OK(pcnt_unit_enable(unit));
|
||||
TEST_ESP_OK(pcnt_unit_start(unit));
|
||||
|
||||
printf("simulating quadrature signals\r\n");
|
||||
@@ -300,6 +279,7 @@ TEST_CASE("pcnt_quadrature_decode_event", "[pcnt]")
|
||||
TEST_ESP_OK(pcnt_del_channel(channelA));
|
||||
TEST_ESP_OK(pcnt_del_channel(channelB));
|
||||
TEST_ESP_OK(pcnt_unit_stop(unit));
|
||||
TEST_ESP_OK(pcnt_unit_disable(unit));
|
||||
TEST_ESP_OK(pcnt_del_unit(unit));
|
||||
}
|
||||
|
||||
@@ -354,6 +334,9 @@ TEST_CASE("pcnt_zero_cross_mode", "[pcnt]")
|
||||
TEST_ESP_OK(pcnt_channel_set_edge_action(channelB, PCNT_CHANNEL_EDGE_ACTION_HOLD, PCNT_CHANNEL_EDGE_ACTION_HOLD));
|
||||
TEST_ESP_OK(pcnt_channel_set_level_action(channelB, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_KEEP));
|
||||
|
||||
printf("enable unit\r\n");
|
||||
TEST_ESP_OK(pcnt_unit_enable(unit));
|
||||
|
||||
printf("start unit\r\n");
|
||||
TEST_ESP_OK(pcnt_unit_start(unit));
|
||||
|
||||
@@ -397,6 +380,7 @@ TEST_CASE("pcnt_zero_cross_mode", "[pcnt]")
|
||||
TEST_ASSERT_EQUAL(PCNT_UNIT_ZERO_CROSS_NEG_ZERO, user_data.mode);
|
||||
|
||||
TEST_ESP_OK(pcnt_unit_stop(unit));
|
||||
TEST_ESP_OK(pcnt_unit_disable(unit));
|
||||
TEST_ESP_OK(pcnt_unit_remove_watch_point(unit, 0));
|
||||
TEST_ESP_OK(pcnt_del_channel(channelA));
|
||||
TEST_ESP_OK(pcnt_del_channel(channelB));
|
||||
|
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define TEST_PCNT_GPIO_A 0
|
||||
#define TEST_PCNT_GPIO_B 2
|
||||
|
||||
#if CONFIG_PCNT_ISR_IRAM_SAFE
|
||||
#define TEST_PCNT_CALLBACK_ATTR IRAM_ATTR
|
||||
#else
|
||||
#define TEST_PCNT_CALLBACK_ATTR
|
||||
#endif // CONFIG_PCNT_ISR_IRAM_SAFE
|
||||
|
||||
void test_gpio_simulate_rising_edge(int gpio_sig, size_t times);
|
||||
void test_gpio_simulate_quadrature_signals(int gpio_sig_a, int gpio_sig_b, size_t times);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
104
components/driver/test_apps/pulse_cnt/main/test_pulse_cnt_iram.c
Normal file
104
components/driver/test_apps/pulse_cnt/main/test_pulse_cnt_iram.c
Normal file
@@ -0,0 +1,104 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "sdkconfig.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
#include "unity.h"
|
||||
#include "driver/pulse_cnt.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_spi_flash.h"
|
||||
#include "esp_attr.h"
|
||||
#include "soc/soc_caps.h"
|
||||
#include "test_pulse_cnt_board.h"
|
||||
|
||||
#if CONFIG_PCNT_ISR_IRAM_SAFE
|
||||
|
||||
static bool IRAM_ATTR test_pcnt_iram_safe_callback(pcnt_unit_handle_t unit, pcnt_watch_event_data_t *event_data, void *user_data)
|
||||
{
|
||||
uint32_t *data = (uint32_t *)user_data;
|
||||
(*data)++;
|
||||
// PCNT control function can still work in ISR
|
||||
pcnt_unit_stop(unit);
|
||||
return false;
|
||||
}
|
||||
|
||||
static void IRAM_ATTR test_pcnt_iram_simulation(int gpio_sig)
|
||||
{
|
||||
// disable flash cache
|
||||
spi_flash_guard_get()->start();
|
||||
test_gpio_simulate_rising_edge(gpio_sig, 2);
|
||||
// enable flash cache
|
||||
spi_flash_guard_get()->end();
|
||||
}
|
||||
|
||||
TEST_CASE("pcnt_iram_interrupt_safe", "[pcnt]")
|
||||
{
|
||||
pcnt_unit_config_t unit_config = {
|
||||
.low_limit = -100,
|
||||
.high_limit = 100
|
||||
};
|
||||
|
||||
printf("install pcnt unit\r\n");
|
||||
pcnt_unit_handle_t unit = NULL;
|
||||
TEST_ESP_OK(pcnt_new_unit(&unit_config, &unit));
|
||||
|
||||
printf("add watch point and event callback\r\n");
|
||||
TEST_ESP_OK(pcnt_unit_add_watch_point(unit, 2));
|
||||
pcnt_event_callbacks_t cbs = {
|
||||
.on_reach = test_pcnt_iram_safe_callback,
|
||||
};
|
||||
uint32_t num_of_event_triggered = 0;
|
||||
TEST_ESP_OK(pcnt_unit_register_event_callbacks(unit, &cbs, &num_of_event_triggered));
|
||||
|
||||
printf("install pcnt channels\r\n");
|
||||
pcnt_chan_config_t channel_config = {
|
||||
.edge_gpio_num = TEST_PCNT_GPIO_A,
|
||||
.level_gpio_num = -1,
|
||||
.flags.io_loop_back = true,
|
||||
};
|
||||
pcnt_channel_handle_t channelA = NULL;
|
||||
pcnt_channel_handle_t channelB = NULL;
|
||||
TEST_ESP_OK(pcnt_new_channel(unit, &channel_config, &channelA));
|
||||
TEST_ESP_OK(pcnt_new_channel(unit, &channel_config, &channelB));
|
||||
|
||||
printf("enable unit\r\n");
|
||||
TEST_ESP_OK(pcnt_unit_enable(unit));
|
||||
|
||||
printf("Set pcnt actions for channels\r\n");
|
||||
// both channels increase on pulse edge
|
||||
TEST_ESP_OK(pcnt_channel_set_edge_action(channelA, PCNT_CHANNEL_EDGE_ACTION_INCREASE, PCNT_CHANNEL_EDGE_ACTION_HOLD));
|
||||
TEST_ESP_OK(pcnt_channel_set_level_action(channelA, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_KEEP));
|
||||
TEST_ESP_OK(pcnt_channel_set_edge_action(channelA, PCNT_CHANNEL_EDGE_ACTION_INCREASE, PCNT_CHANNEL_EDGE_ACTION_HOLD));
|
||||
TEST_ESP_OK(pcnt_channel_set_level_action(channelB, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_KEEP));
|
||||
|
||||
printf("start unit\r\n");
|
||||
TEST_ESP_OK(pcnt_unit_start(unit));
|
||||
|
||||
printf("disable cache and check interrupt triggered\r\n");
|
||||
TEST_ESP_OK(pcnt_unit_clear_count(unit));
|
||||
// the function that will disable the flash must be placed in the IRAM
|
||||
test_pcnt_iram_simulation(TEST_PCNT_GPIO_A);
|
||||
// check if the interrupt has fired up
|
||||
TEST_ASSERT_EQUAL(1, num_of_event_triggered);
|
||||
|
||||
printf("check current count value\r\n");
|
||||
int cur_count = 0;
|
||||
TEST_ESP_OK(pcnt_unit_get_count(unit, &cur_count));
|
||||
// when the watch point got triggered, we disabled the PCNT unit in thr `test_pcnt_iram_safe_callback()`
|
||||
// so the finally count value should equal to the watch point value
|
||||
TEST_ASSERT_EQUAL(2, cur_count);
|
||||
|
||||
printf("delete channels and unit\r\n");
|
||||
TEST_ESP_OK(pcnt_unit_disable(unit));
|
||||
TEST_ESP_OK(pcnt_del_channel(channelA));
|
||||
TEST_ESP_OK(pcnt_del_channel(channelB));
|
||||
TEST_ESP_OK(pcnt_del_unit(unit));
|
||||
}
|
||||
|
||||
#endif // CONFIG_PCNT_ISR_IRAM_SAFE
|
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include "sdkconfig.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "driver/gpio.h"
|
||||
#include "esp_attr.h"
|
||||
#include "test_pulse_cnt_board.h"
|
||||
|
||||
// helper function to simulate several rising edges on gpio
|
||||
IRAM_ATTR void test_gpio_simulate_rising_edge(int gpio_sig, size_t times)
|
||||
{
|
||||
while (times--) {
|
||||
gpio_set_level(gpio_sig, 0);
|
||||
gpio_set_level(gpio_sig, 1);
|
||||
}
|
||||
}
|
||||
|
||||
// helper function to simulate several groups of quadrature signals
|
||||
IRAM_ATTR void test_gpio_simulate_quadrature_signals(int gpio_sig_a, int gpio_sig_b, size_t times)
|
||||
{
|
||||
while (times--) {
|
||||
gpio_set_level(gpio_sig_a, 1);
|
||||
gpio_set_level(gpio_sig_b, 0);
|
||||
vTaskDelay(1);
|
||||
gpio_set_level(gpio_sig_a, 0);
|
||||
gpio_set_level(gpio_sig_b, 0);
|
||||
vTaskDelay(1);
|
||||
gpio_set_level(gpio_sig_a, 0);
|
||||
gpio_set_level(gpio_sig_b, 1);
|
||||
vTaskDelay(1);
|
||||
gpio_set_level(gpio_sig_a, 1);
|
||||
gpio_set_level(gpio_sig_b, 1);
|
||||
vTaskDelay(1);
|
||||
}
|
||||
}
|
@@ -1,6 +1,7 @@
|
||||
CONFIG_COMPILER_DUMP_RTL_FILES=y
|
||||
CONFIG_PCNT_CTRL_FUNC_IN_IRAM=y
|
||||
CONFIG_PCNT_ISR_IRAM_SAFE=y
|
||||
CONFIG_GPIO_CTRL_FUNC_IN_IRAM=y
|
||||
|
||||
# silent the error check, as the error string are stored in rodata, causing RTL check failure
|
||||
CONFIG_COMPILER_OPTIMIZATION_CHECKS_SILENT=y
|
||||
|
Reference in New Issue
Block a user