2024-12-11 06:43 PM - last edited on 2024-12-12 12:16 AM by SofLit
My goal is to connect 2 internal PGAs to 1 ADC (especially ADC2).
I configured in CubeMX and generated source code.
However, when I checked the ADC channel initialization code in IDE project, I could see that only one channel is activated in ADC2.
/**
* @brief ADC2 Initialization Function
* @PAram None
* @retval None
*/
static void MX_ADC2_Init(void)
{
/* USER CODE BEGIN ADC2_Init 0 */
/* USER CODE END ADC2_Init 0 */
ADC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN ADC2_Init 1 */
/* USER CODE END ADC2_Init 1 */
/** Common config
*/
hadc2.Instance = ADC2;
hadc2.Init.ClockPrescaler = ADC_CLOCK_SYNC_PCLK_DIV2;
hadc2.Init.Resolution = ADC_RESOLUTION_12B;
hadc2.Init.DataAlign = ADC_DATAALIGN_RIGHT;
hadc2.Init.GainCompensation = 0;
hadc2.Init.ScanConvMode = ADC_SCAN_DISABLE;
hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc2.Init.LowPowerAutoWait = DISABLE;
hadc2.Init.ContinuousConvMode = DISABLE;
hadc2.Init.NbrOfConversion = 1;
hadc2.Init.DiscontinuousConvMode = DISABLE;
hadc2.Init.ExternalTrigConv = ADC_SOFTWARE_START;
hadc2.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_NONE;
hadc2.Init.DMAContinuousRequests = DISABLE;
hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc2.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc2) != HAL_OK)
{
Error_Handler();
}
/** Configure Regular Channel
*/
sConfig.Channel = ADC_CHANNEL_VOPAMP2;
sConfig.Rank = ADC_REGULAR_RANK_1;
sConfig.SamplingTime = ADC_SAMPLETIME_2CYCLES_5;
sConfig.SingleDiff = ADC_SINGLE_ENDED;
sConfig.OffsetNumber = ADC_OFFSET_NONE;
sConfig.Offset = 0;
if (HAL_ADC_ConfigChannel(&hadc2, &sConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC2_Init 2 */
/* USER CODE END ADC2_Init 2 */
}
I've tried it on several G-series MCUs, but they all show the same symptoms.
What is the problem?
2024-12-12 12:14 AM - edited 2024-12-12 12:16 AM
Hello @just4u78 ,
Could you please share your ioc file?
What CubeMx version are you using?
PS: in next time please use </> to paste your code: https://community.st.com/t5/community-guidelines/how-to-insert-source-code/ta-p/693413
2024-12-18 10:05 PM
I'm appreciate you for your interest in my question.
My goal is to have 1 MCU control 2 BLDC motors with minimal components.
So I want to configure the hardware in a way that the BLDCs are controlled through one shared shunt resistor.
And I want to minimize the components through internal OP amp and comparator.
I created a custom inverter board configuration using the board manager of the motor control workbench, and created CubeMX ioc file and IDE project using it.
(Motor Control WorkBench Version: 6.3.2 / STM32CubeMX Version 6.13.0 / STM32CubeIDE Version: 1.17.0)
And I was able to configure the Motor Control Workbench project through this custom board file.
I modified the current sensing circuit to connect only to ADC2.
This is the generated C initialization code of ADC2. However, there are no 2nd ADC channel.
/**
* @brief ADC2 Initialization Function
* @PAram None
* @retval None
*/
static void MX_ADC2_Init(void)
{
/* USER CODE BEGIN ADC2_Init 0 */
/* USER CODE END ADC2_Init 0 */
ADC_InjectionConfTypeDef sConfigInjected = {0};
/* USER CODE BEGIN ADC2_Init 1 */
/* USER CODE END ADC2_Init 1 */
/** Common config
*/
hadc2.Instance = ADC2;
hadc2.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1;
hadc2.Init.Resolution = ADC_RESOLUTION_12B;
hadc2.Init.DataAlign = ADC_DATAALIGN_LEFT;
hadc2.Init.GainCompensation = 0;
hadc2.Init.ScanConvMode = ADC_SCAN_ENABLE;
hadc2.Init.EOCSelection = ADC_EOC_SINGLE_CONV;
hadc2.Init.LowPowerAutoWait = DISABLE;
hadc2.Init.ContinuousConvMode = DISABLE;
hadc2.Init.NbrOfConversion = 1;
hadc2.Init.DiscontinuousConvMode = DISABLE;
hadc2.Init.DMAContinuousRequests = DISABLE;
hadc2.Init.Overrun = ADC_OVR_DATA_PRESERVED;
hadc2.Init.OversamplingMode = DISABLE;
if (HAL_ADC_Init(&hadc2) != HAL_OK)
{
Error_Handler();
}
/** Configure Injected Channel
*/
sConfigInjected.InjectedChannel = ADC_CHANNEL_VOPAMP3_ADC2;
sConfigInjected.InjectedRank = ADC_INJECTED_RANK_1;
sConfigInjected.InjectedSamplingTime = ADC_SAMPLETIME_6CYCLES_5;
sConfigInjected.InjectedSingleDiff = ADC_SINGLE_ENDED;
sConfigInjected.InjectedOffsetNumber = ADC_OFFSET_NONE;
sConfigInjected.InjectedOffset = 0;
sConfigInjected.InjectedNbrOfConversion = 2;
sConfigInjected.InjectedDiscontinuousConvMode = DISABLE;
sConfigInjected.AutoInjectedConv = DISABLE;
sConfigInjected.QueueInjectedContext = DISABLE;
sConfigInjected.ExternalTrigInjecConv = ADC_EXTERNALTRIGINJEC_T8_TRGO;
sConfigInjected.ExternalTrigInjecConvEdge = ADC_EXTERNALTRIGINJECCONV_EDGE_RISINGFALLING;
sConfigInjected.InjecOversamplingMode = DISABLE;
if (HAL_ADCEx_InjectedConfigChannel(&hadc2, &sConfigInjected) != HAL_OK)
{
Error_Handler();
}
/** Configure Injected Channel
*/
sConfigInjected.InjectedRank = ADC_INJECTED_RANK_2;
if (HAL_ADCEx_InjectedConfigChannel(&hadc2, &sConfigInjected) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN ADC2_Init 2 */
/* USER CODE END ADC2_Init 2 */
}
This code does not initialize the 2 channels that I set, and only sets 1 channel twice.
I tried setting VOPAMP2 instead of ADC2_IN3 in case there was a problem with the channel, but the same problem still occurs.
I changed the MCU to STM32G471 and tested it, but the same problem occurred.
(Refer the attached ioc file - G473_ADC2_Assign_Test.ioc)
I can change it to set the channel manually, but I doubt the code involved will work properly.
So to solve this problem, I think I need to make the automatically generated code work properly.
2024-12-19 12:02 AM
You didn't share your ioc file.
2024-12-19 12:51 AM - edited 2024-12-19 12:56 AM
There was an error uploading the article, so I think the uploaded file was missing while rewriting it.
Please refer to the attached file for re-uploading.
The zip file contains the custom board manager file of the motor control workbench, the motor control workbench project file with custom board manager, and the ioc file of cubemx generated by motor control workbench.
The ioc file is a file for testing the generation of ADC2 initialization code of G471 MCU.