Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
154 changes: 154 additions & 0 deletions cores/esp8266/core_esp8266_sigma_delta.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
/******************************************************************************
* Sigma delta module

This module controls the esp8266 internal sigma delta source
Each pin can be connected to the sigma delta source
The target duty and frequency can be modified via the register GPIO_SIGMA_DELTA

THE TARGET FREQUENCY IS DEFINED AS:

FREQ = 80,000,000/prescaler * target /256 HZ, 0<target<128
FREQ = 80,000,000/prescaler * (256-target) /256 HZ, 128<target<256
target: duty ,0-255
prescaler: clk_div,0-255
so the target and prescale will both affect the freq.

Usage :
1. sigma_delta_enable() : activate the sigma delta source with default prescalar (0) & target (0)
2. sigma_delta_attachPin(pin), any pin 0..15, TBC if gpio16 supports sigma-delta source
This will set the pin to NORMAL output mode (pinMode(pin,OUTPUT))
3. sigma_delta_setPrescaler(uint8_t) : reduce the output frequencies
4. sigma_delta_setTarget(uint8_t) : set the output signal duty cycle, duty cycle = target/256

5. sigma_delta_detachPin(pin), this will revert the pin to NORMAL output mode & GPIO source.
The sigma delta source remains on until :
6. sigma_delta_disable()

*******************************************************************************/

#include "Arduino.h" // using pinMode
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs standard licence header, please.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok done


// definitions in esp8266_peri.h style
#define GPSD ESP8266_REG(0x368) // GPIO_SIGMA_DELTA register @ 0x600000368
#define GPSDT 0 // target, 8 bits
#define GPSDP 8 // prescaler, 8 bits
#define GPSDE 16 // enable

/******************************************************************************
* FunctionName : sigma_delta_start
* Description : enable the internal sigma delta source
* Parameters : none
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR sigma_delta_enable()
{
GPSD = (0 << GPSDT) | (0 << GPSDP) | (1 << GPSDE); //SIGMA_DELTA_TARGET(0) | SIGMA_DELTA_PRESCALER(0) | SIGMA_DELTA_ENABLE(ENABLED)
}

/******************************************************************************
* FunctionName : sigma_delta_disable
* Description : stop the internal sigma delta source
* Parameters : none
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR sigma_delta_disable()
{
GPSD = (0 << GPSDT) | (0 << GPSDP) | (0 << GPSDE); //SIGMA_DELTA_TARGET(0) | SIGMA_DELTA_PRESCALER(0) | SIGMA_DELTA_ENABLE(DISABLED)
}

/******************************************************************************
* FunctionName : sigma_delta_attachPin
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some FunctionName comments got updated to camelCase, but some like this did not.

* Description : connects the sigma delta source to a physical output pin
* Parameters : pin (0..15)
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR sigma_delta_attachPin(uint8_t pin)
{
// make the chosen pin an output pin
pinMode (pin, OUTPUT);
if (pin < 16) {
// set its source to the sigma delta source
GPC(pin) |= (1 << GPCS); //SOURCE 0:GPIO_DATA,1:SigmaDelta
}
}

/******************************************************************************
* FunctionName : sigma_delta_detachPin
* Description : disconnects the sigma delta source from a physical output pin
* Parameters : pin (0..16)
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR sigma_delta_detachPin(uint8_t pin)
{
if (pin < 16) {
// set its source to the sigma delta source
GPC(pin) &= ~(1 << GPCS); //SOURCE 0:GPIO_DATA,1:SigmaDelta
}
}

/******************************************************************************
* FunctionName : sigma_delta_isPinAttached
* Description : query if pin is attached
* Parameters : pin (0..16)
* Returns : bool
*******************************************************************************/
bool ICACHE_FLASH_ATTR sigma_delta_isPinAttached(uint8_t pin)
{
if (pin < 16) {
// set its source to the sigma delta source
return (GPC(pin) & (1 << GPCS)); //SOURCE 0:GPIO_DATA,1:SigmaDelta
}
else
return false;
}

/******************************************************************************
* FunctionName : sigma_delta_getTarget
* Description : get the target value from the GPIO_SIGMA_DELTA register
* Parameters : none
* Returns : uint8_t target value 0..255
*******************************************************************************/
uint8_t ICACHE_FLASH_ATTR sigma_delta_getTarget(void)
{
return (uint8_t)((GPSD >> GPSDT) & 0xFF);
}

