1 | /* mbed Microcontroller Library
|
---|
2 | * Copyright (c) 2006-2013 ARM Limited
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | */
|
---|
16 | #include "mbed_assert.h"
|
---|
17 | #include "analogin_api.h"
|
---|
18 |
|
---|
19 | #include "cmsis.h"
|
---|
20 | #include "PeripheralPins.h"
|
---|
21 |
|
---|
22 | #include "iodefine.h"
|
---|
23 |
|
---|
24 | #define ANALOGIN_MEDIAN_FILTER 0
|
---|
25 |
|
---|
26 | static volatile uint16_t *ADCDR[] = {
|
---|
27 | &ADCADDRA,
|
---|
28 | &ADCADDRB,
|
---|
29 | &ADCADDRC,
|
---|
30 | &ADCADDRD,
|
---|
31 | &ADCADDRE,
|
---|
32 | &ADCADDRF,
|
---|
33 | &ADCADDRG,
|
---|
34 | &ADCADDRH,
|
---|
35 | };
|
---|
36 |
|
---|
37 | void analogin_init(analogin_t *obj, PinName pin) {
|
---|
38 | obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
|
---|
39 | MBED_ASSERT(obj->adc != (ADCName)NC);
|
---|
40 |
|
---|
41 | CPGSTBCR3 &= ~(1 << 1);
|
---|
42 | CPGSTBCR6 &= ~(1 << 7);
|
---|
43 |
|
---|
44 | // 15: ADF 14: ADIE 13: ADST, [12:9] TRGS..0
|
---|
45 | // [8:6] CKS 010 :: 340tclk
|
---|
46 | // [5:3] MDS 000 :: single mode
|
---|
47 | // [2:0] CH 000 :: AN0
|
---|
48 | ADCADCSR = 0x0080;
|
---|
49 |
|
---|
50 | pinmap_pinout(pin, PinMap_ADC);
|
---|
51 | }
|
---|
52 |
|
---|
53 | static inline uint32_t adc_read(analogin_t *obj) {
|
---|
54 | volatile uint16_t data;
|
---|
55 |
|
---|
56 | // Select the appropriate channel and start conversion
|
---|
57 | ADCADCSR &= 0xfff8;
|
---|
58 | ADCADCSR |= (1 << 13 | (obj->adc & 0x7));
|
---|
59 |
|
---|
60 | // Wait end of conversion
|
---|
61 | do {
|
---|
62 | data = ADCADCSR;
|
---|
63 | } while (((data & (1 << 15)) == 0) || ((data & (1 << 13)) != 0));
|
---|
64 |
|
---|
65 | // clear flag
|
---|
66 | ADCADCSR &= ~(1 << 15);
|
---|
67 |
|
---|
68 | return ((*(ADCDR[obj->adc])) >> 4) & 0x0FFF; // 12 bits range
|
---|
69 | }
|
---|
70 |
|
---|
71 | #if ANALOGIN_MEDIAN_FILTER
|
---|
72 | static inline void order(uint32_t *a, uint32_t *b) {
|
---|
73 | if (*a > *b) {
|
---|
74 | uint32_t t = *a;
|
---|
75 | *a = *b;
|
---|
76 | *b = t;
|
---|
77 | }
|
---|
78 | }
|
---|
79 | #endif
|
---|
80 |
|
---|
81 | static inline uint32_t adc_read_u32(analogin_t *obj) {
|
---|
82 | uint32_t value;
|
---|
83 | #if ANALOGIN_MEDIAN_FILTER
|
---|
84 | uint32_t v1 = adc_read(obj);
|
---|
85 | uint32_t v2 = adc_read(obj);
|
---|
86 | uint32_t v3 = adc_read(obj);
|
---|
87 | order(&v1, &v2);
|
---|
88 | order(&v2, &v3);
|
---|
89 | order(&v1, &v2);
|
---|
90 | value = v2;
|
---|
91 | #else
|
---|
92 | value = adc_read(obj);
|
---|
93 | #endif
|
---|
94 | return value;
|
---|
95 | }
|
---|
96 |
|
---|
97 | uint16_t analogin_read_u16(analogin_t *obj) {
|
---|
98 | uint32_t value = adc_read_u32(obj);
|
---|
99 |
|
---|
100 | return (value << 4) | ((value >> 8) & 0x000F); // 12-bit to 16-bit conversion
|
---|
101 | }
|
---|
102 |
|
---|
103 | float analogin_read(analogin_t *obj) {
|
---|
104 | uint32_t value = adc_read_u32(obj);
|
---|
105 |
|
---|
106 | return (float)value * (1.0f / (float)0x0FFF); // 12 bits range
|
---|
107 | }
|
---|