25
25
#include "nu_modutil.h"
26
26
27
27
static uint32_t adc_modinit_mask = 0 ;
28
- volatile int adc_busy_flag = 0 ;
29
28
30
29
static const struct nu_modinit_s adc_modinit_tab [] = {
31
30
{ADC_0_0 , ADC_MODULE , CLK_CLKSEL1_ADC_S_HIRC , CLK_ADC_CLK_DIVIDER (1 ), ADC_RST , ADC_IRQn , NULL },
@@ -58,13 +57,7 @@ void analogin_init(analogin_t *obj, PinName pin)
58
57
59
58
ADC_T * adc_base = (ADC_T * ) NU_MODBASE (obj -> adc );
60
59
uint32_t chn = NU_MODSUBINDEX (obj -> adc );
61
-
62
- // Wait for ADC is not busy, due to all ADC channels share the same module
63
- while (adc_busy_flag != 0 ) {
64
- wait_us (100 );
65
- }
66
- adc_busy_flag = 1 ;
67
-
60
+
68
61
// NOTE: All channels (identified by ADCName) share a ADC module. This reset will also affect other channels of the same ADC module.
69
62
if (! adc_modinit_mask ) {
70
63
// Select clock source of paired channels
@@ -88,10 +81,8 @@ void analogin_init(analogin_t *obj, PinName pin)
88
81
// Just enable channel N
89
82
adc_base -> CHEN |= 1 << chn ;
90
83
}
91
-
84
+
92
85
adc_modinit_mask |= 1 << chn ;
93
-
94
- adc_busy_flag = 0 ;
95
86
}
96
87
97
88
void analogin_free (analogin_t * obj )
@@ -104,12 +95,6 @@ void analogin_free(analogin_t *obj)
104
95
105
96
ADC_T * adc_base = (ADC_T * ) NU_MODBASE (obj -> adc );
106
97
107
- // Wait for ADC is not busy, due to all ADC channels share the same module
108
- while (adc_busy_flag != 0 ) {
109
- wait_us (100 );
110
- }
111
- adc_busy_flag = 1 ;
112
-
113
98
/* Channel-level windup from here */
114
99
115
100
/* Mark channel free */
@@ -132,8 +117,6 @@ void analogin_free(analogin_t *obj)
132
117
CLK_DisableModuleClock (modinit -> clkidx );
133
118
}
134
119
135
- adc_busy_flag = 0 ;
136
-
137
120
/* Free up pins */
138
121
gpio_set (obj -> pin );
139
122
obj -> pin = NC ;
@@ -143,27 +126,19 @@ uint16_t analogin_read_u16(analogin_t *obj)
143
126
{
144
127
ADC_T * adc_base = (ADC_T * ) NU_MODBASE (obj -> adc );
145
128
uint32_t chn = NU_MODSUBINDEX (obj -> adc );
146
-
147
- // Wait for ADC is not busy, due to all ADC channels share the same module
148
- while (adc_busy_flag != 0 ) {
149
- wait_us (100 );
150
- }
151
- adc_busy_flag = 1 ;
152
-
129
+
153
130
// Start the A/D conversion
154
131
adc_base -> CR |= ADC_CR_ADST_Msk ;
155
132
// Wait for conversion finish
156
133
while (! ADC_GET_INT_FLAG (adc_base , ADC_ADF_INT ) & ADC_ADF_INT ) ;
157
134
ADC_CLR_INT_FLAG (ADC , ADC_ADF_INT );
158
135
uint16_t conv_res_12 = ADC_GET_CONVERSION_DATA (adc_base , chn );
159
-
160
- adc_busy_flag = 0 ;
161
-
136
+
162
137
// Just 12 bits are effective. Convert to 16 bits.
163
138
// conv_res_12: 0000 b11b10b9b8 b7b6b5b4 b3b2b1b0
164
139
// conv_res_16: b11b10b9b8 b7b6b5b4 b3b2b1b0 b11b10b9b8
165
140
uint16_t conv_res_16 = (conv_res_12 << 4 ) | (conv_res_12 >> 8 );
166
-
141
+
167
142
return conv_res_16 ;
168
143
}
169
144
0 commit comments