You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
4.0 KiB

  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file adc.c
  5. * @brief This file provides code for the configuration
  6. * of the ADC instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2024 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "adc.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. ADC_HandleTypeDef hadc1;
  25. DMA_HandleTypeDef hdma_adc1;
  26. /* ADC1 init function */
  27. void MX_ADC1_Init(void)
  28. {
  29. /* USER CODE BEGIN ADC1_Init 0 */
  30. /* USER CODE END ADC1_Init 0 */
  31. ADC_ChannelConfTypeDef sConfig = {0};
  32. /* USER CODE BEGIN ADC1_Init 1 */
  33. /* USER CODE END ADC1_Init 1 */
  34. /** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
  35. */
  36. hadc1.Instance = ADC1;
  37. hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV4;
  38. hadc1.Init.Resolution = ADC_RESOLUTION_12B;
  39. hadc1.Init.ScanConvMode = DISABLE;
  40. hadc1.Init.ContinuousConvMode = DISABLE;
  41. hadc1.Init.DiscontinuousConvMode = DISABLE;
  42. hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
  43. hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
  44. hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
  45. hadc1.Init.NbrOfConversion = 1;
  46. hadc1.Init.DMAContinuousRequests = DISABLE;
  47. hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
  48. if (HAL_ADC_Init(&hadc1) != HAL_OK)
  49. {
  50. Error_Handler();
  51. }
  52. /** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
  53. */
  54. sConfig.Channel = ADC_CHANNEL_10;
  55. sConfig.Rank = 1;
  56. sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
  57. if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
  58. {
  59. Error_Handler();
  60. }
  61. /* USER CODE BEGIN ADC1_Init 2 */
  62. /* USER CODE END ADC1_Init 2 */
  63. }
  64. void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
  65. {
  66. GPIO_InitTypeDef GPIO_InitStruct = {0};
  67. if(adcHandle->Instance==ADC1)
  68. {
  69. /* USER CODE BEGIN ADC1_MspInit 0 */
  70. /* USER CODE END ADC1_MspInit 0 */
  71. /* ADC1 clock enable */
  72. __HAL_RCC_ADC1_CLK_ENABLE();
  73. __HAL_RCC_GPIOC_CLK_ENABLE();
  74. /**ADC1 GPIO Configuration
  75. PC0 ------> ADC1_IN10
  76. */
  77. GPIO_InitStruct.Pin = GPIO_PIN_0;
  78. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  79. GPIO_InitStruct.Pull = GPIO_NOPULL;
  80. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  81. /* ADC1 DMA Init */
  82. /* ADC1 Init */
  83. hdma_adc1.Instance = DMA2_Stream0;
  84. hdma_adc1.Init.Channel = DMA_CHANNEL_0;
  85. hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
  86. hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
  87. hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
  88. hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
  89. hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
  90. hdma_adc1.Init.Mode = DMA_NORMAL;
  91. hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
  92. hdma_adc1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  93. if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
  94. {
  95. Error_Handler();
  96. }
  97. __HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
  98. /* USER CODE BEGIN ADC1_MspInit 1 */
  99. /* USER CODE END ADC1_MspInit 1 */
  100. }
  101. }
  102. void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
  103. {
  104. if(adcHandle->Instance==ADC1)
  105. {
  106. /* USER CODE BEGIN ADC1_MspDeInit 0 */
  107. /* USER CODE END ADC1_MspDeInit 0 */
  108. /* Peripheral clock disable */
  109. __HAL_RCC_ADC1_CLK_DISABLE();
  110. /**ADC1 GPIO Configuration
  111. PC0 ------> ADC1_IN10
  112. */
  113. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0);
  114. /* ADC1 DMA DeInit */
  115. HAL_DMA_DeInit(adcHandle->DMA_Handle);
  116. /* USER CODE BEGIN ADC1_MspDeInit 1 */
  117. /* USER CODE END ADC1_MspDeInit 1 */
  118. }
  119. }
  120. /* USER CODE BEGIN 1 */
  121. /* USER CODE END 1 */