@@ -198,55 +198,108 @@ class AnalogOutPWMClass {
198
198
public:
199
199
AnalogOutPWMClass () {
200
200
GPIO_InitTypeDef GPIO_InitStruct;
201
- /* ##-1- Enable peripherals and GPIO Clocks ################################# */
202
- /* HRTIM1 Peripheral clock enable */
201
+
202
+ // Enables peripherals and GPIO Clocks HRTIM1 Peripheral clock enable
203
203
__HAL_RCC_HRTIM1_CLK_ENABLE ();
204
204
205
- /* Enable GPIO Channels Clock */
205
+ // Enable GPIO Channels Clock
206
206
__HAL_RCC_GPIOG_CLK_ENABLE ();
207
207
208
- /* Configure HRTIMA TIMA TA1/A2, TIMB TB1/2, TIMC TC1/2, TIMD TD1/2 and TIME TE1.2
209
- channels as alternate function mode */
210
- /* Common configuration for all channels */
208
+ // Configure HRTIMA TIMA TA1/A2, TIMB TB1/2, TIMC TC1/2, TIMD TD1/2 and TIME TE1.2
209
+ // channels as alternate function mode
210
+
211
211
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
212
212
GPIO_InitStruct.Pull = GPIO_PULLUP;
213
213
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
214
214
GPIO_InitStruct.Alternate = GPIO_AF2_HRTIM1;
215
215
GPIO_InitStruct.Pin = GPIO_PIN_7;
216
216
HAL_GPIO_Init (GPIOG, &GPIO_InitStruct);
217
217
218
- /* ##-1- Configure the HRTIM peripheral ###################################################### */
219
- /* Initialize the HRTIM structure */
218
+ // Configure the HRTIM peripheral
219
+ // Initialize the HRTIM structure
220
220
HrtimHandle.Instance = HRTIM1;
221
221
HrtimHandle.Init .HRTIMInterruptResquests = HRTIM_IT_NONE;
222
222
HrtimHandle.Init .SyncOptions = HRTIM_SYNCOPTION_NONE;
223
223
224
224
HAL_HRTIM_Init (&HrtimHandle);
225
+
226
+ // Configure the HRTIM TIME PWM channels 2
227
+ sConfig_time_base .Mode = HRTIM_MODE_CONTINUOUS;
228
+ sConfig_time_base .Period = 100 ;
229
+ sConfig_time_base .PrescalerRatio = HRTIM_PRESCALERRATIO_DIV1;
230
+ sConfig_time_base .RepetitionCounter = 0 ;
231
+
232
+ HAL_HRTIM_TimeBaseConfig (&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, &sConfig_time_base );
233
+
234
+ sConfig_timerE .DMARequests = HRTIM_TIM_DMA_NONE;
235
+ sConfig_timerE .HalfModeEnable = HRTIM_HALFMODE_DISABLED;
236
+ sConfig_timerE .StartOnSync = HRTIM_SYNCSTART_DISABLED;
237
+ sConfig_timerE .ResetOnSync = HRTIM_SYNCRESET_DISABLED;
238
+ sConfig_timerE .DACSynchro = HRTIM_DACSYNC_NONE;
239
+ sConfig_timerE .PreloadEnable = HRTIM_PRELOAD_ENABLED;
240
+ sConfig_timerE .UpdateGating = HRTIM_UPDATEGATING_INDEPENDENT;
241
+ sConfig_timerE .BurstMode = HRTIM_TIMERBURSTMODE_MAINTAINCLOCK;
242
+ sConfig_timerE .RepetitionUpdate = HRTIM_UPDATEONREPETITION_ENABLED;
243
+ sConfig_timerE .ResetUpdate = HRTIM_TIMUPDATEONRESET_DISABLED;
244
+ sConfig_timerE .InterruptRequests = HRTIM_TIM_IT_NONE;
245
+ sConfig_timerE .PushPull = HRTIM_TIMPUSHPULLMODE_DISABLED;
246
+ sConfig_timerE .FaultEnable = HRTIM_TIMFAULTENABLE_NONE;
247
+ sConfig_timerE .FaultLock = HRTIM_TIMFAULTLOCK_READWRITE;
248
+ sConfig_timerE .DeadTimeInsertion = HRTIM_TIMDEADTIMEINSERTION_DISABLED;
249
+ sConfig_timerE .DelayedProtectionMode = HRTIM_TIMER_D_E_DELAYEDPROTECTION_DISABLED;
250
+ sConfig_timerE .UpdateTrigger = HRTIM_TIMUPDATETRIGGER_NONE;
251
+ sConfig_timerE .ResetTrigger = HRTIM_TIMRESETTRIGGER_NONE;
252
+
253
+ HAL_HRTIM_WaveformTimerConfig (&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E,&sConfig_timerE );
254
+
255
+ sConfig_compare .AutoDelayedMode = HRTIM_AUTODELAYEDMODE_REGULAR;
256
+ sConfig_compare .AutoDelayedTimeout = 0 ;
257
+ sConfig_compare .CompareValue = 1 ;
258
+
259
+ HAL_HRTIM_WaveformCompareConfig (&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_COMPAREUNIT_2, &sConfig_compare );
260
+
261
+ sConfig_output_config .Polarity = HRTIM_OUTPUTPOLARITY_LOW;
262
+ sConfig_output_config .SetSource = HRTIM_OUTPUTRESET_TIMCMP2;
263
+ sConfig_output_config .ResetSource = HRTIM_OUTPUTSET_TIMPER;
264
+ sConfig_output_config .IdleMode = HRTIM_OUTPUTIDLEMODE_NONE;
265
+ sConfig_output_config .IdleLevel = HRTIM_OUTPUTIDLELEVEL_INACTIVE;
266
+ sConfig_output_config .FaultLevel = HRTIM_OUTPUTFAULTLEVEL_NONE;
267
+ sConfig_output_config .ChopperModeEnable = HRTIM_OUTPUTCHOPPERMODE_DISABLED;
268
+ sConfig_output_config .BurstModeEntryDelayed = HRTIM_OUTPUTBURSTMODEENTRY_REGULAR;
269
+ sConfig_output_config .ResetSource = HRTIM_OUTPUTRESET_TIMPER;
270
+ sConfig_output_config .SetSource = HRTIM_OUTPUTSET_TIMCMP2;
271
+
272
+ HAL_HRTIM_WaveformOutputConfig (&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_OUTPUT_TE2, &sConfig_output_config );
273
+
274
+ // Start PWM signals generation
275
+ if (HAL_HRTIM_WaveformOutputStart (&HrtimHandle, HRTIM_OUTPUT_TE2) != HAL_OK)
276
+ {
277
+ // PWM Generation Error
278
+ }
279
+
280
+ // Start HRTIM counter
281
+ if (HAL_HRTIM_WaveformCounterStart (&HrtimHandle, HRTIM_TIMERID_TIMER_E) != HAL_OK)
282
+ {
283
+ // PWM Generation Error
284
+ }
225
285
}
226
286
227
287
~AnalogOutPWMClass (){}
228
- // TODO: check why chamnge PrescalerRatio broke the portenta
229
288
void period_ms (int period) {
230
-
231
289
sConfig_time_base .Mode = HRTIM_MODE_CONTINUOUS;
232
- sConfig_time_base .Period = 100 ;
290
+ sConfig_time_base .Period = period ;
233
291
sConfig_time_base .PrescalerRatio = HRTIM_PRESCALERRATIO_DIV1;
234
292
sConfig_time_base .RepetitionCounter = 0 ;
235
293
236
294
HAL_HRTIM_TimeBaseConfig (&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, &sConfig_time_base );
237
295
}
238
296
239
297
bool write (uint8_t voltage) {
240
- sConfig_Channel .Polarity = HRTIM_OUTPUTPOLARITY_LOW;
241
- sConfig_Channel .IdleLevel = HRTIM_OUTPUTIDLELEVEL_INACTIVE;
242
- sConfig_Channel .Pulse = voltage*10 ;
243
-
244
- HAL_HRTIM_SimplePWMChannelConfig (&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E,
245
- HRTIM_OUTPUT_TE2, &sConfig_Channel );
246
- if (HAL_HRTIM_SimplePWMStart (&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_OUTPUT_TE2) != HAL_OK)
298
+ sConfig_compare .CompareValue = voltage;
299
+ if (HAL_HRTIM_WaveformCompareConfig (&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_COMPAREUNIT_2, &sConfig_compare ) != HAL_OK)
247
300
{
248
301
return false ;
249
- }
302
+ }
250
303
return true ;
251
304
}
252
305
@@ -262,7 +315,9 @@ class AnalogOutPWMClass {
262
315
HRTIM_HandleTypeDef HrtimHandle;
263
316
264
317
HRTIM_TimeBaseCfgTypeDef sConfig_time_base ;
265
- HRTIM_SimplePWMChannelCfgTypeDef sConfig_Channel ;
318
+ HRTIM_TimerCfgTypeDef sConfig_timerE ;
319
+ HRTIM_OutputCfgTypeDef sConfig_output_config ;
320
+ HRTIM_CompareCfgTypeDef sConfig_compare ;
266
321
};
267
322
268
323
@@ -271,13 +326,13 @@ extern AnalogOutPWMClass analopwm;
271
326
272
327
class AnalogOutClass {
273
328
public:
274
- void write (int index, uint8_t voltage) {
329
+ void write (int index, float voltage) {
275
330
switch (index ) {
276
331
case 0 :
277
332
out_0.write (voltage / 10.568 );
278
333
break ;
279
334
case 1 :
280
- out_1.write (voltage);
335
+ out_1.write (( voltage* 9.3679 ) + 1 );
281
336
break ;
282
337
case 2 :
283
338
out_2.write (voltage / 10.568 );
@@ -290,7 +345,7 @@ class AnalogOutClass {
290
345
out_0.period_ms (period);
291
346
break ;
292
347
case 1 :
293
- out_1.period_ms (period);
348
+ out_1.period_ms (( period/ 4 )* 100 );
294
349
break ;
295
350
case 2 :
296
351
out_2.period_ms (period);
0 commit comments