Skip to content

working code for sigma delta generator #4119

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
174 changes: 174 additions & 0 deletions cores/esp8266/core_esp8266_sigma_delta.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
/*
core_esp8266_sigma_delta.c - sigma delta library for esp8266

Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

#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

void sigmaDeltaSetPrescaler(uint8_t prescaler); // avoids compiler warning

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

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

/******************************************************************************
* FunctionName : sigmaDeltaAttachPin
* Description : connects the sigma delta source to a physical output pin
* Parameters : pin (0..15), channel = unused, for compatibility with ESP32
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR sigmaDeltaAttachPin(uint8_t pin, uint8_t channel)
{
(void) channel;
// 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 : sigmaDeltaDetachPin
* Description : disconnects the sigma delta source from a physical output pin
* Parameters : pin (0..16)
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR sigmaDeltaDetachPin(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 : sigmaDeltaIsPinAttached
* Description : query if pin is attached
* Parameters : pin (0..16)
* Returns : bool
*******************************************************************************/
bool ICACHE_FLASH_ATTR sigmaDeltaIsPinAttached(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 : sigmaDeltaSetup
* Description : start the sigma delta generator with the chosen parameters
* Parameters : channel = unused (for compatibility with ESP32),
* freq : 1220-312500 (lowest frequency in the output signal's spectrum)
* Returns : uint32_t the actual frequency, closest to the input parameter
*******************************************************************************/
uint32_t ICACHE_FLASH_ATTR sigmaDeltaSetup(uint8_t channel, uint32_t freq)
{
(void) channel;

uint32_t prescaler = ((uint32_t)10000000/(freq*32)) - 1;

if(prescaler > 0xFF) {
prescaler = 0xFF;
}
sigmaDeltaEnable();
sigmaDeltaSetPrescaler ((uint8_t) prescaler);

return 10000000/((prescaler + 1) * 32);
}

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

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

}
/******************************************************************************
* FunctionName : sigmaDeltaRead
* Description : get the duty cycle for the sigma-delta source
* Parameters : channel = unused, for compatibility with ESP32
* Returns : uint8_t duty cycle value 0..255
*******************************************************************************/
uint8_t ICACHE_FLASH_ATTR sigmaDeltaRead(uint8_t channel)
{
(void) channel;
return (uint8_t)((GPSD >> GPSDT) & 0xFF);
}

/******************************************************************************
* FunctionName : sigmaDeltaSetPrescaler
* 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 sigmaDeltaSetPrescaler(uint8_t prescaler)
{
uint32_t reg = GPSD;

reg = (reg & ~(0xFF << GPSDP)) | ((prescaler & 0xFF) << GPSDP);
GPSD = reg;
}

/******************************************************************************
* FunctionName : sigmaDeltaGetPrescaler
* 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 sigmaDeltaGetPrescaler(void)
{
return (uint8_t)((GPSD >> GPSDP) & 0xFF);
}
50 changes: 45 additions & 5 deletions cores/esp8266/sigma_delta.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,55 @@
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 cycle,range 0-255
prescaler: is a clock divider, range 0-255
so the target and prescale will both affect the freq.
CPU_FREQ has no influence on the sigma delta frequency.

Usage :
1. sigmaDeltaSetup(0,f) : activate the sigma delta source with frequency f and default duty cycle (0)
2. sigmaDeltaAttachPin(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. sigmaDeltaWrite(0,dc) : set the output signal duty cycle, duty cycle = dc/256

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

#ifndef SIGMA_DELTA_H
#define SIGMA_DELTA_H

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

//channel parameter is unused (only for ESP32 compatibility) freq 1220-312500 duty 0-255
void sigmaDeltaEnable(void);
void sigmaDeltaDisable(void);
uint32_t sigmaDeltaSetup(uint8_t channel, uint32_t freq);
void sigmaDeltaWrite(uint8_t channel, uint8_t duty);
uint8_t sigmaDeltaRead(uint8_t channel = 0);
void sigmaDeltaAttachPin(uint8_t pin, uint8_t channel = 0);
void sigmaDeltaDetachPin(uint8_t pin);
bool sigmaDeltaIsPinAttached(uint8_t pin);

// alternative way to control the sigma delta generator frequency
uint8_t sigmaDeltaGetPrescaler(void);
void sigmaDeltaSetPrescaler(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
47 changes: 47 additions & 0 deletions libraries/esp8266/examples/SigmaDeltaDemo/SigmaDeltaDemo.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* Any copyright is dedicated to the Public Domain. */

#include "sigma_delta.h"

void setup() {

Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT); // blinkie & sigma-delta mix
uint32_t reqFreq = 1000;
uint32_t realFreq;

realFreq = sigmaDeltaSetup(0, reqFreq); // chose a low frequency

Serial.println();
Serial.println("Start Sigma Delta Example\n");
Serial.printf("Frequency = %u\n", realFreq);

}

