1
+ /*
2
+ portentaBreakoutAnalog.h
3
+ Part of Arduino - http://www.arduino.cc/
4
+
5
+ Copyright (c) 2018-2019 Arduino SA
6
+
7
+ This library is free software; you can redistribute it and/or
8
+ modify it under the terms of the GNU Lesser General Public
9
+ License as published by the Free Software Foundation; either
10
+ version 2.1 of the License, or (at your option) any later version.
11
+
12
+ This library is distributed in the hope that it will be useful,
13
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
+ Lesser General Public License for more details.
16
+
17
+ You should have received a copy of the GNU Lesser General
18
+ Public License along with this library; if not, write to the
19
+ Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20
+ Boston, MA 02111-1307 USA
21
+ */
22
+
23
+ namespace BreakoutPWM {
24
+
25
+
26
+ class HRTIMPWMClass {
27
+ public:
28
+ HRTIMPWMClass () {
29
+ GPIO_InitTypeDef GPIO_InitStruct;
30
+
31
+ // Enables peripherals and GPIO Clocks HRTIM1 Peripheral clock enable
32
+ __HAL_RCC_HRTIM1_CLK_ENABLE ();
33
+
34
+ // Enable GPIO Channels Clock
35
+ __HAL_RCC_GPIOG_CLK_ENABLE ();
36
+
37
+ // Configure HRTIMA TIMA TA1/A2, TIMB TB1/2, TIMC TC1/2, TIMD TD1/2 and TIME TE1.2
38
+ // channels as alternate function mode
39
+
40
+ GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
41
+ GPIO_InitStruct.Pull = GPIO_PULLUP;
42
+ GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
43
+ GPIO_InitStruct.Alternate = GPIO_AF2_HRTIM1;
44
+ GPIO_InitStruct.Pin = GPIO_PIN_7;
45
+ HAL_GPIO_Init (GPIOG, &GPIO_InitStruct);
46
+
47
+ // Configure the HRTIM peripheral
48
+ // Initialize the HRTIM structure
49
+ HrtimHandle.Instance = HRTIM1;
50
+ HrtimHandle.Init .HRTIMInterruptResquests = HRTIM_IT_NONE;
51
+ HrtimHandle.Init .SyncOptions = HRTIM_SYNCOPTION_NONE;
52
+
53
+ HAL_HRTIM_Init (&HrtimHandle);
54
+ #if 1
55
+ // Configure the HRTIM TIME PWM channels 2
56
+ sConfig_time_base .Mode = HRTIM_MODE_CONTINUOUS;
57
+ sConfig_time_base .Period = 0xFFFD ;
58
+ sConfig_time_base .PrescalerRatio = HRTIM_PRESCALERRATIO_DIV4;
59
+ sConfig_time_base .RepetitionCounter = 0 ;
60
+
61
+ HAL_HRTIM_TimeBaseConfig (&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, &sConfig_time_base );
62
+
63
+ sConfig_timerE .DMARequests = HRTIM_TIM_DMA_NONE;
64
+ sConfig_timerE .HalfModeEnable = HRTIM_HALFMODE_DISABLED;
65
+ sConfig_timerE .StartOnSync = HRTIM_SYNCSTART_DISABLED;
66
+ sConfig_timerE .ResetOnSync = HRTIM_SYNCRESET_DISABLED;
67
+ sConfig_timerE .DACSynchro = HRTIM_DACSYNC_NONE;
68
+ sConfig_timerE .PreloadEnable = HRTIM_PRELOAD_ENABLED;
69
+ sConfig_timerE .UpdateGating = HRTIM_UPDATEGATING_INDEPENDENT;
70
+ sConfig_timerE .BurstMode = HRTIM_TIMERBURSTMODE_MAINTAINCLOCK;
71
+ sConfig_timerE .RepetitionUpdate = HRTIM_UPDATEONREPETITION_ENABLED;
72
+ sConfig_timerE .ResetUpdate = HRTIM_TIMUPDATEONRESET_DISABLED;
73
+ sConfig_timerE .InterruptRequests = HRTIM_TIM_IT_NONE;
74
+ sConfig_timerE .PushPull = HRTIM_TIMPUSHPULLMODE_DISABLED;
75
+ sConfig_timerE .FaultEnable = HRTIM_TIMFAULTENABLE_NONE;
76
+ sConfig_timerE .FaultLock = HRTIM_TIMFAULTLOCK_READWRITE;
77
+ sConfig_timerE .DeadTimeInsertion = HRTIM_TIMDEADTIMEINSERTION_DISABLED;
78
+ sConfig_timerE .DelayedProtectionMode = HRTIM_TIMER_D_E_DELAYEDPROTECTION_DISABLED;
79
+ sConfig_timerE .UpdateTrigger = HRTIM_TIMUPDATETRIGGER_NONE;
80
+ sConfig_timerE .ResetTrigger = HRTIM_TIMRESETTRIGGER_NONE;
81
+
82
+ HAL_HRTIM_WaveformTimerConfig (&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E,&sConfig_timerE );
83
+
84
+ sConfig_compare .AutoDelayedMode = HRTIM_AUTODELAYEDMODE_REGULAR;
85
+ sConfig_compare .AutoDelayedTimeout = 0 ;
86
+ sConfig_compare .CompareValue = 0x7FFE ;
87
+
88
+ HAL_HRTIM_WaveformCompareConfig (&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_COMPAREUNIT_2, &sConfig_compare );
89
+
90
+ sConfig_output_config .Polarity = HRTIM_OUTPUTPOLARITY_LOW;
91
+ sConfig_output_config .SetSource = HRTIM_OUTPUTRESET_TIMCMP2;
92
+ sConfig_output_config .ResetSource = HRTIM_OUTPUTSET_TIMPER;
93
+ sConfig_output_config .IdleMode = HRTIM_OUTPUTIDLEMODE_NONE;
94
+ sConfig_output_config .IdleLevel = HRTIM_OUTPUTIDLELEVEL_INACTIVE;
95
+ sConfig_output_config .FaultLevel = HRTIM_OUTPUTFAULTLEVEL_NONE;
96
+ sConfig_output_config .ChopperModeEnable = HRTIM_OUTPUTCHOPPERMODE_DISABLED;
97
+ sConfig_output_config .BurstModeEntryDelayed = HRTIM_OUTPUTBURSTMODEENTRY_REGULAR;
98
+ sConfig_output_config .ResetSource = HRTIM_OUTPUTRESET_TIMPER;
99
+ sConfig_output_config .SetSource = HRTIM_OUTPUTSET_TIMCMP2;
100
+
101
+ HAL_HRTIM_WaveformOutputConfig (&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_OUTPUT_TE2, &sConfig_output_config );
102
+
103
+ // Start PWM signals generation
104
+ if (HAL_HRTIM_WaveformOutputStart (&HrtimHandle, HRTIM_OUTPUT_TE2) != HAL_OK)
105
+ {
106
+ // PWM Generation Error
107
+ }
108
+
109
+ // Start HRTIM counter
110
+ if (HAL_HRTIM_WaveformCounterStart (&HrtimHandle, HRTIM_TIMERID_TIMER_E) != HAL_OK)
111
+ {
112
+ // PWM Generation Error
113
+ }
114
+ #else
115
+ /*##-6- Configure the HRTIM TIME PWM channels 1 #############################################*/
116
+ sConfig_time_base.Mode = HRTIM_MODE_CONTINUOUS;
117
+ sConfig_time_base.Period = 100;
118
+ sConfig_time_base.PrescalerRatio = HRTIM_PRESCALERRATIO_DIV4;
119
+ sConfig_time_base.RepetitionCounter = 0;
120
+
121
+ HAL_HRTIM_TimeBaseConfig(&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, &sConfig_time_base);
122
+
123
+ sConfig_Channel.Polarity = HRTIM_OUTPUTPOLARITY_LOW;
124
+ sConfig_Channel.IdleLevel = HRTIM_OUTPUTIDLELEVEL_INACTIVE;
125
+ sConfig_Channel.Pulse = 63;
126
+
127
+ HAL_HRTIM_SimplePWMChannelConfig(&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_OUTPUT_TE1, &sConfig_Channel);
128
+
129
+ HAL_HRTIM_SimplePWMStart(&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_OUTPUT_TE2);
130
+ #endif
131
+ }
132
+
133
+ ~HRTIMPWMClass (){}
134
+
135
+ void period_us (int period) {
136
+ sConfig_time_base .Mode = HRTIM_MODE_CONTINUOUS;
137
+ sConfig_time_base .Period = period * 50 ;
138
+ sConfig_time_base .PrescalerRatio = HRTIM_PRESCALERRATIO_DIV4;
139
+ sConfig_time_base .RepetitionCounter = 0 ;
140
+
141
+ HAL_HRTIM_TimeBaseConfig (&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, &sConfig_time_base );
142
+ }
143
+
144
+ bool write (float pulse) {
145
+ if (pulse > 100 ) {
146
+ pulse = 100 ;
147
+ }
148
+ Serial.println (pulse);
149
+
150
+ sConfig_compare .CompareValue = (sConfig_time_base .Period * pulse);
151
+ if (HAL_HRTIM_WaveformCompareConfig (&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_COMPAREUNIT_2, &sConfig_compare ) != HAL_OK)
152
+ {
153
+ return false ;
154
+ }
155
+ return true ;
156
+ }
157
+
158
+ bool stop () {
159
+ if (HAL_HRTIM_SimplePWMStop (&HrtimHandle, HRTIM_TIMERINDEX_TIMER_E, HRTIM_OUTPUT_TE2) != HAL_OK)
160
+ {
161
+ return false ;
162
+ }
163
+ return true ;
164
+ }
165
+
166
+ private:
167
+ HRTIM_HandleTypeDef HrtimHandle;
168
+
169
+ HRTIM_TimeBaseCfgTypeDef sConfig_time_base ;
170
+ HRTIM_TimerCfgTypeDef sConfig_timerE ;
171
+ HRTIM_OutputCfgTypeDef sConfig_output_config ;
172
+ HRTIM_CompareCfgTypeDef sConfig_compare ;
173
+ HRTIM_SimplePWMChannelCfgTypeDef sConfig_Channel ;
174
+ };
175
+
176
+
177
+ void analogWrite (pin_size_t pin, int value);
178
+
179
+ }
0 commit comments