Initial HW PWM (Delta-Sigma) (#609)

This commit is contained in:
Zaltora 2018-05-19 23:47:29 +02:00 committed by Ruslan V. Uss
parent 10f361c8ca
commit 4dc7d825bc
6 changed files with 238 additions and 11 deletions

9
extras/dsm/component.mk Normal file
View file

@ -0,0 +1,9 @@
# Component makefile for extras/dsm
INC_DIRS += $(ROOT)extras/dsm
# args for passing into compile rule generation
extras/dsm_INC_DIR = $(ROOT)extras/dsm
extras/dsm_SRC_DIR = $(ROOT)extras/dsm
$(eval $(call component_compile_rules,extras/dsm))

111
extras/dsm/dsm.c Normal file
View file

@ -0,0 +1,111 @@
/* Implementation of Delta-Sigma modulator 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 "dsm.h"
#include <espressif/esp_common.h>
#include <esp8266.h>
#if (DSM_DEBUG)
#define debug(fmt, ...) printf("%s: " fmt "\n", "DSM", ## __VA_ARGS__)
#else
#define debug(fmt, ...)
#endif
typedef struct dsmInfoDefinition
{
uint8_t running;
uint8_t preScale;
uint8_t target;
bool output;
/* private */
uint8_t usedPins;
uint8_t pins[8];
} DSMInfo;
static DSMInfo dsmInfo;
void dsm_init(uint8_t npins, const uint8_t* pins)
{
/* Assert number of pins is correct */
if (npins > MAX_DSM_PINS)
{
debug("Incorrect number of DSM pins (%d)\n", npins);
return;
}
/* Save pins information */
dsmInfo.usedPins = npins;
for (uint8_t i = 0 ; i < npins; ++i)
{
dsmInfo.pins[i] = pins[i];
/* configure GPIOs */
gpio_enable(pins[i], GPIO_OUTPUT);
}
/* Set output to LOW */
dsm_stop();
/* Flag not running */
dsmInfo.running = 0;
}
void dsm_set_prescale(uint8_t prescale)
{
//TODO: Add a freq/prescale converter
dsmInfo.preScale = prescale;
debug("Set Prescale: %u",dsmInfo.preScale);
}
void dsm_set_target(uint8_t target)
{
dsmInfo.target = target;
if (target == 0 || target == UINT8_MAX)
{
dsmInfo.output = (target == UINT8_MAX);
}
debug("Duty set at %u",dsmInfo.target);
if (dsmInfo.running)
{
dsm_start();
}
}
void dsm_start()
{
if (dsmInfo.target > 0 && dsmInfo.target < UINT8_MAX)
{
for (uint8_t i = 0; i < dsmInfo.usedPins; ++i)
{
SET_MASK_BITS(GPIO.CONF[dsmInfo.pins[i]], GPIO_CONF_SOURCE_DSM);
}
GPIO.DSM = GPIO_DSM_ENABLE | (dsmInfo.preScale << 8) | dsmInfo.target;
}
else
{
for (uint8_t i = 0; i < dsmInfo.usedPins; ++i)
{
gpio_write(dsmInfo.pins[i], dsmInfo.output );
}
}
debug("start");
dsmInfo.running = 1;
}
void dsm_stop()
{
for (uint8_t i = 0; i < dsmInfo.usedPins; ++i)
{
CLEAR_MASK_BITS(GPIO.CONF[dsmInfo.pins[i]], GPIO_CONF_SOURCE_DSM);
gpio_write(dsmInfo.pins[i], false);
}
debug("stop");
dsmInfo.running = 0;
}

36
extras/dsm/dsm.h Normal file
View file

@ -0,0 +1,36 @@
/* Implementation of Delta-Sigma modulator 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_DSM_H_
#define EXTRAS_DSM_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#define MAX_DSM_PINS (8)
#define DSM_DEBUG (0)
/*
* Freq = (80,000,000/prescale) * (target / 256) HZ (0 < target < 128)
* Freq = (80,000,000/prescale) * ((256 - target) / 256) HZ (128 < target < 256)
*/
void dsm_init(uint8_t npins, const uint8_t* pins);
void dsm_set_prescale(uint8_t prescale);
void dsm_set_target(uint8_t target);
void dsm_start();
void dsm_stop();
#ifdef __cplusplus
}
#endif
#endif /* EXTRAS_DSM_H_ */