最大限度地减少中断时间,最大化编译器处理速度,达到STM32F3运算符速度

时间:2015-08-20 07:51:03

标签: c performance interrupt stm32

我目前正在研究STM32F3 Discovery Board。我在C中编写了一个基本程序,看看它是否工作在72 MHz,大约是14 ns。希望它能在这个频率下工作,但是当我想在设备上只切换一个随机引脚时,将其打开或关闭需要660 ns的无限时间循环。然后我用示波器检查了每个命令需要多少。价值观可能不准确,这是我发现的; while循环(仅用于检查)= 120 ns,“for”循环(仅用于检查)= 160 ns 一圈while = 280 ns,一圈for for = 320 ns,这意味着每圈需要160 ns。 asm(“NOP”)= 14 ns(必须等于运营商频率(72 MHz,如预期的那样),空函数调用= 225 ns 我不认为这些值对于其他编译器或其他计算机是相同的,但我甚至不接近72 MHz。我得到的最小中断是2微秒。有没有办法减少这些价值?反正有没有增加gcc编译器的速度?

#include "stm32f30x.h"
#include <common.h>

void InitializeTimer(void) ;
void EnableTimerInterrupt(void);
void TIM2_IRQHandler(void);
GPIO_InitTypeDef configuration;
void dly(int dly_nmbr);
int main(void)
{

  GPIO_InitTypeDef GPIO_InitStructure;
//NVIC_InitTypeDef NVIC_InitStructure;
  RCC_ClocksTypeDef RCC_ClockFreq;
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  /* Configure PA10 in output pushpull mode */
  /*****I called init_pin function from common.c because it was easier
  but I guess it takes some time too*********/
  init_pin(GPIOA,GPIO_Pin_10,GPIO_Mode_OUT,GPIO_Speed_50MHz,GPIO_OType_PP,GPIO_PuPd_NOPULL);

/****I tried usual way to configure the pins too****/
//configuration.GPIO_Pin = GPIO_Pin_10;
//configuration.GPIO_Mode = GPIO_Mode_OUT;
//configuration.GPIO_OType = GPIO_OType_PP;
//configuration.GPIO_Speed = GPIO_Speed_50MHz;
//configuration.GPIO_PuPd = GPIO_PuPd_NOPULL;
//GPIO_Init(GPIOA, &configuration);
//InitializeTimer();
/*** This function fills the RCC_ClockFreq structure with the current
  frequencies of different on chip clocks (for debug purpose) **************/
  RCC_GetClocksFreq(&RCC_ClockFreq);

  /* Enable Clock Security System(CSS): this will generate an NMI exception
  when HSE clock fails *****************************************************/
  RCC_ClockSecuritySystemCmd(ENABLE);

  /* Enable and configure RCC global IRQ channel, will be used to manage HSE ready 
     and PLL ready interrupts. 
     These interrupts are enabled in stm32f0xx_it.c file **********************/
  /* I disabled the interrupts for the purpose of maximizing the program speed*/
//   NVIC_InitStructure.NVIC_IRQChannel = RCC_IRQn;
//   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
//   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//   NVIC_Init(&NVIC_InitStructure);


  /************* Output HSE clock on MCO1 pin(PA8) ************/
  /****************** Enable the GPIOA Clock ******************/
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  /* MCO pin configuration: PA8 */
  GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd  = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_8;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  /* Output System Clock on MCO pin */
  RCC_MCOConfig(RCC_MCOSource_SYSCLK);

  /************** This is the toggling part. I tried each command here ***************/

  while(1)
  {
//     volatile int i = 0;
//     dly(0); 
       /*I called an empty function called dly before and after the toggle 
       to see function call time*/
       GPIO_Toggle(GPIOA,GPIO_Pin_10);
//     dly(0);
//       while(i<=0)
//     {
//       i++;
//     }
//     for(i=0;i<=0;i++);

  }
}
/*Empty Function*/
void dly(int dly_nmbr)
{
}

/************** Interrupt Functions  **************/
// void TIM2_IRQHandler()
//  {
//      if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
//      {
//          TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
//      }
//  }

//  void InitializeTimer()
//  {
//      RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
//  
//      TIM_TimeBaseInitTypeDef timerInitStructure;
//      timerInitStructure.TIM_Prescaler = 0;
//      timerInitStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned2;
//      timerInitStructure.TIM_Period = 2;
//      timerInitStructure.TIM_ClockDivision = 0x0000;                                
//      timerInitStructure.TIM_RepetitionCounter = 100;
//      TIM_TimeBaseInit(TIM2, &timerInitStructure);
//      
//      TIM_Cmd(TIM2, ENABLE);
//      TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
//      EnableTimerInterrupt();
//  }

//  void EnableTimerInterrupt()
//  {
//      NVIC_InitTypeDef nvicStructure;
//      nvicStructure.NVIC_IRQChannel = TIM2_IRQn;
//      nvicStructure.NVIC_IRQChannelCmd = ENABLE;
// //      TIM2->ARR = 1;
// //      TIM2->PSC = 1;
//      NVIC_Init(&nvicStructure);
//      
//  }
/****************** EOF ********************/

0 个答案:

没有答案