/**
|
******************************************************************************
|
* File Name : ADC.c
|
* Description : This file provides code for the configuration
|
* of the ADC instances.
|
******************************************************************************
|
* @attention
|
*
|
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
|
* All rights reserved.</center></h2>
|
*
|
* This software component is licensed by ST under Ultimate Liberty license
|
* SLA0044, the "License"; You may not use this file except in compliance with
|
* the License. You may obtain a copy of the License at:
|
* www.st.com/SLA0044
|
*
|
******************************************************************************
|
*/
|
|
/* Includes ------------------------------------------------------------------*/
|
#include "adc.h"
|
static bool adcInitFlag = false;
|
|
|
#define TEMPERATURE_ARRAY_SIZE 251
|
enum
|
{
|
TEM_INDEX = 0, /* ÎÂ¶È */
|
ADC_INDEX, /* adc²ÉÑùÖµ */
|
};
|
|
/* USER CODE BEGIN 0 */
|
#define NUM_OF_CONVERSION 10
|
/* USER CODE END 0 */
|
|
ADC_HandleTypeDef hadc1;
|
DMA_HandleTypeDef hdma_adc1;
|
//ALIGN_32BYTES(__attribute__((section (".RAM_D3"))) uint16_t ADCxValues[NUM_OF_CONVERSION]);
|
uint16_t ADCxValues[NUM_OF_CONVERSION] = {0};
|
|
/* ADC1 init function */
|
void Board_ADC1Init(void)
|
{
|
ADC_ChannelConfTypeDef sConfig = {0};
|
|
/** Common config
|
*/
|
hadc1.Instance = ADC1;
|
hadc1.Init.ScanConvMode = ADC_SCAN_ENABLE;
|
hadc1.Init.ContinuousConvMode = ENABLE;
|
hadc1.Init.DiscontinuousConvMode = DISABLE;
|
hadc1.Init.NbrOfDiscConversion = 1;
|
hadc1.Init.ExternalTrigConv = ADC_SOFTWARE_START;
|
hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT;
|
hadc1.Init.NbrOfConversion = ADC_CHANNEL_MAX_SIZE;
|
if (HAL_ADC_Init(&hadc1) != HAL_OK)
|
{
|
Error_Handler();
|
}
|
/** Configure Regular Channel
|
*/
|
sConfig.Channel = ADC_CHANNEL_0;
|
sConfig.Rank = ADC_REGULAR_RANK_1;
|
sConfig.SamplingTime = ADC_SAMPLETIME_28CYCLES_5;
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
{
|
Error_Handler();
|
}
|
/** Configure Regular Channel
|
*/
|
sConfig.Channel = ADC_CHANNEL_1;
|
sConfig.Rank = ADC_REGULAR_RANK_2;
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
{
|
Error_Handler();
|
}
|
/** Configure Regular Channel
|
*/
|
sConfig.Channel = ADC_CHANNEL_15;
|
sConfig.Rank = ADC_REGULAR_RANK_3;
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
{
|
Error_Handler();
|
}
|
/** Configure Regular Channel
|
*/
|
sConfig.Channel = ADC_CHANNEL_14;
|
sConfig.Rank = ADC_REGULAR_RANK_4;
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
{
|
Error_Handler();
|
}
|
/** Configure Regular Channel
|
*/
|
sConfig.Channel = ADC_CHANNEL_6;
|
sConfig.Rank = ADC_REGULAR_RANK_5;
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
{
|
Error_Handler();
|
}
|
/** Configure Regular Channel
|
*/
|
sConfig.Channel = ADC_CHANNEL_7;
|
sConfig.Rank = ADC_REGULAR_RANK_6;
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
{
|
Error_Handler();
|
}
|
/** Configure Regular Channel
|
*/
|
sConfig.Channel = ADC_CHANNEL_13;
|
sConfig.Rank = ADC_REGULAR_RANK_7;
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
{
|
Error_Handler();
|
}
|
/** Configure Regular Channel
|
*/
|
sConfig.Channel = ADC_CHANNEL_12;
|
sConfig.Rank = ADC_REGULAR_RANK_8;
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
{
|
Error_Handler();
|
}
|
/** Configure Regular Channel
|
*/
|
sConfig.Channel = ADC_CHANNEL_11;
|
sConfig.Rank = ADC_REGULAR_RANK_9;
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
{
|
Error_Handler();
|
}
|
/** Configure Regular Channel
|
*/
|
sConfig.Channel = ADC_CHANNEL_10;
|
sConfig.Rank = ADC_REGULAR_RANK_10;
|
if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK)
|
{
|
Error_Handler();
|
}
|
|
HAL_Delay(100); /* çå¾
çµå稳å®*/
|
HAL_ADC_Stop(&hadc1);
|
HAL_ADCEx_Calibration_Start(&hadc1);
|
|
|
/* å¯å¨ADCéæ ·*/
|
if (HAL_ADC_Start_DMA(&hadc1, (uint32_t *)ADCxValues, NUM_OF_CONVERSION) != HAL_OK)
|
{
|
Error_Handler();
|
}
|
|
adcInitFlag = true;
|
}
|
|
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_GPIOC_CLK_ENABLE();
|
/**ADC1 GPIO Configuration
|
PC0 ------> ADC1_IN10
|
PC1 ------> ADC1_IN11
|
PC2 ------> ADC1_IN12
|
PC3 ------> ADC1_IN13
|
*/
|
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 | GPIO_PIN_4 |GPIO_PIN_5;
|
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_6|GPIO_PIN_7;
|
GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
/* USER CODE BEGIN ADC1_MspInit 1 */
|
/* ADC1 DMA Init */
|
__HAL_RCC_DMA1_CLK_ENABLE(); /* Enable dma1 clock */
|
|
/* ADC1 DMA Init */
|
/* ADC1 Init */
|
hdma_adc1.Instance = DMA1_Channel1;
|
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_MEDIUM;
|
if (HAL_DMA_Init(&hdma_adc1) != HAL_OK)
|
{
|
Error_Handler();
|
}
|
|
/* DMA interrupt init */
|
/* DMA1_Channel1_IRQn interrupt configuration */
|
//HAL_NVIC_SetPriority(DMA1_Channel1_IRQn, 5, 0);
|
//HAL_NVIC_EnableIRQ(DMA1_Channel1_IRQn);
|
|
__HAL_LINKDMA(adcHandle,DMA_Handle,hdma_adc1);
|
|
/* ADC1 interrupt Init */
|
//HAL_NVIC_SetPriority(ADC1_2_IRQn, 5, 0);
|
//HAL_NVIC_EnableIRQ(ADC1_2_IRQn);
|
|
/* 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
|
PC0 ------> ADC1_IN10
|
PC1 ------> ADC1_IN11
|
PC2 ------> ADC1_IN12
|
PC3 ------> ADC1_IN13
|
*/
|
HAL_GPIO_DeInit(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3 | GPIO_PIN_4 |GPIO_PIN_5);
|
HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_6|GPIO_PIN_7);
|
|
/* USER CODE BEGIN ADC1_MspDeInit 1 */
|
/* ADC1 DMA DeInit */
|
//HAL_DMA_DeInit(adcHandle->DMA_Handle);
|
/* ADC1 interrupt Deinit */
|
//HAL_NVIC_DisableIRQ(ADC1_2_IRQn);
|
|
/* USER CODE END ADC1_MspDeInit 1 */
|
}
|
}
|
|
|
|
int8_t ADC1_ADC_GetSampleData(ADC_SampleData *data, uint16_t size)
|
{
|
CHECK_NULL(data);
|
|
if (!adcInitFlag)
|
return -1;
|
memcpy(data->sampleBuffer, ADCxValues, size);
|
|
return 0;
|
}
|
|
|
|
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
|