Initial HW PWM (Delta-Sigma)

This commit is contained in:
Zaltora 2018-04-16 14:52:29 +02:00
parent 0fa4213577
commit bdfc9cb6e9
3 changed files with 160 additions and 0 deletions

View file

@ -0,0 +1,9 @@
# Component makefile for private/hw_pwm
INC_DIRS += $(ROOT)private/hw_pwm
# args for passing into compile rule generation
private/hw_pwm_INC_DIR = $(ROOT)private/hw_pwm
private/hw_pwm_SRC_DIR = $(ROOT)private/hw_pwm
$(eval $(call component_compile_rules,private/hw_pwm))

120
extras/hw_pwm/pwm.c Normal file
View file

@ -0,0 +1,120 @@
/* Implementation of HW PWM support.
*
* Part of esp-open-rtos
* Copyright (C) 2018 ourairquality (https://github.com/ourairquality)
* Copyright (C) 2018 Zaltora (https://github.com/Zaltora)
* BSD Licensed as described in the file LICENSE
*/
#include "pwm.h"
#include <espressif/esp_common.h>
#include <esp8266.h>
#ifdef PWM_DEBUG
#define debug(fmt, ...) printf("%s: " fmt "\n", "HW_PWM", ## __VA_ARGS__)
#else
#define debug(fmt, ...)
#endif
typedef struct pwmInfoDefinition
{
uint8_t running;
uint8_t preScale;
uint8_t dutyCycle;
bool output;
/* private */
uint8_t usedPins;
uint8_t pins[8];
} PWMInfo;
static PWMInfo pwmInfo;
void hw_pwm_init(uint8_t npins, const uint8_t* pins)
{
/* Assert number of pins is correct */
if (npins > MAX_PWM_PINS)
{
debug("Incorrect number of PWM pins (%d)\n", npins);
return;
}
/* Save pins information */
pwmInfo.usedPins = npins;
for (uint8_t i = 0 ; i < npins; ++i)
{
pwmInfo.pins[i] = pins[i];
/* configure GPIOs */
gpio_enable(pins[i], GPIO_OUTPUT);
}
/* Set output to LOW */
hw_pwm_stop();
/* Flag not running */
pwmInfo.running = 0;
}
//FIXME: Need Confirmation
// Freq = (80,000,000/prescale) * (target / 256) HZ (0 < target < 128)
// Freq = (80,000,000/prescale) * ((256 - target) / 256) HZ (128 < target < 256)
void hw_pwm_set_prescale(uint8_t prescale)
{
//TODO: Add a freq/prescale converter
pwmInfo.preScale = prescale;
debug("Set Prescale: %u",pwmInfo.preScale);
}
void hw_pwm_set_duty(uint8_t duty)
{
pwmInfo.dutyCycle = duty;
if (duty == 0 || duty == UINT8_MAX)
{
pwmInfo.output = (duty == UINT8_MAX);
}
debug("Duty set at %u",pwmInfo.dutyCycle);
hw_pwm_restart();
}
void hw_pwm_restart()
{
if (pwmInfo.running)
{
hw_pwm_stop();
hw_pwm_start();
}
}
void hw_pwm_start()
{
if (pwmInfo.dutyCycle > 0 && pwmInfo.dutyCycle < UINT8_MAX)
{
for (uint8_t i = 0; i < pwmInfo.usedPins; ++i)
{
SET_MASK_BITS(GPIO.CONF[pwmInfo.pins[i]], GPIO_CONF_SOURCE_PWM);
}
GPIO.PWM = GPIO_PWM_ENABLE | (pwmInfo.preScale << 8) | pwmInfo.dutyCycle;
}
else
{
for (uint8_t i = 0; i < pwmInfo.usedPins; ++i)
{
gpio_write(pwmInfo.pins[i], pwmInfo.output );
}
}
debug("start");
pwmInfo.running = 1;
}
void hw_pwm_stop()
{
for (uint8_t i = 0; i < pwmInfo.usedPins; ++i)
{
CLEAR_MASK_BITS(GPIO.CONF[pwmInfo.pins[i]], GPIO_CONF_SOURCE_PWM);
gpio_write(pwmInfo.pins[i], false);
}
debug("stop");
pwmInfo.running = 0;
}

31
extras/hw_pwm/pwm.h Normal file
View file

@ -0,0 +1,31 @@
/* Implementation of HW PWM support.
*
* Part of esp-open-rtos
* Copyright (C) 2018 ourairquality (https://github.com/ourairquality)
* Copyright (C) 2018 Zaltora (https://github.com/Zaltora)
* BSD Licensed as described in the file LICENSE
*/
#ifndef EXTRAS_HW_PWM_H_
#define EXTRAS_HW_PWM_H_
#include <stdint.h>
#define MAX_PWM_PINS 8
#ifdef __cplusplus
extern "C" {
#endif
void hw_pwm_init(uint8_t npins, const uint8_t* pins);
void hw_pwm_set_prescale(uint8_t prescale);
void hw_pwm_set_duty(uint8_t duty);
void hw_pwm_restart();
void hw_pwm_start();
void hw_pwm_stop();
#ifdef __cplusplus
}
#endif
#endif /* EXTRAS_HW_PWM_H_ */