cancel
Showing results for 
Search instead for 
Did you mean: 

How can I use Capture Compare as external trigger source?

DMårt
Lead

Hi!

This might be a simple question, but I still haven't solved it.

I want to create an external trigger source for SDADC 16-bit and I have selected TIM12 Capture Compare 1 as external trigger source.

0693W00000BbUBbQAN.png 

Then I have enable the Output Compare, because I did not find Capture Compare.

0693W00000BbUBgQAN.png 

Then I enable the NVIC for the SDADC.

	HAL_TIM_Base_Start(htim13); /* TIM13 is trigger source for SDADC1 */
	HAL_TIM_Base_Start(htim12); /* TIM12 is trigger source for SDADC2 */
	HAL_TIM_Base_Start(htim16); /* TIM16 is trigger source for SDADC3 */
	if (HAL_SDADC_InjectedStart_IT(hsdadc1) != HAL_OK)
		Error_Handler();
	if (HAL_SDADC_InjectedStart_IT(hsdadc2) != HAL_OK)
		Error_Handler();
	if (HAL_SDADC_InjectedStart_IT(hsdadc3) != HAL_OK) /* Differential only */
		Error_Handler();

But still. Nothing happens when I have this function avaiable.

This never calls. Why?

/* Callbacks for SDADC */
void HAL_SDADC_InjectedConvCpltCallback(SDADC_HandleTypeDef *hsdadc){
	if(hsdadc->Instance == SDADC1) {
		STM32_PLC_Analog_Input_SDADC1(hsdadc);
	}else if (hsdadc->Instance == SDADC2) {
		STM32_PLC_Analog_Input_SDADC2(hsdadc);
	}else if (hsdadc->Instance == SDADC3) {
		STM32_PLC_Analog_Input_SDADC3(hsdadc);
	}
}

STM32MP151AAC3 custom board with STM32-OS as operating system: https://github.com/DanielMartensson/STM32-Computer
1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

Include your chip part number.

You start the timer, but I don't see you starting the channel anywhere. Should be a call to HAL_TIM_OC_Start or similar.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

5 REPLIES 5
TDK
Guru

Include your chip part number.

You start the timer, but I don't see you starting the channel anywhere. Should be a call to HAL_TIM_OC_Start or similar.

If you feel a post has answered your question, please click "Accept as Solution".

If you want to trigger the SDADC using a signal coming from outside the STM32, you have to set the timer's channel to Input Capture.

Read the timer chapter in RM.

JW

So if I want a trigger source that using a signal inside the STM32. How should I do then?

STM32MP151AAC3 custom board with STM32-OS as operating system: https://github.com/DanielMartensson/STM32-Computer

Did not work to use HAL_TIM_OC_Start(htim, TIM_CHANNEL_x)

STM32MP151AAC3 custom board with STM32-OS as operating system: https://github.com/DanielMartensson/STM32-Computer

By the way!

It's working now!

Mode in CubeMX need to be Toggle On Match.

Code:

static uint32_t Channel = 0;
static uint32_t SDADC_Single[12];
static uint32_t SDADC_Differential[5];
 
void STM32_PLC_Start_Analog_Input(TIM_HandleTypeDef* htim12, TIM_HandleTypeDef* htim13, TIM_HandleTypeDef* htim16, SDADC_HandleTypeDef* hsdadc1, SDADC_HandleTypeDef* hsdadc2, SDADC_HandleTypeDef* hsdadc3) {
	HAL_TIM_OC_Start(htim13, TIM_CHANNEL_1); /* TIM13 is trigger source for SDADC1 */
	HAL_TIM_OC_Start(htim12, TIM_CHANNEL_1); /* TIM12 is trigger source for SDADC2 */
	HAL_TIM_OC_Start(htim16, TIM_CHANNEL_1); /* TIM16 is trigger source for SDADC3 */
	if (HAL_SDADC_InjectedStart_IT(hsdadc1) != HAL_OK)
		Error_Handler();
	if (HAL_SDADC_InjectedStart_IT(hsdadc2) != HAL_OK)
		Error_Handler();
	if (HAL_SDADC_InjectedStart_IT(hsdadc3) != HAL_OK) /* Differential only */
		Error_Handler();
}
 
void STM32_PLC_Analog_Input_SDADC1(SDADC_HandleTypeDef* hsdadc1) {
	SDADC_Single[Channel] = HAL_SDADC_InjectedGetValue(hsdadc1, &Channel); /* Channel will have the numbers 0,1,2,3,4,5,6,7,8 but randomly */
}
 
void STM32_PLC_Analog_Input_SDADC2(SDADC_HandleTypeDef* hsdadc2) {
	SDADC_Single[Channel + 9] = HAL_SDADC_InjectedGetValue(hsdadc2, &Channel); /* Channel will have the numbers 0,1,2 but randomly */
}
 
void STM32_PLC_Analog_Input_SDADC3(SDADC_HandleTypeDef* hsdadc3) {
	uint32_t value = HAL_SDADC_InjectedGetValue(hsdadc3, &Channel); /* Channel will have the numbers 0,2,4,6,8 but randomly */
	switch(Channel) {
		case 0:
			SDADC_Differential[0] = value;
			break;
		case 2:
			SDADC_Differential[1] = value;
			break;
		case 4:
			SDADC_Differential[2] = value;
			break;
		case 6:
			SDADC_Differential[3] = value;
			break;
		case 8:
			SDADC_Differential[4] = value;
			break;
	}
}
 
void STM32_PLC_Analog_Input_SDADC1_Set_Prescaler(TIM_HandleTypeDef* htim13, uint16_t prescaler){
	htim13->Instance->PSC = prescaler;
}
 
void STM32_PLC_Analog_Input_SDADC2_Set_Prescaler(TIM_HandleTypeDef* htim12, uint16_t prescaler){
	htim12->Instance->PSC = prescaler;
}
 
void STM32_PLC_Analog_Input_SDADC3_Set_Prescaler(TIM_HandleTypeDef* htim16, uint16_t prescaler){
	htim16->Instance->PSC = prescaler;
}
 
/* Get ADC0 to ADC11 */
uint16_t STM32_PLC_Analog_Input_GetADC(uint8_t i){
	return SDADC_Single[i];
}
 
/* Get DADC0 to DADC4 */
uint16_t STM32_PLC_Analog_Input_GetDADC(uint8_t i){
	return SDADC_Differential[i];
}
 
/* Callbacks for SDADC */
void HAL_SDADC_InjectedConvCpltCallback(SDADC_HandleTypeDef *hsdadc){
	if(hsdadc->Instance == SDADC1) {
		STM32_PLC_Analog_Input_SDADC1(hsdadc);
	}else if (hsdadc->Instance == SDADC2) {
		STM32_PLC_Analog_Input_SDADC2(hsdadc);
	}else if (hsdadc->Instance == SDADC3) {
		STM32_PLC_Analog_Input_SDADC3(hsdadc);
	}
}

STM32MP151AAC3 custom board with STM32-OS as operating system: https://github.com/DanielMartensson/STM32-Computer