1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
|
/* Copyright 2021 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include "adc.h"
#include "adc_chip.h"
#include "clock.h"
#include "common.h"
#include "console.h"
#include "dma.h"
#include "hooks.h"
#include "hwtimer.h"
#include "registers.h"
#include "task.h"
#include "timer.h"
#include "util.h"
struct mutex adc_lock;
struct adc_profile_t {
/* Register values. */
uint32_t cfgr1_reg;
uint32_t cfgr2_reg;
uint32_t smpr_reg; /* Default Sampling Rate */
uint32_t ier_reg;
/* DMA config. */
const struct dma_option *dma_option;
/* Size of DMA buffer, in units of ADC_CH_COUNT. */
int dma_buffer_size;
};
#ifdef CONFIG_ADC_PROFILE_SINGLE
#ifndef CONFIG_ADC_SAMPLE_TIME
#define CONFIG_ADC_SAMPLE_TIME STM32_ADC_SMPR_12_5_CY
#endif
#endif
#if defined(CHIP_FAMILY_STM32L4)
#define ADC_CALIBRATION_TIMEOUT_US 100000U
#define ADC_ENABLE_TIMEOUT_US 200000U
#define ADC_CONVERSION_TIMEOUT_US 200000U
#define NUMBER_OF_ADC_CHANNEL 2
uint8_t adc1_initialized;
#endif
#ifdef CONFIG_ADC_PROFILE_FAST_CONTINUOUS
#ifndef CONFIG_ADC_SAMPLE_TIME
#define CONFIG_ADC_SAMPLE_TIME STM32_ADC_SMPR_1_5_CY
#endif
static const struct dma_option dma_continuous = {
STM32_DMAC_ADC, (void *)&STM32_ADC_DR,
STM32_DMA_CCR_MSIZE_32_BIT | STM32_DMA_CCR_PSIZE_32_BIT |
STM32_DMA_CCR_CIRC,
};
static const struct adc_profile_t profile = {
/* Sample all channels continuously using DMA */
.cfgr1_reg = STM32_ADC_CFGR1_OVRMOD |
STM32_ADC_CFGR1_CONT |
STM32_ADC_CFGR1_DMACFG,
.cfgr2_reg = 0,
.smpr_reg = CONFIG_ADC_SAMPLE_TIME,
/* Fire interrupt at end of sequence. */
.ier_reg = STM32_ADC_IER_EOSEQIE,
.dma_option = &dma_continuous,
/* Double-buffer our samples. */
.dma_buffer_size = 2,
};
#endif
static void adc_init(const struct adc_t *adc)
{
/*
* If clock is already enabled, and ADC module is enabled
* then this is a warm reboot and ADC is already initialized.
*/
if (STM32_RCC_AHB2ENR & STM32_RCC_AHB2ENR_ADCEN &&
(STM32_ADC1_CR & STM32_ADC1_CR_ADEN))
return;
/* Enable ADC clock */
clock_enable_module(MODULE_ADC, 1);
/* set ADC clock to 20MHz */
STM32_ADC1_CCR &= ~0x003C0000;
STM32_ADC1_CCR |= 0x00080000;
STM32_RCC_AHB2ENR |= STM32_RCC_HB2_GPIOA;
STM32_RCC_AHB2ENR |= STM32_RCC_HB2_GPIOB;
/* Set ADC data resolution */
STM32_ADC1_CFGR &= ~STM32_ADC1_CFGR_CONT;
/* Set ADC conversion data alignment */
STM32_ADC1_CFGR &= ~STM32_ADC1_CFGR_ALIGN;
/* Set ADC delayed conversion mode */
STM32_ADC1_CFGR &= ~STM32_ADC1_CFGR_AUTDLY;
}
static void adc_configure(int ain_id, int ain_rank,
enum stm32_adc_smpr sample_rate)
{
/* Select Sampling time and channel to convert */
if (ain_id <= 10) {
STM32_ADC1_SMPR1 &= ~(7 << ((ain_id - 1) * 3));
STM32_ADC1_SMPR1 |= (sample_rate << ((ain_id - 1) * 3));
} else {
STM32_ADC1_SMPR2 &= ~(7 << ((ain_id - 11) * 3));
STM32_ADC1_SMPR2 |= (sample_rate << ((ain_id - 11) * 3));
}
/* Setup Rank */
STM32_ADC1_JSQR &= ~(0x03);
STM32_ADC1_JSQR |= NUMBER_OF_ADC_CHANNEL - 1;
STM32_ADC1_JSQR &= ~(0x1F << (((ain_rank - 1) * 6) + 8));
STM32_ADC1_JSQR |= (ain_id << (((ain_rank - 1) * 6) + 8));
/* Disable DMA */
STM32_ADC1_CFGR &= ~STM32_ADC1_CFGR_DMAEN;
}
int adc_read_channel(enum adc_channel ch)
{
const struct adc_t *adc = adc_channels + ch;
int value = 0;
uint32_t wait_loop_index;
mutex_lock(&adc_lock);
if (adc1_initialized == 0) {
adc_init(adc);
/* Configure Injected Channel N */
for (uint8_t i = 0; i < NUMBER_OF_ADC_CHANNEL; i++) {
const struct adc_t *adc = adc_channels + i;
adc_configure(adc->channel, adc->rank,
adc->sample_rate);
}
if ((STM32_ADC1_CR & STM32_ADC1_CR_ADEN) !=
STM32_ADC1_CR_ADEN) {
/* Disable ADC deep power down (enabled by default after
* reset state)
*/
STM32_ADC1_CR &= ~STM32_ADC1_CR_DEEPPWD;
/* Enable ADC internal voltage regulator */
STM32_ADC1_CR |= STM32_ADC1_CR_ADVREGEN;
}
/*
* Delay for ADC internal voltage regulator stabilization.
* Compute number of CPU cycles to wait for, from delay in us.
*
* Note: Variable divided by 2 to compensate partially
* CPU processing cycles (depends on compilation optimization).
*
* Note: If system core clock frequency is below 200kHz, wait
* time is only a few CPU processing cycles.
*/
wait_loop_index = ((20 * (80000000 / (100000 * 2))) / 10);
while (wait_loop_index-- != 0)
;
/* Run ADC self calibration */
STM32_ADC1_CR |= STM32_ADC1_CR_ADCAL;
/* wait for the end of calibration */
wait_loop_index = ((ADC_CALIBRATION_TIMEOUT_US *
(CPU_CLOCK / (100000 * 2))) / 10);
while (STM32_ADC1_CR & STM32_ADC1_CR_ADCAL) {
if (wait_loop_index-- == 0)
break;
}
/* Enable ADC */
STM32_ADC1_ISR |= STM32_ADC1_ISR_ADRDY;
STM32_ADC1_CR |= STM32_ADC1_CR_ADEN;
wait_loop_index = ((ADC_ENABLE_TIMEOUT_US *
(CPU_CLOCK / (100000 * 2))) / 10);
while (!(STM32_ADC1_ISR & STM32_ADC1_ISR_ADRDY)) {
wait_loop_index--;
if (wait_loop_index == 0)
break;
}
adc1_initialized = 1;
}
/* Start injected conversion */
STM32_ADC1_CR |= BIT(3); /* JADSTART */
/* Wait for end of injected conversion */
wait_loop_index = ((ADC_CONVERSION_TIMEOUT_US *
(CPU_CLOCK / (100000 * 2))) / 10);
while (!(STM32_ADC1_ISR & BIT(6))) {
if (wait_loop_index-- == 0)
break;
}
/* Clear JEOS bit */
STM32_ADC1_ISR |= BIT(6);
/* read converted value */
if (adc->rank == 1)
value = STM32_ADC1_JDR1;
if (adc->rank == 2)
value = STM32_ADC1_JDR2;
mutex_unlock(&adc_lock);
return value * adc->factor_mul / adc->factor_div + adc->shift;
}
void adc_disable(void)
{
/* Disable ADC */
/* Do not Set ADDIS when ADC is disabled */
adc1_initialized = 0;
if (STM32_ADC1_CR & STM32_ADC1_CR_ADEN)
STM32_ADC1_CR |= STM32_ADC1_CR_ADDIS;
/*
* Note that the ADC is not in OFF state immediately.
* Once the ADC is effectively put into OFF state,
* STM32_ADC_CR_ADDIS bit will be cleared by hardware.
*/
}
|