示例#1
0
void initial_tim8(void) {
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
	TIM_TimeBaseStructure.TIM_ClockDivision = 0;
	TIM_TimeBaseStructure.TIM_Prescaler = 1;
	TIM_TimeBaseStructure.TIM_Period = 36000;
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
	TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);
	TIM_SelectOutputTrigger(TIM8, TIM_TRGOSource_Update);
	TIM_SelectOnePulseMode(TIM8, TIM_OPMode_Single);
	
}
示例#2
0
/**
 * @brief miscTIM_run config miscTIM to run with msec interrupt periodically.
 * @param TIM_Typedef* miscTIM, can be only basic timer (TM6 and TM7)
 * @param uint16_t prescaler
 * @param uint16_t reloadVal
 * @return void
 */
 void miscTIM_run (TIM_TypeDef* miscTIM, uint16_t prescaler, uint16_t reloadVal){
    /**< declare vars */
    bool isTIM6;
    uint32_t PCLK1, prescaler_x_reloadVal;
    RCC_ClocksTypeDef clocksStruct;
    NVIC_InitTypeDef nvicStruct;


    /**< check condition */
    if ( miscTIM == TIM6)
        isTIM6 = true;
    else if (miscTIM == TIM7)
        isTIM6 = false;
    else
        return;

    /**< init miscTIM in update mode with prescaler and reloadVal */
    RCC_APB1PeriphClockCmd ( (isTIM6 ? RCC_APB1Periph_TIM6 : RCC_APB1Periph_TIM7), ENABLE );

    TIM_ARRPreloadConfig (miscTIM, ENABLE);
    TIM_ITConfig (miscTIM, TIM_IT_Update, ENABLE);
    TIM_PrescalerConfig (miscTIM, prescaler, TIM_PSCReloadMode_Immediate);
    TIM_SetAutoreload (miscTIM, reloadVal);
    TIM_UpdateDisableConfig (miscTIM, DISABLE);
    TIM_UpdateRequestConfig (miscTIM, TIM_UpdateSource_Global);
    TIM_SelectOnePulseMode (miscTIM, TIM_OPMode_Repetitive);

    TIM_Cmd (miscTIM, ENABLE);

    /**< init NVIC TIM6 channel */
    nvicStruct.NVIC_IRQChannel = TIM6_IRQn;
    nvicStruct.NVIC_IRQChannelCmd = ENABLE;
    nvicStruct.NVIC_IRQChannelPreemptionPriority = 0x0F;
    nvicStruct.NVIC_IRQChannelSubPriority = 0x0F;

    NVIC_Init (&nvicStruct);


    /**< calculate miscTIM_period */
    RCC_GetClocksFreq (&clocksStruct);
    PCLK1 = clocksStruct.PCLK1_Frequency;

    PCLK1 /= 1000;

    prescaler_x_reloadVal = prescaler*reloadVal;

    miscTIM_period = prescaler_x_reloadVal / PCLK1;


    return;
}
示例#3
0
文件: timer.c 项目: httpftpli/key
void timerInit(TIM_TypeDef* TIMx,unsigned short perscaler,unsigned short durationVal, unsigned short TIM_CounterMode,unsigned char isOnePulse, unsigned short intSource){
  unsigned int RCC_APBPeriph_TIMx;
  unsigned int temp = (unsigned int)TIMx;
  switch(temp){
  case (unsigned int)TIM2:
    RCC_APBPeriph_TIMx = RCC_APB1Periph_TIM2;
    RCC_APB1PeriphClockCmd(RCC_APBPeriph_TIMx, ENABLE);
    break;
  case (unsigned int)TIM3:
    RCC_APBPeriph_TIMx = RCC_APB1Periph_TIM3;
    RCC_APB1PeriphClockCmd(RCC_APBPeriph_TIMx, ENABLE);
    break;
  case (unsigned int)TIM6:
    RCC_APBPeriph_TIMx = RCC_APB1Periph_TIM6;
    RCC_APB1PeriphClockCmd(RCC_APBPeriph_TIMx, ENABLE);
    break;
  case (unsigned int)TIM14:
    RCC_APBPeriph_TIMx = RCC_APB1Periph_TIM14;
    RCC_APB1PeriphClockCmd(RCC_APBPeriph_TIMx, ENABLE);
    break;
  case  (unsigned int)TIM1:
    RCC_APBPeriph_TIMx = RCC_APB2Periph_TIM1;
    RCC_APB2PeriphClockCmd(RCC_APBPeriph_TIMx, ENABLE);
    break;
  case  (unsigned int)TIM15:
    RCC_APBPeriph_TIMx = RCC_APB2Periph_TIM15;
    RCC_APB2PeriphClockCmd(RCC_APBPeriph_TIMx, ENABLE);
    break;
  case  (unsigned int)TIM16:
    RCC_APBPeriph_TIMx = RCC_APB2Periph_TIM16;
    RCC_APB2PeriphClockCmd(RCC_APBPeriph_TIMx, ENABLE);
    break;
  case  (unsigned int)TIM17:
    RCC_APBPeriph_TIMx = RCC_APB2Periph_TIM17;
    RCC_APB2PeriphClockCmd(RCC_APBPeriph_TIMx, ENABLE);
    break;
  default:
    return;
  }

  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  TIM_TimeBaseStructure.TIM_Prescaler = perscaler-1;
  TIM_TimeBaseStructure.TIM_Period = durationVal;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode;
  TIM_TimeBaseInit(TIMx, &TIM_TimeBaseStructure);
  TIM_SelectOnePulseMode(TIMx, (isOnePulse==1)?TIM_OPMode_Single :TIM_OPMode_Repetitive);
  if(intSource!=0){
    TIM_ITConfig(TIMx, TIM_IT_Update, ENABLE);
  }
}
示例#4
0
FUNC(int, OS_APPL_CODE) main(void)
{

  /*
   * Program the timer
   *
   *
   */
  NVIC_InitTypeDef NVIC_InitStructure;

  /* TIM2 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  /* Enable the TIM2 gloabal Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  /* Time base configuration */
  /* Prescaler so the counter count every half milli */
  TIM_TimeBaseStructure.TIM_Prescaler = 42000 - 1;

  /* Time set to 30 ms */
  TIM_TimeBaseStructure.TIM_Period = 60 - 1;

  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Down;
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);

  initBoard();

  StartOS(OSDEFAULTAPPMODE);
  return 0;
}
示例#5
0
文件: main.c 项目: Exchizz/Bachelor
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured, 
       this is done through SystemInit() function which is called from startup
       files (startup_stm32f40_41xxx.s/startup_stm32f427_437xx.s/startup_stm32f429_439xx.s)
       before to branch to application main. 
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f4xx.c file
     */     
       
  /* TIM Configuration */
  TIM_Config();

  /* --------------------------------------------------------------------------
    TIM4 configuration: One Pulse mode
    The external signal is connected to TIM4_CH2 pin (PB.07), 
    The Rising edge is used as active edge,
    The One Pulse signal is output on TIM4_CH1 pin (PB.06)
    The TIM_Pulse defines the delay value 
    The (TIM_Period -  TIM_Pulse) defines the One Pulse value.
     
    TIM4 input clock (TIM4CLK) is set to 2 * APB1 clock (PCLK1), 
    since APB1 prescaler is different from 1.   
      TIM4CLK = 2 * PCLK1  
      PCLK1 = HCLK / 4 
      => TIM4CLK = HCLK / 2 = SystemCoreClock /2

    TIM2CLK = SystemCoreClock/2, we want to get TIM2 counter clock at 42 MHz:
     Prescaler = (TIM2CLK / TIM2 counter clock) - 1
     Prescaler = ((SystemCoreClock /2) /42 MHz) - 1
     
    The Autoreload value is 65535 (TIM4->ARR), so the maximum frequency value 
    to trigger the TIM4 input is 42000000/65535 = 641 Hz.

    The TIM_Pulse defines the delay value, the delay value is fixed 
    to 390 us:
    delay =  CCR1/TIM4 counter clock 
          = 16383 / 42000000 = 390 us. 
    The (TIM_Period - TIM_Pulse) defines the One Pulse value, 
    the pulse value is fixed to 1.170 ms:
    One Pulse value = (TIM_Period - TIM_Pulse) / TIM4 counter clock 
                    = (65535 - 16383) / 42000000 = 1.170 ms.

    Note: 
     SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file.
     Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
     function to update SystemCoreClock variable value. Otherwise, any configuration
     based on this variable will be incorrect.    

  --------------------------------------------------------------------------- */

  /* Compute the prescaler value */
  uhPrescalerValue = (uint16_t) ((SystemCoreClock / 2) / 42000000) - 1;
  
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 65535;
  TIM_TimeBaseStructure.TIM_Prescaler = uhPrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

  /* TIM4 PWM2 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 16383;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC1Init(TIM4, &TIM_OCInitStructure);

  /* TIM4 configuration in Input Capture Mode */

  TIM_ICStructInit(&TIM_ICInitStructure);

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStructure.TIM_ICFilter = 0;

  TIM_ICInit(TIM4, &TIM_ICInitStructure);

  /* One Pulse Mode selection */
  TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Single);

  /* Input Trigger selection */
  TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);

  /* Slave Mode selection: Trigger Mode */
  TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Trigger);

  while (1)
  {
  }
}
示例#6
0
/**
  * @brief  Configure the TIM2.
  * @param  None
  * @retval None
  */
