Files
tronone-h7-scaffold/User_Code/bsp/adc/bsp_adc.c

85 lines
2.1 KiB
C
Raw Normal View History

2025-11-20 17:19:43 +08:00
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : bsp_adc.c
* @brief : bsp adc functions
* @author : TuxMonkey
* @date : 2025/11/22
* @version : v1.0
******************************************************************************
* @attention : Pay attention to enable the adc
******************************************************************************
*/
/* USER CODE END Header */
2025-10-28 14:52:24 +08:00
2025-11-20 17:19:43 +08:00
/* Includes ------------------------------------------------------------------*/
#include "bsp_adc.h"
#include "adc.h"
2026-07-20 23:34:13 +08:00
#include <stdbool.h>
2025-11-20 17:19:43 +08:00
/**
* @brief ADC sampling voltage array
*/
2026-07-20 23:34:13 +08:00
__attribute__((section(".dma_buffer"), aligned(32))) volatile uint16_t ADC_Voltage_Val[BSP_ADC_CHANNEL_COUNT];
static bool bsp_adc_started = false;
2025-11-20 17:19:43 +08:00
/**
* @brief Configures the ADC.
* @param None
* @retval None
*/
void BSP_ADC_Init(void)
{
2026-07-20 23:34:13 +08:00
if (bsp_adc_started)
{
return;
}
(void) HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED);
if (HAL_ADC_Start_DMA(&hadc1, (uint32_t *) ADC_Voltage_Val, BSP_ADC_CHANNEL_COUNT) == HAL_OK)
{
bsp_adc_started = true;
}
2025-11-20 17:19:43 +08:00
}
//------------------------------------------------------------------------------
/**
* @brief USER get current voltage.
* @param None
* @retval Voltage
*/
float USER_ADC_Voltage_Update(void)
{
2026-07-20 23:34:13 +08:00
float voltage = ((float) ADC_Voltage_Val[BSP_ADC_BOARD_VOLTAGE_INDEX] * 3.3f / 65535.0f) * 11.0f;
return voltage;
2025-11-20 17:19:43 +08:00
}
//------------------------------------------------------------------------------
2026-07-20 23:34:13 +08:00
uint16_t BSP_ADC_GetRaw(uint32_t index)
{
if (index >= BSP_ADC_CHANNEL_COUNT)
{
return 0U;
}
return ADC_Voltage_Val[index];
}
uint16_t BSP_ADC_GetJoystickRaw(void)
{
return BSP_ADC_GetRaw(BSP_ADC_JOYSTICK_INDEX);
}
uint16_t BSP_ADC_GetJoystickRaw12(void)
{
return (uint16_t) (BSP_ADC_GetJoystickRaw() >> 4U);
}
float BSP_ADC_GetJoystickVoltage(void)
{
return (float) BSP_ADC_GetJoystickRaw() * 3.3f / 65535.0f;
}