gd32_adc.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "gd32_adc.h"
  2. #include "gpio.h"
  3. #include "clock.h"
  4. /* For 12-bits resolution, the total conversion time is
  5. *sampling time + 12.5 ADCCLK cycles
  6. *all channel is enabled oversample to reach 16bit accurate
  7. */
  8. static int volatile adc_work = ADC_WORK_IDLE;
  9. //#define DMA_ADC_CH DMA_CH0
  10. #ifdef DMA_ADC_CH
  11. #define dma_irq DMA_Channel0_IRQn
  12. #define dma_buf_len 66
  13. static uint16_t dma_buf[dma_buf_len];
  14. #endif
  15. void adc_init(void){
  16. rcu_periph_clock_enable(RCU_GPIOA);
  17. rcu_periph_clock_enable(RCU_GPIOB);
  18. gpio_mode_analog_input(GPIOA, GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
  19. gpio_mode_analog_input(GPIOB, GPIO_PIN_1|GPIO_PIN_0);
  20. /* config ADC clock */
  21. rcu_adc_clock_config(RCU_ADCCK_APB2_DIV6); //adc clock:28M
  22. rcu_periph_clock_enable(RCU_ADC);
  23. adc_deinit();
  24. /* ADC trigger config */
  25. adc_external_trigger_source_config(ADC_REGULAR_CHANNEL, ADC_EXTTRIG_REGULAR_NONE);
  26. /* ADC data alignment config */
  27. adc_data_alignment_config(ADC_DATAALIGN_RIGHT);
  28. /* ADC channel length config */
  29. adc_channel_length_config(ADC_REGULAR_CHANNEL, 1);
  30. adc_resolution_config(ADC_RESOLUTION_12B);
  31. //adc_special_function_config(ADC_SCAN_MODE,ENABLE);
  32. adc_discontinuous_mode_config(ADC_REGULAR_CHANNEL, 1);
  33. adc_external_trigger_config(ADC_REGULAR_CHANNEL, ENABLE);
  34. }
  35. #ifdef DMA_ADC_CH
  36. void dma_config(void)
  37. {
  38. /* ADC_DMA_channel configuration */
  39. dma_parameter_struct dma_data_parameter;
  40. /* ADC DMA_channel configuration */
  41. dma_deinit(DMA_ADC_CH);
  42. /* initialize DMA single data mode */
  43. dma_data_parameter.periph_addr = (uint32_t)(&ADC_RDATA);
  44. dma_data_parameter.periph_inc = DMA_PERIPH_INCREASE_DISABLE;
  45. dma_data_parameter.memory_addr = (uint32_t)dma_buf;
  46. dma_data_parameter.memory_inc = DMA_MEMORY_INCREASE_DISABLE;
  47. dma_data_parameter.periph_width = DMA_PERIPHERAL_WIDTH_16BIT;
  48. dma_data_parameter.memory_width = DMA_MEMORY_WIDTH_16BIT;
  49. dma_data_parameter.direction = DMA_PERIPHERAL_TO_MEMORY;
  50. dma_data_parameter.number = 1U;
  51. dma_data_parameter.priority = DMA_PRIORITY_HIGH;
  52. dma_init(DMA_ADC_CH, &dma_data_parameter);
  53. dma_circulation_disable(DMA_ADC_CH);
  54. dma_interrupt_enable(DMA_ADC_CH, DMA_INT_FTF);
  55. nvic_priority_group_set(NVIC_PRIGROUP_PRE4_SUB0);
  56. nvic_irq_enable(dma_irq, 4U, 0U);
  57. }
  58. /* step 1 */
  59. int adc_start_sample(int chan){
  60. //hardware oversample to 16bit
  61. adc_oversample_mode_config(ADC_OVERSAMPLING_ALL_CONVERT, ADC_OVERSAMPLING_SHIFT_4B, ADC_OVERSAMPLING_RATIO_MUL256);
  62. adc_oversample_mode_enable();
  63. /* use max convert time to make sure the adc work fine */
  64. adc_regular_channel_config(0, chan, ADC_SAMPLETIME_55POINT5);//239.5 + 12.5 = 242 cycle, 242/(28*1000000)
  65. adc_enable();
  66. adc_work = ADC_WORK_ING;
  67. }
  68. /* step 2 */
  69. int adc_start_calibration(void){
  70. /* ADC calibration and reset calibration */
  71. adc_calibration_enable();
  72. }
  73. int adc_sample_avg(int chan, int times){
  74. /* configure the number of remaining data to be transferred */
  75. DMA_CHCNT(DMA_ADC_CH) = (times & DMA_CHANNEL_CNT_MASK);
  76. dma_channel_enable(DMA_ADC_CH);
  77. /* ADC DMA function enable */
  78. adc_dma_mode_enable();
  79. /* ADC software trigger enable */
  80. adc_software_trigger_enable(ADC_REGULAR_CHANNEL);
  81. }
  82. void DMA_Channel0_IRQHandler(void){
  83. adc_disable();
  84. dma_interrupt_flag_clear(DMA_ADC_CH, DMA_INT_FLAG_FTF);
  85. dma_channel_disable(DMA_ADC_CH);
  86. }
  87. int adc_get_sample(void){
  88. if (adc_work != ADC_WORK_FINISH){
  89. return 0x7FFFFFFF;
  90. }
  91. int value = 0;
  92. int count = DMA_CHCNT(DMA_ADC_CH) & DMA_CHANNEL_CNT_MASK;
  93. int min = 0xFFFFF;
  94. int max = -0xFFFFF;
  95. int i;
  96. for (i = 0; i < count; i++){
  97. int one = dma_buf[i];
  98. value += one;
  99. if (one > max){
  100. max = one;
  101. }
  102. if (one < min) {
  103. min = one;
  104. }
  105. }
  106. adc_work = ADC_WORK_FINISH;
  107. return (value - min - max)/(count - 2);
  108. }
  109. #else
  110. int adc_sample(int chan, int calibration){
  111. int value = -0xFFFFFF;
  112. int mask = 0xFFFF;
  113. //hardware oversample to 16bit
  114. adc_oversample_mode_config(ADC_OVERSAMPLING_ALL_CONVERT, ADC_OVERSAMPLING_SHIFT_4B, ADC_OVERSAMPLING_RATIO_MUL256);
  115. adc_oversample_mode_enable();
  116. /* use max convert time to make sure the adc work fine */
  117. adc_regular_channel_config(0, chan, ADC_SAMPLETIME_55POINT5);//239.5 + 12.5 = 242 cycle, 242/(28*1000000)
  118. adc_enable();
  119. delay_us(1000); //MUST delay, for adc work fine
  120. if (calibration) {
  121. /* ADC calibration and reset calibration */
  122. adc_calibration_enable();
  123. }
  124. adc_software_trigger_enable(ADC_REGULAR_CHANNEL);
  125. while(SET != adc_flag_get(ADC_FLAG_EOC));
  126. value = adc_regular_data_read();
  127. adc_flag_clear(ADC_FLAG_EOC);
  128. adc_disable();
  129. return value & mask;
  130. }
  131. int adc_sample_avg(int chan, int times){
  132. int value = 0;
  133. int count = 0;
  134. int min = 0xFFFFF;
  135. int max = -0xFFFFF;
  136. //hardware oversample to 16bit
  137. adc_oversample_mode_config(ADC_OVERSAMPLING_ALL_CONVERT, ADC_OVERSAMPLING_SHIFT_4B, ADC_OVERSAMPLING_RATIO_MUL256);
  138. adc_oversample_mode_enable();
  139. /* use max convert time to make sure the adc work fine */
  140. adc_regular_channel_config(0, chan, ADC_SAMPLETIME_55POINT5);//239.5 + 12.5 = 242 cycle, 242/(28*1000000)
  141. adc_enable();
  142. delay_us(1000); //MUST delay, for adc work fine
  143. /* ADC calibration and reset calibration */
  144. adc_calibration_enable();
  145. while(count < times){
  146. adc_software_trigger_enable(ADC_REGULAR_CHANNEL);
  147. while(SET != adc_flag_get(ADC_FLAG_EOC));
  148. int one = adc_regular_data_read();
  149. adc_flag_clear(ADC_FLAG_EOC);
  150. value += (one & 0xFFFF);
  151. count ++;
  152. if (one > max){
  153. max = one;
  154. }
  155. if (one < min) {
  156. min = one;
  157. }
  158. }
  159. adc_disable();
  160. return (value - min - max)/(times-2);
  161. }
  162. #endif /* DMA_ADC_CH */