sdm: refactor example with new driver

This commit is contained in:
morris 2022-06-22 16:28:32 +08:00
parent f33428f2e5
commit edb5600348
14 changed files with 136 additions and 149 deletions

View File

@ -23,13 +23,13 @@ typedef struct sdm_channel_t *sdm_channel_handle_t;
* @brief Sigma Delta channel configuration
*/
typedef struct {
int gpio_num; /*!< GPIO number */
int gpio_num; /*!< GPIO number */
sdm_clock_source_t clk_src; /*!< Clock source */
uint32_t sample_rate_hz; /*!< Sample rate in Hz, it determines how frequent the modulator outputs a pulse */
uint32_t sample_rate_hz; /*!< Sample rate in Hz, it determines how frequent the modulator outputs a pulse */
struct {
uint32_t invert_out: 1; /*!< Whether to invert the output signal */
uint32_t io_loop_back: 1; /*!< For debug/test, the signal output from the GPIO will be fed to the input path as well */
} flags; /*!< Extra flags */
uint32_t invert_out: 1; /*!< Whether to invert the output signal */
uint32_t io_loop_back: 1; /*!< For debug/test, the signal output from the GPIO will be fed to the input path as well */
} flags; /*!< Extra flags */
} sdm_config_t;
/**

View File

@ -91,7 +91,7 @@ static sdm_group_t *sdm_acquire_group_handle(int group_id)
// initialize sdm group members
group->group_id = group_id;
group->spinlock = (portMUX_TYPE)portMUX_INITIALIZER_UNLOCKED;
group->clk_src = SDM_CLK_SRC_NONE;
group->clk_src = 0;
// initialize HAL context
sdm_hal_init(&group->hal, group_id);
// enable clock
@ -206,7 +206,7 @@ esp_err_t sdm_new_channel(const sdm_config_t *config, sdm_channel_handle_t *ret_
int group_id = group->group_id;
int chan_id = chan->chan_id;
ESP_GOTO_ON_FALSE(group->clk_src == SDM_CLK_SRC_NONE || group->clk_src == config->clk_src, ESP_ERR_INVALID_ARG, err, TAG, "clock source conflict");
ESP_GOTO_ON_FALSE(group->clk_src == 0 || group->clk_src == config->clk_src, ESP_ERR_INVALID_ARG, err, TAG, "clock source conflict");
uint32_t src_clk_hz = 0;
switch (config->clk_src) {
case SDM_CLK_SRC_APB:

View File

@ -76,21 +76,17 @@ examples/peripherals/rmt/ir_nec_transceiver:
disable:
- if: SOC_RMT_SUPPORTED != 1
disable_test:
- if: IDF_TARGET in ["esp32c3", "esp32s2", "esp32s3"]
- if: IDF_TARGET != "esp32"
temporary: true
reason: lack of runners
examples/peripherals/rmt/musical_buzzer:
enable:
- if: IDF_TARGET in ["esp32c3", "esp32s2", "esp32s3"]
temporary: true
reason: the other targets are not tested yet
- if: SOC_RMT_SUPPORT_TX_LOOP_COUNT == 1
examples/peripherals/rmt/stepper_motor:
enable:
- if: IDF_TARGET == "esp32s3"
temporary: true
reason: the other targets are not tested yet
- if: SOC_RMT_SUPPORT_TX_LOOP_AUTO_STOP == 1
examples/peripherals/sdio:
disable:
@ -102,11 +98,9 @@ examples/peripherals/secure_element/atecc608_ecdsa:
temporary: true
reason: the other targets are not tested yet
examples/peripherals/sigmadelta:
examples/peripherals/sigma_delta:
disable:
- if: IDF_TARGET == "esp32c2"
temporary: true
reason: target esp32c2 is not supported yet
- if: SOC_SDM_SUPPORTED != 1
examples/peripherals/spi_master/hd_eeprom:
disable:

View File

@ -1,5 +1,5 @@
| Supported Targets | ESP32-C3 | ESP32-S2 | ESP32-S3 |
| ----------------- | -------- | -------- | -------- |
| Supported Targets | ESP32-C3 | ESP32-H2 | ESP32-S2 | ESP32-S3 |
| ----------------- | -------- | -------- | -------- | -------- |
# RMT Transmit Loop Count Example -- Musical Buzzer

View File

@ -8,6 +8,7 @@ from pytest_embedded import Dut
@pytest.mark.esp32s2
@pytest.mark.esp32s3
@pytest.mark.esp32c3
@pytest.mark.esp32h2
@pytest.mark.generic
def test_musical_buzzer_example(dut: Dut) -> None:
dut.expect_exact('example: Create RMT TX channel')

View File

@ -3,4 +3,4 @@
cmake_minimum_required(VERSION 3.16)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(sigmadelta)
project(gpio_sigma_delta)

View File

@ -0,0 +1,54 @@
| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 |
| ----------------- | ----- | -------- | -------- | -------- |
# Sigma Delta Modulation Example
(See the README.md file in the upper level 'examples' directory for more information about examples.)
This example uses the sigma-delta driver to generate modulated output on a GPIO. If you connect a LED or LCD backlight control to the output GPIO, you will see it slowly brightening and dimming.
## How to use example
### Hardware Required
* A development board with any supported Espressif SoC (e.g., ESP32-DevKitC, ESP-WROVER-KIT, etc.)
* A USB cable for Power supply and programming
* An LED and a resistor to limit the LED current. Connect them as below:
```
330R LED
EXAMPLE_SIGMA_DELTA_GPIO_NUM +----/\/\/\----+------|>|-----+ GND
```
### Configure the project
You can change the GPIO number by [EXAMPLE_SIGMA_DELTA_GPIO_NUM](main/sdm_example_main.c).
### Build and Flash
Build the project and flash it to the board, then run the monitor tool to view the serial output:
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
Once the upload is complete and the board is reset, the program should start running. This is reported on the monitor as below:
```
...
I (245) cpu_start: Starting scheduler.
I (249) example: Install sigma delta channel
I (249) gpio: GPIO[0]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:0
I (259) example: Enable sigma delta channel
I (259) example: Change duty cycle continuously
```
Immediately after that the LED should start brightening and dimming.
## 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.

View File

@ -0,0 +1,2 @@
idf_component_register(SRCS "sdm_example_main.c"
INCLUDE_DIRS ".")

View File

@ -0,0 +1,48 @@
/*
* SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Unlicense OR CC0-1.0
*/
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/sdm.h"
#define EXAMPLE_SIGMA_DELTA_GPIO_NUM 0
#define EXAMPLE_LED_DIM_PERIOD_MS 1000
#define EXAMPLE_LED_DIM_DELAY_MS 10
#define EXAMPLE_LED_DIM_DUTY_STEP 2
#define EXAMPLE_LED_DIM_DUTY_MAX 90
#define EXAMPLE_LED_DIM_DUTY_MIN (EXAMPLE_LED_DIM_DUTY_MAX - EXAMPLE_LED_DIM_PERIOD_MS / EXAMPLE_LED_DIM_DELAY_MS * EXAMPLE_LED_DIM_DUTY_STEP)
static const char *TAG = "example";
void app_main(void)
{
ESP_LOGI(TAG, "Install sigma delta channel");
sdm_channel_handle_t sdm_chan = NULL;
sdm_config_t config = {
.clk_src = SDM_CLK_SRC_DEFAULT,
.gpio_num = EXAMPLE_SIGMA_DELTA_GPIO_NUM,
.sample_rate_hz = 1 * 1000 * 1000, // 1MHz sample rate
};
ESP_ERROR_CHECK(sdm_new_channel(&config, &sdm_chan));
ESP_LOGI(TAG, "Enable sigma delta channel");
ESP_ERROR_CHECK(sdm_channel_enable(sdm_chan));
ESP_LOGI(TAG, "Change duty cycle continuously");
int8_t duty = 0;
int step = EXAMPLE_LED_DIM_DUTY_STEP;
while (1) {
ESP_ERROR_CHECK(sdm_channel_set_duty(sdm_chan, duty));
/* By changing delay time, you can change the blink frequency of LED */
vTaskDelay(pdMS_TO_TICKS(EXAMPLE_LED_DIM_DELAY_MS));
duty += step;
if (duty == EXAMPLE_LED_DIM_DUTY_MAX || duty == EXAMPLE_LED_DIM_DUTY_MIN) {
step *= -1;
}
}
}

