-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathSTM32PWMInput.cpp
More file actions
141 lines (114 loc) · 4.37 KB
/
STM32PWMInput.cpp
File metadata and controls
141 lines (114 loc) · 4.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include "./STM32PWMInput.h"
#include <SimpleFOC.h>
#include "communication/SimpleFOCDebug.h"
#if defined(_STM32_DEF_)
STM32PWMInput::STM32PWMInput(int pin, uint32_t pwm_freq){
_pin = digitalPinToPinName(pin);
_pwm_freq = pwm_freq;
};
STM32PWMInput::~STM32PWMInput(){};
int STM32PWMInput::initialize(){
pinmap_pinout(_pin, PinMap_TIM);
uint32_t channel = STM_PIN_CHANNEL(pinmap_function(_pin, PinMap_TIM));
timer.Instance = (TIM_TypeDef *)pinmap_peripheral(_pin, PinMap_TIM);
timer.Init.CounterMode = TIM_COUNTERMODE_UP;
// Check if timer is 16 or 32 bit and set max period accordingly
if (IS_TIM_32B_COUNTER_INSTANCE(timer.Instance)) {
timer.Init.Period = 0xFFFFFFFF; // 32-bit timer max
} else {
timer.Init.Period = 0xFFFF; // 16-bit timer max
}
timer.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
timer.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (channel!=1 && channel!=2) // only channels 1 & 2 supported
return -10;
useChannel2 = (channel==2);// remember the channel
if (HAL_TIM_Base_Init(&timer) != HAL_OK) {
return -1;
}
TIM_ClockConfigTypeDef clkCfg = { .ClockSource=TIM_CLOCKSOURCE_INTERNAL };
if (HAL_TIM_ConfigClockSource(&timer, &clkCfg) != HAL_OK) {
return -2;
}
if (HAL_TIM_IC_Init(&timer) != HAL_OK) {
return -3;
}
TIM_SlaveConfigTypeDef slCfg = {
.SlaveMode = TIM_SLAVEMODE_RESET,
.InputTrigger = (channel==1)?TIM_TS_TI1FP1:TIM_TS_TI2FP2,
.TriggerPolarity = TIM_INPUTCHANNELPOLARITY_RISING,
.TriggerPrescaler = TIM_ICPSC_DIV1,
.TriggerFilter = 0
};
if (HAL_TIM_SlaveConfigSynchro(&timer, &slCfg) != HAL_OK) {
return -4;
}
TIM_IC_InitTypeDef icCfg = {
.ICPolarity = (channel==1)?TIM_INPUTCHANNELPOLARITY_RISING:TIM_INPUTCHANNELPOLARITY_FALLING,
.ICSelection = (channel==1)?TIM_ICSELECTION_DIRECTTI:TIM_ICSELECTION_INDIRECTTI,
.ICPrescaler = TIM_ICPSC_DIV1,
.ICFilter = 0
};
if (HAL_TIM_IC_ConfigChannel(&timer, &icCfg, TIM_CHANNEL_1) != HAL_OK) {
return -5;
}
icCfg.ICPolarity = (channel==1)?TIM_INPUTCHANNELPOLARITY_FALLING:TIM_INPUTCHANNELPOLARITY_RISING;
icCfg.ICSelection = (channel==1)?TIM_ICSELECTION_INDIRECTTI:TIM_ICSELECTION_DIRECTTI;
if (HAL_TIM_IC_ConfigChannel(&timer, &icCfg, TIM_CHANNEL_2) != HAL_OK) {
return -6;
}
TIM_MasterConfigTypeDef mCfg = {
.MasterOutputTrigger = TIM_TRGO_RESET,
.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE
};
if (HAL_TIMEx_MasterConfigSynchronization(&timer, &mCfg) != HAL_OK) {
return -7;
}
if (HAL_TIM_IC_Start(&timer, TIM_CHANNEL_1)!=HAL_OK) {
return -8;
}
if (HAL_TIM_IC_Start(&timer, TIM_CHANNEL_2)!=HAL_OK) {
return -9;
}
// Check if the timer period is longer than the PWM period
// if it isnt set the perescaler to make it longer
// Calculate timer clock frequency
uint32_t timer_clk = HAL_RCC_GetPCLK1Freq();
if (IS_TIM_CLOCK_DIVISION_INSTANCE(timer.Instance)) {
// If APB1 prescaler > 1, timer clock is doubled
if ((RCC->CFGR & RCC_CFGR_PPRE1) != RCC_CFGR_PPRE1_DIV1) {
timer_clk *= 2;
}
}
// Calculate required period (in timer ticks) for one PWM period
uint32_t desired_period_ticks = timer_clk / _pwm_freq;
// Check if timer's max period can fit the desired period
uint32_t max_period = (IS_TIM_32B_COUNTER_INSTANCE(timer.Instance)) ? 0xFFFFFFFF : 0xFFFF;
uint32_t prescaler = 1;
if (desired_period_ticks > max_period) {
prescaler = (desired_period_ticks + max_period - 1) / max_period;
if (prescaler > 0xFFFF) prescaler = 0xFFFF; // limit to 16-bit prescaler
}
// Set the prescaler to achieve the desired period
LL_TIM_SetPrescaler(timer.Instance, prescaler);
timer.Instance->CR1 |= TIM_CR1_CEN;
return 0;
};
float STM32PWMInput::getDutyCyclePercent(){
uint32_t period = getPeriodTicks();
if (period<1) return 0.0f;
return getDutyCycleTicks() / (float)period * 100.0f;
};
uint32_t STM32PWMInput::getDutyCycleTicks(){
if (useChannel2)
return timer.Instance->CCR1;
else
return timer.Instance->CCR2;
};
uint32_t STM32PWMInput::getPeriodTicks(){
if (useChannel2)
return timer.Instance->CCR2;
else
return timer.Instance->CCR1;
};
#endif