-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Closed
Changes from 3 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
d746752
working code for sigma delta generator
stefaandesmet2003 a11cb99
Merge branch 'master' into master
stefaandesmet2003 85b9323
removed example code from header file
stefaandesmet2003 648483c
example for the esp8266 sigma delta generator hardware
stefaandesmet2003 46cf8c4
Merge branch 'master' into master
stefaandesmet2003 c5351a0
modified interface in line with ESP32 implementation
stefaandesmet2003 a1f8886
modified interface in line with ESP32 implementation
stefaandesmet2003 385b7a2
changes requested by earlphilhower
stefaandesmet2003 a60f949
update for travis style_check
stefaandesmet2003 6d177fd
Merge branch 'master' into master
stefaandesmet2003 717bdc3
remove unused parameter in sigmaDeltaGetPrescaler()
stefaandesmet2003 4c7ad74
travis doesn't like compiler warnings
stefaandesmet2003 0a1189e
correct comment for travis
stefaandesmet2003 83aeecd
changed BUILTIN_LED to LED_BUILTIN
stefaandesmet2003 4ebb183
remove unused test code
stefaandesmet2003 ab7204c
for travis style_check
stefaandesmet2003 d67fa53
another attempt for travis style_check
stefaandesmet2003 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,3 @@ | ||
/* | ||
/****************************************************************************** | ||
* 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 | ||
|
@@ -36,35 +7,36 @@ The sigma delta source remains on until : | |
#define GPSDE 16 // enable | ||
|
||
/****************************************************************************** | ||
* FunctionName : sigma_delta_start | ||
* FunctionName : sigmaDeltaEnable | ||
* Description : enable the internal sigma delta source | ||
* Parameters : none | ||
* Returns : none | ||
*******************************************************************************/ | ||
void ICACHE_FLASH_ATTR sigma_delta_enable() | ||
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 : sigma_delta_disable | ||
* FunctionName : sigmaDeltaDisable | ||
* Description : stop the internal sigma delta source | ||
* Parameters : none | ||
* Returns : none | ||
*******************************************************************************/ | ||
void ICACHE_FLASH_ATTR sigma_delta_disable() | ||
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 : 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) | ||
* Parameters : pin (0..15), channel = unused, for compatibility with ESP32 | ||
* Returns : none | ||
*******************************************************************************/ | ||
void ICACHE_FLASH_ATTR sigma_delta_attachPin(uint8_t pin) | ||
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) { | ||
|
@@ -79,7 +51,7 @@ void ICACHE_FLASH_ATTR sigma_delta_attachPin(uint8_t pin) | |
* Parameters : pin (0..16) | ||
* Returns : none | ||
*******************************************************************************/ | ||
void ICACHE_FLASH_ATTR sigma_delta_detachPin(uint8_t pin) | ||
void ICACHE_FLASH_ATTR sigmaDeltaDetachPin(uint8_t pin) | ||
{ | ||
if (pin < 16) { | ||
// set its source to the sigma delta source | ||
|
@@ -88,12 +60,12 @@ void ICACHE_FLASH_ATTR sigma_delta_detachPin(uint8_t pin) | |
} | ||
|
||
/****************************************************************************** | ||
* FunctionName : sigma_delta_isPinAttached | ||
* FunctionName : sigmaDeltaIsPinAttached | ||
* Description : query if pin is attached | ||
* Parameters : pin (0..16) | ||
* Returns : bool | ||
*******************************************************************************/ | ||
bool ICACHE_FLASH_ATTR sigma_delta_isPinAttached(uint8_t pin) | ||
bool ICACHE_FLASH_ATTR sigmaDeltaIsPinAttached(uint8_t pin) | ||
{ | ||
if (pin < 16) { | ||
// set its source to the sigma delta source | ||
|
@@ -104,51 +76,77 @@ bool ICACHE_FLASH_ATTR sigma_delta_isPinAttached(uint8_t pin) | |
} | ||
|
||
/****************************************************************************** | ||
* FunctionName : sigma_delta_getTarget | ||
* Description : get the target value from the GPIO_SIGMA_DELTA register | ||
* Parameters : none | ||
* Returns : uint8_t target value 0..255 | ||
* 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 | ||
*******************************************************************************/ | ||
uint8_t ICACHE_FLASH_ATTR sigma_delta_getTarget(void) | ||
uint32_t ICACHE_FLASH_ATTR sigmaDeltaSetup(uint8_t channel, uint32_t freq) | ||
{ | ||
return (uint8_t)((GPSD >> GPSDT) & 0xFF); | ||
(void) channel; | ||
|
||
uint32_t prescaler = ((uint32_t)10000000/(freq*32)) - 1; | ||
|
||
if(prescaler > 0xFF) { | ||
prescaler = 0xFF; | ||
} | ||
sigmaDeltaEnable(); | ||
sigmaDeltaSetPrescaler ((uint8_t) prescaler); | ||
//sigmaDeltaSetTarget ((uint8_t) 0x80); // 50% duty cycle | ||
|
||
return 10000000/((prescaler + 1) * 32); | ||
} | ||
|
||
/****************************************************************************** | ||
* FunctionName : sigma_delta_setTarget | ||
* Description : set the target (duty cycle) for the sigma-delta source | ||
* Parameters : uint8 target, 0-255, duty cycle = target/256 | ||
* 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 sigma_delta_setTarget(uint8_t target) | ||
void ICACHE_FLASH_ATTR sigmaDeltaWrite(uint8_t channel, uint8_t duty) | ||
{ | ||
uint32_t reg = GPSD; | ||
(void) channel; | ||
|
||
reg = (reg & ~(0xFF << GPSDT)) | ((target & 0xFF) << GPSDT); | ||
reg = (reg & ~(0xFF << GPSDT)) | ((duty & 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 | ||
* FunctionName : sigmaDeltaRead | ||
* Description : set the duty cycle for the sigma-delta source | ||
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. Typo here...this "get the duty..." |
||
* Parameters : none, channel = unused, for compatibility with ESP32 | ||
* Returns : uint8_t duty cycle value 0..255 | ||
*******************************************************************************/ | ||
uint8_t ICACHE_FLASH_ATTR sigma_delta_getPrescaler(uint8_t prescaler) | ||
uint8_t ICACHE_FLASH_ATTR sigmaDeltaRead(uint8_t channel) | ||
{ | ||
return (uint8_t)((GPSD >> GPSDP) & 0xFF); | ||
(void) channel; | ||
return (uint8_t)((GPSD >> GPSDT) & 0xFF); | ||
} | ||
|
||
/****************************************************************************** | ||
* FunctionName : sigma_delta_setPrescaler | ||
* 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 sigma_delta_setPrescaler(uint8_t prescaler) | ||
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(uint8_t prescaler) | ||
{ | ||
return (uint8_t)((GPSD >> GPSDP) & 0xFF); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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