View File

@ -0,0 +1,16 @@
# SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: CC0-1.0
import pytest
from pytest_embedded import Dut
@pytest.mark.esp32
@pytest.mark.esp32s2
@pytest.mark.esp32s3
@pytest.mark.esp32c3
@pytest.mark.generic
def test_sdm_example(dut: Dut) -> None:
dut.expect_exact('example: Install sigma delta channel')
dut.expect_exact('example: Enable sigma delta channel')
dut.expect_exact('example: Change duty cycle continuously')

View File

@ -1,71 +0,0 @@
| Supported Targets | ESP32 | ESP32-C3 | ESP32-S2 | ESP32-S3 |
| ----------------- | ----- | -------- | -------- | -------- |
# Sigma Delta Modulation Example
(See the README.md file in the upper level 'examples' directory for more information about examples.)
This example uses the sigma-delta driver to generate modulated output on a GPIO. If you connect a LED to the output GPIO, you will see it slowly brightening and dimming.
## How to use example
### Hardware Required
Besides the [ESP32 development board](https://www.espressif.com/en/products/hardware/development-boards) you need a LED and a resistor to limit the LED current. Connect them as below:
```
330R LED
GPIO4 +----/\/\/\----+------|>|-----+ GND
```
A resistor in range from 100 Ohm to 1 kOhm should usually be fine. You may use ESP32 development board by other vendors as well, provided they have at least one GPIO output pin exposed.
By default the GPIO output is 4. To change it, edit the line with `GPIO_NUM_4` in `sigmadelta_init()` function inside `main/sigmadelta_test.c`. For example to use GPIO 25, modify the line to contain `GPIO_NUM_25` instead.
### Configure the project
```
idf.py menuconfig
```
### 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
Once the upload is complete and the board is reset, the program should start running. This is reported on the monitor as below:
```
...
I (275) cpu_start: Pro cpu start user code
I (293) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
```
Immediately after that the LED should start brightening and dimming.
## Troubleshooting
If you are using [ESP-WROVER-KIT](https://www.espressif.com/en/products/hardware/esp-wrover-kit/overview) then this board has an RGB LED already installed. GPIO4 is driving blue color of the LED. The brightening and dimming effect of the blue LED may not be distinctly visible because red and green LEDs are not actively driven by this example and will slightly lit. To resolve this issue you can switch both diodes off by adding the following code at the end of `sigmadelta_example_init()` function:
```c
esp_rom_gpio_pad_select_gpio(GPIO_NUM_0);
gpio_set_direction(GPIO_NUM_0, GPIO_MODE_OUTPUT);
gpio_set_level(GPIO_NUM_0, 0);
esp_rom_gpio_pad_select_gpio(GPIO_NUM_2);
gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT);
gpio_set_level(GPIO_NUM_2, 0);
```

View File

@ -1,2 +0,0 @@
idf_component_register(SRCS "sigmadelta_example_main.c"
INCLUDE_DIRS ".")

View File

@ -1,54 +0,0 @@
/* Sigma-delta 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 "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "driver/sigmadelta.h"
/*
* This test code will configure sigma-delta and set GPIO4 as a signal output pin.
* If you connect this GPIO4 with a LED, you will see the LED blinking slowly.
*/
/*
* Configure and initialize the sigma delta modulation
* on channel 0 to output signal on GPIO4
*/
static void sigmadelta_example_init(void)
{
sigmadelta_config_t sigmadelta_cfg = {
.channel = SIGMADELTA_CHANNEL_0,
.sigmadelta_prescale = 80,
.sigmadelta_duty = 0,
.sigmadelta_gpio = GPIO_NUM_4,
};
sigmadelta_config(&sigmadelta_cfg);
}
/*
* Perform the sigma-delta modulation test
* by changing the duty of the output signal.
*/
void app_main(void)
{
sigmadelta_example_init();
int8_t duty = 0;
int inc = 1;
while (1) {
sigmadelta_set_duty(SIGMADELTA_CHANNEL_0, duty);
/* By changing delay time, you can change the blink frequency of LED */
vTaskDelay(10 / portTICK_PERIOD_MS);
duty += inc;
if (duty == 127 || duty == -127) {
inc = (-1) * inc;
}
}
}

View File

@ -1799,7 +1799,6 @@ examples/peripherals/mcpwm/mcpwm_servo_control/main/mcpwm_servo_control_example_
examples/peripherals/sdio/host/main/app_main.c
examples/peripherals/sdio/sdio_test.py
examples/peripherals/sdio/slave/main/app_main.c
examples/peripherals/sigmadelta/main/sigmadelta_example_main.c
examples/peripherals/spi_master/hd_eeprom/components/eeprom/spi_eeprom.c
examples/peripherals/spi_master/hd_eeprom/components/eeprom/spi_eeprom.h
examples/peripherals/spi_master/hd_eeprom/main/spi_eeprom_main.c