static void TIM_Config(void)
{
  GPIO_InitTypeDef         GPIO_InitStructure;
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_ICInitTypeDef        TIM_ICInitStructure;
  TIM_OCInitTypeDef        TIM_OCInitStructure;
  uint16_t PrescalerValue = 0;
  
  /* TIM clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

  /* GPIOA clock enable */
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  
  /* TIM2_CH1 pin (PA.00) and TIM2_CH2 pin (PA.01) configuration */
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
  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_Init(GPIOA, &GPIO_InitStructure);

  /* Connect TIM pins to AF1 */
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_1); 
   
    /* --------------------------------------------------------------------------
    TIM2 configuration: One Pulse mode
    The external signal is connected to TIM2_CH2 pin (PA.01), 
    The Rising edge is used as active edge,
    The One Pulse signal is output on TIM2_CH1 pin (PA.00)
    The TIM_Pulse defines the delay value 
    The (TIM_Period -  TIM_Pulse) defines the One Pulse value.
     
    TIM2 input clock (TIM2CLK) is set to 2*APB1 clock (PCLK1)   
      TIM2CLK = 2*PCLK1  
      PCLK1 = HCLK  
      => TIM2CLK = HCLK  = SystemCoreClock 

    TIM2CLK = SystemCoreClock, we want to get TIM2 counter clock at 36 MHz:
     Prescaler = (TIM2CLK / TIM2 counter clock) - 1
     Prescaler = (SystemCoreClock  /36 MHz) - 1
     
    The Autoreload value is 65535 (TIM2->ARR), so the maximum frequency value 
    to trigger the TIM2 input is 36000000/65535 = 549.3 Hz.

    The TIM_Pulse defines the delay value, the delay value is fixed 
    to 455.1 us:
    delay =  CCR1/TIM2 counter clock = 455.1 us. 
    The (TIM_Period - TIM_Pulse) defines the One Pulse value, 
    the pulse value is fixed to 1.36 ms:
    One Pulse value = (TIM_Period - TIM_Pulse) / TIM2 counter clock = 1.36 ms.

    Note: 
     SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f30x.c file.
     Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate()
     function to update SystemCoreClock variable value. Otherwise, any configuration
     based on this variable will be incorrect.    

  --------------------------------------------------------------------------- */

  /* Compute the prescaler value */
  PrescalerValue = (uint16_t) ((SystemCoreClock ) / 36000000) - 1;
  
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 65535;
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

  /* Init TIM_OCInitStructure */
  TIM_OCStructInit(&TIM_OCInitStructure);
  
  /* TIM2 PWM2 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 16383;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC1Init(TIM2, &TIM_OCInitStructure);

  /* TIM configuration in Input Capture Mode */

  TIM_ICStructInit(&TIM_ICInitStructure);

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStructure.TIM_ICFilter = 0;

  TIM_ICInit(TIM2, &TIM_ICInitStructure);

  /* One Pulse Mode selection */
  TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Single);

  /* Input Trigger selection */
  TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);

  /* Slave Mode selection: Trigger Mode */
  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Trigger);  
}
示例#7
0
文件: main.c 项目: Joe-Merten/Stm32
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f10x_xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
       system_stm32f10x.c file
     */

  /* System Clocks Configuration */
  RCC_Configuration();

  /* Configure the GPIO ports */
  GPIO_Configuration();

  /* TIM4 configuration: One Pulse mode ------------------------
     The external signal is connected to TIM4_CH2 pin (PB.07),
     The Rising edge is used as active edge,
     The One Pulse signal is output on TIM4_CH1 pin (PB.06)
     The TIM_Pulse defines the delay value
     The (TIM_Period -  TIM_Pulse) defines the One Pulse value.
     TIM2CLK = SystemCoreClock, we want to get TIM2 counter clock at 24 MHz:
     - Prescaler = (TIM2CLK / TIM2 counter clock) - 1
     The Autoreload value is 65535 (TIM4->ARR), so the maximum frequency value
     to trigger the TIM4 input is 24000000/65535 = 300 Hz.

     The TIM_Pulse defines the delay value, the delay value is fixed
     to 682.6 us:
     delay =  CCR1/TIM4 counter clock = 682.6 us.
     The (TIM_Period - TIM_Pulse) defines the One Pulse value,
     the pulse value is fixed to 2.048 ms:
     One Pulse value = (TIM_Period - TIM_Pulse) / TIM4 counter clock = 2.048 ms.

  * SystemCoreClock is set to 72 MHz for Low-density, Medium-density, High-density
    and Connectivity line devices and to 24 MHz for Value line devices
  ------------------------------------------------------------ */

  /* Compute the prescaler value */
  PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;
  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 65535;
  TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

  /* TIM4 PWM2 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 16383;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC1Init(TIM4, &TIM_OCInitStructure);

  /* TIM4 configuration in Input Capture Mode */

  TIM_ICStructInit(&TIM_ICInitStructure);

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStructure.TIM_ICFilter = 0;

  TIM_ICInit(TIM4, &TIM_ICInitStructure);

  /* One Pulse Mode selection */
  TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Single);

  /* Input Trigger selection */
  TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);

  /* Slave Mode selection: Trigger Mode */
  TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Trigger);

  while (1)
  {}
}
示例#8
0
void osdCoreInit(void)
{
    GPIO_InitTypeDef gpio;
    TIM_TimeBaseInitTypeDef tim;
    NVIC_InitTypeDef nvic;
    DMA_InitTypeDef dma;
    SPI_InitTypeDef spi;
    TIM_OCInitTypeDef timoc;
	EXTI_InitTypeDef EXTI_InitStructure;

    // OSD mask Pins
    GPIO_StructInit(&gpio);
    gpio.GPIO_Pin = GPIO_Pin_6; // SPI1 MISO
    gpio.GPIO_Mode = GPIO_Mode_AF;
	gpio.GPIO_OType = GPIO_OType_PP;
    gpio.GPIO_Speed = GPIO_Speed_50MHz;
	gpio.GPIO_PuPd  = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &gpio);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
	
    GPIO_StructInit(&gpio);
    gpio.GPIO_Pin = GPIO_Pin_5; // SPI1 CLK slave
    gpio.GPIO_Mode = GPIO_Mode_AF;
	gpio.GPIO_OType = GPIO_OType_PP;
    gpio.GPIO_Speed = GPIO_Speed_100MHz;
	gpio.GPIO_PuPd  = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA, &gpio);
	GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
	
	//OSD level pins
	gpio.GPIO_Pin = GPIO_Pin_2; // SPI2_MISO
    gpio.GPIO_Mode = GPIO_Mode_AF;
	gpio.GPIO_OType = GPIO_OType_PP;
    gpio.GPIO_Speed = GPIO_Speed_50MHz;
	gpio.GPIO_PuPd  = GPIO_PuPd_UP;
	GPIO_Init(GPIOC, &gpio);
	GPIO_PinAFConfig(GPIOC, GPIO_PinSource2, GPIO_AF_SPI2);
	
    gpio.GPIO_Pin = GPIO_Pin_13; // SPI2_SCK
    gpio.GPIO_Mode = GPIO_Mode_AF;
	gpio.GPIO_OType = GPIO_OType_PP;
    gpio.GPIO_Speed = GPIO_Speed_100MHz;
	gpio.GPIO_PuPd  = GPIO_PuPd_UP;
	GPIO_Init(GPIOB, &gpio);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource13, GPIO_AF_SPI2);
	
	// HSYNC captrue timer: Start counting at HSYNC and start pixel timer after at correct x-position
	gpio.GPIO_Pin = GPIO_Pin_3;
	gpio.GPIO_Speed = GPIO_Speed_100MHz;
	gpio.GPIO_Mode  = GPIO_Mode_AF;
	gpio.GPIO_OType = GPIO_OType_PP;
	//gpio.GPIO_PuPd  = GPIO_PuPd_UP;
	gpio.GPIO_PuPd  = GPIO_PuPd_NOPULL;
	GPIO_Init(GPIOB, &gpio);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_TIM2);
	
	TIM_TimeBaseStructInit(&tim);
    tim.TIM_Period = pios_video_type_cfg_act->dc * (pios_video_type_cfg_act->graphics_column_start + x_offset);
    tim.TIM_Prescaler = 0;
    tim.TIM_ClockDivision = 0;
    tim.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(HSYNC_CAPTURE_TIMER, &tim);
	TIM_SelectOnePulseMode(HSYNC_CAPTURE_TIMER, TIM_OPMode_Single);
	TIM_SelectSlaveMode(HSYNC_CAPTURE_TIMER, TIM_SlaveMode_Trigger);
	TIM_SelectInputTrigger(HSYNC_CAPTURE_TIMER, TIM_TS_TI2FP2);
	TIM_SelectMasterSlaveMode(HSYNC_CAPTURE_TIMER, TIM_MasterSlaveMode_Enable);
	TIM_SelectOutputTrigger(HSYNC_CAPTURE_TIMER, TIM_TRGOSource_Update);
	
	// Pixel timer: Outputs clock for SPI
	gpio.GPIO_Pin   = GPIO_Pin_4;
	gpio.GPIO_Speed = GPIO_Speed_100MHz;
	gpio.GPIO_Mode  = GPIO_Mode_AF;
	gpio.GPIO_OType = GPIO_OType_PP;
	gpio.GPIO_PuPd  = GPIO_PuPd_UP;
	GPIO_Init(GPIOB, &gpio);
	GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_TIM3);
	
