]> git.gir.st - tmk_keyboard.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/hal/TARGET_RENESAS/TARGET_RZ_A1H/analogin_api.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[tmk_keyboard.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / hal / TARGET_RENESAS / TARGET_RZ_A1H / analogin_api.c
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 "pinmap.h"
21
22 #include "adc_iodefine.h"
23 #include "cpg_iodefine.h"
24
25 #define ANALOGIN_MEDIAN_FILTER 0
26
27 static const PinMap PinMap_ADC[] = {
28 {P1_8, AN0, 1},
29 {P1_9, AN1, 1},
30 {P1_10, AN2, 1},
31 {P1_11, AN3, 1},
32 {P1_12, AN3, 1},
33 {P1_13, AN5, 1},
34 {P1_14, AN5, 1},
35 {P1_15, AN7, 1},
36 {NC, NC, 0}
37 };
38
39 static volatile uint16_t *ADCDR[] = {
40 &ADCADDRA,
41 &ADCADDRB,
42 &ADCADDRC,
43 &ADCADDRD,
44 &ADCADDRE,
45 &ADCADDRF,
46 &ADCADDRG,
47 &ADCADDRH,
48 };
49
50 void analogin_init(analogin_t *obj, PinName pin) {
51 obj->adc = (ADCName)pinmap_peripheral(pin, PinMap_ADC);
52 MBED_ASSERT(obj->adc != (ADCName)NC);
53
54 CPGSTBCR3 &= ~(1 << 1);
55 CPGSTBCR6 &= ~(1 << 7);
56
57 // 15: ADF 14: ADIE 13: ADST, [12:9] TRGS..0
58 // [8:6] CKS 010 :: 340tclk
59 // [5:3] MDS 000 :: single mode
60 // [2:0] CH 000 :: AN0
61 ADCADCSR = 0x0080;
62
63 pinmap_pinout(pin, PinMap_ADC);
64 }
65
66 static inline uint32_t adc_read(analogin_t *obj) {
67 volatile uint16_t data;
68
69 // Select the appropriate channel and start conversion
70 ADCADCSR &= 0xfff8;
71 ADCADCSR |= (1 << 13 | (obj->adc & 0x7));
72
73 // Wait end of conversion
74 do {
75 data = ADCADCSR;
76 } while (((data & (1 << 15)) == 0) || ((data & (1 << 13)) != 0));
77
78 // clear flag
79 ADCADCSR &= ~(1 << 15);
80
81 return ((*(ADCDR[obj->adc])) >> 4) & 0x0FFF; // 12 bits range
82 }
83
84 #if ANALOGIN_MEDIAN_FILTER
85 static inline void order(uint32_t *a, uint32_t *b) {
86 if (*a > *b) {
87 uint32_t t = *a;
88 *a = *b;
89 *b = t;
90 }
91 }
92 #endif
93
94 static inline uint32_t adc_read_u32(analogin_t *obj) {
95 uint32_t value;
96 #if ANALOGIN_MEDIAN_FILTER
97 uint32_t v1 = adc_read(obj);
98 uint32_t v2 = adc_read(obj);
99 uint32_t v3 = adc_read(obj);
100 order(&v1, &v2);
101 order(&v2, &v3);
102 order(&v1, &v2);
103 value = v2;
104 #else
105 value = adc_read(obj);
106 #endif
107 return value;
108 }
109
110 uint16_t analogin_read_u16(analogin_t *obj) {
111 uint32_t value = adc_read_u32(obj);
112
113 return (value << 4) | ((value >> 8) & 0x000F); // 12-bit to 16-bit conversion
114 }
115
116 float analogin_read(analogin_t *obj) {
117 uint32_t value = adc_read_u32(obj);
118
119 return (float)value * (1.0f / (float)0x0FFF); // 12 bits range
120 }
Imprint / Impressum