| BlueNRG-1 Peripherals drivers Examples

- Main Page
- Modules
- Classes
- Files

- File List
- File Members

ADC_DMA_main.c
Go to the documentation of this file.
1
2 /******************** (C) COPYRIGHT 2016 STMicroelectronics ********************
3 * File Name : ADC_DMA_main.c
4 * Author : VMA DIVISION
5 * Version : 1.0.0
6 * Date : 21-September-2015
7 * Description : Code demostrating the ADC functionality
8 ********************************************************************************
9 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
10 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
11 * AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
12 * INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
13 * CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
14 * INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
15 *******************************************************************************/
16
166 /* Includes ------------------------------------------------------------------*/
167 #include "BlueNRG1.h"
168 #include "BlueNRG1_conf.h"
169 #include "SDK_EVAL_Config.h"
170 #include <stdio.h>
171 #include <stdlib.h>
172
185 /* Private typedef -----------------------------------------------------------*/
186 /* Private define ------------------------------------------------------------*/
187 #define SYST_CLOCK 16000000 /* System clock frequency */
188
189 #define ADC_OUT_ADDRESS (ADC_BASE + 0x14)
190 #define ADC_DMA_CH0 (DMA_CH0)
191
192 #define ADC_DMA_BUFFER_LEN (256)
193
194 /* Private macro -------------------------------------------------------------*/
195 /* Private variables ---------------------------------------------------------*/
196 volatile uint32_t lSystickCounter=0;
197 ADC_InitType xADC_InitType;
198 uint32_t buffer_adc[ADC_DMA_BUFFER_LEN];
199
200 /* Private function prototypes -----------------------------------------------*/
201 void SdkDelayMs(volatile uint32_t lTimeMs);
202 void ADC_Configuration(void);
203 void DMA_Configuration(void);
204
205 /* Private functions ---------------------------------------------------------*/
206
207
214 int main(void)
215 {
216 /* System initialization function */
217 SystemInit();
218
219 /* Identify BlueNRG1 platform */
220 SdkEvalIdentification();
221
222 /* LED initialization function */
223 SdkEvalLedInit(LED1);
224
225 /* UART initialization */
226 SdkEvalComUartInit(UART_BAUDRATE);
227
228 /* SysTick initialization 1ms */
229 SysTick_Config(SYST_CLOCK/1000 - 1);
230
231 /* DMA Initialization */
232 DMA_Configuration();
233
234 /* ADC Initialization */
235 ADC_Configuration();
236
237 /* ADC Initialization */
238 ADC_DmaCmd(ENABLE);
239
240 /* Start new conversion */
241 ADC_Cmd(ENABLE);
242
243 /* Infinite loop */
244 while(1) {
245
246
247 /* Check DMA_CH_UART_TX Transfer Complete interrupt */
248 if(DMA_GetFlagStatus(DMA_FLAG_TC0)) {
249 DMA_ClearFlag(DMA_FLAG_TC0);
250
251 /* ADC_DMA_CH0 disable */
252 DMA_Cmd(ADC_DMA_CH0, DISABLE);
253
254 printf("Acquisition from DMA channel 0\r\n");
255 for(uint16_t i=0;i<ADC_DMA_BUFFER_LEN;i++) {
256 printf("ADC value: %.0f mV\r\n", ADC_ConvertSingleEndedVoltage(buffer_adc[i], (uint8_t)ADC_Input_AdcPin1, xADC_InitType.ADC_ReferenceVoltage, xADC_InitType.ADC_Attenuation)*1000.0);
257 }
258
259 /* Application delay before next measurement */
260 SdkDelayMs(100);
261
262 /* Reload DMA channel 0 */
263 ADC_DMA_CH0->CMAR = (uint32_t)buffer_adc;
264 ADC_DMA_CH0->CNDTR = ADC_DMA_BUFFER_LEN;
265
266 /* Enable DMA channel 0 */
267 DMA_Cmd(ADC_DMA_CH0, ENABLE);
268
269 }
270
271 }
272 }
273
279 void SdkDelayMs(volatile uint32_t lTimeMs)
280 {
281 uint32_t nWaitPeriod = ~lSystickCounter;
282
283 if(nWaitPeriod<lTimeMs)
284 {
285 while( lSystickCounter != 0xFFFFFFFF);
286 nWaitPeriod = lTimeMs-nWaitPeriod;
287 }
288 else
289 nWaitPeriod = lTimeMs+ ~nWaitPeriod;
290
291 while( lSystickCounter != nWaitPeriod ) ;
292
293 }
294
295
301 void ADC_Configuration(void)
302 {
303 SysCtrl_PeripheralClockCmd(CLOCK_PERIPH_ADC, ENABLE);
304
305 /* Configure ADC */
306 /* ADC_Input_AdcPin1 == ADC1 */
307 /* ADC_Input_AdcPin2 == ADC2 */
308 /* ADC_Input_AdcPin12 == ADC1 - ADC2 */
309 xADC_InitType.ADC_DecimationRate = ADC_DecimationRate_200;
310 //ADC_Input_BattSensor; //ADC_Input_TempSensor;// ADC_Input_AdcPin1 // ADC_Input_AdcPin12 // ADC_Input_AdcPin2
311 xADC_InitType.ADC_Input = ADC_Input_AdcPin12; //ADC_Input_AdcPin12;
312 xADC_InitType.ADC_ConversionMode = ADC_ConversionMode_Continuous;
313 xADC_InitType.ADC_ReferenceVoltage = ADC_ReferenceVoltage_0V6; //ADC_ReferenceVoltage_0V6;
314 xADC_InitType.ADC_Attenuation = ADC_Attenuation_9dB54;
315
316 ADC_Init(&xADC_InitType);
317
318 /* Enable auto offset correction */
319 ADC_Calibration(ENABLE);
320 ADC_AutoOffsetUpdate(ENABLE);
321 }
322
323
329 void DMA_Configuration(void)
330 {
331 DMA_InitType DMA_InitStructure;
332
333 SysCtrl_PeripheralClockCmd(CLOCK_PERIPH_DMA, ENABLE);
334
335 /* DMA_CH_UART_TX Initialization */
336 DMA_InitStructure.DMA_PeripheralBaseAddr = ADC_OUT_ADDRESS;
337 DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)buffer_adc;
338 DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
339 DMA_InitStructure.DMA_BufferSize = (uint32_t)ADC_DMA_BUFFER_LEN;
340 DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
341 DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
342 DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word;
343 DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word;
344 DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //DMA_Mode_Circular;
345 DMA_InitStructure.DMA_Priority = DMA_Priority_High;
346 DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
347 DMA_Init(ADC_DMA_CH0, &DMA_InitStructure);
348
349 /* Enable DMA ADC CHANNEL 0 Transfer Complete interrupt */
350 DMA_FlagConfig(ADC_DMA_CH0, DMA_FLAG_TC, ENABLE);
351
352 /* Select DMA ADC CHANNEL 0 */
353 DMA_SelectAdcChannel(DMA_ADC_CHANNEL0, ENABLE);
354
355 /* Enable DMA ADC CHANNEL 0 */
356 DMA_Cmd(ADC_DMA_CH0, ENABLE);
357 }
358
359 #ifdef USE_FULL_ASSERT
360
367 void assert_failed(uint8_t* file, uint32_t line)
368 {
369 /* User can add his own implementation to report the file name and line number,
370 ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
371
372 /* Infinite loop */
373 while (1)
374 {
375 }
376 }
377
378 #endif
379
380
393 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/
buffer_adc
uint32_t buffer_adc[ADC_DMA_BUFFER_LEN]
Definition: ADC_DMA_main.c:198
ADC_Configuration
void ADC_Configuration(void)
ADC_Configuration.
Definition: ADC_DMA_main.c:301
ADC_DMA_CH0
#define ADC_DMA_CH0
Definition: ADC_DMA_main.c:190
SYST_CLOCK
#define SYST_CLOCK
Definition: ADC_DMA_main.c:187
ADC_OUT_ADDRESS
#define ADC_OUT_ADDRESS
Definition: ADC_DMA_main.c:189
ADC_DMA_BUFFER_LEN
#define ADC_DMA_BUFFER_LEN
Definition: ADC_DMA_main.c:192
xADC_InitType
ADC_InitType xADC_InitType
Definition: ADC_DMA_main.c:197
DMA_Configuration
void DMA_Configuration(void)
DMA_Configuration.
Definition: ADC_DMA_main.c:329
lSystickCounter
volatile uint32_t lSystickCounter
Definition: ADC_DMA_main.c:196
SdkDelayMs
void SdkDelayMs(volatile uint32_t lTimeMs)
Delay function in ms.
Definition: ADC_DMA_main.c:279
UART_BAUDRATE
#define UART_BAUDRATE
Definition: ADC_PDM_main.c:225
main
int main(void)
Main program.
Definition: ADC_DMA_main.c:214