hal_adc.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * BSD 2-Clause License
  3. * Copyright (c) 2022, LiteEMF
  4. * All rights reserved.
  5. * This software component is licensed by LiteEMF under BSD 2-Clause license,
  6. * the "License"; You may not use this file except in compliance with the
  7. * License. You may obtain a copy of the License at:
  8. * opensource.org/licenses/BSD-2-Clause
  9. *
  10. */
  11. /************************************************************************************************************
  12. ** Description:
  13. ************************************************************************************************************/
  14. #include "hw_config.h"
  15. #include "hw_board.h"
  16. // #ifdef HW_ADC_MAP
  17. #if 1
  18. #include "api/api_adc.h"
  19. #include "py32f002b_ll_adc.h"
  20. /******************************************************************************************************
  21. ** Defined
  22. *******************************************************************************************************/
  23. /******************************************************************************************************
  24. ** static Parameters
  25. *******************************************************************************************************/
  26. /******************************************************************************************************
  27. ** public Parameters
  28. *******************************************************************************************************/
  29. /*****************************************************************************************************
  30. ** static Function
  31. ******************************************************************************************************/
  32. uint16_t irq_adc[m_adc_num]; //全局变量, 防止adc采样失败数据出错
  33. uint8_t num = 0;
  34. /*****************************************************************************************************
  35. ** Function
  36. ******************************************************************************************************/
  37. /**
  38. * @brief ADC calibration program.
  39. * @param None
  40. * @retval None
  41. */
  42. static void APP_AdcCalibrate(void)
  43. {
  44. #if (USE_TIMEOUT == 1)
  45. uint32_t Timeout = 0;
  46. #endif
  47. if (LL_ADC_IsEnabled(ADC1) == 0)
  48. {
  49. /* Enable ADC calibration */
  50. LL_ADC_StartCalibration(ADC1);
  51. #if (USE_TIMEOUT == 1)
  52. Timeout = ADC_CALIBRATION_TIMEOUT_MS;
  53. #endif
  54. while (LL_ADC_IsCalibrationOnGoing(ADC1) != 0)
  55. {
  56. #if (USE_TIMEOUT == 1)
  57. /* Detects if the calibration has timed out */
  58. if (LL_SYSTICK_IsActiveCounterFlag())
  59. {
  60. if(Timeout-- == 0)
  61. {
  62. }
  63. }
  64. #endif
  65. }
  66. /* The delay between the end of ADC calibration and ADC enablement is at least 4 ADC clocks */
  67. LL_mDelay(1);
  68. }
  69. }
  70. /**
  71. * @brief Enable ADC.
  72. * @param None
  73. * @retval None
  74. */
  75. static void APP_AdcEnable(void)
  76. {
  77. /* Enable ADC */
  78. LL_ADC_Enable(ADC1);
  79. /* The delay between ADC enablement and ADC stabilization is at least 8 ADC clocks */
  80. LL_mDelay(1);
  81. }
  82. /*******************************************************************
  83. ** Parameters:
  84. ** Returns:
  85. ** Description:
  86. *******************************************************************/
  87. uint16_t hal_adc_to_voltage(uint16_t adc)
  88. {
  89. return 0;
  90. }
  91. bool hal_adc_value(uint8_t id, uint16_t* valp)
  92. {
  93. if(id >= m_adc_num) return false;
  94. valp = irq_adc[id];
  95. return true;
  96. }
  97. bool hal_adc_start_scan(void)
  98. {
  99. return false;
  100. }
  101. bool hal_adc_init(void)
  102. {
  103. uint8_t id;
  104. LL_APB1_GRP2_EnableClock(LL_APB1_GRP2_PERIPH_ADC1);
  105. for(id=0; id<m_adc_num; id++){
  106. get_gpio_rcc(m_adc_map[id].pin);
  107. LL_GPIO_SetPinMode(get_gpio_port(m_adc_map[id].pin), get_gpio_pin(m_adc_map[id].pin), LL_GPIO_MODE_ANALOG);
  108. }
  109. /* Set ADC clock to pclk/4 */
  110. LL_ADC_SetClock(ADC1, LL_ADC_CLOCK_SYNC_PCLK_DIV4);
  111. /* Set ADC resolution to 12 bit */
  112. LL_ADC_SetResolution(ADC1, LL_ADC_RESOLUTION_12B);
  113. /* ADC conversion data alignment: right aligned */
  114. LL_ADC_SetDataAlignment(ADC1, LL_ADC_DATA_ALIGN_RIGHT);
  115. /* No ADC low power mode activated */
  116. LL_ADC_SetLowPowerMode(ADC1, LL_ADC_LP_MODE_NONE);
  117. /* Sampling time 239.5 ADC clock cycles */
  118. LL_ADC_SetSamplingTimeCommonChannels(ADC1, LL_ADC_SAMPLINGTIME_239CYCLES_5);
  119. /* ADC regular group conversion trigger from external IP: TIM1 TRGO. */
  120. LL_ADC_REG_SetTriggerSource(ADC1, LL_ADC_REG_TRIG_EXT_TIM1_TRGO);
  121. /* Set Trigger edge to rising edge */
  122. LL_ADC_REG_SetTriggerEdge(ADC1, LL_ADC_REG_TRIG_EXT_RISING);
  123. /* Set ADC conversion mode to single mode: one conversion per trigger */
  124. LL_ADC_REG_SetContinuousMode(ADC1, LL_ADC_REG_CONV_SINGLE);
  125. /* ADC regular group behavior in case of overrun: data overwritten */
  126. LL_ADC_REG_SetOverrun(ADC1, LL_ADC_REG_OVR_DATA_OVERWRITTEN);
  127. /* Disable ADC regular group sequencer discontinuous mode */
  128. LL_ADC_REG_SetSequencerDiscont(ADC1, LL_ADC_REG_SEQ_DISCONT_DISABLE);
  129. for(id=0; id<m_adc_num; id++){
  130. if(id == 0){
  131. LL_ADC_REG_SetSequencerChannels(ADC1, ADC_CH_ATT(id));
  132. }else{
  133. LL_ADC_REG_SetSequencerChAdd(ADC1, ADC_CH_ATT(id));
  134. }
  135. }
  136. /* Dose not enable internal conversion channel */
  137. LL_ADC_SetCommonPathInternalCh(__LL_ADC_COMMON_INSTANCE(ADC1), LL_ADC_PATH_INTERNAL_VREFINT );
  138. /* Enable EOC IT */
  139. LL_ADC_EnableIT_EOC(ADC1);
  140. NVIC_SetPriority(ADC_COMP_IRQn,1);
  141. NVIC_EnableIRQ(ADC_COMP_IRQn);
  142. /* ADC automatic self-calibration */
  143. APP_AdcCalibrate();
  144. /* Enable ADC */
  145. APP_AdcEnable();
  146. /* Start ADC conversion (if it is software triggered then start conversion directly) */
  147. LL_ADC_REG_StartConversion(ADC1);
  148. return true;
  149. }
  150. bool hal_adc_deinit(void)
  151. {
  152. LL_ADC_Disable(ADC1);
  153. return false;
  154. }
  155. /******************************************************************************/
  156. /* Cortex-M0+ Processor Interruption and Exception Handlers */
  157. /******************************************************************************/
  158. void ADC_COMP_IRQHandler(void)
  159. {
  160. /* Clear ADC EOC IT flag */
  161. LL_ADC_ClearFlag_EOC(ADC1);
  162. irq_adc[num++]=ADC1->DR;
  163. if(num >= m_adc_num) i = 0;
  164. }
  165. #endif