diff --git a/fw/Core/Src/main.c b/fw/Core/Src/main.c index 28b6b3c..eda6b45 100644 --- a/fw/Core/Src/main.c +++ b/fw/Core/Src/main.c @@ -54,7 +54,7 @@ static void MX_GPIO_Init(void); static void MX_ADC1_Init(void); static void MX_USB_PCD_Init(void); /* USER CODE BEGIN PFP */ - +uint16_t ADC_ReadChannel(uint32_t channel); /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ @@ -103,9 +103,22 @@ int main(void) while (1) { /* USER CODE END WHILE */ - state = state == GPIO_PIN_RESET ? GPIO_PIN_SET : GPIO_PIN_RESET; + // Read voltages before and after current limiting resistor (R22, 10 ohm) + uint16_t val_ch0 = ADC_ReadChannel(ADC_CHANNEL_0); + uint16_t val_ch1 = ADC_ReadChannel(ADC_CHANNEL_1); + // Convert to voltage (12-bit, 3.3V reference) + float voltage_ch0 = (val_ch0 * 3.3f) / 4095.0f; + float voltage_ch1 = (val_ch1 * 3.3f) / 4095.0f; + + // simple regulation + const float target_voltage = 3.0f; + if (voltage_ch0 < target_voltage) + { + state = GPIO_PIN_SET; + } else { + state = GPIO_PIN_RESET; + } HAL_GPIO_WritePin(VDD_LEDL_EN_GPIO_Port, VDD_LEDL_EN_Pin, state); - HAL_Delay(50); /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ @@ -294,7 +307,38 @@ static void MX_GPIO_Init(void) } /* USER CODE BEGIN 4 */ +uint16_t ADC_ReadChannel(uint32_t channel) +{ + ADC_ChannelConfTypeDef sConfig = {0}; + // Configure the specific channel + sConfig.Channel = channel; + sConfig.Rank = ADC_RANK_CHANNEL_NUMBER; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) + { + Error_Handler(); + } + + // Start conversion + if (HAL_ADC_Start(&hadc1) != HAL_OK) + { + Error_Handler(); + } + + // Poll for completion (timeout 10ms) + if (HAL_ADC_PollForConversion(&hadc1, 10) != HAL_OK) + { + Error_Handler(); + } + + // Get result + uint16_t adc_value = HAL_ADC_GetValue(&hadc1); + + // Stop ADC + HAL_ADC_Stop(&hadc1); + + return adc_value; +} /* USER CODE END 4 */ /**