//	TIM_TimeBaseStructInit(&tim);
//	tim.TIM_Period = 10;
//	tim.TIM_Prescaler = 1;
//	tim.TIM_ClockDivision = 0;
//	tim.TIM_CounterMode = TIM_CounterMode_Up;
//	TIM_TimeBaseInit(PIXEL_TIMER, &tim);
	
	TIM_OCStructInit( &timoc );
    timoc.TIM_OCMode       = TIM_OCMode_PWM1;
	timoc.TIM_OutputState  = TIM_OutputState_Enable;
	timoc.TIM_OutputNState = TIM_OutputNState_Disable;
	timoc.TIM_Pulse        = 1;
	timoc.TIM_OCPolarity   = TIM_OCPolarity_High;
	timoc.TIM_OCNPolarity  = TIM_OCPolarity_High;
	timoc.TIM_OCIdleState  = TIM_OCIdleState_Reset;
	timoc.TIM_OCNIdleState = TIM_OCNIdleState_Reset;
    TIM_OC1Init(PIXEL_TIMER, &timoc );
	TIM_OC1PreloadConfig(PIXEL_TIMER, TIM_OCPreload_Enable);
	TIM_SetCompare1(PIXEL_TIMER, pios_video_type_cfg_act->dc);
	TIM_SetAutoreload(PIXEL_TIMER, pios_video_type_cfg_act->period);
	TIM_ARRPreloadConfig(PIXEL_TIMER, ENABLE);
	TIM_CtrlPWMOutputs(PIXEL_TIMER, ENABLE);
	TIM_SelectInputTrigger(PIXEL_TIMER, TIM_TS_ITR1);
	
	// Line counter: Counts number of HSYNCS (from hsync_capture) and triggers output of first visible line
	TIM_TimeBaseStructInit(&tim);
	tim.TIM_Period = 0xffff;
	tim.TIM_Prescaler = 0;
	tim.TIM_ClockDivision = 0;
	tim.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(LINE_COUNTER_TIMER, &tim);
	
	/* Enable the TIM4 gloabal Interrupt */
	nvic.NVIC_IRQChannel = TIM4_IRQn;
	nvic.NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_HIGHEST;
	nvic.NVIC_IRQChannelSubPriority = 1;
	nvic.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&nvic);
	TIM_SelectInputTrigger(LINE_COUNTER_TIMER, TIM_TS_ITR1);
	TIM_SelectSlaveMode(LINE_COUNTER_TIMER, TIM_SlaveMode_External1);
	TIM_SelectOnePulseMode(LINE_COUNTER_TIMER, TIM_OPMode_Single);
	TIM_ITConfig(LINE_COUNTER_TIMER, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3 | TIM_IT_CC4 | TIM_IT_COM | TIM_IT_Trigger | TIM_IT_Break, DISABLE);
	TIM_Cmd(LINE_COUNTER_TIMER, DISABLE);
	
    // init OSD mask SPI
    SPI_StructInit(&spi);
    spi.SPI_Mode              = SPI_Mode_Slave;
	spi.SPI_Direction         = SPI_Direction_1Line_Tx;
	spi.SPI_DataSize          = SPI_DataSize_8b;
	spi.SPI_NSS               = SPI_NSS_Soft;
	spi.SPI_FirstBit          = SPI_FirstBit_MSB;
	spi.SPI_CRCPolynomial     = 7;
	spi.SPI_CPOL              = SPI_CPOL_Low;
	spi.SPI_CPHA              = SPI_CPHA_2Edge;
	spi.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
    SPI_Init(OSD_MASK_SPI, &spi);
	
	SPI_StructInit(&spi);
    spi.SPI_Mode              = SPI_Mode_Slave;
	spi.SPI_Direction         = SPI_Direction_1Line_Tx;
	spi.SPI_DataSize          = SPI_DataSize_8b;
	spi.SPI_NSS               = SPI_NSS_Soft;
	spi.SPI_FirstBit          = SPI_FirstBit_MSB;
	spi.SPI_CRCPolynomial     = 7;
	spi.SPI_CPOL              = SPI_CPOL_Low;
	spi.SPI_CPHA              = SPI_CPHA_2Edge;
	spi.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2;
	SPI_Init(OSD_LEVEL_SPI, &spi);

    // Configure DMA for SPI - MASK_DMA DMA1_Channel3, LEVEL_DMA DMA1_Channel5
    DMA_StructInit(&dma);
	dma.DMA_Channel 		   = DMA_Channel_3;
    dma.DMA_PeripheralBaseAddr = (uint32_t)&(SPI1->DR);
	dma.DMA_DIR                = DMA_DIR_MemoryToPeripheral;
	dma.DMA_BufferSize         = BUFFER_WIDTH;
	dma.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;
	dma.DMA_MemoryInc          = DMA_MemoryInc_Enable;
	dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	dma.DMA_MemoryDataSize     = DMA_MemoryDataSize_Word;
	dma.DMA_Mode               = DMA_Mode_Normal;
	dma.DMA_Priority           = DMA_Priority_VeryHigh;
	dma.DMA_FIFOMode           = DMA_FIFOMode_Enable;
	dma.DMA_FIFOThreshold      = DMA_FIFOThreshold_Full;
	dma.DMA_MemoryBurst        = DMA_MemoryBurst_INC4;
	dma.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single;
    DMA_Init(OSD_MASK_DMA, &dma);
	
	dma.DMA_Channel 		   = DMA_Channel_0;
	dma.DMA_PeripheralBaseAddr = (uint32_t)&(SPI2->DR);
	dma.DMA_DIR                = DMA_DIR_MemoryToPeripheral;
	dma.DMA_BufferSize         = BUFFER_WIDTH;
	dma.DMA_PeripheralInc      = DMA_PeripheralInc_Disable;
	dma.DMA_MemoryInc          = DMA_MemoryInc_Enable;
	dma.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
	dma.DMA_MemoryDataSize     = DMA_MemoryDataSize_Word;
	dma.DMA_Mode               = DMA_Mode_Normal;
	dma.DMA_Priority           = DMA_Priority_VeryHigh;
	dma.DMA_FIFOMode           = DMA_FIFOMode_Enable;
	dma.DMA_FIFOThreshold      = DMA_FIFOThreshold_Full;
	dma.DMA_MemoryBurst        = DMA_MemoryBurst_INC4;
	dma.DMA_PeripheralBurst    = DMA_PeripheralBurst_Single;
	DMA_Init(OSD_LEVEL_DMA, &dma);
	
	/* Trigger interrupt when transfer complete */
	DMA_ITConfig(OSD_MASK_DMA, DMA_IT_TC, ENABLE);
	DMA_ITConfig(OSD_LEVEL_DMA, DMA_IT_TC, ENABLE);
	
	/* Configure and clear buffers */
	draw_buffer_level = buffer0_level;
	draw_buffer_mask  = buffer0_mask;
	disp_buffer_level = buffer1_level;
	disp_buffer_mask  = buffer1_mask;
	memset(disp_buffer_mask, 0, BUFFER_HEIGHT * BUFFER_WIDTH);
	memset(disp_buffer_level, 0, BUFFER_HEIGHT * BUFFER_WIDTH);
	memset(draw_buffer_mask, 0, BUFFER_HEIGHT * BUFFER_WIDTH);
	memset(draw_buffer_level, 0, BUFFER_HEIGHT * BUFFER_WIDTH);
	
    /* Configure DMA interrupt */
	nvic.NVIC_IRQChannel = OSD_MASK_DMA_IRQ;
  	nvic.NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_HIGHEST;
  	nvic.NVIC_IRQChannelSubPriority = 0;
  	nvic.NVIC_IRQChannelCmd = ENABLE;
  	NVIC_Init(&nvic); 
	nvic.NVIC_IRQChannel = OSD_LEVEL_DMA_IRQ;
  	nvic.NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_HIGHEST;
  	nvic.NVIC_IRQChannelSubPriority = 0;
  	nvic.NVIC_IRQChannelCmd = ENABLE;
  	NVIC_Init(&nvic); 
	
	/* Enable SPI interrupts to DMA */
	SPI_I2S_DMACmd(OSD_MASK_SPI, SPI_I2S_DMAReq_Tx, ENABLE);
	SPI_I2S_DMACmd(OSD_LEVEL_SPI, SPI_I2S_DMAReq_Tx, ENABLE);
	
	// init and enable interrupts for vsync
	gpio.GPIO_Pin = GPIO_PinSource1;
    gpio.GPIO_Speed = GPIO_Speed_100MHz;
	gpio.GPIO_Mode  = GPIO_Mode_IN;
	gpio.GPIO_OType = GPIO_OType_OD;
	gpio.GPIO_PuPd  = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOB, &gpio);
	
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource1);
	
	EXTI_InitStructure.EXTI_Line=EXTI_Line1;
  	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;	
  	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  	EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  	EXTI_Init(&EXTI_InitStructure);
	
	nvic.NVIC_IRQChannel = EXTI1_IRQn;
  	nvic.NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_HIGHEST;
  	nvic.NVIC_IRQChannelSubPriority = 0;
  	nvic.NVIC_IRQChannelCmd = ENABLE;
  	NVIC_Init(&nvic); 
	
	// Enable hsync interrupts
	TIM_ITConfig(LINE_COUNTER_TIMER, TIM_IT_Update, ENABLE);
	
	// Enable the capture timer
	TIM_Cmd(HSYNC_CAPTURE_TIMER, ENABLE);
}
示例#9
0
/*************************************************************
 * TIM4 Initialization
**************************************************************/
void TIM4_Init()
{
	#if (STRCMP($tim4IntEn$, 0) == 0)
    NVIC_InitTypeDef NVIC_InitStructure;
	#endif

	#if (STRCMP($ch1_0Pin$, DISABLE) == 0 || STRCMP($ch1_1Pin$, DISABLE) == 0 || \
	STRCMP($ch2_0Pin$, DISABLE) == 0 || STRCMP($ch2_1Pin$, DISABLE) == 0 || \
    STRCMP($ch3_0Pin$, DISABLE) == 0 || STRCMP($ch3_1Pin$, DISABLE) == 0 || \
    STRCMP($ch4_0Pin$, DISABLE) == 0 || STRCMP($ch4_1Pin$, DISABLE) == 0)
    GPIO_InitTypeDef GPIO_InitStructure;
	#endif	

    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	
    #if ( (STRCMP($ch1En$, DISABLE) == 0 && STRCMP($ch1sel$, CAPTURE_ENABLE) == 0) || \
	(STRCMP($ch2En$, DISABLE) == 0 && STRCMP($ch2sel$, CAPTURE_ENABLE) == 0) || \
	(STRCMP($ch3En$, DISABLE) == 0 && STRCMP($ch3sel$, CAPTURE_ENABLE) == 0) || \
	(STRCMP($ch4En$, DISABLE) == 0 && STRCMP($ch4sel$, CAPTURE_ENABLE) == 0) )
    TIM_OCInitTypeDef  TIM_OCInitStructure;
	#endif

    #if ( (STRCMP($ch1En$, DISABLE) == 0 && STRCMP($ch1sel$, COMPARE_ENABLE) == 0) || \
	(STRCMP($ch2En$, DISABLE) == 0 && STRCMP($ch2sel$, COMPARE_ENABLE) == 0) || \
	(STRCMP($ch3En$, DISABLE) == 0 && STRCMP($ch3sel$, COMPARE_ENABLE) == 0) || \
	(STRCMP($ch4En$, DISABLE) == 0 && STRCMP($ch4sel$, COMPARE_ENABLE) == 0) )	
    TIM_ICInitTypeDef  TIM_ICInitStructure;
	#endif
	
	//PUT_A_NEW_LINE_HERE
    //
    // Enable TIM4 clock
    //	
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);

	#if (STRCMP($tim4PinRemap$, DEFAULT) == 0 )
    //PUT_A_NEW_LINE_HERE
    //
    // Enable TIM4's GPIOD, GPIOE, AFIO clock
    //	
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);
	#else
    //PUT_A_NEW_LINE_HERE
    //
    // Enable TIM4's GPIOB , GPIOE, AFIO clock
    //	
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO, ENABLE);	
	#endif

	#if (STRCMP($tim4PinRemap$, DEFAULT) == 1)
	#if (STRCMP($ch1_0Pin$, DISABLE) == 0 || STRCMP($ch2_0Pin$, DISABLE) == 0 || STRCMP($ch3_0Pin$, DISABLE) == 0 || STRCMP($ch4_0Pin$, DISABLE) == 0)
    //   
    // Configure TIM4 pins: ETR, CH1, CH2, CH3, CH4, when Pin Remap is Default
    //
	#endif
	#if (STRCMP($etrPin$, DISABLE) == 0)
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOE, &GPIO_InitStructure);
	#endif
	#if (STRCMP($ch1_0Pin$, DISABLE) == 0)
    //PUT_A_NEW_LINE_HERE
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6;	
    #if (STRCMP($ch1sel$, CAPTURE_ENABLE) == 1)
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    #else
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    #endif
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
	#endif
	#if (STRCMP($ch2_0Pin$, DISABLE) == 0)
    //PUT_A_NEW_LINE_HERE
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7;	
    #if (STRCMP($ch2sel$, CAPTURE_ENABLE) == 1)
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    #else
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    #endif
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
	#endif
	#if (STRCMP($ch3_0Pin$, DISABLE) == 0)
    //PUT_A_NEW_LINE_HERE
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_8;
    #if (STRCMP($ch3sel$, CAPTURE_ENABLE) == 1)
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    #else
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    #endif
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
	#endif
	#if (STRCMP($ch4_0Pin$, DISABLE) == 0)
    //PUT_A_NEW_LINE_HERE
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_9;
    #if (STRCMP($ch4sel$, CAPTURE_ENABLE) == 1)
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    #else
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    #endif
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);
	#endif
	#endif
	
	#if (STRCMP($tim4PinRemap$, GPIO_Remap_TIM4) == 1)
    //   
    // Configure TIM4 pins: ETR, CH1, CH2, CH3, CH4, when Pin Remap is PartialRemap1
    // 
    GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE);
	#if (STRCMP($etrPin$, DISABLE) == 0)
    //PUT_A_NEW_LINE_HERE
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOE, &GPIO_InitStructure);
	#endif
	#if (STRCMP($ch1_1Pin$, DISABLE) == 0)
    //PUT_A_NEW_LINE_HERE
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_12;	
    #if (STRCMP($ch1sel$, CAPTURE_ENABLE) == 1)
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    #else
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    #endif
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
	#endif
	#if (STRCMP($ch2_1Pin$, DISABLE) == 0)
    //PUT_A_NEW_LINE_HERE
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_13;
    #if (STRCMP($ch2sel$, CAPTURE_ENABLE) == 1)
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    #else
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    #endif
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
	#endif
	#if (STRCMP($ch3_0Pin$, DISABLE) == 0)
    //PUT_A_NEW_LINE_HERE
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_14;
    #if (STRCMP($ch3sel$, CAPTURE_ENABLE) == 1)
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    #else
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    #endif
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
	#endif
	#if (STRCMP($ch4_0Pin$, DISABLE) == 0)
    //PUT_A_NEW_LINE_HERE
    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_15;
    #if (STRCMP($ch4sel$, CAPTURE_ENABLE) == 1)
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    #else
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    #endif
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOD, &GPIO_InitStructure);
	#endif
	#endif
	
	//PUT_A_NEW_LINE_HERE
    //
    // Initializes the TIM4 Time Base Unit
    //	
    TIM_TimeBaseStructure.TIM_Period = $tim4Period$;
    TIM_TimeBaseStructure.TIM_Prescaler = $tim4Psclr$;
    TIM_TimeBaseStructure.TIM_ClockDivision = $tim4ClkDiv$;
    TIM_TimeBaseStructure.TIM_CounterMode = $tim4CntMode$;
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
	
    #if (STRCMP($tim4ARRMode$, DISABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Enables Preload register on ARR
    //	
    TIM_ARRPreloadConfig(TIM3, ENABLE);
	#endif	

    #if (STRCMP($ch1En$, DISABLE) == 0)
	
    #if (STRCMP($ch1sel$, CAPTURE_ENABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Initializes Output Compare Channel1 of the TIM4 
    //	
    TIM_OCInitStructure.TIM_OCMode = $oc1Mode$;
    TIM_OCInitStructure.TIM_OutputState = $oc1State$;
    TIM_OCInitStructure.TIM_Pulse = $oc1Pluse$;
    TIM_OCInitStructure.TIM_OCPolarity = $oc1Polar$;
    TIM_OC1Init(TIM4, &TIM_OCInitStructure);

    #if (STRCMP($oc1Preload$, DISABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Enables Preload register on CCR1
    //	
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
	#endif

    #if (STRCMP($oc1Preload$, DISABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Clears or safeguards the OCREF1 signal on an external event
    //	
	TIM_ClearOC1Ref(TIM4, TIM_OCClear_Enable);  
	#endif

    #if ( STRCMP($oc1FastEn$, DISABLE) == 0 && (STRCMP($oc1Mode$, TIM_OCMode_PWM1) == 1 || STRCMP($oc1Mode$, TIM_OCMode_PWM2) == 1) )
	//PUT_A_NEW_LINE_HERE
    //
    // Configures the TIM4 Output Compare Channel1 Fast feature
    //	
	TIM_OC1FastConfig(TIM4, TIM_OCClear_Enable);  
	#endif	
	#endif	

    #if (STRCMP($ch1sel$, COMPARE_ENABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Initializes Input Capture Channel1 of the TIM4 
    //	
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;                   
    TIM_ICInitStructure.TIM_ICPolarity = $ic1Polar$;     
    TIM_ICInitStructure.TIM_ICSelection = $ic1Sel$;
    TIM_ICInitStructure.TIM_ICPrescaler = $ic1Psc$;
    TIM_ICInitStructure.TIM_ICFilter = $ic1Filter$; 
    TIM_ICInit(TIM4, &TIM_ICInitStructure);
	#endif
    #endif
	
    #if (STRCMP($ch2En$, DISABLE) == 0)
	
    #if (STRCMP($ch2sel$, CAPTURE_ENABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Initializes Output Compare Channel2 of the TIM4 
    //	
    TIM_OCInitStructure.TIM_OCMode = $oc2Mode$;
    TIM_OCInitStructure.TIM_OutputState = $oc2State$;
    TIM_OCInitStructure.TIM_Pulse = $oc2Pluse$;
    TIM_OCInitStructure.TIM_OCPolarity = $oc2Polar$;
    TIM_OC2Init(TIM4, &TIM_OCInitStructure);

    #if (STRCMP($oc2Preload$, DISABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Enables Preload register on CCR2
    //	
    TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
	#endif

    #if (STRCMP($oc2Preload$, DISABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Clears or safeguards the OCREF2 signal on an external event
    //	
	TIM_ClearOC2Ref(TIM4, TIM_OCClear_Enable);  
	#endif

    #if ( STRCMP($oc2FastEn$, DISABLE) == 0 && (STRCMP($oc2Mode$, TIM_OCMode_PWM1) == 1 || STRCMP($oc2Mode$, TIM_OCMode_PWM2) == 1) )
	//PUT_A_NEW_LINE_HERE
    //
    // Configures the TIM4 Output Compare Channel2 Fast feature
    //	
	TIM_OC2FastConfig(TIM4, TIM_OCClear_Enable);  
	#endif	
	#endif	

    #if (STRCMP($ch2sel$, COMPARE_ENABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Initializes Input Capture Channel2 of the TIM4
    //	
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;                   
    TIM_ICInitStructure.TIM_ICPolarity = $ic2Polar$;     
    TIM_ICInitStructure.TIM_ICSelection = $ic2Sel$;
    TIM_ICInitStructure.TIM_ICPrescaler = $ic2Psc$;
    TIM_ICInitStructure.TIM_ICFilter = $ic2Filter$; 
    TIM_ICInit(TIM4, &TIM_ICInitStructure);
	#endif
    #endif


    #if (STRCMP($ch3En$, DISABLE) == 0)
	
    #if (STRCMP($ch3sel$, CAPTURE_ENABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Initializes Output Compare Channel3 of the TIM4 
    //	
    TIM_OCInitStructure.TIM_OCMode = $oc3Mode$;
    TIM_OCInitStructure.TIM_OutputState = $oc3State$;
    TIM_OCInitStructure.TIM_Pulse = $oc3Pluse$;
    TIM_OCInitStructure.TIM_OCPolarity = $oc3Polar$;
    TIM_OC3Init(TIM4, &TIM_OCInitStructure);

    #if (STRCMP($oc3Preload$, DISABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Enables Preload register on CCR3
    //	
    TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
	#endif

    #if (STRCMP($oc3Preload$, DISABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Clears or safeguards the OCREF3 signal on an external event
    //	
	TIM_ClearOC3Ref(TIM4, TIM_OCClear_Enable);  
	#endif

    #if ( STRCMP($oc3FastEn$, DISABLE) == 0 && (STRCMP($oc3Mode$, TIM_OCMode_PWM1) == 1 || STRCMP($oc3Mode$, TIM_OCMode_PWM2) == 1) )
	//PUT_A_NEW_LINE_HERE
    //
    // Configures the TIM4 Output Compare Channel3 Fast feature
    //	
	TIM_OC3FastConfig(TIM4, TIM_OCClear_Enable);  
	#endif	
	#endif	

    #if (STRCMP($ch3sel$, COMPARE_ENABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Initializes Input Capture Channel3 of the TIM4
    //	
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_3;                   
    TIM_ICInitStructure.TIM_ICPolarity = $ic3Polar$;     
    TIM_ICInitStructure.TIM_ICSelection = $ic3Sel$;
    TIM_ICInitStructure.TIM_ICPrescaler = $ic3Psc$;
    TIM_ICInitStructure.TIM_ICFilter = $ic3Filter$; 
    TIM_ICInit(TIM4, &TIM_ICInitStructure);
	#endif
    #endif


    #if (STRCMP($ch4En$, DISABLE) == 0)
	
    #if (STRCMP($ch4sel$, CAPTURE_ENABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Initializes Output Compare Channel4 of the TIM4 
    //	
    TIM_OCInitStructure.TIM_OCMode = $oc4Mode$;
    TIM_OCInitStructure.TIM_OutputState = $oc4State$;
    TIM_OCInitStructure.TIM_Pulse = $oc4Pluse$;
    TIM_OCInitStructure.TIM_OCPolarity = $oc4Polar$;
    TIM_OC4Init(TIM4, &TIM_OCInitStructure);

    #if (STRCMP($oc4Preload$, DISABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Enables Preload register on CCR4
    //	
    TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
	#endif

    #if (STRCMP($oc4Preload$, DISABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Clears or safeguards the OCREF4 signal on an external event
    //	
	TIM_ClearOC4Ref(TIM4, TIM_OCClear_Enable);  
	#endif

    #if ( STRCMP($oc4FastEn$, DISABLE) == 0 && (STRCMP($oc4Mode$, TIM_OCMode_PWM1) == 1 || STRCMP($oc4Mode$, TIM_OCMode_PWM2) == 1) )
	//PUT_A_NEW_LINE_HERE
    //
    // Configures the TIM4 Output Compare Channel4 Fast feature
    //	
	TIM_OC4FastConfig(TIM4, TIM_OCClear_Enable);  
	#endif	
	#endif	

    #if (STRCMP($ch4sel$, COMPARE_ENABLE) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Initializes Input Capture Channel4 of the TIM4
    //	
    TIM_ICInitStructure.TIM_Channel = TIM_Channel_4;                   
    TIM_ICInitStructure.TIM_ICPolarity = $ic4Polar$;     
    TIM_ICInitStructure.TIM_ICSelection = $ic4Sel$;
    TIM_ICInitStructure.TIM_ICPrescaler = $ic4Psc$;
    TIM_ICInitStructure.TIM_ICFilter = $ic4Filter$; 
    TIM_ICInit(TIM4, &TIM_ICInitStructure);
	#endif
    #endif

    //PUT_A_NEW_LINE_HERE
    //
    // Enables TIM4 peripheral
    //	
    TIM_Cmd(TIM4, ENABLE);
	
    #if (STRCMP($masterEn$, DISABLE) == 0 && STRCMP($masterTRGOSrc$, None) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Selects the TIM4 Trigger Output Mode
    //	
    TIM_SelectOutputTrigger(TIM4, $masterTRGOSrc$);
	#endif	

    #if (STRCMP($slaveEn$, DISABLE) == 0 )
	//PUT_A_NEW_LINE_HERE
    //
    // Sets the TIM4 Master/Slave Mode
    //
    TIM_SelectMasterSlaveMode(TIM4, TIM_MasterSlaveMode_Enable);	
	#endif
	
    #if (STRCMP($slaveEn$, DISABLE) == 0 && STRCMP($slaveModeSet$, Disable(Internal Clock)) == 0)
	//PUT_A_NEW_LINE_HERE
    //
    // Selects the TIM4 Slave Mode
    //	
    TIM_SelectSlaveMode(TIM4, $slaveModeSet$);
	#endif
	
    #if (STRCMP($onePulseEn$, TIM_OPMode_Repetitive) == 0 )
	//PUT_A_NEW_LINE_HERE
    //
    // Selects the TIM4's One Pulse Mode
    //
    TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Single);	
	#endif
	
    #if (STRCMP($hallSensorEn$, DISABLE) == 0 )
	//PUT_A_NEW_LINE_HERE
    //
    // Enables the TIM4's Hall sensor interface
    //
    TIM_SelectHallSensor(TIM4, ENABLE);	
	#endif
	
    #if (STRCMP($encoderEn$, DISABLE) == 0 )
	//PUT_A_NEW_LINE_HERE
    //
    // Configures the TIM4 Encoder Interface
    //
    TIM_EncoderInterfaceConfig(TIM4, $extiCh1OutputPin$,
                                $EncodeIC1Polar$, $EncodeIC2Polar$);
	#endif
		
	
    #if (STRCMP($interClkEn$, ENABLE) == 0 )

    #if (STRCMP($interTrigr$, DISABLE) == 0 )
    //PUT_A_NEW_LINE_HERE
    //
    // Configures the TIM4 Internal Trigger as External Clock
    //
    TIM_ITRxExternalClockConfig(TIM4, $interTrigr$);
	#endif

    #if (STRCMP($timxTrigr$, DISABLE) == 0 )
    //PUT_A_NEW_LINE_HERE
    //
    // Configures the TIM4 Trigger as External Clock
    //
    TIM_TIxExternalClockConfig(TIM4, $timxTrigr$, TIM_ICPolarity_Rising, 0x0);
	#endif
	
    #if (STRCMP($extTrigr$, DISABLE) == 0 )
    #if (STRCMP($extTrigr$, ExternalTriggerMode1) == 1 )
    //PUT_A_NEW_LINE_HERE
    //
    // Configures the External clock Mode1
    //
    TIM_ETRClockMode1Config(TIM4, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_Inverted, 0x0);
	#endif
    #if (STRCMP($extTrigr$, ExternalTriggerMode2) == 1 )
    //PUT_A_NEW_LINE_HERE
    //
    // Configures the External clock Mode2
    //
    TIM_ETRClockMode2Config(TIM4, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_Inverted, 0x0);
	#endif
	#endif
	#endif
	
    #if (STRCMP($evtUpdate$, DISABLE) == 0 )
    //PUT_A_NEW_LINE_HERE
    //
    // Enables the TIM4 Update event
    //
    TIM_UpdateRequestConfig(TIM4, $updateRqstSrc$);
    TIM_UpdateDisableConfig(TIM4, DISABLE);
	#endif

    #if (STRCMP($tim4IntEn$, 0) == 0 )
    //PUT_A_NEW_LINE_HERE
    //
    // Enables TIM4 interrupts
    //
    TIM_ITConfig(TIM4, $tim4IntEn$, ENABLE);
    NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
	#endif
	
    #if (STRCMP($tim4dmaEn$, DISABLE) == 0 )
    //PUT_A_NEW_LINE_HERE
    //
    // Enables the TIM4's DMA Requests
    //
    TIM_DMAConfig(TIM4, $dmaBaseAddr$, $dmaBurstLen$);
    #if (STRCMP($tim4dmaRqstSrc$, 0) == 0 )
    TIM_DMACmd(TIM4, $tim4dmaRqstSrc$, ENABLE);
	#endif
    TIM_SelectCCDMA(TIM4, ENABLE);	
	#endif
}
示例#10
0
/**
  * @brief   Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* System Clocks Configuration */
  RCC_Configuration();

  /* Configure the GPIO ports */
  GPIO_Configuration();

  /* TIM4 configuration: One Pulse mode ------------------------
     The external signal is connected to TIM4_CH2 pin (PA.01), 
     The Rising edge is used as active edge,
     The One Pulse signal is output on TIM4_CH1 pin (PA.00)
     The TIM_Pulse defines the delay value 
     The (TIM_Period -  TIM_Pulse) defines the One Pulse value.
     The TIM4CLK is fixed to 72 MHz, the Prescaler is 1, so the 
     TIM4 counter clock is 36 MHz. 
     The Autoreload value is 65535 (TIM4->ARR), so the maximum 
     frequency value to trigger the TIM4 input is 500 Hz.
     The TIM_Pulse defines the delay value, the delay value is fixed 
     to 455.08 us:
     delay =  CCR1/TIM4 counter clock = 455.08 us. 
     The (TIM_Period - TIM_Pulse) defines the One Pulse value, 
     the pulse value is fixed to 1.365ms:
     One Pulse value = (TIM_Period - TIM_Pulse)/TIM4 counter clock = 1.365 ms.
  ------------------------------------------------------------ */

  /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 65535;
  TIM_TimeBaseStructure.TIM_Prescaler = 1;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

  /* TIM4 PWM2 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = 16383;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OC1Init(TIM4, &TIM_OCInitStructure);

  /* TIM4 configuration in Input Capture Mode */

  TIM_ICStructInit(&TIM_ICInitStructure);

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStructure.TIM_ICFilter = 0;

  TIM_ICInit(TIM4, &TIM_ICInitStructure);

  /* One Pulse Mode selection */
  TIM_SelectOnePulseMode(TIM4, TIM_OPMode_Single);

  /* Input Trigger selection */
  TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2);

  /* Slave Mode selection: Trigger Mode */
  TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Trigger);

  while (1)
  {}
}
示例#11
0
/**
  * @brief  pinConfig Arduino pin configuration
  * @param  INPUT or OUTPUT and variations
  * @retval None
  */
