Example #1
0
void pwmout_period_us(pwmout_t* obj, int us)
{
    TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);

    float dc = pwmout_read(obj);

    __HAL_TIM_DISABLE(&TimHandle);

    // Update the SystemCoreClock variable
    SystemCoreClockUpdate();

    TimHandle.Init.Period        = us - 1;
    TimHandle.Init.Prescaler     = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 µs tick
    TimHandle.Init.ClockDivision = 0;
    TimHandle.Init.CounterMode   = TIM_COUNTERMODE_UP;
    HAL_TIM_PWM_Init(&TimHandle);

    // Set duty cycle again
    pwmout_write(obj, dc);

    // Save for future use
    obj->period = us;

    __HAL_TIM_ENABLE(&TimHandle);
}
Example #2
0
void pwmout_period_us(pwmout_t* obj, int us)
{
    TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);

    float dc = pwmout_read(obj);

    __HAL_TIM_DISABLE(&TimHandle);

    TimHandle.Init.Period        = us - 1;
    TimHandle.Init.Prescaler     = (uint16_t)(SystemCoreClock / 1000000) - 1; // 1 us tick
    TimHandle.Init.ClockDivision = 0;
    TimHandle.Init.CounterMode   = TIM_COUNTERMODE_UP;

    if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) {
        error("Cannot initialize PWM");
    }

    // Set duty cycle again
    pwmout_write(obj, dc);

    // Save for future use
    obj->period = us;

    __HAL_TIM_ENABLE(&TimHandle);
}
Example #3
0
void
vMBPortTimersDisable(  )
{
    __HAL_TIM_DISABLE(&htim1);
#if MB_TIMER_DEBUG == 1
    PIO_Clear( &xTimerDebugPins[0] );
#endif   
}
// Clear timer counter to 0, and then start the timer
int timer_reset_n_go(hacs_timer_t tim)
{
  TIM_HandleTypeDef *htim = &tim_handles[tim];

  __HAL_TIM_DISABLE(htim);
  __HAL_TIM_SetCounter(htim, 0);
  __HAL_TIM_ENABLE(htim);
  return HACS_NO_ERROR;
}
Example #5
0
void
vMBPortTimersDisable(  )
{
   /* Disable any pending timers. */
			__HAL_TIM_DISABLE(&htim6);
			__HAL_TIM_SetCounter(&htim6,0);
			__HAL_TIM_DISABLE_IT(&htim6,TIM_IT_UPDATE);
			__HAL_TIM_CLEAR_IT(&htim6,TIM_IT_UPDATE);
}
Example #6
0
void
TCX_IRQHANDLER( void )
{
   //HAL_UART_Transmit(&huart1,  "T", 1, 0xFFFF);
    __HAL_TIM_DISABLE(&htim1);
    __HAL_TIM_CLEAR_IT(&htim1, TIM_IT_UPDATE);
  
#if MB_TIMER_DEBUG == 1
        PIO_Clear( &xTimerDebugPins[0] );
#endif
        ( void )pxMBPortCBTimerExpired(  );
}
Example #7
0
void pwmout_period_us(pwmout_t* obj, int us)
{
    TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);

    float dc = pwmout_read(obj);

    __HAL_TIM_DISABLE(&TimHandle);

    SystemCoreClockUpdate();

    /* To make it simple, we use to possible prescaler values which lead to:
     * pwm unit = 1us, period/pulse can be from 1us to 65535us
     * or
     * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec
     * Be careful that all the channels of a PWM shares the same prescaler
     */
    if (us >  0xFFFF) {
        obj->prescaler = 500;
    } else {
        obj->prescaler = 1;
    }
    TimHandle.Init.Prescaler     = ((SystemCoreClock / 1000000) * obj->prescaler) - 1;

    if (TimHandle.Init.Prescaler > 0xFFFF)
        error("PWM: out of range prescaler");

    TimHandle.Init.Period        = (us - 1) / obj->prescaler;
    if (TimHandle.Init.Period > 0xFFFF)
        error("PWM: out of range period");

    TimHandle.Init.ClockDivision = 0;
    TimHandle.Init.CounterMode   = TIM_COUNTERMODE_UP;

    if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) {
        error("Cannot initialize PWM\n");
    }

    // Save for future use
    obj->period = us;

    // Set duty cycle again
    pwmout_write(obj, dc);

    __HAL_TIM_ENABLE(&TimHandle);
}
Example #8
0
void pwmout_period_ns (pwmout_t* obj, int pres)
{
    TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
    int ns = 2;
    float dc = 0.5; // pwmout_read(obj);

    __HAL_TIM_DISABLE(&TimHandle);

    TimHandle.Init.Period        = ns - 1;
    TimHandle.Init.Prescaler     = (uint16_t)(pres - 1); // (SystemCoreClock / 1000000) - 1; // 1 ns tick
    TimHandle.Init.ClockDivision = 0;
    TimHandle.Init.CounterMode   = TIM_COUNTERMODE_UP;
    HAL_TIM_PWM_Init(&TimHandle);

    // Save for future use
    obj->period = ns;
    
    // Set duty cycle again
    pwmout_write_ex (obj, dc);

    __HAL_TIM_ENABLE(&TimHandle);
}
Example #9
0
void IMU::initializeTimerForUpdate(void) {
	/* TIMx Peripheral clock enable */
	__TIM3_CLK_ENABLE();

	const uint32_t CounterClk = 10000;	//Hz
	const uint16_t OutputClk = 400;	//Hz
	samplingFrequencyInHz = OutputClk;
	samplingPeriodInS = 1.0 / OutputClk;
	//Prescaler = ((SystemCoreClock/2) / TIM3 counter clock) - 1
	const uint16_t Prescaler = (((SystemCoreClock / 2) / CounterClk) - 1);
	//ARR(TIM_Period) = (TIM3 counter clock / TIM3 output clock) - 1
	const uint32_t Period = ((CounterClk / OutputClk) - 1);

	/* Set TIMx instance */
	TimHandle.Instance = TIM3;
	TimHandle.Init.Period = Period;
	TimHandle.Init.Prescaler = Prescaler;
	TimHandle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
	TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
	TimHandle.Init.RepetitionCounter = 0;
	if (HAL_TIM_Base_Init(&TimHandle) != HAL_OK) {
		/* Initialization Error */
		while (1) {
		};
	}

	/* Enable the TIMx global Interrupt */
	HAL_NVIC_EnableIRQ((IRQn_Type) TIM3_IRQn);
	/* Set Interrupt Group Priority */
	HAL_NVIC_SetPriority((IRQn_Type) TIM3_IRQn, 1, 2);
	/* Start Channel1 */
	if (HAL_TIM_Base_Start_IT(&TimHandle) != HAL_OK) {
		/* Starting Error */
		while (1) {
		};
	}

	__HAL_TIM_DISABLE(&TimHandle);
}
int timer_stop(hacs_timer_t tim)
{
  __HAL_TIM_DISABLE(&tim_handles[tim]);
  return HACS_NO_ERROR;
}
Example #11
0
void pwmout_period_us(pwmout_t* obj, int us)
{
    TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
    RCC_ClkInitTypeDef RCC_ClkInitStruct;
    uint32_t PclkFreq;
    uint32_t APBxCLKDivider;
    float dc = pwmout_read(obj);

    __HAL_TIM_DISABLE(&TimHandle);

    // Get clock configuration
    // Note: PclkFreq contains here the Latency (not used after)
    HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq);

    // Get the PCLK and APBCLK divider related to the timer
    switch (obj->pwm) {

    // APB1 clock
    case PWM_2:
    case PWM_3:
    case PWM_4:
    case PWM_5:
    case PWM_12:
    case PWM_13:
    case PWM_14:
        PclkFreq = HAL_RCC_GetPCLK1Freq();
        APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider;
        break;

    // APB2 clock
    case PWM_1:
    case PWM_8:
    case PWM_9:
    case PWM_10:
    case PWM_11:
        PclkFreq = HAL_RCC_GetPCLK2Freq();
        APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider;
        break;
    default:
        return;
    }

    /* To make it simple, we use to possible prescaler values which lead to:
      * pwm unit = 1us, period/pulse can be from 1us to 65535us
      * or
      * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec
      * Be careful that all the channels of a PWM shares the same prescaler
      */
    if (us >  0xFFFF) {
        obj->prescaler = 500;
    } else {
        obj->prescaler = 1;
    }

    // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx
    if (APBxCLKDivider == RCC_HCLK_DIV1)
        TimHandle.Init.Prescaler   = (uint16_t)(((PclkFreq) / 1000000) * obj->prescaler) - 1; // 1 us tick
    else
        TimHandle.Init.Prescaler   = (uint16_t)(((PclkFreq * 2) / 1000000) * obj->prescaler) - 1; // 1 us tick

    if (TimHandle.Init.Prescaler > 0xFFFF)
        error("PWM: out of range prescaler");

    TimHandle.Init.Period        = (us - 1) / obj->prescaler;
    if (TimHandle.Init.Period > 0xFFFF)
        error("PWM: out of range period");

    TimHandle.Init.ClockDivision = 0;
    TimHandle.Init.CounterMode   = TIM_COUNTERMODE_UP;

    if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) {
        error("Cannot initialize PWM\n");
    }

    // Save for future use
    obj->period = us;

    // Set duty cycle again
    pwmout_write(obj, dc);

    __HAL_TIM_ENABLE(&TimHandle);
}
Example #12
0
void 
vMBPortTimerClose( void )
{
    __HAL_TIM_DISABLE_IT(&htim1, TIM_IT_UPDATE);
    __HAL_TIM_DISABLE(&htim1);
}
Example #13
0
void IMU::stopTimerUpdate(void) {
	__HAL_TIM_DISABLE(&TimHandle);
}
Example #14
0
void pwmout_period_us(pwmout_t* obj, int us)
{
    TimHandle.Instance = (TIM_TypeDef *)(obj->pwm);
    RCC_ClkInitTypeDef RCC_ClkInitStruct;
    uint32_t PclkFreq;
    uint32_t APBxCLKDivider;
    float dc = pwmout_read(obj);

    __HAL_TIM_DISABLE(&TimHandle);

    // Get clock configuration
    // Note: PclkFreq contains here the Latency (not used after)
    HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq);

    // Get the PCLK and APBCLK divider related to the timer
    switch (obj->pwm) {

            // APB1 clock
#if defined(TIM2_BASE)
        case PWM_2:
#endif
#if defined(TIM3_BASE)
        case PWM_3:
#endif
#if defined(TIM4_BASE)
        case PWM_4:
#endif
#if defined(TIM5_BASE)
        case PWM_5:
#endif
#if defined(TIM12_BASE)
        case PWM_12:
#endif
#if defined(TIM13_BASE)
        case PWM_13:
#endif
#if defined(TIM14_BASE)
        case PWM_14:
#endif
            PclkFreq = HAL_RCC_GetPCLK1Freq();
            APBxCLKDivider = RCC_ClkInitStruct.APB1CLKDivider;
            break;

            // APB2 clock
#if defined(TIM1_BASE)
        case PWM_1:
#endif
#if defined(TIM8_BASE)
        case PWM_8:
#endif
#if defined(TIM9_BASE)
        case PWM_9:
#endif
#if defined(TIM10_BASE)
        case PWM_10:
#endif
#if defined(TIM11_BASE)
        case PWM_11:
#endif
            PclkFreq = HAL_RCC_GetPCLK2Freq();
            APBxCLKDivider = RCC_ClkInitStruct.APB2CLKDivider;
            break;
        default:
            return;
    }

    TimHandle.Init.Period        = us - 1;
    // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx
    if (APBxCLKDivider == RCC_HCLK_DIV1)
        TimHandle.Init.Prescaler   = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick
    else
        TimHandle.Init.Prescaler   = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick
    TimHandle.Init.ClockDivision = 0;
    TimHandle.Init.CounterMode   = TIM_COUNTERMODE_UP;

    if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK) {
        error("Cannot initialize PWM\n");
    }

    // Set duty cycle again
    pwmout_write(obj, dc);

    // Save for future use
    obj->period = us;

    __HAL_TIM_ENABLE(&TimHandle);
}
Example #15
0
/**
 * @brief Timer Capture Callback function for Frequency Calculations
 */
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
  __HAL_TIM_DISABLE(htim);

  if(htim->Instance == TIM9 || htim->Instance == TIM12)
  {
    /* Get the Input Capture value */
//    if(tim9_12_flag == ENABLE)
      uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
//    else
//      uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2);

    if (uwIC2Value != 0)
    {
      /* Duty cycle computation */
//      if(tim9_12_flag == ENABLE)
        uwDutyCycle = ((HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_2)) * 100) / uwIC2Value;
//      else
//        uwDutyCycle = ((HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1)) * 100) / uwIC2Value;

      /* uwFrequency computation
      TIM4 counter clock = (RCC_Clocks.HCLK_Frequency)/2 */
      if(htim->Instance == TIM9)
        uwFrequency1 = (168000000UL)/(TimCapPsc+1) / uwIC2Value;
      else if(htim->Instance == TIM12)
        uwFrequency1 = (168000000UL)/(2*(TimCapPsc+1)) / uwIC2Value;
    }
    else
    {
      uwDutyCycle = 0;
      uwFrequency1 = 0;
    }
  }
  else if(htim->Instance == TIM10 || htim->Instance == TIM11 || htim->Instance == TIM14 || htim->Instance == TIM13)
  {
    if(uhCaptureIndex == 0)
    {
      /* Get the 1st Input Capture value */
      uwIC2Value1 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
      uhCaptureIndex = 1;
    }
    else if(uhCaptureIndex == 1)
    {
      /* Get the 2nd Input Capture value */
      uwIC2Value2 = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);

      /* Capture computation */
      if (uwIC2Value2 > uwIC2Value1)
      {
        uwDiffCapture = (uwIC2Value2 - uwIC2Value1);
      }
      else  /* (uwIC2Value2 <= uwIC2Value1) */
      {
        uwDiffCapture = ((TimCapPeriod - uwIC2Value1) + uwIC2Value2);
      }

      /* Frequency computation: for this example TIMx (TIM1) is clocked by
         2xAPB2Clk */
      uwFrequency = (2*HAL_RCC_GetPCLK2Freq()) / uwDiffCapture;
      if(htim->Instance == TIM10 || htim->Instance == TIM11)
        uwFrequency = uwFrequency/(TimCapPsc+1);
      else
        uwFrequency = uwFrequency/(2*(TimCapPsc+1));
      uhCaptureIndex = 0;
    }
  }

  __HAL_TIM_ENABLE(htim);
}