Pulling hair out with STM32 PWM/interrupt polarity - Page 1
Forum section: electronics / general

author: uer166
author_rank: Super Contributor
timestamp: July 22, 2020, 12:17:06 am
content: I must be mentally challenged or something. I want a positive polarity PWM, with an interrupt on the falling edge. From the reference manual I selected "PWM Mode 1":"In upcounting, channel 1 is active as long as TIMx_CNT<TIMx_CCR1 else inactive." Basically PWM output is high until counter reaches the compare register, at which point I need the PWM reset an interrupt generated. Other tidbits are:CH Polarity set to highUp counter modeCounter period set to 17000, CCR1 set to PWM Mode 1, value is 10200The attachment shows yellow PWM and blue Timer 1 CCR1 ISR in blue. Also a screenshot of the reference manual of how it should be. Whyyyy is the ISR triggered in the rising, and not the falling edge??Auto-generated code looks fine:Code:TIM_ClockConfigTypeDef sClockSourceConfig = {0};TIM_ClearInputConfigTypeDef sClearInputConfig = {0};TIM_MasterConfigTypeDef sMasterConfig = {0};TIM_OC_InitTypeDef sConfigOC = {0};TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};htim1.Instance = TIM1;htim1.Init.Prescaler = 1;htim1.Init.CounterMode = TIM_COUNTERMODE_UP;htim1.Init.Period = 17000;htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;htim1.Init.RepetitionCounter = 0;htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;if (HAL_TIM_Base_Init(&htim1) != HAL_OK){Error_Handler();}sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK){Error_Handler();}if (HAL_TIM_PWM_Init(&htim1) != HAL_OK){Error_Handler();}sClearInputConfig.ClearInputState = ENABLE;sClearInputConfig.ClearInputSource = TIM_CLEARINPUTSOURCE_COMP1;if (HAL_TIM_ConfigOCrefClear(&htim1, &sClearInputConfig, TIM_CHANNEL_1) != HAL_OK){Error_Handler();}sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK){Error_Handler();}sConfigOC.OCMode = TIM_OCMODE_PWM1;sConfigOC.Pulse = 10200;sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK){Error_Handler();}sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;sBreakDeadTimeConfig.DeadTime = 0;sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;sBreakDeadTimeConfig.BreakFilter = 0;sBreakDeadTimeConfig.BreakAFMode = TIM_BREAK_AFMODE_INPUT;sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;sBreakDeadTimeConfig.Break2Filter = 0;sBreakDeadTimeConfig.Break2AFMode = TIM_BREAK_AFMODE_INPUT;sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK){Error_Handler();}HAL_TIM_MspPostInit(&htim1);

author: uer166
author_rank: Super Contributor
timestamp: July 22, 2020, 12:24:56 am
content: And in the classic uer166 fashion as soon as they post a comment after hours upon hours of debugging, a solution is found. Apparently Cube doesn't actually generate the right Interrupt Enable flags and it has to be done by the user:Code:htim1.Instance->DIER |= TIM_DIER_CC1IE_Msk; // And remove all others (CC2/3/4IE)