/* 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 */ /* Includes ------------------------------------------------------------------*/ #include "bsp_adc.h" #include "adc.h" #include /** * @brief ADC sampling voltage array */ __attribute__((section(".dma_buffer"), aligned(32))) volatile uint16_t ADC_Voltage_Val[BSP_ADC_CHANNEL_COUNT]; static bool bsp_adc_started = false; /** * @brief Configures the ADC. * @param None * @retval None */ void BSP_ADC_Init(void) { 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; } } //------------------------------------------------------------------------------ /** * @brief USER get current voltage. * @param None * @retval Voltage */ float USER_ADC_Voltage_Update(void) { float voltage = ((float) ADC_Voltage_Val[BSP_ADC_BOARD_VOLTAGE_INDEX] * 3.3f / 65535.0f) * 11.0f; return voltage; } //------------------------------------------------------------------------------ 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; }