| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dac.c
Go to the documentation of this file.
1 /*
2 * @brief DAC example
3 * This example show how to use the D/A Conversion in 2 modes: Polling and DMA
4 *
5 * @note
6 * Copyright(C) NXP Semiconductors, 2012
7 * All rights reserved.
8 *
9 * @par
10 * Software that is described herein is for illustrative purposes only
11 * which provides customers with programming information regarding the
12 * LPC products. This software is supplied "AS IS" without any warranties of
13 * any kind, and NXP Semiconductors and its licensor disclaim any and
14 * all warranties, express or implied, including all implied warranties of
15 * merchantability, fitness for a particular purpose and non-infringement of
16 * intellectual property rights. NXP Semiconductors assumes no responsibility
17 * or liability for the use of the software, conveys no license or rights under any
18 * patent, copyright, mask work right, or any other intellectual property rights in
19 * or to any products. NXP Semiconductors reserves the right to make changes
20 * in the software without notification. NXP Semiconductors also makes no
21 * representation or warranty that such application will be suitable for the
22 * specified use without further testing or modification.
23 *
24 * @par
25 * Permission to use, copy, modify, and distribute this software and its
26 * documentation is hereby granted, under NXP Semiconductors' and its
27 * licensor's relevant copyrights in the software, without fee, provided that it
28 * is used in conjunction with NXP Semiconductors microcontrollers. This
29 * copyright, permission, and disclaimer notice must appear in all copies of
30 * this code.
31 */
32
33 #include "board.h"
34
72 /*****************************************************************************
73 * Private types/enumerations/variables
74 ****************************************************************************/
75
76 #define DATA_SIZE 0x400
77 static const char WelcomeMenu[] =
78 "Hello NXP Semiconductors \r\n"
79 "DAC DEMO \r\n"
80 "Press \'c\' to continue or \'x\' to quit\r\n";
81 static const char SelectMenu[] =
82 "Press number 1-2 to choose DAC running mode:\r\n"
83 "\t1: Polling Mode \r\n"
84 "\t2: DMA Mode \r\n";
85 static const char DirectionMenu[] =
86 "Press \'o\' or \'p\' to change sound frequency\r\n";
87
88 /* Work variables */
89 static volatile uint8_t channelTC, dmaChannelNum;
90 static volatile uint8_t DAC_Interrupt_Done_Flag, Interrupt_Continue_Flag;
91 static uint32_t DMAbuffer;
92
93 /* DAC sample rate request time */
94 #define DAC_TIMEOUT 0x3FF
95
96 /*****************************************************************************
97 * Public types/enumerations/variables
98 ****************************************************************************/
99
100 /*****************************************************************************
101 * Private functions
102 ****************************************************************************/
103
104 /* DMA routine for DAC example */
105 static void App_DMA_Test(void)
106 {
107 uint32_t tmp = 0;
108 uint8_t freq_sound = 0xFF;
109 uint8_t uart_buffer = 0;
110
111 DEBUGOUT("DMA mode selected\r\n");
112 DEBUGOUT(DirectionMenu);
113
114 /* Initialize GPDMA controller */
115 Chip_GPDMA_Init(LPC_GPDMA);
116
117 /* Setup GPDMA interrupt */
118 NVIC_DisableIRQ(DMA_IRQn);
119 NVIC_SetPriority(DMA_IRQn, ((0x01 << 3) | 0x01));
120 NVIC_EnableIRQ(DMA_IRQn);
121
122 /* Get the free channel for DMA transfer */
123 dmaChannelNum = Chip_DMA_GetFreeChannel(LPC_GPDMA, GPDMA_CONN_DAC);
124
125 /* Output DAC value until get 'x' character */
126 while (uart_buffer != 'x') {
127 uart_buffer = DEBUGIN();
128 if (uart_buffer == 'p') {
129 freq_sound++;
130 }
131 else if (uart_buffer == 'o') {
132 freq_sound--;
133 }
134
135 /* Start D/A conversion */
136 tmp += (freq_sound % DATA_SIZE);
137 if (tmp == (DATA_SIZE - 1)) {
138 tmp = 0;
139 }
140
141 /* pre-format the data to DACR register */
142 DMAbuffer = (uint32_t) (DAC_VALUE(tmp) | DAC_BIAS_EN);
143 channelTC = 0;
144 Chip_DMA_Transfer(LPC_GPDMA, dmaChannelNum,
145 (uint32_t) &DMAbuffer,
146 GPDMA_CONN_DAC,
147 GPDMA_TRANSFERTYPE_M2P_CONTROLLER_DMA,
148 1);
149
150 /* Waiting for writing DAC value completed */
151 while (channelTC == 0) {}
152 }
153
154 /* Disable interrupts, release DMA channel */
155 Chip_DMA_Stop(LPC_GPDMA, dmaChannelNum);
156 NVIC_DisableIRQ(DMA_IRQn);
157 }
158
159 /* Polling routine for DAC example */
160 static void App_Polling_Test(void)
161 {
162 uint32_t tmp = 0;
163 uint8_t freq_sound = 0xFF;
164 uint8_t uart_buffer = 0;
165
166 DEBUGOUT("Select Polling mode\r\n");
167 DEBUGOUT(DirectionMenu);
168
169 while (uart_buffer != 'x') {
170 uart_buffer = DEBUGIN();
171 if (uart_buffer == 'p') {
172 freq_sound++;
173 }
174 else if (uart_buffer == 'o') {
175 freq_sound--;
176 }
177
178 tmp += (freq_sound % DATA_SIZE);
179 if (tmp == (DATA_SIZE - 1)) {
180 tmp = 0;
181 }
182 Chip_DAC_UpdateValue(LPC_DAC, tmp);
183
184 /* Wait for DAC (DMA) interrupt request */
185 while (!(Chip_DAC_GetIntStatus(LPC_DAC))) {}
186 }
187 }
188
189 /*****************************************************************************
190 * Public functions
191 ****************************************************************************/
192
197 void DMA_IRQHandler(void)
198 {
199 if (Chip_DMA_Interrupt(LPC_GPDMA, dmaChannelNum) == SUCCESS) {
200 channelTC++;
201 }
202 else {
203 /* Error, do nothing */
204 }
205 }
206
211 int main(void)
212 {
213 uint8_t bufferUART;
214 uint32_t dacClk;
215 bool end_Flag = false;
216
217 Board_Init();
218
219 /* Setup DAC pins for board and common CHIP code */
220 Chip_DAC_Init(LPC_DAC);
221
222 /* Setup DAC timeout for polled and DMA modes to 0x3FF */
223 #if defined(CHIP_LPC175X_6X)
224 /* 175x/6x devices have a DAC divider, set it to 1 */
225 Chip_Clock_SetPCLKDiv(SYSCTL_PCLK_DAC, SYSCTL_CLKDIV_1);
226 #endif
227 Chip_DAC_SetDMATimeOut(LPC_DAC, DAC_TIMEOUT);
228
229 /* Compute and show estimated DAC request time */
230 #if defined(CHIP_LPC175X_6X)
231 dacClk = Chip_Clock_GetPeripheralClockRate(SYSCTL_PCLK_DAC);
232 #else
233 dacClk = Chip_Clock_GetPeripheralClockRate();
234 #endif
235 DEBUGOUT("DAC base clock rate = %dHz, DAC request rate = %dHz\r\n",
236 dacClk, (dacClk / DAC_TIMEOUT));
237
238 /* Enable count and DMA support */
239 Chip_DAC_ConfigDAConverterControl(LPC_DAC, DAC_CNT_ENA | DAC_DMA_ENA);
240
241 while (!end_Flag) {
242 DEBUGOUT(WelcomeMenu);
243 while (!end_Flag) {
244 bufferUART = 0xFF;
245 bufferUART = DEBUGIN();
246 if (bufferUART == 'c') {
247 DEBUGOUT(SelectMenu);
248 bufferUART = 0xFF;
249 while (bufferUART == 0xFF) {
250 bufferUART = DEBUGIN();
251 if ((bufferUART != '1') && (bufferUART != '2') && (bufferUART != '3')) {
252 bufferUART = 0xFF;
253 }
254 }
255 switch (bufferUART) {
256 case '1': /* Polling Mode */
257 App_Polling_Test();
258 break;
259
260 case '2': /* DMA mode */
261 App_DMA_Test();
262 break;
263 }
264 break;
265 }
266 else if (bufferUART == 'x') {
267 end_Flag = true;
268 DEBUGOUT("DAC demo terminated!\r\n");
269 }
270 }
271 }
272
273 return 0;
274 }
275