17
17
#include <NuMicro.h>
18
18
19
19
#define ADC_CONTEXT_USES_KERNEL_TIMER
20
+ #define ADC_CONTEXT_ENABLE_ON_COMPLETE
20
21
#include "adc_context.h"
21
22
22
23
LOG_MODULE_REGISTER (adc_numaker , CONFIG_ADC_LOG_LEVEL );
@@ -45,6 +46,7 @@ struct adc_numaker_data {
45
46
uint16_t * repeat_buffer ;
46
47
bool is_differential ;
47
48
uint32_t channels ;
49
+ uint32_t acq_time ;
48
50
};
49
51
50
52
static int adc_numaker_channel_setup (const struct device * dev ,
@@ -54,9 +56,13 @@ static int adc_numaker_channel_setup(const struct device *dev,
54
56
struct adc_numaker_data * data = dev -> data ;
55
57
56
58
if (chan_cfg -> acquisition_time != ADC_ACQ_TIME_DEFAULT ) {
57
- LOG_ERR ("Not support acquisition time" );
58
- return - ENOTSUP ;
59
+ if ((ADC_ACQ_TIME_UNIT (chan_cfg -> acquisition_time ) != ADC_ACQ_TIME_TICKS ) ||
60
+ (ADC_ACQ_TIME_VALUE (chan_cfg -> acquisition_time ) > 255 )) {
61
+ LOG_ERR ("Selected ADC acquisition time is not in 0~255 ticks" );
62
+ return - EINVAL ;
63
+ }
59
64
}
65
+ data -> acq_time = ADC_ACQ_TIME_VALUE (chan_cfg -> acquisition_time );
60
66
61
67
if (chan_cfg -> gain != ADC_GAIN_1 ) {
62
68
LOG_ERR ("Not support channel gain" );
@@ -115,9 +121,6 @@ static void adc_numaker_isr(const struct device *dev)
115
121
uint16_t conv_data ;
116
122
uint32_t pend_flag ;
117
123
118
- /* Clear pending flag first */
119
- pend_flag = eadc -> PENDSTS ;
120
- eadc -> PENDSTS = pend_flag ;
121
124
LOG_DBG ("ADC ISR pend flag: 0x%X\n" , pend_flag );
122
125
LOG_DBG ("ADC ISR STATUS2[0x%x] STATUS3[0x%x]" , eadc -> STATUS2 , eadc -> STATUS3 );
123
126
/* Complete the conversion of channels.
@@ -151,9 +154,6 @@ static void adc_numaker_isr(const struct device *dev)
151
154
eadc -> SCTL [module_id ] = 0 ;
152
155
}
153
156
154
- /* Disable ADC */
155
- EADC_Close (eadc );
156
-
157
157
/* Inform sampling is done */
158
158
adc_context_on_sampling_done (& data -> ctx , data -> dev );
159
159
}
@@ -179,6 +179,8 @@ static void m_adc_numaker_start_scan(const struct device *dev)
179
179
channel_mask &= ~BIT (channel_id );
180
180
EADC_ConfigSampleModule (eadc , module_id ,
181
181
EADC_SOFTWARE_TRIGGER , channel_id );
182
+ /* Set sample module external sampling time to 0 */
183
+ EADC_SetExtendSampleTime (eadc , module_id , data -> acq_time );
182
184
}
183
185
184
186
/* Clear the A/D ADINT0 interrupt flag for safe */
@@ -221,6 +223,19 @@ static void adc_context_update_buffer_pointer(struct adc_context *ctx,
221
223
}
222
224
}
223
225
226
+ static void adc_context_on_complete (struct adc_context * ctx , int status )
227
+ {
228
+ struct adc_numaker_data * data =
229
+ CONTAINER_OF (ctx , struct adc_numaker_data , ctx );
230
+ const struct adc_numaker_config * cfg = data -> dev -> config ;
231
+ EADC_T * eadc = cfg -> eadc_base ;
232
+
233
+ ARG_UNUSED (status );
234
+
235
+ /* Disable ADC */
236
+ EADC_Close (eadc );
237
+ }
238
+
224
239
static int m_adc_numaker_start_read (const struct device * dev ,
225
240
const struct adc_sequence * sequence )
226
241
{
@@ -243,14 +258,9 @@ static int m_adc_numaker_start_read(const struct device *dev,
243
258
244
259
/* Enable the A/D converter */
245
260
if (data -> is_differential ) {
246
- err = EADC_Open (eadc , EADC_CTL_DIFFEN_DIFFERENTIAL );
261
+ EADC_Open (eadc , EADC_CTL_DIFFEN_DIFFERENTIAL );
247
262
} else {
248
- err = EADC_Open (eadc , EADC_CTL_DIFFEN_SINGLE_END );
249
- }
250
-
251
- if (err ) {
252
- LOG_ERR ("ADC Open fail (%u)" , err );
253
- return - ENODEV ;
263
+ EADC_Open (eadc , EADC_CTL_DIFFEN_SINGLE_END );
254
264
}
255
265
256
266
data -> buffer = sequence -> buffer ;
0 commit comments