Merge branch 'feature/refactor_soft_pwm_driver' into 'master'

refactor(pwm):  Refactor pwm driver for esp8266 idf

See merge request sdk/ESP8266_RTOS_SDK!267
This commit is contained in:
Wu Jian Gang
2018-10-19 16:49:24 +08:00
8 changed files with 1011 additions and 13 deletions

View File

@ -1,16 +1,12 @@
// Copyright 2018-2025 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.
/* gpio 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>

View 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 := pwm
include $(IDF_PATH)/make/project.mk

View File

@ -0,0 +1,72 @@
# _PWM Example_
* This example will show you how to use PWM module by running four channels:
* Observe PWM signal with logic analyzer or oscilloscope.
## Pin assignment
* GPIO12 is assigned as the PWM channel 0.
* GPIO13 is assigned as the PWM channel 1.
* GPIO14 is assigned as the PWM channel 2.
* GPIO15 is assigned as the PWM channel 3.
## How to use example
### Hardware Required
* Connection:
* Connect the PWM channel to a logic analyzer or oscilloscope.
### Configure the project
```
make 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:
```
make -j4 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
* LOG:
```
I (220) gpio: GPIO[12]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (225) gpio: GPIO[13]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (247) gpio: GPIO[14]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (251) gpio: GPIO[15]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
I (265) pwm: --- PWM v3.0
I (20276) main: PWM stop
I (30276) main: PWM re-start
I (50276) main: PWM stop
I (60279) main: PWM re-start
I (80279) main: PWM stop
I (90279) main: PWM re-start
I (110272) main: PWM stop
I (120272) main: PWM re-start
```
* WAVE FORM:
![wave](wave.png)

View File

@ -0,0 +1,3 @@
#
# Main Makefile. This is basically the same as a component makefile.
#

View File

@ -0,0 +1,80 @@
/* pwm 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 <stdlib.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_log.h"
#include "esp_system.h"
#include "esp_err.h"
#include "esp8266/gpio_register.h"
#include "esp8266/pin_mux_register.h"
#include "driver/pwm.h"
#define PWM_0_OUT_IO_NUM 12
#define PWM_1_OUT_IO_NUM 13
#define PWM_2_OUT_IO_NUM 14
#define PWM_3_OUT_IO_NUM 15
// PWM period 500us(2Khz), same as depth
#define PWM_PERIOD (500)
static const char *TAG = "pwm_example";
// pwm pin number
const uint32_t pin_num[4] = {
PWM_0_OUT_IO_NUM,
PWM_1_OUT_IO_NUM,
PWM_2_OUT_IO_NUM,
PWM_3_OUT_IO_NUM
};
// dutys table, (duty/PERIOD)*depth
uint32_t duties[4] = {
250, 250, 250, 250,
};
// phase table, (phase/180)*depth
int16_t phase[4] = {
0, 0, 50, -50,
};
void app_main()
{
pwm_init(PWM_PERIOD, duties, 4, pin_num);
pwm_set_channel_invert(0x1 << 0);
pwm_set_phases(phase);
pwm_start();
int16_t count = 0;
while (1) {
if (count == 20) {
//channel0, 1 output hight level.
//channel2, 3 output low level.
pwm_stop(0x3);
ESP_LOGI(TAG, "PWM stop\n");
} else if (count == 30) {
pwm_start();
ESP_LOGI(TAG, "PWM re-start\n");
count = 0;
}
count++;
vTaskDelay(1000 / portTICK_RATE_MS);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB