|
|
|
/* USER CODE BEGIN Header */
|
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @file tim.c
|
|
|
|
* @brief This file provides code for the configuration
|
|
|
|
* of the TIM instances.
|
|
|
|
******************************************************************************
|
|
|
|
* @attention
|
|
|
|
*
|
|
|
|
* Copyright (c) 2023 STMicroelectronics.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This software is licensed under terms that can be found in the LICENSE file
|
|
|
|
* in the root directory of this software component.
|
|
|
|
* If no LICENSE file comes with this software, it is provided AS-IS.
|
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
/* USER CODE END Header */
|
|
|
|
/* Includes ------------------------------------------------------------------*/
|
|
|
|
#include "tim.h"
|
|
|
|
|
|
|
|
/* USER CODE BEGIN 0 */
|
|
|
|
|
|
|
|
/* USER CODE END 0 */
|
|
|
|
|
|
|
|
/* TIM1 init function */
|
|
|
|
void MX_TIM1_Init(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* USER CODE BEGIN TIM1_Init 0 */
|
|
|
|
|
|
|
|
// --- this timer is used to time ADC sampling. ---
|
|
|
|
|
|
|
|
/* USER CODE END TIM1_Init 0 */
|
|
|
|
|
|
|
|
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
|
|
|
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
|
|
|
|
LL_TIM_BDTR_InitTypeDef TIM_BDTRInitStruct = {0};
|
|
|
|
|
|
|
|
/* Peripheral clock enable */
|
|
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_TIM1);
|
|
|
|
|
|
|
|
/* USER CODE BEGIN TIM1_Init 1 */
|
|
|
|
|
|
|
|
/* USER CODE END TIM1_Init 1 */
|
|
|
|
TIM_InitStruct.Prescaler = 64000;
|
|
|
|
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
|
|
|
TIM_InitStruct.Autoreload = 10;
|
|
|
|
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
|
|
|
TIM_InitStruct.RepetitionCounter = 0;
|
|
|
|
LL_TIM_Init(TIM1, &TIM_InitStruct);
|
|
|
|
LL_TIM_DisableARRPreload(TIM1);
|
|
|
|
LL_TIM_SetClockSource(TIM1, LL_TIM_CLOCKSOURCE_INTERNAL);
|
|
|
|
LL_TIM_OC_EnablePreload(TIM1, LL_TIM_CHANNEL_CH1);
|
|
|
|
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
|
|
|
|
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
|
|
|
|
TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
|
|
|
|
TIM_OC_InitStruct.CompareValue = 1;
|
|
|
|
TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
|
|
|
|
TIM_OC_InitStruct.OCNPolarity = LL_TIM_OCPOLARITY_HIGH;
|
|
|
|
TIM_OC_InitStruct.OCIdleState = LL_TIM_OCIDLESTATE_LOW;
|
|
|
|
TIM_OC_InitStruct.OCNIdleState = LL_TIM_OCIDLESTATE_LOW;
|
|
|
|
LL_TIM_OC_Init(TIM1, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
|
|
|
|
LL_TIM_OC_DisableFast(TIM1, LL_TIM_CHANNEL_CH1);
|
|
|
|
LL_TIM_SetTriggerOutput(TIM1, LL_TIM_TRGO_OC1REF);
|
|
|
|
LL_TIM_DisableMasterSlaveMode(TIM1);
|
|
|
|
TIM_BDTRInitStruct.OSSRState = LL_TIM_OSSR_DISABLE;
|
|
|
|
TIM_BDTRInitStruct.OSSIState = LL_TIM_OSSI_DISABLE;
|
|
|
|
TIM_BDTRInitStruct.LockLevel = LL_TIM_LOCKLEVEL_OFF;
|
|
|
|
TIM_BDTRInitStruct.DeadTime = 0;
|
|
|
|
TIM_BDTRInitStruct.BreakState = LL_TIM_BREAK_DISABLE;
|
|
|
|
TIM_BDTRInitStruct.BreakPolarity = LL_TIM_BREAK_POLARITY_HIGH;
|
|
|
|
TIM_BDTRInitStruct.AutomaticOutput = LL_TIM_AUTOMATICOUTPUT_DISABLE;
|
|
|
|
LL_TIM_BDTR_Init(TIM1, &TIM_BDTRInitStruct);
|
|
|
|
/* USER CODE BEGIN TIM1_Init 2 */
|
|
|
|
|
|
|
|
/* USER CODE END TIM1_Init 2 */
|
|
|
|
|
|
|
|
}
|
|
|
|
/* TIM2 init function */
|
|
|
|
void MX_TIM2_Init(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* USER CODE BEGIN TIM2_Init 0 */
|
|
|
|
|
|
|
|
// --- this timer generates the buzzer PWM. The PWM parameters are adjusted on the fly ---
|
|
|
|
|
|
|
|
/* USER CODE END TIM2_Init 0 */
|
|
|
|
|
|
|
|
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
|
|
|
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
|
|
|
|
|
|
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
/* Peripheral clock enable */
|
|
|
|
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM2);
|
|
|
|
|
|
|
|
/* USER CODE BEGIN TIM2_Init 1 */
|
|
|
|
|
|
|
|
/* USER CODE END TIM2_Init 1 */
|
|
|
|
TIM_InitStruct.Prescaler = 2;
|
|
|
|
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
|
|
|
TIM_InitStruct.Autoreload = 65535;
|
|
|
|
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
|
|
|
LL_TIM_Init(TIM2, &TIM_InitStruct);
|
|
|
|
LL_TIM_DisableARRPreload(TIM2);
|
|
|
|
LL_TIM_SetClockSource(TIM2, LL_TIM_CLOCKSOURCE_INTERNAL);
|
|
|
|
LL_TIM_OC_EnablePreload(TIM2, LL_TIM_CHANNEL_CH1);
|
|
|
|
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
|
|
|
|
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
|
|
|
|
TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
|
|
|
|
TIM_OC_InitStruct.CompareValue = 0;
|
|
|
|
TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
|
|
|
|
LL_TIM_OC_Init(TIM2, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
|
|
|
|
LL_TIM_OC_DisableFast(TIM2, LL_TIM_CHANNEL_CH1);
|
|
|
|
LL_TIM_SetTriggerOutput(TIM2, LL_TIM_TRGO_RESET);
|
|
|
|
LL_TIM_DisableMasterSlaveMode(TIM2);
|
|
|
|
/* USER CODE BEGIN TIM2_Init 2 */
|
|
|
|
|
|
|
|
/* USER CODE END TIM2_Init 2 */
|
|
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
|
|
|
|
/**TIM2 GPIO Configuration
|
|
|
|
PA15 ------> TIM2_CH1
|
|
|
|
*/
|
|
|
|
GPIO_InitStruct.Pin = BUZZER_Pin;
|
|
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
|
|
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_HIGH;
|
|
|
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
|
|
|
LL_GPIO_Init(BUZZER_GPIO_Port, &GPIO_InitStruct);
|
|
|
|
|
|
|
|
LL_GPIO_AF_RemapPartial1_TIM2();
|
|
|
|
|
|
|
|
}
|
|
|
|
/* TIM3 init function */
|
|
|
|
void MX_TIM3_Init(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* USER CODE BEGIN TIM3_Init 0 */
|
|
|
|
|
|
|
|
// --- this timer generates the heater PWM. The duty is set as 640 * percent ---
|
|
|
|
|
|
|
|
/* USER CODE END TIM3_Init 0 */
|
|
|
|
|
|
|
|
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
|
|
|
LL_TIM_OC_InitTypeDef TIM_OC_InitStruct = {0};
|
|
|
|
|
|
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
/* Peripheral clock enable */
|
|
|
|
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);
|
|
|
|
|
|
|
|
/* USER CODE BEGIN TIM3_Init 1 */
|
|
|
|
|
|
|
|
/* USER CODE END TIM3_Init 1 */
|
|
|
|
TIM_InitStruct.Prescaler = 1000;
|
|
|
|
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
|
|
|
TIM_InitStruct.Autoreload = 64000;
|
|
|
|
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
|
|
|
LL_TIM_Init(TIM3, &TIM_InitStruct);
|
|
|
|
LL_TIM_DisableARRPreload(TIM3);
|
|
|
|
LL_TIM_SetClockSource(TIM3, LL_TIM_CLOCKSOURCE_INTERNAL);
|
|
|
|
LL_TIM_OC_EnablePreload(TIM3, LL_TIM_CHANNEL_CH1);
|
|
|
|
TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
|
|
|
|
TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
|
|
|
|
TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
|
|
|
|
TIM_OC_InitStruct.CompareValue = 0;
|
|
|
|
TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
|
|
|
|
LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH1, &TIM_OC_InitStruct);
|
|
|
|
LL_TIM_OC_DisableFast(TIM3, LL_TIM_CHANNEL_CH1);
|
|
|
|
LL_TIM_SetTriggerOutput(TIM3, LL_TIM_TRGO_RESET);
|
|
|
|
LL_TIM_DisableMasterSlaveMode(TIM3);
|
|
|
|
/* USER CODE BEGIN TIM3_Init 2 */
|
|
|
|
|
|
|
|
/* USER CODE END TIM3_Init 2 */
|
|
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
|
|
|
|
/**TIM3 GPIO Configuration
|
|
|
|
PA6 ------> TIM3_CH1
|
|
|
|
*/
|
|
|
|
GPIO_InitStruct.Pin = PWM_HEATER_Pin;
|
|
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
|
|
|
|
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
|
|
|
|
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
|
|
|
|
LL_GPIO_Init(PWM_HEATER_GPIO_Port, &GPIO_InitStruct);
|
|
|
|
|
|
|
|
}
|
|
|
|
/* TIM4 init function */
|
|
|
|
void MX_TIM4_Init(void)
|
|
|
|
{
|
|
|
|
|
|
|
|
/* USER CODE BEGIN TIM4_Init 0 */
|
|
|
|
|
|
|
|
// --- This timer captures the rotary encoder. The count must be divided by 4, but it can't be done by the hardware, doesn't work well. ---
|
|
|
|
|
|
|
|
/* USER CODE END TIM4_Init 0 */
|
|
|
|
|
|
|
|
LL_TIM_InitTypeDef TIM_InitStruct = {0};
|
|
|
|
|
|
|
|
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
|
|
|
|
|
|
/* Peripheral clock enable */
|
|
|
|
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM4);
|
|
|
|
|
|
|
|
LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
|
|
|
|
/**TIM4 GPIO Configuration
|
|
|
|
PB6 ------> TIM4_CH1
|
|
|
|
PB7 ------> TIM4_CH2
|
|
|
|
*/
|
|
|
|
GPIO_InitStruct.Pin = KNOB_A_Pin|KNOB_B_Pin;
|
|
|
|
GPIO_InitStruct.Mode = LL_GPIO_MODE_INPUT;
|
|
|
|
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
|
|
|
|
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
|
|
|
|
/* TIM4 interrupt Init */
|
|
|
|
NVIC_SetPriority(TIM4_IRQn, NVIC_EncodePriority(NVIC_GetPriorityGrouping(),5, 0));
|
|
|
|
NVIC_EnableIRQ(TIM4_IRQn);
|
|
|
|
|
|
|
|
/* USER CODE BEGIN TIM4_Init 1 */
|
|
|
|
|
|
|
|
/* USER CODE END TIM4_Init 1 */
|
|
|
|
LL_TIM_SetEncoderMode(TIM4, LL_TIM_ENCODERMODE_X2_TI1);
|
|
|
|
LL_TIM_IC_SetActiveInput(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_ACTIVEINPUT_DIRECTTI);
|
|
|
|
LL_TIM_IC_SetPrescaler(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_ICPSC_DIV1);
|
|
|
|
LL_TIM_IC_SetFilter(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_IC_FILTER_FDIV32_N8);
|
|
|
|
LL_TIM_IC_SetPolarity(TIM4, LL_TIM_CHANNEL_CH1, LL_TIM_IC_POLARITY_RISING);
|
|
|
|
LL_TIM_IC_SetActiveInput(TIM4, LL_TIM_CHANNEL_CH2, LL_TIM_ACTIVEINPUT_DIRECTTI);
|
|
|
|
LL_TIM_IC_SetPrescaler(TIM4, LL_TIM_CHANNEL_CH2, LL_TIM_ICPSC_DIV1);
|
|
|
|
LL_TIM_IC_SetFilter(TIM4, LL_TIM_CHANNEL_CH2, LL_TIM_IC_FILTER_FDIV32_N8);
|
|
|
|
LL_TIM_IC_SetPolarity(TIM4, LL_TIM_CHANNEL_CH2, LL_TIM_IC_POLARITY_RISING);
|
|
|
|
TIM_InitStruct.Prescaler = 0;
|
|
|
|
TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
|
|
|
|
TIM_InitStruct.Autoreload = 65535;
|
|
|
|
TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
|
|
|
|
LL_TIM_Init(TIM4, &TIM_InitStruct);
|
|
|
|
LL_TIM_DisableARRPreload(TIM4);
|
|
|
|
LL_TIM_SetTriggerOutput(TIM4, LL_TIM_TRGO_RESET);
|
|
|
|
LL_TIM_DisableMasterSlaveMode(TIM4);
|
|
|
|
/* USER CODE BEGIN TIM4_Init 2 */
|
|
|
|
|
|
|
|
/* USER CODE END TIM4_Init 2 */
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* USER CODE BEGIN 1 */
|
|
|
|
|
|
|
|
/* USER CODE END 1 */
|