/******************************************************************************
* FunctionName : sigma_delta_setTarget
* Description : set the target (duty cycle) for the sigma-delta source
* Parameters : uint8 target, 0-255, duty cycle = target/256
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR sigma_delta_setTarget(uint8_t target)
{
uint32_t reg = GPSD;

reg = (reg & ~(0xFF << GPSDT)) | ((target & 0xFF) << GPSDT);
GPSD = reg;
}

/******************************************************************************
* FunctionName : sigma_delta_getPrescaler
* Description : get the prescaler value from the GPIO_SIGMA_DELTA register
* Parameters : none
* Returns : uint8 prescaler, CLK_DIV , 0-255
*******************************************************************************/
uint8_t ICACHE_FLASH_ATTR sigma_delta_getPrescaler(uint8_t prescaler)
{
return (uint8_t)((GPSD >> GPSDP) & 0xFF);
}

/******************************************************************************
* FunctionName : sigma_delta_setPrescaler
* Description : set the clock divider for the sigma-delta source
* Parameters : uint8 prescaler, 0-255, divides the 80MHz base clock by this amount
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR sigma_delta_setPrescaler(uint8_t prescaler)
{
uint32_t reg = GPSD;

reg = (reg & ~(0xFF << GPSDP)) | ((prescaler & 0xFF) << GPSDP);
GPSD = reg;
}
107 changes: 102 additions & 5 deletions cores/esp8266/sigma_delta.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,112 @@
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

/*
/******************************************************************************
* Info Sigma delta module
This module controls the esp8266 internal sigma delta source
Each pin can be connected to the sigma delta source
The target duty and frequency can be modified via the register GPIO_SIGMA_DELTA
THE TARGET FREQUENCY IS DEFINED AS:
FREQ = 80,000,000/prescaler * target /256 HZ, 0<target<128
FREQ = 80,000,000/prescaler * (256-target) /256 HZ, 128<target<256
target: duty ,0-255
prescaler: clk_div,0-255
so the target and prescale will both affect the freq.
Usage :
1. sigma_delta_enable() : activate the sigma delta source with default prescalar (0) & target (0)
2. sigma_delta_attachPin(pin), any pin 0..15, TBC if gpio16 supports sigma-delta source
This will set the pin to NORMAL output mode (pinMode(pin,OUTPUT))
3. sigma_delta_setPrescaler(uint8_t) : reduce the output frequencies
4. sigma_delta_setTarget(uint8_t) : set the output signal duty cycle, duty cycle = target/256
5. sigma_delta_detachPin(pin), this will revert the pin to NORMAL output mode & GPIO source.
The sigma delta source remains on until :
6. sigma_delta_disable()
copy this code as example :
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This example should be made into a working example that builds with the rest of the core, so that at least build issues can be tracked.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea. The example would then be under libraries\esp8266\examples ?

#include "sigma_delta.h"
void setup() {
Serial.begin(115200);
pinMode(BUILTIN_LED,OUTPUT); // blinkie & sigma-delta mix
uint32 pin = 4; //D2
sigma_delta_enable();
//sigma_delta_disable();
sigma_delta_attachPin(BUILTIN_LED);
sigma_delta_attachPin(4); // D2
sigma_delta_setPrescaler(255);
Serial.println();
Serial.println("Start Sigma Delta Example\n");
Serial.println("Attached gpio4 to the sigma delta source\n");
Serial.printf("Current target = %i, prescaler = %i\n",sigma_delta_getTarget(),sigma_delta_getPrescaler());
}
void loop() {
uint8_t target, iRepeat;
Serial.println("Attaching the built in led to the sigma delta source now\n");
Serial.printf("Current target = %i, prescaler = %i\n",sigma_delta_getTarget(),sigma_delta_getPrescaler());
sigma_delta_attachPin(BUILTIN_LED);
Serial.println("dimming builtin led...\n");
for (iRepeat=0;iRepeat<10;iRepeat++)
{
for (target=0; target<255;target=target+5)
{
sigma_delta_setTarget(target);
delay(10);
}
for (target=255; target>0;target=target-5)
{
sigma_delta_setTarget(target);
delay(10);
}
}
Serial.println("detaching builtin led & playing a blinkie\n");
sigma_delta_detachPin(BUILTIN_LED);
for (iRepeat=0;iRepeat<20;iRepeat++)
{
digitalWrite(BUILTIN_LED,!digitalRead(BUILTIN_LED));
delay(500);
}
}
*******************************************************************************/

#ifndef SIGMA_DELTA_H
#define SIGMA_DELTA_H

#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif

