AnsweredAssumed Answered

Unexplainable Glitching on STM2F4 (or maybe not)

Question asked by roofie01 on Feb 19, 2015
Latest reply on Feb 20, 2015 by roofie01

I am using an STM32F4 Discovery board, and nothing attached to it except scope probes.
I am programming Timer 3 for PWM mode. Please see the attached image. The yellow trace is Timer 3, the green is Timer 4, which follows Timer 3 by a period of 180 degrees shift. The top PWM train (Timer 3) seems to want to momentarily go high for about 100ms then low, and it doesn't agree with anything I am programming it for. There are supposed to be exactly 4 pulses at this frequency, which there are  - however, that first initial 100ms pulse is not supposed to be there. You can see that Timer 4 (green) follows Timer 3, including the initial pulse - which can be expected.
It's a bit confusing as it doesn't appear to be related to the timer, but the pin configuration, yet both Timer 3 and 4 pins are configured exactly the same.

To see what would happen on the PA6 / Timer3 pin, I commented out the call to the PWM_Timer_Config (in main). I added  "GPIO_ResetBits" for both pins. Even with the pins configured to GPIO_PuPd_UP, or GPIO_PuPd_NOPULL, the pins stay HIGH. If they are configured to GPIO_PuPd_DOWN,  they are at 2.3V. This doesn't seem normal, because the Timer4 pin does not do this. I have 2 boards that both do show the same behavior.
If I uncomment the "exit(0)" in the interrupt handler, it makes no difference. In fact, if I put an "exit(0)" at the end of the PWM_Configure function, I still see the 100ms pulse.

Any suggestions as to the origin or reason for this 100ms pulse at the start? Totally perplexed...what am I missing?


/***********************************************************************

float32_t pwmData[5][2] = { {0.0,0.0}, {0.0,0.0} };

/* Timer 3 (PA6) Interrupt Handler */
void TIM3_IRQHandler(void)
{
    if( TIM_GetITStatus(TIM3, TIM_IT_Update) )
    {
        /* Clear TIM3 Capture compare interrupt pending bit */
        TIM_ClearITPendingBit(TIM3, TIM_IT_Update);
        
        if(sampleCtr == 4 )
        {
            TIM4->CCR1 = 0;
            TIM3->CCR1 = 0;
            TIM4->CNT = 0;
            TIM3->CNT = 0;            
            exit(0);                 //THIS IS IN HERE FOR TESTING ONLY !!!
        }
     sampleCtr++;
     
     }
}        




void PWM_TIMER_Configuration(float32_t pwmData[5][2], uint8_t idx)
{
        /* set up, initialize timers 3 and 4 to drive PWM signal from defined
        GPIO pins. Timer 4 follows Timer3 by a shift of 180 degrees */
        
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;
    
    uint16_t Prescaler, Period;

    TIM_TimeBaseStructInit( &TIM_TimeBaseStructure );   
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Disable);         
    TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Disable);         
        
    // 20 KHz timebase, assumes APB1 H/4 TIMCLK4 H/2
    Prescaler = ((SystemCoreClock / 2) / TIMER_BASE);
    Period = (uint16_t)(TIMER_BASE / pwmData[idx][0]);
    
    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = Period - 1;
    TIM_TimeBaseStructure.TIM_Prescaler = Prescaler - 1;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    //set timer 3 & 4 to same frequency
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

    //assign variable for counter calculation from timer period
    uint16_t timerPeriod = TIM_TimeBaseStructure.TIM_Period;    
    
    //create pulse width for both channels the same
    uint16_t onTime = (uint16_t) (timerPeriod * pwmData[idx][1]);    
    
    /* Output Compare PWM1 Mode configuration: Channel 1 */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = onTime;

    TIM_OC1Init(TIM3, &TIM_OCInitStructure);
    TIM_OC1Init(TIM4, &TIM_OCInitStructure);
    
    //set timer 3 immediately to timerPeriod offset/2 = 180 degrees shift
    TIM4->CNT = TIM3->CNT + (timerPeriod /2 );                    
    
    TIM_Cmd(TIM4, ENABLE);        
    TIM_Cmd(TIM3, ENABLE);        
    
    //Set up TIM3 interrrupt
    NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);    
    TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);  

}


void RCC_Configuration(void)
{
    /* enable peripheral clock for TIM3 */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

    /* enable peripheral clock for TIM4 */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);    
    
    /* enable peripheral clock for TIM6 */
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
    
    /* GPIOA clock enable */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
 
    /* GPIOD clock enable */
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
}
 
//******************************************************************************
 
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
 
  /* GPIOC Configuration: TIM3 CH1 (PA6) */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOA, &GPIO_InitStructure);     
    
  /* Connect TIM3 pins to AF2 */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_TIM3);   

  /* GPIOD Configuration:  TIM4 on PD12 */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
 
  /* Connect TIM4 pins to AF2 */
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4); // PD12 TIM4_CH1 GREEN

  //asm("nop");

}

int main(void)
{
      RCC_Configuration();
 
    RCC_GetClocksFreq(&RCC_Clocks);
    SysTick_Config(RCC_Clocks.HCLK_Frequency / 100);  
    
    GPIO_DeInit(GPIOA);
    GPIO_DeInit(GPIOD);    
    TIM_DeInit(TIM6);
    TIM_DeInit(TIM3);    
    TIM_DeInit(TIM4);    
    GPIO_Configuration();
    
    
    PWMsequence[0][0] = 12.5
    PWMsequence[0][1] = 0.25
    
    //2nd value is index to start    
    PWM_TIMER_Configuration(txSequence, 0);
    
    while(1)
    {
    }
    
}

Attachments

Outcomes