|
| 1 | +/* |
| 2 | + core_esp8266_sigma_delta.c - sigma delta library for esp8266 |
| 3 | + |
| 4 | + Copyright (c) 2014 Ivan Grokhotkov. All rights reserved. |
| 5 | + This file is part of the esp8266 core for Arduino environment. |
| 6 | +
|
| 7 | + This library is free software; you can redistribute it and/or |
| 8 | + modify it under the terms of the GNU Lesser General Public |
| 9 | + License as published by the Free Software Foundation; either |
| 10 | + version 2.1 of the License, or (at your option) any later version. |
| 11 | +
|
| 12 | + This library is distributed in the hope that it will be useful, |
| 13 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + Lesser General Public License for more details. |
| 16 | +
|
| 17 | + You should have received a copy of the GNU Lesser General Public |
| 18 | + License along with this library; if not, write to the Free Software |
| 19 | + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | +*/ |
| 21 | + |
| 22 | +#include "Arduino.h" // using pinMode |
| 23 | + |
| 24 | +// definitions in esp8266_peri.h style |
| 25 | +#define GPSD ESP8266_REG(0x368) // GPIO_SIGMA_DELTA register @ 0x600000368 |
| 26 | +#define GPSDT 0 // target, 8 bits |
| 27 | +#define GPSDP 8 // prescaler, 8 bits |
| 28 | +#define GPSDE 16 // enable |
| 29 | + |
| 30 | +void sigmaDeltaSetPrescaler(uint8_t prescaler); // avoids compiler warning |
| 31 | + |
| 32 | +/****************************************************************************** |
| 33 | + * FunctionName : sigmaDeltaEnable |
| 34 | + * Description : enable the internal sigma delta source |
| 35 | + * Parameters : none |
| 36 | + * Returns : none |
| 37 | +*******************************************************************************/ |
| 38 | +void ICACHE_FLASH_ATTR sigmaDeltaEnable() |
| 39 | +{ |
| 40 | + GPSD = (0 << GPSDT) | (0 << GPSDP) | (1 << GPSDE); //SIGMA_DELTA_TARGET(0) | SIGMA_DELTA_PRESCALER(0) | SIGMA_DELTA_ENABLE(ENABLED) |
| 41 | +} |
| 42 | + |
| 43 | +/****************************************************************************** |
| 44 | + * FunctionName : sigmaDeltaDisable |
| 45 | + * Description : stop the internal sigma delta source |
| 46 | + * Parameters : none |
| 47 | + * Returns : none |
| 48 | +*******************************************************************************/ |
| 49 | +void ICACHE_FLASH_ATTR sigmaDeltaDisable() |
| 50 | +{ |
| 51 | + GPSD = (0 << GPSDT) | (0 << GPSDP) | (0 << GPSDE); //SIGMA_DELTA_TARGET(0) | SIGMA_DELTA_PRESCALER(0) | SIGMA_DELTA_ENABLE(DISABLED) |
| 52 | +} |
| 53 | + |
| 54 | +/****************************************************************************** |
| 55 | + * FunctionName : sigmaDeltaAttachPin |
| 56 | + * Description : connects the sigma delta source to a physical output pin |
| 57 | + * Parameters : pin (0..15), channel = unused, for compatibility with ESP32 |
| 58 | + * Returns : none |
| 59 | +*******************************************************************************/ |
| 60 | +void ICACHE_FLASH_ATTR sigmaDeltaAttachPin(uint8_t pin, uint8_t channel) |
| 61 | +{ |
| 62 | + (void) channel; |
| 63 | + // make the chosen pin an output pin |
| 64 | + pinMode (pin, OUTPUT); |
| 65 | + if (pin < 16) { |
| 66 | + // set its source to the sigma delta source |
| 67 | + GPC(pin) |= (1 << GPCS); //SOURCE 0:GPIO_DATA,1:SigmaDelta |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +/****************************************************************************** |
| 72 | + * FunctionName : sigmaDeltaDetachPin |
| 73 | + * Description : disconnects the sigma delta source from a physical output pin |
| 74 | + * Parameters : pin (0..16) |
| 75 | + * Returns : none |
| 76 | +*******************************************************************************/ |
| 77 | +void ICACHE_FLASH_ATTR sigmaDeltaDetachPin(uint8_t pin) |
| 78 | +{ |
| 79 | + if (pin < 16) { |
| 80 | + // set its source to the sigma delta source |
| 81 | + GPC(pin) &= ~(1 << GPCS); //SOURCE 0:GPIO_DATA,1:SigmaDelta |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/****************************************************************************** |
| 86 | + * FunctionName : sigmaDeltaIsPinAttached |
| 87 | + * Description : query if pin is attached |
| 88 | + * Parameters : pin (0..16) |
| 89 | + * Returns : bool |
| 90 | +*******************************************************************************/ |
| 91 | +bool ICACHE_FLASH_ATTR sigmaDeltaIsPinAttached(uint8_t pin) |
| 92 | +{ |
| 93 | + if (pin < 16) { |
| 94 | + // set its source to the sigma delta source |
| 95 | + return (GPC(pin) & (1 << GPCS)); //SOURCE 0:GPIO_DATA,1:SigmaDelta |
| 96 | + } |
| 97 | + else |
| 98 | + return false; |
| 99 | +} |
| 100 | + |
| 101 | +/****************************************************************************** |
| 102 | + * FunctionName : sigmaDeltaSetup |
| 103 | + * Description : start the sigma delta generator with the chosen parameters |
| 104 | + * Parameters : channel = unused (for compatibility with ESP32), |
| 105 | + * freq : 1220-312500 (lowest frequency in the output signal's spectrum) |
| 106 | + * Returns : uint32_t the actual frequency, closest to the input parameter |
| 107 | +*******************************************************************************/ |
| 108 | +uint32_t ICACHE_FLASH_ATTR sigmaDeltaSetup(uint8_t channel, uint32_t freq) |
| 109 | +{ |
| 110 | + (void) channel; |
| 111 | + |
| 112 | + uint32_t prescaler = ((uint32_t)10000000/(freq*32)) - 1; |
| 113 | + |
| 114 | + if(prescaler > 0xFF) { |
| 115 | + prescaler = 0xFF; |
| 116 | + } |
| 117 | + sigmaDeltaEnable(); |
| 118 | + sigmaDeltaSetPrescaler ((uint8_t) prescaler); |
| 119 | + |
| 120 | + return 10000000/((prescaler + 1) * 32); |
| 121 | +} |
| 122 | + |
| 123 | +/****************************************************************************** |
| 124 | + * FunctionName : sigmaDeltaWrite |
| 125 | + * Description : set the duty cycle for the sigma-delta source |
| 126 | + * Parameters : uint8 duty, 0-255, duty cycle = target/256, |
| 127 | + * channel = unused, for compatibility with ESP32 |
| 128 | + * Returns : none |
| 129 | +*******************************************************************************/ |
| 130 | +void ICACHE_FLASH_ATTR sigmaDeltaWrite(uint8_t channel, uint8_t duty) |
| 131 | +{ |
| 132 | + uint32_t reg = GPSD; |
| 133 | + (void) channel; |
| 134 | + |
| 135 | + reg = (reg & ~(0xFF << GPSDT)) | ((duty & 0xFF) << GPSDT); |
| 136 | + GPSD = reg; |
| 137 | + |
| 138 | +} |
| 139 | +/****************************************************************************** |
| 140 | + * FunctionName : sigmaDeltaRead |
| 141 | + * Description : get the duty cycle for the sigma-delta source |
| 142 | + * Parameters : channel = unused, for compatibility with ESP32 |
| 143 | + * Returns : uint8_t duty cycle value 0..255 |
| 144 | +*******************************************************************************/ |
| 145 | +uint8_t ICACHE_FLASH_ATTR sigmaDeltaRead(uint8_t channel) |
| 146 | +{ |
| 147 | + (void) channel; |
| 148 | + return (uint8_t)((GPSD >> GPSDT) & 0xFF); |
| 149 | +} |
| 150 | + |
| 151 | +/****************************************************************************** |
| 152 | + * FunctionName : sigmaDeltaSetPrescaler |
| 153 | + * Description : set the clock divider for the sigma-delta source |
| 154 | + * Parameters : uint8 prescaler, 0-255, divides the 80MHz base clock by this amount |
| 155 | + * Returns : none |
| 156 | +*******************************************************************************/ |
| 157 | +void ICACHE_FLASH_ATTR sigmaDeltaSetPrescaler(uint8_t prescaler) |
| 158 | +{ |
| 159 | + uint32_t reg = GPSD; |
| 160 | + |
| 161 | + reg = (reg & ~(0xFF << GPSDP)) | ((prescaler & 0xFF) << GPSDP); |
| 162 | + GPSD = reg; |
| 163 | +} |
| 164 | + |
| 165 | +/****************************************************************************** |
| 166 | + * FunctionName : sigmaDeltaGetPrescaler |
| 167 | + * Description : get the prescaler value from the GPIO_SIGMA_DELTA register |
| 168 | + * Parameters : none |
| 169 | + * Returns : uint8 prescaler, CLK_DIV , 0-255 |
| 170 | +*******************************************************************************/ |
| 171 | +uint8_t ICACHE_FLASH_ATTR sigmaDeltaGetPrescaler(void) |
| 172 | +{ |
| 173 | + return (uint8_t)((GPSD >> GPSDP) & 0xFF); |
| 174 | +} |
0 commit comments