Skip to content

Commit

Permalink
2020.3.31 add pwm driver utils
Browse files Browse the repository at this point in the history
2020.3.31 add pwm driver utils
  • Loading branch information
xuhongv committed Mar 30, 2020
1 parent 583de5e commit 5cc0cbc
Show file tree
Hide file tree
Showing 18 changed files with 2,865 additions and 0 deletions.
7 changes: 7 additions & 0 deletions 17_light_utils_pwm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# 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)

set(SUPPORTED_TARGETS esp32)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(mcpwm_brushed_dc_control)
9 changes: 9 additions & 0 deletions 17_light_utils_pwm/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
# project subdirectory.
#

PROJECT_NAME := mcpwm_brushed_dc_control

include $(IDF_PATH)/make/project.mk

10 changes: 10 additions & 0 deletions 17_light_utils_pwm/components/light_driver/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

set(COMPONENT_SRCS "light_driver.c"
"iot_led.c")

set(COMPONENT_ADD_INCLUDEDIRS ". include")

# requirements can't depend on config
set(COMPONENT_REQUIRES mcommon)

register_component()
22 changes: 22 additions & 0 deletions 17_light_utils_pwm/components/light_driver/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Component: Light

* This component defines a light as a well encapsulated object.
* A light device is defined by:
* ledc timer which is used to control the pwm channels of light
* mode of the ledc timer
* frequency of the ledc timer
* pwm channel number of the light
* bit number of the ledc timer
* A light device can provide:
* iot_light_channel_regist function to add channel to corresponding channel id
* iot_light_duty_write function to set the duty of corresponding channel and it support setting duty directly or gradually
* iot_light_breath_write function to set the corresponding channel to breath mode and breath period can be set
* iot_light_blink_starte and iot_light_blink_stop function to make some of channels to blink in appointed period. Note that if any channel works in blink mode, all the other channels would be turned off.

* To use the light device, you need to:
* create a light object returned by iot_light_create()
* regist the light channels according the channel number by iot_light_channel_regist()
* To free the object, you can call iot_light_delete to delete the button object and free the memory.

### NOTE:
> If any channel(s) work(s) in blink mode, all the other channels would be turned off. iot_light_blink_stop() must be called before setting any channel to other mode(write duty or breath).
4 changes: 4 additions & 0 deletions 17_light_utils_pwm/components/light_driver/component.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#
# "main" pseudo-component makefile.
#
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)
194 changes: 194 additions & 0 deletions 17_light_utils_pwm/components/light_driver/include/iot_led.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
// Copyright 2017 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.

#ifndef __IOT_LED_H__
#define __IOT_LED_H__