void loop() {

uint8_t duty, iRepeat;

Serial.println("Attaching the built in led to the sigma delta source now\n");
Serial.printf("Current duty = %i, prescaler = %i\n", sigmaDeltaRead(), sigmaDeltaGetPrescaler());
sigmaDeltaAttachPin(LED_BUILTIN);

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

for (duty = 255; duty > 0; duty = duty - 5) {
sigmaDeltaWrite(0, duty);
delay(10);
}

}
Serial.println("detaching builtin led & playing a blinkie\n");
sigmaDeltaDetachPin(LED_BUILTIN);
for (iRepeat = 0; iRepeat < 20; iRepeat++) {
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
delay(500);
}
}
72 changes: 72 additions & 0 deletions sigma_delta.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
sigma_delta.h - esp8266 sigma-delta source

Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
This file is part of the esp8266 core for Arduino environment.

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
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()

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

#ifndef SIGMA_DELTA_H
#define SIGMA_DELTA_H

#ifdef __cplusplus
Copy link
Collaborator

Choose a reason for hiding this comment

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

The function prototypes here are "extern "C""'d, but the actual function definitions aren't. Any reason not to drop the "extern C" here?

Copy link
Author

Choose a reason for hiding this comment

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

linking stage doesn't work without this. Maybe you know why?
/SigmaDeltaDemo.ino:16: undefined reference to `sigmaDeltaGetPrescaler()'
and the same for all other sigmaDelta functions

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ignore my previous comment. The other "core_esp8266_*" files are all .c. So ignore the request to remove extern C, leave it there. Sorry for the confusion!

Copy link
Author

Choose a reason for hiding this comment

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

Hi @earlephilhower ,
I don't manage to get the style_check right on the travis build. Could you have a look what the issue is, please?
The other builds are ok now.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Will do tonight. I think it's just a matter of getting the examples in Arduino IDE format (i.e. 2 space indent, brackets in the right spot, etc.).

Copy link
Collaborator

Choose a reason for hiding this comment

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

Looks like your problem is DOS linefeeds. Can you save it with Unix ones, that seems to be what IGRR's checker requires to work properly. I can't do it for you as I don't have write permission to your source repo (nor should I, of course!).

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);

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is good, but I think to be most useful to end users, who may not be as familiar w/ clock dividers and sigma-delta operation a simpler API might be helpful.

I'd recommend renaming "setTarget/GetTarget" to simply, "write" or "read" as effectively they're trying to write an analog value and "target," while the name used by the hardware designers in the HDK, doesn't really mean much if you don't understand the error-diffusion behind things.

I'd also suggest a simpler "setFrequency(long f) setter method that would do the math described in the header and figure out the closest prescaler given the F_CPU. That'd make end-user code simpler to understand.

Copy link
Author

Choose a reason for hiding this comment

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

Not sure if I fully understand the setFrequency idea. Every call to 'write' (setTarget) would need to adapt the prescaler as well to fit this chosen frequency? And remove the setPrescaler/getPrescaler functions altogether?
There was some sample code like this in an earlier post to this issue. In many cases the effective output frequency is way off the chosen frequency f. Suppose you chose 1 MHz. With 50% duty cycle the closest you get is 312.5kHz. So you give the user the impression that the generator can be used as a fixed frequency source, but in fact it cannot.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Good point, I withdraw this silly request. :) I was thinking this would be useful for someone to, say, drive a 4-pin PWM fan where they'd need to set frequency = 25khz, duty = %age, but it seems it's not really precise enough for such a use, anyway.

For the examples, please do add the Public Domain attribution. I think the team is trying to get these examples cleaned up and that's an easy thing to do.

Copy link
Author

Choose a reason for hiding this comment

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

Apparently ESP32 has an implementation similar to what you suggested. I modified the code to be compatible. ESP32 has apparently 8 independent channels, esp8266 has only 1. Apart from that, the behavior is the same (eg. in terms of output frequencies.) So it makes sense to align the programming interfaces.
Arduino-esp32 also provides a working sigma delta example; there it's not necessary to explicitly include the sigma_delta.h file in the example code; it would be convenient if this was the same in arduino-esp8266, but my example currently doesn't work without the explicit include

#ifdef __cplusplus
}
#endif

#endif//SIGMA_DELTA_H