@@ -66,6 +66,7 @@ void pwmout_init(pwmout_t* obj, PinName pin)
66
66
obj -> pin = pin ;
67
67
obj -> period = 0 ;
68
68
obj -> pulse = 0 ;
69
+ obj -> prescaler = 1 ;
69
70
70
71
pwmout_period_us (obj , 20000 ); // 20 ms per default
71
72
}
@@ -93,7 +94,7 @@ void pwmout_write(pwmout_t* obj, float value)
93
94
94
95
// Configure channels
95
96
sConfig .OCMode = TIM_OCMODE_PWM1 ;
96
- sConfig .Pulse = obj -> pulse ;
97
+ sConfig .Pulse = obj -> pulse / obj -> prescaler ;
97
98
sConfig .OCPolarity = TIM_OCPOLARITY_HIGH ;
98
99
sConfig .OCFastMode = TIM_OCFAST_ENABLE ;
99
100
@@ -148,21 +149,39 @@ void pwmout_period_us(pwmout_t* obj, int us)
148
149
149
150
__HAL_TIM_DISABLE (& TimHandle );
150
151
151
- TimHandle .Init .Period = us - 1 ;
152
- TimHandle .Init .Prescaler = (uint16_t )(SystemCoreClock / 1000000 ) - 1 ; // 1 us tick
152
+ /* To make it simple, we use to possible prescaler values which lead to:
153
+ * pwm unit = 1us, period/pulse can be from 1us to 65535us
154
+ * or
155
+ * pwm unit = 500us, period/pulse can be from 500us to ~32.76sec
156
+ * Be careful that all the channels of a PWM shares the same prescaler
157
+ */
158
+ if (us > 0xFFFF ) {
159
+ obj -> prescaler = 500 ;
160
+ } else {
161
+ obj -> prescaler = 1 ;
162
+ }
163
+ TimHandle .Init .Prescaler = ((SystemCoreClock / 1000000 ) * obj -> prescaler ) - 1 ;
164
+
165
+ if (TimHandle .Init .Prescaler > 0xFFFF )
166
+ error ("PWM: out of range prescaler" );
167
+
168
+ TimHandle .Init .Period = (us - 1 ) / obj -> prescaler ;
169
+ if (TimHandle .Init .Period > 0xFFFF )
170
+ error ("PWM: out of range period" );
171
+
153
172
TimHandle .Init .ClockDivision = 0 ;
154
173
TimHandle .Init .CounterMode = TIM_COUNTERMODE_UP ;
155
174
156
175
if (HAL_TIM_PWM_Init (& TimHandle ) != HAL_OK ) {
157
176
error ("Cannot initialize PWM" );
158
177
}
159
178
160
- // Set duty cycle again
161
- pwmout_write (obj , dc );
162
-
163
179
// Save for future use
164
180
obj -> period = us ;
165
181
182
+ // Set duty cycle again
183
+ pwmout_write (obj , dc );
184
+
166
185
__HAL_TIM_ENABLE (& TimHandle );
167
186
}
168
187
0 commit comments