blob: f204aa151c384603c25bdb5b8aa8d4e529e3c787 [file] [log] [blame]
Kévin Redon9a12d682018-07-08 13:21:16 +02001/* This program is free software; you can redistribute it and/or modify
2 * it under the terms of the GNU General Public License as published by
3 * the Free Software Foundation; either version 2 of the License, or
4 * (at your option) any later version.
5 *
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
10 *
11 * You should have received a copy of the GNU General Public License
12 * along with this program; if not, write to the Free Software
13 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
14 */
Harald Welte67322482017-02-04 14:43:41 +010015#include "board.h"
16#include "boardver_adc.h"
17
18/* FIXME: share this with mode_cardemu.c */
19#define UV_PER_LSB ((3300 * 1000) / 4096)
20static uint32_t adc2uv(uint16_t adc)
21{
22 uint32_t uv = (uint32_t) adc * UV_PER_LSB;
23 return uv;
24}
25
26/***********************************************************************
27 * ADC for board version detection
28 ***********************************************************************/
29
30#ifdef PIN_VERSION_DET
31
32static int adc_sam3s_reva_errata = 0;
33
34static const Pin pin_version_det = PIN_VERSION_DET;
35
36/* Warning: Don't call this while other code (like the SIM VCC voltage
37 * reading) is running. The idea is you call this once during board
38 * startup and cache the result in a (global) variable if you need it
39 * later throughout the code */
40int get_board_version_adc(void)
41{
42 uint32_t chip_arch = CHIPID->CHIPID_CIDR & CHIPID_CIDR_ARCH_Msk;
43 uint32_t chip_ver = CHIPID->CHIPID_CIDR & CHIPID_CIDR_VERSION_Msk;
44 uint16_t sample;
45 uint32_t uv;
46
47 PIO_Configure(&pin_version_det, 1);
48
49 PMC_EnablePeripheral(ID_ADC);
50
51 ADC->ADC_CR |= ADC_CR_SWRST;
52 if (chip_ver == 0 &&
53 (chip_arch == CHIPID_CIDR_ARCH_SAM3SxA ||
54 chip_arch == CHIPID_CIDR_ARCH_SAM3SxB ||
55 chip_arch == CHIPID_CIDR_ARCH_SAM3SxC)) {
56 TRACE_INFO("Enabling Rev.A ADC Errata work-around\r\n");
57 adc_sam3s_reva_errata = 1;
58 }
59
60 if (adc_sam3s_reva_errata) {
61 /* Errata Work-Around to clear EOCx flags */
62 volatile uint32_t foo;
63 int i;
64 for (i = 0; i < 16; i++)
65 foo = ADC->ADC_CDR[i];
66 }
67
68 /* Initialize ADC for AD2, fADC=48/24=2MHz */
69 ADC->ADC_MR = ADC_MR_TRGEN_DIS | ADC_MR_LOWRES_BITS_12 |
70 ADC_MR_SLEEP_NORMAL | ADC_MR_FWUP_OFF |
71 ADC_MR_FREERUN_OFF | ADC_MR_PRESCAL(23) |
72 ADC_MR_STARTUP_SUT8 | ADC_MR_SETTLING(3) |
73 ADC_MR_ANACH_NONE | ADC_MR_TRACKTIM(4) |
74 ADC_MR_TRANSFER(1) | ADC_MR_USEQ_NUM_ORDER;
75 /* enable AD2 channel only */
76 ADC->ADC_CHER = ADC_CHER_CH2;
77
78 /* Make sure we don't use interrupts as that's what the SIM card
79 * VCC ADC code is using */
80 ADC->ADC_IER = 0;
81 NVIC_DisableIRQ(ADC_IRQn);
82
83 ADC->ADC_CR |= ADC_CR_START;
84
85 /* busy-wait, actually read the value */
86 do { } while (!(ADC->ADC_ISR & ADC_ISR_EOC2));
87 /* convert to voltage */
88 sample = ADC->ADC_CDR[2];
89 uv = adc2uv(sample);
90 TRACE_INFO("VERSION_DET ADC=%u => %u uV\r\n", sample, uv);
91
92 /* FIXME: convert to board version based on thresholds */
93
94 return 0;
95}
96
97#endif /* PIN_VERSION_DET */