|
|
@@ -1,6 +1,8 @@
|
|
|
#include "gd32_adc.h"
|
|
|
#include "gpio.h"
|
|
|
#include "clock.h"
|
|
|
+#include "libs/shark_types.h"
|
|
|
+#include "libs/shark_task.h"
|
|
|
/* For 12-bits resolution, the total conversion time is
|
|
|
*sampling time + 12.5 ADCCLK cycles
|
|
|
*all channel is enabled oversample to reach 16bit accurate
|
|
|
@@ -12,6 +14,7 @@ static int volatile adc_work = ADC_WORK_IDLE;
|
|
|
#define dma_buf_len 66
|
|
|
static uint16_t dma_buf[dma_buf_len];
|
|
|
#endif
|
|
|
+int gd32_adc_error = 0;
|
|
|
void gd32_adc_init(void){
|
|
|
rcu_periph_clock_enable(RCU_GPIOA);
|
|
|
rcu_periph_clock_enable(RCU_GPIOB);
|
|
|
@@ -205,26 +208,28 @@ int adc_sample(int chan, int calibration){
|
|
|
return value & mask;
|
|
|
}
|
|
|
|
|
|
-//times * 0.6ms + 1ms
|
|
|
-int adc_sample_avg(int chan, int times){
|
|
|
+
|
|
|
+static int adc_sample_internal(int chan, int times, int *error) {
|
|
|
int value = 0;
|
|
|
int count = 0;
|
|
|
int min = 0xFFFFF;
|
|
|
int max = -0xFFFFF;
|
|
|
- //gd32_adc_init();
|
|
|
- //hardware oversample to 16bit
|
|
|
- //adc_oversample_mode_config(ADC_OVERSAMPLING_ALL_CONVERT, ADC_OVERSAMPLING_SHIFT_2B, ADC_OVERSAMPLING_RATIO_MUL64);
|
|
|
- //adc_oversample_mode_enable();
|
|
|
- /* use max convert time to make sure the adc work fine */
|
|
|
+
|
|
|
adc_regular_channel_config(0, chan, ADC_SAMPLETIME_55POINT5);////55.5 + 12.5 = 68 cycle, 68 * 256/(7*1000000)
|
|
|
adc_enable();
|
|
|
delay_us(1000); //MUST delay, for adc work fine
|
|
|
/* ADC calibration and reset calibration */
|
|
|
adc_calibration_enable();
|
|
|
-
|
|
|
+ *error = 0;
|
|
|
while(count < times){
|
|
|
+ u64 start_time = shark_get_mseconds();
|
|
|
adc_software_trigger_enable(ADC_REGULAR_CHANNEL);
|
|
|
- while(SET != adc_flag_get(ADC_FLAG_EOC));
|
|
|
+ while(SET != adc_flag_get(ADC_FLAG_EOC)){
|
|
|
+ if (shark_get_mseconds() - start_time >= 10){
|
|
|
+ *error = -1;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ };
|
|
|
int one = adc_regular_data_read();
|
|
|
adc_flag_clear(ADC_FLAG_EOC);
|
|
|
value += (one & 0xFFF);
|
|
|
@@ -241,5 +246,19 @@ int adc_sample_avg(int chan, int times){
|
|
|
return value/times;
|
|
|
}
|
|
|
return (value - min - max)/(times-2);
|
|
|
+
|
|
|
+}
|
|
|
+//times * 0.6ms + 1ms
|
|
|
+int adc_sample_avg(int chan, int times){
|
|
|
+ int error = 0;
|
|
|
+ int value = adc_sample_internal(chan, times, &error);
|
|
|
+ if (error < 0) {
|
|
|
+ gd32_adc_error ++;
|
|
|
+ gd32_adc_deinit();
|
|
|
+ delay_us(5 * 1000);
|
|
|
+ gd32_adc_init();
|
|
|
+ value = adc_sample_internal(chan, times, &error);
|
|
|
+ }
|
|
|
+ return value;
|
|
|
}
|
|
|
#endif /* DMA_ADC_CH */
|