#ifdef __cplusplus
extern "C" {
#endif


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "errno.h"

#include "esp32/rom/rtc.h"
#include "esp32/rom/crc.h"


#include "freertos/FreeRTOS.h"
#include "freertos/queue.h"
#include "freertos/task.h"
#include "freertos/timers.h"
#include "freertos/event_groups.h"

#include "esp_system.h"
#include "esp_partition.h"
#include "esp_event.h"
#include "esp_http_client.h"

#include "lwip/sockets.h"
#include "lwip/netdb.h"
#include "lwip/sockets.h"
#include "driver/ledc.h"

#include "nvs.h"
#include "nvs_flash.h"
#include "cJSON.h"
#include "driver/i2c.h"
#include "sys/param.h"
#include "driver/gpio.h"

#define HW_TIMER_GROUP (0) /**< Hardware timer group */
#define HW_TIMER_ID (0) /**< Hardware timer number */
#define HW_TIMER_DIVIDER (16) /**< Hardware timer clock divider */
#define HW_TIMER_SCALE (TIMER_BASE_CLK / HW_TIMER_DIVIDER) /**< Convert counter value to seconds */
#define GAMMA_CORRECTION 0.8 /**< Gamma curve parameter */
#define GAMMA_TABLE_SIZE 256 /**< Gamma table size, used for led fade*/
#define DUTY_SET_CYCLE (20) /**< Set duty cycle */

/**
* @brief Initialize and set the ledc timer for the iot led
*
* @param timer_num The timer index of ledc timer group used for iot led
* This parameter can be one of LEDC_TIMER_x where x can be (0 .. 3)
*
* @param speed_mode speed mode of ledc timer
* This parameter can be one of LEDC_x_SPEED_MODE where x can be (LOW, HIGH)
*
* @param freq_hz frequency of ledc timer
* This parameter must be less than 5000
*
* @return
* - MDF_OK if sucess
* - MDF_ERR_INVALID_ARG Parameter error
* - MDF_FAIL Can not find a proper pre-divider number base on the given frequency
* and the current duty_resolution.
*/
esp_err_t iot_led_init(ledc_timer_t timer_num, ledc_mode_t speed_mode, uint32_t freq_hz);

/**
* @brief DeInitializes the iot led and free resource
*
* @return
* - MDF_OK if sucess
*/
esp_err_t iot_led_deinit();

/**
* @brief Set the ledc channel used by iot led and associate the gpio port used
* for output
*
* @param channel The ledc channel
* This parameter can be LEDC_CHANNEL_x where x can be (0 .. 15)
* @param gpio_num the ledc output gpio_num
* This parameter can be GPIO_NUM_x where x can be (0, 33)
*
* @note If the operation of esp32 depends on SPI FLASH or PSRAM, then these related
* pins should not be set to output.
*
* @return
* - MDF_OK if sucess
* - MDF_ERR_INVALID_ARG Parameter error
* - MDF_ERR_NOT_INIT if lot_led_init() is not called yet
*/
esp_err_t iot_led_regist_channel(ledc_channel_t channel, gpio_num_t gpio_num);

/**
* @brief Returns the channel value
* @note before calling this function, you need to call iot_led_regist_channel() to
* set the channel
*
* @param channel The ledc channel
* This parameter can be LEDC_CHANNEL_x where x can be (0 .. 15)
* @param dst The address where the channel value is stored
* @return
* - MDF_OK if sucess
* - MDF_ERR_INVALID_ARG if dst is NULL
* - MDF_ERR_NOT_INIT if lot_led_init() is not called yet
*/
esp_err_t iot_led_get_channel(ledc_channel_t channel, uint8_t* dst);

/**
* @brief Set the fade state for the specified channel
* @note before calling this function, you need to call iot_led_regist_channel() to
* set the channel
*
* @param channel The ledc channel
* This parameter can be LEDC_CHANNEL_x where x can be (0 .. 15)
* @param value The target output brightness of iot led
* This parameter can be (0 .. 255)
* @param fade_ms The time from the current value to the target value
* @return
* - MDF_OK if sucess
* - MDF_ERR_NOT_INIT if lot_led_init() is not called yet
*/
esp_err_t iot_led_set_channel(ledc_channel_t channel, uint8_t value, uint32_t fade_ms);

/**
* @brief Set the blink state or loop fade for the specified channel
* @note before calling this function, you need to call iot_led_regist_channel() to
* set the channel
*
* @param channel The ledc channel
* This parameter can be LEDC_CHANNEL_x where x can be (0 .. 15)
* @param value The output brightness of iot led
* This parameter can be (0 .. 255)
* @param period_ms Blink cycle
* @param fade_flag select loop fade or blink
* 1 for loop fade
* 0 for blink
* @return
* - MDF_OK if sucess
* - MDF_ERR_NOT_INIT if lot_led_init() is not called yet
*/
esp_err_t iot_led_start_blink(ledc_channel_t channel, uint8_t value, uint32_t period_ms, bool fade_flag);

/**
* @brief Stop the blink state or loop fade for the specified channel
*
* @param channel The ledc channel
* This parameter can be LEDC_CHANNEL_x where x can be (0 .. 15)
* @return
* - MDF_OK if sucess
* - MDF_ERR_NOT_INIT if lot_led_init() is not called yet
*/
esp_err_t iot_led_stop_blink(ledc_channel_t channel);

/**
* @brief Set the specified gamma_table to control the fade effect, usually
* no need to set
*
* @param gamma_table[GAMMA_TABLE_SIZE] Expected gamma table value
*
* @note Gamma_table is the dimming curve used by the iot_led driver.
* The element type is uint16_t. Each element is treated as a binary
* fixed-point number. The decimal point is before the eighth bit
* and after the ninth bit, so the range of expressions can be
* 0x00.00 ~ 0xff.ff.
* @note default gamma_table is created in iot_led_init()
*
* @return
* - MDF_OK if sucess
* - MDF_ERR_NOT_INIT if lot_led_init() is not called yet
*/
esp_err_t iot_led_set_gamma_table(const uint16_t gamma_table[GAMMA_TABLE_SIZE]);

#ifdef __cplusplus
}
#endif

#endif /**< __IOT_LED_H__ */
Loading

0 comments on commit 5cc0cbc

Please sign in to comment.