2023-09-25 13:12:41 +03:00
|
|
|
|
/**
|
|
|
|
|
******************************************************************************
|
|
|
|
|
* @file adc.c
|
|
|
|
|
* @brief This file provides code for the configuration
|
|
|
|
|
* of the ADC instances.
|
|
|
|
|
******************************************************************************
|
|
|
|
|
* @attention
|
|
|
|
|
*
|
|
|
|
|
* <h2><center>© Copyright (c) 2023 STMicroelectronics.
|
|
|
|
|
* All rights reserved.</center></h2>
|
|
|
|
|
*
|
|
|
|
|
* This software component is licensed by ST under BSD 3-Clause license,
|
|
|
|
|
* the "License"; You may not use this file except in compliance with the
|
|
|
|
|
* License. You may obtain a copy of the License at:
|
|
|
|
|
* opensource.org/licenses/BSD-3-Clause
|
|
|
|
|
*
|
|
|
|
|
******************************************************************************
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
|
|
|
#include "adc.h"
|
2023-10-11 17:28:58 +03:00
|
|
|
|
#include "tim.h"
|
|
|
|
|
#include "string.h"
|
2023-09-25 13:12:41 +03:00
|
|
|
|
/* USER CODE BEGIN 0 */
|
|
|
|
|
|
|
|
|
|
/* USER CODE END 0 */
|
|
|
|
|
|
|
|
|
|
ADC_HandleTypeDef hadc1;
|
|
|
|
|
DMA_HandleTypeDef hdma_adc1;
|
|
|
|
|
|
2023-10-11 17:28:58 +03:00
|
|
|
|
uint16_t DMA_Buff[DMA_BUFFSIZE];
|
|
|
|
|
uint16_t ADC_Buff[ADC_BUFFSIZE];
|
|
|
|
|
uint32_t ind_buff=0; //<2F><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
uint32_t current_buff=0; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
2023-09-25 13:12:41 +03:00
|
|
|
|
/* ADC1 init function */
|
|
|
|
|
void MX_ADC1_Init(void)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN ADC1_Init 0 */
|
|
|
|
|
|
|
|
|
|
/* USER CODE END ADC1_Init 0 */
|
|
|
|
|
|
|
|
|
|
ADC_ChannelConfTypeDef sConfig = {0};
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN ADC1_Init 1 */
|
|
|
|
|
|
|
|
|
|
/* USER CODE END ADC1_Init 1 */
|
|
|
|
|
/** Configure the global features of the ADC (Clock, Resolution, Data Alignment and number of conversion)
|
|
|
|
|
*/
|
|
|
|
|
hadc1.Instance = ADC1;
|
|
|
|
|
hadc1.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
|
2023-10-11 17:28:58 +03:00
|
|
|
|
hadc1.Init.Resolution = ADC_RESOLUTION_12B; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>
|
2023-09-25 13:12:41 +03:00
|
|
|
|
hadc1.Init.ScanConvMode = DISABLE;
|
|
|
|
|
hadc1.Init.ContinuousConvMode = DISABLE;
|
|
|
|
|
hadc1.Init.DiscontinuousConvMode = DISABLE;
|
|
|
|
|
hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING;
|
2023-10-11 17:28:58 +03:00
|
|
|
|
hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIGCONV_T3_TRGO; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2023-09-25 13:12:41 +03:00
|
|
|
|
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
|
|
|
|
hadc1.Init.NbrOfConversion = 1;
|
|
|
|
|
hadc1.Init.DMAContinuousRequests = ENABLE;
|
|
|
|
|
hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
|
|
|
|
|
if (HAL_ADC_Init(&hadc1) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
/** Configure for the selected ADC regular channel its corresponding rank in the sequencer and its sample time.
|
|
|
|
|
*/
|
|
|
|
|
sConfig.Channel = ADC_CHANNEL_0;
|
|
|
|
|
sConfig.Rank = 1;
|
|
|
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_3CYCLES;
|
|
|
|
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
/* USER CODE BEGIN ADC1_Init 2 */
|
|
|
|
|
|
2023-10-11 17:28:58 +03:00
|
|
|
|
HAL_ADC_Start_DMA(&hadc1, (uint32_t *) &DMA_Buff[0], DMA_BUFFSIZE); // <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD> <20> <20><><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> ///halfword??? <20><><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1024, <20><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
HAL_TIM_Base_Start(&htim3); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
HAL_TIM_GenerateEvent(&htim3, TIM_EVENTSOURCE_UPDATE); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>? <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20> <20><><EFBFBD><EFBFBD><EFBFBD>? //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> <20><><EFBFBD><EFBFBD><EFBFBD>
|
2023-09-25 13:12:41 +03:00
|
|
|
|
/* USER CODE END ADC1_Init 2 */
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_ADC_MspInit(ADC_HandleTypeDef* adcHandle)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
|
if(adcHandle->Instance==ADC1)
|
|
|
|
|
{
|
|
|
|
|
/* USER CODE BEGIN ADC1_MspInit 0 */
|
|
|
|
|
|
|
|
|
|
/* USER CODE END ADC1_MspInit 0 */
|
|
|
|
|
/* ADC1 clock enable */
|
|
|
|
|
__HAL_RCC_ADC1_CLK_ENABLE();
|
|
|
|
|
|
|
|
|
|
__HAL_RCC_GPIOA_CLK_ENABLE();
|
|
|
|
|
/**ADC1 GPIO Configuration
|
|
|
|
|
PA0-WKUP ------> ADC1_IN0
|
|
|
|
|
*/
|
|
|
|
|
GPIO_InitStruct.Pin = GPIO_PIN_0;
|
|
|
|
|
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
|
|
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
|
|
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
|
|
|
|
|
|
/* ADC1 DMA Init */
|
|
|
|
|
/* ADC1 Init */
|
|
|
|
|
hdma_adc1.Instance = DMA2_Stream0;
|
|
|
|
|
hdma_adc1.Init.Channel = DMA_CHANNEL_0;
|
|
|
|
|
hdma_adc1.Init.Direction = DMA_PERIPH_TO_MEMORY;
|
|
|
|
|
hdma_adc1.Init.PeriphInc = DMA_PINC_DISABLE;
|
|
|
|
|
hdma_adc1.Init.MemInc = DMA_MINC_ENABLE;
|
|
|
|
|
hdma_adc1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
|
|
|
|
|
hdma_adc1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
|
|
|
|
|
hdma_adc1.Init.Mode = DMA_CIRCULAR;
|
|
|
|
|
hdma_adc1.Init.Priority = DMA_PRIORITY_LOW;
|
|
|
|
|
hdma_adc1.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
|
|
|
|
|
hdma_adc1.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
|
|
|
|
|
hdma_adc1.Init.MemBurst = DMA_MBURST_SINGLE;
|
|
|
|
|
hdma_adc1.Init.PeriphBurst = DMA_PBURST_SINGLE;
|
|
|
|
|
if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
|
|
|
|
|
{
|
|
|
|
|
Error_Handler();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
__HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN ADC1_MspInit 1 */
|
|
|
|
|
|
|
|
|
|
/* USER CODE END ADC1_MspInit 1 */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HAL_ADC_MspDeInit(ADC_HandleTypeDef* adcHandle)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if(adcHandle->Instance==ADC1)
|
|
|
|
|
{
|
|
|
|
|
/* USER CODE BEGIN ADC1_MspDeInit 0 */
|
|
|
|
|
|
|
|
|
|
/* USER CODE END ADC1_MspDeInit 0 */
|
|
|
|
|
/* Peripheral clock disable */
|
|
|
|
|
__HAL_RCC_ADC1_CLK_DISABLE();
|
|
|
|
|
|
|
|
|
|
/**ADC1 GPIO Configuration
|
|
|
|
|
PA0-WKUP ------> ADC1_IN0
|
|
|
|
|
*/
|
|
|
|
|
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0);
|
|
|
|
|
|
|
|
|
|
/* ADC1 DMA DeInit */
|
|
|
|
|
HAL_DMA_DeInit(adcHandle->DMA_Handle);
|
|
|
|
|
/* USER CODE BEGIN ADC1_MspDeInit 1 */
|
|
|
|
|
|
|
|
|
|
/* USER CODE END ADC1_MspDeInit 1 */
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* USER CODE BEGIN 1 */
|
2023-10-11 17:28:58 +03:00
|
|
|
|
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
|
|
|
|
|
{
|
|
|
|
|
memcpy((void *) &ADC_Buff[ind_buff], (void *) DMA_Buff, DMA_BUFFSIZE<<1); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> //<< <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> 2
|
|
|
|
|
current_buff=ind_buff;
|
|
|
|
|
if(ind_buff==0) ind_buff=DMA_BUFFSIZE;
|
|
|
|
|
else ind_buff=0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-25 13:12:41 +03:00
|
|
|
|
|
|
|
|
|
/* USER CODE END 1 */
|
|
|
|
|
|
|
|
|
|
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|