Initial Release
|
@ -0,0 +1,6 @@
|
||||||
|
sdkconfig*
|
||||||
|
pictures/owonread.py
|
||||||
|
.cproject
|
||||||
|
.project
|
||||||
|
.settings/*
|
||||||
|
build/*
|
|
@ -0,0 +1,202 @@
|
||||||
|
|
||||||
|
Apache License
|
||||||
|
Version 2.0, January 2004
|
||||||
|
http://www.apache.org/licenses/
|
||||||
|
|
||||||
|
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
||||||
|
|
||||||
|
1. Definitions.
|
||||||
|
|
||||||
|
"License" shall mean the terms and conditions for use, reproduction,
|
||||||
|
and distribution as defined by Sections 1 through 9 of this document.
|
||||||
|
|
||||||
|
"Licensor" shall mean the copyright owner or entity authorized by
|
||||||
|
the copyright owner that is granting the License.
|
||||||
|
|
||||||
|
"Legal Entity" shall mean the union of the acting entity and all
|
||||||
|
other entities that control, are controlled by, or are under common
|
||||||
|
control with that entity. For the purposes of this definition,
|
||||||
|
"control" means (i) the power, direct or indirect, to cause the
|
||||||
|
direction or management of such entity, whether by contract or
|
||||||
|
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
||||||
|
outstanding shares, or (iii) beneficial ownership of such entity.
|
||||||
|
|
||||||
|
"You" (or "Your") shall mean an individual or Legal Entity
|
||||||
|
exercising permissions granted by this License.
|
||||||
|
|
||||||
|
"Source" form shall mean the preferred form for making modifications,
|
||||||
|
including but not limited to software source code, documentation
|
||||||
|
source, and configuration files.
|
||||||
|
|
||||||
|
"Object" form shall mean any form resulting from mechanical
|
||||||
|
transformation or translation of a Source form, including but
|
||||||
|
not limited to compiled object code, generated documentation,
|
||||||
|
and conversions to other media types.
|
||||||
|
|
||||||
|
"Work" shall mean the work of authorship, whether in Source or
|
||||||
|
Object form, made available under the License, as indicated by a
|
||||||
|
copyright notice that is included in or attached to the work
|
||||||
|
(an example is provided in the Appendix below).
|
||||||
|
|
||||||
|
"Derivative Works" shall mean any work, whether in Source or Object
|
||||||
|
form, that is based on (or derived from) the Work and for which the
|
||||||
|
editorial revisions, annotations, elaborations, or other modifications
|
||||||
|
represent, as a whole, an original work of authorship. For the purposes
|
||||||
|
of this License, Derivative Works shall not include works that remain
|
||||||
|
separable from, or merely link (or bind by name) to the interfaces of,
|
||||||
|
the Work and Derivative Works thereof.
|
||||||
|
|
||||||
|
"Contribution" shall mean any work of authorship, including
|
||||||
|
the original version of the Work and any modifications or additions
|
||||||
|
to that Work or Derivative Works thereof, that is intentionally
|
||||||
|
submitted to Licensor for inclusion in the Work by the copyright owner
|
||||||
|
or by an individual or Legal Entity authorized to submit on behalf of
|
||||||
|
the copyright owner. For the purposes of this definition, "submitted"
|
||||||
|
means any form of electronic, verbal, or written communication sent
|
||||||
|
to the Licensor or its representatives, including but not limited to
|
||||||
|
communication on electronic mailing lists, source code control systems,
|
||||||
|
and issue tracking systems that are managed by, or on behalf of, the
|
||||||
|
Licensor for the purpose of discussing and improving the Work, but
|
||||||
|
excluding communication that is conspicuously marked or otherwise
|
||||||
|
designated in writing by the copyright owner as "Not a Contribution."
|
||||||
|
|
||||||
|
"Contributor" shall mean Licensor and any individual or Legal Entity
|
||||||
|
on behalf of whom a Contribution has been received by Licensor and
|
||||||
|
subsequently incorporated within the Work.
|
||||||
|
|
||||||
|
2. Grant of Copyright License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
copyright license to reproduce, prepare Derivative Works of,
|
||||||
|
publicly display, publicly perform, sublicense, and distribute the
|
||||||
|
Work and such Derivative Works in Source or Object form.
|
||||||
|
|
||||||
|
3. Grant of Patent License. Subject to the terms and conditions of
|
||||||
|
this License, each Contributor hereby grants to You a perpetual,
|
||||||
|
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
||||||
|
(except as stated in this section) patent license to make, have made,
|
||||||
|
use, offer to sell, sell, import, and otherwise transfer the Work,
|
||||||
|
where such license applies only to those patent claims licensable
|
||||||
|
by such Contributor that are necessarily infringed by their
|
||||||
|
Contribution(s) alone or by combination of their Contribution(s)
|
||||||
|
with the Work to which such Contribution(s) was submitted. If You
|
||||||
|
institute patent litigation against any entity (including a
|
||||||
|
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
||||||
|
or a Contribution incorporated within the Work constitutes direct
|
||||||
|
or contributory patent infringement, then any patent licenses
|
||||||
|
granted to You under this License for that Work shall terminate
|
||||||
|
as of the date such litigation is filed.
|
||||||
|
|
||||||
|
4. Redistribution. You may reproduce and distribute copies of the
|
||||||
|
Work or Derivative Works thereof in any medium, with or without
|
||||||
|
modifications, and in Source or Object form, provided that You
|
||||||
|
meet the following conditions:
|
||||||
|
|
||||||
|
(a) You must give any other recipients of the Work or
|
||||||
|
Derivative Works a copy of this License; and
|
||||||
|
|
||||||
|
(b) You must cause any modified files to carry prominent notices
|
||||||
|
stating that You changed the files; and
|
||||||
|
|
||||||
|
(c) You must retain, in the Source form of any Derivative Works
|
||||||
|
that You distribute, all copyright, patent, trademark, and
|
||||||
|
attribution notices from the Source form of the Work,
|
||||||
|
excluding those notices that do not pertain to any part of
|
||||||
|
the Derivative Works; and
|
||||||
|
|
||||||
|
(d) If the Work includes a "NOTICE" text file as part of its
|
||||||
|
distribution, then any Derivative Works that You distribute must
|
||||||
|
include a readable copy of the attribution notices contained
|
||||||
|
within such NOTICE file, excluding those notices that do not
|
||||||
|
pertain to any part of the Derivative Works, in at least one
|
||||||
|
of the following places: within a NOTICE text file distributed
|
||||||
|
as part of the Derivative Works; within the Source form or
|
||||||
|
documentation, if provided along with the Derivative Works; or,
|
||||||
|
within a display generated by the Derivative Works, if and
|
||||||
|
wherever such third-party notices normally appear. The contents
|
||||||
|
of the NOTICE file are for informational purposes only and
|
||||||
|
do not modify the License. You may add Your own attribution
|
||||||
|
notices within Derivative Works that You distribute, alongside
|
||||||
|
or as an addendum to the NOTICE text from the Work, provided
|
||||||
|
that such additional attribution notices cannot be construed
|
||||||
|
as modifying the License.
|
||||||
|
|
||||||
|
You may add Your own copyright statement to Your modifications and
|
||||||
|
may provide additional or different license terms and conditions
|
||||||
|
for use, reproduction, or distribution of Your modifications, or
|
||||||
|
for any such Derivative Works as a whole, provided Your use,
|
||||||
|
reproduction, and distribution of the Work otherwise complies with
|
||||||
|
the conditions stated in this License.
|
||||||
|
|
||||||
|
5. Submission of Contributions. Unless You explicitly state otherwise,
|
||||||
|
any Contribution intentionally submitted for inclusion in the Work
|
||||||
|
by You to the Licensor shall be under the terms and conditions of
|
||||||
|
this License, without any additional terms or conditions.
|
||||||
|
Notwithstanding the above, nothing herein shall supersede or modify
|
||||||
|
the terms of any separate license agreement you may have executed
|
||||||
|
with Licensor regarding such Contributions.
|
||||||
|
|
||||||
|
6. Trademarks. This License does not grant permission to use the trade
|
||||||
|
names, trademarks, service marks, or product names of the Licensor,
|
||||||
|
except as required for reasonable and customary use in describing the
|
||||||
|
origin of the Work and reproducing the content of the NOTICE file.
|
||||||
|
|
||||||
|
7. Disclaimer of Warranty. Unless required by applicable law or
|
||||||
|
agreed to in writing, Licensor provides the Work (and each
|
||||||
|
Contributor provides its Contributions) on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
||||||
|
implied, including, without limitation, any warranties or conditions
|
||||||
|
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
||||||
|
PARTICULAR PURPOSE. You are solely responsible for determining the
|
||||||
|
appropriateness of using or redistributing the Work and assume any
|
||||||
|
risks associated with Your exercise of permissions under this License.
|
||||||
|
|
||||||
|
8. Limitation of Liability. In no event and under no legal theory,
|
||||||
|
whether in tort (including negligence), contract, or otherwise,
|
||||||
|
unless required by applicable law (such as deliberate and grossly
|
||||||
|
negligent acts) or agreed to in writing, shall any Contributor be
|
||||||
|
liable to You for damages, including any direct, indirect, special,
|
||||||
|
incidental, or consequential damages of any character arising as a
|
||||||
|
result of this License or out of the use or inability to use the
|
||||||
|
Work (including but not limited to damages for loss of goodwill,
|
||||||
|
work stoppage, computer failure or malfunction, or any and all
|
||||||
|
other commercial damages or losses), even if such Contributor
|
||||||
|
has been advised of the possibility of such damages.
|
||||||
|
|
||||||
|
9. Accepting Warranty or Additional Liability. While redistributing
|
||||||
|
the Work or Derivative Works thereof, You may choose to offer,
|
||||||
|
and charge a fee for, acceptance of support, warranty, indemnity,
|
||||||
|
or other liability obligations and/or rights consistent with this
|
||||||
|
License. However, in accepting such obligations, You may act only
|
||||||
|
on Your own behalf and on Your sole responsibility, not on behalf
|
||||||
|
of any other Contributor, and only if You agree to indemnify,
|
||||||
|
defend, and hold each Contributor harmless for any liability
|
||||||
|
incurred by, or claims asserted against, such Contributor by reason
|
||||||
|
of your accepting any such warranty or additional liability.
|
||||||
|
|
||||||
|
END OF TERMS AND CONDITIONS
|
||||||
|
|
||||||
|
APPENDIX: How to apply the Apache License to your work.
|
||||||
|
|
||||||
|
To apply the Apache License to your work, attach the following
|
||||||
|
boilerplate notice, with the fields enclosed by brackets "[]"
|
||||||
|
replaced with your own identifying information. (Don't include
|
||||||
|
the brackets!) The text should be enclosed in the appropriate
|
||||||
|
comment syntax for the file format. We also recommend that a
|
||||||
|
file or class name and description of purpose be included on the
|
||||||
|
same "printed page" as the copyright notice for easier
|
||||||
|
identification within third-party archives.
|
||||||
|
|
||||||
|
Copyright [yyyy] [name of copyright owner]
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
|
@ -0,0 +1,9 @@
|
||||||
|
#
|
||||||
|
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||||
|
# project subdirectory.
|
||||||
|
#
|
||||||
|
|
||||||
|
PROJECT_NAME := dac-cosine
|
||||||
|
|
||||||
|
include $(IDF_PATH)/make/project.mk
|
||||||
|
|
|
@ -0,0 +1,176 @@
|
||||||
|
# dac-cosine
|
||||||
|
|
||||||
|
|
||||||
|
## Introduction
|
||||||
|
|
||||||
|
ESP32 is described by Espressif as [Wi-Fi + Bluetooth Combo Chip](https://espressif.com/en/products/hardware/esp32/overview). Besides the core wireless functionality it combines numerous other features like wired communication interfaces ranging from UART or I2S to CAN and Ethernet, several controllers like LED or MCPWM, as well as couple other building blocks including low noise amplifier, hall sensor or camera interface. Lurking in the shadows of these features is cosine waveform generator.
|
||||||
|
|
||||||
|
The purpose of this repository is to review functionality of ESP32's cosine waveform generator and develop a simple API to operate it.
|
||||||
|
|
||||||
|
|
||||||
|
## Documentation is there
|
||||||
|
|
||||||
|
The cosine waveform (CW) generator is described in details in the [ESP32 Technical Reference Manual](http://espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf), but if we check API of [ESP-IDF](https://github.com/espressif/esp-idf), it has no any support implemented (as of July 2017). Fortunately, with commonly available information we can fairly easy put it into action.
|
||||||
|
|
||||||
|
Starting with ESP32 Technical Reference Manual, in chapter 24.7, we learn that the cosine waveform (CW) generator it is part of 8-bit DAC functional block and find the following functional diagram:
|
||||||
|
|
||||||
|
![alt text](pictures/dac.png "ESP32 DAC functional diagram (figure from ESP32 Technical Reference Manual)")
|
||||||
|
|
||||||
|
Diagram is showing "CW generator" box in upper left corner of diagram and two mux transfer blocks that are routed to DAC taking 8 bits on input on one side and providing `dacn_out` analog output on the other.
|
||||||
|
|
||||||
|
Besides CW, DAC is also accepting input from DMA, to generate arbitrary waveform, or from `RTCIO_PAD_DACn_REG` register to provide ordinary conversion of 8-bit numeric value. Several “n” letters on this diagram indicate either 1 or 2 because DAC has two channels.
|
||||||
|
|
||||||
|
|
||||||
|
## Check basic DAC functionality
|
||||||
|
|
||||||
|
To start with we should explore how ordinary conversion of 8-bit numeric values is performed. Checking [DAC API](http://esp-idf.readthedocs.io/en/latest/api-reference/peripherals/dac.html) in ESP-IDF, we will find couple of functions to enable DAC and output an analog value. Unfortunately these functions are not provided in source code. But checking further in [arduino-esp32](https://github.com/espressif/arduino-esp32) repository, we will find [esp32-hal-dac.c](https://github.com/espressif/arduino-esp32/blob/758553a786520b138307691e842e53b25606960b/cores/esp32/esp32-hal-dac.c) file that contains couple lines of code with comments that are behind Arduino `dacWrite` function. This should give us initial understanding which registers are used to provide digital value to be converted to analog output and how to enable or disable an output. Checking further the [ESP32 Technical Reference Manual](http://espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf) and searching for `SENS_SAR_DAC_CTRL1_REG` we will easy find a “Register Summary” section 28.4, that provides this and other register names used in esp32-hal-dac.c with explanation of particular bits and functions.
|
||||||
|
|
||||||
|
|
||||||
|
## Use CW instead of ordinary DAC conversion
|
||||||
|
|
||||||
|
In next step, looking on ESP32 DAC Functional Diagram, we should figure out how to use CW generator block instead of `RTCIO_PAD_DACn_REG` register. Switching between these two is done with `SENS_DACn_CW_EN` bit. Searching for `SENS_DAC_CW` name, we will find it in ESP32 Technical Reference Manual under `SENS_SAR_DAC_CTRL2_REG` register together with explanation:
|
||||||
|
|
||||||
|
![alt text](pictures/explanation_SENS_DAC_CW_EN.png "Explanation of SENS_DAC_CW_ENn bits (from ESP32 Technical Reference Manual)")
|
||||||
|
|
||||||
|
In other words setting `SENS_DAC_CW_EN1` bit will connect CW generator, clearing it will connect `PDAC1_DAC[7:0]` as the input source of DAC on channel 1. One extra crosschecking of this description against ESP32 DAC Functional Diagram and [esp32-hal-dac.c](https://github.com/espressif/arduino-esp32/blob/758553a786520b138307691e842e53b25606960b/cores/esp32/esp32-hal-dac.c) will confirm that this is what we are looking for.
|
||||||
|
|
||||||
|
After additional studying of fields of `SENS_SAR_DAC_CTRL1_REG` register we will also discover `SENS_SW_TONE_EN` bit that is enabling the CW generator itself.
|
||||||
|
|
||||||
|
The last component to complete this exercise is setting of generator's frequency. Applicable information is provided in first paragraph of “Cosine Waveform Generator” section in ESP32 Technical Reference Manual, that “The frequency of CW can be adjusted by register SENS_SAR_SW_FSTEP[15:0]”.
|
||||||
|
|
||||||
|
|
||||||
|
## Enable cosine generator on DAC channel 1
|
||||||
|
|
||||||
|
Now we are ready to write the code to use CW generator. To connect it to channel 1 (GPIO25), the code will consists of the following steps:
|
||||||
|
|
||||||
|
|
||||||
|
1. Enable CW generator by setting `SENS_SW_TONE_EN` bit in `SENS_SAR_DAC_CTRL1_REG` register.
|
||||||
|
|
||||||
|
```c
|
||||||
|
SET_PERI_REG_MASK(SENS_SAR_DAC_CTRL1_REG, SENS_SW_TONE_EN);
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Connect generator to DAC channel 1 by setting `SENS_DAC_CW_EN1` bit in `SENS_SAR_DAC_CTRL2_REG` register.
|
||||||
|
|
||||||
|
```c
|
||||||
|
SET_PERI_REG_MASK(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_CW_EN1_M);
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Set generator's frequecy by writing a value to `SENS_SAR_SW_FSTEP[15:0]` field in `SENS_SAR_DAC_CTRL1_REG` register.
|
||||||
|
|
||||||
|
```c
|
||||||
|
SET_PERI_REG_BITS(SENS_SAR_DAC_CTRL1_REG, SENS_SW_FSTEP, frequency, SENS_SW_FSTEP_S);
|
||||||
|
```
|
||||||
|
|
||||||
|
4. Enable output on DAC channel 1 by calling API function [dac_output_enable()](http://esp-idf.readthedocs.io/en/latest/api-reference/peripherals/dac.html#_CPPv217dac_output_enable13dac_channel_t).
|
||||||
|
|
||||||
|
```c
|
||||||
|
dac_output_enable(DAC_CHANNEL_1);
|
||||||
|
```
|
||||||
|
|
||||||
|
After uploading code to ESP32 and connecting a scope to DAC channel 1 (GPIO25) we should see a similar waveform:
|
||||||
|
|
||||||
|
![alt text](pictures/initial-sine-waveform.png "Initial waveform from cosine generator")
|
||||||
|
|
||||||
|
The waveform looks promising but requires some extra bit tweaking.
|
||||||
|
|
||||||
|
|
||||||
|
## Fix the waveform
|
||||||
|
|
||||||
|
In initial waveform shown above the sinusoid halves are inverted. We should look for some additional configuration bits that let us revert the halves back. Further searching through DAC registers will reveal that `SENS_SAR_DAC_CTRL2_REG` register has two bit `SENS_DAC_INV` field with promising explanation:
|
||||||
|
|
||||||
|
- 00: does not invert any bits,
|
||||||
|
- 01: inverts all bits,
|
||||||
|
- 10: inverts MSB,
|
||||||
|
- 11: inverts all bits except for MSB.
|
||||||
|
|
||||||
|
This field currently contains default value of both bits cleared `00`, so we should try remaining three options by adding the following code:
|
||||||
|
|
||||||
|
```c
|
||||||
|
SET_PERI_REG_BITS(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_INV1, invert, SENS_DAC_INV1_S);
|
||||||
|
```
|
||||||
|
|
||||||
|
Indeed, after setting `invert` bits to `10` (decimal 2), we will finally get expected sinusoidal waveform:
|
||||||
|
|
||||||
|
![alt text](pictures/expected-sine-waveform.png "Sinusoid waveform by ESP32's internal CW generator")
|
||||||
|
|
||||||
|
The blue waveform below is [FFT (Fast Fourier transform)](https://en.wikipedia.org/wiki/Fast_Fourier_transform), with strong initial peak we have been looking for.
|
||||||
|
|
||||||
|
The sinusoid itself has some random artifacts around zero and 180 phase, but I am not sure we can do anything about it. Later testing revealed no such artifacts at channel 2.
|
||||||
|
|
||||||
|
|
||||||
|
## What is the other functionality we can get?
|
||||||
|
|
||||||
|
[ESP32 Technical Reference Manual](http://espressif.com/sites/default/files/documentation/esp32_technical_reference_manual_en.pdf) specifies several parameters of CW we can adjust. It also provides applicable figure:
|
||||||
|
|
||||||
|
![alt text](pictures/dac2.png "Cosine generator parameter setting (figure from ESP32 Technical Reference Manual)")
|
||||||
|
|
||||||
|
After checking of registers we should be able to compile complete list of fields to tweak:
|
||||||
|
|
||||||
|
- Frequency - `SENS_SW_FSTEP`
|
||||||
|
- Scale - `SENS_DAC_SCALE`
|
||||||
|
- Offset - `SENS_DAC_DC`
|
||||||
|
- Inversion -`SENS_DAC_INV`
|
||||||
|
|
||||||
|
Having four parameters to manage and check, it would be reasonable to write an user interface to make it easier to adjust individual values.
|
||||||
|
|
||||||
|
Before you rush to write the UI, I propose an easier and quicker way to examine all parameters in action.
|
||||||
|
|
||||||
|
|
||||||
|
## JTAG makes testing easier
|
||||||
|
|
||||||
|
ESP32 is equipped with JTAG functionality that together with [OpenOCD](https://github.com/espressif/openocd-esp32) software make it much easier to test and troubleshoot the ESP32 hardware and application.
|
||||||
|
|
||||||
|
In this particular case we can use JTAG + OpenOCD to stop application execution, apply CW parameter changes and resume execution to see how CW responds.
|
||||||
|
|
||||||
|
Check documentation how to install and operate JTAG under the following links:
|
||||||
|
|
||||||
|
- [ESP32 Programming Guide / API Guides / Debugging](http://esp-idf.readthedocs.io/en/latest/api-guides/openocd.html)
|
||||||
|
- [JTAG Debugging for ESP32 (PDF)](http://espressif.com/sites/default/files/documentation/jtag_debugging_for_esp32_en.pdf)
|
||||||
|
|
||||||
|
Clone this repository, build the code and load it to ESP32. Connect a scope to DAC channels 1 and 2 (GPIO25 and GPIO26).
|
||||||
|
|
||||||
|
The waveform on channel 1 will be set to default values of 1kHz frequecy without any scaling or offset. We will then tweak CW parameters on channel 2 to see how the output on this channel differs from the reference channel 1.
|
||||||
|
|
||||||
|
After running debugger establish a breakpoint inside main loop of `dactask()`. On
|
||||||
|
"Expressions" tab add all CW parameters to tweak, see upper right corner of debugger window below.
|
||||||
|
|
||||||
|
![alt text](pictures/debugger-setup.png "Debugger setup")
|
||||||
|
|
||||||
|
Once ready you can change individual CW parameters. Then click "Resume" or press F8 to run the application. The application will use new parameter values and halt waiting for another change.
|
||||||
|
|
||||||
|
Below is an example of output of channel 1 (red trace = reference) and channel 2 (yellow trace) with scale set to 1/2 and inversion of all bits except MSB.
|
||||||
|
|
||||||
|
![alt text](pictures/comparing-dac-output.png "Comparing output on DAC channels")
|
||||||
|
|
||||||
|
JTAG interface operated from [Eclipse IDE](https://eclipse.org/) makes is easy and intuitive to interact with the application that has no user interface provided. Within couple of minutes it was possible to probe critical parameter and e.g. discover that at frequencies of around 100 kHz the waveform it is getting visible saw noise. The lowest frequency that can be set is around 125 Hz.
|
||||||
|
|
||||||
|
![alt text](pictures/saw-noise-100kHz.png "Saw noise is getting visible at 100kHz")
|
||||||
|
|
||||||
|
|
||||||
|
## Conclusion
|
||||||
|
|
||||||
|
ESP32 has on board cosine waveform generator with adjustable frequecy, scale and offset. The waveform has 8-bit resolution and can be output to GPIO25 (channel 1) and / or GPIO26 (channel 2) pins. Scale and offset can be set individually per channel. Frequency setting is common to both channels. The waveforms are generated by ESP32's hardware without any overhead on CPUs.
|
||||||
|
|
||||||
|
![alt text](pictures/test-setup.jpg "Setup used for testing of cosing waveform generator of ESP32")
|
||||||
|
|
||||||
|
|
||||||
|
## Contribute
|
||||||
|
|
||||||
|
Feel free to contribute to the project in any way you like!
|
||||||
|
|
||||||
|
If you find any issues with code or description please report them using *Issues* tab above.
|
||||||
|
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
|
||||||
|
This small repository has been prepared thanks to great work of the following teams and individuals:
|
||||||
|
|
||||||
|
* Espressif team that develops and maintains [esp-idf](https://github.com/espressif/esp-idf) and [openocd-esp32](https://github.com/espressif/openocd-esp32) repositories.
|
||||||
|
* [Ivan Grokhotkov](https://twitter.com/i_grr) who provided tips how to operate CW generator.
|
||||||
|
* [robert-hh](https://github.com/robert-hh), developer of excellent tool [owonread](https://github.com/robert-hh/owonread) to get screen shots and track data data from Owon TDS series Oscilloscopes with Linux.
|
||||||
|
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
[Apache License Version 2.0, January 2004](LICENSE)
|
|
@ -0,0 +1,3 @@
|
||||||
|
#
|
||||||
|
# Main Makefile. This is basically the same as a component makefile.
|
||||||
|
#
|
|
@ -0,0 +1,175 @@
|
||||||
|
/* DAC Cosine Generator Example
|
||||||
|
|
||||||
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, this
|
||||||
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
||||||
|
CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
*/
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "freertos/FreeRTOS.h"
|
||||||
|
#include "freertos/task.h"
|
||||||
|
#include "freertos/queue.h"
|
||||||
|
|
||||||
|
#include "soc/rtc_io_reg.h"
|
||||||
|
#include "soc/rtc_cntl_reg.h"
|
||||||
|
#include "soc/sens_reg.h"
|
||||||
|
|
||||||
|
#include "driver/dac.h"
|
||||||
|
|
||||||
|
/* Declare global sine waveform parameters
|
||||||
|
* so they may be then accessed and changed from debugger
|
||||||
|
* over an JTAG interface
|
||||||
|
*/
|
||||||
|
int frequency = 8; // about 1kHz
|
||||||
|
int scale = 1; // 50% of the full scale
|
||||||
|
int offset; // leave it default / 0 = no any offset
|
||||||
|
int invert = 2; // invert MSB to get sine waveform
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Enable cosine waveform generator on a DAC channel
|
||||||
|
*/
|
||||||
|
void dac_cosine_enable(dac_channel_t channel)
|
||||||
|
{
|
||||||
|
// Enable tone generator common to both channels
|
||||||
|
SET_PERI_REG_MASK(SENS_SAR_DAC_CTRL1_REG, SENS_SW_TONE_EN);
|
||||||
|
switch(channel) {
|
||||||
|
case DAC_CHANNEL_1:
|
||||||
|
// Enable / connect tone tone generator on / to this channel
|
||||||
|
SET_PERI_REG_MASK(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_CW_EN1_M);
|
||||||
|
// Invert MSB, otherwise part of waveform will have inverted
|
||||||
|
SET_PERI_REG_BITS(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_INV1, 2, SENS_DAC_INV1_S);
|
||||||
|
break;
|
||||||
|
case DAC_CHANNEL_2:
|
||||||
|
SET_PERI_REG_MASK(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_CW_EN2_M);
|
||||||
|
SET_PERI_REG_BITS(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_INV2, 2, SENS_DAC_INV2_S);
|
||||||
|
break;
|
||||||
|
default :
|
||||||
|
printf("Channel %d\n", channel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Set frequency of internal CW generator common to both DAC channels
|
||||||
|
*
|
||||||
|
* Range 0x0001 - 0xFFFF
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void dac_frequency_set(int frequency)
|
||||||
|
{
|
||||||
|
SET_PERI_REG_BITS(SENS_SAR_DAC_CTRL1_REG, SENS_SW_FSTEP, frequency, SENS_SW_FSTEP_S);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Scale output of a DAC channel using two bit pattern:
|
||||||
|
*
|
||||||
|
* - 00: no scale
|
||||||
|
* - 01: scale to 1/2
|
||||||
|
* - 10: scale to 1/4
|
||||||
|
* - 11: scale to 1/8
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void dac_scale_set(dac_channel_t channel, int scale)
|
||||||
|
{
|
||||||
|
switch(channel) {
|
||||||
|
case DAC_CHANNEL_1:
|
||||||
|
SET_PERI_REG_BITS(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_SCALE1, scale, SENS_DAC_SCALE1_S);
|
||||||
|
break;
|
||||||
|
case DAC_CHANNEL_2:
|
||||||
|
SET_PERI_REG_BITS(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_SCALE2, scale, SENS_DAC_SCALE2_S);
|
||||||
|
break;
|
||||||
|
default :
|
||||||
|
printf("Channel %d\n", channel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Offset output of a DAC channel
|
||||||
|
*
|
||||||
|
* Range 0x00 - 0xFF
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void dac_offset_set(dac_channel_t channel, int offset)
|
||||||
|
{
|
||||||
|
switch(channel) {
|
||||||
|
case DAC_CHANNEL_1:
|
||||||
|
SET_PERI_REG_BITS(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_DC1, offset, SENS_DAC_DC1_S);
|
||||||
|
break;
|
||||||
|
case DAC_CHANNEL_2:
|
||||||
|
SET_PERI_REG_BITS(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_DC2, offset, SENS_DAC_DC2_S);
|
||||||
|
break;
|
||||||
|
default :
|
||||||
|
printf("Channel %d\n", channel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Invert output pattern of a DAC channel
|
||||||
|
*
|
||||||
|
* - 00: does not invert any bits,
|
||||||
|
* - 01: inverts all bits,
|
||||||
|
* - 10: inverts MSB,
|
||||||
|
* - 11: inverts all bits except for MSB
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
void dac_invert_set(dac_channel_t channel, int invert)
|
||||||
|
{
|
||||||
|
switch(channel) {
|
||||||
|
case DAC_CHANNEL_1:
|
||||||
|
SET_PERI_REG_BITS(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_INV1, invert, SENS_DAC_INV1_S);
|
||||||
|
break;
|
||||||
|
case DAC_CHANNEL_2:
|
||||||
|
SET_PERI_REG_BITS(SENS_SAR_DAC_CTRL2_REG, SENS_DAC_INV2, invert, SENS_DAC_INV2_S);
|
||||||
|
break;
|
||||||
|
default :
|
||||||
|
printf("Channel %d\n", channel);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void dactask(void* arg)
|
||||||
|
{
|
||||||
|
while(1){
|
||||||
|
|
||||||
|
// frequency setting is common to both channels
|
||||||
|
dac_frequency_set(frequency);
|
||||||
|
|
||||||
|
/* Tune parameters of channel 2 only
|
||||||
|
* to see and compare changes against channel 1
|
||||||
|
*/
|
||||||
|
dac_scale_set(DAC_CHANNEL_2, scale);
|
||||||
|
dac_offset_set(DAC_CHANNEL_2, offset);
|
||||||
|
dac_invert_set(DAC_CHANNEL_2, invert);
|
||||||
|
|
||||||
|
printf("DAC frequency: %5d, DAC2 scale: %d, offset %3d, invert: %d\n", frequency, scale, offset, invert);
|
||||||
|
vTaskDelay(1000/portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Generate a sine waveform on both DAC channels:
|
||||||
|
*
|
||||||
|
* DAC_CHANNEL_1 - GPIO25
|
||||||
|
* DAC_CHANNEL_2 - GPIO26
|
||||||
|
*
|
||||||
|
* Connect scope to both GPIO25 and GPIO26
|
||||||
|
* to observe the waveform changes
|
||||||
|
* in response to the parameter change
|
||||||
|
*/
|
||||||
|
void app_main()
|
||||||
|
{
|
||||||
|
dac_cosine_enable(DAC_CHANNEL_1);
|
||||||
|
dac_cosine_enable(DAC_CHANNEL_2);
|
||||||
|
|
||||||
|
dac_output_enable(DAC_CHANNEL_1);
|
||||||
|
dac_output_enable(DAC_CHANNEL_2);
|
||||||
|
|
||||||
|
xTaskCreate(dactask, "dactask", 1024*3, NULL, 10, NULL);
|
||||||
|
}
|
After Width: | Height: | Size: 37 KiB |
After Width: | Height: | Size: 69 KiB |
After Width: | Height: | Size: 53 KiB |
After Width: | Height: | Size: 252 KiB |
After Width: | Height: | Size: 37 KiB |
After Width: | Height: | Size: 36 KiB |
After Width: | Height: | Size: 34 KiB |
After Width: | Height: | Size: 38 KiB |
After Width: | Height: | Size: 1.9 MiB |