-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
working code for sigma delta generator #4119
Changes from 1 commit
d746752
a11cb99
85b9323
648483c
46cf8c4
c5351a0
a1f8886
385b7a2
a60f949
6d177fd
717bdc3
4c7ad74
0a1189e
83aeecd
4ebb183
ab7204c
d67fa53
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
|
||
// 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok done