1 // Copyright 2013 Emilie Gillet.
2 //
3 // Author: Emilie Gillet (emilie.o.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 DAC.
28
29 #include "frames/drivers/adc.h"
30
31 #include <stm32f10x_conf.h>
32
33 namespace frames {
34
Init(bool single_channel)35 void Adc::Init(bool single_channel) {
36 DMA_InitTypeDef dma_init;
37 ADC_InitTypeDef adc_init;
38 GPIO_InitTypeDef gpio_init;
39
40 gpio_init.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | \
41 GPIO_Pin_4 | GPIO_Pin_5;
42 gpio_init.GPIO_Speed = GPIO_Speed_10MHz;
43 gpio_init.GPIO_Mode = GPIO_Mode_AIN;
44 GPIO_Init(GPIOA, &gpio_init);
45
46 // Use DMA to automatically copy DAC data register to values_ buffer.
47 DMA_DeInit(DMA1_Channel1);
48 dma_init.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
49 dma_init.DMA_MemoryBaseAddr = (uint32_t)&values_[0];
50 dma_init.DMA_DIR = DMA_DIR_PeripheralSRC;
51 dma_init.DMA_BufferSize = kNumAdcChannels;
52 dma_init.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
53 dma_init.DMA_MemoryInc = DMA_MemoryInc_Enable;
54 dma_init.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
55 dma_init.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
56 dma_init.DMA_Mode = DMA_Mode_Circular;
57 dma_init.DMA_Priority = DMA_Priority_High;
58 dma_init.DMA_M2M = DMA_M2M_Disable;
59 DMA_Init(DMA1_Channel1, &dma_init);
60 DMA_Cmd(DMA1_Channel1, ENABLE);
61
62 ADC_DeInit(ADC1);
63 adc_init.ADC_Mode = ADC_Mode_Independent;
64 adc_init.ADC_ScanConvMode = ENABLE;
65 adc_init.ADC_ContinuousConvMode = ENABLE;
66 adc_init.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
67 adc_init.ADC_DataAlign = ADC_DataAlign_Left;
68 adc_init.ADC_NbrOfChannel = kNumAdcChannels;
69 ADC_Init(ADC1, &adc_init);
70
71 if (single_channel) {
72 ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_71Cycles5);
73 } else {
74 ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5);
75 ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5);
76 ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5);
77 ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_239Cycles5);
78 ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 5, ADC_SampleTime_239Cycles5);
79 ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 6, ADC_SampleTime_239Cycles5);
80 }
81
82 ADC_Cmd(ADC1, ENABLE);
83 ADC_ResetCalibration(ADC1);
84 while (ADC_GetResetCalibrationStatus(ADC1));
85 ADC_StartCalibration(ADC1);
86 while (ADC_GetCalibrationStatus(ADC1));
87
88 ADC_DMACmd(ADC1, ENABLE);
89 ADC_SoftwareStartConvCmd(ADC1, ENABLE);
90 }
91
DeInit()92 void Adc::DeInit() {
93 ADC_SoftwareStartConvCmd(ADC1, DISABLE);
94 ADC_DMACmd(ADC1, DISABLE);
95 ADC_Cmd(ADC1, DISABLE);
96 }
97
98 } // namespace frames
99