hal_adc.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #include "hal_adc.h"
  2. static uint16_t m_au16AdcValue[16];
  3. #define DMA_UNIT (CM_DMA1)
  4. #define DMA_PERIPH_CLK (FCG0_PERIPH_DMA1)
  5. #define DMA_CH (DMA_CH0)
  6. #define DMA_AOS_TRIG_SEL AOS_DMA1_0
  7. #define DMA_TRANS_CNT (0U)
  8. #define DMA_BLOCK_SIZE (16U)
  9. #define DMA_DATA_WIDTH (DMA_DATAWIDTH_16BIT)
  10. #define DMA_SRC_ADDR ((uint32_t)&CM_ADC1->DR0)
  11. #define DMA_DEST_ADDR ((uint32_t)(&m_au16AdcValue[0U]))
  12. #define DMA_TRIG_EVT (EVT_SRC_ADC1_EOCA)
  13. #define DMA_INT_TYPE (DMA_INT_BTC_CH0)
  14. #define DMA_INT_IRQn (DMA1_TC0_BTC0_IRQn)
  15. #define DMA_INT_PRIO (DDL_IRQ_PRIO_03)
  16. #define DMA_INT_FLAG (DMA_FLAG_BTC_CH0)
  17. #define DMA_IRQ_HANDLER DMA1_TC0_BTC0_Handler
  18. void adc_init(void)
  19. {
  20. stc_adc_init_t stcAdcInit;
  21. CLK_SetClockDiv((CLK_BUS_PCLK2 | CLK_BUS_PCLK4), (CLK_PCLK2_DIV8 | CLK_PCLK4_DIV2));
  22. CLK_SetPeriClockSrc(CLK_PERIPHCLK_PCLK);
  23. /* 1. Enable ADC peripheral clock. */
  24. FCG_Fcg3PeriphClockCmd(FCG3_PERIPH_ADC1, ENABLE);
  25. /* 2. Modify the default value depends on the application. */
  26. (void)ADC_StructInit(&stcAdcInit);
  27. /* 3. Initializes ADC. */
  28. (void)ADC_Init(CM_ADC1, &stcAdcInit);
  29. /* 4. ADC channel configuration. */
  30. /* 4.1 Set the ADC pin to analog input mode. */
  31. /*
  32. CH0 PA0 ADC1_IN0
  33. CH1 PA1 ADC1_IN1
  34. CH2 PA2 ADC1_IN2
  35. CH3 PA3 ADC1_IN3
  36. CH4 PA4 ADC12_IN4
  37. CH5 PA5 ADC12_IN5
  38. CH6 PA6 ADC123_IN6
  39. CH7 PA7 ADC123_IN7
  40. CH10 PC0 ADC12_IN10
  41. CH11 PC1 ADC12_IN11
  42. CH12 PC2 ADC1_IN12
  43. CH13 PC3 ADC1_IN13
  44. CH14 PC6 ADC1_IN14
  45. CH15 PC7 ADC1_IN15
  46. */
  47. stc_gpio_init_t stcGpioInit;
  48. GPIO_StructInit(&stcGpioInit);
  49. stcGpioInit.u16PinAttr = PIN_ATTR_ANALOG;
  50. GPIO_Init(GPIO_PORT_A, GPIO_PIN_00, &stcGpioInit);
  51. GPIO_Init(GPIO_PORT_A, GPIO_PIN_01, &stcGpioInit);
  52. //GPIO_Init(GPIO_PORT_A, GPIO_PIN_02, &stcGpioInit);
  53. GPIO_Init(GPIO_PORT_A, GPIO_PIN_03, &stcGpioInit);
  54. GPIO_Init(GPIO_PORT_A, GPIO_PIN_04, &stcGpioInit);
  55. GPIO_Init(GPIO_PORT_A, GPIO_PIN_05, &stcGpioInit);
  56. GPIO_Init(GPIO_PORT_A, GPIO_PIN_06, &stcGpioInit);
  57. GPIO_Init(GPIO_PORT_A, GPIO_PIN_07, &stcGpioInit);
  58. GPIO_Init(GPIO_PORT_C, GPIO_PIN_00, &stcGpioInit);
  59. GPIO_Init(GPIO_PORT_C, GPIO_PIN_01, &stcGpioInit);
  60. GPIO_Init(GPIO_PORT_C, GPIO_PIN_02, &stcGpioInit);
  61. GPIO_Init(GPIO_PORT_C, GPIO_PIN_03, &stcGpioInit);
  62. GPIO_Init(GPIO_PORT_C, GPIO_PIN_04, &stcGpioInit);
  63. GPIO_Init(GPIO_PORT_C, GPIO_PIN_05, &stcGpioInit);
  64. ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH0,ENABLE);
  65. ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH1,ENABLE);
  66. ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH2,ENABLE);
  67. ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH3,ENABLE);
  68. ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH4,ENABLE);
  69. ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH5,ENABLE);
  70. ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH6,ENABLE);
  71. ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH7,ENABLE);
  72. ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH10,ENABLE);
  73. ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH11,ENABLE);
  74. ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH12,ENABLE);
  75. ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH13,ENABLE);
  76. ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH14,ENABLE);
  77. ADC_ChCmd(CM_ADC1, ADC_SEQ_A, ADC_CH15,ENABLE);
  78. ADC_ConvDataAverageConfig(CM_ADC1, ADC_AVG_CNT8);
  79. ADC_ConvDataAverageChCmd(CM_ADC1, ADC_CH0,ENABLE);
  80. ADC_ConvDataAverageChCmd(CM_ADC1, ADC_CH1,ENABLE);
  81. ADC_ConvDataAverageChCmd(CM_ADC1, ADC_CH2,ENABLE);
  82. ADC_ConvDataAverageChCmd(CM_ADC1, ADC_CH3,ENABLE);
  83. ADC_ConvDataAverageChCmd(CM_ADC1, ADC_CH4,ENABLE);
  84. ADC_ConvDataAverageChCmd(CM_ADC1, ADC_CH5,ENABLE);
  85. ADC_ConvDataAverageChCmd(CM_ADC1, ADC_CH6,ENABLE);
  86. ADC_ConvDataAverageChCmd(CM_ADC1, ADC_CH7,ENABLE);
  87. ADC_ConvDataAverageChCmd(CM_ADC1, ADC_CH10,ENABLE);
  88. ADC_ConvDataAverageChCmd(CM_ADC1, ADC_CH11,ENABLE);
  89. ADC_ConvDataAverageChCmd(CM_ADC1, ADC_CH12,ENABLE);
  90. ADC_ConvDataAverageChCmd(CM_ADC1, ADC_CH13,ENABLE);
  91. ADC_ConvDataAverageChCmd(CM_ADC1, ADC_CH14,ENABLE);
  92. ADC_ConvDataAverageChCmd(CM_ADC1, ADC_CH15,ENABLE);
  93. // stc_dma_init_t stcDmaInit;
  94. // stc_dma_repeat_init_t stcDmaRptInit;
  95. // (void)DMA_StructInit(&stcDmaInit);
  96. // stcDmaInit.u32IntEn = DMA_INT_ENABLE;
  97. // stcDmaInit.u32SrcAddr = DMA_SRC_ADDR;
  98. // stcDmaInit.u32DestAddr = DMA_DEST_ADDR;
  99. // stcDmaInit.u32DataWidth = DMA_DATA_WIDTH;
  100. // stcDmaInit.u32BlockSize = DMA_BLOCK_SIZE;
  101. // stcDmaInit.u32TransCount = DMA_TRANS_CNT;
  102. // stcDmaInit.u32SrcAddrInc = DMA_SRC_ADDR_INC;
  103. // stcDmaInit.u32DestAddrInc = DMA_DEST_ADDR_INC;
  104. // /* Enable DMA peripheral clock and AOS function. */
  105. // FCG_Fcg0PeriphClockCmd(DMA_PERIPH_CLK, ENABLE);
  106. // (void)DMA_Init(DMA_UNIT, DMA_CH, &stcDmaInit);
  107. // stcDmaRptInit.u32Mode = DMA_RPT_BOTH;
  108. // stcDmaRptInit.u32SrcCount = DMA_BLOCK_SIZE;
  109. // stcDmaRptInit.u32DestCount = DMA_BLOCK_SIZE;
  110. // (void)DMA_RepeatInit(DMA_UNIT, DMA_CH, &stcDmaRptInit);
  111. // /* Enable AOS clock */
  112. // FCG_Fcg0PeriphClockCmd(FCG0_PERIPH_AOS, ENABLE);
  113. // /* Set DMA trigger source */
  114. // AOS_SetTriggerEventSrc(DMA_AOS_TRIG_SEL, DMA_TRIG_EVT);
  115. // /* DMA IRQ configuration. */
  116. //
  117. // DMA_ClearTransCompleteStatus(DMA_UNIT, DMA_INT_FLAG);
  118. // /* NVIC setting */
  119. // NVIC_ClearPendingIRQ(DMA_INT_IRQn);
  120. // NVIC_SetPriority(DMA_INT_IRQn, DDL_IRQ_PRIO_03);
  121. // NVIC_EnableIRQ(DMA_INT_IRQn);
  122. //
  123. // DMA_Cmd(DMA_UNIT, ENABLE);
  124. // DMA_ChCmd(DMA_UNIT, DMA_CH, ENABLE);
  125. }
  126. //uint8_t m_u8AdcValUpdated = 0;
  127. //void DMA_IRQ_HANDLER(void)
  128. //{
  129. // if (DMA_GetTransCompleteStatus(DMA_UNIT, DMA_INT_FLAG) == SET)
  130. // {
  131. // DMA_ClearTransCompleteStatus(DMA_UNIT, DMA_INT_FLAG);
  132. // m_u8AdcValUpdated = 1U;
  133. // }
  134. //}
  135. //#include "FreeRTOS.h"
  136. //#include "task.h"
  137. //void test_task3(void *pvParameters)
  138. //{
  139. // while(1)
  140. // {
  141. // if(m_u8AdcValUpdated==0)
  142. // {
  143. // ADC_Start(CM_ADC1);
  144. // }
  145. // else
  146. // {
  147. // ADC_Stop(CM_ADC1);
  148. // for(int i=0;i < 16;i++)
  149. // {
  150. // printf("%dV ",m_au16AdcValue[9]/*(float)m_au16AdcValue[9]/4095*3.3*/);
  151. // }
  152. // printf("\n");
  153. // m_u8AdcValUpdated = 0;
  154. // }
  155. // vTaskDelay(500);
  156. // }
  157. //}
  158. uint16_t u16AdcValue;
  159. uint16_t get_adc_value(uint8_t ch)
  160. {
  161. int32_t iRet = LL_ERR;
  162. __IO uint32_t u32TimeCount = 0UL;
  163. /* Can ONLY start sequence A conversion.
  164. Sequence B needs hardware trigger to start conversion. */
  165. ADC_Start(CM_ADC1);
  166. do {
  167. if (ADC_GetStatus(CM_ADC1, ADC_FLAG_EOCA) == SET) {
  168. ADC_ClearStatus(CM_ADC1, ADC_FLAG_EOCA);
  169. iRet = LL_OK;
  170. break;
  171. }
  172. } while (u32TimeCount++ < ADC_TIMEOUT_VAL);
  173. if (iRet == LL_OK)
  174. {
  175. /* Get any ADC value of sequence A channel that needed. */
  176. u16AdcValue = ADC_GetValue(CM_ADC1,ch);
  177. }
  178. else
  179. {
  180. ADC_Stop(CM_ADC1);
  181. }
  182. return u16AdcValue;
  183. }