CE97088.pdf

CE97088 – PSoC® 4 ADC to PWM DMA Transfer
Objective
This code example demonstrates how to set up a PSoC® 4 direct memory access (DMA) channel to transfer data from the
ADC peripheral to the PWM.
Overview
This code example demonstrates a peripheral-to-peripheral data transfer using PSoC 4 DMA. The DMA transfers the ADC
result data to a PWM compare register. The change in ADC value creates a change in PWM duty. The transfer is triggered on
each ADC end of conversion (EOC). This example is a use case of the “Single data element per trigger” mode.
Requirements
Tool: PSoC Creator™ 3.2.
Associated Parts: PSoC 4200M and PSoC 4100M
Related Hardware: CY8CKIT-044
Design
Every DMA channel in PSoC 4 has two descriptor structures. The descriptor comprises information regarding the source and
destination address, the modes of transfer, and other specifics related to a transfer. You can choose to use one or both of the
descriptors in the channel. Refer to the DMA Component datasheet for more details on the use of descriptors.
In this code example, a single DMA channel is configured with a single descriptor, which has the source address as the ADC
result register and the destination as the PWM compare register. The trigger (tr_in) for the DMA is the ADC EOC. The transfer
mode is set as “Single data element per trigger,” which results in the ADC result being transferred to the PWM compare
register every time the EOC is asserted. Figure 1 illustrates the concept of this code example.
Figure 1. ADC-to-PWM DMA Transfer
DMA Channel
ADC
2 bytes
2 bytes
PWM
EOC
tr_in
Descriptor 0
The SAR ADC is set up to run at 218 sps and with a resolution of 12 bits. The PWM is implemented using the TCPWM and is
clocked at 12 MHz. The period of the PWM is selected to match the resolution of the SAR ADC. The sample rate of the SAR
ADC is selected as very slow, compared to the PWM frequency.
Figure 2 shows the DMA Component configuration.
www.cypress.com
Document No. 001-97088 Rev. **
1
PSoC® 4 ADC to PWM DMA Transfer
1.
The ADC result and the PWM compare register are both addressed as 32-bit registers in PSoC 4. Hence the Data
element size in the DMA configuration is set as 4 bytes. Though the relevant bits in the registers are only the lower 16
bits, the transfer needs to be 32 bits since 16-bit accesses from the peripheral registers are not supported.
2.
The descriptor is configured to transfer one data element before being considered complete. The next EOC retriggers the
DMA channel and the descriptor for another single-element transfer.
3.
The Transfer mode is selected as a “Single data element per trigger.” This means that one data element (a 32-bit word)
is transferred from the ADC to the PWM compare register each time the trigger (ADC EOC) is asserted.
Figure 2. DMA Configuration
1
2
3
The only code required for the DMA is to call the Start routine, available in the Component application programming interface
(API). The start function has source and destination addresses as parameters for the transfer.
DMA_1_Start((void*)ADC_SAR_Seq_SAR_CHAN0_RESULT_PTR,(void*)PWM_COMP_CAP_PTR);
The register names for the ADC result and PWM compare registers are located in the “Register” section of the corresponding
Component datasheets. The Registers Technical Reference Manual (TRM) includes the register names and addresses.
The source and destination addresses can also be set using the API functions DMA_SetSrcAddress() and
DMA_SetDstAddress().
Design Considerations
No additional code is required after the DMA initialization, when the CPU is free to do other tasks. The DMA can function in the
PSoC 4 Sleep mode, so you can put the device to sleep after initialization to reduce system power consumption.
Note When the voltage on the ADC is close to zero, there is a chance that the ADC result will go negative due to a negative
offset. This can create an unexpectedly large PWM duty cycle.
www.cypress.com
Document No. 001-97088 Rev. **
2
PSoC® 4 ADC to PWM DMA Transfer
Hardware Setup
The hardware setup explained in this section is for use with the CY8CKIT-044. Figure 3 shows the project schematic.