void sigma_delta_enable(void);
void sigma_delta_disable(void);
void sigma_delta_attachPin(uint8_t pin);
void sigma_delta_detachPin(uint8_t pin);
bool sigma_delta_isPinAttached(uint8_t pin);
uint8_t sigma_delta_getTarget(void);
void sigma_delta_setTarget(uint8_t target);
uint8_t sigma_delta_getPrescaler(void);
void sigma_delta_setPrescaler(uint8_t prescaler);

void sigma_delta_close(uint32_t gpio);
void set_sigma_target(uint8_t target);
void set_sigma_prescale(uint8_t prescale);
void set_sigma_duty_312KHz(uint8_t duty);
#ifdef __cplusplus
}
#endif

#endif//SIGMA_DELTA_H
84 changes: 84 additions & 0 deletions libraries/esp8266/examples/SigmaDeltaDemo/SigmaDeltaDemo.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
Sigma Delta Generator example
This example demonstrates the use of the ESP8266 internal hardware sigma delta source.
Each GPIO pin or multiple pins can be connected to the one sigma delta source.
THE sigma delta output frequency is defined by the ESP8266 hardware as:
FREQ = 80,000,000/prescaler * target /256 HZ, 0<target<128
FREQ = 80,000,000/prescaler * (256-target) /256 HZ, 128<target<256
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these constant even in 2x mode where CPU_F = 160,000,000?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, i never tested 160MHz. Will verify and correct accordingly!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked : CPU frequency has no influence on the SD frequencies

with :
target: 0-255, defines the 8-bit duty cycle of the output signal, , duty cycle = target/256
prescaler: 0-255, reduce the output frequencies
so both the target and prescaler will affect the freq.
Differences with Arduino library analogWrite :
- the arduino analogWrite pwm uses a hardware timer and ISR to generate the pwm signal. The sigma delta pwm doesn't need a hardware timer.
The FRC1 timer can then be used for other purposes.
- the arduino analogWrite pwm produces a more or less fixed frequency around 900Hz. The sigma delta source produces a variable output frequency,
that depends on the duty cycle. Example, with prescaler = 10, the output frequency varies between 31,kHz and 4MHz
- the sigma delta source produces much higher frequencies. This allows the output to be easily smoothed by a RC-filter and realise a decent 8-bit DAC
- the arduino analogWrite pwm has a higher resolution, default 12-bit, and up to 16-bit resolution. The sigma delta source has only 8-bit resolution
The example doesn't require additional hardware.
With an RC-filter (for example R=17k & C=100nF), a 'DC' output signal can be obtained with voltage between 0 and 3.3V
For higher value capacitors, it might be necessary to add a high-speed buffer between the esp8266 output and RC-filter to limit the current ac load on the
pin output driver.
*/
Copy link
Collaborator

@earlephilhower earlephilhower Jan 15, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add attribution and license (CC0 or PD) to the example header?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add attribution and license (CC0 or PD) to the example header?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PD i guess?

#include "sigma_delta.h"

void setup() {

Serial.begin(115200);
pinMode(BUILTIN_LED,OUTPUT); // blinkie & sigma-delta mix

uint32 pin = 4; //D2
sigma_delta_enable();
//sigma_delta_disable();
sigma_delta_attachPin(BUILTIN_LED);
sigma_delta_attachPin(4); // D2

sigma_delta_setPrescaler(255);

Serial.println();
Serial.println("Start Sigma Delta Example\n");
Serial.println("Attached gpio4 to the sigma delta source\n");
Serial.printf("Current target = %i, prescaler = %i\n",sigma_delta_getTarget(),sigma_delta_getPrescaler());
}

void loop() {

uint8_t target, iRepeat;

Serial.println("Attaching the built in led to the sigma delta source now\n");
Serial.printf("Current target = %i, prescaler = %i\n",sigma_delta_getTarget(),sigma_delta_getPrescaler());
sigma_delta_attachPin(BUILTIN_LED);

Serial.println("dimming builtin led...\n");
for (iRepeat=0;iRepeat<10;iRepeat++)
{
for (target=0; target<255;target=target+5)
{
sigma_delta_setTarget(target);
delay(10);
}

for (target=255; target>0;target=target-5)
{
sigma_delta_setTarget(target);
delay(10);
}

}
Serial.println("detaching builtin led & playing a blinkie\n");
sigma_delta_detachPin(BUILTIN_LED);
for (iRepeat=0;iRepeat<20;iRepeat++)
{
digitalWrite(BUILTIN_LED,!digitalRead(BUILTIN_LED));
delay(500);
}

}
Loading