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); }
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); }
// 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; }
void vMBPortTimersEnable( ) { /* Enable the timer with the timeout passed to xMBPortTimersInit( ) */ /* Enable the timer with the timeout passed to xMBPortTimersInit( ) */ __HAL_TIM_CLEAR_IT(&htim6,TIM_IT_UPDATE); __HAL_TIM_ENABLE_IT(&htim6,TIM_IT_UPDATE); __HAL_TIM_SetCounter(&htim6,0); __HAL_TIM_ENABLE(&htim6); }
void vMBPortTimersEnable( ) { // HAL_UART_Transmit(&huart1, "t" , 1, 0xFFFF); #if MB_TIMER_DEBUG == 1 PIO_Set( &xTimerDebugPins[0] ); #endif TIM1->CNT = 0; __HAL_TIM_ENABLE_IT(&htim1, TIM_IT_UPDATE); __HAL_TIM_ENABLE(&htim1); }
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); }
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); }
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); }
void IMU::startTimerUpdate(void) { __HAL_TIM_ENABLE(&TimHandle); }
void MX_TIM_Init(void) { __HAL_RCC_TIM1_CLK_ENABLE(); __HAL_RCC_TIM8_CLK_ENABLE(); TIM_MasterConfigTypeDef sMasterConfig; TIM_OC_InitTypeDef sConfigOC; TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig; TIM_SlaveConfigTypeDef sTimConfig; htim_right.Instance = RIGHT_TIM; htim_right.Init.Prescaler = 0; htim_right.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1; htim_right.Init.Period = 64000000 / 2 / PWM_FREQ; htim_right.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim_right.Init.RepetitionCounter = 0; htim_right.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; HAL_TIM_PWM_Init(&htim_right); sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; HAL_TIMEx_MasterConfigSynchronization(&htim_right, &sMasterConfig); sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 0; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_SET; HAL_TIM_PWM_ConfigChannel(&htim_right, &sConfigOC, TIM_CHANNEL_1); HAL_TIM_PWM_ConfigChannel(&htim_right, &sConfigOC, TIM_CHANNEL_2); HAL_TIM_PWM_ConfigChannel(&htim_right, &sConfigOC, TIM_CHANNEL_3); sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_ENABLE; sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_ENABLE; sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; sBreakDeadTimeConfig.DeadTime = DEAD_TIME; sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_LOW; sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; HAL_TIMEx_ConfigBreakDeadTime(&htim_right, &sBreakDeadTimeConfig); htim_left.Instance = LEFT_TIM; htim_left.Init.Prescaler = 0; htim_left.Init.CounterMode = TIM_COUNTERMODE_CENTERALIGNED1; htim_left.Init.Period = 64000000 / 2 / PWM_FREQ; htim_left.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim_left.Init.RepetitionCounter = 0; htim_left.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; HAL_TIM_PWM_Init(&htim_left); sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE; HAL_TIMEx_MasterConfigSynchronization(&htim_left, &sMasterConfig); sTimConfig.InputTrigger = TIM_TS_ITR0; sTimConfig.SlaveMode = TIM_SLAVEMODE_GATED; HAL_TIM_SlaveConfigSynchronization(&htim_left, &sTimConfig); sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = 0; sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH; sConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW; sConfigOC.OCFastMode = TIM_OCFAST_DISABLE; sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_SET; HAL_TIM_PWM_ConfigChannel(&htim_left, &sConfigOC, TIM_CHANNEL_1); HAL_TIM_PWM_ConfigChannel(&htim_left, &sConfigOC, TIM_CHANNEL_2); HAL_TIM_PWM_ConfigChannel(&htim_left, &sConfigOC, TIM_CHANNEL_3); sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_ENABLE; sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_ENABLE; sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; sBreakDeadTimeConfig.DeadTime = DEAD_TIME; sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_LOW; sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; HAL_TIMEx_ConfigBreakDeadTime(&htim_left, &sBreakDeadTimeConfig); LEFT_TIM->BDTR &= ~TIM_BDTR_MOE; RIGHT_TIM->BDTR &= ~TIM_BDTR_MOE; HAL_TIM_PWM_Start(&htim_left, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim_left, TIM_CHANNEL_2); HAL_TIM_PWM_Start(&htim_left, TIM_CHANNEL_3); HAL_TIMEx_PWMN_Start(&htim_left, TIM_CHANNEL_1); HAL_TIMEx_PWMN_Start(&htim_left, TIM_CHANNEL_2); HAL_TIMEx_PWMN_Start(&htim_left, TIM_CHANNEL_3); HAL_TIM_PWM_Start(&htim_right, TIM_CHANNEL_1); HAL_TIM_PWM_Start(&htim_right, TIM_CHANNEL_2); HAL_TIM_PWM_Start(&htim_right, TIM_CHANNEL_3); HAL_TIMEx_PWMN_Start(&htim_right, TIM_CHANNEL_1); HAL_TIMEx_PWMN_Start(&htim_right, TIM_CHANNEL_2); HAL_TIMEx_PWMN_Start(&htim_right, TIM_CHANNEL_3); htim_left.Instance->RCR = 1; __HAL_TIM_ENABLE(&htim_right); }
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); }
/** * @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); }