cancel
Showing results for 
Search instead for 
Did you mean: 

Toggling pin PE9 using SMT32F407G DISCO board

LMorr.3
Senior II

I am not able to get pin PE9 to toggle using timer1 and Channel1 as a PWM Output. I have tried different configurations for the timer with no success. The timer does run as expected, and all parameters and interrupts work. The only issue is pin PE9 does not toggle or enable at any time while the timer runs.

UPDATE: I connected the pin to one of the dev board's leds and it does turn on as expected. For some reason, when I use the following to inspect the pin status, it always shows as '0' or off. Does anyone know why the led turns on but the GPIOE would show '0' when I stop the debugger to inspect the register?

Here is my config for TIM1:

 TIM_ClockConfigTypeDef sClockSourceConfig = {0};

 TIM_MasterConfigTypeDef sMasterConfig = {0};

 TIM_OC_InitTypeDef sConfigOC = {0};

 TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};

 /* USER CODE BEGIN TIM1_Init 1 */

 /* USER CODE END TIM1_Init 1 */

 htim1.Instance = TIM1;

 htim1.Init.Prescaler = 65535;

 htim1.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim1.Init.Period = 65535;

 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();

 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

 if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)

 {

  Error_Handler();

 }

 sConfigOC.OCMode = TIM_OCMODE_PWM2;

 sConfigOC.Pulse = 10000;

 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.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;

 if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)

 {

  Error_Handler();

 }

 /* USER CODE BEGIN TIM1_Init 2 */

 /* USER CODE END TIM1_Init 2 */

 HAL_TIM_MspPostInit(&htim1);

1 ACCEPTED SOLUTION

Accepted Solutions
LMorr.3
Senior II

Fixed! I had the pin set as Open Drain instead of PushPull for some reason.

Thanks for all your help!

View solution in original post

12 REPLIES 12
TDK
Guru

> htim1.Init.Prescaler = 65535;

> htim1.Init.Period = 65535;

That's a super slow refresh time. Are you waiting long enough to inspect the register?

GPIO_IDR is connected when the pin is in AF mode. There's no reason for it not to reflect the pin's output state unless the pin is being tied low or high. Make it a GPIO and toggle high/low to ensure you're reading/interpreting IDR correctly.

> Does anyone know why the led turns on but the GPIOE would show '0' when I stop the debugger to inspect the register?

How exactly are you making this determination? "GPIOE" isn't a register and certainly there is a possibility that the output state is low, in which case the input state would also be low.

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

I do see the GPIOD->ODR with correct values now.

I'm still having trouble getting it to work with TIM1. I have it working with TIM4. When using TIM1, the output pin stays low.

Sounds like your code has changed quite a bit based on what you are reporting. TIM1 has a MOE bit. Otherwise should be same as TIM4, with different pins and AF mode.
If you feel a post has answered your question, please click "Accept as Solution".
LMorr.3
Senior II

I do see the register being updated now, so that part is working ok. ( I tested with a second timer, TIM4 and it works well )

I'm still not seeing the pin toggle for TIM1. I have the output pin selected with alternate function 'TIM1_CH1'. It DOES work ok on TIM4 but still cannot get TIM1 to do the same.

Maybe there is something else tied to TIM1 which is preventing it from toggling the output pin... I'll be testing more today.

As TDK said above, TIM1 is an Advanced timer and you have to set TIM1_BDTR.MOE to enable its output.

If in doubts, read out the TIM and relevant GPIO registers content and check/post.

JW

LMorr.3
Senior II

I have confirmed the MOE=1, OSSR=0, CC1E=1, CC1NE=0 and use the following to watch the pin status. The pin stays low for some reason:

HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_8)

LMorr.3
Senior II

Is it possible I have a defective SM32F407? I don't see any other settings I can try and it seems odd that such a basic function is not working on TIM1 but is on TIM4.

UPDATE: I added Channel 2 to TIM1 and it does toggle pin PE11 as expected. Channel 1 for TIM1 still does not toggle the pin. I have the same confi for Channel 1 as Channel 2. Is there something else tied to TIM1_CH1 ?

Unlikely. Maybe post your complete code instead of just saying it’s not working.
If you feel a post has answered your question, please click "Accept as Solution".

> HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_8)

This reads PA8. How do you check PE9 with this code?

Why don't you simply look at GPIOE_IDR in debugger?

Use an oscilloscope/logic analyzer to observe the pin's state. Or connect a LED, if you can blink it slowly enough.

And why don't you read out TIM1 and GPIOE registers and check/post?

> Is there something else tied to TIM1_CH1 ?

Check yourself, the schematics to DiscoF4 is here https://www.st.com/en/evaluation-tools/stm32f4discovery.html#cad-resources

JW