2525#include "nu_modutil.h"
2626
2727static uint32_t adc_modinit_mask = 0 ;
28- volatile int adc_busy_flag = 0 ;
2928
3029static const struct nu_modinit_s adc_modinit_tab [] = {
3130 {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)
5857
5958 ADC_T * adc_base = (ADC_T * ) NU_MODBASE (obj -> adc );
6059 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+
6861 // NOTE: All channels (identified by ADCName) share a ADC module. This reset will also affect other channels of the same ADC module.
6962 if (! adc_modinit_mask ) {
7063 // Select clock source of paired channels
@@ -88,10 +81,8 @@ void analogin_init(analogin_t *obj, PinName pin)
8881 // Just enable channel N
8982 adc_base -> CHEN |= 1 << chn ;
9083 }
91-
84+
9285 adc_modinit_mask |= 1 << chn ;
93-
94- adc_busy_flag = 0 ;
9586}
9687
9788void analogin_free (analogin_t * obj )
@@ -104,12 +95,6 @@ void analogin_free(analogin_t *obj)
10495
10596 ADC_T * adc_base = (ADC_T * ) NU_MODBASE (obj -> adc );
10697
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-
11398 /* Channel-level windup from here */
11499
115100 /* Mark channel free */
@@ -132,8 +117,6 @@ void analogin_free(analogin_t *obj)
132117 CLK_DisableModuleClock (modinit -> clkidx );
133118 }
134119
135- adc_busy_flag = 0 ;
136-
137120 /* Free up pins */
138121 gpio_set (obj -> pin );
139122 obj -> pin = NC ;
@@ -143,27 +126,19 @@ uint16_t analogin_read_u16(analogin_t *obj)
143126{
144127 ADC_T * adc_base = (ADC_T * ) NU_MODBASE (obj -> adc );
145128 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+
153130 // Start the A/D conversion
154131 adc_base -> CR |= ADC_CR_ADST_Msk ;
155132 // Wait for conversion finish
156133 while (! ADC_GET_INT_FLAG (adc_base , ADC_ADF_INT ) & ADC_ADF_INT ) ;
157134 ADC_CLR_INT_FLAG (ADC , ADC_ADF_INT );
158135 uint16_t conv_res_12 = ADC_GET_CONVERSION_DATA (adc_base , chn );
159-
160- adc_busy_flag = 0 ;
161-
136+
162137 // Just 12 bits are effective. Convert to 16 bits.
163138 // conv_res_12: 0000 b11b10b9b8 b7b6b5b4 b3b2b1b0
164139 // conv_res_16: b11b10b9b8 b7b6b5b4 b3b2b1b0 b11b10b9b8
165140 uint16_t conv_res_16 = (conv_res_12 << 4 ) | (conv_res_12 >> 8 );
166-
141+
167142 return conv_res_16 ;
168143}
169144
0 commit comments