static void pinConfig(uint16_t pin, uint16_t mode){
  uint16_t L_oldType = ArduinoPort[pin].PinMode; //Does the pin already configured
  uint8_t L_setExtIt = 0;    //Do we have to set ExtIT
  uint8_t L_resetExtIt = 0;  //Do we have to reset ExtIT
  uint8_t L_setTimer = 0;    //Do we have to set Timer for PWM mode
  uint8_t L_resetTimer = 0;  //Do we have to reset Timer cause PWM mode no more used
  
  if(L_oldType == NOT_CONFIGURED) {
    //Enable GPIOx clock cause Pin has not already been configured
    if(ArduinoPort[pin].GPIOx == GPIOA ) {
      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);      
    } else if(ArduinoPort[pin].GPIOx == GPIOB) {
      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
    } else if(ArduinoPort[pin].GPIOx == GPIOC) {
      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
    } else if(ArduinoPort[pin].GPIOx == GPIOF) {
      RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOF, ENABLE);
    } else {
      //Error pin is not connected to STM32
      return;
    }
  }
  if((L_oldType & 0x0001) && !(mode & 0x0001) ) { 
    //Pin has already been configured as INPUT and OUTPUT config is requested
    if((pin == 2)||(pin == 3)) {
      L_resetExtIt = 1; //Reset External Interrupt settings. Cause when pin2/3 are input, ExtIT is automatically configured. 
    }
  }
  if(!(L_oldType & 0x0001) && (mode & 0x0001) && (L_oldType != NOT_CONFIGURED)) { 
    //Pin has already been configured as OUTPUT and INPUT config is requested
    if((pin == 3)||(pin == 5)||(pin == 6)||(pin == 9)||(pin == 10)||(pin == 11)) {
      L_resetTimer = 1; //Reset PWM timer settings.  This pins are dedicated to PWM when output. There are changed to INPUT.
    }    
  }

  //What is the STM32 pin to configure regarding Arduino pin num. Get it from mapping from Arduino toSTM32
  GPIO_InitStructure.GPIO_Pin = ArduinoPort[pin].PinNum;
  //Default GPIO speed clock
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  
  if(mode & 0x0001) { //Requested mode is : INPUT
    //If mode bit 0 = 1 (odd). Request INPUT mode for Pin
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
    switch(mode) {
      case INPUT: 
      case INPUT_NOPULL: 
        //Default GPIO configured as No pull
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
        L_setExtIt = 1; //ExtIT can be configured
        break;
      case INPUT_AF: 
        //Alternate function
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        break;
      case INPUT_AN: 
        //Analog function
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
        break;
      case INPUT_PU: 
        //GPIO configured as pull_up
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
        if(L_oldType == NOT_CONFIGURED)
          L_setExtIt = 1; //ExtIT can be configured if not already done
        break;
      case INPUT_PD: 
        //GPIO configured as pull_down
        GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
        if(L_oldType == NOT_CONFIGURED)
          L_setExtIt = 1; //ExtIT can be configured if not already done
        break;
      default:
        break;
    }
  } else { //Requested mode is : OUTPUT
    //Request OUTPUT mode for Pin
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    switch(mode) {
      case OUTPUT: 
      case OUTPUT_OD: 
        //Default GPIO configured as Open Drain
        GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;     
        break;
      case OUTPUT_AF: 
        //Alternate function
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
        L_setTimer = 1; //Timer for PWM can be configured
        break;
      case OUTPUT_AN: 
        //Analog function
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;          
        break;
      case OUTPUT_PP: 
        //GPIO configured as Output PushPull
        GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;             
        break;
      default:
        break;
    }
  }
  //GPIO Pin Configuration
  GPIO_Init(ArduinoPort[pin].GPIOx, &GPIO_InitStructure);    
  //NOW: Pin has been configured whith requested mode.
  ArduinoPort[pin].PinMode = mode;  //log it!
  
  //Test for Configuration of External interrupt and Timers. Set/Reset if needed!
  if(L_setExtIt) {
   //Configure External Interrupt for Pin2 or Pin3 only
   if((pin == 2)||(pin == 3)) {
    /* Enable SYSCFG clock */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
    if(pin == 2) {
    /* Connect EXTI12 Line to PB12 pin */
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource12);
    /* Configure EXTI12 line */
    EXTI_InitStructure.EXTI_Line = EXTI_Line12;
    } else {
    /* Connect EXTI11 Line to PB11 pin */
    SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB, EXTI_PinSource11);
    /* Configure EXTI11 line */
    EXTI_InitStructure.EXTI_Line = EXTI_Line11;
    }  
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;  
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;
    EXTI_Init(&EXTI_InitStructure);
    /* Enable and set EXTI11/EXTI12 Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel = EXTI4_15_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPriority = 0x00;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);     
   }
  }
  if(L_resetExtIt) {
   //Reset ExtIt for Pin2 or pin3 only
   if(pin == 2) {
    EXTI_InitStructure.EXTI_Line = EXTI_Line12;
    EXTI_InitStructure.EXTI_LineCmd = DISABLE;
    EXTI_Init(&EXTI_InitStructure);      
   }    
   if(pin == 3) {
    EXTI_InitStructure.EXTI_Line = EXTI_Line11;
    EXTI_InitStructure.EXTI_LineCmd = DISABLE;
    EXTI_Init(&EXTI_InitStructure);            
   }    
  }
  if(L_resetTimer) {
    //Reset Timer request
    switch(pin){
    // TIMx counter Disable 
    // TIMx Main Output Disable 
    // TIMx clock Disable
      case 3:
        TIM_Cmd(TIM2, DISABLE);
        TIM_CtrlPWMOutputs(TIM2, DISABLE);     
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, DISABLE);   
        break;
      case 5:
        TIM_Cmd(TIM17, DISABLE);
        TIM_CtrlPWMOutputs(TIM17, DISABLE);     
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17, DISABLE);   
        break;
      case 6:
        TIM_Cmd(TIM16, DISABLE);
        TIM_CtrlPWMOutputs(TIM16, DISABLE);     
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, DISABLE);   
        break;
      case 9:
        TIM_Cmd(TIM14, DISABLE);
        TIM_CtrlPWMOutputs(TIM14, DISABLE);     
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, DISABLE);   
        break;
      case 10:
        TIM_Cmd(TIM1, DISABLE);
        TIM_CtrlPWMOutputs(TIM1, DISABLE);     
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, DISABLE);   
        break;
      case 11:
        TIM_Cmd(TIM3, DISABLE);
        TIM_CtrlPWMOutputs(TIM3, DISABLE);     
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, DISABLE);   
        break;
      default:
        break;
    }
  }
  if(L_setTimer) {
   //Configure Timer for PWM
   TIM_TimeBaseStructure.TIM_Prescaler = 0;
   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
   TIM_TimeBaseStructure.TIM_Period = (SystemCoreClock / 1000 ) - 1; //Default frequency is 1KHz
   ArduinoPort[pin].pwmPeriod = (SystemCoreClock / 1000 ) - 1;
   TIM_TimeBaseStructure.TIM_ClockDivision = 0;
   TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

   TIM_OCInitStructure.TIM_Pulse = 0; //(uint16_t) (((uint32_t) 2 * (ArduinoPort[pin].pwmPeriod - 1)) / 10); //Default duty cycle at 0%
   TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
   TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
   TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
   TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
   TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
   TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
   TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
    
    switch(pin){
    // TIMx clock enable
    // TIMx Main Output Enable 
    // TIMx clock Enable
      case 3:
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);  
        //Connect TIM Channels to Port Alternate Function 
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_2);        
        TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
        TIM_OC4Init(TIM2, &TIM_OCInitStructure);
        //enable Frequ and pulse update
        TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
        TIM_SelectOnePulseMode(TIM2, TIM_OPMode_Repetitive); 
        TIM_Cmd(TIM2, ENABLE);
        TIM_CtrlPWMOutputs(TIM2, ENABLE);     
        break;
      case 5:
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM17, ENABLE);   
        //Connect TIM Channels to Port Alternate Function 
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_2);        
        TIM_TimeBaseInit(TIM17, &TIM_TimeBaseStructure);
        TIM_OC1Init(TIM17, &TIM_OCInitStructure);
        //enable Frequ and pulse update
        TIM_OC1PreloadConfig(TIM17, TIM_OCPreload_Enable);
        TIM_SelectOnePulseMode(TIM17, TIM_OPMode_Repetitive); 
        TIM_Cmd(TIM17, ENABLE);
        TIM_CtrlPWMOutputs(TIM17, ENABLE);     
        break;
      case 6:
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM16, ENABLE);   
        //Connect TIM Channels to Port Alternate Function 
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_2);        
        TIM_TimeBaseInit(TIM16, &TIM_TimeBaseStructure);
        TIM_OC1Init(TIM16, &TIM_OCInitStructure);
        //enable Frequ and pulse update
        TIM_OC1PreloadConfig(TIM16, TIM_OCPreload_Enable);
        TIM_SelectOnePulseMode(TIM16, TIM_OPMode_Repetitive); 
        TIM_Cmd(TIM16, ENABLE);
        TIM_CtrlPWMOutputs(TIM16, ENABLE);     
        break;
      case 9:
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);   
        //Connect TIM Channels to Port Alternate Function 
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource4, GPIO_AF_4);        
        TIM_TimeBaseInit(TIM14, &TIM_TimeBaseStructure);
        TIM_OC1Init(TIM14, &TIM_OCInitStructure);
        //enable Frequ and pulse update
        TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
        TIM_SelectOnePulseMode(TIM1, TIM_OPMode_Repetitive); 
        TIM_Cmd(TIM14, ENABLE);
        TIM_CtrlPWMOutputs(TIM14, ENABLE);     
        break;
      case 10:
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);   
        //Connect TIM Channels to Port Alternate Function 
        GPIO_PinAFConfig(GPIOA, GPIO_PinSource11, GPIO_AF_2);        
        TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
        TIM_OC4Init(TIM1, &TIM_OCInitStructure);
        //enable Frequ and pulse update
        TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
        TIM_SelectOnePulseMode(TIM1, TIM_OPMode_Repetitive); 
        TIM_Cmd(TIM1, ENABLE);
        TIM_CtrlPWMOutputs(TIM1, ENABLE);     
        break;
      case 11:
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);   
        //Connect TIM Channels to Port Alternate Function 
        GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_1);        
        TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
        TIM_OC2Init(TIM3, &TIM_OCInitStructure);
        //enable Frequ and pulse update
        TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
        TIM_SelectOnePulseMode(TIM3, TIM_OPMode_Repetitive); 
        TIM_Cmd(TIM3, ENABLE);
        TIM_CtrlPWMOutputs(TIM3, ENABLE);     
        break;
      default:
        break;
    } 
  }

}
示例#12
0
/******************************************************************************
 * TIM1 Initialization Code Template
******************************************************************************/
void TIM1_Init()
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
    
    #if (((STRCMP($ch1CCSel$, CH1_Cap_Func) == 1) && (STRCMP($ch1CCEnable$, ENABLE) == 1)) || \
	((STRCMP($ch2CCSel$, CH2_Cap_Func) == 1) && (STRCMP($ch2CCEnable$, ENABLE) == 1)) || \
	((STRCMP($ch3CCSel$, CH3_Cap_Func) == 1) && (STRCMP($ch3CCEnable$, ENABLE) == 1)) || \
	((STRCMP($ch4CCSel$, CH4_Cap_Func) == 1) && (STRCMP($ch4CCEnable$, ENABLE) == 1)))
    TIM_ICInitTypeDef TIM_ICInitStruct;
    #endif
    #if (((STRCMP($ch1CCSel$, CH1_Comp_Func) == 1) && (STRCMP($ch1CCEnable$, ENABLE) == 1)) || \
	((STRCMP($ch2CCSel$, CH2_Comp_Func) == 1) && (STRCMP($ch2CCEnable$, ENABLE) == 1)) || \
	((STRCMP($ch3CCSel$, CH3_Comp_Func) == 1) && (STRCMP($ch3CCEnable$, ENABLE) == 1)) || \ 
	((STRCMP($ch4CCSel$, CH4_Comp_Func) == 1) && (STRCMP($ch4CCEnable$, ENABLE) == 1)))
    TIM_OCInitTypeDef TIM_OCInitStruct;
    #endif
  
    #if(STRCMP($brkInt$ ,ENABLE) == 1 || STRCMP($upInt$ ,ENABLE) == 1 || STRCMP($tcInt$ ,0) == 0 || STRCMP($ccInt$ ,0) == 0) 
    NVIC_InitTypeDef NVIC_InitStructure;
	#endif
	
    //PUT_A_NEW_LINE_HERE
    //
    // Enable peripheral clock of TIM1
    //
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);  

    //PUT_A_NEW_LINE_HERE
    //
    // Time base configuration
    //
    TIM_TimeBaseStructInit(&TIM_TimeBaseInitStruct);
    TIM_TimeBaseInitStruct.TIM_Period = $counterPeriod$;
    TIM_TimeBaseInitStruct.TIM_Prescaler = $prescaler$;
    TIM_TimeBaseInitStruct.TIM_ClockDivision = $clockDivision$;
    TIM_TimeBaseInitStruct.TIM_CounterMode = $counterMode$;
    TIM_TimeBaseInitStruct.TIM_RepetitionCounter = $repetition$;
    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStruct);
    
    #if (STRCMP($ch1CCEnable$, ENABLE) == 1)
    //PUT_A_NEW_LINE_HERE
    #if (STRCMP($ch1CCSel$, CH1_Cap_Func) == 1)
    //
    // TIM1 Channel 1 Input Configuration
    //
    TIM_ICStructInit(&TIM_ICInitStruct);
    TIM_ICInitStruct.TIM_Channel = TIM_Channel_1;
    TIM_ICInitStruct.TIM_ICPolarity = $ch1ICPol$;
    TIM_ICInitStruct.TIM_ICSelection = $ch1ICSel$;
    TIM_ICInitStruct.TIM_ICPrescaler = $ch1ICPSC$;
    TIM_ICInitStruct.TIM_ICFilter = $ch1ICFilter$;
    TIM_ICInit(TIM1, &TIM_ICInitStruct);
    #endif
    
    #if (STRCMP($ch1CCSel$, CH1_Comp_Func) ==1)
    //
    // TIM1 Channel 1 Output configuration
    //
    TIM_OCStructInit(&TIM_OCInitStruct);
    TIM_OCInitStruct.TIM_OCMode = $ch1OutputMode$;
    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStruct.TIM_Pulse = $ch1PsValue$;
    TIM_OCInitStruct.TIM_OCPolarity = $ch1OCPol$;
    TIM_OCInitStruct.TIM_OCIdleState = $ch1IdleState$;
    TIM_OC1Init(TIM1, &TIM_OCInitStruct);
    TIM_ForcedOC1Config(TIM1, $ch1ForceAction$);
    #if (STRCMP($ch1CCRPreload$, ENABLE) == 1)
    TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
    #endif
    #if (STRCMP($ch1OREFClear$, ENABLE) == 1)
    TIM_ClearOC1Ref(TIM1, TIM_OCClear_Enable);
    #endif
    #endif
    
    #endif
        
    #if (STRCMP($ch2CCEnable$, ENABLE) == 1)
    //PUT_A_NEW_LINE_HERE
    #if (STRCMP($ch2CCSel$, CH2_Cap_Func) == 1)
    //
    // TIM1 Channel 2 Input Configuration
    //
    TIM_ICStructInit(&TIM_ICInitStruct);
    TIM_ICInitStruct.TIM_Channel = TIM_Channel_2;
    TIM_ICInitStruct.TIM_ICPolarity = $ch2ICPol$;
    TIM_ICInitStruct.TIM_ICSelection = $ch2ICSel$;
    TIM_ICInitStruct.TIM_ICPrescaler = $ch2ICPSC$;
    TIM_ICInitStruct.TIM_ICFilter = $ch2ICFilter$;
    TIM_ICInit(TIM1, &TIM_ICInitStruct);
    #endif
    
    #if (STRCMP($ch2CCSel$, CH2_Comp_Func) ==1)
    //
    // TIM1 Channel 2 Output configuration
    //
    TIM_OCStructInit(&TIM_OCInitStruct);
    TIM_OCInitStruct.TIM_OCMode = $ch2OutputMode$;
    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStruct.TIM_Pulse = $ch2PsValue$;
    TIM_OCInitStruct.TIM_OCPolarity = $ch2OCPol$;
    TIM_OCInitStruct.TIM_OCIdleState = $ch2IdleState$;
    TIM_OC2Init(TIM1, &TIM_OCInitStruct);
    TIM_ForcedOC2Config(TIM1, $ch2ForceAction$);
    #if (STRCMP($ch2CCRPreload$, ENABLE) == 1)
    TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
    #endif
    #if (STRCMP($ch2OREFClear$, ENABLE) == 1)
    TIM_ClearOC2Ref(TIM1, TIM_OCClear_Enable);
    #endif
    #endif
    
    #endif
        
    #if (STRCMP($ch3CCEnable$, ENABLE) == 1)
    //PUT_A_NEW_LINE_HERE
    #if (STRCMP($ch3CCSel$, CH3_Cap_Func) == 1)
    //
    // TIM1 Channel 3 Input Configuration
    //
    TIM_ICStructInit(&TIM_ICInitStruct);
    TIM_ICInitStruct.TIM_Channel = TIM_Channel_3;
    TIM_ICInitStruct.TIM_ICPolarity = $ch3ICPol$;
    TIM_ICInitStruct.TIM_ICSelection = $ch3ICSel$;
    TIM_ICInitStruct.TIM_ICPrescaler = $ch3ICPSC$;
    TIM_ICInitStruct.TIM_ICFilter = $ch3ICFilter$;
    TIM_ICInit(TIM1, &TIM_ICInitStruct);
    #endif
    
    #if (STRCMP($ch3CCSel$, CH3_Comp_Func) ==1)
    //
    // TIM1 Channel 3 Output configuration
    //
    TIM_OCStructInit(&TIM_OCInitStruct);
    TIM_OCInitStruct.TIM_OCMode = $ch3OutputMode$;
    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStruct.TIM_Pulse = $ch3PsValue$;
    TIM_OCInitStruct.TIM_OCPolarity = $ch3OCPol$;
    TIM_OCInitStruct.TIM_OCIdleState = $ch3IdleState$;
    TIM_OC3Init(TIM1, &TIM_OCInitStruct);
    TIM_ForcedOC3Config(TIM1, $ch3ForceAction$);
    #if (STRCMP($ch3CCRPreload$, ENABLE) == 1)
    TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
    #endif
    #if (STRCMP($ch3OREFClear$, ENABLE) == 1)
    TIM_ClearOC3Ref(TIM1, TIM_OCClear_Enable);
    #endif
    #endif
    
    #endif
        
    #if (STRCMP($ch4CCEnable$, ENABLE) == 1)
    //PUT_A_NEW_LINE_HERE
    #if (STRCMP($ch4CCSel$, CH4_Cap_Func) == 1)
    //
    // TIM1 Channel 4 Input Configuration
    //
    TIM_ICStructInit(&TIM_ICInitStruct);
    TIM_ICInitStruct.TIM_Channel = TIM_Channel_4;
    TIM_ICInitStruct.TIM_ICPolarity = $ch4ICPol$;
    TIM_ICInitStruct.TIM_ICSelection = $ch4ICSel$;
    TIM_ICInitStruct.TIM_ICPrescaler = $ch4ICPSC$;
    TIM_ICInitStruct.TIM_ICFilter = $ch4ICFilter$;
    TIM_ICInit(TIM1, &TIM_ICInitStruct);
    #endif
    
    #if (STRCMP($ch4CCSel$, CH4_Comp_Func) ==1)
    //
    // TIM1 Channel 4 Output configuration
    //
    TIM_OCStructInit(&TIM_OCInitStruct);
    TIM_OCInitStruct.TIM_OCMode = $ch4OutputMode$;
    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStruct.TIM_Pulse = $ch4PsValue$;
    TIM_OCInitStruct.TIM_OCPolarity = $ch4OCPol$;
    TIM_OCInitStruct.TIM_OCIdleState = $ch4IdleState$;
    TIM_OC4Init(TIM1, &TIM_OCInitStruct);
    TIM_ForcedOC4Config(TIM1, $ch4ForceAction$);
    #if (STRCMP($ch4CCRPreload$, ENABLE) == 1)
    TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
    #endif
    #if (STRCMP($ch4OREFClear$, ENABLE) == 1)
    TIM_ClearOC4Ref(TIM1, TIM_OCClear_Enable);
    #endif
    #endif
    
    #endif
    
    #if (STRCMP($upEVDis$, ENABLE) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    // Enable the Update event
    //
    TIM_UpdateDisableConfig(TIM1, DISABLE);
    TIM_UpdateRequestConfig(TIM1, $requestIntSrc$);
    #endif
    
    #if ((STRCMP($extTriEnable$, ENABLE) == 1) && (STRCMP($extClkEnable$, ENABLE) == 1))
	#if (STRCMP($extClkMode$, MODE1) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    // ETR clock Mode1 Config
    //
    TIM_ETRClockMode1Config(TIM1, $exTriPSCSet$, $exTriPolSet$, $exTriFilterSet$);
    #endif
    #if (STRCMP($extClkMode$, MODE2) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    // ETR clock Mode2 Config
    //
    TIM_ETRClockMode2Config(TIM1, $exTriPSCSet$, $exTriPolSet$, $exTriFilterSet$);
    #endif
	#endif
	
	#if ((STRCMP($intTriEnable$, ENABLE) == 1) && (STRCMP($extClkEnable$, ENABLE) == 1))
    //PUT_A_NEW_LINE_HERE
    //
    // ITR clock Config
    //
    TIM_ITRxExternalClockConfig(TIM1, $intTriSrc$);
	#endif
	
    #if (STRCMP($mmEnable$, ENABLE) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    // Master Mode selection
    //
    TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
    #endif    
    
    #if (STRCMP($smEnable$, ENABLE) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    // Slave Mode selection
    //
    TIM_SelectSlaveMode(TIM1, $smSel$);
    #endif
    
    #if (STRCMP($hsiEnable$, ENABLE) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    // Enable Hall Sensor Interface
    //
    TIM_SelectHallSensor(TIM1, ENABLE);
    #endif
    
    #if (STRCMP($spmEnable$, ENABLE) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    // Single Pulse Mode selection
    //
    TIM_SelectOnePulseMode(TIM1, $spmSet$);
	#endif
	
	#if (STRCMP($tomEnable$, ENABLE) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    // Trigger Output Mode selection
    //
    TIM_SelectOutputTrigger(TIM1, $tomSrc$);
	#endif
    
    #if (STRCMP($eimEnable$, ENABLE) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    // Encoder Interface Mode selection
    //
    TIM_EncoderInterfaceConfig(TIM1, $eimSet$,
                                $encodeIC1Polar$, $encodeIC2Polar$);
	#endif
	
    //PUT_A_NEW_LINE_HERE
    //
    // Enable TIM1 
    //
    TIM_Cmd(TIM1, ENABLE);

    
    #if(STRCMP($brkInt$, ENABLE) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    // Enable BRK Interrupt
    //
    TIM_ITConfig(TIM1, TIM_IT_Break, ENABLE);
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_BRK_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    #endif
	
	#if(STRCMP($upInt$, ENABLE) == 1)
    //PUT_A_NEW_LINE_HERE
    //
    // Enable UP Interrupt
    //
    TIM_ITConfig(TIM1, TIM_IT_Update, ENABLE);
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    #endif
	
	#if(STRCMP($tcInt$, 0) == 0)
    //PUT_A_NEW_LINE_HERE
    //
    // Enable TRG/COM Interrupt
    //
    TIM_ITConfig(TIM1, $TCInt$, ENABLE);
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_TRG_COM_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    #endif
	
	#if(STRCMP($ccInt$, 0) == 0)
    //PUT_A_NEW_LINE_HERE
    //
    // Enable CC Interrupt
    //
    TIM_ITConfig(TIM1, $CCInt$, ENABLE);
    NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
    #endif
}
示例#13
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  char test[25];
  unsigned int an1, an2, an3, an4, an5;
  float Ta=0;
  int i;
  long msum[4];
  float fm[4], T1, T2, T3, SP;
  unsigned int DSState = 0;
  unsigned long DSTimer=0, t0, t1, dt, HTimer, TTimer, t_sec, t_min;
  unsigned char data[2];
  unsigned char out=0;
  unsigned int decval;
  volatile float pp, pi, pd, f_error, error_old=0, pid_out, pid_out_i, pid_out_p, pid_out_d, error_i=0;
  u8 Send_Buffer[25];
  u8 tmp[4];
