2021-01-19 09:11 AM
Background:
I'm trying to set up DAC1 Ch1 & Ch2 on my motor project and need some understanding of the DAC use. I have read the Ref Manual but am still struggling.
My motor project is using a STM32G431C8Ux (48-pin) device but MCSDK does not offer this as an option so I made a MCSDK project with a STM32G431CBUx device and generated a *.STMX file and *.ioc file. I then created a new *.ioc file for the STM32G431C8Ux by importing data from the first project. This approach was suggested by an ST Field Engr.
This seems to be working and I can spin the motor.
Problem:
My problem is I want to use DAC1 Ch2 to output data for another use. Apparently in my MCSDK project both channels of DAC1 were setup to output data. This has rendered my DAC1_CH2 as not usable. I'm trying to find where in the *.STMX file or *.ico file does it set up the DACs to be used for the ST debug channels. I want to disable the use of DAC1_CH2 by the ST motor code so I can use it in my code.
Presently, this is my Mx_DAC1_Init code:
static void MX_DAC1_Init(void)
{
/* USER CODE BEGIN DAC1_Init 0 */
/* USER CODE END DAC1_Init 0 */
DAC_ChannelConfTypeDef sConfig = {0};
/* USER CODE BEGIN DAC1_Init 1 */
/* USER CODE END DAC1_Init 1 */
/** DAC Initialization
*/
hdac1.Instance = DAC1;
if (HAL_DAC_Init(&hdac1) != HAL_OK)
{
Error_Handler();
}
/** DAC channel OUT1 config
*/
sConfig.DAC_HighFrequency = DAC_HIGH_FREQUENCY_INTERFACE_MODE_AUTOMATIC;
sConfig.DAC_DMADoubleDataMode = DISABLE;
sConfig.DAC_SignedFormat = DISABLE;
sConfig.DAC_SampleAndHold = DAC_SAMPLEANDHOLD_DISABLE;
sConfig.DAC_Trigger = DAC_TRIGGER_SOFTWARE;
sConfig.DAC_Trigger2 = DAC_TRIGGER_NONE;
sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_DISABLE;
sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_EXTERNAL;
sConfig.DAC_UserTrimming = DAC_TRIMMING_FACTORY;
if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_1) != HAL_OK)
{
Error_Handler();
}
/** DAC channel OUT2 config
*/
sConfig.DAC_Trigger = DAC_TRIGGER_SOFTWARE;
sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_EXTERNAL;
if (HAL_DAC_ConfigChannel(&hdac1, &sConfig, DAC_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN DAC1_Init 2 */
/* USER CODE END DAC1_Init 2 */
}
I am using DAC1_CH2 in my own code as shown below. Even if I make y0 a constant 2047, the output is a sinewave that follows the motor bemf. The data appears to be a combination of my write and the ST debug write.
/* y0 is filtered bemf */
y0 = FilterBEMF( x0 );
/* write to DAC to observe during debug .... still spiky ? ---.... */
DAC1->DHR12R2 = y0;
DAC1->SWTRIGR = DAC_SWTRIGR_SWTRIG2;
Thanks,
edit Additional Info:
In file stm32g4xx_mc_it.c I modified the code as shown below:
before:
// Highfrequency task
UI_DACUpdate(TSK_HighFrequencyTask());
/* USER CODE BEGIN HighFreq */
after:
TSK_HighFrequencyTask();
This is not a permanent fix, for one, it is not in USER code. But, It did disable the DAC1 update except for the update I do in my code. This works and I see the sinewave I am generating and there are no 'spikes'.
Tis just shows the MCSDK update of the DAC1 CH1 and CH2 is the source of my problem. Where do I enable/disable this function in the generated C code?
2021-02-09 07:09 AM
Hi @BTrem.1 ,
Have you already tried to disable the DAC in the MCSDK project ?
Select the "DAC Functionality" block and select "Not Used" for both DAC channels.
Then generate the .ioc and proceed as you already have described.
In the C source code you can add the configuration of the DAC as you need.
I hope this can help you !
2021-02-15 07:16 PM
I haven't tried this yet but plan to do so. I've been hesitant to try because I didn't want the code regeneration to replace any code I need in my existing project. I'll try this on a backup directory then do a 'Beyond Compare' folder comparison on the result with my existing project.
Thanks,
BT
2021-06-01 01:51 AM
The question has been set only to the "STM32 Motor Control" topic (the question is only about the STM32 SDK).
Best regards
Laurent Ca...