Commit a6381702 by ZhengJH

Initial commit

parents
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db
# Folder config file
Desktop.ini
# Recycle Bin used on file shares
$RECYCLE.BIN/
# Windows Installer files
*.cab
*.msi
*.msm
*.msp
# Windows shortcuts
*.lnk
# Subversion folders
.svn
# Build folder
build
# VSCode folder
.vscode
# old config file
sdkconfig.old
# =========================
# Operating System Files
# =========================
# 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(comTest)
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#
PROJECT_NAME := ledc_basic
include $(IDF_PATH)/make/project.mk
# _LEDC Basic Example_
(See the README.md file in the upper level 'examples' directory for more information about examples.)
This example shows how to use the LEDC to generate a PWM signal using the `LOW SPEED` mode.
To use `HIGH SPEED` mode check if the selected SoC supports this mode.
## How to use example
### Hardware Required
* A development board with ESP32, ESP32-S2, ESP32-C3 or ESP32-S3 SoC (e.g., ESP32-DevKitC, ESP-WROVER-KIT, etc.)
* A USB cable for power supply and programming
Connect the GPIO to an oscilloscope to see the generated signal:
|ledc channel| GPIO |
|:----------:|:-----:|
| Channel 0 | GPIO5 |
### Configure the project
The example uses fixed PWM frequency of 5 kHz, duty cycle in 50%, and output GPIO pin. To change them, adjust `LEDC_FREQUENCY`, `LEDC_DUTY`, `LEDC_OUTPUT_IO` macros at the top of ledc_basic_example_main.c.
Depending on the selected `LEDC_FREQUENCY`, you will need to change the `LEDC_DUTY_RES`.
To dinamicaly set the duty and frequency, you can use the following functions:
To set the frequency to 2.5 kHZ i.e:
```c
ledc_set_freq(LEDC_MODE, LEDC_TIMER, 2500);
```
Now the duty to 100% i.e:
```c
ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, 8191);
ledc_update_duty(LEDC_MODE, LEDC_CHANNEL);
```
To change the duty cycle you need to calculate the duty range according to the duty resolution.
If duty resolution is 13 bits:
Duty range: `0 to (2 ** 13) - 1 = 8191` where 0 is 0% and 8191 is 100%.
### Build and Flash
* [ESP-IDF Getting Started Guide](https://idf.espressif.com/)
Build the project and flash it to the board, then run monitor tool to view serial output:
```bash
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
Running this example, you will see the PWM signal with a duty cycle of 50%.
![PWM](image/ledc_pwm_signal.png)
## Troubleshooting
* Duty Resolution
* If you get the following error log `ledc: requested frequency and duty resolution can not be achieved, try reducing freq_hz or duty_resolution.` you need to change the `LEDC_DUTY_RES` to a lower resolution and change the range of the duty.
* Programming fail
* Hardware connection is not correct: run `idf.py -p PORT monitor`, and reboot your board to see if there are 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.
set(MAIN_SRCS ledc_basic_example_main.c gpio.c)
idf_component_register(SRCS ${MAIN_SRCS}
INCLUDE_DIRS ".")
#
# Main Makefile. This is basically the same as a component makefile.
#
/******************************************************************************
Copyright (C), 2023-2024, Ortur Intelligent Technologies Co., Ltd.
******************************************************************************
File Name : gpio.c
Version : Initial Draft
Author : ZhengJH
Created : 2024/2/21
Last Modified :
Description : for gpio
Function List :
History :
1.Date : 2024/2/21
Author : ZhengJH
Modification: Created file
******************************************************************************/
/*----------------------------------------------*
* head files *
*----------------------------------------------*/
#include "driver/ledc.h"
#include "esp_err.h"
#include "hwport.h"
/*----------------------------------------------*
* external variables *
*----------------------------------------------*/
/*----------------------------------------------*
* external routine prototypes *
*----------------------------------------------*/
/*----------------------------------------------*
* internal routine prototypes *
*----------------------------------------------*/
/*----------------------------------------------*
* module-wide global variables *
*----------------------------------------------*/
/*----------------------------------------------*
* module-wide global variables *
*----------------------------------------------*/
/*----------------------------------------------*
* constants *
*----------------------------------------------*/
/*----------------------------------------------*
* macros *
*----------------------------------------------*/
/*----------------------------------------------*
* routines' implementations *
*----------------------------------------------*/
void gpio_init(void)
{
// gpio_set_intr_type(GPIO_NUM_0, GPIO_INTR_LOW_LEVEL);
// gpio_set_level(GPIO_NUM_0, 1);
// gpio_install_isr_service(0);
// gpio_isr_handler_add(GPIO_NUM_0, intrHandler, (void*)GPIO_NUM_0);
//step
// gpio_reset_pin(STEP_CTRL);
// /* Set the GPIO as a push/pull output */
// gpio_set_direction(STEP_CTRL, GPIO_MODE_OUTPUT);
// gpio_set_level(STEP_CTRL, 0);
//zero-initialize the config structure.
gpio_config_t io_conf = {};
//disable interrupt
io_conf.intr_type = GPIO_INTR_DISABLE;
//set as output mode
io_conf.mode = GPIO_MODE_INPUT;
//bit mask of the pins that you want to set,e.g.GPIO18/19
io_conf.pin_bit_mask = (1 << DIAG_IN);
//disable pull-down mode
io_conf.pull_down_en = 0;
//disable pull-up mode
io_conf.pull_up_en = 1;
//configure GPIO with the given settings
gpio_config(&io_conf);
//dir
gpio_reset_pin(DIR_CTRL);
/* Set the GPIO as a push/pull output */
gpio_set_direction(DIR_CTRL, GPIO_MODE_OUTPUT);
gpio_set_level(DIR_CTRL, 1);
//pdn
gpio_reset_pin(PDN_CTRL);
/* Set the GPIO as a push/pull output */
gpio_set_direction(PDN_CTRL, GPIO_MODE_OUTPUT);
gpio_set_level(PDN_CTRL, 1);
//pow
gpio_reset_pin(POW_CTRL);
/* Set the GPIO as a push/pull output */
gpio_set_direction(POW_CTRL, GPIO_MODE_OUTPUT);
gpio_set_level(POW_CTRL, 1);
//enn
gpio_reset_pin(ENN_CTRL);
/* Set the GPIO as a push/pull output */
gpio_set_direction(ENN_CTRL, GPIO_MODE_OUTPUT);
gpio_set_level(ENN_CTRL, 1);
}
/******************************************************************************
Copyright (C), 2023-2024, Ortur Intelligent Technologies Co., Ltd.
******************************************************************************
File Name : gpio.h
Version : Initial Draft
Author : ZhengJH
Created : 2024/2/21
Last Modified :
Description : gpio.c header file
Function List :
History :
1.Date : 2024/2/21
Author : ZhengJH
Modification: Created file
******************************************************************************/
/*----------------------------------------------*
* head files *
*----------------------------------------------*/
/*----------------------------------------------*
* external variables *
*----------------------------------------------*/
/*----------------------------------------------*
* external routine prototypes *
*----------------------------------------------*/
/*----------------------------------------------*
* internal routine prototypes *
*----------------------------------------------*/
/*----------------------------------------------*
* module-wide global variables *
*----------------------------------------------*/
/*----------------------------------------------*
* module-wide global variables *
*----------------------------------------------*/
/*----------------------------------------------*
* constants *
*----------------------------------------------*/
/*----------------------------------------------*
* macros *
*----------------------------------------------*/
/*----------------------------------------------*
* routines' implementations *
*----------------------------------------------*/
#ifndef __GPIO_H__
#define __GPIO_H__
#ifdef __cplusplus
#if __cplusplus
extern "C"{
#endif
#endif /* __cplusplus */
extern void gpio_init(void);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif /* __cplusplus */
#endif /* __GPIO_H__ */
/******************************************************************************
Copyright (C), 2023-2024, Ortur Intelligent Technologies Co., Ltd.
******************************************************************************
File Name : hwport.h
Version : Initial Draft
Author : ZhengJH
Created : 2024/2/21
Last Modified :
Description : hardward ralated porting
Function List :
History :
1.Date : 2024/2/21
Author : ZhengJH
Modification: Created file
******************************************************************************/
/*----------------------------------------------*
* head files *
*----------------------------------------------*/
/*----------------------------------------------*
* external variables *
*----------------------------------------------*/
/*----------------------------------------------*
* external routine prototypes *
*----------------------------------------------*/
/*----------------------------------------------*
* internal routine prototypes *
*----------------------------------------------*/
/*----------------------------------------------*
* module-wide global variables *
*----------------------------------------------*/
/*----------------------------------------------*
* module-wide global variables *
*----------------------------------------------*/
/*----------------------------------------------*
* constants *
*----------------------------------------------*/
/*----------------------------------------------*
* macros *
*----------------------------------------------*/
/*----------------------------------------------*
* routines' implementations *
*----------------------------------------------*/
#define STEP_CTRL GPIO_NUM_21
#define ENN_CTRL GPIO_NUM_4 //xyz_en, 步进电机控制芯片使能
#define PDN_CTRL GPIO_NUM_10
#define POW_CTRL GPIO_NUM_14
#define DIR_CTRL GPIO_NUM_7
#define DIAG_IN GPIO_NUM_0
#define LEDC_TIMER LEDC_TIMER_0
#define LEDC_MODE LEDC_LOW_SPEED_MODE
#define LEDC_OUTPUT_IO (STEP_CTRL) // Define the output GPIO
#define LEDC_CHANNEL LEDC_CHANNEL_0
#define LEDC_DUTY_RES LEDC_TIMER_2_BIT // Set duty resolution to 13 bits
#define LEDC_DUTY (2) // Set duty to 50%. ((2 ** 13) - 1) * 50% = 4095
#define LEDC_FREQUENCY (500) // Frequency in Hertz. Set frequency at 5 kHz
/* LEDC (LED Controller) basic 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 "driver/ledc.h"
#include "esp_err.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "hwport.h"
static void gpio_task_example(void* arg)
{
uint8_t diag;
for(;;) {
diag = gpio_get_level(DIAG_IN);
printf("GPIO[%d] intr, val: %d\n", DIAG_IN, diag);
if(!diag)
gpio_set_level(POW_CTRL, 0);
vTaskDelay(200 / portTICK_PERIOD_MS);
}
}
void app_main(void)
{
gpio_init();
//test
vTaskDelay(1000 / portTICK_PERIOD_MS);
xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 0, NULL);
while(1)
{
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
/******************************************************************************
Copyright (C), 2023-2024, Ortur Intelligent Technologies Co., Ltd.
******************************************************************************
File Name : ledc_pwm
Version : Initial Draft
Author : ZhengJH
Created : 2024/2/21
Last Modified :
Description : pwm used ledc
Function List :
History :
1.Date : 2024/2/21
Author : ZhengJH
Modification: Created file
******************************************************************************/
/*----------------------------------------------*
* head files *
*----------------------------------------------*/
#include "hwport.h"
/*----------------------------------------------*
* external variables *
*----------------------------------------------*/
/*----------------------------------------------*
* external routine prototypes *
*----------------------------------------------*/
/*----------------------------------------------*
* internal routine prototypes *
*----------------------------------------------*/
/*----------------------------------------------*
* module-wide global variables *
*----------------------------------------------*/
/*----------------------------------------------*
* module-wide global variables *
*----------------------------------------------*/
/*----------------------------------------------*
* constants *
*----------------------------------------------*/
/*----------------------------------------------*
* macros *
*----------------------------------------------*/
/*----------------------------------------------*
* routines' implementations *
*----------------------------------------------*/
static void example_ledc_init(void)
{
// Prepare and then apply the LEDC PWM timer configuration
ledc_timer_config_t ledc_timer = {
.speed_mode = LEDC_MODE,
.timer_num = LEDC_TIMER,
.duty_resolution = LEDC_DUTY_RES,
.freq_hz = LEDC_FREQUENCY, // Set output frequency at 5 kHz
.clk_cfg = LEDC_AUTO_CLK
};
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
// Prepare and then apply the LEDC PWM channel configuration
ledc_channel_config_t ledc_channel = {
.speed_mode = LEDC_MODE,
.channel = LEDC_CHANNEL,
.timer_sel = LEDC_TIMER,
.intr_type = LEDC_INTR_DISABLE,
.gpio_num = LEDC_OUTPUT_IO,
.duty = 0, // Set duty to 0%
.hpoint = 0
};
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
}
void gc_run(void)
{
static uint8_t gc_dir = 1;
gpio_set_level(DIR_CTRL, gc_dir);
gpio_set_level(ENN_CTRL, 0);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 500));
vTaskDelay(50 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 1000));
vTaskDelay(50 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 2000));
vTaskDelay(50 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 4000));
vTaskDelay(50 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 8000));
vTaskDelay(50 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 10000));
vTaskDelay(50 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 12000));
vTaskDelay(50 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 14000));
vTaskDelay(50 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 16000));
vTaskDelay(680 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 14000));
vTaskDelay(50 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 12000));
vTaskDelay(50 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 10000));
vTaskDelay(50 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 8000));
vTaskDelay(50 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 4000));
vTaskDelay(50 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 2000));
vTaskDelay(50 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 1000));
vTaskDelay(50 / portTICK_PERIOD_MS);
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 500));
vTaskDelay(50 / portTICK_PERIOD_MS);
gpio_set_level(ENN_CTRL, 1);
gc_dir = !gc_dir;
vTaskDelay(500 / portTICK_PERIOD_MS);
}
void ledc_pwm_test(void)
{
// Set the LEDC peripheral configuration
example_ledc_init();
// Set duty to 50%
ESP_ERROR_CHECK(ledc_set_duty(LEDC_MODE, LEDC_CHANNEL, LEDC_DUTY));
// Update duty to apply the new value
ESP_ERROR_CHECK(ledc_update_duty(LEDC_MODE, LEDC_CHANNEL));
ESP_ERROR_CHECK(ledc_set_freq(LEDC_MODE, LEDC_TIMER, 1600));
gpio_set_level(ENN_CTRL, 0); // TMC2209 enable pin, low is enable
gc_run();
gpio_set_level(ENN_CTRL, 1);
ESP_ERROR_CHECK(ledc_stop(LEDC_MODE, LEDC_CHANNEL, 0));
}
/******************************************************************************
Copyright (C), 2023-2024, Ortur Intelligent Technologies Co., Ltd.
******************************************************************************
File Name : usb_acm.c
Version : Initial Draft
Author : ZhengJH
Created : 2024/2/21
Last Modified :
Description : usb acm
Function List :
History :
1.Date : 2024/2/21
Author : ZhengJH
Modification: Created file
******************************************************************************/
/*----------------------------------------------*
* head files *
*----------------------------------------------*/
#include "tinyusb.h"
#include "tusb_cdc_acm.h"
#include "tusb_console.h"
/*----------------------------------------------*
* external variables *
*----------------------------------------------*/
/*----------------------------------------------*
* external routine prototypes *
*----------------------------------------------*/
/*----------------------------------------------*
* internal routine prototypes *
*----------------------------------------------*/
/*----------------------------------------------*
* module-wide global variables *
*----------------------------------------------*/
/*----------------------------------------------*
* module-wide global variables *
*----------------------------------------------*/
/*----------------------------------------------*
* constants *
*----------------------------------------------*/
/*----------------------------------------------*
* macros *
*----------------------------------------------*/
/*----------------------------------------------*
* routines' implementations *
*----------------------------------------------*/
void usb_log_init(void)
{
/* Setting TinyUSB up */
tinyusb_config_t tusb_cfg = { 0 }; // the configuration uses default values
ESP_ERROR_CHECK(tinyusb_driver_install(&tusb_cfg));
tinyusb_config_cdcacm_t acm_cfg = { 0 }; // the configuration uses default values
ESP_ERROR_CHECK(tusb_cdc_acm_init(&acm_cfg));
esp_tusb_init_console(TINYUSB_CDC_ACM_0); // log to usb
//esp_tusb_deinit_console(TINYUSB_CDC_ACM_0); // log to uart
}
This diff is collapsed. Click to expand it.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment