forked from Foundation-Devices/passport2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadc.c
327 lines (271 loc) · 9.74 KB
/
adc.c
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// SPDX-FileCopyrightText: © 2020 Foundation Devices, Inc. <[email protected]>
// SPDX-License-Identifier: GPL-3.0-or-later
//
// Copyright 2020 - Foundation Devices Inc.
//
#ifndef FACTORY_TEST
#include <stdio.h>
#include <stdlib.h>
#else
#define printf(fmt, args...)
#endif
#include <string.h>
#include "stm32h7xx_hal.h"
#include "adc.h"
#define MAX_ADC_16BIT 65535
#define REF_VOLTAGE_MV 3000
#define MAX_SAMPLES_CNT 0xFFFF
#define MILLIVOLTS_PER_REVISION 500
#define PWRMON_I_SENSE_RESISTOR 5
/*
* When doing single samples you cannot rely on
* the value being exact, so adding a small offset
* to the computed milli-volts resolves that issue.
*/
#define BOARD_REV_MV_OFFSET 20
/*
* The number of samples to collect for the average
* Current bounces around a lot so take more samples
* and use an average.
* Voltage may not be as bad so take fewer samples
*/
#define MAX_I_SAMPLES 20
#define MAX_V_SAMPLES 4
static ADC_HandleTypeDef hadc2;
static HAL_StatusTypeDef adc2_init(void) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
HAL_StatusTypeDef rc;
hadc2.Instance = ADC2;
rc = HAL_ADC_DeInit(&hadc2);
if (rc != HAL_OK) {
printf("Failed to DeInit ADC2\n");
return rc;
}
__HAL_RCC_ADC12_CLK_ENABLE();
/* ADC Periph interface clock configuration */
__HAL_RCC_ADC_CONFIG(RCC_ADCCLKSOURCE_CLKP);
/**ADC2 GPIO Configuration
PC0 ------> ADC2_INP10 - NOISE_OUT2
PC1 ------> ADC2_INP11 - NOISE_OUT1
PC4 ------> ADC2_INP4 - PWRMON_V
PC5 ------> ADC2_INP8 - PWRMON_I
*/
GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_4 | GPIO_PIN_5; //
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/** Common config
*/
// The ClockPrescaler can only be modified if ALL ADC instances are disabled!!!
hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV4; // ADC_CLOCK_ASYNC_DIV1
hadc2.Init.Resolution = ADC_RESOLUTION_16B;
hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE; // Needs to be ENABLE it processing more than 1 channel
hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV; // ADC_EOC_SEQ_CONV If doing more than one channel
hadc2.Init.LowPowerAutoWait = ENABLE; // Says to use this with Polling was DISABLE;
hadc2.Init.ContinuousConvMode = ENABLE;
hadc2.Init.NbrOfConversion = 1; // Number of ranks to be converted within regular group sequencer
hadc2.Init.DiscontinuousConvMode = DISABLE; // Cannot be used if continuous mode is enabled.
hadc2.Init.NbrOfDiscConversion = 1;
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START; // NOTE: This is common to ALL ADC instances..
hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc2.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR; // Can specify DMA for management of converted data
hadc2.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; // ADC_OVR_DATA_PRESERVED
hadc2.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE;
/*
* Perform oversampling to read multiple samples
* and compute the average in HW.
*/
hadc2.Init.OversamplingMode = ENABLE;
hadc2.Init.Oversampling.Ratio = 0x20; /* Bit for 32x oversampling */
hadc2.Init.Oversampling.RightBitShift = ADC_RIGHTBITSHIFT_5;
hadc2.Init.Oversampling.TriggeredMode = ADC_TRIGGEREDMODE_SINGLE_TRIGGER;
hadc2.Init.Oversampling.OversamplingStopReset = ADC_REGOVERSAMPLING_CONTINUED_MODE;
rc = HAL_ADC_Init(&hadc2);
if (rc != HAL_OK) {
printf("Failed to init ADC2\n");
;
return rc;
}
/* Run the ADC calibration in single-ended mode */
rc = HAL_ADCEx_Calibration_Start(&hadc2, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
if (rc != HAL_OK) {
printf("ADC2 calibration failed\n");
return rc;
}
return HAL_OK;
}
void adc_enable_noise(void) {
GPIO_InitTypeDef GPIO_InitStruct = {0};
/*
* PD8 Amp2_enable
* PD9 Amp1_enable
* PD10 Noise Bias enable
*/
GPIO_InitStruct.Pin = GPIO_PIN_8 | GPIO_PIN_9 | GPIO_PIN_10;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_8, 1);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_9, 1);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_10, 1);
}
void adc_disable_noise(void) {
/*
* PD8 Amp2_enable
* PD9 Amp1_enable
* PD10 Noise Bias enable
*/
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_8, 0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_9, 0);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_10, 0);
}
/*
* read_noise_inputs() - Reads the two noise output channels and returns
* the count values read.
*/
int adc_read_noise_inputs(uint32_t* noise1, uint32_t* noise2) {
HAL_StatusTypeDef rc;
ADC_ChannelConfTypeDef sConfig = {0};
/* Configure Noiseout 1 input Channel */
sConfig.Channel = ADC_CHANNEL_11; // Noise 1 channel 11 PC1 INP11
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_8CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
sConfig.OffsetRightShift = DISABLE; /* No Right Offset Shift */
sConfig.OffsetSignedSaturation = DISABLE; /* No Signed Saturation */
rc = HAL_ADC_ConfigChannel(&hadc2, &sConfig);
if (rc != HAL_OK) {
printf("Failed to config ADC2 channel 8\n");
return -1;
}
/* Start processing for current (I) values */
rc = HAL_ADC_Start(&hadc2);
if (rc != HAL_OK) {
printf("ADC2 start failed\n");
return -1;
}
rc = HAL_ADC_PollForConversion(&hadc2, HAL_MAX_DELAY);
if (rc != HAL_OK) {
printf("ADC2 poll for conversion failed\n");
return -1;
}
*noise1 = HAL_ADC_GetValue(&hadc2);
rc = HAL_ADC_Stop(&hadc2);
if (rc != HAL_OK) {
printf("ADC2 start failed\n");
return -1;
}
/* Configure Noiseout 2 input Channel */
/* Noise 2 channel 10 PC0 INP10 */
sConfig.Channel = ADC_CHANNEL_10;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_8CYCLES_5;
rc = HAL_ADC_ConfigChannel(&hadc2, &sConfig);
if (rc != HAL_OK) {
printf("Failed to config ADC2 channel 4\n");
;
return -1;
}
/* Now sample for Noise output 2 */
rc = HAL_ADC_Start(&hadc2);
if (rc != HAL_OK) {
printf("ADC2 start failed\n");
return -1;
}
rc = HAL_ADC_PollForConversion(&hadc2, HAL_MAX_DELAY);
if (rc != HAL_OK) {
printf("ADC2 poll for conversion failed\n");
return -1;
}
*noise2 = HAL_ADC_GetValue(&hadc2);
rc = HAL_ADC_Stop(&hadc2);
if (rc != HAL_OK) {
printf("ADC2 start failed\n");
return -1;
}
return 0;
}
/*
* adc_read_powermon() Reads the power monitor current and voltage channels
*/
int adc_read_powermon(uint16_t* current, uint16_t* voltage) {
HAL_StatusTypeDef rc;
ADC_ChannelConfTypeDef sConfig = {0};
uint32_t adc_value_i;
uint32_t adc_value_v;
/* Configure Regular Power Monitor Current Channel */
sConfig.Channel = ADC_CHANNEL_8; // PWRMON_I channel PC5 INP8
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_8CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
sConfig.OffsetRightShift = DISABLE; /* No Right Offset Shift */
sConfig.OffsetSignedSaturation = DISABLE; /* No Signed Saturation */
rc = HAL_ADC_ConfigChannel(&hadc2, &sConfig);
if (rc != HAL_OK) {
printf("Failed to config ADC2 channel 8\n");
;
return -1;
}
// Start processing for current (I) values
rc = HAL_ADC_Start(&hadc2);
if (rc != HAL_OK) {
printf("ADC2 start failed\n");
return -1;
}
rc = HAL_ADC_PollForConversion(&hadc2, HAL_MAX_DELAY);
if (rc != HAL_OK) {
printf("ADC2 poll for conversion failed\n");
return -1;
}
adc_value_i = HAL_ADC_GetValue(&hadc2);
/*
* Current is I sense voltage divided by
* the sense resistor value which is 5 ohms
*/
*current = ((adc_value_i * REF_VOLTAGE_MV) / MAX_SAMPLES_CNT) / PWRMON_I_SENSE_RESISTOR;
rc = HAL_ADC_Stop(&hadc2);
if (rc != HAL_OK) {
printf("ADC2 start failed\n");
return -1;
}
/* Switch to the voltage channel */
/* PWRMON_V channel PC4 INP4 */
sConfig.Channel = ADC_CHANNEL_4;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_8CYCLES_5;
rc = HAL_ADC_ConfigChannel(&hadc2, &sConfig);
if (rc != HAL_OK) {
printf("Failed to config ADC2 channel 4\n");
;
return -1;
}
/* Now sample for voltage (V) */
rc = HAL_ADC_Start(&hadc2);
if (rc != HAL_OK) {
printf("ADC2 start failed\n");
return -1;
}
rc = HAL_ADC_PollForConversion(&hadc2, HAL_MAX_DELAY);
if (rc != HAL_OK) {
printf("ADC2 poll for conversion failed\n");
return -1;
}
adc_value_v = HAL_ADC_GetValue(&hadc2);
*voltage = (adc_value_v * REF_VOLTAGE_MV) / MAX_SAMPLES_CNT;
rc = HAL_ADC_Stop(&hadc2);
if (rc != HAL_OK) {
printf("ADC2 start failed\n");
return -1;
}
return 0;
}
int adc_init(void) {
HAL_StatusTypeDef rc;
rc = adc2_init();
if (rc != HAL_OK) return -1;
return 0;
}