Input setup: Connect a voltage between Vdd and GND to P2[0], for example, using a potentiometer or signal generator.
Output setup: The PWM output is already connected to pin P0[6], which connects to the Red LED on CY8CKIT-044.
Figure 3. Project Schematic
Operation
Changing the input voltage will change the brightness of the Red LED on the CY8CKIT-044.
Note When the voltage on the ADC is close to zero, there is a chance that the ADC result will go negative due to a negative
offset. This will cause the LED to step to very high brightness when the input voltage is very close to zero.
Components
Table 1 lists the PSoC Creator Components used in this example, as well as the hardware resources used by each. The DMA
Component settings are shown in Figure 2.
Table 1. PSoC Creator Components
Component or User Module
Hardware Resources
ADC_SAR_SEQ
SAR ADC
DMA
1 DMA channel
PWM
TCPWM
www.cypress.com
Document No. 001-97088 Rev. **
3
PSoC® 4 ADC to PWM DMA Transfer
Related Documents
Table 2 lists the relevant application notes, code examples, knowledge base articles, device datasheets, and Component
datasheets.
Table 2. Related Documents
Application Notes
AN79953 – Getting
Started with PSoC 4
Introduces you to PSoC 4, an ARM® Cortex™-M0
MCU based programmable system-on-chip. It helps
you explore the architecture and PSoC Creator
development tools.
Code Examples
CE95351 – Fixed Function PWM with PSoC 4
CE95275Sequencing SAR ADC and Die Temperature Sensor with
PSoC 4
CE95272 – SAR ADC in Differential Mode using Pre-Amplifier with PSoC 4
PSoC Creator Component Datasheets
PSoC 4 Sequencing Successive Approximation ADC (ADC_SAR_Seq )
PSoC 4 Timer Counter Pulse Width Modulator (TCPWM)
Device Documentation
PSoC 4 Datasheets
PSoC 4 Technical Reference Manuals
Development Kit (DVK) Documentation
CY8CKIT-044 – PSoC 4 M-Series Pioneer Kit
For questions or suggestions on this code example please contact [email protected]
www.cypress.com
Document No. 001-97088 Rev. **
4
PSoC® 4 ADC to PWM DMA Transfer
Document History
Document Title: CE97088 - PSoC® 4 ADC to PWM DMA Transfer
Document Number: 001-97088
Revision
ECN
**
4866747
www.cypress.com
Orig. of
Change
QVS
Submission
Date
08/13/2015
Description of Change
New code example.
Document No. 001-97088 Rev. **
5
PSoC® 4 ADC to PWM DMA Transfer
Worldwide Sales and Design Support
Cypress maintains a worldwide network of offices, solution centers, manufacturer’s representatives, and distributors. To find
the office closest to you, visit us at Cypress Locations.
PSoC® Solutions
Products
Automotive
cypress.com/go/automotive
psoc.cypress.com/solutions
Clocks & Buffers
cypress.com/go/clocks
PSoC 1 |PSoC 3 | PSoC 4 |PSoC 5LP
Interface
cypress.com/go/interface
Lighting & Power Control
cypress.com/go/powerpsoc
cypress.com/go/plc
Memory
cypress.com/go/memory
PSoC
cypress.com/go/psoc
Touch Sensing
cypress.com/go/touch
USB Controllers
cypress.com/go/usb
Wireless/RF
cypress.com/go/wireless
Cypress Developer Community
Community | Forums |Blogs | Video |Training
Technical Support
cypress.com/go/support
PSoC is a registered trademark and PSoC Creator is a trademark of Cypress Semiconductor Corp. All other trademarks or registered trademarks
referenced herein are the property of their respective owners.
Cypress Semiconductor
198 Champion Court
San Jose, CA 95134-1709
Phone
Fax
Website
: 408-943-2600
: 408-943-4730
: www.cypress.com
© Cypress Semiconductor Corporation, 2015. The information contained herein is subject to change without notice. Cypress Semiconductor
Corporation assumes no responsibility for the use of any circuitry other than circuitry embodied in a Cypress product. Nor does it convey or imply any
license under patent or other rights. Cypress products are not warranted nor intended to be used for medical, life support, life saving, critical control or
safety applications, unless pursuant to an express written agreement with Cypress. Furthermore, Cypress does not authorize its products for use as
critical components in life-support systems where a malfunction or failure may reasonably be expected to result in significant injury to the user. The
inclusion of Cypress products in life-support systems application implies that the manufacturer assumes all risk of such use and in doing so indemnifies
Cypress against all charges.
This Source Code (software and/or firmware) is owned by Cypress Semiconductor Corporation (Cypress) and is protected by and subject to worldwide
patent protection (United States and foreign), United States copyright laws and international treaty provisions. Cypress hereby grants to licensee a
personal, non-exclusive, non-transferable license to copy, use, modify, create derivative works of, and compile the Cypress Source Code and derivative
works for the sole purpose of creating custom software and or firmware in support of licensee product to be used only in conjunction with a Cypress
integrated circuit as specified in the applicable agreement. Any reproduction, modification, translation, compilation, or representation of this Source
Code except as specified above is prohibited without the express written permission of Cypress.
Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS MATERIAL, INCLUDING, BUT
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress reserves the
right to make changes without further notice to the materials described herein. Cypress does not assume any liability arising out of the application or
use of any product or circuit described herein. Cypress does not authorize its products for use as critical components in life-support systems where a
malfunction or failure may reasonably be expected to result in significant injury to the user. The inclusion of Cypress’ product in a life-support systems
application implies that the manufacturer assumes all risk of such use and in doing so indemnifies Cypress against all charges.
Use may be limited by and subject to the applicable Cypress software license agreement.
www.cypress.com
Document No. 001-97088 Rev. **
6
Similar pages