]>
git.gir.st - tmk_keyboard.git/blob - 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
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include "mbed_assert.h"
17 #include "analogin_api.h"
22 #include "adc_iodefine.h"
23 #include "cpg_iodefine.h"
25 #define ANALOGIN_MEDIAN_FILTER 0
27 static const PinMap PinMap_ADC
[] = {
39 static volatile uint16_t *ADCDR
[] = {
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
);
54 CPGSTBCR3
&= ~(1 << 1);
55 CPGSTBCR6
&= ~(1 << 7);
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
63 pinmap_pinout(pin
, PinMap_ADC
);
66 static inline uint32_t adc_read(analogin_t
*obj
) {
67 volatile uint16_t data
;
69 // Select the appropriate channel and start conversion
71 ADCADCSR
|= (1 << 13 | (obj
->adc
& 0x7));
73 // Wait end of conversion
76 } while (((data
& (1 << 15)) == 0) || ((data
& (1 << 13)) != 0));
79 ADCADCSR
&= ~(1 << 15);
81 return ((*(ADCDR
[obj
->adc
])) >> 4) & 0x0FFF; // 12 bits range
84 #if ANALOGIN_MEDIAN_FILTER
85 static inline void order(uint32_t *a
, uint32_t *b
) {
94 static inline uint32_t adc_read_u32(analogin_t
*obj
) {
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
);
105 value
= adc_read(obj
);
110 uint16_t analogin_read_u16(analogin_t
*obj
) {
111 uint32_t value
= adc_read_u32(obj
);
113 return (value
<< 4) | ((value
>> 8) & 0x000F); // 12-bit to 16-bit conversion
116 float analogin_read(analogin_t
*obj
) {
117 uint32_t value
= adc_read_u32(obj
);
119 return (float)value
* (1.0f
/ (float)0x0FFF); // 12 bits range