//char no_windup = 0;

  RCC_Configuration();
  NVIC_Configuration();

  /* SysTick end of count event each 1ms with input clock equal to 9MHz (HCLK/8, default) */
  SysTick_SetReload(9000);

  /* Enable SysTick interrupt */
  SysTick_ITConfig(ENABLE);

  /* Enable the SysTick Counter */
  SysTick_CounterCmd(SysTick_Counter_Enable);

  GPIO_Setup();

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE);

  /* SPI2 Config -------------------------------------------------------------*/
  SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
  SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
  SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
  SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
  SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
  SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
  SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
  SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
  SPI_InitStructure.SPI_CRCPolynomial = 7;
  SPI_Init(SPI2, &SPI_InitStructure);

  /* Enable SPI1 */
  SPI_CalculateCRC(SPI2, DISABLE);
  SPI_Cmd(SPI2, ENABLE);

  InitADC1();      // ADC1 Init

  OWInit(&OneWire, GPIOB, GPIO_Pin_8);

  /* Connect Key Button EXTI Line to Key Button GPIO Pin */
  //GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_Pin_7);

  /* Configure Key Button EXTI Line to generate an interrupt on falling edge */
  //EXTI_InitStructure.EXTI_Line = EXTI_Line7;
  //EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  //EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
  //EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  //EXTI_Init(&EXTI_InitStructure);

    /* Time base configuration */
  TIM_TimeBaseStructure.TIM_Period = 55000;
  TIM_TimeBaseStructure.TIM_Prescaler = 12;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  /* TIM2 PWM2 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  TIM_OCInitStructure.TIM_Channel = TIM_Channel_1;
  TIM_OCInitStructure.TIM_Pulse = 1;//20000;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

  TIM_OCInit(TIM3, &TIM_OCInitStructure);

  /* TIM2 configuration in Input Capture Mode */

  TIM_ICStructInit(&TIM_ICInitStructure);

  TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
  TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
  TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
  TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  TIM_ICInitStructure.TIM_ICFilter = 0;

  TIM_ICInit(TIM3, &TIM_ICInitStructure);

  /* One Pulse Mode selection */
  TIM_SelectOnePulseMode(TIM3, TIM_OPMode_Single);

  /* Input Trigger selection */
  TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2);

  /* Slave Mode selection: Trigger Mode */
  TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);

  ST7565_st7565_init();
  ST7565_st7565_command(CMD_SET_BIAS_9);
  ST7565_st7565_command(CMD_DISPLAY_ON);
  ST7565_st7565_command(CMD_SET_ALLPTS_NORMAL);
  ST7565_st7565_set_brightness(0x0C);

  OWSearch(&OneWire, addr);
  /*sprintf(test, "%02X %02X %02X %02X %02X %02X %02X %02X",
         addr[7],addr[6],
         addr[5],addr[4],
         addr[3],addr[2],
         addr[1],addr[0]);
  ST7565_drawstring(6, 6, test);
*/

  USB_Init();

  ST7565_display(); // show splashscreen
  t0 = millis();
  HTimer = millis();
  TTimer = millis();
  pp = 10;
  pi = 0;
  pd = 150;
  while(1)
  {
	msum[0] = 0;
	msum[1] = 0;
	msum[2] = 0;
	msum[3] = 0;
	for (i = 0; i < 1000; i++) {
      an1 = GetADC1Channel(ADC_Channel_1);
      an2 = GetADC1Channel(ADC_Channel_2);
      an3 = GetADC1Channel(ADC_Channel_3);
      an4 = GetADC1Channel(ADC_Channel_4);
      an5 = GetADC1Channel(ADC_Channel_5);
      msum[0] += an1;
      msum[1] += an3 - an2;
      msum[2] += an4 - an2;
      msum[3] += an5 - an2;
      //DelayuS(333);
	}
	SP = round((msum[0] / 1000.0) * (60.0 / 4096)) * 5;
	fm[1] = (msum[1] / 1000.0);
	fm[2] = (msum[2] / 1000.0) + 12;
	fm[3] = (msum[3] / 1000.0) - 7;

    T1 = (T1 + Ta + Dac2Dt(fm[1])) / 2;
    T2 = (T2 + Ta + Dac2Dt(fm[2])) / 2;
    T3 = (T3 + Ta + Dac2Dt(fm[3])) / 2;

    t1 = millis();
    dt = t1 - t0;
 	t0 = t1;
    if (millis() - HTimer > 1000) {
      f_error = SP - T2;
      //if (noerror_i += error;
      pid_out_p = pp * f_error;
      pid_out_i = pi * error_i;
      pid_out_d = pd * (f_error - error_old);
      pid_out = pid_out_p + pid_out_i + pid_out_d;
      error_old = f_error;
      //out = pid_out;
      if (pid_out > 99) {
    	out = 99;
      } else if (pid_out < 0) {
        out = 0;
      } else {
        out = round(pid_out);
      }
      TIM_SetCompare1(TIM3, 55000-PowerValues[out]);
	  HTimer += 1000;

    //error_old = 10;
    sprintf(test, "T1 : %5.1f E  %5.1f ", T1, f_error);
    ST7565_drawstring(6, 0, test);
	sprintf(test, "T2 : %5.1f ", T2);
    ST7565_drawstring(6, 1, test);
	sprintf(test, "T3 : %5.1f ", T3);
    ST7565_drawstring(6, 2, test);
	sprintf(test, "SP : %5.1f P %6.1f ", SP, pid_out_p);
    ST7565_drawstring(6, 3, test);
	sprintf(test, "Ta : %5.1f I %6.1f ", Ta, pid_out_i);
    ST7565_drawstring(6, 4, test);
    sprintf(test, "dt : %5lu D %6.1f ", dt, pid_out_d);
    ST7565_drawstring(6, 5, test);
 	sprintf(test, "out: %3u %%   %6.1f ", out, pid_out);
    ST7565_drawstring(6, 6, test);
    t_sec = (millis() - TTimer) / 1000;
    t_min = floor(t_sec / 60);
    t_sec %= 60;
 	Send_Buffer[0] = 0x07;
 	decval = round(T1 * 100);
 	memcpy(&Send_Buffer[1], &decval, 2);
 	decval = round(T2 * 100);
 	memcpy(&Send_Buffer[3], &decval, 2);
 	decval = round(T3 * 100);
 	memcpy(&Send_Buffer[5], &decval, 2);
 	decval = round(SP * 100);
 	memcpy(&Send_Buffer[7], &decval, 2);
 	memcpy(&Send_Buffer[9], &out, 1);

 	//sprintf(test, "%02X %02X %02X %02X ", Send_Buffer[1], Send_Buffer[2], Send_Buffer[3], Send_Buffer[4]);
    //ST7565_drawstring(6, 7, test);

    UserToPMABufferCopy(Send_Buffer, ENDP1_TXADDR, 9);
    SetEPTxCount(ENDP1, 9);
    SetEPTxValid(ENDP1);
    ST7565_display();
 	}
    if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0) {
      TTimer = millis();
    }

    //onewire
    switch (DSState){
    case 0:
      OWReset(&OneWire);
      OWWrite(&OneWire, 0xCC);         // skip ROM
      OWWrite(&OneWire, 0x44);         // start conversion
      DSTimer = millis();
      DSState++;
      break;
    case 1:
      if((millis() - DSTimer) >= 1000){
    	OWReset(&OneWire);
        OWSelect(&OneWire, addr);
        OWWrite(&OneWire, 0xBE);             // Read Scratchpad
        data[0] = OWRead(&OneWire);
        data[1] = OWRead(&OneWire);
        Ta = ((data[1] << 8) | data[0]) / 16.0;
        DSState = 0;
      }
      break;
    }
  }
}