1 // Copyright 2014 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 ADC2 - used for scanning pots.
28 
29 #include "elements/drivers/pots_adc.h"
30 
31 #include <stm32f4xx_conf.h>
32 
33 namespace elements {
34 
35 /* static */
36 uint8_t PotsAdc::addresses_[POT_LAST] = {
37   28,  // POT_EXCITER_ENVELOPE_SHAPE,
38   30,  // POT_EXCITER_BOW_LEVEL,
39   5,  // POT_EXCITER_BOW_TIMBRE,
40   1,  // POT_EXCITER_BOW_TIMBRE_ATTENUVERTER,
41   29,  // POT_EXCITER_BLOW_LEVEL,
42   31,  // POT_EXCITER_BLOW_META,
43   2,  // POT_EXCITER_BLOW_META_ATTENUVERTER,
44   7,  // POT_EXCITER_BLOW_TIMBRE,
45   11,  // POT_EXCITER_BLOW_TIMBRE_ATTENUVERTER,
46   22,  // POT_EXCITER_STRIKE_LEVEL,
47   20,  // POT_EXCITER_STRIKE_META,
48   8,  // POT_EXCITER_STRIKE_META_ATTENUVERTER,
49   6,  // POT_EXCITER_STRIKE_TIMBRE,
50   9,  // POT_EXCITER_STRIKE_TIMBRE_ATTENUVERTER,
51   23,  // POT_RESONATOR_COARSE,
52   26,  // POT_RESONATOR_FINE,
53   25,  // POT_RESONATOR_FM_ATTENUVERTER,
54   21,  // POT_RESONATOR_GEOMETRY,
55   19,  // POT_RESONATOR_GEOMETRY_ATTENUVERTER,
56   24,  // POT_RESONATOR_BRIGHTNESS,
57   17,  // POT_RESONATOR_RIGHTNESS_ATTENUVERTER,
58   4,  // POT_RESONATOR_DAMPING,
59   10,  // POT_RESONATOR_DAMPING_ATTENUVERTER,
60   0,  // POT_RESONATOR_POSITION,
61   16,  // POT_RESONATOR_POSITION_ATTENUVERTER,
62   27,  // POT_SPACE,
63   18,  // POT_SPACE_ATTENUVERTER
64 };
65 
Init()66 void PotsAdc::Init() {
67   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE);
68   RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);
69 
70   ADC_CommonInitTypeDef adc_common_init;
71   ADC_InitTypeDef adc_init;
72   GPIO_InitTypeDef gpio_init;
73 
74   // Configure the ADC pin.
75   gpio_init.GPIO_Pin = GPIO_Pin_0;
76   gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
77   gpio_init.GPIO_Mode = GPIO_Mode_AN;
78   GPIO_Init(GPIOC, &gpio_init);
79 
80   // Configure the mux chip selector and inhibit all chips.
81   gpio_init.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8;
82   gpio_init.GPIO_Mode = GPIO_Mode_OUT;
83   gpio_init.GPIO_OType = GPIO_OType_PP;
84   gpio_init.GPIO_Speed = GPIO_Speed_2MHz;
85   gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
86   GPIO_Init(GPIOB, &gpio_init);
87   GPIO_SetBits(GPIOB, GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8);
88 
89   // Configure the address lines.
90   gpio_init.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
91   gpio_init.GPIO_Mode = GPIO_Mode_OUT;
92   gpio_init.GPIO_OType = GPIO_OType_PP;
93   gpio_init.GPIO_Speed = GPIO_Speed_2MHz;
94   gpio_init.GPIO_PuPd = GPIO_PuPd_NOPULL;
95   GPIO_Init(GPIOC, &gpio_init);
96   GPIO_ResetBits(GPIOC, GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15);
97 
98   adc_common_init.ADC_Mode = ADC_Mode_Independent;
99   adc_common_init.ADC_Prescaler = ADC_Prescaler_Div8;
100   adc_common_init.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
101   adc_common_init.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
102   ADC_CommonInit(&adc_common_init);
103 
104   adc_init.ADC_Resolution = ADC_Resolution_12b;
105   adc_init.ADC_ScanConvMode = DISABLE;
106   adc_init.ADC_ContinuousConvMode = DISABLE;
107   adc_init.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
108   adc_init.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
109   adc_init.ADC_DataAlign = ADC_DataAlign_Left;
110   adc_init.ADC_NbrOfConversion = 1;
111   ADC_Init(ADC2, &adc_init);
112 
113   ADC_RegularChannelConfig(ADC2, ADC_Channel_10, 1, ADC_SampleTime_480Cycles);
114 
115   ADC_Cmd(ADC2, ENABLE);
116 
117   index_ = POT_LAST - 1;
118   last_read_ = 0;
119   state_ = false;
120   Scan();
121 }
122 
DeInit()123 void PotsAdc::DeInit() {
124   ADC_DeInit();
125 }
126 
Scan()127 void PotsAdc::Scan() {
128   if (state_) {
129     // Read the value from the previous conversion.
130     values_[index_] = ADC2->DR;
131     last_read_ = index_;
132     ++index_;
133     if (index_ >= POT_LAST) {
134       index_ = 0;
135     }
136 
137     uint8_t address = addresses_[index_];
138 
139     // Inhibit all muxes.
140     GPIO_SetBits(GPIOB, GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8);
141 
142     // Write the mux address.
143     GPIO_WriteBit(GPIOC, GPIO_Pin_15, static_cast<BitAction>(address & 1));
144     GPIO_WriteBit(GPIOC, GPIO_Pin_14, static_cast<BitAction>(address & 2));
145     GPIO_WriteBit(GPIOC, GPIO_Pin_13, static_cast<BitAction>(address & 4));
146 
147     // Activate the right mux.
148     GPIO_ResetBits(GPIOB, GPIO_Pin_5 << (address >> 3));
149   } else {
150     ADC_SoftwareStartConv(ADC2);
151   }
152   state_ = !state_;
153 }
154 
155 }  // namespace elements
156