1 // Copyright 2014 Olivier Gillet.
2 //
3 // Author: Olivier Gillet (ol.gillet@gmail.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in
13 // all copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 // THE SOFTWARE.
22 //
23 // See http://creativecommons.org/licenses/MIT/ for more information.
24 //
25 // -----------------------------------------------------------------------------
26 //
27 // Driver for ADC.
28
29 #include "warps/drivers/adc.h"
30
31 #include <stm32f4xx_conf.h>
32
33 namespace warps {
34
Init()35 void Adc::Init() {
36 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
37 RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
38 RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
39
40 DMA_InitTypeDef dma_init;
41 ADC_CommonInitTypeDef adc_common_init;
42 ADC_InitTypeDef adc_init;
43 GPIO_InitTypeDef gpio_init;
44
45 gpio_init.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
46 gpio_init.GPIO_Pin |= GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
47 gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
48 gpio_init.GPIO_Mode = GPIO_Mode_AN;
49 GPIO_Init(GPIOA, &gpio_init);
50
51 // Use DMA to automatically copy ADC data register to values_ buffer.
52 dma_init.DMA_Channel = DMA_Channel_0;
53 dma_init.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
54 dma_init.DMA_Memory0BaseAddr = (uint32_t)&values_[0];
55 dma_init.DMA_DIR = DMA_DIR_PeripheralToMemory;
56 dma_init.DMA_BufferSize = ADC_LAST;
57 dma_init.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
58 dma_init.DMA_MemoryInc = DMA_MemoryInc_Enable;
59 dma_init.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
60 dma_init.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
61 dma_init.DMA_Mode = DMA_Mode_Circular;
62 dma_init.DMA_Priority = DMA_Priority_High;
63 dma_init.DMA_FIFOMode = DMA_FIFOMode_Disable;
64 dma_init.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
65 dma_init.DMA_MemoryBurst = DMA_MemoryBurst_Single;
66 dma_init.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
67 DMA_Init(DMA2_Stream0, &dma_init);
68 DMA_Cmd(DMA2_Stream0, ENABLE);
69
70 adc_common_init.ADC_Mode = ADC_Mode_Independent;
71 adc_common_init.ADC_Prescaler = ADC_Prescaler_Div8;
72 adc_common_init.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
73 adc_common_init.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;
74 ADC_CommonInit(&adc_common_init);
75
76 adc_init.ADC_Resolution = ADC_Resolution_12b;
77 adc_init.ADC_ScanConvMode = ENABLE;
78 adc_init.ADC_ContinuousConvMode = DISABLE;
79 adc_init.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
80 adc_init.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
81 adc_init.ADC_DataAlign = ADC_DataAlign_Left;
82 adc_init.ADC_NbrOfConversion = ADC_LAST;
83 ADC_Init(ADC1, &adc_init);
84
85 // 168M / 2 / 8 / (8 x (480 + 20)) = 2.6kHz.
86 ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_480Cycles);
87 ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 2, ADC_SampleTime_480Cycles);
88 ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 3, ADC_SampleTime_480Cycles);
89 ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_480Cycles);
90 ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 5, ADC_SampleTime_480Cycles);
91 ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 6, ADC_SampleTime_480Cycles);
92 ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 7, ADC_SampleTime_480Cycles);
93 ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 8, ADC_SampleTime_480Cycles);
94
95 ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
96 ADC_Cmd(ADC1, ENABLE);
97 ADC_DMACmd(ADC1, ENABLE);
98 Convert();
99 }
100
DeInit()101 void Adc::DeInit() {
102 DMA_Cmd(DMA2_Stream0, DISABLE);
103 ADC_DMARequestAfterLastTransferCmd(ADC1, DISABLE);
104 ADC_Cmd(ADC1, DISABLE);
105 ADC_DMACmd(ADC1, DISABLE);
106 ADC_DeInit();
107 }
108
Convert()109 void Adc::Convert() {
110 ADC_SoftwareStartConv(ADC1);
111 }
112
113 } // namespace warps
114