cembeddedstm32bare-metal

STM32F401RE issue with PWM


Im using a Nucleo board for my shenanigans, I have this PA5 pin on my chip that is connected to the LED2 on the board and can generate a PWM signal but it doesn't and I do not have the clarity to solve it

  RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
  RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
  GPIOA->MODER |= ( 0x2UL << GPIO_MODER_MODE5_Pos); //alternate mode
  GPIOA->AFR[0]|= 0x000000010;//alternate function for port A 5
  TIM2->PSC = 840 - 1;     // Set prescaler
  TIM2->ARR = 1000;         // Set auto-reload
  TIM2->CCMR1 |= ( 0x6UL << TIM_CCMR1_OC1M_Pos );
  TIM2->CCMR1 |= TIM_CCMR1_OC1PE;
  TIM2->CR1 |= TIM_CR1_ARPE;
  TIM2->CCER &= ~TIM_CCER_CC1P;
  TIM2->CCER |= TIM_CCER_CC1E;
  TIM2->CR1 |= TIM_CR1_URS;
  TIM2->EGR |= TIM_EGR_UG;
  TIM2->DIER |= ( TIM_DIER_CC1IE | TIM_DIER_UIE );
  TIM2->SR &= ~( TIM_SR_CC1IF | TIM_SR_UIF );

Here's the code, hope someone can help

I tried swapping around the bits of alternate functions but it didn't work, maybe I just dont know how to fiddle with it


Solution

  •   RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
      RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
      GPIOA->MODER |= ( 0x2UL << GPIO_MODER_MODE5_Pos); //alternate mode
      GPIOA->AFR[0]|= 0x000100000;//alternate function for port A 5
      TIM2->PSC = 840 - 1;     // Set prescaler
      TIM2->ARR = 1000;         // Set auto-reload
      TIM2->CCMR1 |= ( 0x6UL << TIM_CCMR1_OC1M_Pos );
      TIM2->CCMR1 |= TIM_CCMR1_OC1PE;
      TIM2->CR1 |= TIM_CR1_ARPE|TIM_CR1_CEN|TIM_CR1_URS;
      TIM2->CCER &= ~TIM_CCER_CC1P;
      TIM2->CCER |= TIM_CCER_CC1E;
      TIM2->EGR |= TIM_EGR_UG;
      TIM2->DIER |= ( TIM_DIER_CC1IE | TIM_DIER_UIE );
      TIM2->SR &= ~( TIM_SR_CC1IF | TIM_SR_UIF );
    

    This is the final code that worked, Thanks to Ilya and pmacfarlane