1 /** mbed Microcontroller Library
2 ******************************************************************************
3 * @file analogin_api.c
4 * @author
5 * @version V1.0.0
6 * @date 2016-08-01
7 * @brief This file provides mbed API for ADC.
8 ******************************************************************************
9 * @attention
10 *
11 * This module is a confidential and proprietary property of RealTek and
12 * possession or use of this module requires written permission of RealTek.
13 *
14 * Copyright(c) 2016, Realtek Semiconductor Corporation. All rights reserved.
15 ******************************************************************************
16 */
17
18 #include "objects.h"
19 #include "PinNames.h"
20 #include "analogin_api.h"
21
22 #include "pinmap.h"
23
24 static const PinMap PinMap_ADC[] = {
25 {PB_4, ADC_CH0, 0},
26 {PB_5, ADC_CH1, 0},
27 {PB_6, ADC_CH2, 0},
28 {PB_7, ADC_CH3, 0},
29 {PB_1, ADC_CH4, 0},
30 {PB_2, ADC_CH5, 0},
31 {PB_3, ADC_CH6, 0},
32 {VBAT_MEAS, ADC_CH7, 0},
33 {NC, NC, 0}
34 };
35
36 /** @defgroup AmebaD_Mbed_API
37 * @{
38 */
39
40 /** @defgroup analog_in MBED_ADC
41 * @brief MBED_ADC driver modules.
42 * @{
43 */
44
45 /** @defgroup MBED_ADC_Exported_Functions MBED_ADC Exported Functions
46 * @{
47 */
48
49 /**
50 * @brief Initializes the ADC device, include clock/function/ADC registers.
51 * @param obj: adc object define in application software.
52 * @param pin: adc PinName according to pinmux spec.
53 * @retval none
54 */
analogin_init(analogin_t * obj,PinName pin)55 void analogin_init(analogin_t *obj, PinName pin)
56 {
57 ADC_InitTypeDef ADC_InitStruct;
58 uint32_t adc_idx;
59
60 adc_idx = pinmap_peripheral(pin, PinMap_ADC);
61 DBG_8195A("analogin_init [%x:%x ]\n", pin, adc_idx);
62 assert_param(adc_idx != NC);
63
64 /* Set ADC channel Number */
65 obj->adc_idx = adc_idx;
66
67 /* Initialize ADC */
68 ADC_StructInit(&ADC_InitStruct);
69 ADC_InitStruct.ADC_CvlistLen = 0;
70 ADC_Init(&ADC_InitStruct);
71
72 ADC_Cmd(ENABLE);
73 }
74
75 /**
76 * @brief Reads data from the specified adc channel fifo.
77 * @param obj: adc object define in application software.
78 * @retval adc channel data(float)
79 */
analogin_read(analogin_t * obj)80 float analogin_read(analogin_t *obj)
81 {
82 float value;
83 uint32_t AnalogDatFull = 0xFFF;
84 uint8_t ChIdx = obj->adc_idx;
85 uint32_t data;
86
87 /* Set channel index into channel switch list*/
88 ADC->ADC_CHSW_LIST[0] = ChIdx;
89
90 /* Clear FIFO */
91 ADC_ClearFIFO();
92
93 /* SW trigger to sample */
94 ADC_SWTrigCmd(ENABLE);
95 while(ADC_Readable()== 0);
96 ADC_SWTrigCmd(DISABLE);
97
98 data = ADC_Read();
99 value = (float)(data) / (float)(AnalogDatFull);
100
101 return (float)value;
102 }
103
104 /**
105 * @brief Reads data from the specified adc channel fifo.
106 * @param obj: adc object define in application software.
107 * @retval 16bit adc channel data(int)
108 */
analogin_read_u16(analogin_t * obj)109 uint16_t analogin_read_u16(analogin_t *obj)
110 {
111 uint8_t ChIdx = obj->adc_idx;
112 uint32_t data;
113
114 /* Set channel index into channel switch list*/
115 ADC->ADC_CHSW_LIST[0] = ChIdx;
116
117 /* Clear FIFO */
118 ADC_ClearFIFO();
119
120 /* SW trigger to sample */
121 ADC_SWTrigCmd(ENABLE);
122 while(ADC_Readable()== 0);
123 ADC_SWTrigCmd(DISABLE);
124
125 data = ADC_Read();
126
127 return (uint16_t)(data & BIT_MASK_DAT_GLOBAL);
128 }
129
130 /**
131 * @brief Deinitializes the ADC device, include clock/function/ADC registers.
132 * @param obj: adc object define in application software.
133 * @retval none
134 */
analogin_deinit(analogin_t * obj)135 void analogin_deinit(analogin_t *obj)
136 {
137 /* To avoid gcc warnings */
138 ( void ) obj;
139
140 /* Clear ADC Status */
141 ADC_INTClear();
142
143 /* Disable ADC */
144 ADC_Cmd(DISABLE);
145 }
146 /**
147 * @}
148 */
149
150 /**
151 * @}
152 */
153
154 /**
155 * @}
156 */
157 /******************* (C) COPYRIGHT 2016 Realtek Semiconductor *****END OF FILE****/
158