adc.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #include "drivers/adc/adc.h"
  2. #include "stm32l1xx_hal.h"
  3. #include "stm32l1xx_hal_adc.h"
  4. #include "stm32l1xx_hal_adc_ex.h"
  5. #include "core/csect.h"
  6. #include "core/config_pins.h"
  7. static bool ADC_Init( eADC_Resolution_t resolution );
  8. static bool ADC_Start( uint32_t chid );
  9. static bool ADC_Measure( uint32_t timeout, tADCValue_t * pResult );
  10. static void ADC_Stop( uint32_t chid );
  11. static uint32_t ADC_GetVoltage(tADCValue_t * pResult);
  12. static void ADC_DeInit();
  13. static tADCValue_t ADCResult = 0;
  14. ADC_HandleTypeDef Adc1Handle;
  15. const ADC_Handle_t ADC1Handle = {
  16. ADC_Init,
  17. ADC_Start,
  18. ADC_Measure,
  19. ADC_Stop,
  20. ADC_GetVoltage,
  21. ADC_DeInit,
  22. };
  23. static bool ADC_Init( eADC_Resolution_t resolution )
  24. {
  25. DI();
  26. /* Configure the ADC1 IN0 for analog input */
  27. __HAL_RCC_HSI_ENABLE();
  28. __HAL_RCC_ADC1_CLK_ENABLE();
  29. GPIO_InitTypeDef GPIO_InitStruct = {0};
  30. GPIO_InitStruct.Pin = GPIO_PIN_1;
  31. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  32. GPIO_InitStruct.Pull = GPIO_NOPULL;
  33. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  34. /* Configure the ADC peripheral */
  35. Adc1Handle.Instance = ADC1;
  36. Adc1Handle.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
  37. switch( resolution )
  38. {
  39. case eADCRes_12bit: Adc1Handle.Init.Resolution = ADC_RESOLUTION_12B; break;
  40. case eADCRes_10bit: Adc1Handle.Init.Resolution = ADC_RESOLUTION_10B; break;
  41. case eADCRes_8bit: Adc1Handle.Init.Resolution = ADC_RESOLUTION_8B; break;
  42. case eADCRes_6bit:
  43. default:
  44. Adc1Handle.Init.Resolution = ADC_RESOLUTION_6B; break;
  45. }
  46. Adc1Handle.Init.ScanConvMode = DISABLE;
  47. Adc1Handle.Init.ContinuousConvMode = DISABLE;
  48. Adc1Handle.Init.DiscontinuousConvMode = DISABLE;
  49. Adc1Handle.Init.NbrOfDiscConversion = 0;
  50. Adc1Handle.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  51. Adc1Handle.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  52. Adc1Handle.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  53. Adc1Handle.Init.NbrOfConversion = 1;
  54. Adc1Handle.Init.DMAContinuousRequests = DISABLE;
  55. Adc1Handle.Init.EOCSelection = DISABLE;
  56. Adc1Handle.Init.LowPowerAutoPowerOff = ADC_AUTOPOWEROFF_DISABLE;
  57. bool bRet = ( HAL_OK == HAL_ADC_Init(&Adc1Handle) );
  58. //HAL_NVIC_SetPriority( ADC1_IRQn, ADC1_INT_PRIORITY, 0);
  59. //HAL_NVIC_EnableIRQ( ADC1_IRQn );
  60. EI();
  61. return bRet;
  62. }
  63. static bool ADC_Start( uint32_t chid )
  64. {
  65. ADC_ChannelConfTypeDef sConfig;
  66. DI();
  67. /* Configure ADC regular channel */
  68. sConfig.Channel = chid;
  69. sConfig.Rank = ADC_REGULAR_RANK_1;
  70. sConfig.SamplingTime = ADC_SAMPLETIME_4CYCLES;
  71. bool bRet = ( HAL_OK == HAL_ADC_ConfigChannel(&Adc1Handle, &sConfig) );
  72. EI();
  73. return bRet;
  74. }
  75. static bool ADC_Measure( uint32_t timeout, tADCValue_t * pResult )
  76. {
  77. bool bResult = false;
  78. if( HAL_OK == HAL_ADC_Start( &Adc1Handle ) )
  79. {
  80. // ADC performs fast conversion within 4 cycles: 2ms timeout is enough if no high-priority interrupts fires
  81. if( HAL_OK == HAL_ADC_PollForConversion( &Adc1Handle, timeout ) )
  82. {
  83. DI();
  84. if( HAL_ADC_ERROR_NONE == HAL_ADC_GetError( &Adc1Handle ) )
  85. {
  86. ADCResult = (tADCValue_t)HAL_ADC_GetValue( &Adc1Handle );
  87. bResult = true;
  88. }
  89. else
  90. {
  91. ADCResult = (tADCValue_t)(-2L);
  92. }
  93. if( bResult )
  94. {
  95. if( pResult )
  96. *pResult = ADCResult;
  97. }
  98. EI();
  99. }
  100. }
  101. return bResult;
  102. }
  103. static void ADC_Stop( uint32_t chid )
  104. {
  105. // To deconfigure the channel the ADC_DeInit() call is required.
  106. (void)chid; // no operations performed
  107. }
  108. static void ADC_DeInit()
  109. {
  110. DI();
  111. HAL_NVIC_DisableIRQ( ADC1_IRQn );
  112. HAL_ADC_Stop( &Adc1Handle );
  113. HAL_ADC_DeInit( &Adc1Handle );
  114. __HAL_RCC_ADC1_CLK_DISABLE();
  115. EI();
  116. }
  117. static uint32_t ADC_GetVoltage(tADCValue_t * pResult)
  118. {
  119. return (uint32_t)((float)(*pResult * ADC_VRED_V / 0xFFF) * 10 / KDIVIDER());
  120. }
  121. void HAL_ADC_ErrorCallback(ADC_HandleTypeDef *hadc)
  122. {
  123. }
  124. void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
  125. {
  126. }